Location: PHPKode > projects > SnortCenter 2.x > snortcenter-release/database.php
<?php
    /*
    ** SnortCenter Copyright (C) 2001,2002,2003 Stefan Dens
    **
    ** Author: Stefan Dens <hide@address.com>
    **
    ** 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.
    */
?>
<?php
    $db = NewACIDDBConnection($DBlib_path, $DBtype);
    $db->acidConnect($DB_dbname, $DB_host, $DB_port, $DB_user, $DB_password);
     
    $db_ver = $db->acidGetDBVersion();
    if ($db_ver && $db_ver < 6) {
        echo "Old database version: $db_ver exist<BR>
            First drop the database!<BR>
            !!! All data will be lost !!!<P>";
    }
    elseif(!$db_ver) {
        $tblVars_present = $db->acidTableExists("vars");
        $tblConfig_present = $db->acidTableExists("config");
        $tblOutput_present = $db->acidTableExists("output");
        $tblPreprocessor_present = $db->acidTableExists("preprocessor");
        $tblRules_present = $db->acidTableExists("rules");
        $tblContent_present = $db->acidTableExists("content");
        $tblUricontent_present = $db->acidTableExists("uricontent");
        $tblReference_present = $db->acidTableExists("reference");
        $tblRulechange_present = $db->acidTableExists("rulechange");
        $tblRuletype_present = $db->acidTableExists("ruletype");
        $tblSchema_present = $db->acidTableExists("schema");
        $tblSensor_present = $db->acidTableExists("sensor");
        $tblSnortcenter_cfg_present = $db->acidTableExists("snortcenter_cfg");
        $tblUsers_present = $db->acidTableExists("users");
        $tblUsers_present = $db->acidTableExists("rules_template");
         
         
        if (!$tblVars_present) {
$sql = "CREATE TABLE vars (
  var_name varchar(255) NOT NULL default '',
  var_value longtext NOT NULL,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'vers': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'vars'", 1);
            $tblVars_present = $db->acidTableExists("vars");
        }

        if (!$tblDecode_present) {
$sql = "CREATE TABLE decode (
  var_name varchar(255) NOT NULL default '',
  var_value longtext NOT NULL,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'decode': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'decode'", 1);
            $tblVars_present = $db->acidTableExists("decode");
        }

         
        if (!$tblConfig_present) {
$sql = "CREATE TABLE config (
  config_name varchar(255) NOT NULL default '',
  config_value longtext,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'config': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'config'", 1);
            $tblConfig_present = $db->acidTableExists("config");
        }
         
        if (!$tblOutput_present) {
$sql = "CREATE TABLE output (
  spo_name varchar(255) NOT NULL default '',
  spo_value longtext,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'output': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'output'", 1);
            $tblOutput_present = $db->acidTableExists("output");
        }
         
        if (!$tblPreprocessor_present) {
$sql = "CREATE TABLE preprocessor (
  spp_name varchar(255) NOT NULL default '',
  spp_value longtext,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'preprocessor': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'preprocessor'", 1);
            $tblPreprocessor_present = $db->acidTableExists("preprocessor");
        }
         
        if (!$tblRules_present) {
$sql = "CREATE TABLE rules (
  action varchar(15) NOT NULL default '',
  proto varchar(5) NOT NULL default '',
  src_ip varchar(255) NOT NULL default '',
  src_port varchar(255) NOT NULL default '',
  dst_ip varchar(255) NOT NULL default '',
  dst_port varchar(255) NOT NULL default '',
  msg varchar(255) default NULL,
  logto varchar(255) default NULL,
  ttl char(3) default NULL,
  tos char(3) default NULL,
  id varchar(5) default NULL,
  ipoption varchar(5) default NULL,
  fragbits varchar(10) default NULL,
  dsize varchar(10) default NULL,
  flags varchar(15) default NULL,
  window varchar(6) default NULL,
  seq varchar(10) default NULL,
  ack varchar(10) default NULL,
  itype char(3) default NULL,
  icode char(3) default NULL,
  icmp_id varchar(5) default NULL,
  icmp_seq varchar(5) default NULL,
  content_list varchar(255) default NULL,
  session varchar(9) default NULL,
  rpc varchar(25) default NULL,
  resp varchar(80) default NULL,
  react varchar(255) default NULL,
  classtype varchar(255) default NULL,
  priority varchar(6) default NULL,
  tag varchar(255) default NULL,
  ip_proto varchar(25) default NULL,
  sameip varchar(6) default NULL,
  stateless varchar(9) default NULL,
  sid bigint(20) unsigned NOT NULL default '0',
  rev int(10) unsigned NOT NULL default '0',
  activates varchar(10) default NULL,
  activates_by varchar(10) default NULL,
  count varchar(10) default NULL,
  operator char(2) NOT NULL default '',
  category varchar(255) default NULL,
  flow varchar(255) default NULL,
  fragoffset varchar(10) default NULL,
  pcre text,
  flowbits varchar(255) default NULL,
  threshold varchar(255) default NULL,
  snortsam varchar(64) default NULL,
  multiple_rest text,
  rule_mark set('del','') NOT NULL default '',
  PRIMARY KEY  (sid,rev),
  KEY category (category),
  KEY rule_mark (rule_mark)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'rules': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'rules'", 1);
            $tblRules_present = $db->acidTableExists("rules");
        }
         
        if (!$tblContent_present) {
$sql = "CREATE TABLE content (
  sid bigint(20) unsigned NOT NULL default '0',
  sequence smallint(6) unsigned NOT NULL default '0',
  content longtext,
  off_set varchar(4) default NULL,
  depth varchar(4) default NULL,
  nocase varchar(6) default NULL,
  regex varchar(5) default NULL,
  rawbytes varchar(8) default NULL,
  distance varchar(10) default NULL,
  within varchar(10) default NULL,
  byte_jump varchar(255) default NULL,
  byte_test varchar(255) default NULL,
  asn1 varchar(255) default NULL,
  isdataat varchar(255) default NULL,
  KEY sid (sid)
)";

            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'content': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'content'", 1);
            $tblContent_present = $db->acidTableExists("content");
        }
         
        if (!$tblUricontent_present) {
$sql = "CREATE TABLE uricontent (
  sid bigint(20) unsigned NOT NULL default '0',
  uricontent longtext,
  off_set varchar(4) default NULL,
  depth varchar(4) default NULL,
  nocase varchar(6) default NULL,
  regex varchar(5) default NULL,
  distance varchar(10) default NULL,
  within varchar(10) default NULL,
  byte_jump varchar(255) default NULL,
  byte_test varchar(255) default NULL,
  asn1 varchar(255) default NULL,
  isdataat varchar(255) default NULL,
  KEY sid (sid)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'uricontent': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'uricontent'", 1);
            $tblUricontent_present = $db->acidTableExists("uricontent");
        }
         
        if (!$tblReference_present) {
$sql = "CREATE TABLE reference (
  sid bigint(20) NOT NULL default '0',
  reference varchar(255) default NULL,
  KEY sid (sid)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'reference': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'reference'", 1);
            $tblReference_present = $db->acidTableExists("reference");
        }
         
        if (!$tblRulechange_present) {
$sql = "CREATE TABLE rulechange (
  sensor_id int(10) unsigned NOT NULL default '0',
  sid int(10) unsigned NOT NULL default '0',
  action varchar(255) NOT NULL default '',
  src_ip varchar(255) NOT NULL default '',
  src_port varchar(255) NOT NULL default '',
  dst_ip varchar(255) NOT NULL default '',
  dst_port varchar(255) NOT NULL default '',
  PRIMARY KEY  (sensor_id,sid)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'rulechange': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'rulechange'", 1);
            $tblRulechange_present = $db->acidTableExists("rulechange");
        }
         
        if (!$tblRuletype_present) {
$sql = "CREATE TABLE ruletype (
  ruletype_name varchar(255) NOT NULL default '',
  ruletype_value longtext,
  snort_default set('yes','no') NOT NULL default 'no',
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'ruletype': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'ruletype'", 1);
            $tblRuletype_present = $db->acidTableExists("ruletype");
        }
         
        if (!$tblSchema_present) {
            $sql = "CREATE TABLE schema (
                vseq int(10) unsigned NOT NULL default '0',
                ctime datetime NOT NULL default '0000-00-00 00:00:00'
                )";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'schema': ".$db->acidErrorMessage(), 0);
            else
            {
                ErrorMessage("Successfully created 'schema'", 1);
                $sql = "INSERT INTO schema VALUES (5, '2002-10-11 00:00:00')";
                $db->acidExecute($sql, -1, -1, false);
                if ($db->acidErrorMessage()  != "")
                    ErrorMessage("Unable to insert  'schema' values: ".$db->acidErrorMessage(), 0);
                else
                    ErrorMessage("Successfully inserted 'schema' values", 1);
            }
            $tblSchema_present = $db->acidTableExists("schema");
        }
         
        if (!$tblSensor_present) {
$sql = "CREATE TABLE sensor (
  sensor_name varchar(255) NOT NULL default '',
  rules longtext,
  rules_template longtext,
  rem_sid_rules_template longtext,
  vars longtext,
  preprocessor longtext,
  output longtext,
  ruletype longtext,
  config longtext,
  id int(10) unsigned NOT NULL auto_increment,
  sensor_ip varchar(15) default NULL,
  sensor_port int(10) unsigned default NULL,
  sensor_user varchar(255) default NULL,
  sensor_passw blob,
  net_proto varchar(255) default NULL,
  cmd_line longtext,
  interface varchar(255) default NULL,
  enable int(1) unsigned default NULL,
  auto_update int(1) unsigned default NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY sensor_name (sensor_name)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'sensor': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'sensor'", 1);
            $tblSensor_present = $db->acidTableExists("sensor");
        }
         
        if (!$tblSnortcenter_cfg_present) {
$sql = "CREATE TABLE snortcenter_cfg (
  filename varchar(25) default NULL,
  md5 varchar(32) default NULL
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'snortcenter_cfg': ".$db->acidErrorMessage(), 0);
            else
                ErrorMessage("Successfully created 'snortcenter_cfg'", 1);
            $tblSnortcenter_cfg_present = $db->acidTableExists("snortcenter_cfg");
        }
         
        if (!$tblUsers_present) {
$sql = "CREATE TABLE users (
  id int(11) NOT NULL auto_increment,
  username varchar(20) NOT NULL default '',
  password varchar(100) NOT NULL default '',
  email varchar(100) NOT NULL default '',
  lost char(1) NOT NULL default '0',
  acl varchar(255) NOT NULL default '',
  PRIMARY KEY  (id),
  UNIQUE KEY username (username)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'users': ".$db->acidErrorMessage(), 0);
            else
            {
                ErrorMessage("Successfully created 'users'", 1);
                $sql = "INSERT INTO users VALUES (1, 'admin', 'eb399bcaca686f8609137153307eecf1', 'hide@address.com', '', 'sensor|alert')";
                $db->acidExecute($sql, -1, -1, false);
                if ($db->acidErrorMessage()  != "")
                    ErrorMessage("Unable to insert user: 'admin': ".$db->acidErrorMessage(), 0);
                else
                    ErrorMessage("Successfully inserted user: '<BLINK>admin</BLINK>', password '<BLINK>change</BLINK>'", 1);
            }
            $tblUsers_present = $db->acidTableExists("users");
        }

        if (!$tblRules_template_present) {
$sql = "CREATE TABLE rules_template (
  id int(11) NOT NULL auto_increment,
  template_name varchar(255) default NULL,
  template_description varchar(65) default NULL,
  def_template set('yes','no') NOT NULL default '',
  add_sid longtext,
  rem_sid longtext,
  PRIMARY KEY  (id)
)";
            $db->acidExecute($sql, -1, -1, false);
            if ($db->acidErrorMessage()  != "")
                ErrorMessage("Unable to CREATE table 'Rules_template': ".$db->acidErrorMessage(), 0);
            else
            {
                ErrorMessage("Successfully created 'Rules_template'", 1);
		$sql="INSERT INTO rules_template VALUES (1, 'Activate Default Active Snort Ruleset', 'Original enabled Snort signatures', 'yes', NULL, NULL)";
		$db->acidExecute($sql, -1, -1, false);
		$sql="INSERT INTO rules_template VALUES (2, 'Activate Default Inactive Snort Ruleset', 'Original disabled Snort signatures', 'yes', NULL, NULL)";
		$db->acidExecute($sql, -1, -1, false);
		$sql="INSERT INTO rules_template VALUES (3, 'Activate unconfirmed deleted rules', 'Old Rules that are removed by the snort developers.', 'yes', NULL, NULL)";
                $db->acidExecute($sql, -1, -1, false);
                if ($db->acidErrorMessage()  != "")
                    ErrorMessage("Unable to insert user: 'default templates': ".$db->acidErrorMessage(), 0);
            }
            $tblUsers_present = $db->acidTableExists("rules_template");
	}        
         
    }
     
     
    $db->acidClose();
    function ErrorMessage ($message, $err) {
        if ($err == 0)
            echo '<FONT COLOR="#FF0000">'.$message.'</FONT><BR>';
        else
            echo '<FONT COLOR="#FFFFFF">'.$message.'</FONT><BR>';
    }
?>

    
        
    
    
Return current item: SnortCenter 2.x