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