r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[tprouty/samba.git] / source3 / lib / secace.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  SEC_ACE handling functions
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  Check if ACE has OBJECT type.
27 ********************************************************************/
28
29 BOOL sec_ace_object(uint8 type)
30 {
31         if (type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
32             type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
33             type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
34             type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
35                 return True;
36         }
37         return False;
38 }
39
40 /*******************************************************************
41  copy a SEC_ACE structure.
42 ********************************************************************/
43 void sec_ace_copy(SEC_ACE *ace_dest, SEC_ACE *ace_src)
44 {
45         ace_dest->type  = ace_src->type;
46         ace_dest->flags = ace_src->flags;
47         ace_dest->size  = ace_src->size;
48         ace_dest->access_mask = ace_src->access_mask;
49         ace_dest->object = ace_src->object;
50         sid_copy(&ace_dest->trustee, &ace_src->trustee);
51 }
52
53 /*******************************************************************
54  Sets up a SEC_ACE structure.
55 ********************************************************************/
56
57 void init_sec_ace(SEC_ACE *t, const DOM_SID *sid, uint8 type, uint32 mask, uint8 flag)
58 {
59         t->type = type;
60         t->flags = flag;
61         t->size = sid_size(sid) + 8;
62         t->access_mask = mask;
63
64         ZERO_STRUCTP(&t->trustee);
65         sid_copy(&t->trustee, sid);
66 }
67
68 /*******************************************************************
69  adds new SID with its permissions to ACE list
70 ********************************************************************/
71
72 NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, SEC_ACE **pp_new, SEC_ACE *old, unsigned *num, DOM_SID *sid, uint32 mask)
73 {
74         unsigned int i = 0;
75         
76         if (!ctx || !pp_new || !old || !sid || !num)  return NT_STATUS_INVALID_PARAMETER;
77
78         *num += 1;
79         
80         if((pp_new[0] = TALLOC_ZERO_ARRAY(ctx, SEC_ACE, *num )) == 0)
81                 return NT_STATUS_NO_MEMORY;
82
83         for (i = 0; i < *num - 1; i ++)
84                 sec_ace_copy(&(*pp_new)[i], &old[i]);
85
86         (*pp_new)[i].type  = 0;
87         (*pp_new)[i].flags = 0;
88         (*pp_new)[i].size  = SEC_ACE_HEADER_SIZE + sid_size(sid);
89         (*pp_new)[i].access_mask = mask;
90         sid_copy(&(*pp_new)[i].trustee, sid);
91         return NT_STATUS_OK;
92 }
93
94 /*******************************************************************
95   modify SID's permissions at ACL 
96 ********************************************************************/
97
98 NTSTATUS sec_ace_mod_sid(SEC_ACE *ace, size_t num, DOM_SID *sid, uint32 mask)
99 {
100         unsigned int i = 0;
101
102         if (!ace || !sid)  return NT_STATUS_INVALID_PARAMETER;
103
104         for (i = 0; i < num; i ++) {
105                 if (sid_compare(&ace[i].trustee, sid) == 0) {
106                         ace[i].access_mask = mask;
107                         return NT_STATUS_OK;
108                 }
109         }
110         return NT_STATUS_NOT_FOUND;
111 }
112
113 /*******************************************************************
114  delete SID from ACL
115 ********************************************************************/
116
117 NTSTATUS sec_ace_del_sid(TALLOC_CTX *ctx, SEC_ACE **pp_new, SEC_ACE *old, uint32 *num, DOM_SID *sid)
118 {
119         unsigned int i     = 0;
120         unsigned int n_del = 0;
121
122         if (!ctx || !pp_new || !old || !sid || !num)  return NT_STATUS_INVALID_PARAMETER;
123
124         if (*num) {
125                 if((pp_new[0] = TALLOC_ZERO_ARRAY(ctx, SEC_ACE, *num )) == 0)
126                         return NT_STATUS_NO_MEMORY;
127         } else {
128                 pp_new[0] = NULL;
129         }
130
131         for (i = 0; i < *num; i ++) {
132                 if (sid_compare(&old[i].trustee, sid) != 0)
133                         sec_ace_copy(&(*pp_new)[i], &old[i]);
134                 else
135                         n_del ++;
136         }
137         if (n_del == 0)
138                 return NT_STATUS_NOT_FOUND;
139         else {
140                 *num -= n_del;
141                 return NT_STATUS_OK;
142         }
143 }
144
145 /*******************************************************************
146  Compares two SEC_ACE structures
147 ********************************************************************/
148
149 BOOL sec_ace_equal(SEC_ACE *s1, SEC_ACE *s2)
150 {
151         /* Trivial case */
152
153         if (!s1 && !s2) {
154                 return True;
155         }
156
157         if (!s1 || !s2) {
158                 return False;
159         }
160
161         /* Check top level stuff */
162
163         if (s1->type != s2->type || s1->flags != s2->flags ||
164             s1->access_mask != s2->access_mask) {
165                 return False;
166         }
167
168         /* Check SID */
169
170         if (!sid_equal(&s1->trustee, &s2->trustee)) {
171                 return False;
172         }
173
174         return True;
175 }
176
177 int nt_ace_inherit_comp( SEC_ACE *a1, SEC_ACE *a2)
178 {
179         int a1_inh = a1->flags & SEC_ACE_FLAG_INHERITED_ACE;
180         int a2_inh = a2->flags & SEC_ACE_FLAG_INHERITED_ACE;
181
182         if (a1_inh == a2_inh)
183                 return 0;
184
185         if (!a1_inh && a2_inh)
186                 return -1;
187         return 1;
188 }
189
190 /*******************************************************************
191   Comparison function to apply the order explained below in a group.
192 *******************************************************************/
193
194 int nt_ace_canon_comp( SEC_ACE *a1, SEC_ACE *a2)
195 {
196         if ((a1->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
197                                 (a2->type != SEC_ACE_TYPE_ACCESS_DENIED))
198                 return -1;
199
200         if ((a2->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
201                                 (a1->type != SEC_ACE_TYPE_ACCESS_DENIED))
202                 return 1;
203
204         /* Both access denied or access allowed. */
205
206         /* 1. ACEs that apply to the object itself */
207
208         if (!(a1->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
209                         (a2->flags & SEC_ACE_FLAG_INHERIT_ONLY))
210                 return -1;
211         else if (!(a2->flags & SEC_ACE_FLAG_INHERIT_ONLY) &&
212                         (a1->flags & SEC_ACE_FLAG_INHERIT_ONLY))
213                 return 1;
214
215         /* 2. ACEs that apply to a subobject of the object, such as
216          * a property set or property. */
217
218         if (a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
219                         !(a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
220                 return -1;
221         else if (a2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT) &&
222                         !(a1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT)))
223                 return 1;
224
225         return 0;
226 }
227
228 /*******************************************************************
229  Functions to convert a SEC_DESC ACE DACL list into canonical order.
230  JRA.
231
232 --- from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/security/order_of_aces_in_a_dacl.asp
233
234 The following describes the preferred order:
235
236  To ensure that noninherited ACEs have precedence over inherited ACEs,
237  place all noninherited ACEs in a group before any inherited ACEs.
238  This ordering ensures, for example, that a noninherited access-denied ACE
239  is enforced regardless of any inherited ACE that allows access.
240
241  Within the groups of noninherited ACEs and inherited ACEs, order ACEs according to ACE type, as the following shows:
242         1. Access-denied ACEs that apply to the object itself
243         2. Access-denied ACEs that apply to a subobject of the object, such as a property set or property
244         3. Access-allowed ACEs that apply to the object itself
245         4. Access-allowed ACEs that apply to a subobject of the object"
246
247 ********************************************************************/
248
249 void dacl_sort_into_canonical_order(SEC_ACE *srclist, unsigned int num_aces)
250 {
251         unsigned int i;
252
253         if (!srclist || num_aces == 0)
254                 return;
255
256         /* Sort so that non-inherited ACE's come first. */
257         qsort( srclist, num_aces, sizeof(srclist[0]), QSORT_CAST nt_ace_inherit_comp);
258
259         /* Find the boundary between non-inherited ACEs. */
260         for (i = 0; i < num_aces; i++ ) {
261                 SEC_ACE *curr_ace = &srclist[i];
262
263                 if (curr_ace->flags & SEC_ACE_FLAG_INHERITED_ACE)
264                         break;
265         }
266
267         /* i now points at entry number of the first inherited ACE. */
268
269         /* Sort the non-inherited ACEs. */
270         if (i)
271                 qsort( srclist, i, sizeof(srclist[0]), QSORT_CAST nt_ace_canon_comp);
272
273         /* Now sort the inherited ACEs. */
274         if (num_aces - i)
275                 qsort( &srclist[i], num_aces - i, sizeof(srclist[0]), QSORT_CAST nt_ace_canon_comp);
276 }
277
278 /*******************************************************************
279  Check if this ACE has a SID in common with the token.
280 ********************************************************************/
281
282 BOOL token_sid_in_ace(const NT_USER_TOKEN *token, const SEC_ACE *ace)
283 {
284         size_t i;
285
286         for (i = 0; i < token->num_sids; i++) {
287                 if (sid_equal(&ace->trustee, &token->user_sids[i]))
288                         return True;
289         }
290
291         return False;
292 }