r14542: Remove librpc, libndr and libnbt from includes.h
[kai/samba-autobuild/.git] / source4 / libcli / security / security_descriptor.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    security descriptror utility functions
5
6    Copyright (C) Andrew Tridgell                2004
7       
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/security.h"
25 #include "libcli/security/proto.h"
26
27 /*
28   return a blank security descriptor (no owners, dacl or sacl)
29 */
30 struct security_descriptor *security_descriptor_initialise(TALLOC_CTX *mem_ctx)
31 {
32         struct security_descriptor *sd;
33
34         sd = talloc(mem_ctx, struct security_descriptor);
35         if (!sd) {
36                 return NULL;
37         }
38
39         sd->revision = SD_REVISION;
40         /* we mark as self relative, even though it isn't while it remains
41            a pointer in memory because this simplifies the ndr code later.
42            All SDs that we store/emit are in fact SELF_RELATIVE
43         */
44         sd->type = SEC_DESC_SELF_RELATIVE;
45
46         sd->owner_sid = NULL;
47         sd->group_sid = NULL;
48         sd->sacl = NULL;
49         sd->dacl = NULL;
50
51         return sd;
52 }
53
54 static struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
55                                              const struct security_acl *oacl)
56 {
57         struct security_acl *nacl;
58         int i;
59
60         nacl = talloc (mem_ctx, struct security_acl);
61         if (nacl == NULL) {
62                 return NULL;
63         }
64
65         nacl->aces = talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
66         if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
67                 goto failed;
68         }
69
70         /* remapping array in trustee dom_sid from old acl to new acl */
71
72         for (i = 0; i < oacl->num_aces; i++) {
73                 nacl->aces[i].trustee.sub_auths = 
74                         talloc_memdup(nacl->aces, nacl->aces[i].trustee.sub_auths,
75                                       sizeof(uint32_t) * nacl->aces[i].trustee.num_auths);
76
77                 if ((nacl->aces[i].trustee.sub_auths == NULL) && (nacl->aces[i].trustee.num_auths > 0)) {
78                         goto failed;
79                 }
80         }
81
82         nacl->revision = oacl->revision;
83         nacl->size = oacl->size;
84         nacl->num_aces = oacl->num_aces;
85         
86         return nacl;
87
88  failed:
89         talloc_free (nacl);
90         return NULL;
91         
92 }
93
94 /* 
95    talloc and copy a security descriptor
96  */
97 struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx, 
98                                                      const struct security_descriptor *osd)
99 {
100         struct security_descriptor *nsd;
101
102         nsd = talloc_zero(mem_ctx, struct security_descriptor);
103         if (!nsd) {
104                 return NULL;
105         }
106
107         if (osd->owner_sid) {
108                 nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
109                 if (nsd->owner_sid == NULL) {
110                         goto failed;
111                 }
112         }
113         
114         if (osd->group_sid) {
115                 nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
116                 if (nsd->group_sid == NULL) {
117                         goto failed;
118                 }
119         }
120
121         if (osd->sacl) {
122                 nsd->sacl = security_acl_dup(nsd, osd->sacl);
123                 if (nsd->sacl == NULL) {
124                         goto failed;
125                 }
126         }
127
128         if (osd->dacl) {
129                 nsd->dacl = security_acl_dup(nsd, osd->dacl);
130                 if (nsd->dacl == NULL) {
131                         goto failed;
132                 }
133         }
134
135         return nsd;
136
137  failed:
138         talloc_free(nsd);
139
140         return NULL;
141 }
142
143 /*
144   add an ACE to the DACL of a security_descriptor
145 */
146 NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd, 
147                                       const struct security_ace *ace)
148 {
149         if (sd->dacl == NULL) {
150                 sd->dacl = talloc(sd, struct security_acl);
151                 if (sd->dacl == NULL) {
152                         return NT_STATUS_NO_MEMORY;
153                 }
154                 sd->dacl->revision = NT4_ACL_REVISION;
155                 sd->dacl->size     = 0;
156                 sd->dacl->num_aces = 0;
157                 sd->dacl->aces     = NULL;
158         }
159
160         sd->dacl->aces = talloc_realloc(sd->dacl, sd->dacl->aces, 
161                                           struct security_ace, sd->dacl->num_aces+1);
162         if (sd->dacl->aces == NULL) {
163                 return NT_STATUS_NO_MEMORY;
164         }
165
166         sd->dacl->aces[sd->dacl->num_aces] = *ace;
167         sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths = 
168                 talloc_memdup(sd->dacl->aces, 
169                               sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths,
170                               sizeof(uint32_t) * 
171                               sd->dacl->aces[sd->dacl->num_aces].trustee.num_auths);
172         if (sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths == NULL) {
173                 return NT_STATUS_NO_MEMORY;
174         }
175         
176         sd->dacl->num_aces++;
177
178         sd->type |= SEC_DESC_DACL_PRESENT;
179
180         return NT_STATUS_OK;
181 }
182
183
184 /*
185   delete the ACE corresponding to the given trustee in the DACL of a security_descriptor
186 */
187 NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd, 
188                                       struct dom_sid *trustee)
189 {
190         int i;
191
192         if (sd->dacl == NULL) {
193                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
194         }
195         
196         for (i=0;i<sd->dacl->num_aces;i++) {
197                 if (dom_sid_equal(trustee, &sd->dacl->aces[i].trustee)) {
198                         memmove(&sd->dacl->aces[i], &sd->dacl->aces[i+1],
199                                 sizeof(sd->dacl->aces[i]) * (sd->dacl->num_aces - (i+1)));
200                         sd->dacl->num_aces--;
201                         if (sd->dacl->num_aces == 0) {
202                                 sd->dacl->aces = NULL;
203                         }
204                         return NT_STATUS_OK;
205                 }
206         }
207         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
208 }
209
210
211 /*
212   compare two security ace structures
213 */
214 BOOL security_ace_equal(const struct security_ace *ace1, 
215                         const struct security_ace *ace2)
216 {
217         if (ace1 == ace2) return True;
218         if (!ace1 || !ace2) return False;
219         if (ace1->type != ace2->type) return False;
220         if (ace1->flags != ace2->flags) return False;
221         if (ace1->access_mask != ace2->access_mask) return False;
222         if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) return False;
223
224         return True;    
225 }
226
227
228 /*
229   compare two security acl structures
230 */
231 BOOL security_acl_equal(const struct security_acl *acl1, 
232                         const struct security_acl *acl2)
233 {
234         int i;
235
236         if (acl1 == acl2) return True;
237         if (!acl1 || !acl2) return False;
238         if (acl1->revision != acl2->revision) return False;
239         if (acl1->num_aces != acl2->num_aces) return False;
240
241         for (i=0;i<acl1->num_aces;i++) {
242                 if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return False;
243         }
244         return True;    
245 }
246
247 /*
248   compare two security descriptors.
249 */
250 BOOL security_descriptor_equal(const struct security_descriptor *sd1, 
251                                const struct security_descriptor *sd2)
252 {
253         if (sd1 == sd2) return True;
254         if (!sd1 || !sd2) return False;
255         if (sd1->revision != sd2->revision) return False;
256         if (sd1->type != sd2->type) return False;
257
258         if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
259         if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
260         if (!security_acl_equal(sd1->sacl, sd2->sacl))      return False;
261         if (!security_acl_equal(sd1->dacl, sd2->dacl))      return False;
262
263         return True;    
264 }
265
266 /*
267   compare two security descriptors, but allow certain (missing) parts
268   to be masked out of the comparison
269 */
270 BOOL security_descriptor_mask_equal(const struct security_descriptor *sd1, 
271                                     const struct security_descriptor *sd2, 
272                                     uint32_t mask)
273 {
274         if (sd1 == sd2) return True;
275         if (!sd1 || !sd2) return False;
276         if (sd1->revision != sd2->revision) return False;
277         if ((sd1->type & mask) != (sd2->type & mask)) return False;
278
279         if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
280         if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
281         if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl))      return False;
282         if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl))      return False;
283
284         return True;    
285 }
286
287
288 /*
289   create a security descriptor using string SIDs. This is used by the
290   torture code to allow the easy creation of complex ACLs
291   This is a varargs function. The list of DACL ACEs ends with a NULL sid.
292
293   Each ACE contains a set of 4 parameters:
294   SID, ACCESS_TYPE, MASK, FLAGS
295
296   a typical call would be:
297
298     sd = security_descriptor_create(mem_ctx,
299                                     mysid,
300                                     mygroup,
301                                     SID_NT_AUTHENTICATED_USERS, 
302                                     SEC_ACE_TYPE_ACCESS_ALLOWED,
303                                     SEC_FILE_ALL,
304                                     SEC_ACE_FLAG_OBJECT_INHERIT,
305                                     NULL);
306   that would create a sd with one DACL ACE
307 */
308 struct security_descriptor *security_descriptor_create(TALLOC_CTX *mem_ctx,
309                                                        const char *owner_sid,
310                                                        const char *group_sid,
311                                                        ...)
312 {
313         va_list ap;
314         struct security_descriptor *sd;
315         const char *sidstr;
316
317         sd = security_descriptor_initialise(mem_ctx);
318         if (sd == NULL) return NULL;
319
320         if (owner_sid) {
321                 sd->owner_sid = dom_sid_parse_talloc(sd, owner_sid);
322                 if (sd->owner_sid == NULL) {
323                         talloc_free(sd);
324                         return NULL;
325                 }
326         }
327         if (group_sid) {
328                 sd->group_sid = dom_sid_parse_talloc(sd, group_sid);
329                 if (sd->group_sid == NULL) {
330                         talloc_free(sd);
331                         return NULL;
332                 }
333         }
334
335         va_start(ap, group_sid);
336         while ((sidstr = va_arg(ap, const char *))) {
337                 struct dom_sid *sid;
338                 struct security_ace *ace = talloc(sd, struct security_ace);
339                 NTSTATUS status;
340
341                 if (ace == NULL) {
342                         talloc_free(sd);
343                         va_end(ap);
344                         return NULL;
345                 }
346                 ace->type = va_arg(ap, unsigned int);
347                 ace->access_mask = va_arg(ap, unsigned int);
348                 ace->flags = va_arg(ap, unsigned int);
349                 sid = dom_sid_parse_talloc(ace, sidstr);
350                 if (sid == NULL) {
351                         va_end(ap);
352                         talloc_free(sd);
353                         return NULL;
354                 }
355                 ace->trustee = *sid;
356                 status = security_descriptor_dacl_add(sd, ace);
357                 /* TODO: check: would talloc_free(ace) here be correct? */
358                 if (!NT_STATUS_IS_OK(status)) {
359                         va_end(ap);
360                         talloc_free(sd);
361                         return NULL;
362                 }
363         }
364         va_end(ap);
365
366         return sd;
367 }