r18745: Use the Samba4 data structures for security descriptors and security descriptor
[Samba.git] / source3 / rpc_parse / parse_sec.c
blob32a8a8cd3291026994a957e0edddadced2f550ae
1 /*
2 * Unix SMB/Netbios implementation.
3 * Version 1.9.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1998,
6 * Copyright (C) Jeremy R. Allison 1995-2005.
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
8 * Copyright (C) Paul Ashton 1997-1998.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
30 /*******************************************************************
31 Reads or writes a SEC_ACCESS structure.
32 ********************************************************************/
34 BOOL sec_io_access(const char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
36 if (t == NULL)
37 return False;
39 prs_debug(ps, depth, desc, "sec_io_access");
40 depth++;
42 if(!prs_uint32("mask", ps, depth, t))
43 return False;
45 return True;
48 /*******************************************************************
49 Reads or writes a SEC_ACE structure.
50 ********************************************************************/
52 BOOL sec_io_ace(const char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
54 uint32 old_offset;
55 uint32 offset_ace_size;
57 if (psa == NULL)
58 return False;
60 prs_debug(ps, depth, desc, "sec_io_ace");
61 depth++;
63 old_offset = prs_offset(ps);
65 if(!prs_uint8("type ", ps, depth, (uint8*)&psa->type))
66 return False;
68 if(!prs_uint8("flags", ps, depth, &psa->flags))
69 return False;
71 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
72 return False;
74 if(!prs_uint32("access_mask", ps, depth, &psa->access_mask))
75 return False;
77 /* check whether object access is present */
78 if (!sec_ace_object(psa->type)) {
79 if (!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
80 return False;
81 } else {
82 if (!prs_uint32("obj_flags", ps, depth, &psa->object.object.flags))
83 return False;
85 if (psa->object.object.flags & SEC_ACE_OBJECT_PRESENT)
86 if (!smb_io_uuid("obj_guid", &psa->object.object.type.type, ps,depth))
87 return False;
89 if (psa->object.object.flags & SEC_ACE_OBJECT_INHERITED_PRESENT)
90 if (!smb_io_uuid("inh_guid", &psa->object.object.inherited_type.inherited_type, ps,depth))
91 return False;
93 if(!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
94 return False;
97 /* Theorectically an ACE can have a size greater than the
98 sum of its components. When marshalling, pad with extra null bytes up to the
99 correct size. */
101 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
102 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
103 uint32 i;
104 uint8 c = 0;
106 for (i = 0; i < extra_len; i++) {
107 if (!prs_uint8("ace extra space", ps, depth, &c))
108 return False;
112 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
113 return False;
115 return True;
118 /*******************************************************************
119 Reads or writes a SEC_ACL structure.
121 First of the xx_io_xx functions that allocates its data structures
122 for you as it reads them.
123 ********************************************************************/
125 BOOL sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
127 unsigned int i;
128 uint32 old_offset;
129 uint32 offset_acl_size;
130 SEC_ACL *psa;
133 * Note that the size is always a multiple of 4 bytes due to the
134 * nature of the data structure. Therefore the prs_align() calls
135 * have been removed as they through us off when doing two-layer
136 * marshalling such as in the printing code (RPC_BUFFER). --jerry
139 if (ppsa == NULL)
140 return False;
142 psa = *ppsa;
144 if(UNMARSHALLING(ps) && psa == NULL) {
146 * This is a read and we must allocate the stuct to read into.
148 if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
149 return False;
150 *ppsa = psa;
153 prs_debug(ps, depth, desc, "sec_io_acl");
154 depth++;
156 old_offset = prs_offset(ps);
158 if(!prs_uint16("revision", ps, depth, (uint16 *)&psa->revision))
159 return False;
161 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_acl_size))
162 return False;
164 if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
165 return False;
167 if (UNMARSHALLING(ps)) {
169 * Even if the num_aces is zero, allocate memory as there's a difference
170 * between a non-present DACL (allow all access) and a DACL with no ACE's
171 * (allow no access).
173 if((psa->aces = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces+1)) == NULL)
174 return False;
177 for (i = 0; i < psa->num_aces; i++) {
178 fstring tmp;
179 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
180 if(!sec_io_ace(tmp, &psa->aces[i], ps, depth))
181 return False;
184 /* Theorectically an ACL can have a size greater than the
185 sum of its components. When marshalling, pad with extra null bytes up to the
186 correct size. */
188 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
189 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
190 uint8 c = 0;
192 for (i = 0; i < extra_len; i++) {
193 if (!prs_uint8("acl extra space", ps, depth, &c))
194 return False;
198 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_acl_size, old_offset))
199 return False;
201 return True;
204 /*******************************************************************
205 Reads or writes a SEC_DESC structure.
206 If reading and the *ppsd = NULL, allocates the structure.
207 ********************************************************************/
209 BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
211 uint32 old_offset;
212 uint32 max_offset = 0; /* after we're done, move offset to end */
213 uint32 tmp_offset = 0;
214 uint32 off_sacl, off_dacl, off_owner_sid, off_grp_sid;
216 SEC_DESC *psd;
218 if (ppsd == NULL)
219 return False;
221 psd = *ppsd;
223 if (psd == NULL) {
224 if(UNMARSHALLING(ps)) {
225 if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
226 return False;
227 *ppsd = psd;
228 } else {
229 /* Marshalling - just ignore. */
230 return True;
234 prs_debug(ps, depth, desc, "sec_io_desc");
235 depth++;
237 /* start of security descriptor stored for back-calc offset purposes */
238 old_offset = prs_offset(ps);
240 if(!prs_uint16("revision ", ps, depth, (uint16*)&psd->revision))
241 return False;
243 if(!prs_uint16("type ", ps, depth, &psd->type))
244 return False;
246 if (MARSHALLING(ps)) {
247 uint32 offset = SEC_DESC_HEADER_SIZE;
250 * Work out the offsets here, as we write it out.
253 if (psd->sacl != NULL) {
254 off_sacl = offset;
255 offset += psd->sacl->size;
256 } else {
257 off_sacl = 0;
260 if (psd->dacl != NULL) {
261 off_dacl = offset;
262 offset += psd->dacl->size;
263 } else {
264 off_dacl = 0;
267 if (psd->owner_sid != NULL) {
268 off_owner_sid = offset;
269 offset += sid_size(psd->owner_sid);
270 } else {
271 off_owner_sid = 0;
274 if (psd->group_sid != NULL) {
275 off_grp_sid = offset;
276 offset += sid_size(psd->group_sid);
277 } else {
278 off_grp_sid = 0;
282 if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
283 return False;
285 if(!prs_uint32("off_grp_sid ", ps, depth, &off_grp_sid))
286 return False;
288 if(!prs_uint32("off_sacl ", ps, depth, &off_sacl))
289 return False;
291 if(!prs_uint32("off_dacl ", ps, depth, &off_dacl))
292 return False;
294 max_offset = MAX(max_offset, prs_offset(ps));
296 if (off_owner_sid != 0) {
298 tmp_offset = prs_offset(ps);
299 if(!prs_set_offset(ps, old_offset + off_owner_sid))
300 return False;
302 if (UNMARSHALLING(ps)) {
303 /* reading */
304 if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
305 return False;
308 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
309 return False;
311 max_offset = MAX(max_offset, prs_offset(ps));
313 if (!prs_set_offset(ps,tmp_offset))
314 return False;
317 if (psd->group_sid != 0) {
319 tmp_offset = prs_offset(ps);
320 if(!prs_set_offset(ps, old_offset + off_grp_sid))
321 return False;
323 if (UNMARSHALLING(ps)) {
324 /* reading */
325 if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
326 return False;
329 if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
330 return False;
332 max_offset = MAX(max_offset, prs_offset(ps));
334 if (!prs_set_offset(ps,tmp_offset))
335 return False;
338 if ((psd->type & SEC_DESC_SACL_PRESENT) && off_sacl) {
339 tmp_offset = prs_offset(ps);
340 if(!prs_set_offset(ps, old_offset + off_sacl))
341 return False;
342 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
343 return False;
344 max_offset = MAX(max_offset, prs_offset(ps));
345 if (!prs_set_offset(ps,tmp_offset))
346 return False;
349 if ((psd->type & SEC_DESC_DACL_PRESENT) && off_dacl != 0) {
350 tmp_offset = prs_offset(ps);
351 if(!prs_set_offset(ps, old_offset + off_dacl))
352 return False;
353 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
354 return False;
355 max_offset = MAX(max_offset, prs_offset(ps));
356 if (!prs_set_offset(ps,tmp_offset))
357 return False;
360 if(!prs_set_offset(ps, max_offset))
361 return False;
363 return True;
366 /*******************************************************************
367 Reads or writes a SEC_DESC_BUF structure.
368 ********************************************************************/
370 BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
372 uint32 off_len;
373 uint32 off_max_len;
374 uint32 old_offset;
375 uint32 size;
376 uint32 len;
377 SEC_DESC_BUF *psdb;
378 uint32 ptr;
380 if (ppsdb == NULL)
381 return False;
383 psdb = *ppsdb;
385 if (UNMARSHALLING(ps) && psdb == NULL) {
386 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
387 return False;
388 *ppsdb = psdb;
391 prs_debug(ps, depth, desc, "sec_io_desc_buf");
392 depth++;
394 if(!prs_align(ps))
395 return False;
397 if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
398 return False;
400 ptr = 1;
401 if(!prs_uint32 ("ptr ", ps, depth, &ptr))
402 return False;
404 len = sec_desc_size(psdb->sd);
405 if(!prs_uint32_pre("len ", ps, depth, &len, &off_len))
406 return False;
408 old_offset = prs_offset(ps);
410 /* reading, length is non-zero; writing, descriptor is non-NULL */
411 if ((UNMARSHALLING(ps) && psdb->sd_size != 0) || (MARSHALLING(ps) && psdb->sd != NULL)) {
412 if(!sec_io_desc("sec ", &psdb->sd, ps, depth))
413 return False;
416 if(!prs_align(ps))
417 return False;
419 size = prs_offset(ps) - old_offset;
420 if(!prs_uint32_post("max_len", ps, depth, &psdb->sd_size, off_max_len, size == 0 ? psdb->sd_size : size))
421 return False;
423 if(!prs_uint32_post("len ", ps, depth, &len, off_len, size))
424 return False;
426 return True;