e4f5cd6
#!/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
#
e4f5cd6
#  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
#
e4f5cd6
#  This script is based on perl script of Jason Vas Dias <jvdias@redhat.com>. 
e4f5cd6
#
e4f5cd6
#  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
e4f5cd6
declare -a zones;
e4f5cd6
declare -a servers;
e4f5cd6
declare -a ports;
e4f5cd6
declare -a only; 
e4f5cd6
e4f5cd6
output_format='plain';
e4f5cd6
zonecnt=0;
e4f5cd6
e4f5cd6
function push () {
e4f5cd6
	local array 
e4f5cd6
	array=( `echo $1` );
e4f5cd6
	array[${#array[*]}]=$2;
e4f5cd6
	echo ${array[@]};
e4f5cd6
}
e4f5cd6
e4f5cd6
function concat () { 
e4f5cd6
	local string
e4f5cd6
	while [ $# -gt 0 ]; do
e4f5cd6
		string=${string}$1;
e4f5cd6
		shift;
e4f5cd6
	done
e4f5cd6
	echo $string;
e4f5cd6
}
e4f5cd6
e4f5cd6
if  [ $# -ge 0 ]; then
e4f5cd6
	if [ "$1" == "-r" ]; then
e4f5cd6
		output_format='resolv';
e4f5cd6
		shift;
e4f5cd6
	elif [ "$1" == "-n" ]; then
e4f5cd6
		output_format='named';
e4f5cd6
		shift;
e4f5cd6
	fi
e4f5cd6
	zone="";
e4f5cd6
	for arg in $*; do
e4f5cd6
		zone=$(push "$zone" " string:'$arg'");
e4f5cd6
	done
e4f5cd6
fi 
jvdias 4b4f5fd
e4f5cd6
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
e4f5cd6
if [ $? -ne 0 ]; then
e4f5cd6
	echo -e "dbus-send failed: $? $!";
e4f5cd6
	exit 1;
e4f5cd6
fi
jvdias 4b4f5fd
e4f5cd6
IFS=$'\n'
jvdias 4b4f5fd
jvdias 4b4f5fd
e4f5cd6
for line in $DNS; do
e4f5cd6
	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"]);}' );
e4f5cd6
	match_port=$( echo "$line" | awk '{ match ($0, /\"([[:digit:]]+)\"$/, a); printf "%s", substr($0, a[1, "start"], a[1,"length"]);}' );
e4f5cd6
	match_string=$( echo "$line" | awk '{ match ($0, /string.+\"([^\"]+)\"$/, a); printf "%s", substr($0, a[1, "start"], a[1,"length"]);}' );
e4f5cd6
	 
e4f5cd6
	if [ "$match_ip" != "" ] && [ "$match_ip" != "..." ]; then
e4f5cd6
		servers[$zonecnt]=$(push "${servers[$zonecnt]}" "$match_ip");
e4f5cd6
	elif [ "$match_port" != "" ]; then 
e4f5cd6
		ports[$zonecnt]=$(push "${ports[$zonecnt]}" "$match_port");
e4f5cd6
	elif [ "$match_string" == "only" ]; then
e4f5cd6
		only[$zonecnt]="1"; 
e4f5cd6
	elif [ "$match_string" != "" ] && [ "$match_string" != "first" ]; then
e4f5cd6
			zonecnt=$((zonecnt + 1));
e4f5cd6
			zones[$zonecnt]="$match_string";
e4f5cd6
	fi
e4f5cd6
	
e4f5cd6
done
jvdias 4b4f5fd
e4f5cd6
if [ "$output_format" == "resolv" ]; then
e4f5cd6
# resolv.conf style:
e4f5cd6
	search_line='search';
e4f5cd6
	nameserver_lines='';
e4f5cd6
	for index in $(seq 1 $zonecnt); do
e4f5cd6
		if [ "` echo ${zones[$index]} | awk ' /\.in-addr\.arpa$/ { print $0 }'`" == '' ]; then
e4f5cd6
			  search_line=$(push "$search_line" "${zones[$index]}");
e4f5cd6
		fi 
e4f5cd6
		IFS=$' ';
e4f5cd6
		for ns in ${servers[$index]}; do
e4f5cd6
		  nameserver_lines=$(concat "$nameserver_lines" "\nnameserver " "$ns");
e4f5cd6
		done
e4f5cd6
	done
e4f5cd6
	echo -n $search_line;
e4f5cd6
	echo -e $nameserver_lines;
e4f5cd6
elif [ "$output_format" == "named" ]; then
e4f5cd6
# named.conf style:
e4f5cd6
	zone_lines='';
e4f5cd6
	for index in $(seq 1 $zonecnt); do
e4f5cd6
		zone_line=$(concat 'zone "' "${zones[$index]}" '." IN { type forward; forwarders { ');
e4f5cd6
		srvcnt=1;
e4f5cd6
		IFS=$' '; 
e4f5cd6
		for ns in ${servers[$index]}; do
e4f5cd6
			srvport=$(eval "echo ${ports[$index]} | awk '{ print \$${srvcnt} }'");
e4f5cd6
			if [ "$srvport" != "53" ]; then
e4f5cd6
				zone_line=$(concat "$zone_line" " $ns port $srvport;");
e4f5cd6
			else 
e4f5cd6
				zone_line=$(concat "$zone_line" " $ns;");
e4f5cd6
			fi
e4f5cd6
			srvcnt=$((srvcnt+1)); 
e4f5cd6
		done
e4f5cd6
		zone_line=$(concat "$zone_line" " };");
e4f5cd6
		if [ "${only[$index]}" == '1' ]; then
e4f5cd6
			zone_line=$(concat "$zone_line" " forward only;");
e4f5cd6
		fi
e4f5cd6
		zone_line=$(concat "$zone_line" " };");
e4f5cd6
		zone_lines=$(concat "$zone_lines" "$zone_line\n");
e4f5cd6
	done
e4f5cd6
	echo -e ${zone_lines%\\n};
e4f5cd6
elif [ "$output_format" == "plain" ]; then
e4f5cd6
# just list:
e4f5cd6
	output='';
e4f5cd6
	for index in $(seq 1 $zonecnt); do
e4f5cd6
		output=$(concat "$output" "${zones[$index]}" "\n");
e4f5cd6
		if [ "${only[$index]}" == "1" ]; then
e4f5cd6
			output=$(concat "$output" "\t" "forward only" "\n");
e4f5cd6
		fi
e4f5cd6
		srvcnt=1;
e4f5cd6
		IFS=$' ';
e4f5cd6
		for ns in ${servers[$index]}; do
e4f5cd6
			srvport=$(eval "echo ${ports[$index]} | awk '{ print \$${srvcnt} }'");
e4f5cd6
			if [ "$srvport" != "53" ]; then
e4f5cd6
				output=$(concat "$output" "\t" "$ns:$srvport" "\n");
e4f5cd6
			else
e4f5cd6
				output=$(concat "$output" "\t" "$ns" "\n");
e4f5cd6
			fi
e4f5cd6
			srvcnt=$((srvcnt+1));
e4f5cd6
		done	
e4f5cd6
	done
e4f5cd6
	echo -e ${output%\\n};
e4f5cd6
fi