Fix IDL for lsa_LookupSids3.
[Samba/gebeck_regimport.git] / source3 / modules / nfs4_acls.c
blob0c3d010dcde64553487648b50ef23dc6e46d619f
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 #define SMB_ACE4_INT_MAGIC 0x76F8A967
29 typedef struct _SMB_ACE4_INT_T
31 uint32 magic;
32 SMB_ACE4PROP_T prop;
33 void *next;
34 } SMB_ACE4_INT_T;
36 #define SMB_ACL4_INT_MAGIC 0x29A3E792
37 typedef struct _SMB_ACL4_INT_T
39 uint32 magic;
40 uint32 naces;
41 SMB_ACE4_INT_T *first;
42 SMB_ACE4_INT_T *last;
43 } SMB_ACL4_INT_T;
45 extern struct current_user current_user;
46 extern int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid);
47 extern NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp,
48 uint32 security_info_sent, SEC_DESC *psd);
50 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *acl)
52 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
53 if (acl==NULL)
55 DEBUG(2, ("acl is NULL\n"));
56 errno = EINVAL;
57 return NULL;
59 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
61 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
62 errno = EINVAL;
63 return NULL;
65 return aclint;
68 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
70 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
71 if (ace==NULL)
73 DEBUG(2, ("ace is NULL\n"));
74 errno = EINVAL;
75 return NULL;
77 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
79 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
80 errno = EINVAL;
81 return NULL;
83 return aceint;
86 SMB4ACL_T *smb_create_smb4acl(void)
88 TALLOC_CTX *mem_ctx = talloc_tos();
89 SMB_ACL4_INT_T *acl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
90 if (acl==NULL)
92 DEBUG(0, ("TALLOC_SIZE failed\n"));
93 errno = ENOMEM;
94 return NULL;
96 acl->magic = SMB_ACL4_INT_MAGIC;
97 /* acl->first, last = NULL not needed */
98 return (SMB4ACL_T *)acl;
101 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *acl, SMB_ACE4PROP_T *prop)
103 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
104 TALLOC_CTX *mem_ctx = talloc_tos();
105 SMB_ACE4_INT_T *ace;
107 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
108 if (ace==NULL)
110 DEBUG(0, ("TALLOC_SIZE failed\n"));
111 errno = ENOMEM;
112 return NULL;
114 ace->magic = SMB_ACE4_INT_MAGIC;
115 /* ace->next = NULL not needed */
116 memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
118 if (aclint->first==NULL)
120 aclint->first = ace;
121 aclint->last = ace;
122 } else {
123 aclint->last->next = (void *)ace;
124 aclint->last = ace;
126 aclint->naces++;
128 return (SMB4ACE_T *)ace;
131 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
133 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
134 if (aceint==NULL)
135 return NULL;
137 return &aceint->prop;
140 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
142 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
143 if (aceint==NULL)
144 return NULL;
146 return (SMB4ACE_T *)aceint->next;
149 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *acl)
151 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
152 if (aclint==NULL)
153 return NULL;
155 return (SMB4ACE_T *)aclint->first;
158 uint32 smb_get_naces(SMB4ACL_T *acl)
160 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
161 if (aclint==NULL)
162 return 0;
164 return aclint->naces;
167 static int smbacl4_GetFileOwner(struct connection_struct *conn,
168 const char *filename,
169 SMB_STRUCT_STAT *psbuf)
171 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
173 /* Get the stat struct for the owner info. */
174 if (SMB_VFS_STAT(conn, filename, psbuf) != 0)
176 DEBUG(8, ("SMB_VFS_STAT failed with error %s\n",
177 strerror(errno)));
178 return -1;
181 return 0;
184 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
186 memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
188 if (fsp->is_directory || fsp->fh->fd == -1) {
189 return smbacl4_GetFileOwner(fsp->conn, fsp->fsp_name, psbuf);
191 if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
193 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
194 strerror(errno)));
195 return -1;
198 return 0;
201 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *acl, /* in */
202 DOM_SID *psid_owner, /* in */
203 DOM_SID *psid_group, /* in */
204 SEC_ACE **ppnt_ace_list, /* out */
205 int *pgood_aces /* out */
208 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
209 SMB_ACE4_INT_T *aceint;
210 SEC_ACE *nt_ace_list = NULL;
211 int good_aces = 0;
213 DEBUG(10, ("smbacl_nfs42win entered"));
215 aclint = get_validated_aclint(acl);
216 /* We do not check for naces being 0 or acl being NULL here because it is done upstream */
217 /* in smb_get_nt_acl_nfs4(). */
218 nt_ace_list = (SEC_ACE *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(SEC_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 SEC_ACCESS mask;
228 DOM_SID sid;
229 SMB_ACE4PROP_T *ace = &aceint->prop;
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 init_sec_access(&mask, ace->aceMask);
263 init_sec_ace(&nt_ace_list[good_aces++], &sid,
264 ace->aceType, mask,
265 ace->aceFlags & 0xf);
268 *ppnt_ace_list = nt_ace_list;
269 *pgood_aces = good_aces;
271 return True;
274 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
275 uint32 security_info,
276 SEC_DESC **ppdesc, SMB4ACL_T *acl)
278 int good_aces = 0;
279 DOM_SID sid_owner, sid_group;
280 size_t sd_size = 0;
281 SEC_ACE *nt_ace_list = NULL;
282 SEC_ACL *psa = NULL;
283 TALLOC_CTX *mem_ctx = talloc_tos();
285 if (acl==NULL || smb_get_naces(acl)==0)
286 return NT_STATUS_ACCESS_DENIED; /* special because we
287 * shouldn't alloc 0 for
288 * win */
290 uid_to_sid(&sid_owner, sbuf->st_uid);
291 gid_to_sid(&sid_group, sbuf->st_gid);
293 if (smbacl4_nfs42win(mem_ctx, acl, &sid_owner, &sid_group, &nt_ace_list, &good_aces)==False) {
294 DEBUG(8,("smbacl4_nfs42win failed\n"));
295 return map_nt_error_from_unix(errno);
298 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
299 if (psa == NULL) {
300 DEBUG(2,("make_sec_acl failed\n"));
301 return NT_STATUS_NO_MEMORY;
304 DEBUG(10,("after make sec_acl\n"));
305 *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
306 (security_info & OWNER_SECURITY_INFORMATION) ? &sid_owner : NULL,
307 (security_info & GROUP_SECURITY_INFORMATION) ? &sid_group : NULL,
308 NULL, psa, &sd_size);
309 if (*ppdesc==NULL) {
310 DEBUG(2,("make_sec_desc failed\n"));
311 return NT_STATUS_NO_MEMORY;
314 DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
315 ndr_size_security_descriptor(*ppdesc, 0)));
317 return NT_STATUS_OK;
320 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
321 uint32 security_info,
322 SEC_DESC **ppdesc, SMB4ACL_T *acl)
324 SMB_STRUCT_STAT sbuf;
326 DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
328 if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
329 return map_nt_error_from_unix(errno);
332 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
335 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
336 const char *name,
337 uint32 security_info,
338 SEC_DESC **ppdesc, SMB4ACL_T *acl)
340 SMB_STRUCT_STAT sbuf;
342 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
344 if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
345 return map_nt_error_from_unix(errno);
348 return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
351 enum smbacl4_mode_enum {e_simple=0, e_special=1};
352 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
354 typedef struct _smbacl4_vfs_params {
355 enum smbacl4_mode_enum mode;
356 bool do_chown;
357 enum smbacl4_acedup_enum acedup;
358 struct db_context *sid_mapping_table;
359 } smbacl4_vfs_params;
362 * Gather special parameters for NFS4 ACL handling
364 static int smbacl4_get_vfs_params(
365 const char *type_name,
366 files_struct *fsp,
367 smbacl4_vfs_params *params
370 static const struct enum_list enum_smbacl4_modes[] = {
371 { e_simple, "simple" },
372 { e_special, "special" }
374 static const struct enum_list enum_smbacl4_acedups[] = {
375 { e_dontcare, "dontcare" },
376 { e_reject, "reject" },
377 { e_ignore, "ignore" },
378 { e_merge, "merge" },
381 memset(params, 0, sizeof(smbacl4_vfs_params));
382 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
383 SNUM(fsp->conn), type_name,
384 "mode", enum_smbacl4_modes, e_simple);
385 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
386 "chown", True);
387 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
388 SNUM(fsp->conn), type_name,
389 "acedup", enum_smbacl4_acedups, e_dontcare);
391 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
392 enum_smbacl4_modes[params->mode].name,
393 params->do_chown ? "true" : "false",
394 enum_smbacl4_acedups[params->acedup].name));
396 return 0;
399 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *acl)
401 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
402 SMB_ACE4_INT_T *aceint;
404 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
406 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
407 SMB_ACE4PROP_T *ace = &aceint->prop;
409 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
410 ace->aceType,
411 ace->aceFlags, ace->flags,
412 ace->aceMask,
413 ace->who.id));
418 * Find 2 NFS4 who-special ACE property (non-copy!!!)
419 * match nonzero if "special" and who is equal
420 * return ace if found matching; otherwise NULL
422 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
423 SMB4ACL_T *acl,
424 SMB_ACE4PROP_T *aceNew)
426 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
427 SMB_ACE4_INT_T *aceint;
429 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
430 SMB_ACE4PROP_T *ace = &aceint->prop;
432 if (ace->flags == aceNew->flags &&
433 ace->aceType==aceNew->aceType &&
434 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
435 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
437 /* keep type safety; e.g. gid is an u.short */
438 if (ace->flags & SMB_ACE4_ID_SPECIAL)
440 if (ace->who.special_id==aceNew->who.special_id)
441 return ace;
442 } else {
443 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
445 if (ace->who.gid==aceNew->who.gid)
446 return ace;
447 } else {
448 if (ace->who.uid==aceNew->who.uid)
449 return ace;
455 return NULL;
458 static bool nfs4_map_sid(smbacl4_vfs_params *params, const DOM_SID *src,
459 DOM_SID *dst)
461 static struct db_context *mapping_db = NULL;
462 TDB_DATA data;
464 if (mapping_db == NULL) {
465 const char *dbname = lp_parm_const_string(
466 -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
468 if (dbname == NULL) {
469 DEBUG(10, ("%s:sidmap not defined\n",
470 SMBACL4_PARAM_TYPE_NAME));
471 return False;
474 become_root();
475 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
476 O_RDONLY, 0600);
477 unbecome_root();
479 if (mapping_db == NULL) {
480 DEBUG(1, ("could not open sidmap: %s\n",
481 strerror(errno)));
482 return False;
486 if (mapping_db->fetch(mapping_db, NULL,
487 string_term_tdb_data(sid_string_tos(src)),
488 &data) == -1) {
489 DEBUG(10, ("could not find mapping for SID %s\n",
490 sid_string_dbg(src)));
491 return False;
494 if ((data.dptr == NULL) || (data.dsize <= 0)
495 || (data.dptr[data.dsize-1] != '\0')) {
496 DEBUG(5, ("invalid mapping for SID %s\n",
497 sid_string_dbg(src)));
498 TALLOC_FREE(data.dptr);
499 return False;
502 if (!string_to_sid(dst, (char *)data.dptr)) {
503 DEBUG(1, ("invalid mapping %s for SID %s\n",
504 (char *)data.dptr, sid_string_dbg(src)));
505 TALLOC_FREE(data.dptr);
506 return False;
509 TALLOC_FREE(data.dptr);
511 return True;
514 static bool smbacl4_fill_ace4(
515 TALLOC_CTX *mem_ctx,
516 const char *filename,
517 smbacl4_vfs_params *params,
518 uid_t ownerUID,
519 gid_t ownerGID,
520 SEC_ACE *ace_nt, /* input */
521 SMB_ACE4PROP_T *ace_v4 /* output */
524 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
526 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
527 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
528 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
529 ace_v4->aceMask = ace_nt->access_mask &
530 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
532 if (ace_v4->aceFlags!=ace_nt->flags)
533 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
534 ace_v4->aceFlags, ace_nt->flags));
536 if (ace_v4->aceMask!=ace_nt->access_mask)
537 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
538 ace_v4->aceMask, ace_nt->access_mask));
540 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
541 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
542 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
543 } else {
544 const char *dom, *name;
545 enum lsa_SidType type;
546 uid_t uid;
547 gid_t gid;
548 DOM_SID sid;
550 sid_copy(&sid, &ace_nt->trustee);
552 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
554 DOM_SID mapped;
556 if (!nfs4_map_sid(params, &sid, &mapped)) {
557 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
558 "unknown\n", filename, sid_string_dbg(&sid)));
559 errno = EINVAL;
560 return False;
563 DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
564 "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
566 if (!lookup_sid(mem_ctx, &mapped, &dom,
567 &name, &type)) {
568 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
569 "mapped from %s is unknown\n",
570 filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
571 errno = EINVAL;
572 return False;
575 sid_copy(&sid, &mapped);
578 if (type == SID_NAME_USER) {
579 if (!sid_to_uid(&sid, &uid)) {
580 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
581 "convert %s to uid\n", filename,
582 sid_string_dbg(&sid)));
583 return False;
586 if (params->mode==e_special && uid==ownerUID) {
587 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
588 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
589 } else {
590 ace_v4->who.uid = uid;
592 } else { /* else group? - TODO check it... */
593 if (!sid_to_gid(&sid, &gid)) {
594 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
595 "convert %s to gid\n", filename,
596 sid_string_dbg(&sid)));
597 return False;
600 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
602 if (params->mode==e_special && gid==ownerGID) {
603 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
604 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
605 } else {
606 ace_v4->who.gid = gid;
611 return True; /* OK */
614 static int smbacl4_MergeIgnoreReject(
615 enum smbacl4_acedup_enum acedup,
616 SMB4ACL_T *acl, /* may modify it */
617 SMB_ACE4PROP_T *ace, /* the "new" ACE */
618 bool *paddNewACE,
619 int i
622 int result = 0;
623 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(acl, ace);
624 if (ace4found)
626 switch(acedup)
628 case e_merge: /* "merge" flags */
629 *paddNewACE = False;
630 ace4found->aceFlags |= ace->aceFlags;
631 ace4found->aceMask |= ace->aceMask;
632 break;
633 case e_ignore: /* leave out this record */
634 *paddNewACE = False;
635 break;
636 case e_reject: /* do an error */
637 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
638 errno = EINVAL; /* SHOULD be set on any _real_ error */
639 result = -1;
640 break;
641 default:
642 break;
645 return result;
648 static SMB4ACL_T *smbacl4_win2nfs4(
649 const char *filename,
650 SEC_ACL *dacl,
651 smbacl4_vfs_params *pparams,
652 uid_t ownerUID,
653 gid_t ownerGID
656 SMB4ACL_T *acl;
657 uint32 i;
658 TALLOC_CTX *mem_ctx = talloc_tos();
660 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
662 acl = smb_create_smb4acl();
663 if (acl==NULL)
664 return NULL;
666 for(i=0; i<dacl->num_aces; i++) {
667 SMB_ACE4PROP_T ace_v4;
668 bool addNewACE = True;
670 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
671 ownerUID, ownerGID,
672 dacl->aces + i, &ace_v4)) {
673 DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
674 filename,
675 sid_string_dbg(&((dacl->aces+i)->trustee))));
676 continue;
679 if (pparams->acedup!=e_dontcare) {
680 if (smbacl4_MergeIgnoreReject(pparams->acedup, acl,
681 &ace_v4, &addNewACE, i))
682 return NULL;
685 if (addNewACE)
686 smb_add_ace4(acl, &ace_v4);
689 return acl;
692 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
693 uint32 security_info_sent,
694 SEC_DESC *psd,
695 set_nfs4acl_native_fn_t set_nfs4_native)
697 smbacl4_vfs_params params;
698 SMB4ACL_T *acl = NULL;
699 bool result;
701 SMB_STRUCT_STAT sbuf;
702 bool need_chown = False;
703 uid_t newUID = (uid_t)-1;
704 gid_t newGID = (gid_t)-1;
706 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
708 if ((security_info_sent & (DACL_SECURITY_INFORMATION |
709 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
711 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
712 security_info_sent));
713 return NT_STATUS_OK; /* won't show error - later to be refined... */
716 /* Special behaviours */
717 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
718 return NT_STATUS_NO_MEMORY;
720 if (smbacl4_fGetFileOwner(fsp, &sbuf))
721 return map_nt_error_from_unix(errno);
723 if (params.do_chown) {
724 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
725 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(8, ("unpack_nt_owners failed"));
728 return status;
730 if (((newUID != (uid_t)-1) && (sbuf.st_uid != newUID)) ||
731 ((newGID != (gid_t)-1) && (sbuf.st_gid != newGID))) {
732 need_chown = True;
734 if (need_chown) {
735 if ((newUID == (uid_t)-1 || newUID == current_user.ut.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;