Blame src/aide.c

Packit 762fc5
/* aide, Advanced Intrusion Detection Environment
Packit 762fc5
 *
Packit 762fc5
 * Copyright (C) 1999-2006,2010-2013,2015,2016 Rami Lehti, Pablo Virolainen,
Packit 762fc5
 * Mike Markley, Richard van den Berg, Hannes von Haugwitz
Packit 762fc5
 * $Header$
Packit 762fc5
 *
Packit 762fc5
 * This program is free software; you can redistribute it and/or
Packit 762fc5
 * modify it under the terms of the GNU General Public License as
Packit 762fc5
 * published by the Free Software Foundation; either version 2 of the
Packit 762fc5
 * License, or (at your option) any later version.
Packit 762fc5
 *
Packit 762fc5
 * This program is distributed in the hope that it will be useful, but
Packit 762fc5
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 762fc5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit 762fc5
 * General Public License for more details.
Packit 762fc5
 *
Packit 762fc5
 * You should have received a copy of the GNU General Public License
Packit 762fc5
 * along with this program; if not, write to the Free Software
Packit 762fc5
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 762fc5
 */
Packit 762fc5
Packit 762fc5
#include "aide.h"
Packit 762fc5
Packit 762fc5
#include <sys/types.h>
Packit 762fc5
#include <sys/stat.h>
Packit 762fc5
#include <stdio.h>
Packit 762fc5
#include <stdlib.h>
Packit 762fc5
#include <errno.h>
Packit 762fc5
#include <signal.h>
Packit 762fc5
#include <sys/types.h>
Packit 762fc5
#include <dirent.h>
Packit 762fc5
#include <time.h>
Packit 762fc5
Packit 762fc5
#if HAVE_UNISTD_H
Packit 762fc5
#include <unistd.h>
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#include "md.h"
Packit 762fc5
#include "commandconf.h"
Packit 762fc5
#include "compare_db.h"
Packit 762fc5
#include "db_config.h"
Packit 762fc5
#include "db_file.h"
Packit 762fc5
#include "do_md.h"
Packit 762fc5
#include "report.h"
Packit 762fc5
#include "gen_list.h"
Packit 762fc5
#include "getopt.h"
Packit 762fc5
#include "list.h"
Packit 762fc5
#include "util.h"
Packit 762fc5
#include "base64.h"
Packit 762fc5
/*for locale support*/
Packit 762fc5
#include "locale-aide.h"
Packit 762fc5
/*for locale support*/
Packit 762fc5
db_config* conf;
Packit 762fc5
Packit 762fc5
#ifndef MAXHOSTNAMELEN
Packit 762fc5
#define MAXHOSTNAMELEN 256
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
static void usage(int exitvalue)
Packit 762fc5
{
Packit 762fc5
  fprintf(stderr, 
Packit 762fc5
	  _("Aide " AIDEVERSION" \n\n"
Packit 762fc5
	    "Usage: aide [options] command\n\n"
Packit 762fc5
	    "Commands:\n"
Packit 762fc5
	    "  -i, --init\t\tInitialize the database\n"
Packit 762fc5
	    "  -C, --check\t\tCheck the database\n"
Packit 762fc5
	    "  -u, --update\t\tCheck and update the database non-interactively\n"
Packit 762fc5
	    "  -E, --compare\t\tCompare two databases\n\n"
Packit 762fc5
	    "Miscellaneous:\n"
Packit 762fc5
	    "  -D, --config-check\tTest the configuration file\n"
Packit 762fc5
	    "  -v, --version\t\tShow version of AIDE and compilation options\n"
Packit 762fc5
	    "  -h, --help\t\tShow this help message\n\n"
Packit 762fc5
	    "Options:\n"
Packit 762fc5
	    "  -c [cfgfile]\t--config=[cfgfile]\tGet config options from [cfgfile]\n"
Packit 762fc5
	    "  -l [REGEX]\t--limit=[REGEX]\t\tLimit command to entries matching [REGEX]\n"
Packit 762fc5
	    "  -B \"OPTION\"\t--before=\"OPTION\"\tBefore configuration file is read define OPTION\n"
Packit 762fc5
	    "  -A \"OPTION\"\t--after=\"OPTION\"\tAfter configuration file is read define OPTION\n"
Packit 762fc5
	    "  -r [reporter]\t--report=[reporter]\tWrite report output to [reporter] url\n"
Packit 762fc5
	    "  -V[level]\t--verbose=[level]\tSet debug message level to [level]\n"
Packit 762fc5
	    "\n")
Packit 762fc5
	  );
Packit 762fc5
  
Packit 762fc5
  exit(exitvalue);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static void print_version(void)
Packit 762fc5
{
Packit 762fc5
  fprintf(stderr,
Packit 762fc5
	  "Aide " AIDEVERSION "\n\n"
Packit 762fc5
	  "Compiled with the following options:\n\n" AIDECOMPILEOPTIONS "\n");
Packit 762fc5
  exit(0);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static int read_param(int argc,char**argv)
Packit 762fc5
{
Packit 762fc5
  int option = -1;
Packit 762fc5
  char* err=NULL;
Packit 762fc5
  int i=0;
Packit 762fc5
  
Packit 762fc5
Packit 762fc5
  static struct option options[] =
Packit 762fc5
  {
Packit 762fc5
    { "help", no_argument, NULL, 'h' },
Packit 762fc5
    { "verbose", optional_argument, NULL, 'V'},
Packit 762fc5
    { "version", no_argument, NULL, 'v'},
Packit 762fc5
    { "config", required_argument, NULL, 'c'},
Packit 762fc5
    { "before", required_argument, NULL, 'B'},
Packit 762fc5
    { "after", required_argument, NULL, 'A'},
Packit 762fc5
    { "report", required_argument, NULL, 'r'},
Packit 762fc5
    { "init", no_argument, NULL, 'i'},
Packit 762fc5
    { "check", no_argument, NULL, 'C'},
Packit 762fc5
    { "update", no_argument, NULL, 'u'},
Packit 762fc5
    { "config-check", no_argument, NULL, 'D'},
Packit 762fc5
    { "limit", required_argument, NULL, 'l'},
Packit 762fc5
    { "compare", no_argument, NULL, 'E'},
Packit 762fc5
    { NULL,0,NULL,0 }
Packit 762fc5
  };
Packit 762fc5
Packit 762fc5
  while(1){
Packit 762fc5
    option = getopt_long(argc, argv, "hV::vc:B:A:r:iCuDE", options, &i);
Packit 762fc5
    if(option==-1)
Packit 762fc5
      break;
Packit 762fc5
    switch(option)
Packit 762fc5
      {
Packit 762fc5
      case 'h':{
Packit 762fc5
	usage(0);
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'v':{
Packit 762fc5
	print_version();
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'V':{
Packit 762fc5
	if(optarg!=NULL){
Packit 762fc5
	  conf->verbose_level=strtol(optarg,&err,10);
Packit 762fc5
	  if(*err!='\0' || conf->verbose_level>255 || conf->verbose_level<0 || 
Packit 762fc5
	     errno==ERANGE){
Packit 762fc5
	    error(0, _("Illegal verbosity level:%s\n"),optarg);
Packit 762fc5
	    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	  }
Packit 762fc5
	  error(230,_("Setting verbosity to %s\n"),optarg);
Packit 762fc5
	}else{
Packit 762fc5
	  conf->verbose_level=20;
Packit 762fc5
	}
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'c':{
Packit 762fc5
	if(optarg!=NULL){
Packit 762fc5
	  conf->config_file=optarg;
Packit 762fc5
	}else{
Packit 762fc5
	  error(0,_("No config-file name given!\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	}
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'B': {
Packit 762fc5
	if (optarg!=NULL) {
Packit 762fc5
	  int errorno=commandconf('B',optarg);
Packit 762fc5
	  if (errorno!=0){
Packit 762fc5
	    error(0,_("Configuration error in before statement:%s\n"),optarg);
Packit 762fc5
	    exit(INVALID_CONFIGURELINE_ERROR);
Packit 762fc5
	  }
Packit 762fc5
	} else {
Packit 762fc5
	  error(0,_("-B must have a parameter\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	}
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'A': {
Packit 762fc5
	if (optarg!=NULL) {
Packit 762fc5
	  int errorno=commandconf('A',optarg);
Packit 762fc5
	  if (errorno!=0){
Packit 762fc5
	    error(0,_("Configuration error in after statement:%s\n"),optarg);
Packit 762fc5
	    exit(INVALID_CONFIGURELINE_ERROR);
Packit 762fc5
	  }
Packit 762fc5
	} else {
Packit 762fc5
	  error(0,_("-A must have a parameter\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	}
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'l': {
Packit 762fc5
            if (optarg!=NULL) {
Packit 762fc5
                const char* pcre_error;
Packit 762fc5
                int pcre_erroffset;
Packit 762fc5
                conf->limit=malloc(strlen(optarg)+1);
Packit 762fc5
                strcpy(conf->limit,optarg);
Packit 762fc5
                if((conf->limit_crx=pcre_compile(conf->limit, PCRE_ANCHORED, &pcre_error, &pcre_erroffset, NULL)) == NULL) {
Packit 762fc5
                    error(0,_("Error in limit regexp '%s' at %i: %s\n"), conf->limit, pcre_erroffset, pcre_error);
Packit 762fc5
                    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
                }
Packit 762fc5
                error(200,_("Limit set to '%s'\n"), conf->limit);
Packit 762fc5
            } else {
Packit 762fc5
                error(0,_("-l must have an argument\n"));
Packit 762fc5
                exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
            }
Packit 762fc5
            break;
Packit 762fc5
      }
Packit 762fc5
      case 'r': {
Packit 762fc5
	if(optarg!=NULL) {
Packit 762fc5
	  do_repurldef(optarg);
Packit 762fc5
	}else {
Packit 762fc5
	  error(0,_("-r must have an argument\n"));
Packit 762fc5
	}
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'i': {
Packit 762fc5
	if(conf->action==0){
Packit 762fc5
	  conf->action=DO_INIT;
Packit 762fc5
	}else {
Packit 762fc5
	  error(0,
Packit 762fc5
		_("Cannot have multiple commands on a single commandline.\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	};
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'C': {
Packit 762fc5
	if(conf->action==0){
Packit 762fc5
	  conf->action=DO_COMPARE;
Packit 762fc5
	}else {
Packit 762fc5
	  error(0,
Packit 762fc5
		_("Cannot have multiple commands on a single commandline.\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	};
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'u': {
Packit 762fc5
	if(conf->action==0){
Packit 762fc5
	  conf->action=DO_INIT|DO_COMPARE;
Packit 762fc5
	}else {
Packit 762fc5
	  error(0,
Packit 762fc5
		_("Cannot have multiple commands on a single commandline.\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	};
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'E': {
Packit 762fc5
	if(conf->action==0){
Packit 762fc5
	  conf->action=DO_DIFF;
Packit 762fc5
	}else {
Packit 762fc5
	  error(0,
Packit 762fc5
		_("Cannot have multiple commands on a single commandline.\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
	};
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      case 'D': {
Packit 762fc5
	conf->config_check=1;
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      default:
Packit 762fc5
	error(0,_("Unknown option given. Exiting\n"));
Packit 762fc5
	  exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
      }
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
  if(optind
Packit 762fc5
    error(0,_("Extra parameters given\n"));
Packit 762fc5
    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
  }
Packit 762fc5
  return RETOK;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static void setdefaults_before_config()
Packit 762fc5
{
Packit 762fc5
  char* urlstr=INITIALERRORSTO;
Packit 762fc5
  url_t* u=NULL;
Packit 762fc5
  char* s=(char*)malloc(sizeof(char)*MAXHOSTNAMELEN+1);
Packit 762fc5
  DB_ATTR_TYPE X;
Packit 762fc5
Packit 762fc5
  /*
Packit 762fc5
    Set up the hostname
Packit 762fc5
  */
Packit 762fc5
  conf=(db_config*)malloc(sizeof(db_config));
Packit 762fc5
  conf->defsyms=NULL;
Packit 762fc5
  
Packit 762fc5
  if (gethostname(s,MAXHOSTNAMELEN)==-1) {
Packit 762fc5
    error(0,_("Couldn't get hostname"));
Packit 762fc5
    free(s);
Packit 762fc5
  } else {
Packit 762fc5
    s=(char*)realloc((void*)s,strlen(s)+1);
Packit 762fc5
    do_define("HOSTNAME",s);
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  /* Setting some defaults */
Packit 762fc5
  conf->report_db=0;  
Packit 762fc5
  conf->tree=NULL;
Packit 762fc5
  conf->config_check=0;
Packit 762fc5
  conf->verbose_level=-1;
Packit 762fc5
  conf->database_add_metadata=1;
Packit 762fc5
  conf->report_detailed_init=0;
Packit 762fc5
  conf->report_base16=0;
Packit 762fc5
  conf->report_quiet=0;
Packit 762fc5
  conf->use_initial_errorsto=1;
Packit 762fc5
  conf->report_url=NULL;
Packit 762fc5
  conf->report_fd=NULL;
Packit 762fc5
  conf->report_syslog=0;
Packit 762fc5
  conf->report_db=0;
Packit 762fc5
#ifdef WITH_E2FSATTRS
Packit 762fc5
  conf->report_ignore_e2fsattrs = 0UL;
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  u=parse_url(urlstr);
Packit 762fc5
  error_init(u,1);
Packit 762fc5
Packit 762fc5
  conf->config_file=CONFIG_FILE;
Packit 762fc5
  conf->config_version=NULL;
Packit 762fc5
  
Packit 762fc5
#ifdef WITH_ACL
Packit 762fc5
  conf->no_acl_on_symlinks=0; /* zero means don't do ACLs on symlinks */
Packit 762fc5
#endif
Packit 762fc5
  
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
  conf->do_configmd=0;
Packit 762fc5
  conf->confmd=NULL;
Packit 762fc5
  conf->confhmactype=CONFIGHMACTYPE;
Packit 762fc5
  conf->old_confmdstr=NULL;
Packit 762fc5
  conf->dbhmactype=DBHMACTYPE;
Packit 762fc5
  conf->dbnewmd=NULL;
Packit 762fc5
  conf->dboldmd=NULL;
Packit 762fc5
#endif
Packit 762fc5
  
Packit 762fc5
  conf->do_dbnewmd=0;
Packit 762fc5
  conf->do_dboldmd=0;
Packit 762fc5
  conf->old_dbnewmdstr=NULL;
Packit 762fc5
  conf->old_dboldmdstr=NULL;
Packit 762fc5
  
Packit 762fc5
  conf->db_out_order=(DB_FIELD*)malloc(sizeof(DB_FIELD)*db_unknown);
Packit 762fc5
  conf->db_out_size=1;
Packit 762fc5
  conf->db_out_order[0]=db_filename;
Packit 762fc5
  conf->symlinks_found=0;
Packit 762fc5
  conf->db_in_size=0;
Packit 762fc5
  conf->db_in_order=NULL;
Packit 762fc5
  conf->db_in_url=NULL;
Packit 762fc5
  conf->db_in=NULL;
Packit 762fc5
  conf->db_new_size=0;
Packit 762fc5
  conf->db_new_order=NULL;
Packit 762fc5
  conf->db_new_url=NULL;
Packit 762fc5
  conf->db_new=NULL;
Packit 762fc5
  conf->db_out_url=NULL;
Packit 762fc5
  conf->db_out=NULL;
Packit 762fc5
Packit 762fc5
  conf->mdc_in=NULL;
Packit 762fc5
  conf->mdc_out=NULL;
Packit 762fc5
Packit 762fc5
  conf->line_db_in=NULL;
Packit 762fc5
  conf->line_db_out=NULL;
Packit 762fc5
Packit 762fc5
  conf->db_attrs = 0;
Packit 762fc5
#if defined(WITH_MHASH) || defined(WITH_GCRYPT)
Packit 762fc5
  conf->db_attrs |= DB_MD5|DB_TIGER|DB_HAVAL|DB_CRC32|DB_SHA1|DB_RMD160|DB_SHA256|DB_SHA512;
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
  conf->db_attrs |= DB_GOST;
Packit 762fc5
#ifdef HAVE_MHASH_WHIRLPOOL
Packit 762fc5
  conf->db_attrs |= DB_WHIRLPOOL;
Packit 762fc5
#endif
Packit 762fc5
#endif
Packit 762fc5
#endif
Packit 762fc5
  
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
  conf->db_gzin=0;
Packit 762fc5
  conf->db_gznew=0;
Packit 762fc5
  conf->gzip_dbout=0;
Packit 762fc5
  conf->db_gzout=0;
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  conf->action=0;
Packit 762fc5
  conf->catch_mmap=0;
Packit 762fc5
Packit 762fc5
  conf->warn_dead_symlinks=0;
Packit 762fc5
Packit 762fc5
  conf->grouped=1;
Packit 762fc5
Packit 762fc5
  conf->summarize_changes=1;
Packit 762fc5
Packit 762fc5
  conf->root_prefix="";
Packit 762fc5
  conf->root_prefix_length=0;
Packit 762fc5
Packit 762fc5
  conf->limit=NULL;
Packit 762fc5
  conf->limit_crx=NULL;
Packit 762fc5
Packit 762fc5
  conf->selrxlst=NULL;
Packit 762fc5
  conf->equrxlst=NULL;
Packit 762fc5
  conf->negrxlst=NULL;
Packit 762fc5
Packit 762fc5
  conf->groupsyms=NULL;
Packit 762fc5
Packit 762fc5
  conf->start_time=time(&(conf->start_time));
Packit 762fc5
Packit 762fc5
  do_groupdef("ANF",DB_NEWFILE);
Packit 762fc5
  do_groupdef("ARF",DB_RMFILE);
Packit 762fc5
  do_groupdef("p",DB_PERM);
Packit 762fc5
  do_groupdef("i",DB_INODE);
Packit 762fc5
  do_groupdef("I",DB_CHECKINODE);
Packit 762fc5
  do_groupdef("n",DB_LNKCOUNT);
Packit 762fc5
  do_groupdef("u",DB_UID);
Packit 762fc5
  do_groupdef("g",DB_GID);
Packit 762fc5
  do_groupdef("l",DB_LINKNAME);
Packit 762fc5
  do_groupdef("s",DB_SIZE);
Packit 762fc5
  do_groupdef("S",DB_SIZEG);
Packit 762fc5
  do_groupdef("b",DB_BCOUNT);
Packit 762fc5
  do_groupdef("m",DB_MTIME);
Packit 762fc5
  do_groupdef("c",DB_CTIME);
Packit 762fc5
  do_groupdef("a",DB_ATIME);
Packit 762fc5
#if defined(WITH_MHASH) || defined(WITH_GCRYPT)
Packit 762fc5
  do_groupdef("md5",DB_MD5);
Packit 762fc5
  do_groupdef("tiger",DB_TIGER);
Packit 762fc5
  do_groupdef("haval",DB_HAVAL);
Packit 762fc5
  do_groupdef("crc32",DB_CRC32);
Packit 762fc5
  do_groupdef("sha1",DB_SHA1);
Packit 762fc5
  do_groupdef("rmd160",DB_RMD160);
Packit 762fc5
  do_groupdef("sha256",DB_SHA256);
Packit 762fc5
  do_groupdef("sha512",DB_SHA512);
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_ACL
Packit 762fc5
  do_groupdef("acl",DB_ACL);
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_XATTR
Packit 762fc5
  do_groupdef("xattrs",DB_XATTRS);
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_SELINUX
Packit 762fc5
  do_groupdef("selinux",DB_SELINUX);
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
  do_groupdef("gost",DB_GOST);
Packit 762fc5
#ifdef HAVE_MHASH_WHIRLPOOL
Packit 762fc5
  do_groupdef("whirlpool",DB_WHIRLPOOL);
Packit 762fc5
#endif
Packit 762fc5
#endif
Packit 762fc5
  do_groupdef("ftype",DB_FTYPE);
Packit 762fc5
#ifdef WITH_E2FSATTRS
Packit 762fc5
  do_groupdef("e2fsattrs",DB_E2FSATTRS);
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
  X=0LLU;
Packit 762fc5
#ifdef WITH_ACL
Packit 762fc5
  X|=DB_ACL;
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_SELINUX
Packit 762fc5
  X|=DB_SELINUX;
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_XATTR
Packit 762fc5
  X|=DB_XATTRS;
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_E2FSATTRS
Packit 762fc5
  X|=DB_E2FSATTRS;
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
Packit 762fc5
  do_groupdef("R",DB_PERM|DB_FTYPE|DB_INODE|DB_LNKCOUNT|DB_UID|DB_GID|DB_SIZE|
Packit 762fc5
          DB_LINKNAME|DB_MTIME|DB_CTIME
Packit 762fc5
#if defined(WITH_MHASH) || defined(WITH_GCRYPT)
Packit 762fc5
          |DB_MD5
Packit 762fc5
#endif
Packit 762fc5
          |X);
Packit 762fc5
Packit 762fc5
  do_groupdef("L",DB_PERM|DB_FTYPE|DB_INODE|DB_LNKCOUNT|DB_UID|DB_GID|DB_LINKNAME|X);
Packit 762fc5
Packit 762fc5
  do_groupdef(">",DB_PERM|DB_FTYPE|DB_INODE|DB_LNKCOUNT|DB_UID|DB_GID|DB_SIZEG|
Packit 762fc5
		  DB_LINKNAME|X);
Packit 762fc5
  do_groupdef("X",X);
Packit 762fc5
  do_groupdef("E",0);
Packit 762fc5
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static void setdefaults_after_config()
Packit 762fc5
{
Packit 762fc5
  if(conf->db_in_url==NULL){
Packit 762fc5
    url_t* u=NULL;
Packit 762fc5
    u=(url_t*)malloc(sizeof(url_t));
Packit 762fc5
    u->type=url_file;
Packit 762fc5
    u->value=DEFAULT_DB;
Packit 762fc5
    conf->db_in_url=u;
Packit 762fc5
  }
Packit 762fc5
  if(conf->db_out_url==NULL){
Packit 762fc5
    url_t* u=NULL;
Packit 762fc5
    u=(url_t*)malloc(sizeof(url_t));
Packit 762fc5
    u->type=url_file;
Packit 762fc5
    u->value=DEFAULT_DB_OUT;
Packit 762fc5
    conf->db_out_url=u;
Packit 762fc5
  }
Packit 762fc5
  if(conf->report_url==NULL){
Packit 762fc5
    url_t* u=NULL;
Packit 762fc5
Packit 762fc5
    /* Don't free this one because conf->report_url needs it */
Packit 762fc5
    u=(url_t*)malloc(sizeof(url_t));
Packit 762fc5
    u->type=url_stdout;
Packit 762fc5
    u->value="";
Packit 762fc5
    error_init(u,0);
Packit 762fc5
  }
Packit 762fc5
  if(conf->action==0){
Packit 762fc5
    conf->action=DO_COMPARE;
Packit 762fc5
  }
Packit 762fc5
  if(conf->verbose_level==-1){
Packit 762fc5
    conf->verbose_level=5;
Packit 762fc5
  }
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
Packit 762fc5
int main(int argc,char**argv)
Packit 762fc5
{
Packit 762fc5
  int errorno=0;
Packit 762fc5
  byte* dig=NULL;
Packit 762fc5
  char* digstr=NULL;
Packit 762fc5
Packit 762fc5
#ifdef USE_LOCALE
Packit 762fc5
  setlocale(LC_ALL,"");
Packit 762fc5
  bindtextdomain(PACKAGE,LOCALEDIR);
Packit 762fc5
  textdomain(PACKAGE);
Packit 762fc5
#endif
Packit 762fc5
  umask(0177);
Packit 762fc5
  init_sighandler();
Packit 762fc5
Packit 762fc5
  setdefaults_before_config();
Packit 762fc5
Packit 762fc5
  if(read_param(argc,argv)==RETFAIL){
Packit 762fc5
    error(0, _("Invalid argument\n") );
Packit 762fc5
    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  errorno=commandconf('C',conf->config_file);
Packit 762fc5
Packit 762fc5
  errorno=commandconf('D',"");
Packit 762fc5
  if (errorno==RETFAIL){
Packit 762fc5
    error(0,_("Configuration error\n"));
Packit 762fc5
    exit(INVALID_CONFIGURELINE_ERROR);
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
  setdefaults_after_config();
Packit 762fc5
  
Packit 762fc5
  /*
Packit 762fc5
    This won't actualy work, because conf->tree is not constructed.
Packit 762fc5
    Now we construct it. And we have THE tree.
Packit 762fc5
   */
Packit 762fc5
  
Packit 762fc5
  conf->tree=gen_tree(conf->selrxlst,conf->negrxlst,conf->equrxlst);
Packit 762fc5
  
Packit 762fc5
  /* Let's do some sanity checks for the config */
Packit 762fc5
  if(cmpurl(conf->db_in_url,conf->db_out_url)==RETOK){
Packit 762fc5
    error(4,_("WARNING:Input and output database urls are the same.\n"));
Packit 762fc5
    if((conf->action&DO_INIT)&&(conf->action&DO_COMPARE)){
Packit 762fc5
      error(0,_("Input and output database urls cannot be the same "
Packit 762fc5
	    "when doing database update\n"));
Packit 762fc5
      exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
    }
Packit 762fc5
    if(conf->action&DO_DIFF){
Packit 762fc5
      error(0,_("Both input databases cannot be the same "
Packit 762fc5
		"when doing database compare\n"));
Packit 762fc5
      exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
    }
Packit 762fc5
  };
Packit 762fc5
  if((conf->action&DO_DIFF)&&(!(conf->db_new_url)||!(conf->db_in_url))){
Packit 762fc5
    error(0,_("Must have both input databases defined for "
Packit 762fc5
	      "database compare.\n"));
Packit 762fc5
    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
  }
Packit 762fc5
  if (conf->action&(DO_INIT|DO_COMPARE) && conf->root_prefix_length > 0) {
Packit 762fc5
      DIR *dir;
Packit 762fc5
      if((dir = opendir(conf->root_prefix)) != NULL) {
Packit 762fc5
          closedir(dir);
Packit 762fc5
      } else {
Packit 762fc5
          char* er=strerror(errno);
Packit 762fc5
          if (er!=NULL) {
Packit 762fc5
              error(0,"opendir() for root prefix %s failed: %s\n", conf->root_prefix,er);
Packit 762fc5
          } else {
Packit 762fc5
              error(0,"opendir() for root prefix %s failed: %i\n", conf->root_prefix,errno);
Packit 762fc5
          }
Packit 762fc5
          exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
      }
Packit 762fc5
  }
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
  if(conf->config_check&&FORCECONFIGMD){
Packit 762fc5
    error(0,"Can't give config checksum when compiled with --enable-forced_configmd\n");
Packit 762fc5
    exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  if((conf->do_configmd||conf->config_check)&& conf->confmd!=0){
Packit 762fc5
    /* The patch automatically adds a newline so will also have to add it. */
Packit 762fc5
    if(newlinelastinconfig==0){
Packit 762fc5
      mhash(conf->confmd,"\n",1);
Packit 762fc5
    };
Packit 762fc5
    mhash(conf->confmd, NULL,0);
Packit 762fc5
    dig=(byte*)malloc(sizeof(byte)*mhash_get_block_size(conf->confhmactype));
Packit 762fc5
    mhash_deinit(conf->confmd,(void*)dig);
Packit 762fc5
    digstr=encode_base64(dig,mhash_get_block_size(conf->confhmactype));
Packit 762fc5
Packit 762fc5
    if(!conf->config_check||FORCECONFIGMD){
Packit 762fc5
      if(strncmp(digstr,conf->old_confmdstr,strlen(digstr))!=0){
Packit 762fc5
	/* FIXME Don't use error and add configurability */
Packit 762fc5
	error(0,_("Config checksum mismatch\n"));
Packit 762fc5
	exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
  } else {
Packit 762fc5
    if(FORCECONFIGMD){
Packit 762fc5
      error(0,_("Config checksum not found. Exiting..\n"));
Packit 762fc5
      exit(INVALID_ARGUMENT_ERROR);
Packit 762fc5
    }
Packit 762fc5
  }
Packit 762fc5
#endif
Packit 762fc5
  conf->use_initial_errorsto=0;
Packit 762fc5
  if (!conf->config_check) {
Packit 762fc5
    if(conf->action&DO_INIT){
Packit 762fc5
      if(db_init(DB_WRITE)==RETFAIL) {
Packit 762fc5
	exit(IO_ERROR);
Packit 762fc5
      }
Packit 762fc5
      /* FIXME db_out_order info should be taken from tree/config */ 
Packit 762fc5
      /* update_db_out_order(-1); OOPS. It was allready done by append_rxlist
Packit 762fc5
	 :) */
Packit 762fc5
      if(db_writespec(conf)==RETFAIL){
Packit 762fc5
	error(0,_("Error while writing database. Exiting..\n"));
Packit 762fc5
	exit(IO_ERROR);
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    if((conf->action&DO_INIT)||(conf->action&DO_COMPARE)){
Packit 762fc5
      if(db_init(DB_DISK)==RETFAIL)
Packit 762fc5
	exit(IO_ERROR);
Packit 762fc5
    }
Packit 762fc5
    if((conf->action&DO_COMPARE)||(conf->action&DO_DIFF)){
Packit 762fc5
      if(db_init(DB_OLD)==RETFAIL)
Packit 762fc5
	exit(IO_ERROR);
Packit 762fc5
    }
Packit 762fc5
    if(conf->action&DO_DIFF){
Packit 762fc5
      if(db_init(DB_NEW)==RETFAIL)
Packit 762fc5
	exit(IO_ERROR);
Packit 762fc5
    }
Packit 762fc5
      
Packit 762fc5
    populate_tree(conf->tree);
Packit 762fc5
    db_close();
Packit 762fc5
    
Packit 762fc5
    exit(gen_report(conf->tree));
Packit 762fc5
    
Packit 762fc5
  }else {
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
    if(conf->confmd){
Packit 762fc5
      error(0,"Config checked. Use the following to patch your config file.\n");
Packit 762fc5
      error(0,"0a1\n");
Packit 762fc5
      if(newlinelastinconfig==1){
Packit 762fc5
	error(0,"> @@begin_config %s\n%lia%li\n> @@end_config\n",digstr,conf_lineno-1,conf_lineno+1);
Packit 762fc5
      }else {
Packit 762fc5
	error(0,"> @@begin_config %s\n%lia%li\n> @@end_config\n",digstr,conf_lineno,conf_lineno+2);
Packit 762fc5
      }
Packit 762fc5
      free(dig);
Packit 762fc5
      free(digstr);
Packit 762fc5
    }
Packit 762fc5
#endif
Packit 762fc5
  }
Packit 762fc5
  return RETOK;
Packit 762fc5
}
Packit 762fc5
const char* aide_key_3=CONFHMACKEY_03;
Packit 762fc5
const char* db_key_3=DBHMACKEY_03;
Packit 762fc5
Packit 762fc5
// vi: ts=8 sw=8