2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2005-01-18 23:51:53 +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 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.
|
|
|
|
*/
|
|
|
|
|
2004-02-06 13:46:08 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-02-20 15:49:29 +00:00
|
|
|
#include <string.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-11-23 22:42:20 +00:00
|
|
|
#include <fcntl.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <clamav.h>
|
|
|
|
|
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
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include "options.h"
|
2004-03-29 00:00:58 +00:00
|
|
|
#include "cfgparser.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
#include "others.h"
|
2004-02-06 13:46:08 +00:00
|
|
|
/* Fixes gcc warning */
|
|
|
|
#include "../libclamav/others.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 "memory.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "shared.h"
|
2004-06-13 02:11:26 +00:00
|
|
|
#include "target.h"
|
2004-08-23 20:38:10 +00:00
|
|
|
#include "misc.h"
|
2004-02-06 13:46:08 +00:00
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
void help(void);
|
|
|
|
void daemonize(void);
|
|
|
|
|
2004-03-30 21:11:25 +00:00
|
|
|
short debug_mode = 0, logok = 0;
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
void clamd(struct optstruct *opt)
|
|
|
|
{
|
|
|
|
struct cfgstruct *copt, *cpt;
|
|
|
|
struct passwd *user;
|
|
|
|
time_t currtime;
|
|
|
|
struct cl_node *root = NULL;
|
|
|
|
const char *dbdir, *cfgfile;
|
2003-10-24 00:20:56 +00:00
|
|
|
int ret, virnum = 0, tcpsock;
|
2004-05-11 00:14:14 +00:00
|
|
|
#ifdef C_LINUX
|
|
|
|
struct stat sb;
|
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
/* initialize some important variables */
|
|
|
|
|
|
|
|
if(optc(opt, 'V')) {
|
2004-08-23 20:38:10 +00:00
|
|
|
print_version();
|
2003-07-29 15:48:06 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(optc(opt, 'h')) {
|
|
|
|
help();
|
|
|
|
}
|
|
|
|
|
2004-02-27 09:45:46 +00:00
|
|
|
if(optl(opt, "debug")) {
|
|
|
|
#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
|
2004-02-17 00:08:25 +00:00
|
|
|
debug_mode = 1;
|
2004-02-27 09:45:46 +00:00
|
|
|
|
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
/* parse the config file */
|
|
|
|
if(optc(opt, 'c'))
|
|
|
|
cfgfile = getargc(opt, 'c');
|
|
|
|
else
|
2005-06-01 00:42:33 +00:00
|
|
|
cfgfile = CONFDIR"/clamd.conf";
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((copt = getcfg(cfgfile, 1)) == NULL) {
|
2003-12-29 05:31:52 +00:00
|
|
|
fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
|
2003-07-29 15:48:06 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-10-02 17:45:47 +00:00
|
|
|
umask(0);
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
/* initialize logger */
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
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 = cfgopt(copt, "LogVerbose")->enabled;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
|
2003-08-06 03:05:51 +00:00
|
|
|
cl_debug();
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((cpt = cfgopt(copt, "LogFile"))->enabled) {
|
2004-03-29 00:00:58 +00:00
|
|
|
logg_file = cpt->strarg;
|
2004-12-20 01:37:36 +00:00
|
|
|
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");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
time(&currtime);
|
|
|
|
if(logg("+++ Started at %s", ctime(&currtime))) {
|
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);
|
2003-07-29 15:48:06 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} 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)
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "LogSyslog")->enabled) {
|
2004-05-11 00:14:14 +00:00
|
|
|
int fac = LOG_LOCAL6;
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
cpt = cfgopt(copt, "LogFacility");
|
|
|
|
if((fac = logg_facility(cpt->strarg)) == -1) {
|
|
|
|
fprintf(stderr, "ERROR: LogFacility: %s: No such facility.\n", cpt->strarg);
|
|
|
|
exit(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;
|
2003-07-29 15:48:06 +00:00
|
|
|
syslog(LOG_INFO, "Daemon started.\n");
|
2004-05-11 00:14:14 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
#endif
|
|
|
|
|
2004-06-13 02:11:26 +00:00
|
|
|
logg("clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
|
|
|
|
|
2004-03-29 00:00:58 +00:00
|
|
|
if(logg_size)
|
|
|
|
logg("Log file size limited to %d bytes.\n", logg_size);
|
2003-07-29 15:48:06 +00:00
|
|
|
else
|
|
|
|
logg("Log file size limit disabled.\n");
|
|
|
|
|
|
|
|
logg("*Verbose logging activated.\n");
|
|
|
|
|
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 */
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "TCPSocket")->enabled && cfgopt(copt, "LocalSocket")->enabled) {
|
|
|
|
fprintf(stderr, "ERROR: You can only select one mode (local or TCP).\n");
|
|
|
|
logg("!Two modes (local and TCP) selected.\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
exit(1);
|
2005-06-01 00:42:33 +00:00
|
|
|
} else if(cfgopt(copt, "TCPSocket")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
tcpsock = 1;
|
2005-06-01 00:42:33 +00:00
|
|
|
} else if(cfgopt(copt, "LocalSocket")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
tcpsock = 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: You must select server type (local/tcp).\n");
|
|
|
|
logg("!Please select server type (local/TCP).\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-01-18 23:51:53 +00:00
|
|
|
/* drop privileges */
|
|
|
|
#ifndef C_OS2
|
2005-06-01 00:42:33 +00:00
|
|
|
if(geteuid() == 0 && (cpt = cfgopt(copt, "User"))->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
if((user = getpwnam(cpt->strarg)) == NULL) {
|
|
|
|
fprintf(stderr, "ERROR: Can't get information about user %s.\n", cpt->strarg);
|
|
|
|
logg("!Can't get information about user %s.\n", cpt->strarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "AllowSupplementaryGroups")->enabled) {
|
2004-06-12 15:50:35 +00:00
|
|
|
#ifdef HAVE_INITGROUPS
|
2004-06-06 01:50:08 +00:00
|
|
|
if(initgroups(cpt->strarg, user->pw_gid)) {
|
|
|
|
fprintf(stderr, "ERROR: initgroups() failed.\n");
|
|
|
|
logg("!initgroups() failed.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-06-12 15:50:35 +00:00
|
|
|
#else
|
|
|
|
logg("AllowSupplementaryGroups: initgroups() not supported.\n");
|
|
|
|
#endif
|
2004-06-06 01:50:08 +00:00
|
|
|
} else {
|
2004-06-12 15:50:35 +00:00
|
|
|
#ifdef HAVE_SETGROUPS
|
2004-06-06 01:50:08 +00:00
|
|
|
if(setgroups(1, &user->pw_gid)) {
|
|
|
|
fprintf(stderr, "ERROR: setgroups() failed.\n");
|
|
|
|
logg("!setgroups() failed.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-06-12 15:50:35 +00:00
|
|
|
#endif
|
2004-06-06 01:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(setgid(user->pw_gid)) {
|
|
|
|
fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
|
|
|
|
logg("!setgid(%d) failed.\n", (int) user->pw_gid);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setuid(user->pw_uid)) {
|
|
|
|
fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
|
|
|
|
logg("!setuid(%d) failed.\n", (int) user->pw_uid);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
logg("Running as user %s (UID %d, GID %d)\n", cpt->strarg, user->pw_uid, user->pw_gid);
|
|
|
|
}
|
2005-01-18 23:51:53 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-01 01:18:57 +00:00
|
|
|
/* set the temporary dir */
|
2005-06-01 00:42:33 +00:00
|
|
|
if((cpt = cfgopt(copt, "TemporaryDirectory"))->enabled)
|
2004-05-13 00:27:02 +00:00
|
|
|
cl_settempdir(cpt->strarg, 0);
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
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
|
|
|
|
2004-02-01 01:18:57 +00:00
|
|
|
/* load the database(s) */
|
2005-06-01 00:42:33 +00:00
|
|
|
dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
|
2003-07-29 15:48:06 +00:00
|
|
|
logg("Reading databases from %s\n", dbdir);
|
|
|
|
|
|
|
|
if((ret = cl_loaddbdir(dbdir, &root, &virnum))) {
|
2003-08-29 14:27:15 +00:00
|
|
|
fprintf(stderr, "ERROR: %s\n", cl_strerror(ret));
|
|
|
|
logg("!%s\n", cl_strerror(ret));
|
2003-07-29 15:48:06 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!root) {
|
|
|
|
fprintf(stderr, "ERROR: Database initialization error.\n");
|
|
|
|
logg("!Database initialization error.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
logg("Protecting against %d viruses.\n", virnum);
|
2004-07-19 17:54:40 +00:00
|
|
|
if((ret = cl_build(root)) != 0) {
|
2003-12-02 22:48:56 +00:00
|
|
|
fprintf(stderr, "ERROR: Database initialization error: %s\n", cl_strerror(ret));;
|
|
|
|
logg("!Database initialization error: %s\n", cl_strerror(ret));;
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
/* fork into background */
|
2005-06-01 00:42:33 +00:00
|
|
|
if(!cfgopt(copt, "Foreground")->enabled)
|
2003-07-29 15:48:06 +00:00
|
|
|
daemonize();
|
|
|
|
|
|
|
|
if(tcpsock)
|
|
|
|
ret = tcpserver(opt, copt, root);
|
|
|
|
else
|
|
|
|
ret = localserver(opt, copt, root);
|
|
|
|
|
2004-01-20 10:37:54 +00:00
|
|
|
printf("server ended; result=%d\n", ret);
|
2003-12-01 19:28:40 +00:00
|
|
|
logg_close();
|
|
|
|
freecfg(copt);
|
2004-01-20 10:37:54 +00:00
|
|
|
printf("free() copt\n");
|
2003-12-01 19:28:40 +00:00
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void help(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n");
|
2005-03-18 11:57:56 +00:00
|
|
|
printf(" Clam AntiVirus Daemon "VERSION"\n");
|
|
|
|
printf(" (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n");
|
2003-11-11 22:10:27 +00:00
|
|
|
|
|
|
|
printf(" --help -h Show this help.\n");
|
|
|
|
printf(" --version -V Show version number.\n");
|
2004-02-17 00:08:25 +00:00
|
|
|
printf(" --debug Enable debug mode.\n");
|
2003-11-11 22:10:27 +00:00
|
|
|
printf(" --config-file=FILE -c FILE Read configuration from FILE.\n\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemonize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2003-11-23 22:42:20 +00:00
|
|
|
|
2004-12-20 01:37:36 +00:00
|
|
|
#ifdef C_OS2
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
|
2003-11-23 22:42:20 +00:00
|
|
|
if((i = open("/dev/null", O_WRONLY)) == -1) {
|
|
|
|
logg("!Cannot open /dev/null. Only use Debug if Foreground is enabled.\n");
|
|
|
|
for(i = 0; i <= 2; i++)
|
|
|
|
close(i);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
close(0);
|
|
|
|
dup2(i, 1);
|
|
|
|
dup2(i, 2);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
if(!debug_mode)
|
|
|
|
chdir("/");
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
if(fork())
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
setsid();
|
2004-12-20 01:37:36 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|