Renamed get_nt_error_msg() to nt_errstr().
[samba.git] / source / 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "nterr.h"
24 #include "sids.h"
25
26 /**********************************************************************************
27  Check if this ACE has a SID in common with the token.
28 **********************************************************************************/
29
30 static BOOL token_sid_in_ace(const NT_USER_TOKEN *token, const SEC_ACE *ace)
31 {
32         size_t i;
33
34         for (i = 0; i < token->num_sids; i++) {
35                 if (sid_equal(&ace->trustee, &token->user_sids[i]))
36                         return True;
37         }
38
39         return False;
40 }
41
42 /*********************************************************************************
43  Check an ACE against a SID.  We return the remaining needed permission
44  bits not yet granted. Zero means permission allowed (no more needed bits).
45 **********************************************************************************/
46
47 static uint32 check_ace(SEC_ACE *ace, NT_USER_TOKEN *token, uint32 acc_desired, 
48                         NTSTATUS *status)
49 {
50         uint32 mask = ace->info.mask;
51
52         /*
53          * Inherit only is ignored.
54          */
55
56         if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
57                 return acc_desired;
58         }
59
60         /*
61          * If this ACE has no SID in common with the token,
62          * ignore it as it cannot be used to make an access
63          * determination.
64          */
65
66         if (!token_sid_in_ace( token, ace))
67                 return acc_desired;     
68
69         switch (ace->type) {
70                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
71                         /*
72                          * This is explicitly allowed.
73                          * Remove the bits from the remaining
74                          * access required. Return the remaining
75                          * bits needed.
76                          */
77                         acc_desired &= ~mask;
78                         break;
79                 case SEC_ACE_TYPE_ACCESS_DENIED:
80                         /*
81                          * This is explicitly denied.
82                          * If any bits match terminate here,
83                          * we are denied.
84                          */
85                         if (acc_desired & mask) {
86                                 *status = NT_STATUS_ACCESS_DENIED;
87                                 return 0xFFFFFFFF;
88                         }
89                         break;
90                 case SEC_ACE_TYPE_SYSTEM_ALARM:
91                 case SEC_ACE_TYPE_SYSTEM_AUDIT:
92                         *status = NT_STATUS_NOT_IMPLEMENTED;
93                         return 0xFFFFFFFF;
94                 default:
95                         *status = NT_STATUS_INVALID_PARAMETER;
96                         return 0xFFFFFFFF;
97         }
98
99         return acc_desired;
100 }
101
102 /*********************************************************************************
103  Maximum access was requested. Calculate the max possible. Fail if it doesn't
104  include other bits requested.
105 **********************************************************************************/ 
106
107 static BOOL get_max_access( SEC_ACL *the_acl, NT_USER_TOKEN *token, uint32 *granted, 
108                             uint32 desired, 
109                             NTSTATUS *status)
110 {
111         uint32 acc_denied = 0;
112         uint32 acc_granted = 0;
113         size_t i;
114         
115         for ( i = 0 ; i < the_acl->num_aces; i++) {
116                 SEC_ACE *ace = &the_acl->ace[i];
117                 uint32 mask = ace->info.mask;
118
119                 if (!token_sid_in_ace( token, ace))
120                         continue;
121
122                 switch (ace->type) {
123                         case SEC_ACE_TYPE_ACCESS_ALLOWED:
124                                 acc_granted |= (mask & ~acc_denied);
125                                 break;
126                         case SEC_ACE_TYPE_ACCESS_DENIED:
127                                 acc_denied |= (mask & ~acc_granted);
128                                 break;
129                         case SEC_ACE_TYPE_SYSTEM_ALARM:
130                         case SEC_ACE_TYPE_SYSTEM_AUDIT:
131                                 *status = NT_STATUS_NOT_IMPLEMENTED;
132                                 *granted = 0;
133                                 return False;
134                         default:
135                                 *status = NT_STATUS_INVALID_PARAMETER;
136                                 *granted = 0;
137                                 return False;
138                 }                           
139         }
140
141         /*
142          * If we were granted no access, or we desired bits that we
143          * didn't get, then deny.
144          */
145
146         if ((acc_granted == 0) || ((acc_granted & desired) != desired)) {
147                 *status = NT_STATUS_ACCESS_DENIED;
148                 *granted = 0;
149                 return False;
150         }
151
152         /*
153          * Return the access we did get.
154          */
155
156         *granted = acc_granted;
157         *status = NT_STATUS_OK;
158         return True;
159 }
160
161 /* Map generic access rights to object specific rights.  This technique is
162    used to give meaning to assigning read, write, execute and all access to
163    objects.  Each type of object has its own mapping of generic to object
164    specific access rights. */
165
166 void se_map_generic(uint32 *access_mask, struct generic_mapping *mapping)
167 {
168         uint32 old_mask = *access_mask;
169
170         if (*access_mask & GENERIC_READ_ACCESS) {
171                 *access_mask &= ~GENERIC_READ_ACCESS;
172                 *access_mask |= mapping->generic_read;
173         }
174
175         if (*access_mask & GENERIC_WRITE_ACCESS) {
176                 *access_mask &= ~GENERIC_WRITE_ACCESS;
177                 *access_mask |= mapping->generic_write;
178         }
179
180         if (*access_mask & GENERIC_EXECUTE_ACCESS) {
181                 *access_mask &= ~GENERIC_EXECUTE_ACCESS;
182                 *access_mask |= mapping->generic_execute;
183         }
184
185         if (*access_mask & GENERIC_ALL_ACCESS) {
186                 *access_mask &= ~GENERIC_ALL_ACCESS;
187                 *access_mask |= mapping->generic_all;
188         }
189
190         if (old_mask != *access_mask) {
191                 DEBUG(10, ("se_map_generic(): mapped mask 0x%08x to 0x%08x\n",
192                            old_mask, *access_mask));
193         }
194 }
195
196 /* Map standard access rights to object specific rights.  This technique is
197    used to give meaning to assigning read, write, execute and all access to
198    objects.  Each type of object has its own mapping of standard to object
199    specific access rights. */
200
201 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
202 {
203         uint32 old_mask = *access_mask;
204
205         if (*access_mask & READ_CONTROL_ACCESS) {
206                 *access_mask &= ~READ_CONTROL_ACCESS;
207                 *access_mask |= mapping->std_read;
208         }
209
210         if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
211                 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
212                 *access_mask |= mapping->std_all;
213         }
214
215         if (old_mask != *access_mask) {
216                 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
217                            old_mask, *access_mask));
218         }
219 }
220
221 /*****************************************************************************
222  Check access rights of a user against a security descriptor.  Look at
223  each ACE in the security descriptor until an access denied ACE denies
224  any of the desired rights to the user or any of the users groups, or one
225  or more ACEs explicitly grant all requested access rights.  See
226  "Access-Checking" document in MSDN.
227 *****************************************************************************/ 
228
229 BOOL se_access_check(SEC_DESC *sd, NT_USER_TOKEN *token,
230                      uint32 acc_desired, uint32 *acc_granted, 
231                      NTSTATUS *status)
232 {
233         extern NT_USER_TOKEN anonymous_token;
234         size_t i;
235         SEC_ACL *the_acl;
236         fstring sid_str;
237         uint32 tmp_acc_desired = acc_desired;
238
239         if (!status || !acc_granted)
240                 return False;
241
242         if (!token)
243                 token = &anonymous_token;
244
245         *status = NT_STATUS_OK;
246         *acc_granted = 0;
247
248         DEBUG(10,("se_access_check: requested access 0x%08x, for NT token with %u entries and first sid %s.\n",
249                  (unsigned int)acc_desired, (unsigned int)token->num_sids,
250                 sid_to_string(sid_str, &token->user_sids[0])));
251
252         /*
253          * No security descriptor or security descriptor with no DACL
254          * present allows all access.
255          */
256
257         /* ACL must have something in it */
258
259         if (!sd || (sd && (!(sd->type & SEC_DESC_DACL_PRESENT) || sd->dacl == NULL))) {
260                 *status = NT_STATUS_OK;
261                 *acc_granted = acc_desired;
262                 DEBUG(5, ("se_access_check: no sd or blank DACL, access allowed\n"));
263                 return True;
264         }
265
266         /* The user sid is the first in the token */
267
268         DEBUG(3, ("se_access_check: user sid is %s\n", sid_to_string(sid_str, &token->user_sids[PRIMARY_USER_SID_INDEX]) ));
269
270         for (i = 1; i < token->num_sids; i++) {
271                 DEBUG(3, ("se_access_check: also %s\n",
272                           sid_to_string(sid_str, &token->user_sids[i])));
273         }
274
275         /* Is the token the owner of the SID ? */
276
277         if (sd->owner_sid) {
278                 for (i = 0; i < token->num_sids; i++) {
279                         if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
280                                 /*
281                                  * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
282                                  */
283                                 if (tmp_acc_desired & WRITE_DAC_ACCESS)
284                                         tmp_acc_desired &= ~WRITE_DAC_ACCESS;
285                                 if (tmp_acc_desired & READ_CONTROL_ACCESS)
286                                         tmp_acc_desired &= ~READ_CONTROL_ACCESS;
287                         }
288                 }
289         }
290
291         the_acl = sd->dacl;
292
293         if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
294                 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
295                 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired, 
296                                        status);
297         }
298
299         for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
300                 SEC_ACE *ace = &the_acl->ace[i];
301
302                 DEBUG(10,("se_access_check: ACE %u: type %d, flags = 0x%02x, SID = %s mask = %x, current desired = %x\n",
303                           (unsigned int)i, ace->type, ace->flags,
304                           sid_to_string(sid_str, &ace->trustee),
305                           (unsigned int) ace->info.mask, 
306                           (unsigned int)tmp_acc_desired ));
307
308                 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
309                 if (NT_STATUS_V(*status)) {
310                         *acc_granted = 0;
311                         DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
312                         return False;
313                 }
314         }
315
316         /*
317          * If there are no more desired permissions left then
318          * access was allowed.
319          */
320
321         if (tmp_acc_desired == 0) {
322                 *acc_granted = acc_desired;
323                 *status = NT_STATUS_OK;
324                 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
325                 return True;
326         }
327                 
328         *acc_granted = 0;
329         *status = NT_STATUS_ACCESS_DENIED;
330         DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
331         return False;
332 }
333
334 /* Create a child security descriptor using another security descriptor as
335    the parent container.  This child object can either be a container or
336    non-container object. */
337
338 SEC_DESC_BUF *se_create_child_secdesc(TALLOC_CTX *ctx, SEC_DESC *parent_ctr, 
339                                       BOOL child_container)
340 {
341         SEC_DESC_BUF *sdb;
342         SEC_DESC *sd;
343         SEC_ACL *new_dacl, *the_acl;
344         SEC_ACE *new_ace_list = NULL;
345         int new_ace_list_ndx = 0, i;
346         size_t size;
347
348         /* Currently we only process the dacl when creating the child.  The
349            sacl should also be processed but this is left out as sacls are
350            not implemented in Samba at the moment.*/
351
352         the_acl = parent_ctr->dacl;
353
354         if (!(new_ace_list = talloc(ctx, sizeof(SEC_ACE) * the_acl->num_aces))) 
355                 return NULL;
356
357         for (i = 0; the_acl && i < the_acl->num_aces; i++) {
358                 SEC_ACE *ace = &the_acl->ace[i];
359                 SEC_ACE *new_ace = &new_ace_list[new_ace_list_ndx];
360                 uint8 new_flags = 0;
361                 BOOL inherit = False;
362                 fstring sid_str;
363
364                 /* The OBJECT_INHERIT_ACE flag causes the ACE to be
365                    inherited by non-container children objects.  Container
366                    children objects will inherit it as an INHERIT_ONLY
367                    ACE. */
368
369                 if (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
370
371                         if (!child_container) {
372                                 new_flags |= SEC_ACE_FLAG_OBJECT_INHERIT;
373                         } else {
374                                 new_flags |= SEC_ACE_FLAG_INHERIT_ONLY;
375                         }
376
377                         inherit = True;
378                 }
379
380                 /* The CONAINER_INHERIT_ACE flag means all child container
381                    objects will inherit and use the ACE. */
382
383                 if (ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
384                         if (!child_container) {
385                                 inherit = False;
386                         } else {
387                                 new_flags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
388                         }
389                 }
390
391                 /* The INHERIT_ONLY_ACE is not used by the se_access_check()
392                    function for the parent container, but is inherited by
393                    all child objects as a normal ACE. */
394
395                 if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
396                         /* Move along, nothing to see here */
397                 }
398
399                 /* The SEC_ACE_FLAG_NO_PROPAGATE_INHERIT flag means the ACE
400                    is inherited by child objects but not grandchildren
401                    objects.  We clear the object inherit and container
402                    inherit flags in the inherited ACE. */
403
404                 if (ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
405                         new_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT |
406                                        SEC_ACE_FLAG_CONTAINER_INHERIT);
407                 }
408
409                 /* Add ACE to ACE list */
410
411                 if (!inherit)
412                         continue;
413
414                 init_sec_access(&new_ace->info, ace->info.mask);
415                 init_sec_ace(new_ace, &ace->trustee, ace->type,
416                              new_ace->info, new_flags);
417
418                 sid_to_string(sid_str, &ace->trustee);
419
420                 DEBUG(5, ("se_create_child_secdesc(): %s:%d/0x%02x/0x%08x "
421                           " inherited as %s:%d/0x%02x/0x%08x\n", sid_str,
422                           ace->type, ace->flags, ace->info.mask,
423                           sid_str, new_ace->type, new_ace->flags,
424                           new_ace->info.mask));
425
426                 new_ace_list_ndx++;
427         }
428
429         /* Create child security descriptor to return */
430         
431         new_dacl = make_sec_acl(ctx, ACL_REVISION, new_ace_list_ndx, new_ace_list);
432
433         /* Use the existing user and group sids.  I don't think this is
434            correct.  Perhaps the user and group should be passed in as
435            parameters by the caller? */
436
437         sd = make_sec_desc(ctx, SEC_DESC_REVISION,
438                            parent_ctr->owner_sid,
439                            parent_ctr->grp_sid,
440                            parent_ctr->sacl,
441                            new_dacl, &size);
442
443         sdb = make_sec_desc_buf(ctx, size, sd);
444
445         return sdb;
446 }