AIX doesn't have MSG_DONTWAIT
[ira/wip.git] / source3 / modules / vfs_scannedonly.c
1 /*
2  * scannedonly VFS module for Samba 3.5
3  *
4  * Copyright 2007,2008,2009 (C) Olivier Sessink
5  *
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.
10  *
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.
15  *
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.
19  *
20  * ABOUT SCANNEDONLY
21  *
22  * scannedonly implements a 'filter' like vfs module that talks over a
23  * unix domain socket or over UDP to a anti-virus engine.
24  *
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)
38  *
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.
42  *
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).
47  *
48  */
49
50 #include "includes.h"
51
52 #include "config.h"
53
54 #define SENDBUFFERSIZE 1450
55
56 struct Tscannedonly {
57         int socket;
58         int domain_socket;
59         int portnum;
60         int scanning_message_len;
61         int recheck_time_open;
62         int recheck_tries_open;
63         int recheck_size_open;
64         int recheck_time_readdir;
65         int recheck_tries_readdir;
66         bool show_special_files;
67         bool rm_hidden_files_on_rmdir;
68         bool hide_nonscanned_files;
69         bool allow_nonscanned_files;
70         char *socketname;
71         char *scanhost;
72         char *scanning_message;
73         char *p_scanned; /* prefix for scanned files */
74         char *p_virus; /* prefix for virus containing files */
75         char *p_failed; /* prefix for failed to scan files */
76         char gsendbuffer[SENDBUFFERSIZE + 1];
77 };
78
79 #define STRUCTSCANO(var) ((struct Tscannedonly *)var)
80
81 struct scannedonly_DIR {
82         char *base;
83         int notify_loop_done;
84         SMB_STRUCT_DIR *DIR;
85 };
86 #define SCANNEDONLY_DEBUG 9
87 /*********************/
88 /* utility functions */
89 /*********************/
90
91 static char *real_path_from_notify_path(TALLOC_CTX *ctx,
92                                         struct Tscannedonly *so,
93                                         const char *path)
94 {
95         char *name;
96         int len, pathlen;
97
98         name = strrchr(path, '/');
99         if (!name) {
100                 return NULL;
101         }
102         pathlen = name - path;
103         name++;
104         len = strlen(name);
105         if (len <= so->scanning_message_len) {
106                 return NULL;
107         }
108
109         if (strcmp(name + (len - so->scanning_message_len),
110                    so->scanning_message) != 0) {
111                 return NULL;
112         }
113
114         return talloc_strndup(ctx,path,
115                               pathlen + len - so->scanning_message_len);
116 }
117
118 static char *cachefile_name(TALLOC_CTX *ctx,
119                             const char *shortname,
120                             const char *base,
121                             const char *p_scanned)
122 {
123         return talloc_asprintf(ctx, "%s%s%s", base, p_scanned, shortname);
124 }
125
126 static char *name_w_ending_slash(TALLOC_CTX *ctx, const char *name)
127 {
128         int len = strlen(name);
129         if (name[len - 1] == '/') {
130                 return talloc_strdup(ctx,name);
131         } else {
132                 return talloc_asprintf(ctx, "%s/", name);
133         }
134 }
135
136 static char *cachefile_name_f_fullpath(TALLOC_CTX *ctx,
137                                        const char *fullpath,
138                                        const char *p_scanned)
139 {
140         const char *base;
141         char *tmp, *cachefile, *shortname;
142         tmp = strrchr(fullpath, '/');
143         if (tmp) {
144                 base = talloc_strndup(ctx, fullpath, (tmp - fullpath) + 1);
145                 shortname = tmp + 1;
146         } else {
147                 base = "";
148                 shortname = (char *)fullpath;
149         }
150         cachefile = cachefile_name(ctx, shortname, base, p_scanned);
151         DEBUG(SCANNEDONLY_DEBUG,
152               ("cachefile_name_f_fullpath cachefile=%s\n", cachefile));
153         return cachefile;
154 }
155
156 static char *construct_full_path(TALLOC_CTX *ctx, vfs_handle_struct * handle,
157                                  const char *somepath, bool ending_slash)
158 {
159         char *tmp;
160
161         if (!somepath) {
162                 return NULL;
163         }
164         if (somepath[0] == '/') {
165                 if (ending_slash) {
166                         return name_w_ending_slash(ctx,somepath);
167                 }
168                 return talloc_strdup(ctx,somepath);
169         }
170         tmp=(char *)somepath;
171         if (tmp[0]=='.'&&tmp[1]=='/') {
172                 tmp+=2;
173         }
174         /* vfs_GetWd() seems to return a path with a slash */
175         if (ending_slash) {
176                 return talloc_asprintf(ctx, "%s/%s/",
177                                        vfs_GetWd(ctx, handle->conn),tmp);
178         }
179         return talloc_asprintf(ctx, "%s/%s",
180                                vfs_GetWd(ctx, handle->conn),tmp);
181 }
182
183 static int connect_to_scanner(vfs_handle_struct * handle)
184 {
185         struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
186
187         if (so->domain_socket) {
188                 struct sockaddr_un saun;
189                 DEBUG(SCANNEDONLY_DEBUG, ("socket=%s\n", so->socketname));
190                 if ((so->socket = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
191                         DEBUG(2, ("failed to create socket %s\n",
192                                   so->socketname));
193                         return -1;
194                 }
195                 saun.sun_family = AF_UNIX;
196                 strncpy(saun.sun_path, so->socketname,
197                         sizeof(saun.sun_path) - 1);
198                 if (connect(so->socket, (struct sockaddr *)(void *)&saun,
199                             SUN_LEN(&saun)) < 0) {
200                         DEBUG(2, ("failed to connect to socket %s\n",
201                                   so->socketname));
202                         return -1;
203                 }
204                 DEBUG(SCANNEDONLY_DEBUG,("bound %s to socket %d\n",
205                                          saun.sun_path, so->socket));
206
207         } else {
208                 so->socket = open_udp_socket(so->scanhost, so->portnum);
209                 if (so->socket < 0) {
210                         DEBUG(2,("failed to open UDP socket to %s:%d\n",
211                                  so->scanhost,so->portnum));
212                         return -1;
213                 }
214         }
215
216         {/* increasing the socket buffer is done because we have large bursts
217             of UDP packets or DGRAM's on a domain socket whenever we hit a
218             large directory with lots of unscanned files. */
219                 int sndsize;
220                 socklen_t size = sizeof(int);
221                 getsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
222                            (char *)&sndsize, &size);
223                 DEBUG(SCANNEDONLY_DEBUG, ("current socket buffer size=%d\n",
224                                           sndsize));
225                 sndsize = 262144;
226                 if (setsockopt(so->socket, SOL_SOCKET, SO_RCVBUF,
227                                (char *)&sndsize,
228                                (int)sizeof(sndsize)) != 0) {
229                         DEBUG(SCANNEDONLY_DEBUG,
230                               ("error setting socket buffer %s (%d)\n",
231                                strerror(errno), errno));
232                 }
233         }
234         set_blocking(so->socket, false);
235         return 0;
236 }
237
238 static void flush_sendbuffer(vfs_handle_struct * handle)
239 {
240         struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
241         int ret, len, loop = 10;
242         if (so->gsendbuffer[0] == '\0') {
243                 return;
244         }
245
246         do {
247                 loop--;
248                 len = strlen(so->gsendbuffer);
249                 ret = send(so->socket, so->gsendbuffer, len, 0);
250                 if (ret == len) {
251                         so->gsendbuffer[0] = '\0';
252                         break;
253                 }
254                 if (ret == -1) {
255                         DEBUG(3,("scannedonly flush_sendbuffer: "
256                                  "error sending on socket %d to scanner:"
257                                  " %s (%d)\n",
258                                  so->socket, strerror(errno), errno));
259                         if (errno == ECONNREFUSED || errno == ENOTCONN
260                             || errno == ECONNRESET) {
261                                 if (connect_to_scanner(handle) == -1)
262                                         break;  /* connecting fails, abort */
263                                 /* try again */
264                         } else if (errno != EINTR) {
265                                 /* on EINTR we just try again, all remaining
266                                    other errors we log the error
267                                    and try again ONCE */
268                                 loop = 1;
269                                 DEBUG(3,("scannedonly flush_sendbuffer: "
270                                          "error sending data to scanner: %s "
271                                          "(%d)\n", strerror(errno), errno));
272                         }
273                 } else {
274                         /* --> partial write: Resend all filenames that were
275                            not or not completely written. a partial filename
276                            written means the filename will not arrive correctly,
277                            so resend it completely */
278                         int pos = 0;
279                         while (pos < len) {
280                                 char *tmp = strchr(so->gsendbuffer+pos, '\n');
281                                 if (tmp && tmp - so->gsendbuffer < ret)
282                                         pos = tmp - so->gsendbuffer + 1;
283                                 else
284                                         break;
285                         }
286                         memmove(so->gsendbuffer, so->gsendbuffer + pos,
287                                 SENDBUFFERSIZE - ret);
288                         /* now try again */
289                 }
290         } while (loop > 0);
291
292         if (so->gsendbuffer[0] != '\0') {
293                 DEBUG(2,
294                       ("scannedonly flush_sendbuffer: "
295                        "failed to send files to AV scanner, "
296                        "discarding files."));
297                 so->gsendbuffer[0] = '\0';
298         }
299 }
300
301 static void notify_scanner(vfs_handle_struct * handle, const char *scanfile)
302 {
303         char *tmp;
304         int tmplen, gsendlen;
305         struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
306         TALLOC_CTX *ctx=talloc_tos();
307         if (scanfile[0] != '/') {
308                 tmp = construct_full_path(ctx,handle, scanfile, false);
309         } else {
310                 tmp = (char *)scanfile;
311         }
312         tmplen = strlen(tmp);
313         gsendlen = strlen(so->gsendbuffer);
314         DEBUG(SCANNEDONLY_DEBUG,
315               ("scannedonly notify_scanner: tmp=%s, tmplen=%d, gsendlen=%d\n",
316                tmp, tmplen, gsendlen));
317         if (gsendlen + tmplen >= SENDBUFFERSIZE) {
318                 flush_sendbuffer(handle);
319         }
320         strlcat(so->gsendbuffer, tmp, SENDBUFFERSIZE + 1);
321         strlcat(so->gsendbuffer, "\n", SENDBUFFERSIZE + 1);
322 }
323
324 static bool is_scannedonly_file(struct Tscannedonly *so, const char *shortname)
325 {
326         if (shortname[0]!='.') {
327                 return false;
328         }
329         if (strncmp(shortname, so->p_scanned, strlen(so->p_scanned)) == 0) {
330                 return true;
331         }
332         if (strncmp(shortname, so->p_virus, strlen(so->p_virus)) == 0) {
333                 return true;
334         }
335         if (strncmp(shortname, so->p_failed, strlen(so->p_failed)) == 0) {
336                 return true;
337         }
338         return false;
339 }
340
341 static bool timespec_is_newer(struct timespec *base, struct timespec *test)
342 {
343         return timespec_compare(base,test) < 0;
344 }
345
346 /*
347 vfs_handle_struct *handle the scannedonly handle
348 scannedonly_DIR * sDIR the scannedonly struct if called from _readdir()
349 or NULL
350 fullpath is a full path starting from / or a relative path to the
351 current working directory
352 shortname is the filename without directory components
353 basename, is the directory without file name component
354 allow_nonexistant return TRUE if stat() on the requested file fails
355 recheck_time, the time in milliseconds to wait for the daemon to
356 create a .scanned file
357 recheck_tries, the number of tries to wait
358 recheck_size, size in Kb of files that should not be waited for
359 loop : boolean if we should try to loop over all files in the directory
360 and send a notify to the scanner for all files that need scanning
361 */
362 static bool scannedonly_allow_access(vfs_handle_struct * handle,
363                                      struct scannedonly_DIR *sDIR,
364                                      struct smb_filename *smb_fname,
365                                      const char *shortname,
366                                      const char *base_name,
367                                      int allow_nonexistant,
368                                      int recheck_time, int recheck_tries,
369                                      int recheck_size, int loop)
370 {
371         struct smb_filename *cache_smb_fname;
372         TALLOC_CTX *ctx=talloc_tos();
373         char *cachefile;
374         int retval;
375         int didloop;
376         DEBUG(SCANNEDONLY_DEBUG,
377               ("smb_fname->base_name=%s, shortname=%s, base_name=%s\n"
378                ,smb_fname->base_name,shortname,base_name));
379
380         if (ISDOT(shortname) || ISDOTDOT(shortname)) {
381                 return true;
382         }
383         if (is_scannedonly_file(STRUCTSCANO(handle->data), shortname)) {
384                 DEBUG(SCANNEDONLY_DEBUG,
385                       ("scannedonly_allow_access, %s is a scannedonly file, "
386                        "return 0\n", shortname));
387                 return false;
388         }
389
390         if (!VALID_STAT(smb_fname->st)) {
391                 DEBUG(SCANNEDONLY_DEBUG,("stat %s\n",smb_fname->base_name));
392                 retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
393                 if (retval != 0) {
394                         /* failed to stat this file?!? --> hide it */
395                         DEBUG(SCANNEDONLY_DEBUG,("no valid stat, return"
396                                                  " allow_nonexistant=%d\n",
397                                                  allow_nonexistant));
398                         return allow_nonexistant;
399                 }
400         }
401         if (!S_ISREG(smb_fname->st.st_ex_mode)) {
402                 DEBUG(SCANNEDONLY_DEBUG,
403                       ("%s is not a regular file, ISDIR=%d\n",
404                        smb_fname->base_name,
405                        S_ISDIR(smb_fname->st.st_ex_mode)));
406                 return (STRUCTSCANO(handle->data)->
407                         show_special_files ||
408                         S_ISDIR(smb_fname->st.st_ex_mode));
409         }
410         if (smb_fname->st.st_ex_size == 0) {
411                 DEBUG(SCANNEDONLY_DEBUG,("empty file, return 1\n"));
412                 return true;    /* empty files cannot contain viruses ! */
413         }
414         cachefile = cachefile_name(ctx,
415                                    shortname,
416                                    base_name,
417                                    STRUCTSCANO(handle->data)->p_scanned);
418         create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,&cache_smb_fname);
419         if (!VALID_STAT(cache_smb_fname->st)) {
420                 retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
421         }
422         if (retval == 0 && VALID_STAT(cache_smb_fname->st)) {
423                 if (timespec_is_newer(&smb_fname->st.st_ex_mtime,
424                                       &cache_smb_fname->st.st_ex_mtime)) {
425                         talloc_free(cache_smb_fname);
426                         return true;
427                 }
428                 /* no cachefile or too old */
429                 SMB_VFS_NEXT_UNLINK(handle, cache_smb_fname);
430                 retval = -1;
431         }
432
433         notify_scanner(handle, smb_fname->base_name);
434
435         didloop = 0;
436         if (loop && sDIR && !sDIR->notify_loop_done) {
437                 /* check the rest of the directory and notify the
438                    scanner if some file needs scanning */
439                 long offset;
440                 SMB_STRUCT_DIRENT *dire;
441
442                 offset = SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
443                 dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, NULL);
444                 while (dire) {
445                         char *fpath2;
446                         struct smb_filename *smb_fname2;
447                         fpath2 = talloc_asprintf(ctx, "%s%s", base_name,dire->d_name);
448                         DEBUG(SCANNEDONLY_DEBUG,
449                               ("scannedonly_allow_access in loop, "
450                                "found %s\n", fpath2));
451                         create_synthetic_smb_fname(ctx, fpath2,NULL,NULL,
452                                                    &smb_fname2);
453                         scannedonly_allow_access(handle, NULL,
454                                                  smb_fname2,
455                                                  dire->d_name,
456                                                  base_name, 0, 0, 0, 0, 0);
457                         talloc_free(fpath2);
458                         talloc_free(smb_fname2);
459                         dire = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR,NULL);
460                 }
461                 sDIR->notify_loop_done = 1;
462                 didloop = 1;
463                 SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
464         }
465         if (recheck_time > 0
466             && ((recheck_size > 0
467                  && smb_fname->st.st_ex_size < (1024 * recheck_size))
468                 || didloop)) {
469                 int i = 0;
470                 flush_sendbuffer(handle);
471                 while (retval != 0      /*&& errno == ENOENT */
472                        && i < recheck_tries) {
473                         struct timespec req = { 0, recheck_time * 10000 };
474                         DEBUG(SCANNEDONLY_DEBUG,
475                               ("scannedonly_allow_access, wait (try=%d "
476                                "(max %d), %d ms) for %s\n",
477                                i, recheck_tries,
478                                recheck_time, cache_smb_fname->base_name));
479                         nanosleep(&req, NULL);
480                         retval = SMB_VFS_NEXT_STAT(handle, cache_smb_fname);
481                         i++;
482                 }
483         }
484         /* still no cachefile, or still too old, return 0 */
485         if (retval != 0
486             || !timespec_is_newer(&smb_fname->st.st_ex_mtime,
487                                   &cache_smb_fname->st.st_ex_mtime)) {
488                 DEBUG(SCANNEDONLY_DEBUG,
489                       ("retval=%d, return 0\n",retval));
490                 return false;
491         }
492         return true;
493 }
494
495 /*********************/
496 /* VFS functions     */
497 /*********************/
498
499 static SMB_STRUCT_DIR *scannedonly_opendir(vfs_handle_struct * handle,
500                                            const char *fname,
501                                            const char *mask, uint32 attr)
502 {
503         SMB_STRUCT_DIR *DIRp;
504         struct scannedonly_DIR *sDIR;
505
506         DIRp = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
507         if (!DIRp) {
508                 return NULL;
509         }
510
511         sDIR = TALLOC_P(NULL, struct scannedonly_DIR);
512         if (fname[0] != '/') {
513                 sDIR->base = construct_full_path(sDIR,handle, fname, true);
514         } else {
515                 sDIR->base = name_w_ending_slash(sDIR, fname);
516         }
517         DEBUG(SCANNEDONLY_DEBUG,
518                         ("scannedonly_opendir, fname=%s, base=%s\n",fname,sDIR->base));
519         sDIR->DIR = DIRp;
520         sDIR->notify_loop_done = 0;
521         return (SMB_STRUCT_DIR *) sDIR;
522 }
523
524 static SMB_STRUCT_DIRENT *scannedonly_readdir(vfs_handle_struct *handle,
525                                               SMB_STRUCT_DIR * dirp,
526                                               SMB_STRUCT_STAT *sbuf)
527 {
528         SMB_STRUCT_DIRENT *result;
529         int allowed = 0;
530         char *tmp;
531         struct smb_filename *smb_fname;
532         char *notify_name;
533         int namelen;
534         SMB_STRUCT_DIRENT *newdirent;
535         TALLOC_CTX *ctx=talloc_tos();
536
537         struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
538         if (!dirp) {
539                 return NULL;
540         }
541
542         result = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, sbuf);
543
544         if (!result)
545                 return NULL;
546
547         if (is_scannedonly_file(STRUCTSCANO(handle->data), result->d_name)) {
548                 DEBUG(SCANNEDONLY_DEBUG,
549                       ("scannedonly_readdir, %s is a scannedonly file, "
550                        "skip to next entry\n", result->d_name));
551                 return scannedonly_readdir(handle, dirp, NULL);
552         }
553         tmp = talloc_asprintf(ctx, "%s%s", sDIR->base, result->d_name);
554         DEBUG(SCANNEDONLY_DEBUG,
555               ("scannedonly_readdir, check access to %s (sbuf=%p)\n",
556                tmp,sbuf));
557
558         /* even if we don't hide nonscanned files or we allow non scanned
559            files we call allow_access because it will notify the daemon to
560            scan these files */
561         create_synthetic_smb_fname(ctx, tmp,NULL,
562                                    sbuf?VALID_STAT(*sbuf)?sbuf:NULL:NULL,
563                                    &smb_fname);
564         allowed = scannedonly_allow_access(
565                 handle, sDIR, smb_fname,
566                 result->d_name,
567                 sDIR->base, 0,
568                 STRUCTSCANO(handle->data)->hide_nonscanned_files
569                 ? STRUCTSCANO(handle->data)->recheck_time_readdir
570                 : 0,
571                 STRUCTSCANO(handle->data)->recheck_tries_readdir,
572                 -1,
573                 1);
574         DEBUG(SCANNEDONLY_DEBUG,
575               ("scannedonly_readdir access to %s (%s) = %d\n", tmp,
576                result->d_name, allowed));
577         if (allowed) {
578                 return result;
579         }
580         DEBUG(SCANNEDONLY_DEBUG,
581               ("hide_nonscanned_files=%d, allow_nonscanned_files=%d\n",
582                STRUCTSCANO(handle->data)->hide_nonscanned_files,
583                STRUCTSCANO(handle->data)->allow_nonscanned_files
584                       ));
585
586         if (!STRUCTSCANO(handle->data)->hide_nonscanned_files
587             || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
588                 return result;
589         }
590
591         DEBUG(SCANNEDONLY_DEBUG,
592               ("scannedonly_readdir, readdir listing for %s not "
593                "allowed, notify user\n", result->d_name));
594         notify_name = talloc_asprintf(
595                 ctx,"%s %s",result->d_name,
596                 STRUCTSCANO(handle->data)->scanning_message);
597         namelen = strlen(notify_name);
598         newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(
599                 ctx, char, sizeof(SMB_STRUCT_DIRENT) + namelen + 1);
600         if (!newdirent) {
601                 return NULL;
602         }
603         memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
604         memcpy(&newdirent->d_name, notify_name, namelen + 1);
605         DEBUG(SCANNEDONLY_DEBUG,
606               ("scannedonly_readdir, return newdirent at %p with "
607                "notification %s\n", newdirent, newdirent->d_name));
608         return newdirent;
609 }
610
611 static void scannedonly_seekdir(struct vfs_handle_struct *handle,
612                                 SMB_STRUCT_DIR * dirp, long offset)
613 {
614         struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
615         SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
616 }
617
618 static long scannedonly_telldir(struct vfs_handle_struct *handle,
619                                 SMB_STRUCT_DIR * dirp)
620 {
621         struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
622         return SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
623 }
624
625 static void scannedonly_rewinddir(struct vfs_handle_struct *handle,
626                                   SMB_STRUCT_DIR * dirp)
627 {
628         struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
629         SMB_VFS_NEXT_REWINDDIR(handle, sDIR->DIR);
630 }
631
632 static int scannedonly_closedir(vfs_handle_struct * handle,
633                                 SMB_STRUCT_DIR * dirp)
634 {
635         int retval;
636         struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
637         flush_sendbuffer(handle);
638         retval = SMB_VFS_NEXT_CLOSEDIR(handle, sDIR->DIR);
639         TALLOC_FREE(sDIR);
640         return retval;
641 }
642
643 static int scannedonly_stat(vfs_handle_struct * handle,
644                             struct smb_filename *smb_fname)
645 {
646         int ret;
647         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
648         DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_stat: %s returned %d\n",
649                                   smb_fname->base_name, ret));
650         if (ret != 0 && errno == ENOENT) {
651                 TALLOC_CTX *ctx=talloc_tos();
652                 char *test_base_name, *tmp_base_name = smb_fname->base_name;
653                 /* possibly this was a fake name (file is being scanned for
654                    viruses.txt): check for that and create the real name and
655                    stat the real name */
656                 test_base_name = real_path_from_notify_path(
657                         ctx,
658                         STRUCTSCANO(handle->data),
659                         smb_fname->base_name);
660                 if (test_base_name) {
661                         smb_fname->base_name = test_base_name;
662                         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
663                         DEBUG(5, ("_stat: %s returned %d\n",
664                                   test_base_name, ret));
665                         smb_fname->base_name = tmp_base_name;
666                 }
667         }
668         return ret;
669 }
670
671 static int scannedonly_lstat(vfs_handle_struct * handle,
672                              struct smb_filename *smb_fname)
673 {
674         int ret;
675         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
676         DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_lstat: %s returned %d\n",
677                                   smb_fname->base_name, ret));
678         if (ret != 0 && errno == ENOENT) {
679                 TALLOC_CTX *ctx=talloc_tos();
680                 char *test_base_name, *tmp_base_name = smb_fname->base_name;
681                 /* possibly this was a fake name (file is being scanned for
682                    viruses.txt): check for that and create the real name and
683                    stat the real name */
684                 test_base_name = real_path_from_notify_path(
685                         ctx, STRUCTSCANO(handle->data), smb_fname->base_name);
686                 if (test_base_name) {
687                         smb_fname->base_name = test_base_name;
688                         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
689                         DEBUG(5, ("_lstat: %s returned %d\n",
690                                   test_base_name, ret));
691                         smb_fname->base_name = tmp_base_name;
692                 }
693         }
694         return ret;
695 }
696
697 static int scannedonly_open(vfs_handle_struct * handle,
698                             struct smb_filename *smb_fname,
699                             files_struct * fsp, int flags, mode_t mode)
700 {
701         const char *base;
702         char *tmp, *shortname;
703         int allowed, write_access = 0;
704         TALLOC_CTX *ctx=talloc_tos();
705         /* if open for writing ignore it */
706         if ((flags & O_ACCMODE) == O_WRONLY) {
707                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
708         }
709         if ((flags & O_ACCMODE) == O_RDWR) {
710                 write_access = 1;
711         }
712         /* check if this file is scanned already */
713         tmp = strrchr(smb_fname->base_name, '/');
714         if (tmp) {
715                 base = talloc_strndup(ctx,smb_fname->base_name,
716                                       (tmp - smb_fname->base_name) + 1);
717                 shortname = tmp + 1;
718         } else {
719                 base = "";
720                 shortname = (char *)smb_fname->base_name;
721         }
722         allowed = scannedonly_allow_access(
723                 handle, NULL, smb_fname, shortname,
724                 base,
725                 write_access,
726                 STRUCTSCANO(handle->data)->recheck_time_open,
727                 STRUCTSCANO(handle->data)->recheck_tries_open,
728                 STRUCTSCANO(handle->data)->recheck_size_open,
729                 0);
730         flush_sendbuffer(handle);
731         DEBUG(SCANNEDONLY_DEBUG, ("scannedonly_open: allow=%d for %s\n",
732                                   allowed, smb_fname->base_name));
733         if (allowed
734             || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
735                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
736         }
737         errno = EACCES;
738         return -1;
739 }
740
741 static int scannedonly_close(vfs_handle_struct * handle, files_struct * fsp)
742 {
743         /* we only have to notify the scanner
744            for files that were open readwrite or writable. */
745         if (fsp->can_write) {
746                 TALLOC_CTX *ctx = talloc_tos();
747                 notify_scanner(handle, construct_full_path(
748                                        ctx,handle,
749                                        fsp->fsp_name->base_name,false));
750                 flush_sendbuffer(handle);
751         }
752         return SMB_VFS_NEXT_CLOSE(handle, fsp);
753 }
754
755 static int scannedonly_rename(vfs_handle_struct * handle,
756                               const struct smb_filename *smb_fname_src,
757                               const struct smb_filename *smb_fname_dst)
758 {
759         /* rename the cache file before we pass the actual rename on */
760         struct smb_filename *smb_fname_src_tmp = NULL;
761         struct smb_filename *smb_fname_dst_tmp = NULL;
762         char *cachefile_src, *cachefile_dst;
763         TALLOC_CTX *ctx = talloc_tos();
764
765         /* Setup temporary smb_filename structs. */
766         cachefile_src = cachefile_name_f_fullpath(
767                 ctx,
768                 smb_fname_src->base_name,
769                 STRUCTSCANO(handle->data)->p_scanned);
770         cachefile_dst = cachefile_name_f_fullpath(
771                 ctx,
772                 smb_fname_dst->base_name,
773                 STRUCTSCANO(handle->data)->p_scanned);
774
775         create_synthetic_smb_fname(ctx, cachefile_src,NULL,NULL,
776                                    &smb_fname_src_tmp);
777         create_synthetic_smb_fname(ctx, cachefile_dst,NULL,NULL,
778                                    &smb_fname_dst_tmp);
779
780         if (SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp, smb_fname_dst_tmp)
781             != 0) {
782                 DEBUG(SCANNEDONLY_DEBUG,
783                       ("failed to rename %s into %s\n", cachefile_src,
784                        cachefile_dst));
785         }
786         return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
787 }
788
789 static int scannedonly_unlink(vfs_handle_struct * handle,
790                               const struct smb_filename *smb_fname)
791 {
792         /* unlink the 'scanned' file too */
793         struct smb_filename *smb_fname_cache = NULL;
794         char * cachefile;
795         TALLOC_CTX *ctx = talloc_tos();
796
797         cachefile = cachefile_name_f_fullpath(
798                 ctx,
799                 smb_fname->base_name,
800                 STRUCTSCANO(handle->data)->p_scanned);
801         create_synthetic_smb_fname(ctx, cachefile,NULL,NULL,
802                                    &smb_fname_cache);
803         if (SMB_VFS_NEXT_UNLINK(handle, smb_fname_cache) != 0) {
804                 DEBUG(SCANNEDONLY_DEBUG, ("_unlink: failed to unlink %s\n",
805                                           smb_fname_cache->base_name));
806         }
807         return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
808 }
809
810 static int scannedonly_rmdir(vfs_handle_struct * handle, const char *path)
811 {
812         /* if there are only .scanned: .virus: or .failed: files, we delete
813            those, because the client cannot see them */
814         DIR *dirp;
815         SMB_STRUCT_DIRENT *dire;
816         TALLOC_CTX *ctx = talloc_tos();
817         bool only_deletable_files = true, have_files = false;
818         char *path_w_slash;
819
820         if (!STRUCTSCANO(handle->data)->rm_hidden_files_on_rmdir)
821                 return SMB_VFS_NEXT_RMDIR(handle, path);
822
823         path_w_slash = name_w_ending_slash(ctx,path);
824         dirp = SMB_VFS_NEXT_OPENDIR(handle, path, NULL, 0);
825         while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL)) != NULL) {
826                 if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
827                         continue;
828                 }
829                 have_files = true;
830                 if (!is_scannedonly_file(STRUCTSCANO(handle->data),
831                                          dire->d_name)) {
832                         struct smb_filename *smb_fname = NULL;
833                         char *fullpath;
834                         int retval;
835
836                         if (STRUCTSCANO(handle->data)->show_special_files) {
837                                 only_deletable_files = false;
838                                 break;
839                         }
840                         /* stat the file and see if it is a
841                            special file */
842                         fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
843                                                   dire->d_name);
844                         create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
845                                                    &smb_fname);
846                         retval = SMB_VFS_NEXT_STAT(handle, smb_fname);
847                         if (retval == 0
848                             && S_ISREG(smb_fname->st.st_ex_mode)) {
849                                 only_deletable_files = false;
850                         }
851                         TALLOC_FREE(fullpath);
852                         TALLOC_FREE(smb_fname);
853                         break;
854                 }
855         }
856         DEBUG(SCANNEDONLY_DEBUG,
857               ("path=%s, have_files=%d, only_deletable_files=%d\n",
858                path, have_files, only_deletable_files));
859         if (have_files && only_deletable_files) {
860                 DEBUG(SCANNEDONLY_DEBUG,
861                       ("scannedonly_rmdir, remove leftover scannedonly "
862                        "files from %s\n", path_w_slash));
863                 SMB_VFS_NEXT_REWINDDIR(handle, dirp);
864                 while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL))
865                        != NULL) {
866                         char *fullpath;
867                         struct smb_filename *smb_fname = NULL;
868                         if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
869                                 continue;
870                         }
871                         fullpath = talloc_asprintf(ctx, "%s%s", path_w_slash,
872                                                   dire->d_name);
873                         create_synthetic_smb_fname(ctx, fullpath,NULL,NULL,
874                                                    &smb_fname);
875                         DEBUG(SCANNEDONLY_DEBUG, ("unlink %s\n", fullpath));
876                         SMB_VFS_NEXT_UNLINK(handle, smb_fname);
877                         TALLOC_FREE(fullpath);
878                         TALLOC_FREE(smb_fname);
879                 }
880         }
881         return SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
882 }
883
884 static void free_scannedonly_data(void **data)
885 {
886         SAFE_FREE(*data);
887 }
888
889 static int scannedonly_connect(struct vfs_handle_struct *handle,
890                                const char *service, const char *user)
891 {
892
893         struct Tscannedonly *so;
894
895         so = SMB_MALLOC_P(struct Tscannedonly);
896         handle->data = (void *)so;
897         handle->free_data = free_scannedonly_data;
898         so->gsendbuffer[0]='\0';
899         so->domain_socket =
900                 lp_parm_bool(SNUM(handle->conn), "scannedonly",
901                              "domain_socket", True);
902         so->socketname =
903                 (char *)lp_parm_const_string(SNUM(handle->conn),
904                                              "scannedonly", "socketname",
905                                              "/var/lib/scannedonly/scan");
906         so->portnum =
907                 lp_parm_int(SNUM(handle->conn), "scannedonly", "portnum",
908                             2020);
909         so->scanhost =
910                 (char *)lp_parm_const_string(SNUM(handle->conn),
911                                              "scannedonly", "scanhost",
912                                              "localhost");
913
914         so->show_special_files =
915                 lp_parm_bool(SNUM(handle->conn), "scannedonly",
916                              "show_special_files", True);
917         so->rm_hidden_files_on_rmdir =
918                 lp_parm_bool(SNUM(handle->conn), "scannedonly",
919                              "rm_hidden_files_on_rmdir", True);
920         so->hide_nonscanned_files =
921                 lp_parm_bool(SNUM(handle->conn), "scannedonly",
922                              "hide_nonscanned_files", False);
923         so->allow_nonscanned_files =
924                 lp_parm_bool(SNUM(handle->conn), "scannedonly",
925                              "allow_nonscanned_files", False);
926         so->scanning_message =
927                 (char *)lp_parm_const_string(SNUM(handle->conn),
928                                              "scannedonly",
929                                              "scanning_message",
930                                              "is being scanned for viruses");
931         so->scanning_message_len = strlen(so->scanning_message);
932         so->recheck_time_open =
933                 lp_parm_int(SNUM(handle->conn), "scannedonly",
934                             "recheck_time_open", 50);
935         so->recheck_tries_open =
936                 lp_parm_int(SNUM(handle->conn), "scannedonly",
937                             "recheck_tries_open", 100);
938         so->recheck_size_open =
939                 lp_parm_int(SNUM(handle->conn), "scannedonly",
940                             "recheck_size_open", 100);
941         so->recheck_time_readdir =
942                 lp_parm_int(SNUM(handle->conn), "scannedonly",
943                             "recheck_time_readdir", 50);
944         so->recheck_tries_readdir =
945                 lp_parm_int(SNUM(handle->conn), "scannedonly",
946                             "recheck_tries_readdir", 20);
947
948         so->p_scanned =
949                 (char *)lp_parm_const_string(SNUM(handle->conn),
950                                              "scannedonly",
951                                              "pref_scanned",
952                                              ".scanned:");
953         so->p_virus =
954                 (char *)lp_parm_const_string(SNUM(handle->conn),
955                                              "scannedonly",
956                                              "pref_virus",
957                                              ".virus:");
958         so->p_failed =
959                 (char *)lp_parm_const_string(SNUM(handle->conn),
960                                              "scannedonly",
961                                              "pref_failed",
962                                              ".failed:");
963         connect_to_scanner(handle);
964
965         return SMB_VFS_NEXT_CONNECT(handle, service, user);
966 }
967
968 /* VFS operations structure */
969 static struct vfs_fn_pointers vfs_scannedonly_fns = {
970         .opendir = scannedonly_opendir,
971         .readdir = scannedonly_readdir,
972         .seekdir = scannedonly_seekdir,
973         .telldir = scannedonly_telldir,
974         .rewind_dir = scannedonly_rewinddir,
975         .closedir = scannedonly_closedir,
976         .rmdir = scannedonly_rmdir,
977         .stat = scannedonly_stat,
978         .lstat = scannedonly_lstat,
979         .open = scannedonly_open,
980         .close_fn = scannedonly_close,
981         .rename = scannedonly_rename,
982         .unlink = scannedonly_unlink,
983         .connect_fn = scannedonly_connect
984 };
985
986 NTSTATUS vfs_scannedonly_init(void)
987 {
988         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "scannedonly",
989                                 &vfs_scannedonly_fns);
990 }