clamav-devel/clamd/clamd.c
e3aaff8e
 /*
ee039e40
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
e3aaff8e
  *
  *  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.
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
a9ebff44
 #include <string.h>
e3aaff8e
 #include <unistd.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
96b02502
 #include <fcntl.h>
e3aaff8e
 #include <time.h>
 #include <pwd.h>
 #include <grp.h>
 #include <clamav.h>
 
afb48b28
 #if defined(USE_SYSLOG) && !defined(C_AIX)
e3aaff8e
 #include <syslog.h>
 #endif
 
cea858e0
 #ifdef C_LINUX
 #include <sys/resource.h>
 #endif
 
e3aaff8e
 #include "options.h"
afb48b28
 #include "cfgparser.h"
e3aaff8e
 #include "others.h"
6d6e8271
 /* Fixes gcc warning */
 #include "../libclamav/others.h"
e3aaff8e
 #include "tcpserver.h"
 #include "localserver.h"
 #include "others.h"
 #include "defaults.h"
afb48b28
 #include "memory.h"
 #include "output.h"
 #include "shared.h"
6d6e8271
 
e3aaff8e
 void help(void);
 void daemonize(void);
 
58bcf502
 short debug_mode = 0, logok = 0;
 
e3aaff8e
 void clamd(struct optstruct *opt)
 {
 	struct cfgstruct *copt, *cpt;
         struct passwd *user;
 	time_t currtime;
 	struct cl_node *root = NULL;
 	const char *dbdir, *cfgfile;
22801d05
 	int ret, virnum = 0, tcpsock;
bbbf3c87
 	char *var;
c695dab4
 #ifdef C_LINUX
 	struct stat sb;
 #endif
e3aaff8e
 
     /* initialize some important variables */
 
     if(optc(opt, 'V')) {
 	printf("clamd / ClamAV version "VERSION"\n");
 	exit(0);
     }
 
     if(optc(opt, 'h')) {
     	help();
     }
 
cea858e0
     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
c238ac42
 	debug_mode = 1;
cea858e0
 
     }
c238ac42
 
e3aaff8e
     /* parse the config file */
     if(optc(opt, 'c'))
 	cfgfile = getargc(opt, 'c');
     else
 	cfgfile = CL_DEFAULT_CFG;
 
     if((copt = parsecfg(cfgfile)) == NULL) {
95d401c4
 	fprintf(stderr, "ERROR: Can't open/parse the config file %s\n", cfgfile);
e3aaff8e
 	exit(1);
     }
 
3ff40d08
     umask(0);
 
e3aaff8e
     /* initialize logger */
 
     if(cfgopt(copt, "LogFileUnlock"))
afb48b28
 	logg_lock = 0;
e3aaff8e
 
     if(cfgopt(copt, "LogTime"))
afb48b28
 	logg_time = 1;
e3aaff8e
 
ee039e40
     if(cfgopt(copt, "LogClean"))
 	logok = 1;
 
e3aaff8e
     if((cpt = cfgopt(copt, "LogFileMaxSize")))
afb48b28
 	logg_size = cpt->numarg;
e3aaff8e
     else
afb48b28
 	logg_size = CL_DEFAULT_LOGSIZE;
e3aaff8e
 
0249f9d2
     if(cfgopt(copt, "Debug")) /* enable debug messages in libclamav */
 	cl_debug();
 
e3aaff8e
     if(cfgopt(copt, "LogVerbose"))
afb48b28
 	logg_verbose = 1;
e3aaff8e
     else
afb48b28
 	logg_verbose = 0;
e3aaff8e
 
     if((cpt = cfgopt(copt, "LogFile"))) {
afb48b28
 	logg_file = cpt->strarg;
 	if(logg_file[0] != '/') {
e3aaff8e
 	    fprintf(stderr, "ERROR: LogFile requires full path.\n");
 	    exit(1);
 	}
 	time(&currtime);
 	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);
e3aaff8e
 	    exit(1);
 	}
     } else
afb48b28
 	logg_file = NULL;
e3aaff8e
 
 
afb48b28
 #if defined(USE_SYSLOG) && !defined(C_AIX)
c695dab4
     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);
afb48b28
 	logg_syslog = 1;
e3aaff8e
 	syslog(LOG_INFO, "Daemon started.\n");
c695dab4
     }
e3aaff8e
 #endif
 
afb48b28
     if(logg_size)
 	logg("Log file size limited to %d bytes.\n", logg_size);
e3aaff8e
     else
 	logg("Log file size limit disabled.\n");
 
     logg("*Verbose logging activated.\n");
 
c695dab4
 #ifdef C_LINUX
     if(stat("/proc", &sb) == -1)
 	procdev = 0;
     else
 	procdev = sb.st_dev;
 #endif
e3aaff8e
 
     /* 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")) {
9f51cb51
 	    if(initgroups(cpt->strarg, user->pw_gid)) {
 		fprintf(stderr, "ERROR: initgroups() failed.\n");
 		logg("!initgroups() failed.\n");
 		exit(1);
 	    }
 	} else {
 	    if(setgroups(1, &user->pw_gid)) {
 		fprintf(stderr, "ERROR: setgroups() failed.\n");
 		logg("!setgroups() failed.\n");
 		exit(1);
 	    }
 	}
 
 	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);
 	}
e3aaff8e
 
 	logg("Running as user %s (UID %d, GID %d)\n", cpt->strarg, user->pw_uid, user->pw_gid);
     }
 
ee039e40
     /* set the temporary dir */
590135f9
     if((cpt = cfgopt(copt, "TemporaryDirectory")))
 	cl_settempdir(cpt->strarg, 0);
 
     if(cfgopt(copt, "LeaveTemporaryFiles"))
 	cl_settempdir(NULL, 1);
e3aaff8e
 
ee039e40
     /* load the database(s) */
95d401c4
     if((cpt = cfgopt(copt, "DatabaseDirectory")) || (cpt = cfgopt(copt, "DataDirectory")))
e3aaff8e
 	dbdir = cpt->strarg;
     else
 	dbdir = cl_retdbdir();
 
     logg("Reading databases from %s\n", dbdir);
 
     if((ret = cl_loaddbdir(dbdir, &root, &virnum))) {
049a18b9
 	fprintf(stderr, "ERROR: %s\n", cl_strerror(ret));
 	logg("!%s\n", cl_strerror(ret));
e3aaff8e
 	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);
2d70a403
     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);
     }
e3aaff8e
 
 
     /* fork into background */
     if(!cfgopt(copt, "Foreground"))
 	daemonize();
 
0249f9d2
 
e3aaff8e
     if(tcpsock)
 	ret = tcpserver(opt, copt, root);
     else
 	ret = localserver(opt, copt, root);
 
81131381
     printf("server ended; result=%d\n", ret);
9e431a95
     logg_close();
     freecfg(copt);
81131381
     printf("free() copt\n");
9e431a95
 
e3aaff8e
 }
 
 void help(void)
 {
 
     printf("\n");
5def21ff
     printf("                           Clam AntiVirus Daemon "VERSION"\n");
13c03b49
     printf("   (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net> and Trog <trog@clamav.net>\n\n");
5def21ff
 
     printf("    --help                   -h             Show this help.\n");
     printf("    --version                -V             Show version number.\n");
c238ac42
     printf("    --debug                                 Enable debug mode.\n");
5def21ff
     printf("    --config-file=FILE       -c FILE        Read configuration from FILE.\n\n");
e3aaff8e
 
     exit(0);
 }
 
 void daemonize(void)
 {
 	int i;
 
96b02502
 
     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);
     }
e3aaff8e
 
c238ac42
     if(!debug_mode)
 	chdir("/");
e3aaff8e
 
     if(fork())
 	exit(0);
 
     setsid();
 }