clamd/clamd.c
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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
b151ef55
  */
 
b3483842
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
24791abc
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
521b19b4
 #include <string.h>
b3483842
 #ifdef HAVE_UNISTD_H
b151ef55
 #include <unistd.h>
 #include <sys/time.h>
b3483842
 #endif
b151ef55
 #include <sys/types.h>
 #include <sys/stat.h>
e89a361a
 #include <fcntl.h>
b151ef55
 #include <time.h>
b3483842
 #ifndef	C_WINDOWS
b151ef55
 #include <pwd.h>
 #include <grp.h>
b3483842
 #endif
b151ef55
 
36f2038b
 #if defined(USE_SYSLOG) && !defined(C_AIX)
b151ef55
 #include <syslog.h>
 #endif
 
6cbea664
 #ifdef C_LINUX
 #include <sys/resource.h>
 #endif
 
e303cf29
 #include "target.h"
 
 #include "libclamav/clamav.h"
 #include "libclamav/others.h"
 
 #include "shared/memory.h"
 #include "shared/output.h"
 #include "shared/options.h"
 #include "shared/cfgparser.h"
 #include "shared/misc.h"
 
 #include "server.h"
b151ef55
 #include "tcpserver.h"
 #include "localserver.h"
 #include "others.h"
36f2038b
 #include "shared.h"
8b242bb9
 
b151ef55
 
7fbb6473
 short debug_mode = 0, logok = 0;
f1c4563e
 short foreground = 0;
 
e303cf29
 void help(void)
 {
     printf("\n");
     printf("                      Clam AntiVirus Daemon "VERSION"\n");
     printf("    (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n");
 
     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");
 
 }
 
2bc31f05
 int main(int argc, char **argv)
b151ef55
 {
 	struct cfgstruct *copt, *cpt;
         struct passwd *user;
 	time_t currtime;
 	struct cl_node *root = NULL;
 	const char *dbdir, *cfgfile;
e303cf29
 	int ret, tcpsock = 0, localsock = 0;
 	unsigned int sigs = 0;
596b1e64
 	int lsockets[2], nlsockets = 0;
8eec8ae5
 	unsigned int dboptions = 0;
096e5bbd
 #ifdef C_LINUX
 	struct stat sb;
 #endif
2bc31f05
 	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}
     	};
 
b3483842
 #ifdef C_WINDOWS
     if(!pthread_win32_process_attach_np()) {
 	mprintf("!Can't start the win32 pthreads layer\n");
         return 1;
     }
 #endif
2bc31f05
 
     opt = opt_parse(argc, argv, short_options, long_options, NULL);
     if(!opt) {
 	mprintf("!Can't parse the command line\n");
 	return 1;
     }
b151ef55
 
2bc31f05
     if(opt_check(opt, "version")) {
e441c102
 	print_version();
e303cf29
 	opt_free(opt);
 	return 0;
b151ef55
     }
 
2bc31f05
     if(opt_check(opt, "help")) {
b151ef55
     	help();
e303cf29
 	opt_free(opt);
 	return 0;
b151ef55
     }
 
2bc31f05
     if(opt_check(opt, "debug")) {
6cbea664
 #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 */
2bc31f05
     if(opt_check(opt, "config-file"))
 	cfgfile = opt_arg(opt, "config-file");
b151ef55
     else
5c4d94a9
 	cfgfile = CONFDIR"/clamd.conf";
b151ef55
 
5c4d94a9
     if((copt = getcfg(cfgfile, 1)) == NULL) {
0d98d74c
 	fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
e303cf29
 	opt_free(opt);
 	return 1;
b151ef55
     }
e303cf29
     opt_free(opt);
b151ef55
 
5e27c1c2
     umask(0);
 
f1c4563e
     /* drop privileges */
b3483842
 #if (!defined(C_OS2)) && (!defined(C_WINDOWS))
f1c4563e
     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);
 	    logg("!Can't get information about user %s.\n", cpt->strarg);
e303cf29
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
f1c4563e
 	}
 
 	if(cfgopt(copt, "AllowSupplementaryGroups")->enabled) {
 #ifdef HAVE_INITGROUPS
 	    if(initgroups(cpt->strarg, user->pw_gid)) {
 		fprintf(stderr, "ERROR: initgroups() failed.\n");
 		logg("!initgroups() failed.\n");
e303cf29
 		logg_close();
 		freecfg(copt);
 		return 1;
f1c4563e
 	    }
 #else
 	    logg("AllowSupplementaryGroups: initgroups() not supported.\n");
 #endif
 	} else {
 #ifdef HAVE_SETGROUPS
 	    if(setgroups(1, &user->pw_gid)) {
 		fprintf(stderr, "ERROR: setgroups() failed.\n");
 		logg("!setgroups() failed.\n");
e303cf29
 		logg_close();
 		freecfg(copt);
 		return 1;
f1c4563e
 	    }
 #endif
 	}
 
 	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);
e303cf29
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
f1c4563e
 	}
 
b3483842
 
f1c4563e
 	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);
e303cf29
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
f1c4563e
 	}
e3035b75
 
 	logg("Running as user %s (UID %d, GID %d)\n", user->pw_name, user->pw_uid, user->pw_gid);
f1c4563e
     }
 #endif
 
b151ef55
     /* initialize logger */
5c4d94a9
     logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
     logg_time = cfgopt(copt, "LogTime")->enabled;
     logok = cfgopt(copt, "LogClean")->enabled;
     logg_size = cfgopt(copt, "LogFileMaxSize")->numarg;
c46b4d08
     logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
b151ef55
 
5c4d94a9
     if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
c72178a4
 	cl_debug();
 
5c4d94a9
     if((cpt = cfgopt(copt, "LogFile"))->enabled) {
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");
e303cf29
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
b151ef55
 	}
 	time(&currtime);
e303cf29
 	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);
e303cf29
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
b151ef55
 	}
     } else
36f2038b
 	logg_file = NULL;
b151ef55
 
36f2038b
 #if defined(USE_SYSLOG) && !defined(C_AIX)
5c4d94a9
     if(cfgopt(copt, "LogSyslog")->enabled) {
096e5bbd
 	    int fac = LOG_LOCAL6;
 
5c4d94a9
 	cpt = cfgopt(copt, "LogFacility");
 	if((fac = logg_facility(cpt->strarg)) == -1) {
e303cf29
 	    logg("!LogFacility: %s: No such facility.\n", cpt->strarg);
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
096e5bbd
 	}
 
 	openlog("clamd", LOG_PID, fac);
36f2038b
 	logg_syslog = 1;
096e5bbd
     }
b151ef55
 #endif
 
e303cf29
     logg("#clamd daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
819c7c41
 
36f2038b
     if(logg_size)
e303cf29
 	logg("#Log file size limited to %d bytes.\n", logg_size);
b151ef55
     else
e303cf29
 	logg("#Log file size limit disabled.\n");
b151ef55
 
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 */
 
596b1e64
     if(cfgopt(copt, "TCPSocket")->enabled)
b151ef55
 	tcpsock = 1;
596b1e64
 
     if(cfgopt(copt, "LocalSocket")->enabled)
 	localsock = 1;
 
     if(!tcpsock && !localsock) {
e303cf29
 	logg("!Please define server type (local and/or TCP).\n");
 	logg_close();
 	freecfg(copt);
 	return 1;
b151ef55
     }
 
50099661
     /* set the temporary dir */
5c4d94a9
     if((cpt = cfgopt(copt, "TemporaryDirectory"))->enabled)
3506c157
 	cl_settempdir(cpt->strarg, 0);
 
5c4d94a9
     if(cfgopt(copt, "LeaveTemporaryFiles")->enabled)
3506c157
 	cl_settempdir(NULL, 1);
b151ef55
 
a94c6905
     /* fork into background */
     if(!cfgopt(copt, "Foreground")->enabled) {
 	daemonize();
 	if(!debug_mode)
 	    chdir("/");
     } else
         foreground = 1;
 
50099661
     /* load the database(s) */
5c4d94a9
     dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
e303cf29
     logg("#Reading databases from %s\n", dbdir);
b151ef55
 
8eec8ae5
     if(!cfgopt(copt, "DetectPhishing")->enabled) {
 	dboptions |= CL_DB_NOPHISHING;
 	logg("Not loading phishing signatures.\n");
     }
 
7def75f3
     if(cfgopt(copt, "NodalCoreAcceleration")->enabled) {
18a65970
 #ifdef HAVE_NCORE
 	dboptions |= CL_DB_NCORE;
a94c6905
 	logg("Enabling support for hardware acceleration.\n");
 #else
 	logg("^Support for hardware acceleration not compiled in.\n");
 #endif
     }
 
e303cf29
     if((ret = cl_load(dbdir, &root, &sigs, dboptions))) {
c6259ac5
 	logg("!%s\n", cl_strerror(ret));
e303cf29
 	logg_close();
 	freecfg(copt);
 	return 1;
b151ef55
     }
 
     if(!root) {
 	logg("!Database initialization error.\n");
e303cf29
 	logg_close();
 	freecfg(copt);
 	return 1;
b151ef55
     }
 
e303cf29
     logg("#Loaded %d signatures.\n", sigs);
f91f55e0
     if((ret = cl_build(root)) != 0) {
e8217f5a
 	logg("!Database initialization error: %s\n", cl_strerror(ret));;
e303cf29
 	logg_close();
 	freecfg(copt);
 	return 1;
e8217f5a
     }
b151ef55
 
e303cf29
     if(tcpsock) {
b3483842
 #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
e303cf29
 	lsockets[nlsockets] = tcpserver(copt);
 	if(lsockets[nlsockets] == -1) {
 	    logg_close();
 	    freecfg(copt);
 	    return 1;
 	}
 	nlsockets++;
     }
596b1e64
 
e303cf29
     if(localsock) {
 	lsockets[nlsockets] = localserver(copt);
 	if(lsockets[nlsockets] == -1) {
 	    logg_close();
 	    freecfg(copt);
 	    if(tcpsock)
 		close(lsockets[0]);
 	    return 1;
 	}
 	nlsockets++;
     }
596b1e64
 
06d74ffa
     ret = acceptloop_th(lsockets, nlsockets, root, dboptions, copt);
b151ef55
 
b3483842
 #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
 
9c1c9007
     logg_close();
     freecfg(copt);
b151ef55
 
e303cf29
     return ret;
b151ef55
 }