Initial import
[samba] / source / 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->mask))
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
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(!prs_uint8("type ", ps, depth, &psa->type))
66                 return False;
67
68         if(!prs_uint8("flags", ps, depth, &psa->flags))
69                 return False;
70
71         if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
72                 return False;
73
74         if(!sec_io_access("info ", &psa->info, ps, depth))
75                 return False;
76
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->obj_flags))
83                         return False;
84
85                 if (psa->obj_flags & SEC_ACE_OBJECT_PRESENT)
86                         if (!smb_io_uuid("obj_guid", &psa->obj_guid, ps,depth))
87                                 return False;
88
89                 if (psa->obj_flags & SEC_ACE_OBJECT_INHERITED_PRESENT)
90                         if (!smb_io_uuid("inh_guid", &psa->inh_guid, ps,depth))
91                                 return False;
92
93                 if(!smb_io_dom_sid("trustee  ", &psa->trustee , ps, depth))
94                         return False;
95         }
96
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. */
100
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;
105
106                 for (i = 0; i < extra_len; i++) {
107                         if (!prs_uint8("ace extra space", ps, depth, &c))
108                                 return False;
109                 }
110         }
111
112         if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
113                 return False;
114
115         return True;
116 }
117
118 /*******************************************************************
119  Reads or writes a SEC_ACL structure.  
120
121  First of the xx_io_xx functions that allocates its data structures
122  for you as it reads them.
123 ********************************************************************/
124
125 BOOL sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
126 {
127         unsigned int i;
128         uint32 old_offset;
129         uint32 offset_acl_size;
130         SEC_ACL *psa;
131
132         /*
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
137          */
138
139         if (ppsa == NULL)
140                 return False;
141
142         psa = *ppsa;
143
144         if(UNMARSHALLING(ps) && psa == NULL) {
145                 /*
146                  * This is a read and we must allocate the stuct to read into.
147                  */
148                 if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
149                         return False;
150                 *ppsa = psa;
151         }
152
153         prs_debug(ps, depth, desc, "sec_io_acl");
154         depth++;
155         
156         old_offset = prs_offset(ps);
157
158         if(!prs_uint16("revision", ps, depth, &psa->revision))
159                 return False;
160
161         if(!prs_uint16_pre("size     ", ps, depth, &psa->size, &offset_acl_size))
162                 return False;
163
164         if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
165                 return False;
166
167         if (UNMARSHALLING(ps)) {
168                 /*
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).
172                  */
173                 if((psa->ace = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces+1)) == NULL)
174                         return False;
175         }
176
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->ace[i], ps, depth))
181                         return False;
182         }
183
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. */
187
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;
191
192                 for (i = 0; i < extra_len; i++) {
193                         if (!prs_uint8("acl extra space", ps, depth, &c))
194                                 return False;
195                 }
196         }
197
198         if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
199                 return False;
200
201         return True;
202 }
203
204 /*******************************************************************
205  Reads or writes a SEC_DESC structure.
206  If reading and the *ppsd = NULL, allocates the structure.
207 ********************************************************************/
208
209 BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
210 {
211         uint32 old_offset;
212         uint32 max_offset = 0; /* after we're done, move offset to end */
213         uint32 tmp_offset = 0;
214
215         SEC_DESC *psd;
216
217         if (ppsd == NULL)
218                 return False;
219
220         psd = *ppsd;
221
222         if (psd == NULL) {
223                 if(UNMARSHALLING(ps)) {
224                         if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
225                                 return False;
226                         *ppsd = psd;
227                 } else {
228                         /* Marshalling - just ignore. */
229                         return True;
230                 }
231         }
232
233         prs_debug(ps, depth, desc, "sec_io_desc");
234         depth++;
235
236         /* start of security descriptor stored for back-calc offset purposes */
237         old_offset = prs_offset(ps);
238
239         if(!prs_uint16("revision ", ps, depth, &psd->revision))
240                 return False;
241
242         if(!prs_uint16("type     ", ps, depth, &psd->type))
243                 return False;
244
245         if (MARSHALLING(ps)) {
246                 uint32 offset = SEC_DESC_HEADER_SIZE;
247
248                 /*
249                  * Work out the offsets here, as we write it out.
250                  */
251
252                 if (psd->sacl != NULL) {
253                         psd->off_sacl = offset;
254                         offset += psd->sacl->size;
255                 } else {
256                         psd->off_sacl = 0;
257                 }
258
259                 if (psd->dacl != NULL) {
260                         psd->off_dacl = offset;
261                         offset += psd->dacl->size;
262                 } else {
263                         psd->off_dacl = 0;
264                 }
265
266                 if (psd->owner_sid != NULL) {
267                         psd->off_owner_sid = offset;
268                         offset += sid_size(psd->owner_sid);
269                 } else {
270                         psd->off_owner_sid = 0;
271                 }
272
273                 if (psd->grp_sid != NULL) {
274                         psd->off_grp_sid = offset;
275                         offset += sid_size(psd->grp_sid);
276                 } else {
277                         psd->off_grp_sid = 0;
278                 }
279         }
280
281         if(!prs_uint32("off_owner_sid", ps, depth, &psd->off_owner_sid))
282                 return False;
283
284         if(!prs_uint32("off_grp_sid  ", ps, depth, &psd->off_grp_sid))
285                 return False;
286
287         if(!prs_uint32("off_sacl     ", ps, depth, &psd->off_sacl))
288                 return False;
289
290         if(!prs_uint32("off_dacl     ", ps, depth, &psd->off_dacl))
291                 return False;
292
293         max_offset = MAX(max_offset, prs_offset(ps));
294
295         if (psd->off_owner_sid != 0) {
296
297                 tmp_offset = prs_offset(ps);
298                 if(!prs_set_offset(ps, old_offset + psd->off_owner_sid))
299                         return False;
300
301                 if (UNMARSHALLING(ps)) {
302                         /* reading */
303                         if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
304                                 return False;
305                 }
306
307                 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
308                         return False;
309
310                 max_offset = MAX(max_offset, prs_offset(ps));
311
312                 if (!prs_set_offset(ps,tmp_offset))
313                         return False;
314         }
315
316         if (psd->off_grp_sid != 0) {
317
318                 tmp_offset = prs_offset(ps);
319                 if(!prs_set_offset(ps, old_offset + psd->off_grp_sid))
320                         return False;
321
322                 if (UNMARSHALLING(ps)) {
323                         /* reading */
324                         if((psd->grp_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
325                                 return False;
326                 }
327
328                 if(!smb_io_dom_sid("grp_sid", psd->grp_sid, ps, depth))
329                         return False;
330                         
331                 max_offset = MAX(max_offset, prs_offset(ps));
332
333                 if (!prs_set_offset(ps,tmp_offset))
334                         return False;
335         }
336
337         if ((psd->type & SEC_DESC_SACL_PRESENT) && psd->off_sacl) {
338                 tmp_offset = prs_offset(ps);
339                 if(!prs_set_offset(ps, old_offset + psd->off_sacl))
340                         return False;
341                 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
342                         return False;
343                 max_offset = MAX(max_offset, prs_offset(ps));
344                 if (!prs_set_offset(ps,tmp_offset))
345                         return False;
346         }
347
348         if ((psd->type & SEC_DESC_DACL_PRESENT) && psd->off_dacl != 0) {
349                 tmp_offset = prs_offset(ps);
350                 if(!prs_set_offset(ps, old_offset + psd->off_dacl))
351                         return False;
352                 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
353                         return False;
354                 max_offset = MAX(max_offset, prs_offset(ps));
355                 if (!prs_set_offset(ps,tmp_offset))
356                         return False;
357         }
358
359         if(!prs_set_offset(ps, max_offset))
360                 return False;
361
362         return True;
363 }
364
365 /*******************************************************************
366  Reads or writes a SEC_DESC_BUF structure.
367 ********************************************************************/
368
369 BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
370 {
371         uint32 off_len;
372         uint32 off_max_len;
373         uint32 old_offset;
374         uint32 size;
375         SEC_DESC_BUF *psdb;
376
377         if (ppsdb == NULL)
378                 return False;
379
380         psdb = *ppsdb;
381
382         if (UNMARSHALLING(ps) && psdb == NULL) {
383                 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
384                         return False;
385                 *ppsdb = psdb;
386         }
387
388         prs_debug(ps, depth, desc, "sec_io_desc_buf");
389         depth++;
390
391         if(!prs_align(ps))
392                 return False;
393         
394         if(!prs_uint32_pre("max_len", ps, depth, &psdb->max_len, &off_max_len))
395                 return False;
396
397         if(!prs_uint32    ("ptr  ", ps, depth, &psdb->ptr))
398                 return False;
399
400         if(!prs_uint32_pre("len    ", ps, depth, &psdb->len, &off_len))
401                 return False;
402
403         old_offset = prs_offset(ps);
404
405         /* reading, length is non-zero; writing, descriptor is non-NULL */
406         if ((UNMARSHALLING(ps) && psdb->len != 0) || (MARSHALLING(ps) && psdb->sec != NULL)) {
407                 if(!sec_io_desc("sec   ", &psdb->sec, ps, depth))
408                         return False;
409         }
410
411         if(!prs_align(ps))
412                 return False;
413         
414         size = prs_offset(ps) - old_offset;
415         if(!prs_uint32_post("max_len", ps, depth, &psdb->max_len, off_max_len, size == 0 ? psdb->max_len : size))
416                 return False;
417
418         if(!prs_uint32_post("len    ", ps, depth, &psdb->len, off_len, size))
419                 return False;
420
421         return True;
422 }