⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db_file.c

📁 This piece of software was written as a replacement and extension for Tripwire. Tripwire is an exce
💻 C
📖 第 1 页 / 共 2 页
字号:
/* aide, Advanced Intrusion Detection Environment * * Copyright (C) 1999,2000,2001,2002 Rami Lehti, Pablo Virolainen * $Header: /cvs-root-aide/aide2/src/db_file.c,v 1.18 2002/05/30 09:42:46 pablo Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdio.h>#include <string.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <errno.h>#include "types.h"#include "base64.h"#include "db_file.h"#include "conf_yacc.h"#include "util.h"#include "aide.h"#include "db_sql.h" /* typedefs */#include "commandconf.h"/*for locale support*/#include "locale-aide.h"/*for locale support*/#ifdef WITH_MHASH#include <mhash.h>#endif#ifdef WITH_ZLIB#include <zlib.h>#endif#define BUFSIZE 16384#include "md.h"#ifdef WITH_ZLIB#define ZBUFSIZE 16384/* FIXME get rid of this */void handle_gzipped_input(int out,gzFile* gzp){  int nread=0;  int err=0;  int* buf=malloc(ZBUFSIZE);  buf[0]='\0';  error(200,"handle_gzipped_input(),%d\n",out);  while(!gzeof(*gzp)){    if((nread=gzread(*gzp,buf,ZBUFSIZE))<0){      error(0,_("gzread() failed:gzerr=%s!\n"),gzerror(*gzp,&err));      exit(1);    } else {      /* gzread returns 0 even if uncompressed bytes were read*/      if(nread==0){	write(out, buf,strlen((char*)buf));      } else {	write(out, buf,nread);      }      error(240,"nread=%d,strlen(buf)=%d,errno=%s,gzerr=%s\n",	    nread,strlen((char*)buf),strerror(errno),	    gzerror(*gzp,&err));      buf[0]='\0';    }  }  close(out);  error(240,"handle_gzipped_input() exiting\n");  exit(0);  /* NOT REACHED */  return;}#endifint dofflush(void){  int retval;#ifdef WITH_ZLIB  if(conf->gzip_dbout){    retval=gzflush(conf->db_gzout,Z_SYNC_FLUSH);  }else {#endif    retval=fflush(conf->db_out); #ifdef WITH_ZLIB  }#endif  return retval;}int dofprintf( const char* s,...){  int retval;  char* temp=NULL;  va_list ap;    va_start(ap,s);  temp=(char*)malloc(3);  if(temp==NULL){    error(0,"Unable to alloc %i bytes\n",3);    return -1;  }  retval=vsnprintf(temp,3,s,ap);    free(temp);    temp=(char*)malloc(retval+2);  if(temp==NULL){    error(0,"Unable to alloc %i bytes\n",retval+2);    return -1;  }    retval=vsnprintf(temp,retval+1,s,ap);  #ifdef WITH_MHASH  if(conf->do_dbnewmd)    mhash(conf->dbnewmd,(void*)temp,retval);#endif#ifdef WITH_ZLIB  if(conf->gzip_dbout){    retval=gzwrite(conf->db_gzout,temp,retval);  }else{#endif    retval=vfprintf(conf->db_out,s,ap);#ifdef WITH_ZLIB  }#endif  free(temp);  va_end(ap);  return retval;}int db_file_read_spec(int db){    int i=0;  int* db_osize=0;  DB_FIELD** db_order=NULL;  FILE** db_filep=NULL;  url_t* db_url=NULL;#ifdef WITH_ZLIB  gzFile* db_gzp=NULL;#endif  switch (db) {  case DB_OLD: {    db_osize=&(conf->db_in_size);    db_order=&(conf->db_in_order);    db_filep=&(conf->db_in);    db_url=conf->db_in_url;    db_lineno=&db_in_lineno;#ifdef WITH_ZLIB    db_gzp=&(conf->db_gzin);#endif    break;  }  case DB_NEW: {    db_osize=&(conf->db_new_size);    db_order=&(conf->db_new_order);    db_filep=&(conf->db_new);    db_url=conf->db_new_url;    db_lineno=&db_new_lineno;#ifdef WITH_ZLIB    db_gzp=&(conf->db_gznew);#endif    break;  }  }  *db_order=(DB_FIELD*) malloc(1*sizeof(DB_FIELD));    while ((i=db_scan())!=TNEWLINE){    switch (i) {          case TID : {      int l;            /* Yes... we do not check if realloc returns nonnull */      *db_order=(DB_FIELD*)	realloc((void*)*db_order,		((*db_osize)+1)*sizeof(DB_FIELD));            if(*db_order==NULL){	return RETFAIL;      }            (*db_order)[*db_osize]=db_unknown;            for (l=0;l<db_unknown;l++){		if (strcmp(db_names[l],dbtext)==0) {	  	  if (check_db_order(*db_order, *db_osize,			     db_value[l])==RETFAIL) {	    error(0,"Field %s redefined in @@dbspec\n",dbtext);	    (*db_order)[*db_osize]=db_unknown;	  } else {	    (*db_order)[*db_osize]=db_value[l];	  }	  (*db_osize)++;	  break;	}      }      for (l=0;l<db_alias_size;l++){		if (strcmp(db_namealias[l],dbtext)==0) {	  	  if (check_db_order(*db_order, *db_osize,			     db_aliasvalue[l])==RETFAIL) {	    error(0,"Field %s redefined in @@dbspec\n",dbtext);	    (*db_order)[*db_osize]=db_unknown;	  } else {	    (*db_order)[*db_osize]=db_aliasvalue[l];	  }	  (*db_osize)++;	  break;	}      }      if(l==db_unknown){	error(0,"Unknown field %s in database\n",dbtext);	(*db_osize)++;      }      break;    }        case TDBSPEC : {      error(0,"Only one @@dbspec in inputdatabase.\n");      return RETFAIL;      break;    }        default : {      error(0,"Aide internal error while reading inputdatabase.\n");      return RETFAIL;    }    }  }  /* Lets generate attr from db_order if database does not have attr */  conf->attr=-1;  for (i=0;i<*db_osize;i++) {    if ((*db_order)[i]==db_attr) {      conf->attr=1;    }  }  if (conf->attr==-1) {    conf->attr=0;    error(0,"Database does not have attr field.\nComparation may be incorrect\nGenerating attr-field from dbspec\nIt might be a good Idea to regenerate databases. Sorry.\n");    for(i=0;i<conf->db_in_size;i++) {      conf->attr|=1<<(*db_order)[i];    }  }  return RETOK;}char** db_readline_file(int db){    char** s=NULL;    int i=0;  int r;  int a=0;  int token=0;  int gotbegin_db=0;  int gotend_db=0;  int* domd=NULL;  MHASH* md=NULL;  char** oldmdstr=NULL;  int* db_osize=0;  DB_FIELD** db_order=NULL;  FILE** db_filep=NULL;  url_t* db_url=NULL;#ifdef WITH_ZLIB  gzFile* db_gzp=NULL;#endif  switch (db) {  case DB_OLD: {    md=&(conf->dboldmd);    domd=&(conf->do_dboldmd);    oldmdstr=&(conf->old_dboldmdstr);    db_osize=&(conf->db_in_size);    db_order=&(conf->db_in_order);    db_filep=&(conf->db_in);    db_url=conf->db_in_url;    db_lineno=&db_in_lineno;#ifdef WITH_ZLIB    db_gzp=&(conf->db_gzin);#endif    break;  }  case DB_NEW: {    md=&(conf->dbnewmd);    domd=&(conf->do_dbnewmd);    oldmdstr=&(conf->old_dbnewmdstr);    db_osize=&(conf->db_new_size);    db_order=&(conf->db_new_order);    db_filep=&(conf->db_new);    db_url=conf->db_new_url;    db_lineno=&db_new_lineno;#ifdef WITH_ZLIB    db_gzp=&(conf->db_gznew);#endif    break;  }  }    if (*db_osize==0) {    db_buff(db,*db_filep);        token=db_scan();    while((token!=TDBSPEC)){      switch(token){      case TUNKNOWN: {	continue;	break;      }      case TBEGIN_DB: {	token=db_scan();	gotbegin_db=1;	continue;	break;      }      case TNEWLINE: {	if(gotbegin_db){	  *domd=1;	  token=db_scan();	  continue;	}else {	  token=TEOF;	  break;	}      }      case TGZIPHEADER: {	error(0,"Gzipheader found inside uncompressed db!\n");	return NULL;	break;      }      default: {	/* If it is anything else we quit */	/* Missing dbspec */	token=TEOF;	break;      }      }      if(token==TEOF){	break;      }      token=db_scan();    }    if(FORCEDBMD&&!gotbegin_db){      error(0,"Database %i does not have checksum!\n");      return NULL;    }    if (token!=TDBSPEC) {      /*       * error.. must be a @@dbspec line       */            switch (db_url->type) {      case url_file : {	error(0,"Filedatabase must have one db_spec spesification\n");	break;      }      case url_stdin : {	error(0,"Pipedatabase must have one db_spec spesification\n");	break;      }      case url_fd: {	error(0,"FD-database must have one db_spec spesification\n");	break;      }      default : {	error(0,"db_readline_file():Unknown or unsupported db in type.\n");		break;      }            }      return s;    }        /*     * Here we read da spec     */        if (db_file_read_spec(db)!=0) {      /* somethin went wrong */      return s;    }      }else {    /* We need to switch the buffer cleanly*/    db_buff(db,NULL);  }  s=(char**)malloc(sizeof(char*)*db_unknown);  /* We NEED this to avoid Bus errors on Suns */  for(i=0;i<db_unknown;i++){    s[i]=NULL;  }    for(i=0;i<*db_osize;i++){    switch (r=db_scan()) {          case TDBSPEC : {            error(0,"Databasefile can have only one db_spec.\nTrying to continue on line %i\n",*db_lineno);            break;    }    case TNAME : {      if ((*db_order)[i]!=db_unknown) {	s[*db_order[i]]=(char*)strdup(dbtext);      }      break;    }        case TID : {      if ((*db_order)[i]!=db_unknown) {	s[(*db_order)[i]]=(char*)strdup(dbtext);      }      break;    }        case TNEWLINE : {            if (i==0) {	i--;	break;      }            /*  */      error(0,"Not enough parameters in db:%i. Trying to continue.\n",	    *db_lineno);      for(a=0;a<i;a++){	free(s[(*db_order)[a]]);	s[(*db_order)[a]]=NULL;      }      i=0;      break;    }    case TBEGIN_DB : {      error(0,_("Corrupt db. Found @@begin_db inside db. Please check\n"));      return NULL;      break;    }    case TEND_DB : {      gotend_db=1;      token=db_scan();      if(token!=TSTRING){	error(0,_("Corrupt db. Checksum garbled\n"));	abort();      } else {	if(*md){	  byte* dig=NULL;	  char* digstr=NULL;	  	  *oldmdstr=strdup(dbtext);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -