first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[samba.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-1998
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
26 #include "includes.h"
27
28 extern int DEBUGLEVEL;
29
30 #define SD_HEADER_SIZE 0x14
31
32 /*******************************************************************
33  Sets up a SEC_ACCESS structure.
34 ********************************************************************/
35
36 void init_sec_access(SEC_ACCESS *t, uint32 mask)
37 {
38         t->mask = mask;
39 }
40
41 /*******************************************************************
42  Reads or writes a SEC_ACCESS structure.
43 ********************************************************************/
44
45 BOOL sec_io_access(char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
46 {
47         if (t == NULL)
48                 return False;
49
50         prs_debug(ps, depth, desc, "sec_io_access");
51         depth++;
52
53         if(!prs_align(ps))
54                 return False;
55         
56         if(!prs_uint32("mask", ps, depth, &(t->mask)))
57                 return False;
58
59         return True;
60 }
61
62
63 /*******************************************************************
64  Sets up a SEC_ACE structure.
65 ********************************************************************/
66
67 void init_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 flag)
68 {
69         t->type = type;
70         t->flags = flag;
71         t->size = sid_size(sid) + 8;
72         t->info = mask;
73
74         ZERO_STRUCTP(&t->sid);
75         sid_copy(&t->sid, sid);
76 }
77
78 /*******************************************************************
79  Reads or writes a SEC_ACE structure.
80 ********************************************************************/
81
82 BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
83 {
84         uint32 old_offset;
85         uint32 offset_ace_size;
86
87         if (psa == NULL)
88                 return False;
89
90         prs_debug(ps, depth, desc, "sec_io_ace");
91         depth++;
92
93         if(!prs_align(ps))
94                 return False;
95         
96         old_offset = prs_offset(ps);
97
98         if(!prs_uint8("type ", ps, depth, &psa->type))
99                 return False;
100
101         if(!prs_uint8("flags", ps, depth, &psa->flags))
102                 return False;
103
104         if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
105                 return False;
106
107         if(!sec_io_access("info ", &psa->info, ps, depth))
108                 return False;
109
110         if(!prs_align(ps))
111                 return False;
112
113         if(!smb_io_dom_sid("sid  ", &psa->sid , ps, depth))
114                 return False;
115
116         if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
117                 return False;
118
119         return True;
120 }
121
122 /*******************************************************************
123  Create a SEC_ACL structure.  
124 ********************************************************************/
125
126 SEC_ACL *make_sec_acl(uint16 revision, int num_aces, SEC_ACE *ace_list)
127 {
128         SEC_ACL *dst;
129         int i;
130
131         if((dst = (SEC_ACL *)malloc(sizeof(SEC_ACL))) == NULL)
132                 return NULL;
133
134         ZERO_STRUCTP(dst);
135
136         dst->revision = revision;
137         dst->num_aces = num_aces;
138         dst->size = 8;
139
140         if((dst->ace_list = (SEC_ACE *)malloc( sizeof(SEC_ACE) * num_aces )) == NULL) {
141                 free_sec_acl(&dst);
142                 return NULL;
143         }
144
145         for (i = 0; i < num_aces; i++) {
146                 dst->ace_list[i] = ace_list[i]; /* Structure copy. */
147                 dst->size += ace_list[i].size;
148         }
149
150         return dst;
151 }
152
153 /*******************************************************************
154  Duplicate a SEC_ACL structure.  
155 ********************************************************************/
156
157 SEC_ACL *dup_sec_acl( SEC_ACL *src)
158 {
159         if(src == NULL)
160                 return NULL;
161
162         return make_sec_acl( src->revision, src->num_aces, src->ace_list);
163 }
164
165 /*******************************************************************
166  Delete a SEC_ACL structure.  
167 ********************************************************************/
168
169 void free_sec_acl(SEC_ACL **ppsa)
170 {
171         SEC_ACL *psa;
172
173         if(ppsa == NULL || *ppsa == NULL)
174                 return;
175
176         psa = *ppsa;
177         if (psa->ace_list != NULL)
178                 free(psa->ace_list);
179
180         free(psa);
181         *ppsa = NULL;
182 }
183
184 /*******************************************************************
185  Reads or writes a SEC_ACL structure.  
186
187  First of the xx_io_xx functions that allocates its data structures
188  for you as it reads them.
189 ********************************************************************/
190
191 BOOL sec_io_acl(char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
192 {
193         int i;
194         uint32 old_offset;
195         uint32 offset_acl_size;
196         SEC_ACL *psa;
197
198         if (ppsa == NULL)
199                 return False;
200
201         psa = *ppsa;
202
203         if(UNMARSHALLING(ps) && psa == NULL) {
204                 /*
205                  * This is a read and we must allocate the stuct to read into.
206                  */
207                 if((psa = (SEC_ACL *)malloc(sizeof(SEC_ACL))) == NULL)
208                         return False;
209                 ZERO_STRUCTP(psa);
210                 *ppsa = psa;
211         }
212
213         prs_debug(ps, depth, desc, "sec_io_acl");
214         depth++;
215
216         if(!prs_align(ps))
217                 return False;
218         
219         old_offset = prs_offset(ps);
220
221         if(!prs_uint16("revision", ps, depth, &psa->revision))
222                 return False;
223
224         if(!prs_uint16_pre("size     ", ps, depth, &psa->size, &offset_acl_size))
225                 return False;
226
227         if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
228                 return False;
229
230         if (UNMARSHALLING(ps) && psa->num_aces != 0) {
231                 /* reading */
232                 if((psa->ace_list = malloc(sizeof(psa->ace_list[0]) * psa->num_aces)) == NULL)
233                         return False;
234                 ZERO_STRUCTP(psa->ace_list);
235         }
236
237         for (i = 0; i < psa->num_aces; i++) {
238                 fstring tmp;
239                 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
240                 if(!sec_io_ace(tmp, &psa->ace_list[i], ps, depth))
241                         return False;
242         }
243
244         if(!prs_align(ps))
245                 return False;
246
247         if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
248                 return False;
249
250         return True;
251 }
252
253 /*******************************************************************
254  Creates a SEC_DESC structure
255 ********************************************************************/
256
257 SEC_DESC *make_sec_desc(uint16 revision, uint16 type,
258                         DOM_SID *owner_sid, DOM_SID *grp_sid,
259                         SEC_ACL *sacl, SEC_ACL *dacl, size_t *sec_desc_size)
260 {
261         SEC_DESC *dst;
262         uint32 offset;
263
264         *sec_desc_size = 0;
265
266         if(( dst = (SEC_DESC *)malloc(sizeof(SEC_DESC))) == NULL)
267                 return NULL;
268
269         ZERO_STRUCTP(dst);
270
271         dst->revision = revision;
272         dst->type     = type;
273
274         dst->off_owner_sid = 0;
275         dst->off_grp_sid   = 0;
276         dst->off_sacl      = 0;
277         dst->off_dacl      = 0;
278
279         if(owner_sid && ((dst->owner_sid = sid_dup(owner_sid)) == NULL))
280                 goto error_exit;
281
282         if(grp_sid && ((dst->grp_sid = sid_dup(grp_sid)) == NULL))
283                 goto error_exit;
284
285         if(sacl && ((dst->sacl = dup_sec_acl(sacl)) == NULL))
286                 goto error_exit;
287
288         if(dacl && ((dst->dacl = dup_sec_acl(dacl)) == NULL))
289                 goto error_exit;
290                 
291         offset = 0x0;
292
293         /*
294          * Work out the linearization sizes.
295          */
296
297         if (dst->owner_sid != NULL) {
298
299                 if (offset == 0)
300                         offset = SD_HEADER_SIZE;
301
302                 dst->off_owner_sid = offset;
303                 offset += ((sid_size(dst->owner_sid) + 3) & ~3);
304         }
305
306         if (dst->grp_sid != NULL) {
307
308                 if (offset == 0)
309                         offset = SD_HEADER_SIZE;
310
311                 dst->off_grp_sid = offset;
312                 offset += ((sid_size(dst->grp_sid) + 3) & ~3);
313         }
314
315         if (dst->sacl != NULL) {
316
317                 if (offset == 0)
318                         offset = SD_HEADER_SIZE;
319
320                 dst->off_sacl = offset;
321                 offset += ((sacl->size + 3) & ~3);
322         }
323
324         if (dst->dacl != NULL) {
325
326                 if (offset == 0)
327                         offset = SD_HEADER_SIZE;
328
329                 dst->off_dacl = offset;
330                 offset += ((dacl->size + 3) & ~3);
331         }
332
333         *sec_desc_size = (size_t)((offset == 0) ? SD_HEADER_SIZE : offset);
334         return dst;
335
336 error_exit:
337
338         *sec_desc_size = 0;
339         free_sec_desc(&dst);
340         return NULL;
341 }
342
343 /*******************************************************************
344  Duplicate a SEC_DESC structure.  
345 ********************************************************************/
346
347 SEC_DESC *dup_sec_desc( SEC_DESC *src)
348 {
349         size_t dummy;
350
351         if(src == NULL)
352                 return NULL;
353
354         return make_sec_desc( src->revision, src->type, 
355                                 src->owner_sid, src->grp_sid, src->sacl,
356                                 src->dacl, &dummy);
357 }
358
359 /*******************************************************************
360  Deletes a SEC_DESC structure
361 ********************************************************************/
362
363 void free_sec_desc(SEC_DESC **ppsd)
364 {
365         SEC_DESC *psd;
366
367         if(ppsd == NULL || *ppsd == NULL)
368                 return;
369
370         psd = *ppsd;
371
372         free_sec_acl(&psd->dacl);
373         free_sec_acl(&psd->dacl);
374         free(psd->owner_sid);
375         free(psd->grp_sid);
376         free(psd);
377         *ppsd = NULL;
378 }
379
380 /*******************************************************************
381  Creates a SEC_DESC structure with typical defaults.
382 ********************************************************************/
383
384 SEC_DESC *make_standard_sec_desc(DOM_SID *owner_sid, DOM_SID *grp_sid,
385                                  SEC_ACL *dacl, size_t *sec_desc_size)
386 {
387         return make_sec_desc(1, SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
388                              owner_sid, grp_sid, NULL, dacl, sec_desc_size);
389 }
390
391
392 /*******************************************************************
393  Reads or writes a SEC_DESC structure.
394  If reading and the *ppsd = NULL, allocates the structure.
395 ********************************************************************/
396
397 BOOL sec_io_desc(char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
398 {
399         uint32 old_offset;
400         uint32 max_offset = 0; /* after we're done, move offset to end */
401         SEC_DESC *psd;
402
403         if (ppsd == NULL)
404                 return False;
405
406         psd = *ppsd;
407
408         if(UNMARSHALLING(ps) && psd == NULL) {
409                 if((psd = (SEC_DESC *)malloc(sizeof(SEC_DESC))) == NULL)
410                         return False;
411                 ZERO_STRUCTP(psd);
412                 *ppsd = psd;
413         }
414
415         prs_debug(ps, depth, desc, "sec_io_desc");
416         depth++;
417
418         if(!prs_align(ps))
419                 return False;
420         
421         /* start of security descriptor stored for back-calc offset purposes */
422         old_offset = prs_offset(ps);
423
424         if(!prs_uint16("revision ", ps, depth, &psd->revision))
425                 return False;
426
427         if(!prs_uint16("type     ", ps, depth, &psd->type))
428                 return False;
429
430         if(!prs_uint32("off_owner_sid", ps, depth, &psd->off_owner_sid))
431                 return False;
432
433         if(!prs_uint32("off_grp_sid  ", ps, depth, &psd->off_grp_sid))
434                 return False;
435
436         if(!prs_uint32("off_sacl     ", ps, depth, &psd->off_sacl))
437                 return False;
438
439         if(!prs_uint32("off_dacl     ", ps, depth, &psd->off_dacl))
440                 return False;
441
442         max_offset = MAX(max_offset, prs_offset(ps));
443
444         if (psd->off_owner_sid != 0) {
445
446                 if (UNMARSHALLING(ps)) {
447                         if(!prs_set_offset(ps, old_offset + psd->off_owner_sid))
448                                 return False;
449                         /* reading */
450                         if((psd->owner_sid = malloc(sizeof(*psd->owner_sid))) == NULL)
451                                 return False;
452                         ZERO_STRUCTP(psd->owner_sid);
453                 }
454
455                 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
456                         return False;
457                 if(!prs_align(ps))
458                         return False;
459         }
460
461         max_offset = MAX(max_offset, prs_offset(ps));
462
463         if (psd->off_grp_sid != 0) {
464
465                 if (UNMARSHALLING(ps)) {
466                         /* reading */
467                         if(!prs_set_offset(ps, old_offset + psd->off_grp_sid))
468                                 return False;
469                         if((psd->grp_sid = malloc(sizeof(*psd->grp_sid))) == NULL)
470                                 return False;
471                         ZERO_STRUCTP(psd->grp_sid);
472                 }
473
474                 if(!smb_io_dom_sid("grp_sid", psd->grp_sid, ps, depth))
475                         return False;
476                 if(!prs_align(ps))
477                         return False;
478         }
479
480         max_offset = MAX(max_offset, prs_offset(ps));
481
482         if (IS_BITS_SET_ALL(psd->type, SEC_DESC_SACL_PRESENT) && psd->off_sacl) {
483                 if(!prs_set_offset(ps, old_offset + psd->off_sacl))
484                         return False;
485                 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
486                         return False;
487                 if(!prs_align(ps))
488                         return False;
489         }
490
491         max_offset = MAX(max_offset, prs_offset(ps));
492
493         if (IS_BITS_SET_ALL(psd->type, SEC_DESC_DACL_PRESENT) && psd->off_dacl != 0) {
494                 if(!prs_set_offset(ps, old_offset + psd->off_dacl))
495                         return False;
496                 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
497                         return False;
498                 if(!prs_align(ps))
499                         return False;
500         }
501
502         max_offset = MAX(max_offset, prs_offset(ps));
503
504         if(!prs_set_offset(ps, max_offset))
505                 return False;
506         return True;
507 }
508
509 /*******************************************************************
510  Creates a SEC_DESC_BUF structure.
511 ********************************************************************/
512
513 SEC_DESC_BUF *make_sec_desc_buf(int len, SEC_DESC *sec_desc)
514 {
515         SEC_DESC_BUF *dst;
516
517         if((dst = (SEC_DESC_BUF *)malloc(sizeof(SEC_DESC_BUF))) == NULL)
518                 return NULL;
519
520         ZERO_STRUCTP(dst);
521
522         /* max buffer size (allocated size) */
523         dst->max_len = len;
524         dst->len = len;
525
526         if(sec_desc && ((dst->sec = dup_sec_desc(sec_desc)) == NULL)) {
527                 free_sec_desc_buf(&dst);
528                 return NULL;
529         }
530
531         return dst;
532 }
533
534 /*******************************************************************
535  Duplicates a SEC_DESC_BUF structure.
536 ********************************************************************/
537
538 SEC_DESC_BUF *dup_sec_desc_buf(SEC_DESC_BUF *src)
539 {
540         if(src == NULL)
541                 return NULL;
542
543         return make_sec_desc_buf( src->len, src->sec);
544 }
545
546 /*******************************************************************
547  Deletes a SEC_DESC_BUF structure.
548 ********************************************************************/
549
550 void free_sec_desc_buf(SEC_DESC_BUF **ppsdb)
551 {
552         SEC_DESC_BUF *psdb;
553
554         if(ppsdb == NULL || *ppsdb == NULL)
555                 return;
556
557         psdb = *ppsdb;
558         free_sec_desc(&psdb->sec);
559         free(psdb);
560         *ppsdb = NULL;
561 }
562
563
564 /*******************************************************************
565  Reads or writes a SEC_DESC_BUF structure.
566 ********************************************************************/
567
568 BOOL sec_io_desc_buf(char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
569 {
570         uint32 off_len;
571         uint32 off_max_len;
572         uint32 old_offset;
573         uint32 size;
574         SEC_DESC_BUF *psdb;
575
576         if (ppsdb == NULL)
577                 return False;
578
579         psdb = *ppsdb;
580
581         if (UNMARSHALLING(ps) && psdb == NULL) {
582                 if((psdb = (SEC_DESC_BUF *)malloc(sizeof(SEC_DESC_BUF))) == NULL)
583                         return False;
584                 ZERO_STRUCTP(psdb);
585                 *ppsdb = psdb;
586         }
587
588         prs_debug(ps, depth, desc, "sec_io_desc_buf");
589         depth++;
590
591         if(!prs_align(ps))
592                 return False;
593         
594         if(!prs_uint32_pre("max_len", ps, depth, &psdb->max_len, &off_max_len))
595                 return False;
596
597         if(!prs_uint32    ("undoc  ", ps, depth, &psdb->undoc))
598                 return False;
599
600         if(!prs_uint32_pre("len    ", ps, depth, &psdb->len, &off_len))
601                 return False;
602
603         old_offset = prs_offset(ps);
604
605         /* reading, length is non-zero; writing, descriptor is non-NULL */
606         if ((psdb->len != 0 || MARSHALLING(ps)) && psdb->sec != NULL) {
607                 if(!sec_io_desc("sec   ", &psdb->sec, ps, depth))
608                         return False;
609         }
610
611         size = prs_offset(ps) - old_offset;
612         if(!prs_uint32_post("max_len", ps, depth, &psdb->max_len, off_max_len, size == 0 ? psdb->max_len : size))
613                 return False;
614
615         if(!prs_uint32_post("len    ", ps, depth, &psdb->len, off_len, size))
616                 return False;
617
618         return True;
619 }