Add info on single-file copying; tweak `--mkpath`.
[rsync.git] / acls.c
diff --git a/acls.c b/acls.c
index f7070dd8abe3d83702c0a18c853d1399b5f079b4..3cf12eeb07c42238b81236850618bd8c2b1b23ba 100644 (file)
--- a/acls.c
+++ b/acls.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 1996 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2006-2009 Wayne Davison
+ * Copyright (C) 2006-2022 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)
@@ -46,7 +48,7 @@ extern int inc_recurse;
 /* When we send the access bits over the wire, we shift them 2 bits to the
  * left and use the lower 2 bits as flags (relevant only to a name entry).
  * This makes the protocol more efficient than sending a value that would
- * be likely to have its hightest bits set. */
+ * be likely to have its highest bits set. */
 #define XFLAG_NAME_FOLLOWS 0x0001u
 #define XFLAG_NAME_IS_USER 0x0002u
 
@@ -133,16 +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;
-               if (racl->names.count != 0)
+#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;
 }
@@ -161,8 +168,6 @@ static rsync_acl *create_racl(void)
 {
        rsync_acl *racl = new(rsync_acl);
 
-       if (!racl)
-               out_of_memory("create_racl");
        *racl = empty_rsync_acl;
 
        return racl;
@@ -325,14 +330,11 @@ static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
        if (temp_ida_list.count) {
 #ifdef SMB_ACL_NEED_SORT
                if (temp_ida_list.count > 1) {
-                       qsort(temp_ida_list.items, temp_ida_list.count,
-                             sizeof (id_access), id_access_sorter);
+                       qsort(temp_ida_list.items, temp_ida_list.count, sizeof (id_access), id_access_sorter);
                }
 #endif
-               if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
-                       out_of_memory("unpack_smb_acl");
-               memcpy(racl->names.idas, temp_ida_list.items,
-                      temp_ida_list.count * sizeof (id_access));
+               racl->names.idas = new_array(id_access, temp_ida_list.count);
+               memcpy(racl->names.idas, temp_ida_list.items, temp_ida_list.count * sizeof (id_access));
        } else
                racl->names.idas = NULL;
 
@@ -341,15 +343,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;
 }
 
@@ -425,7 +418,7 @@ static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
 #ifdef ACLS_NEED_MASK
        mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
        COE( sys_acl_create_entry,(smb_acl, &entry) );
-       COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
+       COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, 0) );
 #else
        if (racl->mask_obj != NO_ENTRY) {
                COE( sys_acl_create_entry,(smb_acl, &entry) );
@@ -497,15 +490,19 @@ static int get_rsync_acl(const char *fname, rsync_acl *racl,
                }
 
                racl->user_obj = IVAL(buf, 0);
+               if (racl->user_obj == NO_ENTRY)
+                       racl->user_obj = (mode >> 6) & 7;
                racl->group_obj = IVAL(buf, 4);
+               if (racl->group_obj == NO_ENTRY)
+                       racl->group_obj = (mode >> 3) & 7;
                racl->mask_obj = IVAL(buf, 8);
                racl->other_obj = IVAL(buf, 12);
+               if (racl->other_obj == NO_ENTRY)
+                       racl->other_obj = mode & 7;
 
                if (cnt) {
                        char *bp = buf + 4*4;
-                       id_access *ida;
-                       if (!(ida = racl->names.idas = new_array(id_access, cnt)))
-                               out_of_memory("get_rsync_acl");
+                       id_access *ida = racl->names.idas = new_array(id_access, cnt);
                        racl->names.count = cnt;
                        for ( ; cnt--; ida++, bp += 4+4) {
                                ida->id = IVAL(bp, 0);
@@ -522,6 +519,7 @@ static int get_rsync_acl(const char *fname, rsync_acl *racl,
 
                sys_acl_free_acl(sacl);
                if (!ok) {
+                       rsyserr(FERROR_XFER, errno, "get_acl: unpack_smb_acl(%s)", fname);
                        return -1;
                }
        } else if (no_acl_syscall_error(errno)) {
@@ -541,6 +539,24 @@ 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;
+       } else if (IS_MISSING_FILE(sxp->st))
+               return 0;
+
        if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
                          sxp->st.st_mode) < 0) {
                free_acl(sxp);
@@ -574,9 +590,9 @@ static void send_ida_entries(int f, const ida_entries *idal)
                const char *name;
                if (ida->access & NAME_IS_USER) {
                        xbits |= XFLAG_NAME_IS_USER;
-                       name = add_uid(ida->id);
+                       name = numeric_ids ? NULL : add_uid(ida->id);
                } else
-                       name = add_gid(ida->id);
+                       name = numeric_ids ? NULL : add_gid(ida->id);
                write_varint(f, ida->id);
                if (inc_recurse && name) {
                        int len = strlen(name);
@@ -639,7 +655,7 @@ void send_acl(int f, stat_x *sxp)
                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(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
 
@@ -683,12 +699,7 @@ static uchar recv_ida_entries(int f, ida_entries *ent)
        uchar computed_mask_bits = 0;
        int i, count = read_varint(f);
 
-       if (count) {
-               if (!(ent->idas = new_array(id_access, count)))
-                       out_of_memory("recv_ida_entries");
-       } else
-               ent->idas = NULL;
-
+       ent->idas = count ? new_array(id_access, count) : NULL;
        ent->count = count;
 
        for (i = 0; i < count; i++) {
@@ -717,7 +728,7 @@ static uchar recv_ida_entries(int f, ida_entries *ent)
        return computed_mask_bits & ~NO_ENTRY;
 }
 
-static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type)
+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;
@@ -753,9 +764,16 @@ static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type)
 #ifdef HAVE_OSX_ACLS
        /* If we received a superfluous mask, throw it away. */
        duo_item->racl.mask_obj = NO_ENTRY;
+       (void)mode;
 #else
-       if (duo_item->racl.names.count && 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;
@@ -766,10 +784,10 @@ static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type)
 /* Receive the ACL info the sender has included for this file-list entry. */
 void receive_acl(int f, struct file_struct *file)
 {
-       F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS);
+       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(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT);
+               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)
@@ -797,14 +815,12 @@ 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);
+       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);
+               F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
        }
 }
 
@@ -881,12 +897,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) );
@@ -966,8 +984,7 @@ static int set_rsync_acl(const char *fname, acl_duo *duo_item,
                mode = 0; /* eliminate compiler warning */
 #else
                if (type == SMB_ACL_TYPE_ACCESS) {
-                       cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
-                                                    cur_mode, mode);
+                       cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl, cur_mode, mode);
                        if (cur_mode == (mode_t)-1)
                                return 0;
                }
@@ -1078,20 +1095,21 @@ int default_perms_for_dir(const char *dir)
        if (sacl == NULL) {
                /* Couldn't get an ACL.  Darn. */
                switch (errno) {
+               case EINVAL:
+                       /* If SMB_ACL_TYPE_DEFAULT isn't valid, then the ACLs must be non-POSIX. */
+                       break;
 #ifdef ENOTSUP
                case ENOTSUP:
 #endif
                case ENOSYS:
                        /* No ACLs are available. */
                        break;
-               case ENOENT:
-                       if (dry_run) {
+               default:
+                       if (dry_run && errno == ENOENT) {
                                /* We're doing a dry run, so the containing directory
                                 * wasn't actually created.  Don't worry about it. */
                                break;
                        }
-                       /* Otherwise fall through. */
-               default:
                        rprintf(FWARNING,
                                "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
                                dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));