2004-08-23 20:38:10 +00:00
|
|
|
/*
|
2007-02-28 02:44:23 +00:00
|
|
|
* Copyright (C) 2004 - 2007 Tomasz Kojm <tkojm@clamav.net>
|
2004-08-23 20:38:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
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>
|
2006-09-27 10:49:26 +00:00
|
|
|
#ifndef C_WINDOWS
|
2006-06-08 20:00:50 +00:00
|
|
|
#include <dirent.h>
|
2006-09-27 10:49:26 +00:00
|
|
|
#endif
|
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
|
|
|
|
2006-09-27 10:49:26 +00:00
|
|
|
#include "shared/cfgparser.h"
|
|
|
|
#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() */
|
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
|
|
|
|
2007-02-21 21:07:23 +00:00
|
|
|
#ifdef CL_EXPERIMENTAL
|
|
|
|
#define VERSION_EXP VERSION"-exp"
|
|
|
|
#else
|
|
|
|
#define VERSION_EXP VERSION
|
|
|
|
#endif
|
|
|
|
|
2005-01-26 14:15:04 +00:00
|
|
|
char *freshdbdir(void)
|
2004-08-23 20:38:10 +00:00
|
|
|
{
|
|
|
|
struct cl_cvd *d1, *d2;
|
2007-03-06 16:31:49 +00:00
|
|
|
struct cfgstruct *copt;
|
|
|
|
const struct cfgstruct *cpt;
|
2006-10-29 19:13:11 +00:00
|
|
|
struct stat foo;
|
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
|
|
|
|
2004-08-23 20:38:10 +00:00
|
|
|
/* try to find fresh directory */
|
|
|
|
dbdir = cl_retdbdir();
|
2005-06-01 00:42:33 +00:00
|
|
|
if((copt = getcfg(CONFDIR"/clamd.conf", 0))) {
|
|
|
|
if((cpt = cfgopt(copt, "DatabaseDirectory"))->enabled || (cpt = cfgopt(copt, "DataDirectory"))->enabled) {
|
2004-12-01 20:11:17 +00:00
|
|
|
if(strcmp(dbdir, cpt->strarg)) {
|
2007-02-28 02:44:23 +00:00
|
|
|
char *daily = (char *) malloc(strlen(cpt->strarg) + strlen(dbdir) + 30);
|
2004-08-23 20:38:10 +00:00
|
|
|
sprintf(daily, "%s/daily.cvd", cpt->strarg);
|
2006-10-29 19:13:11 +00:00
|
|
|
if(stat(daily, &foo) == -1)
|
|
|
|
sprintf(daily, "%s/daily.inc/daily.info", cpt->strarg);
|
|
|
|
|
2004-08-23 20:38:10 +00:00
|
|
|
if((d1 = cl_cvdhead(daily))) {
|
2004-12-01 20:11:17 +00:00
|
|
|
sprintf(daily, "%s/daily.cvd", dbdir);
|
2006-10-29 19:13:11 +00:00
|
|
|
if(stat(daily, &foo) == -1)
|
|
|
|
sprintf(daily, "%s/daily.inc/daily.info", dbdir);
|
|
|
|
|
2004-08-23 20:38:10 +00:00
|
|
|
if((d2 = cl_cvdhead(daily))) {
|
|
|
|
free(daily);
|
|
|
|
if(d1->version > d2->version)
|
|
|
|
dbdir = cpt->strarg;
|
|
|
|
cl_cvdfree(d2);
|
|
|
|
} else {
|
|
|
|
free(daily);
|
|
|
|
dbdir = cpt->strarg;
|
|
|
|
}
|
|
|
|
cl_cvdfree(d1);
|
|
|
|
} else {
|
|
|
|
free(daily);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-26 14:15:04 +00:00
|
|
|
retdir = strdup(dbdir);
|
|
|
|
|
|
|
|
if(copt)
|
|
|
|
freecfg(copt);
|
|
|
|
|
|
|
|
return retdir;
|
2004-08-23 20:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_version(void)
|
|
|
|
{
|
2005-01-26 14:15:04 +00:00
|
|
|
char *dbdir;
|
2004-09-07 21:19:02 +00:00
|
|
|
char *path;
|
2004-08-23 20:38:10 +00:00
|
|
|
struct cl_cvd *daily;
|
2006-10-16 01:10:15 +00:00
|
|
|
struct stat foo;
|
2004-08-23 20:38:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
dbdir = freshdbdir();
|
2007-02-28 02:44:23 +00:00
|
|
|
if(!(path = malloc(strlen(dbdir) + 30))) {
|
2005-01-26 14:15:04 +00:00
|
|
|
free(dbdir);
|
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
|
|
|
|
|
|
|
sprintf(path, "%s/daily.cvd", dbdir);
|
2006-10-16 01:10:15 +00:00
|
|
|
if(stat(path, &foo) == -1)
|
|
|
|
sprintf(path, "%s/daily.inc/daily.info", dbdir);
|
2005-01-26 14:15:04 +00:00
|
|
|
free(dbdir);
|
2004-08-23 20:38:10 +00:00
|
|
|
|
|
|
|
if((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
|
|
|
|
|
|
|
printf("ClamAV "VERSION_EXP"/%d/%s", daily->version, ctime(&t));
|
2004-09-04 15:48:30 +00:00
|
|
|
cl_cvdfree(daily);
|
2004-08-23 20:38:10 +00:00
|
|
|
} else {
|
2007-02-21 21:07:23 +00:00
|
|
|
printf("ClamAV "VERSION_EXP"\n");
|
2004-08-23 20:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
}
|
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:
|
|
|
|
execl("/usr/bin/ditto", "ditto", "--rsrc", 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
|
|
|
|
|
|
|
#else
|
|
|
|
char buffer[FILEBUFF];
|
2006-07-27 12:19:34 +00:00
|
|
|
int s, d, bytes, ret;
|
|
|
|
struct stat sb;
|
|
|
|
|
2004-11-28 23:26:29 +00:00
|
|
|
|
2006-09-27 10:49:26 +00:00
|
|
|
if((s = open(src, O_RDONLY|O_BINARY)) == -1)
|
2004-11-28 23:26:29 +00:00
|
|
|
return -1;
|
|
|
|
|
2006-09-27 10:49:26 +00:00
|
|
|
if((d = open(dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY)) == -1) {
|
2004-11-28 23:26:29 +00:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((bytes = read(s, buffer, FILEBUFF)) > 0)
|
|
|
|
write(d, buffer, bytes);
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
/* njh@bandsman.co.uk: check result of close for NFS file */
|
2006-07-27 12:19:34 +00:00
|
|
|
ret = close(d);
|
|
|
|
|
|
|
|
stat(src, &sb);
|
|
|
|
chmod(dest, sb.st_mode);
|
|
|
|
|
|
|
|
return ret;
|
2004-11-28 23:26:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2005-04-29 01:21:12 +00:00
|
|
|
|
2006-07-27 12:19:34 +00:00
|
|
|
int dircopy(const char *src, const char *dest)
|
|
|
|
{
|
|
|
|
DIR *dd;
|
|
|
|
struct dirent *dent;
|
|
|
|
struct stat sb;
|
|
|
|
char spath[512], dpath[512];
|
|
|
|
|
|
|
|
|
|
|
|
if(stat(dest, &sb) == -1) {
|
2007-03-01 18:38:55 +00:00
|
|
|
if(mkdir(dest, 0755)) {
|
2006-07-27 13:54:08 +00:00
|
|
|
/* mprintf("!dircopy: Can't create temporary directory %s\n", dest); */
|
2006-07-27 12:19:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((dd = opendir(src)) == NULL) {
|
2006-07-27 13:54:08 +00:00
|
|
|
/* mprintf("!dircopy: Can't open directory %s\n", src); */
|
2006-07-27 12:19:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((dent = readdir(dd))) {
|
2006-09-27 10:49:26 +00:00
|
|
|
#if (!defined(C_CYGWIN)) && (!defined(C_INTERIX)) && (!defined(C_WINDOWS))
|
2006-07-27 12:19:34 +00:00
|
|
|
if(dent->d_ino)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(spath, sizeof(spath), "%s/%s", src, dent->d_name);
|
|
|
|
snprintf(dpath, sizeof(dpath), "%s/%s", dest, dent->d_name);
|
|
|
|
|
|
|
|
if(filecopy(spath, dpath) == -1) {
|
2006-07-27 13:54:08 +00:00
|
|
|
/* mprintf("!dircopy: Can't copy %s to %s\n", spath, dpath); */
|
2007-02-22 18:13:13 +00:00
|
|
|
cli_rmdirs(dest);
|
2006-07-27 12:19:34 +00:00
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-29 01:21:12 +00:00
|
|
|
int isnumb(const char *str)
|
|
|
|
{
|
|
|
|
while(*str) {
|
|
|
|
if(!isdigit(*str))
|
|
|
|
return 0;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2006-06-09 10:46:12 +00:00
|
|
|
|
|
|
|
int cvd_unpack(const char *cvd, const char *destdir)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
2006-09-27 10:49:26 +00:00
|
|
|
if((fd = open(cvd, O_RDONLY|O_BINARY)) == -1)
|
2006-06-09 10:46:12 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(lseek(fd, 512, SEEK_SET) == -1) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cli_untgz(fd, destdir) == -1) /* cli_untgz() will close fd */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-30 22:41:21 +00:00
|
|
|
|
|
|
|
void daemonize(void)
|
|
|
|
{
|
2006-09-27 10:49:26 +00:00
|
|
|
#if defined(C_OS2) || defined(C_WINDOWS)
|
|
|
|
fputs("Background mode is not supported on your operating system\n", stderr);
|
2006-08-30 22:41:21 +00:00
|
|
|
return;
|
|
|
|
#else
|
2006-09-27 10:49:26 +00:00
|
|
|
int i;
|
|
|
|
|
2006-08-30 22:41:21 +00:00
|
|
|
|
|
|
|
if((i = open("/dev/null", O_WRONLY)) == -1) {
|
|
|
|
for(i = 0; i <= 2; i++)
|
|
|
|
close(i);
|
|
|
|
|
|
|
|
} else {
|
2007-02-22 14:25:30 +00:00
|
|
|
dup2(i, 0);
|
2006-08-30 22:41:21 +00:00
|
|
|
dup2(i, 1);
|
|
|
|
dup2(i, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fork())
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
#endif
|
|
|
|
}
|