r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[ira/wip.git] / source3 / lib / util_seaccess.c
1 /*
2    Unix SMB/CIFS implementation.
3    Copyright (C) Luke Kenneth Casson Leighton 1996-2000.
4    Copyright (C) Tim Potter 2000.
5    Copyright (C) Re-written by Jeremy Allison 2000.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 extern NT_USER_TOKEN anonymous_token;
24
25 /*********************************************************************************
26  Check an ACE against a SID.  We return the remaining needed permission
27  bits not yet granted. Zero means permission allowed (no more needed bits).
28 **********************************************************************************/
29
30 static uint32 check_ace(SEC_ACE *ace, const NT_USER_TOKEN *token, uint32 acc_desired, 
31                         NTSTATUS *status)
32 {
33         uint32 mask = ace->access_mask;
34
35         /*
36          * Inherit only is ignored.
37          */
38
39         if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
40                 return acc_desired;
41         }
42
43         /*
44          * If this ACE has no SID in common with the token,
45          * ignore it as it cannot be used to make an access
46          * determination.
47          */
48
49         if (!token_sid_in_ace( token, ace))
50                 return acc_desired;     
51
52         switch (ace->type) {
53                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
54                         /*
55                          * This is explicitly allowed.
56                          * Remove the bits from the remaining
57                          * access required. Return the remaining
58                          * bits needed.
59                          */
60                         acc_desired &= ~mask;
61                         break;
62                 case SEC_ACE_TYPE_ACCESS_DENIED:
63                         /*
64                          * This is explicitly denied.
65                          * If any bits match terminate here,
66                          * we are denied.
67                          */
68                         if (acc_desired & mask) {
69                                 *status = NT_STATUS_ACCESS_DENIED;
70                                 return 0xFFFFFFFF;
71                         }
72                         break;
73                 case SEC_ACE_TYPE_SYSTEM_ALARM:
74                 case SEC_ACE_TYPE_SYSTEM_AUDIT:
75                         *status = NT_STATUS_NOT_IMPLEMENTED;
76                         return 0xFFFFFFFF;
77                 default:
78                         *status = NT_STATUS_INVALID_PARAMETER;
79                         return 0xFFFFFFFF;
80         }
81
82         return acc_desired;
83 }
84
85 /*********************************************************************************
86  Maximum access was requested. Calculate the max possible. Fail if it doesn't
87  include other bits requested.
88 **********************************************************************************/ 
89
90 static BOOL get_max_access( SEC_ACL *the_acl, const NT_USER_TOKEN *token, uint32 *granted, 
91                             uint32 desired, 
92                             NTSTATUS *status)
93 {
94         uint32 acc_denied = 0;
95         uint32 acc_granted = 0;
96         size_t i;
97         
98         for ( i = 0 ; i < the_acl->num_aces; i++) {
99                 SEC_ACE *ace = &the_acl->aces[i];
100                 uint32 mask = ace->access_mask;
101
102                 if (!token_sid_in_ace( token, ace))
103                         continue;
104
105                 switch (ace->type) {
106                         case SEC_ACE_TYPE_ACCESS_ALLOWED:
107                                 acc_granted |= (mask & ~acc_denied);
108                                 break;
109                         case SEC_ACE_TYPE_ACCESS_DENIED:
110                                 acc_denied |= (mask & ~acc_granted);
111                                 break;
112                         case SEC_ACE_TYPE_SYSTEM_ALARM:
113                         case SEC_ACE_TYPE_SYSTEM_AUDIT:
114                                 *status = NT_STATUS_NOT_IMPLEMENTED;
115                                 *granted = 0;
116                                 return False;
117                         default:
118                                 *status = NT_STATUS_INVALID_PARAMETER;
119                                 *granted = 0;
120                                 return False;
121                 }                           
122         }
123
124         /*
125          * If we were granted no access, or we desired bits that we
126          * didn't get, then deny.
127          */
128
129         if ((acc_granted == 0) || ((acc_granted & desired) != desired)) {
130                 *status = NT_STATUS_ACCESS_DENIED;
131                 *granted = 0;
132                 return False;
133         }
134
135         /*
136          * Return the access we did get.
137          */
138
139         *granted = acc_granted;
140         *status = NT_STATUS_OK;
141         return True;
142 }
143
144 /* Map generic access rights to object specific rights.  This technique is
145    used to give meaning to assigning read, write, execute and all access to
146    objects.  Each type of object has its own mapping of generic to object
147    specific access rights. */
148
149 void se_map_generic(uint32 *access_mask, struct generic_mapping *mapping)
150 {
151         uint32 old_mask = *access_mask;
152
153         if (*access_mask & GENERIC_READ_ACCESS) {
154                 *access_mask &= ~GENERIC_READ_ACCESS;
155                 *access_mask |= mapping->generic_read;
156         }
157
158         if (*access_mask & GENERIC_WRITE_ACCESS) {
159                 *access_mask &= ~GENERIC_WRITE_ACCESS;
160                 *access_mask |= mapping->generic_write;
161         }
162
163         if (*access_mask & GENERIC_EXECUTE_ACCESS) {
164                 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
165                 *access_mask |= mapping->generic_execute;
166         }
167
168         if (*access_mask & GENERIC_ALL_ACCESS) {
169                 *access_mask &= ~GENERIC_ALL_ACCESS;
170                 *access_mask |= mapping->generic_all;
171         }
172
173         if (old_mask != *access_mask) {
174                 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
175                            old_mask, *access_mask));
176         }
177 }
178
179 /* Map standard access rights to object specific rights.  This technique is
180    used to give meaning to assigning read, write, execute and all access to
181    objects.  Each type of object has its own mapping of standard to object
182    specific access rights. */
183
184 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
185 {
186         uint32 old_mask = *access_mask;
187
188         if (*access_mask & READ_CONTROL_ACCESS) {
189                 *access_mask &= ~READ_CONTROL_ACCESS;
190                 *access_mask |= mapping->std_read;
191         }
192
193         if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
194                 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
195                 *access_mask |= mapping->std_all;
196         }
197
198         if (old_mask != *access_mask) {
199                 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
200                            old_mask, *access_mask));
201         }
202 }
203
204 /*****************************************************************************
205  Check access rights of a user against a security descriptor.  Look at
206  each ACE in the security descriptor until an access denied ACE denies
207  any of the desired rights to the user or any of the users groups, or one
208  or more ACEs explicitly grant all requested access rights.  See
209  "Access-Checking" document in MSDN.
210 *****************************************************************************/ 
211
212 BOOL se_access_check(const SEC_DESC *sd, const NT_USER_TOKEN *token,
213                      uint32 acc_desired, uint32 *acc_granted, 
214                      NTSTATUS *status)
215 {
216         size_t i;
217         SEC_ACL *the_acl;
218         fstring sid_str;
219         uint32 tmp_acc_desired = acc_desired;
220
221         if (!status || !acc_granted)
222                 return False;
223
224         if (!token)
225                 token = &anonymous_token;
226
227         *status = NT_STATUS_OK;
228         *acc_granted = 0;
229
230         DEBUG(10,("se_access_check: requested access 0x%08x, for NT token with %u entries and first sid %s.\n",
231                  (unsigned int)acc_desired, (unsigned int)token->num_sids,
232                 sid_to_string(sid_str, &token->user_sids[0])));
233
234         /*
235          * No security descriptor or security descriptor with no DACL
236          * present allows all access.
237          */
238
239         /* ACL must have something in it */
240
241         if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
242                 *status = NT_STATUS_OK;
243                 *acc_granted = acc_desired;
244                 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
245                 return True;
246         }
247
248         /* The user sid is the first in the token */
249         if (DEBUGLVL(3)) {
250                 DEBUG(3, ("se_access_check: user sid is %s\n", sid_to_string(sid_str, &token->user_sids[PRIMARY_USER_SID_INDEX]) ));
251                 
252                 for (i = 1; i < token->num_sids; i++) {
253                         DEBUGADD(3, ("se_access_check: also %s\n",
254                                   sid_to_string(sid_str, &token->user_sids[i])));
255                 }
256         }
257
258         /* Is the token the owner of the SID ? */
259
260         if (sd->owner_sid) {
261                 for (i = 0; i < token->num_sids; i++) {
262                         if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
263                                 /*
264                                  * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
265                                  */
266                                 if (tmp_acc_desired & WRITE_DAC_ACCESS)
267                                         tmp_acc_desired &= ~WRITE_DAC_ACCESS;
268                                 if (tmp_acc_desired & READ_CONTROL_ACCESS)
269                                         tmp_acc_desired &= ~READ_CONTROL_ACCESS;
270                         }
271                 }
272         }
273
274         the_acl = sd->dacl;
275
276         if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
277                 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
278                 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired, 
279                                        status);
280         }
281
282         for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
283                 SEC_ACE *ace = &the_acl->aces[i];
284
285                 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = 0x%02x, SID = %s mask = %x, current desired = %x\n",
286                           (unsigned int)i, ace->type, ace->flags,
287                           sid_to_string(sid_str, &ace->trustee),
288                           (unsigned int) ace->access_mask, 
289                           (unsigned int)tmp_acc_desired ));
290
291                 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
292                 if (NT_STATUS_V(*status)) {
293                         *acc_granted = 0;
294                         DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
295                         return False;
296                 }
297         }
298
299         /*
300          * If there are no more desired permissions left then
301          * access was allowed.
302          */
303
304         if (tmp_acc_desired == 0) {
305                 *acc_granted = acc_desired;
306                 *status = NT_STATUS_OK;
307                 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
308                 return True;
309         }
310                 
311         *acc_granted = 0;
312         *status = NT_STATUS_ACCESS_DENIED;
313         DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
314         return False;
315 }
316
317
318 /*******************************************************************
319  samr_make_sam_obj_sd
320  ********************************************************************/
321
322 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
323 {
324         DOM_SID adm_sid;
325         DOM_SID act_sid;
326
327         SEC_ACE ace[3];
328         SEC_ACCESS mask;
329
330         SEC_ACL *psa = NULL;
331
332         sid_copy(&adm_sid, &global_sid_Builtin);
333         sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
334
335         sid_copy(&act_sid, &global_sid_Builtin);
336         sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
337
338         /*basic access for every one*/
339         init_sec_access(&mask, GENERIC_RIGHTS_SAM_EXECUTE | GENERIC_RIGHTS_SAM_READ);
340         init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
341
342         /*full access for builtin aliases Administrators and Account Operators*/
343         init_sec_access(&mask, GENERIC_RIGHTS_SAM_ALL_ACCESS);
344         init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
345         init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
346
347         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
348                 return NT_STATUS_NO_MEMORY;
349
350         if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
351                 return NT_STATUS_NO_MEMORY;
352
353         return NT_STATUS_OK;
354 }