2004-08-23 20:38:10 +00:00
|
|
|
/*
|
2009-02-13 10:55:45 +00:00
|
|
|
* Copyright (C) 2007-2009 Sourcefire, Inc.
|
|
|
|
*
|
|
|
|
* Authors: Tomasz Kojm
|
2004-08-23 20:38:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-03-31 20:31:04 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2004-08-23 20:38:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2004-08-23 20:38:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <stdlib.h>
|
2006-09-27 10:49:26 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <unistd.h>
|
2006-09-27 10:49:26 +00:00
|
|
|
#endif
|
2004-08-23 20:38:10 +00:00
|
|
|
#include <string.h>
|
2004-09-04 15:48:30 +00:00
|
|
|
#include <time.h>
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2009-02-17 17:35:31 +00:00
|
|
|
#include <sys/socket.h>
|
2006-06-08 20:00:50 +00:00
|
|
|
#include <dirent.h>
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <fcntl.h>
|
2006-04-10 09:59:51 +00:00
|
|
|
#include <ctype.h>
|
2006-06-08 20:00:50 +00:00
|
|
|
#include <errno.h>
|
2004-08-23 20:38:10 +00:00
|
|
|
|
2009-01-02 21:45:46 +00:00
|
|
|
#include "shared/optparser.h"
|
2006-09-27 10:49:26 +00:00
|
|
|
#include "shared/output.h"
|
2004-09-07 21:19:02 +00:00
|
|
|
|
2006-09-27 10:49:26 +00:00
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
#include "libclamav/cvd.h"
|
2007-03-05 21:05:22 +00:00
|
|
|
#include "libclamav/others.h" /* for cli_rmdirs() */
|
2008-07-18 16:31:02 +00:00
|
|
|
#include "libclamav/regex/regex.h"
|
2008-08-04 10:38:24 +00:00
|
|
|
#include "libclamav/version.h"
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "shared/misc.h"
|
2006-09-27 10:49:26 +00:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
2006-06-09 10:46:12 +00:00
|
|
|
|
2008-08-04 10:38:24 +00:00
|
|
|
#ifndef REPO_VERSION
|
|
|
|
#define REPO_VERSION "exported"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *get_version(void)
|
|
|
|
{
|
|
|
|
if(!strncmp("devel-",VERSION,6) && strcmp("exported",REPO_VERSION)) {
|
2008-10-09 09:00:26 +00:00
|
|
|
return REPO_VERSION""VERSION_SUFFIX;
|
2008-08-04 10:38:24 +00:00
|
|
|
}
|
|
|
|
/* it is a release, or we have nothing better */
|
2008-10-09 09:00:26 +00:00
|
|
|
return VERSION""VERSION_SUFFIX;
|
2008-08-04 10:38:24 +00:00
|
|
|
}
|
2008-01-10 13:57:05 +00:00
|
|
|
/* CL_NOLIBCLAMAV means to omit functions that depends on libclamav */
|
|
|
|
#ifndef CL_NOLIBCLAMAV
|
2005-01-26 14:15:04 +00:00
|
|
|
char *freshdbdir(void)
|
2004-08-23 20:38:10 +00:00
|
|
|
{
|
|
|
|
struct cl_cvd *d1, *d2;
|
2009-01-02 21:45:46 +00:00
|
|
|
struct optstruct *opts;
|
|
|
|
const struct optstruct *opt;
|
2004-08-23 20:38:10 +00:00
|
|
|
const char *dbdir;
|
2005-01-26 14:15:04 +00:00
|
|
|
char *retdir;
|
2004-08-23 20:38:10 +00:00
|
|
|
|
2006-10-29 19:13:11 +00:00
|
|
|
|
2009-01-02 21:45:46 +00:00
|
|
|
/* try to find the most up-to-date db directory */
|
2004-08-23 20:38:10 +00:00
|
|
|
dbdir = cl_retdbdir();
|
2009-01-02 21:45:46 +00:00
|
|
|
if((opts = optparse(CONFDIR"/freshclam.conf", 0, NULL, 0, OPT_FRESHCLAM, 0, NULL))) {
|
|
|
|
if((opt = optget(opts, "DatabaseDirectory"))->enabled) {
|
|
|
|
if(strcmp(dbdir, opt->strarg)) {
|
|
|
|
char *daily = (char *) malloc(strlen(opt->strarg) + strlen(dbdir) + 30);
|
|
|
|
sprintf(daily, "%s/daily.cvd", opt->strarg);
|
2008-01-03 12:10:53 +00:00
|
|
|
if(access(daily, R_OK))
|
2009-01-02 21:45:46 +00:00
|
|
|
sprintf(daily, "%s/daily.cld", opt->strarg);
|
2006-10-29 19:13:11 +00:00
|
|
|
|
2008-02-19 14:37:04 +00:00
|
|
|
if(!access(daily, R_OK) && (d1 = cl_cvdhead(daily))) {
|
2004-12-01 20:11:17 +00:00
|
|
|
sprintf(daily, "%s/daily.cvd", dbdir);
|
2008-01-03 12:10:53 +00:00
|
|
|
if(access(daily, R_OK))
|
|
|
|
sprintf(daily, "%s/daily.cld", dbdir);
|
2006-10-29 19:13:11 +00:00
|
|
|
|
2008-02-19 14:37:04 +00:00
|
|
|
if(!access(daily, R_OK) && (d2 = cl_cvdhead(daily))) {
|
2004-08-23 20:38:10 +00:00
|
|
|
free(daily);
|
|
|
|
if(d1->version > d2->version)
|
2009-01-02 21:45:46 +00:00
|
|
|
dbdir = opt->strarg;
|
2004-08-23 20:38:10 +00:00
|
|
|
cl_cvdfree(d2);
|
|
|
|
} else {
|
|
|
|
free(daily);
|
2009-01-02 21:45:46 +00:00
|
|
|
dbdir = opt->strarg;
|
2004-08-23 20:38:10 +00:00
|
|
|
}
|
|
|
|
cl_cvdfree(d1);
|
|
|
|
} else {
|
|
|
|
free(daily);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-26 14:15:04 +00:00
|
|
|
retdir = strdup(dbdir);
|
|
|
|
|
2009-01-02 21:45:46 +00:00
|
|
|
if(opts)
|
|
|
|
optfree(opts);
|
2005-01-26 14:15:04 +00:00
|
|
|
|
|
|
|
return retdir;
|
2004-08-23 20:38:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 10:42:30 +00:00
|
|
|
void print_version(const char *dbdir)
|
2004-08-23 20:38:10 +00:00
|
|
|
{
|
2009-05-05 14:31:53 +00:00
|
|
|
char *fdbdir = NULL, *path;
|
2008-01-22 10:42:30 +00:00
|
|
|
const char *pt;
|
2004-08-23 20:38:10 +00:00
|
|
|
struct cl_cvd *daily;
|
|
|
|
|
|
|
|
|
2008-01-22 10:42:30 +00:00
|
|
|
if(dbdir)
|
|
|
|
pt = dbdir;
|
|
|
|
else
|
|
|
|
pt = fdbdir = freshdbdir();
|
|
|
|
|
|
|
|
if(!pt) {
|
2008-08-23 20:24:04 +00:00
|
|
|
printf("ClamAV %s\n",get_version());
|
2008-01-22 10:42:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(path = malloc(strlen(pt) + 11))) {
|
|
|
|
if(!dbdir)
|
|
|
|
free(fdbdir);
|
2004-08-23 20:38:10 +00:00
|
|
|
return;
|
2005-01-26 14:15:04 +00:00
|
|
|
}
|
2004-08-23 20:38:10 +00:00
|
|
|
|
2008-01-22 10:42:30 +00:00
|
|
|
sprintf(path, "%s/daily.cvd", pt);
|
2008-01-03 12:10:53 +00:00
|
|
|
if(access(path, R_OK))
|
2008-01-22 10:42:30 +00:00
|
|
|
sprintf(path, "%s/daily.cld", pt);
|
|
|
|
|
|
|
|
if(!dbdir)
|
|
|
|
free(fdbdir);
|
2004-08-23 20:38:10 +00:00
|
|
|
|
2008-01-03 12:10:53 +00:00
|
|
|
if(!access(path, R_OK) && (daily = cl_cvdhead(path))) {
|
2004-09-04 15:48:30 +00:00
|
|
|
time_t t = (time_t) daily->stime;
|
2007-02-21 21:07:23 +00:00
|
|
|
|
2008-08-23 20:24:04 +00:00
|
|
|
printf("ClamAV %s/%d/%s", get_version(), daily->version, ctime(&t));
|
2004-09-04 15:48:30 +00:00
|
|
|
cl_cvdfree(daily);
|
2004-08-23 20:38:10 +00:00
|
|
|
} else {
|
2008-08-23 20:24:04 +00:00
|
|
|
printf("ClamAV %s\n",get_version());
|
2004-08-23 20:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
}
|
2008-01-10 13:57:05 +00:00
|
|
|
#endif
|
2009-02-03 18:47:18 +00:00
|
|
|
|
2009-05-21 13:43:05 +00:00
|
|
|
const char *filelist(const struct optstruct *opts, int *err)
|
|
|
|
{
|
|
|
|
static char buff[1025];
|
|
|
|
static unsigned int cnt = 0;
|
|
|
|
const struct optstruct *opt;
|
|
|
|
static FILE *fs = NULL;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if(!cnt && (opt = optget(opts, "file-list"))->enabled) {
|
|
|
|
if(!fs) {
|
|
|
|
fs = fopen(opt->strarg, "r");
|
|
|
|
if(!fs) {
|
|
|
|
fprintf(stderr, "ERROR: --file-list: Can't open file %s\n", opt->strarg);
|
|
|
|
if(err)
|
|
|
|
*err = 54;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fgets(buff, 1024, fs)) {
|
|
|
|
buff[1024] = 0;
|
|
|
|
len = strlen(buff);
|
|
|
|
if(!len) {
|
|
|
|
fclose(fs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
len--;
|
|
|
|
while(len && ((buff[len] == '\n') || (buff[len] == '\r')))
|
|
|
|
buff[len--] = '\0';
|
|
|
|
return buff;
|
|
|
|
} else {
|
|
|
|
fclose(fs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts->filename ? opts->filename[cnt++] : NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-28 23:26:29 +00:00
|
|
|
int filecopy(const char *src, const char *dest)
|
|
|
|
{
|
|
|
|
#ifdef C_DARWIN
|
2005-04-28 22:58:31 +00:00
|
|
|
pid_t pid;
|
2004-11-28 23:26:29 +00:00
|
|
|
|
2005-04-28 22:58:31 +00:00
|
|
|
/* On Mac OS X use ditto and copy resource fork, too. */
|
|
|
|
switch(pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
2008-01-10 18:01:16 +00:00
|
|
|
execl("/usr/bin/ditto", "ditto", src, dest, NULL);
|
2006-06-09 10:46:12 +00:00
|
|
|
perror("execl(ditto)");
|
2005-04-28 22:58:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wait(NULL);
|
|
|
|
return 0;
|
2004-11-28 23:26:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 22:58:31 +00:00
|
|
|
return -1;
|
2004-11-28 23:26:29 +00:00
|
|
|
|
2009-02-03 18:47:18 +00:00
|
|
|
#else /* C_DARWIN */
|
|
|
|
return cli_filecopy(src, dest);
|
2004-11-28 23:26:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
2005-04-29 01:21:12 +00:00
|
|
|
|
2008-01-22 17:47:15 +00:00
|
|
|
int daemonize(void)
|
2006-08-30 22:41:21 +00:00
|
|
|
{
|
2009-09-24 16:08:52 +02:00
|
|
|
#if defined(C_OS2) || defined(_WIN32)
|
2008-01-22 17:47:15 +00:00
|
|
|
fputs("Background mode is not supported on your operating system\n", stderr);
|
2008-02-11 10:27:51 +00:00
|
|
|
return -1;
|
2006-08-30 22:41:21 +00:00
|
|
|
#else
|
2008-01-22 17:47:15 +00:00
|
|
|
int fds[3], i;
|
|
|
|
pid_t pid;
|
2006-09-27 10:49:26 +00:00
|
|
|
|
2006-08-30 22:41:21 +00:00
|
|
|
|
2008-01-22 17:47:15 +00:00
|
|
|
fds[0] = open("/dev/null", O_RDONLY);
|
|
|
|
fds[1] = open("/dev/null", O_WRONLY);
|
|
|
|
fds[2] = open("/dev/null", O_WRONLY);
|
|
|
|
if(fds[0] == -1 || fds[1] == -1 || fds[2] == -1) {
|
|
|
|
fputs("Can't open /dev/null\n", stderr);
|
2006-08-30 22:41:21 +00:00
|
|
|
for(i = 0; i <= 2; i++)
|
2008-01-22 17:47:15 +00:00
|
|
|
if(fds[i] != -1)
|
|
|
|
close(fds[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-08-30 22:41:21 +00:00
|
|
|
|
2008-01-22 17:47:15 +00:00
|
|
|
for(i = 0; i <= 2; i++) {
|
|
|
|
if(dup2(fds[i], i) == -1) {
|
|
|
|
fprintf(stderr, "dup2(%d, %d) failed\n", fds[i], i); /* may not be printed */
|
|
|
|
for(i = 0; i <= 2; i++)
|
|
|
|
if(fds[i] != -1)
|
|
|
|
close(fds[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-08-30 22:41:21 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 17:47:15 +00:00
|
|
|
for(i = 0; i <= 2; i++)
|
|
|
|
if(fds[i] > 2)
|
|
|
|
close(fds[i]);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if(pid == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(pid)
|
2006-08-30 22:41:21 +00:00
|
|
|
exit(0);
|
|
|
|
|
|
|
|
setsid();
|
2008-01-22 17:47:15 +00:00
|
|
|
return 0;
|
2006-08-30 22:41:21 +00:00
|
|
|
#endif
|
|
|
|
}
|
2008-07-18 16:31:02 +00:00
|
|
|
|
|
|
|
#ifndef CL_NOLIBCLAMAV
|
|
|
|
int match_regex(const char *filename, const char *pattern)
|
|
|
|
{
|
|
|
|
regex_t reg;
|
2008-07-18 18:48:10 +00:00
|
|
|
int match, flags = REG_EXTENDED | REG_NOSUB;
|
2008-07-18 16:31:02 +00:00
|
|
|
char fname[513];
|
2008-08-25 21:59:33 +00:00
|
|
|
#if defined(C_OS2) || defined(C_WINDOWS)
|
2008-07-18 16:31:02 +00:00
|
|
|
size_t len;
|
|
|
|
|
2008-07-18 18:48:10 +00:00
|
|
|
flags |= REG_ICASE; /* case insensitive on Windows */
|
2008-07-18 16:31:02 +00:00
|
|
|
#endif
|
|
|
|
if(cli_regcomp(®, pattern, flags) != 0)
|
|
|
|
return 2;
|
|
|
|
|
2008-08-25 21:59:33 +00:00
|
|
|
#if !defined(C_OS2) && !defined(C_WINDOWS)
|
2008-07-18 16:31:02 +00:00
|
|
|
if(pattern[strlen(pattern) - 1] == '/') {
|
|
|
|
snprintf(fname, 511, "%s/", filename);
|
|
|
|
fname[512] = 0;
|
|
|
|
#else
|
|
|
|
if(pattern[strlen(pattern) - 1] == '\\') {
|
|
|
|
strncpy(fname, filename, 510);
|
|
|
|
fname[509]='\0';
|
|
|
|
len = strlen(fname);
|
|
|
|
if(fname[len - 1] != '\\') {
|
|
|
|
fname[len] = '\\';
|
|
|
|
fname[len + 1] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
strncpy(fname, filename, 513);
|
|
|
|
fname[512]='\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
match = (cli_regexec(®, fname, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
|
|
|
|
cli_regfree(®);
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
#endif
|
2009-02-12 21:14:51 +00:00
|
|
|
|
|
|
|
int cfg_tcpsock(const struct optstruct *opts, struct sockaddr_in *tcpsock, in_addr_t defaultbind)
|
|
|
|
{
|
|
|
|
struct hostent *he;
|
|
|
|
const struct optstruct *opt = optget(opts, "TCPSocket");
|
|
|
|
|
|
|
|
memset(tcpsock, 0, sizeof(*tcpsock));
|
|
|
|
tcpsock->sin_family = AF_INET;
|
|
|
|
tcpsock->sin_port = htons(opt->numarg);
|
|
|
|
|
|
|
|
if(!(opt = optget(opts, "TCPAddr"))->enabled) {
|
|
|
|
tcpsock->sin_addr.s_addr = htonl(defaultbind);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
he = gethostbyname(opt->strarg);
|
|
|
|
if(!he)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tcpsock->sin_addr = *(struct in_addr *) he->h_addr_list[0];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|