2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1999,
6 * Copyright (C) Jeremy R. Allison 1995-1999
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
8 * Copyright (C) Paul Ashton 1997-1999.
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.
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.
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.
28 extern int DEBUGLEVEL;
31 /*******************************************************************
33 ********************************************************************/
34 BOOL make_sec_access(SEC_ACCESS *t, uint32 mask)
41 /*******************************************************************
42 reads or writes a structure.
43 ********************************************************************/
44 BOOL sec_io_access(char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
46 if (t == NULL) return False;
48 prs_debug(ps, depth, desc, "sec_io_access");
53 prs_uint32("mask", ps, depth, &(t->mask));
59 /*******************************************************************
61 ********************************************************************/
62 BOOL make_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 flag)
66 t->size = sid_size(sid) + 8;
69 sid_copy(&t->sid, sid);
74 /*******************************************************************
75 reads or writes a structure.
76 ********************************************************************/
77 BOOL sec_io_ace(char *desc, SEC_ACE *t, prs_struct *ps, int depth)
80 uint32 offset_ace_size;
81 if (t == NULL) return False;
83 prs_debug(ps, depth, desc, "sec_io_ace");
88 old_offset = ps->offset;
90 prs_uint8 ("type ", ps, depth, &(t->type));
91 prs_uint8 ("flags", ps, depth, &(t->flags));
92 prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_ace_size);
94 sec_io_access ("info ", &t->info, ps, depth);
96 smb_io_dom_sid("sid ", &t->sid , ps, depth);
98 prs_uint16_post("size ", ps, depth, &t->size, offset_ace_size, old_offset);
103 /*******************************************************************
105 ********************************************************************/
106 BOOL make_sec_acl(SEC_ACL *t, uint16 revision, int num_aces, SEC_ACE *ace)
109 t->revision = revision;
110 t->num_aces = num_aces;
114 for (i = 0; i < num_aces; i++)
116 t->size += ace[i].size;
122 /*******************************************************************
124 ********************************************************************/
125 void free_sec_acl(SEC_ACL *t)
133 /*******************************************************************
134 reads or writes a structure.
136 first of the xx_io_xx functions that allocates its data structures
137 for you as it reads them.
138 ********************************************************************/
139 BOOL sec_io_acl(char *desc, SEC_ACL *t, prs_struct *ps, int depth)
143 uint32 offset_acl_size;
145 if (t == NULL) return False;
147 prs_debug(ps, depth, desc, "sec_io_acl");
152 old_offset = ps->offset;
154 prs_uint16("revision", ps, depth, &(t->revision));
155 prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_acl_size);
156 prs_uint32("num_aces ", ps, depth, &(t->num_aces ));
158 if (ps->io && t->num_aces != 0)
161 t->ace = (SEC_ACE*)malloc(sizeof(t->ace[0]) * t->num_aces);
162 ZERO_STRUCTP(t->ace);
165 if (t->ace == NULL && t->num_aces != 0)
167 DEBUG(0,("INVALID ACL\n"));
168 ps->offset = 0xfffffffe;
172 for (i = 0; i < MIN(t->num_aces, MAX_SEC_ACES); i++)
175 slprintf(tmp, sizeof(tmp)-1, "ace[%02d]: ", i);
176 sec_io_ace(tmp, &t->ace[i], ps, depth);
181 prs_uint16_post("size ", ps, depth, &t->size , offset_acl_size, old_offset);
187 /*******************************************************************
189 ********************************************************************/
190 int make_sec_desc(SEC_DESC *t, uint16 revision, uint16 type,
191 DOM_SID *owner_sid, DOM_SID *grp_sid,
192 SEC_ACL *sacl, SEC_ACL *dacl)
196 t->revision = revision;
199 t->off_owner_sid = 0;
206 t->owner_sid = owner_sid;
207 t->grp_sid = grp_sid;
217 t->off_dacl = offset;
218 offset += dacl->size;
227 t->off_dacl = offset;
228 offset += dacl->size;
231 if (owner_sid != NULL)
237 t->off_owner_sid = offset;
238 offset += sid_size(owner_sid);
247 t->off_grp_sid = offset;
248 offset += sid_size(grp_sid);
251 return (offset == 0) ? 0x14 : offset;
255 /*******************************************************************
257 ********************************************************************/
258 void free_sec_desc(SEC_DESC *t)
262 free_sec_acl(t->dacl);
267 free_sec_acl(t->dacl);
271 if (t->owner_sid != NULL)
276 if (t->grp_sid != NULL)
283 /*******************************************************************
284 reads or writes a structure.
285 ********************************************************************/
286 static BOOL sec_io_desc(char *desc, SEC_DESC *t, prs_struct *ps, int depth)
289 uint32 off_owner_sid;
295 uint32 max_offset = 0; /* after we're done, move offset to end */
297 if (t == NULL) return False;
299 prs_debug(ps, depth, desc, "sec_io_desc");
304 /* start of security descriptor stored for back-calc offset purposes */
305 old_offset = ps->offset;
307 prs_uint16("revision ", ps, depth, &(t->revision ));
308 prs_uint16("type ", ps, depth, &(t->type ));
310 prs_uint32("off_owner_sid", ps, depth, &(t->off_owner_sid));
311 prs_uint32("off_grp_sid ", ps, depth, &(t->off_grp_sid ));
312 prs_uint32("off_sacl ", ps, depth, &(t->off_sacl ));
313 prs_uint32("off_dacl ", ps, depth, &(t->off_dacl ));
315 prs_uint32_pre("off_owner_sid", ps, depth, &(t->off_owner_sid), &off_owner_sid);
316 prs_uint32_pre("off_grp_sid ", ps, depth, &(t->off_grp_sid ), &off_grp_sid );
317 prs_uint32_pre("off_sacl ", ps, depth, &(t->off_sacl ), &off_sacl );
318 prs_uint32_pre("off_dacl ", ps, depth, &(t->off_dacl ), &off_dacl );
320 max_offset = MAX(max_offset, ps->offset);
322 if (IS_BITS_SET_ALL(t->type, SEC_DESC_DACL_PRESENT))
325 prs_uint32_post("off_dacl ", ps, depth, &(t->off_dacl ), off_dacl , ps->offset - old_offset);
327 ps->offset = old_offset + t->off_dacl;
331 t->dacl = (SEC_ACL*)malloc(sizeof(*t->dacl));
332 ZERO_STRUCTP(t->dacl);
337 DEBUG(0,("INVALID DACL\n"));
338 ps->offset = 0xfffffffe;
342 sec_io_acl ("dacl" , t->dacl , ps, depth);
348 prs_uint32_post("off_dacl ", ps, depth, &(t->off_dacl ), off_dacl , 0);
352 max_offset = MAX(max_offset, ps->offset);
354 if (IS_BITS_SET_ALL(t->type, SEC_DESC_SACL_PRESENT))
357 prs_uint32_post("off_sacl ", ps, depth, &(t->off_sacl ), off_sacl , ps->offset - old_offset);
359 ps->offset = old_offset + t->off_sacl;
363 t->sacl = (SEC_ACL*)malloc(sizeof(*t->sacl));
364 ZERO_STRUCTP(t->sacl);
369 DEBUG(0,("INVALID SACL\n"));
370 ps->offset = 0xfffffffe;
374 sec_io_acl ("sacl" , t->sacl , ps, depth);
380 prs_uint32_post("off_sacl ", ps, depth, &(t->off_sacl ), off_sacl , 0);
384 max_offset = MAX(max_offset, ps->offset);
387 prs_uint32_post("off_owner_sid", ps, depth, &(t->off_owner_sid), off_owner_sid, ps->offset - old_offset);
389 if (t->off_owner_sid != 0)
393 ps->offset = old_offset + t->off_owner_sid;
398 t->owner_sid = (DOM_SID*)malloc(sizeof(*t->owner_sid));
399 ZERO_STRUCTP(t->owner_sid);
402 if (t->owner_sid == NULL)
404 DEBUG(0,("INVALID OWNER SID\n"));
405 ps->offset = 0xfffffffe;
409 smb_io_dom_sid("owner_sid ", t->owner_sid , ps, depth);
413 max_offset = MAX(max_offset, ps->offset);
416 prs_uint32_post("off_grp_sid ", ps, depth, &(t->off_grp_sid ), off_grp_sid , ps->offset - old_offset);
418 if (t->off_grp_sid != 0)
422 ps->offset = old_offset + t->off_grp_sid;
428 t->grp_sid = (DOM_SID*)malloc(sizeof(*t->grp_sid));
429 ZERO_STRUCTP(t->grp_sid);
432 if (t->grp_sid == NULL)
434 DEBUG(0,("INVALID GROUP SID\n"));
435 ps->offset = 0xfffffffe;
439 smb_io_dom_sid("grp_sid", t->grp_sid, ps, depth);
443 max_offset = MAX(max_offset, ps->offset);
445 ps->offset = max_offset;
450 /*******************************************************************
451 creates a SEC_DESC_BUF structure.
452 ********************************************************************/
453 BOOL make_sec_desc_buf(SEC_DESC_BUF *buf, int len, SEC_DESC *data)
457 /* max buffer size (allocated size) */
460 buf->len = data != NULL ? len : 0;
466 /*******************************************************************
467 frees a SEC_DESC_BUF structure.
468 ********************************************************************/
469 void free_sec_desc_buf(SEC_DESC_BUF *buf)
471 if (buf->sec != NULL)
473 free_sec_desc(buf->sec);
479 /*******************************************************************
480 reads or writes a SEC_DESC_BUF structure.
481 ********************************************************************/
482 BOOL sec_io_desc_buf(char *desc, SEC_DESC_BUF *sec, prs_struct *ps, int depth)
489 if (sec == NULL) return False;
491 prs_debug(ps, depth, desc, "sec_io_desc_buf");
496 prs_uint32_pre("max_len", ps, depth, &(sec->max_len), &off_max_len);
497 prs_uint32 ("undoc ", ps, depth, &(sec->undoc ));
498 prs_uint32_pre("len ", ps, depth, &(sec->len ), &off_len);
500 old_offset = ps->offset;
502 if (sec->len != 0 && ps->io)
505 sec->sec = (SEC_DESC*)malloc(sizeof(*sec->sec));
506 ZERO_STRUCTP(sec->sec);
508 if (sec->sec == NULL)
510 DEBUG(0,("INVALID SEC_DESC\n"));
511 ps->offset = 0xfffffffe;
516 /* reading, length is non-zero; writing, descriptor is non-NULL */
517 if ((sec->len != 0 || (!ps->io)) && sec->sec != NULL)
519 sec_io_desc("sec ", sec->sec, ps, depth);
522 size = ps->offset - old_offset;
523 prs_uint32_post("max_len", ps, depth, &(sec->max_len), off_max_len, size == 0 ? sec->max_len : size);
524 prs_uint32_post("len ", ps, depth, &(sec->len ), off_len , size);