clamd/onaccess_scth.c
7ee85372
 /*
  *  Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *
  *  Authors: Mickey Sola
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  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., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #if defined(FANOTIFY)
 
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <pthread.h>
 
 #include "shared/optparser.h"
 #include "shared/output.h"
 
 #include "others.h"
5f249e94
 #include "priv_fts.h"
7ee85372
 
 #include "onaccess_scth.h"
 
e0c481f8
 #include "libclamav/clamav.h"
 
 
 static int onas_scth_scanfile(const char *fname, int fd, int extinfo, struct scth_thrarg *tharg);
 static int onas_scth_handle_dir(const char *pathname, struct scth_thrarg *tharg);
 static int onas_scth_handle_file(const char *pathname, struct scth_thrarg *tharg);
d98d6fdb
 
 static void onas_scth_exit(int sig);
 
7ee85372
 static void onas_scth_exit(int sig) {
 	logg("*ScanOnAccess: onas_scth_exit(), signal %d\n", sig);
 
 	pthread_exit(NULL);
 }
 
e0c481f8
 static int onas_scth_scanfile(const char *fname, int fd, int extinfo, struct scth_thrarg *tharg)
 {
     int ret = 0;
     const char *virname;
 
     return onas_scan(fname, fd, &virname, tharg->engine, tharg->options, extinfo);
 }
 
 static int onas_scth_handle_dir(const char *pathname, struct scth_thrarg *tharg) {
7ee85372
 	FTS *ftsp = NULL;
0baa994c
 	int fd;
7ee85372
 	int ftspopts = FTS_PHYSICAL | FTS_XDEV;
0baa994c
 	int extinfo;
 	int ret;
7ee85372
 	FTSENT *curr = NULL;
 
0baa994c
 	extinfo = optget(tharg->opts, "ExtendedDetectionInfo")->enabled;
e0c481f8
 
d98d6fdb
 	char *const pathargv[] = { (char *) pathname, NULL };
e0c481f8
 	if (!(ftsp = _priv_fts_open(pathargv, ftspopts, NULL))) return CL_EOPEN;
7ee85372
 
5f249e94
 	while ((curr = _priv_fts_read(ftsp))) {
7ee85372
 		if (curr->fts_info != FTS_D) {
e0c481f8
 			if ((fd = safe_open(curr->fts_path, O_RDONLY | O_BINARY)) == -1)
                             return CL_EOPEN;
 
                         if (onas_scth_scanfile(curr->fts_path, fd, extinfo, tharg) == CL_VIRUS);
                             ret = CL_VIRUS;
 
 			close(fd);
7ee85372
 		}
 	}
 
e0c481f8
 	return ret;
7ee85372
 }
 
 
e0c481f8
 static int onas_scth_handle_file(const char *pathname, struct scth_thrarg *tharg) {
 	int fd;
0baa994c
 	int extinfo;
 	int ret;
e0c481f8
 
 	if (!pathname) return CL_ENULLARG;
7ee85372
 
0baa994c
 	extinfo = optget(tharg->opts, "ExtendedDetectionInfo")->enabled;
7ee85372
 
e0c481f8
 	if ((fd = safe_open(pathname, O_RDONLY | O_BINARY)) == -1)
 		return CL_EOPEN;
 	ret = onas_scth_scanfile(pathname, fd, extinfo, tharg);
0baa994c
 
 	close(fd);
e0c481f8
 
 	return ret;
7ee85372
 }
 
 void *onas_scan_th(void *arg) {
 	struct scth_thrarg *tharg = (struct scth_thrarg *) arg;
 	sigset_t sigset;
 	struct sigaction act;
 
 	/* ignore all signals except SIGUSR1 */
 	sigfillset(&sigset);
 	sigdelset(&sigset, SIGUSR1);
 	/* The behavior of a process is undefined after it ignores a
 	 * SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */
 	sigdelset(&sigset, SIGFPE);
 	sigdelset(&sigset, SIGILL);
 	sigdelset(&sigset, SIGSEGV);
 #ifdef SIGBUS
 	sigdelset(&sigset, SIGBUS);
 #endif
 	pthread_sigmask(SIG_SETMASK, &sigset, NULL);
 	memset(&act, 0, sizeof(struct sigaction));
 	act.sa_handler = onas_scth_exit;
 	sigfillset(&(act.sa_mask));
 	sigaction(SIGUSR1, &act, NULL);
 	sigaction(SIGSEGV, &act, NULL);
 
 	if (tharg->options & ONAS_SCTH_ISDIR) {
 		logg("ScanOnAccess: Performing additional scanning on directory '%s'\n", tharg->pathname);
e0c481f8
 		onas_scth_handle_dir(tharg->pathname, tharg);
7ee85372
 	} else if (tharg->options & ONAS_SCTH_ISFILE) {
 		logg("ScanOnAccess: Performing additional scanning on file '%s'\n", tharg->pathname);
e0c481f8
 		onas_scth_handle_file(tharg->pathname, tharg);
7ee85372
 	}
 
 	free(tharg->pathname);
 	tharg->pathname = NULL;
 
 	return NULL;
 }
 #endif