landgraf / rpms / bind

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