fix build warning.
[Samba.git] / source / modules / nfs4_acls.c
blob5e90afa6b3e0d248a785234ba1b261fd82f46d22
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 extern struct current_user current_user;
48 extern int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid);
49 extern NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp,
50 uint32 security_info_sent, SEC_DESC *psd);
52 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *acl)
54 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
55 if (acl==NULL)
57 DEBUG(2, ("acl is NULL\n"));
58 errno = EINVAL;
59 return NULL;
61 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
63 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
64 errno = EINVAL;
65 return NULL;
67 return aclint;
70 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
72 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
73 if (ace==NULL)
75 DEBUG(2, ("ace is NULL\n"));
76 errno = EINVAL;
77 return NULL;
79 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
81 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
82 errno = EINVAL;
83 return NULL;
85 return aceint;
88 SMB4ACL_T *smb_create_smb4acl(void)
90 TALLOC_CTX *mem_ctx = talloc_tos();
91 SMB_ACL4_INT_T *acl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
92 if (acl==NULL)
94 DEBUG(0, ("TALLOC_SIZE failed\n"));
95 errno = ENOMEM;
96 return NULL;
98 acl->magic = SMB_ACL4_INT_MAGIC;
99 /* acl->first, last = NULL not needed */
100 return (SMB4ACL_T *)acl;
103 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *acl, SMB_ACE4PROP_T *prop)
105 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
106 TALLOC_CTX *mem_ctx = talloc_tos();
107 SMB_ACE4_INT_T *ace;
109 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
110 if (ace==NULL)
112 DEBUG(0, ("TALLOC_SIZE failed\n"));
113 errno = ENOMEM;
114 return NULL;
116 ace->magic = SMB_ACE4_INT_MAGIC;
117 /* ace->next = NULL not needed */
118 memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
120 if (aclint->first==NULL)
122 aclint->first = ace;
123 aclint->last = ace;
124 } else {
125 aclint->last->next = (void *)ace;
126 aclint->last = ace;
128 aclint->naces++;
130 return (SMB4ACE_T *)ace;
133 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
135 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
136 if (aceint==NULL)
137 return NULL;
139 return &aceint->prop;
142 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
144 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
145 if (aceint==NULL)
146 return NULL;
148 return (SMB4ACE_T *)aceint->next;
151 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *acl)
153 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
154 if (aclint==NULL)
155 return NULL;
157 return (SMB4ACE_T *)aclint->first;
160 uint32 smb_get_naces(SMB4ACL_T *acl)
162 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
163 if (aclint==NULL)
164 return 0;
166 return aclint->naces;
169 static int smbacl4_GetFileOwner(struct connection_struct *conn,
170 const char *filename,
171 SMB_STRUCT_STAT *psbuf)
173 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
175 /* Get the stat struct for the owner info. */
176 if (SMB_VFS_STAT(conn, filename, psbuf) != 0)
178 DEBUG(8, ("SMB_VFS_STAT failed with error %s\n",
179 strerror(errno)));
180 return -1;
183 return 0;
186 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
188 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
190 if (fsp->is_directory || fsp->fh->fd == -1) {
191 return smbacl4_GetFileOwner(fsp->conn, fsp->fsp_name, psbuf);
193 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
195 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
196 strerror(errno)));
197 return -1;
200 return 0;
203 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *acl, /* in */
204 DOM_SID *psid_owner, /* in */
205 DOM_SID *psid_group, /* in */
206 SEC_ACE **ppnt_ace_list, /* out */
207 int *pgood_aces /* out */
210 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
211 SMB_ACE4_INT_T *aceint;
212 SEC_ACE *nt_ace_list = NULL;
213 int good_aces = 0;
215 DEBUG(10, ("smbacl_nfs42win entered"));
217 aclint = get_validated_aclint(acl);
218 /* We do not check for naces being 0 or acl being NULL here because it is done upstream */
219 /* in smb_get_nt_acl_nfs4(). */
220 nt_ace_list = (SEC_ACE *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(SEC_ACE));
221 if (nt_ace_list==NULL)
223 DEBUG(10, ("talloc error"));
224 errno = ENOMEM;
225 return False;
228 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
229 SEC_ACCESS mask;
230 DOM_SID sid;
231 SMB_ACE4PROP_T *ace = &aceint->prop;
233 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
234 "who: %d\n", aceint->magic, ace->aceType, ace->flags,
235 ace->aceFlags, ace->aceMask, ace->who.id));
237 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
239 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
240 switch (ace->who.special_id) {
241 case SMB_ACE4_WHO_OWNER:
242 sid_copy(&sid, psid_owner);
243 break;
244 case SMB_ACE4_WHO_GROUP:
245 sid_copy(&sid, psid_group);
246 break;
247 case SMB_ACE4_WHO_EVERYONE:
248 sid_copy(&sid, &global_sid_World);
249 break;
250 default:
251 DEBUG(8, ("invalid special who id %d "
252 "ignored\n", ace->who.special_id));
254 } else {
255 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
256 gid_to_sid(&sid, ace->who.gid);
257 } else {
258 uid_to_sid(&sid, ace->who.uid);
261 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
262 sid_string_dbg(&sid)));
264 init_sec_access(&mask, ace->aceMask);
265 init_sec_ace(&nt_ace_list[good_aces++], &sid,
266 ace->aceType, mask,
267 ace->aceFlags & 0xf);
270 *ppnt_ace_list = nt_ace_list;
271 *pgood_aces = good_aces;
273 return True;
276 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
277 uint32 security_info,
278 SEC_DESC **ppdesc, SMB4ACL_T *acl)
280 int good_aces = 0;
281 DOM_SID sid_owner, sid_group;
282 size_t sd_size = 0;
283 SEC_ACE *nt_ace_list = NULL;
284 SEC_ACL *psa = NULL;
285 TALLOC_CTX *mem_ctx = talloc_tos();
287 if (acl==NULL || smb_get_naces(acl)==0)
288 return NT_STATUS_ACCESS_DENIED; /* special because we
289 * shouldn't alloc 0 for
290 * win */
292 uid_to_sid(&sid_owner, sbuf->st_uid);
293 gid_to_sid(&sid_group, sbuf->st_gid);
295 if (smbacl4_nfs42win(mem_ctx, acl, &sid_owner, &sid_group, &nt_ace_list, &good_aces)==False) {
296 DEBUG(8,("smbacl4_nfs42win failed\n"));
297 return map_nt_error_from_unix(errno);
300 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
301 if (psa == NULL) {
302 DEBUG(2,("make_sec_acl failed\n"));
303 return NT_STATUS_NO_MEMORY;
306 DEBUG(10,("after make sec_acl\n"));
307 *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
308 (security_info & OWNER_SECURITY_INFORMATION) ? &sid_owner : NULL,
309 (security_info & GROUP_SECURITY_INFORMATION) ? &sid_group : NULL,
310 NULL, psa, &sd_size);
311 if (*ppdesc==NULL) {
312 DEBUG(2,("make_sec_desc failed\n"));
313 return NT_STATUS_NO_MEMORY;
316 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
317 ndr_size_security_descriptor(*ppdesc, 0)));
319 return NT_STATUS_OK;
322 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
323 uint32 security_info,
324 SEC_DESC **ppdesc, SMB4ACL_T *acl)
326 SMB_STRUCT_STAT sbuf;
328 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
330 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
331 return map_nt_error_from_unix(errno);
334 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
337 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
338 const char *name,
339 uint32 security_info,
340 SEC_DESC **ppdesc, SMB4ACL_T *acl)
342 SMB_STRUCT_STAT sbuf;
344 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
346 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
347 return map_nt_error_from_unix(errno);
350 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
353 enum smbacl4_mode_enum {e_simple=0, e_special=1};
354 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
356 typedef struct _smbacl4_vfs_params {
357 enum smbacl4_mode_enum mode;
358 bool do_chown;
359 enum smbacl4_acedup_enum acedup;
360 struct db_context *sid_mapping_table;
361 } smbacl4_vfs_params;
364 * Gather special parameters for NFS4 ACL handling
366 static int smbacl4_get_vfs_params(
367 const char *type_name,
368 files_struct *fsp,
369 smbacl4_vfs_params *params
372 static const struct enum_list enum_smbacl4_modes[] = {
373 { e_simple, "simple" },
374 { e_special, "special" }
376 static const struct enum_list enum_smbacl4_acedups[] = {
377 { e_dontcare, "dontcare" },
378 { e_reject, "reject" },
379 { e_ignore, "ignore" },
380 { e_merge, "merge" },
383 memset(params, 0, sizeof(smbacl4_vfs_params));
384 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
385 SNUM(fsp->conn), type_name,
386 "mode", enum_smbacl4_modes, e_simple);
387 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
388 "chown", True);
389 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
390 SNUM(fsp->conn), type_name,
391 "acedup", enum_smbacl4_acedups, e_dontcare);
393 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
394 enum_smbacl4_modes[params->mode].name,
395 params->do_chown ? "true" : "false",
396 enum_smbacl4_acedups[params->acedup].name));
398 return 0;
401 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *acl)
403 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
404 SMB_ACE4_INT_T *aceint;
406 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
408 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
409 SMB_ACE4PROP_T *ace = &aceint->prop;
411 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
412 ace->aceType,
413 ace->aceFlags, ace->flags,
414 ace->aceMask,
415 ace->who.id));
420 * Find 2 NFS4 who-special ACE property (non-copy!!!)
421 * match nonzero if "special" and who is equal
422 * return ace if found matching; otherwise NULL
424 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
425 SMB4ACL_T *acl,
426 SMB_ACE4PROP_T *aceNew)
428 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
429 SMB_ACE4_INT_T *aceint;
431 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
432 SMB_ACE4PROP_T *ace = &aceint->prop;
434 if (ace->flags == aceNew->flags &&
435 ace->aceType==aceNew->aceType &&
436 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
437 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
439 /* keep type safety; e.g. gid is an u.short */
440 if (ace->flags & SMB_ACE4_ID_SPECIAL)
442 if (ace->who.special_id==aceNew->who.special_id)
443 return ace;
444 } else {
445 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
447 if (ace->who.gid==aceNew->who.gid)
448 return ace;
449 } else {
450 if (ace->who.uid==aceNew->who.uid)
451 return ace;
457 return NULL;
460 static bool nfs4_map_sid(smbacl4_vfs_params *params, const DOM_SID *src,
461 DOM_SID *dst)
463 static struct db_context *mapping_db = NULL;
464 TDB_DATA data;
466 if (mapping_db == NULL) {
467 const char *dbname = lp_parm_const_string(
468 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
470 if (dbname == NULL) {
471 DEBUG(10, ("%s:sidmap not defined\n",
472 SMBACL4_PARAM_TYPE_NAME));
473 return False;
476 become_root();
477 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
478 O_RDONLY, 0600);
479 unbecome_root();
481 if (mapping_db == NULL) {
482 DEBUG(1, ("could not open sidmap: %s\n",
483 strerror(errno)));
484 return False;
488 if (mapping_db->fetch(mapping_db, NULL,
489 string_term_tdb_data(sid_string_tos(src)),
490 &data) == -1) {
491 DEBUG(10, ("could not find mapping for SID %s\n",
492 sid_string_dbg(src)));
493 return False;
496 if ((data.dptr == NULL) || (data.dsize <= 0)
497 || (data.dptr[data.dsize-1] != '\0')) {
498 DEBUG(5, ("invalid mapping for SID %s\n",
499 sid_string_dbg(src)));
500 TALLOC_FREE(data.dptr);
501 return False;
504 if (!string_to_sid(dst, (char *)data.dptr)) {
505 DEBUG(1, ("invalid mapping %s for SID %s\n",
506 (char *)data.dptr, sid_string_dbg(src)));
507 TALLOC_FREE(data.dptr);
508 return False;
511 TALLOC_FREE(data.dptr);
513 return True;
516 static bool smbacl4_fill_ace4(
517 TALLOC_CTX *mem_ctx,
518 const char *filename,
519 smbacl4_vfs_params *params,
520 uid_t ownerUID,
521 gid_t ownerGID,
522 SEC_ACE *ace_nt, /* input */
523 SMB_ACE4PROP_T *ace_v4 /* output */
526 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
528 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
529 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
530 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
531 ace_v4->aceMask = ace_nt->access_mask &
532 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
534 se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
536 if (ace_v4->aceFlags!=ace_nt->flags)
537 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
538 ace_v4->aceFlags, ace_nt->flags));
540 if (ace_v4->aceMask!=ace_nt->access_mask)
541 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
542 ace_v4->aceMask, ace_nt->access_mask));
544 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
545 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
546 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
547 } else {
548 const char *dom, *name;
549 enum lsa_SidType type;
550 uid_t uid;
551 gid_t gid;
552 DOM_SID sid;
554 sid_copy(&sid, &ace_nt->trustee);
556 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
558 DOM_SID mapped;
560 if (!nfs4_map_sid(params, &sid, &mapped)) {
561 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
562 "unknown\n", filename, sid_string_dbg(&sid)));
563 errno = EINVAL;
564 return False;
567 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
568 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
570 if (!lookup_sid(mem_ctx, &mapped, &dom,
571 &name, &type)) {
572 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
573 "mapped from %s is unknown\n",
574 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
575 errno = EINVAL;
576 return False;
579 sid_copy(&sid, &mapped);
582 if (type == SID_NAME_USER) {
583 if (!sid_to_uid(&sid, &uid)) {
584 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
585 "convert %s to uid\n", filename,
586 sid_string_dbg(&sid)));
587 return False;
590 if (params->mode==e_special && uid==ownerUID) {
591 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
592 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
593 } else {
594 ace_v4->who.uid = uid;
596 } else { /* else group? - TODO check it... */
597 if (!sid_to_gid(&sid, &gid)) {
598 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
599 "convert %s to gid\n", filename,
600 sid_string_dbg(&sid)));
601 return False;
604 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
606 if (params->mode==e_special && gid==ownerGID) {
607 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
608 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
609 } else {
610 ace_v4->who.gid = gid;
615 return True; /* OK */
618 static int smbacl4_MergeIgnoreReject(
619 enum smbacl4_acedup_enum acedup,
620 SMB4ACL_T *acl, /* may modify it */
621 SMB_ACE4PROP_T *ace, /* the "new" ACE */
622 bool *paddNewACE,
623 int i
626 int result = 0;
627 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(acl, ace);
628 if (ace4found)
630 switch(acedup)
632 case e_merge: /* "merge" flags */
633 *paddNewACE = False;
634 ace4found->aceFlags |= ace->aceFlags;
635 ace4found->aceMask |= ace->aceMask;
636 break;
637 case e_ignore: /* leave out this record */
638 *paddNewACE = False;
639 break;
640 case e_reject: /* do an error */
641 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
642 errno = EINVAL; /* SHOULD be set on any _real_ error */
643 result = -1;
644 break;
645 default:
646 break;
649 return result;
652 static SMB4ACL_T *smbacl4_win2nfs4(
653 const char *filename,
654 SEC_ACL *dacl,
655 smbacl4_vfs_params *pparams,
656 uid_t ownerUID,
657 gid_t ownerGID
660 SMB4ACL_T *acl;
661 uint32 i;
662 TALLOC_CTX *mem_ctx = talloc_tos();
664 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
666 acl = smb_create_smb4acl();
667 if (acl==NULL)
668 return NULL;
670 for(i=0; i<dacl->num_aces; i++) {
671 SMB_ACE4PROP_T ace_v4;
672 bool addNewACE = True;
674 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
675 ownerUID, ownerGID,
676 dacl->aces + i, &ace_v4)) {
677 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
678 filename,
679 sid_string_dbg(&((dacl->aces+i)->trustee))));
680 continue;
683 if (pparams->acedup!=e_dontcare) {
684 if (smbacl4_MergeIgnoreReject(pparams->acedup, acl,
685 &ace_v4, &addNewACE, i))
686 return NULL;
689 if (addNewACE)
690 smb_add_ace4(acl, &ace_v4);
693 return acl;
696 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
697 uint32 security_info_sent,
698 SEC_DESC *psd,
699 set_nfs4acl_native_fn_t set_nfs4_native)
701 smbacl4_vfs_params params;
702 SMB4ACL_T *acl = NULL;
703 bool result;
705 SMB_STRUCT_STAT sbuf;
706 bool need_chown = False;
707 uid_t newUID = (uid_t)-1;
708 gid_t newGID = (gid_t)-1;
710 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
712 if ((security_info_sent & (DACL_SECURITY_INFORMATION |
713 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
715 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
716 security_info_sent));
717 return NT_STATUS_OK; /* won't show error - later to be refined... */
720 /* Special behaviours */
721 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
722 return NT_STATUS_NO_MEMORY;
724 if (smbacl4_fGetFileOwner(fsp, &sbuf))
725 return map_nt_error_from_unix(errno);
727 if (params.do_chown) {
728 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
729 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
730 if (!NT_STATUS_IS_OK(status)) {
731 DEBUG(8, ("unpack_nt_owners failed"));
732 return status;
734 if (((newUID != (uid_t)-1) && (sbuf.st_uid != newUID)) ||
735 ((newGID != (gid_t)-1) && (sbuf.st_gid != newGID))) {
736 need_chown = True;
738 if (need_chown) {
739 if ((newUID == (uid_t)-1 || newUID == current_user.ut.uid)) {
740 if(try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
741 DEBUG(3,("chown %s, %u, %u failed. Error = %s.\n",
742 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID,
743 strerror(errno)));
744 return map_nt_error_from_unix(errno);
747 DEBUG(10,("chown %s, %u, %u succeeded.\n",
748 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
749 if (smbacl4_GetFileOwner(fsp->conn, fsp->fsp_name, &sbuf))
750 return map_nt_error_from_unix(errno);
751 need_chown = False;
752 } else { /* chown is needed, but _after_ changing acl */
753 sbuf.st_uid = newUID; /* OWNER@ in case of e_special */
754 sbuf.st_gid = newGID; /* GROUP@ in case of e_special */
759 if ((security_info_sent & DACL_SECURITY_INFORMATION)!=0 && psd->dacl!=NULL)
761 acl = smbacl4_win2nfs4(fsp->fsp_name, psd->dacl, &params, sbuf.st_uid, sbuf.st_gid);
762 if (!acl)
763 return map_nt_error_from_unix(errno);
765 smbacl4_dump_nfs4acl(10, acl);
767 result = set_nfs4_native(fsp, acl);
768 if (result!=True)
770 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
771 return map_nt_error_from_unix(errno);
773 } else
774 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
776 /* Any chown pending? */
777 if (need_chown) {
778 DEBUG(3,("chown#2 %s. uid = %u, gid = %u.\n",
779 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
780 if (try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
781 DEBUG(2,("chown#2 %s, %u, %u failed. Error = %s.\n",
782 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID,
783 strerror(errno)));
784 return map_nt_error_from_unix(errno);
786 DEBUG(10,("chown#2 %s, %u, %u succeeded.\n",
787 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
790 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
791 return NT_STATUS_OK;