clamd/clamd.c
e3aaff8e
 /*
e871e527
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  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
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
67118e92
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
98ac8d19
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
a9ebff44
 #include <string.h>
67118e92
 #ifdef HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
 #include <sys/time.h>
67118e92
 #endif
e3aaff8e
 #include <sys/types.h>
 #include <sys/stat.h>
96b02502
 #include <fcntl.h>
e3aaff8e
 #include <time.h>
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
 #include <pwd.h>
 #include <grp.h>
67118e92
 #endif
e3aaff8e
 
afb48b28
 #if defined(USE_SYSLOG) && !defined(C_AIX)
e3aaff8e
 #include <syslog.h>
 #endif
 
cea858e0
 #ifdef C_LINUX
 #include <sys/resource.h>
 #endif
 
bd8603aa
 #include "target.h"
 
 #include "libclamav/clamav.h"
 #include "libclamav/others.h"
 
 #include "shared/output.h"
 #include "shared/options.h"
 #include "shared/cfgparser.h"
 #include "shared/misc.h"
 
 #include "server.h"
e3aaff8e
 #include "tcpserver.h"
 #include "localserver.h"
 #include "others.h"
afb48b28
 #include "shared.h"
6d6e8271
 
e3aaff8e
 
58bcf502
 short debug_mode = 0, logok = 0;
0ae41a2d
 short foreground = 0;
 
fc83da82
 static void help(void)
bd8603aa
 {
     printf("\n");
     printf("                      Clam AntiVirus Daemon "VERSION"\n");
61409916
     printf("    (C) 2002 - 2007 ClamAV Team - http://www.clamav.net/team\n\n");
bd8603aa
 
     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");
 
 }
 
7b8edc5c
 int main(int argc, char **argv)
e3aaff8e
 {
1095156a
 	struct cfgstruct *copt;
 	const struct cfgstruct *cpt;
ab8d8f52
         struct passwd *user = NULL;
e3aaff8e
 	time_t currtime;
a57e3d41
 	struct cl_engine *engine = NULL;
e3aaff8e
 	const char *dbdir, *cfgfile;
bd8603aa
 	int ret, tcpsock = 0, localsock = 0;
 	unsigned int sigs = 0;
57358cc8
 	int lsockets[2], nlsockets = 0;
d6449522
 	unsigned int dboptions = 0;
c695dab4
 #ifdef C_LINUX
 	struct stat sb;
 #endif
7b8edc5c
 	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}
     	};
 
67118e92
 #ifdef C_WINDOWS
     if(!pthread_win32_process_attach_np()) {
 	mprintf("!Can't start the win32 pthreads layer\n");
         return 1;
     }
 #endif
7b8edc5c
 
     opt = opt_parse(argc, argv, short_options, long_options, NULL);
     if(!opt) {
 	mprintf("!Can't parse the command line\n");
 	return 1;
     }
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "version")) {
7b304dee
 	print_version();
bd8603aa
 	opt_free(opt);
 	return 0;
e3aaff8e
     }
 
7b8edc5c
     if(opt_check(opt, "help")) {
e3aaff8e
     	help();
bd8603aa
 	opt_free(opt);
 	return 0;
e3aaff8e
     }
 
7b8edc5c
     if(opt_check(opt, "debug")) {
cea858e0
 #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
c238ac42
 	debug_mode = 1;
cea858e0
     }
c238ac42
 
e3aaff8e
     /* parse the config file */
7b8edc5c
     if(opt_check(opt, "config-file"))
 	cfgfile = opt_arg(opt, "config-file");
e3aaff8e
     else
81837459
 	cfgfile = CONFDIR"/clamd.conf";
e3aaff8e
 
81837459
     if((copt = getcfg(cfgfile, 1)) == NULL) {
95d401c4
 	fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
bd8603aa
 	opt_free(opt);
 	return 1;
e3aaff8e
     }
bd8603aa
     opt_free(opt);
e3aaff8e
 
3ff40d08
     umask(0);
 
0ae41a2d
     /* drop privileges */
67118e92
 #if (!defined(C_OS2)) && (!defined(C_WINDOWS))
0ae41a2d
     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);
bd8603aa
 	    freecfg(copt);
 	    return 1;
0ae41a2d
 	}
 
 	if(cfgopt(copt, "AllowSupplementaryGroups")->enabled) {
 #ifdef HAVE_INITGROUPS
 	    if(initgroups(cpt->strarg, user->pw_gid)) {
 		fprintf(stderr, "ERROR: initgroups() failed.\n");
bd8603aa
 		freecfg(copt);
 		return 1;
0ae41a2d
 	    }
 #else
ab8d8f52
 	    mprintf("AllowSupplementaryGroups: initgroups() not supported.\n");
0ae41a2d
 #endif
 	} else {
 #ifdef HAVE_SETGROUPS
 	    if(setgroups(1, &user->pw_gid)) {
 		fprintf(stderr, "ERROR: setgroups() failed.\n");
bd8603aa
 		freecfg(copt);
 		return 1;
0ae41a2d
 	    }
 #endif
 	}
 
 	if(setgid(user->pw_gid)) {
 	    fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
bd8603aa
 	    freecfg(copt);
 	    return 1;
0ae41a2d
 	}
 
 	if(setuid(user->pw_uid)) {
 	    fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
bd8603aa
 	    freecfg(copt);
 	    return 1;
0ae41a2d
 	}
     }
 #endif
 
e3aaff8e
     /* initialize logger */
81837459
     logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
     logg_time = cfgopt(copt, "LogTime")->enabled;
     logok = cfgopt(copt, "LogClean")->enabled;
     logg_size = cfgopt(copt, "LogFileMaxSize")->numarg;
234582ae
     logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
e3aaff8e
 
81837459
     if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
0249f9d2
 	cl_debug();
 
81837459
     if((cpt = cfgopt(copt, "LogFile"))->enabled) {
afb48b28
 	logg_file = cpt->strarg;
75ccac9f
 	if(strlen(logg_file) < 2 || (logg_file[0] != '/' && logg_file[0] != '\\' && logg_file[1] != ':')) {
e3aaff8e
 	    fprintf(stderr, "ERROR: LogFile requires full path.\n");
bd8603aa
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
e3aaff8e
 	}
 	time(&currtime);
bd8603aa
 	if(logg("#+++ Started at %s", ctime(&currtime))) {
afb48b28
 	    fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logg_file);
bd8603aa
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
e3aaff8e
 	}
     } else
afb48b28
 	logg_file = NULL;
e3aaff8e
 
afb48b28
 #if defined(USE_SYSLOG) && !defined(C_AIX)
81837459
     if(cfgopt(copt, "LogSyslog")->enabled) {
c695dab4
 	    int fac = LOG_LOCAL6;
 
81837459
 	cpt = cfgopt(copt, "LogFacility");
 	if((fac = logg_facility(cpt->strarg)) == -1) {
bd8603aa
 	    logg("!LogFacility: %s: No such facility.\n", cpt->strarg);
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
c695dab4
 	}
 
 	openlog("clamd", LOG_PID, fac);
afb48b28
 	logg_syslog = 1;
c695dab4
     }
e3aaff8e
 #endif
 
c695dab4
 #ifdef C_LINUX
53c7b870
     procdev = 0;
     if(stat("/proc", &sb) != -1 && !sb.st_size)
c695dab4
 	procdev = sb.st_dev;
 #endif
e3aaff8e
 
     /* check socket type */
 
57358cc8
     if(cfgopt(copt, "TCPSocket")->enabled)
e3aaff8e
 	tcpsock = 1;
57358cc8
 
     if(cfgopt(copt, "LocalSocket")->enabled)
 	localsock = 1;
 
     if(!tcpsock && !localsock) {
bd8603aa
 	logg("!Please define server type (local and/or TCP).\n");
 	logg_close();
 	freecfg(copt);
 	return 1;
e3aaff8e
     }
 
ee039e40
     /* set the temporary dir */
81837459
     if((cpt = cfgopt(copt, "TemporaryDirectory"))->enabled)
590135f9
 	cl_settempdir(cpt->strarg, 0);
 
81837459
     if(cfgopt(copt, "LeaveTemporaryFiles")->enabled)
590135f9
 	cl_settempdir(NULL, 1);
e3aaff8e
 
8765287e
     /* fork into background */
     if(!cfgopt(copt, "Foreground")->enabled) {
 	daemonize();
 	if(!debug_mode)
 	    chdir("/");
     } else
         foreground = 1;
 
50b26397
     logg("clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
 
ab8d8f52
     if(user)
 	logg("Running as user %s (UID %u, GID %u)\n", user->pw_name, user->pw_uid, user->pw_gid);
 
50b26397
     if(logg_size)
 	logg("Log file size limited to %d bytes.\n", logg_size);
     else
 	logg("Log file size limit disabled.\n");
 
ee039e40
     /* load the database(s) */
81837459
     dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
50b26397
     logg("Reading databases from %s\n", dbdir);
e3aaff8e
 
af7d0dde
     if(cfgopt(copt, "PhishingSignatures")->enabled)
9f8098c0
 	dboptions |= CL_DB_PHISHING;
     else
d6449522
 	logg("Not loading phishing signatures.\n");
 
19b3e182
     if(cfgopt(copt,"PhishingScanURLs")->enabled)
 	dboptions |= CL_DB_PHISHING_URLS;
     else
 	logg("Disabling URL based phishing detection.\n");
 
b5456d64
     if(cfgopt(copt, "NodalCoreAcceleration")->enabled) {
87ceb645
 #ifdef HAVE_NCORE
 	dboptions |= CL_DB_NCORE;
8765287e
 	logg("Enabling support for hardware acceleration.\n");
 #else
 	logg("^Support for hardware acceleration not compiled in.\n");
 #endif
     }
 
a57e3d41
     if((ret = cl_load(dbdir, &engine, &sigs, dboptions))) {
049a18b9
 	logg("!%s\n", cl_strerror(ret));
bd8603aa
 	logg_close();
 	freecfg(copt);
 	return 1;
e3aaff8e
     }
 
a57e3d41
     if(!engine) {
e3aaff8e
 	logg("!Database initialization error.\n");
bd8603aa
 	logg_close();
 	freecfg(copt);
 	return 1;
e3aaff8e
     }
 
f203ea83
     logg("Loaded %d signatures.\n", sigs);
a57e3d41
     if((ret = cl_build(engine)) != 0) {
2d70a403
 	logg("!Database initialization error: %s\n", cl_strerror(ret));;
bd8603aa
 	logg_close();
 	freecfg(copt);
 	return 1;
2d70a403
     }
e3aaff8e
 
bd8603aa
     if(tcpsock) {
67118e92
 #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
bd8603aa
 	lsockets[nlsockets] = tcpserver(copt);
 	if(lsockets[nlsockets] == -1) {
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
 	}
 	nlsockets++;
     }
57358cc8
 
bd8603aa
     if(localsock) {
 	lsockets[nlsockets] = localserver(copt);
 	if(lsockets[nlsockets] == -1) {
 	    logg_close();
 	    freecfg(copt);
 	    if(tcpsock)
 		close(lsockets[0]);
 	    return 1;
 	}
 	nlsockets++;
     }
57358cc8
 
a57e3d41
     ret = acceptloop_th(lsockets, nlsockets, engine, dboptions, copt);
e3aaff8e
 
67118e92
 #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
 
9e431a95
     logg_close();
     freecfg(copt);
e3aaff8e
 
bd8603aa
     return ret;
e3aaff8e
 }