2 * Handle passing Access Control Lists between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2006 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 version 2 as
10 * published by the Free Software Foundation.
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "lib/sysacls.h"
31 extern int orig_umask;
32 extern int protocol_version;
33 extern int numeric_ids;
34 extern int inc_recurse;
36 #define XMIT_USER_OBJ (1<<0)
37 #define XMIT_USER_LIST (1<<1)
38 #define XMIT_GROUP_OBJ (1<<2)
39 #define XMIT_GROUP_LIST (1<<3)
40 #define XMIT_MASK_OBJ (1<<4)
41 #define XMIT_OTHER_OBJ (1<<5)
43 /* === ACL structures === */
60 #define NO_ENTRY ((uchar)0x80)
61 typedef struct rsync_acl {
64 /* These will be NO_ENTRY if there's no such entry. */
76 static const rsync_acl empty_rsync_acl = {
77 {NULL, 0}, {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
80 static item_list access_acl_list = EMPTY_ITEM_LIST;
81 static item_list default_acl_list = EMPTY_ITEM_LIST;
83 /* === Calculations on ACL types === */
85 static const char *str_acl_type(SMB_ACL_TYPE_T type)
87 return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
88 : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
89 : "unknown SMB_ACL_TYPE_T";
92 static int calc_sacl_entries(const rsync_acl *racl)
94 /* A System ACL always gets user/group/other permission entries. */
95 return racl->users.count + racl->groups.count
99 + (racl->mask_obj != NO_ENTRY) + 3;
103 /* Extracts and returns the permission bits from the ACL. This cannot be
104 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
105 static int rsync_acl_get_perms(const rsync_acl *racl)
107 return (racl->user_obj << 6)
108 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
112 /* Removes the permission-bit entries from the ACL because these
113 * can be reconstructed from the file's mode. */
114 static void rsync_acl_strip_perms(rsync_acl *racl)
116 racl->user_obj = NO_ENTRY;
117 if (racl->mask_obj == NO_ENTRY)
118 racl->group_obj = NO_ENTRY;
120 if (racl->group_obj == racl->mask_obj)
121 racl->group_obj = NO_ENTRY;
122 racl->mask_obj = NO_ENTRY;
124 racl->other_obj = NO_ENTRY;
127 /* Given an empty rsync_acl, fake up the permission bits. */
128 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
130 racl->user_obj = (mode >> 6) & 7;
131 racl->group_obj = (mode >> 3) & 7;
132 racl->other_obj = mode & 7;
135 /* === Rsync ACL functions === */
137 static rsync_acl *create_racl(void)
139 rsync_acl *racl = new(rsync_acl);
142 out_of_memory("create_racl");
143 *racl = empty_rsync_acl;
148 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
150 id_access *ida1, *ida2;
151 int count = ial1->count;
152 if (count != ial2->count)
156 for (; count--; ida1++, ida2++) {
157 if (ida1->access != ida2->access || ida1->id != ida2->id)
163 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
165 return racl1->user_obj == racl2->user_obj
166 && racl1->group_obj == racl2->group_obj
167 && racl1->mask_obj == racl2->mask_obj
168 && racl1->other_obj == racl2->other_obj
169 && ida_entries_equal(&racl1->users, &racl2->users)
170 && ida_entries_equal(&racl1->groups, &racl2->groups);
173 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
174 * the ACL will be handled by the normal mode-preservation code. This is
175 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
176 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
177 * that it might have several of its permission objects set to NO_ENTRY. */
178 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
179 const rsync_acl *racl2, mode_t m)
181 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
182 return False; /* One has a mask and the other doesn't */
184 /* When there's a mask, the group_obj becomes an extended entry. */
185 if (racl1->mask_obj != NO_ENTRY) {
186 /* A condensed rsync_acl with a mask can only have no
187 * group_obj when it was identical to the mask. This
188 * means that it was also identical to the group attrs
190 if (racl2->group_obj == NO_ENTRY) {
191 if (racl1->group_obj != ((m >> 3) & 7))
193 } else if (racl1->group_obj != racl2->group_obj)
196 return ida_entries_equal(&racl1->users, &racl2->users)
197 && ida_entries_equal(&racl1->groups, &racl2->groups);
200 static void rsync_acl_free(rsync_acl *racl)
202 if (racl->users.idas)
203 free(racl->users.idas);
204 if (racl->groups.idas)
205 free(racl->groups.idas);
206 *racl = empty_rsync_acl;
209 void free_acl(statx *sxp)
212 rsync_acl_free(sxp->acc_acl);
217 rsync_acl_free(sxp->def_acl);
223 static int id_access_sorter(const void *r1, const void *r2)
225 id_access *ida1 = (id_access *)r1;
226 id_access *ida2 = (id_access *)r2;
227 id_t rid1 = ida1->id, rid2 = ida2->id;
228 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
231 static void sort_ida_entries(ida_entries *idal)
235 qsort(idal->idas, idal->count, sizeof idal->idas[0], id_access_sorter);
238 /* === System ACLs === */
240 /* Transfer the count id_access items out of the temp_ida_list into either
241 * the users or groups ida_entries list in racl. */
242 static void save_idas(rsync_acl *racl, SMB_ACL_TAG_T type, item_list *temp_ida_list)
247 if (temp_ida_list->count) {
248 int cnt = temp_ida_list->count;
249 id_access *temp_idas = temp_ida_list->items;
250 if (!(idas = new_array(id_access, cnt)))
251 out_of_memory("save_idas");
252 memcpy(idas, temp_idas, cnt * sizeof *temp_idas);
256 ent = type == SMB_ACL_USER ? &racl->users : &racl->groups;
259 rprintf(FERROR, "save_idas: disjoint list found for type %d\n", type);
260 exit_cleanup(RERR_UNSUPPORTED);
262 ent->count = temp_ida_list->count;
265 /* Truncate the temporary list now that its idas have been saved. */
266 temp_ida_list->count = 0;
269 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
270 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
272 static item_list temp_ida_list = EMPTY_ITEM_LIST;
273 SMB_ACL_TAG_T prior_list_type = 0;
274 SMB_ACL_ENTRY_T entry;
278 errfun = "sys_acl_get_entry";
279 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
281 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
282 SMB_ACL_TAG_T tag_type;
283 SMB_ACL_PERMSET_T permset;
287 if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
288 errfun = "sys_acl_get_tag_type";
291 if ((rc = sys_acl_get_permset(entry, &permset))) {
292 errfun = "sys_acl_get_tag_type";
295 access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
296 | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
297 | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
298 /* continue == done with entry; break == store in temporary ida list */
300 case SMB_ACL_USER_OBJ:
301 if (racl->user_obj == NO_ENTRY)
302 racl->user_obj = access;
304 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
308 case SMB_ACL_GROUP_OBJ:
309 if (racl->group_obj == NO_ENTRY)
310 racl->group_obj = access;
312 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
317 if (racl->mask_obj == NO_ENTRY)
318 racl->mask_obj = access;
320 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
323 if (racl->other_obj == NO_ENTRY)
324 racl->other_obj = access;
326 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
329 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
332 if (!(qualifier = sys_acl_get_qualifier(entry))) {
333 errfun = "sys_acl_get_tag_type";
337 if (tag_type != prior_list_type) {
339 save_idas(racl, prior_list_type, &temp_ida_list);
340 prior_list_type = tag_type;
342 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
343 ida->id = *((id_t *)qualifier);
344 ida->access = access;
345 sys_acl_free_qualifier(qualifier, tag_type);
348 rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
349 rsync_acl_free(racl);
353 save_idas(racl, prior_list_type, &temp_ida_list);
355 sort_ida_entries(&racl->users);
356 sort_ida_entries(&racl->groups);
358 #ifdef ACLS_NEED_MASK
359 if (!racl->users.count && !racl->groups.count && racl->mask_obj != NO_ENTRY) {
360 /* Throw away a superfluous mask, but mask off the
361 * group perms with it first. */
362 racl->group_obj &= racl->mask_obj;
363 racl->mask_obj = NO_ENTRY;
370 /* Synactic sugar for system calls */
372 #define CALL_OR_ERROR(func,args,str) \
380 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
381 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
383 /* Store the permissions in the system ACL entry. */
384 static int store_access_in_entry(uchar access, SMB_ACL_ENTRY_T entry)
386 const char *errfun = NULL;
387 SMB_ACL_PERMSET_T permset;
389 COE( sys_acl_get_permset,(entry, &permset) );
390 COE( sys_acl_clear_perms,(permset) );
392 COE( sys_acl_add_perm,(permset, SMB_ACL_READ) );
394 COE( sys_acl_add_perm,(permset, SMB_ACL_WRITE) );
396 COE( sys_acl_add_perm,(permset, SMB_ACL_EXECUTE) );
397 COE( sys_acl_set_permset,(entry, permset) );
402 rsyserr(FERROR, errno, "store_access_in_entry %s()", errfun);
406 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
407 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
409 #ifdef ACLS_NEED_MASK
414 const char *errfun = NULL;
415 SMB_ACL_ENTRY_T entry;
417 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
418 rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
422 COE( sys_acl_create_entry,(smb_acl, &entry) );
423 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
424 COE2( store_access_in_entry,(racl->user_obj & 7, entry) );
426 for (ida = racl->users.idas, count = racl->users.count; count--; ida++) {
427 COE( sys_acl_create_entry,(smb_acl, &entry) );
428 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
429 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
430 COE2( store_access_in_entry,(ida->access, entry) );
433 COE( sys_acl_create_entry,(smb_acl, &entry) );
434 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
435 COE2( store_access_in_entry,(racl->group_obj & 7, entry) );
437 for (ida = racl->groups.idas, count = racl->groups.count; count--; ida++) {
438 COE( sys_acl_create_entry,(smb_acl, &entry) );
439 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
440 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
441 COE2( store_access_in_entry,(ida->access, entry) );
444 #ifdef ACLS_NEED_MASK
445 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & 7 : racl->mask_obj;
446 COE( sys_acl_create_entry,(smb_acl, &entry) );
447 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
448 COE2( store_access_in_entry,(mask_bits, entry) );
450 if (racl->mask_obj != NO_ENTRY) {
451 COE( sys_acl_create_entry,(smb_acl, &entry) );
452 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
453 COE2( store_access_in_entry,(racl->mask_obj, entry) );
457 COE( sys_acl_create_entry,(smb_acl, &entry) );
458 COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
459 COE2( store_access_in_entry,(racl->other_obj & 7, entry) );
462 if (sys_acl_valid(*smb_acl) < 0)
463 rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
470 rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
472 sys_acl_free_acl(*smb_acl);
476 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
477 const item_list *racl_list)
479 static int access_match = -1, default_match = -1;
480 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
481 size_t count = racl_list->count;
483 /* If this is the first time through or we didn't match the last
484 * time, then start at the end of the list, which should be the
485 * best place to start hunting. */
487 *match = racl_list->count - 1;
489 rsync_acl *base = racl_list->items;
490 if (rsync_acl_equal(base + *match, racl))
493 *match = racl_list->count - 1;
500 static int get_rsync_acl(const char *fname, rsync_acl *racl,
501 SMB_ACL_TYPE_T type, mode_t mode)
503 SMB_ACL_T sacl = sys_acl_get_file(fname, type);
506 BOOL ok = unpack_smb_acl(sacl, racl);
508 sys_acl_free_acl(sacl);
512 } else if (no_acl_syscall_error(errno)) {
513 /* ACLs are not supported, so pretend we have a basic ACL. */
514 if (type == SMB_ACL_TYPE_ACCESS)
515 rsync_acl_fake_perms(racl, mode);
517 rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
518 fname, str_acl_type(type));
525 /* Return the Access Control List for the given filename. */
526 int get_acl(const char *fname, statx *sxp)
528 sxp->acc_acl = create_racl();
529 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
530 sxp->st.st_mode) < 0) {
535 if (S_ISDIR(sxp->st.st_mode)) {
536 sxp->def_acl = create_racl();
537 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
538 sxp->st.st_mode) < 0) {
547 /* === Send functions === */
549 /* The general strategy with the tag_type <-> character mapping is that
550 * lowercase implies that no qualifier follows, where uppercase does.
551 * A similar idiom for the ACL type (access or default) itself, but
552 * lowercase in this instance means there's no ACL following, so the
553 * ACL is a repeat, so the receiver should reuse the last of the same
556 /* Send the ida list over the file descriptor. */
557 static void send_ida_entries(const ida_entries *idal, int user_names, int f)
560 size_t count = idal->count;
562 write_varint(f, idal->count);
564 for (ida = idal->idas; count--; ida++) {
565 char *name = user_names ? add_uid(ida->id) : add_gid(ida->id);
566 write_varint(f, ida->id);
567 if (inc_recurse && name) {
568 int len = strlen(name);
569 write_byte(f, ida->access | (uchar)0x80);
571 write_buf(f, name, len);
573 write_byte(f, ida->access);
577 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
578 item_list *racl_list, int f)
580 int ndx = find_matching_rsync_acl(racl, type, racl_list);
582 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
583 write_varint(f, ndx + 1);
586 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
589 if (racl->user_obj != NO_ENTRY)
590 flags |= XMIT_USER_OBJ;
591 if (racl->users.count)
592 flags |= XMIT_USER_LIST;
594 if (racl->group_obj != NO_ENTRY)
595 flags |= XMIT_GROUP_OBJ;
596 if (racl->groups.count)
597 flags |= XMIT_GROUP_LIST;
599 if (racl->mask_obj != NO_ENTRY)
600 flags |= XMIT_MASK_OBJ;
602 if (racl->other_obj != NO_ENTRY)
603 flags |= XMIT_OTHER_OBJ;
605 write_byte(f, flags);
607 if (flags & XMIT_USER_OBJ)
608 write_byte(f, racl->user_obj);
609 if (flags & XMIT_USER_LIST)
610 send_ida_entries(&racl->users, 1, f);
612 if (flags & XMIT_GROUP_OBJ)
613 write_byte(f, racl->group_obj);
614 if (flags & XMIT_GROUP_LIST)
615 send_ida_entries(&racl->groups, 0, f);
617 if (flags & XMIT_MASK_OBJ)
618 write_byte(f, racl->mask_obj);
620 if (flags & XMIT_OTHER_OBJ)
621 write_byte(f, racl->other_obj);
623 /* Give the allocated data to the new list object. */
625 *racl = empty_rsync_acl;
629 /* Send the ACL from the statx structure down the indicated file descriptor.
630 * This also frees the ACL data. */
631 void send_acl(statx *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 uchar recv_acl_access(uchar *name_follows_val, int f)
654 uchar access = read_byte(f);
656 if (name_follows_val) {
657 if (access & (uchar)0x80) {
658 access &= ~(uchar)0x80;
659 *name_follows_val = 1;
661 *name_follows_val = 0;
664 if (access & ~(4 | 2 | 1)) {
665 rprintf(FERROR, "recv_acl_access: bogus permset %o\n", access);
666 exit_cleanup(RERR_STREAMIO);
672 static uchar recv_ida_entries(ida_entries *ent, int user_names, int f)
674 uchar computed_mask_bits = 0;
675 int i, count = read_varint(f);
678 if (!(ent->idas = new_array(id_access, ent->count)))
679 out_of_memory("recv_ida_entries");
685 for (i = 0; i < count; i++) {
687 id_t id = read_varint(f);
688 int access = recv_acl_access(&has_name, f);
692 id = recv_user_name(f, id);
694 id = recv_group_name(f, id);
695 } else if (user_names) {
696 if (inc_recurse && am_root && !numeric_ids)
699 if (inc_recurse && (!am_root || !numeric_ids))
703 ent->idas[i].id = id;
704 ent->idas[i].access = access;
705 computed_mask_bits |= access;
708 return computed_mask_bits;
711 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
713 uchar computed_mask_bits = 0;
716 int ndx = read_varint(f);
718 if (ndx < 0 || (size_t)ndx > racl_list->count) {
719 rprintf(FERROR, "recv_acl_index: %s ACL index %d > %d\n",
720 str_acl_type(type), ndx, (int)racl_list->count);
721 exit_cleanup(RERR_STREAMIO);
727 ndx = racl_list->count;
728 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
729 duo_item->racl = empty_rsync_acl;
731 flags = read_byte(f);
733 if (flags & XMIT_USER_OBJ)
734 duo_item->racl.user_obj = recv_acl_access(NULL, f);
736 if (flags & XMIT_USER_LIST)
737 computed_mask_bits |= recv_ida_entries(&duo_item->racl.users, 1, f);
739 if (flags & XMIT_GROUP_OBJ)
740 duo_item->racl.group_obj = recv_acl_access(NULL, f);
742 if (flags & XMIT_GROUP_LIST)
743 computed_mask_bits |= recv_ida_entries(&duo_item->racl.groups, 0, f);
745 if (flags & XMIT_MASK_OBJ)
746 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
748 if (flags & XMIT_OTHER_OBJ)
749 duo_item->racl.other_obj = recv_acl_access(NULL, f);
751 if (!duo_item->racl.users.count && !duo_item->racl.groups.count) {
752 /* If we received a superfluous mask, throw it away. */
753 if (duo_item->racl.mask_obj != NO_ENTRY) {
754 /* Mask off the group perms with it first. */
755 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
756 duo_item->racl.mask_obj = NO_ENTRY;
758 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
759 duo_item->racl.mask_obj = computed_mask_bits | (duo_item->racl.group_obj & 7);
761 duo_item->sacl = NULL;
766 /* Receive the ACL info the sender has included for this file-list entry. */
767 void receive_acl(struct file_struct *file, int f)
769 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
771 if (S_ISDIR(file->mode))
772 F_DEF_ACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
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 statx into cached ACL data, setting the index
794 * values in the file struct. */
795 void cache_acl(struct file_struct *file, statx *sxp)
797 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
798 SMB_ACL_TYPE_ACCESS, &access_acl_list);
800 if (S_ISDIR(sxp->st.st_mode)) {
801 F_DEF_ACL(file) = cache_rsync_acl(sxp->def_acl,
802 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
806 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
808 SMB_ACL_ENTRY_T entry;
813 /* If the sticky bit is going on, it's not safe to allow all
814 * the new ACL to go into effect before it gets set. */
815 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
819 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
822 /* If setuid or setgid is going off, it's not safe to allow all
823 * the new ACL to go into effect before they get cleared. */
824 if ((old_mode & S_ISUID && !(mode & S_ISUID))
825 || (old_mode & S_ISGID && !(mode & S_ISGID)))
830 errfun = "sys_acl_get_entry";
831 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
833 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
834 SMB_ACL_TAG_T tag_type;
835 if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
836 errfun = "sys_acl_get_tag_type";
840 case SMB_ACL_USER_OBJ:
841 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
843 case SMB_ACL_GROUP_OBJ:
844 /* group is only empty when identical to group perms. */
845 if (racl->group_obj != NO_ENTRY)
847 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
850 #ifndef ACLS_NEED_MASK
851 /* mask is only empty when we don't need it. */
852 if (racl->mask_obj == NO_ENTRY)
855 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
858 COE2( store_access_in_entry,(mode & 7, entry) );
865 rsyserr(FERROR, errno, "change_sacl_perms: %s()",
871 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
872 /* Ensure that chmod() will be called to restore any lost setid bits. */
873 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
874 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
875 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
878 /* Return the mode of the file on disk, as we will set them. */
879 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
882 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
883 SMB_ACL_TYPE_T type, statx *sxp, mode_t mode)
885 if (type == SMB_ACL_TYPE_DEFAULT
886 && duo_item->racl.user_obj == NO_ENTRY) {
887 if (sys_acl_delete_def_file(fname) < 0) {
888 rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
893 mode_t cur_mode = sxp->st.st_mode;
895 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
897 if (type == SMB_ACL_TYPE_ACCESS) {
898 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
900 if (cur_mode == (mode_t)~0)
903 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
904 rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
905 fname, str_acl_type(type));
908 if (type == SMB_ACL_TYPE_ACCESS)
909 sxp->st.st_mode = cur_mode;
915 /* Set ACL on indicated filename.
917 * This sets extended access ACL entries and default ACL. If convenient,
918 * it sets permission bits along with the access ACL and signals having
919 * done so by modifying sxp->st.st_mode.
921 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
922 * with fname set to NULL to just check if the ACL is unchanged. */
923 int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
929 if (!dry_run && (read_only || list_only)) {
935 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
936 acl_duo *duo_item = access_acl_list.items;
939 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
942 if (!dry_run && fname
943 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
944 sxp, file->mode) < 0)
949 if (!S_ISDIR(sxp->st.st_mode))
952 ndx = F_DEF_ACL(file);
953 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
954 acl_duo *duo_item = default_acl_list.items;
956 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
960 if (!dry_run && fname
961 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
962 sxp, file->mode) < 0)
970 /* Non-incremental recursion needs to convert all the received IDs
971 * in a single pass after the file-list is complete. */
972 static void match_racl_ids(const item_list *racl_list)
974 int list_cnt, name_cnt;
975 acl_duo *duo_item = racl_list->items;
976 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
977 ida_entries *idal = &duo_item->racl.users;
978 for (name_cnt = idal->count; name_cnt--; idal++) {
979 id_access *ida = idal->idas;
980 ida->id = match_uid(ida->id);
982 idal = &duo_item->racl.groups;
983 for (name_cnt = idal->count; name_cnt--; idal++) {
984 id_access *ida = idal->idas;
985 ida->id = match_gid(ida->id);
990 void match_acl_ids(void)
992 match_racl_ids(&access_acl_list);
993 match_racl_ids(&default_acl_list);
996 /* This is used by dest_mode(). */
997 int default_perms_for_dir(const char *dir)
1006 perms = ACCESSPERMS & ~orig_umask;
1007 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1008 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1010 /* Couldn't get an ACL. Darn. */
1016 /* No ACLs are available. */
1020 /* We're doing a dry run, so the containing directory
1021 * wasn't actually created. Don't worry about it. */
1024 /* Otherwise fall through. */
1026 rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1027 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1033 racl = empty_rsync_acl;
1034 ok = unpack_smb_acl(sacl, &racl);
1035 sys_acl_free_acl(sacl);
1037 rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1041 /* Apply the permission-bit entries of the default ACL, if any. */
1042 if (racl.user_obj != NO_ENTRY) {
1043 perms = rsync_acl_get_perms(&racl);
1045 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1048 rsync_acl_free(&racl);
1052 #endif /* SUPPORT_ACLS */