2 Unix SMB/CIFS implementation.
4 fast routines for getting the wire size of security objects
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan Metzmacher 2006-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "../libcli/security/security.h"
30 * Find the wire size of a security_ace that has no trailing coda.
31 * This is used in ndr_pull_security_ace() generated from security.idl
32 * to work out where the coda starts (and in ndr_size_security_ace()
35 static size_t ndr_size_security_ace_core(const struct security_ace *ace, libndr_flags flags)
41 ret = 8 + ndr_size_dom_sid(&ace->trustee, flags);
42 if (sec_ace_object(ace->type)) {
43 ret += 4; /* uint32 bitmap ace->object.object.flags */
44 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
45 ret += 16; /* GUID ace->object.object.type.type */
47 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
48 ret += 16; /* GUID ace->object.object.inherited_type.inherited_type */
56 return the wire size of a security_ace
58 size_t ndr_size_security_ace(const struct security_ace *ace, libndr_flags flags)
60 size_t base = ndr_size_security_ace_core(ace, flags);
62 if (sec_ace_callback(ace->type)) {
63 ret += ace->coda.conditions.length;
64 } else if (ace->type == SEC_ACE_TYPE_SYSTEM_RESOURCE_ATTRIBUTE) {
65 ret += ndr_size_security_ace_coda(&ace->coda, ace->type, flags);
68 * Normal ACEs have a coda.ignored blob that is always or
69 * almost always empty. We aren't going to push it (it is
70 * ignored), so we don't add that length to the size.
73 /* round up to a multiple of 4 (MS-DTYP 2.4.4.1) */
74 ret = (ret + 3ULL) & ~3ULL;
75 if (unlikely(ret < base)) {
76 /* overflow, and there's not much we can do anyway */
83 static inline enum ndr_err_code ndr_maybe_pull_security_ace_object_ctr(struct ndr_pull *ndr,
84 ndr_flags_type ndr_flags,
85 struct security_ace *r)
88 * If this is not an object ACE (as is usually common),
89 * ndr_pull_security_ace_object_ctr() will do nothing.
91 * By avoiding calling the function in that case, we avoid some
92 * tallocing and ndr token busywork.
94 bool is_object = sec_ace_object(r->type);
96 NDR_CHECK(ndr_pull_set_switch_value(ndr, &r->object, is_object));
97 NDR_CHECK(ndr_pull_security_ace_object_ctr(ndr, ndr_flags, &r->object));
99 return NDR_ERR_SUCCESS;
103 _PUBLIC_ enum ndr_err_code ndr_pull_security_ace(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct security_ace *r)
105 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
106 if (ndr_flags & NDR_SCALARS) {
108 NDR_CHECK(ndr_pull_align(ndr, 5));
109 NDR_CHECK(ndr_pull_security_ace_type(ndr, NDR_SCALARS, &r->type));
110 NDR_CHECK(ndr_pull_security_ace_flags(ndr, NDR_SCALARS, &r->flags));
111 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->size));
112 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->access_mask));
113 NDR_CHECK(ndr_maybe_pull_security_ace_object_ctr(ndr, NDR_SCALARS, r));
114 NDR_CHECK(ndr_pull_dom_sid(ndr, NDR_SCALARS, &r->trustee));
115 sub_size = ndr_subcontext_size_of_ace_coda(r, r->size, ndr->flags);
117 r->coda.ignored.data = NULL;
118 r->coda.ignored.length = 0;
120 struct ndr_pull *_ndr_coda;
121 NDR_CHECK(ndr_pull_subcontext_start(ndr, &_ndr_coda, 0, sub_size));
122 NDR_CHECK(ndr_pull_set_switch_value(_ndr_coda, &r->coda, r->type));
123 NDR_CHECK(ndr_pull_security_ace_coda(_ndr_coda, NDR_SCALARS|NDR_BUFFERS, &r->coda));
124 NDR_CHECK(ndr_pull_subcontext_end(ndr, _ndr_coda, 0, sub_size));
126 NDR_CHECK(ndr_pull_trailer_align(ndr, 5));
128 if (ndr_flags & NDR_BUFFERS) {
129 NDR_CHECK(ndr_maybe_pull_security_ace_object_ctr(ndr, NDR_BUFFERS, r));
131 return NDR_ERR_SUCCESS;
135 static inline enum ndr_err_code ndr_maybe_push_security_ace_object_ctr(struct ndr_push *ndr,
136 ndr_flags_type ndr_flags,
137 const struct security_ace *r)
140 * ndr_push_security_ace_object_ctr() does nothing (except tallocing
141 * and ndr_token fiddling) unless the ACE is an object ACE, which is
142 * usually very unlikely.
144 bool is_object = sec_ace_object(r->type);
146 NDR_CHECK(ndr_push_set_switch_value(ndr, &r->object, is_object));
147 NDR_CHECK(ndr_push_security_ace_object_ctr(ndr, ndr_flags, &r->object));
149 return NDR_ERR_SUCCESS;
152 _PUBLIC_ enum ndr_err_code ndr_push_security_ace(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct security_ace *r)
154 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
155 if (ndr_flags & NDR_SCALARS) {
156 NDR_CHECK(ndr_push_align(ndr, 5));
157 NDR_CHECK(ndr_push_security_ace_type(ndr, NDR_SCALARS, r->type));
158 NDR_CHECK(ndr_push_security_ace_flags(ndr, NDR_SCALARS, r->flags));
159 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, ndr_size_security_ace(r, ndr->flags)));
160 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->access_mask));
161 NDR_CHECK(ndr_maybe_push_security_ace_object_ctr(ndr, NDR_SCALARS, r));
162 NDR_CHECK(ndr_push_dom_sid(ndr, NDR_SCALARS, &r->trustee));
163 if (sec_ace_has_extra_blob(r->type)) {
164 struct ndr_push *_ndr_coda;
165 size_t coda_size = ndr_subcontext_size_of_ace_coda(
167 ndr_size_security_ace(r, ndr->flags),
169 NDR_CHECK(ndr_push_subcontext_start(ndr, &_ndr_coda, 0, coda_size));
170 NDR_CHECK(ndr_push_set_switch_value(_ndr_coda, &r->coda, r->type));
171 NDR_CHECK(ndr_push_security_ace_coda(_ndr_coda, NDR_SCALARS|NDR_BUFFERS, &r->coda));
172 NDR_CHECK(ndr_push_subcontext_end(ndr, _ndr_coda, 0, coda_size));
174 NDR_CHECK(ndr_push_trailer_align(ndr, 5));
176 if (ndr_flags & NDR_BUFFERS) {
177 NDR_CHECK(ndr_maybe_push_security_ace_object_ctr(ndr, NDR_BUFFERS, r));
179 return NDR_ERR_SUCCESS;
184 * An ACE coda can't be bigger than the space allowed for by
185 * ace->size, so we need to check this from the context of the ACE.
187 * Usually the coda also can't be any smaller than the remaining
188 * space, because it is defined as a blob consuming everything it can.
190 * This is only used to find the size for the coda subcontext in
193 size_t ndr_subcontext_size_of_ace_coda(const struct security_ace *ace,
201 core_size = ndr_size_security_ace_core(ace, flags);
202 if (ace_size < core_size) {
205 return ace_size - core_size;
209 return the wire size of a security_acl
211 size_t ndr_size_security_acl(const struct security_acl *theacl, libndr_flags flags)
215 if (!theacl) return 0;
217 for (i=0;i<theacl->num_aces;i++) {
218 ret += ndr_size_security_ace(&theacl->aces[i], flags);
224 return the wire size of a security descriptor
226 size_t ndr_size_security_descriptor(const struct security_descriptor *sd, libndr_flags flags)
232 ret += ndr_size_dom_sid(sd->owner_sid, flags);
233 ret += ndr_size_dom_sid(sd->group_sid, flags);
234 ret += ndr_size_security_acl(sd->dacl, flags);
235 ret += ndr_size_security_acl(sd->sacl, flags);
240 return the wire size of a dom_sid
242 size_t ndr_size_dom_sid(const struct dom_sid *sid, libndr_flags flags)
245 return 8 + 4*sid->num_auths;
248 size_t ndr_size_dom_sid28(const struct dom_sid *sid, libndr_flags flags)
250 if (all_zero((const uint8_t *)sid, sizeof(struct dom_sid))) {
253 return ndr_size_dom_sid(sid, flags);
256 size_t ndr_size_dom_sid0(const struct dom_sid *sid, libndr_flags flags)
258 return ndr_size_dom_sid28(sid, flags);
264 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
266 struct dom_sid_buf buf;
267 ndr->print(ndr, "%-25s: %s", name, dom_sid_str_buf(sid, &buf));
270 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
272 ndr_print_dom_sid(ndr, name, sid);
275 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
277 ndr_print_dom_sid(ndr, name, sid);
280 void ndr_print_dom_sid0(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
282 ndr_print_dom_sid(ndr, name, sid);
287 parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
289 enum ndr_err_code ndr_pull_dom_sid2(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct dom_sid *sid)
292 if (!(ndr_flags & NDR_SCALARS)) {
293 return NDR_ERR_SUCCESS;
295 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &num_auths));
296 NDR_CHECK(ndr_pull_dom_sid(ndr, ndr_flags, sid));
297 if (sid->num_auths != num_auths) {
298 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
299 "Bad num_auths %"PRIu32"; should equal %"PRId8,
300 num_auths, sid->num_auths);
302 return NDR_ERR_SUCCESS;
306 parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
308 enum ndr_err_code ndr_push_dom_sid2(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct dom_sid *sid)
310 if (!(ndr_flags & NDR_SCALARS)) {
311 return NDR_ERR_SUCCESS;
313 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, sid->num_auths));
314 return ndr_push_dom_sid(ndr, ndr_flags, sid);
318 parse a dom_sid28 - this is a dom_sid in a fixed 28 byte buffer, so we need to ensure there are only up to 5 sub_auth
320 enum ndr_err_code ndr_pull_dom_sid28(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct dom_sid *sid)
322 enum ndr_err_code status;
323 struct ndr_pull *subndr;
325 if (!(ndr_flags & NDR_SCALARS)) {
326 return NDR_ERR_SUCCESS;
329 subndr = talloc_zero(ndr, struct ndr_pull);
330 NDR_ERR_HAVE_NO_MEMORY(subndr);
331 subndr->flags = ndr->flags;
332 subndr->current_mem_ctx = ndr->current_mem_ctx;
334 subndr->data = ndr->data + ndr->offset;
335 subndr->data_size = 28;
338 status = ndr_pull_advance(ndr, 28);
339 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
344 status = ndr_pull_dom_sid(subndr, ndr_flags, sid);
345 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
346 /* handle a w2k bug which send random data in the buffer */
348 } else if (sid->num_auths == 0) {
349 ZERO_STRUCT(sid->sub_auths);
353 return NDR_ERR_SUCCESS;
357 push a dom_sid28 - this is a dom_sid in a 28 byte fixed buffer
359 enum ndr_err_code ndr_push_dom_sid28(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct dom_sid *sid)
364 if (!(ndr_flags & NDR_SCALARS)) {
365 return NDR_ERR_SUCCESS;
368 if (sid->num_auths > 5) {
369 return ndr_push_error(ndr, NDR_ERR_RANGE,
370 "dom_sid28 allows only up to 5 sub auths [%"PRId8"]",
374 old_offset = ndr->offset;
375 NDR_CHECK(ndr_push_dom_sid(ndr, ndr_flags, sid));
377 padding = 28 - (ndr->offset - old_offset);
380 NDR_CHECK(ndr_push_zero(ndr, padding));
383 return NDR_ERR_SUCCESS;
387 parse a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
389 enum ndr_err_code ndr_pull_dom_sid0(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct dom_sid *sid)
391 if (!(ndr_flags & NDR_SCALARS)) {
392 return NDR_ERR_SUCCESS;
395 if (ndr->data_size == ndr->offset) {
397 return NDR_ERR_SUCCESS;
400 return ndr_pull_dom_sid(ndr, ndr_flags, sid);
404 push a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
406 enum ndr_err_code ndr_push_dom_sid0(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct dom_sid *sid)
408 if (!(ndr_flags & NDR_SCALARS)) {
409 return NDR_ERR_SUCCESS;
413 return NDR_ERR_SUCCESS;
416 if (all_zero((const uint8_t *)sid, sizeof(struct dom_sid))) {
417 return NDR_ERR_SUCCESS;
420 return ndr_push_dom_sid(ndr, ndr_flags, sid);
423 _PUBLIC_ enum ndr_err_code ndr_push_dom_sid(struct ndr_push *ndr, ndr_flags_type ndr_flags, const struct dom_sid *r)
425 uint32_t cntr_sub_auths_0;
426 if (ndr_flags & NDR_SCALARS) {
427 NDR_CHECK(ndr_push_align(ndr, 4));
428 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, r->sid_rev_num));
429 NDR_CHECK(ndr_push_int8(ndr, NDR_SCALARS, r->num_auths));
430 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
431 if (r->num_auths < 0 || r->num_auths > ARRAY_SIZE(r->sub_auths)) {
432 return ndr_push_error(ndr, NDR_ERR_RANGE, "value (%"PRId8") out of range (0 - %zu)", r->num_auths, ARRAY_SIZE(r->sub_auths));
434 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
435 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->sub_auths[cntr_sub_auths_0]));
438 return NDR_ERR_SUCCESS;
441 _PUBLIC_ enum ndr_err_code ndr_pull_dom_sid(struct ndr_pull *ndr, ndr_flags_type ndr_flags, struct dom_sid *r)
443 uint32_t cntr_sub_auths_0;
444 if (ndr_flags & NDR_SCALARS) {
445 NDR_CHECK(ndr_pull_align(ndr, 4));
446 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &r->sid_rev_num));
447 NDR_CHECK(ndr_pull_int8(ndr, NDR_SCALARS, &r->num_auths));
448 if (r->num_auths < 0 || r->num_auths > ARRAY_SIZE(r->sub_auths)) {
449 return ndr_pull_error(ndr, NDR_ERR_RANGE, "value (%"PRId8") out of range (0 - %zu)", r->num_auths, ARRAY_SIZE(r->sub_auths));
451 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
452 ZERO_STRUCT(r->sub_auths);
453 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
454 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->sub_auths[cntr_sub_auths_0]));
457 return NDR_ERR_SUCCESS;