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 static size_t prior_access_count = (size_t)-1;
92 static size_t prior_default_count = (size_t)-1;
94 /* === Calculations on ACL types === */
96 static const char *str_acl_type(SMB_ACL_TYPE_T type)
99 case SMB_ACL_TYPE_ACCESS:
101 return "ACL_TYPE_EXTENDED";
103 return "ACL_TYPE_ACCESS";
105 case SMB_ACL_TYPE_DEFAULT:
106 return "ACL_TYPE_DEFAULT";
110 return "unknown ACL type!";
113 static int calc_sacl_entries(const rsync_acl *racl)
115 /* A System ACL always gets user/group/other permission entries. */
116 return racl->names.count
117 #ifdef ACLS_NEED_MASK
120 + (racl->mask_obj != NO_ENTRY)
125 /* Extracts and returns the permission bits from the ACL. This cannot be
126 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
127 static int rsync_acl_get_perms(const rsync_acl *racl)
129 return (racl->user_obj << 6)
130 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
134 /* Removes the permission-bit entries from the ACL because these
135 * can be reconstructed from the file's mode. */
136 static void rsync_acl_strip_perms(rsync_acl *racl)
138 racl->user_obj = NO_ENTRY;
139 if (racl->mask_obj == NO_ENTRY)
140 racl->group_obj = NO_ENTRY;
142 if (racl->group_obj == racl->mask_obj)
143 racl->group_obj = NO_ENTRY;
144 if (racl->names.count != 0)
145 racl->mask_obj = NO_ENTRY;
147 racl->other_obj = NO_ENTRY;
150 /* Given an empty rsync_acl, fake up the permission bits. */
151 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
153 racl->user_obj = (mode >> 6) & 7;
154 racl->group_obj = (mode >> 3) & 7;
155 racl->other_obj = mode & 7;
158 /* === Rsync ACL functions === */
160 static rsync_acl *create_racl(void)
162 rsync_acl *racl = new(rsync_acl);
165 out_of_memory("create_racl");
166 *racl = empty_rsync_acl;
171 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
173 id_access *ida1, *ida2;
174 int count = ial1->count;
175 if (count != ial2->count)
179 for (; count--; ida1++, ida2++) {
180 if (ida1->access != ida2->access || ida1->id != ida2->id)
186 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
188 return racl1->user_obj == racl2->user_obj
189 && racl1->group_obj == racl2->group_obj
190 && racl1->mask_obj == racl2->mask_obj
191 && racl1->other_obj == racl2->other_obj
192 && ida_entries_equal(&racl1->names, &racl2->names);
195 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
196 * the ACL will be handled by the normal mode-preservation code. This is
197 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
198 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
199 * that it might have several of its permission objects set to NO_ENTRY. */
200 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
201 const rsync_acl *racl2, mode_t m)
203 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
204 return False; /* One has a mask and the other doesn't */
206 /* When there's a mask, the group_obj becomes an extended entry. */
207 if (racl1->mask_obj != NO_ENTRY) {
208 /* A condensed rsync_acl with a mask can only have no
209 * group_obj when it was identical to the mask. This
210 * means that it was also identical to the group attrs
212 if (racl2->group_obj == NO_ENTRY) {
213 if (racl1->group_obj != ((m >> 3) & 7))
215 } else if (racl1->group_obj != racl2->group_obj)
218 return ida_entries_equal(&racl1->names, &racl2->names);
221 static void rsync_acl_free(rsync_acl *racl)
223 if (racl->names.idas)
224 free(racl->names.idas);
225 *racl = empty_rsync_acl;
228 void free_acl(stat_x *sxp)
231 rsync_acl_free(sxp->acc_acl);
236 rsync_acl_free(sxp->def_acl);
242 #ifdef SMB_ACL_NEED_SORT
243 static int id_access_sorter(const void *r1, const void *r2)
245 id_access *ida1 = (id_access *)r1;
246 id_access *ida2 = (id_access *)r2;
247 id_t rid1 = ida1->id, rid2 = ida2->id;
248 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
249 return ida1->access & NAME_IS_USER ? -1 : 1;
250 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
254 /* === System ACLs === */
256 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
257 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
259 static item_list temp_ida_list = EMPTY_ITEM_LIST;
260 SMB_ACL_ENTRY_T entry;
264 errfun = "sys_acl_get_entry";
265 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
267 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
268 SMB_ACL_TAG_T tag_type;
272 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
273 errfun = "sys_acl_get_info";
276 /* continue == done with entry; break == store in temporary ida list */
278 #ifndef HAVE_OSX_ACLS
279 case SMB_ACL_USER_OBJ:
280 if (racl->user_obj == NO_ENTRY)
281 racl->user_obj = access;
283 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
285 case SMB_ACL_GROUP_OBJ:
286 if (racl->group_obj == NO_ENTRY)
287 racl->group_obj = access;
289 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
292 if (racl->mask_obj == NO_ENTRY)
293 racl->mask_obj = access;
295 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
298 if (racl->other_obj == NO_ENTRY)
299 racl->other_obj = access;
301 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
305 access |= NAME_IS_USER;
310 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
313 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
315 ida->access = access;
318 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
319 rsync_acl_free(racl);
323 /* Transfer the count id_access items out of the temp_ida_list
324 * into the names ida_entries list in racl. */
325 if (temp_ida_list.count) {
326 #ifdef SMB_ACL_NEED_SORT
327 if (temp_ida_list.count > 1) {
328 qsort(temp_ida_list.items, temp_ida_list.count,
329 sizeof (id_access), id_access_sorter);
332 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
333 out_of_memory("unpack_smb_acl");
334 memcpy(racl->names.idas, temp_ida_list.items,
335 temp_ida_list.count * sizeof (id_access));
337 racl->names.idas = NULL;
339 racl->names.count = temp_ida_list.count;
341 /* Truncate the temporary list now that its idas have been saved. */
342 temp_ida_list.count = 0;
344 #ifdef ACLS_NEED_MASK
345 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
346 /* Throw away a superfluous mask, but mask off the
347 * group perms with it first. */
348 racl->group_obj &= racl->mask_obj;
349 racl->mask_obj = NO_ENTRY;
356 /* Synactic sugar for system calls */
358 #define CALL_OR_ERROR(func,args,str) \
366 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
367 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
369 #ifndef HAVE_OSX_ACLS
370 /* Store the permissions in the system ACL entry. */
371 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
373 if (sys_acl_set_access_bits(entry, access)) {
374 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
381 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
382 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
384 #ifdef ACLS_NEED_MASK
389 const char *errfun = NULL;
390 SMB_ACL_ENTRY_T entry;
392 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
393 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
397 #ifndef HAVE_OSX_ACLS
398 COE( sys_acl_create_entry,(smb_acl, &entry) );
399 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
402 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
403 #ifdef SMB_ACL_NEED_SORT
404 if (!(ida->access & NAME_IS_USER))
407 COE( sys_acl_create_entry,(smb_acl, &entry) );
408 COE( sys_acl_set_info,
410 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
411 ida->access & ~NAME_IS_USER, ida->id) );
414 #ifndef HAVE_OSX_ACLS
415 COE( sys_acl_create_entry,(smb_acl, &entry) );
416 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
418 #ifdef SMB_ACL_NEED_SORT
419 for ( ; count; ida++, count--) {
420 COE( sys_acl_create_entry,(smb_acl, &entry) );
421 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
425 #ifdef ACLS_NEED_MASK
426 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
427 COE( sys_acl_create_entry,(smb_acl, &entry) );
428 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
430 if (racl->mask_obj != NO_ENTRY) {
431 COE( sys_acl_create_entry,(smb_acl, &entry) );
432 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
436 COE( sys_acl_create_entry,(smb_acl, &entry) );
437 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
441 if (sys_acl_valid(*smb_acl) < 0)
442 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
449 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
451 sys_acl_free_acl(*smb_acl);
455 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
456 const item_list *racl_list)
458 static int access_match = -1, default_match = -1;
459 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
460 size_t count = racl_list->count;
462 /* If this is the first time through or we didn't match the last
463 * time, then start at the end of the list, which should be the
464 * best place to start hunting. */
466 *match = racl_list->count - 1;
468 rsync_acl *base = racl_list->items;
469 if (rsync_acl_equal(base + *match, racl))
472 *match = racl_list->count - 1;
479 static int get_rsync_acl(const char *fname, rsync_acl *racl,
480 SMB_ACL_TYPE_T type, mode_t mode)
484 #ifdef SUPPORT_XATTRS
485 /* --fake-super support: load ACLs from an xattr. */
491 if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
493 cnt = (len - 4*4) / (4+4);
494 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
499 racl->user_obj = IVAL(buf, 0);
500 racl->group_obj = IVAL(buf, 4);
501 racl->mask_obj = IVAL(buf, 8);
502 racl->other_obj = IVAL(buf, 12);
505 char *bp = buf + 4*4;
507 if (!(ida = racl->names.idas = new_array(id_access, cnt)))
508 out_of_memory("get_rsync_acl");
509 racl->names.count = cnt;
510 for ( ; cnt--; ida++, bp += 4+4) {
511 ida->id = IVAL(bp, 0);
512 ida->access = IVAL(bp, 4);
520 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
521 BOOL ok = unpack_smb_acl(sacl, racl);
523 sys_acl_free_acl(sacl);
527 } else if (no_acl_syscall_error(errno)) {
528 /* ACLs are not supported, so pretend we have a basic ACL. */
529 if (type == SMB_ACL_TYPE_ACCESS)
530 rsync_acl_fake_perms(racl, mode);
532 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
533 fname, str_acl_type(type));
540 /* Return the Access Control List for the given filename. */
541 int get_acl(const char *fname, stat_x *sxp)
543 sxp->acc_acl = create_racl();
544 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
545 sxp->st.st_mode) < 0) {
550 if (S_ISDIR(sxp->st.st_mode)) {
551 sxp->def_acl = create_racl();
552 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
553 sxp->st.st_mode) < 0) {
562 /* === Send functions === */
564 /* Send the ida list over the file descriptor. */
565 static void send_ida_entries(int f, const ida_entries *idal)
568 size_t count = idal->count;
570 write_varint(f, idal->count);
572 for (ida = idal->idas; count--; ida++) {
573 uint32 xbits = ida->access << 2;
575 if (ida->access & NAME_IS_USER) {
576 xbits |= XFLAG_NAME_IS_USER;
577 name = add_uid(ida->id);
579 name = add_gid(ida->id);
580 write_varint(f, ida->id);
581 if (inc_recurse && name) {
582 int len = strlen(name);
583 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
585 write_buf(f, name, len);
587 write_varint(f, xbits);
591 static void send_rsync_acl(int f, rsync_acl *racl, SMB_ACL_TYPE_T type,
592 item_list *racl_list)
594 int ndx = find_matching_rsync_acl(racl, type, racl_list);
596 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
597 write_varint(f, ndx + 1);
600 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
603 if (racl->user_obj != NO_ENTRY)
604 flags |= XMIT_USER_OBJ;
605 if (racl->group_obj != NO_ENTRY)
606 flags |= XMIT_GROUP_OBJ;
607 if (racl->mask_obj != NO_ENTRY)
608 flags |= XMIT_MASK_OBJ;
609 if (racl->other_obj != NO_ENTRY)
610 flags |= XMIT_OTHER_OBJ;
611 if (racl->names.count)
612 flags |= XMIT_NAME_LIST;
614 write_byte(f, flags);
616 if (flags & XMIT_USER_OBJ)
617 write_varint(f, racl->user_obj);
618 if (flags & XMIT_GROUP_OBJ)
619 write_varint(f, racl->group_obj);
620 if (flags & XMIT_MASK_OBJ)
621 write_varint(f, racl->mask_obj);
622 if (flags & XMIT_OTHER_OBJ)
623 write_varint(f, racl->other_obj);
624 if (flags & XMIT_NAME_LIST)
625 send_ida_entries(f, &racl->names);
627 /* Give the allocated data to the new list object. */
629 *racl = empty_rsync_acl;
633 /* Send the ACL from the stat_x structure down the indicated file descriptor.
634 * This also frees the ACL data. */
635 void send_acl(int f, stat_x *sxp)
638 sxp->acc_acl = create_racl();
639 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
641 /* Avoid sending values that can be inferred from other data. */
642 rsync_acl_strip_perms(sxp->acc_acl);
644 send_rsync_acl(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
646 if (S_ISDIR(sxp->st.st_mode)) {
648 sxp->def_acl = create_racl();
650 send_rsync_acl(f, sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
654 /* === Receive functions === */
656 static uint32 recv_acl_access(int f, uchar *name_follows_ptr)
658 uint32 access = read_varint(f);
660 if (name_follows_ptr) {
661 int flags = access & 3;
663 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
665 if (flags & XFLAG_NAME_FOLLOWS)
666 *name_follows_ptr = 1;
668 *name_follows_ptr = 0;
669 if (flags & XFLAG_NAME_IS_USER)
670 access |= NAME_IS_USER;
671 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
673 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
675 exit_cleanup(RERR_STREAMIO);
681 static uchar recv_ida_entries(int f, ida_entries *ent)
683 uchar computed_mask_bits = 0;
684 int i, count = read_varint(f);
687 if (!(ent->idas = new_array(id_access, count)))
688 out_of_memory("recv_ida_entries");
694 for (i = 0; i < count; i++) {
696 id_t id = read_varint(f);
697 uint32 access = recv_acl_access(f, &has_name);
700 if (access & NAME_IS_USER)
701 id = recv_user_name(f, id);
703 id = recv_group_name(f, id, NULL);
704 } else if (access & NAME_IS_USER) {
705 if (inc_recurse && am_root && !numeric_ids)
708 if (inc_recurse && (!am_root || !numeric_ids))
709 id = match_gid(id, NULL);
712 ent->idas[i].id = id;
713 ent->idas[i].access = access;
714 computed_mask_bits |= access;
717 return computed_mask_bits & ~NO_ENTRY;
720 static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type)
722 uchar computed_mask_bits = 0;
725 int ndx = read_varint(f);
727 if (ndx < 0 || (size_t)ndx > racl_list->count) {
728 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
729 str_acl_type(type), ndx, (int)racl_list->count);
730 exit_cleanup(RERR_STREAMIO);
736 ndx = racl_list->count;
737 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
738 duo_item->racl = empty_rsync_acl;
740 flags = read_byte(f);
742 if (flags & XMIT_USER_OBJ)
743 duo_item->racl.user_obj = recv_acl_access(f, NULL);
744 if (flags & XMIT_GROUP_OBJ)
745 duo_item->racl.group_obj = recv_acl_access(f, NULL);
746 if (flags & XMIT_MASK_OBJ)
747 duo_item->racl.mask_obj = recv_acl_access(f, NULL);
748 if (flags & XMIT_OTHER_OBJ)
749 duo_item->racl.other_obj = recv_acl_access(f, NULL);
750 if (flags & XMIT_NAME_LIST)
751 computed_mask_bits |= recv_ida_entries(f, &duo_item->racl.names);
754 /* If we received a superfluous mask, throw it away. */
755 duo_item->racl.mask_obj = NO_ENTRY;
757 if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
758 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
761 duo_item->sacl = NULL;
766 /* Receive the ACL info the sender has included for this file-list entry. */
767 void receive_acl(int f, struct file_struct *file)
769 F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS);
771 if (S_ISDIR(file->mode))
772 F_DIR_DEFACL(file) = recv_rsync_acl(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT);
775 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
781 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
783 ndx = racl_list->count;
784 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
785 new_duo->racl = *racl;
786 new_duo->sacl = NULL;
787 *racl = empty_rsync_acl;
793 /* Turn the ACL data in stat_x into cached ACL data, setting the index
794 * values in the file struct. */
795 void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
797 if (prior_access_count == (size_t)-1)
798 prior_access_count = access_acl_list.count;
800 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
801 SMB_ACL_TYPE_ACCESS, &access_acl_list);
803 if (S_ISDIR(sxp->st.st_mode)) {
804 if (prior_default_count == (size_t)-1)
805 prior_default_count = default_acl_list.count;
806 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
807 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
811 static void uncache_duo_acls(item_list *duo_list, size_t start)
813 acl_duo *duo_item = duo_list->items;
814 acl_duo *duo_start = duo_item + start;
816 duo_item += duo_list->count;
817 duo_list->count = start;
819 while (duo_item-- > duo_start) {
820 rsync_acl_free(&duo_item->racl);
822 sys_acl_free_acl(duo_item->sacl);
826 void uncache_tmp_acls(void)
828 if (prior_access_count != (size_t)-1) {
829 uncache_duo_acls(&access_acl_list, prior_access_count);
830 prior_access_count = (size_t)-1;
833 if (prior_default_count != (size_t)-1) {
834 uncache_duo_acls(&default_acl_list, prior_default_count);
835 prior_default_count = (size_t)-1;
839 #ifndef HAVE_OSX_ACLS
840 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
842 SMB_ACL_ENTRY_T entry;
847 /* If the sticky bit is going on, it's not safe to allow all
848 * the new ACL to go into effect before it gets set. */
849 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
853 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
856 /* If setuid or setgid is going off, it's not safe to allow all
857 * the new ACL to go into effect before they get cleared. */
858 if ((old_mode & S_ISUID && !(mode & S_ISUID))
859 || (old_mode & S_ISGID && !(mode & S_ISGID)))
864 errfun = "sys_acl_get_entry";
865 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
867 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
868 SMB_ACL_TAG_T tag_type;
869 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
870 errfun = "sys_acl_get_tag_type";
874 case SMB_ACL_USER_OBJ:
875 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
877 case SMB_ACL_GROUP_OBJ:
878 /* group is only empty when identical to group perms. */
879 if (racl->group_obj != NO_ENTRY)
881 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
884 #ifndef ACLS_NEED_MASK
885 /* mask is only empty when we don't need it. */
886 if (racl->mask_obj == NO_ENTRY)
889 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
892 COE2( store_access_in_entry,(mode & 7, entry) );
899 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
905 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
906 /* Ensure that chmod() will be called to restore any lost setid bits. */
907 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
908 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
909 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
912 /* Return the mode of the file on disk, as we will set them. */
913 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
917 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
918 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
920 if (type == SMB_ACL_TYPE_DEFAULT
921 && duo_item->racl.user_obj == NO_ENTRY) {
923 #ifdef SUPPORT_XATTRS
924 /* --fake-super support: delete default ACL from xattrs. */
926 rc = del_def_xattr_acl(fname);
929 rc = sys_acl_delete_def_file(fname);
931 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
935 #ifdef SUPPORT_XATTRS
936 } else if (am_root < 0) {
937 /* --fake-super support: store ACLs in an xattr. */
938 int cnt = duo_item->racl.names.count;
939 size_t len = 4*4 + cnt * (4+4);
940 char *buf = new_array(char, len);
943 SIVAL(buf, 0, duo_item->racl.user_obj);
944 SIVAL(buf, 4, duo_item->racl.group_obj);
945 SIVAL(buf, 8, duo_item->racl.mask_obj);
946 SIVAL(buf, 12, duo_item->racl.other_obj);
949 char *bp = buf + 4*4;
950 id_access *ida = duo_item->racl.names.idas;
951 for ( ; cnt--; ida++, bp += 4+4) {
952 SIVAL(bp, 0, ida->id);
953 SIVAL(bp, 4, ida->access);
956 rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
961 mode_t cur_mode = sxp->st.st_mode;
963 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
966 mode = 0; /* eliminate compiler warning */
968 if (type == SMB_ACL_TYPE_ACCESS) {
969 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
971 if (cur_mode == (mode_t)-1)
975 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
976 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
977 fname, str_acl_type(type));
980 if (type == SMB_ACL_TYPE_ACCESS)
981 sxp->st.st_mode = cur_mode;
987 /* Given a fname, this sets extended access ACL entries, the default ACL (for a
988 * dir), and the regular mode bits on the file. Call this with fname set to
989 * NULL to just check if the ACL is different.
991 * If the ACL operation has a side-effect of changing the file's mode, the
992 * sxp->st.st_mode value will be changed to match.
994 * Returns 0 for an unchanged ACL, 1 for changed, -1 for failed. */
995 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp, mode_t new_mode)
1001 if (!dry_run && (read_only || list_only)) {
1007 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
1008 acl_duo *duo_item = access_acl_list.items;
1011 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, new_mode);
1014 if (!dry_run && fname
1015 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
1021 if (!S_ISDIR(new_mode))
1024 ndx = F_DIR_DEFACL(file);
1025 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
1026 acl_duo *duo_item = default_acl_list.items;
1028 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
1031 if (!dry_run && fname
1032 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1041 /* Non-incremental recursion needs to convert all the received IDs.
1042 * This is done in a single pass after receiving the whole file-list. */
1043 static void match_racl_ids(const item_list *racl_list)
1045 int list_cnt, name_cnt;
1046 acl_duo *duo_item = racl_list->items;
1047 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1048 ida_entries *idal = &duo_item->racl.names;
1049 id_access *ida = idal->idas;
1050 for (name_cnt = idal->count; name_cnt--; ida++) {
1051 if (ida->access & NAME_IS_USER)
1052 ida->id = match_uid(ida->id);
1054 ida->id = match_gid(ida->id, NULL);
1059 void match_acl_ids(void)
1061 match_racl_ids(&access_acl_list);
1062 match_racl_ids(&default_acl_list);
1065 /* This is used by dest_mode(). */
1066 int default_perms_for_dir(const char *dir)
1075 perms = ACCESSPERMS & ~orig_umask;
1076 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1077 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1079 /* Couldn't get an ACL. Darn. */
1085 /* No ACLs are available. */
1089 /* We're doing a dry run, so the containing directory
1090 * wasn't actually created. Don't worry about it. */
1093 /* Otherwise fall through. */
1096 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1097 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1103 racl = empty_rsync_acl;
1104 ok = unpack_smb_acl(sacl, &racl);
1105 sys_acl_free_acl(sacl);
1107 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1111 /* Apply the permission-bit entries of the default ACL, if any. */
1112 if (racl.user_obj != NO_ENTRY) {
1113 perms = rsync_acl_get_perms(&racl);
1114 if (DEBUG_GTE(ACL, 1))
1115 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1118 rsync_acl_free(&racl);
1122 #endif /* SUPPORT_ACLS */