2 Unix SMB/CIFS implementation.
3 SMB NT Security Descriptor / Unix permission conversion.
4 Copyright (C) Jeremy Allison 1994-2009.
5 Copyright (C) Andreas Gruenbacher 2002.
6 Copyright (C) Simo Sorce <idra@samba.org> 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25 #include "../libcli/security/security.h"
27 #include "passdb/lookup_sid.h"
29 #include "../librpc/gen_ndr/idmap.h"
30 #include "../librpc/gen_ndr/ndr_smb_acl.h"
31 #include "lib/param/loadparm.h"
33 extern const struct generic_mapping file_generic_mapping;
36 #define DBGC_CLASS DBGC_ACLS
38 /****************************************************************************
39 Data structures representing the internal ACE format.
40 ****************************************************************************/
42 enum ace_owner {UID_ACE, GID_ACE, WORLD_ACE};
43 enum ace_attribute {ALLOW_ACE, DENY_ACE}; /* Used for incoming NT ACLS. */
45 typedef struct canon_ace {
46 struct canon_ace *next, *prev;
48 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
49 struct dom_sid trustee;
50 enum ace_owner owner_type;
51 enum ace_attribute attr;
52 struct unixid unix_ug;
53 uint8_t ace_flags; /* From windows ACE entry. */
56 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
59 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
60 * attribute on disk - version 1.
61 * All values are little endian.
63 * | 1 | 1 | 2 | 2 | ....
64 * +------+------+-------------+---------------------+-------------+--------------------+
65 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
66 * +------+------+-------------+---------------------+-------------+--------------------+
71 * +------+-------------------+
72 * | value| uid/gid or world |
74 * +------+-------------------+
76 * Version 2 format. Stores extra Windows metadata about an ACL.
78 * | 1 | 2 | 2 | 2 | ....
79 * +------+----------+-------------+---------------------+-------------+--------------------+
80 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
81 * | 2 | type | | | | |
82 * +------+----------+-------------+---------------------+-------------+--------------------+
87 * +------+------+-------------------+
88 * | ace | value| uid/gid or world |
89 * | flag | type | value |
90 * +------+-------------------+------+
94 #define PAI_VERSION_OFFSET 0
96 #define PAI_V1_FLAG_OFFSET 1
97 #define PAI_V1_NUM_ENTRIES_OFFSET 2
98 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
99 #define PAI_V1_ENTRIES_BASE 6
100 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
101 #define PAI_V1_ENTRY_LENGTH 5
103 #define PAI_V1_VERSION 1
105 #define PAI_V2_TYPE_OFFSET 1
106 #define PAI_V2_NUM_ENTRIES_OFFSET 3
107 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
108 #define PAI_V2_ENTRIES_BASE 7
109 #define PAI_V2_ENTRY_LENGTH 6
111 #define PAI_V2_VERSION 2
114 * In memory format of user.SAMBA_PAI attribute.
118 struct pai_entry *next, *prev;
120 enum ace_owner owner_type;
121 struct unixid unix_ug;
126 unsigned int num_entries;
127 struct pai_entry *entry_list;
128 unsigned int num_def_entries;
129 struct pai_entry *def_entry_list;
132 /************************************************************************
133 Return a uint32_t of the pai_entry principal.
134 ************************************************************************/
136 static uint32_t get_pai_entry_val(struct pai_entry *paie)
138 switch (paie->owner_type) {
140 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.id ));
141 return (uint32_t)paie->unix_ug.id;
143 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.id ));
144 return (uint32_t)paie->unix_ug.id;
147 DEBUG(10,("get_pai_entry_val: world ace\n"));
152 /************************************************************************
153 Return a uint32_t of the entry principal.
154 ************************************************************************/
156 static uint32_t get_entry_val(canon_ace *ace_entry)
158 switch (ace_entry->owner_type) {
160 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
161 return (uint32_t)ace_entry->unix_ug.id;
163 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
164 return (uint32_t)ace_entry->unix_ug.id;
167 DEBUG(10,("get_entry_val: world ace\n"));
172 /************************************************************************
173 Create the on-disk format (always v2 now). Caller must free.
174 ************************************************************************/
176 static char *create_pai_buf_v2(canon_ace *file_ace_list,
177 canon_ace *dir_ace_list,
181 char *pai_buf = NULL;
182 canon_ace *ace_list = NULL;
183 char *entry_offset = NULL;
184 unsigned int num_entries = 0;
185 unsigned int num_def_entries = 0;
188 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
192 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
196 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
198 *store_size = PAI_V2_ENTRIES_BASE +
199 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
201 pai_buf = talloc_array(talloc_tos(), char, *store_size);
206 /* Set up the header. */
207 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
208 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
209 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
210 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
211 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
213 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
214 (unsigned int)sd_type ));
216 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
219 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
220 uint8_t type_val = (uint8_t)ace_list->owner_type;
221 uint32_t entry_val = get_entry_val(ace_list);
223 SCVAL(entry_offset,0,ace_list->ace_flags);
224 SCVAL(entry_offset,1,type_val);
225 SIVAL(entry_offset,2,entry_val);
226 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
228 (unsigned int)ace_list->ace_flags,
229 (unsigned int)type_val,
230 (unsigned int)entry_val ));
232 entry_offset += PAI_V2_ENTRY_LENGTH;
235 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
236 uint8_t type_val = (uint8_t)ace_list->owner_type;
237 uint32_t entry_val = get_entry_val(ace_list);
239 SCVAL(entry_offset,0,ace_list->ace_flags);
240 SCVAL(entry_offset,1,type_val);
241 SIVAL(entry_offset,2,entry_val);
242 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
244 (unsigned int)ace_list->ace_flags,
245 (unsigned int)type_val,
246 (unsigned int)entry_val ));
248 entry_offset += PAI_V2_ENTRY_LENGTH;
254 /************************************************************************
255 Store the user.SAMBA_PAI attribute on disk.
256 ************************************************************************/
258 static void store_inheritance_attributes(files_struct *fsp,
259 canon_ace *file_ace_list,
260 canon_ace *dir_ace_list,
267 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
271 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
272 sd_type, &store_size);
274 if (fsp->fh->fd != -1) {
275 ret = SMB_VFS_FSETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
276 pai_buf, store_size, 0);
278 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name,
279 SAMBA_POSIX_INHERITANCE_EA_NAME,
280 pai_buf, store_size, 0);
283 TALLOC_FREE(pai_buf);
285 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
286 (unsigned int)sd_type,
289 if (ret == -1 && !no_acl_syscall_error(errno)) {
290 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno) ));
294 /************************************************************************
295 Delete the in memory inheritance info.
296 ************************************************************************/
298 static void free_inherited_info(struct pai_val *pal)
301 struct pai_entry *paie, *paie_next;
302 for (paie = pal->entry_list; paie; paie = paie_next) {
303 paie_next = paie->next;
306 for (paie = pal->def_entry_list; paie; paie = paie_next) {
307 paie_next = paie->next;
314 /************************************************************************
315 Get any stored ACE flags.
316 ************************************************************************/
318 static uint16_t get_pai_flags(struct pai_val *pal, canon_ace *ace_entry, bool default_ace)
320 struct pai_entry *paie;
326 /* If the entry exists it is inherited. */
327 for (paie = (default_ace ? pal->def_entry_list : pal->entry_list); paie; paie = paie->next) {
328 if (ace_entry->owner_type == paie->owner_type &&
329 get_entry_val(ace_entry) == get_pai_entry_val(paie))
330 return paie->ace_flags;
335 /************************************************************************
336 Ensure an attribute just read is valid - v1.
337 ************************************************************************/
339 static bool check_pai_ok_v1(const char *pai_buf, size_t pai_buf_data_size)
341 uint16_t num_entries;
342 uint16_t num_def_entries;
344 if (pai_buf_data_size < PAI_V1_ENTRIES_BASE) {
345 /* Corrupted - too small. */
349 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V1_VERSION) {
353 num_entries = SVAL(pai_buf,PAI_V1_NUM_ENTRIES_OFFSET);
354 num_def_entries = SVAL(pai_buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
356 /* Check the entry lists match. */
357 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
359 if (((num_entries + num_def_entries)*PAI_V1_ENTRY_LENGTH) +
360 PAI_V1_ENTRIES_BASE != pai_buf_data_size) {
367 /************************************************************************
368 Ensure an attribute just read is valid - v2.
369 ************************************************************************/
371 static bool check_pai_ok_v2(const char *pai_buf, size_t pai_buf_data_size)
373 uint16_t num_entries;
374 uint16_t num_def_entries;
376 if (pai_buf_data_size < PAI_V2_ENTRIES_BASE) {
377 /* Corrupted - too small. */
381 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V2_VERSION) {
385 num_entries = SVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET);
386 num_def_entries = SVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
388 /* Check the entry lists match. */
389 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
391 if (((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH) +
392 PAI_V2_ENTRIES_BASE != pai_buf_data_size) {
399 /************************************************************************
401 ************************************************************************/
403 static bool get_pai_owner_type(struct pai_entry *paie, const char *entry_offset)
405 paie->owner_type = (enum ace_owner)CVAL(entry_offset,0);
406 switch( paie->owner_type) {
408 paie->unix_ug.type = ID_TYPE_UID;
409 paie->unix_ug.id = (uid_t)IVAL(entry_offset,1);
410 DEBUG(10,("get_pai_owner_type: uid = %u\n",
411 (unsigned int)paie->unix_ug.id ));
414 paie->unix_ug.type = ID_TYPE_GID;
415 paie->unix_ug.id = (gid_t)IVAL(entry_offset,1);
416 DEBUG(10,("get_pai_owner_type: gid = %u\n",
417 (unsigned int)paie->unix_ug.id ));
420 paie->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
421 paie->unix_ug.id = -1;
422 DEBUG(10,("get_pai_owner_type: world ace\n"));
425 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
426 (unsigned int)paie->owner_type ));
432 /************************************************************************
434 ************************************************************************/
436 static const char *create_pai_v1_entries(struct pai_val *paiv,
437 const char *entry_offset,
442 for (i = 0; i < paiv->num_entries; i++) {
443 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
448 paie->ace_flags = SEC_ACE_FLAG_INHERITED_ACE;
449 if (!get_pai_owner_type(paie, entry_offset)) {
455 DLIST_ADD(paiv->entry_list, paie);
457 DLIST_ADD(paiv->def_entry_list, paie);
459 entry_offset += PAI_V1_ENTRY_LENGTH;
464 /************************************************************************
465 Convert to in-memory format from version 1.
466 ************************************************************************/
468 static struct pai_val *create_pai_val_v1(const char *buf, size_t size)
470 const char *entry_offset;
471 struct pai_val *paiv = NULL;
473 if (!check_pai_ok_v1(buf, size)) {
477 paiv = talloc(talloc_tos(), struct pai_val);
482 memset(paiv, '\0', sizeof(struct pai_val));
484 paiv->sd_type = (CVAL(buf,PAI_V1_FLAG_OFFSET) == PAI_V1_ACL_FLAG_PROTECTED) ?
485 SEC_DESC_DACL_PROTECTED : 0;
487 paiv->num_entries = SVAL(buf,PAI_V1_NUM_ENTRIES_OFFSET);
488 paiv->num_def_entries = SVAL(buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
490 entry_offset = buf + PAI_V1_ENTRIES_BASE;
492 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
493 paiv->num_entries, paiv->num_def_entries ));
495 entry_offset = create_pai_v1_entries(paiv, entry_offset, false);
496 if (entry_offset == NULL) {
497 free_inherited_info(paiv);
500 entry_offset = create_pai_v1_entries(paiv, entry_offset, true);
501 if (entry_offset == NULL) {
502 free_inherited_info(paiv);
509 /************************************************************************
511 ************************************************************************/
513 static const char *create_pai_v2_entries(struct pai_val *paiv,
514 unsigned int num_entries,
515 const char *entry_offset,
520 for (i = 0; i < num_entries; i++) {
521 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
526 paie->ace_flags = CVAL(entry_offset,0);
528 if (!get_pai_owner_type(paie, entry_offset+1)) {
533 DLIST_ADD(paiv->entry_list, paie);
535 DLIST_ADD(paiv->def_entry_list, paie);
537 entry_offset += PAI_V2_ENTRY_LENGTH;
542 /************************************************************************
543 Convert to in-memory format from version 2.
544 ************************************************************************/
546 static struct pai_val *create_pai_val_v2(const char *buf, size_t size)
548 const char *entry_offset;
549 struct pai_val *paiv = NULL;
551 if (!check_pai_ok_v2(buf, size)) {
555 paiv = talloc(talloc_tos(), struct pai_val);
560 memset(paiv, '\0', sizeof(struct pai_val));
562 paiv->sd_type = SVAL(buf,PAI_V2_TYPE_OFFSET);
564 paiv->num_entries = SVAL(buf,PAI_V2_NUM_ENTRIES_OFFSET);
565 paiv->num_def_entries = SVAL(buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
567 entry_offset = buf + PAI_V2_ENTRIES_BASE;
569 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
570 (unsigned int)paiv->sd_type,
571 paiv->num_entries, paiv->num_def_entries ));
573 entry_offset = create_pai_v2_entries(paiv, paiv->num_entries,
574 entry_offset, false);
575 if (entry_offset == NULL) {
576 free_inherited_info(paiv);
579 entry_offset = create_pai_v2_entries(paiv, paiv->num_def_entries,
581 if (entry_offset == NULL) {
582 free_inherited_info(paiv);
589 /************************************************************************
590 Convert to in-memory format - from either version 1 or 2.
591 ************************************************************************/
593 static struct pai_val *create_pai_val(const char *buf, size_t size)
598 if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V1_VERSION) {
599 return create_pai_val_v1(buf, size);
600 } else if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V2_VERSION) {
601 return create_pai_val_v2(buf, size);
607 /************************************************************************
608 Load the user.SAMBA_PAI attribute.
609 ************************************************************************/
611 static struct pai_val *fload_inherited_info(files_struct *fsp)
614 size_t pai_buf_size = 1024;
615 struct pai_val *paiv = NULL;
618 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
622 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
627 if (fsp->fh->fd != -1) {
628 ret = SMB_VFS_FGETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
629 pai_buf, pai_buf_size);
631 ret = SMB_VFS_GETXATTR(fsp->conn,
633 SAMBA_POSIX_INHERITANCE_EA_NAME,
634 pai_buf, pai_buf_size);
638 if (errno != ERANGE) {
641 /* Buffer too small - enlarge it. */
643 TALLOC_FREE(pai_buf);
644 if (pai_buf_size > 1024*1024) {
645 return NULL; /* Limit malloc to 1mb. */
647 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
652 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
653 (unsigned long)ret, fsp_str_dbg(fsp)));
656 /* No attribute or not supported. */
658 if (errno != ENOATTR)
659 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
662 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
664 TALLOC_FREE(pai_buf);
668 paiv = create_pai_val(pai_buf, ret);
671 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
672 (unsigned int)paiv->sd_type, fsp_str_dbg(fsp)));
675 TALLOC_FREE(pai_buf);
679 /************************************************************************
680 Load the user.SAMBA_PAI attribute.
681 ************************************************************************/
683 static struct pai_val *load_inherited_info(const struct connection_struct *conn,
684 const struct smb_filename *smb_fname)
687 size_t pai_buf_size = 1024;
688 struct pai_val *paiv = NULL;
691 if (!lp_map_acl_inherit(SNUM(conn))) {
695 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
700 ret = SMB_VFS_GETXATTR(conn, smb_fname,
701 SAMBA_POSIX_INHERITANCE_EA_NAME,
702 pai_buf, pai_buf_size);
705 if (errno != ERANGE) {
708 /* Buffer too small - enlarge it. */
710 TALLOC_FREE(pai_buf);
711 if (pai_buf_size > 1024*1024) {
712 return NULL; /* Limit malloc to 1mb. */
714 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
719 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
720 (unsigned long)ret, smb_fname->base_name));
723 /* No attribute or not supported. */
725 if (errno != ENOATTR)
726 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
729 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
731 TALLOC_FREE(pai_buf);
735 paiv = create_pai_val(pai_buf, ret);
738 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
739 (unsigned int)paiv->sd_type,
740 smb_fname->base_name));
743 TALLOC_FREE(pai_buf);
747 /****************************************************************************
748 Functions to manipulate the internal ACE format.
749 ****************************************************************************/
751 /****************************************************************************
752 Count a linked list of canonical ACE entries.
753 ****************************************************************************/
755 static size_t count_canon_ace_list( canon_ace *l_head )
760 for (ace = l_head; ace; ace = ace->next)
766 /****************************************************************************
767 Free a linked list of canonical ACE entries.
768 ****************************************************************************/
770 static void free_canon_ace_list( canon_ace *l_head )
772 canon_ace *list, *next;
774 for (list = l_head; list; list = next) {
776 DLIST_REMOVE(l_head, list);
781 /****************************************************************************
782 Function to duplicate a canon_ace entry.
783 ****************************************************************************/
785 static canon_ace *dup_canon_ace( canon_ace *src_ace)
787 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
793 dst_ace->prev = dst_ace->next = NULL;
797 /****************************************************************************
798 Print out a canon ace.
799 ****************************************************************************/
801 static void print_canon_ace(canon_ace *pace, int num)
803 struct dom_sid_buf buf;
804 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
805 dbgtext( "SID = %s ", dom_sid_str_buf(&pace->trustee, &buf));
806 if (pace->owner_type == UID_ACE) {
807 dbgtext( "uid %u ", (unsigned int)pace->unix_ug.id);
808 } else if (pace->owner_type == GID_ACE) {
809 dbgtext( "gid %u ", (unsigned int)pace->unix_ug.id);
812 switch (pace->type) {
814 dbgtext( "SMB_ACL_USER ");
816 case SMB_ACL_USER_OBJ:
817 dbgtext( "SMB_ACL_USER_OBJ ");
820 dbgtext( "SMB_ACL_GROUP ");
822 case SMB_ACL_GROUP_OBJ:
823 dbgtext( "SMB_ACL_GROUP_OBJ ");
826 dbgtext( "SMB_ACL_OTHER ");
833 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
835 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
836 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
837 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
840 /****************************************************************************
841 Print out a canon ace list.
842 ****************************************************************************/
844 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
848 if( DEBUGLVL( 10 )) {
849 dbgtext( "print_canon_ace_list: %s\n", name );
850 for (;ace_list; ace_list = ace_list->next, count++)
851 print_canon_ace(ace_list, count );
855 /****************************************************************************
856 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
857 ****************************************************************************/
859 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
863 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
864 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
865 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
870 /****************************************************************************
871 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
872 ****************************************************************************/
874 mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
888 /****************************************************************************
889 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
890 an SMB_ACL_PERMSET_T.
891 ****************************************************************************/
893 int map_acl_perms_to_permset(mode_t mode, SMB_ACL_PERMSET_T *p_permset)
895 if (sys_acl_clear_perms(*p_permset) == -1)
897 if (mode & S_IRUSR) {
898 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
901 if (mode & S_IWUSR) {
902 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
905 if (mode & S_IXUSR) {
906 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
912 /****************************************************************************
913 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
914 ****************************************************************************/
916 static void create_file_sids(const SMB_STRUCT_STAT *psbuf,
917 struct dom_sid *powner_sid,
918 struct dom_sid *pgroup_sid)
920 uid_to_sid( powner_sid, psbuf->st_ex_uid );
921 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
924 /****************************************************************************
925 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
926 delete the second one. If the first is deny, mask the permissions off and delete the allow
927 if the permissions become zero, delete the deny if the permissions are non zero.
928 ****************************************************************************/
930 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
932 canon_ace *l_head = *pp_list_head;
933 canon_ace *curr_ace_outer;
934 canon_ace *curr_ace_outer_next;
937 * First, merge allow entries with identical SIDs, and deny entries
938 * with identical SIDs.
941 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
943 canon_ace *curr_ace_next;
945 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
947 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
948 bool can_merge = false;
950 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
952 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
953 * types are the same. For directory acls we must also
954 * ensure the POSIX ACL types are the same.
956 * For the IDMAP_BOTH case, we must not merge
957 * the UID and GID ACE values for same SID
961 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
962 curr_ace->owner_type == curr_ace_outer->owner_type &&
963 (curr_ace->attr == curr_ace_outer->attr));
965 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
966 curr_ace->owner_type == curr_ace_outer->owner_type &&
967 (curr_ace->type == curr_ace_outer->type) &&
968 (curr_ace->attr == curr_ace_outer->attr));
972 if( DEBUGLVL( 10 )) {
973 dbgtext("merge_aces: Merging ACE's\n");
974 print_canon_ace( curr_ace_outer, 0);
975 print_canon_ace( curr_ace, 0);
978 /* Merge two allow or two deny ACE's. */
980 /* Theoretically we shouldn't merge a dir ACE if
981 * one ACE has the CI flag set, and the other
982 * ACE has the OI flag set, but this is rare
983 * enough we can ignore it. */
985 curr_ace_outer->perms |= curr_ace->perms;
986 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
987 DLIST_REMOVE(l_head, curr_ace);
988 TALLOC_FREE(curr_ace);
989 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
995 * Now go through and mask off allow permissions with deny permissions.
996 * We can delete either the allow or deny here as we know that each SID
997 * appears only once in the list.
1000 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
1001 canon_ace *curr_ace;
1002 canon_ace *curr_ace_next;
1004 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1006 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1008 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1011 * Subtract ACE's with different entries. Due to the ordering constraints
1012 * we've put on the ACL, we know the deny must be the first one.
1015 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
1016 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1017 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1019 if( DEBUGLVL( 10 )) {
1020 dbgtext("merge_aces: Masking ACE's\n");
1021 print_canon_ace( curr_ace_outer, 0);
1022 print_canon_ace( curr_ace, 0);
1025 curr_ace->perms &= ~curr_ace_outer->perms;
1027 if (curr_ace->perms == 0) {
1030 * The deny overrides the allow. Remove the allow.
1033 DLIST_REMOVE(l_head, curr_ace);
1034 TALLOC_FREE(curr_ace);
1035 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1040 * Even after removing permissions, there
1041 * are still allow permissions - delete the deny.
1042 * It is safe to delete the deny here,
1043 * as we are guarenteed by the deny first
1044 * ordering that all the deny entries for
1045 * this SID have already been merged into one
1046 * before we can get to an allow ace.
1049 DLIST_REMOVE(l_head, curr_ace_outer);
1050 TALLOC_FREE(curr_ace_outer);
1055 } /* end for curr_ace */
1056 } /* end for curr_ace_outer */
1058 /* We may have modified the list. */
1060 *pp_list_head = l_head;
1063 /****************************************************************************
1064 Map canon_ace perms to permission bits NT.
1065 The attr element is not used here - we only process deny entries on set,
1066 not get. Deny entries are implicit on get with ace->perms = 0.
1067 ****************************************************************************/
1069 uint32_t map_canon_ace_perms(int snum,
1070 enum security_ace_type *pacl_type,
1074 uint32_t nt_mask = 0;
1076 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1078 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1079 if (directory_ace) {
1080 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1082 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1084 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1086 * Windows NT refuses to display ACEs with no permissions in them (but
1087 * they are perfectly legal with Windows 2000). If the ACE has empty
1088 * permissions we cannot use 0, so we use the otherwise unused
1089 * WRITE_OWNER permission, which we ignore when we set an ACL.
1090 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1091 * to be changed in the future.
1096 if (directory_ace) {
1097 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1098 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1099 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1101 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1102 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1103 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1107 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1108 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1111 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1112 (unsigned int)perms, (unsigned int)nt_mask ));
1117 /****************************************************************************
1118 Map NT perms to a UNIX mode_t.
1119 ****************************************************************************/
1121 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1122 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1123 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1125 static mode_t map_nt_perms( uint32_t *mask, int type)
1131 if((*mask) & GENERIC_ALL_ACCESS)
1132 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1134 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1135 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1136 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1140 if((*mask) & GENERIC_ALL_ACCESS)
1141 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1143 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1144 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1145 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1149 if((*mask) & GENERIC_ALL_ACCESS)
1150 mode = S_IROTH|S_IWOTH|S_IXOTH;
1152 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1153 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1154 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1162 /****************************************************************************
1163 Unpack a struct security_descriptor into a UNIX owner and group.
1164 ****************************************************************************/
1166 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1167 uid_t *puser, gid_t *pgrp,
1168 uint32_t security_info_sent, const struct
1169 security_descriptor *psd)
1174 if(security_info_sent == 0) {
1175 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1176 return NT_STATUS_OK;
1180 * Validate the owner and group SID's.
1183 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1186 * Don't immediately fail if the owner sid cannot be validated.
1187 * This may be a group chown only set.
1190 if (security_info_sent & SECINFO_OWNER) {
1191 if (!sid_to_uid(psd->owner_sid, puser)) {
1192 if (lp_force_unknown_acl_user(SNUM(conn))) {
1193 /* this allows take ownership to work
1195 *puser = get_current_uid(conn);
1197 struct dom_sid_buf buf;
1198 DBG_NOTICE("unable to validate"
1199 " owner sid for %s\n",
1200 dom_sid_str_buf(psd->owner_sid,
1202 return NT_STATUS_INVALID_OWNER;
1205 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1206 (unsigned int)*puser ));
1210 * Don't immediately fail if the group sid cannot be validated.
1211 * This may be an owner chown only set.
1214 if (security_info_sent & SECINFO_GROUP) {
1215 if (!sid_to_gid(psd->group_sid, pgrp)) {
1216 if (lp_force_unknown_acl_user(SNUM(conn))) {
1217 /* this allows take group ownership to work
1219 *pgrp = get_current_gid(conn);
1221 DEBUG(3,("unpack_nt_owners: unable to validate"
1223 return NT_STATUS_INVALID_OWNER;
1226 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1227 (unsigned int)*pgrp));
1230 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1232 return NT_STATUS_OK;
1236 static void trim_ace_perms(canon_ace *pace)
1238 pace->perms = pace->perms & (S_IXUSR|S_IWUSR|S_IRUSR);
1241 static void ensure_minimal_owner_ace_perms(const bool is_directory,
1244 pace->perms |= S_IRUSR;
1246 pace->perms |= (S_IWUSR|S_IXUSR);
1250 /****************************************************************************
1251 Check if a given uid/SID is in a group gid/SID. This is probably very
1252 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1253 ****************************************************************************/
1255 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1257 bool is_sid = false;
1258 bool has_sid = false;
1259 struct security_token *security_token = NULL;
1261 /* "Everyone" always matches every uid. */
1263 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1267 * if we have session info in conn, we already have the (SID
1268 * based) NT token and don't need to do the complex
1269 * user_in_group_sid() call
1271 if (conn->session_info) {
1272 security_token = conn->session_info->security_token;
1273 /* security_token should not be NULL */
1274 SMB_ASSERT(security_token);
1275 is_sid = security_token_is_sid(security_token,
1278 has_sid = security_token_has_sid(security_token,
1279 &group_ace->trustee);
1289 * if it's the current user, we already have the unix token
1290 * and don't need to do the complex user_in_group_sid() call
1292 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1293 const struct security_unix_token *curr_utok = NULL;
1296 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1300 curr_utok = get_current_utok(conn);
1301 for (i=0; i < curr_utok->ngroups; i++) {
1302 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1309 * user_in_group_sid() uses create_token_from_sid()
1310 * which creates an artificial NT token given just a username,
1311 * so this is not reliable for users from foreign domains
1312 * exported by winbindd!
1314 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1317 /****************************************************************************
1318 A well formed POSIX file or default ACL has at least 3 entries, a
1319 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1320 In addition, the owner must always have at least read access.
1321 When using this call on get_acl, the pst struct is valid and contains
1322 the mode of the file.
1323 ****************************************************************************/
1325 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1327 const struct dom_sid *pfile_owner_sid,
1328 const struct dom_sid *pfile_grp_sid,
1329 const SMB_STRUCT_STAT *pst)
1332 bool got_user = false;
1333 bool got_group = false;
1334 bool got_other = false;
1336 for (pace = *pp_ace; pace; pace = pace->next) {
1337 if (pace->type == SMB_ACL_USER_OBJ) {
1339 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1341 } else if (pace->type == SMB_ACL_OTHER) {
1347 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1348 DEBUG(0,("malloc fail.\n"));
1353 pace->type = SMB_ACL_USER_OBJ;
1354 pace->owner_type = UID_ACE;
1355 pace->unix_ug.type = ID_TYPE_UID;
1356 pace->unix_ug.id = pst->st_ex_uid;
1357 pace->trustee = *pfile_owner_sid;
1358 pace->attr = ALLOW_ACE;
1359 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1360 DLIST_ADD(*pp_ace, pace);
1364 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1365 DEBUG(0,("malloc fail.\n"));
1370 pace->type = SMB_ACL_GROUP_OBJ;
1371 pace->owner_type = GID_ACE;
1372 pace->unix_ug.type = ID_TYPE_GID;
1373 pace->unix_ug.id = pst->st_ex_gid;
1374 pace->trustee = *pfile_grp_sid;
1375 pace->attr = ALLOW_ACE;
1376 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1377 DLIST_ADD(*pp_ace, pace);
1381 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1382 DEBUG(0,("malloc fail.\n"));
1387 pace->type = SMB_ACL_OTHER;
1388 pace->owner_type = WORLD_ACE;
1389 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1390 pace->unix_ug.id = -1;
1391 pace->trustee = global_sid_World;
1392 pace->attr = ALLOW_ACE;
1393 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1394 DLIST_ADD(*pp_ace, pace);
1400 /****************************************************************************
1401 A well formed POSIX file or default ACL has at least 3 entries, a
1402 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1403 In addition, the owner must always have at least read access.
1404 When using this call on set_acl, the pst struct has
1405 been modified to have a mode containing the default for this file or directory
1407 ****************************************************************************/
1409 static bool ensure_canon_entry_valid_on_set(connection_struct *conn,
1411 bool is_default_acl,
1412 const struct share_params *params,
1413 const bool is_directory,
1414 const struct dom_sid *pfile_owner_sid,
1415 const struct dom_sid *pfile_grp_sid,
1416 const SMB_STRUCT_STAT *pst)
1419 canon_ace *pace_user = NULL;
1420 canon_ace *pace_group = NULL;
1421 canon_ace *pace_other = NULL;
1422 bool got_duplicate_user = false;
1423 bool got_duplicate_group = false;
1425 for (pace = *pp_ace; pace; pace = pace->next) {
1426 trim_ace_perms(pace);
1427 if (pace->type == SMB_ACL_USER_OBJ) {
1428 ensure_minimal_owner_ace_perms(is_directory, pace);
1430 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1432 } else if (pace->type == SMB_ACL_OTHER) {
1438 canon_ace *pace_iter;
1440 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1441 DEBUG(0,("talloc fail.\n"));
1446 pace->type = SMB_ACL_USER_OBJ;
1447 pace->owner_type = UID_ACE;
1448 pace->unix_ug.type = ID_TYPE_UID;
1449 pace->unix_ug.id = pst->st_ex_uid;
1450 pace->trustee = *pfile_owner_sid;
1451 pace->attr = ALLOW_ACE;
1452 /* Start with existing user permissions, principle of least
1453 surprises for the user. */
1454 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1456 /* See if the owning user is in any of the other groups in
1457 the ACE, or if there's a matching user entry (by uid
1458 or in the case of ID_TYPE_BOTH by SID).
1459 If so, OR in the permissions from that entry. */
1462 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1463 if (pace_iter->type == SMB_ACL_USER &&
1464 pace_iter->unix_ug.id == pace->unix_ug.id) {
1465 pace->perms |= pace_iter->perms;
1466 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1467 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1468 pace->perms |= pace_iter->perms;
1469 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1470 pace->perms |= pace_iter->perms;
1475 if (pace->perms == 0) {
1476 /* If we only got an "everyone" perm, just use that. */
1478 pace->perms = pace_other->perms;
1482 * Ensure we have default parameters for the
1483 * user (owner) even on default ACLs.
1485 ensure_minimal_owner_ace_perms(is_directory, pace);
1487 DLIST_ADD(*pp_ace, pace);
1492 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1493 DEBUG(0,("talloc fail.\n"));
1498 pace->type = SMB_ACL_GROUP_OBJ;
1499 pace->owner_type = GID_ACE;
1500 pace->unix_ug.type = ID_TYPE_GID;
1501 pace->unix_ug.id = pst->st_ex_gid;
1502 pace->trustee = *pfile_grp_sid;
1503 pace->attr = ALLOW_ACE;
1505 /* If we only got an "everyone" perm, just use that. */
1507 pace->perms = pace_other->perms;
1512 DLIST_ADD(*pp_ace, pace);
1517 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1518 DEBUG(0,("talloc fail.\n"));
1523 pace->type = SMB_ACL_OTHER;
1524 pace->owner_type = WORLD_ACE;
1525 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1526 pace->unix_ug.id = -1;
1527 pace->trustee = global_sid_World;
1528 pace->attr = ALLOW_ACE;
1531 DLIST_ADD(*pp_ace, pace);
1535 /* Ensure when setting a POSIX ACL, that the uid for a
1536 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1537 permission entry as an SMB_ACL_USER, and a gid for a
1538 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1539 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1540 then if the ownership or group ownership of this file or
1541 directory gets changed, the user or group can lose their
1544 for (pace = *pp_ace; pace; pace = pace->next) {
1545 if (pace->type == SMB_ACL_USER &&
1546 pace->unix_ug.id == pace_user->unix_ug.id) {
1547 /* Already got one. */
1548 got_duplicate_user = true;
1549 } else if (pace->type == SMB_ACL_GROUP &&
1550 pace->unix_ug.id == pace_group->unix_ug.id) {
1551 /* Already got one. */
1552 got_duplicate_group = true;
1553 } else if ((pace->type == SMB_ACL_GROUP)
1554 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1555 /* If the SID owning the file appears
1556 * in a group entry, then we have
1557 * enough duplication, they will still
1559 got_duplicate_user = true;
1563 /* If the SID is equal for the user and group that we need
1564 to add the duplicate for, add only the group */
1565 if (!got_duplicate_user && !got_duplicate_group
1566 && dom_sid_equal(&pace_group->trustee,
1567 &pace_user->trustee)) {
1568 /* Add a duplicate SMB_ACL_GROUP entry, this
1569 * will cover the owning SID as well, as it
1570 * will always be mapped to both a uid and
1573 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1574 DEBUG(0,("talloc fail.\n"));
1579 pace->type = SMB_ACL_GROUP;;
1580 pace->owner_type = GID_ACE;
1581 pace->unix_ug.type = ID_TYPE_GID;
1582 pace->unix_ug.id = pace_group->unix_ug.id;
1583 pace->trustee = pace_group->trustee;
1584 pace->attr = pace_group->attr;
1585 pace->perms = pace_group->perms;
1587 DLIST_ADD(*pp_ace, pace);
1589 /* We're done here, make sure the
1590 statements below are not executed. */
1591 got_duplicate_user = true;
1592 got_duplicate_group = true;
1595 if (!got_duplicate_user) {
1596 /* Add a duplicate SMB_ACL_USER entry. */
1597 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1598 DEBUG(0,("talloc fail.\n"));
1603 pace->type = SMB_ACL_USER;;
1604 pace->owner_type = UID_ACE;
1605 pace->unix_ug.type = ID_TYPE_UID;
1606 pace->unix_ug.id = pace_user->unix_ug.id;
1607 pace->trustee = pace_user->trustee;
1608 pace->attr = pace_user->attr;
1609 pace->perms = pace_user->perms;
1611 DLIST_ADD(*pp_ace, pace);
1613 got_duplicate_user = true;
1616 if (!got_duplicate_group) {
1617 /* Add a duplicate SMB_ACL_GROUP entry. */
1618 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1619 DEBUG(0,("talloc fail.\n"));
1624 pace->type = SMB_ACL_GROUP;;
1625 pace->owner_type = GID_ACE;
1626 pace->unix_ug.type = ID_TYPE_GID;
1627 pace->unix_ug.id = pace_group->unix_ug.id;
1628 pace->trustee = pace_group->trustee;
1629 pace->attr = pace_group->attr;
1630 pace->perms = pace_group->perms;
1632 DLIST_ADD(*pp_ace, pace);
1634 got_duplicate_group = true;
1640 /****************************************************************************
1641 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1642 If it does not have them, check if there are any entries where the trustee is the
1643 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1644 Note we must not do this to default directory ACLs.
1645 ****************************************************************************/
1647 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1649 bool got_user_obj, got_group_obj;
1650 canon_ace *current_ace;
1653 entries = count_canon_ace_list(ace);
1654 got_user_obj = False;
1655 got_group_obj = False;
1657 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1658 if (current_ace->type == SMB_ACL_USER_OBJ)
1659 got_user_obj = True;
1660 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1661 got_group_obj = True;
1663 if (got_user_obj && got_group_obj) {
1664 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1668 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1669 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1670 dom_sid_equal(¤t_ace->trustee, pfile_owner_sid)) {
1671 current_ace->type = SMB_ACL_USER_OBJ;
1672 got_user_obj = True;
1674 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1675 dom_sid_equal(¤t_ace->trustee, pfile_grp_sid)) {
1676 current_ace->type = SMB_ACL_GROUP_OBJ;
1677 got_group_obj = True;
1681 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1683 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1686 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1687 canon_ace **file_ace, canon_ace **dir_ace,
1688 bool *got_file_allow, bool *got_dir_allow,
1689 bool *all_aces_are_inherit_only,
1690 canon_ace *current_ace)
1694 * Map the given NT permissions into a UNIX mode_t containing only
1695 * S_I(R|W|X)USR bits.
1698 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1699 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1701 /* Store the ace_flag. */
1702 current_ace->ace_flags = psa->flags;
1705 * Now add the created ace to either the file list, the directory
1706 * list, or both. We *MUST* preserve the order here (hence we use
1707 * DLIST_ADD_END) as NT ACLs are order dependent.
1710 if (fsp->fsp_flags.is_directory) {
1713 * We can only add to the default POSIX ACE list if the ACE is
1714 * designed to be inherited by both files and directories.
1717 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1718 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1720 canon_ace *current_dir_ace = current_ace;
1721 DLIST_ADD_END(*dir_ace, current_ace);
1724 * Note if this was an allow ace. We can't process
1725 * any further deny ace's after this.
1728 if (current_ace->attr == ALLOW_ACE)
1729 *got_dir_allow = True;
1731 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1732 DEBUG(0,("add_current_ace_to_acl: "
1734 "inheritable ACL! Deny entry "
1735 "after Allow entry. Failing "
1736 "to set on file %s.\n",
1741 if( DEBUGLVL( 10 )) {
1742 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1743 print_canon_ace( current_ace, 0);
1747 * If this is not an inherit only ACE we need to add a duplicate
1751 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1752 canon_ace *dup_ace = dup_canon_ace(current_ace);
1755 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1760 * We must not free current_ace here as its
1761 * pointer is now owned by the dir_ace list.
1763 current_ace = dup_ace;
1764 /* We've essentially split this ace into two,
1765 * and added the ace with inheritance request
1766 * bits to the directory ACL. Drop those bits for
1767 * the ACE we're adding to the file list. */
1768 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1769 SEC_ACE_FLAG_CONTAINER_INHERIT|
1770 SEC_ACE_FLAG_INHERIT_ONLY);
1773 * We must not free current_ace here as its
1774 * pointer is now owned by the dir_ace list.
1780 * current_ace is now either owned by file_ace
1781 * or is NULL. We can safely operate on current_dir_ace
1782 * to treat mapping for default acl entries differently
1783 * than access acl entries.
1786 if (current_dir_ace->owner_type == UID_ACE) {
1788 * We already decided above this is a uid,
1789 * for default acls ace's only CREATOR_OWNER
1790 * maps to ACL_USER_OBJ. All other uid
1791 * ace's are ACL_USER.
1793 if (dom_sid_equal(¤t_dir_ace->trustee,
1794 &global_sid_Creator_Owner)) {
1795 current_dir_ace->type = SMB_ACL_USER_OBJ;
1797 current_dir_ace->type = SMB_ACL_USER;
1801 if (current_dir_ace->owner_type == GID_ACE) {
1803 * We already decided above this is a gid,
1804 * for default acls ace's only CREATOR_GROUP
1805 * maps to ACL_GROUP_OBJ. All other uid
1806 * ace's are ACL_GROUP.
1808 if (dom_sid_equal(¤t_dir_ace->trustee,
1809 &global_sid_Creator_Group)) {
1810 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1812 current_dir_ace->type = SMB_ACL_GROUP;
1819 * Only add to the file ACL if not inherit only.
1822 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1823 DLIST_ADD_END(*file_ace, current_ace);
1826 * Note if this was an allow ace. We can't process
1827 * any further deny ace's after this.
1830 if (current_ace->attr == ALLOW_ACE)
1831 *got_file_allow = True;
1833 if ((current_ace->attr == DENY_ACE) && *got_file_allow) {
1834 DEBUG(0,("add_current_ace_to_acl: malformed "
1835 "ACL in file ACL ! Deny entry after "
1836 "Allow entry. Failing to set on file "
1837 "%s.\n", fsp_str_dbg(fsp)));
1841 if( DEBUGLVL( 10 )) {
1842 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1843 print_canon_ace( current_ace, 0);
1845 *all_aces_are_inherit_only = False;
1847 * We must not free current_ace here as its
1848 * pointer is now owned by the file_ace list.
1854 * Free if ACE was not added.
1857 TALLOC_FREE(current_ace);
1861 /****************************************************************************
1862 Unpack a struct security_descriptor into two canonical ace lists.
1863 ****************************************************************************/
1865 static bool create_canon_ace_lists(files_struct *fsp,
1866 const SMB_STRUCT_STAT *pst,
1867 struct dom_sid *pfile_owner_sid,
1868 struct dom_sid *pfile_grp_sid,
1869 canon_ace **ppfile_ace,
1870 canon_ace **ppdir_ace,
1871 const struct security_acl *dacl)
1873 bool all_aces_are_inherit_only = (fsp->fsp_flags.is_directory);
1874 canon_ace *file_ace = NULL;
1875 canon_ace *dir_ace = NULL;
1876 canon_ace *current_ace = NULL;
1877 bool got_dir_allow = False;
1878 bool got_file_allow = False;
1885 * Convert the incoming ACL into a more regular form.
1888 for(i = 0; i < dacl->num_aces; i++) {
1889 struct security_ace *psa = &dacl->aces[i];
1891 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1892 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1898 * Deal with the fact that NT 4.x re-writes the canonical format
1899 * that we return for default ACLs. If a directory ACE is identical
1900 * to a inherited directory ACE then NT changes the bits so that the
1901 * first ACE is set to OI|IO and the second ACE for this SID is set
1902 * to CI. We need to repair this. JRA.
1905 for(i = 0; i < dacl->num_aces; i++) {
1906 struct security_ace *psa1 = &dacl->aces[i];
1908 for (j = i + 1; j < dacl->num_aces; j++) {
1909 struct security_ace *psa2 = &dacl->aces[j];
1911 if (psa1->access_mask != psa2->access_mask)
1914 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1918 * Ok - permission bits and SIDs are equal.
1919 * Check if flags were re-written.
1922 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1924 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1925 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1927 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1929 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1930 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1936 for(i = 0; i < dacl->num_aces; i++) {
1937 struct security_ace *psa = &dacl->aces[i];
1940 * Create a canon_ace entry representing this NT DACL ACE.
1943 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1944 free_canon_ace_list(file_ace);
1945 free_canon_ace_list(dir_ace);
1946 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1950 ZERO_STRUCTP(current_ace);
1952 sid_copy(¤t_ace->trustee, &psa->trustee);
1955 * Try and work out if the SID is a user or group
1956 * as we need to flag these differently for POSIX.
1957 * Note what kind of a POSIX ACL this should map to.
1960 if( dom_sid_equal(¤t_ace->trustee, &global_sid_World)) {
1961 current_ace->owner_type = WORLD_ACE;
1962 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1963 current_ace->unix_ug.id = -1;
1964 current_ace->type = SMB_ACL_OTHER;
1965 } else if (dom_sid_equal(¤t_ace->trustee, &global_sid_Creator_Owner)) {
1966 current_ace->owner_type = UID_ACE;
1967 current_ace->unix_ug.type = ID_TYPE_UID;
1968 current_ace->unix_ug.id = pst->st_ex_uid;
1969 current_ace->type = SMB_ACL_USER_OBJ;
1972 * The Creator Owner entry only specifies inheritable permissions,
1973 * never access permissions. WinNT doesn't always set the ACE to
1974 * INHERIT_ONLY, though.
1977 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1979 } else if (dom_sid_equal(¤t_ace->trustee, &global_sid_Creator_Group)) {
1980 current_ace->owner_type = GID_ACE;
1981 current_ace->unix_ug.type = ID_TYPE_GID;
1982 current_ace->unix_ug.id = pst->st_ex_gid;
1983 current_ace->type = SMB_ACL_GROUP_OBJ;
1986 * The Creator Group entry only specifies inheritable permissions,
1987 * never access permissions. WinNT doesn't always set the ACE to
1988 * INHERIT_ONLY, though.
1990 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1993 struct unixid unixid;
1995 if (!sids_to_unixids(¤t_ace->trustee, 1, &unixid)) {
1996 struct dom_sid_buf buf;
1997 free_canon_ace_list(file_ace);
1998 free_canon_ace_list(dir_ace);
1999 TALLOC_FREE(current_ace);
2000 DBG_ERR("sids_to_unixids failed for %s "
2001 "(allocation failure)\n",
2002 dom_sid_str_buf(¤t_ace->trustee,
2007 if (unixid.type == ID_TYPE_BOTH) {
2009 * We must add both a user and group
2011 * This is due to the fact that in POSIX
2012 * user entries are more specific than
2015 current_ace->owner_type = UID_ACE;
2016 current_ace->unix_ug.type = ID_TYPE_UID;
2017 current_ace->unix_ug.id = unixid.id;
2019 (unixid.id == pst->st_ex_uid) ?
2023 /* Add the user object to the posix ACL,
2024 and proceed to the group mapping
2025 below. This handles the talloc_free
2026 of current_ace if not added for some
2028 if (!add_current_ace_to_acl(fsp,
2034 &all_aces_are_inherit_only,
2036 free_canon_ace_list(file_ace);
2037 free_canon_ace_list(dir_ace);
2041 if ((current_ace = talloc(talloc_tos(),
2042 canon_ace)) == NULL) {
2043 free_canon_ace_list(file_ace);
2044 free_canon_ace_list(dir_ace);
2045 DEBUG(0,("create_canon_ace_lists: "
2050 ZERO_STRUCTP(current_ace);
2052 sid_copy(¤t_ace->trustee, &psa->trustee);
2054 current_ace->unix_ug.type = ID_TYPE_GID;
2055 current_ace->unix_ug.id = unixid.id;
2056 current_ace->owner_type = GID_ACE;
2057 /* If it's the primary group, this is a
2058 group_obj, not a group. */
2059 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2060 current_ace->type = SMB_ACL_GROUP_OBJ;
2062 current_ace->type = SMB_ACL_GROUP;
2065 } else if (unixid.type == ID_TYPE_UID) {
2066 current_ace->owner_type = UID_ACE;
2067 current_ace->unix_ug.type = ID_TYPE_UID;
2068 current_ace->unix_ug.id = unixid.id;
2069 /* If it's the owning user, this is a user_obj,
2071 if (current_ace->unix_ug.id == pst->st_ex_uid) {
2072 current_ace->type = SMB_ACL_USER_OBJ;
2074 current_ace->type = SMB_ACL_USER;
2076 } else if (unixid.type == ID_TYPE_GID) {
2077 current_ace->unix_ug.type = ID_TYPE_GID;
2078 current_ace->unix_ug.id = unixid.id;
2079 current_ace->owner_type = GID_ACE;
2080 /* If it's the primary group, this is a
2081 group_obj, not a group. */
2082 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2083 current_ace->type = SMB_ACL_GROUP_OBJ;
2085 current_ace->type = SMB_ACL_GROUP;
2088 struct dom_sid_buf buf;
2090 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2093 if (non_mappable_sid(&psa->trustee)) {
2094 DBG_DEBUG("ignoring "
2095 "non-mappable SID %s\n",
2099 TALLOC_FREE(current_ace);
2103 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2104 DBG_DEBUG("ignoring unknown or "
2109 TALLOC_FREE(current_ace);
2113 free_canon_ace_list(file_ace);
2114 free_canon_ace_list(dir_ace);
2115 DBG_ERR("unable to map SID %s to uid or "
2117 dom_sid_str_buf(¤t_ace->trustee,
2119 TALLOC_FREE(current_ace);
2124 /* handles the talloc_free of current_ace if not added for some reason */
2125 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2126 &got_file_allow, &got_dir_allow,
2127 &all_aces_are_inherit_only, current_ace)) {
2128 free_canon_ace_list(file_ace);
2129 free_canon_ace_list(dir_ace);
2134 if (fsp->fsp_flags.is_directory && all_aces_are_inherit_only) {
2136 * Windows 2000 is doing one of these weird 'inherit acl'
2137 * traverses to conserve NTFS ACL resources. Just pretend
2138 * there was no DACL sent. JRA.
2141 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2142 free_canon_ace_list(file_ace);
2143 free_canon_ace_list(dir_ace);
2148 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2149 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2150 * entries can be converted to *_OBJ. Don't do this for the default
2151 * ACL, we will create them separately for this if needed inside
2152 * ensure_canon_entry_valid_on_set().
2155 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2159 *ppfile_ace = file_ace;
2160 *ppdir_ace = dir_ace;
2165 /****************************************************************************
2166 ASCII art time again... JRA :-).
2168 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2169 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2170 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2171 allow or deny have been merged, so the same SID can only appear once in the deny
2172 list or once in the allow list.
2174 We then process as follows :
2176 ---------------------------------------------------------------------------
2177 First pass - look for a Everyone DENY entry.
2179 If it is deny all (rwx) trunate the list at this point.
2180 Else, walk the list from this point and use the deny permissions of this
2181 entry as a mask on all following allow entries. Finally, delete
2182 the Everyone DENY entry (we have applied it to everything possible).
2184 In addition, in this pass we remove any DENY entries that have
2185 no permissions (ie. they are a DENY nothing).
2186 ---------------------------------------------------------------------------
2187 Second pass - only deal with deny user entries.
2189 DENY user1 (perms XXX)
2192 for all following allow group entries where user1 is in group
2193 new_perms |= group_perms;
2195 user1 entry perms = new_perms & ~ XXX;
2197 Convert the deny entry to an allow entry with the new perms and
2198 push to the end of the list. Note if the user was in no groups
2199 this maps to a specific allow nothing entry for this user.
2201 The common case from the NT ACL choser (userX deny all) is
2202 optimised so we don't do the group lookup - we just map to
2203 an allow nothing entry.
2205 What we're doing here is inferring the allow permissions the
2206 person setting the ACE on user1 wanted by looking at the allow
2207 permissions on the groups the user is currently in. This will
2208 be a snapshot, depending on group membership but is the best
2209 we can do and has the advantage of failing closed rather than
2211 ---------------------------------------------------------------------------
2212 Third pass - only deal with deny group entries.
2214 DENY group1 (perms XXX)
2216 for all following allow user entries where user is in group1
2217 user entry perms = user entry perms & ~ XXX;
2219 If there is a group Everyone allow entry with permissions YYY,
2220 convert the group1 entry to an allow entry and modify its
2223 new_perms = YYY & ~ XXX
2225 and push to the end of the list.
2227 If there is no group Everyone allow entry then convert the
2228 group1 entry to a allow nothing entry and push to the end of the list.
2230 Note that the common case from the NT ACL choser (groupX deny all)
2231 cannot be optimised here as we need to modify user entries who are
2232 in the group to change them to a deny all also.
2234 What we're doing here is modifying the allow permissions of
2235 user entries (which are more specific in POSIX ACLs) to mask
2236 out the explicit deny set on the group they are in. This will
2237 be a snapshot depending on current group membership but is the
2238 best we can do and has the advantage of failing closed rather
2240 ---------------------------------------------------------------------------
2241 Fourth pass - cope with cumulative permissions.
2243 for all allow user entries, if there exists an allow group entry with
2244 more permissive permissions, and the user is in that group, rewrite the
2245 allow user permissions to contain both sets of permissions.
2247 Currently the code for this is #ifdef'ed out as these semantics make
2248 no sense to me. JRA.
2249 ---------------------------------------------------------------------------
2251 Note we *MUST* do the deny user pass first as this will convert deny user
2252 entries into allow user entries which can then be processed by the deny
2255 The above algorithm took a *lot* of thinking about - hence this
2256 explaination :-). JRA.
2257 ****************************************************************************/
2259 /****************************************************************************
2260 Process a canon_ace list entries. This is very complex code. We need
2261 to go through and remove the "deny" permissions from any allow entry that matches
2262 the id of this entry. We have already refused any NT ACL that wasn't in correct
2263 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2264 we just remove it (to fail safe). We have already removed any duplicate ace
2265 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2267 ****************************************************************************/
2269 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2271 canon_ace *ace_list = *pp_ace_list;
2272 canon_ace *curr_ace = NULL;
2273 canon_ace *curr_ace_next = NULL;
2275 /* Pass 1 above - look for an Everyone, deny entry. */
2277 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2278 canon_ace *allow_ace_p;
2280 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2282 if (curr_ace->attr != DENY_ACE)
2285 if (curr_ace->perms == (mode_t)0) {
2287 /* Deny nothing entry - delete. */
2289 DLIST_REMOVE(ace_list, curr_ace);
2293 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2296 /* JRATEST - assert. */
2297 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2299 if (curr_ace->perms == ALL_ACE_PERMS) {
2302 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2303 * list at this point including this entry.
2306 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2308 free_canon_ace_list( curr_ace );
2310 DLIST_REMOVE(ace_list, prev_entry);
2312 /* We deleted the entire list. */
2318 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2321 * Only mask off allow entries.
2324 if (allow_ace_p->attr != ALLOW_ACE)
2327 allow_ace_p->perms &= ~curr_ace->perms;
2331 * Now it's been applied, remove it.
2334 DLIST_REMOVE(ace_list, curr_ace);
2337 /* Pass 2 above - deal with deny user entries. */
2339 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2340 mode_t new_perms = (mode_t)0;
2341 canon_ace *allow_ace_p;
2343 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2345 if (curr_ace->attr != DENY_ACE)
2348 if (curr_ace->owner_type != UID_ACE)
2351 if (curr_ace->perms == ALL_ACE_PERMS) {
2354 * Optimisation - this is a deny everything to this user.
2355 * Convert to an allow nothing and push to the end of the list.
2358 curr_ace->attr = ALLOW_ACE;
2359 curr_ace->perms = (mode_t)0;
2360 DLIST_DEMOTE(ace_list, curr_ace);
2364 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2366 if (allow_ace_p->attr != ALLOW_ACE)
2369 /* We process GID_ACE and WORLD_ACE entries only. */
2371 if (allow_ace_p->owner_type == UID_ACE)
2374 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2375 new_perms |= allow_ace_p->perms;
2379 * Convert to a allow entry, modify the perms and push to the end
2383 curr_ace->attr = ALLOW_ACE;
2384 curr_ace->perms = (new_perms & ~curr_ace->perms);
2385 DLIST_DEMOTE(ace_list, curr_ace);
2388 /* Pass 3 above - deal with deny group entries. */
2390 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2391 canon_ace *allow_ace_p;
2392 canon_ace *allow_everyone_p = NULL;
2394 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2396 if (curr_ace->attr != DENY_ACE)
2399 if (curr_ace->owner_type != GID_ACE)
2402 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2404 if (allow_ace_p->attr != ALLOW_ACE)
2407 /* Store a pointer to the Everyone allow, if it exists. */
2408 if (allow_ace_p->owner_type == WORLD_ACE)
2409 allow_everyone_p = allow_ace_p;
2411 /* We process UID_ACE entries only. */
2413 if (allow_ace_p->owner_type != UID_ACE)
2416 /* Mask off the deny group perms. */
2418 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2419 allow_ace_p->perms &= ~curr_ace->perms;
2423 * Convert the deny to an allow with the correct perms and
2424 * push to the end of the list.
2427 curr_ace->attr = ALLOW_ACE;
2428 if (allow_everyone_p)
2429 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2431 curr_ace->perms = (mode_t)0;
2432 DLIST_DEMOTE(ace_list, curr_ace);
2435 /* Doing this fourth pass allows Windows semantics to be layered
2436 * on top of POSIX semantics. I'm not sure if this is desirable.
2437 * For example, in W2K ACLs there is no way to say, "Group X no
2438 * access, user Y full access" if user Y is a member of group X.
2439 * This seems completely broken semantics to me.... JRA.
2443 /* Pass 4 above - deal with allow entries. */
2445 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2446 canon_ace *allow_ace_p;
2448 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2450 if (curr_ace->attr != ALLOW_ACE)
2453 if (curr_ace->owner_type != UID_ACE)
2456 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2458 if (allow_ace_p->attr != ALLOW_ACE)
2461 /* We process GID_ACE entries only. */
2463 if (allow_ace_p->owner_type != GID_ACE)
2466 /* OR in the group perms. */
2468 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2469 curr_ace->perms |= allow_ace_p->perms;
2474 *pp_ace_list = ace_list;
2477 /****************************************************************************
2478 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2480 ****************************************************************************/
2482 static bool unpack_canon_ace(files_struct *fsp,
2483 const SMB_STRUCT_STAT *pst,
2484 struct dom_sid *pfile_owner_sid,
2485 struct dom_sid *pfile_grp_sid,
2486 canon_ace **ppfile_ace,
2487 canon_ace **ppdir_ace,
2488 uint32_t security_info_sent,
2489 const struct security_descriptor *psd)
2491 canon_ace *file_ace = NULL;
2492 canon_ace *dir_ace = NULL;
2498 if(security_info_sent == 0) {
2499 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2504 * If no DACL then this is a chown only security descriptor.
2507 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2511 * Now go through the DACL and create the canon_ace lists.
2514 if (!create_canon_ace_lists(fsp, pst, pfile_owner_sid, pfile_grp_sid,
2515 &file_ace, &dir_ace, psd->dacl)) {
2519 if ((file_ace == NULL) && (dir_ace == NULL)) {
2520 /* W2K traverse DACL set - ignore. */
2525 * Go through the canon_ace list and merge entries
2526 * belonging to identical users of identical allow or deny type.
2527 * We can do this as all deny entries come first, followed by
2528 * all allow entries (we have mandated this before accepting this acl).
2531 print_canon_ace_list( "file ace - before merge", file_ace);
2532 merge_aces( &file_ace, false);
2534 print_canon_ace_list( "dir ace - before merge", dir_ace);
2535 merge_aces( &dir_ace, true);
2538 * NT ACLs are order dependent. Go through the acl lists and
2539 * process DENY entries by masking the allow entries.
2542 print_canon_ace_list( "file ace - before deny", file_ace);
2543 process_deny_list(fsp->conn, &file_ace);
2545 print_canon_ace_list( "dir ace - before deny", dir_ace);
2546 process_deny_list(fsp->conn, &dir_ace);
2549 * A well formed POSIX file or default ACL has at least 3 entries, a
2550 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2551 * and optionally a mask entry. Ensure this is the case.
2554 print_canon_ace_list( "file ace - before valid", file_ace);
2556 ok = ensure_canon_entry_valid_on_set(
2561 fsp->fsp_flags.is_directory,
2566 free_canon_ace_list(file_ace);
2567 free_canon_ace_list(dir_ace);
2571 print_canon_ace_list( "dir ace - before valid", dir_ace);
2573 if (dir_ace != NULL) {
2574 ok = ensure_canon_entry_valid_on_set(
2579 fsp->fsp_flags.is_directory,
2584 free_canon_ace_list(file_ace);
2585 free_canon_ace_list(dir_ace);
2590 print_canon_ace_list( "file ace - return", file_ace);
2591 print_canon_ace_list( "dir ace - return", dir_ace);
2593 *ppfile_ace = file_ace;
2594 *ppdir_ace = dir_ace;
2599 /******************************************************************************
2600 When returning permissions, try and fit NT display
2601 semantics if possible. Note the the canon_entries here must have been malloced.
2602 The list format should be - first entry = owner, followed by group and other user
2603 entries, last entry = other.
2605 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2606 are not ordered, and match on the most specific entry rather than walking a list,
2607 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2609 Entry 0: owner : deny all except read and write.
2610 Entry 1: owner : allow read and write.
2611 Entry 2: group : deny all except read.
2612 Entry 3: group : allow read.
2613 Entry 4: Everyone : allow read.
2615 But NT cannot display this in their ACL editor !
2616 ********************************************************************************/
2618 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2620 canon_ace *l_head = *pp_list_head;
2621 canon_ace *owner_ace = NULL;
2622 canon_ace *other_ace = NULL;
2623 canon_ace *ace = NULL;
2625 for (ace = l_head; ace; ace = ace->next) {
2626 if (ace->type == SMB_ACL_USER_OBJ)
2628 else if (ace->type == SMB_ACL_OTHER) {
2629 /* Last ace - this is "other" */
2634 if (!owner_ace || !other_ace) {
2635 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2641 * The POSIX algorithm applies to owner first, and other last,
2642 * so ensure they are arranged in this order.
2646 DLIST_PROMOTE(l_head, owner_ace);
2650 DLIST_DEMOTE(l_head, other_ace);
2653 /* We have probably changed the head of the list. */
2655 *pp_list_head = l_head;
2658 /****************************************************************************
2659 Create a linked list of canonical ACE entries.
2660 ****************************************************************************/
2662 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2663 const char *fname, SMB_ACL_T posix_acl,
2664 const SMB_STRUCT_STAT *psbuf,
2665 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2667 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2668 canon_ace *l_head = NULL;
2669 canon_ace *ace = NULL;
2670 canon_ace *next_ace = NULL;
2671 int entry_id = SMB_ACL_FIRST_ENTRY;
2672 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2673 SMB_ACL_ENTRY_T entry;
2676 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2677 SMB_ACL_TAG_T tagtype;
2678 SMB_ACL_PERMSET_T permset;
2680 struct unixid unix_ug;
2681 enum ace_owner owner_type;
2683 entry_id = SMB_ACL_NEXT_ENTRY;
2685 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2688 if (sys_acl_get_permset(entry, &permset) == -1)
2691 /* Decide which SID to use based on the ACL type. */
2693 case SMB_ACL_USER_OBJ:
2694 /* Get the SID from the owner. */
2695 sid_copy(&sid, powner);
2696 unix_ug.type = ID_TYPE_UID;
2697 unix_ug.id = psbuf->st_ex_uid;
2698 owner_type = UID_ACE;
2702 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2704 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2707 uid_to_sid( &sid, *puid);
2708 unix_ug.type = ID_TYPE_UID;
2710 owner_type = UID_ACE;
2713 case SMB_ACL_GROUP_OBJ:
2714 /* Get the SID from the owning group. */
2715 sid_copy(&sid, pgroup);
2716 unix_ug.type = ID_TYPE_GID;
2717 unix_ug.id = psbuf->st_ex_gid;
2718 owner_type = GID_ACE;
2722 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2724 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2727 gid_to_sid( &sid, *pgid);
2728 unix_ug.type = ID_TYPE_GID;
2730 owner_type = GID_ACE;
2734 acl_mask = convert_permset_to_mode_t(permset);
2735 continue; /* Don't count the mask as an entry. */
2737 /* Use the Everyone SID */
2738 sid = global_sid_World;
2739 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2741 owner_type = WORLD_ACE;
2744 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2749 * Add this entry to the list.
2752 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2755 *ace = (canon_ace) {
2757 .perms = convert_permset_to_mode_t(permset),
2761 .owner_type = owner_type
2763 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2765 DLIST_ADD(l_head, ace);
2769 * This next call will ensure we have at least a user/group/world set.
2772 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2778 * Now go through the list, masking the permissions with the
2779 * acl_mask. Ensure all DENY Entries are at the start of the list.
2782 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2784 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2785 next_ace = ace->next;
2787 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2788 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2789 ace->perms &= acl_mask;
2791 if (ace->perms == 0) {
2792 DLIST_PROMOTE(l_head, ace);
2795 if( DEBUGLVL( 10 ) ) {
2796 print_canon_ace(ace, ace_count);
2800 arrange_posix_perms(fname,&l_head );
2802 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2808 free_canon_ace_list(l_head);
2812 /****************************************************************************
2813 Check if the current user group list contains a given group.
2814 ****************************************************************************/
2816 bool current_user_in_group(connection_struct *conn, gid_t gid)
2819 const struct security_unix_token *utok = get_current_utok(conn);
2821 for (i = 0; i < utok->ngroups; i++) {
2822 if (utok->groups[i] == gid) {
2830 /****************************************************************************
2831 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2832 ****************************************************************************/
2834 static bool acl_group_override(connection_struct *conn,
2835 const struct smb_filename *smb_fname)
2837 if ((errno != EPERM) && (errno != EACCES)) {
2841 /* file primary group == user primary or supplementary group */
2842 if (lp_acl_group_control(SNUM(conn)) &&
2843 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2847 /* user has writeable permission */
2848 if (lp_dos_filemode(SNUM(conn)) &&
2849 can_write_to_file(conn, smb_fname)) {
2856 /****************************************************************************
2857 Attempt to apply an ACL to a file or directory.
2858 ****************************************************************************/
2860 static bool set_canon_ace_list(files_struct *fsp,
2863 const SMB_STRUCT_STAT *psbuf,
2864 bool *pacl_set_support)
2866 connection_struct *conn = fsp->conn;
2868 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2871 SMB_ACL_ENTRY_T mask_entry;
2872 bool got_mask_entry = False;
2873 SMB_ACL_PERMSET_T mask_permset;
2874 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2875 bool needs_mask = False;
2876 mode_t mask_perms = 0;
2878 /* Use the psbuf that was passed in. */
2879 if (psbuf != &fsp->fsp_name->st) {
2880 fsp->fsp_name->st = *psbuf;
2883 #if defined(POSIX_ACL_NEEDS_MASK)
2884 /* HP-UX always wants to have a mask (called "class" there). */
2888 if (the_acl == NULL) {
2889 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2893 if( DEBUGLVL( 10 )) {
2894 dbgtext("set_canon_ace_list: setting ACL:\n");
2895 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2896 print_canon_ace( p_ace, i);
2900 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2901 SMB_ACL_ENTRY_T the_entry;
2902 SMB_ACL_PERMSET_T the_permset;
2905 * ACLs only "need" an ACL_MASK entry if there are any named user or
2906 * named group entries. But if there is an ACL_MASK entry, it applies
2907 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2908 * so that it doesn't deny (i.e., mask off) any permissions.
2911 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2913 mask_perms |= p_ace->perms;
2914 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2915 mask_perms |= p_ace->perms;
2919 * Get the entry for this ACE.
2922 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2923 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2924 i, strerror(errno) ));
2928 if (p_ace->type == SMB_ACL_MASK) {
2929 mask_entry = the_entry;
2930 got_mask_entry = True;
2934 * Ok - we now know the ACL calls should be working, don't
2935 * allow fallback to chmod.
2938 *pacl_set_support = True;
2941 * Initialise the entry from the canon_ace.
2945 * First tell the entry what type of ACE this is.
2948 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
2949 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2950 i, strerror(errno) ));
2955 * Only set the qualifier (user or group id) if the entry is a user
2959 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2960 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
2961 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2962 i, strerror(errno) ));
2968 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2971 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
2972 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2973 i, strerror(errno) ));
2977 if (map_acl_perms_to_permset(p_ace->perms, &the_permset) == -1) {
2978 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2979 (unsigned int)p_ace->perms, i, strerror(errno) ));
2984 * ..and apply them to the entry.
2987 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
2988 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2989 i, strerror(errno) ));
2994 print_canon_ace( p_ace, i);
2998 if (needs_mask && !got_mask_entry) {
2999 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
3000 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
3004 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
3005 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
3009 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
3010 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
3014 if (map_acl_perms_to_permset(S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
3015 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
3019 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
3020 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
3026 * Finally apply it to the file or directory.
3029 if (default_ace || fsp->fsp_flags.is_directory || fsp->fh->fd == -1) {
3030 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name,
3031 the_acl_type, the_acl) == -1) {
3033 * Some systems allow all the above calls and only fail with no ACL support
3034 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3036 if (no_acl_syscall_error(errno)) {
3037 *pacl_set_support = False;
3040 if (acl_group_override(conn, fsp->fsp_name)) {
3043 DEBUG(5,("set_canon_ace_list: acl group "
3044 "control on and current user in file "
3045 "%s primary group.\n",
3049 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
3050 fsp->fsp_name, the_acl_type,
3059 DEBUG(2,("set_canon_ace_list: "
3060 "sys_acl_set_file type %s failed for "
3062 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3063 "directory default" : "file",
3064 fsp_str_dbg(fsp), strerror(errno)));
3069 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3071 * Some systems allow all the above calls and only fail with no ACL support
3072 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3074 if (no_acl_syscall_error(errno)) {
3075 *pacl_set_support = False;
3078 if (acl_group_override(conn, fsp->fsp_name)) {
3081 DEBUG(5,("set_canon_ace_list: acl group "
3082 "control on and current user in file "
3083 "%s primary group.\n",
3087 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3095 DEBUG(2,("set_canon_ace_list: "
3096 "sys_acl_set_file failed for file %s "
3098 fsp_str_dbg(fsp), strerror(errno)));
3108 if (the_acl != NULL) {
3109 TALLOC_FREE(the_acl);
3115 /****************************************************************************
3117 ****************************************************************************/
3119 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3121 SMB_ACL_ENTRY_T entry;
3125 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3126 TALLOC_FREE(the_acl);
3132 /****************************************************************************
3133 Convert a canon_ace to a generic 3 element permission - if possible.
3134 ****************************************************************************/
3136 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3138 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3140 size_t ace_count = count_canon_ace_list(file_ace_list);
3142 canon_ace *owner_ace = NULL;
3143 canon_ace *group_ace = NULL;
3144 canon_ace *other_ace = NULL;
3146 if (ace_count > 5) {
3147 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3148 "entries for file %s to convert to posix perms.\n",
3153 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3154 if (ace_p->owner_type == UID_ACE)
3156 else if (ace_p->owner_type == GID_ACE)
3158 else if (ace_p->owner_type == WORLD_ACE)
3162 if (!owner_ace || !group_ace || !other_ace) {
3163 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3164 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3169 * Ensure all ACE entries are owner, group or other.
3170 * We can't set if there are any other SIDs.
3172 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3173 if (ace_p == owner_ace || ace_p == group_ace ||
3174 ace_p == other_ace) {
3177 if (ace_p->owner_type == UID_ACE) {
3178 if (ace_p->unix_ug.id != owner_ace->unix_ug.id) {
3179 DEBUG(3,("Invalid uid %u in ACE for file %s.\n",
3180 (unsigned int)ace_p->unix_ug.id,
3184 } else if (ace_p->owner_type == GID_ACE) {
3185 if (ace_p->unix_ug.id != group_ace->unix_ug.id) {
3186 DEBUG(3,("Invalid gid %u in ACE for file %s.\n",
3187 (unsigned int)ace_p->unix_ug.id,
3193 * There should be no duplicate WORLD_ACE entries.
3196 DEBUG(3,("Invalid type %u, uid %u in "
3197 "ACE for file %s.\n",
3198 (unsigned int)ace_p->owner_type,
3199 (unsigned int)ace_p->unix_ug.id,
3205 *posix_perms = (mode_t)0;
3207 *posix_perms |= owner_ace->perms;
3208 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3209 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3210 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3211 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3212 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3213 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3215 /* The owner must have at least read access. */
3217 *posix_perms |= S_IRUSR;
3218 if (fsp->fsp_flags.is_directory)
3219 *posix_perms |= (S_IWUSR|S_IXUSR);
3221 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3222 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3223 (int)group_ace->perms, (int)other_ace->perms,
3224 (int)*posix_perms, fsp_str_dbg(fsp)));
3229 /****************************************************************************
3230 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3231 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3232 with CI|OI set so it is inherited and also applies to the directory.
3233 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3234 ****************************************************************************/
3236 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3240 for (i = 0; i < num_aces; i++) {
3241 for (j = i+1; j < num_aces; j++) {
3242 uint32_t i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3243 uint32_t j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3244 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3245 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3247 /* We know the lower number ACE's are file entries. */
3248 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3249 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3250 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3251 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3253 (i_flags_ni == 0) &&
3254 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3255 SEC_ACE_FLAG_CONTAINER_INHERIT|
3256 SEC_ACE_FLAG_INHERIT_ONLY))) {
3258 * W2K wants to have access allowed zero access ACE's
3259 * at the end of the list. If the mask is zero, merge
3260 * the non-inherited ACE onto the inherited ACE.
3263 if (nt_ace_list[i].access_mask == 0) {
3264 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3265 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3266 ARRAY_DEL_ELEMENT(nt_ace_list, i, num_aces);
3268 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3269 (unsigned int)i, (unsigned int)j ));
3272 * These are identical except for the flags.
3273 * Merge the inherited ACE onto the non-inherited ACE.
3276 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3277 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3278 ARRAY_DEL_ELEMENT(nt_ace_list, j, num_aces);
3280 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3281 (unsigned int)j, (unsigned int)i ));
3293 /****************************************************************************
3294 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3295 the space for the return elements and returns the size needed to return the
3296 security descriptor. This should be the only external function needed for
3297 the UNIX style get ACL.
3298 ****************************************************************************/
3300 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3302 const SMB_STRUCT_STAT *sbuf,
3303 struct pai_val *pal,
3304 SMB_ACL_T posix_acl,
3306 uint32_t security_info,
3307 TALLOC_CTX *mem_ctx,
3308 struct security_descriptor **ppdesc)
3310 struct dom_sid owner_sid;
3311 struct dom_sid group_sid;
3313 struct security_acl *psa = NULL;
3314 size_t num_acls = 0;
3315 size_t num_def_acls = 0;
3316 size_t num_aces = 0;
3317 canon_ace *file_ace = NULL;
3318 canon_ace *dir_ace = NULL;
3319 struct security_ace *nt_ace_list = NULL;
3320 struct security_descriptor *psd = NULL;
3323 * Get the owner, group and world SIDs.
3326 create_file_sids(sbuf, &owner_sid, &group_sid);
3328 if (security_info & SECINFO_DACL) {
3331 * In the optimum case Creator Owner and Creator Group would be used for
3332 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3333 * would lead to usability problems under Windows: The Creator entries
3334 * are only available in browse lists of directories and not for files;
3335 * additionally the identity of the owning group couldn't be determined.
3336 * We therefore use those identities only for Default ACLs.
3339 /* Create the canon_ace lists. */
3340 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3341 &owner_sid, &group_sid, pal,
3342 SMB_ACL_TYPE_ACCESS);
3344 /* We must have *some* ACLS. */
3346 if (count_canon_ace_list(file_ace) == 0) {
3347 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3351 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3352 dir_ace = canonicalise_acl(conn, name, def_acl,
3354 &global_sid_Creator_Owner,
3355 &global_sid_Creator_Group,
3356 pal, SMB_ACL_TYPE_DEFAULT);
3360 * Create the NT ACE list from the canonical ace lists.
3365 enum security_ace_type nt_acl_type;
3367 num_acls = count_canon_ace_list(file_ace);
3368 num_def_acls = count_canon_ace_list(dir_ace);
3370 nt_ace_list = talloc_zero_array(
3371 talloc_tos(), struct security_ace,
3372 num_acls + num_def_acls);
3374 if (nt_ace_list == NULL) {
3375 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3380 * Create the NT ACE list from the canonical ace lists.
3383 for (ace = file_ace; ace != NULL; ace = ace->next) {
3384 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3387 S_ISDIR(sbuf->st_ex_mode));
3388 init_sec_ace(&nt_ace_list[num_aces++],
3395 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3396 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3399 S_ISDIR(sbuf->st_ex_mode));
3400 init_sec_ace(&nt_ace_list[num_aces++],
3405 SEC_ACE_FLAG_OBJECT_INHERIT|
3406 SEC_ACE_FLAG_CONTAINER_INHERIT|
3407 SEC_ACE_FLAG_INHERIT_ONLY);
3411 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3412 * Win2K needs this to get the inheritance correct when replacing ACLs
3413 * on a directory tree. Based on work by Jim @ IBM.
3416 num_aces = merge_default_aces(nt_ace_list, num_aces);
3420 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3421 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3425 } /* security_info & SECINFO_DACL */
3427 psd = make_standard_sec_desc(mem_ctx,
3428 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3429 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3434 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3440 * Windows 2000: The DACL_PROTECTED flag in the security
3441 * descriptor marks the ACL as non-inheriting, i.e., no
3442 * ACEs from higher level directories propagate to this
3443 * ACL. In the POSIX ACL model permissions are only
3444 * inherited at file create time, so ACLs never contain
3445 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3446 * flag doesn't seem to bother Windows NT.
3447 * Always set this if map acl inherit is turned off.
3449 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3450 psd->type |= SEC_DESC_DACL_PROTECTED;
3452 psd->type |= pal->sd_type;
3456 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3464 TALLOC_FREE(posix_acl);
3467 TALLOC_FREE(def_acl);
3469 free_canon_ace_list(file_ace);
3470 free_canon_ace_list(dir_ace);
3471 free_inherited_info(pal);
3472 TALLOC_FREE(nt_ace_list);
3474 return NT_STATUS_OK;
3477 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3478 TALLOC_CTX *mem_ctx,
3479 struct security_descriptor **ppdesc)
3481 SMB_STRUCT_STAT sbuf;
3482 SMB_ACL_T posix_acl = NULL;
3483 struct pai_val *pal;
3484 TALLOC_CTX *frame = talloc_stackframe();
3489 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3492 /* can it happen that fsp_name == NULL ? */
3493 if (fsp->fsp_flags.is_directory || fsp->fh->fd == -1) {
3494 status = posix_get_nt_acl(fsp->conn, fsp->fsp_name,
3495 security_info, mem_ctx, ppdesc);
3500 /* Get the stat struct for the owner info. */
3501 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3503 return map_nt_error_from_unix(errno);
3506 /* Get the ACL from the fd. */
3507 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, frame);
3509 pal = fload_inherited_info(fsp);
3511 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3512 &sbuf, pal, posix_acl, NULL,
3513 security_info, mem_ctx, ppdesc);
3518 NTSTATUS posix_get_nt_acl(struct connection_struct *conn,
3519 const struct smb_filename *smb_fname_in,
3520 uint32_t security_info,
3521 TALLOC_CTX *mem_ctx,
3522 struct security_descriptor **ppdesc)
3524 SMB_ACL_T posix_acl = NULL;
3525 SMB_ACL_T def_acl = NULL;
3526 struct pai_val *pal;
3527 struct smb_filename *smb_fname = NULL;
3529 TALLOC_CTX *frame = talloc_stackframe();
3534 DEBUG(10,("posix_get_nt_acl: called for file %s\n",
3535 smb_fname_in->base_name ));
3537 smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
3538 if (smb_fname == NULL) {
3540 return NT_STATUS_NO_MEMORY;
3543 /* Get the stat struct for the owner info. */
3545 * We can directly use SMB_VFS_STAT here, as if this was a
3546 * POSIX call on a symlink, we've already refused it.
3547 * For a Windows acl mapped call on a symlink, we want to follow
3550 ret = SMB_VFS_STAT(conn, smb_fname);
3554 return map_nt_error_from_unix(errno);
3557 /* Get the ACL from the path. */
3558 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
3559 SMB_ACL_TYPE_ACCESS, frame);
3561 /* If it's a directory get the default POSIX ACL. */
3562 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
3563 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
3564 SMB_ACL_TYPE_DEFAULT, frame);
3565 def_acl = free_empty_sys_acl(conn, def_acl);
3568 pal = load_inherited_info(conn, smb_fname);
3570 status = posix_get_nt_acl_common(conn,
3571 smb_fname->base_name,
3583 /****************************************************************************
3584 Try to chown a file. We will be able to chown it under the following conditions.
3586 1) If we have root privileges, then it will just work.
3587 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3588 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3589 4) If we have write permission to the file and dos_filemodes is set
3590 then allow chown to the currently authenticated user.
3591 ****************************************************************************/
3593 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3598 if(!CAN_WRITE(fsp->conn)) {
3599 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3603 ret = SMB_VFS_FCHOWN(fsp, uid, gid);
3605 return NT_STATUS_OK;
3608 /* Case (2) / (3) */
3609 if (lp_enable_privileges()) {
3610 bool has_take_ownership_priv = security_token_has_privilege(
3611 get_current_nttok(fsp->conn),
3612 SEC_PRIV_TAKE_OWNERSHIP);
3613 bool has_restore_priv = security_token_has_privilege(
3614 get_current_nttok(fsp->conn),
3617 if (has_restore_priv) {
3619 } else if (has_take_ownership_priv) {
3621 if (uid == get_current_uid(fsp->conn)) {
3624 has_take_ownership_priv = false;
3628 if (has_take_ownership_priv || has_restore_priv) {
3629 status = NT_STATUS_OK;
3631 ret = SMB_VFS_FCHOWN(fsp, uid, gid);
3633 status = map_nt_error_from_unix(errno);
3641 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3642 return NT_STATUS_ACCESS_DENIED;
3645 /* only allow chown to the current user. This is more secure,
3646 and also copes with the case where the SID in a take ownership ACL is
3647 a local SID on the users workstation
3649 if (uid != get_current_uid(fsp->conn)) {
3650 return NT_STATUS_INVALID_OWNER;
3653 status = NT_STATUS_OK;
3655 /* Keep the current file gid the same. */
3656 ret = SMB_VFS_FCHOWN(fsp, uid, (gid_t)-1);
3658 status = map_nt_error_from_unix(errno);
3665 /****************************************************************************
3666 Reply to set a security descriptor on an fsp. security_info_sent is the
3667 description of the following NT ACL.
3668 This should be the only external function needed for the UNIX style set ACL.
3669 We make a copy of psd_orig as internal functions modify the elements inside
3670 it, even though it's a const pointer.
3671 ****************************************************************************/
3673 NTSTATUS set_nt_acl(files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd_orig)
3675 connection_struct *conn = fsp->conn;
3676 uid_t user = (uid_t)-1;
3677 gid_t grp = (gid_t)-1;
3678 struct dom_sid file_owner_sid;
3679 struct dom_sid file_grp_sid;
3680 canon_ace *file_ace_list = NULL;
3681 canon_ace *dir_ace_list = NULL;
3682 bool acl_perms = False;
3683 mode_t orig_mode = (mode_t)0;
3685 bool set_acl_as_root = false;
3686 bool acl_set_support = false;
3688 struct security_descriptor *psd = NULL;
3690 DEBUG(10,("set_nt_acl: called for file %s\n",
3693 if (!CAN_WRITE(conn)) {
3694 DEBUG(10,("set acl rejected on read-only share\n"));
3695 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3698 if (psd_orig == NULL) {
3699 return NT_STATUS_INVALID_PARAMETER;
3703 * MS NFS mode, here's the deal: the client merely wants to
3704 * modify the mode, but roundtripping get_acl/set/acl would
3705 * add additional POSIX ACEs. So in case we get a request
3706 * containing a MS NFS mode SID, we do nothing here.
3708 if (security_descriptor_with_ms_nfs(psd_orig)) {
3709 return NT_STATUS_OK;
3712 psd = security_descriptor_copy(talloc_tos(), psd_orig);
3714 return NT_STATUS_NO_MEMORY;
3718 * Get the current state of the file.
3721 status = vfs_stat_fsp(fsp);
3722 if (!NT_STATUS_IS_OK(status)) {
3726 /* Save the original element we check against. */
3727 orig_mode = fsp->fsp_name->st.st_ex_mode;
3730 * Unpack the user/group/world id's.
3733 /* POSIX can't cope with missing owner/group. */
3734 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3735 security_info_sent &= ~SECINFO_OWNER;
3737 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3738 security_info_sent &= ~SECINFO_GROUP;
3741 /* If UNIX owner is inherited and Windows isn't, then
3742 * setting the UNIX owner based on Windows owner conflicts
3743 * with the inheritance rule
3745 if (lp_inherit_owner(SNUM(conn)) == INHERIT_OWNER_UNIX_ONLY) {
3746 security_info_sent &= ~SECINFO_OWNER;
3749 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
3750 if (!NT_STATUS_IS_OK(status)) {
3755 * Do we need to chown ? If so this must be done first as the incoming
3756 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3760 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
3761 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
3763 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
3764 fsp_str_dbg(fsp), (unsigned int)user,
3765 (unsigned int)grp));
3767 status = try_chown(fsp, user, grp);
3768 if(!NT_STATUS_IS_OK(status)) {
3769 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
3770 "= %s.\n", fsp_str_dbg(fsp),
3773 nt_errstr(status)));
3778 * Recheck the current state of the file, which may have changed.
3779 * (suid/sgid bits, for instance)
3782 status = vfs_stat_fsp(fsp);
3783 if (!NT_STATUS_IS_OK(status)) {
3787 /* Save the original element we check against. */
3788 orig_mode = fsp->fsp_name->st.st_ex_mode;
3790 /* If we successfully chowned, we know we must
3791 * be able to set the acl, so do it as root.
3793 set_acl_as_root = true;
3796 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
3798 if((security_info_sent & SECINFO_DACL) &&
3799 (psd->type & SEC_DESC_DACL_PRESENT) &&
3800 (psd->dacl == NULL)) {
3801 struct security_ace ace[3];
3803 /* We can't have NULL DACL in POSIX.
3804 Use owner/group/Everyone -> full access. */
3806 init_sec_ace(&ace[0],
3808 SEC_ACE_TYPE_ACCESS_ALLOWED,
3811 init_sec_ace(&ace[1],
3813 SEC_ACE_TYPE_ACCESS_ALLOWED,
3816 init_sec_ace(&ace[2],
3818 SEC_ACE_TYPE_ACCESS_ALLOWED,
3821 psd->dacl = make_sec_acl(talloc_tos(),
3825 if (psd->dacl == NULL) {
3826 return NT_STATUS_NO_MEMORY;
3828 security_acl_map_generic(psd->dacl, &file_generic_mapping);
3831 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
3832 &file_grp_sid, &file_ace_list,
3833 &dir_ace_list, security_info_sent, psd);
3835 /* Ignore W2K traverse DACL set. */
3836 if (!file_ace_list && !dir_ace_list) {
3837 return NT_STATUS_OK;
3841 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
3842 free_canon_ace_list(file_ace_list);
3843 free_canon_ace_list(dir_ace_list);
3844 return NT_STATUS_ACCESS_DENIED;
3848 * Only change security if we got a DACL.
3851 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
3852 free_canon_ace_list(file_ace_list);
3853 free_canon_ace_list(dir_ace_list);
3854 return NT_STATUS_OK;
3858 * Try using the POSIX ACL set first. Fall back to chmod if
3859 * we have no ACL support on this filesystem.
3862 if (acl_perms && file_ace_list) {
3863 if (set_acl_as_root) {
3866 ret = set_canon_ace_list(fsp, file_ace_list, false,
3867 &fsp->fsp_name->st, &acl_set_support);
3868 if (set_acl_as_root) {
3871 if (acl_set_support && ret == false) {
3872 DEBUG(3,("set_nt_acl: failed to set file acl on file "
3873 "%s (%s).\n", fsp_str_dbg(fsp),
3875 free_canon_ace_list(file_ace_list);
3876 free_canon_ace_list(dir_ace_list);
3877 return map_nt_error_from_unix(errno);
3881 if (acl_perms && acl_set_support && fsp->fsp_flags.is_directory) {
3883 if (set_acl_as_root) {
3886 ret = set_canon_ace_list(fsp, dir_ace_list, true,
3889 if (set_acl_as_root) {
3893 DEBUG(3,("set_nt_acl: failed to set default "
3894 "acl on directory %s (%s).\n",
3895 fsp_str_dbg(fsp), strerror(errno)));
3896 free_canon_ace_list(file_ace_list);
3897 free_canon_ace_list(dir_ace_list);
3898 return map_nt_error_from_unix(errno);
3904 * No default ACL - delete one if it exists.
3907 if (set_acl_as_root) {
3910 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
3912 if (set_acl_as_root) {
3916 if (acl_group_override(conn, fsp->fsp_name)) {
3917 DEBUG(5,("set_nt_acl: acl group "
3918 "control on and current user "
3919 "in file %s primary group. "
3920 "Override delete_def_acl\n",
3925 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
3932 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
3933 free_canon_ace_list(file_ace_list);
3934 free_canon_ace_list(dir_ace_list);
3935 return map_nt_error_from_unix(errno);
3941 if (acl_set_support) {
3942 if (set_acl_as_root) {
3945 store_inheritance_attributes(fsp,
3949 if (set_acl_as_root) {
3955 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
3958 if(!acl_set_support && acl_perms) {
3961 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
3962 free_canon_ace_list(file_ace_list);
3963 free_canon_ace_list(dir_ace_list);
3964 DEBUG(3,("set_nt_acl: failed to convert file acl to "
3965 "posix permissions for file %s.\n",
3967 return NT_STATUS_ACCESS_DENIED;
3970 if (orig_mode != posix_perms) {
3973 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
3974 fsp_str_dbg(fsp), (unsigned int)posix_perms));
3976 if (set_acl_as_root) {
3979 sret = SMB_VFS_FCHMOD(fsp, posix_perms);
3980 if (set_acl_as_root) {
3984 if (acl_group_override(conn, fsp->fsp_name)) {
3985 DEBUG(5,("set_nt_acl: acl group "
3986 "control on and current user "
3987 "in file %s primary group. "
3992 sret = SMB_VFS_FCHMOD(fsp, posix_perms);
3997 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
3998 "failed. Error = %s.\n",
4000 (unsigned int)posix_perms,
4002 free_canon_ace_list(file_ace_list);
4003 free_canon_ace_list(dir_ace_list);
4004 return map_nt_error_from_unix(errno);
4010 free_canon_ace_list(file_ace_list);
4011 free_canon_ace_list(dir_ace_list);
4013 /* Ensure the stat struct in the fsp is correct. */
4014 status = vfs_stat_fsp(fsp);
4016 return NT_STATUS_OK;
4019 /****************************************************************************
4020 Get the actual group bits stored on a file with an ACL. Has no effect if
4021 the file has no ACL. Needed in dosmode code where the stat() will return
4022 the mask bits, not the real group bits, for a file with an ACL.
4023 ****************************************************************************/
4025 int get_acl_group_bits( connection_struct *conn,
4026 const struct smb_filename *smb_fname,
4029 int entry_id = SMB_ACL_FIRST_ENTRY;
4030 SMB_ACL_ENTRY_T entry;
4031 SMB_ACL_T posix_acl;
4034 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
4035 SMB_ACL_TYPE_ACCESS, talloc_tos());
4036 if (posix_acl == (SMB_ACL_T)NULL)
4039 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4040 SMB_ACL_TAG_T tagtype;
4041 SMB_ACL_PERMSET_T permset;
4043 entry_id = SMB_ACL_NEXT_ENTRY;
4045 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
4048 if (tagtype == SMB_ACL_GROUP_OBJ) {
4049 if (sys_acl_get_permset(entry, &permset) == -1) {
4052 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4053 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
4054 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4055 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4061 TALLOC_FREE(posix_acl);
4065 /****************************************************************************
4066 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4067 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4068 ****************************************************************************/
4070 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4072 int entry_id = SMB_ACL_FIRST_ENTRY;
4073 SMB_ACL_ENTRY_T entry;
4074 int num_entries = 0;
4076 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4077 SMB_ACL_TAG_T tagtype;
4078 SMB_ACL_PERMSET_T permset;
4081 entry_id = SMB_ACL_NEXT_ENTRY;
4083 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
4086 if (sys_acl_get_permset(entry, &permset) == -1)
4092 case SMB_ACL_USER_OBJ:
4093 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4095 case SMB_ACL_GROUP_OBJ:
4096 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4100 * FIXME: The ACL_MASK entry permissions should really be set to
4101 * the union of the permissions of all ACL_USER,
4102 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4103 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4105 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4108 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4114 if (map_acl_perms_to_permset(perms, &permset) == -1)
4117 if (sys_acl_set_permset(entry, permset) == -1)
4122 * If this is a simple 3 element ACL or no elements then it's a standard
4123 * UNIX permission set. Just use chmod...
4126 if ((num_entries == 3) || (num_entries == 0))
4132 /****************************************************************************
4133 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4134 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4135 resulting ACL on TO. Note that name is in UNIX character set.
4136 ****************************************************************************/
4138 static int copy_access_posix_acl(connection_struct *conn,
4139 const struct smb_filename *smb_fname_from,
4140 const struct smb_filename *smb_fname_to,
4143 SMB_ACL_T posix_acl = NULL;
4146 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname_from,
4147 SMB_ACL_TYPE_ACCESS,
4148 talloc_tos())) == NULL)
4151 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4154 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, smb_fname_to,
4155 SMB_ACL_TYPE_ACCESS, posix_acl);
4159 TALLOC_FREE(posix_acl);
4163 /****************************************************************************
4164 Check for an existing default POSIX ACL on a directory.
4165 ****************************************************************************/
4167 static bool directory_has_default_posix_acl(connection_struct *conn,
4168 const struct smb_filename *smb_fname)
4170 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
4171 SMB_ACL_TYPE_DEFAULT,
4173 bool has_acl = False;
4174 SMB_ACL_ENTRY_T entry;
4176 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4181 TALLOC_FREE(def_acl);
4186 /****************************************************************************
4187 If the parent directory has no default ACL but it does have an Access ACL,
4188 inherit this Access ACL to file name.
4189 ****************************************************************************/
4191 int inherit_access_posix_acl(connection_struct *conn,
4192 const char *inherit_from_dir,
4193 const struct smb_filename *smb_fname,
4196 struct smb_filename *inherit_from_fname =
4197 synthetic_smb_fname(talloc_tos(),
4198 smb_fname->base_name,
4202 if (inherit_from_fname == NULL) {
4206 if (directory_has_default_posix_acl(conn, inherit_from_fname))
4209 return copy_access_posix_acl(conn, inherit_from_fname, smb_fname, mode);
4212 /****************************************************************************
4213 Map from wire type to permset.
4214 ****************************************************************************/
4216 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4218 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4222 if (sys_acl_clear_perms(*p_permset) == -1) {
4226 if (wire_perm & SMB_POSIX_ACL_READ) {
4227 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4231 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4232 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4236 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4237 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4244 /****************************************************************************
4245 Map from wire type to tagtype.
4246 ****************************************************************************/
4248 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4251 case SMB_POSIX_ACL_USER_OBJ:
4252 *p_tt = SMB_ACL_USER_OBJ;
4254 case SMB_POSIX_ACL_USER:
4255 *p_tt = SMB_ACL_USER;
4257 case SMB_POSIX_ACL_GROUP_OBJ:
4258 *p_tt = SMB_ACL_GROUP_OBJ;
4260 case SMB_POSIX_ACL_GROUP:
4261 *p_tt = SMB_ACL_GROUP;
4263 case SMB_POSIX_ACL_MASK:
4264 *p_tt = SMB_ACL_MASK;
4266 case SMB_POSIX_ACL_OTHER:
4267 *p_tt = SMB_ACL_OTHER;
4275 /****************************************************************************
4276 Create a new POSIX acl from wire permissions.
4277 FIXME ! How does the share mask/mode fit into this.... ?
4278 ****************************************************************************/
4280 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4283 TALLOC_CTX *mem_ctx)
4286 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4288 if (the_acl == NULL) {
4292 for (i = 0; i < num_acls; i++) {
4293 SMB_ACL_ENTRY_T the_entry;
4294 SMB_ACL_PERMSET_T the_permset;
4295 SMB_ACL_TAG_T tag_type;
4297 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4298 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4299 i, strerror(errno) ));
4303 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4304 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4305 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4309 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4310 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4311 i, strerror(errno) ));
4315 /* Get the permset pointer from the new ACL entry. */
4316 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4317 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4318 i, strerror(errno) ));
4322 /* Map from wire to permissions. */
4323 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4324 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4325 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4329 /* Now apply to the new ACL entry. */
4330 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4331 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4332 i, strerror(errno) ));
4336 if (tag_type == SMB_ACL_USER) {
4337 uint32_t uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4338 uid_t uid = (uid_t)uidval;
4339 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4340 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4341 (unsigned int)uid, i, strerror(errno) ));
4346 if (tag_type == SMB_ACL_GROUP) {
4347 uint32_t gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4348 gid_t gid = (uid_t)gidval;
4349 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4350 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4351 (unsigned int)gid, i, strerror(errno) ));
4361 if (the_acl != NULL) {
4362 TALLOC_FREE(the_acl);
4367 /****************************************************************************
4368 Calls from UNIX extensions - Default POSIX ACL set.
4369 If num_def_acls == 0 and not a directory just return. If it is a directory
4370 and num_def_acls == 0 then remove the default acl. Else set the default acl
4372 ****************************************************************************/
4374 NTSTATUS set_unix_posix_default_acl(connection_struct *conn,
4376 uint16_t num_def_acls,
4379 SMB_ACL_T def_acl = NULL;
4383 if (!fsp->fsp_flags.is_directory) {
4384 return NT_STATUS_INVALID_HANDLE;
4387 if (!num_def_acls) {
4388 /* Remove the default ACL. */
4389 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fsp->fsp_name);
4391 status = map_nt_error_from_unix(errno);
4392 DBG_INFO("acl_delete_def_file failed on "
4393 "directory %s (%s)\n",
4398 return NT_STATUS_OK;
4401 def_acl = create_posix_acl_from_wire(conn,
4405 if (def_acl == NULL) {
4406 return map_nt_error_from_unix(errno);
4409 ret = SMB_VFS_SYS_ACL_SET_FILE(conn,
4411 SMB_ACL_TYPE_DEFAULT,
4414 status = map_nt_error_from_unix(errno);
4415 DBG_INFO("acl_set_file failed on directory %s (%s)\n",
4418 TALLOC_FREE(def_acl);
4422 DBG_DEBUG("set default acl for file %s\n",
4424 TALLOC_FREE(def_acl);
4425 return NT_STATUS_OK;
4428 /****************************************************************************
4429 Remove an ACL from a file. As we don't have acl_delete_entry() available
4430 we must read the current acl and copy all entries except MASK, USER and GROUP
4431 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4433 FIXME ! How does the share mask/mode fit into this.... ?
4434 ****************************************************************************/
4436 static NTSTATUS remove_posix_acl(connection_struct *conn,
4439 SMB_ACL_T file_acl = NULL;
4440 int entry_id = SMB_ACL_FIRST_ENTRY;
4441 SMB_ACL_ENTRY_T entry;
4442 /* Create a new ACL with only 3 entries, u/g/w. */
4443 SMB_ACL_T new_file_acl = NULL;
4444 SMB_ACL_ENTRY_T user_ent = NULL;
4445 SMB_ACL_ENTRY_T group_ent = NULL;
4446 SMB_ACL_ENTRY_T other_ent = NULL;
4450 new_file_acl = sys_acl_init(talloc_tos());
4451 if (new_file_acl == NULL) {
4452 status = map_nt_error_from_unix(errno);
4453 DBG_INFO("failed to init new ACL with 3 entries "
4454 "for file %s %s.\n",
4460 /* Now create the u/g/w entries. */
4461 ret = sys_acl_create_entry(&new_file_acl, &user_ent);
4463 status = map_nt_error_from_unix(errno);
4464 DBG_INFO("Failed to create user entry for file %s. (%s)\n",
4469 ret = sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ);
4471 status = map_nt_error_from_unix(errno);
4472 DBG_INFO("Failed to set user entry for file %s. (%s)\n",
4478 ret = sys_acl_create_entry(&new_file_acl, &group_ent);
4480 status = map_nt_error_from_unix(errno);
4481 DBG_INFO("Failed to create group entry for file %s. (%s)\n",
4486 ret = sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ);
4488 status = map_nt_error_from_unix(errno);
4489 DBG_INFO("Failed to set group entry for file %s. (%s)\n",
4495 ret = sys_acl_create_entry(&new_file_acl, &other_ent);
4497 status = map_nt_error_from_unix(errno);
4498 DBG_INFO("Failed to create other entry for file %s. (%s)\n",
4503 ret = sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER);
4505 status = map_nt_error_from_unix(errno);
4506 DBG_INFO("Failed to set other entry for file %s. (%s)\n",
4512 /* Get the current file ACL. */
4513 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos());
4515 if (file_acl == NULL) {
4516 status = map_nt_error_from_unix(errno);
4517 /* This is only returned if an error occurred. Even for a file with
4518 no acl a u/g/w acl should be returned. */
4519 DBG_INFO("failed to get ACL from file %s (%s).\n",
4525 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4526 SMB_ACL_TAG_T tagtype;
4527 SMB_ACL_PERMSET_T permset;
4529 entry_id = SMB_ACL_NEXT_ENTRY;
4531 ret = sys_acl_get_tag_type(entry, &tagtype);
4533 status = map_nt_error_from_unix(errno);
4534 DBG_INFO("failed to get tagtype from ACL "
4535 "on file %s (%s).\n",
4541 ret = sys_acl_get_permset(entry, &permset);
4543 status = map_nt_error_from_unix(errno);
4544 DBG_INFO("failed to get permset from ACL "
4545 "on file %s (%s).\n",
4551 if (tagtype == SMB_ACL_USER_OBJ) {
4552 ret = sys_acl_set_permset(user_ent, permset);
4554 status = map_nt_error_from_unix(errno);
4555 DBG_INFO("failed to set permset from ACL "
4556 "on file %s (%s).\n",
4561 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4562 ret = sys_acl_set_permset(group_ent, permset);
4564 status = map_nt_error_from_unix(errno);
4565 DBG_INFO("failed to set permset from ACL "
4566 "on file %s (%s).\n",
4571 } else if (tagtype == SMB_ACL_OTHER) {
4572 ret = sys_acl_set_permset(other_ent, permset);
4574 status = map_nt_error_from_unix(errno);
4575 DBG_INFO("failed to set permset from ACL "
4576 "on file %s (%s).\n",
4584 /* Set the new empty file ACL. */
4585 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl);
4587 status = map_nt_error_from_unix(errno);
4588 DBG_INFO("acl_set_file failed on %s (%s)\n",
4594 status = NT_STATUS_OK;
4598 TALLOC_FREE(file_acl);
4599 TALLOC_FREE(new_file_acl);
4603 /****************************************************************************
4604 Calls from UNIX extensions - POSIX ACL set.
4605 If num_def_acls == 0 then read/modify/write acl after removing all entries
4606 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4607 ****************************************************************************/
4609 NTSTATUS set_unix_posix_acl(connection_struct *conn,
4614 SMB_ACL_T file_acl = NULL;
4619 /* Remove the ACL from the file. */
4620 return remove_posix_acl(conn, fsp);
4623 file_acl = create_posix_acl_from_wire(conn,
4627 if (file_acl == NULL) {
4628 return map_nt_error_from_unix(errno);
4631 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl);
4633 status = map_nt_error_from_unix(errno);
4634 DBG_INFO("acl_set_file failed on %s (%s)\n",
4637 TALLOC_FREE(file_acl);
4641 DBG_DEBUG("set acl for file %s\n",
4644 TALLOC_FREE(file_acl);
4645 return NT_STATUS_OK;
4648 /********************************************************************
4649 Pull the NT ACL from a file on disk or the OpenEventlog() access
4650 check. Caller is responsible for freeing the returned security
4651 descriptor via TALLOC_FREE(). This is designed for dealing with
4652 user space access checks in smbd outside of the VFS. For example,
4653 checking access rights in OpenEventlog() or from python.
4655 ********************************************************************/
4657 NTSTATUS get_nt_acl_no_snum(TALLOC_CTX *ctx,
4658 struct auth_session_info *session_info,
4660 uint32_t security_info_wanted,
4661 struct security_descriptor **sd)
4663 TALLOC_CTX *frame = talloc_stackframe();
4664 struct conn_struct_tos *c = NULL;
4665 NTSTATUS status = NT_STATUS_OK;
4666 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
4672 if (smb_fname == NULL) {
4674 return NT_STATUS_NO_MEMORY;
4677 if (!posix_locking_init(false)) {
4679 return NT_STATUS_NO_MEMORY;
4682 status = create_conn_struct_tos(global_messaging_context(),
4687 if (!NT_STATUS_IS_OK(status)) {
4688 DEBUG(0,("create_conn_struct returned %s.\n",
4689 nt_errstr(status)));
4694 status = SMB_VFS_GET_NT_ACL(c->conn,
4696 security_info_wanted,
4699 if (!NT_STATUS_IS_OK(status)) {
4700 DEBUG(0, ("get_nt_acl_no_snum: SMB_VFS_GET_NT_ACL returned %s.\n",
4701 nt_errstr(status)));
4709 int posix_sys_acl_blob_get_file(vfs_handle_struct *handle,
4710 const struct smb_filename *smb_fname_in,
4711 TALLOC_CTX *mem_ctx,
4712 char **blob_description,
4716 TALLOC_CTX *frame = talloc_stackframe();
4717 /* Initialise this to zero, in a portable way */
4718 struct smb_acl_wrapper acl_wrapper = {
4721 struct smb_filename *smb_fname = cp_smb_filename_nostream(frame,
4723 if (smb_fname == NULL) {
4729 acl_wrapper.access_acl
4730 = smb_vfs_call_sys_acl_get_file(handle,
4732 SMB_ACL_TYPE_ACCESS,
4735 ret = smb_vfs_call_stat(handle, smb_fname);
4741 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
4742 acl_wrapper.default_acl
4743 = smb_vfs_call_sys_acl_get_file(handle,
4745 SMB_ACL_TYPE_DEFAULT,
4749 acl_wrapper.owner = smb_fname->st.st_ex_uid;
4750 acl_wrapper.group = smb_fname->st.st_ex_gid;
4751 acl_wrapper.mode = smb_fname->st.st_ex_mode;
4753 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4755 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4761 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4762 if (!*blob_description) {
4772 int posix_sys_acl_blob_get_fd(vfs_handle_struct *handle,
4774 TALLOC_CTX *mem_ctx,
4775 char **blob_description,
4778 SMB_STRUCT_STAT sbuf;
4780 struct smb_acl_wrapper acl_wrapper;
4783 /* This ensures that we also consider the default ACL */
4784 if (fsp->fsp_flags.is_directory || fsp->fh->fd == -1) {
4785 return posix_sys_acl_blob_get_file(handle,
4791 frame = talloc_stackframe();
4793 acl_wrapper.default_acl = NULL;
4795 acl_wrapper.access_acl = smb_vfs_call_sys_acl_get_file(handle,
4797 SMB_ACL_TYPE_ACCESS,
4800 ret = smb_vfs_call_fstat(handle, fsp, &sbuf);
4806 acl_wrapper.owner = sbuf.st_ex_uid;
4807 acl_wrapper.group = sbuf.st_ex_gid;
4808 acl_wrapper.mode = sbuf.st_ex_mode;
4810 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4812 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4818 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4819 if (!*blob_description) {
4829 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
4831 const SMB_STRUCT_STAT *psbuf,
4832 struct security_descriptor **ppdesc)
4834 struct dom_sid owner_sid, group_sid;
4836 struct security_ace aces[4];
4837 uint32_t access_mask = 0;
4838 mode_t mode = psbuf->st_ex_mode;
4839 struct security_acl *new_dacl = NULL;
4842 DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
4844 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4845 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4848 We provide up to 4 ACEs
4855 if (mode & S_IRUSR) {
4856 if (mode & S_IWUSR) {
4857 access_mask |= SEC_RIGHTS_FILE_ALL;
4859 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4862 if (mode & S_IWUSR) {
4863 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4866 init_sec_ace(&aces[idx],
4868 SEC_ACE_TYPE_ACCESS_ALLOWED,
4874 if (mode & S_IRGRP) {
4875 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4877 if (mode & S_IWGRP) {
4878 /* note that delete is not granted - this matches posix behaviour */
4879 access_mask |= SEC_RIGHTS_FILE_WRITE;
4882 init_sec_ace(&aces[idx],
4884 SEC_ACE_TYPE_ACCESS_ALLOWED,
4891 if (mode & S_IROTH) {
4892 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4894 if (mode & S_IWOTH) {
4895 access_mask |= SEC_RIGHTS_FILE_WRITE;
4898 init_sec_ace(&aces[idx],
4900 SEC_ACE_TYPE_ACCESS_ALLOWED,
4906 init_sec_ace(&aces[idx],
4908 SEC_ACE_TYPE_ACCESS_ALLOWED,
4909 SEC_RIGHTS_FILE_ALL,
4913 new_dacl = make_sec_acl(ctx,
4919 return NT_STATUS_NO_MEMORY;
4922 *ppdesc = make_sec_desc(ctx,
4923 SECURITY_DESCRIPTOR_REVISION_1,
4924 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4931 return NT_STATUS_NO_MEMORY;
4933 return NT_STATUS_OK;
4936 static NTSTATUS make_default_acl_windows(TALLOC_CTX *ctx,
4938 const SMB_STRUCT_STAT *psbuf,
4939 struct security_descriptor **ppdesc)
4941 struct dom_sid owner_sid, group_sid;
4943 struct security_ace aces[4];
4944 uint32_t access_mask = 0;
4945 mode_t mode = psbuf->st_ex_mode;
4946 struct security_acl *new_dacl = NULL;
4949 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
4951 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4952 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4955 * We provide 2 ACEs:
4960 if (mode & S_IRUSR) {
4961 if (mode & S_IWUSR) {
4962 access_mask |= SEC_RIGHTS_FILE_ALL;
4964 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4967 if (mode & S_IWUSR) {
4968 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4971 init_sec_ace(&aces[idx],
4973 SEC_ACE_TYPE_ACCESS_ALLOWED,
4978 init_sec_ace(&aces[idx],
4980 SEC_ACE_TYPE_ACCESS_ALLOWED,
4981 SEC_RIGHTS_FILE_ALL,
4985 new_dacl = make_sec_acl(ctx,
4991 return NT_STATUS_NO_MEMORY;
4994 *ppdesc = make_sec_desc(ctx,
4995 SECURITY_DESCRIPTOR_REVISION_1,
4996 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
5003 return NT_STATUS_NO_MEMORY;
5005 return NT_STATUS_OK;
5008 static NTSTATUS make_default_acl_everyone(TALLOC_CTX *ctx,
5010 const SMB_STRUCT_STAT *psbuf,
5011 struct security_descriptor **ppdesc)
5013 struct dom_sid owner_sid, group_sid;
5015 struct security_ace aces[1];
5016 mode_t mode = psbuf->st_ex_mode;
5017 struct security_acl *new_dacl = NULL;
5020 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
5022 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
5023 gid_to_sid(&group_sid, psbuf->st_ex_gid);
5026 * We provide one ACEs: full access for everyone
5029 init_sec_ace(&aces[idx],
5031 SEC_ACE_TYPE_ACCESS_ALLOWED,
5032 SEC_RIGHTS_FILE_ALL,
5036 new_dacl = make_sec_acl(ctx,
5042 return NT_STATUS_NO_MEMORY;
5045 *ppdesc = make_sec_desc(ctx,
5046 SECURITY_DESCRIPTOR_REVISION_1,
5047 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
5054 return NT_STATUS_NO_MEMORY;
5056 return NT_STATUS_OK;
5059 static const struct enum_list default_acl_style_list[] = {
5060 {DEFAULT_ACL_POSIX, "posix"},
5061 {DEFAULT_ACL_WINDOWS, "windows"},
5062 {DEFAULT_ACL_EVERYONE, "everyone"},
5065 const struct enum_list *get_default_acl_style_list(void)
5067 return default_acl_style_list;
5070 NTSTATUS make_default_filesystem_acl(
5072 enum default_acl_style acl_style,
5074 const SMB_STRUCT_STAT *psbuf,
5075 struct security_descriptor **ppdesc)
5079 switch (acl_style) {
5080 case DEFAULT_ACL_POSIX:
5081 status = make_default_acl_posix(ctx, name, psbuf, ppdesc);
5084 case DEFAULT_ACL_WINDOWS:
5085 status = make_default_acl_windows(ctx, name, psbuf, ppdesc);
5088 case DEFAULT_ACL_EVERYONE:
5089 status = make_default_acl_everyone(ctx, name, psbuf, ppdesc);
5093 DBG_ERR("unknown acl style %d", acl_style);
5094 status = NT_STATUS_INTERNAL_ERROR;