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 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)
95 return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
96 : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
97 : "unknown SMB_ACL_TYPE_T";
100 static int calc_sacl_entries(const rsync_acl *racl)
102 /* A System ACL always gets user/group/other permission entries. */
103 return racl->names.count
104 #ifdef ACLS_NEED_MASK
107 + (racl->mask_obj != NO_ENTRY) + 3;
111 /* Extracts and returns the permission bits from the ACL. This cannot be
112 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
113 static int rsync_acl_get_perms(const rsync_acl *racl)
115 return (racl->user_obj << 6)
116 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
120 /* Removes the permission-bit entries from the ACL because these
121 * can be reconstructed from the file's mode. */
122 static void rsync_acl_strip_perms(rsync_acl *racl)
124 racl->user_obj = NO_ENTRY;
125 if (racl->mask_obj == NO_ENTRY)
126 racl->group_obj = NO_ENTRY;
128 if (racl->group_obj == racl->mask_obj)
129 racl->group_obj = NO_ENTRY;
130 racl->mask_obj = NO_ENTRY;
132 racl->other_obj = NO_ENTRY;
135 /* Given an empty rsync_acl, fake up the permission bits. */
136 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
138 racl->user_obj = (mode >> 6) & 7;
139 racl->group_obj = (mode >> 3) & 7;
140 racl->other_obj = mode & 7;
143 /* === Rsync ACL functions === */
145 static rsync_acl *create_racl(void)
147 rsync_acl *racl = new(rsync_acl);
150 out_of_memory("create_racl");
151 *racl = empty_rsync_acl;
156 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
158 id_access *ida1, *ida2;
159 int count = ial1->count;
160 if (count != ial2->count)
164 for (; count--; ida1++, ida2++) {
165 if (ida1->access != ida2->access || ida1->id != ida2->id)
171 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
173 return racl1->user_obj == racl2->user_obj
174 && racl1->group_obj == racl2->group_obj
175 && racl1->mask_obj == racl2->mask_obj
176 && racl1->other_obj == racl2->other_obj
177 && ida_entries_equal(&racl1->names, &racl2->names);
180 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
181 * the ACL will be handled by the normal mode-preservation code. This is
182 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
183 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
184 * that it might have several of its permission objects set to NO_ENTRY. */
185 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
186 const rsync_acl *racl2, mode_t m)
188 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
189 return False; /* One has a mask and the other doesn't */
191 /* When there's a mask, the group_obj becomes an extended entry. */
192 if (racl1->mask_obj != NO_ENTRY) {
193 /* A condensed rsync_acl with a mask can only have no
194 * group_obj when it was identical to the mask. This
195 * means that it was also identical to the group attrs
197 if (racl2->group_obj == NO_ENTRY) {
198 if (racl1->group_obj != ((m >> 3) & 7))
200 } else if (racl1->group_obj != racl2->group_obj)
203 return ida_entries_equal(&racl1->names, &racl2->names);
206 static void rsync_acl_free(rsync_acl *racl)
208 if (racl->names.idas)
209 free(racl->names.idas);
210 *racl = empty_rsync_acl;
213 void free_acl(stat_x *sxp)
216 rsync_acl_free(sxp->acc_acl);
221 rsync_acl_free(sxp->def_acl);
227 #ifdef SMB_ACL_NEED_SORT
228 static int id_access_sorter(const void *r1, const void *r2)
230 id_access *ida1 = (id_access *)r1;
231 id_access *ida2 = (id_access *)r2;
232 id_t rid1 = ida1->id, rid2 = ida2->id;
233 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
234 return ida1->access & NAME_IS_USER ? -1 : 1;
235 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
239 /* === System ACLs === */
241 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
242 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
244 static item_list temp_ida_list = EMPTY_ITEM_LIST;
245 SMB_ACL_ENTRY_T entry;
249 errfun = "sys_acl_get_entry";
250 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
252 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
253 SMB_ACL_TAG_T tag_type;
257 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
258 errfun = "sys_acl_get_tag_type";
261 if ((rc = sys_acl_get_access_bits(entry, &access)) != 0) {
262 errfun = "sys_acl_get_access_bits";
265 /* continue == done with entry; break == store in temporary ida list */
267 case SMB_ACL_USER_OBJ:
268 if (racl->user_obj == NO_ENTRY)
269 racl->user_obj = access;
271 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
274 access |= NAME_IS_USER;
276 case SMB_ACL_GROUP_OBJ:
277 if (racl->group_obj == NO_ENTRY)
278 racl->group_obj = access;
280 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
285 if (racl->mask_obj == NO_ENTRY)
286 racl->mask_obj = access;
288 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
291 if (racl->other_obj == NO_ENTRY)
292 racl->other_obj = access;
294 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
297 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
300 if (!(qualifier = sys_acl_get_qualifier(entry))) {
301 errfun = "sys_acl_get_tag_type";
305 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
306 ida->id = *((id_t *)qualifier);
307 ida->access = access;
308 sys_acl_free_qualifier(qualifier, tag_type);
311 rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
312 rsync_acl_free(racl);
316 /* Transfer the count id_access items out of the temp_ida_list
317 * into the names ida_entries list in racl. */
318 if (temp_ida_list.count) {
319 #ifdef SMB_ACL_NEED_SORT
320 if (temp_ida_list.count > 1) {
321 qsort(temp_ida_list.items, temp_ida_list.count,
322 sizeof (id_access), id_access_sorter);
325 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
326 out_of_memory("unpack_smb_acl");
327 memcpy(racl->names.idas, temp_ida_list.items,
328 temp_ida_list.count * sizeof (id_access));
330 racl->names.idas = NULL;
332 racl->names.count = temp_ida_list.count;
334 /* Truncate the temporary list now that its idas have been saved. */
335 temp_ida_list.count = 0;
337 #ifdef ACLS_NEED_MASK
338 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
339 /* Throw away a superfluous mask, but mask off the
340 * group perms with it first. */
341 racl->group_obj &= racl->mask_obj;
342 racl->mask_obj = NO_ENTRY;
349 /* Synactic sugar for system calls */
351 #define CALL_OR_ERROR(func,args,str) \
359 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
360 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
362 /* Store the permissions in the system ACL entry. */
363 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
365 if (sys_acl_set_access_bits(entry, access)) {
366 rsyserr(FERROR, errno, "store_access_in_entry sys_acl_set_access_bits()");
372 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
373 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
375 #ifdef ACLS_NEED_MASK
380 const char *errfun = NULL;
381 SMB_ACL_ENTRY_T entry;
383 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
384 rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
388 COE( sys_acl_create_entry,(smb_acl, &entry) );
389 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
390 COE2( store_access_in_entry,(racl->user_obj & ~NO_ENTRY, entry) );
392 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
393 #ifdef SMB_ACL_NEED_SORT
394 if (!(ida->access & NAME_IS_USER))
397 COE( sys_acl_create_entry,(smb_acl, &entry) );
398 #ifdef SMB_ACL_NEED_SORT
399 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
401 COE( sys_acl_set_tag_type,(entry, ida->access & NAME_IS_USER
402 ? SMB_ACL_USER : SMB_ACL_GROUP) );
404 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
405 COE2( store_access_in_entry,(ida->access & ~NAME_IS_USER, entry) );
408 COE( sys_acl_create_entry,(smb_acl, &entry) );
409 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
410 COE2( store_access_in_entry,(racl->group_obj & ~NO_ENTRY, entry) );
412 #ifdef SMB_ACL_NEED_SORT
413 for ( ; count; ida++, count--) {
414 COE( sys_acl_create_entry,(smb_acl, &entry) );
415 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
416 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
417 COE2( store_access_in_entry,(ida->access, entry) );
421 #ifdef ACLS_NEED_MASK
422 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
423 COE( sys_acl_create_entry,(smb_acl, &entry) );
424 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
425 COE2( store_access_in_entry,(mask_bits, entry) );
427 if (racl->mask_obj != NO_ENTRY) {
428 COE( sys_acl_create_entry,(smb_acl, &entry) );
429 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
430 COE2( store_access_in_entry,(racl->mask_obj, entry) );
434 COE( sys_acl_create_entry,(smb_acl, &entry) );
435 COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
436 COE2( store_access_in_entry,(racl->other_obj & ~NO_ENTRY, entry) );
439 if (sys_acl_valid(*smb_acl) < 0)
440 rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
447 rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
449 sys_acl_free_acl(*smb_acl);
453 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
454 const item_list *racl_list)
456 static int access_match = -1, default_match = -1;
457 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
458 size_t count = racl_list->count;
460 /* If this is the first time through or we didn't match the last
461 * time, then start at the end of the list, which should be the
462 * best place to start hunting. */
464 *match = racl_list->count - 1;
466 rsync_acl *base = racl_list->items;
467 if (rsync_acl_equal(base + *match, racl))
470 *match = racl_list->count - 1;
477 static int get_rsync_acl(const char *fname, rsync_acl *racl,
478 SMB_ACL_TYPE_T type, mode_t mode)
480 SMB_ACL_T sacl = sys_acl_get_file(fname, type);
483 BOOL ok = unpack_smb_acl(sacl, racl);
485 sys_acl_free_acl(sacl);
489 } else if (no_acl_syscall_error(errno)) {
490 /* ACLs are not supported, so pretend we have a basic ACL. */
491 if (type == SMB_ACL_TYPE_ACCESS)
492 rsync_acl_fake_perms(racl, mode);
494 rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
495 fname, str_acl_type(type));
502 /* Return the Access Control List for the given filename. */
503 int get_acl(const char *fname, stat_x *sxp)
505 sxp->acc_acl = create_racl();
506 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
507 sxp->st.st_mode) < 0) {
512 if (S_ISDIR(sxp->st.st_mode)) {
513 sxp->def_acl = create_racl();
514 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
515 sxp->st.st_mode) < 0) {
524 /* === Send functions === */
526 /* The general strategy with the tag_type <-> character mapping is that
527 * lowercase implies that no qualifier follows, where uppercase does.
528 * A similar idiom for the ACL type (access or default) itself, but
529 * lowercase in this instance means there's no ACL following, so the
530 * ACL is a repeat, so the receiver should reuse the last of the same
533 /* Send the ida list over the file descriptor. */
534 static void send_ida_entries(const ida_entries *idal, int f)
537 size_t count = idal->count;
539 write_varint(f, idal->count);
541 for (ida = idal->idas; count--; ida++) {
542 uint32 xbits = ida->access << 2;
544 if (ida->access & NAME_IS_USER) {
545 xbits |= XFLAG_NAME_IS_USER;
546 name = add_uid(ida->id);
548 name = add_gid(ida->id);
549 write_varint(f, ida->id);
550 if (inc_recurse && name) {
551 int len = strlen(name);
552 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
554 write_buf(f, name, len);
556 write_varint(f, xbits);
560 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
561 item_list *racl_list, int f)
563 int ndx = find_matching_rsync_acl(racl, type, racl_list);
565 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
566 write_varint(f, ndx + 1);
569 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
572 if (racl->user_obj != NO_ENTRY)
573 flags |= XMIT_USER_OBJ;
574 if (racl->group_obj != NO_ENTRY)
575 flags |= XMIT_GROUP_OBJ;
576 if (racl->mask_obj != NO_ENTRY)
577 flags |= XMIT_MASK_OBJ;
578 if (racl->other_obj != NO_ENTRY)
579 flags |= XMIT_OTHER_OBJ;
580 if (racl->names.count)
581 flags |= XMIT_NAME_LIST;
583 write_byte(f, flags);
585 if (flags & XMIT_USER_OBJ)
586 write_varint(f, racl->user_obj);
587 if (flags & XMIT_GROUP_OBJ)
588 write_varint(f, racl->group_obj);
589 if (flags & XMIT_MASK_OBJ)
590 write_varint(f, racl->mask_obj);
591 if (flags & XMIT_OTHER_OBJ)
592 write_varint(f, racl->other_obj);
593 if (flags & XMIT_NAME_LIST)
594 send_ida_entries(&racl->names, f);
596 /* Give the allocated data to the new list object. */
598 *racl = empty_rsync_acl;
602 /* Send the ACL from the stat_x structure down the indicated file descriptor.
603 * This also frees the ACL data. */
604 void send_acl(stat_x *sxp, int f)
607 sxp->acc_acl = create_racl();
608 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
610 /* Avoid sending values that can be inferred from other data. */
611 rsync_acl_strip_perms(sxp->acc_acl);
613 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
615 if (S_ISDIR(sxp->st.st_mode)) {
617 sxp->def_acl = create_racl();
619 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
623 /* === Receive functions === */
625 static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
627 uint32 access = read_varint(f);
629 if (name_follows_ptr) {
630 int flags = access & 3;
632 if (access & ~SMB_ACL_VALID_NAME_BITS)
634 if (flags & XFLAG_NAME_FOLLOWS)
635 *name_follows_ptr = 1;
637 *name_follows_ptr = 0;
638 if (flags & XFLAG_NAME_IS_USER)
639 access |= NAME_IS_USER;
640 } else if (access & ~SMB_ACL_VALID_OBJ_BITS) {
642 rprintf(FERROR, "recv_acl_access: value out of range: %x\n",
644 exit_cleanup(RERR_STREAMIO);
650 static uchar recv_ida_entries(ida_entries *ent, int f)
652 uchar computed_mask_bits = 0;
653 int i, count = read_varint(f);
656 if (!(ent->idas = new_array(id_access, count)))
657 out_of_memory("recv_ida_entries");
663 for (i = 0; i < count; i++) {
665 id_t id = read_varint(f);
666 uint32 access = recv_acl_access(&has_name, f);
669 if (access & NAME_IS_USER)
670 id = recv_user_name(f, id);
672 id = recv_group_name(f, id, NULL);
673 } else if (access & NAME_IS_USER) {
674 if (inc_recurse && am_root && !numeric_ids)
677 if (inc_recurse && (!am_root || !numeric_ids))
678 id = match_gid(id, NULL);
681 ent->idas[i].id = id;
682 ent->idas[i].access = access;
683 computed_mask_bits |= access;
686 return computed_mask_bits & ~NO_ENTRY;
689 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
691 uchar computed_mask_bits = 0;
694 int ndx = read_varint(f);
696 if (ndx < 0 || (size_t)ndx > racl_list->count) {
697 rprintf(FERROR, "recv_acl_index: %s ACL index %d > %d\n",
698 str_acl_type(type), ndx, (int)racl_list->count);
699 exit_cleanup(RERR_STREAMIO);
705 ndx = racl_list->count;
706 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
707 duo_item->racl = empty_rsync_acl;
709 flags = read_byte(f);
711 if (flags & XMIT_USER_OBJ)
712 duo_item->racl.user_obj = recv_acl_access(NULL, f);
713 if (flags & XMIT_GROUP_OBJ)
714 duo_item->racl.group_obj = recv_acl_access(NULL, f);
715 if (flags & XMIT_MASK_OBJ)
716 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
717 if (flags & XMIT_OTHER_OBJ)
718 duo_item->racl.other_obj = recv_acl_access(NULL, f);
719 if (flags & XMIT_NAME_LIST)
720 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
722 if (!duo_item->racl.names.count) {
723 /* If we received a superfluous mask, throw it away. */
724 if (duo_item->racl.mask_obj != NO_ENTRY) {
725 /* Mask off the group perms with it first. */
726 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
727 duo_item->racl.mask_obj = NO_ENTRY;
729 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
730 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
732 duo_item->sacl = NULL;
737 /* Receive the ACL info the sender has included for this file-list entry. */
738 void receive_acl(struct file_struct *file, int f)
740 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
742 if (S_ISDIR(file->mode))
743 F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
746 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
752 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
754 ndx = racl_list->count;
755 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
756 new_duo->racl = *racl;
757 new_duo->sacl = NULL;
758 *racl = empty_rsync_acl;
764 /* Turn the ACL data in stat_x into cached ACL data, setting the index
765 * values in the file struct. */
766 void cache_acl(struct file_struct *file, stat_x *sxp)
768 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
769 SMB_ACL_TYPE_ACCESS, &access_acl_list);
771 if (S_ISDIR(sxp->st.st_mode)) {
772 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
773 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
777 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
779 SMB_ACL_ENTRY_T entry;
784 /* If the sticky bit is going on, it's not safe to allow all
785 * the new ACL to go into effect before it gets set. */
786 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
790 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
793 /* If setuid or setgid is going off, it's not safe to allow all
794 * the new ACL to go into effect before they get cleared. */
795 if ((old_mode & S_ISUID && !(mode & S_ISUID))
796 || (old_mode & S_ISGID && !(mode & S_ISGID)))
801 errfun = "sys_acl_get_entry";
802 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
804 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
805 SMB_ACL_TAG_T tag_type;
806 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
807 errfun = "sys_acl_get_tag_type";
811 case SMB_ACL_USER_OBJ:
812 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
814 case SMB_ACL_GROUP_OBJ:
815 /* group is only empty when identical to group perms. */
816 if (racl->group_obj != NO_ENTRY)
818 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
821 #ifndef ACLS_NEED_MASK
822 /* mask is only empty when we don't need it. */
823 if (racl->mask_obj == NO_ENTRY)
826 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
829 COE2( store_access_in_entry,(mode & 7, entry) );
836 rsyserr(FERROR, errno, "change_sacl_perms: %s()",
842 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
843 /* Ensure that chmod() will be called to restore any lost setid bits. */
844 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
845 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
846 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
849 /* Return the mode of the file on disk, as we will set them. */
850 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
853 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
854 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
856 if (type == SMB_ACL_TYPE_DEFAULT
857 && duo_item->racl.user_obj == NO_ENTRY) {
858 if (sys_acl_delete_def_file(fname) < 0) {
859 rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
864 mode_t cur_mode = sxp->st.st_mode;
866 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
868 if (type == SMB_ACL_TYPE_ACCESS) {
869 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
871 if (cur_mode == (mode_t)~0)
874 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
875 rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
876 fname, str_acl_type(type));
879 if (type == SMB_ACL_TYPE_ACCESS)
880 sxp->st.st_mode = cur_mode;
886 /* Set ACL on indicated filename.
888 * This sets extended access ACL entries and default ACL. If convenient,
889 * it sets permission bits along with the access ACL and signals having
890 * done so by modifying sxp->st.st_mode.
892 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
893 * with fname set to NULL to just check if the ACL is unchanged. */
894 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp)
900 if (!dry_run && (read_only || list_only)) {
906 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
907 acl_duo *duo_item = access_acl_list.items;
910 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
913 if (!dry_run && fname
914 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
915 sxp, file->mode) < 0)
920 if (!S_ISDIR(sxp->st.st_mode))
923 ndx = F_DIR_DEFACL(file);
924 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
925 acl_duo *duo_item = default_acl_list.items;
927 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
931 if (!dry_run && fname
932 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
933 sxp, file->mode) < 0)
941 /* Non-incremental recursion needs to convert all the received IDs.
942 * This is done in a single pass after receiving the whole file-list. */
943 static void match_racl_ids(const item_list *racl_list)
945 int list_cnt, name_cnt;
946 acl_duo *duo_item = racl_list->items;
947 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
948 ida_entries *idal = &duo_item->racl.names;
949 id_access *ida = idal->idas;
950 for (name_cnt = idal->count; name_cnt--; ida++) {
951 if (ida->access & NAME_IS_USER)
952 ida->id = match_uid(ida->id);
954 ida->id = match_gid(ida->id, NULL);
959 void match_acl_ids(void)
961 match_racl_ids(&access_acl_list);
962 match_racl_ids(&default_acl_list);
965 /* This is used by dest_mode(). */
966 int default_perms_for_dir(const char *dir)
975 perms = ACCESSPERMS & ~orig_umask;
976 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
977 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
979 /* Couldn't get an ACL. Darn. */
985 /* No ACLs are available. */
989 /* We're doing a dry run, so the containing directory
990 * wasn't actually created. Don't worry about it. */
993 /* Otherwise fall through. */
995 rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
996 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1002 racl = empty_rsync_acl;
1003 ok = unpack_smb_acl(sacl, &racl);
1004 sys_acl_free_acl(sacl);
1006 rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1010 /* Apply the permission-bit entries of the default ACL, if any. */
1011 if (racl.user_obj != NO_ENTRY) {
1012 perms = rsync_acl_get_perms(&racl);
1014 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1017 rsync_acl_free(&racl);
1021 #endif /* SUPPORT_ACLS */