1219732Sume#!/bin/sh
2282434Sgjb# Copyright (c) 2007-2012 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
32282434Sgjb[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33225524ShrsNL="
34225524Shrs"
35219732Sume
36219732Sume# Platform specific kludges
37219732Sumeif [ -z "$named_service" -a -z "$named_restart" -a \
38219732Sume	-d "@RCDIR@" -a ! -x "@RCDIR@"/named ]
39219732Sumethen
40219732Sume	if [ -x "@RCDIR@"/bind9 ]; then
41219732Sume		# Debian and derivatives
42219732Sume		named_service=bind9
43282434Sgjb	elif [ -x "@RCDIR@"/rc.bind ]; then
44282434Sgjb		# Slackware
45282434Sgjb		named_service=rc.bind
46219732Sume	fi
47219732Sumefi
48219732Sume: ${named_service:=named}
49219732Sume: ${named_restart:=@RESTARTCMD ${named_service}@}
50225524Shrsnewoptions="# Generated by resolvconf$NL"
51219732Sumenewzones="$newoptions"
52219732Sume
53219732Sumeforward=
54219732Sumefor n in $NAMESERVERS; do
55219732Sume	case "$forward" in
56225524Shrs	*"$NL	$n;"*);;
57225524Shrs	*) forward="$forward$NL	$n;";;
58219732Sume	esac
59219732Sumedone
60219732Sumeif [ -n "$forward" ]; then
61225524Shrs	newoptions="${newoptions}forward first;${NL}forwarders {$forward${NL}};$NL"
62219732Sumefi
63219732Sume
64219732Sumefor d in $DOMAINS; do
65225524Shrs	newzones="${newzones}zone \"${d%%:*}\" {$NL"
66225524Shrs	newzones="$newzones	type forward;$NL"
67225524Shrs	newzones="$newzones	forward first;$NL	forwarders {$NL"
68219732Sume	ns="${d#*:}"
69219732Sume	while [ -n "$ns" ]; do
70225524Shrs		newzones="$newzones		${ns%%,*};$NL"
71219732Sume		[ "$ns" = "${ns#*,}" ] && break
72219732Sume		ns="${ns#*,}"
73219732Sume	done
74225524Shrs	newzones="$newzones	};$NL};$NL"
75219732Sumedone
76219732Sume
77282434Sgjb# Try to ensure that config dirs exist
78282434Sgjbif type config_mkdirs >/dev/null 2>&1; then
79282434Sgjb	config_mkdirs "$named_options" "$named_zones"
80282434Sgjbelse
81282434Sgjb	@SBINDIR@/resolvconf -D "$named_options" "$named_zones"
82282434Sgjbfi
83282434Sgjb
84219732Sume# No point in changing files or reloading bind if the end result has not
85219732Sume# changed
86219732Sumechanged=false
87219732Sumeif [ -n "$named_options" ]; then
88219732Sume	if [ ! -f "$named_options" ] || \
89225524Shrs		[ "$(cat "$named_options")" != "$(printf %s "$newoptions")" ]
90219732Sume	then
91225524Shrs		printf %s "$newoptions" >"$named_options"
92219732Sume		changed=true
93219732Sume	fi
94219732Sumefi
95219732Sumeif [ -n "$named_zones" ]; then
96219732Sume	if [ ! -f "$named_zones" ] || \
97225524Shrs		[ "$(cat "$named_zones")" != "$(printf %s "$newzones")" ]
98219732Sume	then
99225524Shrs		printf %s "$newzones" >"$named_zones"
100219732Sume		changed=true
101219732Sume	fi
102219732Sumefi
103219732Sume
104219732Sumeif $changed; then
105219732Sume	eval $named_restart
106219732Sumefi
107