r1085: Now it's had some proper user testing, merge in the deferred open fix. I'm
[tprouty/samba.git] / source / lib / secacl.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  SEC_ACL handling routines
4  *  Copyright (C) Andrew Tridgell              1992-1998,
5  *  Copyright (C) Jeremy R. Allison            1995-2003.
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7  *  Copyright (C) Paul Ashton                  1997-1998.
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 2 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, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 /*******************************************************************
27  Create a SEC_ACL structure.  
28 ********************************************************************/
29
30 SEC_ACL *make_sec_acl(TALLOC_CTX *ctx, uint16 revision, int num_aces, SEC_ACE *ace_list)
31 {
32         SEC_ACL *dst;
33         int i;
34
35         if((dst = (SEC_ACL *)talloc_zero(ctx,sizeof(SEC_ACL))) == NULL)
36                 return NULL;
37
38         dst->revision = revision;
39         dst->num_aces = num_aces;
40         dst->size = SEC_ACL_HEADER_SIZE;
41
42         /* Now we need to return a non-NULL address for the ace list even
43            if the number of aces required is zero.  This is because there
44            is a distinct difference between a NULL ace and an ace with zero
45            entries in it.  This is achieved by checking that num_aces is a
46            positive number. */
47
48         if ((num_aces) && 
49             ((dst->ace = (SEC_ACE *)talloc(ctx, sizeof(SEC_ACE) * num_aces)) 
50              == NULL)) {
51                 return NULL;
52         }
53         
54         for (i = 0; i < num_aces; i++) {
55                 dst->ace[i] = ace_list[i]; /* Structure copy. */
56                 dst->size += ace_list[i].size;
57         }
58
59         return dst;
60 }
61
62 /*******************************************************************
63  Duplicate a SEC_ACL structure.  
64 ********************************************************************/
65
66 SEC_ACL *dup_sec_acl(TALLOC_CTX *ctx, SEC_ACL *src)
67 {
68         if(src == NULL)
69                 return NULL;
70
71         return make_sec_acl(ctx, src->revision, src->num_aces, src->ace);
72 }
73
74 /*******************************************************************
75  Compares two SEC_ACL structures
76 ********************************************************************/
77
78 BOOL sec_acl_equal(SEC_ACL *s1, SEC_ACL *s2)
79 {
80         unsigned int i, j;
81
82         /* Trivial cases */
83
84         if (!s1 && !s2) return True;
85         if (!s1 || !s2) return False;
86
87         /* Check top level stuff */
88
89         if (s1->revision != s2->revision) {
90                 DEBUG(10, ("sec_acl_equal(): revision differs (%d != %d)\n",
91                            s1->revision, s2->revision));
92                 return False;
93         }
94
95         if (s1->num_aces != s2->num_aces) {
96                 DEBUG(10, ("sec_acl_equal(): num_aces differs (%d != %d)\n",
97                            s1->revision, s2->revision));
98                 return False;
99         }
100
101         /* The ACEs could be in any order so check each ACE in s1 against 
102            each ACE in s2. */
103
104         for (i = 0; i < s1->num_aces; i++) {
105                 BOOL found = False;
106
107                 for (j = 0; j < s2->num_aces; j++) {
108                         if (sec_ace_equal(&s1->ace[i], &s2->ace[j])) {
109                                 found = True;
110                                 break;
111                         }
112                 }
113
114                 if (!found) return False;
115         }
116
117         return True;
118 }