clamscan/options.c
b151ef55
 /*
  *  Copyright (C) 2001-2002 Tomasz Kojm <zolw@konarski.edu.pl>
  *
  *  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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  * Sat Sep 14 22:18:20 CEST 2002: included getfirst*(), getnext*() functions
  *			from Alejandro Dubrovsky <s328940@student.uq.edu.au>
  */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <clamav.h>
 #define _GNU_SOURCE
 #include "getopt.h"
 
 
 #include "options.h"
 #include "shared.h"
36f2038b
 #include "memory.h"
 #include "output.h"
b151ef55
 
d927f46c
 extern int clamscan(struct optstruct *opt);
b151ef55
 
cd937ea4
 static char *clamdscan_long[] = { "help", "version", "verbose", "quiet",
b77a9c76
 				  "stdout", "log", "move", "remove",
 				  "config-file", "no-summary",
c8a2757d
 				  "disable-summary", NULL };
cd937ea4
 
 static char clamdscan_short[] = { 'h', 'V', 'v', 'l', 0 };
 
 int clamdscan_mode = 0;
 
b151ef55
 int main(int argc, char **argv)
 {
 	int ret, opt_index, i, len;
 	struct optstruct *opt;
 
 	const char *getopt_parameters = "hvd:wriVl:m";
 
 	static struct option long_options[] = {
521b19b4
 	    /* 
 	     * WARNING: For compatibility reasons options marked as "not used"
 	     *		must still be accepted !
 	     */
cd937ea4
 	    {"help", 0, 0, 'h'},	    /* clamscan + clamdscan */
 	    {"quiet", 0, 0, 0},		    /* clamscan + clamdscan */
 	    {"stdout", 0, 0, 0},	    /* clamscan + clamdscan */
 	    {"verbose", 0, 0, 'v'},	    /* clamscan + clamdscan */
0bcad2b1
 	    {"debug", 0, 0, 0},
cd937ea4
 	    {"version", 0, 0, 'V'},	    /* clamscan + clamdscan */
b151ef55
 	    {"tempdir", 1, 0, 0},
3506c157
 	    {"leave-temps", 0, 0, 0},
a0faaedf
 	    {"config-file", 1, 0, 0}, /* clamdscan */
b151ef55
 	    {"database", 1, 0, 'd'},
 	    {"whole-file", 0, 0, 'w'}, /* not used */
 	    {"force", 0, 0, 0},
 	    {"recursive", 0, 0, 'r'},
026ebd88
 	    {"bell", 0, 0, 0},
 	    {"disable-summary", 0, 0, 0}, /* obsolete */
 	    {"no-summary", 0, 0, 0},
b151ef55
 	    {"infected", 0, 0, 'i'},
 	    {"log", 1, 0, 'l'},
521b19b4
 	    {"log-verbose", 0, 0, 0}, /* not used */
b151ef55
 	    {"threads", 1, 0, 0}, /* not used */
 	    {"one-virus", 0, 0, 0}, /* not used */
 	    {"move", 1, 0, 0},
 	    {"remove", 0, 0, 0},
 	    {"exclude", 1, 0, 0},
e9a4812d
 	    {"exclude-dir", 1, 0, 0},
b151ef55
 	    {"include", 1, 0, 0},
e9a4812d
 	    {"include-dir", 1, 0, 0},
b151ef55
 	    {"max-files", 1, 0, 0},
 	    {"max-space", 1, 0, 0},
a19f21b6
             {"max-ratio", 1, 0, 0},
b151ef55
 	    {"max-recursion", 1, 0, 0},
3fa35dc1
 	    {"max-dir-recursion", 1, 0, 0},
b151ef55
 	    {"disable-archive", 0, 0, 0},
026ebd88
 	    {"no-archive", 0, 0, 0},
f8355d13
 	    {"detect-broken", 0, 0, 0},
8373a9b0
 	    {"block-encrypted", 0, 0, 0},
728f8802
 	    {"block-max", 0, 0, 0},
c2484690
 	    {"no-pe", 0, 0, 0},
c561d2a3
 	    {"no-ole2", 0, 0, 0},
2fe19b26
 	    {"no-html", 0, 0, 0},
94da957a
 	    {"mbox", 0, 0, 'm'}, /* not used */
 	    {"no-mail", 0, 0, 0},
 	    {"mail-follow-urls", 0, 0, 0},
b151ef55
 	    {"unzip", 2, 0, 0},
 	    {"unrar", 2, 0, 0},
30dc0fc7
 	    {"unace", 2, 0, 0}, /* not used */
b151ef55
 	    {"unarj", 2, 0, 0}, /* not used */
 	    {"arj", 2, 0, 0},
 	    {"zoo", 2, 0, 0}, /* not used */
 	    {"unzoo", 2, 0, 0},
 	    {"lha", 2, 0, 0},
 	    {"jar", 2, 0, 0},
 	    {"tar", 2, 0, 0},
 	    {"tgz", 2, 0, 0},
 	    {"deb", 2, 0, 0},
 	    {0, 0, 0, 0}
     	};
 
 
1f301ecc
     opt=(struct optstruct*) mcalloc(1, sizeof(struct optstruct));
b151ef55
     opt->optlist = NULL;
 
cd937ea4
     if(strstr(argv[0], "clamdscan"))
 	clamdscan_mode = 1;
 
b151ef55
     while(1) {
 
 	opt_index=0;
 	ret=getopt_long(argc, argv, getopt_parameters, long_options, &opt_index);
 
 	if (ret == -1)
 	    break;
 
 	switch (ret) {
 	    case 0:
 		register_long_option(opt, long_options[opt_index].name);
 		break;
 
     	    default:
cd937ea4
 		if(strchr(getopt_parameters, ret)) {
 		    if(opt_index)
 			register_char_option(opt, ret, long_options[opt_index].name);
 		    else
 			register_char_option(opt, ret, NULL);
 
 		} else {
b151ef55
 		    mprintf("!Unknown option passed.\n");
 		    free_opt(opt);
a0630a19
 		    if(clamdscan_mode)
 			exit(2);
 		    else
 			exit(40);
b151ef55
 		}
         }
     }
 
     if (optind < argc) {
 
         len=0;
 
 	/* count length of non-option arguments */
 
 	for(i=optind; i<argc; i++)
 	    len+=strlen(argv[i]);
 
 	len=len+argc-optind-1; /* add spaces between arguments */
 	opt->filename=(char*)mcalloc(len + 256, sizeof(char));
 
         for(i=optind; i<argc; i++) {
7cc3891c
 	    strncat(opt->filename, argv[i], strlen(argv[i]));
b151ef55
 	    if(i != argc-1)
7cc3891c
 		strncat(opt->filename, "\t", 1);
b151ef55
 	}
 
e8217f5a
     }
d927f46c
     ret = clamscan(opt);
b151ef55
 
e8217f5a
     free_opt(opt);
 
d927f46c
     return  ret;
b151ef55
 }
 
cd937ea4
 void register_char_option(struct optstruct *opt, char ch, const char *longname)
b151ef55
 {
 	struct optnode *newnode;
cd937ea4
 	int i, found = 0;
 
 
     if(clamdscan_mode) {
 	for(i = 0; clamdscan_short[i]; i++)
 	    if(clamdscan_short[i] == ch)
 		found = 1;
 
 	if(!found) {
 	    if(longname)
09b431f0
 		mprintf("WARNING: Ignoring option -%c (--%s): please edit clamd.conf instead.\n", ch, longname);
cd937ea4
 	    else
09b431f0
 		mprintf("WARNING: Ignoring option -%c: please edit clamd.conf instead.\n", ch);
cd937ea4
 
 	    return;
 	}
     }
b151ef55
 
     newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
     newnode->optchar = ch;
     if(optarg != NULL) {
 	newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
 	strcpy(newnode->optarg, optarg);
     } else newnode->optarg = NULL;
 
     newnode->optname = NULL;
     newnode->next = opt->optlist;
     opt->optlist = newnode;
 }
 
 void register_long_option(struct optstruct *opt, const char *optname)
 {
 	struct optnode *newnode;
26c6ace2
 	int i, found = 0;
cd937ea4
 
 
     if(clamdscan_mode) {
 	for(i = 0; clamdscan_long[i]; i++)
 	    if(!strcmp(clamdscan_long[i], optname))
 		found = 1;
 
 	if(!found) {
09b431f0
 	    mprintf("WARNING: Ignoring option --%s: please edit clamd.conf instead.\n", optname);
cd937ea4
 	    return;
 	}
     }
b151ef55
 
     newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
     newnode->optchar = 0;
     if(optarg != NULL) {
 	newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
 	strcpy(newnode->optarg, optarg);
     } else newnode->optarg = NULL;
 
     newnode->optname = (char *) mcalloc(strlen(optname) + 1, sizeof(char));
     strcpy(newnode->optname, optname);
     newnode->next = opt->optlist;
     opt->optlist = newnode;
 }
 
 int optc(const struct optstruct *opt, char ch)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) return 1;
 	} else break;
 	handler = handler->next;
     }
 
     return(0);
 }
 
 int optl(const struct optstruct *opt, const char *optname)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) return 1;
 	} else break;
 	handler = handler->next;
     }
 
     return(0);
 }
 
 char *getargc(const struct optstruct *opt, char ch)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) return handler->optarg;
 	} else break;
 	handler = handler->next;
     }
 
     return(NULL);
 }
 
 char *getfirstargc(const struct optstruct *opt, char ch, struct optnode **optnode)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) {
 	    	*optnode = handler;
 	    	return handler->optarg;
 	    }
 	} else break;
 	handler = handler->next;
     }
     *optnode = NULL;
     return(NULL);
 }
 
 char *getnextargc(struct optnode **optnode, char ch)
 {
 	struct optnode *handler;
 
     handler = (*optnode)->next;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) {
 	    	*optnode = handler;
 	    	return handler->optarg;
 	    }
 	} else break;
 	handler = handler->next;
     }
     *optnode = NULL;
     return(NULL);
 }
 
 char *getargl(const struct optstruct *opt, const char *optname)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) return handler->optarg;
 	} else break;
 	handler = handler->next;
     }
 
     return(NULL);
 }
 
 char *getfirstargl(const struct optstruct *opt, const char *optname, struct optnode **optnode)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) {
 			*optnode = handler;
 			return handler->optarg;
 		}
 	} else break;
 	handler = handler->next;
     }
     
     *optnode = NULL;
     return(NULL);
 }
 
 char *getnextargl(struct optnode **optnode, const char *optname)
 {
 	struct optnode *handler;
 
     handler = (*optnode)->next;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) {
 			*optnode = handler;
 			return handler->optarg;
 		}
 	} else break;
 	handler = handler->next;
     }
     
     *optnode = NULL;
     return(NULL);
 }
 
 void free_opt(struct optstruct *opt)
 {
 	struct optnode *handler, *prev;
 
e8217f5a
     if(!opt)
b151ef55
 	return;
 
     handler = opt->optlist;
 
     while(handler != NULL) {
 	handler->optchar = 0;
 	if(handler->optarg) free(handler->optarg);
 	if(handler->optname) free(handler->optname);
 	prev = handler;
 	handler = handler->next;
 	free(prev);
     }
 
e8217f5a
     if (opt->filename)
     	free(opt->filename);
b151ef55
     free(opt);
 }