sec_desc_size() needs to handle a null secdesc
[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-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 = (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[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);
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 != NULL)
178                 free(psa->ace);
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 = malloc(sizeof(psa->ace[0]) * psa->num_aces)) == NULL)
233                         return False;
234                 ZERO_STRUCTP(psa->ace);
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[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  Works out the linearization size of a SEC_DESC.
255 ********************************************************************/
256
257 size_t sec_desc_size(SEC_DESC *psd)
258 {
259         size_t offset;
260
261         if (!psd) return 0;
262
263         offset = SD_HEADER_SIZE;
264
265         if (psd->owner_sid != NULL)
266                 offset += ((sid_size(psd->owner_sid) + 3) & ~3);
267
268         if (psd->grp_sid != NULL)
269                 offset += ((sid_size(psd->grp_sid) + 3) & ~3);
270
271         if (psd->sacl != NULL)
272                 offset += ((psd->sacl->size + 3) & ~3);
273
274         if (psd->dacl != NULL)
275                 offset += ((psd->dacl->size + 3) & ~3);
276
277         return offset;
278 }
279
280 /*******************************************************************
281  Creates a SEC_DESC structure
282 ********************************************************************/
283
284 SEC_DESC *make_sec_desc(uint16 revision, uint16 type,
285                         DOM_SID *owner_sid, DOM_SID *grp_sid,
286                         SEC_ACL *sacl, SEC_ACL *dacl, size_t *sd_size)
287 {
288         SEC_DESC *dst;
289         uint32 offset;
290
291         *sd_size = 0;
292
293         if(( dst = (SEC_DESC *)malloc(sizeof(SEC_DESC))) == NULL)
294                 return NULL;
295
296         ZERO_STRUCTP(dst);
297
298         dst->revision = revision;
299         dst->type     = type;
300
301         dst->off_owner_sid = 0;
302         dst->off_grp_sid   = 0;
303         dst->off_sacl      = 0;
304         dst->off_dacl      = 0;
305
306         if(owner_sid && ((dst->owner_sid = sid_dup(owner_sid)) == NULL))
307                 goto error_exit;
308
309         if(grp_sid && ((dst->grp_sid = sid_dup(grp_sid)) == NULL))
310                 goto error_exit;
311
312         if(sacl && ((dst->sacl = dup_sec_acl(sacl)) == NULL))
313                 goto error_exit;
314
315         if(dacl && ((dst->dacl = dup_sec_acl(dacl)) == NULL))
316                 goto error_exit;
317                 
318         offset = 0;
319
320         /*
321          * Work out the linearization sizes.
322          */
323
324         if (dst->owner_sid != NULL) {
325
326                 if (offset == 0)
327                         offset = SD_HEADER_SIZE;
328
329                 dst->off_owner_sid = offset;
330                 offset += ((sid_size(dst->owner_sid) + 3) & ~3);
331         }
332
333         if (dst->grp_sid != NULL) {
334
335                 if (offset == 0)
336                         offset = SD_HEADER_SIZE;
337
338                 dst->off_grp_sid = offset;
339                 offset += ((sid_size(dst->grp_sid) + 3) & ~3);
340         }
341
342         if (dst->sacl != NULL) {
343
344                 if (offset == 0)
345                         offset = SD_HEADER_SIZE;
346
347                 dst->off_sacl = offset;
348                 offset += ((sacl->size + 3) & ~3);
349         }
350
351         if (dst->dacl != NULL) {
352
353                 if (offset == 0)
354                         offset = SD_HEADER_SIZE;
355
356                 dst->off_dacl = offset;
357                 offset += ((dacl->size + 3) & ~3);
358         }
359
360         *sd_size = (size_t)((offset == 0) ? SD_HEADER_SIZE : offset);
361         return dst;
362
363 error_exit:
364
365         *sd_size = 0;
366         free_sec_desc(&dst);
367         return NULL;
368 }
369
370 /*******************************************************************
371  Duplicate a SEC_DESC structure.  
372 ********************************************************************/
373
374 SEC_DESC *dup_sec_desc( SEC_DESC *src)
375 {
376         size_t dummy;
377
378         if(src == NULL)
379                 return NULL;
380
381         return make_sec_desc( src->revision, src->type, 
382                                 src->owner_sid, src->grp_sid, src->sacl,
383                                 src->dacl, &dummy);
384 }
385
386 /*******************************************************************
387  Deletes a SEC_DESC structure
388 ********************************************************************/
389
390 void free_sec_desc(SEC_DESC **ppsd)
391 {
392         SEC_DESC *psd;
393
394         if(ppsd == NULL || *ppsd == NULL)
395                 return;
396
397         psd = *ppsd;
398
399         free_sec_acl(&psd->dacl);
400         free_sec_acl(&psd->dacl);
401         free(psd->owner_sid);
402         free(psd->grp_sid);
403         free(psd);
404         *ppsd = NULL;
405 }
406
407 /*******************************************************************
408  Creates a SEC_DESC structure with typical defaults.
409 ********************************************************************/
410
411 SEC_DESC *make_standard_sec_desc(DOM_SID *owner_sid, DOM_SID *grp_sid,
412                                  SEC_ACL *dacl, size_t *sd_size)
413 {
414         return make_sec_desc(1, SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
415                              owner_sid, grp_sid, NULL, dacl, sd_size);
416 }
417
418 /*******************************************************************
419  Reads or writes a SEC_DESC structure.
420  If reading and the *ppsd = NULL, allocates the structure.
421 ********************************************************************/
422
423 BOOL sec_io_desc(char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
424 {
425         uint32 old_offset;
426         uint32 max_offset = 0; /* after we're done, move offset to end */
427         SEC_DESC *psd;
428
429         if (ppsd == NULL)
430                 return False;
431
432         psd = *ppsd;
433
434         if(UNMARSHALLING(ps) && psd == NULL) {
435                 if((psd = (SEC_DESC *)malloc(sizeof(SEC_DESC))) == NULL)
436                         return False;
437                 ZERO_STRUCTP(psd);
438                 *ppsd = psd;
439         }
440
441         prs_debug(ps, depth, desc, "sec_io_desc");
442         depth++;
443
444         if(!prs_align(ps))
445                 return False;
446         
447         /* start of security descriptor stored for back-calc offset purposes */
448         old_offset = prs_offset(ps);
449
450         if(!prs_uint16("revision ", ps, depth, &psd->revision))
451                 return False;
452
453         if(!prs_uint16("type     ", ps, depth, &psd->type))
454                 return False;
455
456         if(!prs_uint32("off_owner_sid", ps, depth, &psd->off_owner_sid))
457                 return False;
458
459         if(!prs_uint32("off_grp_sid  ", ps, depth, &psd->off_grp_sid))
460                 return False;
461
462         if(!prs_uint32("off_sacl     ", ps, depth, &psd->off_sacl))
463                 return False;
464
465         if(!prs_uint32("off_dacl     ", ps, depth, &psd->off_dacl))
466                 return False;
467
468         max_offset = MAX(max_offset, prs_offset(ps));
469
470         if (psd->off_owner_sid != 0) {
471
472                 if (UNMARSHALLING(ps)) {
473                         if(!prs_set_offset(ps, old_offset + psd->off_owner_sid))
474                                 return False;
475                         /* reading */
476                         if((psd->owner_sid = malloc(sizeof(*psd->owner_sid))) == NULL)
477                                 return False;
478                         ZERO_STRUCTP(psd->owner_sid);
479                 }
480
481                 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
482                         return False;
483                 if(!prs_align(ps))
484                         return False;
485         }
486
487         max_offset = MAX(max_offset, prs_offset(ps));
488
489         if (psd->off_grp_sid != 0) {
490
491                 if (UNMARSHALLING(ps)) {
492                         /* reading */
493                         if(!prs_set_offset(ps, old_offset + psd->off_grp_sid))
494                                 return False;
495                         if((psd->grp_sid = malloc(sizeof(*psd->grp_sid))) == NULL)
496                                 return False;
497                         ZERO_STRUCTP(psd->grp_sid);
498                 }
499
500                 if(!smb_io_dom_sid("grp_sid", psd->grp_sid, ps, depth))
501                         return False;
502                 if(!prs_align(ps))
503                         return False;
504         }
505
506         max_offset = MAX(max_offset, prs_offset(ps));
507
508         if (IS_BITS_SET_ALL(psd->type, SEC_DESC_SACL_PRESENT) && psd->off_sacl) {
509                 if(!prs_set_offset(ps, old_offset + psd->off_sacl))
510                         return False;
511                 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
512                         return False;
513                 if(!prs_align(ps))
514                         return False;
515         }
516
517         max_offset = MAX(max_offset, prs_offset(ps));
518
519         if (IS_BITS_SET_ALL(psd->type, SEC_DESC_DACL_PRESENT) && psd->off_dacl != 0) {
520                 if(!prs_set_offset(ps, old_offset + psd->off_dacl))
521                         return False;
522                 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
523                         return False;
524                 if(!prs_align(ps))
525                         return False;
526         }
527
528         max_offset = MAX(max_offset, prs_offset(ps));
529
530         if(!prs_set_offset(ps, max_offset))
531                 return False;
532         return True;
533 }
534
535 /*******************************************************************
536  Creates a SEC_DESC_BUF structure.
537 ********************************************************************/
538
539 SEC_DESC_BUF *make_sec_desc_buf(size_t len, SEC_DESC *sec_desc)
540 {
541         SEC_DESC_BUF *dst;
542
543         if((dst = (SEC_DESC_BUF *)malloc(sizeof(SEC_DESC_BUF))) == NULL)
544                 return NULL;
545
546         ZERO_STRUCTP(dst);
547
548         /* max buffer size (allocated size) */
549         dst->max_len = (uint32)len;
550         dst->len = (uint32)len;
551
552         if(sec_desc && ((dst->sec = dup_sec_desc(sec_desc)) == NULL)) {
553                 free_sec_desc_buf(&dst);
554                 return NULL;
555         }
556
557         return dst;
558 }
559
560 /*******************************************************************
561  Duplicates a SEC_DESC_BUF structure.
562 ********************************************************************/
563
564 SEC_DESC_BUF *dup_sec_desc_buf(SEC_DESC_BUF *src)
565 {
566         if(src == NULL)
567                 return NULL;
568
569         return make_sec_desc_buf( src->len, src->sec);
570 }
571
572 /*******************************************************************
573  Deletes a SEC_DESC_BUF structure.
574 ********************************************************************/
575
576 void free_sec_desc_buf(SEC_DESC_BUF **ppsdb)
577 {
578         SEC_DESC_BUF *psdb;
579
580         if(ppsdb == NULL || *ppsdb == NULL)
581                 return;
582
583         psdb = *ppsdb;
584         free_sec_desc(&psdb->sec);
585         free(psdb);
586         *ppsdb = NULL;
587 }
588
589
590 /*******************************************************************
591  Reads or writes a SEC_DESC_BUF structure.
592 ********************************************************************/
593
594 BOOL sec_io_desc_buf(char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
595 {
596         uint32 off_len;
597         uint32 off_max_len;
598         uint32 old_offset;
599         uint32 size;
600         SEC_DESC_BUF *psdb;
601
602         if (ppsdb == NULL)
603                 return False;
604
605         psdb = *ppsdb;
606
607         if (UNMARSHALLING(ps) && psdb == NULL) {
608                 if((psdb = (SEC_DESC_BUF *)malloc(sizeof(SEC_DESC_BUF))) == NULL)
609                         return False;
610                 ZERO_STRUCTP(psdb);
611                 *ppsdb = psdb;
612         }
613
614         prs_debug(ps, depth, desc, "sec_io_desc_buf");
615         depth++;
616
617         if(!prs_align(ps))
618                 return False;
619         
620         if(!prs_uint32_pre("max_len", ps, depth, &psdb->max_len, &off_max_len))
621                 return False;
622
623         if(!prs_uint32    ("undoc  ", ps, depth, &psdb->undoc))
624                 return False;
625
626         if(!prs_uint32_pre("len    ", ps, depth, &psdb->len, &off_len))
627                 return False;
628
629         old_offset = prs_offset(ps);
630
631         /* reading, length is non-zero; writing, descriptor is non-NULL */
632         if ((psdb->len != 0 || MARSHALLING(ps)) && psdb->sec != NULL) {
633                 if(!sec_io_desc("sec   ", &psdb->sec, ps, depth))
634                         return False;
635         }
636
637         if(!prs_align(ps))
638                 return False;
639         
640         size = prs_offset(ps) - old_offset;
641         if(!prs_uint32_post("max_len", ps, depth, &psdb->max_len, off_max_len, size == 0 ? psdb->max_len : size))
642                 return False;
643
644         if(!prs_uint32_post("len    ", ps, depth, &psdb->len, off_len, size))
645                 return False;
646
647         return True;
648 }