freshclam/freshclam.c
b151ef55
 /*
2bc31f05
  *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
b151ef55
  */
c2f9389a
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
29ca066f
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
c2f9389a
 #ifdef	HAVE_UNISTD_H
b151ef55
 #include <unistd.h>
c2f9389a
 #endif
b151ef55
 #include <string.h>
d09e8c7c
 #include <errno.h>
29ca066f
 #include <signal.h>
f91f55e0
 #include <time.h>
b151ef55
 #include <sys/types.h>
c2f9389a
 #ifndef	C_WINDOWS
622a7127
 #include <sys/wait.h>
c2f9389a
 #endif
b151ef55
 #include <sys/stat.h>
 #include <fcntl.h>
c2f9389a
 #ifndef	C_WINDOWS
b151ef55
 #include <pwd.h>
 #include <grp.h>
c2f9389a
 #endif
b151ef55
 
36f2038b
 #if defined(USE_SYSLOG) && !defined(C_AIX)
5aad82e2
 #include <syslog.h>
 #endif
 
819c7c41
 #include "target.h"
a43f5fa4
 #include "clamav.h"
 
 #include "shared/options.h"
 #include "shared/output.h"
 #include "shared/misc.h"
 
622a7127
 #include "execute.h"
a43f5fa4
 #include "manager.h"
b151ef55
 
d09e8c7c
 static short terminate = 0;
622a7127
 extern int active_children;
d09e8c7c
 
a43f5fa4
 static short foreground = 1;
f1c4563e
 
d09e8c7c
 static void daemon_sighandler(int sig) {
 
     switch(sig) {
c2f9389a
 #ifdef	SIGCHLD
622a7127
 	case SIGCHLD:
 	    waitpid(-1, NULL, WNOHANG);
 	    active_children--;
 	    break;
c2f9389a
 #endif
622a7127
 
c2f9389a
 #ifdef	SIGALRM
4f21daf4
 	case SIGALRM:
c2f9389a
 		terminate = -1;
 	    break;
 #endif
 #ifdef	SIGUSR1
d09e8c7c
 	case SIGUSR1:
c2f9389a
 		terminate = -1;
d09e8c7c
 	    break;
c2f9389a
 #endif
d09e8c7c
 
c2f9389a
 #ifdef	SIGHUP
d09e8c7c
 	case SIGHUP:
ff8a5bd1
 	    terminate = -2;
d09e8c7c
 	    break;
c2f9389a
 #endif
d09e8c7c
 
 	default:
 	    terminate = 1;
 	    break;
     }
ff8a5bd1
 
d09e8c7c
     return;
 }
 
a43f5fa4
 static void writepid(char *pidfile)
 {
d09e8c7c
 	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);
 }
 
a43f5fa4
 void help(void)
 {
     mprintf_stdout = 1;
 
     mprintf("\n");
     mprintf("                   Clam AntiVirus: freshclam  "VERSION"\n");
     mprintf("    (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n");
 
     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                              only output error messages\n");
     mprintf("    --stdout                             write to stdout instead of stderr\n");
     mprintf("\n");
     mprintf("    --config-file=FILE                   read configuration from FILE.\n");
     mprintf("    --log=FILE           -l FILE         log into FILE\n");
     mprintf("    --daemon             -d              run in daemon mode\n");
     mprintf("    --pid=FILE           -p FILE         save daemon's pid in FILE\n");
     mprintf("    --user=USER          -u USER         run as USER\n");
     mprintf("    --no-dns                             force old non-DNS verification method\n");
     mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
     mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
 #ifdef BUILD_CLAMD
     mprintf("    --daemon-notify[=/path/clamd.conf]   send RELOAD command to clamd\n");
 #endif
     mprintf("    --local-address=IP   -a IP           bind to IP for HTTP downloads\n");
     mprintf("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
     mprintf("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
     mprintf("    --on-outdated-execute=COMMAND        execute COMMAND when software is outdated\n");
 
     mprintf("\n");
 }
 
 int download(const struct cfgstruct *copt, const struct optstruct *opt)
 {
 	int ret = 0, try = 0, maxattempts = 0;
 	struct cfgstruct *cpt;
 
 
     maxattempts = cfgopt(copt, "MaxAttempts")->numarg;
     logg("*Max retries == %d\n", maxattempts);
 
     if(!(cpt = cfgopt(copt, "DatabaseMirror"))->enabled) {
 	logg("^You must specify at least one database mirror.\n");
 	return 56;
     } else {
 
 	while(cpt) {
 	    ret = downloadmanager(copt, opt, cpt->strarg);
 	    alarm(0);
 
 	    if(ret == 52 || ret == 54 || ret == 58 || ret == 59) {
 		if(try < maxattempts - 1) {
 		    logg("Trying again in 5 secs...\n");
 		    try++;
 		    sleep(5);
 		    continue;
 		} else {
 		    logg("Giving up on %s...\n", cpt->strarg);
 		    cpt = (struct cfgstruct *) cpt->nextarg;
 		    if(!cpt) {
 			logg("^Update failed. Your network may be down or none of the mirrors listed in freshclam.conf is working.\n");
 		    }
 		    try = 0;
 		}
 
 	    } else {
 		return ret;
 	    }
 	}
     }
 
     return ret;
 }
 
2bc31f05
 int main(int argc, char **argv)
b151ef55
 {
d927f46c
 	int ret = 52;
0d98d74c
 	char *newdir, *cfgfile;
d09e8c7c
 	char *pidfile = NULL;
0d98d74c
 	struct cfgstruct *copt, *cpt;
c2f9389a
 #ifndef	C_WINDOWS
 	struct sigaction sigact;
 	struct sigaction oldact;
 #endif
 #if !defined(C_CYGWIN)  && !defined(C_OS2) && !defined(C_WINDOWS)
e3f00f7e
 	char *unpuser;
0d98d74c
 	struct passwd *user;
 #endif
f4f0e68d
 	struct stat statbuf;
2bc31f05
 	struct optstruct *opt;
 	const char *short_options = "hvdp:Vl:c:u:a:";
 	static struct option long_options[] = {
 	    {"help", 0, 0, 'h'},
 	    {"quiet", 0, 0, 0},
 	    {"verbose", 0, 0, 'v'},
 	    {"debug", 0, 0, 0},
 	    {"version", 0, 0, 'V'},
 	    {"datadir", 1, 0, 0},
 	    {"log", 1, 0, 'l'},
 	    {"log-verbose", 0, 0, 0}, /* not used */
 	    {"stdout", 0, 0, 0},
 	    {"daemon", 0, 0, 'd'},
 	    {"pid", 1, 0, 'p'},
 	    {"user", 1, 0, 'u'}, /* not used */
 	    {"config-file", 1, 0, 0},
 	    {"no-dns", 0, 0, 0},
 	    {"checks", 1, 0, 'c'},
 	    {"http-proxy", 1, 0, 0},
 	    {"local-address", 1, 0, 'a'},
 	    {"proxy-user", 1, 0, 0},
 	    {"daemon-notify", 2, 0, 0},
 	    {"on-update-execute", 1, 0, 0},
 	    {"on-error-execute", 1, 0, 0},
 	    {"on-outdated-execute", 1, 0, 0},
 	    {0, 0, 0, 0}
     	};
 
 
     opt = opt_parse(argc, argv, short_options, long_options, NULL);
     if(!opt) {
 	mprintf("!Can't parse the command line\n");
 	return 40;
     }
b151ef55
 
2bc31f05
     if(opt_check(opt, "help")) {
50099661
     	help();
a43f5fa4
 	opt_free(opt);
 	return 0;
     }
 
     if(opt_check(opt, "version")) {
 	print_version();
 	opt_free(opt);
 	return 0;
50099661
     }
b151ef55
 
0d98d74c
     /* parse the config file */
2bc31f05
     if((cfgfile = opt_arg(opt, "config-file"))) {
5c4d94a9
 	copt = getcfg(cfgfile, 1);
0d98d74c
     } else {
 	/* TODO: force strict permissions on freshclam.conf */
5c4d94a9
 	if((copt = getcfg((cfgfile = CONFDIR"/freshclam.conf"), 1)) == NULL)
 	    copt = getcfg((cfgfile = CONFDIR"/clamd.conf"), 1);
0d98d74c
     }
 
     if(!copt) {
f1c4563e
 	logg("!Can't parse the config file %s\n", cfgfile);
2bc31f05
 	opt_free(opt);
0d98d74c
 	return 56;
     }
 
c2f9389a
 #ifdef C_WINDOWS
     if(!pthread_win32_process_attach_np()) {
 	mprintf("!Can't start the win32 pthreads layer\n");
 	return 63;
     }
 #endif
 
2bc31f05
     if(opt_check(opt, "http-proxy") || opt_check(opt, "proxy-user"))
f1c4563e
 	logg("WARNING: Proxy settings are now only configurable in the config file.\n");
026ebd88
 
5c4d94a9
     if(cfgopt(copt, "HTTPProxyPassword")->enabled) {
f4f0e68d
 	if(stat(cfgfile, &statbuf) == -1) {
f1c4563e
 	    logg("^Can't stat %s (critical error)\n", cfgfile);
2bc31f05
 	    opt_free(opt);
f4f0e68d
 	    return 56;
 	}
c2f9389a
 
 #if !defined(C_CYGWIN) && !defined(C_WINDOWS)
f4f0e68d
 	if(statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
f1c4563e
 	    logg("^Insecure permissions (for HTTPProxyPassword): %s must have no more than 0700 permissions.\n", cfgfile);
2bc31f05
 	    opt_free(opt);
f4f0e68d
 	    return 56;
 	}
89a917da
 #endif
f4f0e68d
     }
026ebd88
 
c2f9389a
 #if !defined(C_CYGWIN)  && !defined(C_OS2) && !defined(C_WINDOWS)
f4f0e68d
     /* freshclam shouldn't work with root privileges */
a43f5fa4
     if(opt_check(opt, "user"))
2bc31f05
 	unpuser = opt_arg(opt, "user");
a43f5fa4
     else
 	unpuser = cfgopt(copt, "DatabaseOwner")->strarg;
0d98d74c
 
a6c3fdb9
     if(!geteuid()) {
b151ef55
 	if((user = getpwnam(unpuser)) == NULL) {
f1c4563e
 	    logg("^Can't get information about user %s.\n", unpuser);
a43f5fa4
 	    opt_free(opt);
 	    return 60;
b151ef55
 	}
 
5c4d94a9
 	if(cfgopt(copt, "AllowSupplementaryGroups")->enabled) {
a3bc3260
 #ifdef HAVE_INITGROUPS
 	    if(initgroups(unpuser, user->pw_gid)) {
f1c4563e
 		logg("^initgroups() failed.\n");
a43f5fa4
 		opt_free(opt);
 		return 61;
a3bc3260
 	    }
 #endif
 	} else {
819bbe1f
 #ifdef HAVE_SETGROUPS
a3bc3260
 	    if(setgroups(1, &user->pw_gid)) {
f1c4563e
 		logg("^setgroups() failed.\n");
a43f5fa4
 		opt_free(opt);
 		return 61;
a3bc3260
 	    }
819bbe1f
 #endif
a3bc3260
 	}
eeb69538
 
 	if(setgid(user->pw_gid)) {
f1c4563e
 	    logg("^setgid(%d) failed.\n", (int) user->pw_gid);
a43f5fa4
 	    opt_free(opt);
 	    return 61;
eeb69538
 	}
 
 	if(setuid(user->pw_uid)) {
f1c4563e
 	    logg("^setuid(%d) failed.\n", (int) user->pw_uid);
a43f5fa4
 	    opt_free(opt);
 	    return 61;
eeb69538
 	}
b151ef55
     }
 #endif
 
     /* initialize some important variables */
 
2bc31f05
     if(opt_check(opt, "debug") || cfgopt(copt, "Debug")->enabled)
442d8407
 	cl_debug();
 
2bc31f05
     if(opt_check(opt, "verbose"))
7fbb6473
 	mprintf_verbose = 1;
b151ef55
 
2bc31f05
     if(opt_check(opt, "quiet"))
7fbb6473
 	mprintf_quiet = 1;
b151ef55
 
2bc31f05
     if(opt_check(opt, "stdout"))
7fbb6473
 	mprintf_stdout = 1;
b151ef55
 
     /* initialize logger */
 
5c4d94a9
     if(cfgopt(copt, "LogVerbose")->enabled)
36f2038b
 	logg_verbose = 1;
5aad82e2
 
2bc31f05
     if(opt_check(opt, "log")) {
 	logg_file = opt_arg(opt, "log");
b54c4b07
 	if(logg("#--------------------------------------\n")) {
dfa281a5
 	    mprintf("!Problem with internal logger (--log=%s).\n", logg_file);
a43f5fa4
 	    opt_free(opt);
 	    return 62;
026ebd88
 	}
5c4d94a9
     } else if((cpt = cfgopt(copt, "UpdateLogFile"))->enabled) {
36f2038b
 	logg_file = cpt->strarg; 
b54c4b07
 	if(logg("#--------------------------------------\n")) {
dfa281a5
 	    mprintf("!Problem with internal logger (UpdateLogFile = %s).\n", logg_file);
a43f5fa4
 	    opt_free(opt);
 	    return 62;
b151ef55
 	}
0d98d74c
     } else
36f2038b
 	logg_file = NULL;
b151ef55
 
36f2038b
 #if defined(USE_SYSLOG) && !defined(C_AIX)
5c4d94a9
     if(cfgopt(copt, "LogSyslog")->enabled) {
096e5bbd
 	    int fac = LOG_LOCAL6;
 
5c4d94a9
 	if((cpt = cfgopt(copt, "LogFacility"))->enabled) {
096e5bbd
 	    if((fac = logg_facility(cpt->strarg)) == -1) {
 		mprintf("!LogFacility: %s: No such facility.\n", cpt->strarg);
a43f5fa4
 		opt_free(opt);
 		return 62;
096e5bbd
 	    }
 	}
 
 	openlog("freshclam", LOG_PID, fac);
36f2038b
 	logg_syslog = 1;
7fbb6473
     }
5aad82e2
 #endif
 
0d98d74c
     /* change the current working directory */
2bc31f05
     if(opt_check(opt, "datadir"))
 	newdir = opt_arg(opt, "datadir");
5c4d94a9
     else
 	newdir = cfgopt(copt, "DatabaseDirectory")->strarg;
b151ef55
 
     if(chdir(newdir)) {
f1c4563e
 	logg("Can't change dir to %s\n", newdir);
a43f5fa4
 	opt_free(opt);
 	return 50;
b151ef55
     } else
f1c4563e
 	logg("*Current working dir is %s\n", newdir);
b151ef55
 
c2f9389a
 #ifdef	C_WINDOWS
     {
 	    WSADATA wsaData;
 
 	if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
 	    logg("!Error at WSAStartup(): %d\n", WSAGetLastError());
 	    return 1;
 	}
     }
 #endif
 
2bc31f05
     if(opt_check(opt, "daemon")) {
b151ef55
 	    int bigsleep, checks;
c2f9389a
 #ifndef	C_WINDOWS
f331499e
 	    time_t now, wakeup;
b151ef55
 
d09e8c7c
 	memset(&sigact, 0, sizeof(struct sigaction));
 	sigact.sa_handler = daemon_sighandler;
c2f9389a
 #endif
026ebd88
 
2bc31f05
 	if(opt_check(opt, "checks"))
 	    checks = atoi(opt_arg(opt, "checks"));
5c4d94a9
 	else
 	    checks = cfgopt(copt, "Checks")->numarg;
b151ef55
 
881069d7
 	if(checks <= 0) {
f1c4563e
 	    logg("^Number of checks must be a positive integer.\n");
a43f5fa4
 	    opt_free(opt);
 	    return 41;
b151ef55
 	}
 
2bc31f05
 	if(!cfgopt(copt, "DNSDatabaseInfo")->enabled || opt_check(opt, "no-dns")) {
881069d7
 	    if(checks > 50) {
f1c4563e
 		logg("^Number of checks must be between 1 and 50.\n");
a43f5fa4
 		opt_free(opt);
 		return 41;
881069d7
 	    }
 	}
 
0d98d74c
 	bigsleep = 24 * 3600 / checks;
55ae06a5
 
f1c4563e
 	if(!cfgopt(copt, "Foreground")->enabled) {
             foreground = 0;
55ae06a5
 	    daemonize();
a43f5fa4
 	    mprintf_disabled = 1;
f1c4563e
         }
55ae06a5
 
2bc31f05
 	if(opt_check(opt, "pid")) {
 	    pidfile = opt_arg(opt, "pid");
5c4d94a9
 	} else if ((cpt = cfgopt(copt, "PidFile"))->enabled) {
d09e8c7c
 	    pidfile = cpt->strarg;
 	}
 	if (pidfile) {
 	    writepid(pidfile);
 	}
819c7c41
 
622a7127
 	active_children = 0;
 
b54c4b07
 	logg("#freshclam daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
b151ef55
 
c2f9389a
 #ifdef	C_WINDOWS
 	signal(SIGINT, daemon_sighandler);
 	terminate = 0;
 #else
d09e8c7c
 	sigaction(SIGTERM, &sigact, NULL);
 	sigaction(SIGHUP, &sigact, NULL);
 	sigaction(SIGINT, &sigact, NULL);
622a7127
         sigaction(SIGCHLD, &sigact, NULL);
c2f9389a
 #endif
819c7c41
 
d09e8c7c
 	while(!terminate) {
026ebd88
 	    ret = download(copt, opt);
 
622a7127
             if(ret > 1) {
 		    const char *arg = NULL;
b151ef55
 
2bc31f05
 	        if(opt_check(opt, "on-error-execute"))
 		    arg = opt_arg(opt, "on-error-execute");
5c4d94a9
 		else if((cpt = cfgopt(copt, "OnErrorExecute"))->enabled)
622a7127
 		    arg = cpt->strarg;
026ebd88
 
622a7127
 		if(arg)
 		    execute("OnErrorExecute", arg);
026ebd88
 	    }
b151ef55
 
b54c4b07
 	    logg("#--------------------------------------\n");
c2f9389a
 #ifdef	SIGALRM
d09e8c7c
 	    sigaction(SIGALRM, &sigact, &oldact);
c2f9389a
 #endif
 #ifdef	SIGUSR1
d09e8c7c
 	    sigaction(SIGUSR1, &sigact, &oldact);
c2f9389a
 #endif
 
 #ifdef	C_WINDOWS
 	    sleep(bigsleep);
 #else   
f331499e
 	    time(&wakeup);
 	    wakeup += bigsleep;
d09e8c7c
 	    alarm(bigsleep);
f331499e
 	    do {
 		pause();
 		time(&now);
 	    } while (!terminate && now < wakeup);
ff8a5bd1
 
f331499e
 	    if (terminate == -1) {
08cac58a
 		logg("Received signal: wake up\n");
f331499e
 		terminate = 0;
ff8a5bd1
 	    } else if (terminate == -2) {
08cac58a
 		logg("Received signal: re-opening log file\n");
ff8a5bd1
 		terminate = 0;
 		logg_close();
f331499e
 	    }
c2f9389a
 #endif
ff8a5bd1
 
c2f9389a
 #ifdef	SIGALRM
d09e8c7c
 	    sigaction(SIGALRM, &oldact, NULL);
c2f9389a
 #endif
 #ifdef	SIGUSR1
d09e8c7c
 	    sigaction(SIGUSR1, &oldact, NULL);
c2f9389a
 #endif	    
b151ef55
 	}
 
     } else
026ebd88
 	ret = download(copt, opt);
b151ef55
 
2bc31f05
     if(opt_check(opt, "on-error-execute")) {
026ebd88
 	if(ret > 1)
2bc31f05
 	    system(opt_arg(opt, "on-error-execute"));
026ebd88
 
5c4d94a9
     } else if((cpt = cfgopt(copt, "OnErrorExecute"))->enabled) {
c6259ac5
 	if(ret > 1)
0d98d74c
 	    system(cpt->strarg);
026ebd88
     }
d09e8c7c
     if (pidfile) {
         unlink(pidfile);
     }
b151ef55
 
2bc31f05
     opt_free(opt);
c2f9389a
 
 #ifdef C_WINDOWS
     WSACleanup();
 
     if(!pthread_win32_process_detach_np()) {
 	mprintf("!Can't stop the win32 pthreads layer\n");
 	return 63;
     }
 #endif
 
e8217f5a
     return(ret);
b151ef55
 }