<?

/*
** SellaNMS
** $Id: reverses.php,v 1.8 2005/08/22 02:25:41 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

//NOTE: This is a hack script that generates DNS reverses. Modify as desired.

require_once('../config/config.php');

$domain = 'domain.com';
$email = 'hostmaster@domain.com';
$soa = 'ns1.domain.com';
$nameservers = array('ns1.domain.com', 'ns2.domain.com');

$con = @mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
if (!$con) {
	printf("Error: Connection to MySQL server '%s' failed.<BR>\n", $server);
 	return;
} else {
	if (!@mysql_select_db($config['databaseName'], $con)) {
		printf("Error: Connection to MySQL database failed.<BR>\n");
		return;
	}
}

/* Select Hostname, IP and Interface */
$sql = "SELECT element_data.value AS hostname, interface_data.value AS interface, ip_data.value AS ip FROM element, element_data, interface, interface_data, ip, ip_data WHERE (element.id = element_data.element AND element.id = interface.element AND interface.id = interface_data.interface AND interface.id = ip.interface AND ip.id = ip_data.ip) AND ip_data.variable LIKE '.1.3.6.1.2.1.4.20.1.1' AND interface_data.variable = '.1.3.6.1.2.1.2.2.1.2' AND element_data.variable = '.1.3.6.1.2.1.1.5.0' ORDER BY ip";

$result = mysql_query($sql, $con);
printf("<HTML><BODY BGCOLOR=\"ffffff\">\n");
if (@mysql_num_rows($result) < 1) {
	printf("<TABLE>\n<TR><TD COLSPAN=\"8\"><B>No data returned from select. Have you done a discovery?</B></TD></TR>\n</TABLE>\n");
} else {
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$reverse = $row['interface'];
		$reverse = str_replace('-aal5 layer', '', $reverse);
		$reverse = str_replace('ATM', 'at-', $reverse);
		$reverse = str_replace('POS', 'so-', $reverse);
//		$reverse = str_replace('fxp', 'fxp-', $reverse);
//		$reverse = str_replace('rif', 'rif-', $reverse);
		$reverse = ereg_replace('Serial(.*:.*)$', 't1-\1', $reverse);
		$reverse = ereg_replace('Hssi(.*:.*)$', 't1-\1', $reverse);
		$reverse = str_replace('Serial', 't3-', $reverse);
		$reverse = str_replace('Hssi', 't3-', $reverse);
		$reverse = str_replace('FastEthernet', 'fe-', $reverse);
		$reverse = str_replace('Ethernet', 'et-', $reverse);
		$reverse = str_replace('ethernet', 'et-', $reverse);
		$reverse = ereg_replace('[/\.:]', '-', $reverse);
//		$reverse = str_replace('lo', 'lo-', $reverse);
//		$reverse = str_replace('Loopback', 'lo-', $reverse);
		$reverse = str_replace('Loopback', 'lo', $reverse);
		$reverse = sprintf("%s-%s.%s.%s.", ereg_replace('^([a-z]+)-([a-z][a-z]+)-([0-9]+).*$', '\2-\3', strtolower($row['hostname'])), $reverse, ereg_replace('^([a-z]+)-.*$', '\1', strtolower($row['hostname'])), $domain);
		$ip = ereg_replace('[0-9]+\.[0-9]+\.[0-9]+\.(.*)$', '\1', $row['ip']);
		$block = ereg_replace('([0-9]+\.[0-9]+\.[0-9]+)\..*$', '\1', $row['ip']);
		$data[$block][$ip] = strtolower($reverse);
	}

	reset($data);
	while(list($block, $sub_data) = each($data)) {
		$file = "/tmp/" . $block . ".rev";
		$origin = ereg_replace('^([0-9]+)\.([0-9]+)\.([0-9]+)$', '\3.\2.\1.in-addr.arpa.', $block);

		$fp = fopen($file, 'w');
		$buf = '; $Id: reverses.php,v 1.8 2005/08/22 02:25:41 sella Exp $' . "\n" . '$ORIGIN ' . $origin . "\n" . '$TTL 600' . "\n\n";
		fwrite($fp, $buf);

		$buf = "@\t\tIN SOA\t" . $soa . ".\t" . ereg_replace('^([^@]*)@', '\1.', $email) . ". (\n\n";
		fwrite($fp, $buf);

		$buf = sprintf("\t\t%s00\t;Serial 1993,10,22,00th\n\t\t10800\t\t;Refresh\n\t\t3600\t\t;Retry\n\t\t604800\t\t;Expire\n\t\t600 )\t\t;TTL (negative)\n\n", date("Ymd"));
		fwrite($fp, $buf);

		reset($nameservers);
		while(list(, $ns) = each($nameservers)) {
			$buf = sprintf("\t\tIN NS\t\t%s.\n", $ns);
			fwrite($fp, $buf);
		}
		fwrite($fp, "\n");

		uksort($sub_data, "cmp_ip");

		reset($sub_data);
		while(list($ip, $reverse) = each($sub_data)) {
			$buf = sprintf("%s\t\tIN PTR\t\t%s\n", $ip, $reverse);
			fwrite($fp, $buf);
		}

		fclose($fp);
		echo "Wrote: $file<BR>\n";
	}

}
printf("</BODY></HTML>\n");

function cmp_ip($a, $b) {
	if ($a == $b)
		return 0;
	$x = ereg_replace('^[0-9]+\.[0-9]+\.[0-9]+\.', '', $a);
	$y = ereg_replace('^[0-9]+\.[0-9]+\.[0-9]+\.', '', $b);
	if ($x > $y)
		return 1;
	return -1;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/

?>
