Martin Stransky 286aa4
#!/bin/sh
jvdias 4b4f5f
#  
jvdias 4b4f5f
#  This script uses the named D-BUS support, which must be enabled in
jvdias 4b4f5f
#  the running named with the named '-D' option, to get and print the
jvdias 4b4f5f
#  list of forwarding zones in the running server.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  It accepts an optional <zone> first argument which is the DNS name
jvdias 4b4f5f
#  of the zone whose forwarders (if any) will be retrieved.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  If no zone argument is specified, all forwarding zones will be listed.
jvdias 4b4f5f
#
Martin Stransky 286aa4
#  Usage: namedGetForwarders [-n -r] [ <zone> ]
jvdias 4b4f5f
#    -n : output forward zone statements for named.conf
jvdias 4b4f5f
#    -r : output in resolv.conf format
jvdias 4b4f5f
#       : no -r or -n: just list the forwarders
jvdias 4b4f5f
#
Martin Stransky 286aa4
#  This script is based on perl script of Jason Vas Dias <jvdias@redhat.com>. 
Martin Stransky 286aa4
#
Martin Stransky 286aa4
#  Copyright(C) Baris Cicek <baris@nerd.com.tr> Nerd Software. 2007
jvdias 4b4f5f
#
jvdias 4b4f5f
#  This program is free software; you can redistribute it and/or modify
jvdias 4b4f5f
#  it under the terms of the GNU General Public License as published by
jvdias 4b4f5f
#  the Free Software Foundation at
jvdias 4b4f5f
#           http://www.fsf.org/licensing/licenses/gpl.txt
jvdias 4b4f5f
#  and included in this software distribution as the "LICENSE" file.
jvdias 4b4f5f
#
jvdias 4b4f5f
#  This program is distributed in the hope that it will be useful,
jvdias 4b4f5f
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
jvdias 4b4f5f
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jvdias 4b4f5f
#  GNU General Public License for more details.
jvdias 4b4f5f
Martin Stransky 286aa4
declare -a zones;
Martin Stransky 286aa4
declare -a servers;
Martin Stransky 286aa4
declare -a ports;
Martin Stransky 286aa4
declare -a only; 
Martin Stransky 286aa4
Martin Stransky 286aa4
output_format='plain';
Martin Stransky 286aa4
zonecnt=0;
Martin Stransky 286aa4
Martin Stransky 286aa4
function push () {
Martin Stransky 286aa4
	local array 
Martin Stransky 286aa4
	array=( `echo $1` );
Martin Stransky 286aa4
	array[${#array[*]}]=$2;
Martin Stransky 286aa4
	echo ${array[@]};
Martin Stransky 286aa4
}
Martin Stransky 286aa4
Martin Stransky 286aa4
function concat () { 
Martin Stransky 286aa4
	local string
Martin Stransky 286aa4
	while [ $# -gt 0 ]; do
Martin Stransky 286aa4
		string=${string}$1;
Martin Stransky 286aa4
		shift;
Martin Stransky 286aa4
	done
Martin Stransky 286aa4
	echo $string;
Martin Stransky 286aa4
}
Martin Stransky 286aa4
Martin Stransky 286aa4
if  [ $# -ge 0 ]; then
Martin Stransky 286aa4
	if [ "$1" == "-r" ]; then
Martin Stransky 286aa4
		output_format='resolv';
Martin Stransky 286aa4
		shift;
Martin Stransky 286aa4
	elif [ "$1" == "-n" ]; then
Martin Stransky 286aa4
		output_format='named';
Martin Stransky 286aa4
		shift;
Martin Stransky 286aa4
	fi
Martin Stransky 286aa4
	zone="";
Martin Stransky 286aa4
	for arg in $*; do
Martin Stransky 286aa4
		zone=$(push "$zone" " string:'$arg'");
Martin Stransky 286aa4
	done
Martin Stransky 286aa4
fi 
jvdias 4b4f5f
Martin Stransky 286aa4
DNS=`/bin/dbus-send --system --type=method_call --print-reply --reply-timeout=20000 --dest=com.redhat.named /com/redhat/named com.redhat.named.text.GetForwarders $zone`;
jvdias 4b4f5f
Martin Stransky 286aa4
if [ $? -ne 0 ]; then
Martin Stransky 286aa4
	echo -e "dbus-send failed: $? $!";
Martin Stransky 286aa4
	exit 1;
Martin Stransky 286aa4
fi
jvdias 4b4f5f
Martin Stransky 286aa4
IFS=$'\n'
jvdias 4b4f5f
jvdias 4b4f5f
Martin Stransky 286aa4
for line in $DNS; do
Martin Stransky 286aa4
	match_ip=$( echo "$line" | awk --re-interval '{ match ($0, /([[:digit:]]{1,3})\.([[:digit:]]{1,3})\.([[:digit:]]{1,3})\.([[:digit:]]{1,3})/, a); printf "%s.%s.%s.%s", substr($0, a[1, "start"], a[1, "length"]), substr($0, a[2, "start"], a[2, "length"]), substr($0, a[3, "start"], a[3, "length"]), substr($0, a[4, "start"], a[4, "length"]);}' );
Martin Stransky 286aa4
	match_port=$( echo "$line" | awk '{ match ($0, /\"([[:digit:]]+)\"$/, a); printf "%s", substr($0, a[1, "start"], a[1,"length"]);}' );
Martin Stransky 286aa4
	match_string=$( echo "$line" | awk '{ match ($0, /string.+\"([^\"]+)\"$/, a); printf "%s", substr($0, a[1, "start"], a[1,"length"]);}' );
Martin Stransky 286aa4
	 
Martin Stransky 286aa4
	if [ "$match_ip" != "" ] && [ "$match_ip" != "..." ]; then
Martin Stransky 286aa4
		servers[$zonecnt]=$(push "${servers[$zonecnt]}" "$match_ip");
Martin Stransky 286aa4
	elif [ "$match_port" != "" ]; then 
Martin Stransky 286aa4
		ports[$zonecnt]=$(push "${ports[$zonecnt]}" "$match_port");
Martin Stransky 286aa4
	elif [ "$match_string" == "only" ]; then
Martin Stransky 286aa4
		only[$zonecnt]="1"; 
Martin Stransky 286aa4
	elif [ "$match_string" != "" ] && [ "$match_string" != "first" ]; then
Martin Stransky 286aa4
			zonecnt=$((zonecnt + 1));
Martin Stransky 286aa4
			zones[$zonecnt]="$match_string";
Martin Stransky 286aa4
	fi
Martin Stransky 286aa4
	
Martin Stransky 286aa4
done
jvdias 4b4f5f
Martin Stransky 286aa4
if [ "$output_format" == "resolv" ]; then
Martin Stransky 286aa4
# resolv.conf style:
Martin Stransky 286aa4
	search_line='search';
Martin Stransky 286aa4
	nameserver_lines='';
Martin Stransky 286aa4
	for index in $(seq 1 $zonecnt); do
Martin Stransky 286aa4
		if [ "` echo ${zones[$index]} | awk ' /\.in-addr\.arpa$/ { print $0 }'`" == '' ]; then
Martin Stransky 286aa4
			  search_line=$(push "$search_line" "${zones[$index]}");
Martin Stransky 286aa4
		fi 
Martin Stransky 286aa4
		IFS=$' ';
Martin Stransky 286aa4
		for ns in ${servers[$index]}; do
Martin Stransky 286aa4
		  nameserver_lines=$(concat "$nameserver_lines" "\nnameserver " "$ns");
Martin Stransky 286aa4
		done
Martin Stransky 286aa4
	done
Martin Stransky 286aa4
	echo -n $search_line;
Martin Stransky 286aa4
	echo -e $nameserver_lines;
Martin Stransky 286aa4
elif [ "$output_format" == "named" ]; then
Martin Stransky 286aa4
# named.conf style:
Martin Stransky 286aa4
	zone_lines='';
Martin Stransky 286aa4
	for index in $(seq 1 $zonecnt); do
Martin Stransky 286aa4
		zone_line=$(concat 'zone "' "${zones[$index]}" '." IN { type forward; forwarders { ');
Martin Stransky 286aa4
		srvcnt=1;
Martin Stransky 286aa4
		IFS=$' '; 
Martin Stransky 286aa4
		for ns in ${servers[$index]}; do
Martin Stransky 286aa4
			srvport=$(eval "echo ${ports[$index]} | awk '{ print \$${srvcnt} }'");
Martin Stransky 286aa4
			if [ "$srvport" != "53" ]; then
Martin Stransky 286aa4
				zone_line=$(concat "$zone_line" " $ns port $srvport;");
Martin Stransky 286aa4
			else 
Martin Stransky 286aa4
				zone_line=$(concat "$zone_line" " $ns;");
Martin Stransky 286aa4
			fi
Martin Stransky 286aa4
			srvcnt=$((srvcnt+1)); 
Martin Stransky 286aa4
		done
Martin Stransky 286aa4
		zone_line=$(concat "$zone_line" " };");
Martin Stransky 286aa4
		if [ "${only[$index]}" == '1' ]; then
Martin Stransky 286aa4
			zone_line=$(concat "$zone_line" " forward only;");
Martin Stransky 286aa4
		fi
Martin Stransky 286aa4
		zone_line=$(concat "$zone_line" " };");
Martin Stransky 286aa4
		zone_lines=$(concat "$zone_lines" "$zone_line\n");
Martin Stransky 286aa4
	done
Martin Stransky 286aa4
	echo -e ${zone_lines%\\n};
Martin Stransky 286aa4
elif [ "$output_format" == "plain" ]; then
Martin Stransky 286aa4
# just list:
Martin Stransky 286aa4
	output='';
Martin Stransky 286aa4
	for index in $(seq 1 $zonecnt); do
Martin Stransky 286aa4
		output=$(concat "$output" "${zones[$index]}" "\n");
Martin Stransky 286aa4
		if [ "${only[$index]}" == "1" ]; then
Martin Stransky 286aa4
			output=$(concat "$output" "\t" "forward only" "\n");
Martin Stransky 286aa4
		fi
Martin Stransky 286aa4
		srvcnt=1;
Martin Stransky 286aa4
		IFS=$' ';
Martin Stransky 286aa4
		for ns in ${servers[$index]}; do
Martin Stransky 286aa4
			srvport=$(eval "echo ${ports[$index]} | awk '{ print \$${srvcnt} }'");
Martin Stransky 286aa4
			if [ "$srvport" != "53" ]; then
Martin Stransky 286aa4
				output=$(concat "$output" "\t" "$ns:$srvport" "\n");
Martin Stransky 286aa4
			else
Martin Stransky 286aa4
				output=$(concat "$output" "\t" "$ns" "\n");
Martin Stransky 286aa4
			fi
Martin Stransky 286aa4
			srvcnt=$((srvcnt+1));
Martin Stransky 286aa4
		done	
Martin Stransky 286aa4
	done
Martin Stransky 286aa4
	echo -e ${output%\\n};
Martin Stransky 286aa4
fi