:: Exporting NicTool to BIND

2 min read

:: What is
+ NicTool
NicTool is a free DNS management suite that takes all the headaches out of managing DNS. It includes an attractive web interface for users, admins, and clients to access and update their DNS zone data as well as a rich API for provisioning systems to interact with. All zone data is stored in MySQL and is extracted by export scripts to the DNS server of choice (tinydns, BIND, PowerDNS). :: NicTool

+ BIND
BIND is the most widely used DNS software on the Internet. On Unix-like operating systems it is the de facto standard. :: wikipedia
 
:: Installing Requirement
+ Installing Bind on Linux Debian

aptitude install bind9

+ Installing NicTool
 
:: Create script for Export to BIND

mkdir /exportscript
cd /exportscript/
vi export_bind.php

add these line:

<?php

/* nictool bind export script  - created by Mihai Secasiu - //patchlog.com */

/* Version 1.3
BUGS FIXED:
1. SOA records were inverted
2. in-addr.arpa zones were not being generated correctly
3. Fixed a major bug where an empty zones.conf file was generated if the export script was unable to connect to MySQL (very bad as it means no zone data loaded or served!!)
*/


$db_name="nictool";
$db_user="nictool";
$db_pass="nictool";
$db_host="localhost";


$zones_dir="data/"; // directory used for storing the zone files, the script will delete the zone that are set as deleted from this folder.
$zone_template="{ZONE_NAME}.zone";  //  template used for zone filename. {ZONE_NAME} will be replaced by script with the domain name
$conf_file="data/zones.conf"; // file where the script will write the zone entries. The script will truncate the file on open



if($zones_dir[strlen($zones_dir)-1]!='/'){
	$zones_dir.='/';
}

if(!($cf=fopen("$conf_file.tmp",'w'))){
	die("cannot open configuration file: $conf_filen");
}

function get_ns($nsid)
{
	global $nameservers;
	if(isset($nameservers[$nsid]))return $nameservers[$nsid];
	$sql="SELECT name,ttl,description FROM nt_nameserver where nt_nameserver_id=".intval($nsid)." and deleted!='1'";
	if(!($res=mysql_query($sql))){
		die("query: $sql: failed with error: ".mysql_error()."n");
	}
	$nameservers[$nsid]=mysql_fetch_array($res);
	return $nameservers[$nsid];
}


if(!($dbl=mysql_connect($db_host,$db_user,$db_pass))){
	die("cound not connect to the database servern");
}

mysql_select_db($db_name, $dbl) or die("Could not select database: $db_namen");


if(count($argv)>1){
	if(get_ns($argv[1])){
		$ns=$argv[1];
		$nsq="AND (ns0 = $ns OR ns1 = $ns OR ns2 = $ns OR ns3 = $ns OR ns4 = $ns OR ns5 = $ns
			OR ns6 = $ns OR ns7 = $ns OR ns8 = $ns OR ns9 = $ns) ";
	}else{
		die("nameserver id invalid or nameserver deletedn");
	}

}else{
	$nsq="";
}


$c=0;
$sql="SELECT * FROM nt_zone $nsq";
if(!($res=mysql_query($sql))){
	die("query: $sql: failed with error: ".mysql_error()."n");
}
while($z=mysql_fetch_assoc($res)){

	$fn=$zones_dir.preg_replace('/{ZONE_NAME}/',$z['zone'],$zone_template);
	if($z['deleted']=="1"){
		if(is_file($fn)){
			unlink($fn);
			echo "Deleted zone file for zone ${z['zone']}n";
		}
		continue;
	}

	$nst=""; // nameserver text
	$fns=""; // first nameserver for SOA record
	for($i=0;$i<=9;$i++){
		if($z['ns'.$i] && ($ns=get_ns($z['ns'.$i])) ){
			$nst.="@	${ns['ttl']} IN NS	${ns['name']} ; ${ns['description']}n";
			if($fns=="")$fns=$ns['name'];
		}
	}

	echo "Generating zone file for domain: ".$z['zone']." -";
	$t="; ${z['description']}
$ttl ${z['ttl']}
@	IN	SOA	$fns	${z['mailaddr']} (
			${z['serial']}
			${z['refresh']}
			${z['retry']}
			${z['expire']}
			${z['minimum']} )
";
	$t.=$nst;


	$sql="SELECT * FROM nt_zone_record where nt_zone_id=".$z['nt_zone_id']. " and deleted!='1'";

	if(!($rres=mysql_query($sql))){
		die("query: $sql: failed with error: ".mysql_error()."n");
	}
	while($r=mysql_fetch_assoc($rres)){
		$t.="${r['name']} ${r['ttl']} IN ${r['type']}";
		if(in_array($r['type'],array('A','AAAA','NS','CNAME','PTR'))){
			$t.="	${r['address']}";
		}elseif($r['type']=='TXT'){
			$t.="	"${r['address']}"";
		}elseif($r['type']=='MX'){
			$t.="	${r['weight']} ${r['address']}";
		}elseif($r['type']=='SRV'){
			$t.="	${r['priority']} ${r['weight']} ${r['other']} ${r['address']}";
		}
		$t.="	; ${r['description']}n";
	}
	if(!file_put_contents($fn,$t)){
		die("error writing to file: $fnn");
	}
	$ze="
zone "${z['zone']}" IN {
	type master;
	file "$fn";
	allow-update {none; };
	notify no;
};
";

	if(!fwrite($cf,$ze)){
		die("error writing to zones configuration file: $conf_filen");
	}


	echo " file generated OKn";
	$c++;
}

fclose($cf);
rename("$conf_file.tmp",$conf_file);

echo "Successfully exported $c domains. Exiting..n";

 
:: Setup crontab for exporting zone file
+ Create zonecopy.sh

cd /exportscript/
vi zonecopy.sh

add these line:

#!/bin/bash

#######
# exporting
############
cd /exportscript/
/usr/bin/php export_bind.php

##########
# copy zone file
################

# local server
scp data/* /var/cache/bind/data

# remotely
scp data/* [email protected]:/var/cache/bind/data
scp data/* [email protected]:/var/cache/bind/data

#########
# reload
##########
# local
killall -HUP named

# remotely
ssh [email protected] killall -HUP named
ssh [email protected] killall -HUP named

sleep 5

+ Setup crontab

crontab -e

setup like this:

*/5 * * * * /bin/sh /exportscript/zonecopy.sh >/dev/null 1>/dev/null

 
:: Create sub folder in the directory that your script is located in called "data"

cd /exportscript
mkdir data
chown bind.bind data

 
:: Add zones.conf to named.conf

vi /etc/bind/named.conf

add this line:

include "/exportscript/data/zones.conf";

 
:: Setup working directory and users
+ Create directory for exporting

cd /var/cache/bind
mkdir data
chown bind.bind data

+ Modify shell on bind users

vi /etc/passwd

change on this line, like:

bind:x:111:119::/var/cache/bind:/bin/sh

 
:: Testing
Add some DNS zone on NicToolClient, and testing your zone record useing dig, nslookup, host or other domain tool checker.
 
:: Links
+ Google
+ LinuxPower
+ TNPI

How to fix problem with the editor ‘vi’

:: git error with vi error: There was a problem with the editor 'vi'   :: how to fix the problem 0. check path...
Sysadmin.ID
10 sec read

Apache :: MP4 Streaming

:: How to Enable mp4 Streaming in Apache + Installing mod h624 streaming wget //h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gz tar -xzf apache_mod_h264_streaming-2.2.7.tar.gz cd mod_h264_streaming-2.2.7/ ./configure --with-apxs=`which apxs2` make...
Sysadmin.ID
18 sec read

Apache :: Flash / FLV Streaming

:: How to Configure Apache for FLV Streaming + Installing mod_flvx wget //people.apache.org/~pquerna/modules/mod_flvx.c which apxs /usr/bin/apxs /usr/bin/apxs -cia mod_flvx.c + setup mod_flvx in http.conf...
Sysadmin.ID
15 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *