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() */
fc83da82
 #include "shared/misc.h"
3e499d06
 
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
91e39258
 
da5926e2
 #ifdef CL_EXPERIMENTAL
 #define VERSION_EXP	VERSION"-exp"
 #else
 #define VERSION_EXP	VERSION
 #endif
 
98ce643b
 char *freshdbdir(void)
7b304dee
 {
 	struct cl_cvd *d1, *d2;
1095156a
 	struct cfgstruct *copt;
 	const struct cfgstruct *cpt;
d5021bce
 	struct stat foo;
7b304dee
 	const char *dbdir;
98ce643b
 	char *retdir;
7b304dee
 
d5021bce
 
7b304dee
     /* try to find fresh directory */
     dbdir = cl_retdbdir();
81837459
     if((copt = getcfg(CONFDIR"/clamd.conf", 0))) {
 	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);
d5021bce
 		if(stat(daily, &foo) == -1)
 		    sprintf(daily, "%s/daily.inc/daily.info", cpt->strarg);
 
7b304dee
 		if((d1 = cl_cvdhead(daily))) {
6ee0243f
 		    sprintf(daily, "%s/daily.cvd", dbdir);
d5021bce
 		    if(stat(daily, &foo) == -1)
 			sprintf(daily, "%s/daily.inc/daily.info", dbdir);
 
7b304dee
 		    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);
 		}
 	    }
 	}
     }
 
98ce643b
     retdir = strdup(dbdir);
 
     if(copt)
 	freecfg(copt);
 
     return retdir;
7b304dee
 }
 
 void print_version(void)
 {
98ce643b
 	char *dbdir;
2b259453
 	char *path;
7b304dee
 	struct cl_cvd *daily;
d12c1073
 	struct stat foo;
7b304dee
 
 
     dbdir = freshdbdir();
8ca8a18e
     if(!(path = malloc(strlen(dbdir) + 30))) {
98ce643b
 	free(dbdir);
7b304dee
 	return;
98ce643b
     }
7b304dee
 
     sprintf(path, "%s/daily.cvd", dbdir);
d12c1073
     if(stat(path, &foo) == -1)
 	sprintf(path, "%s/daily.inc/daily.info", dbdir);
98ce643b
     free(dbdir);
7b304dee
 
     if((daily = cl_cvdhead(path))) {
7c225c08
 	    time_t t = (time_t) daily->stime;
da5926e2
 
 	printf("ClamAV "VERSION_EXP"/%d/%s", daily->version, ctime(&t));
7c225c08
 	cl_cvdfree(daily);
7b304dee
     } else {
da5926e2
 	printf("ClamAV "VERSION_EXP"\n");
7b304dee
     }
 
     free(path);
 }
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:
 	    execl("/usr/bin/ditto", "ditto", "--rsrc", 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;
 
3e499d06
     if((d = open(dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY)) == -1) {
63abd169
 	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 */
88661e6a
     ret = close(d);
 
     stat(src, &sb);
     chmod(dest, sb.st_mode);
 
     return ret;
63abd169
 
 #endif
 
 }
5fabe557
 
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))) {
3e499d06
 #if   (!defined(C_CYGWIN)) && (!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;
 }
 
5fabe557
 int isnumb(const char *str)
 {
     while(*str) {
218ab79b
 	if(!isdigit(*str & 0xff))
5fabe557
 	    return 0;
 	str++;
     }
 
     return 1;
 }
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;
     }
 
     if(cli_untgz(fd, destdir) == -1) /* cli_untgz() will close fd */
 	return -1;
 
     return 0;
 }
a889f40e
 
 void daemonize(void)
 {
3e499d06
 #if	defined(C_OS2) || defined(C_WINDOWS)
 	fputs("Background mode is not supported on your operating system\n", stderr);
a889f40e
     return;
 #else
3e499d06
 	int i;
 
a889f40e
 
8c9d0e14
     if((i = open("/dev/null", O_RDWR)) == -1) {
a889f40e
 	for(i = 0; i <= 2; i++)
 	    close(i);
 
     } else {
7f26b244
 	dup2(i, 0);
a889f40e
 	dup2(i, 1);
 	dup2(i, 2);
8c9d0e14
 	if(i > 2)
 	    close(i);
a889f40e
     }
 
     if(fork())
 	exit(0);
 
     setsid();
 #endif
 }