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 /* Flags used to indicate what items are being transmitted for an entry. */
37 #define XMIT_USER_OBJ (1<<0)
38 #define XMIT_USER_LIST (1<<1)
39 #define XMIT_GROUP_OBJ (1<<2)
40 #define XMIT_GROUP_LIST (1<<3)
41 #define XMIT_MASK_OBJ (1<<4)
42 #define XMIT_OTHER_OBJ (1<<5)
44 #define NO_ENTRY ((uchar)0x80)
46 /* === ACL structures === */
63 typedef struct rsync_acl {
66 /* These will be NO_ENTRY if there's no such entry. */
78 static const rsync_acl empty_rsync_acl = {
79 {NULL, 0}, {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
82 static item_list access_acl_list = EMPTY_ITEM_LIST;
83 static item_list default_acl_list = EMPTY_ITEM_LIST;
85 /* === Calculations on ACL types === */
87 static const char *str_acl_type(SMB_ACL_TYPE_T type)
89 return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
90 : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
91 : "unknown SMB_ACL_TYPE_T";
94 static int calc_sacl_entries(const rsync_acl *racl)
96 /* A System ACL always gets user/group/other permission entries. */
97 return racl->users.count + racl->groups.count
101 + (racl->mask_obj != NO_ENTRY) + 3;
105 /* Extracts and returns the permission bits from the ACL. This cannot be
106 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
107 static int rsync_acl_get_perms(const rsync_acl *racl)
109 return (racl->user_obj << 6)
110 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
114 /* Removes the permission-bit entries from the ACL because these
115 * can be reconstructed from the file's mode. */
116 static void rsync_acl_strip_perms(rsync_acl *racl)
118 racl->user_obj = NO_ENTRY;
119 if (racl->mask_obj == NO_ENTRY)
120 racl->group_obj = NO_ENTRY;
122 if (racl->group_obj == racl->mask_obj)
123 racl->group_obj = NO_ENTRY;
124 racl->mask_obj = NO_ENTRY;
126 racl->other_obj = NO_ENTRY;
129 /* Given an empty rsync_acl, fake up the permission bits. */
130 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
132 racl->user_obj = (mode >> 6) & 7;
133 racl->group_obj = (mode >> 3) & 7;
134 racl->other_obj = mode & 7;
137 /* === Rsync ACL functions === */
139 static rsync_acl *create_racl(void)
141 rsync_acl *racl = new(rsync_acl);
144 out_of_memory("create_racl");
145 *racl = empty_rsync_acl;
150 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
152 id_access *ida1, *ida2;
153 int count = ial1->count;
154 if (count != ial2->count)
158 for (; count--; ida1++, ida2++) {
159 if (ida1->access != ida2->access || ida1->id != ida2->id)
165 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
167 return racl1->user_obj == racl2->user_obj
168 && racl1->group_obj == racl2->group_obj
169 && racl1->mask_obj == racl2->mask_obj
170 && racl1->other_obj == racl2->other_obj
171 && ida_entries_equal(&racl1->users, &racl2->users)
172 && ida_entries_equal(&racl1->groups, &racl2->groups);
175 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
176 * the ACL will be handled by the normal mode-preservation code. This is
177 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
178 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
179 * that it might have several of its permission objects set to NO_ENTRY. */
180 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
181 const rsync_acl *racl2, mode_t m)
183 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
184 return False; /* One has a mask and the other doesn't */
186 /* When there's a mask, the group_obj becomes an extended entry. */
187 if (racl1->mask_obj != NO_ENTRY) {
188 /* A condensed rsync_acl with a mask can only have no
189 * group_obj when it was identical to the mask. This
190 * means that it was also identical to the group attrs
192 if (racl2->group_obj == NO_ENTRY) {
193 if (racl1->group_obj != ((m >> 3) & 7))
195 } else if (racl1->group_obj != racl2->group_obj)
198 return ida_entries_equal(&racl1->users, &racl2->users)
199 && ida_entries_equal(&racl1->groups, &racl2->groups);
202 static void rsync_acl_free(rsync_acl *racl)
204 if (racl->users.idas)
205 free(racl->users.idas);
206 if (racl->groups.idas)
207 free(racl->groups.idas);
208 *racl = empty_rsync_acl;
211 void free_acl(statx *sxp)
214 rsync_acl_free(sxp->acc_acl);
219 rsync_acl_free(sxp->def_acl);
225 static int id_access_sorter(const void *r1, const void *r2)
227 id_access *ida1 = (id_access *)r1;
228 id_access *ida2 = (id_access *)r2;
229 id_t rid1 = ida1->id, rid2 = ida2->id;
230 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
233 static void sort_ida_entries(ida_entries *idal)
237 qsort(idal->idas, idal->count, sizeof idal->idas[0], id_access_sorter);
240 /* === System ACLs === */
242 /* Transfer the count id_access items out of the temp_ida_list into either
243 * the users or groups ida_entries list in racl. */
244 static void save_idas(rsync_acl *racl, SMB_ACL_TAG_T type, item_list *temp_ida_list)
249 if (temp_ida_list->count) {
250 int cnt = temp_ida_list->count;
251 id_access *temp_idas = temp_ida_list->items;
252 if (!(idas = new_array(id_access, cnt)))
253 out_of_memory("save_idas");
254 memcpy(idas, temp_idas, cnt * sizeof *temp_idas);
258 ent = type == SMB_ACL_USER ? &racl->users : &racl->groups;
261 rprintf(FERROR, "save_idas: disjoint list found for type %d\n", type);
262 exit_cleanup(RERR_UNSUPPORTED);
264 ent->count = temp_ida_list->count;
267 /* Truncate the temporary list now that its idas have been saved. */
268 temp_ida_list->count = 0;
271 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
272 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
274 static item_list temp_ida_list = EMPTY_ITEM_LIST;
275 SMB_ACL_TAG_T prior_list_type = 0;
276 SMB_ACL_ENTRY_T entry;
280 errfun = "sys_acl_get_entry";
281 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
283 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
284 SMB_ACL_TAG_T tag_type;
285 SMB_ACL_PERMSET_T permset;
289 if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
290 errfun = "sys_acl_get_tag_type";
293 if ((rc = sys_acl_get_permset(entry, &permset))) {
294 errfun = "sys_acl_get_tag_type";
297 access = (sys_acl_get_perm(permset, SMB_ACL_READ) ? 4 : 0)
298 | (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? 2 : 0)
299 | (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? 1 : 0);
300 /* continue == done with entry; break == store in temporary ida list */
302 case SMB_ACL_USER_OBJ:
303 if (racl->user_obj == NO_ENTRY)
304 racl->user_obj = access;
306 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
310 case SMB_ACL_GROUP_OBJ:
311 if (racl->group_obj == NO_ENTRY)
312 racl->group_obj = access;
314 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
319 if (racl->mask_obj == NO_ENTRY)
320 racl->mask_obj = access;
322 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
325 if (racl->other_obj == NO_ENTRY)
326 racl->other_obj = access;
328 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
331 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
334 if (!(qualifier = sys_acl_get_qualifier(entry))) {
335 errfun = "sys_acl_get_tag_type";
339 if (tag_type != prior_list_type) {
341 save_idas(racl, prior_list_type, &temp_ida_list);
342 prior_list_type = tag_type;
344 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
345 ida->id = *((id_t *)qualifier);
346 ida->access = access;
347 sys_acl_free_qualifier(qualifier, tag_type);
350 rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
351 rsync_acl_free(racl);
355 save_idas(racl, prior_list_type, &temp_ida_list);
357 sort_ida_entries(&racl->users);
358 sort_ida_entries(&racl->groups);
360 #ifdef ACLS_NEED_MASK
361 if (!racl->users.count && !racl->groups.count && racl->mask_obj != NO_ENTRY) {
362 /* Throw away a superfluous mask, but mask off the
363 * group perms with it first. */
364 racl->group_obj &= racl->mask_obj;
365 racl->mask_obj = NO_ENTRY;
372 /* Synactic sugar for system calls */
374 #define CALL_OR_ERROR(func,args,str) \
382 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
383 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
385 /* Store the permissions in the system ACL entry. */
386 static int store_access_in_entry(uchar access, SMB_ACL_ENTRY_T entry)
388 const char *errfun = NULL;
389 SMB_ACL_PERMSET_T permset;
391 COE( sys_acl_get_permset,(entry, &permset) );
392 COE( sys_acl_clear_perms,(permset) );
394 COE( sys_acl_add_perm,(permset, SMB_ACL_READ) );
396 COE( sys_acl_add_perm,(permset, SMB_ACL_WRITE) );
398 COE( sys_acl_add_perm,(permset, SMB_ACL_EXECUTE) );
399 COE( sys_acl_set_permset,(entry, permset) );
404 rsyserr(FERROR, errno, "store_access_in_entry %s()", errfun);
408 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
409 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
411 #ifdef ACLS_NEED_MASK
416 const char *errfun = NULL;
417 SMB_ACL_ENTRY_T entry;
419 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
420 rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
424 COE( sys_acl_create_entry,(smb_acl, &entry) );
425 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
426 COE2( store_access_in_entry,(racl->user_obj & 7, entry) );
428 for (ida = racl->users.idas, count = racl->users.count; count--; ida++) {
429 COE( sys_acl_create_entry,(smb_acl, &entry) );
430 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
431 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
432 COE2( store_access_in_entry,(ida->access, entry) );
435 COE( sys_acl_create_entry,(smb_acl, &entry) );
436 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
437 COE2( store_access_in_entry,(racl->group_obj & 7, entry) );
439 for (ida = racl->groups.idas, count = racl->groups.count; count--; ida++) {
440 COE( sys_acl_create_entry,(smb_acl, &entry) );
441 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
442 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
443 COE2( store_access_in_entry,(ida->access, entry) );
446 #ifdef ACLS_NEED_MASK
447 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & 7 : racl->mask_obj;
448 COE( sys_acl_create_entry,(smb_acl, &entry) );
449 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
450 COE2( store_access_in_entry,(mask_bits, entry) );
452 if (racl->mask_obj != NO_ENTRY) {
453 COE( sys_acl_create_entry,(smb_acl, &entry) );
454 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
455 COE2( store_access_in_entry,(racl->mask_obj, entry) );
459 COE( sys_acl_create_entry,(smb_acl, &entry) );
460 COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
461 COE2( store_access_in_entry,(racl->other_obj & 7, entry) );
464 if (sys_acl_valid(*smb_acl) < 0)
465 rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
472 rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
474 sys_acl_free_acl(*smb_acl);
478 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
479 const item_list *racl_list)
481 static int access_match = -1, default_match = -1;
482 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
483 size_t count = racl_list->count;
485 /* If this is the first time through or we didn't match the last
486 * time, then start at the end of the list, which should be the
487 * best place to start hunting. */
489 *match = racl_list->count - 1;
491 rsync_acl *base = racl_list->items;
492 if (rsync_acl_equal(base + *match, racl))
495 *match = racl_list->count - 1;
502 static int get_rsync_acl(const char *fname, rsync_acl *racl,
503 SMB_ACL_TYPE_T type, mode_t mode)
505 SMB_ACL_T sacl = sys_acl_get_file(fname, type);
508 BOOL ok = unpack_smb_acl(sacl, racl);
510 sys_acl_free_acl(sacl);
514 } else if (no_acl_syscall_error(errno)) {
515 /* ACLs are not supported, so pretend we have a basic ACL. */
516 if (type == SMB_ACL_TYPE_ACCESS)
517 rsync_acl_fake_perms(racl, mode);
519 rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
520 fname, str_acl_type(type));
527 /* Return the Access Control List for the given filename. */
528 int get_acl(const char *fname, statx *sxp)
530 sxp->acc_acl = create_racl();
531 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
532 sxp->st.st_mode) < 0) {
537 if (S_ISDIR(sxp->st.st_mode)) {
538 sxp->def_acl = create_racl();
539 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
540 sxp->st.st_mode) < 0) {
549 /* === Send functions === */
551 /* The general strategy with the tag_type <-> character mapping is that
552 * lowercase implies that no qualifier follows, where uppercase does.
553 * A similar idiom for the ACL type (access or default) itself, but
554 * lowercase in this instance means there's no ACL following, so the
555 * ACL is a repeat, so the receiver should reuse the last of the same
558 /* Send the ida list over the file descriptor. */
559 static void send_ida_entries(const ida_entries *idal, int user_names, int f)
562 size_t count = idal->count;
564 write_varint(f, idal->count);
566 for (ida = idal->idas; count--; ida++) {
567 char *name = user_names ? add_uid(ida->id) : add_gid(ida->id);
568 write_varint(f, ida->id);
569 if (inc_recurse && name) {
570 int len = strlen(name);
571 write_byte(f, ida->access | (uchar)0x80);
573 write_buf(f, name, len);
575 write_byte(f, ida->access);
579 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
580 item_list *racl_list, int f)
582 int ndx = find_matching_rsync_acl(racl, type, racl_list);
584 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
585 write_varint(f, ndx + 1);
588 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
591 if (racl->user_obj != NO_ENTRY)
592 flags |= XMIT_USER_OBJ;
593 if (racl->users.count)
594 flags |= XMIT_USER_LIST;
596 if (racl->group_obj != NO_ENTRY)
597 flags |= XMIT_GROUP_OBJ;
598 if (racl->groups.count)
599 flags |= XMIT_GROUP_LIST;
601 if (racl->mask_obj != NO_ENTRY)
602 flags |= XMIT_MASK_OBJ;
604 if (racl->other_obj != NO_ENTRY)
605 flags |= XMIT_OTHER_OBJ;
607 write_byte(f, flags);
609 if (flags & XMIT_USER_OBJ)
610 write_byte(f, racl->user_obj);
611 if (flags & XMIT_USER_LIST)
612 send_ida_entries(&racl->users, 1, f);
614 if (flags & XMIT_GROUP_OBJ)
615 write_byte(f, racl->group_obj);
616 if (flags & XMIT_GROUP_LIST)
617 send_ida_entries(&racl->groups, 0, f);
619 if (flags & XMIT_MASK_OBJ)
620 write_byte(f, racl->mask_obj);
622 if (flags & XMIT_OTHER_OBJ)
623 write_byte(f, racl->other_obj);
625 /* Give the allocated data to the new list object. */
627 *racl = empty_rsync_acl;
631 /* Send the ACL from the statx structure down the indicated file descriptor.
632 * This also frees the ACL data. */
633 void send_acl(statx *sxp, int f)
636 sxp->acc_acl = create_racl();
637 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
639 /* Avoid sending values that can be inferred from other data. */
640 rsync_acl_strip_perms(sxp->acc_acl);
642 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
644 if (S_ISDIR(sxp->st.st_mode)) {
646 sxp->def_acl = create_racl();
648 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
652 /* === Receive functions === */
654 static uchar recv_acl_access(uchar *name_follows_val, int f)
656 uchar access = read_byte(f);
658 if (name_follows_val) {
659 if (access & (uchar)0x80) {
660 access &= ~(uchar)0x80;
661 *name_follows_val = 1;
663 *name_follows_val = 0;
666 if (access & ~(4 | 2 | 1)) {
667 rprintf(FERROR, "recv_acl_access: bogus permset %o\n", access);
668 exit_cleanup(RERR_STREAMIO);
674 static uchar recv_ida_entries(ida_entries *ent, int user_names, int f)
676 uchar computed_mask_bits = 0;
677 int i, count = read_varint(f);
680 if (!(ent->idas = new_array(id_access, count)))
681 out_of_memory("recv_ida_entries");
687 for (i = 0; i < count; i++) {
689 id_t id = read_varint(f);
690 int access = recv_acl_access(&has_name, f);
694 id = recv_user_name(f, id);
696 id = recv_group_name(f, id, NULL);
697 } else if (user_names) {
698 if (inc_recurse && am_root && !numeric_ids)
701 if (inc_recurse && (!am_root || !numeric_ids))
702 id = match_gid(id, NULL);
705 ent->idas[i].id = id;
706 ent->idas[i].access = access;
707 computed_mask_bits |= access;
710 return computed_mask_bits;
713 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
715 uchar computed_mask_bits = 0;
718 int ndx = read_varint(f);
720 if (ndx < 0 || (size_t)ndx > racl_list->count) {
721 rprintf(FERROR, "recv_acl_index: %s ACL index %d > %d\n",
722 str_acl_type(type), ndx, (int)racl_list->count);
723 exit_cleanup(RERR_STREAMIO);
729 ndx = racl_list->count;
730 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
731 duo_item->racl = empty_rsync_acl;
733 flags = read_byte(f);
735 if (flags & XMIT_USER_OBJ)
736 duo_item->racl.user_obj = recv_acl_access(NULL, f);
738 if (flags & XMIT_USER_LIST)
739 computed_mask_bits |= recv_ida_entries(&duo_item->racl.users, 1, f);
741 if (flags & XMIT_GROUP_OBJ)
742 duo_item->racl.group_obj = recv_acl_access(NULL, f);
744 if (flags & XMIT_GROUP_LIST)
745 computed_mask_bits |= recv_ida_entries(&duo_item->racl.groups, 0, f);
747 if (flags & XMIT_MASK_OBJ)
748 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
750 if (flags & XMIT_OTHER_OBJ)
751 duo_item->racl.other_obj = recv_acl_access(NULL, f);
753 if (!duo_item->racl.users.count && !duo_item->racl.groups.count) {
754 /* If we received a superfluous mask, throw it away. */
755 if (duo_item->racl.mask_obj != NO_ENTRY) {
756 /* Mask off the group perms with it first. */
757 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
758 duo_item->racl.mask_obj = NO_ENTRY;
760 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
761 duo_item->racl.mask_obj = computed_mask_bits | (duo_item->racl.group_obj & 7);
763 duo_item->sacl = NULL;
768 /* Receive the ACL info the sender has included for this file-list entry. */
769 void receive_acl(struct file_struct *file, int f)
771 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
773 if (S_ISDIR(file->mode))
774 F_DEF_ACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
777 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
783 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
785 ndx = racl_list->count;
786 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
787 new_duo->racl = *racl;
788 new_duo->sacl = NULL;
789 *racl = empty_rsync_acl;
795 /* Turn the ACL data in statx into cached ACL data, setting the index
796 * values in the file struct. */
797 void cache_acl(struct file_struct *file, statx *sxp)
799 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
800 SMB_ACL_TYPE_ACCESS, &access_acl_list);
802 if (S_ISDIR(sxp->st.st_mode)) {
803 F_DEF_ACL(file) = cache_rsync_acl(sxp->def_acl,
804 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
808 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
810 SMB_ACL_ENTRY_T entry;
815 /* If the sticky bit is going on, it's not safe to allow all
816 * the new ACL to go into effect before it gets set. */
817 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
821 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
824 /* If setuid or setgid is going off, it's not safe to allow all
825 * the new ACL to go into effect before they get cleared. */
826 if ((old_mode & S_ISUID && !(mode & S_ISUID))
827 || (old_mode & S_ISGID && !(mode & S_ISGID)))
832 errfun = "sys_acl_get_entry";
833 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
835 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
836 SMB_ACL_TAG_T tag_type;
837 if ((rc = sys_acl_get_tag_type(entry, &tag_type))) {
838 errfun = "sys_acl_get_tag_type";
842 case SMB_ACL_USER_OBJ:
843 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
845 case SMB_ACL_GROUP_OBJ:
846 /* group is only empty when identical to group perms. */
847 if (racl->group_obj != NO_ENTRY)
849 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
852 #ifndef ACLS_NEED_MASK
853 /* mask is only empty when we don't need it. */
854 if (racl->mask_obj == NO_ENTRY)
857 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
860 COE2( store_access_in_entry,(mode & 7, entry) );
867 rsyserr(FERROR, errno, "change_sacl_perms: %s()",
873 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
874 /* Ensure that chmod() will be called to restore any lost setid bits. */
875 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
876 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
877 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
880 /* Return the mode of the file on disk, as we will set them. */
881 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
884 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
885 SMB_ACL_TYPE_T type, statx *sxp, mode_t mode)
887 if (type == SMB_ACL_TYPE_DEFAULT
888 && duo_item->racl.user_obj == NO_ENTRY) {
889 if (sys_acl_delete_def_file(fname) < 0) {
890 rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
895 mode_t cur_mode = sxp->st.st_mode;
897 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
899 if (type == SMB_ACL_TYPE_ACCESS) {
900 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
902 if (cur_mode == (mode_t)~0)
905 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
906 rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
907 fname, str_acl_type(type));
910 if (type == SMB_ACL_TYPE_ACCESS)
911 sxp->st.st_mode = cur_mode;
917 /* Set ACL on indicated filename.
919 * This sets extended access ACL entries and default ACL. If convenient,
920 * it sets permission bits along with the access ACL and signals having
921 * done so by modifying sxp->st.st_mode.
923 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
924 * with fname set to NULL to just check if the ACL is unchanged. */
925 int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
931 if (!dry_run && (read_only || list_only)) {
937 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
938 acl_duo *duo_item = access_acl_list.items;
941 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
944 if (!dry_run && fname
945 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
946 sxp, file->mode) < 0)
951 if (!S_ISDIR(sxp->st.st_mode))
954 ndx = F_DEF_ACL(file);
955 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
956 acl_duo *duo_item = default_acl_list.items;
958 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
962 if (!dry_run && fname
963 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
964 sxp, file->mode) < 0)
972 /* Non-incremental recursion needs to convert all the received IDs.
973 * This is done in a single pass after receiving the whole file-list. */
974 static void match_racl_ids(const item_list *racl_list)
976 int list_cnt, name_cnt;
977 acl_duo *duo_item = racl_list->items;
978 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
979 ida_entries *idal = &duo_item->racl.users;
980 id_access *ida = idal->idas;
981 for (name_cnt = idal->count; name_cnt--; ida++)
982 ida->id = match_uid(ida->id);
984 idal = &duo_item->racl.groups;
986 for (name_cnt = idal->count; name_cnt--; ida++)
987 ida->id = match_gid(ida->id, NULL);
991 void match_acl_ids(void)
993 match_racl_ids(&access_acl_list);
994 match_racl_ids(&default_acl_list);
997 /* This is used by dest_mode(). */
998 int default_perms_for_dir(const char *dir)
1007 perms = ACCESSPERMS & ~orig_umask;
1008 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1009 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1011 /* Couldn't get an ACL. Darn. */
1017 /* No ACLs are available. */
1021 /* We're doing a dry run, so the containing directory
1022 * wasn't actually created. Don't worry about it. */
1025 /* Otherwise fall through. */
1027 rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1028 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1034 racl = empty_rsync_acl;
1035 ok = unpack_smb_acl(sacl, &racl);
1036 sys_acl_free_acl(sacl);
1038 rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1042 /* Apply the permission-bit entries of the default ACL, if any. */
1043 if (racl.user_obj != NO_ENTRY) {
1044 perms = rsync_acl_get_perms(&racl);
1046 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1049 rsync_acl_free(&racl);
1053 #endif /* SUPPORT_ACLS */