Store printer guid in the dsspooler registry key so we don't have to
[Samba/gebeck_regimport.git] / source / modules / vfs_recycle.c
blobb59cb92a28f561fdadf0e6262535cfc48a021ed1
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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
29 static int vfs_recycle_debug_level = DBGC_VFS;
31 #undef DBGC_CLASS
32 #define DBGC_CLASS vfs_recycle_debug_level
34 static const char *delimiter = "|"; /* delimiter for options */
36 /* One per connection */
38 typedef struct recycle_bin_struct
40 TALLOC_CTX *ctx;
41 char *repository; /* name of the recycle bin directory */
42 BOOL keep_dir_tree; /* keep directory structure of deleted file in recycle bin */
43 BOOL versions; /* create versions of deleted files with identical name */
44 BOOL touch; /* touch access date of deleted file */
45 char *exclude; /* which files to exclude */
46 char *exclude_dir; /* which directories to exclude */
47 char *noversions; /* which files to exclude from versioning */
48 SMB_OFF_T maxsize; /* maximum file size to be saved */
49 } recycle_bin_struct;
51 /* VFS operations */
52 static struct vfs_ops default_vfs_ops; /* For passthrough operation */
54 static int recycle_connect(struct connection_struct *conn, const char *service, const char *user);
55 static void recycle_disconnect(struct connection_struct *conn);
56 static int recycle_unlink(connection_struct *, const char *);
58 #define VFS_OP(x) ((void *) x)
60 static vfs_op_tuple recycle_ops[] = {
62 /* Disk operations */
63 {VFS_OP(recycle_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
64 {VFS_OP(recycle_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_TRANSPARENT},
66 /* File operations */
67 {VFS_OP(recycle_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
69 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
72 static BOOL check_bool_param(const char *value)
74 if (strwicmp(value, "yes") == 0 ||
75 strwicmp(value, "true") == 0 ||
76 strwicmp(value, "1") == 0)
77 return True;
79 return False;
82 /**
83 * VFS initialisation function.
85 * @retval initialised vfs_op_tuple array
86 **/
87 vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops,
88 struct smb_vfs_handle_struct *vfs_handle)
90 DEBUG(10, ("Initializing VFS module recycle\n"));
91 *vfs_version = SMB_VFS_INTERFACE_VERSION;
92 memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
93 vfs_recycle_debug_level = debug_add_class("vfs_recycle_bin");
94 if (vfs_recycle_debug_level == -1) {
95 vfs_recycle_debug_level = DBGC_VFS;
96 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
97 } else {
98 DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level));
101 return recycle_ops;
105 * VFS finalization function.
108 void vfs_done(connection_struct *conn)
110 DEBUG(10,("Called for connection %d\n", SNUM(conn)));
113 static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)
115 TALLOC_CTX *ctx = NULL;
116 recycle_bin_struct *recbin;
117 char *servicename;
118 char *tmp_str;
120 DEBUG(10, ("Called for service %s (%d) as user %s\n", service, SNUM(conn), user));
122 if (!(ctx = talloc_init_named("recycle bin"))) {
123 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
124 return 0;
127 recbin = talloc(ctx,sizeof(recycle_bin_struct));
128 if ( recbin == NULL) {
129 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
130 return -1;
132 recbin->ctx = ctx;
134 /* Set defaults */
135 recbin->repository = talloc_strdup(ctx, ".recycle");
136 ALLOC_CHECK(recbin->repository, error);
137 recbin->keep_dir_tree = False;
138 recbin->versions = False;
139 recbin->touch = False;
140 recbin->exclude = "";
141 recbin->exclude_dir = "";
142 recbin->noversions = "";
143 recbin->maxsize = 0;
145 /* parse configuration options */
146 servicename = talloc_strdup(recbin->ctx, lp_servicename(SNUM(conn)));
147 DEBUG(10, ("servicename = %s\n",servicename));
148 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "repository")) != NULL) {
149 recbin->repository = talloc_sub_conn(ctx, conn, tmp_str);
150 ALLOC_CHECK(recbin->repository, error);
151 trim_string(recbin->repository, "/", "/");
152 DEBUG(5, ("recycle.bin: repository = %s\n", recbin->repository));
154 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "keeptree")) != NULL) {
155 if (check_bool_param(tmp_str) == True)
156 recbin->keep_dir_tree = True;
157 DEBUG(5, ("recycle.bin: keeptree = %s\n", tmp_str));
159 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "versions")) != NULL) {
160 if (check_bool_param(tmp_str) == True)
161 recbin->versions = True;
162 DEBUG(5, ("recycle.bin: versions = %s\n", tmp_str));
164 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "touch")) != NULL) {
165 if (check_bool_param(tmp_str) == True)
166 recbin->touch = True;
167 DEBUG(5, ("recycle.bin: touch = %s\n", tmp_str));
169 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "maxsize")) != NULL) {
170 recbin->maxsize = strtoul(tmp_str, NULL, 10);
171 if (recbin->maxsize == 0) {
172 recbin->maxsize = -1;
173 DEBUG(5, ("recycle.bin: maxsize = -infinite-\n"));
174 } else {
175 DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin->maxsize));
178 if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "exclude")) != NULL) {
179 recbin->exclude = talloc_strdup(ctx, tmp_str);
180 ALLOC_CHECK(recbin->exclude, error);
181 DEBUG(5, ("recycle.bin: exclude = %s\n", recbin->exclude));
183 if ((tmp_str = lp_parm_string(servicename,"vfs_recycle_bin", "exclude_dir")) != NULL) {
184 recbin->exclude_dir = talloc_strdup(ctx, tmp_str);
185 ALLOC_CHECK(recbin->exclude_dir, error);
186 DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin->exclude_dir));
188 if ((tmp_str = lp_parm_string(servicename,"vfs_recycle_bin", "noversions")) != NULL) {
189 recbin->noversions = talloc_strdup(ctx, tmp_str);
190 ALLOC_CHECK(recbin->noversions, error);
191 DEBUG(5, ("recycle.bin: noversions = %s\n", recbin->noversions));
194 conn->vfs_private = (void *)recbin;
195 return default_vfs_ops.connect(conn, service, user);
197 error:
198 talloc_destroy(ctx);
199 return -1;
202 static void recycle_disconnect(struct connection_struct *conn)
204 DEBUG(10, ("Disconnecting VFS module recycle bin\n"));
205 if (conn->vfs_private) {
206 talloc_destroy(((recycle_bin_struct *)conn->vfs_private)->ctx);
207 conn->vfs_private = NULL;
209 default_vfs_ops.disconnect(conn);
212 static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)
214 SMB_STRUCT_STAT st;
216 if (default_vfs_ops.stat(conn, dname, &st) == 0) {
217 if (S_ISDIR(st.st_mode)) {
218 return True;
222 return False;
225 static BOOL recycle_file_exist(connection_struct *conn, const char *fname)
227 SMB_STRUCT_STAT st;
229 if (default_vfs_ops.stat(conn, fname, &st) == 0) {
230 if (S_ISREG(st.st_mode)) {
231 return True;
235 return False;
239 * Return file size
240 * @param conn connection
241 * @param fname file name
242 * @return size in bytes
244 static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)
246 SMB_STRUCT_STAT st;
247 if (default_vfs_ops.stat(conn, fname, &st) != 0) {
248 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
249 return (SMB_OFF_T)0;
251 return(st.st_size);
255 * Create directory tree
256 * @param conn connection
257 * @param dname Directory tree to be created
258 * @return Returns True for success
260 static BOOL recycle_create_dir(connection_struct *conn, const char *dname)
262 int 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;
270 mode = S_IREAD | S_IWRITE | S_IEXEC;
272 tmp_str = strdup(dname);
273 ALLOC_CHECK(tmp_str, done);
274 tok_str = tmp_str;
276 len = strlen(dname);
277 new_dir = (char *)malloc(len + 1);
278 ALLOC_CHECK(new_dir, done);
279 *new_dir = '\0';
281 /* Create directory tree if neccessary */
282 for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) {
283 safe_strcat(new_dir, token, len);
284 if (recycle_directory_exist(conn, new_dir))
285 DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir));
286 else {
287 DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir));
288 if (default_vfs_ops.mkdir(conn, new_dir, mode) != 0) {
289 DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
290 ret = False;
291 goto done;
294 safe_strcat(new_dir, "/", len);
297 ret = True;
298 done:
299 SAFE_FREE(tmp_str);
300 SAFE_FREE(new_dir);
301 return ret;
305 * Check if needle is contained exactly in haystack
306 * @param haystack list of parameters separated by delimimiter character
307 * @param needle string to be matched exactly to haystack
308 * @return True if found
310 static BOOL checkparam(const char *haystack, const char *needle)
312 char *token;
313 char *tok_str;
314 char *tmp_str;
315 BOOL ret = False;
317 if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
318 return False;
321 tmp_str = strdup(haystack);
322 ALLOC_CHECK(tmp_str, done);
323 token = tok_str = tmp_str;
325 for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
326 if(strcmp(token, needle) == 0) {
327 ret = True;
328 goto done;
331 done:
332 SAFE_FREE(tmp_str);
333 return ret;
337 * Check if needle is contained in haystack, * and ? patterns are resolved
338 * @param haystack list of parameters separated by delimimiter character
339 * @param needle string to be matched exectly to haystack including pattern matching
340 * @return True if found
342 static BOOL matchparam(const char *haystack, const char *needle)
344 char *token;
345 char *tok_str;
346 char *tmp_str;
347 BOOL ret = False;
349 if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
350 return False;
353 tmp_str = strdup(haystack);
354 ALLOC_CHECK(tmp_str, done);
355 token = tok_str = tmp_str;
357 for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
358 if (!unix_wild_match(token, needle)) {
359 ret = True;
360 goto done;
363 done:
364 SAFE_FREE(tmp_str);
365 return ret;
369 * Touch access date
371 static void recycle_touch(connection_struct *conn, const char *fname)
373 SMB_STRUCT_STAT st;
374 struct utimbuf tb;
375 time_t currtime;
377 if (default_vfs_ops.stat(conn, fname, &st) != 0) {
378 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
379 return;
381 currtime = time(&currtime);
382 tb.actime = currtime;
383 tb.modtime = st.st_mtime;
385 if (default_vfs_ops.utime(conn, fname, &tb) == -1 )
386 DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname, strerror(errno)));
390 * Check if file should be recycled
392 static int recycle_unlink(connection_struct *conn, const char *inname)
394 recycle_bin_struct *recbin;
395 char *file_name = NULL;
396 char *path_name = NULL;
397 char *temp_name = NULL;
398 char *final_name = NULL;
399 char *base;
400 int i;
401 SMB_BIG_UINT dfree, dsize, bsize;
402 SMB_OFF_T file_size, space_avail;
403 BOOL exist;
404 int rc = -1;
406 file_name = strdup(inname);
407 ALLOC_CHECK(file_name, done);
409 if (conn->vfs_private)
410 recbin = (recycle_bin_struct *)conn->vfs_private;
411 else {
412 DEBUG(0, ("Recycle bin not initialized!\n"));
413 rc = default_vfs_ops.unlink(conn, file_name);
414 goto done;
417 if(!recbin->repository || *(recbin->repository) == '\0') {
418 DEBUG(3, ("Recycle path not set, purging %s...\n", file_name));
419 rc = default_vfs_ops.unlink(conn, file_name);
420 goto done;
423 /* we don't recycle the recycle bin... */
424 if (strncmp(file_name, recbin->repository, strlen(recbin->repository)) == 0) {
425 DEBUG(3, ("File is within recycling bin, unlinking ...\n"));
426 rc = default_vfs_ops.unlink(conn, file_name);
427 goto done;
430 file_size = recycle_get_file_size(conn, file_name);
431 /* it is wrong to purge filenames only because they are empty imho
432 * --- simo
434 if(fsize == 0) {
435 DEBUG(3, ("File %s is empty, purging...\n", file_name));
436 rc = default_vfs_ops.unlink(conn,file_name);
437 goto done;
441 /* FIXME: this is wrong, we should check the hole size of the recycle bin is
442 * not greater then maxsize, not the size of the single file, also it is better
443 * to remove older files
445 if(recbin->maxsize > 0 && file_size > recbin->maxsize) {
446 DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name));
447 rc = default_vfs_ops.unlink(conn, file_name);
448 goto done;
451 /* FIXME: this is wrong: moving files with rename does not change the disk space
452 * allocation
454 space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L;
455 DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
456 if(space_avail < file_size) {
457 DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name));
458 rc = default_vfs_ops.unlink(conn, file_name);
459 goto done;
463 /* extract filename and path */
464 path_name = (char *)malloc(PATH_MAX);
465 ALLOC_CHECK(path_name, done);
466 *path_name = '\0';
467 safe_strcpy(path_name, file_name, PATH_MAX);
468 base = strrchr(path_name, '/');
469 if (base == NULL) {
470 base = file_name;
471 safe_strcpy(path_name, "/", PATH_MAX);
473 else {
474 *base = '\0';
475 base++;
478 DEBUG(10, ("recycle.bin: fname = %s\n", file_name)); /* original filename with path */
479 DEBUG(10, ("recycle.bin: fpath = %s\n", path_name)); /* original path */
480 DEBUG(10, ("recycle.bin: base = %s\n", base)); /* filename without path */
482 if (matchparam(recbin->exclude, base)) {
483 DEBUG(3, ("recycle.bin: file %s is excluded \n", base));
484 rc = default_vfs_ops.unlink(conn, file_name);
485 goto done;
488 /* FIXME: this check will fail if we have more than one level of directories,
489 * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 ....
490 * ---simo
492 if (checkparam(recbin->exclude_dir, path_name)) {
493 DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name));
494 rc = default_vfs_ops.unlink(conn, file_name);
495 goto done;
498 temp_name = (char *)malloc(PATH_MAX);
499 ALLOC_CHECK(temp_name, done);
500 safe_strcpy(temp_name, recbin->repository, PATH_MAX);
502 /* see if we need to recreate the original directory structure in the recycle bin */
503 if (recbin->keep_dir_tree == True) {
504 safe_strcat(temp_name, "/", PATH_MAX);
505 safe_strcat(temp_name, path_name, PATH_MAX);
508 exist = recycle_directory_exist(conn, temp_name);
509 if (exist) {
510 DEBUG(10, ("recycle.bin: Directory already exists\n"));
511 } else {
512 DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name));
513 if (recycle_create_dir(conn, temp_name) == False) {
514 DEBUG(3, ("Could not create directory, purging %s...\n", file_name));
515 rc = default_vfs_ops.unlink(conn, file_name);
516 goto done;
520 final_name = (char *)malloc(PATH_MAX);
521 ALLOC_CHECK(final_name, done);
522 snprintf(final_name, PATH_MAX, "%s/%s", temp_name, base);
523 DEBUG(10, ("recycle.bin: recycled file name%s\n", temp_name)); /* new filename with path */
525 /* check if we should delete file from recycle bin */
526 if (recycle_file_exist(conn, final_name)) {
527 if (recbin->versions == False || matchparam(recbin->noversions, base) == True) {
528 DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", final_name));
529 if (default_vfs_ops.unlink(conn, final_name) != 0) {
530 DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno)));
535 /* rename file we move to recycle bin */
536 i = 1;
537 while (recycle_file_exist(conn, final_name)) {
538 snprintf(final_name, PATH_MAX, "%s/Copy #%d of %s", temp_name, i++, base);
541 DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name, final_name));
542 rc = default_vfs_ops.rename(conn, file_name, final_name);
543 if (rc != 0) {
544 DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name));
545 rc = default_vfs_ops.unlink(conn, file_name);
546 goto done;
549 /* touch access date of moved file */
550 if (recbin->touch == True )
551 recycle_touch(conn, final_name);
553 done:
554 SAFE_FREE(file_name);
555 SAFE_FREE(path_name);
556 SAFE_FREE(temp_name);
557 SAFE_FREE(final_name);
558 return rc;