added the following message to all dce/rpc client/server code, except
[metze/samba/wip.git] / source3 / rpc_parse / parse_sec.c
1
2 /*
3  * THIS CODE IS OUT-OF-DATE BY TWO YEARS, IS LEGACY DESIGN AND VERY, VERY,
4  * INCOMPLETE.  PLEASE DO NOT MAKE ANY FURTHER ENHANCEMENTS TO THIS CODE
5  * UNLESS THEY ARE ALSO CARRIED OUT IN THE SAMBA_TNG BRANCH.
6  *
7  * PLEASE DO NOT TREAT THIS CODE AS AUTHORITATIVE IN *ANY* WAY.
8  *
9  * REPEAT, PLEASE DO NOT MAKE ANY MODIFICATIONS TO THIS CODE WITHOUT
10  * FIRST CHECKING THE EQUIVALENT MODULE IN SAMBA_TNG, UPDATING THAT
11  * FIRST, *THEN* CONSIDER MAKING THE SAME MODIFICATION IN THIS BRANCH
12  *
13  * YOU WILL, ALMOST GUARANTEED, FIND THAT THE BUG-FIX OR ENHANCEMENT THAT
14  * YOU THINK IS NECESSARY, HAS ALREADY BEEN IMPLEMENTED IN SAMBA_TNG.
15  * IF IT HAS NOT, YOUR BUG-FIX OR ENHANCEMENT *MUST* GO INTO SAMBA_TNG
16  * AS THE SAMBA_TNG CODE WILL REPLACE THIS MODULE WITHOUT REFERENCE TO
17  * ANYTHING IN IT, WITH THE POSSIBLE RISK THAT THE BUG-FIX OR ENHANCEMENT
18  * MAY BE LOST.
19  *
20  * PLEASE OBSERVE AND RESPECT THIS SIMPLE REQUEST.
21  *
22  * THANK YOU.
23  *
24  * lkcl@samba.org
25  */
26
27 /* 
28  *  Unix SMB/Netbios implementation.
29  *  Version 1.9.
30  *  RPC Pipe client / server routines
31  *  Copyright (C) Andrew Tridgell              1992-1998,
32  *  Copyright (C) Jeremy R. Allison            1995-1998
33  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
34  *  Copyright (C) Paul Ashton                  1997-1998.
35  *  
36  *  This program is free software; you can redistribute it and/or modify
37  *  it under the terms of the GNU General Public License as published by
38  *  the Free Software Foundation; either version 2 of the License, or
39  *  (at your option) any later version.
40  *  
41  *  This program is distributed in the hope that it will be useful,
42  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
43  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44  *  GNU General Public License for more details.
45  *  
46  *  You should have received a copy of the GNU General Public License
47  *  along with this program; if not, write to the Free Software
48  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
49  */
50
51
52 #include "includes.h"
53
54 extern int DEBUGLEVEL;
55
56 #define SD_HEADER_SIZE 0x14
57
58 /*******************************************************************
59  Sets up a SEC_ACCESS structure.
60 ********************************************************************/
61
62 void init_sec_access(SEC_ACCESS *t, uint32 mask)
63 {
64         t->mask = mask;
65 }
66
67 /*******************************************************************
68  Reads or writes a SEC_ACCESS structure.
69 ********************************************************************/
70
71 BOOL sec_io_access(char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
72 {
73         if (t == NULL)
74                 return False;
75
76         prs_debug(ps, depth, desc, "sec_io_access");
77         depth++;
78
79         if(!prs_align(ps))
80                 return False;
81         
82         if(!prs_uint32("mask", ps, depth, &(t->mask)))
83                 return False;
84
85         return True;
86 }
87
88
89 /*******************************************************************
90  Sets up a SEC_ACE structure.
91 ********************************************************************/
92
93 void init_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 flag)
94 {
95         t->type = type;
96         t->flags = flag;
97         t->size = sid_size(sid) + 8;
98         t->info = mask;
99
100         ZERO_STRUCTP(&t->sid);
101         sid_copy(&t->sid, sid);
102 }
103
104 /*******************************************************************
105  Reads or writes a SEC_ACE structure.
106 ********************************************************************/
107
108 BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
109 {
110         uint32 old_offset;
111         uint32 offset_ace_size;
112
113         if (psa == NULL)
114                 return False;
115
116         prs_debug(ps, depth, desc, "sec_io_ace");
117         depth++;
118
119         if(!prs_align(ps))
120                 return False;
121         
122         old_offset = prs_offset(ps);
123
124         if(!prs_uint8("type ", ps, depth, &psa->type))
125                 return False;
126
127         if(!prs_uint8("flags", ps, depth, &psa->flags))
128                 return False;
129
130         if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
131                 return False;
132
133         if(!sec_io_access("info ", &psa->info, ps, depth))
134                 return False;
135
136         if(!prs_align(ps))
137                 return False;
138
139         if(!smb_io_dom_sid("sid  ", &psa->sid , ps, depth))
140                 return False;
141
142         if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
143                 return False;
144
145         return True;
146 }
147
148 /*******************************************************************
149  Create a SEC_ACL structure.  
150 ********************************************************************/
151
152 SEC_ACL *make_sec_acl(uint16 revision, int num_aces, SEC_ACE *ace_list)
153 {
154         SEC_ACL *dst;
155         int i;
156
157         if((dst = (SEC_ACL *)malloc(sizeof(SEC_ACL))) == NULL)
158                 return NULL;
159
160         ZERO_STRUCTP(dst);
161
162         dst->revision = revision;
163         dst->num_aces = num_aces;
164         dst->size = 8;
165
166         if((dst->ace_list = (SEC_ACE *)malloc( sizeof(SEC_ACE) * num_aces )) == NULL) {
167                 free_sec_acl(&dst);
168                 return NULL;
169         }
170
171         for (i = 0; i < num_aces; i++) {
172                 dst->ace_list[i] = ace_list[i]; /* Structure copy. */
173                 dst->size += ace_list[i].size;
174         }
175
176         return dst;
177 }
178
179 /*******************************************************************
180  Duplicate a SEC_ACL structure.  
181 ********************************************************************/
182
183 SEC_ACL *dup_sec_acl( SEC_ACL *src)
184 {
185         if(src == NULL)
186                 return NULL;
187
188         return make_sec_acl( src->revision, src->num_aces, src->ace_list);
189 }
190
191 /*******************************************************************
192  Delete a SEC_ACL structure.  
193 ********************************************************************/
194
195 void free_sec_acl(SEC_ACL **ppsa)
196 {
197         SEC_ACL *psa;
198
199         if(ppsa == NULL || *ppsa == NULL)
200                 return;
201
202         psa = *ppsa;
203         if (psa->ace_list != NULL)
204                 free(psa->ace_list);
205
206         free(psa);
207         *ppsa = NULL;
208 }
209
210 /*******************************************************************
211  Reads or writes a SEC_ACL structure.  
212
213  First of the xx_io_xx functions that allocates its data structures
214  for you as it reads them.
215 ********************************************************************/
216
217 BOOL sec_io_acl(char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
218 {
219         int i;
220         uint32 old_offset;
221         uint32 offset_acl_size;
222         SEC_ACL *psa;
223
224         if (ppsa == NULL)
225                 return False;
226
227         psa = *ppsa;
228
229         if(UNMARSHALLING(ps) && psa == NULL) {
230                 /*
231                  * This is a read and we must allocate the stuct to read into.
232                  */
233                 if((psa = (SEC_ACL *)malloc(sizeof(SEC_ACL))) == NULL)
234                         return False;
235                 ZERO_STRUCTP(psa);
236                 *ppsa = psa;
237         }
238
239         prs_debug(ps, depth, desc, "sec_io_acl");
240         depth++;
241
242         if(!prs_align(ps))
243                 return False;
244         
245         old_offset = prs_offset(ps);
246
247         if(!prs_uint16("revision", ps, depth, &psa->revision))
248                 return False;
249
250         if(!prs_uint16_pre("size     ", ps, depth, &psa->size, &offset_acl_size))
251                 return False;
252
253         if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
254                 return False;
255
256         if (UNMARSHALLING(ps) && psa->num_aces != 0) {
257                 /* reading */
258                 if((psa->ace_list = malloc(sizeof(psa->ace_list[0]) * psa->num_aces)) == NULL)
259                         return False;
260                 ZERO_STRUCTP(psa->ace_list);
261         }
262
263         for (i = 0; i < psa->num_aces; i++) {
264                 fstring tmp;
265                 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
266                 if(!sec_io_ace(tmp, &psa->ace_list[i], ps, depth))
267                         return False;
268         }
269
270         if(!prs_align(ps))
271                 return False;
272
273         if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
274                 return False;
275
276         return True;
277 }
278
279 /*******************************************************************
280  Creates a SEC_DESC structure
281 ********************************************************************/
282
283 SEC_DESC *make_sec_desc(uint16 revision, uint16 type,
284                         DOM_SID *owner_sid, DOM_SID *grp_sid,
285                         SEC_ACL *sacl, SEC_ACL *dacl, size_t *sec_desc_size)
286 {
287         SEC_DESC *dst;
288         uint32 offset;
289
290         *sec_desc_size = 0;
291
292         if(( dst = (SEC_DESC *)malloc(sizeof(SEC_DESC))) == NULL)
293                 return NULL;
294
295         ZERO_STRUCTP(dst);
296
297         dst->revision = revision;
298         dst->type     = type;
299
300         dst->off_owner_sid = 0;
301         dst->off_grp_sid   = 0;
302         dst->off_sacl      = 0;
303         dst->off_dacl      = 0;
304
305         if(owner_sid && ((dst->owner_sid = sid_dup(owner_sid)) == NULL))
306                 goto error_exit;
307
308         if(grp_sid && ((dst->grp_sid = sid_dup(grp_sid)) == NULL))
309                 goto error_exit;
310
311         if(sacl && ((dst->sacl = dup_sec_acl(sacl)) == NULL))
312                 goto error_exit;
313
314         if(dacl && ((dst->dacl = dup_sec_acl(dacl)) == NULL))
315                 goto error_exit;
316                 
317         offset = 0x0;
318
319         /*
320          * Work out the linearization sizes.
321          */
322
323         if (dst->owner_sid != NULL) {
324
325                 if (offset == 0)
326                         offset = SD_HEADER_SIZE;
327
328                 dst->off_owner_sid = offset;
329                 offset += ((sid_size(dst->owner_sid) + 3) & ~3);
330         }
331
332         if (dst->grp_sid != NULL) {
333
334                 if (offset == 0)
335                         offset = SD_HEADER_SIZE;
336
337                 dst->off_grp_sid = offset;
338                 offset += ((sid_size(dst->grp_sid) + 3) & ~3);
339         }
340
341         if (dst->sacl != NULL) {
342
343                 if (offset == 0)
344                         offset = SD_HEADER_SIZE;
345
346                 dst->off_sacl = offset;
347                 offset += ((sacl->size + 3) & ~3);
348         }
349
350         if (dst->dacl != NULL) {
351
352                 if (offset == 0)
353                         offset = SD_HEADER_SIZE;
354
355                 dst->off_dacl = offset;
356                 offset += ((dacl->size + 3) & ~3);
357         }
358
359         *sec_desc_size = (size_t)((offset == 0) ? SD_HEADER_SIZE : offset);
360         return dst;
361
362 error_exit:
363
364         *sec_desc_size = 0;
365         free_sec_desc(&dst);
366         return NULL;
367 }
368
369 /*******************************************************************
370  Duplicate a SEC_DESC structure.  
371 ********************************************************************/
372
373 SEC_DESC *dup_sec_desc( SEC_DESC *src)
374 {
375         size_t dummy;
376
377         if(src == NULL)
378                 return NULL;
379
380         return make_sec_desc( src->revision, src->type, 
381                                 src->owner_sid, src->grp_sid, src->sacl,
382                                 src->dacl, &dummy);
383 }
384
385 /*******************************************************************
386  Deletes a SEC_DESC structure
387 ********************************************************************/
388
389 void free_sec_desc(SEC_DESC **ppsd)
390 {
391         SEC_DESC *psd;
392
393         if(ppsd == NULL || *ppsd == NULL)
394                 return;
395
396         psd = *ppsd;
397
398         free_sec_acl(&psd->dacl);
399         free_sec_acl(&psd->dacl);
400         free(psd->owner_sid);
401         free(psd->grp_sid);
402         free(psd);
403         *ppsd = NULL;
404 }
405
406 /*******************************************************************
407  Creates a SEC_DESC structure with typical defaults.
408 ********************************************************************/
409
410 SEC_DESC *make_standard_sec_desc(DOM_SID *owner_sid, DOM_SID *grp_sid,
411                                  SEC_ACL *dacl, size_t *sec_desc_size)
412 {
413         return make_sec_desc(1, SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
414                              owner_sid, grp_sid, NULL, dacl, sec_desc_size);
415 }
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(int 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 = len;
550         dst->len = 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         size = prs_offset(ps) - old_offset;
638         if(!prs_uint32_post("max_len", ps, depth, &psdb->max_len, off_max_len, size == 0 ? psdb->max_len : size))
639                 return False;
640
641         if(!prs_uint32_post("len    ", ps, depth, &psdb->len, off_len, size))
642                 return False;
643
644         return True;
645 }