smbd: Fix a typo in a few places
[samba.git] / source3 / modules / vfs_recycle.c
index 80332523ed922b4f999c127944ed41f777130b71..327a7eea06e31b8d076f5e4fad72d5148030be15 100644 (file)
@@ -27,6 +27,7 @@
 #include "system/filesys.h"
 #include "../librpc/gen_ndr/ndr_netlogon.h"
 #include "auth.h"
+#include "source3/lib/substitute.h"
 
 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
 
@@ -34,162 +35,118 @@ static int vfs_recycle_debug_level = DBGC_VFS;
 
 #undef DBGC_CLASS
 #define DBGC_CLASS vfs_recycle_debug_level
-static int recycle_unlink(vfs_handle_struct *handle,
-                         const struct smb_filename *smb_fname);
 
-static const char *recycle_repository(vfs_handle_struct *handle)
-{
-       const char *tmp_str = NULL;
-
-       tmp_str = lp_parm_const_string(SNUM(handle->conn), "recycle", "repository",".recycle");
-
-       DEBUG(10, ("recycle: repository = %s\n", tmp_str));
-
-       return tmp_str;
-}
-
-static bool recycle_keep_dir_tree(vfs_handle_struct *handle)
-{
-       bool ret;
-
-       ret = lp_parm_bool(SNUM(handle->conn), "recycle", "keeptree", False);
-
-       DEBUG(10, ("recycle_bin: keeptree = %s\n", ret?"True":"False"));
-
-       return ret;
-}
-
-static bool recycle_versions(vfs_handle_struct *handle)
-{
-       bool ret;
-
-       ret = lp_parm_bool(SNUM(handle->conn), "recycle", "versions", False);
-
-       DEBUG(10, ("recycle: versions = %s\n", ret?"True":"False"));
-
-       return ret;
-}
-
-static bool recycle_touch(vfs_handle_struct *handle)
-{
-       bool ret;
-
-       ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch", False);
-
-       DEBUG(10, ("recycle: touch = %s\n", ret?"True":"False"));
-
-       return ret;
-}
-
-static bool recycle_touch_mtime(vfs_handle_struct *handle)
-{
-       bool ret;
-
-       ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch_mtime", False);
-
-       DEBUG(10, ("recycle: touch_mtime = %s\n", ret?"True":"False"));
-
-       return ret;
-}
-
-static const char **recycle_exclude(vfs_handle_struct *handle)
-{
-       const char **tmp_lp;
-
-       tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude", NULL);
-
-       DEBUG(10, ("recycle: exclude = %s ...\n", tmp_lp?*tmp_lp:""));
-
-       return tmp_lp;
-}
-
-static const char **recycle_exclude_dir(vfs_handle_struct *handle)
-{
-       const char **tmp_lp;
-
-       tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude_dir", NULL);
-
-       DEBUG(10, ("recycle: exclude_dir = %s ...\n", tmp_lp?*tmp_lp:""));
-
-       return tmp_lp;
-}
-
-static const char **recycle_noversions(vfs_handle_struct *handle)
-{
-       const char **tmp_lp;
-
-       tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "noversions", NULL);
-
-       DEBUG(10, ("recycle: noversions = %s\n", tmp_lp?*tmp_lp:""));
-
-       return tmp_lp;
-}
-
-static SMB_OFF_T recycle_maxsize(vfs_handle_struct *handle)
-{
-       SMB_OFF_T maxsize;
-
-       maxsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
-                                           "recycle", "maxsize", NULL));
-
-       DEBUG(10, ("recycle: maxsize = %lu\n", (long unsigned int)maxsize));
-
-       return maxsize;
-}
-
-static SMB_OFF_T recycle_minsize(vfs_handle_struct *handle)
-{
-       SMB_OFF_T minsize;
-
-       minsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
-                                           "recycle", "minsize", NULL));
-
-       DEBUG(10, ("recycle: minsize = %lu\n", (long unsigned int)minsize));
-
-       return minsize;
-}
+struct recycle_config_data {
+       const char *repository;
+       bool keeptree;
+       bool versions;
+       bool touch;
+       bool touch_mtime;
+       const char **exclude;
+       const char **exclude_dir;
+       const char **noversions;
+       mode_t directory_mode;
+       mode_t subdir_mode;
+       off_t minsize;
+       off_t maxsize;
+};
 
-static mode_t recycle_directory_mode(vfs_handle_struct *handle)
+static int vfs_recycle_connect(struct vfs_handle_struct *handle,
+                              const char *service,
+                              const char *user)
 {
-       int dirmode;
+       struct recycle_config_data *config = NULL;
+       int ret;
+       int t;
        const char *buff;
 
-       buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "directory_mode", NULL);
+       ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
+       if (ret < 0) {
+               return ret;
+       }
 
+       if (IS_IPC(handle->conn) || IS_PRINT(handle->conn)) {
+               return 0;
+       }
+
+       config = talloc_zero(handle->conn, struct recycle_config_data);
+       if (config == NULL) {
+               DBG_ERR("talloc_zero() failed\n");
+               errno = ENOMEM;
+               return -1;
+       }
+       config->repository = lp_parm_const_string(SNUM(handle->conn),
+                                                 "recycle",
+                                                 "repository",
+                                                 ".recycle");
+       config->keeptree = lp_parm_bool(SNUM(handle->conn),
+                                       "recycle",
+                                       "keeptree",
+                                       False);
+       config->versions = lp_parm_bool(SNUM(handle->conn),
+                                       "recycle",
+                                       "versions",
+                                       False);
+       config->touch = lp_parm_bool(SNUM(handle->conn),
+                                    "recycle",
+                                    "touch",
+                                    False);
+       config->touch_mtime = lp_parm_bool(SNUM(handle->conn),
+                                          "recycle",
+                                          "touch_mtime",
+                                          False);
+       config->exclude = lp_parm_string_list(SNUM(handle->conn),
+                                             "recycle",
+                                             "exclude",
+                                             NULL);
+       config->exclude_dir = lp_parm_string_list(SNUM(handle->conn),
+                                                 "recycle",
+                                                 "exclude_dir",
+                                                 NULL);
+       config->noversions = lp_parm_string_list(SNUM(handle->conn),
+                                                "recycle",
+                                                "noversions",
+                                                NULL);
+       config->minsize = conv_str_size(lp_parm_const_string(
+               SNUM(handle->conn), "recycle", "minsize", NULL));
+       config->maxsize = conv_str_size(lp_parm_const_string(
+               SNUM(handle->conn), "recycle", "maxsize", NULL));
+
+       buff = lp_parm_const_string(SNUM(handle->conn),
+                                   "recycle",
+                                   "directory_mode",
+                                   NULL);
        if (buff != NULL ) {
-               sscanf(buff, "%o", &dirmode);
+               sscanf(buff, "%o", &t);
        } else {
-               dirmode=S_IRUSR | S_IWUSR | S_IXUSR;
+               t = S_IRUSR | S_IWUSR | S_IXUSR;
        }
+       config->directory_mode = (mode_t)t;
 
-       DEBUG(10, ("recycle: directory_mode = %o\n", dirmode));
-       return (mode_t)dirmode;
-}
-
-static mode_t recycle_subdir_mode(vfs_handle_struct *handle)
-{
-       int dirmode;
-       const char *buff;
-
-       buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "subdir_mode", NULL);
-
+       buff = lp_parm_const_string(SNUM(handle->conn),
+                                   "recycle",
+                                   "subdir_mode",
+                                   NULL);
        if (buff != NULL ) {
-               sscanf(buff, "%o", &dirmode);
+               sscanf(buff, "%o", &t);
        } else {
-               dirmode=recycle_directory_mode(handle);
+               t = config->directory_mode;
        }
+       config->subdir_mode = (mode_t)t;
 
-       DEBUG(10, ("recycle: subdir_mode = %o\n", dirmode));
-       return (mode_t)dirmode;
+       SMB_VFS_HANDLE_SET_DATA(
+               handle, config, NULL, struct recycle_config_data, return -1);
+       return 0;
 }
 
 static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
 {
-       SMB_STRUCT_STAT st;
+       struct smb_filename smb_fname = {
+                       .base_name = discard_const_p(char, dname)
+       };
 
-       if (vfs_stat_smb_fname(handle->conn, dname, &st) == 0) {
-               if (S_ISDIR(st.st_ex_mode)) {
+       if (SMB_VFS_STAT(handle->conn, &smb_fname) == 0) {
+               if (S_ISDIR(smb_fname.st.st_ex_mode)) {
                        return True;
                }
        }
@@ -201,11 +158,10 @@ static bool recycle_file_exist(vfs_handle_struct *handle,
                               const struct smb_filename *smb_fname)
 {
        struct smb_filename *smb_fname_tmp = NULL;
-       NTSTATUS status;
        bool ret = false;
 
-       status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
-       if (!NT_STATUS_IS_OK(status)) {
+       smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
+       if (smb_fname_tmp == NULL) {
                return false;
        }
 
@@ -225,23 +181,22 @@ static bool recycle_file_exist(vfs_handle_struct *handle,
  * @param fname file name
  * @return size in bytes
  **/
-static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle,
+static off_t recycle_get_file_size(vfs_handle_struct *handle,
                                       const struct smb_filename *smb_fname)
 {
        struct smb_filename *smb_fname_tmp = NULL;
-       NTSTATUS status;
-       SMB_OFF_T size;
+       off_t size;
 
-       status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
-       if (!NT_STATUS_IS_OK(status)) {
-               size = (SMB_OFF_T)0;
+       smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
+       if (smb_fname_tmp == NULL) {
+               size = (off_t)0;
                goto out;
        }
 
        if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
-               DEBUG(0,("recycle: stat for %s returned %s\n",
-                        smb_fname_str_dbg(smb_fname_tmp), strerror(errno)));
-               size = (SMB_OFF_T)0;
+               DBG_DEBUG("stat for %s returned %s\n",
+                        smb_fname_str_dbg(smb_fname_tmp), strerror(errno));
+               size = (off_t)0;
                goto out;
        }
 
@@ -255,12 +210,17 @@ static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle,
  * Create directory tree
  * @param conn connection
  * @param dname Directory tree to be created
+ * @param directory mode
+ * @param subdirectory mode
  * @return Returns True for success
  **/
-static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname)
+static bool recycle_create_dir(vfs_handle_struct *handle,
+                              const char *dname,
+                              mode_t dir_mode,
+                              mode_t subdir_mode)
 {
        size_t len;
-       mode_t mode;
+       mode_t mode = dir_mode;
        char *new_dir = NULL;
        char *tmp_str = NULL;
        char *token;
@@ -268,8 +228,6 @@ static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname)
        bool ret = False;
        char *saveptr;
 
-       mode = recycle_directory_mode(handle);
-
        tmp_str = SMB_STRDUP(dname);
        ALLOC_CHECK(tmp_str, done);
        tok_str = tmp_str;
@@ -285,7 +243,7 @@ static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname)
                }
        }
 
-       /* Create directory tree if neccessary */
+       /* Create directory tree if necessary */
        for(token = strtok_r(tok_str, "/", &saveptr); token;
            token = strtok_r(NULL, "/", &saveptr)) {
                if (strlcat(new_dir, token, len+1) >= len+1) {
@@ -294,17 +252,40 @@ static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname)
                if (recycle_directory_exist(handle, new_dir))
                        DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
                else {
+                       struct smb_filename *smb_fname = NULL;
+                       int retval;
+
                        DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
-                       if (SMB_VFS_NEXT_MKDIR(handle, new_dir, mode) != 0) {
-                               DEBUG(1,("recycle: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
+
+                       smb_fname = synthetic_smb_fname(talloc_tos(),
+                                               new_dir,
+                                               NULL,
+                                               NULL,
+                                               0,
+                                               0);
+                       if (smb_fname == NULL) {
+                               goto done;
+                       }
+
+                       retval = SMB_VFS_NEXT_MKDIRAT(handle,
+                                       handle->conn->cwd_fsp,
+                                       smb_fname,
+                                       mode);
+                       if (retval != 0) {
+                               DBG_WARNING("recycle: mkdirat failed "
+                                       "for %s with error: %s\n",
+                                       new_dir,
+                                       strerror(errno));
+                               TALLOC_FREE(smb_fname);
                                ret = False;
                                goto done;
                        }
+                       TALLOC_FREE(smb_fname);
                }
                if (strlcat(new_dir, "/", len+1) >= len+1) {
                        goto done;
                }
-               mode = recycle_subdir_mode(handle);
+               mode = subdir_mode;
        }
 
        ret = True;
@@ -396,45 +377,54 @@ static void recycle_do_touch(vfs_handle_struct *handle,
 {
        struct smb_filename *smb_fname_tmp = NULL;
        struct smb_file_time ft;
-       NTSTATUS status;
        int ret, err;
+       NTSTATUS status;
 
-       ZERO_STRUCT(ft);
+       init_smb_file_time(&ft);
 
-       status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
+       status = synthetic_pathref(talloc_tos(),
+                                  handle->conn->cwd_fsp,
+                                  smb_fname->base_name,
+                                  smb_fname->stream_name,
+                                  NULL,
+                                  smb_fname->twrp,
+                                  smb_fname->flags,
+                                  &smb_fname_tmp);
        if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("synthetic_pathref for '%s' failed: %s\n",
+                         smb_fname_str_dbg(smb_fname), nt_errstr(status));
                return;
        }
 
-       if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
-               DEBUG(0,("recycle: stat for %s returned %s\n",
-                        smb_fname_str_dbg(smb_fname_tmp), strerror(errno)));
-               goto out;
-       }
        /* atime */
        ft.atime = timespec_current();
        /* mtime */
        ft.mtime = touch_mtime ? ft.atime : smb_fname_tmp->st.st_ex_mtime;
 
        become_root();
-       ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, &ft);
+       ret = SMB_VFS_NEXT_FNTIMES(handle, smb_fname_tmp->fsp, &ft);
        err = errno;
        unbecome_root();
        if (ret == -1 ) {
                DEBUG(0, ("recycle: touching %s failed, reason = %s\n",
                          smb_fname_str_dbg(smb_fname_tmp), strerror(err)));
        }
- out:
+
        TALLOC_FREE(smb_fname_tmp);
 }
 
 /**
  * Check if file should be recycled
  **/
-static int recycle_unlink(vfs_handle_struct *handle,
-    const struct smb_filename *smb_fname)
+static int recycle_unlink_internal(vfs_handle_struct *handle,
+                               struct files_struct *dirfsp,
+                               const struct smb_filename *smb_fname,
+                               int flags)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        connection_struct *conn = handle->conn;
+       struct smb_filename *full_fname = NULL;
        char *path_name = NULL;
                char *temp_name = NULL;
        char *final_name = NULL;
@@ -442,19 +432,25 @@ static int recycle_unlink(vfs_handle_struct *handle,
        const char *base;
        char *repository = NULL;
        int i = 1;
-       SMB_OFF_T maxsize, minsize;
-       SMB_OFF_T file_size; /* space_avail;    */
+       off_t file_size; /* space_avail;        */
        bool exist;
-       NTSTATUS status;
        int rc = -1;
-
-       repository = talloc_sub_advanced(NULL, lp_servicename(SNUM(conn)),
-                                       conn->session_info->unix_info->unix_name,
-                                       conn->connectpath,
-                                       conn->session_info->unix_token->gid,
-                                       conn->session_info->unix_info->sanitized_username,
-                                       conn->session_info->info->domain_name,
-                                       recycle_repository(handle));
+       struct recycle_config_data *config;
+
+       SMB_VFS_HANDLE_GET_DATA(handle,
+                               config,
+                               struct recycle_config_data,
+                               return true);
+
+       repository = talloc_sub_full(
+               NULL,
+               lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
+               conn->session_info->unix_info->unix_name,
+               conn->connectpath,
+               conn->session_info->unix_token->gid,
+               conn->session_info->unix_info->sanitized_username,
+               conn->session_info->info->domain_name,
+               config->repository);
        ALLOC_CHECK(repository, done);
        /* shouldn't we allow absolute path names here? --metze */
        /* Yes :-). JRA. */
@@ -463,25 +459,41 @@ static int recycle_unlink(vfs_handle_struct *handle,
        if(!repository || *(repository) == '\0') {
                DEBUG(3, ("recycle: repository path not set, purging %s...\n",
                          smb_fname_str_dbg(smb_fname)));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
+       full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+                                                 dirfsp,
+                                                 smb_fname);
+       if (full_fname == NULL) {
+               return -1;
+       }
+
        /* we don't recycle the recycle bin... */
-       if (strncmp(smb_fname->base_name, repository,
+       if (strncmp(full_fname->base_name, repository,
                    strlen(repository)) == 0) {
                DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n"));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
-       file_size = recycle_get_file_size(handle, smb_fname);
+       file_size = recycle_get_file_size(handle, full_fname);
        /* it is wrong to purge filenames only because they are empty imho
         *   --- simo
         *
        if(fsize == 0) {
                DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
-               rc = SMB_VFS_NEXT_UNLINK(handle,file_name);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       file_name,
+                                       flags);
                goto done;
        }
         */
@@ -490,18 +502,24 @@ static int recycle_unlink(vfs_handle_struct *handle,
         * not greater then maxsize, not the size of the single file, also it is better
         * to remove older files
         */
-       maxsize = recycle_maxsize(handle);
-       if(maxsize > 0 && file_size > maxsize) {
-               DEBUG(3, ("recycle: File %s exceeds maximum recycle size, "
-                         "purging... \n", smb_fname_str_dbg(smb_fname)));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+       if (config->maxsize > 0 && file_size > config->maxsize) {
+               DBG_NOTICE("File %s exceeds maximum recycle size, "
+                          "purging... \n",
+                          smb_fname_str_dbg(full_fname));
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
-       minsize = recycle_minsize(handle);
-       if(minsize > 0 && file_size < minsize) {
-               DEBUG(3, ("recycle: File %s lowers minimum recycle size, "
-                         "purging... \n", smb_fname_str_dbg(smb_fname)));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+       if (config->minsize > 0 && file_size < config->minsize) {
+               DBG_NOTICE("File %s lowers minimum recycle size, "
+                          "purging... \n",
+                          smb_fname_str_dbg(full_fname));
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
@@ -512,45 +530,47 @@ static int recycle_unlink(vfs_handle_struct *handle,
        DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
        if(space_avail < file_size) {
                DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name));
-               rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       file_name,
+                                       flags);
                goto done;
        }
         */
 
        /* extract filename and path */
-       base = strrchr(smb_fname->base_name, '/');
-       if (base == NULL) {
-               base = smb_fname->base_name;
-               path_name = SMB_STRDUP("/");
-               ALLOC_CHECK(path_name, done);
-       }
-       else {
-               path_name = SMB_STRDUP(smb_fname->base_name);
-               ALLOC_CHECK(path_name, done);
-               path_name[base - smb_fname->base_name] = '\0';
-               base++;
+       if (!parent_dirname(talloc_tos(), full_fname->base_name, &path_name, &base)) {
+               rc = -1;
+               errno = ENOMEM;
+               goto done;
        }
 
        /* original filename with path */
-       DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(smb_fname)));
+       DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(full_fname)));
        /* original path */
        DEBUG(10, ("recycle: fpath = %s\n", path_name));
        /* filename without path */
        DEBUG(10, ("recycle: base = %s\n", base));
 
-       if (matchparam(recycle_exclude(handle), base)) {
+       if (matchparam(config->exclude, base)) {
                DEBUG(3, ("recycle: file %s is excluded \n", base));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
-       if (matchdirparam(recycle_exclude_dir(handle), path_name)) {
+       if (matchdirparam(config->exclude_dir, path_name)) {
                DEBUG(3, ("recycle: directory %s is excluded \n", path_name));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
-       if (recycle_keep_dir_tree(handle) == True) {
+       if (config->keeptree) {
                if (asprintf(&temp_name, "%s/%s", repository, path_name) == -1) {
                        ALLOC_CHECK(temp_name, done);
                }
@@ -564,11 +584,18 @@ static int recycle_unlink(vfs_handle_struct *handle,
                DEBUG(10, ("recycle: Directory already exists\n"));
        } else {
                DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
-               if (recycle_create_dir(handle, temp_name) == False) {
+               if (recycle_create_dir(handle,
+                                      temp_name,
+                                      config->directory_mode,
+                                      config->subdir_mode) == False)
+               {
                        DEBUG(3, ("recycle: Could not create directory, "
                                  "purging %s...\n",
-                                 smb_fname_str_dbg(smb_fname)));
-                       rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+                                 smb_fname_str_dbg(full_fname)));
+                       rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                        goto done;
                }
        }
@@ -578,11 +605,17 @@ static int recycle_unlink(vfs_handle_struct *handle,
        }
 
        /* Create smb_fname with final base name and orig stream name. */
-       status = create_synthetic_smb_fname(talloc_tos(), final_name,
-                                           smb_fname->stream_name, NULL,
-                                           &smb_fname_final);
-       if (!NT_STATUS_IS_OK(status)) {
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+       smb_fname_final = synthetic_smb_fname(talloc_tos(),
+                                       final_name,
+                                       full_fname->stream_name,
+                                       NULL,
+                                       full_fname->twrp,
+                                       full_fname->flags);
+       if (smb_fname_final == NULL) {
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
                goto done;
        }
 
@@ -592,11 +625,14 @@ static int recycle_unlink(vfs_handle_struct *handle,
 
        /* check if we should delete file from recycle bin */
        if (recycle_file_exist(handle, smb_fname_final)) {
-               if (recycle_versions(handle) == False || matchparam(recycle_noversions(handle), base) == True) {
+               if (config->versions == False ||
+                   matchparam(config->noversions, base) == True) {
                        DEBUG(3, ("recycle: Removing old file %s from recycle "
                                  "bin\n", smb_fname_str_dbg(smb_fname_final)));
-                       if (SMB_VFS_NEXT_UNLINK(handle,
-                                               smb_fname_final) != 0) {
+                       if (SMB_VFS_NEXT_UNLINKAT(handle,
+                                               dirfsp->conn->cwd_fsp,
+                                               smb_fname_final,
+                                               flags) != 0) {
                                DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
                        }
                }
@@ -613,43 +649,75 @@ static int recycle_unlink(vfs_handle_struct *handle,
                smb_fname_final->base_name = talloc_strdup(smb_fname_final,
                                                           final_name);
                if (smb_fname_final->base_name == NULL) {
-                       rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+                       rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                                               dirfsp,
+                                               smb_fname,
+                                               flags);
                        goto done;
                }
        }
 
-       DEBUG(10, ("recycle: Moving %s to %s\n", smb_fname_str_dbg(smb_fname),
+       DEBUG(10, ("recycle: Moving %s to %s\n", smb_fname_str_dbg(full_fname),
                smb_fname_str_dbg(smb_fname_final)));
-       rc = SMB_VFS_NEXT_RENAME(handle, smb_fname, smb_fname_final);
+       rc = SMB_VFS_NEXT_RENAMEAT(handle,
+                       dirfsp,
+                       smb_fname,
+                       handle->conn->cwd_fsp,
+                       smb_fname_final);
        if (rc != 0) {
                DEBUG(3, ("recycle: Move error %d (%s), purging file %s "
                          "(%s)\n", errno, strerror(errno),
-                         smb_fname_str_dbg(smb_fname),
+                         smb_fname_str_dbg(full_fname),
                          smb_fname_str_dbg(smb_fname_final)));
-               rc = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+               rc = SMB_VFS_NEXT_UNLINKAT(handle,
+                               dirfsp,
+                               smb_fname,
+                               flags);
                goto done;
        }
 
        /* touch access date of moved file */
-       if (recycle_touch(handle) == True || recycle_touch_mtime(handle))
-               recycle_do_touch(handle, smb_fname_final,
-                                recycle_touch_mtime(handle));
+       if (config->touch || config->touch_mtime)
+               recycle_do_touch(handle, smb_fname_final, config->touch_mtime);
 
 done:
-       SAFE_FREE(path_name);
+       TALLOC_FREE(path_name);
        SAFE_FREE(temp_name);
        SAFE_FREE(final_name);
+       TALLOC_FREE(full_fname);
        TALLOC_FREE(smb_fname_final);
        TALLOC_FREE(repository);
        return rc;
 }
 
+static int recycle_unlinkat(vfs_handle_struct *handle,
+               struct files_struct *dirfsp,
+               const struct smb_filename *smb_fname,
+               int flags)
+{
+       int ret;
+
+       if (flags & AT_REMOVEDIR) {
+               ret = SMB_VFS_NEXT_UNLINKAT(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
+       } else {
+               ret = recycle_unlink_internal(handle,
+                                       dirfsp,
+                                       smb_fname,
+                                       flags);
+       }
+       return ret;
+}
+
 static struct vfs_fn_pointers vfs_recycle_fns = {
-       .unlink_fn = recycle_unlink
+       .connect_fn = vfs_recycle_connect,
+       .unlinkat_fn = recycle_unlinkat,
 };
 
-NTSTATUS vfs_recycle_init(void);
-NTSTATUS vfs_recycle_init(void)
+static_decl_vfs;
+NTSTATUS vfs_recycle_init(TALLOC_CTX *ctx)
 {
        NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle",
                                        &vfs_recycle_fns);