r18846: Fix the same bug Volker noticed when marshalling/unmarshalling
[metze/samba/wip.git] / source3 / rpc_parse / parse_sec.c
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.
14  *  
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.
19  *  
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.
23  */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
29
30 /*******************************************************************
31  Reads or writes a SEC_ACCESS structure.
32 ********************************************************************/
33
34 BOOL sec_io_access(const char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
35 {
36         if (t == NULL)
37                 return False;
38
39         prs_debug(ps, depth, desc, "sec_io_access");
40         depth++;
41         
42         if(!prs_uint32("mask", ps, depth, t))
43                 return False;
44
45         return True;
46 }
47
48 /*******************************************************************
49  Reads or writes a SEC_ACE structure.
50 ********************************************************************/
51
52 BOOL sec_io_ace(const char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
53 {
54         uint32 old_offset;
55         uint32 offset_ace_size;
56         uint8 type;
57
58         if (psa == NULL)
59                 return False;
60
61         prs_debug(ps, depth, desc, "sec_io_ace");
62         depth++;
63         
64         old_offset = prs_offset(ps);
65
66         if (MARSHALLING(ps)) {
67                 type = (uint8)psa->type;
68         }
69
70         if(!prs_uint8("type ", ps, depth, &type))
71                 return False;
72
73         if (UNMARSHALLING(ps)) {
74                 psa->type = (enum security_ace_type)type;
75         }
76
77         if(!prs_uint8("flags", ps, depth, &psa->flags))
78                 return False;
79
80         if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
81                 return False;
82
83         if(!prs_uint32("access_mask", ps, depth, &psa->access_mask))
84                 return False;
85
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;
93
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;
97
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;
101
102                 if(!smb_io_dom_sid("trustee  ", &psa->trustee , ps, depth))
103                         return False;
104         }
105
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. */
109
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;
114
115                 for (i = 0; i < extra_len; i++) {
116                         if (!prs_uint8("ace extra space", ps, depth, &c))
117                                 return False;
118                 }
119         }
120
121         if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
122                 return False;
123
124         return True;
125 }
126
127 /*******************************************************************
128  Reads or writes a SEC_ACL structure.  
129
130  First of the xx_io_xx functions that allocates its data structures
131  for you as it reads them.
132 ********************************************************************/
133
134 BOOL sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
135 {
136         unsigned int i;
137         uint32 old_offset;
138         uint32 offset_acl_size;
139         SEC_ACL *psa;
140         uint16 revision;
141
142         /*
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
147          */
148
149         if (ppsa == NULL)
150                 return False;
151
152         psa = *ppsa;
153
154         if(UNMARSHALLING(ps) && psa == NULL) {
155                 /*
156                  * This is a read and we must allocate the stuct to read into.
157                  */
158                 if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
159                         return False;
160                 *ppsa = psa;
161         }
162
163         prs_debug(ps, depth, desc, "sec_io_acl");
164         depth++;
165         
166         old_offset = prs_offset(ps);
167
168         if (MARSHALLING(ps)) {
169                 revision = (uint16)psa->revision;
170         }
171
172         if(!prs_uint16("revision", ps, depth, &revision))
173                 return False;
174
175         if (UNMARSHALLING(ps)) {
176                 psa->revision = (enum security_acl_revision)revision;
177         }
178
179         if(!prs_uint16_pre("size     ", ps, depth, &psa->size, &offset_acl_size))
180                 return False;
181
182         if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
183                 return False;
184
185         if (UNMARSHALLING(ps)) {
186                 /*
187                  * Even if the num_aces is zero, allocate memory as there's a difference
188                  * between a non-present DACL (allow all access) and a DACL with no ACE's
189                  * (allow no access).
190                  */
191                 if((psa->aces = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces+1)) == NULL)
192                         return False;
193         }
194
195         for (i = 0; i < psa->num_aces; i++) {
196                 fstring tmp;
197                 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
198                 if(!sec_io_ace(tmp, &psa->aces[i], ps, depth))
199                         return False;
200         }
201
202         /* Theorectically an ACL can have a size greater than the
203            sum of its components. When marshalling, pad with extra null bytes up to the
204            correct size. */
205
206         if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
207                 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
208                 uint8 c = 0;
209
210                 for (i = 0; i < extra_len; i++) {
211                         if (!prs_uint8("acl extra space", ps, depth, &c))
212                                 return False;
213                 }
214         }
215
216         if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
217                 return False;
218
219         return True;
220 }
221
222 /*******************************************************************
223  Reads or writes a SEC_DESC structure.
224  If reading and the *ppsd = NULL, allocates the structure.
225 ********************************************************************/
226
227 BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
228 {
229         uint32 old_offset;
230         uint32 max_offset = 0; /* after we're done, move offset to end */
231         uint32 tmp_offset = 0;
232         uint32 off_sacl, off_dacl, off_owner_sid, off_grp_sid;
233         uint16 revision;
234
235         SEC_DESC *psd;
236
237         if (ppsd == NULL)
238                 return False;
239
240         psd = *ppsd;
241
242         if (psd == NULL) {
243                 if(UNMARSHALLING(ps)) {
244                         if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
245                                 return False;
246                         *ppsd = psd;
247                 } else {
248                         /* Marshalling - just ignore. */
249                         return True;
250                 }
251         }
252
253         prs_debug(ps, depth, desc, "sec_io_desc");
254         depth++;
255
256         /* start of security descriptor stored for back-calc offset purposes */
257         old_offset = prs_offset(ps);
258
259         if (MARSHALLING(ps)) {
260                 revision = (uint16)psd->revision;
261         }
262
263         if(!prs_uint16("revision", ps, depth, &revision))
264                 return False;
265
266         if (UNMARSHALLING(ps)) {
267                 psd->revision = (enum security_acl_revision)revision;
268         }
269
270         if(!prs_uint16("type     ", ps, depth, &psd->type))
271                 return False;
272
273         if (MARSHALLING(ps)) {
274                 uint32 offset = SEC_DESC_HEADER_SIZE;
275
276                 /*
277                  * Work out the offsets here, as we write it out.
278                  */
279
280                 if (psd->sacl != NULL) {
281                         off_sacl = offset;
282                         offset += psd->sacl->size;
283                 } else {
284                         off_sacl = 0;
285                 }
286
287                 if (psd->dacl != NULL) {
288                         off_dacl = offset;
289                         offset += psd->dacl->size;
290                 } else {
291                         off_dacl = 0;
292                 }
293
294                 if (psd->owner_sid != NULL) {
295                         off_owner_sid = offset;
296                         offset += sid_size(psd->owner_sid);
297                 } else {
298                         off_owner_sid = 0;
299                 }
300
301                 if (psd->group_sid != NULL) {
302                         off_grp_sid = offset;
303                         offset += sid_size(psd->group_sid);
304                 } else {
305                         off_grp_sid = 0;
306                 }
307         }
308
309         if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
310                 return False;
311
312         if(!prs_uint32("off_grp_sid  ", ps, depth, &off_grp_sid))
313                 return False;
314
315         if(!prs_uint32("off_sacl     ", ps, depth, &off_sacl))
316                 return False;
317
318         if(!prs_uint32("off_dacl     ", ps, depth, &off_dacl))
319                 return False;
320
321         max_offset = MAX(max_offset, prs_offset(ps));
322
323         if (off_owner_sid != 0) {
324
325                 tmp_offset = prs_offset(ps);
326                 if(!prs_set_offset(ps, old_offset + off_owner_sid))
327                         return False;
328
329                 if (UNMARSHALLING(ps)) {
330                         /* reading */
331                         if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
332                                 return False;
333                 }
334
335                 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
336                         return False;
337
338                 max_offset = MAX(max_offset, prs_offset(ps));
339
340                 if (!prs_set_offset(ps,tmp_offset))
341                         return False;
342         }
343
344         if (psd->group_sid != 0) {
345
346                 tmp_offset = prs_offset(ps);
347                 if(!prs_set_offset(ps, old_offset + off_grp_sid))
348                         return False;
349
350                 if (UNMARSHALLING(ps)) {
351                         /* reading */
352                         if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
353                                 return False;
354                 }
355
356                 if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
357                         return False;
358                         
359                 max_offset = MAX(max_offset, prs_offset(ps));
360
361                 if (!prs_set_offset(ps,tmp_offset))
362                         return False;
363         }
364
365         if ((psd->type & SEC_DESC_SACL_PRESENT) && off_sacl) {
366                 tmp_offset = prs_offset(ps);
367                 if(!prs_set_offset(ps, old_offset + off_sacl))
368                         return False;
369                 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
370                         return False;
371                 max_offset = MAX(max_offset, prs_offset(ps));
372                 if (!prs_set_offset(ps,tmp_offset))
373                         return False;
374         }
375
376         if ((psd->type & SEC_DESC_DACL_PRESENT) && off_dacl != 0) {
377                 tmp_offset = prs_offset(ps);
378                 if(!prs_set_offset(ps, old_offset + off_dacl))
379                         return False;
380                 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
381                         return False;
382                 max_offset = MAX(max_offset, prs_offset(ps));
383                 if (!prs_set_offset(ps,tmp_offset))
384                         return False;
385         }
386
387         if(!prs_set_offset(ps, max_offset))
388                 return False;
389
390         return True;
391 }
392
393 /*******************************************************************
394  Reads or writes a SEC_DESC_BUF structure.
395 ********************************************************************/
396
397 BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
398 {
399         uint32 off_len;
400         uint32 off_max_len;
401         uint32 old_offset;
402         uint32 size;
403         uint32 len;
404         SEC_DESC_BUF *psdb;
405         uint32 ptr;
406
407         if (ppsdb == NULL)
408                 return False;
409
410         psdb = *ppsdb;
411
412         if (UNMARSHALLING(ps) && psdb == NULL) {
413                 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
414                         return False;
415                 *ppsdb = psdb;
416         }
417
418         prs_debug(ps, depth, desc, "sec_io_desc_buf");
419         depth++;
420
421         if(!prs_align(ps))
422                 return False;
423         
424         if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
425                 return False;
426
427         ptr = 1;
428         if(!prs_uint32    ("ptr  ", ps, depth, &ptr))
429                 return False;
430
431         len = sec_desc_size(psdb->sd);
432         if(!prs_uint32_pre("len    ", ps, depth, &len, &off_len))
433                 return False;
434
435         old_offset = prs_offset(ps);
436
437         /* reading, length is non-zero; writing, descriptor is non-NULL */
438         if ((UNMARSHALLING(ps) && psdb->sd_size != 0) || (MARSHALLING(ps) && psdb->sd != NULL)) {
439                 if(!sec_io_desc("sec   ", &psdb->sd, ps, depth))
440                         return False;
441         }
442
443         if(!prs_align(ps))
444                 return False;
445         
446         size = prs_offset(ps) - old_offset;
447         if(!prs_uint32_post("max_len", ps, depth, &psdb->sd_size, off_max_len, size == 0 ? psdb->sd_size : size))
448                 return False;
449
450         if(!prs_uint32_post("len    ", ps, depth, &len, off_len, size))
451                 return False;
452
453         return True;
454 }