s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / source3 / modules / vfs_recycle.c
blob327a7eea06e31b8d076f5e4fad72d5148030be15
1 /*
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/>.
25 #include "includes.h"
26 #include "smbd/smbd.h"
27 #include "system/filesys.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "auth.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;
36 #undef DBGC_CLASS
37 #define DBGC_CLASS vfs_recycle_debug_level
39 struct recycle_config_data {
40 const char *repository;
41 bool keeptree;
42 bool versions;
43 bool touch;
44 bool touch_mtime;
45 const char **exclude;
46 const char **exclude_dir;
47 const char **noversions;
48 mode_t directory_mode;
49 mode_t subdir_mode;
50 off_t minsize;
51 off_t maxsize;
54 static int vfs_recycle_connect(struct vfs_handle_struct *handle,
55 const char *service,
56 const char *user)
58 struct recycle_config_data *config = NULL;
59 int ret;
60 int t;
61 const char *buff;
63 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
64 if (ret < 0) {
65 return ret;
68 if (IS_IPC(handle->conn) || IS_PRINT(handle->conn)) {
69 return 0;
72 config = talloc_zero(handle->conn, struct recycle_config_data);
73 if (config == NULL) {
74 DBG_ERR("talloc_zero() failed\n");
75 errno = ENOMEM;
76 return -1;
78 config->repository = lp_parm_const_string(SNUM(handle->conn),
79 "recycle",
80 "repository",
81 ".recycle");
82 config->keeptree = lp_parm_bool(SNUM(handle->conn),
83 "recycle",
84 "keeptree",
85 False);
86 config->versions = lp_parm_bool(SNUM(handle->conn),
87 "recycle",
88 "versions",
89 False);
90 config->touch = lp_parm_bool(SNUM(handle->conn),
91 "recycle",
92 "touch",
93 False);
94 config->touch_mtime = lp_parm_bool(SNUM(handle->conn),
95 "recycle",
96 "touch_mtime",
97 False);
98 config->exclude = lp_parm_string_list(SNUM(handle->conn),
99 "recycle",
100 "exclude",
101 NULL);
102 config->exclude_dir = lp_parm_string_list(SNUM(handle->conn),
103 "recycle",
104 "exclude_dir",
105 NULL);
106 config->noversions = lp_parm_string_list(SNUM(handle->conn),
107 "recycle",
108 "noversions",
109 NULL);
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),
116 "recycle",
117 "directory_mode",
118 NULL);
119 if (buff != NULL ) {
120 sscanf(buff, "%o", &t);
121 } else {
122 t = S_IRUSR | S_IWUSR | S_IXUSR;
124 config->directory_mode = (mode_t)t;
126 buff = lp_parm_const_string(SNUM(handle->conn),
127 "recycle",
128 "subdir_mode",
129 NULL);
130 if (buff != NULL ) {
131 sscanf(buff, "%o", &t);
132 } else {
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);
139 return 0;
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)) {
150 return True;
154 return False;
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;
161 bool ret = false;
163 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
164 if (smb_fname_tmp == NULL) {
165 return false;
168 if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) == 0) {
169 if (S_ISREG(smb_fname_tmp->st.st_ex_mode)) {
170 ret = true;
174 TALLOC_FREE(smb_fname_tmp);
175 return ret;
179 * Return file size
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;
188 off_t size;
190 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
191 if (smb_fname_tmp == NULL) {
192 size = (off_t)0;
193 goto out;
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));
199 size = (off_t)0;
200 goto out;
203 size = smb_fname_tmp->st.st_ex_size;
204 out:
205 TALLOC_FREE(smb_fname_tmp);
206 return size;
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,
218 const char *dname,
219 mode_t dir_mode,
220 mode_t subdir_mode)
222 size_t len;
223 mode_t mode = dir_mode;
224 char *new_dir = NULL;
225 char *tmp_str = NULL;
226 char *token;
227 char *tok_str;
228 bool ret = False;
229 char *saveptr;
231 tmp_str = SMB_STRDUP(dname);
232 ALLOC_CHECK(tmp_str, done);
233 tok_str = tmp_str;
235 len = strlen(dname)+1;
236 new_dir = (char *)SMB_MALLOC(len + 1);
237 ALLOC_CHECK(new_dir, done);
238 *new_dir = '\0';
239 if (dname[0] == '/') {
240 /* Absolute path. */
241 if (strlcat(new_dir,"/",len+1) >= len+1) {
242 goto done;
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) {
250 goto done;
252 if (recycle_directory_exist(handle, new_dir))
253 DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
254 else {
255 struct smb_filename *smb_fname = NULL;
256 int retval;
258 DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
260 smb_fname = synthetic_smb_fname(talloc_tos(),
261 new_dir,
262 NULL,
263 NULL,
266 if (smb_fname == NULL) {
267 goto done;
270 retval = SMB_VFS_NEXT_MKDIRAT(handle,
271 handle->conn->cwd_fsp,
272 smb_fname,
273 mode);
274 if (retval != 0) {
275 DBG_WARNING("recycle: mkdirat failed "
276 "for %s with error: %s\n",
277 new_dir,
278 strerror(errno));
279 TALLOC_FREE(smb_fname);
280 ret = False;
281 goto done;
283 TALLOC_FREE(smb_fname);
285 if (strlcat(new_dir, "/", len+1) >= len+1) {
286 goto done;
288 mode = subdir_mode;
291 ret = True;
292 done:
293 SAFE_FREE(tmp_str);
294 SAFE_FREE(new_dir);
295 return ret;
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') {
309 return False;
313 * Walk the components of path, looking for matches with the
314 * exclude list on each component.
317 for (startp = path; startp; startp = endp) {
318 int i;
320 while (*startp == '/') {
321 startp++;
323 endp = strchr(startp, '/');
324 if (endp) {
325 *endp = '\0';
328 for(i=0; dir_exclude_list[i] ; i++) {
329 if(unix_wild_match(dir_exclude_list[i], startp)) {
330 /* Repair path. */
331 if (endp) {
332 *endp = '/';
334 return True;
338 /* Repair path. */
339 if (endp) {
340 *endp = '/';
344 return False;
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)
355 int i;
357 if (haystack_list == NULL || haystack_list[0] == NULL ||
358 *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') {
359 return False;
362 for(i=0; haystack_list[i] ; i++) {
363 if(unix_wild_match(haystack_list[i], needle)) {
364 return True;
368 return False;
372 * Touch access or modify date
374 static void recycle_do_touch(vfs_handle_struct *handle,
375 const struct smb_filename *smb_fname,
376 bool touch_mtime)
378 struct smb_filename *smb_fname_tmp = NULL;
379 struct smb_file_time ft;
380 int ret, err;
381 NTSTATUS status;
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,
389 NULL,
390 smb_fname->twrp,
391 smb_fname->flags,
392 &smb_fname_tmp);
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));
396 return;
399 /* atime */
400 ft.atime = timespec_current();
401 /* mtime */
402 ft.mtime = touch_mtime ? ft.atime : smb_fname_tmp->st.st_ex_mtime;
404 become_root();
405 ret = SMB_VFS_NEXT_FNTIMES(handle, smb_fname_tmp->fsp, &ft);
406 err = errno;
407 unbecome_root();
408 if (ret == -1 ) {
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,
422 int flags)
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;
432 const char *base;
433 char *repository = NULL;
434 int i = 1;
435 off_t file_size; /* space_avail; */
436 bool exist;
437 int rc = -1;
438 struct recycle_config_data *config;
440 SMB_VFS_HANDLE_GET_DATA(handle,
441 config,
442 struct recycle_config_data,
443 return true);
445 repository = talloc_sub_full(
446 NULL,
447 lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
448 conn->session_info->unix_info->unix_name,
449 conn->connectpath,
450 conn->session_info->unix_token->gid,
451 conn->session_info->unix_info->sanitized_username,
452 conn->session_info->info->domain_name,
453 config->repository);
454 ALLOC_CHECK(repository, done);
455 /* shouldn't we allow absolute path names here? --metze */
456 /* Yes :-). JRA. */
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,
463 dirfsp,
464 smb_fname,
465 flags);
466 goto done;
469 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
470 dirfsp,
471 smb_fname);
472 if (full_fname == NULL) {
473 return -1;
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,
481 dirfsp,
482 smb_fname,
483 flags);
484 goto done;
487 file_size = recycle_get_file_size(handle, full_fname);
488 /* it is wrong to purge filenames only because they are empty imho
489 * --- simo
491 if(fsize == 0) {
492 DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
493 rc = SMB_VFS_NEXT_UNLINKAT(handle,
494 dirfsp,
495 file_name,
496 flags);
497 goto done;
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, "
507 "purging... \n",
508 smb_fname_str_dbg(full_fname));
509 rc = SMB_VFS_NEXT_UNLINKAT(handle,
510 dirfsp,
511 smb_fname,
512 flags);
513 goto done;
515 if (config->minsize > 0 && file_size < config->minsize) {
516 DBG_NOTICE("File %s lowers minimum recycle size, "
517 "purging... \n",
518 smb_fname_str_dbg(full_fname));
519 rc = SMB_VFS_NEXT_UNLINKAT(handle,
520 dirfsp,
521 smb_fname,
522 flags);
523 goto done;
526 /* FIXME: this is wrong: moving files with rename does not change the disk space
527 * allocation
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,
534 dirfsp,
535 file_name,
536 flags);
537 goto done;
541 /* extract filename and path */
542 if (!parent_dirname(talloc_tos(), full_fname->base_name, &path_name, &base)) {
543 rc = -1;
544 errno = ENOMEM;
545 goto done;
548 /* original filename with path */
549 DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(full_fname)));
550 /* original path */
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,
558 dirfsp,
559 smb_fname,
560 flags);
561 goto done;
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,
567 dirfsp,
568 smb_fname,
569 flags);
570 goto done;
573 if (config->keeptree) {
574 if (asprintf(&temp_name, "%s/%s", repository, path_name) == -1) {
575 ALLOC_CHECK(temp_name, done);
577 } else {
578 temp_name = SMB_STRDUP(repository);
580 ALLOC_CHECK(temp_name, done);
582 exist = recycle_directory_exist(handle, temp_name);
583 if (exist) {
584 DEBUG(10, ("recycle: Directory already exists\n"));
585 } else {
586 DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
587 if (recycle_create_dir(handle,
588 temp_name,
589 config->directory_mode,
590 config->subdir_mode) == False)
592 DEBUG(3, ("recycle: Could not create directory, "
593 "purging %s...\n",
594 smb_fname_str_dbg(full_fname)));
595 rc = SMB_VFS_NEXT_UNLINKAT(handle,
596 dirfsp,
597 smb_fname,
598 flags);
599 goto done;
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(),
609 final_name,
610 full_fname->stream_name,
611 NULL,
612 full_fname->twrp,
613 full_fname->flags);
614 if (smb_fname_final == NULL) {
615 rc = SMB_VFS_NEXT_UNLINKAT(handle,
616 dirfsp,
617 smb_fname,
618 flags);
619 goto done;
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,
634 smb_fname_final,
635 flags) != 0) {
636 DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
641 /* rename file we move to recycle bin */
642 i = 1;
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,
650 final_name);
651 if (smb_fname_final->base_name == NULL) {
652 rc = SMB_VFS_NEXT_UNLINKAT(handle,
653 dirfsp,
654 smb_fname,
655 flags);
656 goto done;
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,
663 dirfsp,
664 smb_fname,
665 handle->conn->cwd_fsp,
666 smb_fname_final);
667 if (rc != 0) {
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,
673 dirfsp,
674 smb_fname,
675 flags);
676 goto done;
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);
683 done:
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);
690 return rc;
693 static int recycle_unlinkat(vfs_handle_struct *handle,
694 struct files_struct *dirfsp,
695 const struct smb_filename *smb_fname,
696 int flags)
698 int ret;
700 if (flags & AT_REMOVEDIR) {
701 ret = SMB_VFS_NEXT_UNLINKAT(handle,
702 dirfsp,
703 smb_fname,
704 flags);
705 } else {
706 ret = recycle_unlink_internal(handle,
707 dirfsp,
708 smb_fname,
709 flags);
711 return ret;
714 static struct vfs_fn_pointers vfs_recycle_fns = {
715 .connect_fn = vfs_recycle_connect,
716 .unlinkat_fn = recycle_unlinkat,
719 static_decl_vfs;
720 NTSTATUS vfs_recycle_init(TALLOC_CTX *ctx)
722 NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle",
723 &vfs_recycle_fns);
725 if (!NT_STATUS_IS_OK(ret))
726 return 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"));
732 } else {
733 DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level));
736 return ret;