clamconf/clamconf.c
dc4f2ce8
 /*
bb34cb31
  *  Copyright (C) 2006 Sensory Networks, Inc.
  *	      (C) 2007 Tomasz Kojm <tkojm@clamav.net>
  *	      Written by Tomasz Kojm
dc4f2ce8
  *
  *  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.
dc4f2ce8
  *
  *  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
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
477cfb97
 #ifdef HAVE_UNISTD_H
dc4f2ce8
 #include <unistd.h>
477cfb97
 #endif
0481657e
 #include <time.h>
 
 #include "shared/misc.h"
 #include "libclamav/clamav.h"
dc4f2ce8
 
 #include "cfgparser.h"
 #define _GNU_SOURCE
 #include "getopt.h"
 
0bbe0584
 static void printopt(const struct cfgoption *opt, const struct cfgstruct *cpt, int nondef)
dc4f2ce8
 {
0bbe0584
 
     if(!cpt->enabled && opt->numarg == -1) {
 	if(!nondef || (opt->numarg != cpt->numarg))
 	    printf("%s not set\n", opt->name);
dc4f2ce8
 	return;
     }
 
     switch(opt->argtype) {
 	case OPT_STR:
 	case OPT_FULLSTR:
5174f778
 	case OPT_QUOTESTR:
0bbe0584
 	    if(!nondef || !opt->strarg || strcmp(opt->strarg, cpt->strarg))
 		printf("%s = \"%s\"\n", opt->name, cpt->strarg);
dc4f2ce8
 	    break;
 	case OPT_NUM:
 	case OPT_COMPSIZE:
0bbe0584
 	    if(!nondef || (opt->numarg != cpt->numarg))
 		printf("%s = %u\n", opt->name, cpt->numarg);
dc4f2ce8
 	    break;
 	case OPT_BOOL:
0bbe0584
 	    if(!nondef || (opt->numarg != cpt->numarg))
 		printf("%s = %s\n", opt->name, cpt->enabled ? "yes" : "no");
dc4f2ce8
 	    break;
 	default:
f78196f9
 	    printf("%s: UNKNOWN ARGUMENT TYPE\n", opt->name);
dc4f2ce8
     }
 }
 
0bbe0584
 static void printcfg(const char *cfgfile, int nondef)
dc4f2ce8
 {
 	const struct cfgoption *opt;
 	const struct cfgstruct *cpt;
 	struct cfgstruct *cfg;
 	int i;
 	unsigned short cfgowner = 0;
 
 
     if(!(cfg = getcfg(cfgfile, 1))) {
 	printf("Can't parse %s\n", cfgfile);
 	return;
     }
 
     /* pre loop to detect merged config */
     for(i = 0; ; i++) {
 	opt = &cfg_options[i];
 
 	if(!opt->name)
 	    break;
 
 	cpt = cfgopt(cfg, opt->name);
 
 	if((cpt->numarg != opt->numarg) || (cpt->strarg && opt->strarg && strcmp(cpt->strarg, opt->strarg))) {
 	    if((opt->owner & OPT_CLAMD) && !(opt->owner & OPT_FRESHCLAM))
 		cfgowner |= OPT_CLAMD;
 	    else if((opt->owner & OPT_FRESHCLAM) && !(opt->owner & OPT_CLAMD))
 		cfgowner |= OPT_FRESHCLAM;
 	}
     }
 
     if((cfgowner & OPT_CLAMD) && (cfgowner & OPT_FRESHCLAM)) { /* merged cfg */
 	printf("%s: clamd and freshclam directives\n", cfgfile);
0481657e
 	printf("------------------------------\n");
dc4f2ce8
 
 	printf("\n[common]\n");
 	for(i = 0; ; i++) {
 	    opt = &cfg_options[i];
 	    if(!opt->name)
 		break;
 	    if((opt->owner & OPT_CLAMD) && (opt->owner & OPT_FRESHCLAM)) {
 		cpt = cfgopt(cfg, opt->name);
0bbe0584
 		printopt(opt, cpt, nondef);
dc4f2ce8
 	    }
 	}
 
 	printf("\n[clamd]\n");
 	for(i = 0; ; i++) {
 	    opt = &cfg_options[i];
 	    if(!opt->name)
 		break;
 	    if((opt->owner & OPT_CLAMD) && !(opt->owner & OPT_FRESHCLAM)) {
 		cpt = cfgopt(cfg, opt->name);
0bbe0584
 		printopt(opt, cpt, nondef);
dc4f2ce8
 	    }
 	}
 
 	printf("\n[freshclam]\n");
 	for(i = 0; ; i++) {
 	    opt = &cfg_options[i];
 	    if(!opt->name)
 		break;
 	    if((opt->owner & OPT_FRESHCLAM) && !(opt->owner & OPT_CLAMD)) {
 		cpt = cfgopt(cfg, opt->name);
0bbe0584
 		printopt(opt, cpt, nondef);
dc4f2ce8
 	    }
 	}
 
     } else { /* separate cfg */
 
 	if(cfgowner & OPT_CLAMD) {
 	    printf("%s: clamd directives\n", cfgfile);
0481657e
 	    printf("------------------------------\n");
dc4f2ce8
 
 	    for(i = 0; ; i++) {
 		opt = &cfg_options[i];
 		if(!opt->name)
 		    break;
 		if(opt->owner & OPT_CLAMD) {
 		    cpt = cfgopt(cfg, opt->name);
0bbe0584
 		    printopt(opt, cpt, nondef);
dc4f2ce8
 		}
 	    }
 	} else {
 	    printf("%s: freshclam directives\n", cfgfile);
0481657e
 	    printf("------------------------------\n");
dc4f2ce8
 
 	    for(i = 0; ; i++) {
 		opt = &cfg_options[i];
 		if(!opt->name)
 		    break;
 		if(opt->owner & OPT_FRESHCLAM) {
 		    cpt = cfgopt(cfg, opt->name);
0bbe0584
 		    printopt(opt, cpt, nondef);
dc4f2ce8
 		}
 	    }
 	}
     }
     freecfg(cfg);
0481657e
 
 }
 
 static void printdb(const char *dir, const char *db)
 {
 	struct cl_cvd *cvd;
 	char path[256];
 	unsigned int inc = 0;
 	time_t t;
 
 
     snprintf(path, sizeof(path), "%s/%s.cvd", dir, db);
     if(access(path, R_OK) == -1) {
 	snprintf(path, sizeof(path), "%s/%s.inc/%s.info", dir, db, db);
 	inc = 1;
 	if(access(path, R_OK) == -1) {
 	    printf("%s db: Not found\n", db);
 	    return;
 	}
     }
 
     if((cvd = cl_cvdhead(path))) {
 	t = (time_t) cvd->stime;
 	printf("%s db: Format: %s, Version: %u, Build time: %s", db, inc ? ".inc" : ".cvd", cvd->version, ctime(&t));
 	cl_cvdfree(cvd);
     }
dc4f2ce8
 }
 
fc83da82
 static void help(void)
dc4f2ce8
 {
     printf("\n");
     printf("             Clam AntiVirus: Configuration Tool "VERSION"\n");
61409916
     printf("         (C) 2006 - 2007 ClamAV Team - http://www.clamav.net/team\n\n");
dc4f2ce8
 
     printf("    --help                 -h              show help\n");
     printf("    --config-dir DIR       -c DIR          search for config files in DIR\n");
0bbe0584
     printf("    --non-default          -n              only print non-default settings\n");
dc4f2ce8
     printf("\n");
 }
 
 int main(int argc, char **argv)
 {
 	char path[1024];
 	struct stat sb;
0bbe0584
 	int ret, opt_index, nondef = 0;
 	const char *getopt_parameters = "hc:n";
dc4f2ce8
 	static struct option long_options[] = {
 	    {"help", 0, 0, 'h'},
 	    {"config-dir", 1, 0, 'c'},
0bbe0584
 	    {"non-default", 0, 0, 'n'},
dc4f2ce8
 	    {0, 0, 0, 0}
     	};
0481657e
 	char *confdir = strdup(CONFDIR), *dbdir;
dc4f2ce8
 	int found = 0;
 
 
     while(1) {
 	opt_index = 0;
 	ret = getopt_long(argc, argv, getopt_parameters, long_options, &opt_index);
 
 	if (ret == -1)
 	    break;
 
 	switch (ret) {
 	    case 0:
 		break;
 
 	    case 'c':
 		free(confdir);
9c51a150
 		confdir = strdup(optarg);
dc4f2ce8
 		break;
 
 	    case 'h':
 		help();
 		free(confdir);
 		exit(0);
 
0bbe0584
 	    case 'n':
 		nondef = 1;
 		break;
 
dc4f2ce8
     	    default:
 		printf("ERROR: Unknown option passed\n");
 		free(confdir);
 		exit(1);
         }
     }
 
     snprintf(path, sizeof(path), "%s/clamd.conf", confdir);
     if(stat(path, &sb) != -1) {
0bbe0584
 	printcfg(path, nondef);
dc4f2ce8
 	found = 1;
 	printf("\n");
     }
 
     snprintf(path, sizeof(path), "%s/freshclam.conf", confdir);
     if(stat(path, &sb) != -1) {
0bbe0584
 	printcfg(path, nondef);
dc4f2ce8
 	found = 1;
     }
 
     if(!found) {
 	printf("No config files found in %s\n", confdir);
 	free(confdir);
 	return 1;
     }
9c51a150
     free(confdir);
0481657e
 
     printf("\nEngine and signature databases\n");
     printf("------------------------------\n");
 
 #ifdef CL_EXPERIMENTAL
     printf("Engine version: "VERSION" (with experimental code)\n");
 #else
     printf("Engine version: "VERSION"\n");
 #endif
 
     if(strcmp(VERSION, cl_retver()))
 	printf("WARNING: Version mismatch: clamconf: "VERSION", libclamav: %s\n", cl_retver());
 
     printf("Database directory: ");
     dbdir = freshdbdir();
     if(!dbdir) {
 	printf("Failed to retrieve\n");
 	return 1;
     } else printf("%s\n", dbdir);
 
     if(dbdir) {
 	printdb(dbdir, "main");
 	printdb(dbdir, "daily");
 	free(dbdir);
     }
dc4f2ce8
     return 0;
 }