Added OS X ACL support, using slightly tweaked sys_acl_*()
[rsync/rsync.git] / acls.c
diff --git a/acls.c b/acls.c
index cc8d2e9..a2aaf08 100644 (file)
--- a/acls.c
+++ b/acls.c
@@ -6,8 +6,9 @@
  * Copyright (C) 2006 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,8 +16,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ * with this program; if not, visit the http://fsf.org website.
  */
 
 #include "rsync.h"
@@ -29,7 +29,6 @@ extern int am_root;
 extern int read_only;
 extern int list_only;
 extern int orig_umask;
-extern int protocol_version;
 extern int numeric_ids;
 extern int inc_recurse;
 
@@ -211,7 +210,7 @@ static void rsync_acl_free(rsync_acl *racl)
        *racl = empty_rsync_acl;
 }
 
-void free_acl(statx *sxp)
+void free_acl(stat_x *sxp)
 {
        if (sxp->acc_acl) {
                rsync_acl_free(sxp->acc_acl);
@@ -253,35 +252,27 @@ static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
             rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
                SMB_ACL_TAG_T tag_type;
                uint32 access;
-               void *qualifier;
+               id_t g_u_id;
                id_access *ida;
-               if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
-                       errfun = "sys_acl_get_tag_type";
-                       break;
-               }
-               if ((rc = sys_acl_get_access_bits(entry, &access)) != 0) {
-                       errfun = "sys_acl_get_access_bits";
+               if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
+                       errfun = "sys_acl_get_info";
                        break;
                }
                /* continue == done with entry; break == store in temporary ida list */
                switch (tag_type) {
+#ifndef HAVE_OSX_ACLS
                case SMB_ACL_USER_OBJ:
                        if (racl->user_obj == NO_ENTRY)
                                racl->user_obj = access;
                        else
                                rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
                        continue;
-               case SMB_ACL_USER:
-                       access |= NAME_IS_USER;
-                       break;
                case SMB_ACL_GROUP_OBJ:
                        if (racl->group_obj == NO_ENTRY)
                                racl->group_obj = access;
                        else
                                rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
                        continue;
-               case SMB_ACL_GROUP:
-                       break;
                case SMB_ACL_MASK:
                        if (racl->mask_obj == NO_ENTRY)
                                racl->mask_obj = access;
@@ -294,19 +285,19 @@ static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
                        else
                                rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
                        continue;
+#endif
+               case SMB_ACL_USER:
+                       access |= NAME_IS_USER;
+                       break;
+               case SMB_ACL_GROUP:
+                       break;
                default:
                        rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
                        continue;
                }
-               if (!(qualifier = sys_acl_get_qualifier(entry))) {
-                       errfun = "sys_acl_get_tag_type";
-                       rc = EINVAL;
-                       break;
-               }
                ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
-               ida->id = *((id_t *)qualifier);
+               ida->id = g_u_id;
                ida->access = access;
-               sys_acl_free_qualifier(qualifier, tag_type);
        }
        if (rc) {
                rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
@@ -360,6 +351,7 @@ static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
 
+#ifndef HAVE_OSX_ACLS
 /* Store the permissions in the system ACL entry. */
 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
 {
@@ -369,6 +361,7 @@ static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
        }
        return 0;
 }
+#endif
 
 /* Pack rsync ACL -> system ACL verbatim.  Return whether we succeeded. */
 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
@@ -386,9 +379,10 @@ static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
                return False;
        }
 
+#ifndef HAVE_OSX_ACLS
        COE( sys_acl_create_entry,(smb_acl, &entry) );
-       COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
-       COE2( store_access_in_entry,(racl->user_obj & ~NO_ENTRY, entry) );
+       COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
+#endif
 
        for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
 #ifdef SMB_ACL_NEED_SORT
@@ -396,45 +390,40 @@ static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
                        break;
 #endif
                COE( sys_acl_create_entry,(smb_acl, &entry) );
+               COE( sys_acl_set_info,(entry,
 #ifdef SMB_ACL_NEED_SORT
-               COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
+                                      SMB_ACL_USER,
 #else
-               COE( sys_acl_set_tag_type,(entry, ida->access & NAME_IS_USER
-                                               ? SMB_ACL_USER : SMB_ACL_GROUP) );
+                                      ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
 #endif
-               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
-               COE2( store_access_in_entry,(ida->access & ~NAME_IS_USER, entry) );
+                                      ida->access & ~NAME_IS_USER, ida->id) );
        }
 
+#ifndef HAVE_OSX_ACLS
        COE( sys_acl_create_entry,(smb_acl, &entry) );
-       COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
-       COE2( store_access_in_entry,(racl->group_obj & ~NO_ENTRY, entry) );
+       COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
 
 #ifdef SMB_ACL_NEED_SORT
        for ( ; count; ida++, count--) {
                COE( sys_acl_create_entry,(smb_acl, &entry) );
-               COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
-               COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
-               COE2( store_access_in_entry,(ida->access, entry) );
+               COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
        }
 #endif
 
 #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_tag_type,(entry, SMB_ACL_MASK) );
-       COE2( store_access_in_entry,(mask_bits, entry) );
+       COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
 #else
        if (racl->mask_obj != NO_ENTRY) {
                COE( sys_acl_create_entry,(smb_acl, &entry) );
-               COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
-               COE2( store_access_in_entry,(racl->mask_obj, entry) );
+               COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
        }
 #endif
 
        COE( sys_acl_create_entry,(smb_acl, &entry) );
-       COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
-       COE2( store_access_in_entry,(racl->other_obj & ~NO_ENTRY, entry) );
+       COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
+#endif
 
 #ifdef DEBUG
        if (sys_acl_valid(*smb_acl) < 0)
@@ -501,7 +490,7 @@ static int get_rsync_acl(const char *fname, rsync_acl *racl,
 }
 
 /* Return the Access Control List for the given filename. */
-int get_acl(const char *fname, statx *sxp)
+int get_acl(const char *fname, stat_x *sxp)
 {
        sxp->acc_acl = create_racl();
        if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
@@ -524,13 +513,6 @@ int get_acl(const char *fname, statx *sxp)
 
 /* === Send functions === */
 
-/* The general strategy with the tag_type <-> character mapping is that
- * lowercase implies that no qualifier follows, where uppercase does.
- * A similar idiom for the ACL type (access or default) itself, but
- * lowercase in this instance means there's no ACL following, so the
- * ACL is a repeat, so the receiver should reuse the last of the same
- * type ACL. */
-
 /* Send the ida list over the file descriptor. */
 static void send_ida_entries(const ida_entries *idal, int f)
 {
@@ -600,9 +582,9 @@ static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
        }
 }
 
-/* Send the ACL from the statx structure down the indicated file descriptor.
+/* Send the ACL from the stat_x structure down the indicated file descriptor.
  * This also frees the ACL data. */
-void send_acl(statx *sxp, int f)
+void send_acl(stat_x *sxp, int f)
 {
        if (!sxp->acc_acl) {
                sxp->acc_acl = create_racl();
@@ -727,8 +709,11 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
                        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. */
+       }
+#ifndef HAVE_OSX_ACLS
+       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;
+#endif
 
        duo_item->sacl = NULL;
 
@@ -741,7 +726,7 @@ void receive_acl(struct file_struct *file, int f)
        F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
 
        if (S_ISDIR(file->mode))
-               F_DEF_ACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
+               F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
 }
 
 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
@@ -762,19 +747,20 @@ static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl
        return ndx;
 }
 
-/* Turn the ACL data in statx into cached ACL data, setting the index
+/* 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, statx *sxp)
+void cache_acl(struct file_struct *file, stat_x *sxp)
 {
        F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
                                      SMB_ACL_TYPE_ACCESS, &access_acl_list);
 
        if (S_ISDIR(sxp->st.st_mode)) {
-               F_DEF_ACL(file) = cache_rsync_acl(sxp->def_acl,
+               F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
                                      SMB_ACL_TYPE_DEFAULT, &default_acl_list);
        }
 }
 
+#ifndef HAVE_OSX_ACLS
 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
 {
        SMB_ACL_ENTRY_T entry;
@@ -850,9 +836,10 @@ static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode
        /* Return the mode of the file on disk, as we will set them. */
        return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
 }
+#endif
 
 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
-                        SMB_ACL_TYPE_T type, statx *sxp, mode_t mode)
+                        SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
 {
        if (type == SMB_ACL_TYPE_DEFAULT
         && duo_item->racl.user_obj == NO_ENTRY) {
@@ -866,12 +853,16 @@ static int set_rsync_acl(const char *fname, acl_duo *duo_item,
                if (!duo_item->sacl
                 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
                        return -1;
+#ifdef HAVE_OSX_ACLS
+               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);
                        if (cur_mode == (mode_t)~0)
                                return 0;
                }
+#endif
                if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
                        rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
                        fname, str_acl_type(type));
@@ -892,7 +883,7 @@ static int set_rsync_acl(const char *fname, acl_duo *duo_item,
  *
  * 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, statx *sxp)
+int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp)
 {
        int unchanged = 1;
        int32 ndx;
@@ -921,7 +912,7 @@ int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
        if (!S_ISDIR(sxp->st.st_mode))
                return unchanged;
 
-       ndx = F_DEF_ACL(file);
+       ndx = F_DIR_DEFACL(file);
        if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
                acl_duo *duo_item = default_acl_list.items;
                duo_item += ndx;