clamav-devel/freshclam/freshclam.c
e3aaff8e
 /*
  *  Copyright (C) 2002, 2003 Tomasz Kojm <zolw@konarski.edu.pl>
  *			     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.
  */
 
5ca6034b
 /* TODO: Handle SIGALRM more gently */
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
5ca6034b
 #include <signal.h>
e3aaff8e
 #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"
 
5ca6034b
 #define TIMEOUT 1200
e3aaff8e
 
2d70a403
 int freshclam(struct optstruct *opt)
e3aaff8e
 {
 	int ret;
95d401c4
 	char *newdir, *cfgfile;
 	struct cfgstruct *copt, *cpt;
e3aaff8e
 #ifndef C_CYGWIN
22801d05
 	char *unpuser;
95d401c4
 	struct passwd *user;
 #endif
e3aaff8e
 
ee039e40
     if(optc(opt, 'h')) {
 	free_opt(opt);
     	help();
     }
e3aaff8e
 
95d401c4
     /* parse the config file */
     if((cfgfile = getargc(opt, 'c'))) {
 	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;
     }
 
af22ece1
     if(optl(opt, "http-proxy") || optl(opt, "proxy-user"))
 	mprintf("WARNING: Proxy settings are now only configurable in the config file.\n");
 
 
95d401c4
 #ifndef C_CYGWIN
e3aaff8e
     /* freshclam shouldn't work with root priviledges */
af22ece1
     if(optc(opt, 'u')) {
 	unpuser = getargc(opt, 'u');
     } if((cpt = cfgopt(copt, "DatabaseOwner"))) {
95d401c4
 	unpuser = cpt->strarg;
af22ece1
     } else {
 	unpuser = UNPUSER;
     }
95d401c4
 
     if(!getuid()) {
e3aaff8e
 	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 */
 
95d401c4
     if(optl(opt, "debug") || cfgopt(copt, "Debug"))
e4ae7726
 	cl_debug();
 
e3aaff8e
     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 */
 
95d401c4
     if(optl(opt, "log-verbose") || cfgopt(copt, "LogVerbose"))
 	logverbose = 1;
     else
 	logverbose = 0;
e3aaff8e
 
af22ece1
     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"))) {
95d401c4
 	logfile = cpt->strarg; 
e3aaff8e
 	if(logg("--------------------------------------\n")) {
 	    mprintf("!Problem with internal logger.\n");
 	    mexit(1);
 	}
95d401c4
     } else
e3aaff8e
 	logfile = NULL;
 
95d401c4
     /* change the current working directory */
     if(optl(opt, "datadir")) {
e3aaff8e
 	newdir = getargl(opt, "datadir");
95d401c4
     } else {
 	if((cpt = cfgopt(copt, "DatabaseDirectory")))
 	    newdir = cpt->strarg;
 	else
 	    newdir = VIRUSDBDIR;
     }
e3aaff8e
 
     if(chdir(newdir)) {
 	mprintf("Can't change dir to %s\n", newdir);
 	exit(50);
     } else
e4ae7726
 	mprintf("*Current working dir is %s\n", newdir);
e3aaff8e
 
 
     if(optc(opt, 'd')) {
 	    int bigsleep, checks;
 
af22ece1
 
 	if(optc(opt, 'c')) {
 	    checks = atoi(getargc(opt, 'c'));
 	} if((cpt = cfgopt(copt, "Checks"))) {
95d401c4
 	    checks = cpt->numarg;
af22ece1
 	} else {
95d401c4
 	    checks = CL_DEFAULT_CHECKS;
af22ece1
 	}
e3aaff8e
 
 	if(checks <= 0 || checks > 50) {
95d401c4
 	    mprintf("@Number of checks must be between 1 and 50.\n");
e3aaff8e
 	    mexit(41);
 	}
 
95d401c4
 	bigsleep = 24 * 3600 / checks;
e3aaff8e
 	daemonize();
 
 	while(1) {
af22ece1
 	    ret = download(copt, opt);
 
e3aaff8e
 
af22ece1
 	    if(optl(opt, "on-error-execute")) {
 		if(ret > 1)
 		    system(getargl(opt, "on-error-execute"));
 
 	    } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
049a18b9
 		if(ret > 1)
95d401c4
 		    system(cpt->strarg);
af22ece1
 	    }
e3aaff8e
 
 	    logg("\n--------------------------------------\n");
 	    sleep(bigsleep);
 	}
 
     } else
af22ece1
 	ret = download(copt, opt);
e3aaff8e
 
af22ece1
     if(optl(opt, "on-error-execute")) {
 	if(ret > 1)
 	    system(getargl(opt, "on-error-execute"));
 
     } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
049a18b9
 	if(ret > 1)
95d401c4
 	    system(cpt->strarg);
af22ece1
     }
e3aaff8e
 
2d70a403
     return(ret);
e3aaff8e
 }
 
5ca6034b
 void d_timeout(int sig)
 {
     mprintf("@Maximal time (%d seconds) reached.\n", TIMEOUT);
     exit(1);
 }
 
af22ece1
 int download(const struct cfgstruct *copt, const struct optstruct *opt)
e3aaff8e
 {
95d401c4
 	int ret = 0, try = 0, maxattempts = 0;
5ca6034b
 	struct sigaction sigalrm;
95d401c4
 	struct cfgstruct *cpt;
5ca6034b
 
9e431a95
     memset(&sigalrm, 0, sizeof(struct sigaction));
5ca6034b
     sigalrm.sa_handler = d_timeout;
     sigaction(SIGALRM, &sigalrm, NULL);
e3aaff8e
 
95d401c4
     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);
af22ece1
 	    ret = downloadmanager(copt, opt, cpt->strarg);
95d401c4
 	    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;
 	    }
 	}
e3aaff8e
     }
 
     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");
5def21ff
     mprintf("                          Clam AntiVirus: freshclam  "VERSION"\n");
938504c7
     mprintf("                (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>\n\n");
5def21ff
 
     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");
e3aaff8e
     mprintf("\n");
938504c7
     mprintf("    --config-file=FILE                   Read configuration from FILE.\n");
af22ece1
     mprintf("    --log=FILE           -l FILE         log into FILE\n");
     mprintf("    --log-verbose                        log additional information\n");
5def21ff
     mprintf("    --daemon             -d              run in daemon mode\n");
af22ece1
     mprintf("    --user=USER          -u USER         run as USER\n");
938504c7
     mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
95d401c4
     mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
af22ece1
 #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");
 
e3aaff8e
     mprintf("\n");
     exit(0);
 }