s3: VFS: vfs_recycle: Change recycle_unlink_internal() to call UNLINKAT().
[samba.git] / source3 / modules / vfs_recycle.c
1 /*
2  * Recycle bin VFS module for Samba.
3  *
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
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
31 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
32
33 static int vfs_recycle_debug_level = DBGC_VFS;
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS vfs_recycle_debug_level
37  
38 static int recycle_unlink(vfs_handle_struct *handle,
39                           const struct smb_filename *smb_fname);
40
41 static const char *recycle_repository(vfs_handle_struct *handle)
42 {
43         const char *tmp_str = NULL;
44
45         tmp_str = lp_parm_const_string(SNUM(handle->conn), "recycle", "repository",".recycle");
46
47         DEBUG(10, ("recycle: repository = %s\n", tmp_str));
48
49         return tmp_str;
50 }
51
52 static bool recycle_keep_dir_tree(vfs_handle_struct *handle)
53 {
54         bool ret;
55
56         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "keeptree", False);
57
58         DEBUG(10, ("recycle_bin: keeptree = %s\n", ret?"True":"False"));
59
60         return ret;
61 }
62
63 static bool recycle_versions(vfs_handle_struct *handle)
64 {
65         bool ret;
66
67         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "versions", False);
68
69         DEBUG(10, ("recycle: versions = %s\n", ret?"True":"False"));
70
71         return ret;
72 }
73
74 static bool recycle_touch(vfs_handle_struct *handle)
75 {
76         bool ret;
77
78         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch", False);
79
80         DEBUG(10, ("recycle: touch = %s\n", ret?"True":"False"));
81
82         return ret;
83 }
84
85 static bool recycle_touch_mtime(vfs_handle_struct *handle)
86 {
87         bool ret;
88
89         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch_mtime", False);
90
91         DEBUG(10, ("recycle: touch_mtime = %s\n", ret?"True":"False"));
92
93         return ret;
94 }
95
96 static const char **recycle_exclude(vfs_handle_struct *handle)
97 {
98         const char **tmp_lp;
99
100         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude", NULL);
101
102         DEBUG(10, ("recycle: exclude = %s ...\n", tmp_lp?*tmp_lp:""));
103
104         return tmp_lp;
105 }
106
107 static const char **recycle_exclude_dir(vfs_handle_struct *handle)
108 {
109         const char **tmp_lp;
110
111         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude_dir", NULL);
112
113         DEBUG(10, ("recycle: exclude_dir = %s ...\n", tmp_lp?*tmp_lp:""));
114
115         return tmp_lp;
116 }
117
118 static const char **recycle_noversions(vfs_handle_struct *handle)
119 {
120         const char **tmp_lp;
121
122         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "noversions", NULL);
123
124         DEBUG(10, ("recycle: noversions = %s\n", tmp_lp?*tmp_lp:""));
125
126         return tmp_lp;
127 }
128
129 static off_t recycle_maxsize(vfs_handle_struct *handle)
130 {
131         off_t maxsize;
132
133         maxsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
134                                             "recycle", "maxsize", NULL));
135
136         DEBUG(10, ("recycle: maxsize = %lu\n", (long unsigned int)maxsize));
137
138         return maxsize;
139 }
140
141 static off_t recycle_minsize(vfs_handle_struct *handle)
142 {
143         off_t minsize;
144
145         minsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
146                                             "recycle", "minsize", NULL));
147
148         DEBUG(10, ("recycle: minsize = %lu\n", (long unsigned int)minsize));
149
150         return minsize;
151 }
152
153 static mode_t recycle_directory_mode(vfs_handle_struct *handle)
154 {
155         int dirmode;
156         const char *buff;
157
158         buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "directory_mode", NULL);
159
160         if (buff != NULL ) {
161                 sscanf(buff, "%o", &dirmode);
162         } else {
163                 dirmode=S_IRUSR | S_IWUSR | S_IXUSR;
164         }
165
166         DEBUG(10, ("recycle: directory_mode = %o\n", dirmode));
167         return (mode_t)dirmode;
168 }
169
170 static mode_t recycle_subdir_mode(vfs_handle_struct *handle)
171 {
172         int dirmode;
173         const char *buff;
174
175         buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "subdir_mode", NULL);
176
177         if (buff != NULL ) {
178                 sscanf(buff, "%o", &dirmode);
179         } else {
180                 dirmode=recycle_directory_mode(handle);
181         }
182
183         DEBUG(10, ("recycle: subdir_mode = %o\n", dirmode));
184         return (mode_t)dirmode;
185 }
186
187 static bool recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
188 {
189         struct smb_filename smb_fname = {
190                         .base_name = discard_const_p(char, dname)
191         };
192
193         if (SMB_VFS_STAT(handle->conn, &smb_fname) == 0) {
194                 if (S_ISDIR(smb_fname.st.st_ex_mode)) {
195                         return True;
196                 }
197         }
198
199         return False;
200 }
201
202 static bool recycle_file_exist(vfs_handle_struct *handle,
203                                const struct smb_filename *smb_fname)
204 {
205         struct smb_filename *smb_fname_tmp = NULL;
206         bool ret = false;
207
208         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
209         if (smb_fname_tmp == NULL) {
210                 return false;
211         }
212
213         if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) == 0) {
214                 if (S_ISREG(smb_fname_tmp->st.st_ex_mode)) {
215                         ret = true;
216                 }
217         }
218
219         TALLOC_FREE(smb_fname_tmp);
220         return ret;
221 }
222
223 /**
224  * Return file size
225  * @param conn connection
226  * @param fname file name
227  * @return size in bytes
228  **/
229 static off_t recycle_get_file_size(vfs_handle_struct *handle,
230                                        const struct smb_filename *smb_fname)
231 {
232         struct smb_filename *smb_fname_tmp = NULL;
233         off_t size;
234
235         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
236         if (smb_fname_tmp == NULL) {
237                 size = (off_t)0;
238                 goto out;
239         }
240
241         if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
242                 DEBUG(0,("recycle: stat for %s returned %s\n",
243                          smb_fname_str_dbg(smb_fname_tmp), strerror(errno)));
244                 size = (off_t)0;
245                 goto out;
246         }
247
248         size = smb_fname_tmp->st.st_ex_size;
249  out:
250         TALLOC_FREE(smb_fname_tmp);
251         return size;
252 }
253
254 /**
255  * Create directory tree
256  * @param conn connection
257  * @param dname Directory tree to be created
258  * @return Returns True for success
259  **/
260 static bool recycle_create_dir(vfs_handle_struct *handle, const char *dname)
261 {
262         size_t len;
263         mode_t mode;
264         char *new_dir = NULL;
265         char *tmp_str = NULL;
266         char *token;
267         char *tok_str;
268         bool ret = False;
269         char *saveptr;
270
271         mode = recycle_directory_mode(handle);
272
273         tmp_str = SMB_STRDUP(dname);
274         ALLOC_CHECK(tmp_str, done);
275         tok_str = tmp_str;
276
277         len = strlen(dname)+1;
278         new_dir = (char *)SMB_MALLOC(len + 1);
279         ALLOC_CHECK(new_dir, done);
280         *new_dir = '\0';
281         if (dname[0] == '/') {
282                 /* Absolute path. */
283                 if (strlcat(new_dir,"/",len+1) >= len+1) {
284                         goto done;
285                 }
286         }
287
288         /* Create directory tree if neccessary */
289         for(token = strtok_r(tok_str, "/", &saveptr); token;
290             token = strtok_r(NULL, "/", &saveptr)) {
291                 if (strlcat(new_dir, token, len+1) >= len+1) {
292                         goto done;
293                 }
294                 if (recycle_directory_exist(handle, new_dir))
295                         DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
296                 else {
297                         struct smb_filename *smb_fname = NULL;
298                         int retval;
299
300                         DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
301
302                         smb_fname = synthetic_smb_fname(talloc_tos(),
303                                                 new_dir,
304                                                 NULL,
305                                                 NULL,
306                                                 0);
307                         if (smb_fname == NULL) {
308                                 goto done;
309                         }
310
311                         retval = SMB_VFS_NEXT_MKDIRAT(handle,
312                                         handle->conn->cwd_fsp,
313                                         smb_fname,
314                                         mode);
315                         if (retval != 0) {
316                                 DBG_WARNING("recycle: mkdirat failed "
317                                         "for %s with error: %s\n",
318                                         new_dir,
319                                         strerror(errno));
320                                 TALLOC_FREE(smb_fname);
321                                 ret = False;
322                                 goto done;
323                         }
324                         TALLOC_FREE(smb_fname);
325                 }
326                 if (strlcat(new_dir, "/", len+1) >= len+1) {
327                         goto done;
328                 }
329                 mode = recycle_subdir_mode(handle);
330         }
331
332         ret = True;
333 done:
334         SAFE_FREE(tmp_str);
335         SAFE_FREE(new_dir);
336         return ret;
337 }
338
339 /**
340  * Check if any of the components of "exclude_list" are contained in path.
341  * Return True if found
342  **/
343
344 static bool matchdirparam(const char **dir_exclude_list, char *path)
345 {
346         char *startp = NULL, *endp = NULL;
347
348         if (dir_exclude_list == NULL || dir_exclude_list[0] == NULL ||
349                 *dir_exclude_list[0] == '\0' || path == NULL || *path == '\0') {
350                 return False;
351         }
352
353         /* 
354          * Walk the components of path, looking for matches with the
355          * exclude list on each component. 
356          */
357
358         for (startp = path; startp; startp = endp) {
359                 int i;
360
361                 while (*startp == '/') {
362                         startp++;
363                 }
364                 endp = strchr(startp, '/');
365                 if (endp) {
366                         *endp = '\0';
367                 }
368
369                 for(i=0; dir_exclude_list[i] ; i++) {
370                         if(unix_wild_match(dir_exclude_list[i], startp)) {
371                                 /* Repair path. */
372                                 if (endp) {
373                                         *endp = '/';
374                                 }
375                                 return True;
376                         }
377                 }
378
379                 /* Repair path. */
380                 if (endp) {
381                         *endp = '/';
382                 }
383         }
384
385         return False;
386 }
387
388 /**
389  * Check if needle is contained in haystack, * and ? patterns are resolved
390  * @param haystack list of parameters separated by delimimiter character
391  * @param needle string to be matched exectly to haystack including pattern matching
392  * @return True if found
393  **/
394 static bool matchparam(const char **haystack_list, const char *needle)
395 {
396         int i;
397
398         if (haystack_list == NULL || haystack_list[0] == NULL ||
399                 *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') {
400                 return False;
401         }
402
403         for(i=0; haystack_list[i] ; i++) {
404                 if(unix_wild_match(haystack_list[i], needle)) {
405                         return True;
406                 }
407         }
408
409         return False;
410 }
411
412 /**
413  * Touch access or modify date
414  **/
415 static void recycle_do_touch(vfs_handle_struct *handle,
416                              const struct smb_filename *smb_fname,
417                              bool touch_mtime)
418 {
419         struct smb_filename *smb_fname_tmp = NULL;
420         struct smb_file_time ft;
421         int ret, err;
422
423         ZERO_STRUCT(ft);
424
425         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
426         if (smb_fname_tmp == NULL) {
427                 return;
428         }
429
430         if (SMB_VFS_STAT(handle->conn, smb_fname_tmp) != 0) {
431                 DEBUG(0,("recycle: stat for %s returned %s\n",
432                          smb_fname_str_dbg(smb_fname_tmp), strerror(errno)));
433                 goto out;
434         }
435         /* atime */
436         ft.atime = timespec_current();
437         /* mtime */
438         ft.mtime = touch_mtime ? ft.atime : smb_fname_tmp->st.st_ex_mtime;
439
440         become_root();
441         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, &ft);
442         err = errno;
443         unbecome_root();
444         if (ret == -1 ) {
445                 DEBUG(0, ("recycle: touching %s failed, reason = %s\n",
446                           smb_fname_str_dbg(smb_fname_tmp), strerror(err)));
447         }
448  out:
449         TALLOC_FREE(smb_fname_tmp);
450 }
451
452 /**
453  * Check if file should be recycled
454  **/
455 static int recycle_unlink_internal(vfs_handle_struct *handle,
456                                 struct files_struct *dirfsp,
457                                 const struct smb_filename *smb_fname,
458                                 int flags)
459 {
460         connection_struct *conn = handle->conn;
461         char *path_name = NULL;
462         char *temp_name = NULL;
463         char *final_name = NULL;
464         struct smb_filename *smb_fname_final = NULL;
465         const char *base;
466         char *repository = NULL;
467         int i = 1;
468         off_t maxsize, minsize;
469         off_t file_size; /* space_avail;        */
470         bool exist;
471         int rc = -1;
472
473         repository = talloc_sub_advanced(NULL, lp_servicename(talloc_tos(), SNUM(conn)),
474                                         conn->session_info->unix_info->unix_name,
475                                         conn->connectpath,
476                                         conn->session_info->unix_token->gid,
477                                         conn->session_info->unix_info->sanitized_username,
478                                         conn->session_info->info->domain_name,
479                                         recycle_repository(handle));
480         ALLOC_CHECK(repository, done);
481         /* shouldn't we allow absolute path names here? --metze */
482         /* Yes :-). JRA. */
483         trim_char(repository, '\0', '/');
484
485         if(!repository || *(repository) == '\0') {
486                 DEBUG(3, ("recycle: repository path not set, purging %s...\n",
487                           smb_fname_str_dbg(smb_fname)));
488                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
489                                         dirfsp,
490                                         smb_fname,
491                                         flags);
492                 goto done;
493         }
494
495         /* we don't recycle the recycle bin... */
496         if (strncmp(smb_fname->base_name, repository,
497                     strlen(repository)) == 0) {
498                 DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n"));
499                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
500                                         dirfsp,
501                                         smb_fname,
502                                         flags);
503                 goto done;
504         }
505
506         file_size = recycle_get_file_size(handle, smb_fname);
507         /* it is wrong to purge filenames only because they are empty imho
508          *   --- simo
509          *
510         if(fsize == 0) {
511                 DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
512                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
513                                         dirfsp,
514                                         file_name,
515                                         flags);
516                 goto done;
517         }
518          */
519
520         /* FIXME: this is wrong, we should check the whole size of the recycle bin is
521          * not greater then maxsize, not the size of the single file, also it is better
522          * to remove older files
523          */
524         maxsize = recycle_maxsize(handle);
525         if(maxsize > 0 && file_size > maxsize) {
526                 DEBUG(3, ("recycle: File %s exceeds maximum recycle size, "
527                           "purging... \n", smb_fname_str_dbg(smb_fname)));
528                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
529                                         dirfsp,
530                                         smb_fname,
531                                         flags);
532                 goto done;
533         }
534         minsize = recycle_minsize(handle);
535         if(minsize > 0 && file_size < minsize) {
536                 DEBUG(3, ("recycle: File %s lowers minimum recycle size, "
537                           "purging... \n", smb_fname_str_dbg(smb_fname)));
538                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
539                                         dirfsp,
540                                         smb_fname,
541                                         flags);
542                 goto done;
543         }
544
545         /* FIXME: this is wrong: moving files with rename does not change the disk space
546          * allocation
547          *
548         space_avail = SMB_VFS_NEXT_DISK_FREE(handle, ".", True, &bsize, &dfree, &dsize) * 1024L;
549         DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
550         if(space_avail < file_size) {
551                 DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name));
552                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
553                                         dirfsp,
554                                         file_name,
555                                         flags);
556                 goto done;
557         }
558          */
559
560         /* extract filename and path */
561         base = strrchr(smb_fname->base_name, '/');
562         if (base == NULL) {
563                 base = smb_fname->base_name;
564                 path_name = SMB_STRDUP("/");
565                 ALLOC_CHECK(path_name, done);
566         }
567         else {
568                 path_name = SMB_STRDUP(smb_fname->base_name);
569                 ALLOC_CHECK(path_name, done);
570                 path_name[base - smb_fname->base_name] = '\0';
571                 base++;
572         }
573
574         /* original filename with path */
575         DEBUG(10, ("recycle: fname = %s\n", smb_fname_str_dbg(smb_fname)));
576         /* original path */
577         DEBUG(10, ("recycle: fpath = %s\n", path_name));
578         /* filename without path */
579         DEBUG(10, ("recycle: base = %s\n", base));
580
581         if (matchparam(recycle_exclude(handle), base)) {
582                 DEBUG(3, ("recycle: file %s is excluded \n", base));
583                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
584                                         dirfsp,
585                                         smb_fname,
586                                         flags);
587                 goto done;
588         }
589
590         if (matchdirparam(recycle_exclude_dir(handle), path_name)) {
591                 DEBUG(3, ("recycle: directory %s is excluded \n", path_name));
592                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
593                                         dirfsp,
594                                         smb_fname,
595                                         flags);
596                 goto done;
597         }
598
599         if (recycle_keep_dir_tree(handle) == True) {
600                 if (asprintf(&temp_name, "%s/%s", repository, path_name) == -1) {
601                         ALLOC_CHECK(temp_name, done);
602                 }
603         } else {
604                 temp_name = SMB_STRDUP(repository);
605         }
606         ALLOC_CHECK(temp_name, done);
607
608         exist = recycle_directory_exist(handle, temp_name);
609         if (exist) {
610                 DEBUG(10, ("recycle: Directory already exists\n"));
611         } else {
612                 DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
613                 if (recycle_create_dir(handle, temp_name) == False) {
614                         DEBUG(3, ("recycle: Could not create directory, "
615                                   "purging %s...\n",
616                                   smb_fname_str_dbg(smb_fname)));
617                         rc = SMB_VFS_NEXT_UNLINKAT(handle,
618                                         dirfsp,
619                                         smb_fname,
620                                         flags);
621                         goto done;
622                 }
623         }
624
625         if (asprintf(&final_name, "%s/%s", temp_name, base) == -1) {
626                 ALLOC_CHECK(final_name, done);
627         }
628
629         /* Create smb_fname with final base name and orig stream name. */
630         smb_fname_final = synthetic_smb_fname(talloc_tos(),
631                                         final_name,
632                                         smb_fname->stream_name,
633                                         NULL,
634                                         smb_fname->flags);
635         if (smb_fname_final == NULL) {
636                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
637                                         dirfsp,
638                                         smb_fname,
639                                         flags);
640                 goto done;
641         }
642
643         /* new filename with path */
644         DEBUG(10, ("recycle: recycled file name: %s\n",
645                    smb_fname_str_dbg(smb_fname_final)));
646
647         /* check if we should delete file from recycle bin */
648         if (recycle_file_exist(handle, smb_fname_final)) {
649                 if (recycle_versions(handle) == False || matchparam(recycle_noversions(handle), base) == True) {
650                         DEBUG(3, ("recycle: Removing old file %s from recycle "
651                                   "bin\n", smb_fname_str_dbg(smb_fname_final)));
652                         if (SMB_VFS_NEXT_UNLINKAT(handle,
653                                                 dirfsp,
654                                                 smb_fname_final,
655                                                 flags) != 0) {
656                                 DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
657                         }
658                 }
659         }
660
661         /* rename file we move to recycle bin */
662         i = 1;
663         while (recycle_file_exist(handle, smb_fname_final)) {
664                 SAFE_FREE(final_name);
665                 if (asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base) == -1) {
666                         ALLOC_CHECK(final_name, done);
667                 }
668                 TALLOC_FREE(smb_fname_final->base_name);
669                 smb_fname_final->base_name = talloc_strdup(smb_fname_final,
670                                                            final_name);
671                 if (smb_fname_final->base_name == NULL) {
672                         rc = SMB_VFS_NEXT_UNLINKAT(handle,
673                                                 dirfsp,
674                                                 smb_fname,
675                                                 flags);
676                         goto done;
677                 }
678         }
679
680         DEBUG(10, ("recycle: Moving %s to %s\n", smb_fname_str_dbg(smb_fname),
681                 smb_fname_str_dbg(smb_fname_final)));
682         rc = SMB_VFS_NEXT_RENAMEAT(handle,
683                         handle->conn->cwd_fsp,
684                         smb_fname,
685                         handle->conn->cwd_fsp,
686                         smb_fname_final);
687         if (rc != 0) {
688                 DEBUG(3, ("recycle: Move error %d (%s), purging file %s "
689                           "(%s)\n", errno, strerror(errno),
690                           smb_fname_str_dbg(smb_fname),
691                           smb_fname_str_dbg(smb_fname_final)));
692                 rc = SMB_VFS_NEXT_UNLINKAT(handle,
693                                 dirfsp,
694                                 smb_fname,
695                                 flags);
696                 goto done;
697         }
698
699         /* touch access date of moved file */
700         if (recycle_touch(handle) == True || recycle_touch_mtime(handle))
701                 recycle_do_touch(handle, smb_fname_final,
702                                  recycle_touch_mtime(handle));
703
704 done:
705         SAFE_FREE(path_name);
706         SAFE_FREE(temp_name);
707         SAFE_FREE(final_name);
708         TALLOC_FREE(smb_fname_final);
709         TALLOC_FREE(repository);
710         return rc;
711 }
712
713 static int recycle_unlink(vfs_handle_struct *handle,
714                                 const struct smb_filename *smb_fname)
715 {
716         return recycle_unlink_internal(handle,
717                                 handle->conn->cwd_fsp,
718                                 smb_fname,
719                                 0);
720 }
721
722 static int recycle_unlinkat(vfs_handle_struct *handle,
723                 struct files_struct *dirfsp,
724                 const struct smb_filename *smb_fname,
725                 int flags)
726 {
727         int ret;
728
729         if (flags & AT_REMOVEDIR) {
730                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
731                                         dirfsp,
732                                         smb_fname,
733                                         flags);
734         } else {
735                 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
736                 ret = recycle_unlink_internal(handle,
737                                         dirfsp,
738                                         smb_fname,
739                                         flags);
740         }
741         return ret;
742 }
743
744 static struct vfs_fn_pointers vfs_recycle_fns = {
745         .unlink_fn = recycle_unlink,
746         .unlinkat_fn = recycle_unlinkat
747 };
748
749 static_decl_vfs;
750 NTSTATUS vfs_recycle_init(TALLOC_CTX *ctx)
751 {
752         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle",
753                                         &vfs_recycle_fns);
754
755         if (!NT_STATUS_IS_OK(ret))
756                 return ret;
757
758         vfs_recycle_debug_level = debug_add_class("recycle");
759         if (vfs_recycle_debug_level == -1) {
760                 vfs_recycle_debug_level = DBGC_VFS;
761                 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
762         } else {
763                 DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level));
764         }
765
766         return ret;
767 }