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