Location: PHPKode > projects > Cancerbero - The watchdog of the ports > cancerbero-0.6/site/include/update_1_2.php
<?php

# Script to change a cancerbero's database version 1 to version 2

include_once("dbconnect.php");
include_once("net.php");

$database_ver = 2;

$upgrade_version = mysql_query("UPDATE `configuration` SET `database_ver` = ( -1 ) * `database_ver` WHERE 1") or die (mysql_error());

#$query =  mysql_query ("DELETE FROM `configuration` WHERE `cancerbero_ver` != '0.4'") or die (mysql_error());
	
$query = mysql_query ("CREATE TABLE IF NOT EXISTS `ranges` (
			`range_id` int(3) NOT NULL auto_increment,
			`sensor_id` int(3) NOT NULL default '0',
			`range` varchar(18) NOT NULL default '',
			`range_delay_scan` int(6) unsigned NOT NULL DEFAULT '15',
			`range_nmap_opts` INT ( 3 ) NULL,
	
			PRIMARY KEY  (`range_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die (mysql_error());	
	
$query = mysql_query ("INSERT INTO `ranges` ( `range` ) SELECT `range` FROM `configuration`") or die (mysql_error());	
$query = mysql_query ("UPDATE `ranges` SET `range_delay_scan` = (SELECT `delay_scan` FROM `configuration`) WHERE `range_id` = '1'") or die (mysql_error());	
$query = mysql_query ("UPDATE `ranges` SET `range_nmap_opts` = '5' WHERE `range_id` = '1'") or die (mysql_error());	
$query = mysql_query ("UPDATE `ranges` SET `sensor_id` = '1' WHERE `range_id` = '1'") or die (mysql_error());	

$query = mysql_query ("ALTER TABLE `configuration` DROP `cancerbero_ver`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` DROP `range`") or die (mysql_error());	
$query = mysql_query ("ALTER TABLE `configuration` DROP `white_list`") or die (mysql_error());	
$query = mysql_query ("ALTER TABLE `configuration` DROP `loop`") or die (mysql_error());	
$query = mysql_query ("ALTER TABLE `configuration` DROP `delay_scan`") or die (mysql_error());	
$query = mysql_query ("ALTER TABLE `configuration` DROP `sweep_time`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` DROP `max_num_scan`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` DROP `debug`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` DROP `nmap_sweep_opts`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` DROP `nmap_opts`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` ADD PRIMARY KEY ( `database_ver` )") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` CHANGE `database_ver` `database_ver` INT ( 1 ) NOT NULL DEFAULT '1' FIRST") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` ADD `gui_debug` INT( 1 ) NOT NULL DEFAULT '-1'") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` ADD `num_rows` INT( 3 ) NOT NULL DEFAULT '10'") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `configuration` ADD `num_links` INT( 3 ) NOT NULL DEFAULT '7'") or die (mysql_error());

$query = mysql_query ("TRUNCATE TABLE `configuration`") or die (mysql_error());
$query = mysql_query ("INSERT INTO `configuration` (`database_ver`) VALUES (2)") or die (mysql_error());

$query = mysql_query ("ALTER TABLE `hosts` ADD `range_id` INT( 3 ) NOT NULL DEFAULT '0'") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `hosts` CHANGE `priority` `priority` INT( 1 ) NOT NULL DEFAULT '6'") or die (mysql_error());
$query = mysql_query ("UPDATE `hosts` SET `priority` = '6'") or die (mysql_error());
$query = mysql_query ("UPDATE `hosts` SET `range_id` = '1'") or die (mysql_error());

$query = mysql_query ("ALTER TABLE `scans` ADD `range_id` INT( 3 ) DEFAULT '0' NOT NULL AFTER `host_id`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `scans` ADD `os_gen` VARCHAR( 100 ) DEFAULT NULL AFTER `os_guessed`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `scans` ADD KEY ( `range_id` )") or die (mysql_error());

$query = mysql_query ("UPDATE `scans` SET `range_id` = '1'") or die (mysql_error());

$query = mysql_query ("ALTER TABLE `ports` ADD `range_id` INT( 3 ) DEFAULT '0' NOT NULL AFTER `scan_rec_id` ") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD `product` VARCHAR( 255 ) DEFAULT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD `extra_info` VARCHAR( 255 ) DEFAULT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD `port_version` VARCHAR( 255 ) DEFAULT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD `service_confidence` VARCHAR( 255 ) DEFAULT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD `fingerprint` VARCHAR( 255 ) DEFAULT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` ADD KEY ( `range_id` )") or die (mysql_error());

$query = mysql_query ("UPDATE ports SET product = fullversion") or die (mysql_error());

$query = mysql_query ("ALTER TABLE `ports` DROP `fullversion") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `ports` DROP `state`") or die (mysql_error());

$query = mysql_query ("UPDATE `ports` SET `range_id` = '1'") or die (mysql_error());

$query = mysql_query ("CREATE TABLE IF NOT EXISTS `nmap_cfg` (
			`nmap_cfg_id` int(2) NOT NULL auto_increment,
			`nmap_type_cfg` ENUM( 'Sweep', 'Scan' ) NOT NULL,
			`nmap_option_name` VARCHAR( 32 ) NOT NULL,
			`nmap_cmd_cfg` varchar(255) NOT NULL default '',
	
			PRIMARY KEY  (`nmap_cfg_id`),
			UNIQUE (`nmap_type_cfg` ,`nmap_option_name`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die (mysql_error());
	
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Sweep','Polite','-A -T4 -PA445,22 -PS445,22 -p -')") or die (mysql_error());
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Sweep','Normal','-A -r -T5 -PA445,22 -PS445,22 -PE -p -')") or die (mysql_error());
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Sweep','Aggressive','-A -r -PA445,22 -PS445,22 -PE -p - --max_rtt_timeout 100 --min_rtt_timeout 10 --host_timeout 900000 --initial_rtt_timeout 50 --max_scan_delay 5')") or die (mysql_error());
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Scan','Polite','-sP -PA445,22 -PS445,22')") or die (mysql_error());
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Scan','Normal','-sP -T5 -PA445,22 -PS445,22')") or die (mysql_error());
$query = mysql_query ("INSERT INTO `nmap_cfg` (`nmap_type_cfg`,`nmap_option_name`,`nmap_cmd_cfg`) VALUES ('Scan','Aggressive','-sP -PA445,22 -PS445,22 -PE --min_parallelism 100 --max_rtt_timeout 100 --min_rtt_timeout 10 --initial_rtt_timeout 50')") or die (mysql_error());

$query = mysql_query ("ALTER TABLE `program_values` DROP `cancerbero_ver`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `program_values` ADD `sensor_id` INT( 3 ) NOT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `program_values` ADD `status_sweep` VARCHAR( 25 ) NULL AFTER `saw_alive`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `program_values` ADD PRIMARY KEY (`sensor_id`)") or die (mysql_error());

$query = mysql_query ("TRUNCATE TABLE `program_values`") or die (mysql_error());

$query = mysql_query ("CREATE TABLE IF NOT EXISTS `sensors` (
			`sensor_id` int(3) NOT NULL auto_increment,
			`sensor_name` varchar(40) NOT NULL default '',
			`cancerbero_sensor_ver` varchar(8) NOT NULL default '0',
			`sensor_loop` int(1) unsigned default '1',
			`sensor_delay_scan` int(6) unsigned default '15',
			`sensor_sweep_time` int(6) unsigned default '3600',
			`sensor_max_num_scan` int(6) unsigned default '5',
			`sensor_stored_scans` int(6) unsigned default '10',
			`sensor_debug` int(1) default '-1',
			`sensor_nmap_sweep_opts` int(3) default '2',
			`sensor_nmap_opts` int(3) default '5',
	
			PRIMARY KEY  (`sensor_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die (mysql_error());
	
$query = mysql_query ("CREATE TABLE IF NOT EXISTS `white_list` (
			`white_list_id` int(3) NOT NULL auto_increment,
			`range_id` int(3) NOT NULL default '0',
			`white_list` varchar(18) NOT NULL default '',
			`comment` varchar(255) default NULL,
	
			PRIMARY KEY  (`white_list_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die (mysql_error());
	
$query = mysql_query ("ALTER TABLE `users` CHANGE `user_rec_id` `mail_rec_id` INT( 21 ) UNSIGNED NOT NULL AUTO_INCREMENT") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `users` CHANGE `email` `email` VARCHAR( 100 ) NOT NULL") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `users` CHANGE `num_mails` `date_mail` INT( 10 ) NOT NULL DEFAULT '0'") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `users` ADD `range_id` INT( 3 ) DEFAULT '0' NOT NULL AFTER `host_id`") or die (mysql_error());
$query = mysql_query ("ALTER TABLE `users` ADD KEY ( `range_id` ) ") or die (mysql_error());

$query = mysql_query ("UPDATE `users` SET `range_id` = '1'") or die (mysql_error());

$query = mysql_query ("CREATE TABLE IF NOT EXISTS `logs` (
			`logs_id` INT( 21 ) NOT NULL AUTO_INCREMENT ,
			`range_id` INT( 3 ) ,
			`sensor_id` INT( 3 ) ,
			`name_proc` VARCHAR( 32 ) ,
			`facility` VARCHAR( 32 ) ,
			`priority` VARCHAR( 32 ) ,
			`date` INT( 10 ) ,
			`message` VARCHAR( 255 ) ,
	
			PRIMARY KEY ( `logs_id` )
			) TYPE = MYISAM DEFAULT CHARSET=latin1") or die (mysql_error());
	
	
$query = mysql_query ("SELECT range FROM ranges WHERE range_id = 1") or die (mysql_error());
$select_range = mysql_fetch_assoc($query);
$all_ips = list_cidr ($select_range['range']);

foreach ($all_ips as $ip) {
	$query = mysql_query ("SELECT host_id FROM hosts WHERE ip = '$ip'") or die (mysql_error());	
	$host_id = mysql_fetch_assoc ($query);
	if ($host_id['host_id'] == '') {
		$insert_ip = mysql_query ("INSERT INTO hosts (ip, range_id) VALUES ('$ip', 1)") or die (mysql_error());
	}
}

$upgrade_version = mysql_query("UPDATE configuration SET database_ver= $database_ver") or die (mysql_error());

?>
Return current item: Cancerbero - The watchdog of the ports