Use vfs_cifs as a basis for vfs_proxy
[Samba/vfs_proxy.git] / source3 / rpc_parse / parse_sec.c
blobc71b31086a4dde832c7396879ffc4a729df1d93d
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-2005.
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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_PARSE
29 /*******************************************************************
30 Reads or writes a SEC_ACE structure.
31 ********************************************************************/
33 static bool sec_io_ace(const char *desc, SEC_ACE *psa, prs_struct *ps,
34 int depth)
36 uint32 old_offset;
37 uint32 offset_ace_size;
38 uint8 type;
40 if (psa == NULL)
41 return False;
43 prs_debug(ps, depth, desc, "sec_io_ace");
44 depth++;
46 old_offset = prs_offset(ps);
48 if (MARSHALLING(ps)) {
49 type = (uint8)psa->type;
52 if(!prs_uint8("type ", ps, depth, &type))
53 return False;
55 if (UNMARSHALLING(ps)) {
56 psa->type = (enum security_ace_type)type;
59 if(!prs_uint8("flags", ps, depth, &psa->flags))
60 return False;
62 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
63 return False;
65 if(!prs_uint32("access_mask", ps, depth, &psa->access_mask))
66 return False;
68 /* check whether object access is present */
69 if (!sec_ace_object(psa->type)) {
70 if (!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
71 return False;
72 } else {
73 if (!prs_uint32("obj_flags", ps, depth, &psa->object.object.flags))
74 return False;
76 if (psa->object.object.flags & SEC_ACE_OBJECT_PRESENT)
77 if (!smb_io_uuid("obj_guid", &psa->object.object.type.type, ps,depth))
78 return False;
80 if (psa->object.object.flags & SEC_ACE_OBJECT_INHERITED_PRESENT)
81 if (!smb_io_uuid("inh_guid", &psa->object.object.inherited_type.inherited_type, ps,depth))
82 return False;
84 if(!smb_io_dom_sid("trustee ", &psa->trustee , ps, depth))
85 return False;
88 /* Theorectically an ACE can have a size greater than the
89 sum of its components. When marshalling, pad with extra null bytes up to the
90 correct size. */
92 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
93 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
94 uint32 i;
95 uint8 c = 0;
97 for (i = 0; i < extra_len; i++) {
98 if (!prs_uint8("ace extra space", ps, depth, &c))
99 return False;
103 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
104 return False;
106 return True;
109 /*******************************************************************
110 Reads or writes a SEC_ACL structure.
112 First of the xx_io_xx functions that allocates its data structures
113 for you as it reads them.
114 ********************************************************************/
116 static bool sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps,
117 int depth)
119 unsigned int i;
120 uint32 old_offset;
121 uint32 offset_acl_size;
122 SEC_ACL *psa;
123 uint16 revision;
126 * Note that the size is always a multiple of 4 bytes due to the
127 * nature of the data structure. Therefore the prs_align() calls
128 * have been removed as they through us off when doing two-layer
129 * marshalling such as in the printing code (RPC_BUFFER). --jerry
132 if (ppsa == NULL)
133 return False;
135 psa = *ppsa;
137 if(UNMARSHALLING(ps) && psa == NULL) {
139 * This is a read and we must allocate the stuct to read into.
141 if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
142 return False;
143 *ppsa = psa;
146 prs_debug(ps, depth, desc, "sec_io_acl");
147 depth++;
149 old_offset = prs_offset(ps);
151 if (MARSHALLING(ps)) {
152 revision = (uint16)psa->revision;
155 if(!prs_uint16("revision", ps, depth, &revision))
156 return False;
158 if (UNMARSHALLING(ps)) {
159 psa->revision = (enum security_acl_revision)revision;
162 if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_acl_size))
163 return False;
165 if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
166 return False;
168 if (UNMARSHALLING(ps)) {
169 if (psa->num_aces) {
170 if((psa->aces = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces)) == NULL)
171 return False;
172 } else {
173 psa->aces = NULL;
177 for (i = 0; i < psa->num_aces; i++) {
178 fstring tmp;
179 slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
180 if(!sec_io_ace(tmp, &psa->aces[i], ps, depth))
181 return False;
184 /* Theorectically an ACL can have a size greater than the
185 sum of its components. When marshalling, pad with extra null bytes up to the
186 correct size. */
188 if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
189 uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
190 uint8 c = 0;
192 for (i = 0; i < extra_len; i++) {
193 if (!prs_uint8("acl extra space", ps, depth, &c))
194 return False;
198 if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_acl_size, old_offset))
199 return False;
201 return True;
204 /*******************************************************************
205 Reads or writes a SEC_DESC structure.
206 If reading and the *ppsd = NULL, allocates the structure.
207 ********************************************************************/
209 bool sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
211 uint32 old_offset;
212 uint32 max_offset = 0; /* after we're done, move offset to end */
213 uint32 tmp_offset = 0;
214 uint32 off_sacl, off_dacl, off_owner_sid, off_grp_sid;
215 uint16 revision;
217 SEC_DESC *psd;
219 if (ppsd == NULL)
220 return False;
222 psd = *ppsd;
224 if (psd == NULL) {
225 if(UNMARSHALLING(ps)) {
226 if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
227 return False;
228 *ppsd = psd;
229 } else {
230 /* Marshalling - just ignore. */
231 return True;
235 prs_debug(ps, depth, desc, "sec_io_desc");
236 depth++;
238 /* start of security descriptor stored for back-calc offset purposes */
239 old_offset = prs_offset(ps);
241 if (MARSHALLING(ps)) {
242 revision = (uint16)psd->revision;
245 if(!prs_uint16("revision", ps, depth, &revision))
246 return False;
248 if (UNMARSHALLING(ps)) {
249 psd->revision = (enum security_descriptor_revision)revision;
252 if(!prs_uint16("type ", ps, depth, &psd->type))
253 return False;
255 if (MARSHALLING(ps)) {
256 uint32 offset = SEC_DESC_HEADER_SIZE;
259 * Work out the offsets here, as we write it out.
262 if (psd->sacl != NULL) {
263 off_sacl = offset;
264 offset += psd->sacl->size;
265 } else {
266 off_sacl = 0;
269 if (psd->dacl != NULL) {
270 off_dacl = offset;
271 offset += psd->dacl->size;
272 } else {
273 off_dacl = 0;
276 if (psd->owner_sid != NULL) {
277 off_owner_sid = offset;
278 offset += ndr_size_dom_sid(psd->owner_sid, 0);
279 } else {
280 off_owner_sid = 0;
283 if (psd->group_sid != NULL) {
284 off_grp_sid = offset;
285 offset += ndr_size_dom_sid(psd->group_sid, 0);
286 } else {
287 off_grp_sid = 0;
291 if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
292 return False;
294 if(!prs_uint32("off_grp_sid ", ps, depth, &off_grp_sid))
295 return False;
297 if(!prs_uint32("off_sacl ", ps, depth, &off_sacl))
298 return False;
300 if(!prs_uint32("off_dacl ", ps, depth, &off_dacl))
301 return False;
303 max_offset = MAX(max_offset, prs_offset(ps));
305 if (off_owner_sid != 0) {
307 tmp_offset = prs_offset(ps);
308 if(!prs_set_offset(ps, old_offset + off_owner_sid))
309 return False;
311 if (UNMARSHALLING(ps)) {
312 /* reading */
313 if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
314 return False;
317 if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
318 return False;
320 max_offset = MAX(max_offset, prs_offset(ps));
322 if (!prs_set_offset(ps,tmp_offset))
323 return False;
326 if (psd->group_sid != 0) {
328 tmp_offset = prs_offset(ps);
329 if(!prs_set_offset(ps, old_offset + off_grp_sid))
330 return False;
332 if (UNMARSHALLING(ps)) {
333 /* reading */
334 if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
335 return False;
338 if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
339 return False;
341 max_offset = MAX(max_offset, prs_offset(ps));
343 if (!prs_set_offset(ps,tmp_offset))
344 return False;
347 if ((psd->type & SEC_DESC_SACL_PRESENT) && off_sacl) {
348 tmp_offset = prs_offset(ps);
349 if(!prs_set_offset(ps, old_offset + off_sacl))
350 return False;
351 if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
352 return False;
353 max_offset = MAX(max_offset, prs_offset(ps));
354 if (!prs_set_offset(ps,tmp_offset))
355 return False;
358 if ((psd->type & SEC_DESC_DACL_PRESENT) && off_dacl != 0) {
359 tmp_offset = prs_offset(ps);
360 if(!prs_set_offset(ps, old_offset + off_dacl))
361 return False;
362 if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
363 return False;
364 max_offset = MAX(max_offset, prs_offset(ps));
365 if (!prs_set_offset(ps,tmp_offset))
366 return False;
369 if(!prs_set_offset(ps, max_offset))
370 return False;
372 return True;
375 /*******************************************************************
376 Reads or writes a SEC_DESC_BUF structure.
377 ********************************************************************/
379 bool sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
381 uint32 off_len;
382 uint32 off_max_len;
383 uint32 old_offset;
384 uint32 size;
385 uint32 len;
386 SEC_DESC_BUF *psdb;
387 uint32 ptr;
389 if (ppsdb == NULL)
390 return False;
392 psdb = *ppsdb;
394 if (UNMARSHALLING(ps) && psdb == NULL) {
395 if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
396 return False;
397 *ppsdb = psdb;
400 prs_debug(ps, depth, desc, "sec_io_desc_buf");
401 depth++;
403 if(!prs_align(ps))
404 return False;
406 if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
407 return False;
409 ptr = 1;
410 if(!prs_uint32 ("ptr ", ps, depth, &ptr))
411 return False;
413 len = ndr_size_security_descriptor(psdb->sd, 0);
414 if(!prs_uint32_pre("len ", ps, depth, &len, &off_len))
415 return False;
417 old_offset = prs_offset(ps);
419 /* reading, length is non-zero; writing, descriptor is non-NULL */
420 if ((UNMARSHALLING(ps) && psdb->sd_size != 0) || (MARSHALLING(ps) && psdb->sd != NULL)) {
421 if(!sec_io_desc("sec ", &psdb->sd, ps, depth))
422 return False;
425 if(!prs_align(ps))
426 return False;
428 size = prs_offset(ps) - old_offset;
429 if(!prs_uint32_post("max_len", ps, depth, &psdb->sd_size, off_max_len, size == 0 ? psdb->sd_size : size))
430 return False;
432 if(!prs_uint32_post("len ", ps, depth, &len, off_len, size))
433 return False;
435 return True;