Use is_default_acl variable in canonicalise_acl(). (cherry picked from commit 82e7132...
[Samba.git] / source3 / modules / vfs_scannedonly.c
blobd0eba212d240835e9460d9296aac4177ed38021c
1 /*
2 * scannedonly VFS module for Samba 3.5
4 * Copyright 2007,2008,2009,2010 (C) Olivier Sessink
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * ABOUT SCANNEDONLY
22 * scannedonly implements a 'filter' like vfs module that talks over a
23 * unix domain socket or over UDP to a anti-virus engine.
25 * files that are clean have a corresponding .scanned:{filename} file
26 * in the same directory. So why the .scanned: files? They take up
27 * only an inode, because they are 0 bytes. To test if the file is
28 * scanned only a stat() call on the filesystem is needed which is
29 * very quick compared to a database lookup. All modern filesystems
30 * use database technology such as balanced trees for lookups anyway.
31 * The number of inodes in modern filesystems is also not limiting
32 * anymore. The .scanned: files are also easy scriptable. You can
33 * remove them with a simple find command or create them with a
34 * simple touch command. Extended filesystem attributes have similar
35 * properties, but are not supported on all filesystems, so that
36 * would limit the usage of the module (and attributes are not as
37 * easily scriptable)
39 * files that are not clean are sent to the AV-engine. Only the
40 * filename is sent over the socket. The protocol is very simple:
41 * a newline separated list of filenames inside each datagram.
43 * a file AV-scan may be requested multiple times, the AV-engine
44 * should also check if the file has been scanned already. Requests
45 * can also be dropped by the AV-engine (and we thus don't need the
46 * reliability of TCP).
50 #include "includes.h"
51 #include "smbd/smbd.h"
52 #include "system/filesys.h"
54 #include "config.h"
56 #define SENDBUFFERSIZE 1450
58 #ifndef SUN_LEN
59 #define SUN_LEN(sunp) ((size_t)((struct sockaddr_un *)0)->sun_path \
60 + strlen((sunp)->sun_path))
61 #endif
64 struct Tscannedonly {
65 int socket;
66 int domain_socket;
67 int portnum;
68 int scanning_message_len;
69 int recheck_time_open;
70 int recheck_tries_open;
71 int recheck_size_open;
72 int recheck_time_readdir;
73 int recheck_tries_readdir;
74 bool show_special_files;
75 bool rm_hidden_files_on_rmdir;
76 bool hide_nonscanned_files;
77 bool allow_nonscanned_files;
78 char *socketname;
79 char *scanhost;
80 char *scanning_message;
81 char *p_scanned; /* prefix for scanned files */
82 char *p_virus; /* prefix for virus containing files */
83 char *p_failed; /* prefix for failed to scan files */
84 char gsendbuffer[SENDBUFFERSIZE + 1];
87 #define STRUCTSCANO(var) ((struct Tscannedonly *)var)
89 struct scannedonly_DIR {
90 char *base;
91 int notify_loop_done;
92 SMB_STRUCT_DIR *DIR;
94 #define SCANNEDONLY_DEBUG 9
95 /*********************/
96 /* utility functions */
97 /*********************/
99 static char *real_path_from_notify_path(TALLOC_CTX *ctx,
100 struct Tscannedonly *so,
101 const char *path)
103 char *name;
104 int len, pathlen;
106 name = strrchr(path, '/');
107 if (!name) {
108 return NULL;
110 pathlen = name - path;
111 name++;
112 len = strlen(name);
113 if (len <= so->scanning_message_len) {
114 return NULL;
117 if (strcmp(name + (len - so->scanning_message_len),
118 so->scanning_message) != 0) {
119 return NULL;
122 return talloc_strndup(ctx,path,
123 pathlen + len - so->scanning_message_len);
126 static char *cachefile_name(TALLOC_CTX *ctx,
127 const char *shortname,
128 const char *base,
129 const char *p_scanned)
131 return talloc_asprintf(ctx, "%s%s%s", base, p_scanned, shortname);
134 static char *name_w_ending_slash(TALLOC_CTX *ctx, const char *name)
136 int len = strlen(name);
137 if (name[len - 1] == '/') {
138 return talloc_strdup(ctx,name);
139 } else {
140 return talloc_asprintf(ctx, "%s/", name);
144 static char *cachefile_name_f_fullpath(TALLOC_CTX *ctx,
145 const char *fullpath,
146 const char *p_scanned)
148 const char *base;
149 char *tmp, *cachefile, *shortname;
150 tmp = strrchr(fullpath, '/');
151 if (tmp) {
152 base = talloc_strndup(ctx, fullpath, (tmp - fullpath) + 1);
153 shortname = tmp + 1;
154 } else {
155 base = "";
156 shortname = (char *)fullpath;
158 cachefile = cachefile_name(ctx, shortname, base, p_scanned);
159 DEBUG(SCANNEDONLY_DEBUG,
160 ("cachefile_name_f_fullpath cachefile=%s\n", cachefile));
161 return cachefile;
164 static char *construct_full_path(TALLOC_CTX *ctx, vfs_handle_struct * handle,
165 const char *somepath, bool ending_slash)
167 char *tmp;
169 if (!somepath) {
170 return NULL;
172 if (somepath[0] == '/') {
173 if (ending_slash) {
174 return name_w_ending_slash(ctx,somepath);
176 return talloc_strdup(ctx,somepath);
178 tmp=(char *)somepath;
179 if (tmp[0]=='.'&&tmp[1]=='/') {
180 tmp+=2;
182 /* vfs_GetWd() seems to return a path with a slash */
183 if (ending_slash) {
184 return talloc_asprintf(ctx, "%s/%s/",
185 vfs_GetWd(ctx, handle->conn),tmp);
187 return talloc_asprintf(ctx, "%s/%s",
188 vfs_GetWd(ctx, handle->conn),tmp);
191 static int connect_to_scanner(vfs_handle_struct * handle)
193 struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
195 if (so->domain_socket) {
196 struct sockaddr_un saun;
197 DEBUG(SCANNEDONLY_DEBUG, ("socket=%s\n", so->socketname));
198 if ((so->socket = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
199 DEBUG(2, ("failed to create socket %s\n",
200 so->socketname));
201 return -1;
203 saun.sun_family = AF_UNIX;
204 strncpy(saun.sun_path, so->socketname,
205 sizeof(saun.sun_path) - 1);
206 if (connect(so->socket, (struct sockaddr *)(void *)&saun,
207 SUN_LEN(&saun)) < 0) {
208 DEBUG(2, ("failed to connect to socket %s\n",
209 so->socketname));
210 return -1;
212 DEBUG(SCANNEDONLY_DEBUG,("bound %s to socket %d\n",
213 saun.sun_path, so->socket));
215 } else {
216 so->socket = open_udp_socket(so->scanhost, so->portnum);
217 if (so->socket < 0) {
218 DEBUG(2,("failed to open UDP socket to %s:%d\n",
219 so->scanhost,so->portnum));
220 return -1;
224 {/* increasing the socket buffer is done because we have large bursts
225 of UDP packets or DGRAM's on a domain socket whenever we hit a
226 large directory with lots of unscanned files. */
227 int sndsize;
228 socklen_t size = sizeof(int);
229 getsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
230 (char *)&sndsize, &size);
231 DEBUG(SCANNEDONLY_DEBUG, ("current socket buffer size=%d\n",
232 sndsize));
233 sndsize = 262144;
234 if (setsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
235 (char *)&sndsize,
236 (int)sizeof(sndsize)) != 0) {
237 DEBUG(SCANNEDONLY_DEBUG,
238 ("error setting socket buffer %s (%d)\n",
239 strerror(errno), errno));
242 set_blocking(so->socket, false);
243 return 0;
246 static void flush_sendbuffer(vfs_handle_struct * handle)
248 struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
249 int ret, len, loop = 10;
250 if (so->gsendbuffer[0] == '\0') {
251 return;
254 do {
255 loop--;
256 len = strlen(so->gsendbuffer);
257 ret = send(so->socket, so->gsendbuffer, len, 0);
258 if (ret == len) {
259 so->gsendbuffer[0] = '\0';
260 break;
262 if (ret == -1) {
263 DEBUG(3,("scannedonly flush_sendbuffer: "
264 "error sending on socket %d to scanner:"
265 " %s (%d)\n",
266 so->socket, strerror(errno), errno));
267 if (errno == ECONNREFUSED || errno == ENOTCONN
268 || errno == ECONNRESET) {
269 if (connect_to_scanner(handle) == -1)
270 break; /* connecting fails, abort */
271 /* try again */
272 } else if (errno != EINTR) {
273 /* on EINTR we just try again, all remaining
274 other errors we log the error
275 and try again ONCE */
276 loop = 1;
277 DEBUG(3,("scannedonly flush_sendbuffer: "
278 "error sending data to scanner: %s "
279 "(%d)\n", strerror(errno), errno));
281 } else {
282 /* --> partial write: Resend all filenames that were
283 not or not completely written. a partial filename
284 written means the filename will not arrive correctly,
285 so resend it completely */
286 int pos = 0;
287 while (pos < len) {
288 char *tmp = strchr(so->gsendbuffer+pos, '\n');
289 if (tmp && tmp - so->gsendbuffer < ret)
290 pos = tmp - so->gsendbuffer + 1;
291 else
292 break;
294 memmove(so->gsendbuffer, so->gsendbuffer + pos,
295 SENDBUFFERSIZE - ret);
296 /* now try again */
298 } while (loop > 0);
300 if (so->gsendbuffer[0] != '\0') {
301 DEBUG(2,
302 ("scannedonly flush_sendbuffer: "
303 "failed to send files to AV scanner, "
304 "discarding files."));
305 so->gsendbuffer[0] = '\0';
309 static void notify_scanner(vfs_handle_struct * handle, const char *scanfile)
311 char *tmp;
312 int tmplen, gsendlen;
313 struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
314 TALLOC_CTX *ctx=talloc_tos();
315 if (scanfile[0] != '/') {
316 tmp = construct_full_path(ctx,handle, scanfile, false);
317 } else {
318 tmp = (char *)scanfile;
320 tmplen = strlen(tmp);
321 gsendlen = strlen(so->gsendbuffer);
322 DEBUG(SCANNEDONLY_DEBUG,
323 ("scannedonly notify_scanner: tmp=%s, tmplen=%d, gsendlen=%d\n",
324 tmp, tmplen, gsendlen));
325 if (gsendlen + tmplen >= SENDBUFFERSIZE) {
326 flush_sendbuffer(handle);
328 strlcat(so->gsendbuffer, tmp, SENDBUFFERSIZE + 1);
329 strlcat(so->gsendbuffer, "\n", SENDBUFFERSIZE + 1);
332 static bool is_scannedonly_file(struct Tscannedonly *so, const char *shortname)
334 if (shortname[0]!='.') {
335 return false;
337 if (strncmp(shortname, so->p_scanned, strlen(so->p_scanned)) == 0) {
338 return true;
340 if (strncmp(shortname, so->p_virus, strlen(so->p_virus)) == 0) {
341 return true;
343 if (strncmp(shortname, so->p_failed, strlen(so->p_failed)) == 0) {
344 return true;
346 return false;
349 static bool timespec_is_newer(struct timespec *base, struct timespec *test)
351 return timespec_compare(base,test) < 0;
355 vfs_handle_struct *handle the scannedonly handle
356 scannedonly_DIR * sDIR the scannedonly struct if called from _readdir()
357 or NULL
358 fullpath is a full path starting from / or a relative path to the
359 current working directory
360 shortname is the filename without directory components
361 basename, is the directory without file name component
362 allow_nonexistant return TRUE if stat() on the requested file fails
363 recheck_time, the time in milliseconds to wait for the daemon to
364 create a .scanned file
365 recheck_tries, the number of tries to wait
366 recheck_size, size in Kb of files that should not be waited for
367 loop : boolean if we should try to loop over all files in the directory
368 and send a notify to the scanner for all files that need scanning
370 static bool scannedonly_allow_access(vfs_handle_struct * handle,
371 struct scannedonly_DIR *sDIR,
372 struct smb_filename *smb_fname,
373 const char *shortname,
374 const char *base_name,
375 int allow_nonexistant,
376 int recheck_time, int recheck_tries,
377 int recheck_size, int loop)
379 struct smb_filename *cache_smb_fname;
380 TALLOC_CTX *ctx=talloc_tos();
381 char *cachefile;
382 int retval = -1;
383 int didloop;
384 DEBUG(SCANNEDONLY_DEBUG,
385 ("smb_fname->base_name=%s, shortname=%s, base_name=%s\n"
386 ,smb_fname->base_name,shortname,base_name));
388 if (ISDOT(shortname) || ISDOTDOT(shortname)) {
389 return true;
391 if (is_scannedonly_file(STRUCTSCANO(handle->data), shortname)) {
392 DEBUG(SCANNEDONLY_DEBUG,
393 ("scannedonly_allow_access, %s is a scannedonly file, "
394 "return 0\n", shortname));
395 return false;
398 if (!VALID_STAT(smb_fname->st)) {
399 DEBUG(SCANNEDONLY_DEBUG,("stat %s\n",smb_fname->base_name));
400 retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
401 if (retval != 0) {
402 /* failed to stat this file?!? --> hide it */
403 DEBUG(SCANNEDONLY_DEBUG,("no valid stat, return"
404 " allow_nonexistant=%d\n",
405 allow_nonexistant));
406 return allow_nonexistant;
409 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
410 DEBUG(SCANNEDONLY_DEBUG,
411 ("%s is not a regular file, ISDIR=%d\n",
412 smb_fname->base_name,
413 S_ISDIR(smb_fname->st.st_ex_mode)));
414 return (STRUCTSCANO(handle->data)->
415 show_special_files ||
416 S_ISDIR(smb_fname->st.st_ex_mode));
418 if (smb_fname->st.st_ex_size == 0) {
419 DEBUG(SCANNEDONLY_DEBUG,("empty file, return 1\n"));
420 return true; /* empty files cannot contain viruses ! */
422 cachefile = cachefile_name(ctx,
423 shortname,
424 base_name,
425 STRUCTSCANO(handle->data)->p_scanned);
426 create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,&cache_smb_fname);
427 if (!VALID_STAT(cache_smb_fname->st)) {
428 retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
430 if (retval == 0 && VALID_STAT(cache_smb_fname->st)) {
431 if (timespec_is_newer(&smb_fname->st.st_ex_ctime,
432 &cache_smb_fname->st.st_ex_ctime)) {
433 talloc_free(cache_smb_fname);
434 return true;
436 /* no cachefile or too old */
437 SMB_VFS_NEXT_UNLINK(handle, cache_smb_fname);
438 retval = -1;
441 notify_scanner(handle, smb_fname->base_name);
443 didloop = 0;
444 if (loop && sDIR && !sDIR->notify_loop_done) {
445 /* check the rest of the directory and notify the
446 scanner if some file needs scanning */
447 long offset;
448 SMB_STRUCT_DIRENT *dire;
450 offset = SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
451 dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, NULL);
452 while (dire) {
453 char *fpath2;
454 struct smb_filename *smb_fname2;
455 fpath2 = talloc_asprintf(ctx, "%s%s", base_name,dire->d_name);
456 DEBUG(SCANNEDONLY_DEBUG,
457 ("scannedonly_allow_access in loop, "
458 "found %s\n", fpath2));
459 create_synthetic_smb_fname(ctx, fpath2,NULL,NULL,
460 &smb_fname2);
461 scannedonly_allow_access(handle, NULL,
462 smb_fname2,
463 dire->d_name,
464 base_name, 0, 0, 0, 0, 0);
465 talloc_free(fpath2);
466 talloc_free(smb_fname2);
467 dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR,NULL);
469 sDIR->notify_loop_done = 1;
470 didloop = 1;
471 SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
473 if (recheck_time > 0
474 && ((recheck_size > 0
475 && smb_fname->st.st_ex_size < (1024 * recheck_size))
476 || didloop)) {
477 int i = 0;
478 flush_sendbuffer(handle);
479 while (retval != 0 /*&& errno == ENOENT */
480 && i < recheck_tries) {
481 DEBUG(SCANNEDONLY_DEBUG,
482 ("scannedonly_allow_access, wait (try=%d "
483 "(max %d), %d ms) for %s\n",
484 i, recheck_tries,
485 recheck_time, cache_smb_fname->base_name));
486 smb_msleep(recheck_time);
487 retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
488 i++;
491 /* still no cachefile, or still too old, return 0 */
492 if (retval != 0
493 || !timespec_is_newer(&smb_fname->st.st_ex_ctime,
494 &cache_smb_fname->st.st_ex_ctime)) {
495 DEBUG(SCANNEDONLY_DEBUG,
496 ("retval=%d, return 0\n",retval));
497 return false;
499 return true;
502 /*********************/
503 /* VFS functions */
504 /*********************/
506 static SMB_STRUCT_DIR *scannedonly_opendir(vfs_handle_struct * handle,
507 const char *fname,
508 const char *mask, uint32 attr)
510 SMB_STRUCT_DIR *DIRp;
511 struct scannedonly_DIR *sDIR;
513 DIRp = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
514 if (!DIRp) {
515 return NULL;
518 sDIR = TALLOC_P(NULL, struct scannedonly_DIR);
519 if (fname[0] != '/') {
520 sDIR->base = construct_full_path(sDIR,handle, fname, true);
521 } else {
522 sDIR->base = name_w_ending_slash(sDIR, fname);
524 DEBUG(SCANNEDONLY_DEBUG,
525 ("scannedonly_opendir, fname=%s, base=%s\n",fname,sDIR->base));
526 sDIR->DIR = DIRp;
527 sDIR->notify_loop_done = 0;
528 return (SMB_STRUCT_DIR *) sDIR;
531 static SMB_STRUCT_DIR *scannedonly_fdopendir(vfs_handle_struct * handle,
532 files_struct *fsp,
533 const char *mask, uint32 attr)
535 SMB_STRUCT_DIR *DIRp;
536 struct scannedonly_DIR *sDIR;
537 const char *fname;
539 DIRp = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
540 if (!DIRp) {
541 return NULL;
544 fname = (const char *)fsp->fsp_name->base_name;
546 sDIR = TALLOC_P(NULL, struct scannedonly_DIR);
547 if (fname[0] != '/') {
548 sDIR->base = construct_full_path(sDIR,handle, fname, true);
549 } else {
550 sDIR->base = name_w_ending_slash(sDIR, fname);
552 DEBUG(SCANNEDONLY_DEBUG,
553 ("scannedonly_fdopendir, fname=%s, base=%s\n",fname,sDIR->base));
554 sDIR->DIR = DIRp;
555 sDIR->notify_loop_done = 0;
556 return (SMB_STRUCT_DIR *) sDIR;
560 static SMB_STRUCT_DIRENT *scannedonly_readdir(vfs_handle_struct *handle,
561 SMB_STRUCT_DIR * dirp,
562 SMB_STRUCT_STAT *sbuf)
564 SMB_STRUCT_DIRENT *result;
565 int allowed = 0;
566 char *tmp;
567 struct smb_filename *smb_fname;
568 char *notify_name;
569 int namelen;
570 SMB_STRUCT_DIRENT *newdirent;
571 TALLOC_CTX *ctx=talloc_tos();
573 struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
574 if (!dirp) {
575 return NULL;
578 result = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, sbuf);
580 if (!result)
581 return NULL;
583 if (is_scannedonly_file(STRUCTSCANO(handle->data), result->d_name)) {
584 DEBUG(SCANNEDONLY_DEBUG,
585 ("scannedonly_readdir, %s is a scannedonly file, "
586 "skip to next entry\n", result->d_name));
587 return scannedonly_readdir(handle, dirp, NULL);
589 tmp = talloc_asprintf(ctx, "%s%s", sDIR->base, result->d_name);
590 DEBUG(SCANNEDONLY_DEBUG,
591 ("scannedonly_readdir, check access to %s (sbuf=%p)\n",
592 tmp,sbuf));
594 /* even if we don't hide nonscanned files or we allow non scanned
595 files we call allow_access because it will notify the daemon to
596 scan these files */
597 create_synthetic_smb_fname(ctx, tmp,NULL,
598 sbuf?VALID_STAT(*sbuf)?sbuf:NULL:NULL,
599 &smb_fname);
600 allowed = scannedonly_allow_access(
601 handle, sDIR, smb_fname,
602 result->d_name,
603 sDIR->base, 0,
604 STRUCTSCANO(handle->data)->hide_nonscanned_files
605 ? STRUCTSCANO(handle->data)->recheck_time_readdir
606 : 0,
607 STRUCTSCANO(handle->data)->recheck_tries_readdir,
610 DEBUG(SCANNEDONLY_DEBUG,
611 ("scannedonly_readdir access to %s (%s) = %d\n", tmp,
612 result->d_name, allowed));
613 if (allowed) {
614 return result;
616 DEBUG(SCANNEDONLY_DEBUG,
617 ("hide_nonscanned_files=%d, allow_nonscanned_files=%d\n",
618 STRUCTSCANO(handle->data)->hide_nonscanned_files,
619 STRUCTSCANO(handle->data)->allow_nonscanned_files
622 if (!STRUCTSCANO(handle->data)->hide_nonscanned_files
623 || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
624 return result;
627 DEBUG(SCANNEDONLY_DEBUG,
628 ("scannedonly_readdir, readdir listing for %s not "
629 "allowed, notify user\n", result->d_name));
630 notify_name = talloc_asprintf(
631 ctx,"%s %s",result->d_name,
632 STRUCTSCANO(handle->data)->scanning_message);
633 namelen = strlen(notify_name);
634 newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(
635 ctx, char, sizeof(SMB_STRUCT_DIRENT) + namelen + 1);
636 if (!newdirent) {
637 return NULL;
639 memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
640 memcpy(&newdirent->d_name, notify_name, namelen + 1);
641 DEBUG(SCANNEDONLY_DEBUG,
642 ("scannedonly_readdir, return newdirent at %p with "
643 "notification %s\n", newdirent, newdirent->d_name));
644 return newdirent;
647 static void scannedonly_seekdir(struct vfs_handle_struct *handle,
648 SMB_STRUCT_DIR * dirp, long offset)
650 struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
651 SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
654 static long scannedonly_telldir(struct vfs_handle_struct *handle,
655 SMB_STRUCT_DIR * dirp)
657 struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
658 return SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
661 static void scannedonly_rewinddir(struct vfs_handle_struct *handle,
662 SMB_STRUCT_DIR * dirp)
664 struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
665 SMB_VFS_NEXT_REWINDDIR(handle, sDIR->DIR);
668 static int scannedonly_closedir(vfs_handle_struct * handle,
669 SMB_STRUCT_DIR * dirp)
671 int retval;
672 struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
673 flush_sendbuffer(handle);
674 retval = SMB_VFS_NEXT_CLOSEDIR(handle, sDIR->DIR);
675 TALLOC_FREE(sDIR);
676 return retval;
679 static int scannedonly_stat(vfs_handle_struct * handle,
680 struct smb_filename *smb_fname)
682 int ret;
683 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
684 DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_stat: %s returned %d\n",
685 smb_fname->base_name, ret));
686 if (ret != 0 && errno == ENOENT) {
687 TALLOC_CTX *ctx=talloc_tos();
688 char *test_base_name, *tmp_base_name = smb_fname->base_name;
689 /* possibly this was a fake name (file is being scanned for
690 viruses.txt): check for that and create the real name and
691 stat the real name */
692 test_base_name = real_path_from_notify_path(
693 ctx,
694 STRUCTSCANO(handle->data),
695 smb_fname->base_name);
696 if (test_base_name) {
697 smb_fname->base_name = test_base_name;
698 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
699 DEBUG(5, ("_stat: %s returned %d\n",
700 test_base_name, ret));
701 smb_fname->base_name = tmp_base_name;
704 return ret;
707 static int scannedonly_lstat(vfs_handle_struct * handle,
708 struct smb_filename *smb_fname)
710 int ret;
711 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
712 DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_lstat: %s returned %d\n",
713 smb_fname->base_name, ret));
714 if (ret != 0 && errno == ENOENT) {
715 TALLOC_CTX *ctx=talloc_tos();
716 char *test_base_name, *tmp_base_name = smb_fname->base_name;
717 /* possibly this was a fake name (file is being scanned for
718 viruses.txt): check for that and create the real name and
719 stat the real name */
720 test_base_name = real_path_from_notify_path(
721 ctx, STRUCTSCANO(handle->data), smb_fname->base_name);
722 if (test_base_name) {
723 smb_fname->base_name = test_base_name;
724 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
725 DEBUG(5, ("_lstat: %s returned %d\n",
726 test_base_name, ret));
727 smb_fname->base_name = tmp_base_name;
730 return ret;
733 static int scannedonly_open(vfs_handle_struct * handle,
734 struct smb_filename *smb_fname,
735 files_struct * fsp, int flags, mode_t mode)
737 const char *base;
738 char *tmp, *shortname;
739 int allowed, write_access = 0;
740 TALLOC_CTX *ctx=talloc_tos();
741 /* if open for writing ignore it */
742 if ((flags & O_ACCMODE) == O_WRONLY) {
743 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
745 if ((flags & O_ACCMODE) == O_RDWR) {
746 write_access = 1;
748 /* check if this file is scanned already */
749 tmp = strrchr(smb_fname->base_name, '/');
750 if (tmp) {
751 base = talloc_strndup(ctx,smb_fname->base_name,
752 (tmp - smb_fname->base_name) + 1);
753 shortname = tmp + 1;
754 } else {
755 base = "";
756 shortname = (char *)smb_fname->base_name;
758 allowed = scannedonly_allow_access(
759 handle, NULL, smb_fname, shortname,
760 base,
761 write_access,
762 STRUCTSCANO(handle->data)->recheck_time_open,
763 STRUCTSCANO(handle->data)->recheck_tries_open,
764 STRUCTSCANO(handle->data)->recheck_size_open,
766 flush_sendbuffer(handle);
767 DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_open: allow=%d for %s\n",
768 allowed, smb_fname->base_name));
769 if (allowed
770 || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
771 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
773 errno = EACCES;
774 return -1;
777 static int scannedonly_close(vfs_handle_struct * handle, files_struct * fsp)
779 /* we only have to notify the scanner
780 for files that were open readwrite or writable. */
781 if (fsp->can_write) {
782 TALLOC_CTX *ctx = talloc_tos();
783 notify_scanner(handle, construct_full_path(
784 ctx,handle,
785 fsp->fsp_name->base_name,false));
786 flush_sendbuffer(handle);
788 return SMB_VFS_NEXT_CLOSE(handle, fsp);
791 static int scannedonly_rename(vfs_handle_struct * handle,
792 const struct smb_filename *smb_fname_src,
793 const struct smb_filename *smb_fname_dst)
795 /* rename the cache file before we pass the actual rename on */
796 struct smb_filename *smb_fname_src_tmp = NULL;
797 struct smb_filename *smb_fname_dst_tmp = NULL;
798 char *cachefile_src, *cachefile_dst;
799 bool needscandst=false;
800 int ret;
801 TALLOC_CTX *ctx = talloc_tos();
803 /* Setup temporary smb_filename structs. */
804 cachefile_src = cachefile_name_f_fullpath(
805 ctx,
806 smb_fname_src->base_name,
807 STRUCTSCANO(handle->data)->p_scanned);
808 cachefile_dst = cachefile_name_f_fullpath(
809 ctx,
810 smb_fname_dst->base_name,
811 STRUCTSCANO(handle->data)->p_scanned);
812 create_synthetic_smb_fname(ctx, cachefile_src,NULL,NULL,
813 &smb_fname_src_tmp);
814 create_synthetic_smb_fname(ctx, cachefile_dst,NULL,NULL,
815 &smb_fname_dst_tmp);
817 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp, smb_fname_dst_tmp);
818 if (ret == ENOENT) {
819 needscandst=true;
820 } else if (ret != 0) {
821 DEBUG(SCANNEDONLY_DEBUG,
822 ("failed to rename %s into %s error %d: %s\n", cachefile_src,
823 cachefile_dst, ret, strerror(ret)));
824 needscandst=true;
826 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
827 if (ret == 0 && needscandst) {
828 notify_scanner(handle, smb_fname_dst->base_name);
829 flush_sendbuffer(handle);
831 return ret;
834 static int scannedonly_unlink(vfs_handle_struct * handle,
835 const struct smb_filename *smb_fname)
837 /* unlink the 'scanned' file too */
838 struct smb_filename *smb_fname_cache = NULL;
839 char * cachefile;
840 TALLOC_CTX *ctx = talloc_tos();
842 cachefile = cachefile_name_f_fullpath(
843 ctx,
844 smb_fname->base_name,
845 STRUCTSCANO(handle->data)->p_scanned);
846 create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,
847 &smb_fname_cache);
848 if (SMB_VFS_NEXT_UNLINK(handle, smb_fname_cache) != 0) {
849 DEBUG(SCANNEDONLY_DEBUG, ("_unlink: failed to unlink %s\n",
850 smb_fname_cache->base_name));
852 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
855 static int scannedonly_rmdir(vfs_handle_struct * handle, const char *path)
857 /* if there are only .scanned: .virus: or .failed: files, we delete
858 those, because the client cannot see them */
859 DIR *dirp;
860 SMB_STRUCT_DIRENT *dire;
861 TALLOC_CTX *ctx = talloc_tos();
862 bool only_deletable_files = true, have_files = false;
863 char *path_w_slash;
865 if (!STRUCTSCANO(handle->data)->rm_hidden_files_on_rmdir)
866 return SMB_VFS_NEXT_RMDIR(handle, path);
868 path_w_slash = name_w_ending_slash(ctx,path);
869 dirp = SMB_VFS_NEXT_OPENDIR(handle, path, NULL, 0);
870 if (!dirp) {
871 return -1;
873 while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL)) != NULL) {
874 if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
875 continue;
877 have_files = true;
878 if (!is_scannedonly_file(STRUCTSCANO(handle->data),
879 dire->d_name)) {
880 struct smb_filename *smb_fname = NULL;
881 char *fullpath;
882 int retval;
884 if (STRUCTSCANO(handle->data)->show_special_files) {
885 only_deletable_files = false;
886 break;
888 /* stat the file and see if it is a
889 special file */
890 fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
891 dire->d_name);
892 create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
893 &smb_fname);
894 retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
895 if (retval == 0
896 && S_ISREG(smb_fname->st.st_ex_mode)) {
897 only_deletable_files = false;
899 TALLOC_FREE(fullpath);
900 TALLOC_FREE(smb_fname);
901 break;
904 DEBUG(SCANNEDONLY_DEBUG,
905 ("path=%s, have_files=%d, only_deletable_files=%d\n",
906 path, have_files, only_deletable_files));
907 if (have_files && only_deletable_files) {
908 DEBUG(SCANNEDONLY_DEBUG,
909 ("scannedonly_rmdir, remove leftover scannedonly "
910 "files from %s\n", path_w_slash));
911 SMB_VFS_NEXT_REWINDDIR(handle, dirp);
912 while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL))
913 != NULL) {
914 char *fullpath;
915 struct smb_filename *smb_fname = NULL;
916 if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
917 continue;
919 fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
920 dire->d_name);
921 create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
922 &smb_fname);
923 DEBUG(SCANNEDONLY_DEBUG, ("unlink %s\n", fullpath));
924 SMB_VFS_NEXT_UNLINK(handle, smb_fname);
925 TALLOC_FREE(fullpath);
926 TALLOC_FREE(smb_fname);
929 SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
930 return SMB_VFS_NEXT_RMDIR(handle, path);
933 static void free_scannedonly_data(void **data)
935 SAFE_FREE(*data);
938 static int scannedonly_connect(struct vfs_handle_struct *handle,
939 const char *service, const char *user)
942 struct Tscannedonly *so;
944 so = SMB_MALLOC_P(struct Tscannedonly);
945 handle->data = (void *)so;
946 handle->free_data = free_scannedonly_data;
947 so->gsendbuffer[0]='\0';
948 so->domain_socket =
949 lp_parm_bool(SNUM(handle->conn), "scannedonly",
950 "domain_socket", True);
951 so->socketname =
952 (char *)lp_parm_const_string(SNUM(handle->conn),
953 "scannedonly", "socketname",
954 "/var/lib/scannedonly/scan");
955 so->portnum =
956 lp_parm_int(SNUM(handle->conn), "scannedonly", "portnum",
957 2020);
958 so->scanhost =
959 (char *)lp_parm_const_string(SNUM(handle->conn),
960 "scannedonly", "scanhost",
961 "localhost");
963 so->show_special_files =
964 lp_parm_bool(SNUM(handle->conn), "scannedonly",
965 "show_special_files", True);
966 so->rm_hidden_files_on_rmdir =
967 lp_parm_bool(SNUM(handle->conn), "scannedonly",
968 "rm_hidden_files_on_rmdir", True);
969 so->hide_nonscanned_files =
970 lp_parm_bool(SNUM(handle->conn), "scannedonly",
971 "hide_nonscanned_files", False);
972 so->allow_nonscanned_files =
973 lp_parm_bool(SNUM(handle->conn), "scannedonly",
974 "allow_nonscanned_files", False);
975 so->scanning_message =
976 (char *)lp_parm_const_string(SNUM(handle->conn),
977 "scannedonly",
978 "scanning_message",
979 "is being scanned for viruses");
980 so->scanning_message_len = strlen(so->scanning_message);
981 so->recheck_time_open =
982 lp_parm_int(SNUM(handle->conn), "scannedonly",
983 "recheck_time_open", 50);
984 so->recheck_tries_open =
985 lp_parm_int(SNUM(handle->conn), "scannedonly",
986 "recheck_tries_open", 100);
987 so->recheck_size_open =
988 lp_parm_int(SNUM(handle->conn), "scannedonly",
989 "recheck_size_open", 100);
990 so->recheck_time_readdir =
991 lp_parm_int(SNUM(handle->conn), "scannedonly",
992 "recheck_time_readdir", 50);
993 so->recheck_tries_readdir =
994 lp_parm_int(SNUM(handle->conn), "scannedonly",
995 "recheck_tries_readdir", 20);
997 so->p_scanned =
998 (char *)lp_parm_const_string(SNUM(handle->conn),
999 "scannedonly",
1000 "pref_scanned",
1001 ".scanned:");
1002 so->p_virus =
1003 (char *)lp_parm_const_string(SNUM(handle->conn),
1004 "scannedonly",
1005 "pref_virus",
1006 ".virus:");
1007 so->p_failed =
1008 (char *)lp_parm_const_string(SNUM(handle->conn),
1009 "scannedonly",
1010 "pref_failed",
1011 ".failed:");
1012 connect_to_scanner(handle);
1014 return SMB_VFS_NEXT_CONNECT(handle, service, user);
1017 /* VFS operations structure */
1018 static struct vfs_fn_pointers vfs_scannedonly_fns = {
1019 .opendir = scannedonly_opendir,
1020 .fdopendir = scannedonly_fdopendir,
1021 .readdir = scannedonly_readdir,
1022 .seekdir = scannedonly_seekdir,
1023 .telldir = scannedonly_telldir,
1024 .rewind_dir = scannedonly_rewinddir,
1025 .closedir = scannedonly_closedir,
1026 .rmdir = scannedonly_rmdir,
1027 .stat = scannedonly_stat,
1028 .lstat = scannedonly_lstat,
1029 .open_fn = scannedonly_open,
1030 .close_fn = scannedonly_close,
1031 .rename = scannedonly_rename,
1032 .unlink = scannedonly_unlink,
1033 .connect_fn = scannedonly_connect
1036 NTSTATUS vfs_scannedonly_init(void)
1038 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "scannedonly",
1039 &vfs_scannedonly_fns);