clamav/clamd/clamd.c

410 lines
9.7 KiB
C
Raw Normal View History

2003-07-29 15:48:06 +00:00
/*
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
2003-07-29 15:48:06 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
2003-07-29 15:48:06 +00:00
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
2003-07-29 15:48:06 +00:00
*/
#ifdef _MSC_VER
#include <winsock.h>
#endif
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
2003-07-29 15:48:06 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
2003-07-29 15:48:06 +00:00
#include <unistd.h>
#include <sys/time.h>
#endif
2003-07-29 15:48:06 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2003-07-29 15:48:06 +00:00
#include <time.h>
#ifndef C_WINDOWS
2003-07-29 15:48:06 +00:00
#include <pwd.h>
#include <grp.h>
#endif
2003-07-29 15:48:06 +00:00
2004-03-29 00:00:58 +00:00
#if defined(USE_SYSLOG) && !defined(C_AIX)
2003-07-29 15:48:06 +00:00
#include <syslog.h>
#endif
2004-02-27 09:45:46 +00:00
#ifdef C_LINUX
#include <sys/resource.h>
#endif
2006-09-05 20:45:39 +00:00
#include "target.h"
#include "libclamav/clamav.h"
#include "libclamav/others.h"
#include "libclamav/matcher-ac.h"
2006-09-05 20:45:39 +00:00
#include "shared/output.h"
#include "shared/options.h"
#include "shared/cfgparser.h"
#include "shared/misc.h"
#include "server.h"
2003-07-29 15:48:06 +00:00
#include "tcpserver.h"
#include "localserver.h"
#include "others.h"
2004-03-29 00:00:58 +00:00
#include "shared.h"
2003-07-29 15:48:06 +00:00
2004-03-30 21:11:25 +00:00
short debug_mode = 0, logok = 0;
short foreground = 0;
static void help(void)
2006-09-05 20:45:39 +00:00
{
printf("\n");
printf(" Clam AntiVirus Daemon "VERSION"\n");
printf(" (C) 2002 - 2007 ClamAV Team - http://www.clamav.net/team\n\n");
2006-09-05 20:45:39 +00:00
printf(" --help -h Show this help.\n");
printf(" --version -V Show version number.\n");
printf(" --debug Enable debug mode.\n");
printf(" --config-file=FILE -c FILE Read configuration from FILE.\n\n");
}
int main(int argc, char **argv)
2003-07-29 15:48:06 +00:00
{
struct cfgstruct *copt;
const struct cfgstruct *cpt;
2007-03-19 20:36:17 +00:00
struct passwd *user = NULL;
2003-07-29 15:48:06 +00:00
time_t currtime;
2007-01-30 21:26:43 +00:00
struct cl_engine *engine = NULL;
2003-07-29 15:48:06 +00:00
const char *dbdir, *cfgfile;
2006-09-05 20:45:39 +00:00
int ret, tcpsock = 0, localsock = 0;
unsigned int sigs = 0;
int lsockets[2], nlsockets = 0;
unsigned int dboptions = 0;
2004-05-11 00:14:14 +00:00
#ifdef C_LINUX
struct stat sb;
#endif
struct optstruct *opt;
const char *short_options = "hc:V";
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"config-file", 1, 0, 'c'},
{"version", 0, 0, 'V'},
{"debug", 0, 0, 0},
{0, 0, 0, 0}
};
#ifdef C_WINDOWS
if(!pthread_win32_process_attach_np()) {
mprintf("!Can't start the win32 pthreads layer\n");
return 1;
}
#endif
opt = opt_parse(argc, argv, short_options, long_options, NULL);
if(!opt) {
mprintf("!Can't parse the command line\n");
return 1;
}
2003-07-29 15:48:06 +00:00
if(opt_check(opt, "help")) {
2003-07-29 15:48:06 +00:00
help();
2006-09-05 20:45:39 +00:00
opt_free(opt);
return 0;
2003-07-29 15:48:06 +00:00
}
if(opt_check(opt, "debug")) {
2004-02-27 09:45:46 +00:00
#if defined(C_LINUX)
/* njh@bandsman.co.uk: create a dump if needed */
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
if(setrlimit(RLIMIT_CORE, &rlim) < 0)
perror("setrlimit");
#endif
debug_mode = 1;
2004-02-27 09:45:46 +00:00
}
2003-07-29 15:48:06 +00:00
/* parse the config file */
if(opt_check(opt, "config-file"))
cfgfile = opt_arg(opt, "config-file");
2003-07-29 15:48:06 +00:00
else
cfgfile = CONFDIR"/clamd.conf";
2003-07-29 15:48:06 +00:00
if((copt = getcfg(cfgfile, 1)) == NULL) {
fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
2006-09-05 20:45:39 +00:00
opt_free(opt);
return 1;
2003-07-29 15:48:06 +00:00
}
if(opt_check(opt, "version")) {
print_version(cfgopt(copt, "DatabaseDirectory")->strarg);
opt_free(opt);
freecfg(copt);
return 0;
}
2006-09-05 20:45:39 +00:00
opt_free(opt);
2003-07-29 15:48:06 +00:00
2003-10-02 17:45:47 +00:00
umask(0);
/* drop privileges */
#if (!defined(C_OS2)) && (!defined(C_WINDOWS))
if(geteuid() == 0 && (cpt = cfgopt(copt, "User"))->enabled) {
if((user = getpwnam(cpt->strarg)) == NULL) {
fprintf(stderr, "ERROR: Can't get information about user %s.\n", cpt->strarg);
2006-09-05 20:45:39 +00:00
freecfg(copt);
return 1;
}
if(cfgopt(copt, "AllowSupplementaryGroups")->enabled) {
#ifdef HAVE_INITGROUPS
if(initgroups(cpt->strarg, user->pw_gid)) {
fprintf(stderr, "ERROR: initgroups() failed.\n");
2006-09-05 20:45:39 +00:00
freecfg(copt);
return 1;
}
#else
2007-03-19 20:36:17 +00:00
mprintf("AllowSupplementaryGroups: initgroups() not supported.\n");
#endif
} else {
#ifdef HAVE_SETGROUPS
if(setgroups(1, &user->pw_gid)) {
fprintf(stderr, "ERROR: setgroups() failed.\n");
2006-09-05 20:45:39 +00:00
freecfg(copt);
return 1;
}
#endif
}
if(setgid(user->pw_gid)) {
fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
2006-09-05 20:45:39 +00:00
freecfg(copt);
return 1;
}
if(setuid(user->pw_uid)) {
fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
2006-09-05 20:45:39 +00:00
freecfg(copt);
return 1;
}
}
#endif
2003-07-29 15:48:06 +00:00
/* initialize logger */
logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
logg_time = cfgopt(copt, "LogTime")->enabled;
logok = cfgopt(copt, "LogClean")->enabled;
logg_size = cfgopt(copt, "LogFileMaxSize")->numarg;
logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
2003-07-29 15:48:06 +00:00
if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
2003-08-06 03:05:51 +00:00
cl_debug();
if((cpt = cfgopt(copt, "LogFile"))->enabled) {
char timestr[32];
2004-03-29 00:00:58 +00:00
logg_file = cpt->strarg;
if(strlen(logg_file) < 2 || (logg_file[0] != '/' && logg_file[0] != '\\' && logg_file[1] != ':')) {
2003-07-29 15:48:06 +00:00
fprintf(stderr, "ERROR: LogFile requires full path.\n");
2006-09-05 20:45:39 +00:00
logg_close();
freecfg(copt);
return 1;
2003-07-29 15:48:06 +00:00
}
time(&currtime);
if(logg("#+++ Started at %s", cli_ctime(&currtime, timestr, sizeof(timestr)))) {
2004-03-29 00:00:58 +00:00
fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logg_file);
2006-09-05 20:45:39 +00:00
logg_close();
freecfg(copt);
return 1;
2003-07-29 15:48:06 +00:00
}
} else
2004-03-29 00:00:58 +00:00
logg_file = NULL;
2003-07-29 15:48:06 +00:00
2004-03-29 00:00:58 +00:00
#if defined(USE_SYSLOG) && !defined(C_AIX)
if(cfgopt(copt, "LogSyslog")->enabled) {
2004-05-11 00:14:14 +00:00
int fac = LOG_LOCAL6;
cpt = cfgopt(copt, "LogFacility");
if((fac = logg_facility(cpt->strarg)) == -1) {
2006-09-05 20:45:39 +00:00
logg("!LogFacility: %s: No such facility.\n", cpt->strarg);
logg_close();
freecfg(copt);
return 1;
2004-05-11 00:14:14 +00:00
}
openlog("clamd", LOG_PID, fac);
2004-03-29 00:00:58 +00:00
logg_syslog = 1;
2004-05-11 00:14:14 +00:00
}
2003-07-29 15:48:06 +00:00
#endif
2004-05-11 00:14:14 +00:00
#ifdef C_LINUX
2004-10-20 21:54:08 +00:00
procdev = 0;
if(stat("/proc", &sb) != -1 && !sb.st_size)
2004-05-11 00:14:14 +00:00
procdev = sb.st_dev;
#endif
2003-07-29 15:48:06 +00:00
/* check socket type */
if(cfgopt(copt, "TCPSocket")->enabled)
2003-07-29 15:48:06 +00:00
tcpsock = 1;
if(cfgopt(copt, "LocalSocket")->enabled)
localsock = 1;
if(!tcpsock && !localsock) {
2006-09-05 20:45:39 +00:00
logg("!Please define server type (local and/or TCP).\n");
logg_close();
freecfg(copt);
return 1;
2003-07-29 15:48:06 +00:00
}
2004-02-01 01:18:57 +00:00
/* set the temporary dir */
if((cpt = cfgopt(copt, "TemporaryDirectory"))->enabled)
2004-05-13 00:27:02 +00:00
cl_settempdir(cpt->strarg, 0);
if(cfgopt(copt, "LeaveTemporaryFiles")->enabled)
2004-05-13 00:27:02 +00:00
cl_settempdir(NULL, 1);
2003-07-29 15:48:06 +00:00
logg("#clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
2007-03-19 20:36:17 +00:00
if(user)
logg("#Running as user %s (UID %u, GID %u)\n", user->pw_name, user->pw_uid, user->pw_gid);
2007-03-19 20:36:17 +00:00
if(logg_size)
logg("#Log file size limited to %d bytes.\n", logg_size);
else
logg("#Log file size limit disabled.\n");
2004-02-01 01:18:57 +00:00
/* load the database(s) */
dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
logg("#Reading databases from %s\n", dbdir);
2003-07-29 15:48:06 +00:00
if(cfgopt(copt, "DetectPUA")->enabled)
dboptions |= CL_DB_PUA;
else
logg("#Not loading PUA signatures.\n");
if(cfgopt(copt, "PhishingSignatures")->enabled)
2006-12-20 01:23:50 +00:00
dboptions |= CL_DB_PHISHING;
else
logg("#Not loading phishing signatures.\n");
if(cfgopt(copt,"PhishingScanURLs")->enabled)
dboptions |= CL_DB_PHISHING_URLS;
else
logg("#Disabling URL based phishing detection.\n");
if(cfgopt(copt,"DevACOnly")->enabled) {
logg("#Only using the A-C matcher.\n");
dboptions |= CL_DB_ACONLY;
}
if((cpt = cfgopt(copt, "DevACDepth"))->enabled) {
cli_ac_setdepth(AC_DEFAULT_MIN_DEPTH, cpt->numarg);
logg("#Max A-C depth set to %u\n", cpt->numarg);
}
2007-01-30 21:26:43 +00:00
if((ret = cl_load(dbdir, &engine, &sigs, dboptions))) {
2003-08-29 14:27:15 +00:00
logg("!%s\n", cl_strerror(ret));
2006-09-05 20:45:39 +00:00
logg_close();
freecfg(copt);
return 1;
2003-07-29 15:48:06 +00:00
}
2007-01-30 21:26:43 +00:00
if(!engine) {
2003-07-29 15:48:06 +00:00
logg("!Database initialization error.\n");
2006-09-05 20:45:39 +00:00
logg_close();
freecfg(copt);
return 1;
2003-07-29 15:48:06 +00:00
}
logg("#Loaded %u signatures.\n", sigs);
2007-01-30 21:26:43 +00:00
if((ret = cl_build(engine)) != 0) {
logg("!Database initialization error: %s\n", cl_strerror(ret));;
2006-09-05 20:45:39 +00:00
logg_close();
freecfg(copt);
return 1;
}
2003-07-29 15:48:06 +00:00
2006-09-05 20:45:39 +00:00
if(tcpsock) {
#ifdef C_WINDOWS
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
logg("!Error at WSAStartup(): %d\n", WSAGetLastError());
logg_close();
freecfg(copt);
return 1;
}
#endif
2006-09-05 20:45:39 +00:00
lsockets[nlsockets] = tcpserver(copt);
if(lsockets[nlsockets] == -1) {
logg_close();
freecfg(copt);
return 1;
}
nlsockets++;
}
2006-09-05 20:45:39 +00:00
if(localsock) {
lsockets[nlsockets] = localserver(copt);
if(lsockets[nlsockets] == -1) {
logg_close();
freecfg(copt);
if(tcpsock)
close(lsockets[0]);
return 1;
}
nlsockets++;
}
/* fork into background */
if(!cfgopt(copt, "Foreground")->enabled) {
if(daemonize() == -1) {
logg("!daemonize() failed\n");
logg_close();
freecfg(copt);
return 1;
}
if(!debug_mode)
if(chdir("/") == -1)
logg("^Can't change current working directory to root\n");
} else
foreground = 1;
2007-01-30 21:26:43 +00:00
ret = acceptloop_th(lsockets, nlsockets, engine, dboptions, copt);
2003-07-29 15:48:06 +00:00
#ifdef C_WINDOWS
if(tcpsock)
WSACleanup();
if(!pthread_win32_process_detach_np()) {
logg("!Can't stop the win32 pthreads layer\n");
logg_close();
freecfg(copt);
return 1;
}
#endif
logg_close();
freecfg(copt);
2003-07-29 15:48:06 +00:00
2006-09-05 20:45:39 +00:00
return ret;
2003-07-29 15:48:06 +00:00
}