shared/misc.c
7b304dee
 /*
8ca8a18e
  *  Copyright (C) 2004 - 2007 Tomasz Kojm <tkojm@clamav.net>
7b304dee
  *
  *  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.
7b304dee
  *
  *  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.
7b304dee
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
63abd169
 #include <stdlib.h>
3e499d06
 #ifdef	HAVE_UNISTD_H
63abd169
 #include <unistd.h>
3e499d06
 #endif
7b304dee
 #include <string.h>
7c225c08
 #include <time.h>
63abd169
 #include <sys/types.h>
 #include <sys/stat.h>
3e499d06
 #ifndef	C_WINDOWS
c6f15a5f
 #include <dirent.h>
3e499d06
 #endif
63abd169
 #include <fcntl.h>
7bbd5f7f
 #include <ctype.h>
c6f15a5f
 #include <errno.h>
7b304dee
 
3e499d06
 #include "shared/cfgparser.h"
 #include "shared/output.h"
2b259453
 
3e499d06
 #include "libclamav/clamav.h"
 #include "libclamav/cvd.h"
04bdd146
 #include "libclamav/others.h" /* for cli_rmdirs() */
f45d19ac
 #include "libclamav/regex/regex.h"
add738d2
 #include "libclamav/version.h"
fc83da82
 #include "shared/misc.h"
3e499d06
 
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
91e39258
 
add738d2
 #ifndef REPO_VERSION
 #define REPO_VERSION "exported"
 #endif
 
 #ifdef CL_EXPERIMENTAL
 #define EXP_VER "-exp"
 #else
 #define EXP_VER
 #endif
 
 const char *get_version(void)
 {
 	if(!strncmp("devel-",VERSION,6) && strcmp("exported",REPO_VERSION)) {
 		return REPO_VERSION""EXP_VER;
 	}
 	/* it is a release, or we have nothing better */
 	return VERSION""EXP_VER;
 }
2a363377
 /* CL_NOLIBCLAMAV means to omit functions that depends on libclamav */
 #ifndef CL_NOLIBCLAMAV
98ce643b
 char *freshdbdir(void)
7b304dee
 {
 	struct cl_cvd *d1, *d2;
1095156a
 	struct cfgstruct *copt;
 	const struct cfgstruct *cpt;
7b304dee
 	const char *dbdir;
98ce643b
 	char *retdir;
7b304dee
 
d5021bce
 
7b304dee
     /* try to find fresh directory */
     dbdir = cl_retdbdir();
e48e6201
     if((copt = getcfg(CONFDIR"/freshclam.conf", 0))) {
81837459
 	if((cpt = cfgopt(copt, "DatabaseDirectory"))->enabled || (cpt = cfgopt(copt, "DataDirectory"))->enabled) {
6ee0243f
 	    if(strcmp(dbdir, cpt->strarg)) {
8ca8a18e
 		    char *daily = (char *) malloc(strlen(cpt->strarg) + strlen(dbdir) + 30);
7b304dee
 		sprintf(daily, "%s/daily.cvd", cpt->strarg);
cb9d09c2
 		if(access(daily, R_OK))
 		    sprintf(daily, "%s/daily.cld", cpt->strarg);
d5021bce
 
e48e6201
 		if(!access(daily, R_OK) && (d1 = cl_cvdhead(daily))) {
6ee0243f
 		    sprintf(daily, "%s/daily.cvd", dbdir);
cb9d09c2
 		    if(access(daily, R_OK))
 			sprintf(daily, "%s/daily.cld", dbdir);
d5021bce
 
e48e6201
 		    if(!access(daily, R_OK) && (d2 = cl_cvdhead(daily))) {
7b304dee
 			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);
 		}
 	    }
 	}
     }
 
98ce643b
     retdir = strdup(dbdir);
 
     if(copt)
 	freecfg(copt);
 
     return retdir;
7b304dee
 }
 
add738d2
 
0aa3ba06
 void print_version(const char *dbdir)
7b304dee
 {
0aa3ba06
 	char *fdbdir, *path;
 	const char *pt;
7b304dee
 	struct cl_cvd *daily;
 
 
0aa3ba06
     if(dbdir)
 	pt = dbdir;
     else
 	pt = fdbdir = freshdbdir();
 
     if(!pt) {
04be06be
 	printf("ClamAV %s\n",get_version());
0aa3ba06
 	return;
     }
 
     if(!(path = malloc(strlen(pt) + 11))) {
 	if(!dbdir)
 	    free(fdbdir);
7b304dee
 	return;
98ce643b
     }
7b304dee
 
0aa3ba06
     sprintf(path, "%s/daily.cvd", pt);
cb9d09c2
     if(access(path, R_OK))
0aa3ba06
 	sprintf(path, "%s/daily.cld", pt);
 
     if(!dbdir)
 	free(fdbdir);
7b304dee
 
cb9d09c2
     if(!access(path, R_OK) && (daily = cl_cvdhead(path))) {
7c225c08
 	    time_t t = (time_t) daily->stime;
da5926e2
 
04be06be
 	printf("ClamAV %s/%d/%s", get_version(), daily->version, ctime(&t));
7c225c08
 	cl_cvdfree(daily);
7b304dee
     } else {
04be06be
 	printf("ClamAV %s\n",get_version());
7b304dee
     }
 
     free(path);
 }
2a363377
 #endif
63abd169
 int filecopy(const char *src, const char *dest)
 {
 
 #ifdef C_DARWIN
c8e6cded
 	pid_t pid;
63abd169
 
c8e6cded
     /* On Mac OS X use ditto and copy resource fork, too. */
     switch(pid = fork()) {
 	case -1:
 	    return -1;
 	case 0:
206865af
 	    execl("/usr/bin/ditto", "ditto", src, dest, NULL);
91e39258
 	    perror("execl(ditto)");
c8e6cded
 	    break;
 	default:
 	    wait(NULL);
 	    return 0;
63abd169
     }
 
c8e6cded
     return -1;
63abd169
 
 #else
 	char buffer[FILEBUFF];
88661e6a
 	int s, d, bytes, ret;
 	struct stat sb;
 
63abd169
 
3e499d06
     if((s = open(src, O_RDONLY|O_BINARY)) == -1)
63abd169
 	return -1;
 
dfc10927
     if((d = open(dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 0644)) == -1) {
63abd169
 	close(s);
 	return -1;
     }
 
     while((bytes = read(s, buffer, FILEBUFF)) > 0)
38fe8af4
 	if(write(d, buffer, bytes) < bytes) {
 	    close(s);
 	    close(d);
 	    return -1;
 	}
63abd169
 
     close(s);
     /* njh@bandsman.co.uk: check result of close for NFS file */
88661e6a
     ret = close(d);
 
     stat(src, &sb);
     chmod(dest, sb.st_mode);
 
     return ret;
63abd169
 
 #endif
 
 }
5fabe557
 
2a363377
 #ifndef CL_NOLIBCLAMAV
88661e6a
 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) {
f27ab437
 	if(mkdir(dest, 0755)) {
706694e6
 	    /* mprintf("!dircopy: Can't create temporary directory %s\n", dest); */
88661e6a
 	    return -1;
 	}
     }
 
     if((dd = opendir(src)) == NULL) {
706694e6
         /* mprintf("!dircopy: Can't open directory %s\n", src); */
88661e6a
         return -1;
     }
 
     while((dent = readdir(dd))) {
6670d61d
 #if (!defined(C_INTERIX)) && (!defined(C_WINDOWS))
88661e6a
 	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) {
706694e6
 		/* mprintf("!dircopy: Can't copy %s to %s\n", spath, dpath); */
66ba1785
 		cli_rmdirs(dest);
88661e6a
 		closedir(dd);
 		return -1;
 	    }
 	}
     }
 
     closedir(dd);
     return 0;
 }
2a363377
 #endif
88661e6a
 
2a363377
 #ifndef CL_NOLIBCLAMAV
91e39258
 int cvd_unpack(const char *cvd, const char *destdir)
 {
 	int fd;
 
 
3e499d06
     if((fd = open(cvd, O_RDONLY|O_BINARY)) == -1)
91e39258
 	return -1;
 
     if(lseek(fd, 512, SEEK_SET) == -1) {
 	close(fd);
 	return -1;
     }
 
68a364d4
     if(cli_untgz(fd, destdir) == -1) {
 	close(fd);
91e39258
 	return -1;
68a364d4
     }
     close(fd);
91e39258
 
     return 0;
 }
2a363377
 #endif
a889f40e
 
587d344b
 int daemonize(void)
a889f40e
 {
587d344b
 #if defined(C_OS2) || defined(C_WINDOWS)
     fputs("Background mode is not supported on your operating system\n", stderr);
c91956ea
     return -1;
a889f40e
 #else
587d344b
 	int fds[3], i;
 	pid_t pid;
3e499d06
 
a889f40e
 
587d344b
     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);
a889f40e
 	for(i = 0; i <= 2; i++)
587d344b
 	    if(fds[i] != -1)
 		close(fds[i]);
 	return -1;
     }
a889f40e
 
587d344b
     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;
 	}
a889f40e
     }
 
587d344b
     for(i = 0; i <= 2; i++)
 	if(fds[i] > 2)
 	    close(fds[i]);
 
     pid = fork();
 
     if(pid == -1)
 	return -1;
 
     if(pid)
a889f40e
 	exit(0);
 
     setsid();
587d344b
     return 0;
a889f40e
 #endif
 }
f45d19ac
 
 #ifndef CL_NOLIBCLAMAV
 int match_regex(const char *filename, const char *pattern)
 {
 	regex_t reg;
0129373e
 	int match, flags = REG_EXTENDED | REG_NOSUB;
f45d19ac
 	char fname[513];
6670d61d
 #if defined(C_OS2) || defined(C_WINDOWS)
f45d19ac
 	size_t len;
 
0129373e
 	flags |= REG_ICASE; /* case insensitive on Windows */
f45d19ac
 #endif
 	if(cli_regcomp(&reg, pattern, flags) != 0)
 	    return 2;
 
6670d61d
 #if !defined(C_OS2) && !defined(C_WINDOWS)
f45d19ac
 	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(&reg, fname, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
 	cli_regfree(&reg);
 	return match;
 }
 #endif