lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[nivanova/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_acl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - ACL support
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/passwd.h"
24 #include "auth/auth.h"
25 #include "vfs_posix.h"
26 #include "librpc/gen_ndr/xattr.h"
27 #include "libcli/security/security.h"
28 #include "param/param.h"
29 #include "../lib/util/unix_privs.h"
30 #include "lib/util/samba_modules.h"
31
32 /* the list of currently registered ACL backends */
33 static struct pvfs_acl_backend {
34         const struct pvfs_acl_ops *ops;
35 } *backends = NULL;
36 static int num_backends;
37
38 /*
39   register a pvfs acl backend. 
40
41   The 'name' can be later used by other backends to find the operations
42   structure for this backend.  
43 */
44 NTSTATUS pvfs_acl_register(const struct pvfs_acl_ops *ops)
45 {
46         struct pvfs_acl_ops *new_ops;
47
48         if (pvfs_acl_backend_byname(ops->name) != NULL) {
49                 DEBUG(0,("pvfs acl backend '%s' already registered\n", ops->name));
50                 return NT_STATUS_OBJECT_NAME_COLLISION;
51         }
52
53         backends = talloc_realloc(talloc_autofree_context(), backends, struct pvfs_acl_backend, num_backends+1);
54         NT_STATUS_HAVE_NO_MEMORY(backends);
55
56         new_ops = (struct pvfs_acl_ops *)talloc_memdup(backends, ops, sizeof(*ops));
57         new_ops->name = talloc_strdup(new_ops, ops->name);
58
59         backends[num_backends].ops = new_ops;
60
61         num_backends++;
62
63         DEBUG(3,("NTVFS backend '%s' registered\n", ops->name));
64
65         return NT_STATUS_OK;
66 }
67
68
69 /*
70   return the operations structure for a named backend
71 */
72 const struct pvfs_acl_ops *pvfs_acl_backend_byname(const char *name)
73 {
74         int i;
75
76         for (i=0;i<num_backends;i++) {
77                 if (strcmp(backends[i].ops->name, name) == 0) {
78                         return backends[i].ops;
79                 }
80         }
81
82         return NULL;
83 }
84
85 NTSTATUS pvfs_acl_init(void)
86 {
87         static bool initialized = false;
88 #define _MODULE_PROTO(init) extern NTSTATUS init(TALLOC_CTX *);
89         STATIC_pvfs_acl_MODULES_PROTO;
90         init_module_fn static_init[] = { STATIC_pvfs_acl_MODULES };
91         init_module_fn *shared_init;
92
93         if (initialized) return NT_STATUS_OK;
94         initialized = true;
95
96         shared_init = load_samba_modules(NULL, "pvfs_acl");
97
98         run_init_functions(NULL, static_init);
99         run_init_functions(NULL, shared_init);
100
101         talloc_free(shared_init);
102
103         return NT_STATUS_OK;
104 }
105
106
107 /*
108   map a single access_mask from generic to specific bits for files/dirs
109 */
110 static uint32_t pvfs_translate_mask(uint32_t access_mask)
111 {
112         if (access_mask & SEC_MASK_GENERIC) {
113                 if (access_mask & SEC_GENERIC_READ)    access_mask |= SEC_RIGHTS_FILE_READ;
114                 if (access_mask & SEC_GENERIC_WRITE)   access_mask |= SEC_RIGHTS_FILE_WRITE;
115                 if (access_mask & SEC_GENERIC_EXECUTE) access_mask |= SEC_RIGHTS_FILE_EXECUTE;
116                 if (access_mask & SEC_GENERIC_ALL)     access_mask |= SEC_RIGHTS_FILE_ALL;
117                 access_mask &= ~SEC_MASK_GENERIC;
118         }
119         return access_mask;
120 }
121
122
123 /*
124   map any generic access bits in the given acl
125   this relies on the fact that the mappings for files and directories
126   are the same
127 */
128 static void pvfs_translate_generic_bits(struct security_acl *acl)
129 {
130         unsigned i;
131
132         if (!acl) return;
133
134         for (i=0;i<acl->num_aces;i++) {
135                 struct security_ace *ace = &acl->aces[i];
136                 ace->access_mask = pvfs_translate_mask(ace->access_mask);
137         }
138 }
139
140
141 /*
142   setup a default ACL for a file
143 */
144 static NTSTATUS pvfs_default_acl(struct pvfs_state *pvfs,
145                                  struct ntvfs_request *req,
146                                  struct pvfs_filename *name, int fd, 
147                                  struct security_descriptor **psd)
148 {
149         struct security_descriptor *sd;
150         NTSTATUS status;
151         struct security_ace ace;
152         mode_t mode;
153         struct id_map *ids;
154
155         *psd = security_descriptor_initialise(req);
156         if (*psd == NULL) {
157                 return NT_STATUS_NO_MEMORY;
158         }
159         sd = *psd;
160
161         ids = talloc_zero_array(sd, struct id_map, 2);
162         NT_STATUS_HAVE_NO_MEMORY(ids);
163
164         ids[0].xid.id = name->st.st_uid;
165         ids[0].xid.type = ID_TYPE_UID;
166         ids[0].sid = NULL;
167
168         ids[1].xid.id = name->st.st_gid;
169         ids[1].xid.type = ID_TYPE_GID;
170         ids[1].sid = NULL;
171
172         status = wbc_xids_to_sids(ids, 2);
173         NT_STATUS_NOT_OK_RETURN(status);
174
175         sd->owner_sid = talloc_steal(sd, ids[0].sid);
176         sd->group_sid = talloc_steal(sd, ids[1].sid);
177
178         talloc_free(ids);
179         sd->type |= SEC_DESC_DACL_PRESENT;
180
181         mode = name->st.st_mode;
182
183         /*
184           we provide up to 4 ACEs
185             - Owner
186             - Group
187             - Everyone
188             - Administrator
189          */
190
191
192         /* setup owner ACE */
193         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
194         ace.flags = 0;
195         ace.trustee = *sd->owner_sid;
196         ace.access_mask = 0;
197
198         if (mode & S_IRUSR) {
199                 if (mode & S_IWUSR) {
200                         ace.access_mask |= SEC_RIGHTS_FILE_ALL;
201                 } else {
202                         ace.access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
203                 }
204         }
205         if (mode & S_IWUSR) {
206                 ace.access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
207         }
208         if (ace.access_mask) {
209                 security_descriptor_dacl_add(sd, &ace);
210         }
211
212
213         /* setup group ACE */
214         ace.trustee = *sd->group_sid;
215         ace.access_mask = 0;
216         if (mode & S_IRGRP) {
217                 ace.access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
218         }
219         if (mode & S_IWGRP) {
220                 /* note that delete is not granted - this matches posix behaviour */
221                 ace.access_mask |= SEC_RIGHTS_FILE_WRITE;
222         }
223         if (ace.access_mask) {
224                 security_descriptor_dacl_add(sd, &ace);
225         }
226
227         /* setup other ACE */
228         ace.trustee = *dom_sid_parse_talloc(req, SID_WORLD);
229         ace.access_mask = 0;
230         if (mode & S_IROTH) {
231                 ace.access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
232         }
233         if (mode & S_IWOTH) {
234                 ace.access_mask |= SEC_RIGHTS_FILE_WRITE;
235         }
236         if (ace.access_mask) {
237                 security_descriptor_dacl_add(sd, &ace);
238         }
239
240         /* setup system ACE */
241         ace.trustee = *dom_sid_parse_talloc(req, SID_NT_SYSTEM);
242         ace.access_mask = SEC_RIGHTS_FILE_ALL;
243         security_descriptor_dacl_add(sd, &ace);
244         
245         return NT_STATUS_OK;
246 }
247                                  
248
249 /*
250   omit any security_descriptor elements not specified in the given
251   secinfo flags
252 */
253 static void normalise_sd_flags(struct security_descriptor *sd, uint32_t secinfo_flags)
254 {
255         if (!(secinfo_flags & SECINFO_OWNER)) {
256                 sd->owner_sid = NULL;
257         }
258         if (!(secinfo_flags & SECINFO_GROUP)) {
259                 sd->group_sid = NULL;
260         }
261         if (!(secinfo_flags & SECINFO_DACL)) {
262                 sd->dacl = NULL;
263         }
264         if (!(secinfo_flags & SECINFO_SACL)) {
265                 sd->sacl = NULL;
266         }
267 }
268
269 static bool pvfs_privileged_access(uid_t uid)
270 {
271         uid_t euid;
272
273         if (uid_wrapper_enabled()) {
274                 setenv("UID_WRAPPER_MYUID", "1", 1);
275         }
276
277         euid = geteuid();
278
279         if (uid_wrapper_enabled()) {
280                 unsetenv("UID_WRAPPER_MYUID");
281         }
282
283         return (uid == euid);
284 }
285
286 /*
287   answer a setfileinfo for an ACL
288 */
289 NTSTATUS pvfs_acl_set(struct pvfs_state *pvfs, 
290                       struct ntvfs_request *req,
291                       struct pvfs_filename *name, int fd, 
292                       uint32_t access_mask,
293                       union smb_setfileinfo *info)
294 {
295         uint32_t secinfo_flags = info->set_secdesc.in.secinfo_flags;
296         struct security_descriptor *new_sd, *sd, orig_sd;
297         NTSTATUS status = NT_STATUS_NOT_FOUND;
298         uid_t old_uid = -1;
299         gid_t old_gid = -1;
300         uid_t new_uid = -1;
301         gid_t new_gid = -1;
302         struct id_map *ids;
303
304         if (pvfs->acl_ops != NULL) {
305                 status = pvfs->acl_ops->acl_load(pvfs, name, fd, req, &sd);
306         }
307         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
308                 status = pvfs_default_acl(pvfs, req, name, fd, &sd);
309         }
310         if (!NT_STATUS_IS_OK(status)) {
311                 return status;
312         }
313
314         ids = talloc(req, struct id_map);
315         NT_STATUS_HAVE_NO_MEMORY(ids);
316         ZERO_STRUCT(ids->xid);
317         ids->sid = NULL;
318         ids->status = ID_UNKNOWN;
319
320         new_sd = info->set_secdesc.in.sd;
321         orig_sd = *sd;
322
323         old_uid = name->st.st_uid;
324         old_gid = name->st.st_gid;
325
326         /* only set the elements that have been specified */
327         if (secinfo_flags & SECINFO_OWNER) {
328                 if (!(access_mask & SEC_STD_WRITE_OWNER)) {
329                         return NT_STATUS_ACCESS_DENIED;
330                 }
331                 if (!dom_sid_equal(sd->owner_sid, new_sd->owner_sid)) {
332                         ids->sid = new_sd->owner_sid;
333                         status = wbc_sids_to_xids(ids, 1);
334                         NT_STATUS_NOT_OK_RETURN(status);
335
336                         if (ids->xid.type == ID_TYPE_BOTH ||
337                             ids->xid.type == ID_TYPE_UID) {
338                                 new_uid = ids->xid.id;
339                         }
340                 }
341                 sd->owner_sid = new_sd->owner_sid;
342         }
343
344         if (secinfo_flags & SECINFO_GROUP) {
345                 if (!(access_mask & SEC_STD_WRITE_OWNER)) {
346                         return NT_STATUS_ACCESS_DENIED;
347                 }
348                 if (!dom_sid_equal(sd->group_sid, new_sd->group_sid)) {
349                         ids->sid = new_sd->group_sid;
350                         status = wbc_sids_to_xids(ids, 1);
351                         NT_STATUS_NOT_OK_RETURN(status);
352
353                         if (ids->xid.type == ID_TYPE_BOTH ||
354                             ids->xid.type == ID_TYPE_GID) {
355                                 new_gid = ids->xid.id;
356                         }
357
358                 }
359                 sd->group_sid = new_sd->group_sid;
360         }
361
362         if (secinfo_flags & SECINFO_DACL) {
363                 if (!(access_mask & SEC_STD_WRITE_DAC)) {
364                         return NT_STATUS_ACCESS_DENIED;
365                 }
366                 sd->dacl = new_sd->dacl;
367                 pvfs_translate_generic_bits(sd->dacl);
368                 sd->type |= SEC_DESC_DACL_PRESENT;
369         }
370
371         if (secinfo_flags & SECINFO_SACL) {
372                 if (!(access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
373                         return NT_STATUS_ACCESS_DENIED;
374                 }
375                 sd->sacl = new_sd->sacl;
376                 pvfs_translate_generic_bits(sd->sacl);
377                 sd->type |= SEC_DESC_SACL_PRESENT;
378         }
379
380         if (secinfo_flags & SECINFO_PROTECTED_DACL) {
381                 if (new_sd->type & SEC_DESC_DACL_PROTECTED) {
382                         sd->type |= SEC_DESC_DACL_PROTECTED;
383                 } else {
384                         sd->type &= ~SEC_DESC_DACL_PROTECTED;
385                 }
386         }
387
388         if (secinfo_flags & SECINFO_PROTECTED_SACL) {
389                 if (new_sd->type & SEC_DESC_SACL_PROTECTED) {
390                         sd->type |= SEC_DESC_SACL_PROTECTED;
391                 } else {
392                         sd->type &= ~SEC_DESC_SACL_PROTECTED;
393                 }
394         }
395
396         if (new_uid == old_uid) {
397                 new_uid = -1;
398         }
399
400         if (new_gid == old_gid) {
401                 new_gid = -1;
402         }
403
404         /* if there's something to change try it */
405         if (new_uid != -1 || new_gid != -1) {
406                 int ret;
407                 if (fd == -1) {
408                         ret = chown(name->full_name, new_uid, new_gid);
409                 } else {
410                         ret = fchown(fd, new_uid, new_gid);
411                 }
412                 if (errno == EPERM) {
413                         if (pvfs_privileged_access(name->st.st_uid)) {
414                                 ret = 0;
415                         } else {
416                                 /* try again as root if we have SEC_PRIV_RESTORE or
417                                    SEC_PRIV_TAKE_OWNERSHIP */
418                                 if (security_token_has_privilege(req->session_info->security_token,
419                                                                  SEC_PRIV_RESTORE) ||
420                                     security_token_has_privilege(req->session_info->security_token,
421                                                                  SEC_PRIV_TAKE_OWNERSHIP)) {
422                                         void *privs;
423                                         privs = root_privileges();
424                                         if (fd == -1) {
425                                                 ret = chown(name->full_name, new_uid, new_gid);
426                                         } else {
427                                                 ret = fchown(fd, new_uid, new_gid);
428                                         }
429                                         talloc_free(privs);
430                                 }
431                         }
432                 }
433                 if (ret == -1) {
434                         return pvfs_map_errno(pvfs, errno);
435                 }
436         }
437
438         /* we avoid saving if the sd is the same. This means when clients
439            copy files and end up copying the default sd that we don't
440            needlessly use xattrs */
441         if (!security_descriptor_equal(sd, &orig_sd) && pvfs->acl_ops) {
442                 status = pvfs->acl_ops->acl_save(pvfs, name, fd, sd);
443         }
444
445         return status;
446 }
447
448
449 /*
450   answer a fileinfo query for the ACL
451 */
452 NTSTATUS pvfs_acl_query(struct pvfs_state *pvfs, 
453                         struct ntvfs_request *req,
454                         struct pvfs_filename *name, int fd, 
455                         union smb_fileinfo *info)
456 {
457         NTSTATUS status = NT_STATUS_NOT_FOUND;
458         struct security_descriptor *sd;
459
460         if (pvfs->acl_ops) {
461                 status = pvfs->acl_ops->acl_load(pvfs, name, fd, req, &sd);
462         }
463         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
464                 status = pvfs_default_acl(pvfs, req, name, fd, &sd);
465         }
466         if (!NT_STATUS_IS_OK(status)) {
467                 return status;
468         }
469
470         normalise_sd_flags(sd, info->query_secdesc.in.secinfo_flags);
471
472         info->query_secdesc.out.sd = sd;
473
474         return NT_STATUS_OK;
475 }
476
477
478 /*
479   check the read only bit against any of the write access bits
480 */
481 static bool pvfs_read_only(struct pvfs_state *pvfs, uint32_t access_mask)
482 {
483         if ((pvfs->flags & PVFS_FLAG_READONLY) &&
484             (access_mask & (SEC_FILE_WRITE_DATA |
485                             SEC_FILE_APPEND_DATA | 
486                             SEC_FILE_WRITE_EA | 
487                             SEC_FILE_WRITE_ATTRIBUTE | 
488                             SEC_STD_DELETE | 
489                             SEC_STD_WRITE_DAC | 
490                             SEC_STD_WRITE_OWNER | 
491                             SEC_DIR_DELETE_CHILD))) {
492                 return true;
493         }
494         return false;
495 }
496
497 /*
498   see if we are a member of the appropriate unix group
499  */
500 static bool pvfs_group_member(struct pvfs_state *pvfs, gid_t gid)
501 {
502         int i, ngroups;
503         gid_t *groups;
504         if (getegid() == gid) {
505                 return true;
506         }
507         ngroups = getgroups(0, NULL);
508         if (ngroups <= 0) {
509                 return false;
510         }
511         groups = talloc_array(pvfs, gid_t, ngroups);
512         if (groups == NULL) {
513                 return false;
514         }
515         if (getgroups(ngroups, groups) != ngroups) {
516                 talloc_free(groups);
517                 return false;
518         }
519         for (i=0; i<ngroups; i++) {
520                 if (groups[i] == gid) break;
521         }
522         talloc_free(groups);
523         return i < ngroups;
524 }
525
526 /*
527   default access check function based on unix permissions
528   doing this saves on building a full security descriptor
529   for the common case of access check on files with no 
530   specific NT ACL
531
532   If name is NULL then treat as a new file creation
533 */
534 static NTSTATUS pvfs_access_check_unix(struct pvfs_state *pvfs,
535                                        struct ntvfs_request *req,
536                                        struct pvfs_filename *name,
537                                        uint32_t *access_mask)
538 {
539         uint32_t max_bits = 0;
540         struct security_token *token = req->session_info->security_token;
541
542         if (pvfs_read_only(pvfs, *access_mask)) {
543                 return NT_STATUS_ACCESS_DENIED;
544         }
545
546         if (name == NULL) {
547                 max_bits |= SEC_RIGHTS_FILE_ALL | SEC_STD_ALL;
548         } else if (pvfs_privileged_access(name->st.st_uid)) {
549                 /* use the IxUSR bits */
550                 if ((name->st.st_mode & S_IWUSR)) {
551                         max_bits |= SEC_RIGHTS_FILE_ALL | SEC_STD_ALL;
552                 } else if ((name->st.st_mode & (S_IRUSR | S_IXUSR))) {
553                         max_bits |= SEC_RIGHTS_FILE_READ | SEC_RIGHTS_FILE_EXECUTE | SEC_STD_ALL;
554                 }
555         } else if (pvfs_group_member(pvfs, name->st.st_gid)) {
556                 /* use the IxGRP bits */
557                 if ((name->st.st_mode & S_IWGRP)) {
558                         max_bits |= SEC_RIGHTS_FILE_ALL | SEC_STD_ALL;
559                 } else if ((name->st.st_mode & (S_IRGRP | S_IXGRP))) {
560                         max_bits |= SEC_RIGHTS_FILE_READ | SEC_RIGHTS_FILE_EXECUTE | SEC_STD_ALL;
561                 }
562         } else {
563                 /* use the IxOTH bits */
564                 if ((name->st.st_mode & S_IWOTH)) {
565                         max_bits |= SEC_RIGHTS_FILE_ALL | SEC_STD_ALL;
566                 } else if ((name->st.st_mode & (S_IROTH | S_IXOTH))) {
567                         max_bits |= SEC_RIGHTS_FILE_READ | SEC_RIGHTS_FILE_EXECUTE | SEC_STD_ALL;
568                 }
569         }
570
571         if (*access_mask & SEC_FLAG_MAXIMUM_ALLOWED) {
572                 *access_mask |= max_bits;
573                 *access_mask &= ~SEC_FLAG_MAXIMUM_ALLOWED;
574         }
575
576         if ((*access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
577             security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
578                 max_bits |= SEC_FLAG_SYSTEM_SECURITY;
579         }
580         
581         if (((*access_mask & ~max_bits) & SEC_RIGHTS_PRIV_RESTORE) &&
582             security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
583                 max_bits |= ~(SEC_RIGHTS_PRIV_RESTORE);
584         }
585         if (((*access_mask & ~max_bits) & SEC_RIGHTS_PRIV_BACKUP) &&
586             security_token_has_privilege(token, SEC_PRIV_BACKUP)) {
587                 max_bits |= ~(SEC_RIGHTS_PRIV_BACKUP);
588         }
589
590         if (*access_mask & ~max_bits) {
591                 DEBUG(5,(__location__ " denied access to '%s' - wanted 0x%08x but got 0x%08x (missing 0x%08x)\n",
592                          name?name->full_name:"(new file)", *access_mask, max_bits, *access_mask & ~max_bits));
593                 return NT_STATUS_ACCESS_DENIED;
594         }
595
596         if (pvfs->ntvfs->ctx->protocol < PROTOCOL_SMB2_02) {
597                 /* on SMB, this bit is always granted, even if not
598                    asked for */
599                 *access_mask |= SEC_FILE_READ_ATTRIBUTE;
600         }
601
602         return NT_STATUS_OK;
603 }
604
605
606 /*
607   check the security descriptor on a file, if any
608   
609   *access_mask is modified with the access actually granted
610 */
611 NTSTATUS pvfs_access_check(struct pvfs_state *pvfs, 
612                            struct ntvfs_request *req,
613                            struct pvfs_filename *name,
614                            uint32_t *access_mask)
615 {
616         struct security_token *token = req->session_info->security_token;
617         struct xattr_NTACL *acl;
618         NTSTATUS status;
619         struct security_descriptor *sd;
620         bool allow_delete = false;
621
622         /* on SMB2 a blank access mask is always denied */
623         if (pvfs->ntvfs->ctx->protocol >= PROTOCOL_SMB2_02 &&
624             *access_mask == 0) {
625                 return NT_STATUS_ACCESS_DENIED;
626         }
627
628         if (pvfs_read_only(pvfs, *access_mask)) {
629                 return NT_STATUS_ACCESS_DENIED;
630         }
631
632         if (*access_mask & SEC_FLAG_MAXIMUM_ALLOWED ||
633             *access_mask & SEC_STD_DELETE) {
634                 status = pvfs_access_check_parent(pvfs, req,
635                                                   name, SEC_DIR_DELETE_CHILD);
636                 if (NT_STATUS_IS_OK(status)) {
637                         allow_delete = true;
638                         *access_mask &= ~SEC_STD_DELETE;
639                 }
640         }
641
642         acl = talloc(req, struct xattr_NTACL);
643         if (acl == NULL) {
644                 return NT_STATUS_NO_MEMORY;
645         }
646
647         /* expand the generic access bits to file specific bits */
648         *access_mask = pvfs_translate_mask(*access_mask);
649         if (pvfs->ntvfs->ctx->protocol < PROTOCOL_SMB2_02) {
650                 *access_mask &= ~SEC_FILE_READ_ATTRIBUTE;
651         }
652
653         status = pvfs_acl_load(pvfs, name, -1, acl);
654         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
655                 talloc_free(acl);
656                 status = pvfs_access_check_unix(pvfs, req, name, access_mask);
657                 goto done;
658         }
659         if (!NT_STATUS_IS_OK(status)) {
660                 return status;
661         }
662
663         switch (acl->version) {
664         case 1:
665                 sd = acl->info.sd;
666                 break;
667         default:
668                 return NT_STATUS_INVALID_ACL;
669         }
670
671         /* check the acl against the required access mask */
672         status = se_access_check(sd, token, *access_mask, access_mask);
673         talloc_free(acl);
674
675         /* if we used a NT acl, then allow access override if the
676            share allows for posix permission override
677         */
678         if (NT_STATUS_IS_OK(status)) {
679                 name->allow_override = (pvfs->flags & PVFS_FLAG_PERM_OVERRIDE) != 0;
680         }
681
682 done:
683         if (pvfs->ntvfs->ctx->protocol < PROTOCOL_SMB2_02) {
684                 /* on SMB, this bit is always granted, even if not
685                    asked for */
686                 *access_mask |= SEC_FILE_READ_ATTRIBUTE;
687         }
688
689         if (allow_delete) {
690                 *access_mask |= SEC_STD_DELETE;
691         }
692
693         return status;
694 }
695
696
697 /*
698   a simplified interface to access check, designed for calls that
699   do not take or return an access check mask
700 */
701 NTSTATUS pvfs_access_check_simple(struct pvfs_state *pvfs, 
702                                   struct ntvfs_request *req,
703                                   struct pvfs_filename *name,
704                                   uint32_t access_needed)
705 {
706         if (access_needed == 0) {
707                 return NT_STATUS_OK;
708         }
709         return pvfs_access_check(pvfs, req, name, &access_needed);
710 }
711
712 /*
713   access check for creating a new file/directory
714 */
715 NTSTATUS pvfs_access_check_create(struct pvfs_state *pvfs, 
716                                   struct ntvfs_request *req,
717                                   struct pvfs_filename *name,
718                                   uint32_t *access_mask,
719                                   bool container,
720                                   struct security_descriptor **sd)
721 {
722         struct pvfs_filename *parent;
723         NTSTATUS status;
724         uint32_t parent_mask;
725         bool allow_delete = false;
726
727         if (pvfs_read_only(pvfs, *access_mask)) {
728                 return NT_STATUS_ACCESS_DENIED;
729         }
730
731         status = pvfs_resolve_parent(pvfs, req, name, &parent);
732         NT_STATUS_NOT_OK_RETURN(status);
733
734         if (container) {
735                 parent_mask = SEC_DIR_ADD_SUBDIR;
736         } else {
737                 parent_mask = SEC_DIR_ADD_FILE;
738         }
739         if (*access_mask & SEC_FLAG_MAXIMUM_ALLOWED ||
740             *access_mask & SEC_STD_DELETE) {
741                 parent_mask |= SEC_DIR_DELETE_CHILD;
742         }
743
744         status = pvfs_access_check(pvfs, req, parent, &parent_mask);
745         if (NT_STATUS_IS_OK(status)) {
746                 if (parent_mask & SEC_DIR_DELETE_CHILD) {
747                         allow_delete = true;
748                 }
749         } else if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
750                 /*
751                  * on ACCESS_DENIED we get the rejected bits
752                  * remove the non critical SEC_DIR_DELETE_CHILD
753                  * and check if something else was rejected.
754                  */
755                 parent_mask &= ~SEC_DIR_DELETE_CHILD;
756                 if (parent_mask != 0) {
757                         return NT_STATUS_ACCESS_DENIED;
758                 }
759                 status = NT_STATUS_OK;
760         } else {
761                 return status;
762         }
763
764         if (*sd == NULL) {
765                 status = pvfs_acl_inherited_sd(pvfs, req, req, parent, container, sd);
766         }
767
768         talloc_free(parent);
769         if (!NT_STATUS_IS_OK(status)) {
770                 return status;
771         }
772
773         /* expand the generic access bits to file specific bits */
774         *access_mask = pvfs_translate_mask(*access_mask);
775
776         if (*access_mask & SEC_FLAG_MAXIMUM_ALLOWED) {
777                 *access_mask |= SEC_RIGHTS_FILE_ALL;
778                 *access_mask &= ~SEC_FLAG_MAXIMUM_ALLOWED;
779         }
780
781         if (pvfs->ntvfs->ctx->protocol < PROTOCOL_SMB2_02) {
782                 /* on SMB, this bit is always granted, even if not
783                    asked for */
784                 *access_mask |= SEC_FILE_READ_ATTRIBUTE;
785         }
786
787         if (allow_delete) {
788                 *access_mask |= SEC_STD_DELETE;
789         }
790
791         return NT_STATUS_OK;
792 }
793
794 /*
795   access check for creating a new file/directory - no access mask supplied
796 */
797 NTSTATUS pvfs_access_check_parent(struct pvfs_state *pvfs, 
798                                   struct ntvfs_request *req,
799                                   struct pvfs_filename *name,
800                                   uint32_t access_mask)
801 {
802         struct pvfs_filename *parent;
803         NTSTATUS status;
804
805         status = pvfs_resolve_parent(pvfs, req, name, &parent);
806         if (!NT_STATUS_IS_OK(status)) {
807                 return status;
808         }
809
810         status = pvfs_access_check_simple(pvfs, req, parent, access_mask);
811         if (NT_STATUS_IS_OK(status) && parent->allow_override) {
812                 name->allow_override = true;
813         }
814         return status;
815 }
816
817
818 /*
819   determine if an ACE is inheritable
820 */
821 static bool pvfs_inheritable_ace(struct pvfs_state *pvfs,
822                                  const struct security_ace *ace,
823                                  bool container)
824 {
825         if (!container) {
826                 return (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) != 0;
827         }
828
829         if (ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
830                 return true;
831         }
832
833         if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) &&
834             !(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT)) {
835                 return true;
836         }
837
838         return false;
839 }
840
841 /*
842   this is the core of ACL inheritance. It copies any inheritable
843   aces from the parent SD to the child SD. Note that the algorithm 
844   depends on whether the child is a container or not
845 */
846 static NTSTATUS pvfs_acl_inherit_aces(struct pvfs_state *pvfs, 
847                                       struct security_descriptor *parent_sd,
848                                       struct security_descriptor *sd,
849                                       bool container)
850 {
851         int i;
852         
853         for (i=0;i<parent_sd->dacl->num_aces;i++) {
854                 struct security_ace ace = parent_sd->dacl->aces[i];
855                 NTSTATUS status;
856                 const struct dom_sid *creator = NULL, *new_id = NULL;
857                 uint32_t orig_flags;
858
859                 if (!pvfs_inheritable_ace(pvfs, &ace, container)) {
860                         continue;
861                 }
862
863                 orig_flags = ace.flags;
864
865                 /* see the RAW-ACLS inheritance test for details on these rules */
866                 if (!container) {
867                         ace.flags = 0;
868                 } else {
869                         ace.flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
870
871                         if (!(ace.flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
872                                 ace.flags |= SEC_ACE_FLAG_INHERIT_ONLY;
873                         }
874                         if (ace.flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
875                                 ace.flags = 0;
876                         }
877                 }
878
879                 /* the CREATOR sids are special when inherited */
880                 if (dom_sid_equal(&ace.trustee, pvfs->sid_cache.creator_owner)) {
881                         creator = pvfs->sid_cache.creator_owner;
882                         new_id = sd->owner_sid;
883                 } else if (dom_sid_equal(&ace.trustee, pvfs->sid_cache.creator_group)) {
884                         creator = pvfs->sid_cache.creator_group;
885                         new_id = sd->group_sid;
886                 } else {
887                         new_id = &ace.trustee;
888                 }
889
890                 if (creator && container && 
891                     (ace.flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
892                         uint32_t flags = ace.flags;
893
894                         ace.trustee = *new_id;
895                         ace.flags = 0;
896                         status = security_descriptor_dacl_add(sd, &ace);
897                         if (!NT_STATUS_IS_OK(status)) {
898                                 return status;
899                         }
900
901                         ace.trustee = *creator;
902                         ace.flags = flags | SEC_ACE_FLAG_INHERIT_ONLY;
903                         status = security_descriptor_dacl_add(sd, &ace);
904                 } else if (container && 
905                            !(orig_flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT)) {
906                         status = security_descriptor_dacl_add(sd, &ace);
907                 } else {
908                         ace.trustee = *new_id;
909                         status = security_descriptor_dacl_add(sd, &ace);
910                 }
911
912                 if (!NT_STATUS_IS_OK(status)) {
913                         return status;
914                 }
915         }
916
917         return NT_STATUS_OK;
918 }
919
920
921
922 /*
923   calculate the ACL on a new file/directory based on the inherited ACL
924   from the parent. If there is no inherited ACL then return a NULL
925   ACL, which means the default ACL should be used
926 */
927 NTSTATUS pvfs_acl_inherited_sd(struct pvfs_state *pvfs, 
928                                TALLOC_CTX *mem_ctx,
929                                struct ntvfs_request *req,
930                                struct pvfs_filename *parent,
931                                bool container,
932                                struct security_descriptor **ret_sd)
933 {
934         struct xattr_NTACL *acl;
935         NTSTATUS status;
936         struct security_descriptor *parent_sd, *sd;
937         struct id_map *ids;
938         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
939
940         *ret_sd = NULL;
941
942         acl = talloc(req, struct xattr_NTACL);
943         if (acl == NULL) {
944                 TALLOC_FREE(tmp_ctx);
945                 return NT_STATUS_NO_MEMORY;
946         }
947
948         status = pvfs_acl_load(pvfs, parent, -1, acl);
949         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
950                 talloc_free(tmp_ctx);
951                 return NT_STATUS_OK;
952         }
953         if (!NT_STATUS_IS_OK(status)) {
954                 TALLOC_FREE(tmp_ctx);
955                 return status;
956         }
957
958         switch (acl->version) {
959         case 1:
960                 parent_sd = acl->info.sd;
961                 break;
962         default:
963                 talloc_free(tmp_ctx);
964                 return NT_STATUS_INVALID_ACL;
965         }
966
967         if (parent_sd == NULL ||
968             parent_sd->dacl == NULL ||
969             parent_sd->dacl->num_aces == 0) {
970                 /* go with the default ACL */
971                 talloc_free(tmp_ctx);
972                 return NT_STATUS_OK;
973         }
974
975         /* create the new sd */
976         sd = security_descriptor_initialise(req);
977         if (sd == NULL) {
978                 TALLOC_FREE(tmp_ctx);
979                 return NT_STATUS_NO_MEMORY;
980         }
981
982         ids = talloc_array(sd, struct id_map, 2);
983         if (ids == NULL) {
984                 TALLOC_FREE(tmp_ctx);
985                 return NT_STATUS_NO_MEMORY;
986         }
987
988         ids[0].xid.id = geteuid();
989         ids[0].xid.type = ID_TYPE_UID;
990         ids[0].sid = NULL;
991         ids[0].status = ID_UNKNOWN;
992
993         ids[1].xid.id = getegid();
994         ids[1].xid.type = ID_TYPE_GID;
995         ids[1].sid = NULL;
996         ids[1].status = ID_UNKNOWN;
997
998         status = wbc_xids_to_sids(ids, 2);
999         if (!NT_STATUS_IS_OK(status)) {
1000                 TALLOC_FREE(tmp_ctx);
1001                 return status;
1002         }
1003
1004         sd->owner_sid = talloc_steal(sd, ids[0].sid);
1005         sd->group_sid = talloc_steal(sd, ids[1].sid);
1006
1007         sd->type |= SEC_DESC_DACL_PRESENT;
1008
1009         /* fill in the aces from the parent */
1010         status = pvfs_acl_inherit_aces(pvfs, parent_sd, sd, container);
1011         if (!NT_STATUS_IS_OK(status)) {
1012                 TALLOC_FREE(tmp_ctx);
1013                 return status;
1014         }
1015
1016         /* if there is nothing to inherit then we fallback to the
1017            default acl */
1018         if (sd->dacl == NULL || sd->dacl->num_aces == 0) {
1019                 talloc_free(tmp_ctx);
1020                 return NT_STATUS_OK;
1021         }
1022
1023         *ret_sd = talloc_steal(mem_ctx, sd);
1024
1025         talloc_free(tmp_ctx);
1026         return NT_STATUS_OK;
1027 }
1028
1029
1030 /*
1031   setup an ACL on a new file/directory based on the inherited ACL from
1032   the parent. If there is no inherited ACL then we don't set anything,
1033   as the default ACL applies anyway
1034 */
1035 NTSTATUS pvfs_acl_inherit(struct pvfs_state *pvfs, 
1036                           struct ntvfs_request *req,
1037                           struct pvfs_filename *name,
1038                           int fd)
1039 {
1040         struct xattr_NTACL acl;
1041         NTSTATUS status;
1042         struct security_descriptor *sd;
1043         struct pvfs_filename *parent;
1044         bool container;
1045
1046         /* form the parents path */
1047         status = pvfs_resolve_parent(pvfs, req, name, &parent);
1048         NT_STATUS_NOT_OK_RETURN(status);
1049
1050         container = (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) ? true:false;
1051
1052         status = pvfs_acl_inherited_sd(pvfs, req, req, parent, container, &sd);
1053         if (!NT_STATUS_IS_OK(status)) {
1054                 talloc_free(parent);
1055                 return status;
1056         }
1057
1058         if (sd == NULL) {
1059                 return NT_STATUS_OK;
1060         }
1061
1062         acl.version = 1;
1063         acl.info.sd = sd;
1064
1065         status = pvfs_acl_save(pvfs, name, fd, &acl);
1066         talloc_free(sd);
1067         talloc_free(parent);
1068
1069         return status;
1070 }
1071
1072 /*
1073   return the maximum allowed access mask
1074 */
1075 NTSTATUS pvfs_access_maximal_allowed(struct pvfs_state *pvfs, 
1076                                      struct ntvfs_request *req,
1077                                      struct pvfs_filename *name,
1078                                      uint32_t *maximal_access)
1079 {
1080         *maximal_access = SEC_FLAG_MAXIMUM_ALLOWED;
1081         return pvfs_access_check(pvfs, req, name, maximal_access);
1082 }