main.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <log.h>
#include <strutil.h>
#include <tail.h>
#include <configfile.h>

Include dependency graph for main.c:

Go to the source code of this file.

Defines

#define BASEOPTS   5

Functions

void makeopts (void)
void showProgInfo (FILE *o)
void usage (FILE *o)
int main (int argc, char *argv[])

Variables

char * proginfo = "secwatch " VERSION " built " __TIME__" " __DATE__ " Copyright (C) 2006 Nic Stevens.\nPlease see COPYING for licensing information.\n"
char * dfltConfig = "/usr/local/etc/secwatch.conf"
int maxage = -1
int fork_flag = 1
int dmpCfg = 0
int dmpPat = 0
char * utxt []
static struct option _opts []
option * opts = NULL
int numopts = 0
char ** argvw


Define Documentation

#define BASEOPTS   5
 

Definition at line 63 of file main.c.

Referenced by makeopts(), and usage().


Function Documentation

int main int  argc,
char *  argv[]
 

Definition at line 149 of file main.c.

References _cfgitem::a, addtailsource(), ci_boolean, ci_flist, ci_integer, ci_logfac, ci_logpri, ci_pathname, ci_text, config, confVarPath(), confVarText(), dfltConfig, dmpCfg, dmpPat, dumpConfig(), dumpPats(), getBooleanValue(), _cfgitem::i, loadPatterns(), makeopts(), numopts, opts, readConfig(), _cfgitem::s, showProgInfo(), split(), sysLogFac(), sysLogPri(), trim(), usage(), and _cfgitem::val.

00149                                  {
00150      int c, i, x;
00151      int optind, error = 0;
00152      char _maxage[10];
00153      char ebuf[10240];
00154      char *s, * configFile = dfltConfig;
00155      char ** logs;
00156      argvw = argv;
00157      makeopts();
00158      while(1) {
00159           c = getopt_long(argc, argv, "nf:dphv",opts,&optind);
00160           if(c == -1)
00161                break;
00162           switch(c) {
00163           case 'd':
00164           case 0: // no-fork, no option, do nothing
00165           case 'p':
00166           case '1':
00167           case '2':
00168                break;
00169           case 'f':
00170                configFile = optarg;
00171                break;
00172           case 'h':
00173                usage(stdout);
00174                return 0;
00175                break;
00176           case 'v':
00177                showProgInfo(stdout);
00178                return 0;
00179                break;
00180           default:
00181                if(c >= 6 && c < numopts) {
00182                     i = c -6;
00183                     switch(config[i].type) {
00184                     case ci_pathname:
00185                          config[i].val.s = strdup(optarg);
00186                          break;
00187                     case ci_text:
00188                          config[i].val.s = strdup(optarg);
00189                          break;
00190                     case ci_logfac:
00191                          config[i].val.i = sysLogFac(optarg);
00192                          break;
00193                     case ci_logpri:
00194                          config[i].val.i = sysLogPri(optarg);
00195                          break;
00196                     case ci_boolean:
00197                          getBooleanValue(optarg,&config[i].val.i);
00198                          break;
00199                     case ci_integer:
00200                          config[i].val.i = atoi(optarg);
00201                          break;
00202                     case ci_flist:
00203                          config[i].val.a = split(",",optarg);
00204                          break;
00205                     }
00206                } else {
00207                     fprintf(stderr,"%s: invalid argument(%d)\n",*argv,c);
00208                     error = 1;
00209                     break;
00210                }
00211           }
00212      }
00213      if(error) {
00214           usage(stderr);
00215           return -1;
00216      }
00217      if(configFile != NULL) {
00218           if(readConfig(configFile,ebuf,sizeof(ebuf))) {
00219                fprintf(stderr,"%s: Configuration error %s\n",*argv,ebuf);
00220                return -1;
00221           }
00222      }
00223      if(dmpCfg || dmpPat)
00224           showProgInfo(stdout);
00225      
00226      if(dmpCfg) 
00227           dumpConfig();
00228           
00229      if(loadPatterns(confVarPath("patterns",&x),&ebuf,sizeof(ebuf))) {
00230           fprintf(stderr,"%s: Pattern error: %s\n",*argv,ebuf);
00231           return -1;
00232      }
00233      if(dmpPat)
00234           dumpPats();
00235 
00236      if((logs = split(",",confVarText("logfiles",&x))) == NULL) {
00237           fprintf(stderr,"%s: No logs defined.\n",*argv);
00238           return -1;
00239      }
00240      for(i = 0; logs[i] != 0; i++) {
00241           logs[i] = trim(logs[i]);
00242           if(addtailsource(logs[i])) {
00243                fprintf(stderr,"%s: Error adding log source \"%s\": %m\n",*argv,logs[i]);
00244                return -1;
00245           }
00246      }
00247      if(dmpPat | dmpCfg)
00248           return 0;
00249      if(fork_flag) {
00250           if((i = fork()) == -1) {
00251                fprintf(stderr,"%s: Cannot fork: %m\n",*argv);
00252                return -1;
00253           }
00254           if(i == 0) 
00255                return 0;
00256      }
00257      setsid();
00258      secwatch();
00259 }

Here is the call graph for this function:

void makeopts void   ) 
 

Definition at line 67 of file main.c.

References BASEOPTS, numopts, and opts.

Referenced by main().

00067                     {
00068      int i, ip;
00069      char desc[100];
00070      struct option op;
00071      for(i = 0; i < BASEOPTS; i++) {
00072           numopts++;
00073           if((opts = realloc(opts,sizeof(struct option)*(numopts+1))) == NULL) {
00074                fprintf(stderr,"Fatal: Could not create option space\n");
00075                exit(-1);
00076           }
00077           opts[i] = _opts[i];
00078           opts[i+1].name = NULL;
00079      }
00080      for(i = 0; config[i].name != NULL; i++) {
00081           ip = numopts++;
00082           if((opts = realloc(opts,(sizeof(struct option )*(numopts+1)))) == NULL) {
00083                fprintf(stderr,"Fatal: Could not create option space\n");
00084                exit(-1);
00085           }
00086           sprintf(desc,"set-%s",config[i].name);
00087           op.has_arg = 1;
00088           op.name = strdup(desc);
00089           op.flag = NULL;
00090           op.val = BASEOPTS+i;
00091           opts[ip] = op;
00092           opts[numopts].name = NULL;
00093      }
00094 }

void showProgInfo FILE *  o  ) 
 

Definition at line 96 of file main.c.

References proginfo.

Referenced by main(), and usage().

00096                            {
00097      fprintf(o,"%s\n",proginfo);
00098 }

void usage FILE *  o  ) 
 

Definition at line 99 of file main.c.

References _cfgitem::a, BASEOPTS, ci_boolean, ci_flist, ci_integer, ci_logfac, ci_logpri, ci_pathname, ci_text, config, confVarInt(), confVarPath(), confVarText(), _cfgitem::i, _cfgitem::name, opts, showProgInfo(), sysLogFacId(), sysLogPriId(), _cfgitem::type, utxt, and _cfgitem::val.

Referenced by main().

00099                     {
00100      int i, j;
00101      showProgInfo(o);
00102      for(i = 0; opts[i].val != 0; i++) {
00103           if(i < BASEOPTS)
00104                fprintf(o,"-%c, --%-12.12s  %s\n",
00105                        opts[i].val,opts[i].name,utxt[i]);
00106           else {
00107                cfgitem_t *c = &config[i-BASEOPTS];
00108                char **a, *p, buf[128];
00109                int x;
00110                p = &buf[0];
00111                switch(c->type) {
00112                case ci_pathname:
00113                     p = confVarPath(c->name,&x);
00114                     break;
00115                case ci_text:
00116                     p = confVarText(c->name,&x);
00117                     break;
00118                case ci_integer:
00119                     sprintf(buf,"%d",confVarInt(c->name,&x));
00120                     break;
00121                case ci_boolean:
00122                     if(c->val.i)
00123                          strcpy(p,"true");
00124                     else
00125                          strcpy(p,"false");
00126                     break;
00127                case ci_logpri:
00128                case ci_logfac:
00129                     p = (c->type == ci_logpri) ? sysLogPriId(c->val.i) : sysLogFacId(c->val.i);
00130                     break;
00131                case ci_flist:
00132                     bzero(buf,sizeof(buf));
00133                     for(j = 0; c->val.a[j] != NULL; j++) {
00134                          char *foo = ",";
00135                          strcat(buf,c->val.a[j]);
00136                          if(c->val.a[j+1] != NULL)
00137                               strcat(buf,foo);
00138                     }
00139                     break;
00140                }
00141                fprintf(o,"    --%-12.12s  %s [Default: %s]\n",
00142                        opts[i].name,
00143                        utxt[i],
00144                        p);
00145           }
00146      }
00147 }

Here is the call graph for this function:


Variable Documentation

struct option _opts[] [static]
 

Initial value:

 {
     {"dump-cfg",no_argument,&dmpCfg,'d'},
     {"dump-pat",no_argument,&dmpPat,'p'},
     {"config", required_argument, 0,   'f'},
     {"help",   no_argument,       0,   'h'},
     {"version",no_argument,       0,   'v'},
     {0,0,0,0}
}

Definition at line 55 of file main.c.

char** argvw
 

Definition at line 148 of file main.c.

char* dfltConfig = "/usr/local/etc/secwatch.conf"
 

Definition at line 20 of file main.c.

Referenced by main().

int dmpCfg = 0
 

Definition at line 23 of file main.c.

Referenced by main().

int dmpPat = 0
 

Definition at line 24 of file main.c.

Referenced by main().

int fork_flag = 1
 

Definition at line 22 of file main.c.

int maxage = -1
 

Definition at line 21 of file main.c.

int numopts = 0
 

Definition at line 65 of file main.c.

Referenced by main(), and makeopts().

struct option* opts = NULL
 

Definition at line 64 of file main.c.

Referenced by main(), makeopts(), and usage().

char* proginfo = "secwatch " VERSION " built " __TIME__" " __DATE__ " Copyright (C) 2006 Nic Stevens.\nPlease see COPYING for licensing information.\n"
 

Definition at line 19 of file main.c.

Referenced by showProgInfo().

char* utxt[]
 

Initial value:

 {
          "Dump configuration",
          "Dump patterns",
          "Configuration filename",
          "Show this list",
          "Show program version and exit.",
          "set the name of the patterns file",
          "set the name of the file holding the PID of the daemon",
          "set the name of the IP list",
          "set a comma delimited list of log files to scan",
          "set the maximum number of attemtps before firewalling",
          "set the maximum age, in minutes, of an IP ban",
          "set to true or false to have the daemon fork or stay in foreground",
          "set the address to send alerts to",
          "set the address to send alerts from",
          "set the location of the sendmail binary",
          "set the syslog priority to use",
          "set the syslog facility to use",
          "set the firewall reject selections",
          "set the inprej firewall rule",
          "set the outrej firewall rule",
          "set the fwdrej firewall rule",
          "set the firewall accept selections",
          "set the inpalw firewall rule",
          "set the outalw firewall rule",
          "set the fwdalw firewall rule",
          NULL
}

Definition at line 26 of file main.c.

Referenced by usage().


Generated on Tue Oct 31 10:17:35 2006 for secwatch by  doxygen 1.4.6