s3: Remove smbd_messaging_context() from reply_writeunlock()
[Samba/gebeck_regimport.git] / source3 / modules / nfs4_acls.c
blob36a3057e669b72659ecefa77162719ccb1172525
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 "nfs4_acls.h"
22 #include "librpc/gen_ndr/ndr_security.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_ACLS
27 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
29 extern const struct generic_mapping file_generic_mapping;
31 #define SMB_ACE4_INT_MAGIC 0x76F8A967
32 typedef struct _SMB_ACE4_INT_T
34 uint32 magic;
35 SMB_ACE4PROP_T prop;
36 void *next;
37 } SMB_ACE4_INT_T;
39 #define SMB_ACL4_INT_MAGIC 0x29A3E792
40 typedef struct _SMB_ACL4_INT_T
42 uint32 magic;
43 uint32 naces;
44 SMB_ACE4_INT_T *first;
45 SMB_ACE4_INT_T *last;
46 } SMB_ACL4_INT_T;
48 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *theacl)
50 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
51 if (theacl==NULL)
53 DEBUG(2, ("acl is NULL\n"));
54 errno = EINVAL;
55 return NULL;
57 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
59 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
60 errno = EINVAL;
61 return NULL;
63 return aclint;
66 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
68 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
69 if (ace==NULL)
71 DEBUG(2, ("ace is NULL\n"));
72 errno = EINVAL;
73 return NULL;
75 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
77 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
78 errno = EINVAL;
79 return NULL;
81 return aceint;
84 SMB4ACL_T *smb_create_smb4acl(void)
86 TALLOC_CTX *mem_ctx = talloc_tos();
87 SMB_ACL4_INT_T *theacl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
88 if (theacl==NULL)
90 DEBUG(0, ("TALLOC_SIZE failed\n"));
91 errno = ENOMEM;
92 return NULL;
94 theacl->magic = SMB_ACL4_INT_MAGIC;
95 /* theacl->first, last = NULL not needed */
96 return (SMB4ACL_T *)theacl;
99 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *theacl, SMB_ACE4PROP_T *prop)
101 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
102 TALLOC_CTX *mem_ctx = talloc_tos();
103 SMB_ACE4_INT_T *ace;
105 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
106 if (ace==NULL)
108 DEBUG(0, ("TALLOC_SIZE failed\n"));
109 errno = ENOMEM;
110 return NULL;
112 ace->magic = SMB_ACE4_INT_MAGIC;
113 /* ace->next = NULL not needed */
114 memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
116 if (aclint->first==NULL)
118 aclint->first = ace;
119 aclint->last = ace;
120 } else {
121 aclint->last->next = (void *)ace;
122 aclint->last = ace;
124 aclint->naces++;
126 return (SMB4ACE_T *)ace;
129 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
131 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
132 if (aceint==NULL)
133 return NULL;
135 return &aceint->prop;
138 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
140 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
141 if (aceint==NULL)
142 return NULL;
144 return (SMB4ACE_T *)aceint->next;
147 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *theacl)
149 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
150 if (aclint==NULL)
151 return NULL;
153 return (SMB4ACE_T *)aclint->first;
156 uint32 smb_get_naces(SMB4ACL_T *theacl)
158 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
159 if (aclint==NULL)
160 return 0;
162 return aclint->naces;
165 static int smbacl4_GetFileOwner(struct connection_struct *conn,
166 const char *filename,
167 SMB_STRUCT_STAT *psbuf)
169 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
171 /* Get the stat struct for the owner info. */
172 if (vfs_stat_smb_fname(conn, filename, psbuf) != 0)
174 DEBUG(8, ("vfs_stat_smb_fname failed with error %s\n",
175 strerror(errno)));
176 return -1;
179 return 0;
182 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
184 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
186 if (fsp->is_directory || fsp->fh->fd == -1) {
187 return smbacl4_GetFileOwner(fsp->conn,
188 fsp->fsp_name->base_name, psbuf);
190 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
192 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
193 strerror(errno)));
194 return -1;
197 return 0;
200 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *theacl, /* in */
201 struct dom_sid *psid_owner, /* in */
202 struct dom_sid *psid_group, /* in */
203 bool is_directory, /* in */
204 struct security_ace **ppnt_ace_list, /* out */
205 int *pgood_aces /* out */
208 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
209 SMB_ACE4_INT_T *aceint;
210 struct security_ace *nt_ace_list = NULL;
211 int good_aces = 0;
213 DEBUG(10, ("smbacl_nfs42win entered\n"));
215 aclint = get_validated_aclint(theacl);
216 /* We do not check for naces being 0 or theacl being NULL here because it is done upstream */
217 /* in smb_get_nt_acl_nfs4(). */
218 nt_ace_list = (struct security_ace *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(struct security_ace));
219 if (nt_ace_list==NULL)
221 DEBUG(10, ("talloc error"));
222 errno = ENOMEM;
223 return False;
226 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
227 uint32_t mask;
228 struct dom_sid sid;
229 SMB_ACE4PROP_T *ace = &aceint->prop;
230 uint32_t mapped_ace_flags;
232 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
233 "who: %d\n", aceint->magic, ace->aceType, ace->flags,
234 ace->aceFlags, ace->aceMask, ace->who.id));
236 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
238 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
239 switch (ace->who.special_id) {
240 case SMB_ACE4_WHO_OWNER:
241 sid_copy(&sid, psid_owner);
242 break;
243 case SMB_ACE4_WHO_GROUP:
244 sid_copy(&sid, psid_group);
245 break;
246 case SMB_ACE4_WHO_EVERYONE:
247 sid_copy(&sid, &global_sid_World);
248 break;
249 default:
250 DEBUG(8, ("invalid special who id %d "
251 "ignored\n", ace->who.special_id));
253 } else {
254 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
255 gid_to_sid(&sid, ace->who.gid);
256 } else {
257 uid_to_sid(&sid, ace->who.uid);
260 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
261 sid_string_dbg(&sid)));
263 if (is_directory && (ace->aceMask & SMB_ACE4_ADD_FILE)) {
264 ace->aceMask |= SMB_ACE4_DELETE_CHILD;
267 mapped_ace_flags = ace->aceFlags & 0xf;
268 if (!is_directory && (mapped_ace_flags & (SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE))) {
270 * GPFS sets inherits dir_inhert and file_inherit flags
271 * to files, too, which confuses windows, and seems to
272 * be wrong anyways. ==> Map these bits away for files.
274 DEBUG(10, ("removing inherit flags from nfs4 ace\n"));
275 mapped_ace_flags &= ~(SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE);
277 DEBUG(10, ("mapped ace flags: 0x%x => 0x%x\n",
278 ace->aceFlags, mapped_ace_flags));
280 mask = ace->aceMask;
281 init_sec_ace(&nt_ace_list[good_aces++], &sid,
282 ace->aceType, mask,
283 mapped_ace_flags);
286 *ppnt_ace_list = nt_ace_list;
287 *pgood_aces = good_aces;
289 return True;
292 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
293 uint32 security_info,
294 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
296 int good_aces = 0;
297 struct dom_sid sid_owner, sid_group;
298 size_t sd_size = 0;
299 struct security_ace *nt_ace_list = NULL;
300 struct security_acl *psa = NULL;
301 TALLOC_CTX *mem_ctx = talloc_tos();
303 if (theacl==NULL || smb_get_naces(theacl)==0)
304 return NT_STATUS_ACCESS_DENIED; /* special because we
305 * shouldn't alloc 0 for
306 * win */
308 uid_to_sid(&sid_owner, sbuf->st_ex_uid);
309 gid_to_sid(&sid_group, sbuf->st_ex_gid);
311 if (smbacl4_nfs42win(mem_ctx, theacl, &sid_owner, &sid_group,
312 S_ISDIR(sbuf->st_ex_mode),
313 &nt_ace_list, &good_aces)==False) {
314 DEBUG(8,("smbacl4_nfs42win failed\n"));
315 return map_nt_error_from_unix(errno);
318 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
319 if (psa == NULL) {
320 DEBUG(2,("make_sec_acl failed\n"));
321 return NT_STATUS_NO_MEMORY;
324 DEBUG(10,("after make sec_acl\n"));
325 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
326 (security_info & SECINFO_OWNER) ? &sid_owner : NULL,
327 (security_info & SECINFO_GROUP) ? &sid_group : NULL,
328 NULL, psa, &sd_size);
329 if (*ppdesc==NULL) {
330 DEBUG(2,("make_sec_desc failed\n"));
331 return NT_STATUS_NO_MEMORY;
334 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
335 (int)ndr_size_security_descriptor(*ppdesc, 0)));
337 return NT_STATUS_OK;
340 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
341 uint32 security_info,
342 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
344 SMB_STRUCT_STAT sbuf;
346 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
348 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
349 return map_nt_error_from_unix(errno);
352 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
355 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
356 const char *name,
357 uint32 security_info,
358 struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
360 SMB_STRUCT_STAT sbuf;
362 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
364 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
365 return map_nt_error_from_unix(errno);
368 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
371 enum smbacl4_mode_enum {e_simple=0, e_special=1};
372 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
374 typedef struct _smbacl4_vfs_params {
375 enum smbacl4_mode_enum mode;
376 bool do_chown;
377 enum smbacl4_acedup_enum acedup;
378 struct db_context *sid_mapping_table;
379 } smbacl4_vfs_params;
382 * Gather special parameters for NFS4 ACL handling
384 static int smbacl4_get_vfs_params(
385 const char *type_name,
386 files_struct *fsp,
387 smbacl4_vfs_params *params
390 static const struct enum_list enum_smbacl4_modes[] = {
391 { e_simple, "simple" },
392 { e_special, "special" }
394 static const struct enum_list enum_smbacl4_acedups[] = {
395 { e_dontcare, "dontcare" },
396 { e_reject, "reject" },
397 { e_ignore, "ignore" },
398 { e_merge, "merge" },
401 memset(params, 0, sizeof(smbacl4_vfs_params));
402 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
403 SNUM(fsp->conn), type_name,
404 "mode", enum_smbacl4_modes, e_simple);
405 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
406 "chown", True);
407 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
408 SNUM(fsp->conn), type_name,
409 "acedup", enum_smbacl4_acedups, e_dontcare);
411 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
412 enum_smbacl4_modes[params->mode].name,
413 params->do_chown ? "true" : "false",
414 enum_smbacl4_acedups[params->acedup].name));
416 return 0;
419 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *theacl)
421 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
422 SMB_ACE4_INT_T *aceint;
424 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
426 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
427 SMB_ACE4PROP_T *ace = &aceint->prop;
429 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
430 ace->aceType,
431 ace->aceFlags, ace->flags,
432 ace->aceMask,
433 ace->who.id));
438 * Find 2 NFS4 who-special ACE property (non-copy!!!)
439 * match nonzero if "special" and who is equal
440 * return ace if found matching; otherwise NULL
442 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
443 SMB4ACL_T *theacl,
444 SMB_ACE4PROP_T *aceNew)
446 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
447 SMB_ACE4_INT_T *aceint;
449 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
450 SMB_ACE4PROP_T *ace = &aceint->prop;
452 DEBUG(10,("ace type:0x%x flags:0x%x aceFlags:0x%x "
453 "new type:0x%x flags:0x%x aceFlags:0x%x\n",
454 ace->aceType, ace->flags, ace->aceFlags,
455 aceNew->aceType, aceNew->flags,aceNew->aceFlags));
457 if (ace->flags == aceNew->flags &&
458 ace->aceType==aceNew->aceType &&
459 ((ace->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)==
460 (aceNew->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)) &&
461 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
462 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
464 /* keep type safety; e.g. gid is an u.short */
465 if (ace->flags & SMB_ACE4_ID_SPECIAL)
467 if (ace->who.special_id==aceNew->who.special_id)
468 return ace;
469 } else {
470 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
472 if (ace->who.gid==aceNew->who.gid)
473 return ace;
474 } else {
475 if (ace->who.uid==aceNew->who.uid)
476 return ace;
482 return NULL;
485 static bool nfs4_map_sid(smbacl4_vfs_params *params, const struct dom_sid *src,
486 struct dom_sid *dst)
488 static struct db_context *mapping_db = NULL;
489 TDB_DATA data;
491 if (mapping_db == NULL) {
492 const char *dbname = lp_parm_const_string(
493 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
495 if (dbname == NULL) {
496 DEBUG(10, ("%s:sidmap not defined\n",
497 SMBACL4_PARAM_TYPE_NAME));
498 return False;
501 become_root();
502 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
503 O_RDONLY, 0600);
504 unbecome_root();
506 if (mapping_db == NULL) {
507 DEBUG(1, ("could not open sidmap: %s\n",
508 strerror(errno)));
509 return False;
513 if (mapping_db->fetch(mapping_db, NULL,
514 string_term_tdb_data(sid_string_tos(src)),
515 &data) == -1) {
516 DEBUG(10, ("could not find mapping for SID %s\n",
517 sid_string_dbg(src)));
518 return False;
521 if ((data.dptr == NULL) || (data.dsize <= 0)
522 || (data.dptr[data.dsize-1] != '\0')) {
523 DEBUG(5, ("invalid mapping for SID %s\n",
524 sid_string_dbg(src)));
525 TALLOC_FREE(data.dptr);
526 return False;
529 if (!string_to_sid(dst, (char *)data.dptr)) {
530 DEBUG(1, ("invalid mapping %s for SID %s\n",
531 (char *)data.dptr, sid_string_dbg(src)));
532 TALLOC_FREE(data.dptr);
533 return False;
536 TALLOC_FREE(data.dptr);
538 return True;
541 static bool smbacl4_fill_ace4(
542 TALLOC_CTX *mem_ctx,
543 const char *filename,
544 smbacl4_vfs_params *params,
545 uid_t ownerUID,
546 gid_t ownerGID,
547 const struct security_ace *ace_nt, /* input */
548 SMB_ACE4PROP_T *ace_v4 /* output */
551 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
553 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
554 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
555 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
556 ace_v4->aceMask = ace_nt->access_mask &
557 (SEC_STD_ALL | SEC_FILE_ALL);
559 se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
561 if (ace_v4->aceFlags!=ace_nt->flags)
562 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
563 ace_v4->aceFlags, ace_nt->flags));
565 if (ace_v4->aceMask!=ace_nt->access_mask)
566 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
567 ace_v4->aceMask, ace_nt->access_mask));
569 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
570 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
571 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
572 } else {
573 const char *dom, *name;
574 enum lsa_SidType type;
575 uid_t uid;
576 gid_t gid;
577 struct dom_sid sid;
579 sid_copy(&sid, &ace_nt->trustee);
581 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
583 struct dom_sid mapped;
585 if (!nfs4_map_sid(params, &sid, &mapped)) {
586 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
587 "unknown\n", filename, sid_string_dbg(&sid)));
588 errno = EINVAL;
589 return False;
592 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
593 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
595 if (!lookup_sid(mem_ctx, &mapped, &dom,
596 &name, &type)) {
597 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
598 "mapped from %s is unknown\n",
599 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
600 errno = EINVAL;
601 return False;
604 sid_copy(&sid, &mapped);
607 if (type == SID_NAME_USER) {
608 if (!sid_to_uid(&sid, &uid)) {
609 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
610 "convert %s to uid\n", filename,
611 sid_string_dbg(&sid)));
612 return False;
615 if (params->mode==e_special && uid==ownerUID) {
616 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
617 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
618 } else {
619 ace_v4->who.uid = uid;
621 } else { /* else group? - TODO check it... */
622 if (!sid_to_gid(&sid, &gid)) {
623 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
624 "convert %s to gid\n", filename,
625 sid_string_dbg(&sid)));
626 return False;
629 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
631 if (params->mode==e_special && gid==ownerGID) {
632 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
633 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
634 } else {
635 ace_v4->who.gid = gid;
640 return True; /* OK */
643 static int smbacl4_MergeIgnoreReject(
644 enum smbacl4_acedup_enum acedup,
645 SMB4ACL_T *theacl, /* may modify it */
646 SMB_ACE4PROP_T *ace, /* the "new" ACE */
647 bool *paddNewACE,
648 int i
651 int result = 0;
652 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(theacl, ace);
653 if (ace4found)
655 switch(acedup)
657 case e_merge: /* "merge" flags */
658 *paddNewACE = False;
659 ace4found->aceFlags |= ace->aceFlags;
660 ace4found->aceMask |= ace->aceMask;
661 break;
662 case e_ignore: /* leave out this record */
663 *paddNewACE = False;
664 break;
665 case e_reject: /* do an error */
666 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
667 errno = EINVAL; /* SHOULD be set on any _real_ error */
668 result = -1;
669 break;
670 default:
671 break;
674 return result;
677 static SMB4ACL_T *smbacl4_win2nfs4(
678 const char *filename,
679 const struct security_acl *dacl,
680 smbacl4_vfs_params *pparams,
681 uid_t ownerUID,
682 gid_t ownerGID
685 SMB4ACL_T *theacl;
686 uint32 i;
687 TALLOC_CTX *mem_ctx = talloc_tos();
689 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
691 theacl = smb_create_smb4acl();
692 if (theacl==NULL)
693 return NULL;
695 for(i=0; i<dacl->num_aces; i++) {
696 SMB_ACE4PROP_T ace_v4;
697 bool addNewACE = True;
699 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
700 ownerUID, ownerGID,
701 dacl->aces + i, &ace_v4)) {
702 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
703 filename,
704 sid_string_dbg(&((dacl->aces+i)->trustee))));
705 continue;
708 if (pparams->acedup!=e_dontcare) {
709 if (smbacl4_MergeIgnoreReject(pparams->acedup, theacl,
710 &ace_v4, &addNewACE, i))
711 return NULL;
714 if (addNewACE)
715 smb_add_ace4(theacl, &ace_v4);
718 return theacl;
721 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
722 uint32 security_info_sent,
723 const struct security_descriptor *psd,
724 set_nfs4acl_native_fn_t set_nfs4_native)
726 smbacl4_vfs_params params;
727 SMB4ACL_T *theacl = NULL;
728 bool result;
730 SMB_STRUCT_STAT sbuf;
731 bool set_acl_as_root = false;
732 uid_t newUID = (uid_t)-1;
733 gid_t newGID = (gid_t)-1;
734 int saved_errno;
736 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
738 if ((security_info_sent & (SECINFO_DACL |
739 SECINFO_GROUP | SECINFO_OWNER)) == 0)
741 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
742 security_info_sent));
743 return NT_STATUS_OK; /* won't show error - later to be refined... */
746 /* Special behaviours */
747 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
748 return NT_STATUS_NO_MEMORY;
750 if (smbacl4_fGetFileOwner(fsp, &sbuf))
751 return map_nt_error_from_unix(errno);
753 if (params.do_chown) {
754 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
755 NTSTATUS status = unpack_nt_owners(fsp->conn, &newUID, &newGID, security_info_sent, psd);
756 if (!NT_STATUS_IS_OK(status)) {
757 DEBUG(8, ("unpack_nt_owners failed"));
758 return status;
760 if (((newUID != (uid_t)-1) && (sbuf.st_ex_uid != newUID)) ||
761 ((newGID != (gid_t)-1) && (sbuf.st_ex_gid != newGID))) {
763 if(try_chown(fsp->conn, fsp->fsp_name, newUID,
764 newGID)) {
765 DEBUG(3,("chown %s, %u, %u failed. Error = "
766 "%s.\n", fsp_str_dbg(fsp),
767 (unsigned int)newUID,
768 (unsigned int)newGID,
769 strerror(errno)));
770 return map_nt_error_from_unix(errno);
773 DEBUG(10,("chown %s, %u, %u succeeded.\n",
774 fsp_str_dbg(fsp), (unsigned int)newUID,
775 (unsigned int)newGID));
776 if (smbacl4_GetFileOwner(fsp->conn,
777 fsp->fsp_name->base_name,
778 &sbuf))
779 return map_nt_error_from_unix(errno);
781 /* If we successfully chowned, we know we must
782 * be able to set the acl, so do it as root.
784 set_acl_as_root = true;
788 if (!(security_info_sent & SECINFO_DACL) || psd->dacl ==NULL) {
789 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
790 return NT_STATUS_OK;
793 theacl = smbacl4_win2nfs4(fsp->fsp_name->base_name, psd->dacl, &params,
794 sbuf.st_ex_uid, sbuf.st_ex_gid);
795 if (!theacl)
796 return map_nt_error_from_unix(errno);
798 smbacl4_dump_nfs4acl(10, theacl);
800 if (set_acl_as_root) {
801 become_root();
803 result = set_nfs4_native(fsp, theacl);
804 saved_errno = errno;
805 if (set_acl_as_root) {
806 unbecome_root();
808 if (result!=True) {
809 errno = saved_errno;
810 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
811 return map_nt_error_from_unix(errno);
814 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
815 return NT_STATUS_OK;