shared/misc.c
7b304dee
 /*
c442ca9c
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
086eab5c
  *
  *  Authors: Tomasz Kojm
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>
4cd80898
 #ifndef _WIN32
e114b109
 #include <sys/socket.h>
4cd80898
 #endif
c6f15a5f
 #include <dirent.h>
63abd169
 #include <fcntl.h>
7bbd5f7f
 #include <ctype.h>
c6f15a5f
 #include <errno.h>
7b304dee
 
e247b45c
 #include "shared/optparser.h"
3e499d06
 #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
 
add738d2
 #ifndef REPO_VERSION
 #define REPO_VERSION "exported"
 #endif
 
 const char *get_version(void)
 {
 	if(!strncmp("devel-",VERSION,6) && strcmp("exported",REPO_VERSION)) {
b105842c
 		return REPO_VERSION""VERSION_SUFFIX;
add738d2
 	}
 	/* it is a release, or we have nothing better */
b105842c
 	return VERSION""VERSION_SUFFIX;
add738d2
 }
60ba0f86
 /* CL_NOLIBCLAMAV means to omit functions that depends on libclamav */
 #ifndef CL_NOLIBCLAMAV
 char *freshdbdir(void)
 {
 	struct cl_cvd *d1, *d2;
 	struct optstruct *opts;
 	const struct optstruct *opt;
 	const char *dbdir;
 	char *retdir;
 
 
     /* try to find the most up-to-date db directory */
     dbdir = cl_retdbdir();
     if((opts = optparse(CONFDIR_FRESHCLAM, 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);
637d2461
 		    if (daily == NULL) {
 			fprintf(stderr, "Unable to allocate memory for db directory...\n");
 			return NULL;
 		    }
60ba0f86
 		sprintf(daily, "%s"PATHSEP"daily.cvd", opt->strarg);
 		if(access(daily, R_OK))
 		    sprintf(daily, "%s"PATHSEP"daily.cld", opt->strarg);
 
 		if(!access(daily, R_OK) && (d1 = cl_cvdhead(daily))) {
 		    sprintf(daily, "%s"PATHSEP"daily.cvd", dbdir);
 		    if(access(daily, R_OK))
 			sprintf(daily, "%s"PATHSEP"daily.cld", dbdir);
 
 		    if(!access(daily, R_OK) && (d2 = cl_cvdhead(daily))) {
 			free(daily);
 			if(d1->version > d2->version)
 			    dbdir = opt->strarg;
 			cl_cvdfree(d2);
 		    } else {
 			free(daily);
 			dbdir = opt->strarg;
 		    }
 		    cl_cvdfree(d1);
 		} else {
 		    free(daily);
 		}
 	    }
 	}
     }
 
     retdir = strdup(dbdir);
 
     if(opts)
 	optfree(opts);
 
     return retdir;
 }
 
 void print_version(const char *dbdir)
 {
 	char *fdbdir = NULL, *path;
 	const char *pt;
 	struct cl_cvd *daily;
 	time_t db_time;
 	unsigned int db_version = 0;
 
 
     if(dbdir)
 	pt = dbdir;
     else
 	pt = fdbdir = freshdbdir();
 
     if(!pt) {
 	printf("ClamAV %s\n",get_version());
 	return;
     }
 
     if(!(path = malloc(strlen(pt) + 11))) {
 	if(!dbdir)
 	    free(fdbdir);
 	return;
     }
 
     sprintf(path, "%s"PATHSEP"daily.cvd", pt);
     if(!access(path, R_OK)) {
 	daily = cl_cvdhead(path);
 	if(daily) {
 	    db_version = daily->version;
 	    db_time = daily->stime;
 	    cl_cvdfree(daily);
 	}
     }
 
     sprintf(path, "%s"PATHSEP"daily.cld", pt);
     if(!access(path, R_OK)) {
 	daily = cl_cvdhead(path);
 	if(daily) {
 	    if(daily->version > db_version) {
 		db_version = daily->version;
 		db_time = daily->stime;
 	    }
 	    cl_cvdfree(daily);
 	}
     }
 
     if(!dbdir)
 	free(fdbdir);
 
     if(db_version) {
 	printf("ClamAV %s/%u/%s", get_version(), db_version, ctime(&db_time));
     } else {
 	printf("ClamAV %s\n",get_version());
     }
 
     free(path);
 }
 
 int check_flevel(void)
 {
     if(cl_retflevel() < CL_FLEVEL) {
 	fprintf(stderr, "ERROR: This tool requires libclamav with functionality level %u or higher (current f-level: %u)\n", CL_FLEVEL, cl_retflevel());
 	return 1;
     }
     return 0;
 }
 #endif
 
c2b6681b
 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;
 }
 
63abd169
 int filecopy(const char *src, const char *dest)
 {
081f6473
 #ifdef _WIN32
     return (!CopyFileA(src, dest, 0));
 #elif defined(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
 
afff80ef
 #else /* C_DARWIN */
     return cli_filecopy(src, dest);
63abd169
 #endif
 }
5fabe557
 
587d344b
 int daemonize(void)
a889f40e
 {
b2354dc1
 #ifdef _WIN32
587d344b
     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
 
 int match_regex(const char *filename, const char *pattern)
 {
 	regex_t reg;
0129373e
 	int match, flags = REG_EXTENDED | REG_NOSUB;
f45d19ac
 	char fname[513];
b2354dc1
 #ifdef _WIN32
0129373e
 	flags |= REG_ICASE; /* case insensitive on Windows */
f45d19ac
 #endif
 	if(cli_regcomp(&reg, pattern, flags) != 0)
 	    return 2;
 
58481352
 	if(pattern[strlen(pattern) - 1] == *PATHSEP) {
 	    snprintf(fname, 511, "%s"PATHSEP, filename);
f45d19ac
 	    fname[512] = 0;
 	} 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;
 }
7a997ac9
 
11195c0b
 int cli_is_abspath(const char *path) {
 #ifdef _WIN32
     int len = strlen(path);
72c63093
     return (len > 2 && path[0] == '\\' && path[1] == '\\') || (len >= 2 && ((*path >= 'a' && *path <= 'z') || (*path >= 'A' && *path <= 'Z')) && path[1] == ':');
11195c0b
 #else
     return *path == '/';
 #endif
 }
4dccd075
 
 unsigned int countlines(const char *filename)
 {
 	FILE *fh;
 	char buff[1024];
 	unsigned int lines = 0;
 
 
     if((fh = fopen(filename, "r")) == NULL)
 	return 0;
 
     while(fgets(buff, sizeof(buff), fh)) {
 	if(buff[0] == '#') continue;
 	lines++;
     }
 
     fclose(fh);
     return lines;
 }