r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[kai/samba-autobuild/.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 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 #include "includes.h"
24
25 /*******************************************************************
26  Create a SEC_ACL structure.  
27 ********************************************************************/
28
29 SEC_ACL *make_sec_acl(TALLOC_CTX *ctx, uint16 revision, int num_aces, SEC_ACE *ace_list)
30 {
31         SEC_ACL *dst;
32         int i;
33
34         if((dst = TALLOC_ZERO_P(ctx,SEC_ACL)) == NULL)
35                 return NULL;
36
37         dst->revision = revision;
38         dst->num_aces = num_aces;
39         dst->size = SEC_ACL_HEADER_SIZE;
40
41         /* Now we need to return a non-NULL address for the ace list even
42            if the number of aces required is zero.  This is because there
43            is a distinct difference between a NULL ace and an ace with zero
44            entries in it.  This is achieved by checking that num_aces is a
45            positive number. */
46
47         if ((num_aces) && 
48             ((dst->aces = TALLOC_ARRAY(ctx, SEC_ACE, num_aces)) 
49              == NULL)) {
50                 return NULL;
51         }
52         
53         for (i = 0; i < num_aces; i++) {
54                 dst->aces[i] = ace_list[i]; /* Structure copy. */
55                 dst->size += ace_list[i].size;
56         }
57
58         return dst;
59 }
60
61 /*******************************************************************
62  Duplicate a SEC_ACL structure.  
63 ********************************************************************/
64
65 SEC_ACL *dup_sec_acl(TALLOC_CTX *ctx, SEC_ACL *src)
66 {
67         if(src == NULL)
68                 return NULL;
69
70         return make_sec_acl(ctx, src->revision, src->num_aces, src->aces);
71 }
72
73 /*******************************************************************
74  Compares two SEC_ACL structures
75 ********************************************************************/
76
77 BOOL sec_acl_equal(SEC_ACL *s1, SEC_ACL *s2)
78 {
79         unsigned int i, j;
80
81         /* Trivial cases */
82
83         if (!s1 && !s2) return True;
84         if (!s1 || !s2) return False;
85
86         /* Check top level stuff */
87
88         if (s1->revision != s2->revision) {
89                 DEBUG(10, ("sec_acl_equal(): revision differs (%d != %d)\n",
90                            s1->revision, s2->revision));
91                 return False;
92         }
93
94         if (s1->num_aces != s2->num_aces) {
95                 DEBUG(10, ("sec_acl_equal(): num_aces differs (%d != %d)\n",
96                            s1->revision, s2->revision));
97                 return False;
98         }
99
100         /* The ACEs could be in any order so check each ACE in s1 against 
101            each ACE in s2. */
102
103         for (i = 0; i < s1->num_aces; i++) {
104                 BOOL found = False;
105
106                 for (j = 0; j < s2->num_aces; j++) {
107                         if (sec_ace_equal(&s1->aces[i], &s2->aces[j])) {
108                                 found = True;
109                                 break;
110                         }
111                 }
112
113                 if (!found) return False;
114         }
115
116         return True;
117 }