"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/src/configuration.cc" (5 Aug 2009, 14960 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2     SRG - Squid Report Generator
    3     Copyright 2005 University of Waikato
    4 
    5     This file is part of SRG.
    6 
    7     SRG is free software; you can redistribute it and/or modify
    8     it under the terms of the GNU General Public License as published by
    9     the Free Software Foundation; either version 2 of the License, or
   10     (at your option) any later version.
   11 
   12     SRG is distributed in the hope that it will be useful,
   13     but WITHOUT ANY WARRANTY; without even the implied warranty of
   14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15     GNU General Public License for more details.
   16 
   17     You should have received a copy of the GNU General Public License
   18     along with SRG; if not, write to the Free Software
   19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 */
   22 #include "srg.h"
   23 #include "libconfig.h"
   24 #include "prototypes.h"
   25 #include <getopt.h>
   26 
   27 /* Tell people how to use srg */
   28 void usage(void)
   29 {
   30   printf("usage: %s [options] <logfile> [logfile] ...\n"
   31   "Options:\n"
   32   "-C <config file>              Location of the srg config file.\n"
   33   "-d                            Enable debugging output\n"
   34   "-f <date>[time]               Start time to begin analysation at\n"
   35   "--help -h                     Display this help\n"
   36   "-H                            Generate HTML rather than PHP reports\n"
   37   "-m <days>                     Specify maximum number of days to keep a\n"
   38   "                              report\n"
   39   "-M                            Output a summary report suitable for \n"
   40   "                              emailing.\n"
   41   "-o <directory>                Output Directory\n"
   42   "-O <URL>                      URL where reports will be located\n"
   43   "-t <date>[time]               End time to stop analysation at\n"
   44   "-v                            Verbose mode. Print relevant messages.\n"
   45   "-V                            Print Version and exit.\n"
   46   "\n"
   47   "Date and time should be specified in the following format for -f and -t\n"
   48   " Date: YYYY-MM-DD\n"
   49   " Time: :HH:MM:SS - note leading :\n"
   50   " eg. 2005-01-08:15:46:30 (Jan 8 2005 15:46:30)\n"
   51   "Logfile(s) to process should be specified at the end of the options\n"
   52   , progname);
   53 
   54 }
   55 
   56 /* Configure srg */
   57 bool do_configuration(int argc, char **argv) {
   58 
   59     char ch;
   60     
   61     /* Initialise configuration options */
   62     set_defaults();
   63     
   64     /* The real config options */
   65     char *conffile = NULL;
   66 
   67     /* Temp config options for config *f*ile */
   68     char *accesslogf = NULL;
   69     char *outputdirf = NULL;
   70     char *outputurlf = NULL;
   71     char *titlef = NULL;
   72     char *filterbyf = NULL;
   73     char *filtercritf = NULL;
   74     char *groupbyf = NULL;
   75     char *groupbynetf = NULL;
   76     char *ip2userf = NULL;
   77     char *sitefilterf = NULL;
   78     char *resourcedirf = NULL;
   79     int debugf = 0;
   80     int sitereportf = 0;
   81     int maxagef = 0;
   82     int authf = 0;
   83     int verbosef = 0;
   84     int emailreportf = 0;
   85     int sortcolumnsf = 0;
   86     int disable_phpf = 0;
   87         
   88     /* temp config options for *c*ommand line */
   89     char *accesslogc = NULL;
   90     char *outputdirc = NULL;
   91     char *outputurlc = NULL;
   92     int maxagec = 0;
   93     int debugc = 0;
   94     int verbosec = 0;
   95     int emailreportc = 0;
   96     char *time_fromc = NULL;
   97     char *time_toc = NULL;
   98     int disable_phpc = 0;
   99     
  100     /* Config options from the config file */
  101     config_t main_config[] = {
  102         {"log_file", TYPE_STR|TYPE_NULL, &accesslogf},
  103         {"output_dir", TYPE_STR|TYPE_NULL, &outputdirf},
  104         {"output_url", TYPE_STR|TYPE_NULL, &outputurlf},
  105         {"title", TYPE_STR|TYPE_NULL, &titlef},
  106         {"emailreport", TYPE_BOOL|TYPE_NULL, &srg.emailreport}, 
  107         {"filterby", TYPE_STR|TYPE_NULL, &filterbyf},
  108         {"filtercrit", TYPE_STR|TYPE_NULL, &filtercritf},
  109         {"groupby", TYPE_STR|TYPE_NULL, &groupbyf},
  110         {"groupbynetmask", TYPE_STR|TYPE_NULL, &groupbynetf},
  111         {"disable_php", TYPE_BOOL|TYPE_NULL, &disable_phpf},
  112         {"debug", TYPE_BOOL|TYPE_NULL, &debugf},
  113         {"location", TYPE_BOOL|TYPE_NULL, &srg.locationStats},
  114         {"disable_sitereport", TYPE_BOOL|TYPE_NULL, &sitereportf},
  115         {"show_times", TYPE_BOOL|TYPE_NULL, &srg.showtimes},
  116         {"show_rates", TYPE_BOOL|TYPE_NULL, &srg.showrates},
  117         {"nonameexists_showip", TYPE_BOOL|TYPE_NULL, &srg.nonameshowip},
  118         {"iptouser_file", TYPE_STR|TYPE_NULL, &ip2userf},
  119         {"destinationsitefilter_file", TYPE_STR|TYPE_NULL, &sitefilterf},
  120         {"dontshow_onlydeniedusers", TYPE_BOOL|TYPE_NULL, &srg.hideDeniedOnly},
  121         {"php_authentication", TYPE_BOOL|TYPE_NULL, &authf},
  122         {"max_age", TYPE_INT|TYPE_NULL, &maxagef},
  123         {"lookup_hosts", TYPE_BOOL|TYPE_NULL, &srg.lookupHosts},
  124         {"verbose", TYPE_BOOL|TYPE_NULL, &verbosef},
  125         {"sort_columns", TYPE_BOOL|TYPE_NULL, &sortcolumnsf},
  126         {"resource_dir", TYPE_STR|TYPE_NULL, &resourcedirf},
  127         {"min_connects", TYPE_INT|TYPE_NULL, &srg.minimumConnects},
  128         {NULL, 0, NULL}
  129     };
  130     
  131     static struct option long_options[] = {
  132         {"help", 0, 0, 'h'},
  133         {0, 0, 0, 0}
  134     };
  135     int optindex=0;
  136 
  137     /* Parse command line arguments */
  138     while((ch = getopt_long(argc, argv, "C:df:hHm:Mo:O:t:vV", long_options, 
  139                     &optindex)) != -1) {
  140         switch(ch)
  141         {
  142         case 'C':
  143             conffile = strdup(optarg);
  144             break;
  145         case 'd':
  146             debugc++;
  147             break;
  148         case 'f':
  149             time_fromc = strdup(optarg);
  150             break;
  151         case 'h':
  152             usage();
  153             exit(0);
  154             break;
  155         case 'H':
  156             disable_phpc++;
  157             break;
  158         case 'm':
  159             maxagec = atoi(optarg);
  160             break;
  161         case 'M':
  162             emailreportc++;
  163             break;
  164         case 'o':
  165             outputdirc = strdup(optarg);
  166             break;
  167         case 'O':
  168             outputurlc = strdup(optarg);
  169             break;
  170         case 't':
  171             time_toc = strdup(optarg);
  172             break;
  173         case 'v':
  174             verbosec = 1;
  175             break;
  176         case 'V':
  177             printf("srg (Squid Report Generator) %s\n\t%s\n", 
  178                     version, cvsid);
  179             exit(1);
  180         case '?':
  181             usage();
  182             exit(1);
  183         break;
  184         }
  185     }
  186     
  187     /* Check for extra non options on the command line */
  188     while (optind < argc) {
  189         /* Assume it is a filename to process and add it to the list */
  190         if (accesslogc) {
  191             char *temp = strdup(accesslogc);
  192             free(accesslogc);
  193             asprintf(&accesslogc, "%s %s", temp, argv[optind]);
  194             free(temp);
  195         } else {
  196             accesslogc = strdup(argv[optind]);
  197         }
  198         optind++;
  199     }
  200     
  201     /* Try and read a configuration file */
  202     if (conffile == NULL) {
  203         conffile = strdup(CONFFILE);
  204     }
  205 
  206     /* Check if configuration file exists */
  207     if (access(conffile, R_OK)!=0) {
  208         fprintf(stderr, "WARNING: Configuration file not found!\n");
  209     } else {
  210         if (parse_config(main_config,conffile)) {
  211             fprintf(stderr, "Error in configuration file (%s)!\n", conffile);
  212             exit(1);
  213         }
  214     }
  215     free(conffile);
  216     
  217     /* Check if debugging information should be printed */
  218     if (debugc > 0 || debugf > 0) {
  219         fprintf(stderr, "Setting 'debug' and 'verbose' rules to true\n");
  220         srg.debug++;
  221         srg.verbose = 1;
  222     }
  223 
  224     if (verbosec > 0 || verbosef > 0) {
  225         if (srg.debug)
  226             fprintf(stderr, "Setting verbose to true\n");
  227         srg.verbose = 1;
  228     }
  229 
  230     if (resourcedirf != NULL) {
  231         if (srg.debug)
  232             fprintf(stderr, "Setting resource dir to %s from config file\n",
  233                     resourcedirf);
  234         srg.resourceDir = strdup(resourcedirf);
  235     } else {
  236         srg.resourceDir = strdup(RESOURCEDIR);
  237     }
  238     free(resourcedirf);
  239         
  240     if (titlef != NULL) {
  241         if (srg.debug)
  242             fprintf(stderr, "Setting title to %s from config file\n",titlef);
  243         srg.title = strdup(titlef);
  244     }
  245     free(titlef);
  246     
  247     if (accesslogc != NULL) {
  248         if (srg.debug)
  249             fprintf(stderr, "Setting input file(s) to %s from "
  250                     "command line\n",accesslogc);
  251         srg.accessLog = strdup(accesslogc);
  252     } else if (accesslogf != NULL) {
  253         if (srg.debug)
  254             fprintf(stderr, "Setting input file(s) to %s from "
  255                     "config file\n",accesslogf);
  256         srg.accessLog = strdup(accesslogf);
  257     }
  258     free(accesslogc);
  259     free(accesslogf);
  260     
  261     if (outputdirc != NULL) {
  262         if (srg.debug)
  263             fprintf(stderr, "Setting output directory to %s "
  264                     "from command line\n",outputdirc);
  265         srg.outputDir = strdup(outputdirc);
  266     } else if (outputdirf != NULL) {
  267         if (srg.debug)
  268             fprintf(stderr, "Setting output directory to %s "
  269                     "from config file\n",outputdirf);
  270         srg.outputDir = strdup(outputdirf);
  271     }
  272     free(outputdirc);
  273     free(outputdirf);
  274     
  275     if (outputurlc != NULL) {
  276         if (srg.debug)
  277             fprintf(stderr, "Setting output URL to %s "
  278                     "from command line\n",outputurlc);
  279         srg.outputURL = strdup(outputurlc);
  280     } else if (outputurlf != NULL) {
  281         if (srg.debug)
  282             fprintf(stderr, "Setting output URL to %s "
  283                     "from config file\n",outputurlf);
  284         srg.outputURL = strdup(outputurlf);
  285     }
  286     free(outputurlc);
  287     free(outputurlf);
  288 
  289     if (disable_phpc==1 || disable_phpf==1) {
  290         srg.outputMode = OUTPUT_HTML;
  291         srg.indexfname = strdup("index.html");
  292     }
  293     
  294     if (ip2userf != NULL) {
  295         if (srg.debug)
  296             fprintf(stderr, "Setting IP to user file to %s "
  297                     "from config file\n",ip2userf);
  298         srg.ip2user = strdup(ip2userf);
  299     }
  300     free(ip2userf);
  301 
  302     if (sitefilterf != NULL) {
  303         if (srg.debug)
  304             fprintf(stderr, "Setting destination site filter file to %s "
  305                     "from config file\n", sitefilterf);
  306         srg.sitefilter = strdup(sitefilterf);
  307     }
  308     free(sitefilterf);
  309     
  310     if (sitereportf > 0) {
  311         if (srg.debug)
  312             fprintf(stderr, "Setting disable site report to true from " 
  313                     "configu file\n");
  314         srg.siteStats = 0;
  315     }
  316     
  317     if (maxagec > 0) {
  318         if (srg.debug)
  319             fprintf(stderr, "Setting maximum report age to "
  320                     "%d from command line\n",maxagec);
  321         srg.maxreportage = maxagec;
  322     } else if (maxagef > 0) {
  323         if (srg.debug)
  324             fprintf(stderr, "Setting maximum report age to "
  325                     "%d from config file\n",maxagef);
  326         srg.maxreportage = maxagef;
  327     }
  328 
  329     if (filterbyf != NULL && filtercritf != NULL) {
  330         if (strcmp(filterbyf,"BY_IP")==0)
  331             srg.filter.by = BY_IP;
  332         else if (strcmp(filterbyf, "BY_SUBNET")==0)
  333             srg.filter.by = BY_SUBNET;
  334         else if (strcmp(filterbyf, "BY_USER")==0)
  335             srg.filter.by = BY_USER;
  336         else {
  337             fprintf(stderr, "Error in config file: '%s' is "
  338                     "an invalid option to filterby!\n", filterbyf);
  339             usage();
  340             exit(1);
  341         }
  342         if (srg.debug)
  343             fprintf(stderr, "Setting 'filter' to %s from "
  344                     "config file\n",filterbyf);
  345     }
  346     free(filterbyf);    
  347     
  348     if (filtercritf != NULL && filterbyf != NULL) {
  349         if (srg.debug)
  350             fprintf(stderr, "Setting filter criteria to %s "
  351                     "from config file\n",filtercritf);
  352         switch (srg.filter.by) {
  353         case BY_USER:
  354             srg.filter.user = strdup(filtercritf);
  355             break;
  356         case BY_IP:
  357             if (inet_aton(filtercritf, &srg.filter.address)==0) {
  358                 fprintf(stderr, "Invalid filter criteria for "
  359                         "address filter: %s\n", filtercritf);
  360                 exit(1);
  361             }
  362             break;
  363         case BY_SUBNET:
  364             char *networkAddress;
  365             char *netmask;
  366             char *tmp = filtercritf;
  367             netmask = break_string(tmp, '/');
  368             networkAddress = tmp;
  369             if (inet_aton(netmask, &srg.filter.netmask)==0) {
  370                 fprintf(stderr, "Invalid filter criteria for "
  371                         "subnet filter (netmask): %s\n", tmp);
  372                 exit(1);
  373             }
  374             if (inet_aton(networkAddress, 
  375                     &srg.filter.network)==0) {
  376                 fprintf(stderr, "Invalid filter criteria for "
  377                         "subnet filter (network): %s\n", tmp);
  378                 exit(1);
  379             }
  380             break;
  381         }   
  382     }
  383     free(filtercritf);
  384     filtercritf = NULL;
  385     
  386     if (groupbyf != NULL) {
  387         if (strcmp(groupbyf,"A")==0)
  388             srg.groupBy += BY_IP;
  389         else if (strcmp(groupbyf, "S")==0) {
  390             srg.groupBy += BY_SUBNET;
  391             if (inet_aton(groupbynetf, &srg.groupByNetmask)==0) {
  392                 fprintf(stderr, "Error in config file: %s is "
  393                         "not a valid grouping netmask!\n", 
  394                         groupbynetf);
  395                 exit(1);
  396             }
  397         }
  398         else if (strcmp(groupbyf, "U")==0)
  399             srg.groupBy += BY_USER;
  400         else {
  401             fprintf(stderr, "Error in config file: '%s' is "
  402                     "an invalid option to groupby!\n", groupbyf);
  403             exit(1);
  404         }
  405         if (srg.debug)
  406             fprintf(stderr, "Setting 'groupby' rule to %s "
  407                     "from config file\n",groupbyf);
  408     }
  409     free(groupbyf);
  410     free(groupbynetf);
  411     
  412     if (authf > 0) {
  413         if (srg.outputMode != OUTPUT_PHP) {
  414         fprintf(stderr, "Authentication cannot be used with HTML output!");
  415             usage();
  416             exit(1);
  417         }
  418         if (srg.debug)
  419             fprintf(stderr, "Setting 'PHP authentication' option to true\n");
  420         srg.authenticate++;
  421     }
  422     if (emailreportc || emailreportf) 
  423         srg.emailreport++;
  424     if (sortcolumnsf) {
  425         if (srg.debug)
  426             fprintf(stderr, "Setting 'column sorting' rule to "
  427                     "true\n");
  428         srg.sortcolumns++;
  429         srg.usejs++;
  430     }
  431 
  432     /* if the period is specified at all, set the srg variables so. */
  433     if (time_fromc) {
  434         srg.startTime = parse_timestring(time_fromc);
  435         if (srg.startTime == (time_t)-1) {
  436             fprintf(stderr, "%s: could not parse %s to a valid start time!\n",
  437                     argv[0], time_fromc);
  438             exit(1);
  439         }
  440         if (srg.debug) {
  441             fprintf(stderr, "Starting at %d (%s)\n", srg.startTime,
  442                     time_fromc);
  443         }
  444     }
  445     if (time_toc) {
  446         srg.endTime = parse_timestring(time_toc);
  447         if (srg.endTime == (time_t)-1) {
  448             fprintf(stderr, "%s: could not parse %s to a valid end time!\n",
  449                     argv[0], time_toc);
  450             exit(1);
  451         }
  452         if (srg.debug) {
  453             fprintf(stderr, "Finishing at %d (%s)\n", srg.endTime,
  454                     time_toc);
  455         }
  456     }
  457     
  458     if (srg.startTime != (time_t)-1 && srg.endTime != (time_t)-1) {
  459         /* Check that the end time is after the start time */
  460         if (srg.startTime == srg.endTime) {
  461             struct tm * starttm = localtime(&srg.startTime);
  462             if (starttm->tm_hour == 0 && starttm->tm_min == 0 && 
  463                     starttm->tm_sec == 0) {
  464                 if (srg.debug)
  465                     fprintf(stderr,"No times specified but same date. "
  466                             "Assuming start at 00:00 and end at 23:59\n");
  467                 srg.endTime += 86399;
  468             }
  469         }
  470         if ((srg.startTime >= srg.endTime) && (srg.endTime != (time_t)-1)) {
  471             fprintf(stderr, "%s: End Time must be after Start Time!\n", 
  472                     argv[0]);
  473             exit(1);
  474         }
  475     }
  476     free(time_fromc);
  477     free(time_toc);
  478     
  479     /* Only allowed to filter on one Criteria */
  480     if (srg.groupBy > BY_MAX) {
  481         fprintf(stderr, "%s: -a -s -u options are mutually " 
  482                 "exclusive!\n", argv[0]);
  483         usage();
  484         exit(1);
  485     }
  486 
  487     /* Check required parameters are set */
  488     if (srg.groupBy == BY_SUBNET) {
  489         if (srg.groupByNetmask.s_addr==0) {
  490             fprintf(stderr, "%s: please specify a netmask "
  491                     "with -n!\n", argv[0]);
  492             usage();
  493             exit(1);
  494         }
  495     }
  496     
  497     if (strlen(srg.accessLog)<=0) {
  498         fprintf(stderr, "%s: Please specify a logfile to analyse!\n", argv[0]);
  499         usage();
  500         exit(1);
  501     }
  502 
  503     return true;
  504 
  505 }
  506 
  507 /* Set program defaults */
  508 void set_defaults(void) {
  509 
  510     /* General Options */
  511     srg.debug=0;
  512     srg.verbose=0;
  513     srg.groupBy=0;
  514     srg.showtimes=0;
  515     srg.showrates=0;
  516     srg.nonameshowip=0;
  517     srg.startTime=(time_t)-1;
  518     srg.endTime=(time_t)-1;
  519     srg.minTime=(time_t)-1;
  520     srg.maxTime=(time_t)-1;
  521     srg.title="SRG - Squid Log Analysis";
  522     srg.ip2user=NULL;
  523     srg.locationStats=1;
  524     srg.siteStats=1;
  525     srg.hideDeniedOnly=0;
  526     srg.authenticate=0;
  527     srg.lookupHosts=0;
  528     srg.emailreport=0;
  529     srg.maxreportage=0;
  530     srg.minimumConnects=0;
  531     
  532     srg.accessLog="";
  533 
  534     /* Grouping & Filtering Options */
  535     srg.groupByNetmask.s_addr=0;
  536     srg.sortcolumns=0;
  537     srg.filter.by= BY_NONE;
  538     srg.filter.user = NULL;
  539     srg.filter.address.s_addr=0;
  540     srg.filter.network.s_addr=0;
  541     srg.filter.netmask.s_addr=0;
  542     
  543     /* Output Options */
  544     srg.outputDir="srg_reports";
  545     srg.outputURL="/";
  546     srg.outputMode = OUTPUT_PHP;
  547     srg.resourceDir="";
  548     srg.usejs=0;
  549     srg.indexfname="index.php";
  550     srg.phpheader="header.php";
  551     srg.phpfooter="footer.php";
  552     srg.cssfile=SRG_CSS_FILE;
  553     srg.jsfile=SRG_JS_FILE;
  554 
  555 }