a75942c077069fb3fcfd7f77f33e975d5686ab14
[sfrench/samba-autobuild/.git] / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/security/security.h"
24
25 /*
26   return a blank security descriptor (no owners, dacl or sacl)
27 */
28 struct security_descriptor *security_descriptor_initialise(TALLOC_CTX *mem_ctx)
29 {
30         struct security_descriptor *sd;
31
32         sd = talloc(mem_ctx, struct security_descriptor);
33         if (!sd) {
34                 return NULL;
35         }
36
37         sd->revision = SD_REVISION;
38         /* we mark as self relative, even though it isn't while it remains
39            a pointer in memory because this simplifies the ndr code later.
40            All SDs that we store/emit are in fact SELF_RELATIVE
41         */
42         sd->type = SEC_DESC_SELF_RELATIVE;
43
44         sd->owner_sid = NULL;
45         sd->group_sid = NULL;
46         sd->sacl = NULL;
47         sd->dacl = NULL;
48
49         return sd;
50 }
51
52 struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
53                                              const struct security_acl *oacl)
54 {
55         struct security_acl *nacl;
56
57         if (oacl == NULL) {
58                 return NULL;
59         }
60
61         nacl = talloc (mem_ctx, struct security_acl);
62         if (nacl == NULL) {
63                 return NULL;
64         }
65
66         nacl->aces = (struct security_ace *)talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
67         if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
68                 goto failed;
69         }
70
71         nacl->revision = oacl->revision;
72         nacl->size = oacl->size;
73         nacl->num_aces = oacl->num_aces;
74         
75         return nacl;
76
77  failed:
78         talloc_free (nacl);
79         return NULL;
80         
81 }
82
83 struct security_acl *security_acl_concatenate(TALLOC_CTX *mem_ctx,
84                                               const struct security_acl *acl1,
85                                               const struct security_acl *acl2)
86 {
87         struct security_acl *nacl;
88         uint32_t i;
89
90         if (!acl1 && !acl2)
91                 return NULL;
92
93         if (!acl1){
94                 nacl = security_acl_dup(mem_ctx, acl2);
95                 return nacl;
96         }
97
98         if (!acl2){
99                 nacl = security_acl_dup(mem_ctx, acl1);
100                 return nacl;
101         }
102
103         nacl = talloc (mem_ctx, struct security_acl);
104         if (nacl == NULL) {
105                 return NULL;
106         }
107
108         nacl->revision = acl1->revision;
109         nacl->size = acl1->size + acl2->size;
110         nacl->num_aces = acl1->num_aces + acl2->num_aces;
111
112         if (nacl->num_aces == 0)
113                 return nacl;
114
115         nacl->aces = (struct security_ace *)talloc_array (mem_ctx, struct security_ace, acl1->num_aces+acl2->num_aces);
116         if ((nacl->aces == NULL) && (nacl->num_aces > 0)) {
117                 goto failed;
118         }
119
120         for (i = 0; i < acl1->num_aces; i++)
121                 nacl->aces[i] = acl1->aces[i];
122         for (i = 0; i < acl2->num_aces; i++)
123                 nacl->aces[i + acl1->num_aces] = acl2->aces[i];
124
125         return nacl;
126
127  failed:
128         talloc_free (nacl);
129         return NULL;
130
131 }
132
133 /* 
134    talloc and copy a security descriptor
135  */
136 struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx, 
137                                                      const struct security_descriptor *osd)
138 {
139         struct security_descriptor *nsd;
140
141         nsd = talloc_zero(mem_ctx, struct security_descriptor);
142         if (!nsd) {
143                 return NULL;
144         }
145
146         if (osd->owner_sid) {
147                 nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
148                 if (nsd->owner_sid == NULL) {
149                         goto failed;
150                 }
151         }
152         
153         if (osd->group_sid) {
154                 nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
155                 if (nsd->group_sid == NULL) {
156                         goto failed;
157                 }
158         }
159
160         if (osd->sacl) {
161                 nsd->sacl = security_acl_dup(nsd, osd->sacl);
162                 if (nsd->sacl == NULL) {
163                         goto failed;
164                 }
165         }
166
167         if (osd->dacl) {
168                 nsd->dacl = security_acl_dup(nsd, osd->dacl);
169                 if (nsd->dacl == NULL) {
170                         goto failed;
171                 }
172         }
173
174         nsd->revision = osd->revision;
175         nsd->type = osd->type;
176
177         return nsd;
178
179  failed:
180         talloc_free(nsd);
181
182         return NULL;
183 }
184
185 /*
186   add an ACE to an ACL of a security_descriptor
187 */
188
189 static NTSTATUS security_descriptor_acl_add(struct security_descriptor *sd,
190                                             bool add_to_sacl,
191                                             const struct security_ace *ace)
192 {
193         struct security_acl *acl = NULL;
194
195         if (add_to_sacl) {
196                 acl = sd->sacl;
197         } else {
198                 acl = sd->dacl;
199         }
200
201         if (acl == NULL) {
202                 acl = talloc(sd, struct security_acl);
203                 if (acl == NULL) {
204                         return NT_STATUS_NO_MEMORY;
205                 }
206                 acl->revision = SECURITY_ACL_REVISION_NT4;
207                 acl->size     = 0;
208                 acl->num_aces = 0;
209                 acl->aces     = NULL;
210         }
211
212         acl->aces = talloc_realloc(acl, acl->aces,
213                                    struct security_ace, acl->num_aces+1);
214         if (acl->aces == NULL) {
215                 return NT_STATUS_NO_MEMORY;
216         }
217
218         acl->aces[acl->num_aces] = *ace;
219
220         switch (acl->aces[acl->num_aces].type) {
221         case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
222         case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
223         case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
224         case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
225                 acl->revision = SECURITY_ACL_REVISION_ADS;
226                 break;
227         default:
228                 break;
229         }
230
231         acl->num_aces++;
232
233         if (add_to_sacl) {
234                 sd->sacl = acl;
235                 sd->type |= SEC_DESC_SACL_PRESENT;
236         } else {
237                 sd->dacl = acl;
238                 sd->type |= SEC_DESC_DACL_PRESENT;
239         }
240
241         return NT_STATUS_OK;
242 }
243
244 /*
245   add an ACE to the SACL of a security_descriptor
246 */
247
248 NTSTATUS security_descriptor_sacl_add(struct security_descriptor *sd,
249                                       const struct security_ace *ace)
250 {
251         return security_descriptor_acl_add(sd, true, ace);
252 }
253
254 /*
255   add an ACE to the DACL of a security_descriptor
256 */
257
258 NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd,
259                                       const struct security_ace *ace)
260 {
261         return security_descriptor_acl_add(sd, false, ace);
262 }
263
264 /*
265   delete the ACE corresponding to the given trustee in an ACL of a
266   security_descriptor
267 */
268
269 static NTSTATUS security_descriptor_acl_del(struct security_descriptor *sd,
270                                             bool sacl_del,
271                                             const struct dom_sid *trustee)
272 {
273         uint32_t i;
274         bool found = false;
275         struct security_acl *acl = NULL;
276
277         if (sacl_del) {
278                 acl = sd->sacl;
279         } else {
280                 acl = sd->dacl;
281         }
282
283         if (acl == NULL) {
284                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
285         }
286
287         /* there can be multiple ace's for one trustee */
288         for (i=0;i<acl->num_aces;i++) {
289                 if (dom_sid_equal(trustee, &acl->aces[i].trustee)) {
290                         memmove(&acl->aces[i], &acl->aces[i+1],
291                                 sizeof(acl->aces[i]) * (acl->num_aces - (i+1)));
292                         acl->num_aces--;
293                         if (acl->num_aces == 0) {
294                                 acl->aces = NULL;
295                         }
296                         found = true;
297                 }
298         }
299
300         if (!found) {
301                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
302         }
303
304         acl->revision = SECURITY_ACL_REVISION_NT4;
305
306         for (i=0;i<acl->num_aces;i++) {
307                 switch (acl->aces[i].type) {
308                 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
309                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
310                 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
311                 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
312                         acl->revision = SECURITY_ACL_REVISION_ADS;
313                         return NT_STATUS_OK;
314                 default:
315                         break; /* only for the switch statement */
316                 }
317         }
318
319         return NT_STATUS_OK;
320 }
321
322 /*
323   delete the ACE corresponding to the given trustee in the DACL of a
324   security_descriptor
325 */
326
327 NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd,
328                                       const struct dom_sid *trustee)
329 {
330         return security_descriptor_acl_del(sd, false, trustee);
331 }
332
333 /*
334   delete the ACE corresponding to the given trustee in the SACL of a
335   security_descriptor
336 */
337
338 NTSTATUS security_descriptor_sacl_del(struct security_descriptor *sd,
339                                       const struct dom_sid *trustee)
340 {
341         return security_descriptor_acl_del(sd, true, trustee);
342 }
343
344 /*
345   compare two security ace structures
346 */
347 bool security_ace_equal(const struct security_ace *ace1,
348                         const struct security_ace *ace2)
349 {
350         if (ace1 == ace2) {
351                 return true;
352         }
353         if ((ace1 == NULL) || (ace2 == NULL)) {
354                 return false;
355         }
356         if (ace1->type != ace2->type) {
357                 return false;
358         }
359         if (ace1->flags != ace2->flags) {
360                 return false;
361         }
362         if (ace1->access_mask != ace2->access_mask) {
363                 return false;
364         }
365         if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) {
366                 return false;
367         }
368
369         return true;
370 }
371
372
373 /*
374   compare two security acl structures
375 */
376 bool security_acl_equal(const struct security_acl *acl1, 
377                         const struct security_acl *acl2)
378 {
379         uint32_t i;
380
381         if (acl1 == acl2) return true;
382         if (!acl1 || !acl2) return false;
383         if (acl1->revision != acl2->revision) return false;
384         if (acl1->num_aces != acl2->num_aces) return false;
385
386         for (i=0;i<acl1->num_aces;i++) {
387                 if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return false;
388         }
389         return true;    
390 }
391
392 /*
393   compare two security descriptors.
394 */
395 bool security_descriptor_equal(const struct security_descriptor *sd1, 
396                                const struct security_descriptor *sd2)
397 {
398         if (sd1 == sd2) return true;
399         if (!sd1 || !sd2) return false;
400         if (sd1->revision != sd2->revision) return false;
401         if (sd1->type != sd2->type) return false;
402
403         if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return false;
404         if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return false;
405         if (!security_acl_equal(sd1->sacl, sd2->sacl))      return false;
406         if (!security_acl_equal(sd1->dacl, sd2->dacl))      return false;
407
408         return true;    
409 }
410
411 /*
412   compare two security descriptors, but allow certain (missing) parts
413   to be masked out of the comparison
414 */
415 bool security_descriptor_mask_equal(const struct security_descriptor *sd1, 
416                                     const struct security_descriptor *sd2, 
417                                     uint32_t mask)
418 {
419         if (sd1 == sd2) return true;
420         if (!sd1 || !sd2) return false;
421         if (sd1->revision != sd2->revision) return false;
422         if ((sd1->type & mask) != (sd2->type & mask)) return false;
423
424         if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return false;
425         if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return false;
426         if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl))      return false;
427         if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl))      return false;
428
429         return true;    
430 }
431
432
433 static struct security_descriptor *security_descriptor_appendv(struct security_descriptor *sd,
434                                                                bool add_ace_to_sacl,
435                                                                va_list ap)
436 {
437         const char *sidstr;
438
439         while ((sidstr = va_arg(ap, const char *))) {
440                 struct dom_sid *sid;
441                 struct security_ace *ace = talloc_zero(sd, struct security_ace);
442                 NTSTATUS status;
443
444                 if (ace == NULL) {
445                         talloc_free(sd);
446                         return NULL;
447                 }
448                 ace->type = va_arg(ap, unsigned int);
449                 ace->access_mask = va_arg(ap, unsigned int);
450                 ace->flags = va_arg(ap, unsigned int);
451                 sid = dom_sid_parse_talloc(ace, sidstr);
452                 if (sid == NULL) {
453                         talloc_free(sd);
454                         return NULL;
455                 }
456                 ace->trustee = *sid;
457                 if (add_ace_to_sacl) {
458                         status = security_descriptor_sacl_add(sd, ace);
459                 } else {
460                         status = security_descriptor_dacl_add(sd, ace);
461                 }
462                 /* TODO: check: would talloc_free(ace) here be correct? */
463                 if (!NT_STATUS_IS_OK(status)) {
464                         talloc_free(sd);
465                         return NULL;
466                 }
467         }
468
469         return sd;
470 }
471
472 struct security_descriptor *security_descriptor_append(struct security_descriptor *sd,
473                                                        ...)
474 {
475         va_list ap;
476
477         va_start(ap, sd);
478         sd = security_descriptor_appendv(sd, false, ap);
479         va_end(ap);
480
481         return sd;
482 }
483
484 static struct security_descriptor *security_descriptor_createv(TALLOC_CTX *mem_ctx,
485                                                                uint16_t sd_type,
486                                                                const char *owner_sid,
487                                                                const char *group_sid,
488                                                                bool add_ace_to_sacl,
489                                                                va_list ap)
490 {
491         struct security_descriptor *sd;
492
493         sd = security_descriptor_initialise(mem_ctx);
494         if (sd == NULL) {
495                 return NULL;
496         }
497
498         sd->type |= sd_type;
499
500         if (owner_sid) {
501                 sd->owner_sid = dom_sid_parse_talloc(sd, owner_sid);
502                 if (sd->owner_sid == NULL) {
503                         talloc_free(sd);
504                         return NULL;
505                 }
506         }
507         if (group_sid) {
508                 sd->group_sid = dom_sid_parse_talloc(sd, group_sid);
509                 if (sd->group_sid == NULL) {
510                         talloc_free(sd);
511                         return NULL;
512                 }
513         }
514
515         return security_descriptor_appendv(sd, add_ace_to_sacl, ap);
516 }
517
518 /*
519   create a security descriptor using string SIDs. This is used by the
520   torture code to allow the easy creation of complex ACLs
521   This is a varargs function. The list of DACL ACEs ends with a NULL sid.
522
523   Each ACE contains a set of 4 parameters:
524   SID, ACCESS_TYPE, MASK, FLAGS
525
526   a typical call would be:
527
528     sd = security_descriptor_dacl_create(mem_ctx,
529                                          sd_type_flags,
530                                          mysid,
531                                          mygroup,
532                                          SID_NT_AUTHENTICATED_USERS,
533                                          SEC_ACE_TYPE_ACCESS_ALLOWED,
534                                          SEC_FILE_ALL,
535                                          SEC_ACE_FLAG_OBJECT_INHERIT,
536                                          NULL);
537   that would create a sd with one DACL ACE
538 */
539
540 struct security_descriptor *security_descriptor_dacl_create(TALLOC_CTX *mem_ctx,
541                                                             uint16_t sd_type,
542                                                             const char *owner_sid,
543                                                             const char *group_sid,
544                                                             ...)
545 {
546         struct security_descriptor *sd = NULL;
547         va_list ap;
548         va_start(ap, group_sid);
549         sd = security_descriptor_createv(mem_ctx, sd_type, owner_sid,
550                                          group_sid, false, ap);
551         va_end(ap);
552
553         return sd;
554 }
555
556 struct security_descriptor *security_descriptor_sacl_create(TALLOC_CTX *mem_ctx,
557                                                             uint16_t sd_type,
558                                                             const char *owner_sid,
559                                                             const char *group_sid,
560                                                             ...)
561 {
562         struct security_descriptor *sd = NULL;
563         va_list ap;
564         va_start(ap, group_sid);
565         sd = security_descriptor_createv(mem_ctx, sd_type, owner_sid,
566                                          group_sid, true, ap);
567         va_end(ap);
568
569         return sd;
570 }
571
572 struct security_ace *security_ace_create(TALLOC_CTX *mem_ctx,
573                                          const char *sid_str,
574                                          enum security_ace_type type,
575                                          uint32_t access_mask,
576                                          uint8_t flags)
577
578 {
579         struct security_ace *ace;
580         bool ok;
581
582         ace = talloc_zero(mem_ctx, struct security_ace);
583         if (ace == NULL) {
584                 return NULL;
585         }
586
587         ok = dom_sid_parse(sid_str, &ace->trustee);
588         if (!ok) {
589                 talloc_free(ace);
590                 return NULL;
591         }
592         ace->type = type;
593         ace->access_mask = access_mask;
594         ace->flags = flags;
595
596         return ace;
597 }
598
599 /*******************************************************************
600  Check for MS NFS ACEs in a sd
601 *******************************************************************/
602 bool security_descriptor_with_ms_nfs(const struct security_descriptor *psd)
603 {
604         int i;
605
606         if (psd->dacl == NULL) {
607                 return false;
608         }
609
610         for (i = 0; i < psd->dacl->num_aces; i++) {
611                 if (dom_sid_compare_domain(
612                             &global_sid_Unix_NFS,
613                             &psd->dacl->aces[i].trustee) == 0) {
614                         return true;
615                 }
616         }
617
618         return false;
619 }