nfs4_acls: pass ACE_FLAG_INHERITED_ACE down from the client
[Samba.git] / source3 / modules / nfs4_acls.c
blob2e7d806b848678f1a8d412d78ed6b656f69921f2
1 /*
2 * NFS4 ACL handling
4 * Copyright (C) Jim McDonough, 2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "nfs4_acls.h"
23 #include "librpc/gen_ndr/ndr_security.h"
24 #include "../libcli/security/dom_sid.h"
25 #include "../libcli/security/security.h"
26 #include "include/dbwrap.h"
27 #include "system/filesys.h"
28 #include "passdb/lookup_sid.h"
29 #include "util_tdb.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_ACLS
34 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
36 extern const struct generic_mapping file_generic_mapping;
38 #define SMB_ACE4_INT_MAGIC 0x76F8A967
39 typedef struct _SMB_ACE4_INT_T
41 uint32 magic;
42 SMB_ACE4PROP_T prop;
43 void *next;
44 } SMB_ACE4_INT_T;
46 #define SMB_ACL4_INT_MAGIC 0x29A3E792
47 typedef struct _SMB_ACL4_INT_T
49 uint32 magic;
50 uint32 naces;
51 SMB_ACE4_INT_T *first;
52 SMB_ACE4_INT_T *last;
53 } SMB_ACL4_INT_T;
55 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *theacl)
57 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
58 if (theacl==NULL)
60 DEBUG(2, ("acl is NULL\n"));
61 errno = EINVAL;
62 return NULL;
64 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
66 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
67 errno = EINVAL;
68 return NULL;
70 return aclint;
73 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
75 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
76 if (ace==NULL)
78 DEBUG(2, ("ace is NULL\n"));
79 errno = EINVAL;
80 return NULL;
82 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
84 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
85 errno = EINVAL;
86 return NULL;
88 return aceint;
91 SMB4ACL_T *smb_create_smb4acl(void)
93 TALLOC_CTX *mem_ctx = talloc_tos();
94 SMB_ACL4_INT_T *theacl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
95 if (theacl==NULL)
97 DEBUG(0, ("TALLOC_SIZE failed\n"));
98 errno = ENOMEM;
99 return NULL;
101 theacl->magic = SMB_ACL4_INT_MAGIC;
102 /* theacl->first, last = NULL not needed */
103 return (SMB4ACL_T *)theacl;
106 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *theacl, SMB_ACE4PROP_T *prop)
108 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
109 TALLOC_CTX *mem_ctx = talloc_tos();
110 SMB_ACE4_INT_T *ace;
112 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
113 if (ace==NULL)
115 DEBUG(0, ("TALLOC_SIZE failed\n"));
116 errno = ENOMEM;
117 return NULL;
119 ace->magic = SMB_ACE4_INT_MAGIC;
120 /* ace->next = NULL not needed */
121 memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
123 if (aclint->first==NULL)
125 aclint->first = ace;
126 aclint->last = ace;
127 } else {
128 aclint->last->next = (void *)ace;
129 aclint->last = ace;
131 aclint->naces++;
133 return (SMB4ACE_T *)ace;
136 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
138 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
139 if (aceint==NULL)
140 return NULL;
142 return &aceint->prop;
145 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
147 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
148 if (aceint==NULL)
149 return NULL;
151 return (SMB4ACE_T *)aceint->next;
154 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *theacl)
156 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
157 if (aclint==NULL)
158 return NULL;
160 return (SMB4ACE_T *)aclint->first;
163 uint32 smb_get_naces(SMB4ACL_T *theacl)
165 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
166 if (aclint==NULL)
167 return 0;
169 return aclint->naces;
172 static int smbacl4_GetFileOwner(struct connection_struct *conn,
173 const char *filename,
174 SMB_STRUCT_STAT *psbuf)
176 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
178 /* Get the stat struct for the owner info. */
179 if (vfs_stat_smb_fname(conn, filename, psbuf) != 0)
181 DEBUG(8, ("vfs_stat_smb_fname failed with error %s\n",
182 strerror(errno)));
183 return -1;
186 return 0;
189 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
191 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
193 if (fsp->fh->fd == -1) {
194 return smbacl4_GetFileOwner(fsp->conn,
195 fsp->fsp_name->base_name, psbuf);
197 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
199 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
200 strerror(errno)));
201 return -1;
204 return 0;
207 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *theacl, /* in */
208 struct dom_sid *psid_owner, /* in */
209 struct dom_sid *psid_group, /* in */
210 bool is_directory, /* in */
211 struct security_ace **ppnt_ace_list, /* out */
212 int *pgood_aces /* out */
215 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
216 SMB_ACE4_INT_T *aceint;
217 struct security_ace *nt_ace_list = NULL;
218 int good_aces = 0;
220 DEBUG(10, ("smbacl_nfs42win entered\n"));
222 aclint = get_validated_aclint(theacl);
223 /* We do not check for naces being 0 or theacl being NULL here because it is done upstream */
224 /* in smb_get_nt_acl_nfs4(). */
225 nt_ace_list = (struct security_ace *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(struct security_ace));
226 if (nt_ace_list==NULL)
228 DEBUG(10, ("talloc error"));
229 errno = ENOMEM;
230 return False;
233 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
234 uint32_t mask;
235 struct dom_sid sid;
236 SMB_ACE4PROP_T *ace = &aceint->prop;
237 uint32_t mapped_ace_flags;
239 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
240 "who: %d\n", aceint->magic, ace->aceType, ace->flags,
241 ace->aceFlags, ace->aceMask, ace->who.id));
243 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
245 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
246 switch (ace->who.special_id) {
247 case SMB_ACE4_WHO_OWNER:
248 sid_copy(&sid, psid_owner);
249 break;
250 case SMB_ACE4_WHO_GROUP:
251 sid_copy(&sid, psid_group);
252 break;
253 case SMB_ACE4_WHO_EVERYONE:
254 sid_copy(&sid, &global_sid_World);
255 break;
256 default:
257 DEBUG(8, ("invalid special who id %d "
258 "ignored\n", ace->who.special_id));
260 } else {
261 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
262 gid_to_sid(&sid, ace->who.gid);
263 } else {
264 uid_to_sid(&sid, ace->who.uid);
267 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
268 sid_string_dbg(&sid)));
270 if (is_directory && (ace->aceMask & SMB_ACE4_ADD_FILE)) {
271 ace->aceMask |= SMB_ACE4_DELETE_CHILD;
274 mapped_ace_flags = ace->aceFlags & 0xf;
275 if (ace->aceFlags & SMB_ACE4_INHERITED_ACE) {
276 mapped_ace_flags |= SEC_ACE_FLAG_INHERITED_ACE;
278 if (!is_directory && (mapped_ace_flags & (SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE))) {
280 * GPFS sets inherits dir_inhert and file_inherit flags
281 * to files, too, which confuses windows, and seems to
282 * be wrong anyways. ==> Map these bits away for files.
284 DEBUG(10, ("removing inherit flags from nfs4 ace\n"));
285 mapped_ace_flags &= ~(SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE);
287 DEBUG(10, ("mapped ace flags: 0x%x => 0x%x\n",
288 ace->aceFlags, mapped_ace_flags));
290 /* Windows clients expect SYNC on acls to
291 correctly allow rename. See bug #7909. */
292 mask = ace->aceMask | SMB_ACE4_SYNCHRONIZE;
293 init_sec_ace(&nt_ace_list[good_aces++], &sid,
294 ace->aceType, mask,
295 mapped_ace_flags);
298 *ppnt_ace_list = nt_ace_list;
299 *pgood_aces = good_aces;
301 return True;
304 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
305 uint32 security_info,
306 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
308 int good_aces = 0;
309 struct dom_sid sid_owner, sid_group;
310 size_t sd_size = 0;
311 struct security_ace *nt_ace_list = NULL;
312 struct security_acl *psa = NULL;
313 TALLOC_CTX *mem_ctx = talloc_tos();
315 if (theacl==NULL || smb_get_naces(theacl)==0)
316 return NT_STATUS_ACCESS_DENIED; /* special because we
317 * shouldn't alloc 0 for
318 * win */
320 uid_to_sid(&sid_owner, sbuf->st_ex_uid);
321 gid_to_sid(&sid_group, sbuf->st_ex_gid);
323 if (smbacl4_nfs42win(mem_ctx, theacl, &sid_owner, &sid_group,
324 S_ISDIR(sbuf->st_ex_mode),
325 &nt_ace_list, &good_aces)==False) {
326 DEBUG(8,("smbacl4_nfs42win failed\n"));
327 return map_nt_error_from_unix(errno);
330 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
331 if (psa == NULL) {
332 DEBUG(2,("make_sec_acl failed\n"));
333 return NT_STATUS_NO_MEMORY;
336 DEBUG(10,("after make sec_acl\n"));
337 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
338 (security_info & SECINFO_OWNER) ? &sid_owner : NULL,
339 (security_info & SECINFO_GROUP) ? &sid_group : NULL,
340 NULL, psa, &sd_size);
341 if (*ppdesc==NULL) {
342 DEBUG(2,("make_sec_desc failed\n"));
343 return NT_STATUS_NO_MEMORY;
346 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
347 (int)ndr_size_security_descriptor(*ppdesc, 0)));
349 return NT_STATUS_OK;
352 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
353 uint32 security_info,
354 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
356 SMB_STRUCT_STAT sbuf;
358 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
360 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
361 return map_nt_error_from_unix(errno);
364 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
367 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
368 const char *name,
369 uint32 security_info,
370 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
372 SMB_STRUCT_STAT sbuf;
374 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
376 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
377 return map_nt_error_from_unix(errno);
380 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
383 enum smbacl4_mode_enum {e_simple=0, e_special=1};
384 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
386 typedef struct _smbacl4_vfs_params {
387 enum smbacl4_mode_enum mode;
388 bool do_chown;
389 enum smbacl4_acedup_enum acedup;
390 struct db_context *sid_mapping_table;
391 } smbacl4_vfs_params;
394 * Gather special parameters for NFS4 ACL handling
396 static int smbacl4_get_vfs_params(
397 const char *type_name,
398 files_struct *fsp,
399 smbacl4_vfs_params *params
402 static const struct enum_list enum_smbacl4_modes[] = {
403 { e_simple, "simple" },
404 { e_special, "special" }
406 static const struct enum_list enum_smbacl4_acedups[] = {
407 { e_dontcare, "dontcare" },
408 { e_reject, "reject" },
409 { e_ignore, "ignore" },
410 { e_merge, "merge" },
413 memset(params, 0, sizeof(smbacl4_vfs_params));
414 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
415 SNUM(fsp->conn), type_name,
416 "mode", enum_smbacl4_modes, e_simple);
417 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
418 "chown", True);
419 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
420 SNUM(fsp->conn), type_name,
421 "acedup", enum_smbacl4_acedups, e_dontcare);
423 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
424 enum_smbacl4_modes[params->mode].name,
425 params->do_chown ? "true" : "false",
426 enum_smbacl4_acedups[params->acedup].name));
428 return 0;
431 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *theacl)
433 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
434 SMB_ACE4_INT_T *aceint;
436 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
438 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
439 SMB_ACE4PROP_T *ace = &aceint->prop;
441 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
442 ace->aceType,
443 ace->aceFlags, ace->flags,
444 ace->aceMask,
445 ace->who.id));
450 * Find 2 NFS4 who-special ACE property (non-copy!!!)
451 * match nonzero if "special" and who is equal
452 * return ace if found matching; otherwise NULL
454 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
455 SMB4ACL_T *theacl,
456 SMB_ACE4PROP_T *aceNew)
458 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
459 SMB_ACE4_INT_T *aceint;
461 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
462 SMB_ACE4PROP_T *ace = &aceint->prop;
464 DEBUG(10,("ace type:0x%x flags:0x%x aceFlags:0x%x "
465 "new type:0x%x flags:0x%x aceFlags:0x%x\n",
466 ace->aceType, ace->flags, ace->aceFlags,
467 aceNew->aceType, aceNew->flags,aceNew->aceFlags));
469 if (ace->flags == aceNew->flags &&
470 ace->aceType==aceNew->aceType &&
471 ((ace->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)==
472 (aceNew->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)) &&
473 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
474 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
476 /* keep type safety; e.g. gid is an u.short */
477 if (ace->flags & SMB_ACE4_ID_SPECIAL)
479 if (ace->who.special_id==aceNew->who.special_id)
480 return ace;
481 } else {
482 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
484 if (ace->who.gid==aceNew->who.gid)
485 return ace;
486 } else {
487 if (ace->who.uid==aceNew->who.uid)
488 return ace;
494 return NULL;
497 static bool nfs4_map_sid(smbacl4_vfs_params *params, const struct dom_sid *src,
498 struct dom_sid *dst)
500 static struct db_context *mapping_db = NULL;
501 TDB_DATA data;
503 if (mapping_db == NULL) {
504 const char *dbname = lp_parm_const_string(
505 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
507 if (dbname == NULL) {
508 DEBUG(10, ("%s:sidmap not defined\n",
509 SMBACL4_PARAM_TYPE_NAME));
510 return False;
513 become_root();
514 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
515 O_RDONLY, 0600);
516 unbecome_root();
518 if (mapping_db == NULL) {
519 DEBUG(1, ("could not open sidmap: %s\n",
520 strerror(errno)));
521 return False;
525 if (mapping_db->fetch(mapping_db, NULL,
526 string_term_tdb_data(sid_string_tos(src)),
527 &data) == -1) {
528 DEBUG(10, ("could not find mapping for SID %s\n",
529 sid_string_dbg(src)));
530 return False;
533 if ((data.dptr == NULL) || (data.dsize <= 0)
534 || (data.dptr[data.dsize-1] != '\0')) {
535 DEBUG(5, ("invalid mapping for SID %s\n",
536 sid_string_dbg(src)));
537 TALLOC_FREE(data.dptr);
538 return False;
541 if (!string_to_sid(dst, (char *)data.dptr)) {
542 DEBUG(1, ("invalid mapping %s for SID %s\n",
543 (char *)data.dptr, sid_string_dbg(src)));
544 TALLOC_FREE(data.dptr);
545 return False;
548 TALLOC_FREE(data.dptr);
550 return True;
553 static bool smbacl4_fill_ace4(
554 TALLOC_CTX *mem_ctx,
555 const char *filename,
556 smbacl4_vfs_params *params,
557 uid_t ownerUID,
558 gid_t ownerGID,
559 const struct security_ace *ace_nt, /* input */
560 SMB_ACE4PROP_T *ace_v4 /* output */
563 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
565 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
566 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
567 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
568 if (ace_nt->flags & SEC_ACE_FLAG_INHERITED_ACE) {
569 ace_v4->aceFlags |= SMB_ACE4_INHERITED_ACE;
571 ace_v4->aceMask = ace_nt->access_mask &
572 (SEC_STD_ALL | SEC_FILE_ALL);
574 se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
576 if (ace_v4->aceFlags!=ace_nt->flags)
577 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
578 ace_v4->aceFlags, ace_nt->flags));
580 if (ace_v4->aceMask!=ace_nt->access_mask)
581 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
582 ace_v4->aceMask, ace_nt->access_mask));
584 if (dom_sid_equal(&ace_nt->trustee, &global_sid_World)) {
585 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
586 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
587 } else {
588 const char *dom, *name;
589 enum lsa_SidType type;
590 uid_t uid;
591 gid_t gid;
592 struct dom_sid sid;
594 sid_copy(&sid, &ace_nt->trustee);
596 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
598 struct dom_sid mapped;
600 if (!nfs4_map_sid(params, &sid, &mapped)) {
601 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
602 "unknown\n", filename, sid_string_dbg(&sid)));
603 errno = EINVAL;
604 return False;
607 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
608 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
610 if (!lookup_sid(mem_ctx, &mapped, &dom,
611 &name, &type)) {
612 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
613 "mapped from %s is unknown\n",
614 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
615 errno = EINVAL;
616 return False;
619 sid_copy(&sid, &mapped);
622 if (type == SID_NAME_USER) {
623 if (!sid_to_uid(&sid, &uid)) {
624 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
625 "convert %s to uid\n", filename,
626 sid_string_dbg(&sid)));
627 return False;
630 if (params->mode==e_special && uid==ownerUID) {
631 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
632 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
633 } else {
634 ace_v4->who.uid = uid;
636 } else { /* else group? - TODO check it... */
637 if (!sid_to_gid(&sid, &gid)) {
638 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
639 "convert %s to gid\n", filename,
640 sid_string_dbg(&sid)));
641 return False;
644 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
646 if (params->mode==e_special && gid==ownerGID) {
647 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
648 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
649 } else {
650 ace_v4->who.gid = gid;
655 return True; /* OK */
658 static int smbacl4_MergeIgnoreReject(
659 enum smbacl4_acedup_enum acedup,
660 SMB4ACL_T *theacl, /* may modify it */
661 SMB_ACE4PROP_T *ace, /* the "new" ACE */
662 bool *paddNewACE,
663 int i
666 int result = 0;
667 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(theacl, ace);
668 if (ace4found)
670 switch(acedup)
672 case e_merge: /* "merge" flags */
673 *paddNewACE = False;
674 ace4found->aceFlags |= ace->aceFlags;
675 ace4found->aceMask |= ace->aceMask;
676 break;
677 case e_ignore: /* leave out this record */
678 *paddNewACE = False;
679 break;
680 case e_reject: /* do an error */
681 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
682 errno = EINVAL; /* SHOULD be set on any _real_ error */
683 result = -1;
684 break;
685 default:
686 break;
689 return result;
692 static SMB4ACL_T *smbacl4_win2nfs4(
693 const char *filename,
694 const struct security_acl *dacl,
695 smbacl4_vfs_params *pparams,
696 uid_t ownerUID,
697 gid_t ownerGID
700 SMB4ACL_T *theacl;
701 uint32 i;
702 TALLOC_CTX *mem_ctx = talloc_tos();
704 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
706 theacl = smb_create_smb4acl();
707 if (theacl==NULL)
708 return NULL;
710 for(i=0; i<dacl->num_aces; i++) {
711 SMB_ACE4PROP_T ace_v4;
712 bool addNewACE = True;
714 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
715 ownerUID, ownerGID,
716 dacl->aces + i, &ace_v4)) {
717 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
718 filename,
719 sid_string_dbg(&((dacl->aces+i)->trustee))));
720 continue;
723 if (pparams->acedup!=e_dontcare) {
724 if (smbacl4_MergeIgnoreReject(pparams->acedup, theacl,
725 &ace_v4, &addNewACE, i))
726 return NULL;
729 if (addNewACE)
730 smb_add_ace4(theacl, &ace_v4);
733 return theacl;
736 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
737 uint32 security_info_sent,
738 const struct security_descriptor *psd,
739 set_nfs4acl_native_fn_t set_nfs4_native)
741 smbacl4_vfs_params params;
742 SMB4ACL_T *theacl = NULL;
743 bool result;
745 SMB_STRUCT_STAT sbuf;
746 bool set_acl_as_root = false;
747 uid_t newUID = (uid_t)-1;
748 gid_t newGID = (gid_t)-1;
749 int saved_errno;
751 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
753 if ((security_info_sent & (SECINFO_DACL |
754 SECINFO_GROUP | SECINFO_OWNER)) == 0)
756 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
757 security_info_sent));
758 return NT_STATUS_OK; /* won't show error - later to be refined... */
761 /* Special behaviours */
762 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
763 return NT_STATUS_NO_MEMORY;
765 if (smbacl4_fGetFileOwner(fsp, &sbuf))
766 return map_nt_error_from_unix(errno);
768 if (params.do_chown) {
769 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
770 NTSTATUS status = unpack_nt_owners(fsp->conn, &newUID, &newGID, security_info_sent, psd);
771 if (!NT_STATUS_IS_OK(status)) {
772 DEBUG(8, ("unpack_nt_owners failed"));
773 return status;
775 if (((newUID != (uid_t)-1) && (sbuf.st_ex_uid != newUID)) ||
776 ((newGID != (gid_t)-1) && (sbuf.st_ex_gid != newGID))) {
778 status = try_chown(fsp, newUID, newGID);
779 if (!NT_STATUS_IS_OK(status)) {
780 DEBUG(3,("chown %s, %u, %u failed. Error = "
781 "%s.\n", fsp_str_dbg(fsp),
782 (unsigned int)newUID,
783 (unsigned int)newGID,
784 nt_errstr(status)));
785 return status;
788 DEBUG(10,("chown %s, %u, %u succeeded.\n",
789 fsp_str_dbg(fsp), (unsigned int)newUID,
790 (unsigned int)newGID));
791 if (smbacl4_GetFileOwner(fsp->conn,
792 fsp->fsp_name->base_name,
793 &sbuf))
794 return map_nt_error_from_unix(errno);
796 /* If we successfully chowned, we know we must
797 * be able to set the acl, so do it as root.
799 set_acl_as_root = true;
803 if (!(security_info_sent & SECINFO_DACL) || psd->dacl ==NULL) {
804 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
805 return NT_STATUS_OK;
808 theacl = smbacl4_win2nfs4(fsp->fsp_name->base_name, psd->dacl, &params,
809 sbuf.st_ex_uid, sbuf.st_ex_gid);
810 if (!theacl)
811 return map_nt_error_from_unix(errno);
813 smbacl4_dump_nfs4acl(10, theacl);
815 if (set_acl_as_root) {
816 become_root();
818 result = set_nfs4_native(fsp, theacl);
819 saved_errno = errno;
820 if (set_acl_as_root) {
821 unbecome_root();
823 if (result!=True) {
824 errno = saved_errno;
825 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
826 return map_nt_error_from_unix(errno);
829 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
830 return NT_STATUS_OK;