r22080: Fix directory recycle module bug #4486.
[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 2 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, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include "includes.h"
27
28 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
29
30 static int vfs_recycle_debug_level = DBGC_VFS;
31
32 #undef DBGC_CLASS
33 #define DBGC_CLASS vfs_recycle_debug_level
34  
35 static int recycle_connect(vfs_handle_struct *handle, const char *service, const char *user);
36 static void recycle_disconnect(vfs_handle_struct *handle);
37 static int recycle_unlink(vfs_handle_struct *handle, const char *name);
38
39 static vfs_op_tuple recycle_ops[] = {
40
41         /* Disk operations */
42         {SMB_VFS_OP(recycle_connect),   SMB_VFS_OP_CONNECT,     SMB_VFS_LAYER_TRANSPARENT},
43         {SMB_VFS_OP(recycle_disconnect),        SMB_VFS_OP_DISCONNECT,  SMB_VFS_LAYER_TRANSPARENT},
44
45         /* File operations */
46         {SMB_VFS_OP(recycle_unlink),    SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_TRANSPARENT},
47
48         {SMB_VFS_OP(NULL),              SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
49 };
50
51 static int recycle_connect(vfs_handle_struct *handle, const char *service, const char *user)
52 {
53         DEBUG(10,("recycle_connect() connect to service[%s] as user[%s].\n",
54                 service,user));
55
56         return SMB_VFS_NEXT_CONNECT(handle, service, user);
57 }
58
59 static void recycle_disconnect(vfs_handle_struct *handle)
60 {
61         DEBUG(10,("recycle_disconnect() connect to service[%s].\n",
62                 lp_servicename(SNUM(handle->conn))));
63
64         SMB_VFS_NEXT_DISCONNECT(handle);
65 }
66
67 static const char *recycle_repository(vfs_handle_struct *handle)
68 {
69         const char *tmp_str = NULL;
70         
71
72         tmp_str = lp_parm_const_string(SNUM(handle->conn), "recycle", "repository",".recycle");
73
74         DEBUG(10, ("recycle: repository = %s\n", tmp_str));
75         
76         return tmp_str;
77 }
78
79 static BOOL recycle_keep_dir_tree(vfs_handle_struct *handle)
80 {
81         BOOL ret;
82         
83         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "keeptree", False);
84
85         DEBUG(10, ("recycle_bin: keeptree = %s\n", ret?"True":"False"));
86         
87         return ret;
88 }
89
90 static BOOL recycle_versions(vfs_handle_struct *handle)
91 {
92         BOOL ret;
93
94         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "versions", False);
95
96         DEBUG(10, ("recycle: versions = %s\n", ret?"True":"False"));
97         
98         return ret;
99 }
100
101 static BOOL recycle_touch(vfs_handle_struct *handle)
102 {
103         BOOL ret;
104
105         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch", False);
106
107         DEBUG(10, ("recycle: touch = %s\n", ret?"True":"False"));
108         
109         return ret;
110 }
111
112 static BOOL recycle_touch_mtime(vfs_handle_struct *handle)
113 {
114         BOOL ret;
115
116         ret = lp_parm_bool(SNUM(handle->conn), "recycle", "touch_mtime", False);
117
118         DEBUG(10, ("recycle: touch_mtime = %s\n", ret?"True":"False"));
119         
120         return ret;
121 }
122
123 static const char **recycle_exclude(vfs_handle_struct *handle)
124 {
125         const char **tmp_lp;
126         
127         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude", NULL);
128
129         DEBUG(10, ("recycle: exclude = %s ...\n", tmp_lp?*tmp_lp:""));
130         
131         return tmp_lp;
132 }
133
134 static const char **recycle_exclude_dir(vfs_handle_struct *handle)
135 {
136         const char **tmp_lp;
137         
138         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "exclude_dir", NULL);
139
140         DEBUG(10, ("recycle: exclude_dir = %s ...\n", tmp_lp?*tmp_lp:""));
141         
142         return tmp_lp;
143 }
144
145 static const char **recycle_noversions(vfs_handle_struct *handle)
146 {
147         const char **tmp_lp;
148         
149         tmp_lp = lp_parm_string_list(SNUM(handle->conn), "recycle", "noversions", NULL);
150
151         DEBUG(10, ("recycle: noversions = %s\n", tmp_lp?*tmp_lp:""));
152         
153         return tmp_lp;
154 }
155
156 static SMB_OFF_T recycle_maxsize(vfs_handle_struct *handle)
157 {
158         SMB_OFF_T maxsize;
159         
160         maxsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
161                                             "recycle", "maxsize", NULL));
162
163         DEBUG(10, ("recycle: maxsize = %lu\n", (long unsigned int)maxsize));
164         
165         return maxsize;
166 }
167
168 static SMB_OFF_T recycle_minsize(vfs_handle_struct *handle)
169 {
170         SMB_OFF_T minsize;
171         
172         minsize = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
173                                             "recycle", "minsize", NULL));
174
175         DEBUG(10, ("recycle: minsize = %lu\n", (long unsigned int)minsize));
176         
177         return minsize;
178 }
179
180 static mode_t recycle_directory_mode(vfs_handle_struct *handle)
181 {
182         int dirmode;
183         const char *buff;
184
185         buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "directory_mode", NULL);
186
187         if (buff != NULL ) {
188                 sscanf(buff, "%o", &dirmode);
189         } else {
190                 dirmode=S_IRUSR | S_IWUSR | S_IXUSR;
191         }
192
193         DEBUG(10, ("recycle: directory_mode = %o\n", dirmode));
194         return (mode_t)dirmode;
195 }
196
197 static mode_t recycle_subdir_mode(vfs_handle_struct *handle)
198 {
199         int dirmode;
200         const char *buff;
201
202         buff = lp_parm_const_string(SNUM(handle->conn), "recycle", "subdir_mode", NULL);
203
204         if (buff != NULL ) {
205                 sscanf(buff, "%o", &dirmode);
206         } else {
207                 dirmode=recycle_directory_mode(handle);
208         }
209
210         DEBUG(10, ("recycle: subdir_mode = %o\n", dirmode));
211         return (mode_t)dirmode;
212 }
213
214 static BOOL recycle_directory_exist(vfs_handle_struct *handle, const char *dname)
215 {
216         SMB_STRUCT_STAT st;
217
218         if (SMB_VFS_NEXT_STAT(handle, dname, &st) == 0) {
219                 if (S_ISDIR(st.st_mode)) {
220                         return True;
221                 }
222         }
223
224         return False;
225 }
226
227 static BOOL recycle_file_exist(vfs_handle_struct *handle, const char *fname)
228 {
229         SMB_STRUCT_STAT st;
230
231         if (SMB_VFS_NEXT_STAT(handle, fname, &st) == 0) {
232                 if (S_ISREG(st.st_mode)) {
233                         return True;
234                 }
235         }
236
237         return False;
238 }
239
240 /**
241  * Return file size
242  * @param conn connection
243  * @param fname file name
244  * @return size in bytes
245  **/
246 static SMB_OFF_T recycle_get_file_size(vfs_handle_struct *handle, const char *fname)
247 {
248         SMB_STRUCT_STAT st;
249
250         if (SMB_VFS_NEXT_STAT(handle, fname, &st) != 0) {
251                 DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno)));
252                 return (SMB_OFF_T)0;
253         }
254
255         return(st.st_size);
256 }
257
258 /**
259  * Create directory tree
260  * @param conn connection
261  * @param dname Directory tree to be created
262  * @return Returns True for success
263  **/
264 static BOOL recycle_create_dir(vfs_handle_struct *handle, const char *dname)
265 {
266         size_t len;
267         mode_t mode;
268         char *new_dir = NULL;
269         char *tmp_str = NULL;
270         char *token;
271         char *tok_str;
272         BOOL ret = False;
273
274         mode = recycle_directory_mode(handle);
275
276         tmp_str = SMB_STRDUP(dname);
277         ALLOC_CHECK(tmp_str, done);
278         tok_str = tmp_str;
279
280         len = strlen(dname)+1;
281         new_dir = (char *)SMB_MALLOC(len + 1);
282         ALLOC_CHECK(new_dir, done);
283         *new_dir = '\0';
284         if (dname[0] == '/') {
285                 /* Absolute path. */
286                 safe_strcat(new_dir,"/",len);
287         }
288
289         /* Create directory tree if neccessary */
290         for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) {
291                 safe_strcat(new_dir, token, len);
292                 if (recycle_directory_exist(handle, new_dir))
293                         DEBUG(10, ("recycle: dir %s already exists\n", new_dir));
294                 else {
295                         DEBUG(5, ("recycle: creating new dir %s\n", new_dir));
296                         if (SMB_VFS_NEXT_MKDIR(handle, new_dir, mode) != 0) {
297                                 DEBUG(1,("recycle: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
298                                 ret = False;
299                                 goto done;
300                         }
301                 }
302                 safe_strcat(new_dir, "/", len);
303                 mode = recycle_subdir_mode(handle);
304         }
305
306         ret = True;
307 done:
308         SAFE_FREE(tmp_str);
309         SAFE_FREE(new_dir);
310         return ret;
311 }
312
313 /**
314  * Check if any of the components of "exclude_list" are contained in path.
315  * Return True if found
316  **/
317
318 static BOOL matchdirparam(const char **dir_exclude_list, char *path)
319 {
320         char *startp = NULL, *endp = NULL;
321
322         if (dir_exclude_list == NULL || dir_exclude_list[0] == NULL ||
323                 *dir_exclude_list[0] == '\0' || path == NULL || *path == '\0') {
324                 return False;
325         }
326
327         /* 
328          * Walk the components of path, looking for matches with the
329          * exclude list on each component. 
330          */
331
332         for (startp = path; startp; startp = endp) {
333                 int i;
334
335                 while (*startp == '/') {
336                         startp++;
337                 }
338                 endp = strchr(startp, '/');
339                 if (endp) {
340                         *endp = '\0';
341                 }
342
343                 for(i=0; dir_exclude_list[i] ; i++) {
344                         if(unix_wild_match(dir_exclude_list[i], startp)) {
345                                 /* Repair path. */
346                                 if (endp) {
347                                         *endp = '/';
348                                 }
349                                 return True;
350                         }
351                 }
352
353                 /* Repair path. */
354                 if (endp) {
355                         *endp = '/';
356                 }
357         }
358
359         return False;
360 }
361
362 /**
363  * Check if needle is contained in haystack, * and ? patterns are resolved
364  * @param haystack list of parameters separated by delimimiter character
365  * @param needle string to be matched exectly to haystack including pattern matching
366  * @return True if found
367  **/
368 static BOOL matchparam(const char **haystack_list, const char *needle)
369 {
370         int i;
371
372         if (haystack_list == NULL || haystack_list[0] == NULL ||
373                 *haystack_list[0] == '\0' || needle == NULL || *needle == '\0') {
374                 return False;
375         }
376
377         for(i=0; haystack_list[i] ; i++) {
378                 if(unix_wild_match(haystack_list[i], needle)) {
379                         return True;
380                 }
381         }
382
383         return False;
384 }
385
386 /**
387  * Touch access or modify date
388  **/
389 static void recycle_do_touch(vfs_handle_struct *handle, const char *fname, BOOL touch_mtime)
390 {
391         SMB_STRUCT_STAT st;
392         struct timespec ts[2];
393         
394         if (SMB_VFS_NEXT_STAT(handle, fname, &st) != 0) {
395                 DEBUG(0,("recycle: stat for %s returned %s\n", fname, strerror(errno)));
396                 return;
397         }
398         ts[0] = timespec_current(); /* atime */
399         ts[1] = touch_mtime ? ts[0] : get_mtimespec(&st); /* mtime */
400
401         if (SMB_VFS_NEXT_NTIMES(handle, fname, ts) == -1 ) {
402                 DEBUG(0, ("recycle: touching %s failed, reason = %s\n", fname, strerror(errno)));
403         }
404 }
405
406 extern userdom_struct current_user_info;
407
408 /**
409  * Check if file should be recycled
410  **/
411 static int recycle_unlink(vfs_handle_struct *handle, const char *file_name)
412 {
413         connection_struct *conn = handle->conn;
414         char *path_name = NULL;
415         char *temp_name = NULL;
416         char *final_name = NULL;
417         const char *base;
418         char *repository = NULL;
419         int i = 1;
420         SMB_OFF_T maxsize, minsize;
421         SMB_OFF_T file_size; /* space_avail;    */
422         BOOL exist;
423         int rc = -1;
424
425         repository = talloc_sub_advanced(NULL, lp_servicename(SNUM(conn)),
426                                         conn->user,
427                                         conn->connectpath, conn->gid,
428                                         get_current_username(),
429                                         current_user_info.domain,
430                                         recycle_repository(handle));
431         ALLOC_CHECK(repository, done);
432         /* shouldn't we allow absolute path names here? --metze */
433         /* Yes :-). JRA. */
434         trim_char(repository, '\0', '/');
435         
436         if(!repository || *(repository) == '\0') {
437                 DEBUG(3, ("recycle: repository path not set, purging %s...\n", file_name));
438                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
439                 goto done;
440         }
441
442         /* we don't recycle the recycle bin... */
443         if (strncmp(file_name, repository, strlen(repository)) == 0) {
444                 DEBUG(3, ("recycle: File is within recycling bin, unlinking ...\n"));
445                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
446                 goto done;
447         }
448
449         file_size = recycle_get_file_size(handle, file_name);
450         /* it is wrong to purge filenames only because they are empty imho
451          *   --- simo
452          *
453         if(fsize == 0) {
454                 DEBUG(3, ("recycle: File %s is empty, purging...\n", file_name));
455                 rc = SMB_VFS_NEXT_UNLINK(handle,file_name);
456                 goto done;
457         }
458          */
459
460         /* FIXME: this is wrong, we should check the whole size of the recycle bin is
461          * not greater then maxsize, not the size of the single file, also it is better
462          * to remove older files
463          */
464         maxsize = recycle_maxsize(handle);
465         if(maxsize > 0 && file_size > maxsize) {
466                 DEBUG(3, ("recycle: File %s exceeds maximum recycle size, purging... \n", file_name));
467                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
468                 goto done;
469         }
470         minsize = recycle_minsize(handle);
471         if(minsize > 0 && file_size < minsize) {
472                 DEBUG(3, ("recycle: File %s lowers minimum recycle size, purging... \n", file_name));
473                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
474                 goto done;
475         }
476
477         /* FIXME: this is wrong: moving files with rename does not change the disk space
478          * allocation
479          *
480         space_avail = SMB_VFS_NEXT_DISK_FREE(handle, ".", True, &bsize, &dfree, &dsize) * 1024L;
481         DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
482         if(space_avail < file_size) {
483                 DEBUG(3, ("recycle: Not enough diskspace, purging file %s\n", file_name));
484                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
485                 goto done;
486         }
487          */
488
489         /* extract filename and path */
490         base = strrchr(file_name, '/');
491         if (base == NULL) {
492                 base = file_name;
493                 path_name = SMB_STRDUP("/");
494                 ALLOC_CHECK(path_name, done);
495         }
496         else {
497                 path_name = SMB_STRDUP(file_name);
498                 ALLOC_CHECK(path_name, done);
499                 path_name[base - file_name] = '\0';
500                 base++;
501         }
502
503         DEBUG(10, ("recycle: fname = %s\n", file_name));        /* original filename with path */
504         DEBUG(10, ("recycle: fpath = %s\n", path_name));        /* original path */
505         DEBUG(10, ("recycle: base = %s\n", base));              /* filename without path */
506
507         if (matchparam(recycle_exclude(handle), base)) {
508                 DEBUG(3, ("recycle: file %s is excluded \n", base));
509                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
510                 goto done;
511         }
512
513         if (matchdirparam(recycle_exclude_dir(handle), path_name)) {
514                 DEBUG(3, ("recycle: directory %s is excluded \n", path_name));
515                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
516                 goto done;
517         }
518
519         if (recycle_keep_dir_tree(handle) == True) {
520                 asprintf(&temp_name, "%s/%s", repository, path_name);
521         } else {
522                 temp_name = SMB_STRDUP(repository);
523         }
524         ALLOC_CHECK(temp_name, done);
525
526         exist = recycle_directory_exist(handle, temp_name);
527         if (exist) {
528                 DEBUG(10, ("recycle: Directory already exists\n"));
529         } else {
530                 DEBUG(10, ("recycle: Creating directory %s\n", temp_name));
531                 if (recycle_create_dir(handle, temp_name) == False) {
532                         DEBUG(3, ("recycle: Could not create directory, purging %s...\n", file_name));
533                         rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
534                         goto done;
535                 }
536         }
537
538         asprintf(&final_name, "%s/%s", temp_name, base);
539         ALLOC_CHECK(final_name, done);
540         DEBUG(10, ("recycle: recycled file name: %s\n", final_name));           /* new filename with path */
541
542         /* check if we should delete file from recycle bin */
543         if (recycle_file_exist(handle, final_name)) {
544                 if (recycle_versions(handle) == False || matchparam(recycle_noversions(handle), base) == True) {
545                         DEBUG(3, ("recycle: Removing old file %s from recycle bin\n", final_name));
546                         if (SMB_VFS_NEXT_UNLINK(handle, final_name) != 0) {
547                                 DEBUG(1, ("recycle: Error deleting old file: %s\n", strerror(errno)));
548                         }
549                 }
550         }
551
552         /* rename file we move to recycle bin */
553         i = 1;
554         while (recycle_file_exist(handle, final_name)) {
555                 SAFE_FREE(final_name);
556                 asprintf(&final_name, "%s/Copy #%d of %s", temp_name, i++, base);
557         }
558
559         DEBUG(10, ("recycle: Moving %s to %s\n", file_name, final_name));
560         rc = SMB_VFS_NEXT_RENAME(handle, file_name, final_name);
561         if (rc != 0) {
562                 DEBUG(3, ("recycle: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name));
563                 rc = SMB_VFS_NEXT_UNLINK(handle, file_name);
564                 goto done;
565         }
566
567         /* touch access date of moved file */
568         if (recycle_touch(handle) == True || recycle_touch_mtime(handle))
569                 recycle_do_touch(handle, final_name, recycle_touch_mtime(handle));
570
571 done:
572         SAFE_FREE(path_name);
573         SAFE_FREE(temp_name);
574         SAFE_FREE(final_name);
575         TALLOC_FREE(repository);
576         return rc;
577 }
578
579 NTSTATUS vfs_recycle_init(void);
580 NTSTATUS vfs_recycle_init(void)
581 {
582         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "recycle", recycle_ops);
583
584         if (!NT_STATUS_IS_OK(ret))
585                 return ret;
586         
587         vfs_recycle_debug_level = debug_add_class("recycle");
588         if (vfs_recycle_debug_level == -1) {
589                 vfs_recycle_debug_level = DBGC_VFS;
590                 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
591         } else {
592                 DEBUG(10, ("vfs_recycle: Debug class number of 'recycle': %d\n", vfs_recycle_debug_level));
593         }
594         
595         return ret;
596 }