X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/a2c473bb59789eeeb45e541bd405cc8ccd3577f5..6500e0769a56919df1d47759828104a08df850bb:/acls.c diff --git a/acls.c b/acls.c index 3670a4da..ef2751c3 100644 --- a/acls.c +++ b/acls.c @@ -3,7 +3,7 @@ * * Copyright (C) 1996 Andrew Tridgell * Copyright (C) 1996 Paul Mackerras - * Copyright (C) 2006 Wayne Davison + * Copyright (C) 2006-2009 Wayne Davison * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,6 +31,8 @@ extern int list_only; extern int orig_umask; extern int numeric_ids; extern int inc_recurse; +extern int preserve_devices; +extern int preserve_specials; /* Flags used to indicate what items are being transmitted for an entry. */ #define XMIT_USER_OBJ (1<<0) @@ -88,6 +90,9 @@ static const rsync_acl empty_rsync_acl = { static item_list access_acl_list = EMPTY_ITEM_LIST; static item_list default_acl_list = EMPTY_ITEM_LIST; +static size_t prior_access_count = (size_t)-1; +static size_t prior_default_count = (size_t)-1; + /* === Calculations on ACL types === */ static const char *str_acl_type(SMB_ACL_TYPE_T type) @@ -101,6 +106,8 @@ static const char *str_acl_type(SMB_ACL_TYPE_T type) #endif case SMB_ACL_TYPE_DEFAULT: return "ACL_TYPE_DEFAULT"; + default: + break; } return "unknown ACL type!"; } @@ -110,10 +117,11 @@ static int calc_sacl_entries(const rsync_acl *racl) /* A System ACL always gets user/group/other permission entries. */ return racl->names.count #ifdef ACLS_NEED_MASK - + 4; + + 1 #else - + (racl->mask_obj != NO_ENTRY) + 3; + + (racl->mask_obj != NO_ENTRY) #endif + + 3; } /* Extracts and returns the permission bits from the ACL. This cannot be @@ -127,15 +135,21 @@ static int rsync_acl_get_perms(const rsync_acl *racl) /* Removes the permission-bit entries from the ACL because these * can be reconstructed from the file's mode. */ -static void rsync_acl_strip_perms(rsync_acl *racl) +static void rsync_acl_strip_perms(stat_x *sxp) { + rsync_acl *racl = sxp->acc_acl; + racl->user_obj = NO_ENTRY; if (racl->mask_obj == NO_ENTRY) racl->group_obj = NO_ENTRY; else { - if (racl->group_obj == racl->mask_obj) + int group_perms = (sxp->st.st_mode >> 3) & 7; + if (racl->group_obj == group_perms) racl->group_obj = NO_ENTRY; - racl->mask_obj = NO_ENTRY; +#ifndef HAVE_SOLARIS_ACLS + if (racl->names.count != 0 && racl->mask_obj == group_perms) + racl->mask_obj = NO_ENTRY; +#endif } racl->other_obj = NO_ENTRY; } @@ -334,15 +348,6 @@ static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl) /* Truncate the temporary list now that its idas have been saved. */ temp_ida_list.count = 0; -#ifdef ACLS_NEED_MASK - if (!racl->names.count && racl->mask_obj != NO_ENTRY) { - /* Throw away a superfluous mask, but mask off the - * group perms with it first. */ - racl->group_obj &= racl->mask_obj; - racl->mask_obj = NO_ENTRY; - } -#endif - return True; } @@ -534,6 +539,23 @@ static int get_rsync_acl(const char *fname, rsync_acl *racl, int get_acl(const char *fname, stat_x *sxp) { sxp->acc_acl = create_racl(); + + if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) { + /* Everyone supports this. */ + } else if (S_ISLNK(sxp->st.st_mode)) { + return 0; + } else if (IS_SPECIAL(sxp->st.st_mode)) { +#ifndef NO_SPECIAL_ACLS + if (!preserve_specials) +#endif + return 0; + } else if (IS_DEVICE(sxp->st.st_mode)) { +#ifndef NO_DEVICE_ACLS + if (!preserve_devices) +#endif + return 0; + } + if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, sxp->st.st_mode) < 0) { free_acl(sxp); @@ -555,7 +577,7 @@ int get_acl(const char *fname, stat_x *sxp) /* === Send functions === */ /* Send the ida list over the file descriptor. */ -static void send_ida_entries(const ida_entries *idal, int f) +static void send_ida_entries(int f, const ida_entries *idal) { id_access *ida; size_t count = idal->count; @@ -581,8 +603,8 @@ static void send_ida_entries(const ida_entries *idal, int f) } } -static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, - item_list *racl_list, int f) +static void send_rsync_acl(int f, rsync_acl *racl, SMB_ACL_TYPE_T type, + item_list *racl_list) { int ndx = find_matching_rsync_acl(racl, type, racl_list); @@ -615,7 +637,7 @@ static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, if (flags & XMIT_OTHER_OBJ) write_varint(f, racl->other_obj); if (flags & XMIT_NAME_LIST) - send_ida_entries(&racl->names, f); + send_ida_entries(f, &racl->names); /* Give the allocated data to the new list object. */ *new_racl = *racl; @@ -625,28 +647,28 @@ static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, /* Send the ACL from the stat_x structure down the indicated file descriptor. * This also frees the ACL data. */ -void send_acl(stat_x *sxp, int f) +void send_acl(int f, stat_x *sxp) { if (!sxp->acc_acl) { sxp->acc_acl = create_racl(); rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode); } /* Avoid sending values that can be inferred from other data. */ - rsync_acl_strip_perms(sxp->acc_acl); + rsync_acl_strip_perms(sxp); - send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f); + send_rsync_acl(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list); if (S_ISDIR(sxp->st.st_mode)) { if (!sxp->def_acl) sxp->def_acl = create_racl(); - send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f); + send_rsync_acl(f, sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list); } } /* === Receive functions === */ -static uint32 recv_acl_access(uchar *name_follows_ptr, int f) +static uint32 recv_acl_access(int f, uchar *name_follows_ptr) { uint32 access = read_varint(f); @@ -671,7 +693,7 @@ static uint32 recv_acl_access(uchar *name_follows_ptr, int f) return access; } -static uchar recv_ida_entries(ida_entries *ent, int f) +static uchar recv_ida_entries(int f, ida_entries *ent) { uchar computed_mask_bits = 0; int i, count = read_varint(f); @@ -687,7 +709,7 @@ static uchar recv_ida_entries(ida_entries *ent, int f) for (i = 0; i < count; i++) { uchar has_name; id_t id = read_varint(f); - uint32 access = recv_acl_access(&has_name, f); + uint32 access = recv_acl_access(f, &has_name); if (has_name) { if (access & NAME_IS_USER) @@ -710,7 +732,7 @@ static uchar recv_ida_entries(ida_entries *ent, int f) return computed_mask_bits & ~NO_ENTRY; } -static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f) +static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type, mode_t mode) { uchar computed_mask_bits = 0; acl_duo *duo_item; @@ -725,7 +747,7 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f) if (ndx != 0) return ndx - 1; - + ndx = racl_list->count; duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000); duo_item->racl = empty_rsync_acl; @@ -733,29 +755,28 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f) flags = read_byte(f); if (flags & XMIT_USER_OBJ) - duo_item->racl.user_obj = recv_acl_access(NULL, f); + duo_item->racl.user_obj = recv_acl_access(f, NULL); if (flags & XMIT_GROUP_OBJ) - duo_item->racl.group_obj = recv_acl_access(NULL, f); + duo_item->racl.group_obj = recv_acl_access(f, NULL); if (flags & XMIT_MASK_OBJ) - duo_item->racl.mask_obj = recv_acl_access(NULL, f); + duo_item->racl.mask_obj = recv_acl_access(f, NULL); if (flags & XMIT_OTHER_OBJ) - duo_item->racl.other_obj = recv_acl_access(NULL, f); + duo_item->racl.other_obj = recv_acl_access(f, NULL); if (flags & XMIT_NAME_LIST) - computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f); + computed_mask_bits |= recv_ida_entries(f, &duo_item->racl.names); #ifdef HAVE_OSX_ACLS /* If we received a superfluous mask, throw it away. */ duo_item->racl.mask_obj = NO_ENTRY; #else - if (!duo_item->racl.names.count) { - /* If we received a superfluous mask, throw it away. */ - if (duo_item->racl.mask_obj != NO_ENTRY) { - /* Mask off the group perms with it first. */ - duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY; - duo_item->racl.mask_obj = NO_ENTRY; - } - } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */ - duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY; + if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) { + /* Mask must be non-empty with lists. */ + if (type == SMB_ACL_TYPE_ACCESS) + computed_mask_bits = (mode >> 3) & 7; + else + computed_mask_bits |= duo_item->racl.group_obj & ~NO_ENTRY; + duo_item->racl.mask_obj = computed_mask_bits; + } #endif duo_item->sacl = NULL; @@ -764,12 +785,12 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f) } /* Receive the ACL info the sender has included for this file-list entry. */ -void receive_acl(struct file_struct *file, int f) +void receive_acl(int f, struct file_struct *file) { - F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f); + F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS, file->mode); if (S_ISDIR(file->mode)) - F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f); + F_DIR_DEFACL(file) = recv_rsync_acl(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT, 0); } static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list) @@ -792,17 +813,50 @@ static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl /* Turn the ACL data in stat_x into cached ACL data, setting the index * values in the file struct. */ -void cache_acl(struct file_struct *file, stat_x *sxp) +void cache_tmp_acl(struct file_struct *file, stat_x *sxp) { + if (prior_access_count == (size_t)-1) + prior_access_count = access_acl_list.count; + F_ACL(file) = cache_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list); if (S_ISDIR(sxp->st.st_mode)) { + if (prior_default_count == (size_t)-1) + prior_default_count = default_acl_list.count; F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list); } } +static void uncache_duo_acls(item_list *duo_list, size_t start) +{ + acl_duo *duo_item = duo_list->items; + acl_duo *duo_start = duo_item + start; + + duo_item += duo_list->count; + duo_list->count = start; + + while (duo_item-- > duo_start) { + rsync_acl_free(&duo_item->racl); + if (duo_item->sacl) + sys_acl_free_acl(duo_item->sacl); + } +} + +void uncache_tmp_acls(void) +{ + if (prior_access_count != (size_t)-1) { + uncache_duo_acls(&access_acl_list, prior_access_count); + prior_access_count = (size_t)-1; + } + + if (prior_default_count != (size_t)-1) { + uncache_duo_acls(&default_acl_list, prior_default_count); + prior_default_count = (size_t)-1; + } +} + #ifndef HAVE_OSX_ACLS static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode) { @@ -848,12 +902,14 @@ static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode COE2( store_access_in_entry,((mode >> 3) & 7, entry) ); break; case SMB_ACL_MASK: +#ifndef HAVE_SOLARIS_ACLS #ifndef ACLS_NEED_MASK /* mask is only empty when we don't need it. */ if (racl->mask_obj == NO_ENTRY) break; #endif COE2( store_access_in_entry,((mode >> 3) & 7, entry) ); +#endif break; case SMB_ACL_OTHER: COE2( store_access_in_entry,(mode & 7, entry) ); @@ -866,7 +922,7 @@ static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()", errfun); } - return (mode_t)~0; + return (mode_t)-1; } #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS @@ -935,7 +991,7 @@ static int set_rsync_acl(const char *fname, acl_duo *duo_item, if (type == SMB_ACL_TYPE_ACCESS) { cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl, cur_mode, mode); - if (cur_mode == (mode_t)~0) + if (cur_mode == (mode_t)-1) return 0; } #endif @@ -951,17 +1007,17 @@ static int set_rsync_acl(const char *fname, acl_duo *duo_item, return 0; } -/* Set ACL on indicated filename. +/* Given a fname, this sets extended access ACL entries, the default ACL (for a + * dir), and the regular mode bits on the file. Call this with fname set to + * NULL to just check if the ACL is different. * - * This sets extended access ACL entries and default ACL. If convenient, - * it sets permission bits along with the access ACL and signals having - * done so by modifying sxp->st.st_mode. + * If the ACL operation has a side-effect of changing the file's mode, the + * sxp->st.st_mode value will be changed to match. * - * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this - * with fname set to NULL to just check if the ACL is unchanged. */ -int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp) + * Returns 0 for an unchanged ACL, 1 for changed, -1 for failed. */ +int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp, mode_t new_mode) { - int unchanged = 1; + int changed = 0; int32 ndx; BOOL eq; @@ -975,18 +1031,18 @@ int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp) acl_duo *duo_item = access_acl_list.items; duo_item += ndx; eq = sxp->acc_acl - && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode); + && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, new_mode); if (!eq) { - unchanged = 0; + changed = 1; if (!dry_run && fname && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS, - sxp, file->mode) < 0) - unchanged = -1; + sxp, new_mode) < 0) + return -1; } } - if (!S_ISDIR(sxp->st.st_mode)) - return unchanged; + if (!S_ISDIR(new_mode)) + return changed; ndx = F_DIR_DEFACL(file); if (ndx >= 0 && (size_t)ndx < default_acl_list.count) { @@ -994,16 +1050,15 @@ int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp) duo_item += ndx; eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl); if (!eq) { - if (unchanged > 0) - unchanged = 0; + changed = 1; if (!dry_run && fname && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT, - sxp, file->mode) < 0) - unchanged = -1; + sxp, new_mode) < 0) + return -1; } } - return unchanged; + return changed; } /* Non-incremental recursion needs to convert all the received IDs. @@ -1079,7 +1134,7 @@ int default_perms_for_dir(const char *dir) /* Apply the permission-bit entries of the default ACL, if any. */ if (racl.user_obj != NO_ENTRY) { perms = rsync_acl_get_perms(&racl); - if (verbose > 2) + if (DEBUG_GTE(ACL, 1)) rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir); }