s3: Allow up to 20480 entries in LookupSids, as mandated by the official IDL
[Samba.git] / source3 / modules / nfs4_acls.c
blob658f2b4e8de877ebb0120a587d4a4d2fa9cc8de7
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"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_ACLS
26 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
28 extern const struct generic_mapping file_generic_mapping;
30 #define SMB_ACE4_INT_MAGIC 0x76F8A967
31 typedef struct _SMB_ACE4_INT_T
33 uint32 magic;
34 SMB_ACE4PROP_T prop;
35 void *next;
36 } SMB_ACE4_INT_T;
38 #define SMB_ACL4_INT_MAGIC 0x29A3E792
39 typedef struct _SMB_ACL4_INT_T
41 uint32 magic;
42 uint32 naces;
43 SMB_ACE4_INT_T *first;
44 SMB_ACE4_INT_T *last;
45 } SMB_ACL4_INT_T;
47 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *theacl)
49 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
50 if (theacl==NULL)
52 DEBUG(2, ("acl is NULL\n"));
53 errno = EINVAL;
54 return NULL;
56 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
58 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
59 errno = EINVAL;
60 return NULL;
62 return aclint;
65 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
67 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
68 if (ace==NULL)
70 DEBUG(2, ("ace is NULL\n"));
71 errno = EINVAL;
72 return NULL;
74 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
76 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
77 errno = EINVAL;
78 return NULL;
80 return aceint;
83 SMB4ACL_T *smb_create_smb4acl(void)
85 TALLOC_CTX *mem_ctx = talloc_tos();
86 SMB_ACL4_INT_T *theacl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
87 if (theacl==NULL)
89 DEBUG(0, ("TALLOC_SIZE failed\n"));
90 errno = ENOMEM;
91 return NULL;
93 theacl->magic = SMB_ACL4_INT_MAGIC;
94 /* theacl->first, last = NULL not needed */
95 return (SMB4ACL_T *)theacl;
98 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *theacl, SMB_ACE4PROP_T *prop)
100 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
101 TALLOC_CTX *mem_ctx = talloc_tos();
102 SMB_ACE4_INT_T *ace;
104 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
105 if (ace==NULL)
107 DEBUG(0, ("TALLOC_SIZE failed\n"));
108 errno = ENOMEM;
109 return NULL;
111 ace->magic = SMB_ACE4_INT_MAGIC;
112 /* ace->next = NULL not needed */
113 memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
115 if (aclint->first==NULL)
117 aclint->first = ace;
118 aclint->last = ace;
119 } else {
120 aclint->last->next = (void *)ace;
121 aclint->last = ace;
123 aclint->naces++;
125 return (SMB4ACE_T *)ace;
128 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
130 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
131 if (aceint==NULL)
132 return NULL;
134 return &aceint->prop;
137 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
139 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
140 if (aceint==NULL)
141 return NULL;
143 return (SMB4ACE_T *)aceint->next;
146 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *theacl)
148 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
149 if (aclint==NULL)
150 return NULL;
152 return (SMB4ACE_T *)aclint->first;
155 uint32 smb_get_naces(SMB4ACL_T *theacl)
157 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
158 if (aclint==NULL)
159 return 0;
161 return aclint->naces;
164 static int smbacl4_GetFileOwner(struct connection_struct *conn,
165 const char *filename,
166 SMB_STRUCT_STAT *psbuf)
168 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
170 /* Get the stat struct for the owner info. */
171 if (vfs_stat_smb_fname(conn, filename, psbuf) != 0)
173 DEBUG(8, ("vfs_stat_smb_fname failed with error %s\n",
174 strerror(errno)));
175 return -1;
178 return 0;
181 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
183 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
185 if (fsp->is_directory || fsp->fh->fd == -1) {
186 return smbacl4_GetFileOwner(fsp->conn,
187 fsp->fsp_name->base_name, psbuf);
189 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
191 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
192 strerror(errno)));
193 return -1;
196 return 0;
199 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *theacl, /* in */
200 DOM_SID *psid_owner, /* in */
201 DOM_SID *psid_group, /* in */
202 bool is_directory, /* in */
203 SEC_ACE **ppnt_ace_list, /* out */
204 int *pgood_aces /* out */
207 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
208 SMB_ACE4_INT_T *aceint;
209 SEC_ACE *nt_ace_list = NULL;
210 int good_aces = 0;
212 DEBUG(10, ("smbacl_nfs42win entered\n"));
214 aclint = get_validated_aclint(theacl);
215 /* We do not check for naces being 0 or theacl being NULL here because it is done upstream */
216 /* in smb_get_nt_acl_nfs4(). */
217 nt_ace_list = (SEC_ACE *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(SEC_ACE));
218 if (nt_ace_list==NULL)
220 DEBUG(10, ("talloc error"));
221 errno = ENOMEM;
222 return False;
225 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
226 uint32_t mask;
227 DOM_SID sid;
228 SMB_ACE4PROP_T *ace = &aceint->prop;
229 uint32_t mapped_ace_flags;
231 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
232 "who: %d\n", aceint->magic, ace->aceType, ace->flags,
233 ace->aceFlags, ace->aceMask, ace->who.id));
235 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
237 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
238 switch (ace->who.special_id) {
239 case SMB_ACE4_WHO_OWNER:
240 sid_copy(&sid, psid_owner);
241 break;
242 case SMB_ACE4_WHO_GROUP:
243 sid_copy(&sid, psid_group);
244 break;
245 case SMB_ACE4_WHO_EVERYONE:
246 sid_copy(&sid, &global_sid_World);
247 break;
248 default:
249 DEBUG(8, ("invalid special who id %d "
250 "ignored\n", ace->who.special_id));
252 } else {
253 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
254 gid_to_sid(&sid, ace->who.gid);
255 } else {
256 uid_to_sid(&sid, ace->who.uid);
259 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
260 sid_string_dbg(&sid)));
262 if (is_directory && (ace->aceMask & SMB_ACE4_ADD_FILE)) {
263 ace->aceMask |= SMB_ACE4_DELETE_CHILD;
266 mapped_ace_flags = ace->aceFlags & 0xf;
267 if (!is_directory && (mapped_ace_flags & (SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE))) {
269 * GPFS sets inherits dir_inhert and file_inherit flags
270 * to files, too, which confuses windows, and seems to
271 * be wrong anyways. ==> Map these bits away for files.
273 DEBUG(10, ("removing inherit flags from nfs4 ace\n"));
274 mapped_ace_flags &= ~(SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE);
276 DEBUG(10, ("mapped ace flags: 0x%x => 0x%x\n",
277 ace->aceFlags, mapped_ace_flags));
279 mask = ace->aceMask;
280 init_sec_ace(&nt_ace_list[good_aces++], &sid,
281 ace->aceType, mask,
282 mapped_ace_flags);
285 *ppnt_ace_list = nt_ace_list;
286 *pgood_aces = good_aces;
288 return True;
291 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
292 uint32 security_info,
293 SEC_DESC **ppdesc, SMB4ACL_T *theacl)
295 int good_aces = 0;
296 DOM_SID sid_owner, sid_group;
297 size_t sd_size = 0;
298 SEC_ACE *nt_ace_list = NULL;
299 SEC_ACL *psa = NULL;
300 TALLOC_CTX *mem_ctx = talloc_tos();
302 if (theacl==NULL || smb_get_naces(theacl)==0)
303 return NT_STATUS_ACCESS_DENIED; /* special because we
304 * shouldn't alloc 0 for
305 * win */
307 uid_to_sid(&sid_owner, sbuf->st_ex_uid);
308 gid_to_sid(&sid_group, sbuf->st_ex_gid);
310 if (smbacl4_nfs42win(mem_ctx, theacl, &sid_owner, &sid_group,
311 S_ISDIR(sbuf->st_ex_mode),
312 &nt_ace_list, &good_aces)==False) {
313 DEBUG(8,("smbacl4_nfs42win failed\n"));
314 return map_nt_error_from_unix(errno);
317 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
318 if (psa == NULL) {
319 DEBUG(2,("make_sec_acl failed\n"));
320 return NT_STATUS_NO_MEMORY;
323 DEBUG(10,("after make sec_acl\n"));
324 *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
325 (security_info & OWNER_SECURITY_INFORMATION) ? &sid_owner : NULL,
326 (security_info & GROUP_SECURITY_INFORMATION) ? &sid_group : NULL,
327 NULL, psa, &sd_size);
328 if (*ppdesc==NULL) {
329 DEBUG(2,("make_sec_desc failed\n"));
330 return NT_STATUS_NO_MEMORY;
333 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
334 (int)ndr_size_security_descriptor(*ppdesc, NULL, 0)));
336 return NT_STATUS_OK;
339 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
340 uint32 security_info,
341 SEC_DESC **ppdesc, SMB4ACL_T *theacl)
343 SMB_STRUCT_STAT sbuf;
345 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
347 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
348 return map_nt_error_from_unix(errno);
351 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
354 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
355 const char *name,
356 uint32 security_info,
357 SEC_DESC **ppdesc, SMB4ACL_T *theacl)
359 SMB_STRUCT_STAT sbuf;
361 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
363 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
364 return map_nt_error_from_unix(errno);
367 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
370 enum smbacl4_mode_enum {e_simple=0, e_special=1};
371 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
373 typedef struct _smbacl4_vfs_params {
374 enum smbacl4_mode_enum mode;
375 bool do_chown;
376 enum smbacl4_acedup_enum acedup;
377 struct db_context *sid_mapping_table;
378 } smbacl4_vfs_params;
381 * Gather special parameters for NFS4 ACL handling
383 static int smbacl4_get_vfs_params(
384 const char *type_name,
385 files_struct *fsp,
386 smbacl4_vfs_params *params
389 static const struct enum_list enum_smbacl4_modes[] = {
390 { e_simple, "simple" },
391 { e_special, "special" }
393 static const struct enum_list enum_smbacl4_acedups[] = {
394 { e_dontcare, "dontcare" },
395 { e_reject, "reject" },
396 { e_ignore, "ignore" },
397 { e_merge, "merge" },
400 memset(params, 0, sizeof(smbacl4_vfs_params));
401 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
402 SNUM(fsp->conn), type_name,
403 "mode", enum_smbacl4_modes, e_simple);
404 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
405 "chown", True);
406 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
407 SNUM(fsp->conn), type_name,
408 "acedup", enum_smbacl4_acedups, e_dontcare);
410 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
411 enum_smbacl4_modes[params->mode].name,
412 params->do_chown ? "true" : "false",
413 enum_smbacl4_acedups[params->acedup].name));
415 return 0;
418 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *theacl)
420 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
421 SMB_ACE4_INT_T *aceint;
423 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
425 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
426 SMB_ACE4PROP_T *ace = &aceint->prop;
428 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
429 ace->aceType,
430 ace->aceFlags, ace->flags,
431 ace->aceMask,
432 ace->who.id));
437 * Find 2 NFS4 who-special ACE property (non-copy!!!)
438 * match nonzero if "special" and who is equal
439 * return ace if found matching; otherwise NULL
441 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
442 SMB4ACL_T *theacl,
443 SMB_ACE4PROP_T *aceNew)
445 SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
446 SMB_ACE4_INT_T *aceint;
448 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
449 SMB_ACE4PROP_T *ace = &aceint->prop;
451 DEBUG(10,("ace type:0x%x flags:0x%x aceFlags:0x%x "
452 "new type:0x%x flags:0x%x aceFlags:0x%x\n",
453 ace->aceType, ace->flags, ace->aceFlags,
454 aceNew->aceType, aceNew->flags,aceNew->aceFlags));
456 if (ace->flags == aceNew->flags &&
457 ace->aceType==aceNew->aceType &&
458 ((ace->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)==
459 (aceNew->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)) &&
460 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
461 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
463 /* keep type safety; e.g. gid is an u.short */
464 if (ace->flags & SMB_ACE4_ID_SPECIAL)
466 if (ace->who.special_id==aceNew->who.special_id)
467 return ace;
468 } else {
469 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
471 if (ace->who.gid==aceNew->who.gid)
472 return ace;
473 } else {
474 if (ace->who.uid==aceNew->who.uid)
475 return ace;
481 return NULL;
484 static bool nfs4_map_sid(smbacl4_vfs_params *params, const DOM_SID *src,
485 DOM_SID *dst)
487 static struct db_context *mapping_db = NULL;
488 TDB_DATA data;
490 if (mapping_db == NULL) {
491 const char *dbname = lp_parm_const_string(
492 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
494 if (dbname == NULL) {
495 DEBUG(10, ("%s:sidmap not defined\n",
496 SMBACL4_PARAM_TYPE_NAME));
497 return False;
500 become_root();
501 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
502 O_RDONLY, 0600);
503 unbecome_root();
505 if (mapping_db == NULL) {
506 DEBUG(1, ("could not open sidmap: %s\n",
507 strerror(errno)));
508 return False;
512 if (mapping_db->fetch(mapping_db, NULL,
513 string_term_tdb_data(sid_string_tos(src)),
514 &data) == -1) {
515 DEBUG(10, ("could not find mapping for SID %s\n",
516 sid_string_dbg(src)));
517 return False;
520 if ((data.dptr == NULL) || (data.dsize <= 0)
521 || (data.dptr[data.dsize-1] != '\0')) {
522 DEBUG(5, ("invalid mapping for SID %s\n",
523 sid_string_dbg(src)));
524 TALLOC_FREE(data.dptr);
525 return False;
528 if (!string_to_sid(dst, (char *)data.dptr)) {
529 DEBUG(1, ("invalid mapping %s for SID %s\n",
530 (char *)data.dptr, sid_string_dbg(src)));
531 TALLOC_FREE(data.dptr);
532 return False;
535 TALLOC_FREE(data.dptr);
537 return True;
540 static bool smbacl4_fill_ace4(
541 TALLOC_CTX *mem_ctx,
542 const char *filename,
543 smbacl4_vfs_params *params,
544 uid_t ownerUID,
545 gid_t ownerGID,
546 const SEC_ACE *ace_nt, /* input */
547 SMB_ACE4PROP_T *ace_v4 /* output */
550 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
552 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
553 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
554 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
555 ace_v4->aceMask = ace_nt->access_mask &
556 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
558 se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
560 if (ace_v4->aceFlags!=ace_nt->flags)
561 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
562 ace_v4->aceFlags, ace_nt->flags));
564 if (ace_v4->aceMask!=ace_nt->access_mask)
565 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
566 ace_v4->aceMask, ace_nt->access_mask));
568 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
569 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
570 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
571 } else {
572 const char *dom, *name;
573 enum lsa_SidType type;
574 uid_t uid;
575 gid_t gid;
576 DOM_SID sid;
578 sid_copy(&sid, &ace_nt->trustee);
580 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
582 DOM_SID mapped;
584 if (!nfs4_map_sid(params, &sid, &mapped)) {
585 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
586 "unknown\n", filename, sid_string_dbg(&sid)));
587 errno = EINVAL;
588 return False;
591 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
592 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
594 if (!lookup_sid(mem_ctx, &mapped, &dom,
595 &name, &type)) {
596 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
597 "mapped from %s is unknown\n",
598 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
599 errno = EINVAL;
600 return False;
603 sid_copy(&sid, &mapped);
606 if (type == SID_NAME_USER) {
607 if (!sid_to_uid(&sid, &uid)) {
608 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
609 "convert %s to uid\n", filename,
610 sid_string_dbg(&sid)));
611 return False;
614 if (params->mode==e_special && uid==ownerUID) {
615 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
616 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
617 } else {
618 ace_v4->who.uid = uid;
620 } else { /* else group? - TODO check it... */
621 if (!sid_to_gid(&sid, &gid)) {
622 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
623 "convert %s to gid\n", filename,
624 sid_string_dbg(&sid)));
625 return False;
628 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
630 if (params->mode==e_special && gid==ownerGID) {
631 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
632 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
633 } else {
634 ace_v4->who.gid = gid;
639 return True; /* OK */
642 static int smbacl4_MergeIgnoreReject(
643 enum smbacl4_acedup_enum acedup,
644 SMB4ACL_T *theacl, /* may modify it */
645 SMB_ACE4PROP_T *ace, /* the "new" ACE */
646 bool *paddNewACE,
647 int i
650 int result = 0;
651 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(theacl, ace);
652 if (ace4found)
654 switch(acedup)
656 case e_merge: /* "merge" flags */
657 *paddNewACE = False;
658 ace4found->aceFlags |= ace->aceFlags;
659 ace4found->aceMask |= ace->aceMask;
660 break;
661 case e_ignore: /* leave out this record */
662 *paddNewACE = False;
663 break;
664 case e_reject: /* do an error */
665 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
666 errno = EINVAL; /* SHOULD be set on any _real_ error */
667 result = -1;
668 break;
669 default:
670 break;
673 return result;
676 static SMB4ACL_T *smbacl4_win2nfs4(
677 const char *filename,
678 const SEC_ACL *dacl,
679 smbacl4_vfs_params *pparams,
680 uid_t ownerUID,
681 gid_t ownerGID
684 SMB4ACL_T *theacl;
685 uint32 i;
686 TALLOC_CTX *mem_ctx = talloc_tos();
688 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
690 theacl = smb_create_smb4acl();
691 if (theacl==NULL)
692 return NULL;
694 for(i=0; i<dacl->num_aces; i++) {
695 SMB_ACE4PROP_T ace_v4;
696 bool addNewACE = True;
698 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
699 ownerUID, ownerGID,
700 dacl->aces + i, &ace_v4)) {
701 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
702 filename,
703 sid_string_dbg(&((dacl->aces+i)->trustee))));
704 continue;
707 if (pparams->acedup!=e_dontcare) {
708 if (smbacl4_MergeIgnoreReject(pparams->acedup, theacl,
709 &ace_v4, &addNewACE, i))
710 return NULL;
713 if (addNewACE)
714 smb_add_ace4(theacl, &ace_v4);
717 return theacl;
720 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
721 uint32 security_info_sent,
722 const SEC_DESC *psd,
723 set_nfs4acl_native_fn_t set_nfs4_native)
725 smbacl4_vfs_params params;
726 SMB4ACL_T *theacl = NULL;
727 bool result;
729 SMB_STRUCT_STAT sbuf;
730 bool set_acl_as_root = false;
731 uid_t newUID = (uid_t)-1;
732 gid_t newGID = (gid_t)-1;
733 int saved_errno;
735 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
737 if ((security_info_sent & (DACL_SECURITY_INFORMATION |
738 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
740 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
741 security_info_sent));
742 return NT_STATUS_OK; /* won't show error - later to be refined... */
745 /* Special behaviours */
746 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
747 return NT_STATUS_NO_MEMORY;
749 if (smbacl4_fGetFileOwner(fsp, &sbuf))
750 return map_nt_error_from_unix(errno);
752 if (params.do_chown) {
753 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
754 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
755 if (!NT_STATUS_IS_OK(status)) {
756 DEBUG(8, ("unpack_nt_owners failed"));
757 return status;
759 if (((newUID != (uid_t)-1) && (sbuf.st_ex_uid != newUID)) ||
760 ((newGID != (gid_t)-1) && (sbuf.st_ex_gid != newGID))) {
762 if(try_chown(fsp->conn, fsp->fsp_name, newUID,
763 newGID)) {
764 DEBUG(3,("chown %s, %u, %u failed. Error = "
765 "%s.\n", fsp_str_dbg(fsp),
766 (unsigned int)newUID,
767 (unsigned int)newGID,
768 strerror(errno)));
769 return map_nt_error_from_unix(errno);
772 DEBUG(10,("chown %s, %u, %u succeeded.\n",
773 fsp_str_dbg(fsp), (unsigned int)newUID,
774 (unsigned int)newGID));
775 if (smbacl4_GetFileOwner(fsp->conn,
776 fsp->fsp_name->base_name,
777 &sbuf))
778 return map_nt_error_from_unix(errno);
780 /* If we successfully chowned, we know we must
781 * be able to set the acl, so do it as root.
783 set_acl_as_root = true;
787 if (!(security_info_sent & DACL_SECURITY_INFORMATION) || psd->dacl ==NULL) {
788 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
789 return NT_STATUS_OK;
792 theacl = smbacl4_win2nfs4(fsp->fsp_name->base_name, psd->dacl, &params,
793 sbuf.st_ex_uid, sbuf.st_ex_gid);
794 if (!theacl)
795 return map_nt_error_from_unix(errno);
797 smbacl4_dump_nfs4acl(10, theacl);
799 if (set_acl_as_root) {
800 become_root();
802 result = set_nfs4_native(fsp, theacl);
803 saved_errno = errno;
804 if (set_acl_as_root) {
805 unbecome_root();
807 if (result!=True) {
808 errno = saved_errno;
809 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
810 return map_nt_error_from_unix(errno);
813 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
814 return NT_STATUS_OK;