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