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 "lib/param/loadparm.h"
32 extern const struct generic_mapping file_generic_mapping
;
35 #define DBGC_CLASS DBGC_ACLS
37 /****************************************************************************
38 Data structures representing the internal ACE format.
39 ****************************************************************************/
41 enum ace_owner
{UID_ACE
, GID_ACE
, WORLD_ACE
};
42 enum ace_attribute
{ALLOW_ACE
, DENY_ACE
}; /* Used for incoming NT ACLS. */
44 typedef struct canon_ace
{
45 struct canon_ace
*next
, *prev
;
47 mode_t perms
; /* Only use S_I(R|W|X)USR mode bits here. */
48 struct dom_sid trustee
;
49 enum ace_owner owner_type
;
50 enum ace_attribute attr
;
51 struct unixid unix_ug
;
52 uint8_t ace_flags
; /* From windows ACE entry. */
55 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
58 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
59 * attribute on disk - version 1.
60 * All values are little endian.
62 * | 1 | 1 | 2 | 2 | ....
63 * +------+------+-------------+---------------------+-------------+--------------------+
64 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
65 * +------+------+-------------+---------------------+-------------+--------------------+
70 * +------+-------------------+
71 * | value| uid/gid or world |
73 * +------+-------------------+
75 * Version 2 format. Stores extra Windows metadata about an ACL.
77 * | 1 | 2 | 2 | 2 | ....
78 * +------+----------+-------------+---------------------+-------------+--------------------+
79 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
80 * | 2 | type | | | | |
81 * +------+----------+-------------+---------------------+-------------+--------------------+
86 * +------+------+-------------------+
87 * | ace | value| uid/gid or world |
88 * | flag | type | value |
89 * +------+-------------------+------+
93 #define PAI_VERSION_OFFSET 0
95 #define PAI_V1_FLAG_OFFSET 1
96 #define PAI_V1_NUM_ENTRIES_OFFSET 2
97 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
98 #define PAI_V1_ENTRIES_BASE 6
99 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
100 #define PAI_V1_ENTRY_LENGTH 5
102 #define PAI_V1_VERSION 1
104 #define PAI_V2_TYPE_OFFSET 1
105 #define PAI_V2_NUM_ENTRIES_OFFSET 3
106 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
107 #define PAI_V2_ENTRIES_BASE 7
108 #define PAI_V2_ENTRY_LENGTH 6
110 #define PAI_V2_VERSION 2
113 * In memory format of user.SAMBA_PAI attribute.
117 struct pai_entry
*next
, *prev
;
119 enum ace_owner owner_type
;
120 struct unixid unix_ug
;
125 unsigned int num_entries
;
126 struct pai_entry
*entry_list
;
127 unsigned int num_def_entries
;
128 struct pai_entry
*def_entry_list
;
131 /************************************************************************
132 Return a uint32 of the pai_entry principal.
133 ************************************************************************/
135 static uint32_t get_pai_entry_val(struct pai_entry
*paie
)
137 switch (paie
->owner_type
) {
139 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie
->unix_ug
.id
));
140 return (uint32_t)paie
->unix_ug
.id
;
142 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie
->unix_ug
.id
));
143 return (uint32_t)paie
->unix_ug
.id
;
146 DEBUG(10,("get_pai_entry_val: world ace\n"));
151 /************************************************************************
152 Return a uint32 of the entry principal.
153 ************************************************************************/
155 static uint32_t get_entry_val(canon_ace
*ace_entry
)
157 switch (ace_entry
->owner_type
) {
159 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry
->unix_ug
.id
));
160 return (uint32_t)ace_entry
->unix_ug
.id
;
162 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry
->unix_ug
.id
));
163 return (uint32_t)ace_entry
->unix_ug
.id
;
166 DEBUG(10,("get_entry_val: world ace\n"));
171 /************************************************************************
172 Create the on-disk format (always v2 now). Caller must free.
173 ************************************************************************/
175 static char *create_pai_buf_v2(canon_ace
*file_ace_list
,
176 canon_ace
*dir_ace_list
,
180 char *pai_buf
= NULL
;
181 canon_ace
*ace_list
= NULL
;
182 char *entry_offset
= NULL
;
183 unsigned int num_entries
= 0;
184 unsigned int num_def_entries
= 0;
187 for (ace_list
= file_ace_list
; ace_list
; ace_list
= ace_list
->next
) {
191 for (ace_list
= dir_ace_list
; ace_list
; ace_list
= ace_list
->next
) {
195 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries
, num_def_entries
));
197 *store_size
= PAI_V2_ENTRIES_BASE
+
198 ((num_entries
+ num_def_entries
)*PAI_V2_ENTRY_LENGTH
);
200 pai_buf
= talloc_array(talloc_tos(), char, *store_size
);
205 /* Set up the header. */
206 memset(pai_buf
, '\0', PAI_V2_ENTRIES_BASE
);
207 SCVAL(pai_buf
,PAI_VERSION_OFFSET
,PAI_V2_VERSION
);
208 SSVAL(pai_buf
,PAI_V2_TYPE_OFFSET
, sd_type
);
209 SSVAL(pai_buf
,PAI_V2_NUM_ENTRIES_OFFSET
,num_entries
);
210 SSVAL(pai_buf
,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET
,num_def_entries
);
212 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
213 (unsigned int)sd_type
));
215 entry_offset
= pai_buf
+ PAI_V2_ENTRIES_BASE
;
218 for (ace_list
= file_ace_list
; ace_list
; ace_list
= ace_list
->next
) {
219 uint8_t type_val
= (uint8_t)ace_list
->owner_type
;
220 uint32_t entry_val
= get_entry_val(ace_list
);
222 SCVAL(entry_offset
,0,ace_list
->ace_flags
);
223 SCVAL(entry_offset
,1,type_val
);
224 SIVAL(entry_offset
,2,entry_val
);
225 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
227 (unsigned int)ace_list
->ace_flags
,
228 (unsigned int)type_val
,
229 (unsigned int)entry_val
));
231 entry_offset
+= PAI_V2_ENTRY_LENGTH
;
234 for (ace_list
= dir_ace_list
; ace_list
; ace_list
= ace_list
->next
) {
235 uint8_t type_val
= (uint8_t)ace_list
->owner_type
;
236 uint32_t entry_val
= get_entry_val(ace_list
);
238 SCVAL(entry_offset
,0,ace_list
->ace_flags
);
239 SCVAL(entry_offset
,1,type_val
);
240 SIVAL(entry_offset
,2,entry_val
);
241 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
243 (unsigned int)ace_list
->ace_flags
,
244 (unsigned int)type_val
,
245 (unsigned int)entry_val
));
247 entry_offset
+= PAI_V2_ENTRY_LENGTH
;
253 /************************************************************************
254 Store the user.SAMBA_PAI attribute on disk.
255 ************************************************************************/
257 static void store_inheritance_attributes(files_struct
*fsp
,
258 canon_ace
*file_ace_list
,
259 canon_ace
*dir_ace_list
,
266 if (!lp_map_acl_inherit(SNUM(fsp
->conn
))) {
270 pai_buf
= create_pai_buf_v2(file_ace_list
, dir_ace_list
,
271 sd_type
, &store_size
);
273 if (fsp
->fh
->fd
!= -1) {
274 ret
= SMB_VFS_FSETXATTR(fsp
, SAMBA_POSIX_INHERITANCE_EA_NAME
,
275 pai_buf
, store_size
, 0);
277 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->fsp_name
->base_name
,
278 SAMBA_POSIX_INHERITANCE_EA_NAME
,
279 pai_buf
, store_size
, 0);
282 TALLOC_FREE(pai_buf
);
284 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
285 (unsigned int)sd_type
,
288 if (ret
== -1 && !no_acl_syscall_error(errno
)) {
289 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno
) ));
293 /************************************************************************
294 Delete the in memory inheritance info.
295 ************************************************************************/
297 static void free_inherited_info(struct pai_val
*pal
)
300 struct pai_entry
*paie
, *paie_next
;
301 for (paie
= pal
->entry_list
; paie
; paie
= paie_next
) {
302 paie_next
= paie
->next
;
305 for (paie
= pal
->def_entry_list
; paie
; paie
= paie_next
) {
306 paie_next
= paie
->next
;
313 /************************************************************************
314 Get any stored ACE flags.
315 ************************************************************************/
317 static uint16_t get_pai_flags(struct pai_val
*pal
, canon_ace
*ace_entry
, bool default_ace
)
319 struct pai_entry
*paie
;
325 /* If the entry exists it is inherited. */
326 for (paie
= (default_ace
? pal
->def_entry_list
: pal
->entry_list
); paie
; paie
= paie
->next
) {
327 if (ace_entry
->owner_type
== paie
->owner_type
&&
328 get_entry_val(ace_entry
) == get_pai_entry_val(paie
))
329 return paie
->ace_flags
;
334 /************************************************************************
335 Ensure an attribute just read is valid - v1.
336 ************************************************************************/
338 static bool check_pai_ok_v1(const char *pai_buf
, size_t pai_buf_data_size
)
341 uint16 num_def_entries
;
343 if (pai_buf_data_size
< PAI_V1_ENTRIES_BASE
) {
344 /* Corrupted - too small. */
348 if (CVAL(pai_buf
,PAI_VERSION_OFFSET
) != PAI_V1_VERSION
) {
352 num_entries
= SVAL(pai_buf
,PAI_V1_NUM_ENTRIES_OFFSET
);
353 num_def_entries
= SVAL(pai_buf
,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET
);
355 /* Check the entry lists match. */
356 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
358 if (((num_entries
+ num_def_entries
)*PAI_V1_ENTRY_LENGTH
) +
359 PAI_V1_ENTRIES_BASE
!= pai_buf_data_size
) {
366 /************************************************************************
367 Ensure an attribute just read is valid - v2.
368 ************************************************************************/
370 static bool check_pai_ok_v2(const char *pai_buf
, size_t pai_buf_data_size
)
373 uint16 num_def_entries
;
375 if (pai_buf_data_size
< PAI_V2_ENTRIES_BASE
) {
376 /* Corrupted - too small. */
380 if (CVAL(pai_buf
,PAI_VERSION_OFFSET
) != PAI_V2_VERSION
) {
384 num_entries
= SVAL(pai_buf
,PAI_V2_NUM_ENTRIES_OFFSET
);
385 num_def_entries
= SVAL(pai_buf
,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET
);
387 /* Check the entry lists match. */
388 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
390 if (((num_entries
+ num_def_entries
)*PAI_V2_ENTRY_LENGTH
) +
391 PAI_V2_ENTRIES_BASE
!= pai_buf_data_size
) {
398 /************************************************************************
400 ************************************************************************/
402 static bool get_pai_owner_type(struct pai_entry
*paie
, const char *entry_offset
)
404 paie
->owner_type
= (enum ace_owner
)CVAL(entry_offset
,0);
405 switch( paie
->owner_type
) {
407 paie
->unix_ug
.type
= ID_TYPE_UID
;
408 paie
->unix_ug
.id
= (uid_t
)IVAL(entry_offset
,1);
409 DEBUG(10,("get_pai_owner_type: uid = %u\n",
410 (unsigned int)paie
->unix_ug
.id
));
413 paie
->unix_ug
.type
= ID_TYPE_GID
;
414 paie
->unix_ug
.id
= (gid_t
)IVAL(entry_offset
,1);
415 DEBUG(10,("get_pai_owner_type: gid = %u\n",
416 (unsigned int)paie
->unix_ug
.id
));
419 paie
->unix_ug
.type
= ID_TYPE_NOT_SPECIFIED
;
420 paie
->unix_ug
.id
= -1;
421 DEBUG(10,("get_pai_owner_type: world ace\n"));
424 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
425 (unsigned int)paie
->owner_type
));
431 /************************************************************************
433 ************************************************************************/
435 static const char *create_pai_v1_entries(struct pai_val
*paiv
,
436 const char *entry_offset
,
441 for (i
= 0; i
< paiv
->num_entries
; i
++) {
442 struct pai_entry
*paie
= talloc(talloc_tos(), struct pai_entry
);
447 paie
->ace_flags
= SEC_ACE_FLAG_INHERITED_ACE
;
448 if (!get_pai_owner_type(paie
, entry_offset
)) {
454 DLIST_ADD(paiv
->entry_list
, paie
);
456 DLIST_ADD(paiv
->def_entry_list
, paie
);
458 entry_offset
+= PAI_V1_ENTRY_LENGTH
;
463 /************************************************************************
464 Convert to in-memory format from version 1.
465 ************************************************************************/
467 static struct pai_val
*create_pai_val_v1(const char *buf
, size_t size
)
469 const char *entry_offset
;
470 struct pai_val
*paiv
= NULL
;
472 if (!check_pai_ok_v1(buf
, size
)) {
476 paiv
= talloc(talloc_tos(), struct pai_val
);
481 memset(paiv
, '\0', sizeof(struct pai_val
));
483 paiv
->sd_type
= (CVAL(buf
,PAI_V1_FLAG_OFFSET
) == PAI_V1_ACL_FLAG_PROTECTED
) ?
484 SEC_DESC_DACL_PROTECTED
: 0;
486 paiv
->num_entries
= SVAL(buf
,PAI_V1_NUM_ENTRIES_OFFSET
);
487 paiv
->num_def_entries
= SVAL(buf
,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET
);
489 entry_offset
= buf
+ PAI_V1_ENTRIES_BASE
;
491 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
492 paiv
->num_entries
, paiv
->num_def_entries
));
494 entry_offset
= create_pai_v1_entries(paiv
, entry_offset
, false);
495 if (entry_offset
== NULL
) {
496 free_inherited_info(paiv
);
499 entry_offset
= create_pai_v1_entries(paiv
, entry_offset
, true);
500 if (entry_offset
== NULL
) {
501 free_inherited_info(paiv
);
508 /************************************************************************
510 ************************************************************************/
512 static const char *create_pai_v2_entries(struct pai_val
*paiv
,
513 unsigned int num_entries
,
514 const char *entry_offset
,
519 for (i
= 0; i
< num_entries
; i
++) {
520 struct pai_entry
*paie
= talloc(talloc_tos(), struct pai_entry
);
525 paie
->ace_flags
= CVAL(entry_offset
,0);
527 if (!get_pai_owner_type(paie
, entry_offset
+1)) {
532 DLIST_ADD(paiv
->entry_list
, paie
);
534 DLIST_ADD(paiv
->def_entry_list
, paie
);
536 entry_offset
+= PAI_V2_ENTRY_LENGTH
;
541 /************************************************************************
542 Convert to in-memory format from version 2.
543 ************************************************************************/
545 static struct pai_val
*create_pai_val_v2(const char *buf
, size_t size
)
547 const char *entry_offset
;
548 struct pai_val
*paiv
= NULL
;
550 if (!check_pai_ok_v2(buf
, size
)) {
554 paiv
= talloc(talloc_tos(), struct pai_val
);
559 memset(paiv
, '\0', sizeof(struct pai_val
));
561 paiv
->sd_type
= SVAL(buf
,PAI_V2_TYPE_OFFSET
);
563 paiv
->num_entries
= SVAL(buf
,PAI_V2_NUM_ENTRIES_OFFSET
);
564 paiv
->num_def_entries
= SVAL(buf
,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET
);
566 entry_offset
= buf
+ PAI_V2_ENTRIES_BASE
;
568 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
569 (unsigned int)paiv
->sd_type
,
570 paiv
->num_entries
, paiv
->num_def_entries
));
572 entry_offset
= create_pai_v2_entries(paiv
, paiv
->num_entries
,
573 entry_offset
, false);
574 if (entry_offset
== NULL
) {
575 free_inherited_info(paiv
);
578 entry_offset
= create_pai_v2_entries(paiv
, paiv
->num_def_entries
,
580 if (entry_offset
== NULL
) {
581 free_inherited_info(paiv
);
588 /************************************************************************
589 Convert to in-memory format - from either version 1 or 2.
590 ************************************************************************/
592 static struct pai_val
*create_pai_val(const char *buf
, size_t size
)
597 if (CVAL(buf
,PAI_VERSION_OFFSET
) == PAI_V1_VERSION
) {
598 return create_pai_val_v1(buf
, size
);
599 } else if (CVAL(buf
,PAI_VERSION_OFFSET
) == PAI_V2_VERSION
) {
600 return create_pai_val_v2(buf
, size
);
606 /************************************************************************
607 Load the user.SAMBA_PAI attribute.
608 ************************************************************************/
610 static struct pai_val
*fload_inherited_info(files_struct
*fsp
)
613 size_t pai_buf_size
= 1024;
614 struct pai_val
*paiv
= NULL
;
617 if (!lp_map_acl_inherit(SNUM(fsp
->conn
))) {
621 if ((pai_buf
= talloc_array(talloc_tos(), char, pai_buf_size
)) == NULL
) {
626 if (fsp
->fh
->fd
!= -1) {
627 ret
= SMB_VFS_FGETXATTR(fsp
, SAMBA_POSIX_INHERITANCE_EA_NAME
,
628 pai_buf
, pai_buf_size
);
630 ret
= SMB_VFS_GETXATTR(fsp
->conn
,
631 fsp
->fsp_name
->base_name
,
632 SAMBA_POSIX_INHERITANCE_EA_NAME
,
633 pai_buf
, pai_buf_size
);
637 if (errno
!= ERANGE
) {
640 /* Buffer too small - enlarge it. */
642 TALLOC_FREE(pai_buf
);
643 if (pai_buf_size
> 1024*1024) {
644 return NULL
; /* Limit malloc to 1mb. */
646 if ((pai_buf
= talloc_array(talloc_tos(), char, pai_buf_size
)) == NULL
)
651 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
652 (unsigned long)ret
, fsp_str_dbg(fsp
)));
655 /* No attribute or not supported. */
657 if (errno
!= ENOATTR
)
658 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno
) ));
661 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno
) ));
663 TALLOC_FREE(pai_buf
);
667 paiv
= create_pai_val(pai_buf
, ret
);
670 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
671 (unsigned int)paiv
->sd_type
, fsp_str_dbg(fsp
)));
674 TALLOC_FREE(pai_buf
);
678 /************************************************************************
679 Load the user.SAMBA_PAI attribute.
680 ************************************************************************/
682 static struct pai_val
*load_inherited_info(const struct connection_struct
*conn
,
686 size_t pai_buf_size
= 1024;
687 struct pai_val
*paiv
= NULL
;
690 if (!lp_map_acl_inherit(SNUM(conn
))) {
694 if ((pai_buf
= talloc_array(talloc_tos(), char, pai_buf_size
)) == NULL
) {
699 ret
= SMB_VFS_GETXATTR(conn
, fname
,
700 SAMBA_POSIX_INHERITANCE_EA_NAME
,
701 pai_buf
, pai_buf_size
);
704 if (errno
!= ERANGE
) {
707 /* Buffer too small - enlarge it. */
709 TALLOC_FREE(pai_buf
);
710 if (pai_buf_size
> 1024*1024) {
711 return NULL
; /* Limit malloc to 1mb. */
713 if ((pai_buf
= talloc_array(talloc_tos(), char, pai_buf_size
)) == NULL
)
718 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n", (unsigned long)ret
, fname
));
721 /* No attribute or not supported. */
723 if (errno
!= ENOATTR
)
724 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno
) ));
727 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno
) ));
729 TALLOC_FREE(pai_buf
);
733 paiv
= create_pai_val(pai_buf
, ret
);
736 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
737 (unsigned int)paiv
->sd_type
,
741 TALLOC_FREE(pai_buf
);
745 /****************************************************************************
746 Functions to manipulate the internal ACE format.
747 ****************************************************************************/
749 /****************************************************************************
750 Count a linked list of canonical ACE entries.
751 ****************************************************************************/
753 static size_t count_canon_ace_list( canon_ace
*l_head
)
758 for (ace
= l_head
; ace
; ace
= ace
->next
)
764 /****************************************************************************
765 Free a linked list of canonical ACE entries.
766 ****************************************************************************/
768 static void free_canon_ace_list( canon_ace
*l_head
)
770 canon_ace
*list
, *next
;
772 for (list
= l_head
; list
; list
= next
) {
774 DLIST_REMOVE(l_head
, list
);
779 /****************************************************************************
780 Function to duplicate a canon_ace entry.
781 ****************************************************************************/
783 static canon_ace
*dup_canon_ace( canon_ace
*src_ace
)
785 canon_ace
*dst_ace
= talloc(talloc_tos(), canon_ace
);
791 dst_ace
->prev
= dst_ace
->next
= NULL
;
795 /****************************************************************************
796 Print out a canon ace.
797 ****************************************************************************/
799 static void print_canon_ace(canon_ace
*pace
, int num
)
801 dbgtext( "canon_ace index %d. Type = %s ", num
, pace
->attr
== ALLOW_ACE
? "allow" : "deny" );
802 dbgtext( "SID = %s ", sid_string_dbg(&pace
->trustee
));
803 if (pace
->owner_type
== UID_ACE
) {
804 const char *u_name
= uidtoname(pace
->unix_ug
.id
);
805 dbgtext( "uid %u (%s) ", (unsigned int)pace
->unix_ug
.id
, u_name
);
806 } else if (pace
->owner_type
== GID_ACE
) {
807 char *g_name
= gidtoname(pace
->unix_ug
.id
);
808 dbgtext( "gid %u (%s) ", (unsigned int)pace
->unix_ug
.id
, g_name
);
811 switch (pace
->type
) {
813 dbgtext( "SMB_ACL_USER ");
815 case SMB_ACL_USER_OBJ
:
816 dbgtext( "SMB_ACL_USER_OBJ ");
819 dbgtext( "SMB_ACL_GROUP ");
821 case SMB_ACL_GROUP_OBJ
:
822 dbgtext( "SMB_ACL_GROUP_OBJ ");
825 dbgtext( "SMB_ACL_OTHER ");
832 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace
->ace_flags
);
834 dbgtext( "%c", pace
->perms
& S_IRUSR
? 'r' : '-');
835 dbgtext( "%c", pace
->perms
& S_IWUSR
? 'w' : '-');
836 dbgtext( "%c\n", pace
->perms
& S_IXUSR
? 'x' : '-');
839 /****************************************************************************
840 Print out a canon ace list.
841 ****************************************************************************/
843 static void print_canon_ace_list(const char *name
, canon_ace
*ace_list
)
847 if( DEBUGLVL( 10 )) {
848 dbgtext( "print_canon_ace_list: %s\n", name
);
849 for (;ace_list
; ace_list
= ace_list
->next
, count
++)
850 print_canon_ace(ace_list
, count
);
854 /****************************************************************************
855 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
856 ****************************************************************************/
858 static mode_t
convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset
)
862 ret
|= (sys_acl_get_perm(permset
, SMB_ACL_READ
) ? S_IRUSR
: 0);
863 ret
|= (sys_acl_get_perm(permset
, SMB_ACL_WRITE
) ? S_IWUSR
: 0);
864 ret
|= (sys_acl_get_perm(permset
, SMB_ACL_EXECUTE
) ? S_IXUSR
: 0);
869 /****************************************************************************
870 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
871 ****************************************************************************/
873 static mode_t
unix_perms_to_acl_perms(mode_t mode
, int r_mask
, int w_mask
, int x_mask
)
887 /****************************************************************************
888 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
889 an SMB_ACL_PERMSET_T.
890 ****************************************************************************/
892 static int map_acl_perms_to_permset(connection_struct
*conn
, mode_t mode
, SMB_ACL_PERMSET_T
*p_permset
)
894 if (sys_acl_clear_perms(*p_permset
) == -1)
896 if (mode
& S_IRUSR
) {
897 if (sys_acl_add_perm(*p_permset
, SMB_ACL_READ
) == -1)
900 if (mode
& S_IWUSR
) {
901 if (sys_acl_add_perm(*p_permset
, SMB_ACL_WRITE
) == -1)
904 if (mode
& S_IXUSR
) {
905 if (sys_acl_add_perm(*p_permset
, SMB_ACL_EXECUTE
) == -1)
911 /****************************************************************************
912 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
913 ****************************************************************************/
915 void create_file_sids(const SMB_STRUCT_STAT
*psbuf
, struct dom_sid
*powner_sid
, struct dom_sid
*pgroup_sid
)
917 uid_to_sid( powner_sid
, psbuf
->st_ex_uid
);
918 gid_to_sid( pgroup_sid
, psbuf
->st_ex_gid
);
921 /****************************************************************************
922 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
923 delete the second one. If the first is deny, mask the permissions off and delete the allow
924 if the permissions become zero, delete the deny if the permissions are non zero.
925 ****************************************************************************/
927 static void merge_aces( canon_ace
**pp_list_head
, bool dir_acl
)
929 canon_ace
*l_head
= *pp_list_head
;
930 canon_ace
*curr_ace_outer
;
931 canon_ace
*curr_ace_outer_next
;
934 * First, merge allow entries with identical SIDs, and deny entries
935 * with identical SIDs.
938 for (curr_ace_outer
= l_head
; curr_ace_outer
; curr_ace_outer
= curr_ace_outer_next
) {
940 canon_ace
*curr_ace_next
;
942 curr_ace_outer_next
= curr_ace_outer
->next
; /* Save the link in case we delete. */
944 for (curr_ace
= curr_ace_outer
->next
; curr_ace
; curr_ace
= curr_ace_next
) {
945 bool can_merge
= false;
947 curr_ace_next
= curr_ace
->next
; /* Save the link in case of delete. */
949 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
950 * types are the same. For directory acls we must also
951 * ensure the POSIX ACL types are the same.
953 * For the IDMAP_BOTH case, we must not merge
954 * the UID and GID ACE values for same SID
958 can_merge
= (curr_ace
->unix_ug
.id
== curr_ace_outer
->unix_ug
.id
&&
959 curr_ace
->owner_type
== curr_ace_outer
->owner_type
&&
960 (curr_ace
->attr
== curr_ace_outer
->attr
));
962 can_merge
= (curr_ace
->unix_ug
.id
== curr_ace_outer
->unix_ug
.id
&&
963 curr_ace
->owner_type
== curr_ace_outer
->owner_type
&&
964 (curr_ace
->type
== curr_ace_outer
->type
) &&
965 (curr_ace
->attr
== curr_ace_outer
->attr
));
969 if( DEBUGLVL( 10 )) {
970 dbgtext("merge_aces: Merging ACE's\n");
971 print_canon_ace( curr_ace_outer
, 0);
972 print_canon_ace( curr_ace
, 0);
975 /* Merge two allow or two deny ACE's. */
977 /* Theoretically we shouldn't merge a dir ACE if
978 * one ACE has the CI flag set, and the other
979 * ACE has the OI flag set, but this is rare
980 * enough we can ignore it. */
982 curr_ace_outer
->perms
|= curr_ace
->perms
;
983 curr_ace_outer
->ace_flags
|= curr_ace
->ace_flags
;
984 DLIST_REMOVE(l_head
, curr_ace
);
985 TALLOC_FREE(curr_ace
);
986 curr_ace_outer_next
= curr_ace_outer
->next
; /* We may have deleted the link. */
992 * Now go through and mask off allow permissions with deny permissions.
993 * We can delete either the allow or deny here as we know that each SID
994 * appears only once in the list.
997 for (curr_ace_outer
= l_head
; curr_ace_outer
; curr_ace_outer
= curr_ace_outer_next
) {
999 canon_ace
*curr_ace_next
;
1001 curr_ace_outer_next
= curr_ace_outer
->next
; /* Save the link in case we delete. */
1003 for (curr_ace
= curr_ace_outer
->next
; curr_ace
; curr_ace
= curr_ace_next
) {
1005 curr_ace_next
= curr_ace
->next
; /* Save the link in case of delete. */
1008 * Subtract ACE's with different entries. Due to the ordering constraints
1009 * we've put on the ACL, we know the deny must be the first one.
1012 if (curr_ace
->unix_ug
.id
== curr_ace_outer
->unix_ug
.id
&&
1013 (curr_ace
->owner_type
== curr_ace_outer
->owner_type
) &&
1014 (curr_ace_outer
->attr
== DENY_ACE
) && (curr_ace
->attr
== ALLOW_ACE
)) {
1016 if( DEBUGLVL( 10 )) {
1017 dbgtext("merge_aces: Masking ACE's\n");
1018 print_canon_ace( curr_ace_outer
, 0);
1019 print_canon_ace( curr_ace
, 0);
1022 curr_ace
->perms
&= ~curr_ace_outer
->perms
;
1024 if (curr_ace
->perms
== 0) {
1027 * The deny overrides the allow. Remove the allow.
1030 DLIST_REMOVE(l_head
, curr_ace
);
1031 TALLOC_FREE(curr_ace
);
1032 curr_ace_outer_next
= curr_ace_outer
->next
; /* We may have deleted the link. */
1037 * Even after removing permissions, there
1038 * are still allow permissions - delete the deny.
1039 * It is safe to delete the deny here,
1040 * as we are guarenteed by the deny first
1041 * ordering that all the deny entries for
1042 * this SID have already been merged into one
1043 * before we can get to an allow ace.
1046 DLIST_REMOVE(l_head
, curr_ace_outer
);
1047 TALLOC_FREE(curr_ace_outer
);
1052 } /* end for curr_ace */
1053 } /* end for curr_ace_outer */
1055 /* We may have modified the list. */
1057 *pp_list_head
= l_head
;
1060 /****************************************************************************
1061 Map canon_ace perms to permission bits NT.
1062 The attr element is not used here - we only process deny entries on set,
1063 not get. Deny entries are implicit on get with ace->perms = 0.
1064 ****************************************************************************/
1066 uint32_t map_canon_ace_perms(int snum
,
1067 enum security_ace_type
*pacl_type
,
1071 uint32_t nt_mask
= 0;
1073 *pacl_type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
1075 if (lp_acl_map_full_control(snum
) && ((perms
& ALL_ACE_PERMS
) == ALL_ACE_PERMS
)) {
1076 if (directory_ace
) {
1077 nt_mask
= UNIX_DIRECTORY_ACCESS_RWX
;
1079 nt_mask
= (UNIX_ACCESS_RWX
& ~DELETE_ACCESS
);
1081 } else if ((perms
& ALL_ACE_PERMS
) == (mode_t
)0) {
1083 * Windows NT refuses to display ACEs with no permissions in them (but
1084 * they are perfectly legal with Windows 2000). If the ACE has empty
1085 * permissions we cannot use 0, so we use the otherwise unused
1086 * WRITE_OWNER permission, which we ignore when we set an ACL.
1087 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1088 * to be changed in the future.
1093 if (directory_ace
) {
1094 nt_mask
|= ((perms
& S_IRUSR
) ? UNIX_DIRECTORY_ACCESS_R
: 0 );
1095 nt_mask
|= ((perms
& S_IWUSR
) ? UNIX_DIRECTORY_ACCESS_W
: 0 );
1096 nt_mask
|= ((perms
& S_IXUSR
) ? UNIX_DIRECTORY_ACCESS_X
: 0 );
1098 nt_mask
|= ((perms
& S_IRUSR
) ? UNIX_ACCESS_R
: 0 );
1099 nt_mask
|= ((perms
& S_IWUSR
) ? UNIX_ACCESS_W
: 0 );
1100 nt_mask
|= ((perms
& S_IXUSR
) ? UNIX_ACCESS_X
: 0 );
1104 if ((perms
& S_IWUSR
) && lp_dos_filemode(snum
)) {
1105 nt_mask
|= (SEC_STD_WRITE_DAC
|SEC_STD_WRITE_OWNER
|DELETE_ACCESS
);
1108 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1109 (unsigned int)perms
, (unsigned int)nt_mask
));
1114 /****************************************************************************
1115 Map NT perms to a UNIX mode_t.
1116 ****************************************************************************/
1118 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1119 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1120 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1122 static mode_t
map_nt_perms( uint32
*mask
, int type
)
1128 if((*mask
) & GENERIC_ALL_ACCESS
)
1129 mode
= S_IRUSR
|S_IWUSR
|S_IXUSR
;
1131 mode
|= ((*mask
) & (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IRUSR
: 0;
1132 mode
|= ((*mask
) & (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWUSR
: 0;
1133 mode
|= ((*mask
) & (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXUSR
: 0;
1137 if((*mask
) & GENERIC_ALL_ACCESS
)
1138 mode
= S_IRGRP
|S_IWGRP
|S_IXGRP
;
1140 mode
|= ((*mask
) & (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IRGRP
: 0;
1141 mode
|= ((*mask
) & (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWGRP
: 0;
1142 mode
|= ((*mask
) & (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXGRP
: 0;
1146 if((*mask
) & GENERIC_ALL_ACCESS
)
1147 mode
= S_IROTH
|S_IWOTH
|S_IXOTH
;
1149 mode
|= ((*mask
) & (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IROTH
: 0;
1150 mode
|= ((*mask
) & (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWOTH
: 0;
1151 mode
|= ((*mask
) & (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXOTH
: 0;
1159 /****************************************************************************
1160 Unpack a struct security_descriptor into a UNIX owner and group.
1161 ****************************************************************************/
1163 NTSTATUS
unpack_nt_owners(struct connection_struct
*conn
,
1164 uid_t
*puser
, gid_t
*pgrp
,
1165 uint32 security_info_sent
, const struct
1166 security_descriptor
*psd
)
1168 struct dom_sid owner_sid
;
1169 struct dom_sid grp_sid
;
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 memset(&owner_sid
, '\0', sizeof(owner_sid
));
1184 memset(&grp_sid
, '\0', sizeof(grp_sid
));
1186 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1189 * Don't immediately fail if the owner sid cannot be validated.
1190 * This may be a group chown only set.
1193 if (security_info_sent
& SECINFO_OWNER
) {
1194 sid_copy(&owner_sid
, psd
->owner_sid
);
1195 if (!sid_to_uid(&owner_sid
, puser
)) {
1196 if (lp_force_unknown_acl_user(SNUM(conn
))) {
1197 /* this allows take ownership to work
1199 *puser
= get_current_uid(conn
);
1201 DEBUG(3,("unpack_nt_owners: unable to validate"
1202 " owner sid for %s\n",
1203 sid_string_dbg(&owner_sid
)));
1204 return NT_STATUS_INVALID_OWNER
;
1207 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1208 (unsigned int)*puser
));
1212 * Don't immediately fail if the group sid cannot be validated.
1213 * This may be an owner chown only set.
1216 if (security_info_sent
& SECINFO_GROUP
) {
1217 sid_copy(&grp_sid
, psd
->group_sid
);
1218 if (!sid_to_gid( &grp_sid
, pgrp
)) {
1219 if (lp_force_unknown_acl_user(SNUM(conn
))) {
1220 /* this allows take group ownership to work
1222 *pgrp
= get_current_gid(conn
);
1224 DEBUG(3,("unpack_nt_owners: unable to validate"
1226 return NT_STATUS_INVALID_OWNER
;
1229 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1230 (unsigned int)*pgrp
));
1233 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1235 return NT_STATUS_OK
;
1239 static void trim_ace_perms(canon_ace
*pace
)
1241 pace
->perms
= pace
->perms
& (S_IXUSR
|S_IWUSR
|S_IRUSR
);
1244 static void ensure_minimal_owner_ace_perms(const bool is_directory
,
1247 pace
->perms
|= S_IRUSR
;
1249 pace
->perms
|= (S_IWUSR
|S_IXUSR
);
1253 /****************************************************************************
1254 Check if a given uid/SID is in a group gid/SID. This is probably very
1255 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1256 ****************************************************************************/
1258 static bool uid_entry_in_group(connection_struct
*conn
, canon_ace
*uid_ace
, canon_ace
*group_ace
)
1260 /* "Everyone" always matches every uid. */
1262 if (dom_sid_equal(&group_ace
->trustee
, &global_sid_World
))
1266 * if it's the current user, we already have the unix token
1267 * and don't need to do the complex user_in_group_sid() call
1269 if (uid_ace
->unix_ug
.id
== get_current_uid(conn
)) {
1270 const struct security_unix_token
*curr_utok
= NULL
;
1273 if (group_ace
->unix_ug
.id
== get_current_gid(conn
)) {
1277 curr_utok
= get_current_utok(conn
);
1278 for (i
=0; i
< curr_utok
->ngroups
; i
++) {
1279 if (group_ace
->unix_ug
.id
== curr_utok
->groups
[i
]) {
1286 * user_in_group_sid() uses create_token_from_sid()
1287 * which creates an artificial NT token given just a username,
1288 * so this is not reliable for users from foreign domains
1289 * exported by winbindd!
1291 return user_sid_in_group_sid(&uid_ace
->trustee
, &group_ace
->trustee
);
1294 /****************************************************************************
1295 A well formed POSIX file or default ACL has at least 3 entries, a
1296 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1297 In addition, the owner must always have at least read access.
1298 When using this call on get_acl, the pst struct is valid and contains
1299 the mode of the file.
1300 ****************************************************************************/
1302 static bool ensure_canon_entry_valid_on_get(connection_struct
*conn
,
1304 const struct dom_sid
*pfile_owner_sid
,
1305 const struct dom_sid
*pfile_grp_sid
,
1306 const SMB_STRUCT_STAT
*pst
)
1309 bool got_user
= false;
1310 bool got_group
= false;
1311 bool got_other
= false;
1313 for (pace
= *pp_ace
; pace
; pace
= pace
->next
) {
1314 if (pace
->type
== SMB_ACL_USER_OBJ
) {
1316 } else if (pace
->type
== SMB_ACL_GROUP_OBJ
) {
1318 } else if (pace
->type
== SMB_ACL_OTHER
) {
1324 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1325 DEBUG(0,("malloc fail.\n"));
1330 pace
->type
= SMB_ACL_USER_OBJ
;
1331 pace
->owner_type
= UID_ACE
;
1332 pace
->unix_ug
.type
= ID_TYPE_UID
;
1333 pace
->unix_ug
.id
= pst
->st_ex_uid
;
1334 pace
->trustee
= *pfile_owner_sid
;
1335 pace
->attr
= ALLOW_ACE
;
1336 pace
->perms
= unix_perms_to_acl_perms(pst
->st_ex_mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
1337 DLIST_ADD(*pp_ace
, pace
);
1341 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1342 DEBUG(0,("malloc fail.\n"));
1347 pace
->type
= SMB_ACL_GROUP_OBJ
;
1348 pace
->owner_type
= GID_ACE
;
1349 pace
->unix_ug
.type
= ID_TYPE_GID
;
1350 pace
->unix_ug
.id
= pst
->st_ex_gid
;
1351 pace
->trustee
= *pfile_grp_sid
;
1352 pace
->attr
= ALLOW_ACE
;
1353 pace
->perms
= unix_perms_to_acl_perms(pst
->st_ex_mode
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
1354 DLIST_ADD(*pp_ace
, pace
);
1358 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1359 DEBUG(0,("malloc fail.\n"));
1364 pace
->type
= SMB_ACL_OTHER
;
1365 pace
->owner_type
= WORLD_ACE
;
1366 pace
->unix_ug
.type
= ID_TYPE_NOT_SPECIFIED
;
1367 pace
->unix_ug
.id
= -1;
1368 pace
->trustee
= global_sid_World
;
1369 pace
->attr
= ALLOW_ACE
;
1370 pace
->perms
= unix_perms_to_acl_perms(pst
->st_ex_mode
, S_IROTH
, S_IWOTH
, S_IXOTH
);
1371 DLIST_ADD(*pp_ace
, pace
);
1377 /****************************************************************************
1378 A well formed POSIX file or default ACL has at least 3 entries, a
1379 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1380 In addition, the owner must always have at least read access.
1381 When using this call on set_acl, the pst struct has
1382 been modified to have a mode containing the default for this file or directory
1384 ****************************************************************************/
1386 static bool ensure_canon_entry_valid_on_set(connection_struct
*conn
,
1388 bool is_default_acl
,
1389 const struct share_params
*params
,
1390 const bool is_directory
,
1391 const struct dom_sid
*pfile_owner_sid
,
1392 const struct dom_sid
*pfile_grp_sid
,
1393 const SMB_STRUCT_STAT
*pst
)
1396 canon_ace
*pace_user
= NULL
;
1397 canon_ace
*pace_group
= NULL
;
1398 canon_ace
*pace_other
= NULL
;
1399 bool got_duplicate_user
= false;
1400 bool got_duplicate_group
= false;
1402 for (pace
= *pp_ace
; pace
; pace
= pace
->next
) {
1403 trim_ace_perms(pace
);
1404 if (pace
->type
== SMB_ACL_USER_OBJ
) {
1405 ensure_minimal_owner_ace_perms(is_directory
, pace
);
1407 } else if (pace
->type
== SMB_ACL_GROUP_OBJ
) {
1409 } else if (pace
->type
== SMB_ACL_OTHER
) {
1415 canon_ace
*pace_iter
;
1417 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1418 DEBUG(0,("talloc fail.\n"));
1423 pace
->type
= SMB_ACL_USER_OBJ
;
1424 pace
->owner_type
= UID_ACE
;
1425 pace
->unix_ug
.type
= ID_TYPE_UID
;
1426 pace
->unix_ug
.id
= pst
->st_ex_uid
;
1427 pace
->trustee
= *pfile_owner_sid
;
1428 pace
->attr
= ALLOW_ACE
;
1429 /* Start with existing user permissions, principle of least
1430 surprises for the user. */
1431 pace
->perms
= unix_perms_to_acl_perms(pst
->st_ex_mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
1433 /* See if the owning user is in any of the other groups in
1434 the ACE, or if there's a matching user entry (by uid
1435 or in the case of ID_TYPE_BOTH by SID).
1436 If so, OR in the permissions from that entry. */
1439 for (pace_iter
= *pp_ace
; pace_iter
; pace_iter
= pace_iter
->next
) {
1440 if (pace_iter
->type
== SMB_ACL_USER
&&
1441 pace_iter
->unix_ug
.id
== pace
->unix_ug
.id
) {
1442 pace
->perms
|= pace_iter
->perms
;
1443 } else if (pace_iter
->type
== SMB_ACL_GROUP_OBJ
|| pace_iter
->type
== SMB_ACL_GROUP
) {
1444 if (dom_sid_equal(&pace
->trustee
, &pace_iter
->trustee
)) {
1445 pace
->perms
|= pace_iter
->perms
;
1446 } else if (uid_entry_in_group(conn
, pace
, pace_iter
)) {
1447 pace
->perms
|= pace_iter
->perms
;
1452 if (pace
->perms
== 0) {
1453 /* If we only got an "everyone" perm, just use that. */
1455 pace
->perms
= pace_other
->perms
;
1459 * Ensure we have default parameters for the
1460 * user (owner) even on default ACLs.
1462 ensure_minimal_owner_ace_perms(is_directory
, pace
);
1464 DLIST_ADD(*pp_ace
, pace
);
1469 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1470 DEBUG(0,("talloc fail.\n"));
1475 pace
->type
= SMB_ACL_GROUP_OBJ
;
1476 pace
->owner_type
= GID_ACE
;
1477 pace
->unix_ug
.type
= ID_TYPE_GID
;
1478 pace
->unix_ug
.id
= pst
->st_ex_gid
;
1479 pace
->trustee
= *pfile_grp_sid
;
1480 pace
->attr
= ALLOW_ACE
;
1482 /* If we only got an "everyone" perm, just use that. */
1484 pace
->perms
= pace_other
->perms
;
1489 DLIST_ADD(*pp_ace
, pace
);
1494 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1495 DEBUG(0,("talloc fail.\n"));
1500 pace
->type
= SMB_ACL_OTHER
;
1501 pace
->owner_type
= WORLD_ACE
;
1502 pace
->unix_ug
.type
= ID_TYPE_NOT_SPECIFIED
;
1503 pace
->unix_ug
.id
= -1;
1504 pace
->trustee
= global_sid_World
;
1505 pace
->attr
= ALLOW_ACE
;
1508 DLIST_ADD(*pp_ace
, pace
);
1512 /* Ensure when setting a POSIX ACL, that the uid for a
1513 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1514 permission entry as an SMB_ACL_USER, and a gid for a
1515 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1516 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1517 then if the ownership or group ownership of this file or
1518 directory gets changed, the user or group can lose their
1521 for (pace
= *pp_ace
; pace
; pace
= pace
->next
) {
1522 if (pace
->type
== SMB_ACL_USER
&&
1523 pace
->unix_ug
.id
== pace_user
->unix_ug
.id
) {
1524 /* Already got one. */
1525 got_duplicate_user
= true;
1526 } else if (pace
->type
== SMB_ACL_GROUP
&&
1527 pace
->unix_ug
.id
== pace_group
->unix_ug
.id
) {
1528 /* Already got one. */
1529 got_duplicate_group
= true;
1530 } else if ((pace
->type
== SMB_ACL_GROUP
)
1531 && (dom_sid_equal(&pace
->trustee
, &pace_user
->trustee
))) {
1532 /* If the SID owning the file appears
1533 * in a group entry, then we have
1534 * enough duplication, they will still
1536 got_duplicate_user
= true;
1540 /* If the SID is equal for the user and group that we need
1541 to add the duplicate for, add only the group */
1542 if (!got_duplicate_user
&& !got_duplicate_group
1543 && dom_sid_equal(&pace_group
->trustee
,
1544 &pace_user
->trustee
)) {
1545 /* Add a duplicate SMB_ACL_GROUP entry, this
1546 * will cover the owning SID as well, as it
1547 * will always be mapped to both a uid and
1550 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1551 DEBUG(0,("talloc fail.\n"));
1556 pace
->type
= SMB_ACL_GROUP
;;
1557 pace
->owner_type
= GID_ACE
;
1558 pace
->unix_ug
.type
= ID_TYPE_GID
;
1559 pace
->unix_ug
.id
= pace_group
->unix_ug
.id
;
1560 pace
->trustee
= pace_group
->trustee
;
1561 pace
->attr
= pace_group
->attr
;
1562 pace
->perms
= pace_group
->perms
;
1564 DLIST_ADD(*pp_ace
, pace
);
1566 /* We're done here, make sure the
1567 statements below are not executed. */
1568 got_duplicate_user
= true;
1569 got_duplicate_group
= true;
1572 if (!got_duplicate_user
) {
1573 /* Add a duplicate SMB_ACL_USER entry. */
1574 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1575 DEBUG(0,("talloc fail.\n"));
1580 pace
->type
= SMB_ACL_USER
;;
1581 pace
->owner_type
= UID_ACE
;
1582 pace
->unix_ug
.type
= ID_TYPE_UID
;
1583 pace
->unix_ug
.id
= pace_user
->unix_ug
.id
;
1584 pace
->trustee
= pace_user
->trustee
;
1585 pace
->attr
= pace_user
->attr
;
1586 pace
->perms
= pace_user
->perms
;
1588 DLIST_ADD(*pp_ace
, pace
);
1590 got_duplicate_user
= true;
1593 if (!got_duplicate_group
) {
1594 /* Add a duplicate SMB_ACL_GROUP entry. */
1595 if ((pace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1596 DEBUG(0,("talloc fail.\n"));
1601 pace
->type
= SMB_ACL_GROUP
;;
1602 pace
->owner_type
= GID_ACE
;
1603 pace
->unix_ug
.type
= ID_TYPE_GID
;
1604 pace
->unix_ug
.id
= pace_group
->unix_ug
.id
;
1605 pace
->trustee
= pace_group
->trustee
;
1606 pace
->attr
= pace_group
->attr
;
1607 pace
->perms
= pace_group
->perms
;
1609 DLIST_ADD(*pp_ace
, pace
);
1611 got_duplicate_group
= true;
1617 /****************************************************************************
1618 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1619 If it does not have them, check if there are any entries where the trustee is the
1620 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1621 Note we must not do this to default directory ACLs.
1622 ****************************************************************************/
1624 static void check_owning_objs(canon_ace
*ace
, struct dom_sid
*pfile_owner_sid
, struct dom_sid
*pfile_grp_sid
)
1626 bool got_user_obj
, got_group_obj
;
1627 canon_ace
*current_ace
;
1630 entries
= count_canon_ace_list(ace
);
1631 got_user_obj
= False
;
1632 got_group_obj
= False
;
1634 for (i
=0, current_ace
= ace
; i
< entries
; i
++, current_ace
= current_ace
->next
) {
1635 if (current_ace
->type
== SMB_ACL_USER_OBJ
)
1636 got_user_obj
= True
;
1637 else if (current_ace
->type
== SMB_ACL_GROUP_OBJ
)
1638 got_group_obj
= True
;
1640 if (got_user_obj
&& got_group_obj
) {
1641 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1645 for (i
=0, current_ace
= ace
; i
< entries
; i
++, current_ace
= current_ace
->next
) {
1646 if (!got_user_obj
&& current_ace
->owner_type
== UID_ACE
&&
1647 dom_sid_equal(¤t_ace
->trustee
, pfile_owner_sid
)) {
1648 current_ace
->type
= SMB_ACL_USER_OBJ
;
1649 got_user_obj
= True
;
1651 if (!got_group_obj
&& current_ace
->owner_type
== GID_ACE
&&
1652 dom_sid_equal(¤t_ace
->trustee
, pfile_grp_sid
)) {
1653 current_ace
->type
= SMB_ACL_GROUP_OBJ
;
1654 got_group_obj
= True
;
1658 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1660 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1663 static bool add_current_ace_to_acl(files_struct
*fsp
, struct security_ace
*psa
,
1664 canon_ace
**file_ace
, canon_ace
**dir_ace
,
1665 bool *got_file_allow
, bool *got_dir_allow
,
1666 bool *all_aces_are_inherit_only
,
1667 canon_ace
*current_ace
)
1671 * Map the given NT permissions into a UNIX mode_t containing only
1672 * S_I(R|W|X)USR bits.
1675 current_ace
->perms
|= map_nt_perms( &psa
->access_mask
, S_IRUSR
);
1676 current_ace
->attr
= (psa
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) ? ALLOW_ACE
: DENY_ACE
;
1678 /* Store the ace_flag. */
1679 current_ace
->ace_flags
= psa
->flags
;
1682 * Now add the created ace to either the file list, the directory
1683 * list, or both. We *MUST* preserve the order here (hence we use
1684 * DLIST_ADD_END) as NT ACLs are order dependent.
1687 if (fsp
->is_directory
) {
1690 * We can only add to the default POSIX ACE list if the ACE is
1691 * designed to be inherited by both files and directories.
1694 if ((psa
->flags
& (SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
)) ==
1695 (SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
1697 canon_ace
*current_dir_ace
= current_ace
;
1698 DLIST_ADD_END(*dir_ace
, current_ace
, canon_ace
*);
1701 * Note if this was an allow ace. We can't process
1702 * any further deny ace's after this.
1705 if (current_ace
->attr
== ALLOW_ACE
)
1706 *got_dir_allow
= True
;
1708 if ((current_ace
->attr
== DENY_ACE
) && *got_dir_allow
) {
1709 DEBUG(0,("add_current_ace_to_acl: "
1711 "inheritable ACL! Deny entry "
1712 "after Allow entry. Failing "
1713 "to set on file %s.\n",
1718 if( DEBUGLVL( 10 )) {
1719 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1720 print_canon_ace( current_ace
, 0);
1724 * If this is not an inherit only ACE we need to add a duplicate
1728 if (!(psa
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
)) {
1729 canon_ace
*dup_ace
= dup_canon_ace(current_ace
);
1732 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1737 * We must not free current_ace here as its
1738 * pointer is now owned by the dir_ace list.
1740 current_ace
= dup_ace
;
1741 /* We've essentially split this ace into two,
1742 * and added the ace with inheritance request
1743 * bits to the directory ACL. Drop those bits for
1744 * the ACE we're adding to the file list. */
1745 current_ace
->ace_flags
&= ~(SEC_ACE_FLAG_OBJECT_INHERIT
|
1746 SEC_ACE_FLAG_CONTAINER_INHERIT
|
1747 SEC_ACE_FLAG_INHERIT_ONLY
);
1750 * We must not free current_ace here as its
1751 * pointer is now owned by the dir_ace list.
1757 * current_ace is now either owned by file_ace
1758 * or is NULL. We can safely operate on current_dir_ace
1759 * to treat mapping for default acl entries differently
1760 * than access acl entries.
1763 if (current_dir_ace
->owner_type
== UID_ACE
) {
1765 * We already decided above this is a uid,
1766 * for default acls ace's only CREATOR_OWNER
1767 * maps to ACL_USER_OBJ. All other uid
1768 * ace's are ACL_USER.
1770 if (dom_sid_equal(¤t_dir_ace
->trustee
,
1771 &global_sid_Creator_Owner
)) {
1772 current_dir_ace
->type
= SMB_ACL_USER_OBJ
;
1774 current_dir_ace
->type
= SMB_ACL_USER
;
1778 if (current_dir_ace
->owner_type
== GID_ACE
) {
1780 * We already decided above this is a gid,
1781 * for default acls ace's only CREATOR_GROUP
1782 * maps to ACL_GROUP_OBJ. All other uid
1783 * ace's are ACL_GROUP.
1785 if (dom_sid_equal(¤t_dir_ace
->trustee
,
1786 &global_sid_Creator_Group
)) {
1787 current_dir_ace
->type
= SMB_ACL_GROUP_OBJ
;
1789 current_dir_ace
->type
= SMB_ACL_GROUP
;
1796 * Only add to the file ACL if not inherit only.
1799 if (current_ace
&& !(psa
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
)) {
1800 DLIST_ADD_END(*file_ace
, current_ace
, canon_ace
*);
1803 * Note if this was an allow ace. We can't process
1804 * any further deny ace's after this.
1807 if (current_ace
->attr
== ALLOW_ACE
)
1808 *got_file_allow
= True
;
1810 if ((current_ace
->attr
== DENY_ACE
) && *got_file_allow
) {
1811 DEBUG(0,("add_current_ace_to_acl: malformed "
1812 "ACL in file ACL ! Deny entry after "
1813 "Allow entry. Failing to set on file "
1814 "%s.\n", fsp_str_dbg(fsp
)));
1818 if( DEBUGLVL( 10 )) {
1819 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1820 print_canon_ace( current_ace
, 0);
1822 *all_aces_are_inherit_only
= False
;
1824 * We must not free current_ace here as its
1825 * pointer is now owned by the file_ace list.
1831 * Free if ACE was not added.
1834 TALLOC_FREE(current_ace
);
1838 /****************************************************************************
1839 Unpack a struct security_descriptor into two canonical ace lists.
1840 ****************************************************************************/
1842 static bool create_canon_ace_lists(files_struct
*fsp
,
1843 const SMB_STRUCT_STAT
*pst
,
1844 struct dom_sid
*pfile_owner_sid
,
1845 struct dom_sid
*pfile_grp_sid
,
1846 canon_ace
**ppfile_ace
,
1847 canon_ace
**ppdir_ace
,
1848 const struct security_acl
*dacl
)
1850 bool all_aces_are_inherit_only
= (fsp
->is_directory
? True
: False
);
1851 canon_ace
*file_ace
= NULL
;
1852 canon_ace
*dir_ace
= NULL
;
1853 canon_ace
*current_ace
= NULL
;
1854 bool got_dir_allow
= False
;
1855 bool got_file_allow
= False
;
1862 * Convert the incoming ACL into a more regular form.
1865 for(i
= 0; i
< dacl
->num_aces
; i
++) {
1866 struct security_ace
*psa
= &dacl
->aces
[i
];
1868 if((psa
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED
) && (psa
->type
!= SEC_ACE_TYPE_ACCESS_DENIED
)) {
1869 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1875 * Deal with the fact that NT 4.x re-writes the canonical format
1876 * that we return for default ACLs. If a directory ACE is identical
1877 * to a inherited directory ACE then NT changes the bits so that the
1878 * first ACE is set to OI|IO and the second ACE for this SID is set
1879 * to CI. We need to repair this. JRA.
1882 for(i
= 0; i
< dacl
->num_aces
; i
++) {
1883 struct security_ace
*psa1
= &dacl
->aces
[i
];
1885 for (j
= i
+ 1; j
< dacl
->num_aces
; j
++) {
1886 struct security_ace
*psa2
= &dacl
->aces
[j
];
1888 if (psa1
->access_mask
!= psa2
->access_mask
)
1891 if (!dom_sid_equal(&psa1
->trustee
, &psa2
->trustee
))
1895 * Ok - permission bits and SIDs are equal.
1896 * Check if flags were re-written.
1899 if (psa1
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
1901 psa1
->flags
|= (psa2
->flags
& (SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
));
1902 psa2
->flags
&= ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
);
1904 } else if (psa2
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
1906 psa2
->flags
|= (psa1
->flags
& (SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
));
1907 psa1
->flags
&= ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
);
1913 for(i
= 0; i
< dacl
->num_aces
; i
++) {
1914 struct security_ace
*psa
= &dacl
->aces
[i
];
1917 * Create a canon_ace entry representing this NT DACL ACE.
1920 if ((current_ace
= talloc(talloc_tos(), canon_ace
)) == NULL
) {
1921 free_canon_ace_list(file_ace
);
1922 free_canon_ace_list(dir_ace
);
1923 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1927 ZERO_STRUCTP(current_ace
);
1929 sid_copy(¤t_ace
->trustee
, &psa
->trustee
);
1932 * Try and work out if the SID is a user or group
1933 * as we need to flag these differently for POSIX.
1934 * Note what kind of a POSIX ACL this should map to.
1937 if( dom_sid_equal(¤t_ace
->trustee
, &global_sid_World
)) {
1938 current_ace
->owner_type
= WORLD_ACE
;
1939 current_ace
->unix_ug
.type
= ID_TYPE_NOT_SPECIFIED
;
1940 current_ace
->unix_ug
.id
= -1;
1941 current_ace
->type
= SMB_ACL_OTHER
;
1942 } else if (dom_sid_equal(¤t_ace
->trustee
, &global_sid_Creator_Owner
)) {
1943 current_ace
->owner_type
= UID_ACE
;
1944 current_ace
->unix_ug
.type
= ID_TYPE_UID
;
1945 current_ace
->unix_ug
.id
= pst
->st_ex_uid
;
1946 current_ace
->type
= SMB_ACL_USER_OBJ
;
1949 * The Creator Owner entry only specifies inheritable permissions,
1950 * never access permissions. WinNT doesn't always set the ACE to
1951 * INHERIT_ONLY, though.
1954 psa
->flags
|= SEC_ACE_FLAG_INHERIT_ONLY
;
1956 } else if (dom_sid_equal(¤t_ace
->trustee
, &global_sid_Creator_Group
)) {
1957 current_ace
->owner_type
= GID_ACE
;
1958 current_ace
->unix_ug
.type
= ID_TYPE_GID
;
1959 current_ace
->unix_ug
.id
= pst
->st_ex_gid
;
1960 current_ace
->type
= SMB_ACL_GROUP_OBJ
;
1963 * The Creator Group entry only specifies inheritable permissions,
1964 * never access permissions. WinNT doesn't always set the ACE to
1965 * INHERIT_ONLY, though.
1967 psa
->flags
|= SEC_ACE_FLAG_INHERIT_ONLY
;
1970 struct unixid unixid
;
1972 if (!sids_to_unixids(¤t_ace
->trustee
, 1, &unixid
)) {
1973 free_canon_ace_list(file_ace
);
1974 free_canon_ace_list(dir_ace
);
1975 TALLOC_FREE(current_ace
);
1976 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
1977 "failed for %s (allocation failure)\n",
1978 sid_string_dbg(¤t_ace
->trustee
)));
1982 if (unixid
.type
== ID_TYPE_BOTH
) {
1984 * We must add both a user and group
1986 * This is due to the fact that in POSIX
1987 * user entries are more specific than
1990 current_ace
->owner_type
= UID_ACE
;
1991 current_ace
->unix_ug
.type
= ID_TYPE_UID
;
1992 current_ace
->unix_ug
.id
= unixid
.id
;
1994 (unixid
.id
== pst
->st_ex_uid
) ?
1998 /* Add the user object to the posix ACL,
1999 and proceed to the group mapping
2000 below. This handles the talloc_free
2001 of current_ace if not added for some
2003 if (!add_current_ace_to_acl(fsp
,
2009 &all_aces_are_inherit_only
,
2011 free_canon_ace_list(file_ace
);
2012 free_canon_ace_list(dir_ace
);
2016 if ((current_ace
= talloc(talloc_tos(),
2017 canon_ace
)) == NULL
) {
2018 free_canon_ace_list(file_ace
);
2019 free_canon_ace_list(dir_ace
);
2020 DEBUG(0,("create_canon_ace_lists: "
2025 ZERO_STRUCTP(current_ace
);
2027 sid_copy(¤t_ace
->trustee
, &psa
->trustee
);
2029 current_ace
->unix_ug
.type
= ID_TYPE_GID
;
2030 current_ace
->unix_ug
.id
= unixid
.id
;
2031 current_ace
->owner_type
= GID_ACE
;
2032 /* If it's the primary group, this is a
2033 group_obj, not a group. */
2034 if (current_ace
->unix_ug
.id
== pst
->st_ex_gid
) {
2035 current_ace
->type
= SMB_ACL_GROUP_OBJ
;
2037 current_ace
->type
= SMB_ACL_GROUP
;
2040 } else if (unixid
.type
== ID_TYPE_UID
) {
2041 current_ace
->owner_type
= UID_ACE
;
2042 current_ace
->unix_ug
.type
= ID_TYPE_UID
;
2043 current_ace
->unix_ug
.id
= unixid
.id
;
2044 /* If it's the owning user, this is a user_obj,
2046 if (current_ace
->unix_ug
.id
== pst
->st_ex_uid
) {
2047 current_ace
->type
= SMB_ACL_USER_OBJ
;
2049 current_ace
->type
= SMB_ACL_USER
;
2051 } else if (unixid
.type
== ID_TYPE_GID
) {
2052 current_ace
->unix_ug
.type
= ID_TYPE_GID
;
2053 current_ace
->unix_ug
.id
= unixid
.id
;
2054 current_ace
->owner_type
= GID_ACE
;
2055 /* If it's the primary group, this is a
2056 group_obj, not a group. */
2057 if (current_ace
->unix_ug
.id
== pst
->st_ex_gid
) {
2058 current_ace
->type
= SMB_ACL_GROUP_OBJ
;
2060 current_ace
->type
= SMB_ACL_GROUP
;
2064 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2067 if (non_mappable_sid(&psa
->trustee
)) {
2068 DEBUG(10, ("create_canon_ace_lists: ignoring "
2069 "non-mappable SID %s\n",
2070 sid_string_dbg(&psa
->trustee
)));
2071 TALLOC_FREE(current_ace
);
2075 if (lp_force_unknown_acl_user(SNUM(fsp
->conn
))) {
2076 DEBUG(10, ("create_canon_ace_lists: ignoring "
2077 "unknown or foreign SID %s\n",
2078 sid_string_dbg(&psa
->trustee
)));
2079 TALLOC_FREE(current_ace
);
2083 free_canon_ace_list(file_ace
);
2084 free_canon_ace_list(dir_ace
);
2085 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2086 "%s to uid or gid.\n",
2087 sid_string_dbg(¤t_ace
->trustee
)));
2088 TALLOC_FREE(current_ace
);
2093 /* handles the talloc_free of current_ace if not added for some reason */
2094 if (!add_current_ace_to_acl(fsp
, psa
, &file_ace
, &dir_ace
,
2095 &got_file_allow
, &got_dir_allow
,
2096 &all_aces_are_inherit_only
, current_ace
)) {
2097 free_canon_ace_list(file_ace
);
2098 free_canon_ace_list(dir_ace
);
2103 if (fsp
->is_directory
&& all_aces_are_inherit_only
) {
2105 * Windows 2000 is doing one of these weird 'inherit acl'
2106 * traverses to conserve NTFS ACL resources. Just pretend
2107 * there was no DACL sent. JRA.
2110 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2111 free_canon_ace_list(file_ace
);
2112 free_canon_ace_list(dir_ace
);
2117 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2118 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2119 * entries can be converted to *_OBJ. Don't do this for the default
2120 * ACL, we will create them separately for this if needed inside
2121 * ensure_canon_entry_valid_on_set().
2124 check_owning_objs(file_ace
, pfile_owner_sid
, pfile_grp_sid
);
2128 *ppfile_ace
= file_ace
;
2129 *ppdir_ace
= dir_ace
;
2134 /****************************************************************************
2135 ASCII art time again... JRA :-).
2137 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2138 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2139 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2140 allow or deny have been merged, so the same SID can only appear once in the deny
2141 list or once in the allow list.
2143 We then process as follows :
2145 ---------------------------------------------------------------------------
2146 First pass - look for a Everyone DENY entry.
2148 If it is deny all (rwx) trunate the list at this point.
2149 Else, walk the list from this point and use the deny permissions of this
2150 entry as a mask on all following allow entries. Finally, delete
2151 the Everyone DENY entry (we have applied it to everything possible).
2153 In addition, in this pass we remove any DENY entries that have
2154 no permissions (ie. they are a DENY nothing).
2155 ---------------------------------------------------------------------------
2156 Second pass - only deal with deny user entries.
2158 DENY user1 (perms XXX)
2161 for all following allow group entries where user1 is in group
2162 new_perms |= group_perms;
2164 user1 entry perms = new_perms & ~ XXX;
2166 Convert the deny entry to an allow entry with the new perms and
2167 push to the end of the list. Note if the user was in no groups
2168 this maps to a specific allow nothing entry for this user.
2170 The common case from the NT ACL choser (userX deny all) is
2171 optimised so we don't do the group lookup - we just map to
2172 an allow nothing entry.
2174 What we're doing here is inferring the allow permissions the
2175 person setting the ACE on user1 wanted by looking at the allow
2176 permissions on the groups the user is currently in. This will
2177 be a snapshot, depending on group membership but is the best
2178 we can do and has the advantage of failing closed rather than
2180 ---------------------------------------------------------------------------
2181 Third pass - only deal with deny group entries.
2183 DENY group1 (perms XXX)
2185 for all following allow user entries where user is in group1
2186 user entry perms = user entry perms & ~ XXX;
2188 If there is a group Everyone allow entry with permissions YYY,
2189 convert the group1 entry to an allow entry and modify its
2192 new_perms = YYY & ~ XXX
2194 and push to the end of the list.
2196 If there is no group Everyone allow entry then convert the
2197 group1 entry to a allow nothing entry and push to the end of the list.
2199 Note that the common case from the NT ACL choser (groupX deny all)
2200 cannot be optimised here as we need to modify user entries who are
2201 in the group to change them to a deny all also.
2203 What we're doing here is modifying the allow permissions of
2204 user entries (which are more specific in POSIX ACLs) to mask
2205 out the explicit deny set on the group they are in. This will
2206 be a snapshot depending on current group membership but is the
2207 best we can do and has the advantage of failing closed rather
2209 ---------------------------------------------------------------------------
2210 Fourth pass - cope with cumulative permissions.
2212 for all allow user entries, if there exists an allow group entry with
2213 more permissive permissions, and the user is in that group, rewrite the
2214 allow user permissions to contain both sets of permissions.
2216 Currently the code for this is #ifdef'ed out as these semantics make
2217 no sense to me. JRA.
2218 ---------------------------------------------------------------------------
2220 Note we *MUST* do the deny user pass first as this will convert deny user
2221 entries into allow user entries which can then be processed by the deny
2224 The above algorithm took a *lot* of thinking about - hence this
2225 explaination :-). JRA.
2226 ****************************************************************************/
2228 /****************************************************************************
2229 Process a canon_ace list entries. This is very complex code. We need
2230 to go through and remove the "deny" permissions from any allow entry that matches
2231 the id of this entry. We have already refused any NT ACL that wasn't in correct
2232 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2233 we just remove it (to fail safe). We have already removed any duplicate ace
2234 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2236 ****************************************************************************/
2238 static void process_deny_list(connection_struct
*conn
, canon_ace
**pp_ace_list
)
2240 canon_ace
*ace_list
= *pp_ace_list
;
2241 canon_ace
*curr_ace
= NULL
;
2242 canon_ace
*curr_ace_next
= NULL
;
2244 /* Pass 1 above - look for an Everyone, deny entry. */
2246 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
2247 canon_ace
*allow_ace_p
;
2249 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
2251 if (curr_ace
->attr
!= DENY_ACE
)
2254 if (curr_ace
->perms
== (mode_t
)0) {
2256 /* Deny nothing entry - delete. */
2258 DLIST_REMOVE(ace_list
, curr_ace
);
2262 if (!dom_sid_equal(&curr_ace
->trustee
, &global_sid_World
))
2265 /* JRATEST - assert. */
2266 SMB_ASSERT(curr_ace
->owner_type
== WORLD_ACE
);
2268 if (curr_ace
->perms
== ALL_ACE_PERMS
) {
2271 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2272 * list at this point including this entry.
2275 canon_ace
*prev_entry
= DLIST_PREV(curr_ace
);
2277 free_canon_ace_list( curr_ace
);
2279 DLIST_REMOVE(ace_list
, prev_entry
);
2281 /* We deleted the entire list. */
2287 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
2290 * Only mask off allow entries.
2293 if (allow_ace_p
->attr
!= ALLOW_ACE
)
2296 allow_ace_p
->perms
&= ~curr_ace
->perms
;
2300 * Now it's been applied, remove it.
2303 DLIST_REMOVE(ace_list
, curr_ace
);
2306 /* Pass 2 above - deal with deny user entries. */
2308 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
2309 mode_t new_perms
= (mode_t
)0;
2310 canon_ace
*allow_ace_p
;
2312 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
2314 if (curr_ace
->attr
!= DENY_ACE
)
2317 if (curr_ace
->owner_type
!= UID_ACE
)
2320 if (curr_ace
->perms
== ALL_ACE_PERMS
) {
2323 * Optimisation - this is a deny everything to this user.
2324 * Convert to an allow nothing and push to the end of the list.
2327 curr_ace
->attr
= ALLOW_ACE
;
2328 curr_ace
->perms
= (mode_t
)0;
2329 DLIST_DEMOTE(ace_list
, curr_ace
, canon_ace
*);
2333 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
2335 if (allow_ace_p
->attr
!= ALLOW_ACE
)
2338 /* We process GID_ACE and WORLD_ACE entries only. */
2340 if (allow_ace_p
->owner_type
== UID_ACE
)
2343 if (uid_entry_in_group(conn
, curr_ace
, allow_ace_p
))
2344 new_perms
|= allow_ace_p
->perms
;
2348 * Convert to a allow entry, modify the perms and push to the end
2352 curr_ace
->attr
= ALLOW_ACE
;
2353 curr_ace
->perms
= (new_perms
& ~curr_ace
->perms
);
2354 DLIST_DEMOTE(ace_list
, curr_ace
, canon_ace
*);
2357 /* Pass 3 above - deal with deny group entries. */
2359 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
2360 canon_ace
*allow_ace_p
;
2361 canon_ace
*allow_everyone_p
= NULL
;
2363 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
2365 if (curr_ace
->attr
!= DENY_ACE
)
2368 if (curr_ace
->owner_type
!= GID_ACE
)
2371 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
2373 if (allow_ace_p
->attr
!= ALLOW_ACE
)
2376 /* Store a pointer to the Everyone allow, if it exists. */
2377 if (allow_ace_p
->owner_type
== WORLD_ACE
)
2378 allow_everyone_p
= allow_ace_p
;
2380 /* We process UID_ACE entries only. */
2382 if (allow_ace_p
->owner_type
!= UID_ACE
)
2385 /* Mask off the deny group perms. */
2387 if (uid_entry_in_group(conn
, allow_ace_p
, curr_ace
))
2388 allow_ace_p
->perms
&= ~curr_ace
->perms
;
2392 * Convert the deny to an allow with the correct perms and
2393 * push to the end of the list.
2396 curr_ace
->attr
= ALLOW_ACE
;
2397 if (allow_everyone_p
)
2398 curr_ace
->perms
= allow_everyone_p
->perms
& ~curr_ace
->perms
;
2400 curr_ace
->perms
= (mode_t
)0;
2401 DLIST_DEMOTE(ace_list
, curr_ace
, canon_ace
*);
2404 /* Doing this fourth pass allows Windows semantics to be layered
2405 * on top of POSIX semantics. I'm not sure if this is desirable.
2406 * For example, in W2K ACLs there is no way to say, "Group X no
2407 * access, user Y full access" if user Y is a member of group X.
2408 * This seems completely broken semantics to me.... JRA.
2412 /* Pass 4 above - deal with allow entries. */
2414 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
2415 canon_ace
*allow_ace_p
;
2417 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
2419 if (curr_ace
->attr
!= ALLOW_ACE
)
2422 if (curr_ace
->owner_type
!= UID_ACE
)
2425 for (allow_ace_p
= ace_list
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
2427 if (allow_ace_p
->attr
!= ALLOW_ACE
)
2430 /* We process GID_ACE entries only. */
2432 if (allow_ace_p
->owner_type
!= GID_ACE
)
2435 /* OR in the group perms. */
2437 if (uid_entry_in_group(conn
, curr_ace
, allow_ace_p
))
2438 curr_ace
->perms
|= allow_ace_p
->perms
;
2443 *pp_ace_list
= ace_list
;
2446 /****************************************************************************
2447 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2449 ****************************************************************************/
2451 static bool unpack_canon_ace(files_struct
*fsp
,
2452 const SMB_STRUCT_STAT
*pst
,
2453 struct dom_sid
*pfile_owner_sid
,
2454 struct dom_sid
*pfile_grp_sid
,
2455 canon_ace
**ppfile_ace
,
2456 canon_ace
**ppdir_ace
,
2457 uint32 security_info_sent
,
2458 const struct security_descriptor
*psd
)
2460 canon_ace
*file_ace
= NULL
;
2461 canon_ace
*dir_ace
= NULL
;
2466 if(security_info_sent
== 0) {
2467 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2472 * If no DACL then this is a chown only security descriptor.
2475 if(!(security_info_sent
& SECINFO_DACL
) || !psd
->dacl
)
2479 * Now go through the DACL and create the canon_ace lists.
2482 if (!create_canon_ace_lists( fsp
, pst
, pfile_owner_sid
, pfile_grp_sid
,
2483 &file_ace
, &dir_ace
, psd
->dacl
))
2486 if ((file_ace
== NULL
) && (dir_ace
== NULL
)) {
2487 /* W2K traverse DACL set - ignore. */
2492 * Go through the canon_ace list and merge entries
2493 * belonging to identical users of identical allow or deny type.
2494 * We can do this as all deny entries come first, followed by
2495 * all allow entries (we have mandated this before accepting this acl).
2498 print_canon_ace_list( "file ace - before merge", file_ace
);
2499 merge_aces( &file_ace
, false);
2501 print_canon_ace_list( "dir ace - before merge", dir_ace
);
2502 merge_aces( &dir_ace
, true);
2505 * NT ACLs are order dependent. Go through the acl lists and
2506 * process DENY entries by masking the allow entries.
2509 print_canon_ace_list( "file ace - before deny", file_ace
);
2510 process_deny_list(fsp
->conn
, &file_ace
);
2512 print_canon_ace_list( "dir ace - before deny", dir_ace
);
2513 process_deny_list(fsp
->conn
, &dir_ace
);
2516 * A well formed POSIX file or default ACL has at least 3 entries, a
2517 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2518 * and optionally a mask entry. Ensure this is the case.
2521 print_canon_ace_list( "file ace - before valid", file_ace
);
2523 if (!ensure_canon_entry_valid_on_set(fsp
->conn
, &file_ace
, false, fsp
->conn
->params
,
2524 fsp
->is_directory
, pfile_owner_sid
, pfile_grp_sid
, pst
)) {
2525 free_canon_ace_list(file_ace
);
2526 free_canon_ace_list(dir_ace
);
2530 print_canon_ace_list( "dir ace - before valid", dir_ace
);
2532 if (dir_ace
&& !ensure_canon_entry_valid_on_set(fsp
->conn
, &dir_ace
, true, fsp
->conn
->params
,
2533 fsp
->is_directory
, pfile_owner_sid
, pfile_grp_sid
, pst
)) {
2534 free_canon_ace_list(file_ace
);
2535 free_canon_ace_list(dir_ace
);
2539 print_canon_ace_list( "file ace - return", file_ace
);
2540 print_canon_ace_list( "dir ace - return", dir_ace
);
2542 *ppfile_ace
= file_ace
;
2543 *ppdir_ace
= dir_ace
;
2548 /******************************************************************************
2549 When returning permissions, try and fit NT display
2550 semantics if possible. Note the the canon_entries here must have been malloced.
2551 The list format should be - first entry = owner, followed by group and other user
2552 entries, last entry = other.
2554 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2555 are not ordered, and match on the most specific entry rather than walking a list,
2556 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2558 Entry 0: owner : deny all except read and write.
2559 Entry 1: owner : allow read and write.
2560 Entry 2: group : deny all except read.
2561 Entry 3: group : allow read.
2562 Entry 4: Everyone : allow read.
2564 But NT cannot display this in their ACL editor !
2565 ********************************************************************************/
2567 static void arrange_posix_perms(const char *filename
, canon_ace
**pp_list_head
)
2569 canon_ace
*l_head
= *pp_list_head
;
2570 canon_ace
*owner_ace
= NULL
;
2571 canon_ace
*other_ace
= NULL
;
2572 canon_ace
*ace
= NULL
;
2574 for (ace
= l_head
; ace
; ace
= ace
->next
) {
2575 if (ace
->type
== SMB_ACL_USER_OBJ
)
2577 else if (ace
->type
== SMB_ACL_OTHER
) {
2578 /* Last ace - this is "other" */
2583 if (!owner_ace
|| !other_ace
) {
2584 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2590 * The POSIX algorithm applies to owner first, and other last,
2591 * so ensure they are arranged in this order.
2595 DLIST_PROMOTE(l_head
, owner_ace
);
2599 DLIST_DEMOTE(l_head
, other_ace
, canon_ace
*);
2602 /* We have probably changed the head of the list. */
2604 *pp_list_head
= l_head
;
2607 /****************************************************************************
2608 Create a linked list of canonical ACE entries.
2609 ****************************************************************************/
2611 static canon_ace
*canonicalise_acl(struct connection_struct
*conn
,
2612 const char *fname
, SMB_ACL_T posix_acl
,
2613 const SMB_STRUCT_STAT
*psbuf
,
2614 const struct dom_sid
*powner
, const struct dom_sid
*pgroup
, struct pai_val
*pal
, SMB_ACL_TYPE_T the_acl_type
)
2616 mode_t acl_mask
= (S_IRUSR
|S_IWUSR
|S_IXUSR
);
2617 canon_ace
*l_head
= NULL
;
2618 canon_ace
*ace
= NULL
;
2619 canon_ace
*next_ace
= NULL
;
2620 int entry_id
= SMB_ACL_FIRST_ENTRY
;
2621 bool is_default_acl
= (the_acl_type
== SMB_ACL_TYPE_DEFAULT
);
2622 SMB_ACL_ENTRY_T entry
;
2625 while ( posix_acl
&& (sys_acl_get_entry(posix_acl
, entry_id
, &entry
) == 1)) {
2626 SMB_ACL_TAG_T tagtype
;
2627 SMB_ACL_PERMSET_T permset
;
2629 struct unixid unix_ug
;
2630 enum ace_owner owner_type
;
2632 entry_id
= SMB_ACL_NEXT_ENTRY
;
2634 /* Is this a MASK entry ? */
2635 if (sys_acl_get_tag_type(entry
, &tagtype
) == -1)
2638 if (sys_acl_get_permset(entry
, &permset
) == -1)
2641 /* Decide which SID to use based on the ACL type. */
2643 case SMB_ACL_USER_OBJ
:
2644 /* Get the SID from the owner. */
2645 sid_copy(&sid
, powner
);
2646 unix_ug
.type
= ID_TYPE_UID
;
2647 unix_ug
.id
= psbuf
->st_ex_uid
;
2648 owner_type
= UID_ACE
;
2652 uid_t
*puid
= (uid_t
*)sys_acl_get_qualifier(entry
);
2654 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2657 uid_to_sid( &sid
, *puid
);
2658 unix_ug
.type
= ID_TYPE_UID
;
2660 owner_type
= UID_ACE
;
2663 case SMB_ACL_GROUP_OBJ
:
2664 /* Get the SID from the owning group. */
2665 sid_copy(&sid
, pgroup
);
2666 unix_ug
.type
= ID_TYPE_GID
;
2667 unix_ug
.id
= psbuf
->st_ex_gid
;
2668 owner_type
= GID_ACE
;
2672 gid_t
*pgid
= (gid_t
*)sys_acl_get_qualifier(entry
);
2674 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2677 gid_to_sid( &sid
, *pgid
);
2678 unix_ug
.type
= ID_TYPE_GID
;
2680 owner_type
= GID_ACE
;
2684 acl_mask
= convert_permset_to_mode_t(permset
);
2685 continue; /* Don't count the mask as an entry. */
2687 /* Use the Everyone SID */
2688 sid
= global_sid_World
;
2689 unix_ug
.type
= ID_TYPE_NOT_SPECIFIED
;
2691 owner_type
= WORLD_ACE
;
2694 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype
));
2699 * Add this entry to the list.
2702 if ((ace
= talloc(talloc_tos(), canon_ace
)) == NULL
)
2706 ace
->type
= tagtype
;
2707 ace
->perms
= convert_permset_to_mode_t(permset
);
2708 ace
->attr
= ALLOW_ACE
;
2710 ace
->unix_ug
= unix_ug
;
2711 ace
->owner_type
= owner_type
;
2712 ace
->ace_flags
= get_pai_flags(pal
, ace
, is_default_acl
);
2714 DLIST_ADD(l_head
, ace
);
2718 * This next call will ensure we have at least a user/group/world set.
2721 if (!ensure_canon_entry_valid_on_get(conn
, &l_head
,
2727 * Now go through the list, masking the permissions with the
2728 * acl_mask. Ensure all DENY Entries are at the start of the list.
2731 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl
? "Default" : "Access"));
2733 for ( ace_count
= 0, ace
= l_head
; ace
; ace
= next_ace
, ace_count
++) {
2734 next_ace
= ace
->next
;
2736 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2737 if (ace
->type
!= SMB_ACL_OTHER
&& ace
->type
!= SMB_ACL_USER_OBJ
)
2738 ace
->perms
&= acl_mask
;
2740 if (ace
->perms
== 0) {
2741 DLIST_PROMOTE(l_head
, ace
);
2744 if( DEBUGLVL( 10 ) ) {
2745 print_canon_ace(ace
, ace_count
);
2749 arrange_posix_perms(fname
,&l_head
);
2751 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head
);
2757 free_canon_ace_list(l_head
);
2761 /****************************************************************************
2762 Check if the current user group list contains a given group.
2763 ****************************************************************************/
2765 bool current_user_in_group(connection_struct
*conn
, gid_t gid
)
2768 const struct security_unix_token
*utok
= get_current_utok(conn
);
2770 for (i
= 0; i
< utok
->ngroups
; i
++) {
2771 if (utok
->groups
[i
] == gid
) {
2779 /****************************************************************************
2780 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2781 ****************************************************************************/
2783 static bool acl_group_override(connection_struct
*conn
,
2784 const struct smb_filename
*smb_fname
)
2786 if ((errno
!= EPERM
) && (errno
!= EACCES
)) {
2790 /* file primary group == user primary or supplementary group */
2791 if (lp_acl_group_control(SNUM(conn
)) &&
2792 current_user_in_group(conn
, smb_fname
->st
.st_ex_gid
)) {
2796 /* user has writeable permission */
2797 if (lp_dos_filemode(SNUM(conn
)) &&
2798 can_write_to_file(conn
, smb_fname
)) {
2805 /****************************************************************************
2806 Attempt to apply an ACL to a file or directory.
2807 ****************************************************************************/
2809 static bool set_canon_ace_list(files_struct
*fsp
,
2812 const SMB_STRUCT_STAT
*psbuf
,
2813 bool *pacl_set_support
)
2815 connection_struct
*conn
= fsp
->conn
;
2817 SMB_ACL_T the_acl
= sys_acl_init(talloc_tos());
2820 SMB_ACL_ENTRY_T mask_entry
;
2821 bool got_mask_entry
= False
;
2822 SMB_ACL_PERMSET_T mask_permset
;
2823 SMB_ACL_TYPE_T the_acl_type
= (default_ace
? SMB_ACL_TYPE_DEFAULT
: SMB_ACL_TYPE_ACCESS
);
2824 bool needs_mask
= False
;
2825 mode_t mask_perms
= 0;
2827 /* Use the psbuf that was passed in. */
2828 if (psbuf
!= &fsp
->fsp_name
->st
) {
2829 fsp
->fsp_name
->st
= *psbuf
;
2832 #if defined(POSIX_ACL_NEEDS_MASK)
2833 /* HP-UX always wants to have a mask (called "class" there). */
2837 if (the_acl
== NULL
) {
2838 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2842 if( DEBUGLVL( 10 )) {
2843 dbgtext("set_canon_ace_list: setting ACL:\n");
2844 for (i
= 0, p_ace
= the_ace
; p_ace
; p_ace
= p_ace
->next
, i
++ ) {
2845 print_canon_ace( p_ace
, i
);
2849 for (i
= 0, p_ace
= the_ace
; p_ace
; p_ace
= p_ace
->next
, i
++ ) {
2850 SMB_ACL_ENTRY_T the_entry
;
2851 SMB_ACL_PERMSET_T the_permset
;
2854 * ACLs only "need" an ACL_MASK entry if there are any named user or
2855 * named group entries. But if there is an ACL_MASK entry, it applies
2856 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2857 * so that it doesn't deny (i.e., mask off) any permissions.
2860 if (p_ace
->type
== SMB_ACL_USER
|| p_ace
->type
== SMB_ACL_GROUP
) {
2862 mask_perms
|= p_ace
->perms
;
2863 } else if (p_ace
->type
== SMB_ACL_GROUP_OBJ
) {
2864 mask_perms
|= p_ace
->perms
;
2868 * Get the entry for this ACE.
2871 if (sys_acl_create_entry(&the_acl
, &the_entry
) == -1) {
2872 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2873 i
, strerror(errno
) ));
2877 if (p_ace
->type
== SMB_ACL_MASK
) {
2878 mask_entry
= the_entry
;
2879 got_mask_entry
= True
;
2883 * Ok - we now know the ACL calls should be working, don't
2884 * allow fallback to chmod.
2887 *pacl_set_support
= True
;
2890 * Initialise the entry from the canon_ace.
2894 * First tell the entry what type of ACE this is.
2897 if (sys_acl_set_tag_type(the_entry
, p_ace
->type
) == -1) {
2898 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2899 i
, strerror(errno
) ));
2904 * Only set the qualifier (user or group id) if the entry is a user
2908 if ((p_ace
->type
== SMB_ACL_USER
) || (p_ace
->type
== SMB_ACL_GROUP
)) {
2909 if (sys_acl_set_qualifier(the_entry
,(void *)&p_ace
->unix_ug
.id
) == -1) {
2910 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2911 i
, strerror(errno
) ));
2917 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2920 if (sys_acl_get_permset(the_entry
, &the_permset
) == -1) {
2921 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2922 i
, strerror(errno
) ));
2926 if (map_acl_perms_to_permset(conn
, p_ace
->perms
, &the_permset
) == -1) {
2927 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2928 (unsigned int)p_ace
->perms
, i
, strerror(errno
) ));
2933 * ..and apply them to the entry.
2936 if (sys_acl_set_permset(the_entry
, the_permset
) == -1) {
2937 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2938 i
, strerror(errno
) ));
2943 print_canon_ace( p_ace
, i
);
2947 if (needs_mask
&& !got_mask_entry
) {
2948 if (sys_acl_create_entry(&the_acl
, &mask_entry
) == -1) {
2949 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno
) ));
2953 if (sys_acl_set_tag_type(mask_entry
, SMB_ACL_MASK
) == -1) {
2954 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno
) ));
2958 if (sys_acl_get_permset(mask_entry
, &mask_permset
) == -1) {
2959 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno
) ));
2963 if (map_acl_perms_to_permset(conn
, S_IRUSR
|S_IWUSR
|S_IXUSR
, &mask_permset
) == -1) {
2964 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno
) ));
2968 if (sys_acl_set_permset(mask_entry
, mask_permset
) == -1) {
2969 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno
) ));
2975 * Finally apply it to the file or directory.
2978 if(default_ace
|| fsp
->is_directory
|| fsp
->fh
->fd
== -1) {
2979 if (SMB_VFS_SYS_ACL_SET_FILE(conn
, fsp
->fsp_name
->base_name
,
2980 the_acl_type
, the_acl
) == -1) {
2982 * Some systems allow all the above calls and only fail with no ACL support
2983 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
2985 if (no_acl_syscall_error(errno
)) {
2986 *pacl_set_support
= False
;
2989 if (acl_group_override(conn
, fsp
->fsp_name
)) {
2992 DEBUG(5,("set_canon_ace_list: acl group "
2993 "control on and current user in file "
2994 "%s primary group.\n",
2998 sret
= SMB_VFS_SYS_ACL_SET_FILE(conn
,
2999 fsp
->fsp_name
->base_name
, the_acl_type
,
3008 DEBUG(2,("set_canon_ace_list: "
3009 "sys_acl_set_file type %s failed for "
3011 the_acl_type
== SMB_ACL_TYPE_DEFAULT
?
3012 "directory default" : "file",
3013 fsp_str_dbg(fsp
), strerror(errno
)));
3018 if (SMB_VFS_SYS_ACL_SET_FD(fsp
, the_acl
) == -1) {
3020 * Some systems allow all the above calls and only fail with no ACL support
3021 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3023 if (no_acl_syscall_error(errno
)) {
3024 *pacl_set_support
= False
;
3027 if (acl_group_override(conn
, fsp
->fsp_name
)) {
3030 DEBUG(5,("set_canon_ace_list: acl group "
3031 "control on and current user in file "
3032 "%s primary group.\n",
3036 sret
= SMB_VFS_SYS_ACL_SET_FD(fsp
, the_acl
);
3044 DEBUG(2,("set_canon_ace_list: "
3045 "sys_acl_set_file failed for file %s "
3047 fsp_str_dbg(fsp
), strerror(errno
)));
3057 if (the_acl
!= NULL
) {
3058 TALLOC_FREE(the_acl
);
3064 /****************************************************************************
3066 ****************************************************************************/
3068 SMB_ACL_T
free_empty_sys_acl(connection_struct
*conn
, SMB_ACL_T the_acl
)
3070 SMB_ACL_ENTRY_T entry
;
3074 if (sys_acl_get_entry(the_acl
, SMB_ACL_FIRST_ENTRY
, &entry
) != 1) {
3075 TALLOC_FREE(the_acl
);
3081 /****************************************************************************
3082 Convert a canon_ace to a generic 3 element permission - if possible.
3083 ****************************************************************************/
3085 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3087 static bool convert_canon_ace_to_posix_perms( files_struct
*fsp
, canon_ace
*file_ace_list
, mode_t
*posix_perms
)
3089 size_t ace_count
= count_canon_ace_list(file_ace_list
);
3091 canon_ace
*owner_ace
= NULL
;
3092 canon_ace
*group_ace
= NULL
;
3093 canon_ace
*other_ace
= NULL
;
3095 if (ace_count
!= 3) {
3096 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3097 "entries for file %s to convert to posix perms.\n",
3102 for (ace_p
= file_ace_list
; ace_p
; ace_p
= ace_p
->next
) {
3103 if (ace_p
->owner_type
== UID_ACE
)
3105 else if (ace_p
->owner_type
== GID_ACE
)
3107 else if (ace_p
->owner_type
== WORLD_ACE
)
3111 if (!owner_ace
|| !group_ace
|| !other_ace
) {
3112 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3113 "standard entries for file %s.\n", fsp_str_dbg(fsp
)));
3117 *posix_perms
= (mode_t
)0;
3119 *posix_perms
|= owner_ace
->perms
;
3120 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IRUSR
, S_IRGRP
);
3121 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IWUSR
, S_IWGRP
);
3122 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IXUSR
, S_IXGRP
);
3123 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IRUSR
, S_IROTH
);
3124 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IWUSR
, S_IWOTH
);
3125 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IXUSR
, S_IXOTH
);
3127 /* The owner must have at least read access. */
3129 *posix_perms
|= S_IRUSR
;
3130 if (fsp
->is_directory
)
3131 *posix_perms
|= (S_IWUSR
|S_IXUSR
);
3133 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3134 "to perm=0%o for file %s.\n", (int)owner_ace
->perms
,
3135 (int)group_ace
->perms
, (int)other_ace
->perms
,
3136 (int)*posix_perms
, fsp_str_dbg(fsp
)));
3141 /****************************************************************************
3142 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3143 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3144 with CI|OI set so it is inherited and also applies to the directory.
3145 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3146 ****************************************************************************/
3148 static size_t merge_default_aces( struct security_ace
*nt_ace_list
, size_t num_aces
)
3152 for (i
= 0; i
< num_aces
; i
++) {
3153 for (j
= i
+1; j
< num_aces
; j
++) {
3154 uint32 i_flags_ni
= (nt_ace_list
[i
].flags
& ~SEC_ACE_FLAG_INHERITED_ACE
);
3155 uint32 j_flags_ni
= (nt_ace_list
[j
].flags
& ~SEC_ACE_FLAG_INHERITED_ACE
);
3156 bool i_inh
= (nt_ace_list
[i
].flags
& SEC_ACE_FLAG_INHERITED_ACE
) ? True
: False
;
3157 bool j_inh
= (nt_ace_list
[j
].flags
& SEC_ACE_FLAG_INHERITED_ACE
) ? True
: False
;
3159 /* We know the lower number ACE's are file entries. */
3160 if ((nt_ace_list
[i
].type
== nt_ace_list
[j
].type
) &&
3161 (nt_ace_list
[i
].size
== nt_ace_list
[j
].size
) &&
3162 (nt_ace_list
[i
].access_mask
== nt_ace_list
[j
].access_mask
) &&
3163 dom_sid_equal(&nt_ace_list
[i
].trustee
, &nt_ace_list
[j
].trustee
) &&
3165 (i_flags_ni
== 0) &&
3166 (j_flags_ni
== (SEC_ACE_FLAG_OBJECT_INHERIT
|
3167 SEC_ACE_FLAG_CONTAINER_INHERIT
|
3168 SEC_ACE_FLAG_INHERIT_ONLY
))) {
3170 * W2K wants to have access allowed zero access ACE's
3171 * at the end of the list. If the mask is zero, merge
3172 * the non-inherited ACE onto the inherited ACE.
3175 if (nt_ace_list
[i
].access_mask
== 0) {
3176 nt_ace_list
[j
].flags
= SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|
3177 (i_inh
? SEC_ACE_FLAG_INHERITED_ACE
: 0);
3178 if (num_aces
- i
- 1 > 0)
3179 memmove(&nt_ace_list
[i
], &nt_ace_list
[i
+1], (num_aces
-i
-1) *
3180 sizeof(struct security_ace
));
3182 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3183 (unsigned int)i
, (unsigned int)j
));
3186 * These are identical except for the flags.
3187 * Merge the inherited ACE onto the non-inherited ACE.
3190 nt_ace_list
[i
].flags
= SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|
3191 (i_inh
? SEC_ACE_FLAG_INHERITED_ACE
: 0);
3192 if (num_aces
- j
- 1 > 0)
3193 memmove(&nt_ace_list
[j
], &nt_ace_list
[j
+1], (num_aces
-j
-1) *
3194 sizeof(struct security_ace
));
3196 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3197 (unsigned int)j
, (unsigned int)i
));
3209 * Add or Replace ACE entry.
3210 * In some cases we need to add a specific ACE for compatibility reasons.
3211 * When doing that we must make sure we are not actually creating a duplicate
3212 * entry. So we need to search whether an ACE entry already exist and eventually
3213 * replacce the access mask, or add a completely new entry if none was found.
3215 * This function assumes the array has enough space to add a new entry without
3216 * any reallocation of memory.
3219 static void add_or_replace_ace(struct security_ace
*nt_ace_list
, size_t *num_aces
,
3220 const struct dom_sid
*sid
, enum security_ace_type type
,
3221 uint32_t mask
, uint8_t flags
)
3225 /* first search for a duplicate */
3226 for (i
= 0; i
< *num_aces
; i
++) {
3227 if (dom_sid_equal(&nt_ace_list
[i
].trustee
, sid
) &&
3228 (nt_ace_list
[i
].flags
== flags
)) break;
3231 if (i
< *num_aces
) { /* found */
3232 nt_ace_list
[i
].type
= type
;
3233 nt_ace_list
[i
].access_mask
= mask
;
3234 DEBUG(10, ("Replacing ACE %d with SID %s and flags %02x\n",
3235 i
, sid_string_dbg(sid
), flags
));
3239 /* not found, append it */
3240 init_sec_ace(&nt_ace_list
[(*num_aces
)++], sid
, type
, mask
, flags
);
3244 /****************************************************************************
3245 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3246 the space for the return elements and returns the size needed to return the
3247 security descriptor. This should be the only external function needed for
3248 the UNIX style get ACL.
3249 ****************************************************************************/
3251 static NTSTATUS
posix_get_nt_acl_common(struct connection_struct
*conn
,
3253 const SMB_STRUCT_STAT
*sbuf
,
3254 struct pai_val
*pal
,
3255 SMB_ACL_T posix_acl
,
3257 uint32_t security_info
,
3258 TALLOC_CTX
*mem_ctx
,
3259 struct security_descriptor
**ppdesc
)
3261 struct dom_sid owner_sid
;
3262 struct dom_sid group_sid
;
3264 struct security_acl
*psa
= NULL
;
3265 size_t num_acls
= 0;
3266 size_t num_def_acls
= 0;
3267 size_t num_aces
= 0;
3268 canon_ace
*file_ace
= NULL
;
3269 canon_ace
*dir_ace
= NULL
;
3270 struct security_ace
*nt_ace_list
= NULL
;
3271 size_t num_profile_acls
= 0;
3272 struct dom_sid orig_owner_sid
;
3273 struct security_descriptor
*psd
= NULL
;
3277 * Get the owner, group and world SIDs.
3280 create_file_sids(sbuf
, &owner_sid
, &group_sid
);
3282 if (lp_profile_acls(SNUM(conn
))) {
3283 /* For WXP SP1 the owner must be administrators. */
3284 sid_copy(&orig_owner_sid
, &owner_sid
);
3285 sid_copy(&owner_sid
, &global_sid_Builtin_Administrators
);
3286 sid_copy(&group_sid
, &global_sid_Builtin_Users
);
3287 num_profile_acls
= 3;
3290 if ((security_info
& SECINFO_DACL
) && !(security_info
& SECINFO_PROTECTED_DACL
)) {
3293 * In the optimum case Creator Owner and Creator Group would be used for
3294 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3295 * would lead to usability problems under Windows: The Creator entries
3296 * are only available in browse lists of directories and not for files;
3297 * additionally the identity of the owning group couldn't be determined.
3298 * We therefore use those identities only for Default ACLs.
3301 /* Create the canon_ace lists. */
3302 file_ace
= canonicalise_acl(conn
, name
, posix_acl
, sbuf
,
3303 &owner_sid
, &group_sid
, pal
,
3304 SMB_ACL_TYPE_ACCESS
);
3306 /* We must have *some* ACLS. */
3308 if (count_canon_ace_list(file_ace
) == 0) {
3309 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name
));
3313 if (S_ISDIR(sbuf
->st_ex_mode
) && def_acl
) {
3314 dir_ace
= canonicalise_acl(conn
, name
, def_acl
,
3316 &global_sid_Creator_Owner
,
3317 &global_sid_Creator_Group
,
3318 pal
, SMB_ACL_TYPE_DEFAULT
);
3322 * Create the NT ACE list from the canonical ace lists.
3327 enum security_ace_type nt_acl_type
;
3329 num_acls
= count_canon_ace_list(file_ace
);
3330 num_def_acls
= count_canon_ace_list(dir_ace
);
3332 /* Allocate the ace list. */
3333 if ((nt_ace_list
= talloc_array(talloc_tos(), struct security_ace
,num_acls
+ num_profile_acls
+ num_def_acls
)) == NULL
) {
3334 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3338 memset(nt_ace_list
, '\0', (num_acls
+ num_def_acls
) * sizeof(struct security_ace
) );
3341 * Create the NT ACE list from the canonical ace lists.
3344 for (ace
= file_ace
; ace
!= NULL
; ace
= ace
->next
) {
3345 uint32_t acc
= map_canon_ace_perms(SNUM(conn
),
3348 S_ISDIR(sbuf
->st_ex_mode
));
3349 init_sec_ace(&nt_ace_list
[num_aces
++],
3356 /* The User must have access to a profile share - even
3357 * if we can't map the SID. */
3358 if (lp_profile_acls(SNUM(conn
))) {
3359 add_or_replace_ace(nt_ace_list
, &num_aces
,
3360 &global_sid_Builtin_Users
,
3361 SEC_ACE_TYPE_ACCESS_ALLOWED
,
3362 FILE_GENERIC_ALL
, 0);
3365 for (ace
= dir_ace
; ace
!= NULL
; ace
= ace
->next
) {
3366 uint32_t acc
= map_canon_ace_perms(SNUM(conn
),
3369 S_ISDIR(sbuf
->st_ex_mode
));
3370 init_sec_ace(&nt_ace_list
[num_aces
++],
3375 SEC_ACE_FLAG_OBJECT_INHERIT
|
3376 SEC_ACE_FLAG_CONTAINER_INHERIT
|
3377 SEC_ACE_FLAG_INHERIT_ONLY
);
3380 /* The User must have access to a profile share - even
3381 * if we can't map the SID. */
3382 if (lp_profile_acls(SNUM(conn
))) {
3383 add_or_replace_ace(nt_ace_list
, &num_aces
,
3384 &global_sid_Builtin_Users
,
3385 SEC_ACE_TYPE_ACCESS_ALLOWED
,
3387 SEC_ACE_FLAG_OBJECT_INHERIT
|
3388 SEC_ACE_FLAG_CONTAINER_INHERIT
|
3389 SEC_ACE_FLAG_INHERIT_ONLY
);
3393 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3394 * Win2K needs this to get the inheritance correct when replacing ACLs
3395 * on a directory tree. Based on work by Jim @ IBM.
3398 num_aces
= merge_default_aces(nt_ace_list
, num_aces
);
3400 if (lp_profile_acls(SNUM(conn
))) {
3401 for (i
= 0; i
< num_aces
; i
++) {
3402 if (dom_sid_equal(&nt_ace_list
[i
].trustee
, &owner_sid
)) {
3403 add_or_replace_ace(nt_ace_list
, &num_aces
,
3405 nt_ace_list
[i
].type
,
3406 nt_ace_list
[i
].access_mask
,
3407 nt_ace_list
[i
].flags
);
3415 if((psa
= make_sec_acl( talloc_tos(), NT4_ACL_REVISION
, num_aces
, nt_ace_list
)) == NULL
) {
3416 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3420 } /* security_info & SECINFO_DACL */
3422 psd
= make_standard_sec_desc(mem_ctx
,
3423 (security_info
& SECINFO_OWNER
) ? &owner_sid
: NULL
,
3424 (security_info
& SECINFO_GROUP
) ? &group_sid
: NULL
,
3429 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3435 * Windows 2000: The DACL_PROTECTED flag in the security
3436 * descriptor marks the ACL as non-inheriting, i.e., no
3437 * ACEs from higher level directories propagate to this
3438 * ACL. In the POSIX ACL model permissions are only
3439 * inherited at file create time, so ACLs never contain
3440 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3441 * flag doesn't seem to bother Windows NT.
3442 * Always set this if map acl inherit is turned off.
3444 if (pal
== NULL
|| !lp_map_acl_inherit(SNUM(conn
))) {
3445 psd
->type
|= SEC_DESC_DACL_PROTECTED
;
3447 psd
->type
|= pal
->sd_type
;
3451 dacl_sort_into_canonical_order(psd
->dacl
->aces
, (unsigned int)psd
->dacl
->num_aces
);
3459 TALLOC_FREE(posix_acl
);
3462 TALLOC_FREE(def_acl
);
3464 free_canon_ace_list(file_ace
);
3465 free_canon_ace_list(dir_ace
);
3466 free_inherited_info(pal
);
3467 TALLOC_FREE(nt_ace_list
);
3469 return NT_STATUS_OK
;
3472 NTSTATUS
posix_fget_nt_acl(struct files_struct
*fsp
, uint32_t security_info
,
3473 TALLOC_CTX
*mem_ctx
,
3474 struct security_descriptor
**ppdesc
)
3476 SMB_STRUCT_STAT sbuf
;
3477 SMB_ACL_T posix_acl
= NULL
;
3478 struct pai_val
*pal
;
3479 TALLOC_CTX
*frame
= talloc_stackframe();
3484 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3487 /* can it happen that fsp_name == NULL ? */
3488 if (fsp
->is_directory
|| fsp
->fh
->fd
== -1) {
3489 status
= posix_get_nt_acl(fsp
->conn
, fsp
->fsp_name
->base_name
,
3490 security_info
, mem_ctx
, ppdesc
);
3495 /* Get the stat struct for the owner info. */
3496 if(SMB_VFS_FSTAT(fsp
, &sbuf
) != 0) {
3498 return map_nt_error_from_unix(errno
);
3501 /* Get the ACL from the fd. */
3502 posix_acl
= SMB_VFS_SYS_ACL_GET_FD(fsp
, frame
);
3504 pal
= fload_inherited_info(fsp
);
3506 status
= posix_get_nt_acl_common(fsp
->conn
, fsp
->fsp_name
->base_name
,
3507 &sbuf
, pal
, posix_acl
, NULL
,
3508 security_info
, mem_ctx
, ppdesc
);
3513 NTSTATUS
posix_get_nt_acl(struct connection_struct
*conn
, const char *name
,
3514 uint32_t security_info
,
3515 TALLOC_CTX
*mem_ctx
,
3516 struct security_descriptor
**ppdesc
)
3518 SMB_ACL_T posix_acl
= NULL
;
3519 SMB_ACL_T def_acl
= NULL
;
3520 struct pai_val
*pal
;
3521 struct smb_filename smb_fname
;
3523 TALLOC_CTX
*frame
= talloc_stackframe();
3528 DEBUG(10,("posix_get_nt_acl: called for file %s\n", name
));
3530 ZERO_STRUCT(smb_fname
);
3531 smb_fname
.base_name
= discard_const_p(char, name
);
3533 /* Get the stat struct for the owner info. */
3534 if (lp_posix_pathnames()) {
3535 ret
= SMB_VFS_LSTAT(conn
, &smb_fname
);
3537 ret
= SMB_VFS_STAT(conn
, &smb_fname
);
3542 return map_nt_error_from_unix(errno
);
3545 /* Get the ACL from the path. */
3546 posix_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, name
,
3547 SMB_ACL_TYPE_ACCESS
, frame
);
3549 /* If it's a directory get the default POSIX ACL. */
3550 if(S_ISDIR(smb_fname
.st
.st_ex_mode
)) {
3551 def_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, name
,
3552 SMB_ACL_TYPE_DEFAULT
, frame
);
3553 def_acl
= free_empty_sys_acl(conn
, def_acl
);
3556 pal
= load_inherited_info(conn
, name
);
3558 status
= posix_get_nt_acl_common(conn
, name
, &smb_fname
.st
, pal
,
3559 posix_acl
, def_acl
, security_info
,
3566 /****************************************************************************
3567 Try to chown a file. We will be able to chown it under the following conditions.
3569 1) If we have root privileges, then it will just work.
3570 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3571 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3572 4) If we have write permission to the file and dos_filemodes is set
3573 then allow chown to the currently authenticated user.
3574 ****************************************************************************/
3576 NTSTATUS
try_chown(files_struct
*fsp
, uid_t uid
, gid_t gid
)
3580 if(!CAN_WRITE(fsp
->conn
)) {
3581 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
3585 status
= vfs_chown_fsp(fsp
, uid
, gid
);
3586 if (NT_STATUS_IS_OK(status
)) {
3590 /* Case (2) / (3) */
3591 if (lp_enable_privileges()) {
3592 bool has_take_ownership_priv
= security_token_has_privilege(
3593 get_current_nttok(fsp
->conn
),
3594 SEC_PRIV_TAKE_OWNERSHIP
);
3595 bool has_restore_priv
= security_token_has_privilege(
3596 get_current_nttok(fsp
->conn
),
3599 if (has_restore_priv
) {
3601 } else if (has_take_ownership_priv
) {
3603 if (uid
== get_current_uid(fsp
->conn
)) {
3606 has_take_ownership_priv
= false;
3610 if (has_take_ownership_priv
|| has_restore_priv
) {
3612 status
= vfs_chown_fsp(fsp
, uid
, gid
);
3619 if (!lp_dos_filemode(SNUM(fsp
->conn
))) {
3620 return NT_STATUS_ACCESS_DENIED
;
3623 /* only allow chown to the current user. This is more secure,
3624 and also copes with the case where the SID in a take ownership ACL is
3625 a local SID on the users workstation
3627 if (uid
!= get_current_uid(fsp
->conn
)) {
3628 return NT_STATUS_ACCESS_DENIED
;
3632 /* Keep the current file gid the same. */
3633 status
= vfs_chown_fsp(fsp
, uid
, (gid_t
)-1);
3640 /* Disable this - prevents ACL inheritance from the ACL editor. JRA. */
3642 /****************************************************************************
3643 Take care of parent ACL inheritance.
3644 ****************************************************************************/
3646 NTSTATUS
append_parent_acl(files_struct
*fsp
,
3647 const struct security_descriptor
*pcsd
,
3648 struct security_descriptor
**pp_new_sd
)
3650 struct smb_filename
*smb_dname
= NULL
;
3651 struct security_descriptor
*parent_sd
= NULL
;
3652 files_struct
*parent_fsp
= NULL
;
3653 TALLOC_CTX
*mem_ctx
= talloc_tos();
3654 char *parent_name
= NULL
;
3655 struct security_ace
*new_ace
= NULL
;
3656 unsigned int num_aces
= pcsd
->dacl
->num_aces
;
3660 struct security_descriptor
*psd
= dup_sec_desc(talloc_tos(), pcsd
);
3661 bool is_dacl_protected
= (pcsd
->type
& SEC_DESC_DACL_PROTECTED
);
3664 return NT_STATUS_NO_MEMORY
;
3667 if (!parent_dirname(mem_ctx
, fsp
->fsp_name
->base_name
, &parent_name
,
3669 return NT_STATUS_NO_MEMORY
;
3672 status
= create_synthetic_smb_fname(mem_ctx
, parent_name
, NULL
, NULL
,
3674 if (!NT_STATUS_IS_OK(status
)) {
3678 status
= SMB_VFS_CREATE_FILE(
3679 fsp
->conn
, /* conn */
3681 0, /* root_dir_fid */
3682 smb_dname
, /* fname */
3683 FILE_READ_ATTRIBUTES
, /* access_mask */
3684 FILE_SHARE_NONE
, /* share_access */
3685 FILE_OPEN
, /* create_disposition*/
3686 FILE_DIRECTORY_FILE
, /* create_options */
3687 0, /* file_attributes */
3688 INTERNAL_OPEN_ONLY
, /* oplock_request */
3689 0, /* allocation_size */
3692 &parent_fsp
, /* result */
3695 if (!NT_STATUS_IS_OK(status
)) {
3696 TALLOC_FREE(smb_dname
);
3700 status
= SMB_VFS_GET_NT_ACL(parent_fsp
->conn
, smb_dname
->base_name
,
3701 SECINFO_DACL
, &parent_sd
);
3703 close_file(NULL
, parent_fsp
, NORMAL_CLOSE
);
3704 TALLOC_FREE(smb_dname
);
3706 if (!NT_STATUS_IS_OK(status
)) {
3711 * Make room for potentially all the ACLs from
3712 * the parent. We used to add the ugw triple here,
3713 * as we knew we were dealing with POSIX ACLs.
3714 * We no longer need to do so as we can guarentee
3715 * that a default ACL from the parent directory will
3716 * be well formed for POSIX ACLs if it came from a
3717 * POSIX ACL source, and if we're not writing to a
3718 * POSIX ACL sink then we don't care if it's not well
3722 num_aces
+= parent_sd
->dacl
->num_aces
;
3724 if((new_ace
= talloc_zero_array(mem_ctx
, struct security_ace
,
3725 num_aces
)) == NULL
) {
3726 return NT_STATUS_NO_MEMORY
;
3729 /* Start by copying in all the given ACE entries. */
3730 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
3731 sec_ace_copy(&new_ace
[i
], &psd
->dacl
->aces
[i
]);
3735 * Note that we're ignoring "inherit permissions" here
3736 * as that really only applies to newly created files. JRA.
3739 /* Finally append any inherited ACEs. */
3740 for (j
= 0; j
< parent_sd
->dacl
->num_aces
; j
++) {
3741 struct security_ace
*se
= &parent_sd
->dacl
->aces
[j
];
3743 if (fsp
->is_directory
) {
3744 if (!(se
->flags
& SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
3745 /* Doesn't apply to a directory - ignore. */
3746 DEBUG(10,("append_parent_acl: directory %s "
3747 "ignoring non container "
3748 "inherit flags %u on ACE with sid %s "
3751 (unsigned int)se
->flags
,
3752 sid_string_dbg(&se
->trustee
),
3757 if (!(se
->flags
& SEC_ACE_FLAG_OBJECT_INHERIT
)) {
3758 /* Doesn't apply to a file - ignore. */
3759 DEBUG(10,("append_parent_acl: file %s "
3760 "ignoring non object "
3761 "inherit flags %u on ACE with sid %s "
3764 (unsigned int)se
->flags
,
3765 sid_string_dbg(&se
->trustee
),
3771 if (is_dacl_protected
) {
3772 /* If the DACL is protected it means we must
3773 * not overwrite an existing ACE entry with the
3774 * same SID. This is order N^2. Ouch :-(. JRA. */
3776 for (k
= 0; k
< psd
->dacl
->num_aces
; k
++) {
3777 if (dom_sid_equal(&psd
->dacl
->aces
[k
].trustee
,
3782 if (k
< psd
->dacl
->num_aces
) {
3783 /* SID matched. Ignore. */
3784 DEBUG(10,("append_parent_acl: path %s "
3785 "ignoring ACE with protected sid %s "
3788 sid_string_dbg(&se
->trustee
),
3794 sec_ace_copy(&new_ace
[i
], se
);
3795 if (se
->flags
& SEC_ACE_FLAG_NO_PROPAGATE_INHERIT
) {
3796 new_ace
[i
].flags
&= ~(SEC_ACE_FLAG_VALID_INHERIT
);
3798 new_ace
[i
].flags
|= SEC_ACE_FLAG_INHERITED_ACE
;
3800 if (fsp
->is_directory
) {
3802 * Strip off any inherit only. It's applied.
3804 new_ace
[i
].flags
&= ~(SEC_ACE_FLAG_INHERIT_ONLY
);
3805 if (se
->flags
& SEC_ACE_FLAG_NO_PROPAGATE_INHERIT
) {
3806 /* No further inheritance. */
3808 ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|
3809 SEC_ACE_FLAG_OBJECT_INHERIT
);
3813 * Strip off any container or inherit
3814 * flags, they can't apply to objects.
3816 new_ace
[i
].flags
&= ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|
3817 SEC_ACE_FLAG_INHERIT_ONLY
|
3818 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT
);
3822 DEBUG(10,("append_parent_acl: path %s "
3823 "inheriting ACE with sid %s "
3826 sid_string_dbg(&se
->trustee
),
3830 psd
->dacl
->aces
= new_ace
;
3831 psd
->dacl
->num_aces
= i
;
3832 psd
->type
&= ~(SEC_DESC_DACL_AUTO_INHERITED
|
3833 SEC_DESC_DACL_AUTO_INHERIT_REQ
);
3840 /****************************************************************************
3841 Reply to set a security descriptor on an fsp. security_info_sent is the
3842 description of the following NT ACL.
3843 This should be the only external function needed for the UNIX style set ACL.
3844 We make a copy of psd_orig as internal functions modify the elements inside
3845 it, even though it's a const pointer.
3846 ****************************************************************************/
3848 NTSTATUS
set_nt_acl(files_struct
*fsp
, uint32 security_info_sent
, const struct security_descriptor
*psd_orig
)
3850 connection_struct
*conn
= fsp
->conn
;
3851 uid_t user
= (uid_t
)-1;
3852 gid_t grp
= (gid_t
)-1;
3853 struct dom_sid file_owner_sid
;
3854 struct dom_sid file_grp_sid
;
3855 canon_ace
*file_ace_list
= NULL
;
3856 canon_ace
*dir_ace_list
= NULL
;
3857 bool acl_perms
= False
;
3858 mode_t orig_mode
= (mode_t
)0;
3860 bool set_acl_as_root
= false;
3861 bool acl_set_support
= false;
3863 struct security_descriptor
*psd
= NULL
;
3865 DEBUG(10,("set_nt_acl: called for file %s\n",
3868 if (!CAN_WRITE(conn
)) {
3869 DEBUG(10,("set acl rejected on read-only share\n"));
3870 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
3874 return NT_STATUS_INVALID_PARAMETER
;
3877 psd
= dup_sec_desc(talloc_tos(), psd_orig
);
3879 return NT_STATUS_NO_MEMORY
;
3883 * Get the current state of the file.
3886 status
= vfs_stat_fsp(fsp
);
3887 if (!NT_STATUS_IS_OK(status
)) {
3891 /* Save the original element we check against. */
3892 orig_mode
= fsp
->fsp_name
->st
.st_ex_mode
;
3895 * Unpack the user/group/world id's.
3898 /* POSIX can't cope with missing owner/group. */
3899 if ((security_info_sent
& SECINFO_OWNER
) && (psd
->owner_sid
== NULL
)) {
3900 security_info_sent
&= ~SECINFO_OWNER
;
3902 if ((security_info_sent
& SECINFO_GROUP
) && (psd
->group_sid
== NULL
)) {
3903 security_info_sent
&= ~SECINFO_GROUP
;
3906 status
= unpack_nt_owners( conn
, &user
, &grp
, security_info_sent
, psd
);
3907 if (!NT_STATUS_IS_OK(status
)) {
3912 * Do we need to chown ? If so this must be done first as the incoming
3913 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3917 if (((user
!= (uid_t
)-1) && (fsp
->fsp_name
->st
.st_ex_uid
!= user
)) ||
3918 (( grp
!= (gid_t
)-1) && (fsp
->fsp_name
->st
.st_ex_gid
!= grp
))) {
3920 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
3921 fsp_str_dbg(fsp
), (unsigned int)user
,
3922 (unsigned int)grp
));
3924 status
= try_chown(fsp
, user
, grp
);
3925 if(!NT_STATUS_IS_OK(status
)) {
3926 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
3927 "= %s.\n", fsp_str_dbg(fsp
),
3930 nt_errstr(status
)));
3935 * Recheck the current state of the file, which may have changed.
3936 * (suid/sgid bits, for instance)
3939 status
= vfs_stat_fsp(fsp
);
3940 if (!NT_STATUS_IS_OK(status
)) {
3944 /* Save the original element we check against. */
3945 orig_mode
= fsp
->fsp_name
->st
.st_ex_mode
;
3947 /* If we successfully chowned, we know we must
3948 * be able to set the acl, so do it as root.
3950 set_acl_as_root
= true;
3953 create_file_sids(&fsp
->fsp_name
->st
, &file_owner_sid
, &file_grp_sid
);
3955 if((security_info_sent
& SECINFO_DACL
) &&
3956 (psd
->type
& SEC_DESC_DACL_PRESENT
) &&
3957 (psd
->dacl
== NULL
)) {
3958 struct security_ace ace
[3];
3960 /* We can't have NULL DACL in POSIX.
3961 Use owner/group/Everyone -> full access. */
3963 init_sec_ace(&ace
[0],
3965 SEC_ACE_TYPE_ACCESS_ALLOWED
,
3968 init_sec_ace(&ace
[1],
3970 SEC_ACE_TYPE_ACCESS_ALLOWED
,
3973 init_sec_ace(&ace
[2],
3975 SEC_ACE_TYPE_ACCESS_ALLOWED
,
3978 psd
->dacl
= make_sec_acl(talloc_tos(),
3982 if (psd
->dacl
== NULL
) {
3983 return NT_STATUS_NO_MEMORY
;
3985 security_acl_map_generic(psd
->dacl
, &file_generic_mapping
);
3988 acl_perms
= unpack_canon_ace(fsp
, &fsp
->fsp_name
->st
, &file_owner_sid
,
3989 &file_grp_sid
, &file_ace_list
,
3990 &dir_ace_list
, security_info_sent
, psd
);
3992 /* Ignore W2K traverse DACL set. */
3993 if (!file_ace_list
&& !dir_ace_list
) {
3994 return NT_STATUS_OK
;
3998 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
3999 free_canon_ace_list(file_ace_list
);
4000 free_canon_ace_list(dir_ace_list
);
4001 return NT_STATUS_ACCESS_DENIED
;
4005 * Only change security if we got a DACL.
4008 if(!(security_info_sent
& SECINFO_DACL
) || (psd
->dacl
== NULL
)) {
4009 free_canon_ace_list(file_ace_list
);
4010 free_canon_ace_list(dir_ace_list
);
4011 return NT_STATUS_OK
;
4015 * Try using the POSIX ACL set first. Fall back to chmod if
4016 * we have no ACL support on this filesystem.
4019 if (acl_perms
&& file_ace_list
) {
4020 if (set_acl_as_root
) {
4023 ret
= set_canon_ace_list(fsp
, file_ace_list
, false,
4024 &fsp
->fsp_name
->st
, &acl_set_support
);
4025 if (set_acl_as_root
) {
4028 if (acl_set_support
&& ret
== false) {
4029 DEBUG(3,("set_nt_acl: failed to set file acl on file "
4030 "%s (%s).\n", fsp_str_dbg(fsp
),
4032 free_canon_ace_list(file_ace_list
);
4033 free_canon_ace_list(dir_ace_list
);
4034 return map_nt_error_from_unix(errno
);
4038 if (acl_perms
&& acl_set_support
&& fsp
->is_directory
) {
4040 if (set_acl_as_root
) {
4043 ret
= set_canon_ace_list(fsp
, dir_ace_list
, true,
4046 if (set_acl_as_root
) {
4050 DEBUG(3,("set_nt_acl: failed to set default "
4051 "acl on directory %s (%s).\n",
4052 fsp_str_dbg(fsp
), strerror(errno
)));
4053 free_canon_ace_list(file_ace_list
);
4054 free_canon_ace_list(dir_ace_list
);
4055 return map_nt_error_from_unix(errno
);
4061 * No default ACL - delete one if it exists.
4064 if (set_acl_as_root
) {
4067 sret
= SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn
,
4068 fsp
->fsp_name
->base_name
);
4069 if (set_acl_as_root
) {
4073 if (acl_group_override(conn
, fsp
->fsp_name
)) {
4074 DEBUG(5,("set_nt_acl: acl group "
4075 "control on and current user "
4076 "in file %s primary group. "
4077 "Override delete_def_acl\n",
4082 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
4084 fsp
->fsp_name
->base_name
);
4089 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno
)));
4090 free_canon_ace_list(file_ace_list
);
4091 free_canon_ace_list(dir_ace_list
);
4092 return map_nt_error_from_unix(errno
);
4098 if (acl_set_support
) {
4099 if (set_acl_as_root
) {
4102 store_inheritance_attributes(fsp
,
4106 if (set_acl_as_root
) {
4112 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
4115 if(!acl_set_support
&& acl_perms
) {
4118 if (!convert_canon_ace_to_posix_perms( fsp
, file_ace_list
, &posix_perms
)) {
4119 free_canon_ace_list(file_ace_list
);
4120 free_canon_ace_list(dir_ace_list
);
4121 DEBUG(3,("set_nt_acl: failed to convert file acl to "
4122 "posix permissions for file %s.\n",
4124 return NT_STATUS_ACCESS_DENIED
;
4127 if (orig_mode
!= posix_perms
) {
4130 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
4131 fsp_str_dbg(fsp
), (unsigned int)posix_perms
));
4133 if (set_acl_as_root
) {
4136 sret
= SMB_VFS_CHMOD(conn
, fsp
->fsp_name
->base_name
,
4138 if (set_acl_as_root
) {
4142 if (acl_group_override(conn
, fsp
->fsp_name
)) {
4143 DEBUG(5,("set_nt_acl: acl group "
4144 "control on and current user "
4145 "in file %s primary group. "
4150 sret
= SMB_VFS_CHMOD(conn
,
4151 fsp
->fsp_name
->base_name
,
4157 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
4158 "failed. Error = %s.\n",
4160 (unsigned int)posix_perms
,
4162 free_canon_ace_list(file_ace_list
);
4163 free_canon_ace_list(dir_ace_list
);
4164 return map_nt_error_from_unix(errno
);
4170 free_canon_ace_list(file_ace_list
);
4171 free_canon_ace_list(dir_ace_list
);
4173 /* Ensure the stat struct in the fsp is correct. */
4174 status
= vfs_stat_fsp(fsp
);
4176 return NT_STATUS_OK
;
4179 /****************************************************************************
4180 Get the actual group bits stored on a file with an ACL. Has no effect if
4181 the file has no ACL. Needed in dosmode code where the stat() will return
4182 the mask bits, not the real group bits, for a file with an ACL.
4183 ****************************************************************************/
4185 int get_acl_group_bits( connection_struct
*conn
, const char *fname
, mode_t
*mode
)
4187 int entry_id
= SMB_ACL_FIRST_ENTRY
;
4188 SMB_ACL_ENTRY_T entry
;
4189 SMB_ACL_T posix_acl
;
4192 posix_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, fname
,
4193 SMB_ACL_TYPE_ACCESS
, talloc_tos());
4194 if (posix_acl
== (SMB_ACL_T
)NULL
)
4197 while (sys_acl_get_entry(posix_acl
, entry_id
, &entry
) == 1) {
4198 SMB_ACL_TAG_T tagtype
;
4199 SMB_ACL_PERMSET_T permset
;
4201 entry_id
= SMB_ACL_NEXT_ENTRY
;
4203 if (sys_acl_get_tag_type(entry
, &tagtype
) ==-1)
4206 if (tagtype
== SMB_ACL_GROUP_OBJ
) {
4207 if (sys_acl_get_permset(entry
, &permset
) == -1) {
4210 *mode
&= ~(S_IRGRP
|S_IWGRP
|S_IXGRP
);
4211 *mode
|= (sys_acl_get_perm(permset
, SMB_ACL_READ
) ? S_IRGRP
: 0);
4212 *mode
|= (sys_acl_get_perm(permset
, SMB_ACL_WRITE
) ? S_IWGRP
: 0);
4213 *mode
|= (sys_acl_get_perm(permset
, SMB_ACL_EXECUTE
) ? S_IXGRP
: 0);
4219 TALLOC_FREE(posix_acl
);
4223 /****************************************************************************
4224 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4225 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4226 ****************************************************************************/
4228 static int chmod_acl_internals( connection_struct
*conn
, SMB_ACL_T posix_acl
, mode_t mode
)
4230 int entry_id
= SMB_ACL_FIRST_ENTRY
;
4231 SMB_ACL_ENTRY_T entry
;
4232 int num_entries
= 0;
4234 while ( sys_acl_get_entry(posix_acl
, entry_id
, &entry
) == 1) {
4235 SMB_ACL_TAG_T tagtype
;
4236 SMB_ACL_PERMSET_T permset
;
4239 entry_id
= SMB_ACL_NEXT_ENTRY
;
4241 if (sys_acl_get_tag_type(entry
, &tagtype
) == -1)
4244 if (sys_acl_get_permset(entry
, &permset
) == -1)
4250 case SMB_ACL_USER_OBJ
:
4251 perms
= unix_perms_to_acl_perms(mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
4253 case SMB_ACL_GROUP_OBJ
:
4254 perms
= unix_perms_to_acl_perms(mode
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
4258 * FIXME: The ACL_MASK entry permissions should really be set to
4259 * the union of the permissions of all ACL_USER,
4260 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4261 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4263 perms
= S_IRUSR
|S_IWUSR
|S_IXUSR
;
4266 perms
= unix_perms_to_acl_perms(mode
, S_IROTH
, S_IWOTH
, S_IXOTH
);
4272 if (map_acl_perms_to_permset(conn
, perms
, &permset
) == -1)
4275 if (sys_acl_set_permset(entry
, permset
) == -1)
4280 * If this is a simple 3 element ACL or no elements then it's a standard
4281 * UNIX permission set. Just use chmod...
4284 if ((num_entries
== 3) || (num_entries
== 0))
4290 /****************************************************************************
4291 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4292 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4293 resulting ACL on TO. Note that name is in UNIX character set.
4294 ****************************************************************************/
4296 static int copy_access_posix_acl(connection_struct
*conn
, const char *from
, const char *to
, mode_t mode
)
4298 SMB_ACL_T posix_acl
= NULL
;
4301 if ((posix_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, from
,
4302 SMB_ACL_TYPE_ACCESS
,
4303 talloc_tos())) == NULL
)
4306 if ((ret
= chmod_acl_internals(conn
, posix_acl
, mode
)) == -1)
4309 ret
= SMB_VFS_SYS_ACL_SET_FILE(conn
, to
, SMB_ACL_TYPE_ACCESS
, posix_acl
);
4313 TALLOC_FREE(posix_acl
);
4317 /****************************************************************************
4318 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4319 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4320 Note that name is in UNIX character set.
4321 ****************************************************************************/
4323 int chmod_acl(connection_struct
*conn
, const char *name
, mode_t mode
)
4325 return copy_access_posix_acl(conn
, name
, name
, mode
);
4328 /****************************************************************************
4329 Check for an existing default POSIX ACL on a directory.
4330 ****************************************************************************/
4332 static bool directory_has_default_posix_acl(connection_struct
*conn
, const char *fname
)
4334 SMB_ACL_T def_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, fname
,
4335 SMB_ACL_TYPE_DEFAULT
,
4337 bool has_acl
= False
;
4338 SMB_ACL_ENTRY_T entry
;
4340 if (def_acl
!= NULL
&& (sys_acl_get_entry(def_acl
, SMB_ACL_FIRST_ENTRY
, &entry
) == 1)) {
4345 TALLOC_FREE(def_acl
);
4350 /****************************************************************************
4351 If the parent directory has no default ACL but it does have an Access ACL,
4352 inherit this Access ACL to file name.
4353 ****************************************************************************/
4355 int inherit_access_posix_acl(connection_struct
*conn
, const char *inherit_from_dir
,
4356 const char *name
, mode_t mode
)
4358 if (directory_has_default_posix_acl(conn
, inherit_from_dir
))
4361 return copy_access_posix_acl(conn
, inherit_from_dir
, name
, mode
);
4364 /****************************************************************************
4365 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4366 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4367 ****************************************************************************/
4369 int fchmod_acl(files_struct
*fsp
, mode_t mode
)
4371 connection_struct
*conn
= fsp
->conn
;
4372 SMB_ACL_T posix_acl
= NULL
;
4375 if ((posix_acl
= SMB_VFS_SYS_ACL_GET_FD(fsp
, talloc_tos())) == NULL
)
4378 if ((ret
= chmod_acl_internals(conn
, posix_acl
, mode
)) == -1)
4381 ret
= SMB_VFS_SYS_ACL_SET_FD(fsp
, posix_acl
);
4385 TALLOC_FREE(posix_acl
);
4389 /****************************************************************************
4390 Map from wire type to permset.
4391 ****************************************************************************/
4393 static bool unix_ex_wire_to_permset(connection_struct
*conn
, unsigned char wire_perm
, SMB_ACL_PERMSET_T
*p_permset
)
4395 if (wire_perm
& ~(SMB_POSIX_ACL_READ
|SMB_POSIX_ACL_WRITE
|SMB_POSIX_ACL_EXECUTE
)) {
4399 if (sys_acl_clear_perms(*p_permset
) == -1) {
4403 if (wire_perm
& SMB_POSIX_ACL_READ
) {
4404 if (sys_acl_add_perm(*p_permset
, SMB_ACL_READ
) == -1) {
4408 if (wire_perm
& SMB_POSIX_ACL_WRITE
) {
4409 if (sys_acl_add_perm(*p_permset
, SMB_ACL_WRITE
) == -1) {
4413 if (wire_perm
& SMB_POSIX_ACL_EXECUTE
) {
4414 if (sys_acl_add_perm(*p_permset
, SMB_ACL_EXECUTE
) == -1) {
4421 /****************************************************************************
4422 Map from wire type to tagtype.
4423 ****************************************************************************/
4425 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt
, SMB_ACL_TAG_T
*p_tt
)
4428 case SMB_POSIX_ACL_USER_OBJ
:
4429 *p_tt
= SMB_ACL_USER_OBJ
;
4431 case SMB_POSIX_ACL_USER
:
4432 *p_tt
= SMB_ACL_USER
;
4434 case SMB_POSIX_ACL_GROUP_OBJ
:
4435 *p_tt
= SMB_ACL_GROUP_OBJ
;
4437 case SMB_POSIX_ACL_GROUP
:
4438 *p_tt
= SMB_ACL_GROUP
;
4440 case SMB_POSIX_ACL_MASK
:
4441 *p_tt
= SMB_ACL_MASK
;
4443 case SMB_POSIX_ACL_OTHER
:
4444 *p_tt
= SMB_ACL_OTHER
;
4452 /****************************************************************************
4453 Create a new POSIX acl from wire permissions.
4454 FIXME ! How does the share mask/mode fit into this.... ?
4455 ****************************************************************************/
4457 static SMB_ACL_T
create_posix_acl_from_wire(connection_struct
*conn
,
4460 TALLOC_CTX
*mem_ctx
)
4463 SMB_ACL_T the_acl
= sys_acl_init(mem_ctx
);
4465 if (the_acl
== NULL
) {
4469 for (i
= 0; i
< num_acls
; i
++) {
4470 SMB_ACL_ENTRY_T the_entry
;
4471 SMB_ACL_PERMSET_T the_permset
;
4472 SMB_ACL_TAG_T tag_type
;
4474 if (sys_acl_create_entry(&the_acl
, &the_entry
) == -1) {
4475 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4476 i
, strerror(errno
) ));
4480 if (!unix_ex_wire_to_tagtype(CVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
)), &tag_type
)) {
4481 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4482 CVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
)), i
));
4486 if (sys_acl_set_tag_type(the_entry
, tag_type
) == -1) {
4487 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4488 i
, strerror(errno
) ));
4492 /* Get the permset pointer from the new ACL entry. */
4493 if (sys_acl_get_permset(the_entry
, &the_permset
) == -1) {
4494 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4495 i
, strerror(errno
) ));
4499 /* Map from wire to permissions. */
4500 if (!unix_ex_wire_to_permset(conn
, CVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1), &the_permset
)) {
4501 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4502 CVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
) + 1), i
));
4506 /* Now apply to the new ACL entry. */
4507 if (sys_acl_set_permset(the_entry
, the_permset
) == -1) {
4508 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4509 i
, strerror(errno
) ));
4513 if (tag_type
== SMB_ACL_USER
) {
4514 uint32 uidval
= IVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
4515 uid_t uid
= (uid_t
)uidval
;
4516 if (sys_acl_set_qualifier(the_entry
,(void *)&uid
) == -1) {
4517 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4518 (unsigned int)uid
, i
, strerror(errno
) ));
4523 if (tag_type
== SMB_ACL_GROUP
) {
4524 uint32 gidval
= IVAL(pdata
,(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
4525 gid_t gid
= (uid_t
)gidval
;
4526 if (sys_acl_set_qualifier(the_entry
,(void *)&gid
) == -1) {
4527 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4528 (unsigned int)gid
, i
, strerror(errno
) ));
4538 if (the_acl
!= NULL
) {
4539 TALLOC_FREE(the_acl
);
4544 /****************************************************************************
4545 Calls from UNIX extensions - Default POSIX ACL set.
4546 If num_def_acls == 0 and not a directory just return. If it is a directory
4547 and num_def_acls == 0 then remove the default acl. Else set the default acl
4549 ****************************************************************************/
4551 bool set_unix_posix_default_acl(connection_struct
*conn
, const char *fname
, const SMB_STRUCT_STAT
*psbuf
,
4552 uint16 num_def_acls
, const char *pdata
)
4554 SMB_ACL_T def_acl
= NULL
;
4556 if (!S_ISDIR(psbuf
->st_ex_mode
)) {
4558 DEBUG(5,("set_unix_posix_default_acl: Can't set default ACL on non-directory file %s\n", fname
));
4566 if (!num_def_acls
) {
4567 /* Remove the default ACL. */
4568 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn
, fname
) == -1) {
4569 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4570 fname
, strerror(errno
) ));
4576 if ((def_acl
= create_posix_acl_from_wire(conn
, num_def_acls
,
4578 talloc_tos())) == NULL
) {
4582 if (SMB_VFS_SYS_ACL_SET_FILE(conn
, fname
, SMB_ACL_TYPE_DEFAULT
, def_acl
) == -1) {
4583 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4584 fname
, strerror(errno
) ));
4585 TALLOC_FREE(def_acl
);
4589 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n", fname
));
4590 TALLOC_FREE(def_acl
);
4594 /****************************************************************************
4595 Remove an ACL from a file. As we don't have acl_delete_entry() available
4596 we must read the current acl and copy all entries except MASK, USER and GROUP
4597 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4599 FIXME ! How does the share mask/mode fit into this.... ?
4600 ****************************************************************************/
4602 static bool remove_posix_acl(connection_struct
*conn
, files_struct
*fsp
, const char *fname
)
4604 SMB_ACL_T file_acl
= NULL
;
4605 int entry_id
= SMB_ACL_FIRST_ENTRY
;
4606 SMB_ACL_ENTRY_T entry
;
4608 /* Create a new ACL with only 3 entries, u/g/w. */
4609 SMB_ACL_T new_file_acl
= sys_acl_init(talloc_tos());
4610 SMB_ACL_ENTRY_T user_ent
= NULL
;
4611 SMB_ACL_ENTRY_T group_ent
= NULL
;
4612 SMB_ACL_ENTRY_T other_ent
= NULL
;
4614 if (new_file_acl
== NULL
) {
4615 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname
));
4619 /* Now create the u/g/w entries. */
4620 if (sys_acl_create_entry(&new_file_acl
, &user_ent
) == -1) {
4621 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4622 fname
, strerror(errno
) ));
4625 if (sys_acl_set_tag_type(user_ent
, SMB_ACL_USER_OBJ
) == -1) {
4626 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4627 fname
, strerror(errno
) ));
4631 if (sys_acl_create_entry(&new_file_acl
, &group_ent
) == -1) {
4632 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4633 fname
, strerror(errno
) ));
4636 if (sys_acl_set_tag_type(group_ent
, SMB_ACL_GROUP_OBJ
) == -1) {
4637 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4638 fname
, strerror(errno
) ));
4642 if (sys_acl_create_entry(&new_file_acl
, &other_ent
) == -1) {
4643 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4644 fname
, strerror(errno
) ));
4647 if (sys_acl_set_tag_type(other_ent
, SMB_ACL_OTHER
) == -1) {
4648 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4649 fname
, strerror(errno
) ));
4653 /* Get the current file ACL. */
4654 if (fsp
&& fsp
->fh
->fd
!= -1) {
4655 file_acl
= SMB_VFS_SYS_ACL_GET_FD(fsp
, talloc_tos());
4657 file_acl
= SMB_VFS_SYS_ACL_GET_FILE(conn
, fname
,
4658 SMB_ACL_TYPE_ACCESS
,
4662 if (file_acl
== NULL
) {
4663 /* This is only returned if an error occurred. Even for a file with
4664 no acl a u/g/w acl should be returned. */
4665 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4666 fname
, strerror(errno
) ));
4670 while ( sys_acl_get_entry(file_acl
, entry_id
, &entry
) == 1) {
4671 SMB_ACL_TAG_T tagtype
;
4672 SMB_ACL_PERMSET_T permset
;
4674 entry_id
= SMB_ACL_NEXT_ENTRY
;
4676 if (sys_acl_get_tag_type(entry
, &tagtype
) == -1) {
4677 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4678 fname
, strerror(errno
) ));
4682 if (sys_acl_get_permset(entry
, &permset
) == -1) {
4683 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4684 fname
, strerror(errno
) ));
4688 if (tagtype
== SMB_ACL_USER_OBJ
) {
4689 if (sys_acl_set_permset(user_ent
, permset
) == -1) {
4690 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4691 fname
, strerror(errno
) ));
4693 } else if (tagtype
== SMB_ACL_GROUP_OBJ
) {
4694 if (sys_acl_set_permset(group_ent
, permset
) == -1) {
4695 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4696 fname
, strerror(errno
) ));
4698 } else if (tagtype
== SMB_ACL_OTHER
) {
4699 if (sys_acl_set_permset(other_ent
, permset
) == -1) {
4700 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4701 fname
, strerror(errno
) ));
4706 /* Set the new empty file ACL. */
4707 if (fsp
&& fsp
->fh
->fd
!= -1) {
4708 if (SMB_VFS_SYS_ACL_SET_FD(fsp
, new_file_acl
) == -1) {
4709 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4710 fname
, strerror(errno
) ));
4714 if (SMB_VFS_SYS_ACL_SET_FILE(conn
, fname
, SMB_ACL_TYPE_ACCESS
, new_file_acl
) == -1) {
4715 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4716 fname
, strerror(errno
) ));
4726 TALLOC_FREE(file_acl
);
4729 TALLOC_FREE(new_file_acl
);
4734 /****************************************************************************
4735 Calls from UNIX extensions - POSIX ACL set.
4736 If num_def_acls == 0 then read/modify/write acl after removing all entries
4737 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4738 ****************************************************************************/
4740 bool set_unix_posix_acl(connection_struct
*conn
, files_struct
*fsp
, const char *fname
, uint16 num_acls
, const char *pdata
)
4742 SMB_ACL_T file_acl
= NULL
;
4745 /* Remove the ACL from the file. */
4746 return remove_posix_acl(conn
, fsp
, fname
);
4749 if ((file_acl
= create_posix_acl_from_wire(conn
, num_acls
,
4751 talloc_tos())) == NULL
) {
4755 if (fsp
&& fsp
->fh
->fd
!= -1) {
4756 /* The preferred way - use an open fd. */
4757 if (SMB_VFS_SYS_ACL_SET_FD(fsp
, file_acl
) == -1) {
4758 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4759 fname
, strerror(errno
) ));
4760 TALLOC_FREE(file_acl
);
4764 if (SMB_VFS_SYS_ACL_SET_FILE(conn
, fname
, SMB_ACL_TYPE_ACCESS
, file_acl
) == -1) {
4765 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4766 fname
, strerror(errno
) ));
4767 TALLOC_FREE(file_acl
);
4772 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname
));
4773 TALLOC_FREE(file_acl
);
4777 /********************************************************************
4778 Pull the NT ACL from a file on disk or the OpenEventlog() access
4779 check. Caller is responsible for freeing the returned security
4780 descriptor via TALLOC_FREE(). This is designed for dealing with
4781 user space access checks in smbd outside of the VFS. For example,
4782 checking access rights in OpenEventlog().
4784 Assume we are dealing with files (for now)
4785 ********************************************************************/
4787 struct security_descriptor
*get_nt_acl_no_snum( TALLOC_CTX
*ctx
, const char *fname
, uint32 security_info_wanted
)
4789 struct security_descriptor
*ret_sd
;
4790 connection_struct
*conn
;
4792 struct fd_handle fh
;
4794 TALLOC_CTX
*frame
= talloc_stackframe();
4796 conn
= talloc_zero(frame
, connection_struct
);
4798 DEBUG(0, ("talloc failed\n"));
4802 if (!(conn
->params
= talloc(conn
, struct share_params
))) {
4803 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
4808 conn
->params
->service
= -1;
4810 set_conn_connectpath(conn
, "/");
4812 if (!smbd_vfs_init(conn
)) {
4813 DEBUG(0,("get_nt_acl_no_snum: Unable to create a fake connection struct!\n"));
4819 ZERO_STRUCT( finfo
);
4822 finfo
.fnum
= FNUM_FIELD_INVALID
;
4827 status
= create_synthetic_smb_fname(frame
, fname
, NULL
, NULL
,
4829 if (!NT_STATUS_IS_OK(status
)) {
4835 if (!NT_STATUS_IS_OK(SMB_VFS_FGET_NT_ACL( &finfo
,
4836 security_info_wanted
,
4838 DEBUG(0,("get_nt_acl_no_snum: get_nt_acl returned zero.\n"));
4839 TALLOC_FREE(finfo
.fsp_name
);
4845 TALLOC_FREE(finfo
.fsp_name
);
4852 /* Stolen shamelessly from pvfs_default_acl() in source4 :-). */
4854 NTSTATUS
make_default_filesystem_acl(TALLOC_CTX
*ctx
,
4856 SMB_STRUCT_STAT
*psbuf
,
4857 struct security_descriptor
**ppdesc
)
4859 struct dom_sid owner_sid
, group_sid
;
4861 struct security_ace aces
[4];
4862 uint32_t access_mask
= 0;
4863 mode_t mode
= psbuf
->st_ex_mode
;
4864 struct security_acl
*new_dacl
= NULL
;
4867 DEBUG(10,("make_default_filesystem_acl: file %s mode = 0%o\n",
4870 uid_to_sid(&owner_sid
, psbuf
->st_ex_uid
);
4871 gid_to_sid(&group_sid
, psbuf
->st_ex_gid
);
4874 We provide up to 4 ACEs
4881 if (mode
& S_IRUSR
) {
4882 if (mode
& S_IWUSR
) {
4883 access_mask
|= SEC_RIGHTS_FILE_ALL
;
4885 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
4888 if (mode
& S_IWUSR
) {
4889 access_mask
|= SEC_RIGHTS_FILE_WRITE
| SEC_STD_DELETE
;
4892 init_sec_ace(&aces
[idx
],
4894 SEC_ACE_TYPE_ACCESS_ALLOWED
,
4900 if (mode
& S_IRGRP
) {
4901 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
4903 if (mode
& S_IWGRP
) {
4904 /* note that delete is not granted - this matches posix behaviour */
4905 access_mask
|= SEC_RIGHTS_FILE_WRITE
;
4908 init_sec_ace(&aces
[idx
],
4910 SEC_ACE_TYPE_ACCESS_ALLOWED
,
4917 if (mode
& S_IROTH
) {
4918 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
4920 if (mode
& S_IWOTH
) {
4921 access_mask
|= SEC_RIGHTS_FILE_WRITE
;
4924 init_sec_ace(&aces
[idx
],
4926 SEC_ACE_TYPE_ACCESS_ALLOWED
,
4932 init_sec_ace(&aces
[idx
],
4934 SEC_ACE_TYPE_ACCESS_ALLOWED
,
4935 SEC_RIGHTS_FILE_ALL
,
4939 new_dacl
= make_sec_acl(ctx
,
4945 return NT_STATUS_NO_MEMORY
;
4948 *ppdesc
= make_sec_desc(ctx
,
4949 SECURITY_DESCRIPTOR_REVISION_1
,
4950 SEC_DESC_SELF_RELATIVE
|SEC_DESC_DACL_PRESENT
,
4957 return NT_STATUS_NO_MEMORY
;
4959 return NT_STATUS_OK
;