2 * Recycle bin VFS module for Samba.
4 * Copyright (C) 2001, Brandon Stone, Amherst College, <bbstone@amherst.edu>.
5 * Copyright (C) 2002, Jeremy Allison - modified to make a VFS module.
6 * Copyright (C) 2002, Alexander Bokovoy - cascaded VFS adoption,
7 * Copyright (C) 2002, Juergen Hasch - added some options.
8 * Copyright (C) 2002, Simo Sorce
9 * Copyright (C) 2002, Stefan (metze) Metzmacher
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "smbd/smbd.h"
27 #include "system/filesys.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
30 #include "source3/lib/substitute.h"
32 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
34 static int vfs_recycle_debug_level = DBGC_VFS;
37 #define DBGC_CLASS vfs_recycle_debug_level
39 struct recycle_config_data {
40 const char *repository;
46 const char **exclude_dir;
47 const char **noversions;
48 mode_t directory_mode;
54 static int vfs_recycle_connect(struct vfs_handle_struct *handle,
58 struct recycle_config_data *config = NULL;
63 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
68 if (IS_IPC(handle->conn) || IS_PRINT(handle->conn)) {
72 config = talloc_zero(handle->conn, struct recycle_config_data);
74 DBG_ERR("talloc_zero() failed\n");
78 config->repository = lp_parm_const_string(SNUM(handle->conn),
82 config->keeptree = lp_parm_bool(SNUM(handle->conn),
86 config->versions = lp_parm_bool(SNUM(handle->conn),
90 config->touch = lp_parm_bool(SNUM(handle->conn),
94 config->touch_mtime = lp_parm_bool(SNUM(handle->conn),
98 config->exclude = lp_parm_string_list(SNUM(handle->conn),
102 config->exclude_dir = lp_parm_string_list(SNUM(handle->conn),
106 config->noversions = lp_parm_string_list(SNUM(handle->conn),
110 config->minsize = conv_str_size(lp_parm_const_string(
111 SNUM(handle->conn), "recycle", "minsize", NULL));
112 config->maxsize = conv_str_size(lp_parm_const_string(
113 SNUM(handle->conn), "recycle", "maxsize", NULL));
115 buff = lp_parm_const_string(SNUM(handle->conn),
120 sscanf(buff, "%o", &t);
122 t = S_IRUSR | S_IWUSR | S_IXUSR;
124 config->directory_mode = (mode_t)t;
126 buff = lp_parm_const_string(SNUM(handle->conn),
131 sscanf(buff, "%o", &t);
133 t = config->directory_mode;
135 config->subdir_mode = (mode_t)t;
137 SMB_VFS_HANDLE_SET_DATA(
138 handle, config, NULL, struct recycle_config_data, return -1);
142 static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
144 struct smb_filename smb_fname = {
145 .base_name = discard_const_p(char, dname)
148 if (SMB_VFS_STAT(handle->conn, &smb_fname) == 0) {
149 if (S_ISDIR(smb_fname.st.st_ex_mode)) {
157 static bool recycle_file_exist(vfs_handle_struct *handle,
158 const struct smb_filename *smb_fname)
160 struct smb_filename *smb_fname_tmp = NULL;
163 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
164 if (smb_fname_tmp == NULL) {
168 if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) == 0) {
169 if (S_ISREG(smb_fname_tmp->st.st_ex_mode)) {
174 TALLOC_FREE(smb_fname_tmp);
180 * @param conn connection
181 * @param fname file name
182 * @return size in bytes
184 static off_t recycle_get_file_size(vfs_handle_struct *handle,
185 const struct smb_filename *smb_fname)
187 struct smb_filename *smb_fname_tmp = NULL;
190 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
191 if (smb_fname_tmp == NULL) {
196 if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
197 DBG_DEBUG("stat for %s returned %s\n",
198 smb_fname_str_dbg(smb_fname_tmp), strerror(errno));
203 size = smb_fname_tmp->st.st_ex_size;
205 TALLOC_FREE(smb_fname_tmp);
210 * Create directory tree
211 * @param conn connection
212 * @param dname Directory tree to be created
213 * @param directory mode
214 * @param subdirectory mode
215 * @return Returns True for success
217 static bool recycle_create_dir(vfs_handle_struct *handle,
223 mode_t mode = dir_mode;
224 char *new_dir = NULL;
225 char *tmp_str = NULL;
231 tmp_str = SMB_STRDUP(dname);
232 ALLOC_CHECK(tmp_str, done);
235 len = strlen(dname)+1;
236 new_dir = (char *)SMB_MALLOC(len + 1);
237 ALLOC_CHECK(new_dir, done);
239 if (dname[0] == '/') {
241 if (strlcat(new_dir,"/",len+1) >= len+1) {
246 /* Create directory tree if necessary */
247 for(token = strtok_r(tok_str, "/", &saveptr); token;
248 token = strtok_r(NULL, "/", &saveptr)) {
249 if (strlcat(new_dir, token, len+1) >= len+1) {
252 if (recycle_directory_exist(handle, new_dir))
253 DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
255 struct smb_filename *smb_fname = NULL;
258 DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
260 smb_fname = synthetic_smb_fname(talloc_tos(),
266 if (smb_fname == NULL) {
270 retval = SMB_VFS_NEXT_MKDIRAT(handle,
271 handle->conn->cwd_fsp,
275 DBG_WARNING("recycle: mkdirat failed "
276 "for %s with error: %s\n",
279 TALLOC_FREE(smb_fname);
283 TALLOC_FREE(smb_fname);
285 if (strlcat(new_dir, "/", len+1) >= len+1) {
299 * Check if any of the components of "exclude_list" are contained in path.
300 * Return True if found
303 static bool matchdirparam(const char **dir_exclude_list, char *path)
305 char *startp = NULL, *endp = NULL;
307 if (dir_exclude_list == NULL || dir_exclude_list[0] == NULL ||
308 *dir_exclude_list[0] == '\0' || path == NULL || *path == '\0') {
313 * Walk the components of path, looking for matches with the
314 * exclude list on each component.
317 for (startp = path; startp; startp = endp) {
320 while (*startp == '/') {
323 endp = strchr(startp, '/');
328 for(i=0; dir_exclude_list[i] ; i++) {
329 if(unix_wild_match(dir_exclude_list[i], startp)) {
348 * Check if needle is contained in haystack, * and ? patterns are resolved
349 * @param haystack list of parameters separated by delimimiter character
350 * @param needle string to be matched exectly to haystack including pattern matching
351 * @return True if found
353 static bool matchparam(const char **haystack_list, const char *needle)
357 if (haystack_list == NULL || haystack_list[0] == NULL ||
358 *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') {
362 for(i=0; haystack_list[i] ; i++) {
363 if(unix_wild_match(haystack_list[i], needle)) {
372 * Touch access or modify date
374 static void recycle_do_touch(vfs_handle_struct *handle,
375 const struct smb_filename *smb_fname,
378 struct smb_filename *smb_fname_tmp = NULL;
379 struct smb_file_time ft;
383 init_smb_file_time(&ft);
385 status = synthetic_pathref(talloc_tos(),
386 handle->conn->cwd_fsp,
387 smb_fname->base_name,
388 smb_fname->stream_name,
393 if (!NT_STATUS_IS_OK(status)) {
394 DBG_DEBUG("synthetic_pathref for '%s' failed: %s\n",
395 smb_fname_str_dbg(smb_fname), nt_errstr(status));
400 ft.atime = timespec_current();
402 ft.mtime = touch_mtime ? ft.atime : smb_fname_tmp->st.st_ex_mtime;
405 ret = SMB_VFS_NEXT_FNTIMES(handle, smb_fname_tmp->fsp, &ft);
409 DEBUG(0, ("recycle: touching %s failed, reason = %s\n",
410 smb_fname_str_dbg(smb_fname_tmp), strerror(err)));
413 TALLOC_FREE(smb_fname_tmp);
417 * Check if file should be recycled
419 static int recycle_unlink_internal(vfs_handle_struct *handle,
420 struct files_struct *dirfsp,
421 const struct smb_filename *smb_fname,
424 const struct loadparm_substitution *lp_sub =
425 loadparm_s3_global_substitution();
426 connection_struct *conn = handle->conn;
427 struct smb_filename *full_fname = NULL;
428 char *path_name = NULL;
429 char *temp_name = NULL;
430 char *final_name = NULL;
431 struct smb_filename *smb_fname_final = NULL;
433 char *repository = NULL;
435 off_t file_size; /* space_avail; */
438 struct recycle_config_data *config;
440 SMB_VFS_HANDLE_GET_DATA(handle,
442 struct recycle_config_data,
445 repository = talloc_sub_full(
447 lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
448 conn->session_info->unix_info->unix_name,
450 conn->session_info->unix_token->gid,
451 conn->session_info->unix_info->sanitized_username,
452 conn->session_info->info->domain_name,
454 ALLOC_CHECK(repository, done);
455 /* shouldn't we allow absolute path names here? --metze */
457 trim_char(repository, '\0', '/');
459 if(!repository || *(repository) == '\0') {
460 DEBUG(3, ("recycle: repository path not set, purging %s...\n",
461 smb_fname_str_dbg(smb_fname)));
462 rc = SMB_VFS_NEXT_UNLINKAT(handle,
469 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
472 if (full_fname == NULL) {
476 /* we don't recycle the recycle bin... */
477 if (strncmp(full_fname->base_name, repository,
478 strlen(repository)) == 0) {
479 DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n"));
480 rc = SMB_VFS_NEXT_UNLINKAT(handle,
487 file_size = recycle_get_file_size(handle, full_fname);
488 /* it is wrong to purge filenames only because they are empty imho
492 DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
493 rc = SMB_VFS_NEXT_UNLINKAT(handle,
501 /* FIXME: this is wrong, we should check the whole size of the recycle bin is
502 * not greater then maxsize, not the size of the single file, also it is better
503 * to remove older files
505 if (config->maxsize > 0 && file_size > config->maxsize) {
506 DBG_NOTICE("File %s exceeds maximum recycle size, "
508 smb_fname_str_dbg(full_fname));
509 rc = SMB_VFS_NEXT_UNLINKAT(handle,
515 if (config->minsize > 0 && file_size < config->minsize) {
516 DBG_NOTICE("File %s lowers minimum recycle size, "
518 smb_fname_str_dbg(full_fname));
519 rc = SMB_VFS_NEXT_UNLINKAT(handle,
526 /* FIXME: this is wrong: moving files with rename does not change the disk space
529 space_avail = SMB_VFS_NEXT_DISK_FREE(handle, ".", True, &bsize, &dfree, &dsize) * 1024L;
530 DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
531 if(space_avail < file_size) {
532 DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name));
533 rc = SMB_VFS_NEXT_UNLINKAT(handle,
541 /* extract filename and path */
542 if (!parent_dirname(talloc_tos(), full_fname->base_name, &path_name, &base)) {
548 /* original filename with path */
549 DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(full_fname)));
551 DEBUG(10, ("recycle: fpath = %s\n", path_name));
552 /* filename without path */
553 DEBUG(10, ("recycle: base = %s\n", base));
555 if (matchparam(config->exclude, base)) {
556 DEBUG(3, ("recycle: file %s is excluded \n", base));
557 rc = SMB_VFS_NEXT_UNLINKAT(handle,
564 if (matchdirparam(config->exclude_dir, path_name)) {
565 DEBUG(3, ("recycle: directory %s is excluded \n", path_name));
566 rc = SMB_VFS_NEXT_UNLINKAT(handle,
573 if (config->keeptree) {
574 if (asprintf(&temp_name, "%s/%s", repository, path_name) == -1) {
575 ALLOC_CHECK(temp_name, done);
578 temp_name = SMB_STRDUP(repository);
580 ALLOC_CHECK(temp_name, done);
582 exist = recycle_directory_exist(handle, temp_name);
584 DEBUG(10, ("recycle: Directory already exists\n"));
586 DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
587 if (recycle_create_dir(handle,
589 config->directory_mode,
590 config->subdir_mode) == False)
592 DEBUG(3, ("recycle: Could not create directory, "
594 smb_fname_str_dbg(full_fname)));
595 rc = SMB_VFS_NEXT_UNLINKAT(handle,
603 if (asprintf(&final_name, "%s/%s", temp_name, base) == -1) {
604 ALLOC_CHECK(final_name, done);
607 /* Create smb_fname with final base name and orig stream name. */
608 smb_fname_final = synthetic_smb_fname(talloc_tos(),
610 full_fname->stream_name,
614 if (smb_fname_final == NULL) {
615 rc = SMB_VFS_NEXT_UNLINKAT(handle,
622 /* new filename with path */
623 DEBUG(10, ("recycle: recycled file name: %s\n",
624 smb_fname_str_dbg(smb_fname_final)));
626 /* check if we should delete file from recycle bin */
627 if (recycle_file_exist(handle, smb_fname_final)) {
628 if (config->versions == False ||
629 matchparam(config->noversions, base) == True) {
630 DEBUG(3, ("recycle: Removing old file %s from recycle "
631 "bin\n", smb_fname_str_dbg(smb_fname_final)));
632 if (SMB_VFS_NEXT_UNLINKAT(handle,
633 dirfsp->conn->cwd_fsp,
636 DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
641 /* rename file we move to recycle bin */
643 while (recycle_file_exist(handle, smb_fname_final)) {
644 SAFE_FREE(final_name);
645 if (asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base) == -1) {
646 ALLOC_CHECK(final_name, done);
648 TALLOC_FREE(smb_fname_final->base_name);
649 smb_fname_final->base_name = talloc_strdup(smb_fname_final,
651 if (smb_fname_final->base_name == NULL) {
652 rc = SMB_VFS_NEXT_UNLINKAT(handle,
660 DEBUG(10, ("recycle: Moving %s to %s\n", smb_fname_str_dbg(full_fname),
661 smb_fname_str_dbg(smb_fname_final)));
662 rc = SMB_VFS_NEXT_RENAMEAT(handle,
665 handle->conn->cwd_fsp,
668 DEBUG(3, ("recycle: Move error %d (%s), purging file %s "
669 "(%s)\n", errno, strerror(errno),
670 smb_fname_str_dbg(full_fname),
671 smb_fname_str_dbg(smb_fname_final)));
672 rc = SMB_VFS_NEXT_UNLINKAT(handle,
679 /* touch access date of moved file */
680 if (config->touch || config->touch_mtime)
681 recycle_do_touch(handle, smb_fname_final, config->touch_mtime);
684 TALLOC_FREE(path_name);
685 SAFE_FREE(temp_name);
686 SAFE_FREE(final_name);
687 TALLOC_FREE(full_fname);
688 TALLOC_FREE(smb_fname_final);
689 TALLOC_FREE(repository);
693 static int recycle_unlinkat(vfs_handle_struct *handle,
694 struct files_struct *dirfsp,
695 const struct smb_filename *smb_fname,
700 if (flags & AT_REMOVEDIR) {
701 ret = SMB_VFS_NEXT_UNLINKAT(handle,
706 ret = recycle_unlink_internal(handle,
714 static struct vfs_fn_pointers vfs_recycle_fns = {
715 .connect_fn = vfs_recycle_connect,
716 .unlinkat_fn = recycle_unlinkat,
720 NTSTATUS vfs_recycle_init(TALLOC_CTX *ctx)
722 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle",
725 if (!NT_STATUS_IS_OK(ret))
728 vfs_recycle_debug_level = debug_add_class("recycle");
729 if (vfs_recycle_debug_level == -1) {
730 vfs_recycle_debug_level = DBGC_VFS;
731 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
733 DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level));