r23041: Remainder of fix for 4630: fix special case of unix_to_nt_time() for
[Samba/nascimento.git] / source3 / rpc_parse / parse_sec.c
blob72accad1b6cd0de39f5f7f106b1e1927460bb60b
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;
56 uint8 type;
58 if (psa == NULL)
59 return False;
61 prs_debug(ps, depth, desc, "sec_io_ace");
62 depth++;
64 old_offset = prs_offset(ps);
66 if (MARSHALLING(ps)) {
67 type = (uint8)psa->type;
70 if(!prs_uint8("type ", ps, depth, &type))
71 return False;
73 if (UNMARSHALLING(ps)) {
74 psa->type = (enum security_ace_type)type;
77 if(!prs_uint8("flags", ps, depth, &psa->flags))
78 return False;
80 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
81 return False;
83 if(!prs_uint32("access_mask", ps, depth, &psa->access_mask))
84 return False;
86 /* check whether object access is present */
87 if (!sec_ace_object(psa->type)) {
88 if (!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
89 return False;
90 } else {
91 if (!prs_uint32("obj_flags", ps, depth, &psa->object.object.flags))
92 return False;
94 if (psa->object.object.flags & SEC_ACE_OBJECT_PRESENT)
95 if (!smb_io_uuid("obj_guid", &psa->object.object.type.type, ps,depth))
96 return False;
98 if (psa->object.object.flags & SEC_ACE_OBJECT_INHERITED_PRESENT)
99 if (!smb_io_uuid("inh_guid", &psa->object.object.inherited_type.inherited_type, ps,depth))
100 return False;
102 if(!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
103 return False;
106 /* Theorectically an ACE can have a size greater than the
107 sum of its components. When marshalling, pad with extra null bytes up to the
108 correct size. */
110 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
111 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
112 uint32 i;
113 uint8 c = 0;
115 for (i = 0; i < extra_len; i++) {
116 if (!prs_uint8("ace extra space", ps, depth, &c))
117 return False;
121 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
122 return False;
124 return True;
127 /*******************************************************************
128 Reads or writes a SEC_ACL structure.
130 First of the xx_io_xx functions that allocates its data structures
131 for you as it reads them.
132 ********************************************************************/
134 BOOL sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
136 unsigned int i;
137 uint32 old_offset;
138 uint32 offset_acl_size;
139 SEC_ACL *psa;
140 uint16 revision;
143 * Note that the size is always a multiple of 4 bytes due to the
144 * nature of the data structure. Therefore the prs_align() calls
145 * have been removed as they through us off when doing two-layer
146 * marshalling such as in the printing code (RPC_BUFFER). --jerry
149 if (ppsa == NULL)
150 return False;
152 psa = *ppsa;
154 if(UNMARSHALLING(ps) && psa == NULL) {
156 * This is a read and we must allocate the stuct to read into.
158 if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
159 return False;
160 *ppsa = psa;
163 prs_debug(ps, depth, desc, "sec_io_acl");
164 depth++;
166 old_offset = prs_offset(ps);
168 if (MARSHALLING(ps)) {
169 revision = (uint16)psa->revision;
172 if(!prs_uint16("revision", ps, depth, &revision))
173 return False;
175 if (UNMARSHALLING(ps)) {
176 psa->revision = (enum security_acl_revision)revision;
179 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_acl_size))
180 return False;
182 if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
183 return False;
185 if (UNMARSHALLING(ps)) {
186 if (psa->num_aces) {
187 if((psa->aces = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces)) == NULL)
188 return False;
189 } else {
190 psa->aces = NULL;
194 for (i = 0; i < psa->num_aces; i++) {
195 fstring tmp;
196 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
197 if(!sec_io_ace(tmp, &psa->aces[i], ps, depth))
198 return False;
201 /* Theorectically an ACL can have a size greater than the
202 sum of its components. When marshalling, pad with extra null bytes up to the
203 correct size. */
205 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
206 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
207 uint8 c = 0;
209 for (i = 0; i < extra_len; i++) {
210 if (!prs_uint8("acl extra space", ps, depth, &c))
211 return False;
215 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_acl_size, old_offset))
216 return False;
218 return True;
221 /*******************************************************************
222 Reads or writes a SEC_DESC structure.
223 If reading and the *ppsd = NULL, allocates the structure.
224 ********************************************************************/
226 BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
228 uint32 old_offset;
229 uint32 max_offset = 0; /* after we're done, move offset to end */
230 uint32 tmp_offset = 0;
231 uint32 off_sacl, off_dacl, off_owner_sid, off_grp_sid;
232 uint16 revision;
234 SEC_DESC *psd;
236 if (ppsd == NULL)
237 return False;
239 psd = *ppsd;
241 if (psd == NULL) {
242 if(UNMARSHALLING(ps)) {
243 if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
244 return False;
245 *ppsd = psd;
246 } else {
247 /* Marshalling - just ignore. */
248 return True;
252 prs_debug(ps, depth, desc, "sec_io_desc");
253 depth++;
255 /* start of security descriptor stored for back-calc offset purposes */
256 old_offset = prs_offset(ps);
258 if (MARSHALLING(ps)) {
259 revision = (uint16)psd->revision;
262 if(!prs_uint16("revision", ps, depth, &revision))
263 return False;
265 if (UNMARSHALLING(ps)) {
266 psd->revision = (enum security_acl_revision)revision;
269 if(!prs_uint16("type ", ps, depth, &psd->type))
270 return False;
272 if (MARSHALLING(ps)) {
273 uint32 offset = SEC_DESC_HEADER_SIZE;
276 * Work out the offsets here, as we write it out.
279 if (psd->sacl != NULL) {
280 off_sacl = offset;
281 offset += psd->sacl->size;
282 } else {
283 off_sacl = 0;
286 if (psd->dacl != NULL) {
287 off_dacl = offset;
288 offset += psd->dacl->size;
289 } else {
290 off_dacl = 0;
293 if (psd->owner_sid != NULL) {
294 off_owner_sid = offset;
295 offset += sid_size(psd->owner_sid);
296 } else {
297 off_owner_sid = 0;
300 if (psd->group_sid != NULL) {
301 off_grp_sid = offset;
302 offset += sid_size(psd->group_sid);
303 } else {
304 off_grp_sid = 0;
308 if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
309 return False;
311 if(!prs_uint32("off_grp_sid ", ps, depth, &off_grp_sid))
312 return False;
314 if(!prs_uint32("off_sacl ", ps, depth, &off_sacl))
315 return False;
317 if(!prs_uint32("off_dacl ", ps, depth, &off_dacl))
318 return False;
320 max_offset = MAX(max_offset, prs_offset(ps));
322 if (off_owner_sid != 0) {
324 tmp_offset = prs_offset(ps);
325 if(!prs_set_offset(ps, old_offset + off_owner_sid))
326 return False;
328 if (UNMARSHALLING(ps)) {
329 /* reading */
330 if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
331 return False;
334 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
335 return False;
337 max_offset = MAX(max_offset, prs_offset(ps));
339 if (!prs_set_offset(ps,tmp_offset))
340 return False;
343 if (psd->group_sid != 0) {
345 tmp_offset = prs_offset(ps);
346 if(!prs_set_offset(ps, old_offset + off_grp_sid))
347 return False;
349 if (UNMARSHALLING(ps)) {
350 /* reading */
351 if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
352 return False;
355 if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
356 return False;
358 max_offset = MAX(max_offset, prs_offset(ps));
360 if (!prs_set_offset(ps,tmp_offset))
361 return False;
364 if ((psd->type & SEC_DESC_SACL_PRESENT) && off_sacl) {
365 tmp_offset = prs_offset(ps);
366 if(!prs_set_offset(ps, old_offset + off_sacl))
367 return False;
368 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
369 return False;
370 max_offset = MAX(max_offset, prs_offset(ps));
371 if (!prs_set_offset(ps,tmp_offset))
372 return False;
375 if ((psd->type & SEC_DESC_DACL_PRESENT) && off_dacl != 0) {
376 tmp_offset = prs_offset(ps);
377 if(!prs_set_offset(ps, old_offset + off_dacl))
378 return False;
379 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
380 return False;
381 max_offset = MAX(max_offset, prs_offset(ps));
382 if (!prs_set_offset(ps,tmp_offset))
383 return False;
386 if(!prs_set_offset(ps, max_offset))
387 return False;
389 return True;
392 /*******************************************************************
393 Reads or writes a SEC_DESC_BUF structure.
394 ********************************************************************/
396 BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
398 uint32 off_len;
399 uint32 off_max_len;
400 uint32 old_offset;
401 uint32 size;
402 uint32 len;
403 SEC_DESC_BUF *psdb;
404 uint32 ptr;
406 if (ppsdb == NULL)
407 return False;
409 psdb = *ppsdb;
411 if (UNMARSHALLING(ps) && psdb == NULL) {
412 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
413 return False;
414 *ppsdb = psdb;
417 prs_debug(ps, depth, desc, "sec_io_desc_buf");
418 depth++;
420 if(!prs_align(ps))
421 return False;
423 if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
424 return False;
426 ptr = 1;
427 if(!prs_uint32 ("ptr ", ps, depth, &ptr))
428 return False;
430 len = sec_desc_size(psdb->sd);
431 if(!prs_uint32_pre("len ", ps, depth, &len, &off_len))
432 return False;
434 old_offset = prs_offset(ps);
436 /* reading, length is non-zero; writing, descriptor is non-NULL */
437 if ((UNMARSHALLING(ps) && psdb->sd_size != 0) || (MARSHALLING(ps) && psdb->sd != NULL)) {
438 if(!sec_io_desc("sec ", &psdb->sd, ps, depth))
439 return False;
442 if(!prs_align(ps))
443 return False;
445 size = prs_offset(ps) - old_offset;
446 if(!prs_uint32_post("max_len", ps, depth, &psdb->sd_size, off_max_len, size == 0 ? psdb->sd_size : size))
447 return False;
449 if(!prs_uint32_post("len ", ps, depth, &len, off_len, size))
450 return False;
452 return True;