Makefile: No need for c++ patch, also use fhs
[Samba/vfs_proxy.git] / source3 / modules / nfs4_acls.c
blobf411176590113f9c368cdbfb3943fc48a9c1a240
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 *acl)
49 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
50 if (acl==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 *acl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
87 if (acl==NULL)
89 DEBUG(0, ("TALLOC_SIZE failed\n"));
90 errno = ENOMEM;
91 return NULL;
93 acl->magic = SMB_ACL4_INT_MAGIC;
94 /* acl->first, last = NULL not needed */
95 return (SMB4ACL_T *)acl;
98 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *acl, SMB_ACE4PROP_T *prop)
100 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
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 *acl)
148 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
149 if (aclint==NULL)
150 return NULL;
152 return (SMB4ACE_T *)aclint->first;
155 uint32 smb_get_naces(SMB4ACL_T *acl)
157 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
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 (SMB_VFS_STAT(conn, filename, psbuf) != 0)
173 DEBUG(8, ("SMB_VFS_STAT 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, fsp->fsp_name, psbuf);
188 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
190 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
191 strerror(errno)));
192 return -1;
195 return 0;
198 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *acl, /* in */
199 DOM_SID *psid_owner, /* in */
200 DOM_SID *psid_group, /* in */
201 SEC_ACE **ppnt_ace_list, /* out */
202 int *pgood_aces /* out */
205 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
206 SMB_ACE4_INT_T *aceint;
207 SEC_ACE *nt_ace_list = NULL;
208 int good_aces = 0;
210 DEBUG(10, ("smbacl_nfs42win entered"));
212 aclint = get_validated_aclint(acl);
213 /* We do not check for naces being 0 or acl being NULL here because it is done upstream */
214 /* in smb_get_nt_acl_nfs4(). */
215 nt_ace_list = (SEC_ACE *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(SEC_ACE));
216 if (nt_ace_list==NULL)
218 DEBUG(10, ("talloc error"));
219 errno = ENOMEM;
220 return False;
223 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
224 uint32_t mask;
225 DOM_SID sid;
226 SMB_ACE4PROP_T *ace = &aceint->prop;
228 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
229 "who: %d\n", aceint->magic, ace->aceType, ace->flags,
230 ace->aceFlags, ace->aceMask, ace->who.id));
232 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
234 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
235 switch (ace->who.special_id) {
236 case SMB_ACE4_WHO_OWNER:
237 sid_copy(&sid, psid_owner);
238 break;
239 case SMB_ACE4_WHO_GROUP:
240 sid_copy(&sid, psid_group);
241 break;
242 case SMB_ACE4_WHO_EVERYONE:
243 sid_copy(&sid, &global_sid_World);
244 break;
245 default:
246 DEBUG(8, ("invalid special who id %d "
247 "ignored\n", ace->who.special_id));
249 } else {
250 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
251 gid_to_sid(&sid, ace->who.gid);
252 } else {
253 uid_to_sid(&sid, ace->who.uid);
256 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
257 sid_string_dbg(&sid)));
259 mask = ace->aceMask;
260 init_sec_ace(&nt_ace_list[good_aces++], &sid,
261 ace->aceType, mask,
262 ace->aceFlags & 0xf);
265 *ppnt_ace_list = nt_ace_list;
266 *pgood_aces = good_aces;
268 return True;
271 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
272 uint32 security_info,
273 SEC_DESC **ppdesc, SMB4ACL_T *acl)
275 int good_aces = 0;
276 DOM_SID sid_owner, sid_group;
277 size_t sd_size = 0;
278 SEC_ACE *nt_ace_list = NULL;
279 SEC_ACL *psa = NULL;
280 TALLOC_CTX *mem_ctx = talloc_tos();
282 if (acl==NULL || smb_get_naces(acl)==0)
283 return NT_STATUS_ACCESS_DENIED; /* special because we
284 * shouldn't alloc 0 for
285 * win */
287 uid_to_sid(&sid_owner, sbuf->st_uid);
288 gid_to_sid(&sid_group, sbuf->st_gid);
290 if (smbacl4_nfs42win(mem_ctx, acl, &sid_owner, &sid_group, &nt_ace_list, &good_aces)==False) {
291 DEBUG(8,("smbacl4_nfs42win failed\n"));
292 return map_nt_error_from_unix(errno);
295 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
296 if (psa == NULL) {
297 DEBUG(2,("make_sec_acl failed\n"));
298 return NT_STATUS_NO_MEMORY;
301 DEBUG(10,("after make sec_acl\n"));
302 *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
303 (security_info & OWNER_SECURITY_INFORMATION) ? &sid_owner : NULL,
304 (security_info & GROUP_SECURITY_INFORMATION) ? &sid_group : NULL,
305 NULL, psa, &sd_size);
306 if (*ppdesc==NULL) {
307 DEBUG(2,("make_sec_desc failed\n"));
308 return NT_STATUS_NO_MEMORY;
311 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
312 ndr_size_security_descriptor(*ppdesc, 0)));
314 return NT_STATUS_OK;
317 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
318 uint32 security_info,
319 SEC_DESC **ppdesc, SMB4ACL_T *acl)
321 SMB_STRUCT_STAT sbuf;
323 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
325 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
326 return map_nt_error_from_unix(errno);
329 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
332 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
333 const char *name,
334 uint32 security_info,
335 SEC_DESC **ppdesc, SMB4ACL_T *acl)
337 SMB_STRUCT_STAT sbuf;
339 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
341 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
342 return map_nt_error_from_unix(errno);
345 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
348 enum smbacl4_mode_enum {e_simple=0, e_special=1};
349 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
351 typedef struct _smbacl4_vfs_params {
352 enum smbacl4_mode_enum mode;
353 bool do_chown;
354 enum smbacl4_acedup_enum acedup;
355 struct db_context *sid_mapping_table;
356 } smbacl4_vfs_params;
359 * Gather special parameters for NFS4 ACL handling
361 static int smbacl4_get_vfs_params(
362 const char *type_name,
363 files_struct *fsp,
364 smbacl4_vfs_params *params
367 static const struct enum_list enum_smbacl4_modes[] = {
368 { e_simple, "simple" },
369 { e_special, "special" }
371 static const struct enum_list enum_smbacl4_acedups[] = {
372 { e_dontcare, "dontcare" },
373 { e_reject, "reject" },
374 { e_ignore, "ignore" },
375 { e_merge, "merge" },
378 memset(params, 0, sizeof(smbacl4_vfs_params));
379 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
380 SNUM(fsp->conn), type_name,
381 "mode", enum_smbacl4_modes, e_simple);
382 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
383 "chown", True);
384 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
385 SNUM(fsp->conn), type_name,
386 "acedup", enum_smbacl4_acedups, e_dontcare);
388 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
389 enum_smbacl4_modes[params->mode].name,
390 params->do_chown ? "true" : "false",
391 enum_smbacl4_acedups[params->acedup].name));
393 return 0;
396 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *acl)
398 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
399 SMB_ACE4_INT_T *aceint;
401 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
403 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
404 SMB_ACE4PROP_T *ace = &aceint->prop;
406 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
407 ace->aceType,
408 ace->aceFlags, ace->flags,
409 ace->aceMask,
410 ace->who.id));
415 * Find 2 NFS4 who-special ACE property (non-copy!!!)
416 * match nonzero if "special" and who is equal
417 * return ace if found matching; otherwise NULL
419 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
420 SMB4ACL_T *acl,
421 SMB_ACE4PROP_T *aceNew)
423 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
424 SMB_ACE4_INT_T *aceint;
426 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
427 SMB_ACE4PROP_T *ace = &aceint->prop;
429 if (ace->flags == aceNew->flags &&
430 ace->aceType==aceNew->aceType &&
431 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
432 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
434 /* keep type safety; e.g. gid is an u.short */
435 if (ace->flags & SMB_ACE4_ID_SPECIAL)
437 if (ace->who.special_id==aceNew->who.special_id)
438 return ace;
439 } else {
440 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
442 if (ace->who.gid==aceNew->who.gid)
443 return ace;
444 } else {
445 if (ace->who.uid==aceNew->who.uid)
446 return ace;
452 return NULL;
455 static bool nfs4_map_sid(smbacl4_vfs_params *params, const DOM_SID *src,
456 DOM_SID *dst)
458 static struct db_context *mapping_db = NULL;
459 TDB_DATA data;
461 if (mapping_db == NULL) {
462 const char *dbname = lp_parm_const_string(
463 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
465 if (dbname == NULL) {
466 DEBUG(10, ("%s:sidmap not defined\n",
467 SMBACL4_PARAM_TYPE_NAME));
468 return False;
471 become_root();
472 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
473 O_RDONLY, 0600);
474 unbecome_root();
476 if (mapping_db == NULL) {
477 DEBUG(1, ("could not open sidmap: %s\n",
478 strerror(errno)));
479 return False;
483 if (mapping_db->fetch(mapping_db, NULL,
484 string_term_tdb_data(sid_string_tos(src)),
485 &data) == -1) {
486 DEBUG(10, ("could not find mapping for SID %s\n",
487 sid_string_dbg(src)));
488 return False;
491 if ((data.dptr == NULL) || (data.dsize <= 0)
492 || (data.dptr[data.dsize-1] != '\0')) {
493 DEBUG(5, ("invalid mapping for SID %s\n",
494 sid_string_dbg(src)));
495 TALLOC_FREE(data.dptr);
496 return False;
499 if (!string_to_sid(dst, (char *)data.dptr)) {
500 DEBUG(1, ("invalid mapping %s for SID %s\n",
501 (char *)data.dptr, sid_string_dbg(src)));
502 TALLOC_FREE(data.dptr);
503 return False;
506 TALLOC_FREE(data.dptr);
508 return True;
511 static bool smbacl4_fill_ace4(
512 TALLOC_CTX *mem_ctx,
513 const char *filename,
514 smbacl4_vfs_params *params,
515 uid_t ownerUID,
516 gid_t ownerGID,
517 const SEC_ACE *ace_nt, /* input */
518 SMB_ACE4PROP_T *ace_v4 /* output */
521 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
523 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
524 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
525 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
526 ace_v4->aceMask = ace_nt->access_mask &
527 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
529 se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
531 if (ace_v4->aceFlags!=ace_nt->flags)
532 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
533 ace_v4->aceFlags, ace_nt->flags));
535 if (ace_v4->aceMask!=ace_nt->access_mask)
536 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
537 ace_v4->aceMask, ace_nt->access_mask));
539 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
540 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
541 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
542 } else {
543 const char *dom, *name;
544 enum lsa_SidType type;
545 uid_t uid;
546 gid_t gid;
547 DOM_SID sid;
549 sid_copy(&sid, &ace_nt->trustee);
551 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
553 DOM_SID mapped;
555 if (!nfs4_map_sid(params, &sid, &mapped)) {
556 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
557 "unknown\n", filename, sid_string_dbg(&sid)));
558 errno = EINVAL;
559 return False;
562 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
563 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
565 if (!lookup_sid(mem_ctx, &mapped, &dom,
566 &name, &type)) {
567 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
568 "mapped from %s is unknown\n",
569 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
570 errno = EINVAL;
571 return False;
574 sid_copy(&sid, &mapped);
577 if (type == SID_NAME_USER) {
578 if (!sid_to_uid(&sid, &uid)) {
579 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
580 "convert %s to uid\n", filename,
581 sid_string_dbg(&sid)));
582 return False;
585 if (params->mode==e_special && uid==ownerUID) {
586 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
587 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
588 } else {
589 ace_v4->who.uid = uid;
591 } else { /* else group? - TODO check it... */
592 if (!sid_to_gid(&sid, &gid)) {
593 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
594 "convert %s to gid\n", filename,
595 sid_string_dbg(&sid)));
596 return False;
599 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
601 if (params->mode==e_special && gid==ownerGID) {
602 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
603 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
604 } else {
605 ace_v4->who.gid = gid;
610 return True; /* OK */
613 static int smbacl4_MergeIgnoreReject(
614 enum smbacl4_acedup_enum acedup,
615 SMB4ACL_T *acl, /* may modify it */
616 SMB_ACE4PROP_T *ace, /* the "new" ACE */
617 bool *paddNewACE,
618 int i
621 int result = 0;
622 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(acl, ace);
623 if (ace4found)
625 switch(acedup)
627 case e_merge: /* "merge" flags */
628 *paddNewACE = False;
629 ace4found->aceFlags |= ace->aceFlags;
630 ace4found->aceMask |= ace->aceMask;
631 break;
632 case e_ignore: /* leave out this record */
633 *paddNewACE = False;
634 break;
635 case e_reject: /* do an error */
636 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
637 errno = EINVAL; /* SHOULD be set on any _real_ error */
638 result = -1;
639 break;
640 default:
641 break;
644 return result;
647 static SMB4ACL_T *smbacl4_win2nfs4(
648 const char *filename,
649 const SEC_ACL *dacl,
650 smbacl4_vfs_params *pparams,
651 uid_t ownerUID,
652 gid_t ownerGID
655 SMB4ACL_T *acl;
656 uint32 i;
657 TALLOC_CTX *mem_ctx = talloc_tos();
659 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
661 acl = smb_create_smb4acl();
662 if (acl==NULL)
663 return NULL;
665 for(i=0; i<dacl->num_aces; i++) {
666 SMB_ACE4PROP_T ace_v4;
667 bool addNewACE = True;
669 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
670 ownerUID, ownerGID,
671 dacl->aces + i, &ace_v4)) {
672 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
673 filename,
674 sid_string_dbg(&((dacl->aces+i)->trustee))));
675 continue;
678 if (pparams->acedup!=e_dontcare) {
679 if (smbacl4_MergeIgnoreReject(pparams->acedup, acl,
680 &ace_v4, &addNewACE, i))
681 return NULL;
684 if (addNewACE)
685 smb_add_ace4(acl, &ace_v4);
688 return acl;
691 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
692 uint32 security_info_sent,
693 const SEC_DESC *psd,
694 set_nfs4acl_native_fn_t set_nfs4_native)
696 smbacl4_vfs_params params;
697 SMB4ACL_T *acl = NULL;
698 bool result;
700 SMB_STRUCT_STAT sbuf;
701 bool need_chown = False;
702 uid_t newUID = (uid_t)-1;
703 gid_t newGID = (gid_t)-1;
705 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
707 if ((security_info_sent & (DACL_SECURITY_INFORMATION |
708 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
710 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
711 security_info_sent));
712 return NT_STATUS_OK; /* won't show error - later to be refined... */
715 /* Special behaviours */
716 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
717 return NT_STATUS_NO_MEMORY;
719 if (smbacl4_fGetFileOwner(fsp, &sbuf))
720 return map_nt_error_from_unix(errno);
722 if (params.do_chown) {
723 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
724 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
725 if (!NT_STATUS_IS_OK(status)) {
726 DEBUG(8, ("unpack_nt_owners failed"));
727 return status;
729 if (((newUID != (uid_t)-1) && (sbuf.st_uid != newUID)) ||
730 ((newGID != (gid_t)-1) && (sbuf.st_gid != newGID))) {
731 need_chown = True;
733 if (need_chown) {
734 if ((newUID == (uid_t)-1
735 || newUID == fsp->conn->server_info->utok.uid)) {
736 if(try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
737 DEBUG(3,("chown %s, %u, %u failed. Error = %s.\n",
738 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID,
739 strerror(errno)));
740 return map_nt_error_from_unix(errno);
743 DEBUG(10,("chown %s, %u, %u succeeded.\n",
744 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
745 if (smbacl4_GetFileOwner(fsp->conn, fsp->fsp_name, &sbuf))
746 return map_nt_error_from_unix(errno);
747 need_chown = False;
748 } else { /* chown is needed, but _after_ changing acl */
749 sbuf.st_uid = newUID; /* OWNER@ in case of e_special */
750 sbuf.st_gid = newGID; /* GROUP@ in case of e_special */
755 if ((security_info_sent & DACL_SECURITY_INFORMATION)!=0 && psd->dacl!=NULL)
757 acl = smbacl4_win2nfs4(fsp->fsp_name, psd->dacl, &params, sbuf.st_uid, sbuf.st_gid);
758 if (!acl)
759 return map_nt_error_from_unix(errno);
761 smbacl4_dump_nfs4acl(10, acl);
763 result = set_nfs4_native(fsp, acl);
764 if (result!=True)
766 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
767 return map_nt_error_from_unix(errno);
769 } else
770 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
772 /* Any chown pending? */
773 if (need_chown) {
774 DEBUG(3,("chown#2 %s. uid = %u, gid = %u.\n",
775 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
776 if (try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
777 DEBUG(2,("chown#2 %s, %u, %u failed. Error = %s.\n",
778 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID,
779 strerror(errno)));
780 return map_nt_error_from_unix(errno);
782 DEBUG(10,("chown#2 %s, %u, %u succeeded.\n",
783 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
786 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
787 return NT_STATUS_OK;