vfs_fruit: pass handle to ad_fset()
[metze/samba-autobuild/.git] / source3 / modules / vfs_fruit.c
index 08747dc3cee7d62e5c6042c8f460f92913d26fbf..5f4ae06d845fa62b2eb0a06b87855a8221d0e28d 100644 (file)
@@ -22,7 +22,6 @@
 #include "smbd/smbd.h"
 #include "system/filesys.h"
 #include "lib/util/time.h"
-#include "../lib/crypto/md5.h"
 #include "system/shmem.h"
 #include "locking/proto.h"
 #include "smbd/globals.h"
@@ -35,6 +34,9 @@
 #include "offload_token.h"
 #include "string_replace.h"
 
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
+
 /*
  * Enhanced OS X and Netatalk compatibility
  * ========================================
@@ -535,8 +537,14 @@ struct fio {
  */
 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                               adouble_type_t type);
+static struct adouble *ad_get(TALLOC_CTX *ctx,
+                             vfs_handle_struct *handle,
+                             const struct smb_filename *smb_fname,
+                             adouble_type_t type);
 static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
-static int ad_fset(struct adouble *ad, files_struct *fsp);
+static int ad_fset(struct vfs_handle_struct *handle,
+                  struct adouble *ad,
+                  files_struct *fsp);
 static int adouble_path(TALLOC_CTX *ctx,
                        const struct smb_filename *smb_fname__in,
                        struct smb_filename **ppsmb_fname_out);
@@ -1446,27 +1454,37 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
  * @return -1 in case an error occurred, 0 if no conversion was done, 1
  * otherwise
  **/
-static int ad_convert(struct adouble *ad,
+static int ad_convert(struct vfs_handle_struct *handle,
                      const struct smb_filename *smb_fname)
 {
+       struct adouble *ad = NULL;
        bool ok;
        bool converted_xattr = false;
        bool blank;
+       int ret;
+
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
+       if (ad == NULL) {
+               return 0;
+       }
 
        ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        ok = ad_convert_blank_rfork(ad, &blank);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        if (converted_xattr || blank) {
                ok = ad_convert_truncate(ad, smb_fname);
                if (!ok) {
-                       return -1;
+                       ret = -1;
+                       goto done;
                }
        }
 
@@ -1474,15 +1492,20 @@ static int ad_convert(struct adouble *ad,
        if (!ok) {
                DBG_ERR("Failed to convert [%s]\n",
                        smb_fname_str_dbg(smb_fname));
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        ok = ad_convert_delete_adfile(ad, smb_fname);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       return 0;
+       ret = 0;
+done:
+       TALLOC_FREE(ad);
+       return ret;
 }
 
 /**
@@ -1735,17 +1758,6 @@ static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
                return -1;
        }
 
-       /*
-        * Try to fixup AppleDouble files created by OS X with xattrs
-        * appended to the ADEID_FINDERI entry.
-        */
-
-       ret = ad_convert(ad, smb_fname);
-       if (ret != 0) {
-               DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
-               return len;
-       }
-
        return len;
 }
 
@@ -2057,7 +2069,9 @@ static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
  *
  * @return            status code, 0 means success
  **/
-static int ad_fset(struct adouble *ad, files_struct *fsp)
+static int ad_fset(struct vfs_handle_struct *handle,
+                  struct adouble *ad,
+                  files_struct *fsp)
 {
        int rc = -1;
        ssize_t len;
@@ -2079,7 +2093,7 @@ static int ad_fset(struct adouble *ad, files_struct *fsp)
 
        switch (ad->ad_type) {
        case ADOUBLE_META:
-               rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
+               rc = SMB_VFS_NEXT_SETXATTR(handle,
                                           fsp->fsp_name,
                                           AFPINFO_EA_NETATALK,
                                           ad->ad_data,
@@ -2087,7 +2101,7 @@ static int ad_fset(struct adouble *ad, files_struct *fsp)
                break;
 
        case ADOUBLE_RSRC:
-               len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
+               len = SMB_VFS_NEXT_PWRITE(handle,
                                          fsp,
                                          ad->ad_data,
                                          AD_DATASZ_DOT_UND,
@@ -2133,9 +2147,8 @@ static bool is_afpresource_stream(const struct smb_filename *smb_fname)
 }
 
 /**
- * Test whether stream is an Apple stream, not used atm
+ * Test whether stream is an Apple stream.
  **/
-#if 0
 static bool is_apple_stream(const struct smb_filename *smb_fname)
 {
        if (is_afpinfo_stream(smb_fname)) {
@@ -2146,7 +2159,6 @@ static bool is_apple_stream(const struct smb_filename *smb_fname)
        }
        return false;
 }
-#endif
 
 /**
  * Initialize config struct from our smb.conf config parameters
@@ -2383,10 +2395,11 @@ static AfpInfo *afpinfo_unpack(TALLOC_CTX *ctx, const void *data)
  **/
 static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
 {
-       MD5_CTX ctx;
+       gnutls_hash_hd_t hash_hnd = NULL;
        unsigned char hash[16];
-       SMB_INO_T result;
+       SMB_INO_T result = 0;
        char *upper_sname;
+       int rc;
 
        DBG_DEBUG("fruit_inode called for %ju/%ju [%s]\n",
                  (uintmax_t)sbuf->st_ex_dev,
@@ -2395,23 +2408,43 @@ static SMB_INO_T fruit_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
        upper_sname = talloc_strdup_upper(talloc_tos(), sname);
        SMB_ASSERT(upper_sname != NULL);
 
-       MD5Init(&ctx);
-       MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
-                 sizeof(sbuf->st_ex_dev));
-       MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
-                 sizeof(sbuf->st_ex_ino));
-       MD5Update(&ctx, (unsigned char *)upper_sname,
-                 talloc_get_size(upper_sname)-1);
-       MD5Final(hash, &ctx);
+       rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_MD5);
+       if (rc < 0) {
+               goto out;
+       }
 
-       TALLOC_FREE(upper_sname);
+       rc = gnutls_hash(hash_hnd, &(sbuf->st_ex_dev), sizeof(sbuf->st_ex_dev));
+       if (rc < 0) {
+               gnutls_hash_deinit(hash_hnd, NULL);
+               goto out;
+       }
+       rc = gnutls_hash(hash_hnd,
+                        &(sbuf->st_ex_ino),
+                        sizeof(sbuf->st_ex_ino));
+       if (rc < 0) {
+               gnutls_hash_deinit(hash_hnd, NULL);
+               goto out;
+       }
+       rc = gnutls_hash(hash_hnd,
+                        upper_sname,
+                        talloc_get_size(upper_sname) - 1);
+       if (rc < 0) {
+               gnutls_hash_deinit(hash_hnd, NULL);
+               goto out;
+       }
+
+       gnutls_hash_deinit(hash_hnd, hash);
 
        /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
        memcpy(&result, hash, sizeof(result));
+       ZERO_ARRAY(hash);
 
        DBG_DEBUG("fruit_inode \"%s\": ino=%ju\n",
                  sname, (uintmax_t)result);
 
+out:
+       TALLOC_FREE(upper_sname);
+
        return result;
 }
 
@@ -2647,7 +2680,7 @@ static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
        off_t offset = in_offset;
        off_t len = 1;
        int type = F_WRLCK;
-       pid_t pid;
+       pid_t pid = 0;
 
        result = SMB_VFS_GETLOCK(fsp, &offset, &len, &type, &pid);
        if (result == false) {
@@ -2664,156 +2697,146 @@ static bool test_netatalk_lock(files_struct *fsp, off_t in_offset)
 static NTSTATUS fruit_check_access(vfs_handle_struct *handle,
                                   files_struct *fsp,
                                   uint32_t access_mask,
-                                  uint32_t deny_mode)
+                                  uint32_t share_mode)
 {
        NTSTATUS status = NT_STATUS_OK;
-       bool open_for_reading, open_for_writing, deny_read, deny_write;
        off_t off;
-       bool have_read = false;
-       int flags;
+       bool share_for_read = (share_mode & FILE_SHARE_READ);
+       bool share_for_write = (share_mode & FILE_SHARE_WRITE);
+       bool netatalk_already_open_for_reading = false;
+       bool netatalk_already_open_for_writing = false;
+       bool netatalk_already_open_with_deny_read = false;
+       bool netatalk_already_open_with_deny_write = false;
 
        /* FIXME: hardcoded data fork, add resource fork */
        enum apple_fork fork_type = APPLE_FORK_DATA;
 
-       DEBUG(10, ("fruit_check_access: %s, am: %s/%s, dm: %s/%s\n",
+       DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
                  fsp_str_dbg(fsp),
                  access_mask & FILE_READ_DATA ? "READ" :"-",
                  access_mask & FILE_WRITE_DATA ? "WRITE" : "-",
-                 deny_mode & DENY_READ ? "DENY_READ" : "-",
-                 deny_mode & DENY_WRITE ? "DENY_WRITE" : "-"));
+                 share_mode);
 
        if (fsp->fh->fd == -1) {
                return NT_STATUS_OK;
        }
 
-       flags = fcntl(fsp->fh->fd, F_GETFL);
-       if (flags == -1) {
-               DBG_ERR("fcntl get flags [%s] fd [%d] failed [%s]\n",
-                       fsp_str_dbg(fsp), fsp->fh->fd, strerror(errno));
-               return map_nt_error_from_unix(errno);
-       }
-
-       if (flags & (O_RDONLY|O_RDWR)) {
-               /*
-                * Applying fcntl read locks requires an fd opened for
-                * reading. This means we won't be applying locks for
-                * files openend write-only, but what can we do...
-                */
-               have_read = true;
-       }
+       /* Read NetATalk opens and deny modes on the file. */
+       netatalk_already_open_for_reading = test_netatalk_lock(fsp,
+                               access_to_netatalk_brl(fork_type,
+                                       FILE_READ_DATA));
 
-       /*
-        * Check read access and deny read mode
-        */
-       if ((access_mask & FILE_READ_DATA) || (deny_mode & DENY_READ)) {
-               /* Check access */
-               open_for_reading = test_netatalk_lock(
-                       fsp, access_to_netatalk_brl(fork_type, FILE_READ_DATA));
+       netatalk_already_open_with_deny_read = test_netatalk_lock(fsp,
+                               denymode_to_netatalk_brl(fork_type,
+                                       DENY_READ));
 
-               deny_read = test_netatalk_lock(
-                       fsp, denymode_to_netatalk_brl(fork_type, DENY_READ));
+       netatalk_already_open_for_writing = test_netatalk_lock(fsp,
+                               access_to_netatalk_brl(fork_type,
+                                       FILE_WRITE_DATA));
 
-               DEBUG(10, ("read: %s, deny_write: %s\n",
-                         open_for_reading == true ? "yes" : "no",
-                         deny_read == true ? "yes" : "no"));
+       netatalk_already_open_with_deny_write = test_netatalk_lock(fsp,
+                               denymode_to_netatalk_brl(fork_type,
+                                       DENY_WRITE));
 
-               if (((access_mask & FILE_READ_DATA) && deny_read)
-                   || ((deny_mode & DENY_READ) && open_for_reading)) {
-                       return NT_STATUS_SHARING_VIOLATION;
-               }
+       /* If there are any conflicts - sharing violation. */
+       if ((access_mask & FILE_READ_DATA) &&
+                       netatalk_already_open_with_deny_read) {
+               return NT_STATUS_SHARING_VIOLATION;
+       }
 
-               /* Set locks */
-               if ((access_mask & FILE_READ_DATA) && have_read) {
-                       struct byte_range_lock *br_lck = NULL;
+       if (!share_for_read &&
+                       netatalk_already_open_for_reading) {
+               return NT_STATUS_SHARING_VIOLATION;
+       }
 
-                       off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
-                       br_lck = do_lock(
-                               handle->conn->sconn->msg_ctx, fsp,
-                               fsp->op->global->open_persistent_id, 1, off,
-                               READ_LOCK, POSIX_LOCK, false,
-                               &status, NULL);
+       if ((access_mask & FILE_WRITE_DATA) &&
+                       netatalk_already_open_with_deny_write) {
+               return NT_STATUS_SHARING_VIOLATION;
+       }
 
-                       TALLOC_FREE(br_lck);
+       if (!share_for_write &&
+                       netatalk_already_open_for_writing) {
+               return NT_STATUS_SHARING_VIOLATION;
+       }
 
-                       if (!NT_STATUS_IS_OK(status))  {
-                               return status;
-                       }
-               }
+       if (!(access_mask & FILE_READ_DATA)) {
+               /*
+                * Nothing we can do here, we need read access
+                * to set locks.
+                */
+               return NT_STATUS_OK;
+       }
 
-               if ((deny_mode & DENY_READ) && have_read) {
-                       struct byte_range_lock *br_lck = NULL;
+       /* Set NetAtalk locks matching our access */
+       if (access_mask & FILE_READ_DATA) {
+               struct byte_range_lock *br_lck = NULL;
 
-                       off = denymode_to_netatalk_brl(fork_type, DENY_READ);
-                       br_lck = do_lock(
-                               handle->conn->sconn->msg_ctx, fsp,
-                               fsp->op->global->open_persistent_id, 1, off,
-                               READ_LOCK, POSIX_LOCK, false,
-                               &status, NULL);
+               off = access_to_netatalk_brl(fork_type, FILE_READ_DATA);
+               br_lck = do_lock(
+                       handle->conn->sconn->msg_ctx, fsp,
+                       fsp->op->global->open_persistent_id, 1, off,
+                       READ_LOCK, POSIX_LOCK, false,
+                       &status, NULL);
 
-                       TALLOC_FREE(br_lck);
+               TALLOC_FREE(br_lck);
 
-                       if (!NT_STATUS_IS_OK(status)) {
-                               return status;
-                       }
+               if (!NT_STATUS_IS_OK(status))  {
+                       return status;
                }
        }
 
-       /*
-        * Check write access and deny write mode
-        */
-       if ((access_mask & FILE_WRITE_DATA) || (deny_mode & DENY_WRITE)) {
-               /* Check access */
-               open_for_writing = test_netatalk_lock(
-                       fsp, access_to_netatalk_brl(fork_type, FILE_WRITE_DATA));
+       if (!share_for_read) {
+               struct byte_range_lock *br_lck = NULL;
 
-               deny_write = test_netatalk_lock(
-                       fsp, denymode_to_netatalk_brl(fork_type, DENY_WRITE));
+               off = denymode_to_netatalk_brl(fork_type, DENY_READ);
+               br_lck = do_lock(
+                       handle->conn->sconn->msg_ctx, fsp,
+                       fsp->op->global->open_persistent_id, 1, off,
+                       READ_LOCK, POSIX_LOCK, false,
+                       &status, NULL);
 
-               DEBUG(10, ("write: %s, deny_write: %s\n",
-                         open_for_writing == true ? "yes" : "no",
-                         deny_write == true ? "yes" : "no"));
+               TALLOC_FREE(br_lck);
 
-               if (((access_mask & FILE_WRITE_DATA) && deny_write)
-                   || ((deny_mode & DENY_WRITE) && open_for_writing)) {
-                       return NT_STATUS_SHARING_VIOLATION;
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
+       }
 
-               /* Set locks */
-               if ((access_mask & FILE_WRITE_DATA) && have_read) {
-                       struct byte_range_lock *br_lck = NULL;
+       if (access_mask & FILE_WRITE_DATA) {
+               struct byte_range_lock *br_lck = NULL;
 
-                       off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
-                       br_lck = do_lock(
-                               handle->conn->sconn->msg_ctx, fsp,
-                               fsp->op->global->open_persistent_id, 1, off,
-                               READ_LOCK, POSIX_LOCK, false,
-                               &status, NULL);
+               off = access_to_netatalk_brl(fork_type, FILE_WRITE_DATA);
+               br_lck = do_lock(
+                       handle->conn->sconn->msg_ctx, fsp,
+                       fsp->op->global->open_persistent_id, 1, off,
+                       READ_LOCK, POSIX_LOCK, false,
+                       &status, NULL);
 
-                       TALLOC_FREE(br_lck);
+               TALLOC_FREE(br_lck);
 
-                       if (!NT_STATUS_IS_OK(status)) {
-                               return status;
-                       }
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
-               if ((deny_mode & DENY_WRITE) && have_read) {
-                       struct byte_range_lock *br_lck = NULL;
+       }
 
-                       off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
-                       br_lck = do_lock(
-                               handle->conn->sconn->msg_ctx, fsp,
-                               fsp->op->global->open_persistent_id, 1, off,
-                               READ_LOCK, POSIX_LOCK, false,
-                               &status, NULL);
+       if (!share_for_write) {
+               struct byte_range_lock *br_lck = NULL;
 
-                       TALLOC_FREE(br_lck);
+               off = denymode_to_netatalk_brl(fork_type, DENY_WRITE);
+               br_lck = do_lock(
+                       handle->conn->sconn->msg_ctx, fsp,
+                       fsp->op->global->open_persistent_id, 1, off,
+                       READ_LOCK, POSIX_LOCK, false,
+                       &status, NULL);
 
-                       if (!NT_STATUS_IS_OK(status)) {
-                               return status;
-                       }
+               TALLOC_FREE(br_lck);
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
        }
 
-       return status;
+       return NT_STATUS_OK;
 }
 
 static NTSTATUS check_aapl(vfs_handle_struct *handle,
@@ -3403,66 +3426,68 @@ static int fruit_connect(vfs_handle_struct *handle,
        return rc;
 }
 
+static int fruit_fake_fd(void)
+{
+       int pipe_fds[2];
+       int fd;
+       int ret;
+
+       /*
+        * Return a valid fd, but ensure any attempt to use it returns
+        * an error (EPIPE). Once we get a write on the handle, we open
+        * the real fd.
+        */
+       ret = pipe(pipe_fds);
+       if (ret != 0) {
+               return -1;
+       }
+       fd = pipe_fds[0];
+       close(pipe_fds[1]);
+
+       return fd;
+}
+
 static int fruit_open_meta_stream(vfs_handle_struct *handle,
                                  struct smb_filename *smb_fname,
                                  files_struct *fsp,
                                  int flags,
                                  mode_t mode)
 {
-       AfpInfo *ai = NULL;
-       char afpinfo_buf[AFP_INFO_SIZE];
-       ssize_t len, written;
-       int hostfd = -1;
-       int rc = -1;
+       struct fruit_config_data *config = NULL;
+       struct fio *fio = NULL;
+       int open_flags = flags & ~O_CREAT;
+       int fd;
 
-       hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
-       if (hostfd == -1) {
-               return -1;
-       }
+       DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
 
-       if (!(flags & (O_CREAT | O_TRUNC))) {
-               return hostfd;
-       }
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
+                               struct fruit_config_data, return -1);
 
-       ai = afpinfo_new(talloc_tos());
-       if (ai == NULL) {
-               rc = -1;
-               goto fail;
-       }
+       fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
+       fio->type = ADOUBLE_META;
+       fio->config = config;
 
-       len = afpinfo_pack(ai, afpinfo_buf);
-       if (len != AFP_INFO_SIZE) {
-               rc = -1;
-               goto fail;
+       fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, open_flags, mode);
+       if (fd != -1) {
+               return fd;
        }
 
-       /* Set fd, needed in SMB_VFS_NEXT_PWRITE() */
-       fsp->fh->fd = hostfd;
-
-       written = SMB_VFS_NEXT_PWRITE(handle, fsp, afpinfo_buf,
-                                     AFP_INFO_SIZE, 0);
-       fsp->fh->fd = -1;
-       if (written != AFP_INFO_SIZE) {
-               DBG_ERR("bad write [%zd/%d]\n", written, AFP_INFO_SIZE);
-               rc = -1;
-               goto fail;
+       if (!(flags & O_CREAT)) {
+               VFS_REMOVE_FSP_EXTENSION(handle, fsp);
+               return -1;
        }
 
-       rc = 0;
+       fd = fruit_fake_fd();
+       if (fd == -1) {
+               VFS_REMOVE_FSP_EXTENSION(handle, fsp);
+               return -1;
+       }
 
-fail:
-       DBG_DEBUG("rc=%d, fd=%d\n", rc, hostfd);
+       fio->fake_fd = true;
+       fio->flags = flags;
+       fio->mode = mode;
 
-       if (rc != 0) {
-               int saved_errno = errno;
-               if (hostfd >= 0) {
-                       fsp->fh->fd = hostfd;
-                       SMB_VFS_NEXT_CLOSE(handle, fsp);
-               }
-               hostfd = -1;
-               errno = saved_errno;
-       }
-       return hostfd;
+       return fd;
 }
 
 static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
@@ -3471,56 +3496,42 @@ static int fruit_open_meta_netatalk(vfs_handle_struct *handle,
                                    int flags,
                                    mode_t mode)
 {
-       int rc;
-       int fakefd = -1;
+       struct fruit_config_data *config = NULL;
+       struct fio *fio = NULL;
        struct adouble *ad = NULL;
-       int fds[2];
+       bool meta_exists = false;
+       int fd;
 
        DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname));
 
-       /*
-        * Return a valid fd, but ensure any attempt to use it returns an error
-        * (EPIPE). All operations on the smb_fname or the fsp will use path
-        * based syscalls.
-        */
-       rc = pipe(fds);
-       if (rc != 0) {
-               goto exit;
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_META);
+       if (ad != NULL) {
+               meta_exists = true;
        }
-       fakefd = fds[0];
-       close(fds[1]);
-
-       if (flags & (O_CREAT | O_TRUNC)) {
-               /*
-                * The attribute does not exist or needs to be truncated,
-                * create an AppleDouble EA
-                */
-               ad = ad_init(fsp, handle, ADOUBLE_META);
-               if (ad == NULL) {
-                       rc = -1;
-                       goto exit;
-               }
 
-               rc = ad_set(ad, fsp->fsp_name);
-               if (rc != 0) {
-                       rc = -1;
-                       goto exit;
-               }
+       TALLOC_FREE(ad);
 
-               TALLOC_FREE(ad);
+       if (!meta_exists && !(flags & O_CREAT)) {
+               errno = ENOENT;
+               return -1;
        }
 
-exit:
-       DEBUG(10, ("fruit_open meta rc=%d, fd=%d\n", rc, fakefd));
-       if (rc != 0) {
-               int saved_errno = errno;
-               if (fakefd >= 0) {
-                       close(fakefd);
-               }
-               fakefd = -1;
-               errno = saved_errno;
+       fd = fruit_fake_fd();
+       if (fd == -1) {
+               return -1;
        }
-       return fakefd;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
+                               struct fruit_config_data, return -1);
+
+       fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
+       fio->type = ADOUBLE_META;
+       fio->config = config;
+       fio->fake_fd = true;
+       fio->flags = flags;
+       fio->mode = mode;
+
+       return fd;
 }
 
 static int fruit_open_meta(vfs_handle_struct *handle,
@@ -3529,7 +3540,6 @@ static int fruit_open_meta(vfs_handle_struct *handle,
 {
        int fd;
        struct fruit_config_data *config = NULL;
-       struct fio *fio = NULL;
 
        DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname));
 
@@ -3554,14 +3564,6 @@ static int fruit_open_meta(vfs_handle_struct *handle,
 
        DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname), fd);
 
-       if (fd == -1) {
-               return -1;
-       }
-
-       fio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct fio, NULL);
-       fio->type = ADOUBLE_META;
-       fio->config = config;
-
        return fd;
 }
 
@@ -3613,7 +3615,7 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
 
                fsp->fh->fd = hostfd;
 
-               rc = ad_fset(ad, fsp);
+               rc = ad_fset(handle, ad, fsp);
                fsp->fh->fd = -1;
                if (rc != 0) {
                        rc = -1;
@@ -3740,6 +3742,87 @@ static int fruit_open(vfs_handle_struct *handle,
        return fd;
 }
 
+static int fruit_close_meta(vfs_handle_struct *handle,
+                           files_struct *fsp)
+{
+       int ret;
+       struct fruit_config_data *config = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
+                               struct fruit_config_data, return -1);
+
+       switch (config->meta) {
+       case FRUIT_META_STREAM:
+               ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
+               break;
+
+       case FRUIT_META_NETATALK:
+               ret = close(fsp->fh->fd);
+               fsp->fh->fd = -1;
+               break;
+
+       default:
+               DBG_ERR("Unexpected meta config [%d]\n", config->meta);
+               return -1;
+       }
+
+       return ret;
+}
+
+
+static int fruit_close_rsrc(vfs_handle_struct *handle,
+                           files_struct *fsp)
+{
+       int ret;
+       struct fruit_config_data *config = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
+                               struct fruit_config_data, return -1);
+
+       switch (config->rsrc) {
+       case FRUIT_RSRC_STREAM:
+       case FRUIT_RSRC_ADFILE:
+               ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
+               break;
+
+       case FRUIT_RSRC_XATTR:
+               ret = close(fsp->fh->fd);
+               fsp->fh->fd = -1;
+               break;
+
+       default:
+               DBG_ERR("Unexpected rsrc config [%d]\n", config->rsrc);
+               return -1;
+       }
+
+       return ret;
+}
+
+static int fruit_close(vfs_handle_struct *handle,
+                       files_struct *fsp)
+{
+       int ret;
+       int fd;
+
+       fd = fsp->fh->fd;
+
+       DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp->fsp_name), fd);
+
+       if (!is_ntfs_stream_smb_fname(fsp->fsp_name)) {
+               return SMB_VFS_NEXT_CLOSE(handle, fsp);
+       }
+
+       if (is_afpinfo_stream(fsp->fsp_name)) {
+               ret = fruit_close_meta(handle, fsp);
+       } else if (is_afpresource_stream(fsp->fsp_name)) {
+               ret = fruit_close_rsrc(handle, fsp);
+       } else {
+               ret = SMB_VFS_NEXT_CLOSE(handle, fsp);
+       }
+
+       return ret;
+}
+
 static int fruit_rename(struct vfs_handle_struct *handle,
                        const struct smb_filename *smb_fname_src,
                        const struct smb_filename *smb_fname_dst)
@@ -4555,6 +4638,12 @@ static ssize_t fruit_pwrite_meta_stream(vfs_handle_struct *handle,
        }
 
        if (ai_empty_finderinfo(ai)) {
+               /*
+                * Writing an all 0 blob to the metadata stream results in the
+                * stream being removed on a macOS server. This ensures we
+                * behave the same and it verified by the "delete AFP_AfpInfo by
+                * writing all 0" test.
+                */
                ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, 0);
                if (ret != 0) {
                        DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
@@ -4614,7 +4703,7 @@ static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
 
        memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
 
-       ret = ad_fset(ad, fsp);
+       ret = ad_fset(handle, ad, fsp);
        if (ret != 0) {
                DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
                TALLOC_FREE(ad);
@@ -4627,6 +4716,12 @@ static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
                return n;
        }
 
+       /*
+        * Writing an all 0 blob to the metadata stream results in the stream
+        * being removed on a macOS server. This ensures we behave the same and
+        * it verified by the "delete AFP_AfpInfo by writing all 0" test.
+        */
+
        ok = set_delete_on_close(
                fsp,
                true,
@@ -4647,34 +4742,67 @@ static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
 {
        struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
        ssize_t nwritten;
+       uint8_t buf[AFP_INFO_SIZE];
+       size_t to_write;
+       size_t to_copy;
+       int cmp;
 
-       /*
-        * Writing an all 0 blob to the metadata stream
-        * results in the stream being removed on a macOS
-        * server. This ensures we behave the same and it
-        * verified by the "delete AFP_AfpInfo by writing all
-        * 0" test.
-        */
-       if (n != AFP_INFO_SIZE || offset != 0) {
-               DBG_ERR("unexpected offset=%jd or size=%jd\n",
-                       (intmax_t)offset, (intmax_t)n);
+       if (fio == NULL) {
+               DBG_ERR("Failed to fetch fsp extension");
                return -1;
        }
 
-       if (fio == NULL) {
-               DBG_ERR("Failed to fetch fsp extension");
+       if (n < 3) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (offset != 0 && n < 60) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       cmp = memcmp(data, "AFP", 3);
+       if (cmp != 0) {
+               errno = EINVAL;
                return -1;
        }
 
+       if (n <= AFP_OFF_FinderInfo) {
+               /*
+                * Nothing to do here really, just return
+                */
+               return n;
+       }
+
+       offset = 0;
+
+       to_copy = n;
+       if (to_copy > AFP_INFO_SIZE) {
+               to_copy = AFP_INFO_SIZE;
+       }
+       memcpy(buf, data, to_copy);
+
+       to_write = n;
+       if (to_write != AFP_INFO_SIZE) {
+               to_write = AFP_INFO_SIZE;
+       }
+
        switch (fio->config->meta) {
        case FRUIT_META_STREAM:
-               nwritten = fruit_pwrite_meta_stream(handle, fsp, data,
-                                                   n, offset);
+               nwritten = fruit_pwrite_meta_stream(handle,
+                                                   fsp,
+                                                   buf,
+                                                   to_write,
+                                                   offset);
                break;
 
        case FRUIT_META_NETATALK:
-               nwritten = fruit_pwrite_meta_netatalk(handle, fsp, data,
-                                                     n, offset);
+               nwritten = fruit_pwrite_meta_netatalk(handle,
+                                                     fsp,
+                                                     buf,
+                                                     to_write,
+                                                     offset);
                break;
 
        default:
@@ -4682,7 +4810,14 @@ static ssize_t fruit_pwrite_meta(vfs_handle_struct *handle,
                return -1;
        }
 
-       return nwritten;
+       if (nwritten != to_write) {
+               return -1;
+       }
+
+       /*
+        * Return the requested amount, verified against macOS SMB server
+        */
+       return n;
 }
 
 static ssize_t fruit_pwrite_rsrc_stream(vfs_handle_struct *handle,
@@ -4724,7 +4859,7 @@ static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
 
        if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
                ad_setentrylen(ad, ADEID_RFORK, n + offset);
-               ret = ad_fset(ad, fsp);
+               ret = ad_fset(handle, ad, fsp);
                if (ret != 0) {
                        DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
                        TALLOC_FREE(ad);
@@ -5173,6 +5308,7 @@ static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
                                   SMB_STRUCT_STAT *sbuf)
 {
        struct fio *fio = (struct fio *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+       struct smb_filename smb_fname;
        ino_t ino;
        int ret;
 
@@ -5192,11 +5328,15 @@ static int fruit_fstat_meta_stream(vfs_handle_struct *handle,
                return 0;
        }
 
-       ret = fruit_stat_base(handle, fsp->base_fsp->fsp_name, false);
+       smb_fname = (struct smb_filename) {
+               .base_name = fsp->fsp_name->base_name,
+       };
+
+       ret = fruit_stat_base(handle, &smb_fname, false);
        if (ret != 0) {
                return -1;
        }
-       *sbuf = fsp->base_fsp->fsp_name->st;
+       *sbuf = smb_fname.st;
 
        ino = fruit_inode(sbuf, fsp->fsp_name->stream_name);
 
@@ -5402,16 +5542,7 @@ static NTSTATUS fruit_streaminfo_meta_stream(
 {
        struct stream_struct *stream = *pstreams;
        unsigned int num_streams = *pnum_streams;
-       struct smb_filename *sname = NULL;
-       char *full_name = NULL;
-       uint32_t name_hash;
-       struct share_mode_lock *lck = NULL;
-       struct file_id id = {0};
-       bool delete_on_close_set;
        int i;
-       int ret;
-       NTSTATUS status;
-       bool ok;
 
        for (i = 0; i < num_streams; i++) {
                if (strequal_m(stream[i].name, AFPINFO_STREAM)) {
@@ -5435,70 +5566,8 @@ static NTSTATUS fruit_streaminfo_meta_stream(
                                                  stream[i].size);
        }
 
-       /*
-        * Now check if there's a delete-on-close pending on the stream. If so,
-        * hide the stream. This behaviour was verified against a macOS 10.12
-        * SMB server.
-        */
-
-       sname = synthetic_smb_fname(talloc_tos(),
-                                   smb_fname->base_name,
-                                   AFPINFO_STREAM_NAME,
-                                   NULL, 0);
-       if (sname == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto out;
-       }
-
-       ret = fruit_stat_base(handle, sname, false);
-       if (ret != 0) {
-               status = map_nt_error_from_unix(errno);
-               goto out;
-       }
-
-       sname->st.st_ex_ino = fruit_inode(&sname->st, AFPINFO_STREAM);
-
-       id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sname->st);
-
-       lck = get_existing_share_mode_lock(talloc_tos(), id);
-       if (lck == NULL) {
-               status = NT_STATUS_OK;
-               goto out;
-       }
-
-       full_name = talloc_asprintf(talloc_tos(),
-                                   "%s%s",
-                                   sname->base_name,
-                                   AFPINFO_STREAM);
-       if (full_name == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto out;
-       }
-
-       status = file_name_hash(handle->conn, full_name, &name_hash);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
-       delete_on_close_set = is_delete_on_close_set(lck, name_hash);
-       if (delete_on_close_set) {
-               ok = del_fruit_stream(mem_ctx,
-                                     pnum_streams,
-                                     pstreams,
-                                     AFPINFO_STREAM);
-               if (!ok) {
-                       status = NT_STATUS_INTERNAL_ERROR;
-                       goto out;
-               }
-       }
 
-       status  = NT_STATUS_OK;
-
-out:
-       TALLOC_FREE(sname);
-       TALLOC_FREE(lck);
-       TALLOC_FREE(full_name);
-       return status;
+       return NT_STATUS_OK;
 }
 
 static NTSTATUS fruit_streaminfo_meta_netatalk(
@@ -5892,7 +5961,7 @@ static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
 
        ad_setentrylen(ad, ADEID_RFORK, offset);
 
-       rc = ad_fset(ad, fsp);
+       rc = ad_fset(handle, ad, fsp);
        if (rc != 0) {
                DBG_ERR("ad_fset [%s] failed [%s]\n",
                        fsp_str_dbg(fsp), strerror(errno));
@@ -6011,6 +6080,8 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
        struct fruit_config_data *config = NULL;
        files_struct *fsp = NULL;
        struct fio *fio = NULL;
+       bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
+       int ret;
 
        status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
        if (!NT_STATUS_IS_OK(status)) {
@@ -6020,6 +6091,14 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
        SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
                                return NT_STATUS_UNSUCCESSFUL);
 
+       if (is_apple_stream(smb_fname) && !internal_open) {
+               ret = ad_convert(handle, smb_fname);
+               if (ret != 0) {
+                       DBG_ERR("ad_convert() failed\n");
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+       }
+
        status = SMB_VFS_NEXT_CREATE_FILE(
                handle, req, root_dir_fid, smb_fname,
                access_mask, share_access,
@@ -6075,7 +6154,7 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
                status = fruit_check_access(
                        handle, *result,
                        access_mask,
-                       map_share_mode_to_deny_mode(share_access, 0));
+                       share_access);
                if (!NT_STATUS_IS_OK(status)) {
                        goto fail;
                }
@@ -6102,6 +6181,7 @@ static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
        struct fruit_config_data *config = NULL;
        struct readdir_attr_data *attr_data;
        NTSTATUS status;
+       int ret;
 
        SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data,
@@ -6113,6 +6193,12 @@ static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
 
        DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
 
+       ret = ad_convert(handle, fname);
+       if (ret != 0) {
+               DBG_ERR("ad_convert() failed\n");
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
        *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
        if (*pattr_data == NULL) {
                return NT_STATUS_UNSUCCESSFUL;
@@ -7064,6 +7150,7 @@ static struct vfs_fn_pointers vfs_fruit_fns = {
        .rename_fn = fruit_rename,
        .rmdir_fn = fruit_rmdir,
        .open_fn = fruit_open,
+       .close_fn = fruit_close,
        .pread_fn = fruit_pread,
        .pwrite_fn = fruit_pwrite,
        .pread_send_fn = fruit_pread_send,