vfs_fruit: update handling of read-only creation of resource fork
[samba.git] / source3 / modules / vfs_fruit.c
index 73605c63445e9fcd81d419b7b8a083c24ff64499..caa06201308d6c5a7ccf233aa34912cc44b45e81 100644 (file)
@@ -142,6 +142,8 @@ struct fruit_config_data {
        const char *model;
        bool time_machine;
        off_t time_machine_max_size;
+       bool wipe_intentionally_left_blank_rfork;
+       bool delete_empty_adfiles;
 
        /*
         * Additional options, all enabled by default,
@@ -467,6 +469,45 @@ static const uint32_t set_eid[] = {
        AD_DEV, AD_INO, AD_SYN, AD_ID
 };
 
+static char empty_resourcefork[] = {
+       0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E,
+       0x54, 0x68, 0x69, 0x73, 0x20, 0x72, 0x65, 0x73,
+       0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x66, 0x6F,
+       0x72, 0x6B, 0x20, 0x69, 0x6E, 0x74, 0x65, 0x6E,
+       0x74, 0x69, 0x6F, 0x6E, 0x61, 0x6C, 0x6C, 0x79,
+       0x20, 0x6C, 0x65, 0x66, 0x74, 0x20, 0x62, 0x6C,
+       0x61, 0x6E, 0x6B, 0x20, 0x20, 0x20, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x1C, 0x00, 0x1E, 0xFF, 0xFF
+};
+
 struct fio {
        /* tcon config handle */
        struct fruit_config_data *config;
@@ -942,16 +983,75 @@ static bool ad_unpack(struct adouble *ad, const size_t nentries,
        return true;
 }
 
+static bool ad_convert_move_reso(struct adouble *ad,
+                                const struct smb_filename *smb_fname)
+{
+       char *map = MAP_FAILED;
+       size_t maplen;
+       ssize_t len;
+       int rc;
+       bool ok;
+
+       if (ad_getentrylen(ad, ADEID_RFORK) == 0) {
+               return true;
+       }
+
+       maplen = ad_getentryoff(ad, ADEID_RFORK) +
+               ad_getentrylen(ad, ADEID_RFORK);
+
+       /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
+       map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
+                  ad->ad_fd, 0);
+       if (map == MAP_FAILED) {
+               DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
+               return false;
+       }
+
+
+       memmove(map + ADEDOFF_RFORK_DOT_UND,
+               map + ad_getentryoff(ad, ADEID_RFORK),
+               ad_getentrylen(ad, ADEID_RFORK));
+
+       rc = munmap(map, maplen);
+       if (rc != 0) {
+               DBG_ERR("munmap failed: %s\n", strerror(errno));
+               return false;
+       }
+
+       ad_setentryoff(ad, ADEID_RFORK, ADEDOFF_RFORK_DOT_UND);
+
+       ok = ad_pack(ad);
+       if (!ok) {
+               DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
+               return false;
+       }
+
+       len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
+       if (len != AD_DATASZ_DOT_UND) {
+               DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
+               return false;
+       }
+
+       return true;
+}
+
 static bool ad_convert_xattr(struct adouble *ad,
                             const struct smb_filename *smb_fname,
-                            char *map)
+                            bool *converted_xattr)
 {
        static struct char_mappings **string_replace_cmaps = NULL;
+       char *map = MAP_FAILED;
+       size_t maplen;
        uint16_t i;
+       ssize_t len;
        int saved_errno = 0;
        NTSTATUS status;
+       int rc;
+       bool ok;
+
+       *converted_xattr = false;
 
-       if (ad->adx_header.adx_num_attrs == 0) {
+       if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
                return true;
        }
 
@@ -967,6 +1067,17 @@ static bool ad_convert_xattr(struct adouble *ad,
                TALLOC_FREE(mappings);
        }
 
+       maplen = ad_getentryoff(ad, ADEID_RFORK) +
+               ad_getentrylen(ad, ADEID_RFORK);
+
+       /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
+       map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
+                  ad->ad_fd, 0);
+       if (map == MAP_FAILED) {
+               DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
+               return false;
+       }
+
        for (i = 0; i < ad->adx_header.adx_num_attrs; i++) {
                struct ad_xattr_entry *e = &ad->adx_entries[i];
                char *mapped_name = NULL;
@@ -985,14 +1096,16 @@ static bool ad_convert_xattr(struct adouble *ad,
                    !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))
                {
                        DBG_ERR("string_replace_allocate failed\n");
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
 
                tmp = mapped_name;
                mapped_name = talloc_asprintf(talloc_tos(), ":%s", tmp);
                TALLOC_FREE(tmp);
                if (mapped_name == NULL) {
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
 
                stream_name = synthetic_smb_fname(talloc_tos(),
@@ -1003,7 +1116,8 @@ static bool ad_convert_xattr(struct adouble *ad,
                TALLOC_FREE(mapped_name);
                if (stream_name == NULL) {
                        DBG_ERR("synthetic_smb_fname failed\n");
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
 
                DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
@@ -1030,7 +1144,8 @@ static bool ad_convert_xattr(struct adouble *ad,
                TALLOC_FREE(stream_name);
                if (!NT_STATUS_IS_OK(status)) {
                        DBG_ERR("SMB_VFS_CREATE_FILE failed\n");
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
 
                nwritten = SMB_VFS_PWRITE(fsp,
@@ -1042,18 +1157,49 @@ static bool ad_convert_xattr(struct adouble *ad,
                        saved_errno = errno;
                        close_file(NULL, fsp, ERROR_CLOSE);
                        errno = saved_errno;
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
 
                status = close_file(NULL, fsp, NORMAL_CLOSE);
                if (!NT_STATUS_IS_OK(status)) {
-                       return -1;
+                       ok = false;
+                       goto fail;
                }
                fsp = NULL;
        }
 
        ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
-       return true;
+
+       ok = ad_pack(ad);
+       if (!ok) {
+               DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
+               goto fail;
+       }
+
+       len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
+       if (len != AD_DATASZ_DOT_UND) {
+               DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
+               ok = false;
+               goto fail;
+       }
+
+       ok = ad_convert_move_reso(ad, smb_fname);
+       if (!ok) {
+               goto fail;
+       }
+
+       *converted_xattr = true;
+       ok = true;
+
+fail:
+       rc = munmap(map, maplen);
+       if (rc != 0) {
+               DBG_ERR("munmap failed: %s\n", strerror(errno));
+               return false;
+       }
+
+       return ok;
 }
 
 static bool ad_convert_finderinfo(struct adouble *ad,
@@ -1068,6 +1214,12 @@ static bool ad_convert_finderinfo(struct adouble *ad,
        ssize_t nwritten;
        NTSTATUS status;
        int saved_errno = 0;
+       int cmp;
+
+       cmp = memcmp(ad->ad_filler, AD_FILLER_TAG_OSX, ADEDLEN_FILLER);
+       if (cmp != 0) {
+               return true;
+       }
 
        p_ad = ad_get_entry(ad, ADEID_FINDERI);
        if (p_ad == NULL) {
@@ -1152,83 +1304,158 @@ static bool ad_convert_finderinfo(struct adouble *ad,
        return true;
 }
 
-/**
- * Convert from Apple's ._ file to Netatalk
- *
- * Apple's AppleDouble may contain a FinderInfo entry longer then 32
- * bytes containing packed xattrs.
- *
- * @return -1 in case an error occurred, 0 if no conversion was done, 1
- * otherwise
- **/
-static int ad_convert(struct adouble *ad,
-                     const struct smb_filename *smb_fname)
+static bool ad_convert_truncate(struct adouble *ad,
+                               const struct smb_filename *smb_fname)
 {
-       int rc = 0;
-       char *map = MAP_FAILED;
-       size_t origlen;
+       int rc;
+
+       /*
+        * FIXME: direct ftruncate(), but we don't have a fsp for the
+        * VFS call
+        */
+       rc = ftruncate(ad->ad_fd, ADEDOFF_RFORK_DOT_UND +
+                      ad_getentrylen(ad, ADEID_RFORK));
+       if (rc != 0) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool ad_convert_blank_rfork(struct adouble *ad,
+                                  bool *blank)
+{
+       struct fruit_config_data *config = NULL;
+       uint8_t *map = MAP_FAILED;
+       size_t maplen;
+       int cmp;
        ssize_t len;
+       int rc;
        bool ok;
 
-       if (ad_getentrylen(ad, ADEID_FINDERI) == ADEDLEN_FINDERI) {
-               return 0;
+       *blank = false;
+
+       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+                               struct fruit_config_data, return false);
+
+       if (!config->wipe_intentionally_left_blank_rfork) {
+               return true;
        }
 
-       origlen = ad_getentryoff(ad, ADEID_RFORK) +
+       if (ad_getentrylen(ad, ADEID_RFORK) != sizeof(empty_resourcefork)) {
+               return true;
+       }
+
+       maplen = ad_getentryoff(ad, ADEID_RFORK) +
                ad_getentrylen(ad, ADEID_RFORK);
 
        /* FIXME: direct use of mmap(), vfs_aio_fork does it too */
-       map = mmap(NULL, origlen, PROT_READ|PROT_WRITE, MAP_SHARED,
+       map = mmap(NULL, maplen, PROT_READ|PROT_WRITE, MAP_SHARED,
                   ad->ad_fd, 0);
        if (map == MAP_FAILED) {
-               DEBUG(2, ("mmap AppleDouble: %s\n", strerror(errno)));
-               return -1;
+               DBG_ERR("mmap AppleDouble: %s\n", strerror(errno));
+               return false;
+       }
+
+       cmp = memcmp(map + ADEDOFF_RFORK_DOT_UND,
+                    empty_resourcefork,
+                    sizeof(empty_resourcefork));
+       rc = munmap(map, maplen);
+       if (rc != 0) {
+               DBG_ERR("munmap failed: %s\n", strerror(errno));
+               return false;
+       }
+
+       if (cmp != 0) {
+               return true;
        }
 
-       ok = ad_convert_xattr(ad, smb_fname, map);
+       ad_setentrylen(ad, ADEID_RFORK, 0);
+
+       ok = ad_pack(ad);
        if (!ok) {
-               munmap(map, origlen);
-               return -1;
+               return false;
+       }
+
+       len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
+       if (len != AD_DATASZ_DOT_UND) {
+               return false;
        }
 
+       *blank = true;
+       return true;
+}
+
+static bool ad_convert_delete_adfile(struct adouble *ad,
+                                    const struct smb_filename *smb_fname)
+{
+       struct fruit_config_data *config = NULL;
+       struct smb_filename *ad_name = NULL;
+       int rc;
+
        if (ad_getentrylen(ad, ADEID_RFORK) > 0) {
-               memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
-                       map + ad_getentryoff(ad, ADEID_RFORK),
-                       ad_getentrylen(ad, ADEID_RFORK));
+               return true;
        }
 
-       ad_setentryoff(ad, ADEID_RFORK,
-                      ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
+       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+                               struct fruit_config_data, return false);
 
-       /*
-        * FIXME: direct ftruncate(), but we don't have a fsp for the
-        * VFS call
-        */
-       rc = ftruncate(ad->ad_fd, ad_getentryoff(ad, ADEID_RFORK)
-                      + ad_getentrylen(ad, ADEID_RFORK));
+       if (!config->delete_empty_adfiles) {
+               return true;
+       }
+
+       rc = adouble_path(talloc_tos(), smb_fname, &ad_name);
        if (rc != 0) {
-               munmap(map, origlen);
-               return -1;
+               return false;
        }
 
-       rc = munmap(map, origlen);
+       rc = SMB_VFS_NEXT_UNLINK(ad->ad_handle, ad_name);
        if (rc != 0) {
-               DBG_ERR("munmap failed: %s\n", strerror(errno));
-               return -1;
+               DBG_ERR("Unlinking [%s] failed: %s\n",
+                       smb_fname_str_dbg(ad_name), strerror(errno));
+               TALLOC_FREE(ad_name);
+               return false;
        }
 
-       ok = ad_pack(ad);
+       DBG_WARNING("Unlinked [%s] after conversion\n", smb_fname_str_dbg(ad_name));
+       TALLOC_FREE(ad_name);
+
+       return true;
+}
+
+/**
+ * Convert from Apple's ._ file to Netatalk
+ *
+ * Apple's AppleDouble may contain a FinderInfo entry longer then 32
+ * bytes containing packed xattrs.
+ *
+ * @return -1 in case an error occurred, 0 if no conversion was done, 1
+ * otherwise
+ **/
+static int ad_convert(struct adouble *ad,
+                     const struct smb_filename *smb_fname)
+{
+       bool ok;
+       bool converted_xattr = false;
+       bool blank;
+
+       ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
        if (!ok) {
-               DBG_WARNING("ad_pack [%s] failed\n", smb_fname->base_name);
                return -1;
        }
 
-       len = sys_pwrite(ad->ad_fd, ad->ad_data, AD_DATASZ_DOT_UND, 0);
-       if (len != AD_DATASZ_DOT_UND) {
-               DBG_ERR("%s: bad size: %zd\n", smb_fname->base_name, len);
+       ok = ad_convert_blank_rfork(ad, &blank);
+       if (!ok) {
                return -1;
        }
 
+       if (converted_xattr || blank) {
+               ok = ad_convert_truncate(ad, smb_fname);
+               if (!ok) {
+                       return -1;
+               }
+       }
+
        ok = ad_convert_finderinfo(ad, smb_fname);
        if (!ok) {
                DBG_ERR("Failed to convert [%s]\n",
@@ -1236,6 +1463,11 @@ static int ad_convert(struct adouble *ad,
                return -1;
        }
 
+       ok = ad_convert_delete_adfile(ad, smb_fname);
+       if (!ok) {
+               return -1;
+       }
+
        return 0;
 }
 
@@ -2015,6 +2247,14 @@ static int init_fruit_config(vfs_handle_struct *handle)
                config->time_machine_max_size = conv_str_size(tm_size_str);
        }
 
+       config->wipe_intentionally_left_blank_rfork = lp_parm_bool(
+               SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
+               "wipe_intentionally_left_blank_rfork", false);
+
+       config->delete_empty_adfiles = lp_parm_bool(
+               SNUM(handle->conn), FRUIT_PARAM_TYPE_NAME,
+               "delete_empty_adfiles", false);
+
        SMB_VFS_HANDLE_SET_DATA(handle, config,
                                NULL, struct fruit_config_data,
                                return -1);
@@ -3335,12 +3575,9 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
                goto exit;
        }
 
-       /* Sanitize flags */
-       if (flags & O_WRONLY) {
-               /* We always need read access for the metadata header too */
-               flags &= ~O_WRONLY;
-               flags |= O_RDWR;
-       }
+       /* We always need read/write access for the metadata header too */
+       flags &= ~(O_RDONLY | O_WRONLY);
+       flags |= O_RDWR;
 
        hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
                                   flags, mode);
@@ -3427,20 +3664,6 @@ static int fruit_open_rsrc(vfs_handle_struct *handle,
        SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return -1);
 
-       if (((flags & O_ACCMODE) == O_RDONLY)
-           && (flags & O_CREAT)
-           && !VALID_STAT(fsp->fsp_name->st))
-       {
-               /*
-                * This means the stream doesn't exist. macOS SMB server fails
-                * this with NT_STATUS_OBJECT_NAME_NOT_FOUND, so must we. Cf bug
-                * 12565 and the test for this combination in
-                * test_rfork_create().
-                */
-               errno = ENOENT;
-               return -1;
-       }
-
        switch (config->rsrc) {
        case FRUIT_RSRC_STREAM:
                fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);