s3:winbindd: use the new sid_check_is_for_passdb() in idmap_find_domain_with_sid()
[kai/samba.git] / librpc / ndr / ndr_sec_helper.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    fast routines for getting the wire size of security objects
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan Metzmacher 2006-2008
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "../libcli/security/security.h"
27
28 /*
29   return the wire size of a security_ace
30 */
31 size_t ndr_size_security_ace(const struct security_ace *ace, int flags)
32 {
33         size_t ret;
34
35         if (!ace) return 0;
36
37         ret = 8 + ndr_size_dom_sid(&ace->trustee, flags);
38
39         switch (ace->type) {
40         case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
41         case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
42         case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
43         case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
44                 ret += 4; /* uint32 bitmap ace->object.object.flags */
45                 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
46                         ret += 16; /* GUID ace->object.object.type.type */
47                 }
48                 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
49                         ret += 16; /* GUID ace->object.object.inherited_typeinherited_type */
50                 }
51                 break;
52         default:
53                 break;
54         }
55
56         return ret;
57 }
58
59 enum ndr_err_code ndr_pull_security_ace(struct ndr_pull *ndr, int ndr_flags, struct security_ace *r)
60 {
61         if (ndr_flags & NDR_SCALARS) {
62                 uint32_t start_ofs = ndr->offset;
63                 uint32_t size = 0;
64                 uint32_t pad = 0;
65                 NDR_CHECK(ndr_pull_align(ndr, 4));
66                 NDR_CHECK(ndr_pull_security_ace_type(ndr, NDR_SCALARS, &r->type));
67                 NDR_CHECK(ndr_pull_security_ace_flags(ndr, NDR_SCALARS, &r->flags));
68                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->size));
69                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->access_mask));
70                 NDR_CHECK(ndr_pull_set_switch_value(ndr, &r->object, r->type));
71                 NDR_CHECK(ndr_pull_security_ace_object_ctr(ndr, NDR_SCALARS, &r->object));
72                 NDR_CHECK(ndr_pull_dom_sid(ndr, NDR_SCALARS, &r->trustee));
73                 size = ndr->offset - start_ofs;
74                 if (r->size < size) {
75                         return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
76                                               "ndr_pull_security_ace: r->size %u < size %u",
77                                               (unsigned)r->size, size);
78                 }
79                 pad = r->size - size;
80                 NDR_PULL_NEED_BYTES(ndr, pad);
81                 ndr->offset += pad;
82         }
83         if (ndr_flags & NDR_BUFFERS) {
84                 NDR_CHECK(ndr_pull_security_ace_object_ctr(ndr, NDR_BUFFERS, &r->object));
85         }
86         return NDR_ERR_SUCCESS;
87 }
88
89 /*
90   return the wire size of a security_acl
91 */
92 size_t ndr_size_security_acl(const struct security_acl *theacl, int flags)
93 {
94         size_t ret;
95         int i;
96         if (!theacl) return 0;
97         ret = 8;
98         for (i=0;i<theacl->num_aces;i++) {
99                 ret += ndr_size_security_ace(&theacl->aces[i], flags);
100         }
101         return ret;
102 }
103
104 /*
105   return the wire size of a security descriptor
106 */
107 size_t ndr_size_security_descriptor(const struct security_descriptor *sd, int flags)
108 {
109         size_t ret;
110         if (!sd) return 0;
111         
112         ret = 20;
113         ret += ndr_size_dom_sid(sd->owner_sid, flags);
114         ret += ndr_size_dom_sid(sd->group_sid, flags);
115         ret += ndr_size_security_acl(sd->dacl, flags);
116         ret += ndr_size_security_acl(sd->sacl, flags);
117         return ret;
118 }
119
120 /*
121   return the wire size of a dom_sid
122 */
123 size_t ndr_size_dom_sid(const struct dom_sid *sid, int flags)
124 {
125         if (!sid) return 0;
126         return 8 + 4*sid->num_auths;
127 }
128
129 size_t ndr_size_dom_sid28(const struct dom_sid *sid, int flags)
130 {
131         struct dom_sid zero_sid;
132
133         if (!sid) return 0;
134
135         ZERO_STRUCT(zero_sid);
136
137         if (memcmp(&zero_sid, sid, sizeof(zero_sid)) == 0) {
138                 return 0;
139         }
140
141         return 8 + 4*sid->num_auths;
142 }
143
144 size_t ndr_size_dom_sid0(const struct dom_sid *sid, int flags)
145 {
146         return ndr_size_dom_sid28(sid, flags);
147 }
148
149 /*
150   print a dom_sid
151 */
152 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
153 {
154         ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
155 }
156
157 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
158 {
159         ndr_print_dom_sid(ndr, name, sid);
160 }
161
162 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
163 {
164         ndr_print_dom_sid(ndr, name, sid);
165 }
166
167 void ndr_print_dom_sid0(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
168 {
169         ndr_print_dom_sid(ndr, name, sid);
170 }
171
172
173 /*
174   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
175 */
176 enum ndr_err_code ndr_pull_dom_sid2(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
177 {
178         uint32_t num_auths;
179         if (!(ndr_flags & NDR_SCALARS)) {
180                 return NDR_ERR_SUCCESS;
181         }
182         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &num_auths));
183         NDR_CHECK(ndr_pull_dom_sid(ndr, ndr_flags, sid));
184         if (sid->num_auths != num_auths) {
185                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
186                                       "Bad array size %u should exceed %u", 
187                                       num_auths, sid->num_auths);
188         }
189         return NDR_ERR_SUCCESS;
190 }
191
192 /*
193   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
194 */
195 enum ndr_err_code ndr_push_dom_sid2(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
196 {
197         if (!(ndr_flags & NDR_SCALARS)) {
198                 return NDR_ERR_SUCCESS;
199         }
200         NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, sid->num_auths));
201         return ndr_push_dom_sid(ndr, ndr_flags, sid);
202 }
203
204 /*
205   parse a dom_sid28 - this is a dom_sid in a fixed 28 byte buffer, so we need to ensure there are only upto 5 sub_auth
206 */
207 enum ndr_err_code ndr_pull_dom_sid28(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
208 {
209         enum ndr_err_code status;
210         struct ndr_pull *subndr;
211
212         if (!(ndr_flags & NDR_SCALARS)) {
213                 return NDR_ERR_SUCCESS;
214         }
215
216         subndr = talloc_zero(ndr, struct ndr_pull);
217         NDR_ERR_HAVE_NO_MEMORY(subndr);
218         subndr->flags           = ndr->flags;
219         subndr->current_mem_ctx = ndr->current_mem_ctx;
220
221         subndr->data            = ndr->data + ndr->offset;
222         subndr->data_size       = 28;
223         subndr->offset          = 0;
224
225         NDR_CHECK(ndr_pull_advance(ndr, 28));
226
227         status = ndr_pull_dom_sid(subndr, ndr_flags, sid);
228         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
229                 /* handle a w2k bug which send random data in the buffer */
230                 ZERO_STRUCTP(sid);
231         } else if (sid->num_auths == 0 && sid->sub_auths) {
232                 ZERO_STRUCT(sid->sub_auths);
233         }
234
235         return NDR_ERR_SUCCESS;
236 }
237
238 /*
239   push a dom_sid28 - this is a dom_sid in a 28 byte fixed buffer
240 */
241 enum ndr_err_code ndr_push_dom_sid28(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
242 {
243         uint32_t old_offset;
244         uint32_t padding;
245
246         if (!(ndr_flags & NDR_SCALARS)) {
247                 return NDR_ERR_SUCCESS;
248         }
249
250         if (sid->num_auths > 5) {
251                 return ndr_push_error(ndr, NDR_ERR_RANGE, 
252                                       "dom_sid28 allows only upto 5 sub auth [%u]", 
253                                       sid->num_auths);
254         }
255
256         old_offset = ndr->offset;
257         NDR_CHECK(ndr_push_dom_sid(ndr, ndr_flags, sid));
258
259         padding = 28 - (ndr->offset - old_offset);
260
261         if (padding > 0) {
262                 NDR_CHECK(ndr_push_zero(ndr, padding));
263         }
264
265         return NDR_ERR_SUCCESS;
266 }
267
268 /*
269   parse a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
270 */
271 enum ndr_err_code ndr_pull_dom_sid0(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
272 {
273         if (!(ndr_flags & NDR_SCALARS)) {
274                 return NDR_ERR_SUCCESS;
275         }
276
277         if (ndr->data_size == ndr->offset) {
278                 ZERO_STRUCTP(sid);
279                 return NDR_ERR_SUCCESS;
280         }
281
282         return ndr_pull_dom_sid(ndr, ndr_flags, sid);
283 }
284
285 /*
286   push a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
287 */
288 enum ndr_err_code ndr_push_dom_sid0(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
289 {
290         struct dom_sid zero_sid;
291
292         if (!(ndr_flags & NDR_SCALARS)) {
293                 return NDR_ERR_SUCCESS;
294         }
295
296         if (!sid) {
297                 return NDR_ERR_SUCCESS;
298         }
299
300         ZERO_STRUCT(zero_sid);
301
302         if (memcmp(&zero_sid, sid, sizeof(zero_sid)) == 0) {
303                 return NDR_ERR_SUCCESS;
304         }
305
306         return ndr_push_dom_sid(ndr, ndr_flags, sid);
307 }
308
309 _PUBLIC_ enum ndr_err_code ndr_push_dom_sid(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *r)
310 {
311         uint32_t cntr_sub_auths_0;
312         if (ndr_flags & NDR_SCALARS) {
313                 NDR_CHECK(ndr_push_align(ndr, 4));
314                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, r->sid_rev_num));
315                 NDR_CHECK(ndr_push_int8(ndr, NDR_SCALARS, r->num_auths));
316                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
317                 if (r->num_auths < 0 || r->num_auths > ARRAY_SIZE(r->sub_auths)) {
318                         return ndr_push_error(ndr, NDR_ERR_RANGE, "value out of range");
319                 }
320                 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
321                         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->sub_auths[cntr_sub_auths_0]));
322                 }
323         }
324         return NDR_ERR_SUCCESS;
325 }
326
327 _PUBLIC_ enum ndr_err_code ndr_pull_dom_sid(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *r)
328 {
329         uint32_t cntr_sub_auths_0;
330         if (ndr_flags & NDR_SCALARS) {
331                 NDR_CHECK(ndr_pull_align(ndr, 4));
332                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &r->sid_rev_num));
333                 NDR_CHECK(ndr_pull_int8(ndr, NDR_SCALARS, &r->num_auths));
334                 if (r->num_auths < 0 || r->num_auths > ARRAY_SIZE(r->sub_auths)) {
335                         return ndr_pull_error(ndr, NDR_ERR_RANGE, "value out of range");
336                 }
337                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
338                 ZERO_STRUCT(r->sub_auths);
339                 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
340                         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->sub_auths[cntr_sub_auths_0]));
341                 }
342         }
343         return NDR_ERR_SUCCESS;
344 }