named.in revision 219732
1219732Sume#!/bin/sh
2219732Sume# Copyright (c) 2007-2009 Roy Marples
3219732Sume# All rights reserved
4219732Sume
5219732Sume# named subscriber for resolvconf
6219732Sume
7219732Sume# Redistribution and use in source and binary forms, with or without
8219732Sume# modification, are permitted provided that the following conditions
9219732Sume# are met:
10219732Sume#     * Redistributions of source code must retain the above copyright
11219732Sume#       notice, this list of conditions and the following disclaimer.
12219732Sume#     * Redistributions in binary form must reproduce the above
13219732Sume#       copyright notice, this list of conditions and the following
14219732Sume#       disclaimer in the documentation and/or other materials provided
15219732Sume#       with the distribution.
16219732Sume#
17219732Sume# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18219732Sume# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19219732Sume# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20219732Sume# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21219732Sume# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22219732Sume# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23219732Sume# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219732Sume# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219732Sume# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219732Sume# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27219732Sume# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219732Sume
29219732Sume[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30219732Sume. "@SYSCONFDIR@/resolvconf.conf" || exit 1
31219732Sume[ -z "$named_zones" -a -z "$named_options" ] && exit 0
32219732Sume[ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
33219732Sume
34219732Sume# Platform specific kludges
35219732Sumeif [ -z "$named_service" -a -z "$named_restart" -a \
36219732Sume	-d "@RCDIR@" -a ! -x "@RCDIR@"/named ]
37219732Sumethen
38219732Sume	if [ -x "@RCDIR@"/bind9 ]; then
39219732Sume		# Debian and derivatives
40219732Sume		named_service=bind9
41219732Sume	fi
42219732Sumefi
43219732Sume: ${named_service:=named}
44219732Sume: ${named_restart:=@RESTARTCMD ${named_service}@}
45219732Sumenewoptions="# Generated by resolvconf\n"
46219732Sumenewzones="$newoptions"
47219732Sume
48219732Sumeforward=
49219732Sumefor n in $NAMESERVERS; do
50219732Sume	case "$forward" in
51219732Sume	*"\n\t$n;"*);;
52219732Sume	*) forward="$forward\n\t$n;";;
53219732Sume	esac
54219732Sumedone
55219732Sumeif [ -n "$forward" ]; then
56219732Sume	newoptions="${newoptions}forward first;\nforwarders {$forward\n};\n"
57219732Sumefi
58219732Sume
59219732Sumefor d in $DOMAINS; do
60219732Sume	newzones="${newzones}zone \"${d%%:*}\" {\n"
61219732Sume	newzones="$newzones\ttype forward;\n"
62219732Sume	newzones="$newzones\tforward first;\n\tforwarders {\n"
63219732Sume	ns="${d#*:}"
64219732Sume	while [ -n "$ns" ]; do
65219732Sume		newzones="$newzones\t\t${ns%%,*};\n"
66219732Sume		[ "$ns" = "${ns#*,}" ] && break
67219732Sume		ns="${ns#*,}"
68219732Sume	done
69219732Sume	newzones="$newzones\t};\n};\n"
70219732Sumedone
71219732Sume
72219732Sume# No point in changing files or reloading bind if the end result has not
73219732Sume# changed
74219732Sumechanged=false
75219732Sumeif [ -n "$named_options" ]; then
76219732Sume	if [ ! -f "$named_options" ] || \
77219732Sume		[ "$(cat "$named_options")" != "$(printf "$newoptions")" ]
78219732Sume	then
79219732Sume		printf "$newoptions" >"$named_options"
80219732Sume		changed=true
81219732Sume	fi
82219732Sumefi
83219732Sumeif [ -n "$named_zones" ]; then
84219732Sume	if [ ! -f "$named_zones" ] || \
85219732Sume		[ "$(cat "$named_zones")" != "$(printf "$newzones")" ]
86219732Sume	then
87219732Sume		printf "$newzones" >"$named_zones"
88219732Sume		changed=true
89219732Sume	fi
90219732Sumefi
91219732Sume
92219732Sumeif $changed; then
93219732Sume	eval $named_restart
94219732Sumefi
95