r23779: Change from v2 or later to v3 or later.
[gd/samba-autobuild/.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 3 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                 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;
191                 }
192         }
193
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;
199         }
200
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. */
204
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;
208
209                 for (i = 0; i < extra_len; i++) {
210                         if (!prs_uint8("acl extra space", ps, depth, &c))
211                                 return False;
212                 }
213         }
214
215         if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
216                 return False;
217
218         return True;
219 }
220
221 /*******************************************************************
222  Reads or writes a SEC_DESC structure.
223  If reading and the *ppsd = NULL, allocates the structure.
224 ********************************************************************/
225
226 BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
227 {
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;
233
234         SEC_DESC *psd;
235
236         if (ppsd == NULL)
237                 return False;
238
239         psd = *ppsd;
240
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;
249                 }
250         }
251
252         prs_debug(ps, depth, desc, "sec_io_desc");
253         depth++;
254
255         /* start of security descriptor stored for back-calc offset purposes */
256         old_offset = prs_offset(ps);
257
258         if (MARSHALLING(ps)) {
259                 revision = (uint16)psd->revision;
260         }
261
262         if(!prs_uint16("revision", ps, depth, &revision))
263                 return False;
264
265         if (UNMARSHALLING(ps)) {
266                 psd->revision = (enum security_acl_revision)revision;
267         }
268
269         if(!prs_uint16("type     ", ps, depth, &psd->type))
270                 return False;
271
272         if (MARSHALLING(ps)) {
273                 uint32 offset = SEC_DESC_HEADER_SIZE;
274
275                 /*
276                  * Work out the offsets here, as we write it out.
277                  */
278
279                 if (psd->sacl != NULL) {
280                         off_sacl = offset;
281                         offset += psd->sacl->size;
282                 } else {
283                         off_sacl = 0;
284                 }
285
286                 if (psd->dacl != NULL) {
287                         off_dacl = offset;
288                         offset += psd->dacl->size;
289                 } else {
290                         off_dacl = 0;
291                 }
292
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;
298                 }
299
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;
305                 }
306         }
307
308         if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
309                 return False;
310
311         if(!prs_uint32("off_grp_sid  ", ps, depth, &off_grp_sid))
312                 return False;
313
314         if(!prs_uint32("off_sacl     ", ps, depth, &off_sacl))
315                 return False;
316
317         if(!prs_uint32("off_dacl     ", ps, depth, &off_dacl))
318                 return False;
319
320         max_offset = MAX(max_offset, prs_offset(ps));
321
322         if (off_owner_sid != 0) {
323
324                 tmp_offset = prs_offset(ps);
325                 if(!prs_set_offset(ps, old_offset + off_owner_sid))
326                         return False;
327
328                 if (UNMARSHALLING(ps)) {
329                         /* reading */
330                         if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
331                                 return False;
332                 }
333
334                 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
335                         return False;
336
337                 max_offset = MAX(max_offset, prs_offset(ps));
338
339                 if (!prs_set_offset(ps,tmp_offset))
340                         return False;
341         }
342
343         if (psd->group_sid != 0) {
344
345                 tmp_offset = prs_offset(ps);
346                 if(!prs_set_offset(ps, old_offset + off_grp_sid))
347                         return False;
348
349                 if (UNMARSHALLING(ps)) {
350                         /* reading */
351                         if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
352                                 return False;
353                 }
354
355                 if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
356                         return False;
357                         
358                 max_offset = MAX(max_offset, prs_offset(ps));
359
360                 if (!prs_set_offset(ps,tmp_offset))
361                         return False;
362         }
363
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;
373         }
374
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;
384         }
385
386         if(!prs_set_offset(ps, max_offset))
387                 return False;
388
389         return True;
390 }
391
392 /*******************************************************************
393  Reads or writes a SEC_DESC_BUF structure.
394 ********************************************************************/
395
396 BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
397 {
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;
405
406         if (ppsdb == NULL)
407                 return False;
408
409         psdb = *ppsdb;
410
411         if (UNMARSHALLING(ps) && psdb == NULL) {
412                 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
413                         return False;
414                 *ppsdb = psdb;
415         }
416
417         prs_debug(ps, depth, desc, "sec_io_desc_buf");
418         depth++;
419
420         if(!prs_align(ps))
421                 return False;
422         
423         if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
424                 return False;
425
426         ptr = 1;
427         if(!prs_uint32    ("ptr  ", ps, depth, &ptr))
428                 return False;
429
430         len = sec_desc_size(psdb->sd);
431         if(!prs_uint32_pre("len    ", ps, depth, &len, &off_len))
432                 return False;
433
434         old_offset = prs_offset(ps);
435
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;
440         }
441
442         if(!prs_align(ps))
443                 return False;
444         
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;
448
449         if(!prs_uint32_post("len    ", ps, depth, &len, off_len, size))
450                 return False;
451
452         return True;
453 }