vfs_fruit: pass handle to ad_convert_delete_adfile()
[metze/samba/wip.git] / source3 / modules / vfs_fruit.c
index 50b6fac8b95fa2666f9fcb940fd521dfad6b01ea..9689c6464e0b42644c9063099d64fe02fc2571f8 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,16 @@ struct fio {
  */
 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                               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 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(vfs_handle_struct *handle,
+                 struct adouble *ad,
+                 const struct smb_filename *smb_fname);
+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);
@@ -1049,7 +1059,8 @@ static bool ad_convert_move_reso(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_xattr(struct adouble *ad,
+static bool ad_convert_xattr(vfs_handle_struct *handle,
+                            struct adouble *ad,
                             const struct smb_filename *smb_fname,
                             bool *converted_xattr)
 {
@@ -1100,7 +1111,7 @@ static bool ad_convert_xattr(struct adouble *ad,
                files_struct *fsp = NULL;
                ssize_t nwritten;
 
-               status = string_replace_allocate(ad->ad_handle->conn,
+               status = string_replace_allocate(handle->conn,
                                                 e->adx_name,
                                                 string_replace_cmaps,
                                                 talloc_tos(),
@@ -1137,7 +1148,7 @@ static bool ad_convert_xattr(struct adouble *ad,
                DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
 
                status = SMB_VFS_CREATE_FILE(
-                       ad->ad_handle->conn,            /* conn */
+                       handle->conn,                   /* conn */
                        NULL,                           /* req */
                        0,                              /* root_dir_fid */
                        stream_name,                    /* fname */
@@ -1216,7 +1227,8 @@ fail:
        return ok;
 }
 
-static bool ad_convert_finderinfo(struct adouble *ad,
+static bool ad_convert_finderinfo(vfs_handle_struct *handle,
+                                 struct adouble *ad,
                                  const struct smb_filename *smb_fname)
 {
        char *p_ad = NULL;
@@ -1273,7 +1285,7 @@ static bool ad_convert_finderinfo(struct adouble *ad,
        DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
 
        status = SMB_VFS_CREATE_FILE(
-               ad->ad_handle->conn,            /* conn */
+               handle->conn,                   /* conn */
                NULL,                           /* req */
                0,                              /* root_dir_fid */
                stream_name,                    /* fname */
@@ -1336,7 +1348,8 @@ static bool ad_convert_truncate(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_blank_rfork(struct adouble *ad,
+static bool ad_convert_blank_rfork(vfs_handle_struct *handle,
+                                  struct adouble *ad,
                                   bool *blank)
 {
        struct fruit_config_data *config = NULL;
@@ -1349,7 +1362,7 @@ static bool ad_convert_blank_rfork(struct adouble *ad,
 
        *blank = false;
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return false);
 
        if (!config->wipe_intentionally_left_blank_rfork) {
@@ -1400,7 +1413,8 @@ static bool ad_convert_blank_rfork(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_delete_adfile(struct adouble *ad,
+static bool ad_convert_delete_adfile(vfs_handle_struct *handle,
+                                    struct adouble *ad,
                                     const struct smb_filename *smb_fname)
 {
        struct fruit_config_data *config = NULL;
@@ -1411,7 +1425,7 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
                return true;
        }
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return false);
 
        if (!config->delete_empty_adfiles) {
@@ -1423,7 +1437,7 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
                return false;
        }
 
-       rc = SMB_VFS_NEXT_UNLINK(ad->ad_handle, ad_name);
+       rc = SMB_VFS_NEXT_UNLINK(handle, ad_name);
        if (rc != 0) {
                DBG_ERR("Unlinking [%s] failed: %s\n",
                        smb_fname_str_dbg(ad_name), strerror(errno));
@@ -1446,50 +1460,66 @@ 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);
+       ok = ad_convert_xattr(handle, ad, smb_fname, &converted_xattr);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       ok = ad_convert_blank_rfork(ad, &blank);
+       ok = ad_convert_blank_rfork(handle, 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;
                }
        }
 
-       ok = ad_convert_finderinfo(ad, smb_fname);
+       ok = ad_convert_finderinfo(handle, ad, smb_fname);
        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);
+       ok = ad_convert_delete_adfile(handle, ad, smb_fname);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       return 0;
+       ret = 0;
+done:
+       TALLOC_FREE(ad);
+       return ret;
 }
 
 /**
  * Read and parse Netatalk AppleDouble metadata xattr
  **/
-static ssize_t ad_read_meta(struct adouble *ad,
-                               const struct smb_filename *smb_fname)
+static ssize_t ad_read_meta(vfs_handle_struct *handle,
+                           struct adouble *ad,
+                           const struct smb_filename *smb_fname)
 {
        int      rc = 0;
        ssize_t  ealen;
@@ -1497,7 +1527,7 @@ static ssize_t ad_read_meta(struct adouble *ad,
 
        DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
 
-       ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
+       ealen = SMB_VFS_GETXATTR(handle->conn, smb_fname,
                                 AFPINFO_EA_NETATALK, ad->ad_data,
                                 AD_DATASZ_XATTR);
        if (ealen == -1) {
@@ -1554,7 +1584,9 @@ exit:
                ealen = -1;
                if (errno == EINVAL) {
                        become_root();
-                       removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
+                       (void)SMB_VFS_REMOVEXATTR(handle->conn,
+                                                 smb_fname,
+                                                 AFPINFO_EA_NETATALK);
                        unbecome_root();
                        errno = ENOENT;
                }
@@ -1656,7 +1688,8 @@ static int ad_open(vfs_handle_struct *handle,
        return 0;
 }
 
-static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
+static ssize_t ad_read_rsrc_xattr(vfs_handle_struct *handle,
+                                 struct adouble *ad)
 {
        int ret;
        SMB_STRUCT_STAT st;
@@ -1664,7 +1697,7 @@ static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
        /* FIXME: direct sys_fstat(), don't have an fsp */
        ret = sys_fstat(ad->ad_fd, &st,
                        lp_fake_directory_create_times(
-                               SNUM(ad->ad_handle->conn)));
+                               SNUM(handle->conn)));
        if (ret != 0) {
                return -1;
        }
@@ -1673,8 +1706,9 @@ static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
        return st.st_ex_size;
 }
 
-static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
-                               const struct smb_filename *smb_fname)
+static ssize_t ad_read_rsrc_adouble(vfs_handle_struct *handle,
+                                   struct adouble *ad,
+                                   const struct smb_filename *smb_fname)
 {
        SMB_STRUCT_STAT sbuf;
        char *p_ad = NULL;
@@ -1684,7 +1718,7 @@ static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
        bool ok;
 
        ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
-                               SNUM(ad->ad_handle->conn)));
+                               SNUM(handle->conn)));
        if (ret != 0) {
                return -1;
        }
@@ -1735,36 +1769,26 @@ 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;
 }
 
 /**
  * Read and parse resource fork, either ._ AppleDouble file or xattr
  **/
-static ssize_t ad_read_rsrc(struct adouble *ad,
-                       const struct smb_filename *smb_fname)
+static ssize_t ad_read_rsrc(vfs_handle_struct *handle,
+                           struct adouble *ad,
+                           const struct smb_filename *smb_fname)
 {
        struct fruit_config_data *config = NULL;
        ssize_t len;
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return -1);
 
        if (config->rsrc == FRUIT_RSRC_XATTR) {
-               len = ad_read_rsrc_xattr(ad);
+               len = ad_read_rsrc_xattr(handle, ad);
        } else {
-               len = ad_read_rsrc_adouble(ad, smb_fname);
+               len = ad_read_rsrc_adouble(handle, ad, smb_fname);
        }
 
        return len;
@@ -1773,13 +1797,15 @@ static ssize_t ad_read_rsrc(struct adouble *ad,
 /**
  * Read and unpack an AppleDouble metadata xattr or resource
  **/
-static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
+static ssize_t ad_read(vfs_handle_struct *handle,
+                      struct adouble *ad,
+                      const struct smb_filename *smb_fname)
 {
        switch (ad->ad_type) {
        case ADOUBLE_META:
-               return ad_read_meta(ad, smb_fname);
+               return ad_read_meta(handle, ad, smb_fname);
        case ADOUBLE_RSRC:
-               return ad_read_rsrc(ad, smb_fname);
+               return ad_read_rsrc(handle, ad, smb_fname);
        default:
                return -1;
        }
@@ -1958,7 +1984,7 @@ static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
 
        }
 
-       len = ad_read(ad, smb_fname);
+       len = ad_read(handle, ad, smb_fname);
        if (len == -1) {
                DEBUG(10, ("error reading AppleDouble for %s\n",
                        smb_fname->base_name));
@@ -2020,7 +2046,9 @@ static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
  *
  * @return            status code, 0 means success
  **/
-static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
+static int ad_set(vfs_handle_struct *handle,
+                 struct adouble *ad,
+                 const struct smb_filename *smb_fname)
 {
        bool ok;
        int ret;
@@ -2038,7 +2066,7 @@ static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
                return -1;
        }
 
-       ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
+       ret = SMB_VFS_SETXATTR(handle->conn,
                               smb_fname,
                               AFPINFO_EA_NETATALK,
                               ad->ad_data,
@@ -2057,7 +2085,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 +2109,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 +2117,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 +2163,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 +2175,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 +2411,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 +2424,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;
+       }
+
+       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;
+       }
 
-       TALLOC_FREE(upper_sname);
+       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 +2696,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 +2713,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,
@@ -3592,7 +3631,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;
@@ -3719,6 +3758,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)
@@ -4599,7 +4719,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);
@@ -4755,7 +4875,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);
@@ -5204,6 +5324,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;
 
@@ -5223,11 +5344,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);
 
@@ -5788,7 +5913,7 @@ static int fruit_ntimes(vfs_handle_struct *handle,
        ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
                   convert_time_t_to_uint32_t(ft->create_time.tv_sec));
 
-       rc = ad_set(ad, smb_fname);
+       rc = ad_set(handle, ad, smb_fname);
 
 exit:
 
@@ -5852,7 +5977,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));
@@ -5971,6 +6096,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)) {
@@ -5980,6 +6107,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,
@@ -6035,7 +6170,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;
                }
@@ -6062,6 +6197,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,
@@ -6073,6 +6209,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;
@@ -7024,6 +7166,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,