clamav-devel/clamd/server-th.c
e3aaff8e
 /*
81131381
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
c238ac42
  *			      Trog <trog@clamav.net>
e3aaff8e
  *
  *  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 2 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.
  */
 
 #include <pthread.h>
c238ac42
 #include <errno.h>
e3aaff8e
 #include <signal.h>
c238ac42
 #include <stdio.h>
 #include <time.h>
e3aaff8e
 
 #include "server.h"
c238ac42
 #include "thrmgr.h"
81131381
 #include "session.h"
c238ac42
 #include "defaults.h"
 #include "clamuko.h"
 #include "others.h"
e3aaff8e
 
c238ac42
 #define BUFFSIZE 1024
 #define FALSE (0)
 #define TRUE (1)
e3aaff8e
 
c238ac42
 int progexit = 0;
 pthread_mutex_t exit_mutex;
 int reload = 0;
 pthread_mutex_t reload_mutex;
e8c9ccdb
 
c238ac42
 typedef struct client_conn_tag {
     int sd;
     int options;
     const struct cfgstruct *copt;
     const struct cl_node *root;
     const struct cl_limits *limits;
 } client_conn_t;
81131381
 
c238ac42
 void scanner_thread(void *arg)
e3aaff8e
 {
c238ac42
 	client_conn_t *conn = (client_conn_t *) arg;
e3aaff8e
 	sigset_t sigset;
81131381
 	int ret;
e3aaff8e
 
c238ac42
 
e3aaff8e
     /* ignore all signals */
     sigfillset(&sigset);
     pthread_sigmask(SIG_SETMASK, &sigset, NULL);
 
c238ac42
     ret = command(conn->sd, conn->root, conn->limits, conn->options, conn->copt);
e3aaff8e
 
81131381
     switch(ret) {
 	case COMMAND_QUIT:
c238ac42
 	    pthread_mutex_lock(&exit_mutex);
81131381
 	    progexit = 1;
c238ac42
 	    pthread_mutex_unlock(&exit_mutex);
81131381
 	    break;
e3aaff8e
 
81131381
 	case COMMAND_RELOAD:
c238ac42
 	    pthread_mutex_lock(&reload_mutex);
81131381
 	    reload = 1;
c238ac42
 	    pthread_mutex_unlock(&reload_mutex);
81131381
 	    break;
c238ac42
 	}
e3aaff8e
 
c238ac42
     close(conn->sd);
     free(conn);
     return;
e3aaff8e
 }
 
c238ac42
 void sighandler_th(int sig)
e3aaff8e
 {
c238ac42
 	time_t currtime;
 	int maxwait = CL_DEFAULT_MAXWHILEWAIT * 5;
 	int i;
e3aaff8e
 
c238ac42
     switch(sig) {
 	case SIGINT:
 	case SIGTERM:
 	    progexit = 1;
 	    logg("*Signal %d caught -> exiting.\n", sig);
 	    time(&currtime);
 	    logg("--- Stopped at %s", ctime(&currtime));
 	    exit(0);
 	    break; /* not reached */
e3aaff8e
 
c238ac42
 	    case SIGSEGV:
 		logg("Segmentation fault :-( Bye..\n");
 		exit(11); /* probably not reached at all */
 		break; /* not reached */
e3aaff8e
 
c238ac42
 	    case SIGHUP:
 		/* sighup = 1;
 		logg("SIGHUP catched: log file re-opened.\n"); */
 		break;
     }
 }
e3aaff8e
 
c238ac42
 static struct cl_node *reload_db(struct cl_node *root, const struct cfgstruct *copt, int do_check)
 {
 	char *dbdir;
 	int virnum=0, retval;
 	struct cfgstruct *cpt;
 	static struct cl_stat *dbstat=NULL;
e3aaff8e
 
9e431a95
 
c238ac42
     if(do_check) {
 	if(dbstat == NULL) {
 	    logg("No stats for Database check - forcing reload\n");
 	    return root;
e3aaff8e
 	}
 
c238ac42
 	if(cl_statchkdir(dbstat) == 1) {
 	    logg("SelfCheck: Database modification detected. Forcing reload.\n");
 	    return root;
 	} else {
 	    logg("SelfCheck: Database status OK.\n");
 	    return NULL;
e3aaff8e
 	}
c238ac42
     }
e3aaff8e
 
c238ac42
     /* release old structure */
     if(root) {
 	cl_freetrie(root);
 	root = NULL;
     }
e3aaff8e
 
c238ac42
     if((cpt = cfgopt(copt, "DatabaseDirectory")) || (cpt = cfgopt(copt, "DataDirectory"))) {
 	dbdir = cpt->strarg;
     } else {
 	dbdir = cl_retdbdir();
     }
     logg("Reading databases from %s\n", dbdir);
e3aaff8e
 
c238ac42
     if(dbstat == NULL) {
 	dbstat = (struct cl_stat *) mmalloc(sizeof(struct cl_stat));
     } else {
 	cl_statfree(dbstat);
     }
46c2e927
 
c238ac42
     memset(dbstat, 0, sizeof(struct cl_stat));
     cl_statinidir(dbdir, dbstat);
     if((retval = cl_loaddbdir(dbdir, &root, &virnum))) {
 	logg("!reload db failed: %s\n", cl_strerror(retval));
 	exit(-1);
     }
46c2e927
 
c238ac42
     if(!root) {
 	logg("!load db failed: %s\n", cl_strerror(retval));
 	exit(-1);
     }
e3aaff8e
 
c238ac42
     if((retval = cl_buildtrie(root)) != 0) {
 	logg("!Database initialization error: can't build the trie: %s\n",
 	cl_strerror(retval));
 	exit(-1);
e3aaff8e
     }
c238ac42
     logg("Database correctly reloaded (%d viruses)\n", virnum);
e3aaff8e
 
c238ac42
     return root;
e3aaff8e
 }
 
8ac7b1ce
 int acceptloop_th(int socketd, struct cl_node *root, const struct cfgstruct *copt)
e3aaff8e
 {
c238ac42
 	int new_sd, max_threads, options=0;
 	thrmgr_t thrmgr;
 	struct sigaction sigact;
 	mode_t old_umask;
e3aaff8e
 	struct cl_limits limits;
 	pthread_attr_t thattr;
 	sigset_t sigset;
c238ac42
 	client_conn_t *client_conn;
 	struct cfgstruct *cpt;
 	char *buff[BUFFSIZE+1];
 	unsigned int selfchk;
 	time_t start_time, current_time;
 	
e3aaff8e
 #if defined(C_BIGSTACK) || defined(C_BSD)
c238ac42
         size_t stacksize;
e3aaff8e
 #endif
 
c238ac42
 #ifdef CLAMUKO
 	pthread_t clamuko_pid;
 	pthread_attr_t clamuko_attr;
 	struct thrarg *tharg;
 #endif
 	memset(&sigact, 0, sizeof(struct sigaction));
e3aaff8e
 
     /* save the PID */
     if((cpt = cfgopt(copt, "PidFile"))) {
 	    FILE *fd;
c238ac42
 	old_umask = umask(0006);
e3aaff8e
 	if((fd = fopen(cpt->strarg, "w")) == NULL) {
 	    logg("!Can't save PID in file %s\n", cpt->strarg);
 	} else {
 	    fprintf(fd, "%d", getpid());
 	    fclose(fd);
 	}
 	umask(old_umask);
     }
 
     logg("*Listening daemon: PID: %d\n", getpid());
c238ac42
     if((cpt = cfgopt(copt, "MaxThreads"))) {
 	max_threads = cpt->numarg;
     } else {
 	max_threads = CL_DEFAULT_MAXTHREADS;
     }
e3aaff8e
 
     if(cfgopt(copt, "ScanArchive") || cfgopt(copt, "ClamukoScanArchive")) {
 
 	/* set up limits */
8adf0608
 	memset(&limits, 0, sizeof(struct cl_limits));
e3aaff8e
 
 	if((cpt = cfgopt(copt, "ArchiveMaxFileSize"))) {
c238ac42
 	    if((limits.maxfilesize = cpt->numarg)) {
e3aaff8e
 		logg("Archive: Archived file size limit set to %d bytes.\n", limits.maxfilesize);
c238ac42
 	    } else {
e3aaff8e
 		logg("^Archive: File size limit protection disabled.\n");
c238ac42
 	    }
e3aaff8e
 	} else {
 	    limits.maxfilesize = 10485760;
 	    logg("^USING HARDCODED LIMIT: Archive: Archived file size limit set to %d bytes.\n", limits.maxfilesize);
 	}
 
 	if((cpt = cfgopt(copt, "ArchiveMaxRecursion"))) {
c238ac42
 	    if((limits.maxreclevel = cpt->numarg)) {
e3aaff8e
 		logg("Archive: Recursion level limit set to %d.\n", limits.maxreclevel);
c238ac42
 	    } else {
e3aaff8e
 		logg("^Archive: Recursion level limit protection disabled.\n");
c238ac42
 	    }
e3aaff8e
 	} else {
 	    limits.maxreclevel = 5;
 	    logg("^USING HARDCODED LIMIT: Archive: Recursion level set to %d.\n", limits.maxreclevel);
 	}
 
 	if((cpt = cfgopt(copt, "ArchiveMaxFiles"))) {
c238ac42
 	    if((limits.maxfiles = cpt->numarg)) {
e3aaff8e
 		logg("Archive: Files limit set to %d.\n", limits.maxfiles);
c238ac42
 	    } else {
e3aaff8e
 		logg("^Archive: Files limit protection disabled.\n");
c238ac42
 	    }
e3aaff8e
 	} else {
 	    limits.maxfiles = 1000;
 	    logg("^USING HARDCODED LIMIT: Archive: Files limit set to %d.\n", limits.maxfiles);
 	}
 
a6945b5d
 	if((cpt = cfgopt(copt, "ArchiveMaxCompressionRatio"))) {
c238ac42
 	    if((limits.maxratio = cpt->numarg)) {
a6945b5d
 		logg("Archive: Compression ratio limit set to %d.\n", limits.maxratio);
c238ac42
 	    } else {
a6945b5d
 		logg("^Archive: Compression ratio limit disabled.\n");
c238ac42
 	    }
a6945b5d
 	} else {
 	    limits.maxratio = 200;
 	    logg("^USING HARDCODED LIMIT: Archive: Compression ratio limit set to %d.\n", limits.maxratio);
 	}
 
e3aaff8e
 	if(cfgopt(copt, "ArchiveLimitMemoryUsage")) {
 	    limits.archivememlim = 1;
9e431a95
 	    logg("Archive: Limited memory usage.\n");
c238ac42
 	} else {
e3aaff8e
 	    limits.archivememlim = 0;
c238ac42
 	}
e3aaff8e
     }
 
c238ac42
     if(cfgopt(copt, "ScanArchive")) {
e3aaff8e
 	logg("Archive support enabled.\n");
 	options |= CL_ARCHIVE;
8139fd99
 
 	if(cfgopt(copt, "ScanRAR")) {
 	    logg("RAR support enabled.\n");
 	} else {
 	    logg("RAR support disabled.\n");
 	    options |= CL_DISABLERAR;
 	}
e3aaff8e
     } else {
 	logg("Archive support disabled.\n");
     }
 
c238ac42
     if(cfgopt(copt, "ScanMail")) {
e3aaff8e
 	logg("Mail files support enabled.\n");
 	options |= CL_MAIL;
     } else {
 	logg("Mail files support disabled.\n");
     }
 
c238ac42
     if(cfgopt(copt, "ScanOLE2")) {
e8c9ccdb
 	logg("OLE2 support enabled.\n");
 	options |= CL_OLE2;
     } else {
 	logg("OLE2 support disabled.\n");
     }
 
c238ac42
     if((cpt = cfgopt(copt, "SelfCheck"))) {
 	selfchk = cpt->numarg;
     } else {
 	selfchk = CL_DEFAULT_SELFCHECK;
     }
 
     if(!selfchk) {
 	logg("Self checking disabled.\n");
     } else {
 	logg("Self checking every %d seconds.\n", selfchk);
     }
e3aaff8e
 
     pthread_attr_init(&thattr);
     pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_DETACHED);
 
 #ifdef CLAMUKO
c238ac42
     pthread_attr_init(&clamuko_attr);
     pthread_attr_setdetachstate(&clamuko_attr, PTHREAD_CREATE_JOINABLE);
 
     tharg = (struct thrarg *) mmalloc(sizeof(struct thrarg));
     tharg->copt = copt;
     tharg->root = root;
     tharg->limits = &limits;
     tharg->options = options;
 
     pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
e3aaff8e
 #else
c238ac42
     logg("!Clamuko is not available.\n");
e3aaff8e
 #endif
 
     /* set up signal handling */
     sigfillset(&sigset);
     sigdelset(&sigset, SIGINT);
     sigdelset(&sigset, SIGTERM);
b4912e71
     sigdelset(&sigset, SIGSEGV);
f456f28e
     sigdelset(&sigset, SIGHUP);
e3aaff8e
     sigprocmask(SIG_SETMASK, &sigset, NULL);
c238ac42
  
b4912e71
     /* SIGINT, SIGTERM, SIGSEGV */
8ac7b1ce
     sigact.sa_handler = sighandler_th;
e3aaff8e
     sigemptyset(&sigact.sa_mask);
     sigaddset(&sigact.sa_mask, SIGINT);
     sigaddset(&sigact.sa_mask, SIGTERM);
f456f28e
     sigaddset(&sigact.sa_mask, SIGHUP);
e3aaff8e
     sigaction(SIGINT, &sigact, NULL);
     sigaction(SIGTERM, &sigact, NULL);
 
c238ac42
     if(!debug_mode)
 	sigaction(SIGSEGV, &sigact, NULL);
 
     sigaction(SIGHUP, &sigact, NULL);
e3aaff8e
 
 #if defined(C_BIGSTACK) || defined(C_BSD)
     /*
      * njh@bandsman.co.uk:
      * libclamav/scanners.c uses a *huge* buffer
      * (128K not BUFSIZ from stdio.h).
      * We need to allow for that.
      */
     pthread_attr_getstacksize(&thattr, &stacksize);
3ed976e6
     cli_dbgmsg("set stacksize to %u\n", stacksize + SCANBUFF + 64 * 1024);
     pthread_attr_setstacksize(&thattr, stacksize + SCANBUFF + 64 * 1024);
e3aaff8e
 #endif
 
c238ac42
     pthread_mutex_init(&exit_mutex, NULL);
     pthread_mutex_init(&reload_mutex, NULL);
e3aaff8e
 
c238ac42
     if(thrmgr_init(&thrmgr, max_threads, 1, scanner_thread) != 0) {
 	logg("thrmgr_init failed");
 	exit(-1);
     }
e3aaff8e
 
c238ac42
     time(&start_time);
e3aaff8e
 
c238ac42
     for(;;) {				
 	new_sd = accept(socketd, NULL, NULL);
 	if((new_sd == -1) && (errno != EINTR)) {
 	    logg("!accept() failed: %s", strerror_r(errno, buff, BUFFSIZE));
 	    /* very bad - need to exit or restart */
e3aaff8e
 	    continue;
 	}
 
c238ac42
 	client_conn = (client_conn_t *) mmalloc(sizeof(struct client_conn_tag));
 	client_conn->sd = new_sd;
 	client_conn->options = options;
 	client_conn->copt = copt;
 	client_conn->root = root;
 	client_conn->limits = &limits;
 	thrmgr_add(&thrmgr, client_conn);
 
 	pthread_mutex_lock(&exit_mutex);
 	if(progexit) {
 	    pthread_mutex_unlock(&exit_mutex);
 	    break;
e3aaff8e
 	}
c238ac42
 	pthread_mutex_unlock(&exit_mutex);
e3aaff8e
 
c238ac42
 	if(selfchk) {
 	    time(&current_time);
 	    if((current_time - start_time) > selfchk) {
 		if(reload_db(root, copt, TRUE)) {
 		    pthread_mutex_lock(&reload_mutex);
 		    reload = 1;
 		    pthread_mutex_unlock(&reload_mutex);
 		}
 		time(&start_time);
 	    }
0f387b1b
 	}
e3aaff8e
 
c238ac42
 	pthread_mutex_lock(&reload_mutex);
 	if(reload) {
 	    reload = 0;
 	    pthread_mutex_unlock(&reload_mutex);
 	    /* Destroy the thread manager.
 	     * This waits for all current tasks to end
 	     */
 	    thrmgr_destroy(&thrmgr);
 	    root = reload_db(root, copt, FALSE);
 	    if(thrmgr_init(&thrmgr, max_threads, 1, scanner_thread) != 0) {
 		logg("!thrmgr_init failed");
 		pthread_mutex_unlock(&reload_mutex);
 		exit(-1);
 	    }
 #ifdef CLAMUKO
 	    logg("Stopping and restarting Clamuko.\n");
 	    pthread_kill(clamuko_pid, SIGUSR1);
 	    pthread_join(clamuko_pid, NULL);
 	    tharg->root = root;
 	    pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
2d70a403
 #endif
c238ac42
 	} else {
 	    pthread_mutex_unlock(&reload_mutex);
 	}
     }
e3aaff8e
 
c238ac42
 #ifdef CLAMUKO
     logg("Stopping Clamuko.\n");
     pthread_kill(clamuko_pid, SIGUSR1);
     pthread_join(clamuko_pid, NULL);
6a2532ca
 #endif
c238ac42
     logg("Exiting (clean)\n");
     return 0;
b4912e71
 }