The branch, master has been updated
       via  2d4dda0... s3: Lift the version of the scannedonly VFS module
       via  31e1428... s3: Add the "scannedonly" vfs virus scanner interface 
module
      from  3d18439... Strip trailing spaces

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 2d4dda0688d5c88fb73ae17db970afe9d0f77f6a
Author: Volker Lendecke <[email protected]>
Date:   Tue Jan 12 22:58:51 2010 +0100

    s3: Lift the version of the scannedonly VFS module

commit 31e142854bbb29132143f895dee9568576175dd5
Author: Olivier Sessink <[email protected]>
Date:   Mon Jan 11 21:53:37 2010 +0100

    s3: Add the "scannedonly" vfs virus scanner interface module

-----------------------------------------------------------------------

Summary of changes:
 source3/Makefile.in               |    5 +
 source3/configure.in              |    2 +
 source3/modules/vfs_scannedonly.c |  995 +++++++++++++++++++++++++++++++++++++
 3 files changed, 1002 insertions(+), 0 deletions(-)
 create mode 100644 source3/modules/vfs_scannedonly.c


Changeset truncated at 500 lines:

diff --git a/source3/Makefile.in b/source3/Makefile.in
index bbacdb1..f87cb88 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -740,6 +740,7 @@ VFS_ONEFS_SHADOW_COPY_OBJ = modules/vfs_onefs_shadow_copy.o 
modules/onefs_shadow
 PERFCOUNT_ONEFS_OBJ = modules/perfcount_onefs.o
 PERFCOUNT_TEST_OBJ = modules/perfcount_test.o
 VFS_DIRSORT_OBJ = modules/vfs_dirsort.o
+VFS_SCANNEDONLY_OBJ = modules/vfs_scannedonly.o
 
 PLAINTEXT_AUTH_OBJ = auth/pampass.o auth/pass_check.o
 
@@ -2830,6 +2831,10 @@ bin/dirso...@shlibext@: $(BINARY_PREREQS) 
$(VFS_DIRSORT_OBJ)
        @echo "Building plugin $@"
        @$(SHLD_MODULE) $(VFS_DIRSORT_OBJ)
 
+bin/scannedon...@shlibext@: $(BINARY_PREREQS) $(VFS_SCANNEDONLY_OBJ)
+       @echo "Building plugin $@"
+       @$(SHLD_MODULE) $(VFS_SCANNEDONLY_OBJ)
+
 #########################################################
 ## IdMap NSS plugins
 
diff --git a/source3/configure.in b/source3/configure.in
index e3f53b4..d17cdac 100644
--- a/source3/configure.in
+++ b/source3/configure.in
@@ -439,6 +439,7 @@ default_shared_modules="$default_shared_modules vfs_acl_tdb"
 default_shared_modules="$default_shared_modules vfs_smb_traffic_analyzer"
 default_shared_modules="$default_shared_modules vfs_preopen"
 default_shared_modules="$default_shared_modules vfs_catia"
+default_shared_modules="$default_shared_modules vfs_scannedonly"
 
 if test "x$developer" = xyes; then
    default_static_modules="$default_static_modules rpc_rpcecho pdb_ads"
@@ -6552,6 +6553,7 @@ SMB_MODULE(vfs_smb_traffic_analyzer, 
\$(VFS_SMB_TRAFFIC_ANALYZER_OBJ), "bin/smb_
 SMB_MODULE(vfs_onefs, \$(VFS_ONEFS), "bin/onefs.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_onefs_shadow_copy, \$(VFS_ONEFS_SHADOW_COPY), 
"bin/onefs_shadow_copy.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_dirsort, \$(VFS_DIRSORT_OBJ), "bin/dirsort.$SHLIBEXT", VFS)
+SMB_MODULE(vfs_scannedonly, \$(VFS_SCANNEDONLY_OBJ), 
"bin/scannedonly.$SHLIBEXT", VFS)
 
 SMB_SUBSYSTEM(VFS,smbd/vfs.o)
 
diff --git a/source3/modules/vfs_scannedonly.c 
b/source3/modules/vfs_scannedonly.c
new file mode 100644
index 0000000..6995898
--- /dev/null
+++ b/source3/modules/vfs_scannedonly.c
@@ -0,0 +1,995 @@
+/*
+ * scannedonly VFS module for Samba 3.5
+ *
+ * Copyright 2007,2008,2009 (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 "config.h"
+
+#define SENDBUFFERSIZE 1450
+
+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;
+       char *socketname;
+       char *scanhost;
+       char *scanning_message;
+       char *p_scanned; /* prefix for scanned files */
+       char *p_virus; /* prefix for virus containing files */
+       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 notify_loop_done;
+       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, *shortname;
+       tmp = strrchr(fullpath, '/');
+       if (tmp) {
+               base = talloc_strndup(ctx, fullpath, (tmp - fullpath) + 1);
+               shortname = tmp + 1;
+       } else {
+               base = "";
+               shortname = (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 *path_plus_name(TALLOC_CTX *ctx, const char *base,
+                           const char *filename)
+{
+       return talloc_asprintf(ctx, "%s%s", base,filename);
+}
+
+static char *construct_full_path(TALLOC_CTX *ctx, vfs_handle_struct * handle,
+                                const char *somepath, bool ending_slash)
+{
+       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=(char *)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,
+                       strlen(so->socketname) + 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, MSG_DONTWAIT);
+               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)
+{
+       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 = (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);
+       }
+       strncat(so->gsendbuffer, tmp, tmplen);
+       strncat(so->gsendbuffer, "\n", 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;
+       int didloop;
+       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_mtime,
+                                     &cache_smb_fname->st.st_ex_mtime)) {
+                       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);
+
+       didloop = 0;
+       if (loop && sDIR && !sDIR->notify_loop_done) {
+               /* 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;


-- 
Samba Shared Repository

Reply via email to