2 Unix SMB/Netbios implementation.
4 SMB NT Security Descriptor / Unix permission conversion.
5 Copyright (C) Jeremy Allison 1994-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /****************************************************************************
25 Data structures representing the internal ACE format.
26 ****************************************************************************/
28 enum ace_owner
{UID_ACE
, GID_ACE
, WORLD_ACE
};
29 enum ace_attribute
{ALLOW_ACE
, DENY_ACE
}; /* Used for incoming NT ACLS. */
31 typedef union posix_id
{
37 typedef struct canon_ace
{
38 struct canon_ace
*next
, *prev
;
40 mode_t perms
; /* Only use S_I(R|W|X)USR mode bits here. */
42 enum ace_owner owner_type
;
43 enum ace_attribute attr
;
47 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
49 /****************************************************************************
50 Functions to manipulate the internal ACE format.
51 ****************************************************************************/
53 /****************************************************************************
54 Count a linked list of canonical ACE entries.
55 ****************************************************************************/
57 static size_t count_canon_ace_list( canon_ace
*list_head
)
62 for (ace
= list_head
; ace
; ace
= ace
->next
)
68 /****************************************************************************
69 Free a linked list of canonical ACE entries.
70 ****************************************************************************/
72 static void free_canon_ace_list( canon_ace
*list_head
)
75 canon_ace
*old_head
= list_head
;
76 DLIST_REMOVE(list_head
, list_head
);
81 /****************************************************************************
82 Function to duplicate a canon_ace entry.
83 ****************************************************************************/
85 static canon_ace
*dup_canon_ace( canon_ace
*src_ace
)
87 canon_ace
*dst_ace
= (canon_ace
*)malloc(sizeof(canon_ace
));
93 dst_ace
->prev
= dst_ace
->next
= NULL
;
97 /****************************************************************************
98 Print out a canon ace.
99 ****************************************************************************/
101 static void print_canon_ace(canon_ace
*pace
, int num
)
105 dbgtext( "canon_ace index %d. Type = %s ", num
, pace
->attr
== ALLOW_ACE
? "allow" : "deny" );
106 dbgtext( "SID = %s ", sid_to_string( str
, &pace
->trustee
));
107 if (pace
->owner_type
== UID_ACE
) {
108 char *u_name
= uidtoname(pace
->unix_ug
.uid
);
109 dbgtext( "uid %u (%s) ", (unsigned int)pace
->unix_ug
.uid
, u_name
);
110 } else if (pace
->owner_type
== GID_ACE
) {
111 char *g_name
= gidtoname(pace
->unix_ug
.gid
);
112 dbgtext( "gid %u (%s) ", (unsigned int)pace
->unix_ug
.gid
, g_name
);
115 switch (pace
->type
) {
117 dbgtext( "SMB_ACL_USER ");
119 case SMB_ACL_USER_OBJ
:
120 dbgtext( "SMB_ACL_USER_OBJ ");
123 dbgtext( "SMB_ACL_GROUP ");
125 case SMB_ACL_GROUP_OBJ
:
126 dbgtext( "SMB_ACL_GROUP_OBJ ");
129 dbgtext( "SMB_ACL_OTHER ");
133 dbgtext( "%c", pace
->perms
& S_IRUSR
? 'r' : '-');
134 dbgtext( "%c", pace
->perms
& S_IWUSR
? 'w' : '-');
135 dbgtext( "%c\n", pace
->perms
& S_IXUSR
? 'x' : '-');
138 /****************************************************************************
139 Print out a canon ace list.
140 ****************************************************************************/
142 static void print_canon_ace_list(const char *name
, canon_ace
*ace_list
)
146 if( DEBUGLVL( 10 )) {
147 dbgtext( "print_canon_ace_list: %s\n", name
);
148 for (;ace_list
; ace_list
= ace_list
->next
, count
++)
149 print_canon_ace(ace_list
, count
);
153 /****************************************************************************
154 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
155 ****************************************************************************/
157 static mode_t
convert_permset_to_mode_t(connection_struct
*conn
, SMB_ACL_PERMSET_T permset
)
161 ret
|= (conn
->vfs_ops
.sys_acl_get_perm(conn
, permset
, SMB_ACL_READ
) ? S_IRUSR
: 0);
162 ret
|= (conn
->vfs_ops
.sys_acl_get_perm(conn
, permset
, SMB_ACL_WRITE
) ? S_IWUSR
: 0);
163 ret
|= (conn
->vfs_ops
.sys_acl_get_perm(conn
, permset
, SMB_ACL_EXECUTE
) ? S_IXUSR
: 0);
168 /****************************************************************************
169 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
170 ****************************************************************************/
172 static mode_t
unix_perms_to_acl_perms(mode_t mode
, int r_mask
, int w_mask
, int x_mask
)
186 /****************************************************************************
187 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
188 an SMB_ACL_PERMSET_T.
189 ****************************************************************************/
191 static int map_acl_perms_to_permset(connection_struct
*conn
, mode_t mode
, SMB_ACL_PERMSET_T
*p_permset
)
193 if (conn
->vfs_ops
.sys_acl_clear_perms(conn
, *p_permset
) == -1)
195 if (mode
& S_IRUSR
) {
196 if (conn
->vfs_ops
.sys_acl_add_perm(conn
, *p_permset
, SMB_ACL_READ
) == -1)
199 if (mode
& S_IWUSR
) {
200 if (conn
->vfs_ops
.sys_acl_add_perm(conn
, *p_permset
, SMB_ACL_WRITE
) == -1)
203 if (mode
& S_IXUSR
) {
204 if (conn
->vfs_ops
.sys_acl_add_perm(conn
, *p_permset
, SMB_ACL_EXECUTE
) == -1)
209 /****************************************************************************
210 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
211 ****************************************************************************/
213 static void create_file_sids(SMB_STRUCT_STAT
*psbuf
, DOM_SID
*powner_sid
, DOM_SID
*pgroup_sid
)
215 uid_to_sid( powner_sid
, psbuf
->st_uid
);
216 gid_to_sid( pgroup_sid
, psbuf
->st_gid
);
219 /****************************************************************************
220 Merge aces with a common sid - if both are allow or deny, OR the permissions together and
221 delete the second one. If the first is deny, mask the permissions off and delete the allow
222 if the permissions become zero, delete the deny if the permissions are non zero.
223 ****************************************************************************/
225 static void merge_aces( canon_ace
**pp_list_head
)
227 canon_ace
*list_head
= *pp_list_head
;
228 canon_ace
*curr_ace_outer
;
229 canon_ace
*curr_ace_outer_next
;
232 * First, merge allow entries with identical SIDs, and deny entries
233 * with identical SIDs.
236 for (curr_ace_outer
= list_head
; curr_ace_outer
; curr_ace_outer
= curr_ace_outer_next
) {
238 canon_ace
*curr_ace_next
;
240 curr_ace_outer_next
= curr_ace_outer
->next
; /* Save the link in case we delete. */
242 for (curr_ace
= curr_ace_outer
->next
; curr_ace
; curr_ace
= curr_ace_next
) {
244 curr_ace_next
= curr_ace
->next
; /* Save the link in case of delete. */
246 if (sid_equal(&curr_ace
->trustee
, &curr_ace_outer
->trustee
) &&
247 (curr_ace
->attr
== curr_ace_outer
->attr
)) {
249 if( DEBUGLVL( 10 )) {
250 dbgtext("merge_aces: Merging ACE's\n");
251 print_canon_ace( curr_ace_outer
, 0);
252 print_canon_ace( curr_ace
, 0);
255 /* Merge two allow or two deny ACE's. */
257 curr_ace_outer
->perms
|= curr_ace
->perms
;
258 DLIST_REMOVE(list_head
, curr_ace
);
260 curr_ace_outer_next
= curr_ace_outer
->next
; /* We may have deleted the link. */
266 * Now go through and mask off allow permissions with deny permissions.
267 * We can delete either the allow or deny here as we know that each SID
268 * appears only once in the list.
271 for (curr_ace_outer
= list_head
; curr_ace_outer
; curr_ace_outer
= curr_ace_outer_next
) {
273 canon_ace
*curr_ace_next
;
275 curr_ace_outer_next
= curr_ace_outer
->next
; /* Save the link in case we delete. */
277 for (curr_ace
= curr_ace_outer
->next
; curr_ace
; curr_ace
= curr_ace_next
) {
279 curr_ace_next
= curr_ace
->next
; /* Save the link in case of delete. */
282 * Subtract ACE's with different entries. Due to the ordering constraints
283 * we've put on the ACL, we know the deny must be the first one.
286 if (sid_equal(&curr_ace
->trustee
, &curr_ace_outer
->trustee
) &&
287 (curr_ace_outer
->attr
== DENY_ACE
) && (curr_ace
->attr
== ALLOW_ACE
)) {
289 if( DEBUGLVL( 10 )) {
290 dbgtext("merge_aces: Masking ACE's\n");
291 print_canon_ace( curr_ace_outer
, 0);
292 print_canon_ace( curr_ace
, 0);
295 curr_ace
->perms
&= ~curr_ace_outer
->perms
;
297 if (curr_ace
->perms
== 0) {
300 * The deny overrides the allow. Remove the allow.
303 DLIST_REMOVE(list_head
, curr_ace
);
305 curr_ace_outer_next
= curr_ace_outer
->next
; /* We may have deleted the link. */
310 * Even after removing permissions, there
311 * are still allow permissions - delete the deny.
312 * It is safe to delete the deny here,
313 * as we are guarenteed by the deny first
314 * ordering that all the deny entries for
315 * this SID have already been merged into one
316 * before we can get to an allow ace.
319 DLIST_REMOVE(list_head
, curr_ace_outer
);
320 SAFE_FREE(curr_ace_outer
);
325 } /* end for curr_ace */
326 } /* end for curr_ace_outer */
328 /* We may have modified the list. */
330 *pp_list_head
= list_head
;
333 /****************************************************************************
334 Map canon_ace perms to permission bits NT.
335 The attr element is not used here - we only process deny entries on set,
336 not get. Deny entries are implicit on get with ace->perms = 0.
337 ****************************************************************************/
339 static SEC_ACCESS
map_canon_ace_perms(int *pacl_type
, DOM_SID
*powner_sid
, canon_ace
*ace
)
344 *pacl_type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
346 if ((ace
->perms
& ALL_ACE_PERMS
) == ALL_ACE_PERMS
) {
347 nt_mask
= UNIX_ACCESS_RWX
;
348 } else if ((ace
->perms
& ALL_ACE_PERMS
) == (mode_t
)0) {
349 nt_mask
= UNIX_ACCESS_NONE
;
351 nt_mask
|= ((ace
->perms
& S_IRUSR
) ? UNIX_ACCESS_R
: 0 );
352 nt_mask
|= ((ace
->perms
& S_IWUSR
) ? UNIX_ACCESS_W
: 0 );
353 nt_mask
|= ((ace
->perms
& S_IXUSR
) ? UNIX_ACCESS_X
: 0 );
356 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
357 (unsigned int)ace
->perms
, (unsigned int)nt_mask
));
359 init_sec_access(&sa
,nt_mask
);
363 /****************************************************************************
364 Map NT perms to a UNIX mode_t.
365 ****************************************************************************/
367 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES)
368 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA|FILE_WRITE_ATTRIBUTES)
369 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
371 static mode_t
map_nt_perms( SEC_ACCESS sec_access
, int type
)
377 if(sec_access
.mask
& GENERIC_ALL_ACCESS
)
378 mode
= S_IRUSR
|S_IWUSR
|S_IXUSR
;
380 mode
|= (sec_access
.mask
& (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IRUSR
: 0;
381 mode
|= (sec_access
.mask
& (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWUSR
: 0;
382 mode
|= (sec_access
.mask
& (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXUSR
: 0;
386 if(sec_access
.mask
& GENERIC_ALL_ACCESS
)
387 mode
= S_IRGRP
|S_IWGRP
|S_IXGRP
;
389 mode
|= (sec_access
.mask
& (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IRGRP
: 0;
390 mode
|= (sec_access
.mask
& (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWGRP
: 0;
391 mode
|= (sec_access
.mask
& (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXGRP
: 0;
395 if(sec_access
.mask
& GENERIC_ALL_ACCESS
)
396 mode
= S_IROTH
|S_IWOTH
|S_IXOTH
;
398 mode
|= (sec_access
.mask
& (GENERIC_READ_ACCESS
|FILE_SPECIFIC_READ_BITS
)) ? S_IROTH
: 0;
399 mode
|= (sec_access
.mask
& (GENERIC_WRITE_ACCESS
|FILE_SPECIFIC_WRITE_BITS
)) ? S_IWOTH
: 0;
400 mode
|= (sec_access
.mask
& (GENERIC_EXECUTE_ACCESS
|FILE_SPECIFIC_EXECUTE_BITS
)) ? S_IXOTH
: 0;
408 /****************************************************************************
409 Unpack a SEC_DESC into a UNIX owner and group.
410 ****************************************************************************/
412 static BOOL
unpack_nt_owners( int snum
, SMB_STRUCT_STAT
*psbuf
, uid_t
*puser
, gid_t
*pgrp
, uint32 security_info_sent
, SEC_DESC
*psd
)
416 enum SID_NAME_USE sid_type
;
421 if(security_info_sent
== 0) {
422 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
427 * Validate the owner and group SID's.
430 memset(&owner_sid
, '\0', sizeof(owner_sid
));
431 memset(&grp_sid
, '\0', sizeof(grp_sid
));
433 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
436 * Don't immediately fail if the owner sid cannot be validated.
437 * This may be a group chown only set.
440 if (security_info_sent
& OWNER_SECURITY_INFORMATION
) {
441 sid_copy(&owner_sid
, psd
->owner_sid
);
442 if (!sid_to_uid( &owner_sid
, puser
, &sid_type
)) {
443 if (lp_force_unknown_acl_user(snum
)) {
444 /* this allows take ownership to work reasonably */
445 extern struct current_user current_user
;
446 *puser
= current_user
.uid
;
448 DEBUG(3,("unpack_nt_owners: unable to validate owner sid for %s.\n",
449 sid_string_static(&owner_sid
)));
456 * Don't immediately fail if the group sid cannot be validated.
457 * This may be an owner chown only set.
460 if (security_info_sent
& GROUP_SECURITY_INFORMATION
) {
461 sid_copy(&grp_sid
, psd
->grp_sid
);
462 if (!sid_to_gid( &grp_sid
, pgrp
, &sid_type
)) {
463 if (lp_force_unknown_acl_user(snum
)) {
464 /* this allows take group ownership to work reasonably */
465 extern struct current_user current_user
;
466 *pgrp
= current_user
.gid
;
468 DEBUG(3,("unpack_nt_owners: unable to validate group sid.\n"));
474 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
479 /****************************************************************************
480 Ensure the enforced permissions for this share apply.
481 ****************************************************************************/
483 static void apply_default_perms(files_struct
*fsp
, canon_ace
*pace
, mode_t type
)
485 int snum
= SNUM(fsp
->conn
);
486 mode_t and_bits
= (mode_t
)0;
487 mode_t or_bits
= (mode_t
)0;
489 /* Get the initial bits to apply. */
491 if (fsp
->is_directory
) {
492 and_bits
= lp_dir_security_mask(snum
);
493 or_bits
= lp_force_dir_security_mode(snum
);
495 and_bits
= lp_security_mask(snum
);
496 or_bits
= lp_force_security_mode(snum
);
499 /* Now bounce them into the S_USR space. */
502 /* Ensure owner has read access. */
503 pace
->perms
|= S_IRUSR
;
504 if (fsp
->is_directory
)
505 pace
->perms
|= (S_IWUSR
|S_IXUSR
);
506 and_bits
= unix_perms_to_acl_perms(and_bits
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
507 or_bits
= unix_perms_to_acl_perms(or_bits
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
510 and_bits
= unix_perms_to_acl_perms(and_bits
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
511 or_bits
= unix_perms_to_acl_perms(or_bits
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
514 and_bits
= unix_perms_to_acl_perms(and_bits
, S_IROTH
, S_IWOTH
, S_IXOTH
);
515 or_bits
= unix_perms_to_acl_perms(or_bits
, S_IROTH
, S_IWOTH
, S_IXOTH
);
519 pace
->perms
= ((pace
->perms
& and_bits
)|or_bits
);
522 /****************************************************************************
523 Check if a given uid/SID is in a group gid/SID. This is probably very
524 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
525 ****************************************************************************/
527 static BOOL
uid_entry_in_group( canon_ace
*uid_ace
, canon_ace
*group_ace
)
529 extern DOM_SID global_sid_World
;
533 /* "Everyone" always matches every uid. */
535 if (sid_equal(&group_ace
->trustee
, &global_sid_World
))
538 fstrcpy(u_name
, uidtoname(uid_ace
->unix_ug
.uid
));
539 fstrcpy(g_name
, gidtoname(group_ace
->unix_ug
.gid
));
542 * Due to the winbind interfaces we need to do this via names,
546 return user_in_group_list(u_name
, g_name
);
549 /****************************************************************************
550 A well formed POSIX file or default ACL has at least 3 entries, a
551 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
552 In addition, the owner must always have at least read access.
553 When using this call on get_acl, the pst struct is valid and contains
554 the mode of the file. When using this call on set_acl, the pst struct has
555 been modified to have a mode containing the default for this file or directory
557 ****************************************************************************/
559 static BOOL
ensure_canon_entry_valid(canon_ace
**pp_ace
,
561 DOM_SID
*pfile_owner_sid
,
562 DOM_SID
*pfile_grp_sid
,
563 SMB_STRUCT_STAT
*pst
,
566 extern DOM_SID global_sid_World
;
568 BOOL got_user
= False
;
569 BOOL got_grp
= False
;
570 BOOL got_other
= False
;
571 canon_ace
*pace_other
= NULL
;
572 canon_ace
*pace_group
= NULL
;
574 for (pace
= *pp_ace
; pace
; pace
= pace
->next
) {
575 if (pace
->type
== SMB_ACL_USER_OBJ
) {
578 apply_default_perms(fsp
, pace
, S_IRUSR
);
581 } else if (pace
->type
== SMB_ACL_GROUP_OBJ
) {
584 * Ensure create mask/force create mode is respected on set.
588 apply_default_perms(fsp
, pace
, S_IRGRP
);
592 } else if (pace
->type
== SMB_ACL_OTHER
) {
595 * Ensure create mask/force create mode is respected on set.
599 apply_default_perms(fsp
, pace
, S_IROTH
);
606 if ((pace
= (canon_ace
*)malloc(sizeof(canon_ace
))) == NULL
) {
607 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
612 pace
->type
= SMB_ACL_USER_OBJ
;
613 pace
->owner_type
= UID_ACE
;
614 pace
->unix_ug
.uid
= pst
->st_uid
;
615 pace
->trustee
= *pfile_owner_sid
;
616 pace
->attr
= ALLOW_ACE
;
619 /* If we only got an "everyone" perm, just use that. */
620 if (!got_grp
&& got_other
)
621 pace
->perms
= pace_other
->perms
;
622 else if (got_grp
&& uid_entry_in_group(pace
, pace_group
))
623 pace
->perms
= pace_group
->perms
;
625 pace
->perms
= unix_perms_to_acl_perms(pst
->st_mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
626 apply_default_perms(fsp
, pace
, S_IRUSR
);
628 pace
->perms
= unix_perms_to_acl_perms(pst
->st_mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
631 DLIST_ADD(*pp_ace
, pace
);
635 if ((pace
= (canon_ace
*)malloc(sizeof(canon_ace
))) == NULL
) {
636 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
641 pace
->type
= SMB_ACL_GROUP_OBJ
;
642 pace
->owner_type
= GID_ACE
;
643 pace
->unix_ug
.uid
= pst
->st_gid
;
644 pace
->trustee
= *pfile_grp_sid
;
645 pace
->attr
= ALLOW_ACE
;
647 /* If we only got an "everyone" perm, just use that. */
649 pace
->perms
= pace_other
->perms
;
651 pace
->perms
= unix_perms_to_acl_perms(pst
->st_mode
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
652 apply_default_perms(fsp
, pace
, S_IRGRP
);
654 pace
->perms
= unix_perms_to_acl_perms(pst
->st_mode
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
657 DLIST_ADD(*pp_ace
, pace
);
661 if ((pace
= (canon_ace
*)malloc(sizeof(canon_ace
))) == NULL
) {
662 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
667 pace
->type
= SMB_ACL_OTHER
;
668 pace
->owner_type
= WORLD_ACE
;
669 pace
->unix_ug
.world
= -1;
670 pace
->trustee
= global_sid_World
;
671 pace
->attr
= ALLOW_ACE
;
672 pace
->perms
= unix_perms_to_acl_perms(pst
->st_mode
, S_IROTH
, S_IWOTH
, S_IXOTH
);
673 apply_default_perms(fsp
, pace
, S_IROTH
);
675 DLIST_ADD(*pp_ace
, pace
);
681 /****************************************************************************
682 Unpack a SEC_DESC into two canonical ace lists.
683 ****************************************************************************/
685 static BOOL
create_canon_ace_lists(files_struct
*fsp
,
686 DOM_SID
*pfile_owner_sid
,
687 DOM_SID
*pfile_grp_sid
,
688 canon_ace
**ppfile_ace
, canon_ace
**ppdir_ace
,
691 extern DOM_SID global_sid_World
;
692 extern struct generic_mapping file_generic_mapping
;
693 BOOL all_aces_are_inherit_only
= (fsp
->is_directory
? True
: False
);
694 canon_ace
*file_ace
= NULL
;
695 canon_ace
*dir_ace
= NULL
;
696 canon_ace
*tmp_ace
= NULL
;
697 canon_ace
*current_ace
= NULL
;
698 BOOL got_dir_allow
= False
;
699 BOOL got_file_allow
= False
;
706 * Convert the incoming ACL into a more regular form.
709 for(i
= 0; i
< dacl
->num_aces
; i
++) {
710 SEC_ACE
*psa
= &dacl
->ace
[i
];
712 if((psa
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED
) && (psa
->type
!= SEC_ACE_TYPE_ACCESS_DENIED
)) {
713 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
718 * The security mask may be UNIX_ACCESS_NONE which should map into
719 * no permissions (we overload the WRITE_OWNER bit for this) or it
720 * should be one of the ALL/EXECUTE/READ/WRITE bits. Arrange for this
721 * to be so. Any other bits override the UNIX_ACCESS_NONE bit.
725 * Convert GENERIC bits to specific bits.
728 se_map_generic(&psa
->info
.mask
, &file_generic_mapping
);
730 psa
->info
.mask
&= (UNIX_ACCESS_NONE
|FILE_ALL_ACCESS
);
732 if(psa
->info
.mask
!= UNIX_ACCESS_NONE
)
733 psa
->info
.mask
&= ~UNIX_ACCESS_NONE
;
737 * Deal with the fact that NT 4.x re-writes the canonical format
738 * that we return for default ACLs. If a directory ACE is identical
739 * to a inherited directory ACE then NT changes the bits so that the
740 * first ACE is set to OI|IO and the second ACE for this SID is set
741 * to CI. We need to repair this. JRA.
744 for(i
= 0; i
< dacl
->num_aces
; i
++) {
745 SEC_ACE
*psa1
= &dacl
->ace
[i
];
747 for (j
= i
+ 1; j
< dacl
->num_aces
; j
++) {
748 SEC_ACE
*psa2
= &dacl
->ace
[j
];
750 if (psa1
->info
.mask
!= psa2
->info
.mask
)
753 if (!sid_equal(&psa1
->trustee
, &psa2
->trustee
))
757 * Ok - permission bits and SIDs are equal.
758 * Check if flags were re-written.
761 if (psa1
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
763 psa1
->flags
|= (psa2
->flags
& (SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
));
764 psa2
->flags
&= ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
);
766 } else if (psa2
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
768 psa2
->flags
|= (psa1
->flags
& (SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
));
769 psa1
->flags
&= ~(SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_OBJECT_INHERIT
);
775 for(i
= 0; i
< dacl
->num_aces
; i
++) {
776 enum SID_NAME_USE sid_type
;
777 SEC_ACE
*psa
= &dacl
->ace
[i
];
780 * Ignore non-mappable SIDs (NT Authority, BUILTIN etc).
783 if (non_mappable_sid(&psa
->trustee
)) {
785 DEBUG(10,("create_canon_ace_lists: ignoring non-mappable SID %s\n",
786 sid_to_string(str
, &psa
->trustee
) ));
791 * Create a cannon_ace entry representing this NT DACL ACE.
794 if ((current_ace
= (canon_ace
*)malloc(sizeof(canon_ace
))) == NULL
) {
795 free_canon_ace_list(file_ace
);
796 free_canon_ace_list(dir_ace
);
797 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
801 ZERO_STRUCTP(current_ace
);
803 sid_copy(¤t_ace
->trustee
, &psa
->trustee
);
806 * Try and work out if the SID is a user or group
807 * as we need to flag these differently for POSIX.
810 if( sid_equal(¤t_ace
->trustee
, &global_sid_World
)) {
811 current_ace
->owner_type
= WORLD_ACE
;
812 current_ace
->unix_ug
.world
= -1;
813 } else if (sid_to_uid( ¤t_ace
->trustee
, ¤t_ace
->unix_ug
.uid
, &sid_type
)) {
814 current_ace
->owner_type
= UID_ACE
;
815 } else if (sid_to_gid( ¤t_ace
->trustee
, ¤t_ace
->unix_ug
.gid
, &sid_type
)) {
816 current_ace
->owner_type
= GID_ACE
;
820 free_canon_ace_list(file_ace
);
821 free_canon_ace_list(dir_ace
);
822 DEBUG(0,("create_canon_ace_lists: unable to map SID %s to uid or gid.\n",
823 sid_to_string(str
, ¤t_ace
->trustee
) ));
824 SAFE_FREE(current_ace
);
829 * Map the given NT permissions into a UNIX mode_t containing only
830 * S_I(R|W|X)USR bits.
833 current_ace
->perms
|= map_nt_perms( psa
->info
, S_IRUSR
);
834 current_ace
->attr
= (psa
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) ? ALLOW_ACE
: DENY_ACE
;
837 * Now note what kind of a POSIX ACL this should map to.
840 if(sid_equal(¤t_ace
->trustee
, pfile_owner_sid
)) {
842 current_ace
->type
= SMB_ACL_USER_OBJ
;
844 } else if( sid_equal(¤t_ace
->trustee
, pfile_grp_sid
)) {
846 current_ace
->type
= SMB_ACL_GROUP_OBJ
;
848 } else if( sid_equal(¤t_ace
->trustee
, &global_sid_World
)) {
850 current_ace
->type
= SMB_ACL_OTHER
;
854 * Could be a SMB_ACL_USER or SMB_ACL_GROUP. Check by
855 * looking at owner_type.
858 current_ace
->type
= (current_ace
->owner_type
== UID_ACE
) ? SMB_ACL_USER
: SMB_ACL_GROUP
;
862 * Now add the created ace to either the file list, the directory
863 * list, or both. We *MUST* preserve the order here (hence we use
864 * DLIST_ADD_END) as NT ACLs are order dependent.
867 if (fsp
->is_directory
) {
870 * We can only add to the default POSIX ACE list if the ACE is
871 * designed to be inherited by both files and directories.
874 if ((psa
->flags
& (SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
)) ==
875 (SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
877 DLIST_ADD_END(dir_ace
, current_ace
, tmp_ace
);
880 * Note if this was an allow ace. We can't process
881 * any further deny ace's after this.
884 if (current_ace
->attr
== ALLOW_ACE
)
885 got_dir_allow
= True
;
887 if ((current_ace
->attr
== DENY_ACE
) && got_dir_allow
) {
888 DEBUG(0,("create_canon_ace_lists: malformed ACL in inheritable ACL ! \
889 Deny entry after Allow entry. Failing to set on file %s.\n", fsp
->fsp_name
));
890 free_canon_ace_list(file_ace
);
891 free_canon_ace_list(dir_ace
);
892 SAFE_FREE(current_ace
);
896 if( DEBUGLVL( 10 )) {
897 dbgtext("create_canon_ace_lists: adding dir ACL:\n");
898 print_canon_ace( current_ace
, 0);
902 * If this is not an inherit only ACE we need to add a duplicate
906 if (!(psa
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
)) {
907 canon_ace
*dup_ace
= dup_canon_ace(current_ace
);
910 DEBUG(0,("create_canon_ace_lists: malloc fail !\n"));
911 free_canon_ace_list(file_ace
);
912 free_canon_ace_list(dir_ace
);
916 current_ace
= dup_ace
;
924 * Only add to the file ACL if not inherit only.
927 if (!(psa
->flags
& SEC_ACE_FLAG_INHERIT_ONLY
)) {
928 DLIST_ADD_END(file_ace
, current_ace
, tmp_ace
);
931 * Note if this was an allow ace. We can't process
932 * any further deny ace's after this.
935 if (current_ace
->attr
== ALLOW_ACE
)
936 got_file_allow
= True
;
938 if ((current_ace
->attr
== DENY_ACE
) && got_file_allow
) {
939 DEBUG(0,("create_canon_ace_lists: malformed ACL in file ACL ! \
940 Deny entry after Allow entry. Failing to set on file %s.\n", fsp
->fsp_name
));
941 free_canon_ace_list(file_ace
);
942 free_canon_ace_list(dir_ace
);
943 SAFE_FREE(current_ace
);
947 if( DEBUGLVL( 10 )) {
948 dbgtext("create_canon_ace_lists: adding file ACL:\n");
949 print_canon_ace( current_ace
, 0);
951 all_aces_are_inherit_only
= False
;
956 * Free if ACE was not added.
959 SAFE_FREE(current_ace
);
962 if (fsp
->is_directory
&& all_aces_are_inherit_only
) {
964 * Windows 2000 is doing one of these weird 'inherit acl'
965 * traverses to conserve NTFS ACL resources. Just pretend
966 * there was no DACL sent. JRA.
969 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
970 free_canon_ace_list(file_ace
);
971 free_canon_ace_list(dir_ace
);
976 *ppfile_ace
= file_ace
;
977 *ppdir_ace
= dir_ace
;
982 /****************************************************************************
983 ASCII art time again... JRA :-).
985 We have 3 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
986 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
987 entries). Secondly, the merge code has ensured that all duplicate SID entries for
988 allow or deny have been merged, so the same SID can only appear once in the deny
989 list or once in the allow list.
991 We then process as follows :
993 ---------------------------------------------------------------------------
994 First pass - look for a Everyone DENY entry.
996 If it is deny all (rwx) trunate the list at this point.
997 Else, walk the list from this point and use the deny permissions of this
998 entry as a mask on all following allow entries. Finally, delete
999 the Everyone DENY entry (we have applied it to everything possible).
1001 In addition, in this pass we remove any DENY entries that have
1002 no permissions (ie. they are a DENY nothing).
1003 ---------------------------------------------------------------------------
1004 Second pass - only deal with deny user entries.
1006 DENY user1 (perms XXX)
1009 for all following allow group entries where user1 is in group
1010 new_perms |= group_perms;
1012 user1 entry perms = new_perms & ~ XXX;
1014 Convert the deny entry to an allow entry with the new perms and
1015 push to the end of the list. Note if the user was in no groups
1016 this maps to a specific allow nothing entry for this user.
1018 The common case from the NT ACL choser (userX deny all) is
1019 optimised so we don't do the group lookup - we just map to
1020 an allow nothing entry.
1022 What we're doing here is inferring the allow permissions the
1023 person setting the ACE on user1 wanted by looking at the allow
1024 permissions on the groups the user is currently in. This will
1025 be a snapshot, depending on group membership but is the best
1026 we can do and has the advantage of failing closed rather than
1028 ---------------------------------------------------------------------------
1029 Third pass - only deal with deny group entries.
1031 DENY group1 (perms XXX)
1033 for all following allow user entries where user is in group1
1034 user entry perms = user entry perms & ~ XXX;
1036 If there is a group Everyone allow entry with permissions YYY,
1037 convert the group1 entry to an allow entry and modify its
1040 new_perms = YYY & ~ XXX
1042 and push to the end of the list.
1044 If there is no group Everyone allow entry then convert the
1045 group1 entry to a allow nothing entry and push to the end of the list.
1047 Note that the common case from the NT ACL choser (groupX deny all)
1048 cannot be optimised here as we need to modify user entries who are
1049 in the group to change them to a deny all also.
1051 What we're doing here is modifying the allow permissions of
1052 user entries (which are more specific in POSIX ACLs) to mask
1053 out the explicit deny set on the group they are in. This will
1054 be a snapshot depending on current group membership but is the
1055 best we can do and has the advantage of failing closed rather
1057 ---------------------------------------------------------------------------
1059 Note we *MUST* do the deny user pass first as this will convert deny user
1060 entries into allow user entries which can then be processed by the deny
1063 The above algorithm took a *lot* of thinking about - hence this
1064 explaination :-). JRA.
1065 ****************************************************************************/
1067 /****************************************************************************
1068 Process a canon_ace list entries. This is very complex code. We need
1069 to go through and remove the "deny" permissions from any allow entry that matches
1070 the id of this entry. We have already refused any NT ACL that wasn't in correct
1071 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
1072 we just remove it (to fail safe). We have already removed any duplicate ace
1073 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
1075 ****************************************************************************/
1077 static void process_deny_list( canon_ace
**pp_ace_list
)
1079 extern DOM_SID global_sid_World
;
1080 canon_ace
*ace_list
= *pp_ace_list
;
1081 canon_ace
*curr_ace
= NULL
;
1082 canon_ace
*curr_ace_next
= NULL
;
1084 /* Pass 1 above - look for an Everyone, deny entry. */
1086 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
1087 canon_ace
*allow_ace_p
;
1089 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
1091 if (curr_ace
->attr
!= DENY_ACE
)
1094 if (curr_ace
->perms
== (mode_t
)0) {
1096 /* Deny nothing entry - delete. */
1098 DLIST_REMOVE(ace_list
, curr_ace
);
1102 if (!sid_equal(&curr_ace
->trustee
, &global_sid_World
))
1105 /* JRATEST - assert. */
1106 SMB_ASSERT(curr_ace
->owner_type
== WORLD_ACE
);
1108 if (curr_ace
->perms
== ALL_ACE_PERMS
) {
1111 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
1112 * list at this point including this entry.
1115 canon_ace
*prev_entry
= curr_ace
->prev
;
1117 free_canon_ace_list( curr_ace
);
1119 prev_entry
->next
= NULL
;
1121 /* We deleted the entire list. */
1127 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
1130 * Only mask off allow entries.
1133 if (allow_ace_p
->attr
!= ALLOW_ACE
)
1136 allow_ace_p
->perms
&= ~curr_ace
->perms
;
1140 * Now it's been applied, remove it.
1143 DLIST_REMOVE(ace_list
, curr_ace
);
1146 /* Pass 2 above - deal with deny user entries. */
1148 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
1149 mode_t new_perms
= (mode_t
)0;
1150 canon_ace
*allow_ace_p
;
1153 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
1155 if (curr_ace
->attr
!= DENY_ACE
)
1158 if (curr_ace
->owner_type
!= UID_ACE
)
1161 if (curr_ace
->perms
== ALL_ACE_PERMS
) {
1164 * Optimisation - this is a deny everything to this user.
1165 * Convert to an allow nothing and push to the end of the list.
1168 curr_ace
->attr
= ALLOW_ACE
;
1169 curr_ace
->perms
= (mode_t
)0;
1170 DLIST_DEMOTE(ace_list
, curr_ace
, tmp_ace
);
1174 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
1176 if (allow_ace_p
->attr
!= ALLOW_ACE
)
1179 /* We process GID_ACE and WORLD_ACE entries only. */
1181 if (allow_ace_p
->owner_type
== UID_ACE
)
1184 if (uid_entry_in_group( curr_ace
, allow_ace_p
))
1185 new_perms
|= allow_ace_p
->perms
;
1189 * Convert to a allow entry, modify the perms and push to the end
1193 curr_ace
->attr
= ALLOW_ACE
;
1194 curr_ace
->perms
= (new_perms
& ~curr_ace
->perms
);
1195 DLIST_DEMOTE(ace_list
, curr_ace
, tmp_ace
);
1198 /* Pass 3 above - deal with deny group entries. */
1200 for (curr_ace
= ace_list
; curr_ace
; curr_ace
= curr_ace_next
) {
1202 canon_ace
*allow_ace_p
;
1203 canon_ace
*allow_everyone_p
= NULL
;
1205 curr_ace_next
= curr_ace
->next
; /* So we can't lose the link. */
1207 if (curr_ace
->attr
!= DENY_ACE
)
1210 if (curr_ace
->owner_type
!= GID_ACE
)
1213 for (allow_ace_p
= curr_ace
->next
; allow_ace_p
; allow_ace_p
= allow_ace_p
->next
) {
1215 if (allow_ace_p
->attr
!= ALLOW_ACE
)
1218 /* Store a pointer to the Everyone allow, if it exists. */
1219 if (allow_ace_p
->owner_type
== WORLD_ACE
)
1220 allow_everyone_p
= allow_ace_p
;
1222 /* We process UID_ACE entries only. */
1224 if (allow_ace_p
->owner_type
!= UID_ACE
)
1227 /* Mask off the deny group perms. */
1229 if (uid_entry_in_group( allow_ace_p
, curr_ace
))
1230 allow_ace_p
->perms
&= ~curr_ace
->perms
;
1234 * Convert the deny to an allow with the correct perms and
1235 * push to the end of the list.
1238 curr_ace
->attr
= ALLOW_ACE
;
1239 if (allow_everyone_p
)
1240 curr_ace
->perms
= allow_everyone_p
->perms
& ~curr_ace
->perms
;
1242 curr_ace
->perms
= (mode_t
)0;
1243 DLIST_DEMOTE(ace_list
, curr_ace
, tmp_ace
);
1247 *pp_ace_list
= ace_list
;
1250 /****************************************************************************
1251 Create a default mode that will be used if a security descriptor entry has
1252 no user/group/world entries.
1253 ****************************************************************************/
1255 static mode_t
create_default_mode(files_struct
*fsp
, BOOL interitable_mode
)
1257 int snum
= SNUM(fsp
->conn
);
1258 mode_t and_bits
= (mode_t
)0;
1259 mode_t or_bits
= (mode_t
)0;
1260 mode_t mode
= interitable_mode
? unix_mode( fsp
->conn
, FILE_ATTRIBUTE_ARCHIVE
, fsp
->fsp_name
) : S_IRUSR
;
1262 if (fsp
->is_directory
)
1263 mode
|= (S_IWUSR
|S_IXUSR
);
1266 * Now AND with the create mode/directory mode bits then OR with the
1267 * force create mode/force directory mode bits.
1270 if (fsp
->is_directory
) {
1271 and_bits
= lp_dir_security_mask(snum
);
1272 or_bits
= lp_force_dir_security_mode(snum
);
1274 and_bits
= lp_security_mask(snum
);
1275 or_bits
= lp_force_security_mode(snum
);
1278 return ((mode
& and_bits
)|or_bits
);
1281 /****************************************************************************
1282 Unpack a SEC_DESC into two canonical ace lists. We don't depend on this
1284 ****************************************************************************/
1286 static BOOL
unpack_canon_ace(files_struct
*fsp
,
1287 SMB_STRUCT_STAT
*pst
,
1288 DOM_SID
*pfile_owner_sid
,
1289 DOM_SID
*pfile_grp_sid
,
1290 canon_ace
**ppfile_ace
, canon_ace
**ppdir_ace
,
1291 uint32 security_info_sent
, SEC_DESC
*psd
)
1293 canon_ace
*file_ace
= NULL
;
1294 canon_ace
*dir_ace
= NULL
;
1299 if(security_info_sent
== 0) {
1300 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
1305 * If no DACL then this is a chown only security descriptor.
1308 if(!(security_info_sent
& DACL_SECURITY_INFORMATION
) || !psd
->dacl
)
1312 * Now go through the DACL and create the canon_ace lists.
1315 if (!create_canon_ace_lists( fsp
, pfile_owner_sid
, pfile_grp_sid
,
1316 &file_ace
, &dir_ace
, psd
->dacl
))
1319 if ((file_ace
== NULL
) && (dir_ace
== NULL
)) {
1320 /* W2K traverse DACL set - ignore. */
1325 * Go through the canon_ace list and merge entries
1326 * belonging to identical users of identical allow or deny type.
1327 * We can do this as all deny entries come first, followed by
1328 * all allow entries (we have mandated this before accepting this acl).
1331 print_canon_ace_list( "file ace - before merge", file_ace
);
1332 merge_aces( &file_ace
);
1334 print_canon_ace_list( "dir ace - before merge", dir_ace
);
1335 merge_aces( &dir_ace
);
1338 * NT ACLs are order dependent. Go through the acl lists and
1339 * process DENY entries by masking the allow entries.
1342 print_canon_ace_list( "file ace - before deny", file_ace
);
1343 process_deny_list( &file_ace
);
1345 print_canon_ace_list( "dir ace - before deny", dir_ace
);
1346 process_deny_list( &dir_ace
);
1349 * A well formed POSIX file or default ACL has at least 3 entries, a
1350 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
1351 * and optionally a mask entry. Ensure this is the case.
1354 print_canon_ace_list( "file ace - before valid", file_ace
);
1357 * A default 3 element mode entry for a file should be r-- --- ---.
1358 * A default 3 element mode entry for a directory should be rwx --- ---.
1361 pst
->st_mode
= create_default_mode(fsp
, False
);
1363 if (!ensure_canon_entry_valid(&file_ace
, fsp
, pfile_owner_sid
, pfile_grp_sid
, pst
, True
)) {
1364 free_canon_ace_list(file_ace
);
1365 free_canon_ace_list(dir_ace
);
1369 print_canon_ace_list( "dir ace - before valid", dir_ace
);
1372 * A default inheritable 3 element mode entry for a directory should be the
1373 * mode Samba will use to create a file within. Ensure user rwx bits are set if
1377 pst
->st_mode
= create_default_mode(fsp
, True
);
1379 if (!ensure_canon_entry_valid(&dir_ace
, fsp
, pfile_owner_sid
, pfile_grp_sid
, pst
, True
)) {
1380 free_canon_ace_list(file_ace
);
1381 free_canon_ace_list(dir_ace
);
1385 print_canon_ace_list( "file ace - return", file_ace
);
1386 print_canon_ace_list( "dir ace - return", dir_ace
);
1388 *ppfile_ace
= file_ace
;
1389 *ppdir_ace
= dir_ace
;
1394 /******************************************************************************
1395 When returning permissions, try and fit NT display
1396 semantics if possible. Note the the canon_entries here must have been malloced.
1397 The list format should be - first entry = owner, followed by group and other user
1398 entries, last entry = other.
1400 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
1401 are not ordered, and match on the most specific entry rather than walking a list,
1402 then a simple POSIX permission of rw-r--r-- should really map to 6 entries,
1404 Entry 0: owner : deny all except read and write.
1405 Entry 1: group : deny all except read.
1406 Entry 2: Everyone : deny all except read.
1407 Entry 3: owner : allow read and write.
1408 Entry 4: group : allow read.
1409 Entry 5: Everyone : allow read.
1411 But NT cannot display this in their ACL editor !
1412 ********************************************************************************/
1414 static void arrange_posix_perms( char *filename
, canon_ace
**pp_list_head
)
1416 canon_ace
*list_head
= *pp_list_head
;
1417 canon_ace
*owner_ace
= NULL
;
1418 canon_ace
*other_ace
= NULL
;
1419 canon_ace
*ace
= NULL
;
1421 for (ace
= list_head
; ace
; ace
= ace
->next
) {
1422 if (ace
->type
== SMB_ACL_USER_OBJ
)
1424 else if (ace
->type
== SMB_ACL_OTHER
) {
1425 /* Last ace - this is "other" */
1430 if (!owner_ace
|| !other_ace
) {
1431 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
1437 * The POSIX algorithm applies to owner first, and other last,
1438 * so ensure they are arranged in this order.
1442 DLIST_PROMOTE(list_head
, owner_ace
);
1446 DLIST_DEMOTE(list_head
, other_ace
, ace
);
1449 /* We have probably changed the head of the list. */
1451 *pp_list_head
= list_head
;
1454 /****************************************************************************
1455 Create a linked list of canonical ACE entries.
1456 ****************************************************************************/
1458 static canon_ace
*canonicalise_acl( files_struct
*fsp
, SMB_ACL_T posix_acl
, SMB_STRUCT_STAT
*psbuf
,
1459 DOM_SID
*powner
, DOM_SID
*pgroup
)
1461 extern DOM_SID global_sid_World
;
1462 connection_struct
*conn
= fsp
->conn
;
1463 mode_t acl_mask
= (S_IRUSR
|S_IWUSR
|S_IXUSR
);
1464 canon_ace
*list_head
= NULL
;
1465 canon_ace
*ace
= NULL
;
1466 canon_ace
*next_ace
= NULL
;
1467 int entry_id
= SMB_ACL_FIRST_ENTRY
;
1468 SMB_ACL_ENTRY_T entry
;
1471 while ( posix_acl
&& (conn
->vfs_ops
.sys_acl_get_entry(conn
, posix_acl
, entry_id
, &entry
) == 1)) {
1472 SMB_ACL_TAG_T tagtype
;
1473 SMB_ACL_PERMSET_T permset
;
1476 enum ace_owner owner_type
;
1479 if (entry_id
== SMB_ACL_FIRST_ENTRY
)
1480 entry_id
= SMB_ACL_NEXT_ENTRY
;
1482 /* Is this a MASK entry ? */
1483 if (conn
->vfs_ops
.sys_acl_get_tag_type(conn
, entry
, &tagtype
) == -1)
1486 if (conn
->vfs_ops
.sys_acl_get_permset(conn
, entry
, &permset
) == -1)
1489 /* Decide which SID to use based on the ACL type. */
1491 case SMB_ACL_USER_OBJ
:
1492 /* Get the SID from the owner. */
1493 uid_to_sid( &sid
, psbuf
->st_uid
);
1494 unix_ug
.uid
= psbuf
->st_uid
;
1495 owner_type
= UID_ACE
;
1499 uid_t
*puid
= (uid_t
*)conn
->vfs_ops
.sys_acl_get_qualifier(conn
, entry
);
1501 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
1504 uid_to_sid( &sid
, *puid
);
1505 unix_ug
.uid
= *puid
;
1506 owner_type
= UID_ACE
;
1507 conn
->vfs_ops
.sys_acl_free_qualifier(conn
, (void *)puid
,tagtype
);
1510 case SMB_ACL_GROUP_OBJ
:
1511 /* Get the SID from the owning group. */
1512 gid_to_sid( &sid
, psbuf
->st_gid
);
1513 unix_ug
.gid
= psbuf
->st_gid
;
1514 owner_type
= GID_ACE
;
1518 gid_t
*pgid
= (gid_t
*)conn
->vfs_ops
.sys_acl_get_qualifier(conn
, entry
);
1520 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
1523 gid_to_sid( &sid
, *pgid
);
1524 unix_ug
.gid
= *pgid
;
1525 owner_type
= GID_ACE
;
1526 conn
->vfs_ops
.sys_acl_free_qualifier(conn
, (void *)pgid
,tagtype
);
1530 acl_mask
= convert_permset_to_mode_t(conn
, permset
);
1531 continue; /* Don't count the mask as an entry. */
1533 /* Use the Everyone SID */
1534 sid
= global_sid_World
;
1536 owner_type
= WORLD_ACE
;
1539 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype
));
1544 * Add this entry to the list.
1547 if ((ace
= (canon_ace
*)malloc(sizeof(canon_ace
))) == NULL
)
1551 ace
->type
= tagtype
;
1552 ace
->perms
= convert_permset_to_mode_t(conn
, permset
);
1553 ace
->attr
= ALLOW_ACE
;
1555 ace
->unix_ug
= unix_ug
;
1556 ace
->owner_type
= owner_type
;
1558 DLIST_ADD(list_head
, ace
);
1562 * This next call will ensure we have at least a user/group/world set.
1565 if (!ensure_canon_entry_valid(&list_head
, fsp
, powner
, pgroup
, psbuf
, False
))
1568 arrange_posix_perms(fsp
->fsp_name
,&list_head
);
1571 * Now go through the list, masking the permissions with the
1572 * acl_mask. Ensure all DENY Entries are at the start of the list.
1575 DEBUG(10,("canonicalise_acl: ace entries before arrange :\n"));
1577 for ( ace_count
= 0, ace
= list_head
; ace
; ace
= next_ace
, ace_count
++) {
1578 next_ace
= ace
->next
;
1580 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
1581 if (ace
->type
!= SMB_ACL_OTHER
&& ace
->type
!= SMB_ACL_USER_OBJ
)
1582 ace
->perms
&= acl_mask
;
1584 if (ace
->perms
== 0) {
1585 DLIST_PROMOTE(list_head
, ace
);
1588 if( DEBUGLVL( 10 ) ) {
1589 print_canon_ace(ace
, ace_count
);
1593 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", list_head
);
1599 free_canon_ace_list(list_head
);
1603 /****************************************************************************
1604 Attempt to apply an ACL to a file or directory.
1605 ****************************************************************************/
1607 static BOOL
set_canon_ace_list(files_struct
*fsp
, canon_ace
*the_ace
, BOOL default_ace
, BOOL
*pacl_set_support
)
1609 connection_struct
*conn
= fsp
->conn
;
1611 SMB_ACL_T the_acl
= conn
->vfs_ops
.sys_acl_init(conn
, (int)count_canon_ace_list(the_ace
) + 1);
1614 SMB_ACL_ENTRY_T mask_entry
;
1615 SMB_ACL_PERMSET_T mask_permset
;
1616 SMB_ACL_TYPE_T the_acl_type
= (default_ace
? SMB_ACL_TYPE_DEFAULT
: SMB_ACL_TYPE_ACCESS
);
1618 if (the_acl
== NULL
) {
1620 if (errno
!= ENOSYS
) {
1622 * Only print this error message if we have some kind of ACL
1623 * support that's not working. Otherwise we would always get this.
1625 DEBUG(0,("set_canon_ace_list: Unable to init %s ACL. (%s)\n",
1626 default_ace
? "default" : "file", strerror(errno
) ));
1628 *pacl_set_support
= False
;
1632 for (i
= 0, p_ace
= the_ace
; p_ace
; p_ace
= p_ace
->next
, i
++ ) {
1633 SMB_ACL_ENTRY_T the_entry
;
1634 SMB_ACL_PERMSET_T the_permset
;
1637 * Get the entry for this ACE.
1640 if (conn
->vfs_ops
.sys_acl_create_entry(conn
, &the_acl
, &the_entry
) == -1) {
1641 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
1642 i
, strerror(errno
) ));
1647 * Ok - we now know the ACL calls should be working, don't
1648 * allow fallback to chmod.
1651 *pacl_set_support
= True
;
1654 * Initialise the entry from the canon_ace.
1658 * First tell the entry what type of ACE this is.
1661 if (conn
->vfs_ops
.sys_acl_set_tag_type(conn
, the_entry
, p_ace
->type
) == -1) {
1662 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
1663 i
, strerror(errno
) ));
1668 * Only set the qualifier (user or group id) if the entry is a user
1672 if ((p_ace
->type
== SMB_ACL_USER
) || (p_ace
->type
== SMB_ACL_GROUP
)) {
1673 if (conn
->vfs_ops
.sys_acl_set_qualifier(conn
, the_entry
,(void *)&p_ace
->unix_ug
.uid
) == -1) {
1674 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
1675 i
, strerror(errno
) ));
1681 * Convert the mode_t perms in the canon_ace to a POSIX permset.
1684 if (conn
->vfs_ops
.sys_acl_get_permset(conn
, the_entry
, &the_permset
) == -1) {
1685 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
1686 i
, strerror(errno
) ));
1690 if (map_acl_perms_to_permset(conn
, p_ace
->perms
, &the_permset
) == -1) {
1691 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
1692 (unsigned int)p_ace
->perms
, i
, strerror(errno
) ));
1697 * ..and apply them to the entry.
1700 if (conn
->vfs_ops
.sys_acl_set_permset(conn
, the_entry
, the_permset
) == -1) {
1701 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
1702 i
, strerror(errno
) ));
1707 print_canon_ace( p_ace
, i
);
1711 * Add in a mask of rwx.
1714 if (conn
->vfs_ops
.sys_acl_create_entry( conn
, &the_acl
, &mask_entry
) == -1) {
1715 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno
) ));
1719 if (conn
->vfs_ops
.sys_acl_set_tag_type(conn
, mask_entry
, SMB_ACL_MASK
) == -1) {
1720 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno
) ));
1724 if (conn
->vfs_ops
.sys_acl_get_permset(conn
, mask_entry
, &mask_permset
) == -1) {
1725 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno
) ));
1729 if (map_acl_perms_to_permset(conn
, S_IRUSR
|S_IWUSR
|S_IXUSR
, &mask_permset
) == -1) {
1730 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno
) ));
1734 if (conn
->vfs_ops
.sys_acl_set_permset(conn
, mask_entry
, mask_permset
) == -1) {
1735 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno
) ));
1740 * Check if the ACL is valid.
1743 if (conn
->vfs_ops
.sys_acl_valid(conn
, the_acl
) == -1) {
1744 DEBUG(0,("set_canon_ace_list: ACL type (%s) is invalid for set (%s).\n",
1745 the_acl_type
== SMB_ACL_TYPE_DEFAULT
? "directory default" : "file",
1751 * Finally apply it to the file or directory.
1754 if(default_ace
|| fsp
->is_directory
|| fsp
->fd
== -1) {
1755 if (conn
->vfs_ops
.sys_acl_set_file(conn
, dos_to_unix_static(fsp
->fsp_name
), the_acl_type
, the_acl
) == -1) {
1757 * Some systems allow all the above calls and only fail with no ACL support
1758 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
1760 if (errno
== ENOSYS
)
1761 *pacl_set_support
= False
;
1762 DEBUG(2,("set_canon_ace_list: conn->vfs_ops.sys_acl_set_file type %s failed for file %s (%s).\n",
1763 the_acl_type
== SMB_ACL_TYPE_DEFAULT
? "directory default" : "file",
1764 fsp
->fsp_name
, strerror(errno
) ));
1768 if (conn
->vfs_ops
.sys_acl_set_fd(fsp
, fsp
->fd
, the_acl
) == -1) {
1770 * Some systems allow all the above calls and only fail with no ACL support
1771 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
1773 if (errno
== ENOSYS
)
1774 *pacl_set_support
= False
;
1775 DEBUG(2,("set_canon_ace_list: conn->vfs_ops.sys_acl_set_file failed for file %s (%s).\n",
1776 fsp
->fsp_name
, strerror(errno
) ));
1785 if (the_acl
!= NULL
)
1786 conn
->vfs_ops
.sys_acl_free_acl(conn
, the_acl
);
1791 /****************************************************************************
1792 Convert a canon_ace to a generic 3 element permission - if possible.
1793 ****************************************************************************/
1795 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
1797 static BOOL
convert_canon_ace_to_posix_perms( files_struct
*fsp
, canon_ace
*file_ace_list
, mode_t
*posix_perms
)
1799 int snum
= SNUM(fsp
->conn
);
1800 size_t ace_count
= count_canon_ace_list(file_ace_list
);
1802 canon_ace
*owner_ace
= NULL
;
1803 canon_ace
*group_ace
= NULL
;
1804 canon_ace
*other_ace
= NULL
;
1808 if (ace_count
!= 3) {
1809 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE entries for file %s to convert to \
1810 posix perms.\n", fsp
->fsp_name
));
1814 for (ace_p
= file_ace_list
; ace_p
; ace_p
= ace_p
->next
) {
1815 if (ace_p
->owner_type
== UID_ACE
)
1817 else if (ace_p
->owner_type
== GID_ACE
)
1819 else if (ace_p
->owner_type
== WORLD_ACE
)
1823 if (!owner_ace
|| !group_ace
|| !other_ace
) {
1824 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get standard entries for file %s.\n",
1829 *posix_perms
= (mode_t
)0;
1831 *posix_perms
|= owner_ace
->perms
;
1832 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IRUSR
, S_IRGRP
);
1833 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IWUSR
, S_IWGRP
);
1834 *posix_perms
|= MAP_PERM(group_ace
->perms
, S_IXUSR
, S_IXGRP
);
1835 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IRUSR
, S_IROTH
);
1836 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IWUSR
, S_IWOTH
);
1837 *posix_perms
|= MAP_PERM(other_ace
->perms
, S_IXUSR
, S_IXOTH
);
1839 /* The owner must have at least read access. */
1841 *posix_perms
|= S_IRUSR
;
1842 if (fsp
->is_directory
)
1843 *posix_perms
|= (S_IWUSR
|S_IXUSR
);
1845 /* If requested apply the masks. */
1847 /* Get the initial bits to apply. */
1849 if (fsp
->is_directory
) {
1850 and_bits
= lp_dir_security_mask(snum
);
1851 or_bits
= lp_force_dir_security_mode(snum
);
1853 and_bits
= lp_security_mask(snum
);
1854 or_bits
= lp_force_security_mode(snum
);
1857 *posix_perms
= (((*posix_perms
) & and_bits
)|or_bits
);
1859 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o to perm=0%o for file %s.\n",
1860 (int)owner_ace
->perms
, (int)group_ace
->perms
, (int)other_ace
->perms
, (int)*posix_perms
,
1866 static int nt_ace_comp( SEC_ACE
*a1
, SEC_ACE
*a2
)
1868 if (a1
->type
== a2
->type
)
1871 if (a1
->type
== SEC_ACE_TYPE_ACCESS_DENIED
&& a2
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
)
1876 /****************************************************************************
1877 Reply to query a security descriptor from an fsp. If it succeeds it allocates
1878 the space for the return elements and returns the size needed to return the
1879 security descriptor. This should be the only external function needed for
1880 the UNIX style get ACL.
1881 ****************************************************************************/
1883 size_t get_nt_acl(files_struct
*fsp
, SEC_DESC
**ppdesc
)
1885 extern DOM_SID global_sid_Builtin_Administrators
;
1886 extern DOM_SID global_sid_Builtin_Users
;
1887 connection_struct
*conn
= fsp
->conn
;
1888 SMB_STRUCT_STAT sbuf
;
1889 SEC_ACE
*nt_ace_list
= NULL
;
1893 SEC_ACL
*psa
= NULL
;
1894 size_t num_acls
= 0;
1895 size_t num_dir_acls
= 0;
1896 size_t num_aces
= 0;
1897 SMB_ACL_T posix_acl
= NULL
;
1898 SMB_ACL_T dir_acl
= NULL
;
1899 canon_ace
*file_ace
= NULL
;
1900 canon_ace
*dir_ace
= NULL
;
1901 size_t num_profile_acls
= 0;
1905 DEBUG(10,("get_nt_acl: called for file %s\n", fsp
->fsp_name
));
1907 if(fsp
->is_directory
|| fsp
->fd
== -1) {
1909 /* Get the stat struct for the owner info. */
1910 if(vfs_stat(fsp
->conn
,fsp
->fsp_name
, &sbuf
) != 0) {
1914 * Get the ACL from the path.
1917 posix_acl
= conn
->vfs_ops
.sys_acl_get_file( conn
, dos_to_unix_static(fsp
->fsp_name
), SMB_ACL_TYPE_ACCESS
);
1920 * If it's a directory get the default POSIX ACL.
1923 if(fsp
->is_directory
)
1924 dir_acl
= conn
->vfs_ops
.sys_acl_get_file( conn
, dos_to_unix_static(fsp
->fsp_name
), SMB_ACL_TYPE_DEFAULT
);
1928 /* Get the stat struct for the owner info. */
1929 if(vfs_fstat(fsp
,fsp
->fd
,&sbuf
) != 0) {
1933 * Get the ACL from the fd.
1935 posix_acl
= conn
->vfs_ops
.sys_acl_get_fd(fsp
, fsp
->fd
);
1938 DEBUG(5,("get_nt_acl : file ACL %s, directory ACL %s\n",
1939 posix_acl
? "present" : "absent",
1940 dir_acl
? "present" : "absent" ));
1943 * Get the owner, group and world SIDs.
1946 if (lp_profile_acls(SNUM(fsp
->conn
))) {
1947 /* For WXP SP1 the owner must be administrators. */
1948 sid_copy(&owner_sid
, &global_sid_Builtin_Administrators
);
1949 sid_copy(&group_sid
, &global_sid_Builtin_Users
);
1950 num_profile_acls
= 2;
1952 create_file_sids(&sbuf
, &owner_sid
, &group_sid
);
1955 /* Create the canon_ace lists. */
1956 file_ace
= canonicalise_acl( fsp
, posix_acl
, &sbuf
, &owner_sid
, &group_sid
);
1957 num_acls
= count_canon_ace_list(file_ace
);
1959 /* We must have *some* ACLS. */
1961 if (num_acls
== 0) {
1962 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", fsp
->fsp_name
));
1966 if (fsp
->is_directory
) {
1968 * If we have to fake a default ACL then this is the mode to use.
1970 sbuf
.st_mode
= unix_mode( fsp
->conn
, FILE_ATTRIBUTE_ARCHIVE
, fsp
->fsp_name
);
1972 dir_ace
= canonicalise_acl(fsp
, dir_acl
, &sbuf
, &owner_sid
, &group_sid
);
1973 num_dir_acls
= count_canon_ace_list(dir_ace
);
1976 /* Allocate the ace list. */
1977 if ((nt_ace_list
= (SEC_ACE
*)malloc((num_acls
+ num_profile_acls
+ num_dir_acls
)* sizeof(SEC_ACE
))) == NULL
) {
1978 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
1982 memset(nt_ace_list
, '\0', (num_acls
+ num_dir_acls
) * sizeof(SEC_ACE
) );
1985 * Create the NT ACE list from the canonical ace lists.
1995 for (i
= 0; i
< num_acls
; i
++, ace
= ace
->next
) {
1998 acc
= map_canon_ace_perms(&nt_acl_type
, &owner_sid
, ace
);
1999 init_sec_ace(&nt_ace_list
[num_aces
++], &ace
->trustee
, nt_acl_type
, acc
, 0);
2002 /* The User must have access to a profile share - even if we can't map the SID. */
2003 if (lp_profile_acls(SNUM(fsp
->conn
))) {
2005 init_sec_access(&acc
,FILE_GENERIC_ALL
);
2006 init_sec_ace(&nt_ace_list
[num_aces
++], &global_sid_Builtin_Users
, SEC_ACE_TYPE_ACCESS_ALLOWED
, acc
, 0);
2011 for (i
= 0; i
< num_dir_acls
; i
++, ace
= ace
->next
) {
2014 acc
= map_canon_ace_perms(&nt_acl_type
, &owner_sid
, ace
);
2015 init_sec_ace(&nt_ace_list
[num_aces
++], &ace
->trustee
, nt_acl_type
, acc
,
2016 SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_INHERIT_ONLY
);
2019 /* The User must have access to a profile share - even if we can't map the SID. */
2020 if (lp_profile_acls(SNUM(fsp
->conn
))) {
2022 init_sec_access(&acc
,FILE_GENERIC_ALL
);
2023 init_sec_ace(&nt_ace_list
[num_aces
++], &global_sid_Builtin_Users
, SEC_ACE_TYPE_ACCESS_ALLOWED
, acc
,
2024 SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|
2025 SEC_ACE_FLAG_INHERIT_ONLY
);
2029 * Sort to force deny entries to the front.
2032 if (num_acls
+ num_dir_acls
)
2033 qsort( nt_ace_list
, num_acls
+ num_dir_acls
, sizeof(nt_ace_list
[0]), QSORT_CAST nt_ace_comp
);
2037 if((psa
= make_sec_acl( main_loop_talloc_get(), ACL_REVISION
, num_aces
, nt_ace_list
)) == NULL
) {
2038 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
2043 *ppdesc
= make_standard_sec_desc( main_loop_talloc_get(), &owner_sid
, &group_sid
, psa
, &sd_size
);
2046 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
2053 conn
->vfs_ops
.sys_acl_free_acl(conn
, posix_acl
);
2055 conn
->vfs_ops
.sys_acl_free_acl(conn
, dir_acl
);
2056 free_canon_ace_list(file_ace
);
2057 free_canon_ace_list(dir_ace
);
2058 SAFE_FREE(nt_ace_list
);
2064 try to chown a file. We will be able to chown it under the following conditions
2066 1) if we have root privileges, then it will just work
2067 2) if we have write permission to the file and dos_filemodes is set
2068 then allow chown to the currently authenticated user.
2071 static int try_chown(connection_struct
*conn
, const char *fname
, uid_t uid
, gid_t gid
)
2074 extern struct current_user current_user
;
2078 /* try the direct way first */
2079 ret
= vfs_chown(conn
, fname
, uid
, gid
);
2083 if(!CAN_WRITE(conn
) || !lp_dos_filemode(SNUM(conn
)))
2086 if (vfs_stat(conn
,fname
,&st
))
2089 fsp
= open_file_fchmod(conn
,fname
,&st
);
2093 /* only allow chown to the current user. This is more secure,
2094 and also copes with the case where the SID in a take ownership ACL is
2095 a local SID on the users workstation
2097 uid
= current_user
.uid
;
2100 /* Keep the current file gid the same. */
2101 ret
= vfswrap_fchown(fsp
, fsp
->fd
, uid
, (gid_t
)-1);
2104 close_file_fchmod(fsp
);
2109 /****************************************************************************
2110 Reply to set a security descriptor on an fsp. security_info_sent is the
2111 description of the following NT ACL.
2112 This should be the only external function needed for the UNIX style set ACL.
2113 ****************************************************************************/
2115 BOOL
set_nt_acl(files_struct
*fsp
, uint32 security_info_sent
, SEC_DESC
*psd
)
2117 connection_struct
*conn
= fsp
->conn
;
2118 uid_t user
= (uid_t
)-1;
2119 gid_t grp
= (gid_t
)-1;
2120 SMB_STRUCT_STAT sbuf
;
2121 DOM_SID file_owner_sid
;
2122 DOM_SID file_grp_sid
;
2123 canon_ace
*file_ace_list
= NULL
;
2124 canon_ace
*dir_ace_list
= NULL
;
2125 BOOL acl_perms
= False
;
2126 mode_t orig_mode
= (mode_t
)0;
2130 DEBUG(10,("set_nt_acl: called for file %s\n", fsp
->fsp_name
));
2133 * Get the current state of the file.
2136 if(fsp
->is_directory
|| fsp
->fd
== -1) {
2137 if(vfs_stat(fsp
->conn
,fsp
->fsp_name
, &sbuf
) != 0)
2140 if(vfs_fstat(fsp
,fsp
->fd
,&sbuf
) != 0)
2144 /* Save the original elements we check against. */
2145 orig_mode
= sbuf
.st_mode
;
2146 orig_uid
= sbuf
.st_uid
;
2147 orig_gid
= sbuf
.st_gid
;
2150 * Unpack the user/group/world id's.
2153 if (!unpack_nt_owners( SNUM(conn
), &sbuf
, &user
, &grp
, security_info_sent
, psd
))
2157 * Do we need to chown ?
2160 if((user
!= (uid_t
)-1 || grp
!= (uid_t
)-1) && (orig_uid
!= user
|| orig_gid
!= grp
)) {
2162 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
2163 fsp
->fsp_name
, (unsigned int)user
, (unsigned int)grp
));
2165 if(try_chown( fsp
->conn
, fsp
->fsp_name
, user
, grp
) == -1) {
2166 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error = %s.\n",
2167 fsp
->fsp_name
, (unsigned int)user
, (unsigned int)grp
, strerror(errno
) ));
2172 * Recheck the current state of the file, which may have changed.
2173 * (suid/sgid bits, for instance)
2176 if(fsp
->is_directory
) {
2177 if(vfs_stat(fsp
->conn
, fsp
->fsp_name
, &sbuf
) != 0) {
2185 ret
= vfs_stat(fsp
->conn
, fsp
->fsp_name
, &sbuf
);
2187 ret
= vfs_fstat(fsp
,fsp
->fd
,&sbuf
);
2193 /* Save the original elements we check against. */
2194 orig_mode
= sbuf
.st_mode
;
2195 orig_uid
= sbuf
.st_uid
;
2196 orig_gid
= sbuf
.st_gid
;
2199 create_file_sids(&sbuf
, &file_owner_sid
, &file_grp_sid
);
2201 acl_perms
= unpack_canon_ace( fsp
, &sbuf
, &file_owner_sid
, &file_grp_sid
,
2202 &file_ace_list
, &dir_ace_list
, security_info_sent
, psd
);
2204 if ((file_ace_list
== NULL
) && (dir_ace_list
== NULL
)) {
2205 /* W2K traverse DACL set - ignore. */
2210 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
2211 free_canon_ace_list(file_ace_list
);
2212 free_canon_ace_list(dir_ace_list
);
2217 * Only change security if we got a DACL.
2220 if((security_info_sent
& DACL_SECURITY_INFORMATION
) && (psd
->dacl
!= NULL
)) {
2222 BOOL acl_set_support
= False
;
2226 * Try using the POSIX ACL set first. Fall back to chmod if
2227 * we have no ACL support on this filesystem.
2230 if (acl_perms
&& file_ace_list
) {
2231 ret
= set_canon_ace_list(fsp
, file_ace_list
, False
, &acl_set_support
);
2232 if (acl_set_support
&& ret
== False
) {
2233 DEBUG(3,("set_nt_acl: failed to set file acl on file %s (%s).\n", fsp
->fsp_name
, strerror(errno
) ));
2234 free_canon_ace_list(file_ace_list
);
2235 free_canon_ace_list(dir_ace_list
);
2240 if (acl_perms
&& acl_set_support
&& fsp
->is_directory
) {
2242 if (!set_canon_ace_list(fsp
, dir_ace_list
, True
, &acl_set_support
)) {
2243 DEBUG(3,("set_nt_acl: failed to set default acl on directory %s (%s).\n", fsp
->fsp_name
, strerror(errno
) ));
2244 free_canon_ace_list(file_ace_list
);
2245 free_canon_ace_list(dir_ace_list
);
2251 * No default ACL - delete one if it exists.
2254 if (conn
->vfs_ops
.sys_acl_delete_def_file(conn
, dos_to_unix_static(fsp
->fsp_name
)) == -1) {
2255 DEBUG(3,("set_nt_acl: conn->vfs_ops.sys_acl_delete_def_file failed (%s)\n", strerror(errno
)));
2256 free_canon_ace_list(file_ace_list
);
2263 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
2266 if(!acl_set_support
&& acl_perms
) {
2269 if (!convert_canon_ace_to_posix_perms( fsp
, file_ace_list
, &posix_perms
)) {
2270 free_canon_ace_list(file_ace_list
);
2271 free_canon_ace_list(dir_ace_list
);
2272 DEBUG(3,("set_nt_acl: failed to convert file acl to posix permissions for file %s.\n",
2277 if (orig_mode
!= posix_perms
) {
2279 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
2280 fsp
->fsp_name
, (unsigned int)posix_perms
));
2282 if(conn
->vfs_ops
.chmod(conn
,dos_to_unix_static(fsp
->fsp_name
), posix_perms
) == -1) {
2283 DEBUG(3,("set_nt_acl: chmod %s, 0%o failed. Error = %s.\n",
2284 fsp
->fsp_name
, (unsigned int)posix_perms
, strerror(errno
) ));
2285 free_canon_ace_list(file_ace_list
);
2286 free_canon_ace_list(dir_ace_list
);
2293 free_canon_ace_list(file_ace_list
);
2294 free_canon_ace_list(dir_ace_list
);
2299 /****************************************************************************
2300 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
2301 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
2302 ****************************************************************************/
2304 static int chmod_acl_internals( connection_struct
*conn
, SMB_ACL_T posix_acl
, mode_t mode
)
2306 int entry_id
= SMB_ACL_FIRST_ENTRY
;
2307 SMB_ACL_ENTRY_T entry
;
2308 int num_entries
= 0;
2310 while ( conn
->vfs_ops
.sys_acl_get_entry(conn
, posix_acl
, entry_id
, &entry
) == 1) {
2311 SMB_ACL_TAG_T tagtype
;
2312 SMB_ACL_PERMSET_T permset
;
2316 if (entry_id
== SMB_ACL_FIRST_ENTRY
)
2317 entry_id
= SMB_ACL_NEXT_ENTRY
;
2319 if (conn
->vfs_ops
.sys_acl_get_tag_type(conn
, entry
, &tagtype
) == -1)
2322 if (conn
->vfs_ops
.sys_acl_get_permset(conn
, entry
, &permset
) == -1)
2328 case SMB_ACL_USER_OBJ
:
2329 perms
= unix_perms_to_acl_perms(mode
, S_IRUSR
, S_IWUSR
, S_IXUSR
);
2331 case SMB_ACL_GROUP_OBJ
:
2332 perms
= unix_perms_to_acl_perms(mode
, S_IRGRP
, S_IWGRP
, S_IXGRP
);
2335 perms
= S_IRUSR
|S_IWUSR
|S_IXUSR
;
2338 perms
= unix_perms_to_acl_perms(mode
, S_IROTH
, S_IWOTH
, S_IXOTH
);
2344 if (map_acl_perms_to_permset(conn
, perms
, &permset
) == -1)
2347 if (conn
->vfs_ops
.sys_acl_set_permset(conn
, entry
, permset
) == -1)
2352 * If this is a simple 3 element ACL or no elements then it's a standard
2353 * UNIX permission set. Just use chmod...
2356 if ((num_entries
== 3) || (num_entries
== 0))
2362 /****************************************************************************
2363 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
2364 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
2365 Note that name is in UNIX character set.
2366 ****************************************************************************/
2368 int chmod_acl(connection_struct
*conn
, const char *name
, mode_t mode
)
2370 SMB_ACL_T posix_acl
= NULL
;
2373 if ((posix_acl
= conn
->vfs_ops
.sys_acl_get_file(conn
, name
, SMB_ACL_TYPE_ACCESS
)) == NULL
)
2376 if ((ret
= chmod_acl_internals(conn
, posix_acl
, mode
)) == -1)
2379 ret
= conn
->vfs_ops
.sys_acl_set_file(conn
, name
, SMB_ACL_TYPE_ACCESS
, posix_acl
);
2383 conn
->vfs_ops
.sys_acl_free_acl(conn
, posix_acl
);
2387 /****************************************************************************
2388 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
2389 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
2390 ****************************************************************************/
2392 int fchmod_acl(files_struct
*fsp
, int fd
, mode_t mode
)
2394 connection_struct
*conn
= fsp
->conn
;
2395 SMB_ACL_T posix_acl
= NULL
;
2398 if ((posix_acl
= conn
->vfs_ops
.sys_acl_get_fd(fsp
, fd
)) == NULL
)
2401 if ((ret
= chmod_acl_internals(conn
, posix_acl
, mode
)) == -1)
2404 ret
= conn
->vfs_ops
.sys_acl_set_fd(fsp
, fd
, posix_acl
);
2408 conn
->vfs_ops
.sys_acl_free_acl(conn
, posix_acl
);
2412 BOOL
directory_has_default_acl(connection_struct
*conn
, const char *fname
)
2414 SMB_ACL_T dir_acl
= conn
->vfs_ops
.sys_acl_get_file( conn
, fname
, SMB_ACL_TYPE_DEFAULT
);
2415 BOOL has_acl
= False
;
2416 SMB_ACL_ENTRY_T entry
;
2418 if (dir_acl
!= NULL
&& (conn
->vfs_ops
.sys_acl_get_entry(conn
, dir_acl
, SMB_ACL_FIRST_ENTRY
, &entry
) == 1))
2422 conn
->vfs_ops
.sys_acl_free_acl(conn
, dir_acl
);