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 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 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_GROUP_OBJ (1<<1)
39 #define XMIT_MASK_OBJ (1<<2)
40 #define XMIT_OTHER_OBJ (1<<3)
41 #define XMIT_NAME_LIST (1<<4)
43 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
45 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
47 /* When we send the access bits over the wire, we shift them 2 bits to the
48 * left and use the lower 2 bits as flags (relevant only to a name entry).
49 * This makes the protocol more efficient than sending a value that would
50 * be likely to have its hightest bits set. */
51 #define XFLAG_NAME_FOLLOWS 0x0001u
52 #define XFLAG_NAME_IS_USER 0x0002u
54 /* === ACL structures === */
71 typedef struct rsync_acl {
73 /* These will be NO_ENTRY if there's no such entry. */
85 static const rsync_acl empty_rsync_acl = {
86 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
89 static item_list access_acl_list = EMPTY_ITEM_LIST;
90 static item_list default_acl_list = EMPTY_ITEM_LIST;
92 /* === Calculations on ACL types === */
94 static const char *str_acl_type(SMB_ACL_TYPE_T type)
96 return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
97 : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
98 : "unknown SMB_ACL_TYPE_T";
101 static int calc_sacl_entries(const rsync_acl *racl)
103 /* A System ACL always gets user/group/other permission entries. */
104 return racl->names.count
105 #ifdef ACLS_NEED_MASK
108 + (racl->mask_obj != NO_ENTRY) + 3;
112 /* Extracts and returns the permission bits from the ACL. This cannot be
113 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
114 static int rsync_acl_get_perms(const rsync_acl *racl)
116 return (racl->user_obj << 6)
117 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
121 /* Removes the permission-bit entries from the ACL because these
122 * can be reconstructed from the file's mode. */
123 static void rsync_acl_strip_perms(rsync_acl *racl)
125 racl->user_obj = NO_ENTRY;
126 if (racl->mask_obj == NO_ENTRY)
127 racl->group_obj = NO_ENTRY;
129 if (racl->group_obj == racl->mask_obj)
130 racl->group_obj = NO_ENTRY;
131 racl->mask_obj = NO_ENTRY;
133 racl->other_obj = NO_ENTRY;
136 /* Given an empty rsync_acl, fake up the permission bits. */
137 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
139 racl->user_obj = (mode >> 6) & 7;
140 racl->group_obj = (mode >> 3) & 7;
141 racl->other_obj = mode & 7;
144 /* === Rsync ACL functions === */
146 static rsync_acl *create_racl(void)
148 rsync_acl *racl = new(rsync_acl);
151 out_of_memory("create_racl");
152 *racl = empty_rsync_acl;
157 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
159 id_access *ida1, *ida2;
160 int count = ial1->count;
161 if (count != ial2->count)
165 for (; count--; ida1++, ida2++) {
166 if (ida1->access != ida2->access || ida1->id != ida2->id)
172 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
174 return racl1->user_obj == racl2->user_obj
175 && racl1->group_obj == racl2->group_obj
176 && racl1->mask_obj == racl2->mask_obj
177 && racl1->other_obj == racl2->other_obj
178 && ida_entries_equal(&racl1->names, &racl2->names);
181 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
182 * the ACL will be handled by the normal mode-preservation code. This is
183 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
184 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
185 * that it might have several of its permission objects set to NO_ENTRY. */
186 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
187 const rsync_acl *racl2, mode_t m)
189 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
190 return False; /* One has a mask and the other doesn't */
192 /* When there's a mask, the group_obj becomes an extended entry. */
193 if (racl1->mask_obj != NO_ENTRY) {
194 /* A condensed rsync_acl with a mask can only have no
195 * group_obj when it was identical to the mask. This
196 * means that it was also identical to the group attrs
198 if (racl2->group_obj == NO_ENTRY) {
199 if (racl1->group_obj != ((m >> 3) & 7))
201 } else if (racl1->group_obj != racl2->group_obj)
204 return ida_entries_equal(&racl1->names, &racl2->names);
207 static void rsync_acl_free(rsync_acl *racl)
209 if (racl->names.idas)
210 free(racl->names.idas);
211 *racl = empty_rsync_acl;
214 void free_acl(statx *sxp)
217 rsync_acl_free(sxp->acc_acl);
222 rsync_acl_free(sxp->def_acl);
228 #ifdef SMB_ACL_NEED_SORT
229 static int id_access_sorter(const void *r1, const void *r2)
231 id_access *ida1 = (id_access *)r1;
232 id_access *ida2 = (id_access *)r2;
233 id_t rid1 = ida1->id, rid2 = ida2->id;
234 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
235 return ida1->access & NAME_IS_USER ? -1 : 1;
236 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
240 /* === System ACLs === */
242 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
243 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
245 static item_list temp_ida_list = EMPTY_ITEM_LIST;
246 SMB_ACL_ENTRY_T entry;
250 errfun = "sys_acl_get_entry";
251 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
253 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
254 SMB_ACL_TAG_T tag_type;
258 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
259 errfun = "sys_acl_get_tag_type";
262 if ((rc = sys_acl_get_access_bits(entry, &access)) != 0) {
263 errfun = "sys_acl_get_access_bits";
266 /* continue == done with entry; break == store in temporary ida list */
268 case SMB_ACL_USER_OBJ:
269 if (racl->user_obj == NO_ENTRY)
270 racl->user_obj = access;
272 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
275 access |= NAME_IS_USER;
277 case SMB_ACL_GROUP_OBJ:
278 if (racl->group_obj == NO_ENTRY)
279 racl->group_obj = access;
281 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
286 if (racl->mask_obj == NO_ENTRY)
287 racl->mask_obj = access;
289 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
292 if (racl->other_obj == NO_ENTRY)
293 racl->other_obj = access;
295 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
298 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
301 if (!(qualifier = sys_acl_get_qualifier(entry))) {
302 errfun = "sys_acl_get_tag_type";
306 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
307 ida->id = *((id_t *)qualifier);
308 ida->access = access;
309 sys_acl_free_qualifier(qualifier, tag_type);
312 rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
313 rsync_acl_free(racl);
317 /* Transfer the count id_access items out of the temp_ida_list
318 * into the names ida_entries list in racl. */
319 if (temp_ida_list.count) {
320 #ifdef SMB_ACL_NEED_SORT
321 if (temp_ida_list.count > 1) {
322 qsort(temp_ida_list.items, temp_ida_list.count,
323 sizeof (id_access), id_access_sorter);
326 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
327 out_of_memory("unpack_smb_acl");
328 memcpy(racl->names.idas, temp_ida_list.items,
329 temp_ida_list.count * sizeof (id_access));
331 racl->names.idas = NULL;
333 racl->names.count = temp_ida_list.count;
335 /* Truncate the temporary list now that its idas have been saved. */
336 temp_ida_list.count = 0;
338 #ifdef ACLS_NEED_MASK
339 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
340 /* Throw away a superfluous mask, but mask off the
341 * group perms with it first. */
342 racl->group_obj &= racl->mask_obj;
343 racl->mask_obj = NO_ENTRY;
350 /* Synactic sugar for system calls */
352 #define CALL_OR_ERROR(func,args,str) \
360 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
361 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
363 /* Store the permissions in the system ACL entry. */
364 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
366 if (sys_acl_set_access_bits(entry, access)) {
367 rsyserr(FERROR, errno, "store_access_in_entry sys_acl_set_access_bits()");
373 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
374 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
376 #ifdef ACLS_NEED_MASK
381 const char *errfun = NULL;
382 SMB_ACL_ENTRY_T entry;
384 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
385 rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
389 COE( sys_acl_create_entry,(smb_acl, &entry) );
390 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
391 COE2( store_access_in_entry,(racl->user_obj & ~NO_ENTRY, entry) );
393 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
394 #ifdef SMB_ACL_NEED_SORT
395 if (!(ida->access & NAME_IS_USER))
398 COE( sys_acl_create_entry,(smb_acl, &entry) );
399 #ifdef SMB_ACL_NEED_SORT
400 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
402 COE( sys_acl_set_tag_type,(entry, ida->access & NAME_IS_USER
403 ? SMB_ACL_USER : SMB_ACL_GROUP) );
405 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
406 COE2( store_access_in_entry,(ida->access & ~NAME_IS_USER, entry) );
409 COE( sys_acl_create_entry,(smb_acl, &entry) );
410 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
411 COE2( store_access_in_entry,(racl->group_obj & ~NO_ENTRY, entry) );
413 #ifdef SMB_ACL_NEED_SORT
414 for ( ; count; ida++, count--) {
415 COE( sys_acl_create_entry,(smb_acl, &entry) );
416 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
417 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
418 COE2( store_access_in_entry,(ida->access, entry) );
422 #ifdef ACLS_NEED_MASK
423 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
424 COE( sys_acl_create_entry,(smb_acl, &entry) );
425 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
426 COE2( store_access_in_entry,(mask_bits, entry) );
428 if (racl->mask_obj != NO_ENTRY) {
429 COE( sys_acl_create_entry,(smb_acl, &entry) );
430 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
431 COE2( store_access_in_entry,(racl->mask_obj, entry) );
435 COE( sys_acl_create_entry,(smb_acl, &entry) );
436 COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
437 COE2( store_access_in_entry,(racl->other_obj & ~NO_ENTRY, entry) );
440 if (sys_acl_valid(*smb_acl) < 0)
441 rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
448 rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
450 sys_acl_free_acl(*smb_acl);
454 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
455 const item_list *racl_list)
457 static int access_match = -1, default_match = -1;
458 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
459 size_t count = racl_list->count;
461 /* If this is the first time through or we didn't match the last
462 * time, then start at the end of the list, which should be the
463 * best place to start hunting. */
465 *match = racl_list->count - 1;
467 rsync_acl *base = racl_list->items;
468 if (rsync_acl_equal(base + *match, racl))
471 *match = racl_list->count - 1;
478 static int get_rsync_acl(const char *fname, rsync_acl *racl,
479 SMB_ACL_TYPE_T type, mode_t mode)
481 SMB_ACL_T sacl = sys_acl_get_file(fname, type);
484 BOOL ok = unpack_smb_acl(sacl, racl);
486 sys_acl_free_acl(sacl);
490 } else if (no_acl_syscall_error(errno)) {
491 /* ACLs are not supported, so pretend we have a basic ACL. */
492 if (type == SMB_ACL_TYPE_ACCESS)
493 rsync_acl_fake_perms(racl, mode);
495 rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
496 fname, str_acl_type(type));
503 /* Return the Access Control List for the given filename. */
504 int get_acl(const char *fname, statx *sxp)
506 sxp->acc_acl = create_racl();
507 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
508 sxp->st.st_mode) < 0) {
513 if (S_ISDIR(sxp->st.st_mode)) {
514 sxp->def_acl = create_racl();
515 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
516 sxp->st.st_mode) < 0) {
525 /* === Send functions === */
527 /* The general strategy with the tag_type <-> character mapping is that
528 * lowercase implies that no qualifier follows, where uppercase does.
529 * A similar idiom for the ACL type (access or default) itself, but
530 * lowercase in this instance means there's no ACL following, so the
531 * ACL is a repeat, so the receiver should reuse the last of the same
534 /* Send the ida list over the file descriptor. */
535 static void send_ida_entries(const ida_entries *idal, int f)
538 size_t count = idal->count;
540 write_varint(f, idal->count);
542 for (ida = idal->idas; count--; ida++) {
543 uint32 xbits = ida->access << 2;
545 if (ida->access & NAME_IS_USER) {
546 xbits |= XFLAG_NAME_IS_USER;
547 name = add_uid(ida->id);
549 name = add_gid(ida->id);
550 write_varint(f, ida->id);
551 if (inc_recurse && name) {
552 int len = strlen(name);
553 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
555 write_buf(f, name, len);
557 write_varint(f, xbits);
561 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
562 item_list *racl_list, int f)
564 int ndx = find_matching_rsync_acl(racl, type, racl_list);
566 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
567 write_varint(f, ndx + 1);
570 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
573 if (racl->user_obj != NO_ENTRY)
574 flags |= XMIT_USER_OBJ;
575 if (racl->group_obj != NO_ENTRY)
576 flags |= XMIT_GROUP_OBJ;
577 if (racl->mask_obj != NO_ENTRY)
578 flags |= XMIT_MASK_OBJ;
579 if (racl->other_obj != NO_ENTRY)
580 flags |= XMIT_OTHER_OBJ;
581 if (racl->names.count)
582 flags |= XMIT_NAME_LIST;
584 write_byte(f, flags);
586 if (flags & XMIT_USER_OBJ)
587 write_varint(f, racl->user_obj);
588 if (flags & XMIT_GROUP_OBJ)
589 write_varint(f, racl->group_obj);
590 if (flags & XMIT_MASK_OBJ)
591 write_varint(f, racl->mask_obj);
592 if (flags & XMIT_OTHER_OBJ)
593 write_varint(f, racl->other_obj);
594 if (flags & XMIT_NAME_LIST)
595 send_ida_entries(&racl->names, f);
597 /* Give the allocated data to the new list object. */
599 *racl = empty_rsync_acl;
603 /* Send the ACL from the statx structure down the indicated file descriptor.
604 * This also frees the ACL data. */
605 void send_acl(statx *sxp, int f)
608 sxp->acc_acl = create_racl();
609 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
611 /* Avoid sending values that can be inferred from other data. */
612 rsync_acl_strip_perms(sxp->acc_acl);
614 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
616 if (S_ISDIR(sxp->st.st_mode)) {
618 sxp->def_acl = create_racl();
620 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
624 /* === Receive functions === */
626 static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
628 uint32 access = read_varint(f);
630 if (name_follows_ptr) {
631 int flags = access & 3;
633 if (access & ~SMB_ACL_VALID_NAME_BITS)
635 if (flags & XFLAG_NAME_FOLLOWS)
636 *name_follows_ptr = 1;
638 *name_follows_ptr = 0;
639 if (flags & XFLAG_NAME_IS_USER)
640 access |= NAME_IS_USER;
641 } else if (access & ~SMB_ACL_VALID_OBJ_BITS) {
643 rprintf(FERROR, "recv_acl_access: value out of range: %x\n",
645 exit_cleanup(RERR_STREAMIO);
651 static uchar recv_ida_entries(ida_entries *ent, int f)
653 uchar computed_mask_bits = 0;
654 int i, count = read_varint(f);
657 if (!(ent->idas = new_array(id_access, count)))
658 out_of_memory("recv_ida_entries");
664 for (i = 0; i < count; i++) {
666 id_t id = read_varint(f);
667 uint32 access = recv_acl_access(&has_name, f);
670 if (access & NAME_IS_USER)
671 id = recv_user_name(f, id);
673 id = recv_group_name(f, id, NULL);
674 } else if (access & NAME_IS_USER) {
675 if (inc_recurse && am_root && !numeric_ids)
678 if (inc_recurse && (!am_root || !numeric_ids))
679 id = match_gid(id, NULL);
682 ent->idas[i].id = id;
683 ent->idas[i].access = access;
684 computed_mask_bits |= access;
687 return computed_mask_bits & ~NO_ENTRY;
690 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
692 uchar computed_mask_bits = 0;
695 int ndx = read_varint(f);
697 if (ndx < 0 || (size_t)ndx > racl_list->count) {
698 rprintf(FERROR, "recv_acl_index: %s ACL index %d > %d\n",
699 str_acl_type(type), ndx, (int)racl_list->count);
700 exit_cleanup(RERR_STREAMIO);
706 ndx = racl_list->count;
707 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
708 duo_item->racl = empty_rsync_acl;
710 flags = read_byte(f);
712 if (flags & XMIT_USER_OBJ)
713 duo_item->racl.user_obj = recv_acl_access(NULL, f);
714 if (flags & XMIT_GROUP_OBJ)
715 duo_item->racl.group_obj = recv_acl_access(NULL, f);
716 if (flags & XMIT_MASK_OBJ)
717 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
718 if (flags & XMIT_OTHER_OBJ)
719 duo_item->racl.other_obj = recv_acl_access(NULL, f);
720 if (flags & XMIT_NAME_LIST)
721 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
723 if (!duo_item->racl.names.count) {
724 /* If we received a superfluous mask, throw it away. */
725 if (duo_item->racl.mask_obj != NO_ENTRY) {
726 /* Mask off the group perms with it first. */
727 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
728 duo_item->racl.mask_obj = NO_ENTRY;
730 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
731 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
733 duo_item->sacl = NULL;
738 /* Receive the ACL info the sender has included for this file-list entry. */
739 void receive_acl(struct file_struct *file, int f)
741 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
743 if (S_ISDIR(file->mode))
744 F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
747 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
753 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
755 ndx = racl_list->count;
756 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
757 new_duo->racl = *racl;
758 new_duo->sacl = NULL;
759 *racl = empty_rsync_acl;
765 /* Turn the ACL data in statx into cached ACL data, setting the index
766 * values in the file struct. */
767 void cache_acl(struct file_struct *file, statx *sxp)
769 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
770 SMB_ACL_TYPE_ACCESS, &access_acl_list);
772 if (S_ISDIR(sxp->st.st_mode)) {
773 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
774 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
778 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
780 SMB_ACL_ENTRY_T entry;
785 /* If the sticky bit is going on, it's not safe to allow all
786 * the new ACL to go into effect before it gets set. */
787 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
791 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
794 /* If setuid or setgid is going off, it's not safe to allow all
795 * the new ACL to go into effect before they get cleared. */
796 if ((old_mode & S_ISUID && !(mode & S_ISUID))
797 || (old_mode & S_ISGID && !(mode & S_ISGID)))
802 errfun = "sys_acl_get_entry";
803 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
805 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
806 SMB_ACL_TAG_T tag_type;
807 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
808 errfun = "sys_acl_get_tag_type";
812 case SMB_ACL_USER_OBJ:
813 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
815 case SMB_ACL_GROUP_OBJ:
816 /* group is only empty when identical to group perms. */
817 if (racl->group_obj != NO_ENTRY)
819 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
822 #ifndef ACLS_NEED_MASK
823 /* mask is only empty when we don't need it. */
824 if (racl->mask_obj == NO_ENTRY)
827 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
830 COE2( store_access_in_entry,(mode & 7, entry) );
837 rsyserr(FERROR, errno, "change_sacl_perms: %s()",
843 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
844 /* Ensure that chmod() will be called to restore any lost setid bits. */
845 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
846 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
847 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
850 /* Return the mode of the file on disk, as we will set them. */
851 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
854 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
855 SMB_ACL_TYPE_T type, statx *sxp, mode_t mode)
857 if (type == SMB_ACL_TYPE_DEFAULT
858 && duo_item->racl.user_obj == NO_ENTRY) {
859 if (sys_acl_delete_def_file(fname) < 0) {
860 rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
865 mode_t cur_mode = sxp->st.st_mode;
867 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
869 if (type == SMB_ACL_TYPE_ACCESS) {
870 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
872 if (cur_mode == (mode_t)~0)
875 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
876 rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
877 fname, str_acl_type(type));
880 if (type == SMB_ACL_TYPE_ACCESS)
881 sxp->st.st_mode = cur_mode;
887 /* Set ACL on indicated filename.
889 * This sets extended access ACL entries and default ACL. If convenient,
890 * it sets permission bits along with the access ACL and signals having
891 * done so by modifying sxp->st.st_mode.
893 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
894 * with fname set to NULL to just check if the ACL is unchanged. */
895 int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
901 if (!dry_run && (read_only || list_only)) {
907 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
908 acl_duo *duo_item = access_acl_list.items;
911 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
914 if (!dry_run && fname
915 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
916 sxp, file->mode) < 0)
921 if (!S_ISDIR(sxp->st.st_mode))
924 ndx = F_DIR_DEFACL(file);
925 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
926 acl_duo *duo_item = default_acl_list.items;
928 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
932 if (!dry_run && fname
933 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
934 sxp, file->mode) < 0)
942 /* Non-incremental recursion needs to convert all the received IDs.
943 * This is done in a single pass after receiving the whole file-list. */
944 static void match_racl_ids(const item_list *racl_list)
946 int list_cnt, name_cnt;
947 acl_duo *duo_item = racl_list->items;
948 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
949 ida_entries *idal = &duo_item->racl.names;
950 id_access *ida = idal->idas;
951 for (name_cnt = idal->count; name_cnt--; ida++) {
952 if (ida->access & NAME_IS_USER)
953 ida->id = match_uid(ida->id);
955 ida->id = match_gid(ida->id, NULL);
960 void match_acl_ids(void)
962 match_racl_ids(&access_acl_list);
963 match_racl_ids(&default_acl_list);
966 /* This is used by dest_mode(). */
967 int default_perms_for_dir(const char *dir)
976 perms = ACCESSPERMS & ~orig_umask;
977 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
978 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
980 /* Couldn't get an ACL. Darn. */
986 /* No ACLs are available. */
990 /* We're doing a dry run, so the containing directory
991 * wasn't actually created. Don't worry about it. */
994 /* Otherwise fall through. */
996 rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
997 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1003 racl = empty_rsync_acl;
1004 ok = unpack_smb_acl(sacl, &racl);
1005 sys_acl_free_acl(sacl);
1007 rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1011 /* Apply the permission-bit entries of the default ACL, if any. */
1012 if (racl.user_obj != NO_ENTRY) {
1013 perms = rsync_acl_get_perms(&racl);
1015 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1018 rsync_acl_free(&racl);
1022 #endif /* SUPPORT_ACLS */