e3aaff8e |
/* |
6ccc6990 |
* Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
* 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 */
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> |
dd95cc2d |
#include <errno.h> |
5ca6034b |
#include <signal.h> |
e3aaff8e |
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
|
fb787a06 |
#if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
#include <syslog.h>
#endif
|
e3aaff8e |
#include "options.h"
#include "shared.h"
#include "others.h" |
fb787a06 |
#include "clamd/others.h" |
e3aaff8e |
#include "manager.h"
#include "defaults.h"
#include "freshclam.h"
|
5ca6034b |
#define TIMEOUT 1200 |
e3aaff8e |
|
dd95cc2d |
static short terminate = 0;
static void daemon_sighandler(int sig) {
char *action = NULL;
switch(sig) {
case SIGALRM:
case SIGUSR1:
action = "wake up"; |
85e6e26a |
terminate = -1; |
dd95cc2d |
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 { |
c32360c1 |
fprintf(fd, "%d", (int) getpid()); |
dd95cc2d |
fclose(fd);
}
umask(old_umask);
}
|
2d70a403 |
int freshclam(struct optstruct *opt) |
e3aaff8e |
{ |
c32360c1 |
int ret = 52; |
95d401c4 |
char *newdir, *cfgfile; |
dd95cc2d |
char *pidfile = NULL; |
95d401c4 |
struct cfgstruct *copt, *cpt; |
dd95cc2d |
struct sigaction sigact;
struct sigaction oldact; |
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 */ |
6ccc6990 |
if((cfgfile = getargl(opt, "config-file"))) { |
95d401c4 |
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'); |
d685f6dd |
} else 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 */
|
fb787a06 |
if(cfgopt(copt, "LogVerbose"))
logverbose = 1;
else
logverbose = 0;
|
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;
|
fb787a06 |
#if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
if((cpt = cfgopt(copt, "LogSyslog"))) {
openlog("freshclam", LOG_PID, LOG_LOCAL6);
use_syslog = 1;
syslog(LOG_INFO, "Freshclam started.\n");
} else
use_syslog = 0;
#endif
|
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; |
85e6e26a |
time_t now, wakeup; |
e3aaff8e |
|
dd95cc2d |
memset(&sigact, 0, sizeof(struct sigaction));
sigact.sa_handler = daemon_sighandler; |
af22ece1 |
if(optc(opt, 'c')) {
checks = atoi(getargc(opt, 'c')); |
d685f6dd |
} else 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(); |
2df0f7e9 |
if (optc(opt, 'p')) {
pidfile = getargc(opt, 'p'); |
c32360c1 |
} else if ((cpt = cfgopt(copt, "PidFile"))) { |
dd95cc2d |
pidfile = cpt->strarg;
}
if (pidfile) {
writepid(pidfile);
}
logg("freshclam daemon started (pid=%d)\n", getpid()); |
e3aaff8e |
|
dd95cc2d |
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGHUP, &sigact, NULL);
sigaction(SIGINT, &sigact, NULL);
while(!terminate) { |
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"); |
dd95cc2d |
sigaction(SIGALRM, &sigact, &oldact);
sigaction(SIGUSR1, &sigact, &oldact); |
85e6e26a |
time(&wakeup);
wakeup += bigsleep; |
dd95cc2d |
alarm(bigsleep); |
85e6e26a |
do {
pause();
time(&now);
} while (!terminate && now < wakeup);
if (terminate == -1) {
terminate = 0;
} |
dd95cc2d |
sigaction(SIGALRM, &oldact, NULL);
sigaction(SIGUSR1, &oldact, NULL); |
e3aaff8e |
}
} 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 |
} |
dd95cc2d |
if (pidfile) {
unlink(pidfile);
} |
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"); |
dd95cc2d |
mprintf(" --config-file=FILE read configuration from FILE.\n"); |
af22ece1 |
mprintf(" --log=FILE -l FILE log into FILE\n"); |
5def21ff |
mprintf(" --daemon -d run in daemon mode\n"); |
fb787a06 |
mprintf(" --pid=FILE -p FILE save daemon's pid in FILE\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);
} |