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/>.
21 #include "nfs4_acls.h"
23 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
25 #define SMB_ACE4_INT_MAGIC 0x76F8A967
26 typedef struct _SMB_ACE4_INT_T
33 #define SMB_ACL4_INT_MAGIC 0x29A3E792
34 typedef struct _SMB_ACL4_INT_T
38 SMB_ACE4_INT_T *first;
42 extern struct current_user current_user;
43 extern int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid);
44 extern NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp,
45 uint32 security_info_sent, SEC_DESC *psd);
47 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *acl)
49 SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
52 DEBUG(2, ("acl is NULL\n"));
56 if (aclint->magic!=SMB_ACL4_INT_MAGIC)
58 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
65 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
67 SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
70 DEBUG(2, ("ace is NULL\n"));
74 if (aceint->magic!=SMB_ACE4_INT_MAGIC)
76 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
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));
89 DEBUG(0, ("TALLOC_SIZE failed\n"));
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();
104 ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
107 DEBUG(0, ("TALLOC_SIZE failed\n"));
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)
120 aclint->last->next = (void *)ace;
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);
134 return &aceint->prop;
137 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
139 SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
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);
152 return (SMB4ACE_T *)aclint->first;
155 uint32 smb_get_naces(SMB4ACL_T *acl)
157 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
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",
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,fsp->fh->fd, psbuf) != 0)
190 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
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;
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"));
223 for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
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);
239 case SMB_ACE4_WHO_GROUP:
240 sid_copy(&sid, psid_group);
242 case SMB_ACE4_WHO_EVERYONE:
243 sid_copy(&sid, &global_sid_World);
246 DEBUG(8, ("invalid special who id %d "
247 "ignored\n", ace->who.special_id));
250 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
251 gid_to_sid(&sid, ace->who.gid);
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 init_sec_access(&mask, ace->aceMask);
260 init_sec_ace(&nt_ace_list[good_aces++], &sid,
262 ace->aceFlags & 0xf);
265 *ppnt_ace_list = nt_ace_list;
266 *pgood_aces = good_aces;
271 NTSTATUS smb_get_nt_acl_nfs4(files_struct *fsp,
272 uint32 security_info,
273 SEC_DESC **ppdesc, SMB4ACL_T *acl)
276 SMB_STRUCT_STAT sbuf;
277 DOM_SID sid_owner, sid_group;
279 SEC_ACE *nt_ace_list = NULL;
281 TALLOC_CTX *mem_ctx = talloc_tos();
283 DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
285 if (acl==NULL || smb_get_naces(acl)==0)
286 return NT_STATUS_ACCESS_DENIED; /* special because we
287 * shouldn't alloc 0 for
290 if (smbacl4_fGetFileOwner(fsp, &sbuf))
291 return map_nt_error_from_unix(errno);
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);
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);
312 DEBUG(2,("make_sec_desc failed\n"));
313 return NT_STATUS_NO_MEMORY;
316 DEBUG(10, ("smb_get_nt_acl_nfs4 successfully exited with sd_size %d\n",
317 sec_desc_size(*ppdesc)));
322 enum smbacl4_mode_enum {e_simple=0, e_special=1};
323 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
325 typedef struct _smbacl4_vfs_params {
326 enum smbacl4_mode_enum mode;
328 enum smbacl4_acedup_enum acedup;
329 } smbacl4_vfs_params;
332 * Gather special parameters for NFS4 ACL handling
334 static int smbacl4_get_vfs_params(
335 const char *type_name,
337 smbacl4_vfs_params *params
340 static const struct enum_list enum_smbacl4_modes[] = {
341 { e_simple, "simple" },
342 { e_special, "special" }
344 static const struct enum_list enum_smbacl4_acedups[] = {
345 { e_dontcare, "dontcare" },
346 { e_reject, "reject" },
347 { e_ignore, "ignore" },
348 { e_merge, "merge" },
351 memset(params, 0, sizeof(smbacl4_vfs_params));
352 params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
353 SNUM(fsp->conn), type_name,
354 "mode", enum_smbacl4_modes, e_simple);
355 params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
357 params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
358 SNUM(fsp->conn), type_name,
359 "acedup", enum_smbacl4_acedups, e_dontcare);
361 DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
362 enum_smbacl4_modes[params->mode].name,
363 params->do_chown ? "true" : "false",
364 enum_smbacl4_acedups[params->acedup].name));
369 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *acl)
371 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
372 SMB_ACE4_INT_T *aceint;
374 DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
376 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
377 SMB_ACE4PROP_T *ace = &aceint->prop;
379 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
381 ace->aceFlags, ace->flags,
388 * Find 2 NFS4 who-special ACE property (non-copy!!!)
389 * match nonzero if "special" and who is equal
390 * return ace if found matching; otherwise NULL
392 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
394 SMB_ACE4PROP_T *aceNew)
396 SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
397 SMB_ACE4_INT_T *aceint;
399 for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
400 SMB_ACE4PROP_T *ace = &aceint->prop;
402 if (ace->flags == aceNew->flags &&
403 ace->aceType==aceNew->aceType &&
404 (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
405 (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
407 /* keep type safety; e.g. gid is an u.short */
408 if (ace->flags & SMB_ACE4_ID_SPECIAL)
410 if (ace->who.special_id==aceNew->who.special_id)
413 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
415 if (ace->who.gid==aceNew->who.gid)
418 if (ace->who.uid==aceNew->who.uid)
428 static int smbacl4_fill_ace4(
430 smbacl4_vfs_params *params,
433 SEC_ACE *ace_nt, /* input */
434 SMB_ACE4PROP_T *ace_v4 /* output */
437 const char *dom, *name;
438 enum lsa_SidType type;
442 DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
444 memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
445 ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
446 ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
447 ace_v4->aceMask = ace_nt->access_mask &
448 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
450 if (ace_v4->aceFlags!=ace_nt->flags)
451 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
452 ace_v4->aceFlags, ace_nt->flags));
454 if (ace_v4->aceMask!=ace_nt->access_mask)
455 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
456 ace_v4->aceMask, ace_nt->access_mask));
458 if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
459 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
460 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
462 if (!lookup_sid(mem_ctx, &ace_nt->trustee, &dom, &name, &type)) {
463 DEBUG(8, ("Could not find %s' type\n",
464 sid_string_dbg(&ace_nt->trustee)));
469 if (type == SID_NAME_USER) {
470 if (!sid_to_uid(&ace_nt->trustee, &uid)) {
471 DEBUG(2, ("Could not convert %s to uid\n",
472 sid_string_dbg(&ace_nt->trustee)));
476 if (params->mode==e_special && uid==ownerUID) {
477 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
478 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
480 ace_v4->who.uid = uid;
482 } else { /* else group? - TODO check it... */
483 if (!sid_to_gid(&ace_nt->trustee, &gid)) {
484 DEBUG(2, ("Could not convert %s to gid\n",
485 sid_string_dbg(&ace_nt->trustee)));
488 ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
490 if (params->mode==e_special && gid==ownerGID) {
491 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
492 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
494 ace_v4->who.gid = gid;
502 static int smbacl4_MergeIgnoreReject(
503 enum smbacl4_acedup_enum acedup,
504 SMB4ACL_T *acl, /* may modify it */
505 SMB_ACE4PROP_T *ace, /* the "new" ACE */
511 SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(acl, ace);
516 case e_merge: /* "merge" flags */
518 ace4found->aceFlags |= ace->aceFlags;
519 ace4found->aceMask |= ace->aceMask;
521 case e_ignore: /* leave out this record */
524 case e_reject: /* do an error */
525 DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
526 errno = EINVAL; /* SHOULD be set on any _real_ error */
536 static SMB4ACL_T *smbacl4_win2nfs4(
538 smbacl4_vfs_params *pparams,
545 TALLOC_CTX *mem_ctx = talloc_tos();
547 DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
549 acl = smb_create_smb4acl();
553 for(i=0; i<dacl->num_aces; i++) {
554 SMB_ACE4PROP_T ace_v4;
555 bool addNewACE = True;
557 if (smbacl4_fill_ace4(mem_ctx, pparams, ownerUID, ownerGID,
558 dacl->aces + i, &ace_v4))
561 if (pparams->acedup!=e_dontcare) {
562 if (smbacl4_MergeIgnoreReject(pparams->acedup, acl,
563 &ace_v4, &addNewACE, i))
568 smb_add_ace4(acl, &ace_v4);
574 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
575 uint32 security_info_sent,
577 set_nfs4acl_native_fn_t set_nfs4_native)
579 smbacl4_vfs_params params;
580 SMB4ACL_T *acl = NULL;
583 SMB_STRUCT_STAT sbuf;
584 uid_t newUID = (uid_t)-1;
585 gid_t newGID = (gid_t)-1;
587 DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
589 if ((security_info_sent & (DACL_SECURITY_INFORMATION |
590 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
592 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
593 security_info_sent));
594 return NT_STATUS_OK; /* won't show error - later to be refined... */
597 /* Special behaviours */
598 if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, ¶ms))
599 return NT_STATUS_NO_MEMORY;
601 if (smbacl4_fGetFileOwner(fsp, &sbuf))
602 return map_nt_error_from_unix(errno);
604 if (params.do_chown) {
605 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
606 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
607 if (!NT_STATUS_IS_OK(status)) {
608 DEBUG(8, ("unpack_nt_owners failed"));
611 if (((newUID != (uid_t)-1) && (sbuf.st_uid != newUID)) ||
612 ((newGID != (gid_t)-1) && (sbuf.st_gid != newGID))) {
613 if(try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
614 DEBUG(3,("chown %s, %u, %u failed. Error = %s.\n",
615 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID, strerror(errno) ));
616 if (errno == EPERM) {
617 return NT_STATUS_INVALID_OWNER;
619 return map_nt_error_from_unix(errno);
621 DEBUG(10,("chown %s, %u, %u succeeded.\n",
622 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
623 if (smbacl4_fGetFileOwner(fsp, &sbuf))
624 return map_nt_error_from_unix(errno);
628 if ((security_info_sent & DACL_SECURITY_INFORMATION)!=0 && psd->dacl!=NULL)
630 acl = smbacl4_win2nfs4(psd->dacl, ¶ms, sbuf.st_uid, sbuf.st_gid);
632 return map_nt_error_from_unix(errno);
634 smbacl4_dump_nfs4acl(10, acl);
636 result = set_nfs4_native(fsp, acl);
639 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
640 return map_nt_error_from_unix(errno);
643 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
645 DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));