2 * Handle passing Access Control Lists between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2006-2008 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
23 #include "lib/sysacls.h"
31 extern int orig_umask;
32 extern int numeric_ids;
33 extern int inc_recurse;
35 /* Flags used to indicate what items are being transmitted for an entry. */
36 #define XMIT_USER_OBJ (1<<0)
37 #define XMIT_GROUP_OBJ (1<<1)
38 #define XMIT_MASK_OBJ (1<<2)
39 #define XMIT_OTHER_OBJ (1<<3)
40 #define XMIT_NAME_LIST (1<<4)
42 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
44 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
46 /* When we send the access bits over the wire, we shift them 2 bits to the
47 * left and use the lower 2 bits as flags (relevant only to a name entry).
48 * This makes the protocol more efficient than sending a value that would
49 * be likely to have its hightest bits set. */
50 #define XFLAG_NAME_FOLLOWS 0x0001u
51 #define XFLAG_NAME_IS_USER 0x0002u
53 /* === ACL structures === */
70 typedef struct rsync_acl {
72 /* These will be NO_ENTRY if there's no such entry. */
84 static const rsync_acl empty_rsync_acl = {
85 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
88 static item_list access_acl_list = EMPTY_ITEM_LIST;
89 static item_list default_acl_list = EMPTY_ITEM_LIST;
91 /* === Calculations on ACL types === */
93 static const char *str_acl_type(SMB_ACL_TYPE_T type)
96 case SMB_ACL_TYPE_ACCESS:
98 return "ACL_TYPE_EXTENDED";
100 return "ACL_TYPE_ACCESS";
102 case SMB_ACL_TYPE_DEFAULT:
103 return "ACL_TYPE_DEFAULT";
107 return "unknown ACL type!";
110 static int calc_sacl_entries(const rsync_acl *racl)
112 /* A System ACL always gets user/group/other permission entries. */
113 return racl->names.count
114 #ifdef ACLS_NEED_MASK
117 + (racl->mask_obj != NO_ENTRY) + 3;
121 /* Extracts and returns the permission bits from the ACL. This cannot be
122 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
123 static int rsync_acl_get_perms(const rsync_acl *racl)
125 return (racl->user_obj << 6)
126 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
130 /* Removes the permission-bit entries from the ACL because these
131 * can be reconstructed from the file's mode. */
132 static void rsync_acl_strip_perms(rsync_acl *racl)
134 racl->user_obj = NO_ENTRY;
135 if (racl->mask_obj == NO_ENTRY)
136 racl->group_obj = NO_ENTRY;
138 if (racl->group_obj == racl->mask_obj)
139 racl->group_obj = NO_ENTRY;
140 racl->mask_obj = NO_ENTRY;
142 racl->other_obj = NO_ENTRY;
145 /* Given an empty rsync_acl, fake up the permission bits. */
146 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
148 racl->user_obj = (mode >> 6) & 7;
149 racl->group_obj = (mode >> 3) & 7;
150 racl->other_obj = mode & 7;
153 /* === Rsync ACL functions === */
155 static rsync_acl *create_racl(void)
157 rsync_acl *racl = new(rsync_acl);
160 out_of_memory("create_racl");
161 *racl = empty_rsync_acl;
166 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
168 id_access *ida1, *ida2;
169 int count = ial1->count;
170 if (count != ial2->count)
174 for (; count--; ida1++, ida2++) {
175 if (ida1->access != ida2->access || ida1->id != ida2->id)
181 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
183 return racl1->user_obj == racl2->user_obj
184 && racl1->group_obj == racl2->group_obj
185 && racl1->mask_obj == racl2->mask_obj
186 && racl1->other_obj == racl2->other_obj
187 && ida_entries_equal(&racl1->names, &racl2->names);
190 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
191 * the ACL will be handled by the normal mode-preservation code. This is
192 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
193 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
194 * that it might have several of its permission objects set to NO_ENTRY. */
195 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
196 const rsync_acl *racl2, mode_t m)
198 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
199 return False; /* One has a mask and the other doesn't */
201 /* When there's a mask, the group_obj becomes an extended entry. */
202 if (racl1->mask_obj != NO_ENTRY) {
203 /* A condensed rsync_acl with a mask can only have no
204 * group_obj when it was identical to the mask. This
205 * means that it was also identical to the group attrs
207 if (racl2->group_obj == NO_ENTRY) {
208 if (racl1->group_obj != ((m >> 3) & 7))
210 } else if (racl1->group_obj != racl2->group_obj)
213 return ida_entries_equal(&racl1->names, &racl2->names);
216 static void rsync_acl_free(rsync_acl *racl)
218 if (racl->names.idas)
219 free(racl->names.idas);
220 *racl = empty_rsync_acl;
223 void free_acl(stat_x *sxp)
226 rsync_acl_free(sxp->acc_acl);
231 rsync_acl_free(sxp->def_acl);
237 #ifdef SMB_ACL_NEED_SORT
238 static int id_access_sorter(const void *r1, const void *r2)
240 id_access *ida1 = (id_access *)r1;
241 id_access *ida2 = (id_access *)r2;
242 id_t rid1 = ida1->id, rid2 = ida2->id;
243 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
244 return ida1->access & NAME_IS_USER ? -1 : 1;
245 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
249 /* === System ACLs === */
251 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
252 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
254 static item_list temp_ida_list = EMPTY_ITEM_LIST;
255 SMB_ACL_ENTRY_T entry;
259 errfun = "sys_acl_get_entry";
260 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
262 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
263 SMB_ACL_TAG_T tag_type;
267 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
268 errfun = "sys_acl_get_info";
271 /* continue == done with entry; break == store in temporary ida list */
273 #ifndef HAVE_OSX_ACLS
274 case SMB_ACL_USER_OBJ:
275 if (racl->user_obj == NO_ENTRY)
276 racl->user_obj = access;
278 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
280 case SMB_ACL_GROUP_OBJ:
281 if (racl->group_obj == NO_ENTRY)
282 racl->group_obj = access;
284 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
287 if (racl->mask_obj == NO_ENTRY)
288 racl->mask_obj = access;
290 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
293 if (racl->other_obj == NO_ENTRY)
294 racl->other_obj = access;
296 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
300 access |= NAME_IS_USER;
305 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
308 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
310 ida->access = access;
313 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
314 rsync_acl_free(racl);
318 /* Transfer the count id_access items out of the temp_ida_list
319 * into the names ida_entries list in racl. */
320 if (temp_ida_list.count) {
321 #ifdef SMB_ACL_NEED_SORT
322 if (temp_ida_list.count > 1) {
323 qsort(temp_ida_list.items, temp_ida_list.count,
324 sizeof (id_access), id_access_sorter);
327 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
328 out_of_memory("unpack_smb_acl");
329 memcpy(racl->names.idas, temp_ida_list.items,
330 temp_ida_list.count * sizeof (id_access));
332 racl->names.idas = NULL;
334 racl->names.count = temp_ida_list.count;
336 /* Truncate the temporary list now that its idas have been saved. */
337 temp_ida_list.count = 0;
339 #ifdef ACLS_NEED_MASK
340 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
341 /* Throw away a superfluous mask, but mask off the
342 * group perms with it first. */
343 racl->group_obj &= racl->mask_obj;
344 racl->mask_obj = NO_ENTRY;
351 /* Synactic sugar for system calls */
353 #define CALL_OR_ERROR(func,args,str) \
361 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
362 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
364 #ifndef HAVE_OSX_ACLS
365 /* Store the permissions in the system ACL entry. */
366 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
368 if (sys_acl_set_access_bits(entry, access)) {
369 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
376 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
377 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
379 #ifdef ACLS_NEED_MASK
384 const char *errfun = NULL;
385 SMB_ACL_ENTRY_T entry;
387 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
388 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
392 #ifndef HAVE_OSX_ACLS
393 COE( sys_acl_create_entry,(smb_acl, &entry) );
394 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
397 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
398 #ifdef SMB_ACL_NEED_SORT
399 if (!(ida->access & NAME_IS_USER))
402 COE( sys_acl_create_entry,(smb_acl, &entry) );
403 COE( sys_acl_set_info,
405 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
406 ida->access & ~NAME_IS_USER, ida->id) );
409 #ifndef HAVE_OSX_ACLS
410 COE( sys_acl_create_entry,(smb_acl, &entry) );
411 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
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_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
420 #ifdef ACLS_NEED_MASK
421 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
422 COE( sys_acl_create_entry,(smb_acl, &entry) );
423 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
425 if (racl->mask_obj != NO_ENTRY) {
426 COE( sys_acl_create_entry,(smb_acl, &entry) );
427 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
431 COE( sys_acl_create_entry,(smb_acl, &entry) );
432 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
436 if (sys_acl_valid(*smb_acl) < 0)
437 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
444 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
446 sys_acl_free_acl(*smb_acl);
450 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
451 const item_list *racl_list)
453 static int access_match = -1, default_match = -1;
454 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
455 size_t count = racl_list->count;
457 /* If this is the first time through or we didn't match the last
458 * time, then start at the end of the list, which should be the
459 * best place to start hunting. */
461 *match = racl_list->count - 1;
463 rsync_acl *base = racl_list->items;
464 if (rsync_acl_equal(base + *match, racl))
467 *match = racl_list->count - 1;
474 static int get_rsync_acl(const char *fname, rsync_acl *racl,
475 SMB_ACL_TYPE_T type, mode_t mode)
479 #ifdef SUPPORT_XATTRS
480 /* --fake-super support: load ACLs from an xattr. */
486 if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
488 cnt = (len - 4*4) / (4+4);
489 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
494 racl->user_obj = IVAL(buf, 0);
495 racl->group_obj = IVAL(buf, 4);
496 racl->mask_obj = IVAL(buf, 8);
497 racl->other_obj = IVAL(buf, 12);
500 char *bp = buf + 4*4;
502 if (!(ida = racl->names.idas = new_array(id_access, cnt)))
503 out_of_memory("get_rsync_acl");
504 racl->names.count = cnt;
505 for ( ; cnt--; ida++, bp += 4+4) {
506 ida->id = IVAL(bp, 0);
507 ida->access = IVAL(bp, 4);
515 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
516 BOOL ok = unpack_smb_acl(sacl, racl);
518 sys_acl_free_acl(sacl);
522 } else if (no_acl_syscall_error(errno)) {
523 /* ACLs are not supported, so pretend we have a basic ACL. */
524 if (type == SMB_ACL_TYPE_ACCESS)
525 rsync_acl_fake_perms(racl, mode);
527 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
528 fname, str_acl_type(type));
535 /* Return the Access Control List for the given filename. */
536 int get_acl(const char *fname, stat_x *sxp)
538 sxp->acc_acl = create_racl();
539 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
540 sxp->st.st_mode) < 0) {
545 if (S_ISDIR(sxp->st.st_mode)) {
546 sxp->def_acl = create_racl();
547 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
548 sxp->st.st_mode) < 0) {
557 /* === Send functions === */
559 /* Send the ida list over the file descriptor. */
560 static void send_ida_entries(const ida_entries *idal, int f)
563 size_t count = idal->count;
565 write_varint(f, idal->count);
567 for (ida = idal->idas; count--; ida++) {
568 uint32 xbits = ida->access << 2;
570 if (ida->access & NAME_IS_USER) {
571 xbits |= XFLAG_NAME_IS_USER;
572 name = add_uid(ida->id);
574 name = add_gid(ida->id);
575 write_varint(f, ida->id);
576 if (inc_recurse && name) {
577 int len = strlen(name);
578 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
580 write_buf(f, name, len);
582 write_varint(f, xbits);
586 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
587 item_list *racl_list, int f)
589 int ndx = find_matching_rsync_acl(racl, type, racl_list);
591 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
592 write_varint(f, ndx + 1);
595 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
598 if (racl->user_obj != NO_ENTRY)
599 flags |= XMIT_USER_OBJ;
600 if (racl->group_obj != NO_ENTRY)
601 flags |= XMIT_GROUP_OBJ;
602 if (racl->mask_obj != NO_ENTRY)
603 flags |= XMIT_MASK_OBJ;
604 if (racl->other_obj != NO_ENTRY)
605 flags |= XMIT_OTHER_OBJ;
606 if (racl->names.count)
607 flags |= XMIT_NAME_LIST;
609 write_byte(f, flags);
611 if (flags & XMIT_USER_OBJ)
612 write_varint(f, racl->user_obj);
613 if (flags & XMIT_GROUP_OBJ)
614 write_varint(f, racl->group_obj);
615 if (flags & XMIT_MASK_OBJ)
616 write_varint(f, racl->mask_obj);
617 if (flags & XMIT_OTHER_OBJ)
618 write_varint(f, racl->other_obj);
619 if (flags & XMIT_NAME_LIST)
620 send_ida_entries(&racl->names, f);
622 /* Give the allocated data to the new list object. */
624 *racl = empty_rsync_acl;
628 /* Send the ACL from the stat_x structure down the indicated file descriptor.
629 * This also frees the ACL data. */
630 void send_acl(stat_x *sxp, int f)
633 sxp->acc_acl = create_racl();
634 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
636 /* Avoid sending values that can be inferred from other data. */
637 rsync_acl_strip_perms(sxp->acc_acl);
639 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
641 if (S_ISDIR(sxp->st.st_mode)) {
643 sxp->def_acl = create_racl();
645 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
649 /* === Receive functions === */
651 static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
653 uint32 access = read_varint(f);
655 if (name_follows_ptr) {
656 int flags = access & 3;
658 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
660 if (flags & XFLAG_NAME_FOLLOWS)
661 *name_follows_ptr = 1;
663 *name_follows_ptr = 0;
664 if (flags & XFLAG_NAME_IS_USER)
665 access |= NAME_IS_USER;
666 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
668 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
670 exit_cleanup(RERR_STREAMIO);
676 static uchar recv_ida_entries(ida_entries *ent, int f)
678 uchar computed_mask_bits = 0;
679 int i, count = read_varint(f);
682 if (!(ent->idas = new_array(id_access, count)))
683 out_of_memory("recv_ida_entries");
689 for (i = 0; i < count; i++) {
691 id_t id = read_varint(f);
692 uint32 access = recv_acl_access(&has_name, f);
695 if (access & NAME_IS_USER)
696 id = recv_user_name(f, id);
698 id = recv_group_name(f, id, NULL);
699 } else if (access & NAME_IS_USER) {
700 if (inc_recurse && am_root && !numeric_ids)
703 if (inc_recurse && (!am_root || !numeric_ids))
704 id = match_gid(id, NULL);
707 ent->idas[i].id = id;
708 ent->idas[i].access = access;
709 computed_mask_bits |= access;
712 return computed_mask_bits & ~NO_ENTRY;
715 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
717 uchar computed_mask_bits = 0;
720 int ndx = read_varint(f);
722 if (ndx < 0 || (size_t)ndx > racl_list->count) {
723 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
724 str_acl_type(type), ndx, (int)racl_list->count);
725 exit_cleanup(RERR_STREAMIO);
731 ndx = racl_list->count;
732 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
733 duo_item->racl = empty_rsync_acl;
735 flags = read_byte(f);
737 if (flags & XMIT_USER_OBJ)
738 duo_item->racl.user_obj = recv_acl_access(NULL, f);
739 if (flags & XMIT_GROUP_OBJ)
740 duo_item->racl.group_obj = recv_acl_access(NULL, f);
741 if (flags & XMIT_MASK_OBJ)
742 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
743 if (flags & XMIT_OTHER_OBJ)
744 duo_item->racl.other_obj = recv_acl_access(NULL, f);
745 if (flags & XMIT_NAME_LIST)
746 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
749 /* If we received a superfluous mask, throw it away. */
750 duo_item->racl.mask_obj = NO_ENTRY;
752 if (!duo_item->racl.names.count) {
753 /* If we received a superfluous mask, throw it away. */
754 if (duo_item->racl.mask_obj != NO_ENTRY) {
755 /* Mask off the group perms with it first. */
756 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
757 duo_item->racl.mask_obj = NO_ENTRY;
759 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
760 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
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_DIR_DEFACL(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 stat_x into cached ACL data, setting the index
796 * values in the file struct. */
797 void cache_acl(struct file_struct *file, stat_x *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_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
804 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
808 #ifndef HAVE_OSX_ACLS
809 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
811 SMB_ACL_ENTRY_T entry;
816 /* If the sticky bit is going on, it's not safe to allow all
817 * the new ACL to go into effect before it gets set. */
818 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
822 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
825 /* If setuid or setgid is going off, it's not safe to allow all
826 * the new ACL to go into effect before they get cleared. */
827 if ((old_mode & S_ISUID && !(mode & S_ISUID))
828 || (old_mode & S_ISGID && !(mode & S_ISGID)))
833 errfun = "sys_acl_get_entry";
834 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
836 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
837 SMB_ACL_TAG_T tag_type;
838 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
839 errfun = "sys_acl_get_tag_type";
843 case SMB_ACL_USER_OBJ:
844 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
846 case SMB_ACL_GROUP_OBJ:
847 /* group is only empty when identical to group perms. */
848 if (racl->group_obj != NO_ENTRY)
850 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
853 #ifndef ACLS_NEED_MASK
854 /* mask is only empty when we don't need it. */
855 if (racl->mask_obj == NO_ENTRY)
858 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
861 COE2( store_access_in_entry,(mode & 7, entry) );
868 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
874 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
875 /* Ensure that chmod() will be called to restore any lost setid bits. */
876 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
877 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
878 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
881 /* Return the mode of the file on disk, as we will set them. */
882 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
886 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
887 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
889 if (type == SMB_ACL_TYPE_DEFAULT
890 && duo_item->racl.user_obj == NO_ENTRY) {
892 #ifdef SUPPORT_XATTRS
893 /* --fake-super support: delete default ACL from xattrs. */
895 rc = del_def_xattr_acl(fname);
898 rc = sys_acl_delete_def_file(fname);
900 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
904 #ifdef SUPPORT_XATTRS
905 } else if (am_root < 0) {
906 /* --fake-super support: store ACLs in an xattr. */
907 int cnt = duo_item->racl.names.count;
908 size_t len = 4*4 + cnt * (4+4);
909 char *buf = new_array(char, len);
912 SIVAL(buf, 0, duo_item->racl.user_obj);
913 SIVAL(buf, 4, duo_item->racl.group_obj);
914 SIVAL(buf, 8, duo_item->racl.mask_obj);
915 SIVAL(buf, 12, duo_item->racl.other_obj);
918 char *bp = buf + 4*4;
919 id_access *ida = duo_item->racl.names.idas;
920 for ( ; cnt--; ida++, bp += 4+4) {
921 SIVAL(bp, 0, ida->id);
922 SIVAL(bp, 4, ida->access);
925 rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
930 mode_t cur_mode = sxp->st.st_mode;
932 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
935 mode = 0; /* eliminate compiler warning */
937 if (type == SMB_ACL_TYPE_ACCESS) {
938 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
940 if (cur_mode == (mode_t)~0)
944 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
945 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
946 fname, str_acl_type(type));
949 if (type == SMB_ACL_TYPE_ACCESS)
950 sxp->st.st_mode = cur_mode;
956 /* Set ACL on indicated filename.
958 * This sets extended access ACL entries and default ACL. If convenient,
959 * it sets permission bits along with the access ACL and signals having
960 * done so by modifying sxp->st.st_mode.
962 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
963 * with fname set to NULL to just check if the ACL is unchanged. */
964 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp)
970 if (!dry_run && (read_only || list_only)) {
976 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
977 acl_duo *duo_item = access_acl_list.items;
980 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
983 if (!dry_run && fname
984 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
985 sxp, file->mode) < 0)
990 if (!S_ISDIR(sxp->st.st_mode))
993 ndx = F_DIR_DEFACL(file);
994 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
995 acl_duo *duo_item = default_acl_list.items;
997 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
1001 if (!dry_run && fname
1002 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1003 sxp, file->mode) < 0)
1011 /* Non-incremental recursion needs to convert all the received IDs.
1012 * This is done in a single pass after receiving the whole file-list. */
1013 static void match_racl_ids(const item_list *racl_list)
1015 int list_cnt, name_cnt;
1016 acl_duo *duo_item = racl_list->items;
1017 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1018 ida_entries *idal = &duo_item->racl.names;
1019 id_access *ida = idal->idas;
1020 for (name_cnt = idal->count; name_cnt--; ida++) {
1021 if (ida->access & NAME_IS_USER)
1022 ida->id = match_uid(ida->id);
1024 ida->id = match_gid(ida->id, NULL);
1029 void match_acl_ids(void)
1031 match_racl_ids(&access_acl_list);
1032 match_racl_ids(&default_acl_list);
1035 /* This is used by dest_mode(). */
1036 int default_perms_for_dir(const char *dir)
1045 perms = ACCESSPERMS & ~orig_umask;
1046 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1047 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1049 /* Couldn't get an ACL. Darn. */
1055 /* No ACLs are available. */
1059 /* We're doing a dry run, so the containing directory
1060 * wasn't actually created. Don't worry about it. */
1063 /* Otherwise fall through. */
1066 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1067 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1073 racl = empty_rsync_acl;
1074 ok = unpack_smb_acl(sacl, &racl);
1075 sys_acl_free_acl(sacl);
1077 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1081 /* Apply the permission-bit entries of the default ACL, if any. */
1082 if (racl.user_obj != NO_ENTRY) {
1083 perms = rsync_acl_get_perms(&racl);
1085 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1088 rsync_acl_free(&racl);
1092 #endif /* SUPPORT_ACLS */