cab4261adf14782f854e0d37fbfb77a38e8f84a7
[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, const 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 generic access rights to object specific rights for all the ACE's
180  * in a security_acl.
181  */
182
183 void security_acl_map_generic(struct security_acl *sa,
184                                 const struct generic_mapping *mapping)
185 {
186         unsigned int i;
187
188         if (!sa) {
189                 return;
190         }
191
192         for (i = 0; i < sa->num_aces; i++) {
193                 se_map_generic(&sa->aces[i].access_mask, mapping);
194         }
195 }
196
197 /* Map standard access rights to object specific rights.  This technique is
198    used to give meaning to assigning read, write, execute and all access to
199    objects.  Each type of object has its own mapping of standard to object
200    specific access rights. */
201
202 void se_map_standard(uint32 *access_mask, struct standard_mapping *mapping)
203 {
204         uint32 old_mask = *access_mask;
205
206         if (*access_mask & READ_CONTROL_ACCESS) {
207                 *access_mask &= ~READ_CONTROL_ACCESS;
208                 *access_mask |= mapping->std_read;
209         }
210
211         if (*access_mask & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS)) {
212                 *access_mask &= ~(DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS);
213                 *access_mask |= mapping->std_all;
214         }
215
216         if (old_mask != *access_mask) {
217                 DEBUG(10, ("se_map_standard(): mapped mask 0x%08x to 0x%08x\n",
218                            old_mask, *access_mask));
219         }
220 }
221
222 /*****************************************************************************
223  Check access rights of a user against a security descriptor.  Look at
224  each ACE in the security descriptor until an access denied ACE denies
225  any of the desired rights to the user or any of the users groups, or one
226  or more ACEs explicitly grant all requested access rights.  See
227  "Access-Checking" document in MSDN.
228 *****************************************************************************/ 
229
230 bool se_access_check(const SEC_DESC *sd, const NT_USER_TOKEN *token,
231                      uint32 acc_desired, uint32 *acc_granted, 
232                      NTSTATUS *status)
233 {
234         size_t i;
235         SEC_ACL *the_acl;
236         uint32 tmp_acc_desired = acc_desired;
237
238         if (!status || !acc_granted)
239                 return False;
240
241         if (!token)
242                 token = &anonymous_token;
243
244         *status = NT_STATUS_OK;
245         *acc_granted = 0;
246
247         DEBUG(10,("se_access_check: requested access 0x%08x, for NT token "
248                   "with %u entries and first sid %s.\n",
249                   (unsigned int)acc_desired, (unsigned int)token->num_sids,
250                   sid_string_dbg(&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         if (DEBUGLVL(3)) {
268                 DEBUG(3, ("se_access_check: user sid is %s\n",
269                           sid_string_dbg(
270                                   &token->user_sids[PRIMARY_USER_SID_INDEX])));
271                 
272                 for (i = 1; i < token->num_sids; i++) {
273                         DEBUGADD(3, ("se_access_check: also %s\n",
274                                      sid_string_dbg(&token->user_sids[i])));
275                 }
276         }
277
278         /* Is the token the owner of the SID ? */
279
280         if (sd->owner_sid) {
281                 for (i = 0; i < token->num_sids; i++) {
282                         if (sid_equal(&token->user_sids[i], sd->owner_sid)) {
283                                 /*
284                                  * The owner always has SEC_RIGHTS_WRITE_DAC & READ_CONTROL.
285                                  */
286                                 if (tmp_acc_desired & WRITE_DAC_ACCESS)
287                                         tmp_acc_desired &= ~WRITE_DAC_ACCESS;
288                                 if (tmp_acc_desired & READ_CONTROL_ACCESS)
289                                         tmp_acc_desired &= ~READ_CONTROL_ACCESS;
290                         }
291                 }
292         }
293
294         the_acl = sd->dacl;
295
296         if (tmp_acc_desired & MAXIMUM_ALLOWED_ACCESS) {
297                 tmp_acc_desired &= ~MAXIMUM_ALLOWED_ACCESS;
298                 return get_max_access( the_acl, token, acc_granted, tmp_acc_desired, 
299                                        status);
300         }
301
302         for ( i = 0 ; i < the_acl->num_aces && tmp_acc_desired != 0; i++) {
303                 SEC_ACE *ace = &the_acl->aces[i];
304
305                 DEBUGADD(10,("se_access_check: ACE %u: type %d, flags = "
306                              "0x%02x, SID = %s mask = %x, current desired "
307                              "= %x\n", (unsigned int)i, ace->type, ace->flags,
308                              sid_string_dbg(&ace->trustee),
309                              (unsigned int) ace->access_mask,
310                              (unsigned int)tmp_acc_desired ));
311
312                 tmp_acc_desired = check_ace( ace, token, tmp_acc_desired, status);
313                 if (NT_STATUS_V(*status)) {
314                         *acc_granted = 0;
315                         DEBUG(5,("se_access_check: ACE %u denied with status %s.\n", (unsigned int)i, nt_errstr(*status)));
316                         return False;
317                 }
318         }
319
320         /*
321          * If there are no more desired permissions left then
322          * access was allowed.
323          */
324
325         if (tmp_acc_desired == 0) {
326                 *acc_granted = acc_desired;
327                 *status = NT_STATUS_OK;
328                 DEBUG(5,("se_access_check: access (%x) granted.\n", (unsigned int)acc_desired ));
329                 return True;
330         }
331                 
332         *acc_granted = 0;
333         *status = NT_STATUS_ACCESS_DENIED;
334         DEBUG(5,("se_access_check: access (%x) denied.\n", (unsigned int)acc_desired ));
335         return False;
336 }
337
338
339 /*******************************************************************
340  samr_make_sam_obj_sd
341  ********************************************************************/
342
343 NTSTATUS samr_make_sam_obj_sd(TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size)
344 {
345         DOM_SID adm_sid;
346         DOM_SID act_sid;
347
348         SEC_ACE ace[3];
349         SEC_ACCESS mask;
350
351         SEC_ACL *psa = NULL;
352
353         sid_copy(&adm_sid, &global_sid_Builtin);
354         sid_append_rid(&adm_sid, BUILTIN_ALIAS_RID_ADMINS);
355
356         sid_copy(&act_sid, &global_sid_Builtin);
357         sid_append_rid(&act_sid, BUILTIN_ALIAS_RID_ACCOUNT_OPS);
358
359         /*basic access for every one*/
360         init_sec_access(&mask, GENERIC_RIGHTS_SAM_EXECUTE | GENERIC_RIGHTS_SAM_READ);
361         init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
362
363         /*full access for builtin aliases Administrators and Account Operators*/
364         init_sec_access(&mask, GENERIC_RIGHTS_SAM_ALL_ACCESS);
365         init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
366         init_sec_ace(&ace[2], &act_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
367
368         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
369                 return NT_STATUS_NO_MEMORY;
370
371         if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
372                                   SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
373                                   psa, sd_size)) == NULL)
374                 return NT_STATUS_NO_MEMORY;
375
376         return NT_STATUS_OK;
377 }