| 1 | /*
|
|---|
| 2 | * scannedonly VFS module for Samba 3.5
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 2007,2008,2009,2010 (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 | #include "smbd/smbd.h"
|
|---|
| 52 | #include "system/filesys.h"
|
|---|
| 53 |
|
|---|
| 54 | #include "config.h"
|
|---|
| 55 |
|
|---|
| 56 | #define SENDBUFFERSIZE 1450
|
|---|
| 57 |
|
|---|
| 58 | #ifndef SUN_LEN
|
|---|
| 59 | #define SUN_LEN(sunp) ((size_t)((struct sockaddr_un *)0)->sun_path \
|
|---|
| 60 | + strlen((sunp)->sun_path))
|
|---|
| 61 | #endif
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 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];
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | #define STRUCTSCANO(var) ((struct Tscannedonly *)var)
|
|---|
| 88 |
|
|---|
| 89 | struct scannedonly_DIR {
|
|---|
| 90 | char *base;
|
|---|
| 91 | int notify_loop_done;
|
|---|
| 92 | SMB_STRUCT_DIR *DIR;
|
|---|
| 93 | };
|
|---|
| 94 | #define SCANNEDONLY_DEBUG 9
|
|---|
| 95 | /*********************/
|
|---|
| 96 | /* utility functions */
|
|---|
| 97 | /*********************/
|
|---|
| 98 |
|
|---|
| 99 | static char *real_path_from_notify_path(TALLOC_CTX *ctx,
|
|---|
| 100 | struct Tscannedonly *so,
|
|---|
| 101 | const char *path)
|
|---|
| 102 | {
|
|---|
| 103 | char *name;
|
|---|
| 104 | int len, pathlen;
|
|---|
| 105 |
|
|---|
| 106 | name = strrchr(path, '/');
|
|---|
| 107 | if (!name) {
|
|---|
| 108 | return NULL;
|
|---|
| 109 | }
|
|---|
| 110 | pathlen = name - path;
|
|---|
| 111 | name++;
|
|---|
| 112 | len = strlen(name);
|
|---|
| 113 | if (len <= so->scanning_message_len) {
|
|---|
| 114 | return NULL;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | if (strcmp(name + (len - so->scanning_message_len),
|
|---|
| 118 | so->scanning_message) != 0) {
|
|---|
| 119 | return NULL;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | return talloc_strndup(ctx,path,
|
|---|
| 123 | pathlen + len - so->scanning_message_len);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static char *cachefile_name(TALLOC_CTX *ctx,
|
|---|
| 127 | const char *shortname,
|
|---|
| 128 | const char *base,
|
|---|
| 129 | const char *p_scanned)
|
|---|
| 130 | {
|
|---|
| 131 | return talloc_asprintf(ctx, "%s%s%s", base, p_scanned, shortname);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static char *name_w_ending_slash(TALLOC_CTX *ctx, const char *name)
|
|---|
| 135 | {
|
|---|
| 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);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static char *cachefile_name_f_fullpath(TALLOC_CTX *ctx,
|
|---|
| 145 | const char *fullpath,
|
|---|
| 146 | const char *p_scanned)
|
|---|
| 147 | {
|
|---|
| 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;
|
|---|
| 157 | }
|
|---|
| 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;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static char *construct_full_path(TALLOC_CTX *ctx, vfs_handle_struct * handle,
|
|---|
| 165 | const char *somepath, bool ending_slash)
|
|---|
| 166 | {
|
|---|
| 167 | char *tmp;
|
|---|
| 168 |
|
|---|
| 169 | if (!somepath) {
|
|---|
| 170 | return NULL;
|
|---|
| 171 | }
|
|---|
| 172 | if (somepath[0] == '/') {
|
|---|
| 173 | if (ending_slash) {
|
|---|
| 174 | return name_w_ending_slash(ctx,somepath);
|
|---|
| 175 | }
|
|---|
| 176 | return talloc_strdup(ctx,somepath);
|
|---|
| 177 | }
|
|---|
| 178 | tmp=(char *)somepath;
|
|---|
| 179 | if (tmp[0]=='.'&&tmp[1]=='/') {
|
|---|
| 180 | tmp+=2;
|
|---|
| 181 | }
|
|---|
| 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);
|
|---|
| 186 | }
|
|---|
| 187 | return talloc_asprintf(ctx, "%s/%s",
|
|---|
| 188 | vfs_GetWd(ctx, handle->conn),tmp);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static int connect_to_scanner(vfs_handle_struct * handle)
|
|---|
| 192 | {
|
|---|
| 193 | struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
|
|---|
| 194 |
|
|---|
| 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;
|
|---|
| 202 | }
|
|---|
| 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;
|
|---|
| 211 | }
|
|---|
| 212 | DEBUG(SCANNEDONLY_DEBUG,("bound %s to socket %d\n",
|
|---|
| 213 | saun.sun_path, so->socket));
|
|---|
| 214 |
|
|---|
| 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;
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 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));
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 | set_blocking(so->socket, false);
|
|---|
| 243 | return 0;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | static void flush_sendbuffer(vfs_handle_struct * handle)
|
|---|
| 247 | {
|
|---|
| 248 | struct Tscannedonly *so = (struct Tscannedonly *)handle->data;
|
|---|
| 249 | int ret, len, loop = 10;
|
|---|
| 250 | if (so->gsendbuffer[0] == '\0') {
|
|---|
| 251 | return;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 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;
|
|---|
| 261 | }
|
|---|
| 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));
|
|---|
| 280 | }
|
|---|
| 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;
|
|---|
| 293 | }
|
|---|
| 294 | memmove(so->gsendbuffer, so->gsendbuffer + pos,
|
|---|
| 295 | SENDBUFFERSIZE - ret);
|
|---|
| 296 | /* now try again */
|
|---|
| 297 | }
|
|---|
| 298 | } while (loop > 0);
|
|---|
| 299 |
|
|---|
| 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';
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | static void notify_scanner(vfs_handle_struct * handle, const char *scanfile)
|
|---|
| 310 | {
|
|---|
| 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;
|
|---|
| 319 | }
|
|---|
| 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);
|
|---|
| 327 | }
|
|---|
| 328 | strlcat(so->gsendbuffer, tmp, SENDBUFFERSIZE + 1);
|
|---|
| 329 | strlcat(so->gsendbuffer, "\n", SENDBUFFERSIZE + 1);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | static bool is_scannedonly_file(struct Tscannedonly *so, const char *shortname)
|
|---|
| 333 | {
|
|---|
| 334 | if (shortname[0]!='.') {
|
|---|
| 335 | return false;
|
|---|
| 336 | }
|
|---|
| 337 | if (strncmp(shortname, so->p_scanned, strlen(so->p_scanned)) == 0) {
|
|---|
| 338 | return true;
|
|---|
| 339 | }
|
|---|
| 340 | if (strncmp(shortname, so->p_virus, strlen(so->p_virus)) == 0) {
|
|---|
| 341 | return true;
|
|---|
| 342 | }
|
|---|
| 343 | if (strncmp(shortname, so->p_failed, strlen(so->p_failed)) == 0) {
|
|---|
| 344 | return true;
|
|---|
| 345 | }
|
|---|
| 346 | return false;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | static bool timespec_is_newer(struct timespec *base, struct timespec *test)
|
|---|
| 350 | {
|
|---|
| 351 | return timespec_compare(base,test) < 0;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /*
|
|---|
| 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
|
|---|
| 369 | */
|
|---|
| 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)
|
|---|
| 378 | {
|
|---|
| 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));
|
|---|
| 387 |
|
|---|
| 388 | if (ISDOT(shortname) || ISDOTDOT(shortname)) {
|
|---|
| 389 | return true;
|
|---|
| 390 | }
|
|---|
| 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;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 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;
|
|---|
| 407 | }
|
|---|
| 408 | }
|
|---|
| 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));
|
|---|
| 417 | }
|
|---|
| 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 ! */
|
|---|
| 421 | }
|
|---|
| 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);
|
|---|
| 429 | }
|
|---|
| 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;
|
|---|
| 435 | }
|
|---|
| 436 | /* no cachefile or too old */
|
|---|
| 437 | SMB_VFS_NEXT_UNLINK(handle, cache_smb_fname);
|
|---|
| 438 | retval = -1;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | notify_scanner(handle, smb_fname->base_name);
|
|---|
| 442 |
|
|---|
| 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;
|
|---|
| 449 |
|
|---|
| 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);
|
|---|
| 468 | }
|
|---|
| 469 | sDIR->notify_loop_done = 1;
|
|---|
| 470 | didloop = 1;
|
|---|
| 471 | SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
|
|---|
| 472 | }
|
|---|
| 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++;
|
|---|
| 489 | }
|
|---|
| 490 | }
|
|---|
| 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;
|
|---|
| 498 | }
|
|---|
| 499 | return true;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | /*********************/
|
|---|
| 503 | /* VFS functions */
|
|---|
| 504 | /*********************/
|
|---|
| 505 |
|
|---|
| 506 | static SMB_STRUCT_DIR *scannedonly_opendir(vfs_handle_struct * handle,
|
|---|
| 507 | const char *fname,
|
|---|
| 508 | const char *mask, uint32 attr)
|
|---|
| 509 | {
|
|---|
| 510 | SMB_STRUCT_DIR *DIRp;
|
|---|
| 511 | struct scannedonly_DIR *sDIR;
|
|---|
| 512 |
|
|---|
| 513 | DIRp = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
|
|---|
| 514 | if (!DIRp) {
|
|---|
| 515 | return NULL;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 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);
|
|---|
| 523 | }
|
|---|
| 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;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | static SMB_STRUCT_DIR *scannedonly_fdopendir(vfs_handle_struct * handle,
|
|---|
| 532 | files_struct *fsp,
|
|---|
| 533 | const char *mask, uint32 attr)
|
|---|
| 534 | {
|
|---|
| 535 | SMB_STRUCT_DIR *DIRp;
|
|---|
| 536 | struct scannedonly_DIR *sDIR;
|
|---|
| 537 | const char *fname;
|
|---|
| 538 |
|
|---|
| 539 | DIRp = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
|
|---|
| 540 | if (!DIRp) {
|
|---|
| 541 | return NULL;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | fname = (const char *)fsp->fsp_name->base_name;
|
|---|
| 545 |
|
|---|
| 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);
|
|---|
| 551 | }
|
|---|
| 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;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 | static SMB_STRUCT_DIRENT *scannedonly_readdir(vfs_handle_struct *handle,
|
|---|
| 561 | SMB_STRUCT_DIR * dirp,
|
|---|
| 562 | SMB_STRUCT_STAT *sbuf)
|
|---|
| 563 | {
|
|---|
| 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();
|
|---|
| 572 |
|
|---|
| 573 | struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
|
|---|
| 574 | if (!dirp) {
|
|---|
| 575 | return NULL;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | result = SMB_VFS_NEXT_READDIR(handle, sDIR->DIR, sbuf);
|
|---|
| 579 |
|
|---|
| 580 | if (!result)
|
|---|
| 581 | return NULL;
|
|---|
| 582 |
|
|---|
| 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);
|
|---|
| 588 | }
|
|---|
| 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));
|
|---|
| 593 |
|
|---|
| 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,
|
|---|
| 608 | -1,
|
|---|
| 609 | 1);
|
|---|
| 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;
|
|---|
| 615 | }
|
|---|
| 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
|
|---|
| 620 | ));
|
|---|
| 621 |
|
|---|
| 622 | if (!STRUCTSCANO(handle->data)->hide_nonscanned_files
|
|---|
| 623 | || STRUCTSCANO(handle->data)->allow_nonscanned_files) {
|
|---|
| 624 | return result;
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| 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;
|
|---|
| 638 | }
|
|---|
| 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;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | static void scannedonly_seekdir(struct vfs_handle_struct *handle,
|
|---|
| 648 | SMB_STRUCT_DIR * dirp, long offset)
|
|---|
| 649 | {
|
|---|
| 650 | struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
|
|---|
| 651 | SMB_VFS_NEXT_SEEKDIR(handle, sDIR->DIR, offset);
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | static long scannedonly_telldir(struct vfs_handle_struct *handle,
|
|---|
| 655 | SMB_STRUCT_DIR * dirp)
|
|---|
| 656 | {
|
|---|
| 657 | struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
|
|---|
| 658 | return SMB_VFS_NEXT_TELLDIR(handle, sDIR->DIR);
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | static void scannedonly_rewinddir(struct vfs_handle_struct *handle,
|
|---|
| 662 | SMB_STRUCT_DIR * dirp)
|
|---|
| 663 | {
|
|---|
| 664 | struct scannedonly_DIR *sDIR = (struct scannedonly_DIR *)dirp;
|
|---|
| 665 | SMB_VFS_NEXT_REWINDDIR(handle, sDIR->DIR);
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | static int scannedonly_closedir(vfs_handle_struct * handle,
|
|---|
| 669 | SMB_STRUCT_DIR * dirp)
|
|---|
| 670 | {
|
|---|
| 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;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | static int scannedonly_stat(vfs_handle_struct * handle,
|
|---|
| 680 | struct smb_filename *smb_fname)
|
|---|
| 681 | {
|
|---|
| 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;
|
|---|
| 702 | }
|
|---|
| 703 | }
|
|---|
| 704 | return ret;
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | static int scannedonly_lstat(vfs_handle_struct * handle,
|
|---|
| 708 | struct smb_filename *smb_fname)
|
|---|
| 709 | {
|
|---|
| 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;
|
|---|
| 728 | }
|
|---|
| 729 | }
|
|---|
| 730 | return ret;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | static int scannedonly_open(vfs_handle_struct * handle,
|
|---|
| 734 | struct smb_filename *smb_fname,
|
|---|
| 735 | files_struct * fsp, int flags, mode_t mode)
|
|---|
| 736 | {
|
|---|
| 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);
|
|---|
| 744 | }
|
|---|
| 745 | if ((flags & O_ACCMODE) == O_RDWR) {
|
|---|
| 746 | write_access = 1;
|
|---|
| 747 | }
|
|---|
| 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;
|
|---|
| 757 | }
|
|---|
| 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,
|
|---|
| 765 | 0);
|
|---|
| 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);
|
|---|
| 772 | }
|
|---|
| 773 | errno = EACCES;
|
|---|
| 774 | return -1;
|
|---|
| 775 | }
|
|---|
| 776 |
|
|---|
| 777 | static int scannedonly_close(vfs_handle_struct * handle, files_struct * fsp)
|
|---|
| 778 | {
|
|---|
| 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);
|
|---|
| 787 | }
|
|---|
| 788 | return SMB_VFS_NEXT_CLOSE(handle, fsp);
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 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)
|
|---|
| 794 | {
|
|---|
| 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();
|
|---|
| 802 |
|
|---|
| 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);
|
|---|
| 816 |
|
|---|
| 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;
|
|---|
| 825 | }
|
|---|
| 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);
|
|---|
| 830 | }
|
|---|
| 831 | return ret;
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | static int scannedonly_unlink(vfs_handle_struct * handle,
|
|---|
| 835 | const struct smb_filename *smb_fname)
|
|---|
| 836 | {
|
|---|
| 837 | /* unlink the 'scanned' file too */
|
|---|
| 838 | struct smb_filename *smb_fname_cache = NULL;
|
|---|
| 839 | char * cachefile;
|
|---|
| 840 | TALLOC_CTX *ctx = talloc_tos();
|
|---|
| 841 |
|
|---|
| 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));
|
|---|
| 851 | }
|
|---|
| 852 | return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | static int scannedonly_rmdir(vfs_handle_struct * handle, const char *path)
|
|---|
| 856 | {
|
|---|
| 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;
|
|---|
| 864 |
|
|---|
| 865 | if (!STRUCTSCANO(handle->data)->rm_hidden_files_on_rmdir)
|
|---|
| 866 | return SMB_VFS_NEXT_RMDIR(handle, path);
|
|---|
| 867 |
|
|---|
| 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;
|
|---|
| 872 | }
|
|---|
| 873 | while ((dire = SMB_VFS_NEXT_READDIR(handle, dirp, NULL)) != NULL) {
|
|---|
| 874 | if (ISDOT(dire->d_name) || ISDOTDOT(dire->d_name)) {
|
|---|
| 875 | continue;
|
|---|
| 876 | }
|
|---|
| 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;
|
|---|
| 883 |
|
|---|
| 884 | if (STRUCTSCANO(handle->data)->show_special_files) {
|
|---|
| 885 | only_deletable_files = false;
|
|---|
| 886 | break;
|
|---|
| 887 | }
|
|---|
| 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;
|
|---|
| 898 | }
|
|---|
| 899 | TALLOC_FREE(fullpath);
|
|---|
| 900 | TALLOC_FREE(smb_fname);
|
|---|
| 901 | break;
|
|---|
| 902 | }
|
|---|
| 903 | }
|
|---|
| 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;
|
|---|
| 918 | }
|
|---|
| 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);
|
|---|
| 927 | }
|
|---|
| 928 | }
|
|---|
| 929 | SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
|
|---|
| 930 | return SMB_VFS_NEXT_RMDIR(handle, path);
|
|---|
| 931 | }
|
|---|
| 932 |
|
|---|
| 933 | static void free_scannedonly_data(void **data)
|
|---|
| 934 | {
|
|---|
| 935 | SAFE_FREE(*data);
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | static int scannedonly_connect(struct vfs_handle_struct *handle,
|
|---|
| 939 | const char *service, const char *user)
|
|---|
| 940 | {
|
|---|
| 941 |
|
|---|
| 942 | struct Tscannedonly *so;
|
|---|
| 943 |
|
|---|
| 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");
|
|---|
| 962 |
|
|---|
| 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);
|
|---|
| 996 |
|
|---|
| 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);
|
|---|
| 1013 |
|
|---|
| 1014 | return SMB_VFS_NEXT_CONNECT(handle, service, user);
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 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
|
|---|
| 1034 | };
|
|---|
| 1035 |
|
|---|
| 1036 | NTSTATUS vfs_scannedonly_init(void)
|
|---|
| 1037 | {
|
|---|
| 1038 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "scannedonly",
|
|---|
| 1039 | &vfs_scannedonly_fns);
|
|---|
| 1040 | }
|
|---|