summaryrefslogtreecommitdiffstats
path: root/examples/VFS/recycle/recycle.c
blob: 4e7b6c7bcf31c65327857ad80095afb8d9ead5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* 
 * Recycle bin VFS module for Samba.
 *
 * Copyright (C) 2001, Brandon Stone, Amherst College, <bbstone@amherst.edu>.
 * Copyright (C) 2002, Jeremy Allison - modified to make a VFS module.
 * Copyright (C) 2002, Juergen Hasch - added some options.
  *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"
#include <stdio.h>
#include <sys/stat.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <errno.h>
#include <string.h>
#include <includes.h>
#include <vfs.h>

const char delimiter = '|';		/* delimiter for options */

/* One per connection */

typedef struct recycle_bin_struct
{
	TALLOC_CTX *ctx;
	char	*recycle_bin;		/* name of the recycle bin directory */
	BOOL	keep_directories;	/* keep directory structure of deleted file in recycle bin */
	BOOL	versions;		/* create versions of deleted files with identical name */
	BOOL	touch;			/* touch access date of deleted file */
	char	*exclude;		/* which files to exclude */
	char	*exclude_dir;		/* which directories to exclude */
	char	*noversions;		/* which files to exclude from versioning */
	SMB_OFF_T max_size;		/* maximum file size to be saved */
} recycle_bin_struct;

/* Global Variables */
static recycle_bin_struct *current;

/* VFS operations */

extern struct vfs_ops default_vfs_ops;   /* For passthrough operation */

static int recycle_unlink(connection_struct *, const char *);
static int recycle_connect(struct connection_struct *conn, const char *service, const char *user);
static void recycle_disconnect(struct connection_struct *conn);

BOOL checkparam(char *haystack,char *needle);

struct vfs_ops recycle_ops = {
    
	/* Disk operations */

	recycle_connect,		/* connect */
	recycle_disconnect,		/* disconnect */
	NULL,				/* disk free */

	/* Directory operations */

	NULL,				/* opendir */
	NULL,				/* readdir */
	NULL,				/* mkdir */
	NULL,				/* rmdir */
	NULL,				/* closedir */

	/* File operations */

	NULL,				/* open */
	NULL,				/* close */
	NULL,				/* read  */
	NULL,				/* write */
	NULL,				/* lseek */
	NULL,				/* rename */
	NULL,				/* fsync */
	NULL,				/* stat  */
	NULL,				/* fstat */
	NULL,				/* lstat */
	recycle_unlink,
	NULL,				/* chmod */
	NULL,				/* fchmod */
	NULL,				/* chown */
	NULL,				/* fchown */
	NULL,				/* chdir */
	NULL,				/* getwd */
	NULL,				/* utime */
	NULL,				/* ftruncate */
	NULL,				/* lock */
	NULL,				/* symlink */
	NULL,				/* readlink */
	NULL,				/* link */
	NULL,				/* mknod */
	NULL,				/* realpath */
	NULL,				/* fget_nt_acl */
	NULL,				/* get_nt_acl */
	NULL,				/* fset_nt_acl */
	NULL,				/* set_nt_acl */

	NULL,				/* chmod_acl */
	NULL,				/* fchmod_acl */

	NULL,				/* sys_acl_get_entry */
	NULL,				/* sys_acl_get_tag_type */
	NULL,				/* sys_acl_get_permset */
	NULL,				/* sys_acl_get_qualifier */
	NULL,				/* sys_acl_get_file */
	NULL,				/* sys_acl_get_fd */
	NULL,				/* sys_acl_clear_perms */
	NULL,				/* sys_acl_add_perm */
	NULL,				/* sys_acl_to_text */
	NULL,				/* sys_acl_init */
	NULL,				/* sys_acl_create_entry */
	NULL,				/* sys_acl_set_tag_type */
	NULL,				/* sys_acl_set_qualifier */
	NULL,				/* sys_acl_set_permset */
	NULL,				/* sys_acl_valid */
	NULL,				/* sys_acl_set_file */
	NULL,				/* sys_acl_set_fd */
	NULL,				/* sys_acl_delete_def_file */
	NULL,				/* sys_acl_get_perm */
	NULL,				/* sys_acl_free_text */
	NULL,				/* sys_acl_free_acl */
	NULL				/* sys_acl_free_qualifier */
};

/**
 * Parse recycle bin configuration parameters
 *
 * @retval False if out of memory
 **/
static BOOL do_parameter(char *pszParmName, char *pszParmValue)
{
	if (StrCaseCmp("name",pszParmName)==0) {
		current->recycle_bin = (char *)talloc(current->ctx,sizeof(pstring));
		if (current->recycle_bin == NULL)
			return False;
		current->recycle_bin = safe_strcpy(current->recycle_bin,pszParmValue,sizeof(pstring));
		standard_sub_basic(current->recycle_bin, strlen(current->recycle_bin));
		trim_string(current->recycle_bin,"/","/");
		DEBUG(10, ("name=%s\n", current->recycle_bin));
	} else if (StrCaseCmp("mode",pszParmName)==0) {
		if (checkparam(pszParmValue,"KEEP_DIRECTORIES") == True)
			current->keep_directories = True;
		if (checkparam(pszParmValue,"VERSIONS") == True)
			current->versions = True;
		if (checkparam(pszParmValue,"TOUCH") == True)
			current->touch = True;
		DEBUG(10, ("mode=%s\n", pszParmValue));
	} else if (StrCaseCmp("maxsize",pszParmName)==0) {
		current->max_size = strtoul(pszParmValue,NULL,10);
		DEBUG(10, ("max_size=%ld\n", (long int)current->max_size));
	} else if (StrCaseCmp("exclude",pszParmName)==0) {
		current->exclude = talloc_strdup(current->ctx, pszParmValue);
		if (current->exclude == NULL)
			return False;
		DEBUG(10, ("exclude=%s\n", current->exclude));
	} else if (StrCaseCmp("excludedir",pszParmName)==0) {
		current->exclude_dir = talloc_strdup(current->ctx, pszParmValue);
		if (current->exclude_dir == NULL)
			return False;
		DEBUG(10, ("exclude_dir=%s\n", current->exclude_dir));
	} else if (StrCaseCmp("noversions",pszParmName)==0) {
		current->noversions = talloc_strdup(current->ctx, pszParmValue);
		if (current->noversions == NULL)
			return False;
		DEBUG(10, ("noversions=%s\n", current->noversions));
	}
	return True;
}

/**
 * We don't care for sections in configuration file
 *
 **/
static BOOL do_section(char *pszSectionName)
{
	return True;
}

/**
 * VFS initialisation function.
 *
 * @retval initialised vfs_ops structure
 **/
struct vfs_ops *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops)
{
	struct vfs_ops tmp_ops;
	DEBUG(3, ("Initializing VFS module recycle\n"));

	*vfs_version = SMB_VFS_INTERFACE_VERSION;
	memcpy(&tmp_ops, def_vfs_ops, sizeof(struct vfs_ops));
	tmp_ops.unlink = recycle_unlink;
	tmp_ops.connect = recycle_connect;
	tmp_ops.disconnect = recycle_disconnect;
	memcpy(&recycle_ops, &tmp_ops, sizeof(struct vfs_ops));
	return &recycle_ops;
}

static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)
{
	const char *p;
	pstring conf_file;
	int rc;
	TALLOC_CTX *ctx=NULL;

	DEBUG(3,("Called for service %s (%d) as user %s\n", service, SNUM(conn), user));
	
	if (!(ctx = talloc_init_named("recycle bin"))) {
		DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
		return 0;
	}
	
	/* read configuration file */
	*conf_file='\0';
	p = (const char *)lp_vfs_options(SNUM(conn));
	if (p != NULL && strlen(p) > 0) {
		pstrcpy(conf_file,p);
		DEBUG(10,("Using configuration file %s\n",conf_file));
	}
	
	current = talloc(ctx,sizeof(recycle_bin_struct));
	if ( current == NULL) {
		DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
		return -1;
	}
	current->ctx = ctx;
	/* Set defaults */
	current->recycle_bin = talloc_strdup(ctx,".recycle");
	current->keep_directories = False;
	current->versions = False;
	current->touch = False;
	current->exclude = "";
	current->exclude_dir = "";
	current->noversions = "";
	current->max_size = 0;
	if (strlen(conf_file) > 0) {
		rc=pm_process( conf_file, do_section, do_parameter);
		DEBUG(10, ("pm_process returned %d\n", rc));
	}
	conn->vfs_private= (void *)current;
	return 0;
}

static void recycle_disconnect(struct connection_struct *conn)
{
	DEBUG(3, ("Disconnecting VFS module recycle_bin\n"));
	talloc_destroy(((recycle_bin_struct*)conn->vfs_private)->ctx);
	default_vfs_ops.disconnect(conn);
}

static BOOL recycle_XXX_exist(connection_struct *conn, const char *dname, BOOL isdir)
{
	SMB_STRUCT_STAT st;

	if (default_vfs_ops.stat(conn,dname,&st) != 0)
		return(False);

	if (isdir)
		return S_ISDIR(st.st_mode) ? True : False;
	else
		return S_ISREG(st.st_mode) ? True : False;
}

static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)
{
	return recycle_XXX_exist(conn, dname, True);
}

static BOOL recycle_file_exist(connection_struct *conn, const char *fname)
{
	return recycle_XXX_exist(conn, fname, False);
}

/**
 * Return file size
 * @param conn connection
 * @param fname file name
 * @return size in bytes
 **/
static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)
{
	SMB_STRUCT_STAT st;
	if (default_vfs_ops.stat(conn,fname,&st) != 0) {
		DEBUG(0,("stat for %s returned %s\n",fname,strerror(errno)));
		return (SMB_OFF_T)0;
	}
	return(st.st_size);
}

/**
 * Create directory tree
 * @param conn connection
 * @param dname Directory tree to be created
 * @return Returns True for success
 **/
static BOOL recycle_create_dir(connection_struct *conn, const char *dname)
{
	char *c,*y;
	int i;
	
	mode_t mode;
	pstring tempstr;
	pstring newdir;
	
	*newdir='\0';
	mode=S_IREAD|S_IWRITE|S_IEXEC;
	pstrcpy(tempstr,dname);
	y=tempstr;
	/* Create directory tree if neccessary */
	while((c=strsep(&y,"/"))) {
		pstrcat(newdir,c);
		if (recycle_directory_exist(conn,newdir))
			DEBUG(3, ("dir %s already exists\n",newdir));
		else {
			DEBUG(3, ("creating new dir %s\n",newdir));
			i=default_vfs_ops.mkdir(conn,newdir,mode);
			if (i) {
				DEBUG(3,("mkdir failed for %s with error %s\n",newdir,strerror(errno)));
				return False;
			}
		}
		pstrcat(newdir,"/");	
		}
	return True;
}

/**
 * Check if needle is contained exactly in haystack
 * @param haystack list of parameters separated by delimimiter character
 * @param needle string to be matched exactly to haystack
 * @return True if found
 **/
BOOL checkparam(char *haystack,char *needle)
{
	char *p,*c;
	pstring str;
	int i,len;
	
	if (haystack==NULL || strlen(haystack)==0 || needle == NULL || strlen(needle)== 0)
		return False;
		
	pstrcpy(str,haystack);
	len=strlen(str)+1;
	p=c=str;

	for (i=0; i < len; i++, p++) {
		if (*p == delimiter || *p == '\0') {
			*p='\0';
			if(strncmp(c,needle,c-p) == 0)
				return True;
			c=p+1;
		}
	}
	return False;
}

/**
 * Check if needle is contained in haystack, * and ? patterns are resolved
 * @param haystack list of parameters separated by delimimiter character
 * @param needle string to be matched exectly to haystack including pattern matching
 * @return True if found
 **/
BOOL matchparam(char *haystack,char *needle)
{
	char *p,*c;
	pstring str;
	int i,len;
	
	if (haystack==NULL || strlen(haystack)==0 || needle == NULL || strlen(needle)== 0)
		return False;
		
	pstrcpy(str,haystack);
	len=strlen(str)+1;
	p=c=str;

	for (i=0; i < len; i++, p++) {
		if (*p == delimiter || *p == '\0') {
			*p='\0';
			if (!unix_wild_match(c,needle))
				return True;
			c=p+1;
		}
	}
	return False;
}

/**
 * Touch access date
 **/
void recycle_touch(connection_struct *conn, const char *fname)
{
	SMB_STRUCT_STAT st;
	struct utimbuf tb;
	time_t current;

	if (default_vfs_ops.stat(conn,fname,&st) != 0) {
		DEBUG(0,("stat for %s returned %s\n",fname,strerror(errno)));
		return;
	}
	current = time(&current);
	tb.actime = current;
	tb.modtime = st.st_mtime;	

	if (default_vfs_ops.utime(conn, fname, &tb) == -1 )
		DEBUG(0, ("Touching %s failed, reason = %s\n",fname,strerror(errno)));
	}

/**
 * Check if file should be recycled
 **/
static int recycle_unlink(connection_struct *conn, const char *inname)
{
	pstring fname,fpath, bin;
	char *base, *ext;
	int i=1, len, addlen;
	SMB_BIG_UINT dfree,dsize,bsize;
	SMB_OFF_T fsize,space_avail;
	BOOL exist;
	int rc;

	pstrcpy(fname,inname);
	if (conn->vfs_private)
		current = (recycle_bin_struct *)conn->vfs_private;
	else {
		DEBUG(0,("Recycle bin not initialized!\n"));
		return default_vfs_ops.unlink(conn,fname);
	}
		
	if(!current->recycle_bin || !*(current->recycle_bin)) {
		DEBUG(3, ("Recycle path not set, purging %s...\n", fname));
		return default_vfs_ops.unlink(conn,fname);
	}
	
	/* we don't recycle the recycle bin... */
	if (strstr(fname,current->recycle_bin)==fname) {
		DEBUG(3, ("File is within recycling bin\n"));
		return default_vfs_ops.unlink(conn,fname);
	}
	
	fsize = recycle_get_file_size(conn,fname);
	if(fsize == 0) {
		DEBUG(3, ("File %s is empty, purging...\n", fname));
		return default_vfs_ops.unlink(conn,fname);
	}
	
	if(current->max_size > 0 && fsize > current->max_size) {
		DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", fname));
		return default_vfs_ops.unlink(conn,fname);
	} 
	
	space_avail = default_vfs_ops.disk_free(conn,".",True,&bsize,&dfree,&dsize)*1024L;
	DEBUG(10,("space_avail = %Lu, fsize = %Lu\n",space_avail,fsize));
	if(space_avail < fsize) {
		DEBUG(3, ("Not enough diskspace, purging file %s\n",fname));
		return default_vfs_ops.unlink(conn,fname);
	}

	/* extract filename and path */
	pstrcpy(fpath,"/");
	pstrcat(fpath, fname);
	base = strrchr(fpath, '/');
	if (base == NULL) {
		ext = strrchr(fname, '.');
		base = (char *)fname;
		pstrcpy(fpath,"/");
	}
	else {
		ext = strrchr(base, '.');
		*(base++) = '\0';
	}

	DEBUG(10, ("fname:%s\n", fname));	/* original filename with path */
	DEBUG(10, ("fpath:%s\n", fpath));	/* original path */
	DEBUG(10, ("base:%s\n", base));		/* filename without path */
	DEBUG(10, ("ext:%s\n", ext));		/* filename extension */
	
	if (matchparam(current->exclude,base)) {
		DEBUG(3, ("file %s is excluded \n",base));
		return default_vfs_ops.unlink(conn,fname);
	}

	if (checkparam(current->exclude_dir,fpath)) {
		DEBUG(3, ("directory %s is excluded \n",fpath));
		return default_vfs_ops.unlink(conn,fname);
	} 

	pstrcpy(bin, current->recycle_bin);

	/* see if we need to recreate the original directory structure in the recycle bin */
	if (current->keep_directories == True)
		pstrcat(bin, fpath);

	exist=recycle_directory_exist(conn,bin);
	if (exist)
		DEBUG(10, ("Directory already exists\n"));
	else {
		DEBUG(10, ("Creating directory %s\n",bin));
		rc=recycle_create_dir(conn,bin);
		if (rc == False)
			{
			DEBUG(3, ("Could not create directory, purging %s...\n", fname));
			return default_vfs_ops.unlink(conn,fname);			
			}
	}

	pstrcat(bin, "/");
	pstrcat(bin,base);
	DEBUG(10, ("bin:%s\n", bin));		/* new filename with path */	
	
	/* check if we should delete file from recycle bin */
	if (recycle_file_exist(conn,bin)) {
		if (current->versions == False || matchparam(current->noversions,base) == True) {
			DEBUG(3, ("Removing old file %s from recycle bin\n",bin));
			default_vfs_ops.unlink(conn,bin);
		}
	}

	/* rename file we move to recycle bin */
	len = strlen(bin);
	addlen = sizeof(pstring)-len-1;
	while(recycle_file_exist(conn,bin)) {
		slprintf(bin+len, addlen, " (Copy #%d)", i++);
		pstrcat(bin, ext);
	}
	
	DEBUG(10, ("Moving source=%s to dest=%s\n", fname, bin));
	rc = default_vfs_ops.rename(conn, fname, bin);
	if (rc == -1) {
		DEBUG(3, ("Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno),fname,bin));
		return default_vfs_ops.unlink(conn,fname);
	}

	/* touch access date of moved file */
	if (current->touch == True )
		recycle_touch(conn,bin);
	return rc;
}