summaryrefslogtreecommitdiff
path: root/source3/modules/vfs_scannedonly.c
blob: df1e140f3ce97e292f81ee34a830c26bee85a60c (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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/*
 * scannedonly VFS module for Samba 3.5 and beyond
 *
 * Copyright 2007,2008,2009,2010,2011 (C) Olivier Sessink
 *
 * 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 3 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.
 *
 * ABOUT SCANNEDONLY
 *
 * scannedonly implements a 'filter' like vfs module that talks over a
 * unix domain socket or over UDP to a anti-virus engine.
 *
 * files that are clean have a corresponding .scanned:{filename} file
 * in the same directory. So why the .scanned: files? They take up
 * only an inode, because they are 0 bytes. To test if the file is
 * scanned only a stat() call on the filesystem is needed which is
 * very quick compared to a database lookup. All modern filesystems
 * use database technology such as balanced trees for lookups anyway.
 * The number of inodes in modern filesystems is also not limiting
 * anymore. The .scanned: files are also easy scriptable. You can
 * remove them with a simple find command or create them with a
 * simple touch command. Extended filesystem attributes have similar
 * properties, but are not supported on all filesystems, so that
 * would limit the usage of the module (and attributes are not as
 * easily scriptable)
 *
 * files that are not clean are sent to the AV-engine. Only the
 * filename is sent over the socket. The protocol is very simple:
 * a newline separated list of filenames inside each datagram.
 *
 * a file AV-scan may be requested multiple times, the AV-engine
 * should also check if the file has been scanned already. Requests
 * can also be dropped by the AV-engine (and we thus don't need the
 * reliability of TCP).
 *
 */

#include "includes.h"
#include "smbd/smbd.h"
#include "system/filesys.h"

#include "config.h"

#define SENDBUFFERSIZE 1450

#ifndef       SUN_LEN
#define       SUN_LEN(sunp)   ((size_t)((struct sockaddr_un *)0)->sun_path \
				+ strlen((sunp)->sun_path))
#endif


struct Tscannedonly {
	int socket;
	int domain_socket;
	int portnum;
	int scanning_message_len;
	int recheck_time_open;
	int recheck_tries_open;
	int recheck_size_open;
	int recheck_time_readdir;
	int recheck_tries_readdir;
	bool show_special_files;
	bool rm_hidden_files_on_rmdir;
	bool hide_nonscanned_files;
	bool allow_nonscanned_files;
	const char *socketname;
	const char *scanhost;
	const char *scanning_message;
	const char *p_scanned; /* prefix for scanned files */
	const char *p_virus; /* prefix for virus containing files */
	const char *p_failed; /* prefix for failed to scan files */
	char gsendbuffer[SENDBUFFERSIZE + 1];
};

#define STRUCTSCANO(var) ((struct Tscannedonly *)var)

struct scannedonly_DIR {
	char *base;
	int recheck_tries_done; /* if 0 the directory listing has not yet
	been checked for files that need to be scanned. */
	SMB_STRUCT_DIR *DIR;
};
#define SCANNEDONLY_DEBUG 9
/*********************/
/* utility functions */
/*********************/

static char *real_path_from_notify_path(TALLOC_CTX *ctx,
					struct Tscannedonly *so,
					const char *path)
{
	char *name;
	int len, pathlen;

	name = strrchr(path, '/');
	if (!name) {
		return NULL;
	}
	pathlen = name - path;
	name++;
	len = strlen(name);
	if (len <= so->scanning_message_len) {
		return NULL;
	}

	if (strcmp(name + (len - so->scanning_message_len),
		   so->scanning_message) != 0) {
		return NULL;
	}

	return talloc_strndup(ctx,path,
			      pathlen + len - so->scanning_message_len);
}

static char *cachefile_name(TALLOC_CTX *ctx,
			    const char *shortname,
			    const char *base,
			    const char *p_scanned)
{
	return talloc_asprintf(ctx, "%s%s%s", base, p_scanned, shortname);
}

static char *name_w_ending_slash(TALLOC_CTX *ctx, const char *name)
{
	int len = strlen(name);
	if (name[len - 1] == '/') {
		return talloc_strdup(ctx,name);
	} else {
		return talloc_asprintf(ctx, "%s/", name);
	}
}

static char *cachefile_name_f_fullpath(TALLOC_CTX *ctx,
				       const char *fullpath,
				       const char *p_scanned)
{
	const char *base;
	char *tmp, *cachefile;
	const char *shortname;
	tmp = strrchr(fullpath, '/');
	if (tmp) {
		base = talloc_strndup(ctx, fullpath, (tmp - fullpath) + 1);
		shortname = tmp + 1;
	} else {
		base = "";
		shortname = (const char *)fullpath;
	}
	cachefile = cachefile_name(ctx, shortname, base, p_scanned);
	DEBUG(SCANNEDONLY_DEBUG,
	      ("cachefile_name_f_fullpath cachefile=%s\n", cachefile));
	return cachefile;
}

static char *construct_full_path(TALLOC_CTX *ctx, vfs_handle_struct * handle,
				 const char *somepath, bool ending_slash)
{
	const char *tmp;

	if (!somepath) {
		return NULL;
	}
	if (somepath[0] == '/') {
		if (ending_slash) {
			return name_w_ending_slash(ctx,somepath);
		}
		return talloc_strdup(ctx,somepath);
	}
	tmp = somepath;
	if (tmp[0]=='.'&&tmp[1]=='/') {
		tmp+=2;
	}
	/* vfs_GetWd() seems to return a path with a slash */
	if (ending_slash) {
		return talloc_asprintf(ctx, "%s/%s/",
				       vfs_GetWd(ctx, handle->conn),tmp);
	}
	return talloc_asprintf(ctx, "%s/%s",
			       vfs_GetWd(ctx, handle->conn),tmp);
}

static int connect_to_scanner(vfs_handle_struct * handle)
{
	struct Tscannedonly *so = (struct Tscannedonly *)handle->data;

	if (so->domain_socket) {
		struct sockaddr_un saun;
		DEBUG(SCANNEDONLY_DEBUG, ("socket=%s\n", so->socketname));
		if ((so->socket = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
			DEBUG(2, ("failed to create socket %s\n",
				  so->socketname));
			return -1;
		}
		saun.sun_family = AF_UNIX;
		strncpy(saun.sun_path, so->socketname,
			sizeof(saun.sun_path) - 1);
		if (connect(so->socket, (struct sockaddr *)(void *)&saun,
			    SUN_LEN(&saun)) < 0) {
			DEBUG(2, ("failed to connect to socket %s\n",
				  so->socketname));
			return -1;
		}
		DEBUG(SCANNEDONLY_DEBUG,("bound %s to socket %d\n",
					 saun.sun_path, so->socket));

	} else {
		so->socket = open_udp_socket(so->scanhost, so->portnum);
		if (so->socket < 0) {
			DEBUG(2,("failed to open UDP socket to %s:%d\n",
				 so->scanhost,so->portnum));
			return -1;
		}
	}

	{/* increasing the socket buffer is done because we have large bursts
	    of UDP packets or DGRAM's on a domain socket whenever we hit a
	    large directory with lots of unscanned files. */
		int sndsize;
		socklen_t size = sizeof(int);
		getsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
			   (char *)&sndsize, &size);
		DEBUG(SCANNEDONLY_DEBUG, ("current socket buffer size=%d\n",
					  sndsize));
		sndsize = 262144;
		if (setsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
			       (char *)&sndsize,
			       (int)sizeof(sndsize)) != 0) {
			DEBUG(SCANNEDONLY_DEBUG,
			      ("error setting socket buffer %s (%d)\n",
			       strerror(errno), errno));
		}
	}
	set_blocking(so->socket, false);
	return 0;
}

static void flush_sendbuffer(vfs_handle_struct * handle)
{
	struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
	int ret, len, loop = 10;
	if (so->gsendbuffer[0] == '\0') {
		return;
	}

	do {
		loop--;
		len = strlen(so->gsendbuffer);
		ret = send(so->socket, so->gsendbuffer, len, 0);
		if (ret == len) {
			so->gsendbuffer[0] = '\0';
			break;
		}
		if (ret == -1) {
			DEBUG(3,("scannedonly flush_sendbuffer: "
				 "error sending on socket %d to scanner:"
				 " %s (%d)\n",
				 so->socket, strerror(errno), errno));
			if (errno == ECONNREFUSED || errno == ENOTCONN
			    || errno == ECONNRESET) {
				if (connect_to_scanner(handle) == -1)
					break;	/* connecting fails, abort */
				/* try again */
			} else if (errno != EINTR) {
				/* on EINTR we just try again, all remaining
				   other errors we log the error
				   and try again ONCE */
				loop = 1;
				DEBUG(3,("scannedonly flush_sendbuffer: "
					 "error sending data to scanner: %s "
					 "(%d)\n", strerror(errno), errno));
			}
		} else {
			/* --> partial write: Resend all filenames that were
			   not or not completely written. a partial filename
			   written means the filename will not arrive correctly,
			   so resend it completely */
			int pos = 0;
			while (pos < len) {
				char *tmp = strchr(so->gsendbuffer+pos, '\n');
				if (tmp && tmp - so->gsendbuffer < ret)
					pos = tmp - so->gsendbuffer + 1;
				else
					break;
			}
			memmove(so->gsendbuffer, so->gsendbuffer + pos,
				SENDBUFFERSIZE - ret);
			/* now try again */
		}
	} while (loop > 0);

	if (so->gsendbuffer[0] != '\0') {
		DEBUG(2,
		      ("scannedonly flush_sendbuffer: "
		       "failed to send files to AV scanner, "
		       "discarding files."));
		so->gsendbuffer[0] = '\0';
	}
}

static void notify_scanner(vfs_handle_struct * handle, const char *scanfile)
{
	const char *tmp;
	int tmplen, gsendlen;
	struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
	TALLOC_CTX *ctx=talloc_tos();
	if (scanfile[0] != '/') {
		tmp = construct_full_path(ctx,handle, scanfile, false);
	} else {
		tmp = (const char *)scanfile;
	}
	tmplen = strlen(tmp);
	gsendlen = strlen(so->gsendbuffer);
	DEBUG(SCANNEDONLY_DEBUG,
	      ("scannedonly notify_scanner: tmp=%s, tmplen=%d, gsendlen=%d\n",
	       tmp, tmplen, gsendlen));
	if (gsendlen + tmplen >= SENDBUFFERSIZE) {
		flush_sendbuffer(handle);
	}
	strlcat(so->gsendbuffer, tmp, SENDBUFFERSIZE + 1);
	strlcat(so->gsendbuffer, "\n", SENDBUFFERSIZE + 1);
}

static bool is_scannedonly_file(struct Tscannedonly *so, const char *shortname)
{
	if (shortname[0]!='.') {
		return false;
	}
	if (strncmp(shortname, so->p_scanned, strlen(so->p_scanned)) == 0) {
		return true;
	}
	if (strncmp(shortname, so->p_virus, strlen(so->p_virus)) == 0) {
		return true;
	}
	if (strncmp(shortname, so->p_failed, strlen(so->p_failed)) == 0) {
		return true;
	}
	return false;
}

static bool timespec_is_newer(struct timespec *base, struct timespec *test)
{
	return timespec_compare(base,test) < 0;
}

/*
vfs_handle_struct *handle the scannedonly handle
scannedonly_DIR * sDIR the scannedonly struct if called from _readdir()
or NULL
fullpath is a full path starting from / or a relative path to the
current working directory
shortname is the filename without directory components
basename, is the directory without file name component
allow_nonexistant return TRUE if stat() on the requested file fails
recheck_time, the time in milliseconds to wait for the daemon to
create a .scanned file
recheck_tries, the number of tries to wait
recheck_size, size in Kb of files that should not be waited for
loop : boolean if we should try to loop over all files in the directory
and send a notify to the scanner for all files that need scanning
*/
static bool scannedonly_allow_access(vfs_handle_struct * handle,
				     struct scannedonly_DIR *sDIR,
				     struct smb_filename *smb_fname,
				     const char *shortname,
				     const char *base_name,
				     int allow_nonexistant,
				     int recheck_time, int recheck_tries,
				     int recheck_size, int loop)
{
	struct smb_filename *cache_smb_fname;
	TALLOC_CTX *ctx=talloc_tos();
	char *cachefile;
	int retval = -1;
	DEBUG(SCANNEDONLY_DEBUG,
	      ("smb_fname->base_name=%s, shortname=%s, base_name=%s\n"
	       ,smb_fname->base_name,shortname,base_name));

	if (ISDOT(shortname) || ISDOTDOT(shortname)) {
		return true;
	}
	if (is_scannedonly_file(STRUCTSCANO(handle->data), shortname)) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("scannedonly_allow_access, %s is a scannedonly file, "
		       "return 0\n", shortname));
		return false;
	}

	if (!VALID_STAT(smb_fname->st)) {
		DEBUG(SCANNEDONLY_DEBUG,("stat %s\n",smb_fname->base_name));
		retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
		if (retval != 0) {
			/* failed to stat this file?!? --> hide it */
			DEBUG(SCANNEDONLY_DEBUG,("no valid stat, return"
						 " allow_nonexistant=%d\n",
						 allow_nonexistant));
			return allow_nonexistant;
		}
	}
	if (!S_ISREG(smb_fname->st.st_ex_mode)) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("%s is not a regular file, ISDIR=%d\n",
		       smb_fname->base_name,
		       S_ISDIR(smb_fname->st.st_ex_mode)));
		return (STRUCTSCANO(handle->data)->
			show_special_files ||
			S_ISDIR(smb_fname->st.st_ex_mode));
	}
	if (smb_fname->st.st_ex_size == 0) {
		DEBUG(SCANNEDONLY_DEBUG,("empty file, return 1\n"));
		return true;	/* empty files cannot contain viruses ! */
	}
	cachefile = cachefile_name(ctx,
				   shortname,
				   base_name,
				   STRUCTSCANO(handle->data)->p_scanned);
	create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,&cache_smb_fname);
	if (!VALID_STAT(cache_smb_fname->st)) {
		retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
	}
	if (retval == 0 && VALID_STAT(cache_smb_fname->st)) {
		if (timespec_is_newer(&smb_fname->st.st_ex_ctime,
				      &cache_smb_fname->st.st_ex_ctime)) {
			talloc_free(cache_smb_fname);
			return true;
		}
		/* no cachefile or too old */
		SMB_VFS_NEXT_UNLINK(handle, cache_smb_fname);
		retval = -1;
	}

	notify_scanner(handle, smb_fname->base_name);

	if (loop && sDIR && sDIR->recheck_tries_done == 0) {
		/* check the rest of the directory and notify the
		   scanner if some file needs scanning */
		long offset;
		SMB_STRUCT_DIRENT *dire;

		offset = SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
		dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, NULL);
		while (dire) {
			char *fpath2;
			struct smb_filename *smb_fname2;
			fpath2 = talloc_asprintf(ctx, "%s%s", base_name,dire->d_name);
			DEBUG(SCANNEDONLY_DEBUG,
			      ("scannedonly_allow_access in loop, "
			       "found %s\n", fpath2));
			create_synthetic_smb_fname(ctx, fpath2,NULL,NULL,
						   &smb_fname2);
			scannedonly_allow_access(handle, NULL,
						 smb_fname2,
						 dire->d_name,
						 base_name, 0, 0, 0, 0, 0);
			talloc_free(fpath2);
			talloc_free(smb_fname2);
			dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR,NULL);
		}
		sDIR->recheck_tries_done = 1;
		SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
	}
	if (recheck_time > 0
	    && ((recheck_size > 0
		 && smb_fname->st.st_ex_size < (1024 * recheck_size))
		 || (sDIR && sDIR->recheck_tries_done < recheck_tries)
		)) {
		int numloops = sDIR ? sDIR->recheck_tries_done : 0;
		flush_sendbuffer(handle);
		while (retval != 0	/*&& errno == ENOENT */
		       && numloops < recheck_tries) {
			DEBUG(SCANNEDONLY_DEBUG,
			      ("scannedonly_allow_access, wait (try=%d "
			       "(max %d), %d ms) for %s\n",
			       numloops, recheck_tries,
			       recheck_time, cache_smb_fname->base_name));
			smb_msleep(recheck_time);
			retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
			numloops++;
		}
		if (sDIR)
			sDIR->recheck_tries_done = numloops;
	}
	/* still no cachefile, or still too old, return 0 */
	if (retval != 0
	    || !timespec_is_newer(&smb_fname->st.st_ex_ctime,
				  &cache_smb_fname->st.st_ex_ctime)) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("retval=%d, return 0\n",retval));
		return false;
	}
	return true;
}

/*********************/
/* VFS functions     */
/*********************/

static SMB_STRUCT_DIR *scannedonly_opendir(vfs_handle_struct * handle,
					   const char *fname,
					   const char *mask, uint32 attr)
{
	SMB_STRUCT_DIR *DIRp;
	struct scannedonly_DIR *sDIR;

	DIRp = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
	if (!DIRp) {
		return NULL;
	}

	sDIR = talloc(NULL, struct scannedonly_DIR);
	if (fname[0] != '/') {
		sDIR->base = construct_full_path(sDIR,handle, fname, true);
	} else {
		sDIR->base = name_w_ending_slash(sDIR, fname);
	}
	DEBUG(SCANNEDONLY_DEBUG,
			("scannedonly_opendir, fname=%s, base=%s\n",fname,sDIR->base));
	sDIR->DIR = DIRp;
	sDIR->recheck_tries_done = 0;
	return (SMB_STRUCT_DIR *) sDIR;
}

static SMB_STRUCT_DIR *scannedonly_fdopendir(vfs_handle_struct * handle,
					   files_struct *fsp,
					   const char *mask, uint32 attr)
{
	SMB_STRUCT_DIR *DIRp;
	struct scannedonly_DIR *sDIR;
	const char *fname;

	DIRp = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
	if (!DIRp) {
		return NULL;
	}

	fname = (const char *)fsp->fsp_name->base_name;

	sDIR = talloc(NULL, struct scannedonly_DIR);
	if (fname[0] != '/') {
		sDIR->base = construct_full_path(sDIR,handle, fname, true);
	} else {
		sDIR->base = name_w_ending_slash(sDIR, fname);
	}
	DEBUG(SCANNEDONLY_DEBUG,
			("scannedonly_fdopendir, fname=%s, base=%s\n",fname,sDIR->base));
	sDIR->DIR = DIRp;
	sDIR->recheck_tries_done = 0;
	return (SMB_STRUCT_DIR *) sDIR;
}


static SMB_STRUCT_DIRENT *scannedonly_readdir(vfs_handle_struct *handle,
					      SMB_STRUCT_DIR * dirp,
					      SMB_STRUCT_STAT *sbuf)
{
	SMB_STRUCT_DIRENT *result;
	int allowed = 0;
	char *tmp;
	struct smb_filename *smb_fname;
	char *notify_name;
	int namelen;
	SMB_STRUCT_DIRENT *newdirent;
	TALLOC_CTX *ctx=talloc_tos();

	struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
	if (!dirp) {
		return NULL;
	}

	result = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, sbuf);

	if (!result)
		return NULL;

	if (is_scannedonly_file(STRUCTSCANO(handle->data), result->d_name)) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("scannedonly_readdir, %s is a scannedonly file, "
		       "skip to next entry\n", result->d_name));
		return scannedonly_readdir(handle, dirp, NULL);
	}
	tmp = talloc_asprintf(ctx, "%s%s", sDIR->base, result->d_name);
	DEBUG(SCANNEDONLY_DEBUG,
	      ("scannedonly_readdir, check access to %s (sbuf=%p)\n",
	       tmp,sbuf));

	/* even if we don't hide nonscanned files or we allow non scanned
	   files we call allow_access because it will notify the daemon to
	   scan these files */
	create_synthetic_smb_fname(ctx, tmp,NULL,
				   sbuf?VALID_STAT(*sbuf)?sbuf:NULL:NULL,
				   &smb_fname);
	allowed = scannedonly_allow_access(
		handle, sDIR, smb_fname,
		result->d_name,
		sDIR->base, 0,
		STRUCTSCANO(handle->data)->hide_nonscanned_files
		? STRUCTSCANO(handle->data)->recheck_time_readdir
		: 0,
		STRUCTSCANO(handle->data)->recheck_tries_readdir,
		-1,
		1);
	DEBUG(SCANNEDONLY_DEBUG,
	      ("scannedonly_readdir access to %s (%s) = %d\n", tmp,
	       result->d_name, allowed));
	if (allowed) {
		return result;
	}
	DEBUG(SCANNEDONLY_DEBUG,
	      ("hide_nonscanned_files=%d, allow_nonscanned_files=%d\n",
	       STRUCTSCANO(handle->data)->hide_nonscanned_files,
	       STRUCTSCANO(handle->data)->allow_nonscanned_files
		      ));

	if (!STRUCTSCANO(handle->data)->hide_nonscanned_files
	    || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
		return result;
	}

	DEBUG(SCANNEDONLY_DEBUG,
	      ("scannedonly_readdir, readdir listing for %s not "
	       "allowed, notify user\n", result->d_name));
	notify_name = talloc_asprintf(
		ctx,"%s %s",result->d_name,
		STRUCTSCANO(handle->data)->scanning_message);
	namelen = strlen(notify_name);
	newdirent = (SMB_STRUCT_DIRENT *)talloc_array(
		ctx, char, sizeof(SMB_STRUCT_DIRENT) + namelen + 1);
	if (!newdirent) {
		return NULL;
	}
	memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
	memcpy(&newdirent->d_name, notify_name, namelen + 1);
	DEBUG(SCANNEDONLY_DEBUG,
	      ("scannedonly_readdir, return newdirent at %p with "
	       "notification %s\n", newdirent, newdirent->d_name));
	return newdirent;
}

static void scannedonly_seekdir(struct vfs_handle_struct *handle,
				SMB_STRUCT_DIR * dirp, long offset)
{
	struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
	SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
}

static long scannedonly_telldir(struct vfs_handle_struct *handle,
				SMB_STRUCT_DIR * dirp)
{
	struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
	return SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
}

static void scannedonly_rewinddir(struct vfs_handle_struct *handle,
				  SMB_STRUCT_DIR * dirp)
{
	struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
	SMB_VFS_NEXT_REWINDDIR(handle, sDIR->DIR);
}

static int scannedonly_closedir(vfs_handle_struct * handle,
				SMB_STRUCT_DIR * dirp)
{
	int retval;
	struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
	flush_sendbuffer(handle);
	retval = SMB_VFS_NEXT_CLOSEDIR(handle, sDIR->DIR);
	TALLOC_FREE(sDIR);
	return retval;
}

static int scannedonly_stat(vfs_handle_struct * handle,
			    struct smb_filename *smb_fname)
{
	int ret;
	ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
	DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_stat: %s returned %d\n",
				  smb_fname->base_name, ret));
	if (ret != 0 && errno == ENOENT) {
		TALLOC_CTX *ctx=talloc_tos();
		char *test_base_name, *tmp_base_name = smb_fname->base_name;
		/* possibly this was a fake name (file is being scanned for
		   viruses.txt): check for that and create the real name and
		   stat the real name */
		test_base_name = real_path_from_notify_path(
			ctx,
			STRUCTSCANO(handle->data),
			smb_fname->base_name);
		if (test_base_name) {
			smb_fname->base_name = test_base_name;
			ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
			DEBUG(5, ("_stat: %s returned %d\n",
				  test_base_name, ret));
			smb_fname->base_name = tmp_base_name;
		}
	}
	return ret;
}

static int scannedonly_lstat(vfs_handle_struct * handle,
			     struct smb_filename *smb_fname)
{
	int ret;
	ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
	DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_lstat: %s returned %d\n",
				  smb_fname->base_name, ret));
	if (ret != 0 && errno == ENOENT) {
		TALLOC_CTX *ctx=talloc_tos();
		char *test_base_name, *tmp_base_name = smb_fname->base_name;
		/* possibly this was a fake name (file is being scanned for
		   viruses.txt): check for that and create the real name and
		   stat the real name */
		test_base_name = real_path_from_notify_path(
			ctx, STRUCTSCANO(handle->data), smb_fname->base_name);
		if (test_base_name) {
			smb_fname->base_name = test_base_name;
			ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
			DEBUG(5, ("_lstat: %s returned %d\n",
				  test_base_name, ret));
			smb_fname->base_name = tmp_base_name;
		}
	}
	return ret;
}

static int scannedonly_open(vfs_handle_struct * handle,
			    struct smb_filename *smb_fname,
			    files_struct * fsp, int flags, mode_t mode)
{
	const char *base;
	char *tmp, *shortname;
	int allowed, write_access = 0;
	TALLOC_CTX *ctx=talloc_tos();
	/* if open for writing ignore it */
	if ((flags & O_ACCMODE) == O_WRONLY) {
		return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
	}
	if ((flags & O_ACCMODE) == O_RDWR) {
		write_access = 1;
	}
	/* check if this file is scanned already */
	tmp = strrchr(smb_fname->base_name, '/');
	if (tmp) {
		base = talloc_strndup(ctx,smb_fname->base_name,
				      (tmp - smb_fname->base_name) + 1);
		shortname = tmp + 1;
	} else {
		base = "";
		shortname = (char *)smb_fname->base_name;
	}
	allowed = scannedonly_allow_access(
		handle, NULL, smb_fname, shortname,
		base,
		write_access,
		STRUCTSCANO(handle->data)->recheck_time_open,
		STRUCTSCANO(handle->data)->recheck_tries_open,
		STRUCTSCANO(handle->data)->recheck_size_open,
		0);
	flush_sendbuffer(handle);
	DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_open: allow=%d for %s\n",
				  allowed, smb_fname->base_name));
	if (allowed
	    || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
		return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
	}
	errno = EACCES;
	return -1;
}

static int scannedonly_close(vfs_handle_struct * handle, files_struct * fsp)
{
	/* we only have to notify the scanner
	   for files that were open readwrite or writable. */
	if (fsp->can_write) {
		TALLOC_CTX *ctx = talloc_tos();
		notify_scanner(handle, construct_full_path(
				       ctx,handle,
				       fsp->fsp_name->base_name,false));
		flush_sendbuffer(handle);
	}
	return SMB_VFS_NEXT_CLOSE(handle, fsp);
}

static int scannedonly_rename(vfs_handle_struct * handle,
			      const struct smb_filename *smb_fname_src,
			      const struct smb_filename *smb_fname_dst)
{
	/* rename the cache file before we pass the actual rename on */
	struct smb_filename *smb_fname_src_tmp = NULL;
	struct smb_filename *smb_fname_dst_tmp = NULL;
	char *cachefile_src, *cachefile_dst;
	bool needscandst=false;
	int ret;
	TALLOC_CTX *ctx = talloc_tos();

	/* Setup temporary smb_filename structs. */
	cachefile_src = cachefile_name_f_fullpath(
		ctx,
		smb_fname_src->base_name,
		STRUCTSCANO(handle->data)->p_scanned);
	cachefile_dst =	cachefile_name_f_fullpath(
		ctx,
		smb_fname_dst->base_name,
		STRUCTSCANO(handle->data)->p_scanned);
	create_synthetic_smb_fname(ctx, cachefile_src,NULL,NULL,
				   &smb_fname_src_tmp);
	create_synthetic_smb_fname(ctx, cachefile_dst,NULL,NULL,
				   &smb_fname_dst_tmp);

	ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp, smb_fname_dst_tmp);
	if (ret == ENOENT) {
		needscandst=true;
	} else if (ret != 0) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("failed to rename %s into %s error %d: %s\n", cachefile_src,
		       cachefile_dst, ret, strerror(ret)));
		needscandst=true;
	}
	ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
	if (ret == 0 && needscandst) {
		notify_scanner(handle, smb_fname_dst->base_name);
		flush_sendbuffer(handle);
	}
	return ret;
}

static int scannedonly_unlink(vfs_handle_struct * handle,
			      const struct smb_filename *smb_fname)
{
	/* unlink the 'scanned' file too */
	struct smb_filename *smb_fname_cache = NULL;
	char * cachefile;
	TALLOC_CTX *ctx = talloc_tos();

	cachefile = cachefile_name_f_fullpath(
		ctx,
		smb_fname->base_name,
		STRUCTSCANO(handle->data)->p_scanned);
	create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,
				   &smb_fname_cache);
	if (SMB_VFS_NEXT_UNLINK(handle, smb_fname_cache) != 0) {
		DEBUG(SCANNEDONLY_DEBUG, ("_unlink: failed to unlink %s\n",
					  smb_fname_cache->base_name));
	}
	return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
}

static int scannedonly_rmdir(vfs_handle_struct * handle, const char *path)
{
	/* if there are only .scanned: .virus: or .failed: files, we delete
	   those, because the client cannot see them */
	DIR *dirp;
	SMB_STRUCT_DIRENT *dire;
	TALLOC_CTX *ctx = talloc_tos();
	bool only_deletable_files = true, have_files = false;
	char *path_w_slash;

	if (!STRUCTSCANO(handle->data)->rm_hidden_files_on_rmdir)
		return SMB_VFS_NEXT_RMDIR(handle, path);

	path_w_slash = name_w_ending_slash(ctx,path);
	dirp = SMB_VFS_NEXT_OPENDIR(handle, path, NULL, 0);
	if (!dirp) {
		return -1;
	}
	while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL)) != NULL) {
		if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
			continue;
		}
		have_files = true;
		if (!is_scannedonly_file(STRUCTSCANO(handle->data),
					 dire->d_name)) {
			struct smb_filename *smb_fname = NULL;
			char *fullpath;
			int retval;

			if (STRUCTSCANO(handle->data)->show_special_files) {
				only_deletable_files = false;
				break;
			}
			/* stat the file and see if it is a
			   special file */
			fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
						  dire->d_name);
			create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
						   &smb_fname);
			retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
			if (retval == 0
			    && S_ISREG(smb_fname->st.st_ex_mode)) {
				only_deletable_files = false;
			}
			TALLOC_FREE(fullpath);
			TALLOC_FREE(smb_fname);
			break;
		}
	}
	DEBUG(SCANNEDONLY_DEBUG,
	      ("path=%s, have_files=%d, only_deletable_files=%d\n",
	       path, have_files, only_deletable_files));
	if (have_files && only_deletable_files) {
		DEBUG(SCANNEDONLY_DEBUG,
		      ("scannedonly_rmdir, remove leftover scannedonly "
		       "files from %s\n", path_w_slash));
		SMB_VFS_NEXT_REWINDDIR(handle, dirp);
		while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL))
		       != NULL) {
			char *fullpath;
			struct smb_filename *smb_fname = NULL;
			if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
				continue;
			}
			fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
						  dire->d_name);
			create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
						   &smb_fname);
			DEBUG(SCANNEDONLY_DEBUG, ("unlink %s\n", fullpath));
			SMB_VFS_NEXT_UNLINK(handle, smb_fname);
			TALLOC_FREE(fullpath);
			TALLOC_FREE(smb_fname);
		}
	}
	SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
	return SMB_VFS_NEXT_RMDIR(handle, path);
}

static void free_scannedonly_data(void **data)
{
	SAFE_FREE(*data);
}

static int scannedonly_connect(struct vfs_handle_struct *handle,
			       const char *service, const char *user)
{

	struct Tscannedonly *so;

	so = SMB_MALLOC_P(struct Tscannedonly);
	handle->data = (void *)so;
	handle->free_data = free_scannedonly_data;
	so->gsendbuffer[0]='\0';
	so->domain_socket =
		lp_parm_bool(SNUM(handle->conn), "scannedonly",
			     "domain_socket", True);
	so->socketname = lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly", "socketname",
					     "/var/lib/scannedonly/scan");

	so->portnum =
		lp_parm_int(SNUM(handle->conn), "scannedonly", "portnum",
			    2020);
	so->scanhost = lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly", "scanhost",
					     "localhost");

	so->show_special_files =
		lp_parm_bool(SNUM(handle->conn), "scannedonly",
			     "show_special_files", True);
	so->rm_hidden_files_on_rmdir =
		lp_parm_bool(SNUM(handle->conn), "scannedonly",
			     "rm_hidden_files_on_rmdir", True);
	so->hide_nonscanned_files =
		lp_parm_bool(SNUM(handle->conn), "scannedonly",
			     "hide_nonscanned_files", False);
	so->allow_nonscanned_files =
		lp_parm_bool(SNUM(handle->conn), "scannedonly",
			     "allow_nonscanned_files", False);
	so->scanning_message = lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly",
					     "scanning_message",
					     "is being scanned for viruses");
	so->scanning_message_len = strlen(so->scanning_message);
	so->recheck_time_open =
		lp_parm_int(SNUM(handle->conn), "scannedonly",
			    "recheck_time_open", 50);
	so->recheck_tries_open =
		lp_parm_int(SNUM(handle->conn), "scannedonly",
			    "recheck_tries_open", 100);
	so->recheck_size_open =
		lp_parm_int(SNUM(handle->conn), "scannedonly",
			    "recheck_size_open", 100);
	so->recheck_time_readdir =
		lp_parm_int(SNUM(handle->conn), "scannedonly",
			    "recheck_time_readdir", 50);
	so->recheck_tries_readdir =
		lp_parm_int(SNUM(handle->conn), "scannedonly",
			    "recheck_tries_readdir", 20);

	so->p_scanned =
		lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly",
					     "pref_scanned",
					     ".scanned:");
	so->p_virus =
		lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly",
					     "pref_virus",
					     ".virus:");
	so->p_failed =
		lp_parm_const_string(SNUM(handle->conn),
					     "scannedonly",
					     "pref_failed",
					     ".failed:");
	connect_to_scanner(handle);

	return SMB_VFS_NEXT_CONNECT(handle, service, user);
}

/* VFS operations structure */
static struct vfs_fn_pointers vfs_scannedonly_fns = {
	.opendir = scannedonly_opendir,
	.fdopendir = scannedonly_fdopendir,
	.readdir = scannedonly_readdir,
	.seekdir = scannedonly_seekdir,
	.telldir = scannedonly_telldir,
	.rewind_dir = scannedonly_rewinddir,
	.closedir = scannedonly_closedir,
	.rmdir = scannedonly_rmdir,
	.stat = scannedonly_stat,
	.lstat = scannedonly_lstat,
	.open_fn = scannedonly_open,
	.close_fn = scannedonly_close,
	.rename = scannedonly_rename,
	.unlink = scannedonly_unlink,
	.connect_fn = scannedonly_connect
};

NTSTATUS vfs_scannedonly_init(void)
{
	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "scannedonly",
				&vfs_scannedonly_fns);
}