freshclam/freshclam.c
b151ef55
 /*
ae1f747c
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *			     Damien Curtain <damien@pagefault.org>
  *
  *  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.
  */
 
29ca066f
 /* TODO: Handle SIGALRM more gently */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
d09e8c7c
 #include <errno.h>
29ca066f
 #include <signal.h>
b151ef55
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
 
 #include "options.h"
 #include "shared.h"
 #include "others.h"
 #include "manager.h"
 #include "defaults.h"
 #include "freshclam.h"
 
29ca066f
 #define TIMEOUT 1200
b151ef55
 
d09e8c7c
 static short terminate = 0;
 
 
 static void daemon_sighandler(int sig) {
 	char *action = NULL;
 
     switch(sig) {
 	case SIGALRM:
 	case SIGUSR1:
 	    action = "wake up";
f331499e
 	    terminate = -1;
d09e8c7c
 	    break;
 
 	case SIGHUP:
 	    action = "re-opening log file";
 	    break;
 
 	default:
 	    action = "terminating";
 	    terminate = 1;
 	    break;
     }
     logg("Received signal %d, %s\n", sig, action);
     if (sig == SIGHUP) {
 	logg(NULL);	/* forces log file re-opening */
     }
     return;
 }
 
 
 static void writepid(char *pidfile) {
 	FILE *fd;
 	int old_umask;
     old_umask = umask(0006);
     if((fd = fopen(pidfile, "w")) == NULL) {
 	logg("!Can't save PID to file %s: %s\n", pidfile, strerror(errno));
     } else {
d927f46c
 	fprintf(fd, "%d", (int) getpid());
d09e8c7c
 	fclose(fd);
     }
     umask(old_umask);
 }
 
 
e8217f5a
 int freshclam(struct optstruct *opt)
b151ef55
 {
d927f46c
 	int ret = 52;
0d98d74c
 	char *newdir, *cfgfile;
d09e8c7c
 	char *pidfile = NULL;
0d98d74c
 	struct cfgstruct *copt, *cpt;
d09e8c7c
 	struct sigaction sigact;
 	struct sigaction oldact;
b151ef55
 #ifndef C_CYGWIN
e3f00f7e
 	char *unpuser;
0d98d74c
 	struct passwd *user;
 #endif
b151ef55
 
50099661
     if(optc(opt, 'h')) {
 	free_opt(opt);
     	help();
     }
b151ef55
 
0d98d74c
     /* parse the config file */
ae1f747c
     if((cfgfile = getargl(opt, "config-file"))) {
0d98d74c
 	copt = parsecfg(cfgfile);
     } else {
 	/* TODO: force strict permissions on freshclam.conf */
 	if((copt = parsecfg((cfgfile = CONFDIR"/freshclam.conf"))) == NULL)
 	    copt = parsecfg((cfgfile = CONFDIR"/clamav.conf"));
     }
 
     if(!copt) {
 	mprintf("!Can't parse the config file %s\n", cfgfile);
 	return 56;
     }
 
026ebd88
     if(optl(opt, "http-proxy") || optl(opt, "proxy-user"))
 	mprintf("WARNING: Proxy settings are now only configurable in the config file.\n");
 
 
0d98d74c
 #ifndef C_CYGWIN
b151ef55
     /* freshclam shouldn't work with root priviledges */
026ebd88
     if(optc(opt, 'u')) {
 	unpuser = getargc(opt, 'u');
ff28b69c
     } else if((cpt = cfgopt(copt, "DatabaseOwner"))) {
0d98d74c
 	unpuser = cpt->strarg;
026ebd88
     } else {
 	unpuser = UNPUSER;
     }
0d98d74c
 
     if(!getuid()) {
b151ef55
 	if((user = getpwnam(unpuser)) == NULL) {
 	    mprintf("@Can't get information about user %s.\n", unpuser);
 	    exit(60); /* this is critical problem, so we just exit here */
 	}
 
 	setgroups(1, &user->pw_gid);
 	setgid(user->pw_gid);
 	setuid(user->pw_uid);
     }
 #endif
 
     /* initialize some important variables */
 
0d98d74c
     if(optl(opt, "debug") || cfgopt(copt, "Debug"))
442d8407
 	cl_debug();
 
b151ef55
     mprintf_disabled = 0;
 
     if(optc(opt, 'v')) mprintf_verbose = 1;
     else mprintf_verbose = 0;
 
     if(optl(opt, "quiet")) mprintf_quiet = 1;
     else mprintf_quiet = 0;
 
     if(optl(opt, "stdout")) mprintf_stdout = 1;
     else mprintf_stdout = 0;
 
     if(optc(opt, 'V')) {
 	mprintf("freshclam / ClamAV version "VERSION"\n");
 	mexit(0);
     }
 
 
     /* initialize logger */
 
026ebd88
     if(optc(opt, 'l')) {
 	logfile = getargc(opt, 'l');
 	if(logg("--------------------------------------\n")) {
 	    mprintf("!Problem with internal logger.\n");
 	    mexit(1);
 	}
     } else if((cpt = cfgopt(copt, "UpdateLogFile"))) {
0d98d74c
 	logfile = cpt->strarg; 
b151ef55
 	if(logg("--------------------------------------\n")) {
 	    mprintf("!Problem with internal logger.\n");
 	    mexit(1);
 	}
0d98d74c
     } else
b151ef55
 	logfile = NULL;
 
0d98d74c
     /* change the current working directory */
     if(optl(opt, "datadir")) {
b151ef55
 	newdir = getargl(opt, "datadir");
0d98d74c
     } else {
 	if((cpt = cfgopt(copt, "DatabaseDirectory")))
 	    newdir = cpt->strarg;
 	else
 	    newdir = VIRUSDBDIR;
     }
b151ef55
 
     if(chdir(newdir)) {
 	mprintf("Can't change dir to %s\n", newdir);
 	exit(50);
     } else
442d8407
 	mprintf("*Current working dir is %s\n", newdir);
b151ef55
 
 
     if(optc(opt, 'd')) {
 	    int bigsleep, checks;
f331499e
 	    time_t now, wakeup;
b151ef55
 
d09e8c7c
 	memset(&sigact, 0, sizeof(struct sigaction));
 	sigact.sa_handler = daemon_sighandler;
026ebd88
 
 	if(optc(opt, 'c')) {
 	    checks = atoi(getargc(opt, 'c'));
ff28b69c
 	} else if((cpt = cfgopt(copt, "Checks"))) {
0d98d74c
 	    checks = cpt->numarg;
026ebd88
 	} else {
0d98d74c
 	    checks = CL_DEFAULT_CHECKS;
026ebd88
 	}
b151ef55
 
 	if(checks <= 0 || checks > 50) {
0d98d74c
 	    mprintf("@Number of checks must be between 1 and 50.\n");
b151ef55
 	    mexit(41);
 	}
 
0d98d74c
 	bigsleep = 24 * 3600 / checks;
b151ef55
 	daemonize();
cb06e8ab
 	if (optc(opt, 'p')) {
 	    pidfile = getargc(opt, 'p');
d927f46c
 	} else if ((cpt = cfgopt(copt, "PidFile"))) {
d09e8c7c
 	    pidfile = cpt->strarg;
 	}
 	if (pidfile) {
 	    writepid(pidfile);
 	}
 	logg("freshclam daemon started (pid=%d)\n", getpid());
b151ef55
 
d09e8c7c
 	sigaction(SIGTERM, &sigact, NULL);
 	sigaction(SIGHUP, &sigact, NULL);
 	sigaction(SIGINT, &sigact, NULL);
 	while(!terminate) {
026ebd88
 	    ret = download(copt, opt);
 
b151ef55
 
026ebd88
 	    if(optl(opt, "on-error-execute")) {
 		if(ret > 1)
 		    system(getargl(opt, "on-error-execute"));
 
 	    } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
c6259ac5
 		if(ret > 1)
0d98d74c
 		    system(cpt->strarg);
026ebd88
 	    }
b151ef55
 
 	    logg("\n--------------------------------------\n");
d09e8c7c
 	    sigaction(SIGALRM, &sigact, &oldact);
 	    sigaction(SIGUSR1, &sigact, &oldact);
f331499e
 	    time(&wakeup);
 	    wakeup += bigsleep;
d09e8c7c
 	    alarm(bigsleep);
f331499e
 	    do {
 		pause();
 		time(&now);
 	    } while (!terminate && now < wakeup);
 	    if (terminate == -1) {
 		terminate = 0;
 	    }
d09e8c7c
 	    sigaction(SIGALRM, &oldact, NULL);
 	    sigaction(SIGUSR1, &oldact, NULL);
b151ef55
 	}
 
     } else
026ebd88
 	ret = download(copt, opt);
b151ef55
 
026ebd88
     if(optl(opt, "on-error-execute")) {
 	if(ret > 1)
 	    system(getargl(opt, "on-error-execute"));
 
     } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
c6259ac5
 	if(ret > 1)
0d98d74c
 	    system(cpt->strarg);
026ebd88
     }
d09e8c7c
     if (pidfile) {
         unlink(pidfile);
     }
b151ef55
 
e8217f5a
     return(ret);
b151ef55
 }
 
29ca066f
 void d_timeout(int sig)
 {
     mprintf("@Maximal time (%d seconds) reached.\n", TIMEOUT);
     exit(1);
 }
 
026ebd88
 int download(const struct cfgstruct *copt, const struct optstruct *opt)
b151ef55
 {
0d98d74c
 	int ret = 0, try = 0, maxattempts = 0;
29ca066f
 	struct sigaction sigalrm;
0d98d74c
 	struct cfgstruct *cpt;
29ca066f
 
9c1c9007
     memset(&sigalrm, 0, sizeof(struct sigaction));
29ca066f
     sigalrm.sa_handler = d_timeout;
     sigaction(SIGALRM, &sigalrm, NULL);
b151ef55
 
0d98d74c
     if((cpt = cfgopt(copt, "MaxAttempts")))
 	maxattempts = cpt->numarg;
 
     mprintf("*Max retries == %d\n", maxattempts);
 
     if((cpt = cfgopt(copt, "DatabaseMirror")) == NULL) {
 	mprintf("@You must specify at least one database mirror.\n");
 	return 57;
     } else {
 
 	while(cpt) {
 	    alarm(TIMEOUT);
026ebd88
 	    ret = downloadmanager(copt, opt, cpt->strarg);
0d98d74c
 	    alarm(0);
 
 	    if(ret == 52 || ret == 54) {
 		if(try < maxattempts - 1) {
 		    mprintf("Trying again...\n");
 		    logg("Trying again...\n");
 		    try++;
 		    sleep(1);
 		    continue;
 		} else {
 		    mprintf("Giving up...\n");
 		    logg("Giving up...\n");
 		    cpt = (struct cfgstruct *) cpt->nextarg;
 		    try = 0;
 		}
 
 	    } else {
 		return ret;
 	    }
 	}
b151ef55
     }
 
     return ret;
 }
 
 void daemonize(void)
 {
 	int i;
 
     for(i = 0; i < 3; i++)
 	close(i);
 
     umask(0);
 
     if(fork())
 	exit(0);
 
     setsid();
     mprintf_disabled = 1;
 }
 
 void help(void)
 {
 
     mprintf_stdout = 1;
 
     mprintf("\n");
a0faaedf
     mprintf("                          Clam AntiVirus: freshclam  "VERSION"\n");
8b1cee14
     mprintf("                (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>\n\n");
a0faaedf
 
     mprintf("    --help               -h              show help\n");
     mprintf("    --version            -V              print version number and exit\n");
     mprintf("    --verbose            -v              be verbose\n");
     mprintf("    --debug                              enable debug messages\n");
     mprintf("    --quiet                              be quiet, output only error messages\n");
     mprintf("    --stdout                             write to stdout instead of stderr\n");
     mprintf("                                         (this help is always written to stdout)\n");
b151ef55
     mprintf("\n");
d09e8c7c
     mprintf("    --config-file=FILE                   read configuration from FILE.\n");
026ebd88
     mprintf("    --log=FILE           -l FILE         log into FILE\n");
a0faaedf
     mprintf("    --daemon             -d              run in daemon mode\n");
d09e8c7c
     mprintf("    --pid                -p FILE         save daemon's pid in FILE\n");
026ebd88
     mprintf("    --user=USER          -u USER         run as USER\n");
8b1cee14
     mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
0d98d74c
     mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
026ebd88
 #ifdef BUILD_CLAMD
     mprintf("    --daemon-notify[=/path/clamav.conf]  send RELOAD command to clamd\n");
 #endif
     mprintf("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
     mprintf("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
 
b151ef55
     mprintf("\n");
     exit(0);
 }