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