Blame src/db.c

Packit 762fc5
/* aide, Advanced Intrusion Detection Environment
Packit 762fc5
 *
Packit 762fc5
 * Copyright (C) 1999-2006,2010,2011,2013 Rami Lehti, Pablo Virolainen,
Packit 762fc5
 * 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
#include <stdio.h>
Packit 762fc5
#include <stdlib.h>
Packit 762fc5
#include <errno.h>
Packit 762fc5
#include "db.h"
Packit 762fc5
#include "db_file.h"
Packit 762fc5
#include "db_disk.h"
Packit 762fc5
#include "md.h"
Packit Service b31cf3
#include "fopen.h"
Packit 762fc5
Packit 762fc5
#ifdef WITH_PSQL
Packit 762fc5
#include "db_sql.h"
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#include "db_config.h"
Packit 762fc5
#include "report.h"
Packit 762fc5
#include "be.h"
Packit 762fc5
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
#include <mhash.h>
Packit 762fc5
#endif
Packit 762fc5
Packit 762fc5
#include "base64.h"
Packit 762fc5
#include "util.h"
Packit 762fc5
/*for locale support*/
Packit 762fc5
#include "locale-aide.h"
Packit 762fc5
/*for locale support*/
Packit 762fc5
Packit 762fc5
db_line* db_char2line(char** ss,int db);
Packit 762fc5
long readint(char* s,char* err);
Packit 762fc5
AIDE_OFF_TYPE readlong(char* s,char* err);
Packit 762fc5
long readoct(char* s,char* err);
Packit 762fc5
time_t base64totime_t(char*);
Packit 762fc5
Packit 762fc5
const char* db_names[db_unknown+1] = {
Packit 762fc5
   "name",
Packit 762fc5
   "lname",
Packit 762fc5
   "perm",
Packit 762fc5
   "uid",
Packit 762fc5
   "gid",
Packit 762fc5
   "size",
Packit 762fc5
   "atime",
Packit 762fc5
   "ctime",
Packit 762fc5
   "mtime",
Packit 762fc5
   "inode",
Packit 762fc5
   "bcount",
Packit 762fc5
   "lcount",
Packit 762fc5
   "md5",
Packit 762fc5
   "sha1",
Packit 762fc5
   "rmd160",
Packit 762fc5
   "tiger",
Packit 762fc5
   "crc32",
Packit 762fc5
   "haval",
Packit 762fc5
   "gost",
Packit 762fc5
   "crc32b",
Packit 762fc5
   "attr",
Packit 762fc5
   "acl",
Packit 762fc5
   "bsize",
Packit 762fc5
   "rdev",
Packit 762fc5
   "dev",
Packit 762fc5
   "checkmask",
Packit 762fc5
   "allownewfiles",
Packit 762fc5
   "allowrmfiles",
Packit 762fc5
   "sha256",
Packit 762fc5
   "sha512",
Packit 762fc5
   "whirlpool",
Packit 762fc5
   "selinux",
Packit 762fc5
   "xattrs",
Packit 762fc5
   "e2fsattrs",
Packit 762fc5
   "unknown"} ;
Packit 762fc5
Packit 762fc5
const int db_value[db_unknown+1] = {
Packit 762fc5
   db_filename,         /* "name",   */
Packit 762fc5
   db_linkname,         /* "lname",   */
Packit 762fc5
   db_perm,             /* "perm",    */
Packit 762fc5
   db_uid,              /* "uid",     */
Packit 762fc5
   db_gid,              /* "gid",     */
Packit 762fc5
   db_size,             /* "size",    */
Packit 762fc5
   db_atime,            /* "atime",   */
Packit 762fc5
   db_ctime,            /* "ctime",   */
Packit 762fc5
   db_mtime,            /* "mtime",   */
Packit 762fc5
   db_inode,            /* "inode",   */
Packit 762fc5
   db_bcount,           /* "bcount",  */
Packit 762fc5
   db_lnkcount,         /* "lcount",  */
Packit 762fc5
   db_md5,              /* "md5",     */
Packit 762fc5
   db_sha1,             /* "sha1",    */
Packit 762fc5
   db_rmd160,           /* "rmd160",  */
Packit 762fc5
   db_tiger,            /* "tiger",   */
Packit 762fc5
   db_crc32,            /* "crc32",   */
Packit 762fc5
   db_haval,            /* "haval",   */
Packit 762fc5
   db_gost,             /* "gost",    */
Packit 762fc5
   db_crc32b,           /* "crc32b",  */
Packit 762fc5
   db_attr,             /* attributes */
Packit 762fc5
   db_acl,              /* "acl"      */
Packit 762fc5
   db_bsize,            /* "bsize"    */
Packit 762fc5
   db_rdev,             /* "rdev"     */
Packit 762fc5
   db_dev,              /* "dev"      */
Packit 762fc5
   db_checkmask,        /* "checkmask" */
Packit 762fc5
   db_allownewfile,     /* "allownewfile" */
Packit 762fc5
   db_allowrmfile,      /* "allowrmfile" */
Packit 762fc5
   db_sha256,           /* "sha256",  */
Packit 762fc5
   db_sha512,           /* "sha512",  */
Packit 762fc5
   db_whirlpool,        /* "whirlpool", */
Packit 762fc5
   db_selinux,          /* "selinux", */
Packit 762fc5
   db_xattrs,           /* "xattrs",  */
Packit 762fc5
   db_e2fsattrs,        /* "e2fsattrs",  */
Packit 762fc5
   db_unknown };        /* "unknown"  */
Packit 762fc5
Packit 762fc5
const char* db_namealias[db_alias_size] = {
Packit 762fc5
  "count" } ;
Packit 762fc5
Packit 762fc5
const int db_aliasvalue[db_alias_size] = {
Packit 762fc5
  db_lnkcount } ;       /* "count",  */
Packit 762fc5
Packit 762fc5
static struct md_container *init_db_attrs(URL_TYPE type) {
Packit 762fc5
    struct md_container *mdc = NULL;
Packit 762fc5
    if (conf->db_attrs) {
Packit 762fc5
        switch (type) {
Packit 762fc5
            case url_stdout:
Packit 762fc5
            case url_stderr:
Packit 762fc5
            case url_fd:
Packit 762fc5
            case url_file:
Packit 762fc5
            #ifdef WITH_CURL
Packit 762fc5
            case url_http:
Packit 762fc5
            case url_https:
Packit 762fc5
            case url_ftp:
Packit 762fc5
            #endif /* WITH CURL */
Packit 762fc5
                mdc = malloc(sizeof(struct md_container)); /* freed in close_db_attrs */
Packit 762fc5
                mdc->todo_attr = conf->db_attrs;
Packit 762fc5
                init_md(mdc);
Packit 762fc5
                break;
Packit 762fc5
            #ifdef WITH_PSQL
Packit 762fc5
            case url_sql:
Packit 762fc5
                break;
Packit 762fc5
            #endif /* WITH_PSQL */
Packit 762fc5
            default :
Packit 762fc5
                error(200,_("init_db_attrs(): Unknown url type.\n"));
Packit 762fc5
        }
Packit 762fc5
    }
Packit 762fc5
    return mdc;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static db_line *close_db_attrs (struct md_container *mdc, char *url_value) {
Packit 762fc5
    db_line *line = NULL;
Packit 762fc5
    if (mdc != NULL) {
Packit 762fc5
        close_md(mdc);
Packit 762fc5
        line = malloc(sizeof(struct db_line));
Packit 762fc5
        line->filename = url_value;
Packit 762fc5
        line->perm = 0;
Packit 762fc5
        line->attr = conf->db_attrs;
Packit 762fc5
        md2line(mdc, line);
Packit 762fc5
        free(mdc);
Packit 762fc5
    }
Packit 762fc5
    return line;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
int db_init(int db)
Packit 762fc5
{
Packit 762fc5
  void* rv=NULL;
Packit 762fc5
  
Packit 762fc5
  error(200,"db_init %i\n",db);
Packit 762fc5
  
Packit 762fc5
  switch(db) {
Packit 762fc5
Packit 762fc5
  case DB_DISK: {
Packit 762fc5
    /*
Packit 762fc5
      Should we actually do something here?
Packit 762fc5
     */
Packit 762fc5
    return db_disk_init();
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
Packit 762fc5
  case DB_OLD: {
Packit 762fc5
    conf->mdc_in = init_db_attrs((conf->db_in_url)->type);
Packit 762fc5
    rv=be_init(1,conf->db_in_url,0);
Packit 762fc5
    if(rv==NULL) {
Packit 762fc5
      error(200,_("db_in is null\n"));      
Packit 762fc5
      return RETFAIL;
Packit 762fc5
    }
Packit 762fc5
    conf->db_in=rv;
Packit 762fc5
    error(200,_("db_in is nonnull\n"));
Packit 762fc5
    return RETOK;
Packit 762fc5
  }
Packit 762fc5
  case DB_WRITE: {    
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
    conf->mdc_out = init_db_attrs((conf->db_out_url)->type);
Packit 762fc5
    if(conf->gzip_dbout){
Packit 762fc5
       rv=be_init(0,conf->db_out_url,conf->gzip_dbout);
Packit 762fc5
       conf->db_gzout=rv;
Packit 762fc5
    }
Packit 762fc5
    else{
Packit 762fc5
#endif
Packit 762fc5
      rv=be_init(0,conf->db_out_url,0);
Packit 762fc5
      conf->db_out=rv;
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
    }
Packit 762fc5
#endif
Packit 762fc5
    
Packit 762fc5
    if(rv==NULL){
Packit 762fc5
      error(200,_("db_out is null\n"));
Packit 762fc5
      return RETFAIL;
Packit 762fc5
    }
Packit 762fc5
    error(200,_("db_out is nonnull %s\n"),conf->db_out_url->value);
Packit 762fc5
    return RETOK;
Packit 762fc5
  }
Packit 762fc5
  case DB_NEW: {
Packit 762fc5
    conf->mdc_out = init_db_attrs((conf->db_new_url)->type);
Packit 762fc5
    rv=be_init(1,conf->db_new_url,0);
Packit 762fc5
    if(rv==NULL) {
Packit 762fc5
      error(200,_("db_new is null\n"));      
Packit 762fc5
      return RETFAIL;
Packit 762fc5
    }
Packit 762fc5
    conf->db_new=rv;
Packit 762fc5
    error(200,_("db_new is nonnull\n"));
Packit 762fc5
    return RETOK;
Packit 762fc5
  }
Packit 762fc5
  }
Packit 762fc5
  return RETFAIL;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
db_line* db_readline(int db){
Packit 762fc5
  db_line* s=NULL;
Packit 762fc5
  int i=0;
Packit 762fc5
  url_t* db_url=NULL;
Packit 762fc5
  FILE** db_filep=NULL;
Packit 762fc5
  int* db_osize=0;
Packit 762fc5
  DB_FIELD** db_order=NULL;
Packit 762fc5
Packit 762fc5
  switch (db) {
Packit 762fc5
  case DB_DISK: {
Packit 762fc5
    /*
Packit 762fc5
      Nothing else to be done?
Packit 762fc5
     */
Packit 762fc5
    s=db_readline_disk();
Packit 762fc5
    return s;
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  case DB_OLD: {
Packit 762fc5
    db_url=conf->db_in_url;
Packit 762fc5
    db_filep=&(conf->db_in);
Packit 762fc5
    db_osize=&(conf->db_in_size);
Packit 762fc5
    db_order=&(conf->db_in_order);
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
  case DB_NEW: {
Packit 762fc5
    db_url=conf->db_new_url;
Packit 762fc5
    db_filep=&(conf->db_new);
Packit 762fc5
    db_osize=&(conf->db_new_size);
Packit 762fc5
    db_order=&(conf->db_new_order);
Packit 762fc5
    break;
Packit 762fc5
  }
Packit Service b31cf3
  default: {
Packit Service b31cf3
    return NULL;
Packit Service b31cf3
  }
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
  switch (db_url->type) {
Packit 762fc5
#ifdef WITH_CURL
Packit 762fc5
  case url_http:
Packit 762fc5
  case url_https:
Packit 762fc5
  case url_ftp:
Packit 762fc5
#endif /* WITH CURL */
Packit 762fc5
  case url_stdin:
Packit 762fc5
  case url_fd:
Packit 762fc5
  case url_file: {
Packit 762fc5
    /* Should set errno */
Packit 762fc5
    /* Please FIXME */
Packit 762fc5
    if ((*db_filep)!=NULL) {
Packit 762fc5
      char** ss=db_readline_file(db);
Packit 762fc5
      if (ss!=NULL){
Packit 762fc5
	s=db_char2line(ss,db);
Packit 762fc5
Packit 762fc5
	for(i=0;i<*db_osize;i++){
Packit 762fc5
	  if((*db_order)[i]!=db_unknown && 
Packit 762fc5
	     ss[(*db_order)[i]]!=NULL){
Packit 762fc5
	    free(ss[(*db_order)[i]]);
Packit 762fc5
	    ss[(*db_order)[i]]=NULL;
Packit 762fc5
	  }
Packit 762fc5
	}
Packit 762fc5
	free(ss);
Packit 762fc5
	
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
Packit 762fc5
#ifdef WITH_PSQL
Packit 762fc5
  case url_sql: {
Packit 762fc5
    error(255,"db_sql readline...");
Packit 762fc5
    s=db_readline_sql(db, conf);
Packit 762fc5
    
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#endif
Packit 762fc5
  default : {
Packit 762fc5
    error(0,_("db_readline():Url-type backend not implemented\n"));
Packit 762fc5
    return NULL;
Packit 762fc5
  }
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  return s;
Packit 762fc5
  
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
byte* base64tobyte(char* src,int len,size_t *ret_len)
Packit 762fc5
{
Packit 762fc5
  if(strcmp(src,"0")!=0){
Packit 762fc5
    return decode_base64(src,len,ret_len);
Packit 762fc5
  }
Packit 762fc5
  return NULL;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
static char *db_readchar(char *s)
Packit 762fc5
{
Packit 762fc5
  if (s == NULL)
Packit 762fc5
    return (NULL);
Packit 762fc5
  
Packit 762fc5
  if (s[0] == '0')
Packit 762fc5
  {
Packit 762fc5
    if (s[1] == '\0')
Packit 762fc5
      return (NULL);
Packit 762fc5
    
Packit 762fc5
    if (s[1] == '-')
Packit 762fc5
      return (strdup(""));
Packit 762fc5
Packit 762fc5
    if (s[1] == '0')
Packit 762fc5
    {
Packit 762fc5
      memmove(s, s+1, strlen(s+1)+1);
Packit 762fc5
      // Hope this removes core
Packit 762fc5
      // dumping in some environments. Has something to do with
Packit 762fc5
      // memory (de)allocation.
Packit 762fc5
    }
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
  decode_string(s);
Packit 762fc5
Packit 762fc5
  return strdup(s);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
#define WARN_ONCE(x) case db_ ## x : {                                  \
Packit 762fc5
      static int warn_once_ ## x = 0;                                   \
Packit 762fc5
      if (! warn_once_ ## x )                                           \
Packit 762fc5
        error(0,_("Hash %s uses MHASH, which is not enabled.\n"),       \
Packit 762fc5
              #x );                                                     \
Packit 762fc5
      warn_once_ ## x = 1;                                              \
Packit 762fc5
    } break
Packit 762fc5
Packit 762fc5
db_line* db_char2line(char** ss,int db){
Packit 762fc5
Packit 762fc5
  int i;
Packit 762fc5
  db_line* line=(db_line*)malloc(sizeof(db_line)*1);
Packit Service b31cf3
  int* db_osize=NULL;
Packit 762fc5
  DB_FIELD** db_order=NULL;
Packit 762fc5
Packit 762fc5
  switch (db) {
Packit 762fc5
  case DB_OLD: {
Packit 762fc5
    db_osize=&(conf->db_in_size);
Packit 762fc5
    db_order=&(conf->db_in_order);
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
  case DB_NEW: {
Packit 762fc5
    db_osize=&(conf->db_new_size);
Packit 762fc5
    db_order=&(conf->db_new_order);
Packit 762fc5
    break;
Packit 762fc5
  }
Packit Service b31cf3
  default: {
Packit Service b31cf3
    free(line);
Packit Service b31cf3
    return NULL;
Packit Service b31cf3
  }
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
Packit 762fc5
  line->md5=NULL;
Packit 762fc5
  line->sha1=NULL;
Packit 762fc5
  line->rmd160=NULL;
Packit 762fc5
  line->tiger=NULL;
Packit 762fc5
Packit 762fc5
  line->crc32=NULL; /* MHASH stuff.. */
Packit 762fc5
  line->crc32b=NULL;
Packit 762fc5
  line->haval=NULL;
Packit 762fc5
  line->gost=NULL;
Packit 762fc5
  line->whirlpool=NULL;
Packit 762fc5
  
Packit 762fc5
  line->sha256=NULL;
Packit 762fc5
  line->sha512=NULL;
Packit 762fc5
  line->perm=0;
Packit 762fc5
  line->uid=0;
Packit 762fc5
  line->gid=0;
Packit 762fc5
  line->atime=0;
Packit 762fc5
  line->ctime=0;
Packit 762fc5
  line->mtime=0;
Packit 762fc5
  line->inode=0;
Packit 762fc5
  line->nlink=0;
Packit 762fc5
  line->bcount=0;
Packit 762fc5
  line->size=0;
Packit 762fc5
  line->filename=NULL;
Packit 762fc5
  line->fullpath=NULL;
Packit 762fc5
  line->linkname=NULL;
Packit 762fc5
  line->acl=NULL;
Packit 762fc5
  line->xattrs=NULL;
Packit 762fc5
  line->e2fsattrs=0;
Packit 762fc5
  line->cntx=NULL;
Packit 762fc5
  
Packit 762fc5
  line->attr=conf->attr; /* attributes from @@dbspec */
Packit 762fc5
Packit 762fc5
  for(i=0;i<*db_osize;i++){
Packit 762fc5
    switch ((*db_order)[i]) {
Packit 762fc5
    case db_filename : {
Packit 762fc5
      if(ss[(*db_order)[i]]!=NULL){
Packit 762fc5
	decode_string(ss[(*db_order)[i]]);
Packit 762fc5
	line->fullpath=strdup(ss[(*db_order)[i]]);
Packit 762fc5
	line->filename=line->fullpath;
Packit 762fc5
      } else {
Packit 762fc5
	error(0,"db_char2line():Error while reading database\n");
Packit 762fc5
	exit(EXIT_FAILURE);
Packit 762fc5
      }
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_linkname : {
Packit 762fc5
      line->linkname = db_readchar(ss[(*db_order)[i]]);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_mtime : {
Packit 762fc5
      line->mtime=base64totime_t(ss[(*db_order)[i]]);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_bcount : {
Packit 762fc5
      line->bcount=readint(ss[(*db_order)[i]],"bcount");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_atime : {
Packit 762fc5
      line->atime=base64totime_t(ss[(*db_order)[i]]);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_ctime : {
Packit 762fc5
      line->ctime=base64totime_t(ss[(*db_order)[i]]);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_inode : {
Packit 762fc5
      line->inode=readint(ss[(*db_order)[i]],"inode");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
    case db_uid : {
Packit 762fc5
      line->uid=readint(ss[(*db_order)[i]],"uid");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_gid : {
Packit 762fc5
      line->gid=readint(ss[(*db_order)[i]],"gid");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_size : {
Packit 762fc5
      line->size=readlong(ss[(*db_order)[i]],"size");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_md5 : {
Packit 762fc5
      line->md5=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			     strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_sha1 : {
Packit 762fc5
      line->sha1=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			      strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_rmd160 : {
Packit 762fc5
      line->rmd160=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
				strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_tiger : {
Packit 762fc5
      line->tiger=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			       strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_crc32 : {
Packit 762fc5
      line->crc32=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			       strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_haval : {
Packit 762fc5
      line->haval=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			       strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
    case db_gost : {
Packit 762fc5
      line->gost=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			       strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_crc32b : {
Packit 762fc5
      line->crc32b=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
			       strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_whirlpool : {
Packit 762fc5
      line->whirlpool=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
                                   strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#else
Packit 762fc5
      WARN_ONCE(gost);
Packit 762fc5
      WARN_ONCE(crc32b);
Packit 762fc5
      WARN_ONCE(whirlpool);
Packit 762fc5
#endif
Packit 762fc5
    case db_sha256 : {
Packit 762fc5
      line->sha256=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
                                strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    case db_sha512 : {
Packit 762fc5
      line->sha512=base64tobyte(ss[(*db_order)[i]],
Packit 762fc5
                                strlen(ss[(*db_order)[i]]), NULL);
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#ifdef WITH_SUN_ACL
Packit 762fc5
    case db_acl : {
Packit 762fc5
      char* endp,*pos;
Packit 762fc5
      int entries,lc;
Packit 762fc5
      line->acl=NULL;
Packit 762fc5
      
Packit 762fc5
      entries=strtol(ss[(*db_order)[i]],&endp,10);
Packit 762fc5
      if (endp==ss[(*db_order)[i]]) {
Packit 762fc5
 	/* Something went wrong */
Packit 762fc5
	break;
Packit 762fc5
      }
Packit 762fc5
      pos=endp+1; /* Warning! if acl in database is corrupted then
Packit 762fc5
		     this will break down. */
Packit 762fc5
      
Packit 762fc5
      line->acl=malloc(sizeof(acl_type));
Packit 762fc5
      line->acl->entries=entries;
Packit 762fc5
      line->acl->acl=malloc(sizeof(aclent_t)*entries);
Packit 762fc5
      for (lc=0;lc
Packit 762fc5
	line->acl->acl[lc].a_type=strtol(pos,&endp,10);
Packit 762fc5
	pos=endp+1;
Packit 762fc5
	line->acl->acl[lc].a_id=strtol(pos,&endp,10);
Packit 762fc5
	pos=endp+1;
Packit 762fc5
	line->acl->acl[lc].a_perm=strtol(pos,&endp,10);
Packit 762fc5
	pos=endp+1;
Packit 762fc5
      }
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#endif
Packit 762fc5
#ifdef WITH_POSIX_ACL
Packit 762fc5
    case db_acl : {
Packit 762fc5
      char *tval = NULL;
Packit 762fc5
      
Packit 762fc5
      tval = strtok(ss[(*db_order)[i]], ",");
Packit 762fc5
Packit 762fc5
      line->acl = NULL;
Packit 762fc5
Packit 762fc5
      if (tval[0] == '0')
Packit 762fc5
        line->acl = NULL;
Packit 762fc5
      else if (!strcmp(tval, "POSIX"))
Packit 762fc5
      {
Packit 762fc5
        line->acl = malloc(sizeof(acl_type));        
Packit 762fc5
        line->acl->acl_a = NULL;
Packit 762fc5
        line->acl->acl_d = NULL;
Packit 762fc5
        
Packit 762fc5
        tval = strtok(NULL, ",");
Packit 762fc5
        line->acl->acl_a = (char *)base64tobyte(tval, strlen(tval), NULL);
Packit 762fc5
        tval = strtok(NULL, ",");
Packit 762fc5
        line->acl->acl_d = (char *)base64tobyte(tval, strlen(tval), NULL);
Packit 762fc5
      }
Packit 762fc5
      /* else, it's broken... */
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#endif
Packit 762fc5
      case db_xattrs : {
Packit 762fc5
        size_t num = 0;
Packit 762fc5
        char *tval = NULL;
Packit 762fc5
        
Packit 762fc5
        tval = strtok(ss[(*db_order)[i]], ",");
Packit 762fc5
        num = readlong(tval, "xattrs");
Packit 762fc5
        if (num)
Packit 762fc5
        {
Packit 762fc5
          line->xattrs = malloc(sizeof(xattrs_type));
Packit 762fc5
          line->xattrs->ents = calloc(sizeof(xattr_node), num);
Packit 762fc5
          line->xattrs->sz  = num;
Packit 762fc5
          line->xattrs->num = num;
Packit 762fc5
          num = 0;
Packit 762fc5
          while (num < line->xattrs->num)
Packit 762fc5
          {
Packit 762fc5
            byte  *val = NULL;
Packit 762fc5
            size_t vsz = 0;
Packit 762fc5
            
Packit 762fc5
            tval = strtok(NULL, ",");
Packit Service b31cf3
            char * tmp = strdup(tval);
Packit Service b31cf3
            line->xattrs->ents[num].key = db_readchar(tmp);
Packit Service b31cf3
            free(tmp);
Packit 762fc5
            tval = strtok(NULL, ",");
Packit 762fc5
            val = base64tobyte(tval, strlen(tval), &vsz;;
Packit 762fc5
            line->xattrs->ents[num].val = val;
Packit 762fc5
            line->xattrs->ents[num].vsz = vsz;
Packit 762fc5
Packit 762fc5
            ++num;
Packit 762fc5
          }
Packit 762fc5
        }
Packit 762fc5
        break;
Packit 762fc5
      }
Packit 762fc5
Packit 762fc5
      case db_selinux : {
Packit 762fc5
        byte  *val = NULL;
Packit 762fc5
        
Packit 762fc5
        val = base64tobyte(ss[(*db_order)[i]], strlen(ss[(*db_order)[i]]),NULL);
Packit 762fc5
        line->cntx = (char *)val;
Packit 762fc5
        break;
Packit 762fc5
      }
Packit 762fc5
      
Packit 762fc5
    case db_perm : {
Packit 762fc5
      line->perm=readoct(ss[(*db_order)[i]],"permissions");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
    case db_lnkcount : {
Packit 762fc5
      line->nlink=readint(ss[(*db_order)[i]],"nlink");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
    case db_attr : {
Packit 762fc5
      line->attr=readlong(ss[(*db_order)[i]],"attr");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
    case db_e2fsattrs : {
Packit 762fc5
      line->e2fsattrs=readlong(ss[(*db_order)[i]],"e2fsattrs");
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
Packit 762fc5
    case db_unknown : {
Packit 762fc5
      /* Unknown fields are ignored. */
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
    default : {
Packit 762fc5
      error(0,_("Not implemented in db_char2line %i \n"),(*db_order)[i]);
Packit Service b31cf3
      free_db_line(line);
Packit Service b31cf3
      free(line);
Packit 762fc5
      return NULL;
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
    }
Packit 762fc5
    
Packit 762fc5
  }
Packit 762fc5
Packit 762fc5
  return line;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
time_t base64totime_t(char* s){
Packit 762fc5
  
Packit 762fc5
  byte* b=decode_base64(s,strlen(s),NULL);
Packit 762fc5
  char* endp;
Packit 762fc5
  
Packit 762fc5
  if (b==NULL||strcmp(s,"0")==0) {
Packit 762fc5
    
Packit 762fc5
    /* Should we print error here? */
Packit 762fc5
    free(b);
Packit 762fc5
    
Packit 762fc5
    return 0;
Packit 762fc5
  } else {
Packit 762fc5
    time_t t = strtol((char *)b,&endp,10);
Packit 762fc5
    
Packit 762fc5
    if (endp[0]!='\0') {
Packit 762fc5
      error(0,"Error converting base64\n");
Packit 762fc5
      free(b);
Packit 762fc5
      return 0;
Packit 762fc5
    }
Packit 762fc5
    free(b);
Packit 762fc5
    return t;
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
  
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
long readint(char* s,char* err){
Packit 762fc5
  long i;
Packit 762fc5
  char* e;
Packit 762fc5
  i=strtol(s,&e,10);
Packit 762fc5
  if (e[0]!='\0') {
Packit 762fc5
    error(0,_("Could not read %s from database"),err);
Packit 762fc5
  }
Packit 762fc5
  return i;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
AIDE_OFF_TYPE readlong(char* s,char* err){
Packit 762fc5
  AIDE_OFF_TYPE i;
Packit 762fc5
  char* e;
Packit 762fc5
  i=AIDE_STRTOLL_FUNC(s,&e,10);
Packit 762fc5
  if (e[0]!='\0') {
Packit 762fc5
    error(0,_("Could not read %s from database"),err);
Packit 762fc5
  }
Packit 762fc5
  return i;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
long readoct(char* s,char* err){
Packit 762fc5
  long i;
Packit 762fc5
  char* e;
Packit 762fc5
  i=strtol(s,&e,8);
Packit 762fc5
  if (e[0]!='\0') {
Packit 762fc5
    error(0,_("Could not read %s from database. String %s \n"),err,s);
Packit 762fc5
  }
Packit 762fc5
  return i;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
Packit 762fc5
int db_writespec(db_config* dbconf)
Packit 762fc5
{
Packit 762fc5
  switch (dbconf->db_out_url->type) {
Packit 762fc5
  case url_stdout:
Packit 762fc5
  case url_stderr:
Packit 762fc5
  case url_fd:
Packit 762fc5
  case url_file: {
Packit 762fc5
    if(
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
       (dbconf->gzip_dbout && dbconf->db_gzout) ||
Packit 762fc5
#endif
Packit 762fc5
       (dbconf->db_out!=NULL)){
Packit 762fc5
      if(db_writespec_file(dbconf)==RETOK){
Packit 762fc5
	return RETOK;
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#ifdef WITH_CURL
Packit 762fc5
  case url_http:
Packit 762fc5
  case url_https:
Packit 762fc5
  case url_ftp:
Packit 762fc5
    {
Packit 762fc5
      
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#endif /* WITH CURL */
Packit 762fc5
#ifdef WITH_PSQL
Packit 762fc5
  case url_sql: {
Packit 762fc5
    if(dbconf->db_out!=NULL){
Packit 762fc5
      if(db_writespec_sql(dbconf)==RETOK){
Packit 762fc5
	return RETOK;
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#endif
Packit 762fc5
  default:{
Packit 762fc5
    error(0,_("Unknown output in db out.\n"));    
Packit 762fc5
    return RETFAIL;
Packit 762fc5
  }
Packit 762fc5
  }
Packit 762fc5
  return RETFAIL;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
int db_writeline(db_line* line,db_config* dbconf){
Packit 762fc5
Packit 762fc5
  if (line==NULL||dbconf==NULL) return RETOK;
Packit 762fc5
  
Packit 762fc5
  switch (dbconf->db_out_url->type) {
Packit 762fc5
#ifdef WITH_CURL
Packit 762fc5
  case url_http:
Packit 762fc5
  case url_https:
Packit 762fc5
  case url_ftp:
Packit 762fc5
#endif /* WITH CURL */
Packit 762fc5
  case url_stdout:
Packit 762fc5
  case url_stderr:
Packit 762fc5
  case url_fd:
Packit 762fc5
  case url_file: {
Packit 762fc5
    if (
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
       (dbconf->gzip_dbout && dbconf->db_gzout) ||
Packit 762fc5
#endif
Packit 762fc5
       (dbconf->db_out!=NULL)) {
Packit 762fc5
      if (db_writeline_file(line,dbconf,dbconf->db_out_url)==RETOK) {
Packit 762fc5
	return RETOK;
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    return RETFAIL;
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#ifdef WITH_PSQL
Packit 762fc5
  case url_sql: {
Packit 762fc5
    if (dbconf->db_out!=NULL) {
Packit 762fc5
      if (db_writeline_sql(line,dbconf)==RETOK) {
Packit 762fc5
	return RETOK;
Packit 762fc5
      }
Packit 762fc5
    }
Packit 762fc5
    return RETFAIL;
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#endif
Packit 762fc5
  default : {
Packit 762fc5
    error(0,_("Unknown output in db out.\n"));    
Packit 762fc5
    return RETFAIL;
Packit 762fc5
  } 
Packit 762fc5
  }
Packit 762fc5
  return RETFAIL;
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
void db_close() {
Packit 762fc5
  switch (conf->db_out_url->type) {
Packit 762fc5
  case url_stdout:
Packit 762fc5
  case url_stderr:
Packit 762fc5
  case url_fd:
Packit 762fc5
  case url_file: {
Packit 762fc5
    if (
Packit 762fc5
#ifdef WITH_ZLIB
Packit 762fc5
       (conf->gzip_dbout && conf->db_gzout) ||
Packit 762fc5
#endif
Packit 762fc5
       (conf->db_out!=NULL)) {
Packit 762fc5
        db_close_file(conf);
Packit 762fc5
    }
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#ifdef WITH_CURL
Packit 762fc5
  case url_http:
Packit 762fc5
  case url_https:
Packit 762fc5
  case url_ftp:
Packit 762fc5
    {
Packit 762fc5
        if (conf->db_out!=NULL) {
Packit Service b31cf3
            url_fclose((URL_FILE*)conf->db_out);
Packit 762fc5
        }
Packit 762fc5
      break;
Packit 762fc5
    }
Packit 762fc5
#endif /* WITH CURL */
Packit 762fc5
#ifdef WITH_PSQL
Packit 762fc5
  case url_sql: {
Packit 762fc5
    if (conf->db_out!=NULL) {
Packit 762fc5
      db_close_sql(conf->db_out);
Packit 762fc5
    }
Packit 762fc5
    break;
Packit 762fc5
  }
Packit 762fc5
#endif
Packit 762fc5
  default : {
Packit 762fc5
    error(0,_("db_close():Unknown output in db out.\n"));    
Packit 762fc5
  } 
Packit 762fc5
  }
Packit 762fc5
  conf->line_db_in = close_db_attrs(conf->mdc_in, (conf->db_in_url)->value);
Packit 762fc5
  conf->line_db_out = close_db_attrs(conf->mdc_out, (conf->action&DO_DIFF
Packit 762fc5
          ? conf->db_new_url : conf->db_out_url)->value);
Packit 762fc5
}
Packit 762fc5
Packit 762fc5
void free_db_line(db_line* dl)
Packit 762fc5
{
Packit 762fc5
  if (dl==NULL) {
Packit 762fc5
    return;
Packit 762fc5
  }
Packit 762fc5
  
Packit 762fc5
#define checked_free(x) do { free(x); x=NULL; } while (0)
Packit 762fc5
Packit 762fc5
  checked_free(dl->md5);
Packit 762fc5
  checked_free(dl->sha1);
Packit 762fc5
  checked_free(dl->rmd160);
Packit 762fc5
  checked_free(dl->tiger);
Packit 762fc5
  dl->filename=NULL;
Packit 762fc5
  checked_free(dl->fullpath);
Packit 762fc5
  checked_free(dl->linkname);
Packit 762fc5
  
Packit 762fc5
#ifdef WITH_MHASH
Packit 762fc5
  checked_free(dl->crc32);
Packit 762fc5
  checked_free(dl->crc32b);
Packit 762fc5
  checked_free(dl->gost);
Packit 762fc5
  checked_free(dl->haval);
Packit 762fc5
#endif
Packit 762fc5
  
Packit 762fc5
  checked_free(dl->sha256);
Packit 762fc5
  checked_free(dl->sha512);
Packit 762fc5
  checked_free(dl->whirlpool);
Packit 762fc5
Packit 762fc5
  if (dl->acl)
Packit 762fc5
  {
Packit 762fc5
#ifdef WITH_ACL
Packit 762fc5
    free(dl->acl->acl_a);
Packit 762fc5
    free(dl->acl->acl_d);
Packit 762fc5
#endif
Packit 762fc5
  }
Packit 762fc5
  checked_free(dl->acl);
Packit 762fc5
  
Packit 762fc5
  if (dl->xattrs)
Packit 762fc5
    free(dl->xattrs->ents);
Packit 762fc5
  checked_free(dl->xattrs);
Packit 762fc5
  checked_free(dl->cntx);
Packit 762fc5
}
Packit 762fc5
const char* aide_key_5=CONFHMACKEY_05;
Packit 762fc5
const char* db_key_5=DBHMACKEY_05;