2 * Handle passing Access Control Lists between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2006-2009 Wayne Davison
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 along
19 * with this program; if not, visit the http://fsf.org website.
23 #include "lib/sysacls.h"
31 extern int orig_umask
;
32 extern int numeric_ids
;
33 extern int inc_recurse
;
35 /* Flags used to indicate what items are being transmitted for an entry. */
36 #define XMIT_USER_OBJ (1<<0)
37 #define XMIT_GROUP_OBJ (1<<1)
38 #define XMIT_MASK_OBJ (1<<2)
39 #define XMIT_OTHER_OBJ (1<<3)
40 #define XMIT_NAME_LIST (1<<4)
42 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
44 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
46 /* When we send the access bits over the wire, we shift them 2 bits to the
47 * left and use the lower 2 bits as flags (relevant only to a name entry).
48 * This makes the protocol more efficient than sending a value that would
49 * be likely to have its hightest bits set. */
50 #define XFLAG_NAME_FOLLOWS 0x0001u
51 #define XFLAG_NAME_IS_USER 0x0002u
53 /* === ACL structures === */
70 typedef struct rsync_acl
{
72 /* These will be NO_ENTRY if there's no such entry. */
84 static const rsync_acl empty_rsync_acl
= {
85 {NULL
, 0}, NO_ENTRY
, NO_ENTRY
, NO_ENTRY
, NO_ENTRY
88 static item_list access_acl_list
= EMPTY_ITEM_LIST
;
89 static item_list default_acl_list
= EMPTY_ITEM_LIST
;
91 /* === Calculations on ACL types === */
93 static const char *str_acl_type(SMB_ACL_TYPE_T type
)
96 case SMB_ACL_TYPE_ACCESS
:
98 return "ACL_TYPE_EXTENDED";
100 return "ACL_TYPE_ACCESS";
102 case SMB_ACL_TYPE_DEFAULT
:
103 return "ACL_TYPE_DEFAULT";
107 return "unknown ACL type!";
110 static int calc_sacl_entries(const rsync_acl
*racl
)
112 /* A System ACL always gets user/group/other permission entries. */
113 return racl
->names
.count
114 #ifdef ACLS_NEED_MASK
117 + (racl
->mask_obj
!= NO_ENTRY
) + 3;
121 /* Extracts and returns the permission bits from the ACL. This cannot be
122 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
123 static int rsync_acl_get_perms(const rsync_acl
*racl
)
125 return (racl
->user_obj
<< 6)
126 + ((racl
->mask_obj
!= NO_ENTRY
? racl
->mask_obj
: racl
->group_obj
) << 3)
130 /* Removes the permission-bit entries from the ACL because these
131 * can be reconstructed from the file's mode. */
132 static void rsync_acl_strip_perms(rsync_acl
*racl
)
134 racl
->user_obj
= NO_ENTRY
;
135 if (racl
->mask_obj
== NO_ENTRY
)
136 racl
->group_obj
= NO_ENTRY
;
138 if (racl
->group_obj
== racl
->mask_obj
)
139 racl
->group_obj
= NO_ENTRY
;
140 if (racl
->names
.count
!= 0)
141 racl
->mask_obj
= NO_ENTRY
;
143 racl
->other_obj
= NO_ENTRY
;
146 /* Given an empty rsync_acl, fake up the permission bits. */
147 static void rsync_acl_fake_perms(rsync_acl
*racl
, mode_t mode
)
149 racl
->user_obj
= (mode
>> 6) & 7;
150 racl
->group_obj
= (mode
>> 3) & 7;
151 racl
->other_obj
= mode
& 7;
154 /* === Rsync ACL functions === */
156 static rsync_acl
*create_racl(void)
158 rsync_acl
*racl
= new(rsync_acl
);
161 out_of_memory("create_racl");
162 *racl
= empty_rsync_acl
;
167 static BOOL
ida_entries_equal(const ida_entries
*ial1
, const ida_entries
*ial2
)
169 id_access
*ida1
, *ida2
;
170 int count
= ial1
->count
;
171 if (count
!= ial2
->count
)
175 for (; count
--; ida1
++, ida2
++) {
176 if (ida1
->access
!= ida2
->access
|| ida1
->id
!= ida2
->id
)
182 static BOOL
rsync_acl_equal(const rsync_acl
*racl1
, const rsync_acl
*racl2
)
184 return racl1
->user_obj
== racl2
->user_obj
185 && racl1
->group_obj
== racl2
->group_obj
186 && racl1
->mask_obj
== racl2
->mask_obj
187 && racl1
->other_obj
== racl2
->other_obj
188 && ida_entries_equal(&racl1
->names
, &racl2
->names
);
191 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
192 * the ACL will be handled by the normal mode-preservation code. This is
193 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
194 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
195 * that it might have several of its permission objects set to NO_ENTRY. */
196 static BOOL
rsync_acl_equal_enough(const rsync_acl
*racl1
,
197 const rsync_acl
*racl2
, mode_t m
)
199 if ((racl1
->mask_obj
^ racl2
->mask_obj
) & NO_ENTRY
)
200 return False
; /* One has a mask and the other doesn't */
202 /* When there's a mask, the group_obj becomes an extended entry. */
203 if (racl1
->mask_obj
!= NO_ENTRY
) {
204 /* A condensed rsync_acl with a mask can only have no
205 * group_obj when it was identical to the mask. This
206 * means that it was also identical to the group attrs
208 if (racl2
->group_obj
== NO_ENTRY
) {
209 if (racl1
->group_obj
!= ((m
>> 3) & 7))
211 } else if (racl1
->group_obj
!= racl2
->group_obj
)
214 return ida_entries_equal(&racl1
->names
, &racl2
->names
);
217 static void rsync_acl_free(rsync_acl
*racl
)
219 if (racl
->names
.idas
)
220 free(racl
->names
.idas
);
221 *racl
= empty_rsync_acl
;
224 void free_acl(stat_x
*sxp
)
227 rsync_acl_free(sxp
->acc_acl
);
232 rsync_acl_free(sxp
->def_acl
);
238 #ifdef SMB_ACL_NEED_SORT
239 static int id_access_sorter(const void *r1
, const void *r2
)
241 id_access
*ida1
= (id_access
*)r1
;
242 id_access
*ida2
= (id_access
*)r2
;
243 id_t rid1
= ida1
->id
, rid2
= ida2
->id
;
244 if ((ida1
->access
^ ida2
->access
) & NAME_IS_USER
)
245 return ida1
->access
& NAME_IS_USER
? -1 : 1;
246 return rid1
== rid2
? 0 : rid1
< rid2
? -1 : 1;
250 /* === System ACLs === */
252 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
253 static BOOL
unpack_smb_acl(SMB_ACL_T sacl
, rsync_acl
*racl
)
255 static item_list temp_ida_list
= EMPTY_ITEM_LIST
;
256 SMB_ACL_ENTRY_T entry
;
260 errfun
= "sys_acl_get_entry";
261 for (rc
= sys_acl_get_entry(sacl
, SMB_ACL_FIRST_ENTRY
, &entry
);
263 rc
= sys_acl_get_entry(sacl
, SMB_ACL_NEXT_ENTRY
, &entry
)) {
264 SMB_ACL_TAG_T tag_type
;
268 if ((rc
= sys_acl_get_info(entry
, &tag_type
, &access
, &g_u_id
)) != 0) {
269 errfun
= "sys_acl_get_info";
272 /* continue == done with entry; break == store in temporary ida list */
274 #ifndef HAVE_OSX_ACLS
275 case SMB_ACL_USER_OBJ
:
276 if (racl
->user_obj
== NO_ENTRY
)
277 racl
->user_obj
= access
;
279 rprintf(FINFO
, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
281 case SMB_ACL_GROUP_OBJ
:
282 if (racl
->group_obj
== NO_ENTRY
)
283 racl
->group_obj
= access
;
285 rprintf(FINFO
, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
288 if (racl
->mask_obj
== NO_ENTRY
)
289 racl
->mask_obj
= access
;
291 rprintf(FINFO
, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
294 if (racl
->other_obj
== NO_ENTRY
)
295 racl
->other_obj
= access
;
297 rprintf(FINFO
, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
301 access
|= NAME_IS_USER
;
306 rprintf(FINFO
, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
309 ida
= EXPAND_ITEM_LIST(&temp_ida_list
, id_access
, -10);
311 ida
->access
= access
;
314 rsyserr(FERROR_XFER
, errno
, "unpack_smb_acl: %s()", errfun
);
315 rsync_acl_free(racl
);
319 /* Transfer the count id_access items out of the temp_ida_list
320 * into the names ida_entries list in racl. */
321 if (temp_ida_list
.count
) {
322 #ifdef SMB_ACL_NEED_SORT
323 if (temp_ida_list
.count
> 1) {
324 qsort(temp_ida_list
.items
, temp_ida_list
.count
,
325 sizeof (id_access
), id_access_sorter
);
328 if (!(racl
->names
.idas
= new_array(id_access
, temp_ida_list
.count
)))
329 out_of_memory("unpack_smb_acl");
330 memcpy(racl
->names
.idas
, temp_ida_list
.items
,
331 temp_ida_list
.count
* sizeof (id_access
));
333 racl
->names
.idas
= NULL
;
335 racl
->names
.count
= temp_ida_list
.count
;
337 /* Truncate the temporary list now that its idas have been saved. */
338 temp_ida_list
.count
= 0;
340 #ifdef ACLS_NEED_MASK
341 if (!racl
->names
.count
&& racl
->mask_obj
!= NO_ENTRY
) {
342 /* Throw away a superfluous mask, but mask off the
343 * group perms with it first. */
344 racl
->group_obj
&= racl
->mask_obj
;
345 racl
->mask_obj
= NO_ENTRY
;
352 /* Synactic sugar for system calls */
354 #define CALL_OR_ERROR(func,args,str) \
362 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
363 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
365 #ifndef HAVE_OSX_ACLS
366 /* Store the permissions in the system ACL entry. */
367 static int store_access_in_entry(uint32 access
, SMB_ACL_ENTRY_T entry
)
369 if (sys_acl_set_access_bits(entry
, access
)) {
370 rsyserr(FERROR_XFER
, errno
, "store_access_in_entry sys_acl_set_access_bits()");
377 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
378 static BOOL
pack_smb_acl(SMB_ACL_T
*smb_acl
, const rsync_acl
*racl
)
380 #ifdef ACLS_NEED_MASK
385 const char *errfun
= NULL
;
386 SMB_ACL_ENTRY_T entry
;
388 if (!(*smb_acl
= sys_acl_init(calc_sacl_entries(racl
)))) {
389 rsyserr(FERROR_XFER
, errno
, "pack_smb_acl: sys_acl_init()");
393 #ifndef HAVE_OSX_ACLS
394 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
395 COE( sys_acl_set_info
,(entry
, SMB_ACL_USER_OBJ
, racl
->user_obj
& ~NO_ENTRY
, 0) );
398 for (ida
= racl
->names
.idas
, count
= racl
->names
.count
; count
; ida
++, count
--) {
399 #ifdef SMB_ACL_NEED_SORT
400 if (!(ida
->access
& NAME_IS_USER
))
403 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
404 COE( sys_acl_set_info
,
406 ida
->access
& NAME_IS_USER
? SMB_ACL_USER
: SMB_ACL_GROUP
,
407 ida
->access
& ~NAME_IS_USER
, ida
->id
) );
410 #ifndef HAVE_OSX_ACLS
411 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
412 COE( sys_acl_set_info
,(entry
, SMB_ACL_GROUP_OBJ
, racl
->group_obj
& ~NO_ENTRY
, 0) );
414 #ifdef SMB_ACL_NEED_SORT
415 for ( ; count
; ida
++, count
--) {
416 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
417 COE( sys_acl_set_info
,(entry
, SMB_ACL_GROUP
, ida
->access
, ida
->id
) );
421 #ifdef ACLS_NEED_MASK
422 mask_bits
= racl
->mask_obj
== NO_ENTRY
? racl
->group_obj
& ~NO_ENTRY
: racl
->mask_obj
;
423 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
424 COE( sys_acl_set_info
,(entry
, SMB_ACL_MASK
, mask_bits
, NULL
) );
426 if (racl
->mask_obj
!= NO_ENTRY
) {
427 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
428 COE( sys_acl_set_info
,(entry
, SMB_ACL_MASK
, racl
->mask_obj
, 0) );
432 COE( sys_acl_create_entry
,(smb_acl
, &entry
) );
433 COE( sys_acl_set_info
,(entry
, SMB_ACL_OTHER
, racl
->other_obj
& ~NO_ENTRY
, 0) );
437 if (sys_acl_valid(*smb_acl
) < 0)
438 rprintf(FERROR_XFER
, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
445 rsyserr(FERROR_XFER
, errno
, "pack_smb_acl %s()", errfun
);
447 sys_acl_free_acl(*smb_acl
);
451 static int find_matching_rsync_acl(const rsync_acl
*racl
, SMB_ACL_TYPE_T type
,
452 const item_list
*racl_list
)
454 static int access_match
= -1, default_match
= -1;
455 int *match
= type
== SMB_ACL_TYPE_ACCESS
? &access_match
: &default_match
;
456 size_t count
= racl_list
->count
;
458 /* If this is the first time through or we didn't match the last
459 * time, then start at the end of the list, which should be the
460 * best place to start hunting. */
462 *match
= racl_list
->count
- 1;
464 rsync_acl
*base
= racl_list
->items
;
465 if (rsync_acl_equal(base
+ *match
, racl
))
468 *match
= racl_list
->count
- 1;
475 static int get_rsync_acl(const char *fname
, rsync_acl
*racl
,
476 SMB_ACL_TYPE_T type
, mode_t mode
)
480 #ifdef SUPPORT_XATTRS
481 /* --fake-super support: load ACLs from an xattr. */
487 if ((buf
= get_xattr_acl(fname
, type
== SMB_ACL_TYPE_ACCESS
, &len
)) == NULL
)
489 cnt
= (len
- 4*4) / (4+4);
490 if (len
< 4*4 || len
!= (size_t)cnt
*(4+4) + 4*4) {
495 racl
->user_obj
= IVAL(buf
, 0);
496 racl
->group_obj
= IVAL(buf
, 4);
497 racl
->mask_obj
= IVAL(buf
, 8);
498 racl
->other_obj
= IVAL(buf
, 12);
501 char *bp
= buf
+ 4*4;
503 if (!(ida
= racl
->names
.idas
= new_array(id_access
, cnt
)))
504 out_of_memory("get_rsync_acl");
505 racl
->names
.count
= cnt
;
506 for ( ; cnt
--; ida
++, bp
+= 4+4) {
507 ida
->id
= IVAL(bp
, 0);
508 ida
->access
= IVAL(bp
, 4);
516 if ((sacl
= sys_acl_get_file(fname
, type
)) != 0) {
517 BOOL ok
= unpack_smb_acl(sacl
, racl
);
519 sys_acl_free_acl(sacl
);
523 } else if (no_acl_syscall_error(errno
)) {
524 /* ACLs are not supported, so pretend we have a basic ACL. */
525 if (type
== SMB_ACL_TYPE_ACCESS
)
526 rsync_acl_fake_perms(racl
, mode
);
528 rsyserr(FERROR_XFER
, errno
, "get_acl: sys_acl_get_file(%s, %s)",
529 fname
, str_acl_type(type
));
536 /* Return the Access Control List for the given filename. */
537 int get_acl(const char *fname
, stat_x
*sxp
)
539 sxp
->acc_acl
= create_racl();
540 if (get_rsync_acl(fname
, sxp
->acc_acl
, SMB_ACL_TYPE_ACCESS
,
541 sxp
->st
.st_mode
) < 0) {
546 if (S_ISDIR(sxp
->st
.st_mode
)) {
547 sxp
->def_acl
= create_racl();
548 if (get_rsync_acl(fname
, sxp
->def_acl
, SMB_ACL_TYPE_DEFAULT
,
549 sxp
->st
.st_mode
) < 0) {
558 /* === Send functions === */
560 /* Send the ida list over the file descriptor. */
561 static void send_ida_entries(const ida_entries
*idal
, int f
)
564 size_t count
= idal
->count
;
566 write_varint(f
, idal
->count
);
568 for (ida
= idal
->idas
; count
--; ida
++) {
569 uint32 xbits
= ida
->access
<< 2;
571 if (ida
->access
& NAME_IS_USER
) {
572 xbits
|= XFLAG_NAME_IS_USER
;
573 name
= add_uid(ida
->id
);
575 name
= add_gid(ida
->id
);
576 write_varint(f
, ida
->id
);
577 if (inc_recurse
&& name
) {
578 int len
= strlen(name
);
579 write_varint(f
, xbits
| XFLAG_NAME_FOLLOWS
);
581 write_buf(f
, name
, len
);
583 write_varint(f
, xbits
);
587 static void send_rsync_acl(rsync_acl
*racl
, SMB_ACL_TYPE_T type
,
588 item_list
*racl_list
, int f
)
590 int ndx
= find_matching_rsync_acl(racl
, type
, racl_list
);
592 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
593 write_varint(f
, ndx
+ 1);
596 rsync_acl
*new_racl
= EXPAND_ITEM_LIST(racl_list
, rsync_acl
, 1000);
599 if (racl
->user_obj
!= NO_ENTRY
)
600 flags
|= XMIT_USER_OBJ
;
601 if (racl
->group_obj
!= NO_ENTRY
)
602 flags
|= XMIT_GROUP_OBJ
;
603 if (racl
->mask_obj
!= NO_ENTRY
)
604 flags
|= XMIT_MASK_OBJ
;
605 if (racl
->other_obj
!= NO_ENTRY
)
606 flags
|= XMIT_OTHER_OBJ
;
607 if (racl
->names
.count
)
608 flags
|= XMIT_NAME_LIST
;
610 write_byte(f
, flags
);
612 if (flags
& XMIT_USER_OBJ
)
613 write_varint(f
, racl
->user_obj
);
614 if (flags
& XMIT_GROUP_OBJ
)
615 write_varint(f
, racl
->group_obj
);
616 if (flags
& XMIT_MASK_OBJ
)
617 write_varint(f
, racl
->mask_obj
);
618 if (flags
& XMIT_OTHER_OBJ
)
619 write_varint(f
, racl
->other_obj
);
620 if (flags
& XMIT_NAME_LIST
)
621 send_ida_entries(&racl
->names
, f
);
623 /* Give the allocated data to the new list object. */
625 *racl
= empty_rsync_acl
;
629 /* Send the ACL from the stat_x structure down the indicated file descriptor.
630 * This also frees the ACL data. */
631 void send_acl(stat_x
*sxp
, int f
)
634 sxp
->acc_acl
= create_racl();
635 rsync_acl_fake_perms(sxp
->acc_acl
, sxp
->st
.st_mode
);
637 /* Avoid sending values that can be inferred from other data. */
638 rsync_acl_strip_perms(sxp
->acc_acl
);
640 send_rsync_acl(sxp
->acc_acl
, SMB_ACL_TYPE_ACCESS
, &access_acl_list
, f
);
642 if (S_ISDIR(sxp
->st
.st_mode
)) {
644 sxp
->def_acl
= create_racl();
646 send_rsync_acl(sxp
->def_acl
, SMB_ACL_TYPE_DEFAULT
, &default_acl_list
, f
);
650 /* === Receive functions === */
652 static uint32
recv_acl_access(uchar
*name_follows_ptr
, int f
)
654 uint32 access
= read_varint(f
);
656 if (name_follows_ptr
) {
657 int flags
= access
& 3;
659 if (am_root
>= 0 && access
& ~SMB_ACL_VALID_NAME_BITS
)
661 if (flags
& XFLAG_NAME_FOLLOWS
)
662 *name_follows_ptr
= 1;
664 *name_follows_ptr
= 0;
665 if (flags
& XFLAG_NAME_IS_USER
)
666 access
|= NAME_IS_USER
;
667 } else if (am_root
>= 0 && access
& ~SMB_ACL_VALID_OBJ_BITS
) {
669 rprintf(FERROR_XFER
, "recv_acl_access: value out of range: %x\n",
671 exit_cleanup(RERR_STREAMIO
);
677 static uchar
recv_ida_entries(ida_entries
*ent
, int f
)
679 uchar computed_mask_bits
= 0;
680 int i
, count
= read_varint(f
);
683 if (!(ent
->idas
= new_array(id_access
, count
)))
684 out_of_memory("recv_ida_entries");
690 for (i
= 0; i
< count
; i
++) {
692 id_t id
= read_varint(f
);
693 uint32 access
= recv_acl_access(&has_name
, f
);
696 if (access
& NAME_IS_USER
)
697 id
= recv_user_name(f
, id
);
699 id
= recv_group_name(f
, id
, NULL
);
700 } else if (access
& NAME_IS_USER
) {
701 if (inc_recurse
&& am_root
&& !numeric_ids
)
704 if (inc_recurse
&& (!am_root
|| !numeric_ids
))
705 id
= match_gid(id
, NULL
);
708 ent
->idas
[i
].id
= id
;
709 ent
->idas
[i
].access
= access
;
710 computed_mask_bits
|= access
;
713 return computed_mask_bits
& ~NO_ENTRY
;
716 static int recv_rsync_acl(item_list
*racl_list
, SMB_ACL_TYPE_T type
, int f
)
718 uchar computed_mask_bits
= 0;
721 int ndx
= read_varint(f
);
723 if (ndx
< 0 || (size_t)ndx
> racl_list
->count
) {
724 rprintf(FERROR_XFER
, "recv_acl_index: %s ACL index %d > %d\n",
725 str_acl_type(type
), ndx
, (int)racl_list
->count
);
726 exit_cleanup(RERR_STREAMIO
);
732 ndx
= racl_list
->count
;
733 duo_item
= EXPAND_ITEM_LIST(racl_list
, acl_duo
, 1000);
734 duo_item
->racl
= empty_rsync_acl
;
736 flags
= read_byte(f
);
738 if (flags
& XMIT_USER_OBJ
)
739 duo_item
->racl
.user_obj
= recv_acl_access(NULL
, f
);
740 if (flags
& XMIT_GROUP_OBJ
)
741 duo_item
->racl
.group_obj
= recv_acl_access(NULL
, f
);
742 if (flags
& XMIT_MASK_OBJ
)
743 duo_item
->racl
.mask_obj
= recv_acl_access(NULL
, f
);
744 if (flags
& XMIT_OTHER_OBJ
)
745 duo_item
->racl
.other_obj
= recv_acl_access(NULL
, f
);
746 if (flags
& XMIT_NAME_LIST
)
747 computed_mask_bits
|= recv_ida_entries(&duo_item
->racl
.names
, f
);
750 /* If we received a superfluous mask, throw it away. */
751 duo_item
->racl
.mask_obj
= NO_ENTRY
;
753 if (duo_item
->racl
.names
.count
&& duo_item
->racl
.mask_obj
== NO_ENTRY
) /* Must be non-empty with lists. */
754 duo_item
->racl
.mask_obj
= (computed_mask_bits
| duo_item
->racl
.group_obj
) & ~NO_ENTRY
;
757 duo_item
->sacl
= NULL
;
762 /* Receive the ACL info the sender has included for this file-list entry. */
763 void receive_acl(struct file_struct
*file
, int f
)
765 F_ACL(file
) = recv_rsync_acl(&access_acl_list
, SMB_ACL_TYPE_ACCESS
, f
);
767 if (S_ISDIR(file
->mode
))
768 F_DIR_DEFACL(file
) = recv_rsync_acl(&default_acl_list
, SMB_ACL_TYPE_DEFAULT
, f
);
771 static int cache_rsync_acl(rsync_acl
*racl
, SMB_ACL_TYPE_T type
, item_list
*racl_list
)
777 else if ((ndx
= find_matching_rsync_acl(racl
, type
, racl_list
)) == -1) {
779 ndx
= racl_list
->count
;
780 new_duo
= EXPAND_ITEM_LIST(racl_list
, acl_duo
, 1000);
781 new_duo
->racl
= *racl
;
782 new_duo
->sacl
= NULL
;
783 *racl
= empty_rsync_acl
;
789 /* Turn the ACL data in stat_x into cached ACL data, setting the index
790 * values in the file struct. */
791 void cache_acl(struct file_struct
*file
, stat_x
*sxp
)
793 F_ACL(file
) = cache_rsync_acl(sxp
->acc_acl
,
794 SMB_ACL_TYPE_ACCESS
, &access_acl_list
);
796 if (S_ISDIR(sxp
->st
.st_mode
)) {
797 F_DIR_DEFACL(file
) = cache_rsync_acl(sxp
->def_acl
,
798 SMB_ACL_TYPE_DEFAULT
, &default_acl_list
);
802 #ifndef HAVE_OSX_ACLS
803 static mode_t
change_sacl_perms(SMB_ACL_T sacl
, rsync_acl
*racl
, mode_t old_mode
, mode_t mode
)
805 SMB_ACL_ENTRY_T entry
;
810 /* If the sticky bit is going on, it's not safe to allow all
811 * the new ACL to go into effect before it gets set. */
812 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
816 if (mode
& S_ISVTX
&& !(old_mode
& S_ISVTX
))
819 /* If setuid or setgid is going off, it's not safe to allow all
820 * the new ACL to go into effect before they get cleared. */
821 if ((old_mode
& S_ISUID
&& !(mode
& S_ISUID
))
822 || (old_mode
& S_ISGID
&& !(mode
& S_ISGID
)))
827 errfun
= "sys_acl_get_entry";
828 for (rc
= sys_acl_get_entry(sacl
, SMB_ACL_FIRST_ENTRY
, &entry
);
830 rc
= sys_acl_get_entry(sacl
, SMB_ACL_NEXT_ENTRY
, &entry
)) {
831 SMB_ACL_TAG_T tag_type
;
832 if ((rc
= sys_acl_get_tag_type(entry
, &tag_type
)) != 0) {
833 errfun
= "sys_acl_get_tag_type";
837 case SMB_ACL_USER_OBJ
:
838 COE2( store_access_in_entry
,((mode
>> 6) & 7, entry
) );
840 case SMB_ACL_GROUP_OBJ
:
841 /* group is only empty when identical to group perms. */
842 if (racl
->group_obj
!= NO_ENTRY
)
844 COE2( store_access_in_entry
,((mode
>> 3) & 7, entry
) );
847 #ifndef ACLS_NEED_MASK
848 /* mask is only empty when we don't need it. */
849 if (racl
->mask_obj
== NO_ENTRY
)
852 COE2( store_access_in_entry
,((mode
>> 3) & 7, entry
) );
855 COE2( store_access_in_entry
,(mode
& 7, entry
) );
862 rsyserr(FERROR_XFER
, errno
, "change_sacl_perms: %s()",
868 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
869 /* Ensure that chmod() will be called to restore any lost setid bits. */
870 if (old_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
)
871 && BITS_EQUAL(old_mode
, mode
, CHMOD_BITS
))
872 old_mode
&= ~(S_ISUID
| S_ISGID
| S_ISVTX
);
875 /* Return the mode of the file on disk, as we will set them. */
876 return (old_mode
& ~ACCESSPERMS
) | (mode
& ACCESSPERMS
);
880 static int set_rsync_acl(const char *fname
, acl_duo
*duo_item
,
881 SMB_ACL_TYPE_T type
, stat_x
*sxp
, mode_t mode
)
883 if (type
== SMB_ACL_TYPE_DEFAULT
884 && duo_item
->racl
.user_obj
== NO_ENTRY
) {
886 #ifdef SUPPORT_XATTRS
887 /* --fake-super support: delete default ACL from xattrs. */
889 rc
= del_def_xattr_acl(fname
);
892 rc
= sys_acl_delete_def_file(fname
);
894 rsyserr(FERROR_XFER
, errno
, "set_acl: sys_acl_delete_def_file(%s)",
898 #ifdef SUPPORT_XATTRS
899 } else if (am_root
< 0) {
900 /* --fake-super support: store ACLs in an xattr. */
901 int cnt
= duo_item
->racl
.names
.count
;
902 size_t len
= 4*4 + cnt
* (4+4);
903 char *buf
= new_array(char, len
);
906 SIVAL(buf
, 0, duo_item
->racl
.user_obj
);
907 SIVAL(buf
, 4, duo_item
->racl
.group_obj
);
908 SIVAL(buf
, 8, duo_item
->racl
.mask_obj
);
909 SIVAL(buf
, 12, duo_item
->racl
.other_obj
);
912 char *bp
= buf
+ 4*4;
913 id_access
*ida
= duo_item
->racl
.names
.idas
;
914 for ( ; cnt
--; ida
++, bp
+= 4+4) {
915 SIVAL(bp
, 0, ida
->id
);
916 SIVAL(bp
, 4, ida
->access
);
919 rc
= set_xattr_acl(fname
, type
== SMB_ACL_TYPE_ACCESS
, buf
, len
);
924 mode_t cur_mode
= sxp
->st
.st_mode
;
926 && !pack_smb_acl(&duo_item
->sacl
, &duo_item
->racl
))
929 mode
= 0; /* eliminate compiler warning */
931 if (type
== SMB_ACL_TYPE_ACCESS
) {
932 cur_mode
= change_sacl_perms(duo_item
->sacl
, &duo_item
->racl
,
934 if (cur_mode
== (mode_t
)~0)
938 if (sys_acl_set_file(fname
, type
, duo_item
->sacl
) < 0) {
939 rsyserr(FERROR_XFER
, errno
, "set_acl: sys_acl_set_file(%s, %s)",
940 fname
, str_acl_type(type
));
943 if (type
== SMB_ACL_TYPE_ACCESS
)
944 sxp
->st
.st_mode
= cur_mode
;
950 /* Set ACL on indicated filename.
952 * This sets extended access ACL entries and default ACL. If convenient,
953 * it sets permission bits along with the access ACL and signals having
954 * done so by modifying sxp->st.st_mode.
956 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
957 * with fname set to NULL to just check if the ACL is unchanged. */
958 int set_acl(const char *fname
, const struct file_struct
*file
, stat_x
*sxp
)
964 if (!dry_run
&& (read_only
|| list_only
)) {
970 if (ndx
>= 0 && (size_t)ndx
< access_acl_list
.count
) {
971 acl_duo
*duo_item
= access_acl_list
.items
;
974 && rsync_acl_equal_enough(sxp
->acc_acl
, &duo_item
->racl
, file
->mode
);
977 if (!dry_run
&& fname
978 && set_rsync_acl(fname
, duo_item
, SMB_ACL_TYPE_ACCESS
,
979 sxp
, file
->mode
) < 0)
984 if (!S_ISDIR(sxp
->st
.st_mode
))
987 ndx
= F_DIR_DEFACL(file
);
988 if (ndx
>= 0 && (size_t)ndx
< default_acl_list
.count
) {
989 acl_duo
*duo_item
= default_acl_list
.items
;
991 eq
= sxp
->def_acl
&& rsync_acl_equal(sxp
->def_acl
, &duo_item
->racl
);
995 if (!dry_run
&& fname
996 && set_rsync_acl(fname
, duo_item
, SMB_ACL_TYPE_DEFAULT
,
997 sxp
, file
->mode
) < 0)
1005 /* Non-incremental recursion needs to convert all the received IDs.
1006 * This is done in a single pass after receiving the whole file-list. */
1007 static void match_racl_ids(const item_list
*racl_list
)
1009 int list_cnt
, name_cnt
;
1010 acl_duo
*duo_item
= racl_list
->items
;
1011 for (list_cnt
= racl_list
->count
; list_cnt
--; duo_item
++) {
1012 ida_entries
*idal
= &duo_item
->racl
.names
;
1013 id_access
*ida
= idal
->idas
;
1014 for (name_cnt
= idal
->count
; name_cnt
--; ida
++) {
1015 if (ida
->access
& NAME_IS_USER
)
1016 ida
->id
= match_uid(ida
->id
);
1018 ida
->id
= match_gid(ida
->id
, NULL
);
1023 void match_acl_ids(void)
1025 match_racl_ids(&access_acl_list
);
1026 match_racl_ids(&default_acl_list
);
1029 /* This is used by dest_mode(). */
1030 int default_perms_for_dir(const char *dir
)
1039 perms
= ACCESSPERMS
& ~orig_umask
;
1040 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1041 sacl
= sys_acl_get_file(dir
, SMB_ACL_TYPE_DEFAULT
);
1043 /* Couldn't get an ACL. Darn. */
1049 /* No ACLs are available. */
1053 /* We're doing a dry run, so the containing directory
1054 * wasn't actually created. Don't worry about it. */
1057 /* Otherwise fall through. */
1060 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1061 dir
, str_acl_type(SMB_ACL_TYPE_DEFAULT
), strerror(errno
));
1067 racl
= empty_rsync_acl
;
1068 ok
= unpack_smb_acl(sacl
, &racl
);
1069 sys_acl_free_acl(sacl
);
1071 rprintf(FWARNING
, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1075 /* Apply the permission-bit entries of the default ACL, if any. */
1076 if (racl
.user_obj
!= NO_ENTRY
) {
1077 perms
= rsync_acl_get_perms(&racl
);
1079 rprintf(FINFO
, "got ACL-based default perms %o for directory %s\n", perms
, dir
);
1082 rsync_acl_free(&racl
);
1086 #endif /* SUPPORT_ACLS */