2 * Unix SMB/Netbios implementation.
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.
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 void make_sec_access(SEC_ACCESS *t, uint32 mask)
39 /*******************************************************************
40 reads or writes a structure.
41 ********************************************************************/
42 void sec_io_access(char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
44 if (t == NULL) return;
46 prs_debug(ps, depth, desc, "sec_io_access");
51 prs_uint32("mask", ps, depth, &(t->mask));
55 /*******************************************************************
57 ********************************************************************/
58 void make_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 flag)
62 t->size = sid_size(sid) + 8;
65 sid_copy(&t->sid, sid);
68 /*******************************************************************
69 reads or writes a structure.
70 ********************************************************************/
71 void sec_io_ace(char *desc, SEC_ACE *t, prs_struct *ps, int depth)
74 uint32 offset_ace_size;
75 if (t == NULL) return;
77 prs_debug(ps, depth, desc, "sec_io_ace");
82 old_offset = ps->offset;
84 prs_uint8 ("type ", ps, depth, &(t->type));
85 prs_uint8 ("flags", ps, depth, &(t->flags));
86 prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_ace_size);
88 sec_io_access ("info ", &t->info, ps, depth);
90 smb_io_dom_sid("sid ", &t->sid , ps, depth);
92 prs_uint16_post("size ", ps, depth, &t->size, offset_ace_size, old_offset);
95 /*******************************************************************
97 ********************************************************************/
98 void make_sec_acl(SEC_ACL *t, uint16 revision, int num_aces, SEC_ACE *ace)
101 t->revision = revision;
102 t->num_aces = num_aces;
106 for (i = 0; i < num_aces; i++)
108 t->size += ace[i].size;
112 /*******************************************************************
114 ********************************************************************/
115 void free_sec_acl(SEC_ACL *t)
123 /*******************************************************************
124 reads or writes a structure.
126 first of the xx_io_xx functions that allocates its data structures
127 for you as it reads them.
128 ********************************************************************/
129 void sec_io_acl(char *desc, SEC_ACL *t, prs_struct *ps, int depth)
133 uint32 offset_acl_size;
135 if (t == NULL) return;
137 prs_debug(ps, depth, desc, "sec_io_acl");
142 old_offset = ps->offset;
144 prs_uint16("revision", ps, depth, &(t->revision));
145 prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_acl_size);
146 prs_uint32("num_aces ", ps, depth, &(t->num_aces ));
148 if (ps->io && t->num_aces != 0)
151 t->ace = malloc(sizeof(t->ace[0]) * t->num_aces);
152 ZERO_STRUCTPN(t->ace);
155 if (t->ace == NULL && t->num_aces != 0)
157 DEBUG(0,("INVALID ACL\n"));
158 ps->offset = 0xfffffffe;
162 for (i = 0; i < MIN(t->num_aces, MAX_SEC_ACES); i++)
165 snprintf(tmp, sizeof(tmp), "ace[%02d]: ", i);
166 sec_io_ace(tmp, &t->ace[i], ps, depth);
171 prs_uint16_post("size ", ps, depth, &t->size , offset_acl_size, old_offset);
175 /*******************************************************************
177 ********************************************************************/
178 int make_sec_desc(SEC_DESC *t, uint16 revision, uint16 type,
179 DOM_SID *owner_sid, DOM_SID *grp_sid,
180 SEC_ACL *sacl, SEC_ACL *dacl)
184 t->revision = revision;
187 t->off_owner_sid = 0;
194 t->owner_sid = owner_sid;
195 t->grp_sid = grp_sid;
205 t->off_dacl = offset;
206 offset += dacl->size;
215 t->off_dacl = offset;
216 offset += dacl->size;
219 if (owner_sid != NULL)
225 t->off_owner_sid = offset;
226 offset += sid_size(owner_sid);
235 t->off_grp_sid = offset;
236 offset += sid_size(grp_sid);
239 return (offset == 0) ? 0x14 : offset;
243 /*******************************************************************
245 ********************************************************************/
246 void free_sec_desc(SEC_DESC *t)
250 free_sec_acl(t->dacl);
255 free_sec_acl(t->dacl);
258 if (t->owner_sid != NULL)
263 if (t->grp_sid != NULL)
270 /*******************************************************************
271 reads or writes a structure.
272 ********************************************************************/
273 static void sec_io_desc(char *desc, SEC_DESC *t, prs_struct *ps, int depth)
276 uint32 off_owner_sid;
282 uint32 max_offset = 0; /* after we're done, move offset to end */
284 if (t == NULL) return;
286 prs_debug(ps, depth, desc, "sec_io_desc");
291 /* start of security descriptor stored for back-calc offset purposes */
292 old_offset = ps->offset;
294 prs_uint16("revision ", ps, depth, &(t->revision ));
295 prs_uint16("type ", ps, depth, &(t->type ));
297 prs_uint32("off_owner_sid", ps, depth, &(t->off_owner_sid));
298 prs_uint32("off_grp_sid ", ps, depth, &(t->off_grp_sid ));
299 prs_uint32("off_sacl ", ps, depth, &(t->off_sacl ));
300 prs_uint32("off_dacl ", ps, depth, &(t->off_dacl ));
302 prs_uint32_pre("off_owner_sid", ps, depth, &(t->off_owner_sid), &off_owner_sid);
303 prs_uint32_pre("off_grp_sid ", ps, depth, &(t->off_grp_sid ), &off_grp_sid );
304 prs_uint32_pre("off_sacl ", ps, depth, &(t->off_sacl ), &off_sacl );
305 prs_uint32_pre("off_dacl ", ps, depth, &(t->off_dacl ), &off_dacl );
307 max_offset = MAX(max_offset, ps->offset);
309 if (IS_BITS_SET_ALL(t->type, SEC_DESC_DACL_PRESENT))
312 prs_uint32_post("off_dacl ", ps, depth, &(t->off_dacl ), off_dacl , ps->offset - old_offset);
314 ps->offset = old_offset + t->off_dacl;
318 t->dacl = malloc(sizeof(*t->dacl));
319 ZERO_STRUCTPN(t->dacl);
324 DEBUG(0,("INVALID DACL\n"));
325 ps->offset = 0xfffffffe;
329 sec_io_acl ("dacl" , t->dacl , ps, depth);
335 prs_uint32_post("off_dacl ", ps, depth, &(t->off_dacl ), off_dacl , 0);
339 max_offset = MAX(max_offset, ps->offset);
341 if (IS_BITS_SET_ALL(t->type, SEC_DESC_SACL_PRESENT))
344 prs_uint32_post("off_sacl ", ps, depth, &(t->off_sacl ), off_sacl , ps->offset - old_offset);
346 ps->offset = old_offset + t->off_sacl;
350 t->sacl = malloc(sizeof(*t->sacl));
351 ZERO_STRUCTPN(t->sacl);
356 DEBUG(0,("INVALID SACL\n"));
357 ps->offset = 0xfffffffe;
361 sec_io_acl ("sacl" , t->sacl , ps, depth);
367 prs_uint32_post("off_sacl ", ps, depth, &(t->off_sacl ), off_sacl , 0);
371 max_offset = MAX(max_offset, ps->offset);
374 prs_uint32_post("off_owner_sid", ps, depth, &(t->off_owner_sid), off_owner_sid, ps->offset - old_offset);
376 if (t->off_owner_sid != 0)
380 ps->offset = old_offset + t->off_owner_sid;
385 t->owner_sid = malloc(sizeof(*t->owner_sid));
386 ZERO_STRUCTPN(t->owner_sid);
389 if (t->owner_sid == NULL)
391 DEBUG(0,("INVALID OWNER SID\n"));
392 ps->offset = 0xfffffffe;
396 smb_io_dom_sid("owner_sid ", t->owner_sid , ps, depth);
400 max_offset = MAX(max_offset, ps->offset);
403 prs_uint32_post("off_grp_sid ", ps, depth, &(t->off_grp_sid ), off_grp_sid , ps->offset - old_offset);
405 if (t->off_grp_sid != 0)
409 ps->offset = old_offset + t->off_grp_sid;
414 t->grp_sid = malloc(sizeof(*t->grp_sid));
415 ZERO_STRUCTPN(t->grp_sid);
418 if (t->grp_sid == NULL)
420 DEBUG(0,("INVALID GROUP SID\n"));
421 ps->offset = 0xfffffffe;
425 smb_io_dom_sid("grp_sid", t->grp_sid, ps, depth);
429 max_offset = MAX(max_offset, ps->offset);
431 ps->offset = max_offset;
434 /*******************************************************************
435 creates a SEC_DESC_BUF structure.
436 ********************************************************************/
437 void make_sec_desc_buf(SEC_DESC_BUF *buf, int len, SEC_DESC *data)
441 /* max buffer size (allocated size) */
444 buf->len = data != NULL ? len : 0;
448 /*******************************************************************
449 frees a SEC_DESC_BUF structure.
450 ********************************************************************/
451 void free_sec_desc_buf(SEC_DESC_BUF *buf)
453 if (buf->sec != NULL)
455 free_sec_desc(buf->sec);
461 /*******************************************************************
462 reads or writes a SEC_DESC_BUF structure.
463 ********************************************************************/
464 void sec_io_desc_buf(char *desc, SEC_DESC_BUF *sec, prs_struct *ps, int depth)
471 if (sec == NULL) return;
473 prs_debug(ps, depth, desc, "sec_io_desc_buf");
478 prs_uint32_pre("max_len", ps, depth, &(sec->max_len), &off_max_len);
479 prs_uint32 ("undoc ", ps, depth, &(sec->undoc ));
480 prs_uint32_pre("len ", ps, depth, &(sec->len ), &off_len);
482 old_offset = ps->offset;
484 if (sec->len != 0 && ps->io)
487 sec->sec = malloc(sizeof(*sec->sec));
488 ZERO_STRUCTPN(sec->sec);
490 if (sec->sec == NULL)
492 DEBUG(0,("INVALID SEC_DESC\n"));
493 ps->offset = 0xfffffffe;
498 /* reading, length is non-zero; writing, descriptor is non-NULL */
499 if ((sec->len != 0 || (!ps->io)) && sec->sec != NULL)
501 sec_io_desc("sec ", sec->sec, ps, depth);
504 size = ps->offset - old_offset;
505 prs_uint32_post("max_len", ps, depth, &(sec->max_len), off_max_len, size == 0 ? sec->max_len : size);
506 prs_uint32_post("len ", ps, depth, &(sec->len ), off_len , size);