* Copyright (C) 2002, Alexander Bokovoy - cascaded VFS adoption,
* Copyright (C) 2002, Juergen Hasch - added some options.
* Copyright (C) 2002, Simo Sorce
+ * Copyright (C) 2002, Stefan (metze) Metzmacher
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
+#include "smbd/smbd.h"
+#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)
#undef DBGC_CLASS
#define DBGC_CLASS vfs_recycle_debug_level
-static const char *delimiter = "|"; /* delimiter for options */
-
-/* One per connection */
-
-typedef struct recycle_bin_struct
-{
- TALLOC_CTX *mem_ctx;
- char *repository; /* name of the recycle bin directory */
- BOOL keep_dir_tree; /* keep directory structure of deleted file in recycle bin */
- BOOL versions; /* create versions of deleted files with identical name */
- BOOL touch; /* touch access date of deleted file */
- char *exclude; /* which files to exclude */
- char *exclude_dir; /* which directories to exclude */
- char *noversions; /* which files to exclude from versioning */
- SMB_OFF_T maxsize; /* maximum file size to be saved */
-} recycle_bin_struct;
-
-typedef struct recycle_bin_connections {
- int conn;
- recycle_bin_struct *data;
- struct recycle_bin_connections *next;
-} recycle_bin_connections;
-
-typedef struct recycle_bin_private_data {
- TALLOC_CTX *mem_ctx;
- recycle_bin_connections *conns;
-} recycle_bin_private_data;
-
-struct smb_vfs_handle_struct *recycle_bin_private_handle;
-
-/* VFS operations */
-static struct vfs_ops default_vfs_ops; /* For passthrough operation */
-
-static int recycle_connect(struct connection_struct *conn, const char *service, const char *user);
-static void recycle_disconnect(struct connection_struct *conn);
-static int recycle_unlink(connection_struct *, const char *);
-
-#define VFS_OP(x) ((void *) x)
-
-static vfs_op_tuple recycle_ops[] = {
-
- /* Disk operations */
- {VFS_OP(recycle_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
- {VFS_OP(recycle_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_TRANSPARENT},
-
- /* File operations */
- {VFS_OP(recycle_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
-
- {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+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;
};
-/**
- * VFS initialisation function.
- *
- * @retval initialised vfs_op_tuple array
- **/
-static vfs_op_tuple *recycle_init(const struct vfs_ops *def_vfs_ops,
- struct smb_vfs_handle_struct *vfs_handle)
+static int vfs_recycle_connect(struct vfs_handle_struct *handle,
+ const char *service,
+ const char *user)
{
- TALLOC_CTX *mem_ctx = NULL;
-
- DEBUG(10, ("Initializing VFS module recycle\n"));
- memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
- vfs_recycle_debug_level = debug_add_class("vfs_recycle_bin");
- if (vfs_recycle_debug_level == -1) {
- vfs_recycle_debug_level = DBGC_VFS;
- DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
- } else {
- DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level));
- }
-
- recycle_bin_private_handle = vfs_handle;
- if (!(mem_ctx = talloc_init("recycle bin data"))) {
- DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
- return NULL;
- }
+ const struct loadparm_substitution *lp_sub =
+ loadparm_s3_global_substitution();
+ struct recycle_config_data *config = NULL;
+ int ret;
+ int t;
+ const char *buff = NULL;
+ const char **tmplist = NULL;
+ char *repository = NULL;
- recycle_bin_private_handle->data = talloc(mem_ctx, sizeof(recycle_bin_private_data));
- if (recycle_bin_private_handle->data == NULL) {
- DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
- return NULL;
+ ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
+ if (ret < 0) {
+ return ret;
}
- ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->mem_ctx = mem_ctx;
- ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->conns = NULL;
- return recycle_ops;
-}
-
-static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)
-{
- TALLOC_CTX *ctx = NULL;
- recycle_bin_struct *recbin;
- recycle_bin_connections *recconn;
- recycle_bin_connections *recconnbase;
- recycle_bin_private_data *recdata;
- const char *tmp_str;
-
- DEBUG(10, ("Called for service %s (%d) as user %s\n", service, SNUM(conn), user));
-
- if (recycle_bin_private_handle)
- recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
- else {
- DEBUG(0, ("Recycle bin not initialized!\n"));
- return -1;
+ if (IS_IPC(handle->conn) || IS_PRINT(handle->conn)) {
+ return 0;
}
- if (!(ctx = talloc_init("recycle bin connection"))) {
- DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
+ config = talloc_zero(handle->conn, struct recycle_config_data);
+ if (config == NULL) {
+ DBG_ERR("talloc_zero() failed\n");
+ errno = ENOMEM;
return -1;
}
-
- recbin = talloc_zero(ctx, sizeof(recycle_bin_struct));
- if (recbin == NULL) {
- DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
+ buff = lp_parm_const_string(SNUM(handle->conn),
+ "recycle",
+ "repository",
+ ".recycle");
+ repository = talloc_sub_full(
+ config,
+ lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn)),
+ handle->conn->session_info->unix_info->unix_name,
+ handle->conn->connectpath,
+ handle->conn->session_info->unix_token->gid,
+ handle->conn->session_info->unix_info->sanitized_username,
+ handle->conn->session_info->info->domain_name,
+ buff);
+ if (repository == NULL) {
+ DBG_ERR("talloc_sub_full() failed\n");
+ TALLOC_FREE(config);
+ errno = ENOMEM;
return -1;
}
- recbin->mem_ctx = ctx;
-
- /* parse configuration options */
- if ((tmp_str = lp_parm_const_string(SNUM(conn), "vfs_recycle_bin", "repository", ".recycle")) != NULL) {
- recbin->repository = talloc_sub_conn(recbin->mem_ctx, conn, tmp_str);
- ALLOC_CHECK(recbin->repository, error);
- trim_string(recbin->repository, "/", "/");
- DEBUG(5, ("recycle.bin: repository = %s\n", recbin->repository));
- } else {
- DEBUG(0,("recycle.bin: no repository found (fail) !\n"));
- goto error;
- }
-
- recbin->keep_dir_tree = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "keeptree", False);
- DEBUG(5, ("recycle.bin: keeptree = %d\n", recbin->keep_dir_tree));
-
- recbin->versions = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "versions", False);
- DEBUG(5, ("recycle.bin: versions = %d\n", recbin->versions));
-
- recbin->touch = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "touch", False);
- DEBUG(5, ("recycle.bin: touch = %d\n", recbin->touch));
-
- recbin->maxsize = lp_parm_ulong(SNUM(conn), "vfs_recycle_bin", "maxsize" , 0);
- if (recbin->maxsize == 0) {
- recbin->maxsize = -1;
- DEBUG(5, ("recycle.bin: maxsize = -infinite-\n"));
+ /* shouldn't we allow absolute path names here? --metze */
+ /* Yes :-). JRA. */
+ trim_char(repository, '\0', '/');
+ config->repository = repository;
+
+ 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);
+ tmplist = lp_parm_string_list(SNUM(handle->conn),
+ "recycle",
+ "exclude",
+ NULL);
+ if (tmplist != NULL) {
+ char **tmpcpy = str_list_copy(config, tmplist);
+ if (tmpcpy == NULL) {
+ DBG_ERR("str_list_copy() failed\n");
+ TALLOC_FREE(config);
+ errno = ENOMEM;
+ return -1;
+ }
+ config->exclude = discard_const_p(const char *, tmpcpy);
+ }
+ tmplist = lp_parm_string_list(SNUM(handle->conn),
+ "recycle",
+ "exclude_dir",
+ NULL);
+ if (tmplist != NULL) {
+ char **tmpcpy = str_list_copy(config, tmplist);
+ if (tmpcpy == NULL) {
+ DBG_ERR("str_list_copy() failed\n");
+ TALLOC_FREE(config);
+ errno = ENOMEM;
+ return -1;
+ }
+ config->exclude_dir = discard_const_p(const char *, tmpcpy);
+ }
+ tmplist = lp_parm_string_list(SNUM(handle->conn),
+ "recycle",
+ "noversions",
+ NULL);
+ if (tmplist != NULL) {
+ char **tmpcpy = str_list_copy(config, tmplist);
+ if (tmpcpy == NULL) {
+ DBG_ERR("str_list_copy() failed\n");
+ TALLOC_FREE(config);
+ errno = ENOMEM;
+ return -1;
+ }
+ config->noversions = discard_const_p(const char *, tmpcpy);
+ }
+ 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", &t);
} else {
- DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin->maxsize));
- }
-
- if ((tmp_str = lp_parm_const_string(SNUM(conn), "vfs_recycle_bin", "exclude", "")) != NULL) {
- recbin->exclude = talloc_strdup(recbin->mem_ctx, tmp_str);
- ALLOC_CHECK(recbin->exclude, error);
- DEBUG(5, ("recycle.bin: exclude = %s\n", recbin->exclude));
- }
- if ((tmp_str = lp_parm_const_string(SNUM(conn), "vfs_recycle_bin", "exclude_dir", "")) != NULL) {
- recbin->exclude_dir = talloc_strdup(recbin->mem_ctx, tmp_str);
- ALLOC_CHECK(recbin->exclude_dir, error);
- DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin->exclude_dir));
- }
- if ((tmp_str = lp_parm_const_string(SNUM(conn), "vfs_recycle_bin", "noversions", "")) != NULL) {
- recbin->noversions = talloc_strdup(recbin->mem_ctx, tmp_str);
- ALLOC_CHECK(recbin->noversions, error);
- DEBUG(5, ("recycle.bin: noversions = %s\n", recbin->noversions));
+ t = S_IRUSR | S_IWUSR | S_IXUSR;
}
+ config->directory_mode = (mode_t)t;
- recconn = talloc(recdata->mem_ctx, sizeof(recycle_bin_connections));
- if (recconn == NULL) {
- DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
- goto error;
- }
- recconn->conn = SNUM(conn);
- recconn->data = recbin;
- recconn->next = NULL;
- if (recdata->conns) {
- recconnbase = recdata->conns;
- while (recconnbase->next != NULL) recconnbase = recconnbase->next;
- recconnbase->next = recconn;
+ buff = lp_parm_const_string(SNUM(handle->conn),
+ "recycle",
+ "subdir_mode",
+ NULL);
+ if (buff != NULL ) {
+ sscanf(buff, "%o", &t);
} else {
- recdata->conns = recconn;
+ t = config->directory_mode;
}
- return default_vfs_ops.connect(conn, service, user);
+ config->subdir_mode = (mode_t)t;
-error:
- talloc_destroy(ctx);
- return -1;
+ SMB_VFS_HANDLE_SET_DATA(
+ handle, config, NULL, struct recycle_config_data, return -1);
+ return 0;
}
-static void recycle_disconnect(struct connection_struct *conn)
+static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
{
- recycle_bin_private_data *recdata;
- recycle_bin_connections *recconn;
-
- DEBUG(10, ("Disconnecting VFS module recycle bin\n"));
-
- if (recycle_bin_private_handle)
- recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
- else {
- DEBUG(0, ("Recycle bin not initialized!\n"));
- return;
- }
+ struct smb_filename smb_fname = {
+ .base_name = discard_const_p(char, dname)
+ };
- if (recdata) {
- if (recdata->conns) {
- if (recdata->conns->conn == SNUM(conn)) {
- talloc_destroy(recdata->conns->data->mem_ctx);
- recdata->conns = recdata->conns->next;
- } else {
- recconn = recdata->conns;
- while (recconn->next) {
- if (recconn->next->conn == SNUM(conn)) {
- talloc_destroy(recconn->next->data->mem_ctx);
- recconn->next = recconn->next->next;
- break;
- }
- recconn = recconn->next;
- }
- }
- }
- }
- default_vfs_ops.disconnect(conn);
-}
-
-static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)
-{
- SMB_STRUCT_STAT st;
-
- if (default_vfs_ops.stat(conn, dname, &st) == 0) {
- if (S_ISDIR(st.st_mode)) {
+ if (SMB_VFS_STAT(handle->conn, &smb_fname) == 0) {
+ if (S_ISDIR(smb_fname.st.st_ex_mode)) {
return True;
}
}
return False;
}
-static BOOL recycle_file_exist(connection_struct *conn, const char *fname)
+static bool recycle_file_exist(vfs_handle_struct *handle,
+ const struct smb_filename *smb_fname)
{
- SMB_STRUCT_STAT st;
+ struct smb_filename *smb_fname_tmp = NULL;
+ bool ret = false;
- if (default_vfs_ops.stat(conn, fname, &st) == 0) {
- if (S_ISREG(st.st_mode)) {
- return True;
+ smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
+ if (smb_fname_tmp == NULL) {
+ return false;
+ }
+
+ if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) == 0) {
+ if (S_ISREG(smb_fname_tmp->st.st_ex_mode)) {
+ ret = true;
}
}
- return False;
+ TALLOC_FREE(smb_fname_tmp);
+ return ret;
}
/**
* @param fname file name
* @return size in bytes
**/
-static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)
+static off_t recycle_get_file_size(vfs_handle_struct *handle,
+ const struct smb_filename *smb_fname)
{
- SMB_STRUCT_STAT st;
- if (default_vfs_ops.stat(conn, fname, &st) != 0) {
- DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
- return (SMB_OFF_T)0;
+ struct smb_filename *smb_fname_tmp = NULL;
+ off_t size;
+
+ smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
+ if (smb_fname_tmp == NULL) {
+ size = (off_t)0;
+ goto out;
}
- return(st.st_size);
+
+ if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
+ DBG_DEBUG("stat for %s returned %s\n",
+ smb_fname_str_dbg(smb_fname_tmp), strerror(errno));
+ size = (off_t)0;
+ goto out;
+ }
+
+ size = smb_fname_tmp->st.st_ex_size;
+ out:
+ TALLOC_FREE(smb_fname_tmp);
+ return size;
}
/**
* 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(connection_struct *conn, const char *dname)
+static bool recycle_create_dir(vfs_handle_struct *handle,
+ const char *dname,
+ mode_t dir_mode,
+ mode_t subdir_mode)
{
- int len;
- mode_t mode;
+ size_t len;
+ mode_t mode = dir_mode;
char *new_dir = NULL;
char *tmp_str = NULL;
char *token;
char *tok_str;
- BOOL ret = False;
+ bool ret = False;
+ char *saveptr;
- mode = S_IREAD | S_IWRITE | S_IEXEC;
-
- tmp_str = strdup(dname);
+ tmp_str = SMB_STRDUP(dname);
ALLOC_CHECK(tmp_str, done);
tok_str = tmp_str;
- len = strlen(dname);
- new_dir = (char *)malloc(len + 1);
+ len = strlen(dname)+1;
+ new_dir = (char *)SMB_MALLOC(len + 1);
ALLOC_CHECK(new_dir, done);
*new_dir = '\0';
+ if (dname[0] == '/') {
+ /* Absolute path. */
+ if (strlcat(new_dir,"/",len+1) >= len+1) {
+ goto done;
+ }
+ }
- /* Create directory tree if neccessary */
- for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) {
- safe_strcat(new_dir, token, len);
- if (recycle_directory_exist(conn, new_dir))
- DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir));
+ /* 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) {
+ goto done;
+ }
+ if (recycle_directory_exist(handle, new_dir))
+ DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
else {
- DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir));
- if (default_vfs_ops.mkdir(conn, new_dir, mode) != 0) {
- DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
+ struct smb_filename *smb_fname = NULL;
+ int retval;
+
+ DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
+
+ 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);
}
- safe_strcat(new_dir, "/", len);
+ if (strlcat(new_dir, "/", len+1) >= len+1) {
+ goto done;
}
+ mode = subdir_mode;
+ }
ret = True;
done:
}
/**
- * Check if needle is contained exactly in haystack
- * @param haystack list of parameters separated by delimimiter character
- * @param needle string to be matched exactly to haystack
- * @return True if found
+ * Check if any of the components of "exclude_list" are contained in path.
+ * Return True if found
**/
-static BOOL checkparam(const char *haystack, const char *needle)
+
+static bool matchdirparam(const char **dir_exclude_list, char *path)
{
- char *token;
- char *tok_str;
- char *tmp_str;
- BOOL ret = False;
+ char *startp = NULL, *endp = NULL;
- if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
+ if (dir_exclude_list == NULL || dir_exclude_list[0] == NULL ||
+ *dir_exclude_list[0] == '\0' || path == NULL || *path == '\0') {
return False;
}
- tmp_str = strdup(haystack);
- ALLOC_CHECK(tmp_str, done);
- token = tok_str = tmp_str;
+ /*
+ * Walk the components of path, looking for matches with the
+ * exclude list on each component.
+ */
- for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
- if(strcmp(token, needle) == 0) {
- ret = True;
- goto done;
+ for (startp = path; startp; startp = endp) {
+ int i;
+
+ while (*startp == '/') {
+ startp++;
+ }
+ endp = strchr(startp, '/');
+ if (endp) {
+ *endp = '\0';
+ }
+
+ for(i=0; dir_exclude_list[i] ; i++) {
+ if(unix_wild_match(dir_exclude_list[i], startp)) {
+ /* Repair path. */
+ if (endp) {
+ *endp = '/';
+ }
+ return True;
+ }
+ }
+
+ /* Repair path. */
+ if (endp) {
+ *endp = '/';
}
}
-done:
- SAFE_FREE(tmp_str);
- return ret;
+
+ return False;
}
/**
* @param needle string to be matched exectly to haystack including pattern matching
* @return True if found
**/
-static BOOL matchparam(const char *haystack, const char *needle)
+static bool matchparam(const char **haystack_list, const char *needle)
{
- char *token;
- char *tok_str;
- char *tmp_str;
- BOOL ret = False;
+ int i;
- if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
+ if (haystack_list == NULL || haystack_list[0] == NULL ||
+ *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') {
return False;
}
- tmp_str = strdup(haystack);
- ALLOC_CHECK(tmp_str, done);
- token = tok_str = tmp_str;
-
- for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
- if (!unix_wild_match(token, needle)) {
- ret = True;
- goto done;
+ for(i=0; haystack_list[i] ; i++) {
+ if(unix_wild_match(haystack_list[i], needle)) {
+ return True;
}
}
-done:
- SAFE_FREE(tmp_str);
- return ret;
+
+ return False;
}
/**
- * Touch access date
+ * Touch access or modify date
**/
-static void recycle_touch(connection_struct *conn, const char *fname)
+static void recycle_do_touch(vfs_handle_struct *handle,
+ const struct smb_filename *smb_fname,
+ bool touch_mtime)
{
- SMB_STRUCT_STAT st;
- struct utimbuf tb;
- time_t currtime;
-
- if (default_vfs_ops.stat(conn, fname, &st) != 0) {
- DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
+ struct smb_filename *smb_fname_tmp = NULL;
+ struct smb_file_time ft;
+ int ret, err;
+ NTSTATUS status;
+
+ init_smb_file_time(&ft);
+
+ 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;
}
- currtime = time(&currtime);
- tb.actime = currtime;
- tb.modtime = st.st_mtime;
- if (default_vfs_ops.utime(conn, fname, &tb) == -1 )
- DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname, strerror(errno)));
+ /* 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_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)));
}
+ TALLOC_FREE(smb_fname_tmp);
+}
+
/**
* Check if file should be recycled
**/
-static int recycle_unlink(connection_struct *conn, const char *file_name)
+static int recycle_unlink_internal(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ int flags)
{
- recycle_bin_private_data *recdata;
- recycle_bin_connections *recconn;
- recycle_bin_struct *recbin;
+ TALLOC_CTX *frame = NULL;
+ struct smb_filename *full_fname = NULL;
char *path_name = NULL;
- char *temp_name = NULL;
- char *final_name = NULL;
- const char *base;
- int i;
-/* SMB_BIG_UINT dfree, dsize, bsize; */
- SMB_OFF_T file_size; /* space_avail; */
- BOOL exist;
+ const char *temp_name = NULL;
+ const char *final_name = NULL;
+ struct smb_filename *smb_fname_final = NULL;
+ const char *base = NULL;
+ int i = 1;
+ off_t file_size; /* space_avail; */
+ bool exist;
int rc = -1;
-
- recbin = NULL;
- if (recycle_bin_private_handle) {
- recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
- if (recdata) {
- if (recdata->conns) {
- recconn = recdata->conns;
- while (recconn && recconn->conn != SNUM(conn)) recconn = recconn->next;
- if (recconn != NULL) {
- recbin = recconn->data;
- }
- }
- }
- }
- if (recbin == NULL) {
- DEBUG(0, ("Recycle bin not initialized!\n"));
- rc = default_vfs_ops.unlink(conn, file_name);
+ struct recycle_config_data *config = NULL;
+ struct vfs_rename_how rhow = { .flags = 0, };
+
+ SMB_VFS_HANDLE_GET_DATA(handle,
+ config,
+ struct recycle_config_data,
+ return -1);
+
+ frame = talloc_stackframe();
+
+ if (config->repository[0] == '\0') {
+ DEBUG(3, ("recycle: repository path not set, purging %s...\n",
+ smb_fname_str_dbg(smb_fname)));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
goto done;
}
- if(!recbin->repository || *(recbin->repository) == '\0') {
- DEBUG(3, ("Recycle path not set, purging %s...\n", file_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ full_fname = full_path_from_dirfsp_atname(frame,
+ dirfsp,
+ smb_fname);
+ if (full_fname == NULL) {
+ rc = -1;
+ errno = ENOMEM;
goto done;
}
/* we don't recycle the recycle bin... */
- if (strncmp(file_name, recbin->repository, strlen(recbin->repository)) == 0) {
- DEBUG(3, ("File is within recycling bin, unlinking ...\n"));
- rc = default_vfs_ops.unlink(conn, file_name);
+ if (strncmp(full_fname->base_name, config->repository,
+ strlen(config->repository)) == 0) {
+ DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n"));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
goto done;
}
- file_size = recycle_get_file_size(conn, file_name);
+ 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, ("File %s is empty, purging...\n", file_name));
- rc = default_vfs_ops.unlink(conn,file_name);
+ DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ file_name,
+ flags);
goto done;
}
*/
- /* FIXME: this is wrong, we should check the hole size of the recycle bin is
+ /* FIXME: this is wrong, we should check the whole size of the recycle bin is
* not greater then maxsize, not the size of the single file, also it is better
* to remove older files
*/
- if(recbin->maxsize > 0 && file_size > recbin->maxsize) {
- DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ 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;
+ }
+ 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;
}
/* FIXME: this is wrong: moving files with rename does not change the disk space
* allocation
*
- space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L;
+ space_avail = SMB_VFS_NEXT_DISK_FREE(handle, ".", True, &bsize, &dfree, &dsize) * 1024L;
DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
if(space_avail < file_size) {
- DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ file_name,
+ flags);
goto done;
}
*/
/* extract filename and path */
- base = strrchr(file_name, '/');
- if (base == NULL) {
- base = file_name;
- path_name = strdup("/");
- ALLOC_CHECK(path_name, done);
- }
- else {
- path_name = strdup(file_name);
- ALLOC_CHECK(path_name, done);
- path_name[base - file_name] = '\0';
- base++;
- }
-
- DEBUG(10, ("recycle.bin: fname = %s\n", file_name)); /* original filename with path */
- DEBUG(10, ("recycle.bin: fpath = %s\n", path_name)); /* original path */
- DEBUG(10, ("recycle.bin: base = %s\n", base)); /* filename without path */
-
- if (matchparam(recbin->exclude, base)) {
- DEBUG(3, ("recycle.bin: file %s is excluded \n", base));
- rc = default_vfs_ops.unlink(conn, file_name);
+ if (!parent_dirname(frame, full_fname->base_name, &path_name, &base)) {
+ rc = -1;
+ errno = ENOMEM;
goto done;
}
- /* FIXME: this check will fail if we have more than one level of directories,
- * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 ....
- * ---simo
- */
- if (checkparam(recbin->exclude_dir, path_name)) {
- DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ /* original filename with path */
+ 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(config->exclude, base)) {
+ DEBUG(3, ("recycle: file %s is excluded \n", base));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
goto done;
}
- /* see if we need to recreate the original directory structure in the recycle bin */
- if (recbin->keep_dir_tree == True) {
- asprintf(&temp_name, "%s/%s", recbin->repository, path_name);
+ if (matchdirparam(config->exclude_dir, path_name)) {
+ DEBUG(3, ("recycle: directory %s is excluded \n", path_name));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
+ goto done;
+ }
+
+ if (config->keeptree) {
+ temp_name = talloc_asprintf(frame, "%s/%s",
+ config->repository,
+ path_name);
+ if (temp_name == NULL) {
+ rc = -1;
+ goto done;
+ }
} else {
- temp_name = strdup(recbin->repository);
+ temp_name = config->repository;
}
- ALLOC_CHECK(temp_name, done);
- exist = recycle_directory_exist(conn, temp_name);
+ exist = recycle_directory_exist(handle, temp_name);
if (exist) {
- DEBUG(10, ("recycle.bin: Directory already exists\n"));
+ DEBUG(10, ("recycle: Directory already exists\n"));
} else {
- DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name));
- if (recycle_create_dir(conn, temp_name) == False) {
- DEBUG(3, ("Could not create directory, purging %s...\n", file_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
+ 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(full_fname)));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
goto done;
}
}
- asprintf(&final_name, "%s/%s", temp_name, base);
- ALLOC_CHECK(final_name, done);
- DEBUG(10, ("recycle.bin: recycled file name: %s\n", temp_name)); /* new filename with path */
+ final_name = talloc_asprintf(frame, "%s/%s",
+ temp_name, base);
+ if (final_name == NULL) {
+ rc = -1;
+ goto done;
+ }
+
+ /* Create smb_fname with final base name and orig stream name. */
+ smb_fname_final = synthetic_smb_fname(frame,
+ 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;
+ }
+
+ /* new filename with path */
+ DEBUG(10, ("recycle: recycled file name: %s\n",
+ smb_fname_str_dbg(smb_fname_final)));
/* check if we should delete file from recycle bin */
- if (recycle_file_exist(conn, final_name)) {
- if (recbin->versions == False || matchparam(recbin->noversions, base) == True) {
- DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", final_name));
- if (default_vfs_ops.unlink(conn, final_name) != 0) {
- DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno)));
+ if (recycle_file_exist(handle, smb_fname_final)) {
+ 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_UNLINKAT(handle,
+ dirfsp->conn->cwd_fsp,
+ smb_fname_final,
+ flags) != 0) {
+ DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
}
}
}
/* rename file we move to recycle bin */
i = 1;
- while (recycle_file_exist(conn, final_name)) {
- snprintf(final_name, PATH_MAX, "%s/Copy #%d of %s", temp_name, i++, base);
+ while (recycle_file_exist(handle, smb_fname_final)) {
+ char *copy = NULL;
+
+ TALLOC_FREE(smb_fname_final->base_name);
+ copy = talloc_asprintf(smb_fname_final, "%s/Copy #%d of %s",
+ temp_name, i++, base);
+ if (copy == NULL) {
+ rc = -1;
+ goto done;
+ }
+ smb_fname_final->base_name = copy;
}
- DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name, final_name));
- rc = default_vfs_ops.rename(conn, file_name, final_name);
+ 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_RENAMEAT(handle,
+ dirfsp,
+ smb_fname,
+ handle->conn->cwd_fsp,
+ smb_fname_final,
+ &rhow);
if (rc != 0) {
- DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name));
- rc = default_vfs_ops.unlink(conn, file_name);
+ DEBUG(3, ("recycle: Move error %d (%s), purging file %s "
+ "(%s)\n", errno, strerror(errno),
+ smb_fname_str_dbg(full_fname),
+ smb_fname_str_dbg(smb_fname_final)));
+ rc = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname,
+ flags);
goto done;
}
/* touch access date of moved file */
- if (recbin->touch == True )
- recycle_touch(conn, final_name);
+ if (config->touch || config->touch_mtime)
+ recycle_do_touch(handle, smb_fname_final, config->touch_mtime);
done:
- SAFE_FREE(path_name);
- SAFE_FREE(temp_name);
- SAFE_FREE(final_name);
+ TALLOC_FREE(frame);
return rc;
}
-int vfs_recycle_init(void)
+static int recycle_unlinkat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ int flags)
{
- return smb_register_vfs("recycle", recycle_init, SMB_VFS_INTERFACE_VERSION);
+ 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 = {
+ .connect_fn = vfs_recycle_connect,
+ .unlinkat_fn = recycle_unlinkat,
+};
+
+static_decl_vfs;
+NTSTATUS vfs_recycle_init(TALLOC_CTX *ctx)
+{
+ NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle",
+ &vfs_recycle_fns);
+
+ if (!NT_STATUS_IS_OK(ret))
+ return ret;
+
+ vfs_recycle_debug_level = debug_add_class("recycle");
+ if (vfs_recycle_debug_level == -1) {
+ vfs_recycle_debug_level = DBGC_VFS;
+ DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
+ } else {
+ DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level));
+ }
+
+ return ret;
}