b151ef55 |
/* |
4bed6861 |
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net> |
b151ef55 |
*
* 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.
*/
|
8b242bb9 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
b151ef55 |
#include <stdio.h>
#include <stdlib.h> |
521b19b4 |
#include <string.h> |
b151ef55 |
#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>
|
36f2038b |
#if defined(USE_SYSLOG) && !defined(C_AIX) |
b151ef55 |
#include <syslog.h>
#endif
|
6cbea664 |
#ifdef C_LINUX
#include <sys/resource.h>
#endif
|
b151ef55 |
#include "options.h" |
36f2038b |
#include "cfgparser.h" |
b151ef55 |
#include "others.h" |
8b242bb9 |
/* Fixes gcc warning */
#include "../libclamav/others.h" |
b151ef55 |
#include "tcpserver.h"
#include "localserver.h"
#include "others.h"
#include "defaults.h" |
36f2038b |
#include "memory.h"
#include "output.h"
#include "shared.h" |
819c7c41 |
#include "target.h" |
e441c102 |
#include "misc.h" |
8b242bb9 |
|
b151ef55 |
void help(void);
void daemonize(void);
|
7fbb6473 |
short debug_mode = 0, logok = 0;
|
b151ef55 |
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; |
096e5bbd |
#ifdef C_LINUX
struct stat sb;
#endif |
b151ef55 |
/* initialize some important variables */
if(optc(opt, 'V')) { |
e441c102 |
print_version(); |
b151ef55 |
exit(0);
}
if(optc(opt, 'h')) {
help();
}
|
6cbea664 |
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 |
4775d04e |
debug_mode = 1; |
6cbea664 |
} |
4775d04e |
|
b151ef55 |
/* parse the config file */
if(optc(opt, 'c'))
cfgfile = getargc(opt, 'c');
else
cfgfile = CL_DEFAULT_CFG;
|
819c7c41 |
if((copt = parsecfg(cfgfile, 1)) == 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")) |
36f2038b |
logg_lock = 0; |
b151ef55 |
if(cfgopt(copt, "LogTime")) |
36f2038b |
logg_time = 1; |
b151ef55 |
|
50099661 |
if(cfgopt(copt, "LogClean"))
logok = 1;
|
b151ef55 |
if((cpt = cfgopt(copt, "LogFileMaxSize"))) |
36f2038b |
logg_size = cpt->numarg; |
b151ef55 |
else |
36f2038b |
logg_size = CL_DEFAULT_LOGSIZE; |
b151ef55 |
|
c72178a4 |
if(cfgopt(copt, "Debug")) /* enable debug messages in libclamav */
cl_debug();
|
b151ef55 |
if(cfgopt(copt, "LogVerbose")) |
36f2038b |
logg_verbose = 1; |
b151ef55 |
else |
36f2038b |
logg_verbose = 0; |
b151ef55 |
if((cpt = cfgopt(copt, "LogFile"))) { |
36f2038b |
logg_file = cpt->strarg; |
193c72c5 |
if(strlen(logg_file) < 2 || (logg_file[0] != '/' && logg_file[0] != '\\' && logg_file[1] != ':')) { |
b151ef55 |
fprintf(stderr, "ERROR: LogFile requires full path.\n");
exit(1);
}
time(&currtime);
if(logg("+++ Started at %s", ctime(&currtime))) { |
36f2038b |
fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logg_file); |
b151ef55 |
exit(1);
}
} else |
36f2038b |
logg_file = NULL; |
b151ef55 |
|
36f2038b |
#if defined(USE_SYSLOG) && !defined(C_AIX) |
096e5bbd |
if(cfgopt(copt, "LogSyslog")) {
int fac = LOG_LOCAL6;
if((cpt = cfgopt(copt, "LogFacility"))) {
if((fac = logg_facility(cpt->strarg)) == -1) {
fprintf(stderr, "ERROR: LogFacility: %s: No such facility.\n", cpt->strarg);
exit(1);
}
}
openlog("clamd", LOG_PID, fac); |
36f2038b |
logg_syslog = 1; |
b151ef55 |
syslog(LOG_INFO, "Daemon started.\n"); |
096e5bbd |
} |
b151ef55 |
#endif
|
819c7c41 |
logg("clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
|
36f2038b |
if(logg_size)
logg("Log file size limited to %d bytes.\n", logg_size); |
b151ef55 |
else
logg("Log file size limit disabled.\n");
logg("*Verbose logging activated.\n");
|
096e5bbd |
#ifdef C_LINUX |
4b7a1d76 |
procdev = 0;
if(stat("/proc", &sb) != -1 && !sb.st_size) |
096e5bbd |
procdev = sb.st_dev;
#endif |
b151ef55 |
/* 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);
}
|
4bed6861 |
/* drop privileges */
#ifndef C_OS2 |
a6c3fdb9 |
if(geteuid() == 0 && (cpt = cfgopt(copt, "User"))) { |
b151ef55 |
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")) { |
819bbe1f |
#ifdef HAVE_INITGROUPS |
eeb69538 |
if(initgroups(cpt->strarg, user->pw_gid)) {
fprintf(stderr, "ERROR: initgroups() failed.\n");
logg("!initgroups() failed.\n");
exit(1);
} |
819bbe1f |
#else
logg("AllowSupplementaryGroups: initgroups() not supported.\n");
#endif |
eeb69538 |
} else { |
819bbe1f |
#ifdef HAVE_SETGROUPS |
eeb69538 |
if(setgroups(1, &user->pw_gid)) {
fprintf(stderr, "ERROR: setgroups() failed.\n");
logg("!setgroups() failed.\n");
exit(1);
} |
819bbe1f |
#endif |
eeb69538 |
}
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);
} |
b151ef55 |
logg("Running as user %s (UID %d, GID %d)\n", cpt->strarg, user->pw_uid, user->pw_gid);
} |
4bed6861 |
#endif |
b151ef55 |
|
50099661 |
/* set the temporary dir */ |
3506c157 |
if((cpt = cfgopt(copt, "TemporaryDirectory")))
cl_settempdir(cpt->strarg, 0);
if(cfgopt(copt, "LeaveTemporaryFiles"))
cl_settempdir(NULL, 1); |
b151ef55 |
|
50099661 |
/* 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); |
f91f55e0 |
if((ret = cl_build(root)) != 0) { |
e8217f5a |
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();
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"); |
8ef2ff6f |
printf(" Clam AntiVirus Daemon "VERSION"\n");
printf(" (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n"); |
a0faaedf |
printf(" --help -h Show this help.\n");
printf(" --version -V Show version number.\n"); |
4775d04e |
printf(" --debug Enable debug mode.\n"); |
a0faaedf |
printf(" --config-file=FILE -c FILE Read configuration from FILE.\n\n"); |
b151ef55 |
exit(0);
}
void daemonize(void)
{
int i;
|
e89a361a |
|
193c72c5 |
#ifdef C_OS2
return;
#else
|
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 |
|
4775d04e |
if(!debug_mode)
chdir("/"); |
b151ef55 |
if(fork())
exit(0);
setsid(); |
193c72c5 |
#endif |
b151ef55 |
} |