clamav/clamav-devel/clamd/clamd.c

285 lines
6.7 KiB
C
Raw Normal View History

2003-07-29 15:48:06 +00:00
/*
2004-02-01 01:18:57 +00:00
* Copyright (C) 2002 - 2004 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.
*/
#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>
2003-07-29 15:48:06 +00:00
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2003-07-29 15:48:06 +00:00
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <clamav.h>
#if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
#include <syslog.h>
#endif
#include "options.h"
#include "cfgfile.h"
#include "others.h"
/* Fixes gcc warning */
#include "../libclamav/others.h"
2003-07-29 15:48:06 +00:00
#include "tcpserver.h"
#include "localserver.h"
#include "others.h"
#include "defaults.h"
2003-07-29 15:48:06 +00:00
void help(void);
void daemonize(void);
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-02-01 16:53:12 +00:00
char *var;
2003-07-29 15:48:06 +00:00
/* initialize some important variables */
if(optc(opt, 'V')) {
printf("clamd / ClamAV version "VERSION"\n");
exit(0);
}
if(optc(opt, 'h')) {
help();
}
if(optl(opt, "debug"))
debug_mode = 1;
else
debug_mode = 0;
2003-07-29 15:48:06 +00:00
/* parse the config file */
if(optc(opt, 'c'))
cfgfile = getargc(opt, 'c');
else
cfgfile = CL_DEFAULT_CFG;
if((copt = parsecfg(cfgfile)) == NULL) {
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 */
if(cfgopt(copt, "LogFileUnlock"))
loglock = 0;
else
loglock = 1;
if(cfgopt(copt, "LogTime"))
logtime = 1;
else
logtime = 0;
2004-02-01 01:18:57 +00:00
if(cfgopt(copt, "LogClean"))
logok = 1;
else
logok = 0;
2003-07-29 15:48:06 +00:00
if((cpt = cfgopt(copt, "LogFileMaxSize")))
logsize = cpt->numarg;
else
logsize = CL_DEFAULT_LOGSIZE;
2003-08-06 03:05:51 +00:00
if(cfgopt(copt, "Debug")) /* enable debug messages in libclamav */
cl_debug();
2003-07-29 15:48:06 +00:00
if(cfgopt(copt, "LogVerbose"))
logverbose = 1;
else
logverbose = 0;
if((cpt = cfgopt(copt, "LogFile"))) {
logfile = cpt->strarg;
if(logfile[0] != '/') {
fprintf(stderr, "ERROR: LogFile requires full path.\n");
exit(1);
}
time(&currtime);
if(logg("+++ Started at %s", ctime(&currtime))) {
fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logfile);
exit(1);
}
} else
logfile = NULL;
#if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
if((cpt = cfgopt(copt, "LogSyslog"))) {
openlog("clamd", LOG_PID, LOG_LOCAL6);
use_syslog = 1;
syslog(LOG_INFO, "Daemon started.\n");
} else
use_syslog = 0;
#endif
if(logsize)
logg("Log file size limited to %d bytes.\n", logsize);
else
logg("Log file size limit disabled.\n");
logg("*Verbose logging activated.\n");
/* check socket type */
if(cfgopt(copt, "TCPSocket") && cfgopt(copt, "LocalSocket")) {
fprintf(stderr, "ERROR: You can select one mode only (local/TCP).\n");
logg("!Two modes (local & TCP) selected.\n");
exit(1);
} else if(cfgopt(copt, "TCPSocket")) {
tcpsock = 1;
} else if(cfgopt(copt, "LocalSocket")) {
tcpsock = 0;
} else {
fprintf(stderr, "ERROR: You must select server type (local/tcp).\n");
logg("!Please select server type (local/TCP).\n");
exit(1);
}
/* drop priviledges */
if(getuid() == 0 && (cpt = cfgopt(copt, "User"))) {
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);
}
if(cfgopt(copt, "AllowSupplementaryGroups")) {
initgroups(cpt->strarg, user->pw_gid);
} else
setgroups(1, &user->pw_gid);
setgid(user->pw_gid);
setuid(user->pw_uid);
logg("Running as user %s (UID %d, GID %d)\n", cpt->strarg, user->pw_uid, user->pw_gid);
}
2004-02-01 01:18:57 +00:00
/* set the temporary dir */
if((cpt = cfgopt(copt, "TemporaryDirectory"))) {
2004-02-01 16:53:12 +00:00
var = (char *) mcalloc(8 + strlen(cpt->strarg), sizeof(char));
sprintf(var, "TMPDIR=%s", cpt->strarg);
if(!putenv(var))
2004-02-01 01:18:57 +00:00
logg("Setting %s as global temporary directory\n", cpt->strarg);
else
logg("!Can't set TMPDIR variable - insufficient space in the environment.\n");
2004-02-01 16:53:12 +00:00
free(var);
2004-02-01 01:18:57 +00:00
}
2003-07-29 15:48:06 +00:00
2004-02-01 01:18:57 +00:00
/* load the database(s) */
if((cpt = cfgopt(copt, "DatabaseDirectory")) || (cpt = cfgopt(copt, "DataDirectory")))
2003-07-29 15:48:06 +00:00
dbdir = cpt->strarg;
else
dbdir = cl_retdbdir();
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);
if((ret = cl_buildtrie(root)) != 0) {
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 */
if(!cfgopt(copt, "Foreground"))
daemonize();
2003-08-06 03:05:51 +00:00
2003-07-29 15:48:06 +00:00
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);
logg_close();
freecfg(copt);
2004-01-20 10:37:54 +00:00
printf("free() copt\n");
2003-07-29 15:48:06 +00:00
}
void help(void)
{
printf("\n");
2003-11-11 22:10:27 +00:00
printf(" Clam AntiVirus Daemon "VERSION"\n");
2004-01-20 10:37:54 +00:00
printf(" (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>\n\n");
2003-11-11 22:10:27 +00:00
printf(" --help -h Show this help.\n");
printf(" --version -V Show version number.\n");
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;
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
if(!debug_mode)
chdir("/");
2003-07-29 15:48:06 +00:00
if(fork())
exit(0);
setsid();
}