b151ef55 |
/*
* Copyright (C) 2002, 2003 Tomasz Kojm <zolw@konarski.edu.pl>
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h> |
e89a361a |
#include <fcntl.h> |
b151ef55 |
#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"
#include "tcpserver.h"
#include "localserver.h"
#include "others.h"
#include "defaults.h"
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; |
e3f00f7e |
int ret, virnum = 0, tcpsock; |
b151ef55 |
/* initialize some important variables */
if(optc(opt, 'V')) {
printf("clamd / ClamAV version "VERSION"\n");
exit(0);
}
if(optc(opt, 'h')) {
help();
}
/* parse the config file */
if(optc(opt, 'c'))
cfgfile = getargc(opt, 'c');
else
cfgfile = CL_DEFAULT_CFG;
if((copt = parsecfg(cfgfile)) == NULL) { |
0d98d74c |
fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile); |
b151ef55 |
exit(1);
}
|
5e27c1c2 |
umask(0);
|
b151ef55 |
/* initialize logger */
if(cfgopt(copt, "LogFileUnlock"))
loglock = 0;
else
loglock = 1;
if(cfgopt(copt, "LogTime"))
logtime = 1;
else
logtime = 0;
if((cpt = cfgopt(copt, "LogFileMaxSize")))
logsize = cpt->numarg;
else
logsize = CL_DEFAULT_LOGSIZE;
|
c72178a4 |
if(cfgopt(copt, "Debug")) /* enable debug messages in libclamav */
cl_debug();
|
b151ef55 |
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);
}
/* load the database(s) */
|
0d98d74c |
if((cpt = cfgopt(copt, "DatabaseDirectory")) || (cpt = cfgopt(copt, "DataDirectory"))) |
b151ef55 |
dbdir = cpt->strarg;
else
dbdir = cl_retdbdir();
logg("Reading databases from %s\n", dbdir);
if((ret = cl_loaddbdir(dbdir, &root, &virnum))) { |
c6259ac5 |
fprintf(stderr, "ERROR: %s\n", cl_strerror(ret));
logg("!%s\n", cl_strerror(ret)); |
b151ef55 |
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); |
e8217f5a |
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);
} |
b151ef55 |
/* fork into background */
if(!cfgopt(copt, "Foreground"))
daemonize();
|
c72178a4 |
|
b151ef55 |
if(tcpsock)
ret = tcpserver(opt, copt, root);
else
ret = localserver(opt, copt, root);
|
ace125c7 |
printf("server ended; result=%d\n", ret); |
9c1c9007 |
logg_close();
freecfg(copt); |
ace125c7 |
printf("free() copt\n"); |
9c1c9007 |
|
b151ef55 |
}
void help(void)
{
printf("\n"); |
a0faaedf |
printf(" Clam AntiVirus Daemon "VERSION"\n"); |
ace125c7 |
printf(" (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>\n\n"); |
a0faaedf |
printf(" --help -h Show this help.\n");
printf(" --version -V Show version number.\n");
printf(" --config-file=FILE -c FILE Read configuration from FILE.\n\n"); |
b151ef55 |
exit(0);
}
void daemonize(void)
{
int i;
|
e89a361a |
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);
} |
b151ef55 |
chdir("/");
if(fork())
exit(0);
setsid();
} |