1219732Sume#!/bin/sh
2304515Spfg# Copyright (c) 2007-2016 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 \
38304515Spfg	-d "$RCDIR" -a ! -x "$RCDIR"/named ]
39219732Sumethen
40304515Spfg	if [ -x "$RCDIR"/bind9 ]; then
41219732Sume		# Debian and derivatives
42219732Sume		named_service=bind9
43304515Spfg	elif [ -x "$RCDIR"/rc.bind ]; then
44282434Sgjb		# Slackware
45282434Sgjb		named_service=rc.bind
46219732Sume	fi
47219732Sumefi
48219732Sume: ${named_service:=named}
49304515Spfg
50304515Spfg: ${named_pid:=/var/run/$named_service.pid}
51304515Spfg[ -s "$named_pid" ] || named_pid=/var/run/$named_service/$named_service.pid
52304515Spfg[ -s "$named_pid" ] || unset named_pid
53304515Spfg
54225524Shrsnewoptions="# Generated by resolvconf$NL"
55219732Sumenewzones="$newoptions"
56219732Sume
57219732Sumeforward=
58219732Sumefor n in $NAMESERVERS; do
59219732Sume	case "$forward" in
60225524Shrs	*"$NL	$n;"*);;
61225524Shrs	*) forward="$forward$NL	$n;";;
62219732Sume	esac
63219732Sumedone
64219732Sumeif [ -n "$forward" ]; then
65225524Shrs	newoptions="${newoptions}forward first;${NL}forwarders {$forward${NL}};$NL"
66219732Sumefi
67219732Sume
68219732Sumefor d in $DOMAINS; do
69225524Shrs	newzones="${newzones}zone \"${d%%:*}\" {$NL"
70225524Shrs	newzones="$newzones	type forward;$NL"
71225524Shrs	newzones="$newzones	forward first;$NL	forwarders {$NL"
72219732Sume	ns="${d#*:}"
73219732Sume	while [ -n "$ns" ]; do
74225524Shrs		newzones="$newzones		${ns%%,*};$NL"
75219732Sume		[ "$ns" = "${ns#*,}" ] && break
76219732Sume		ns="${ns#*,}"
77219732Sume	done
78225524Shrs	newzones="$newzones	};$NL};$NL"
79219732Sumedone
80219732Sume
81282434Sgjb# Try to ensure that config dirs exist
82282434Sgjbif type config_mkdirs >/dev/null 2>&1; then
83282434Sgjb	config_mkdirs "$named_options" "$named_zones"
84282434Sgjbelse
85282434Sgjb	@SBINDIR@/resolvconf -D "$named_options" "$named_zones"
86282434Sgjbfi
87282434Sgjb
88219732Sume# No point in changing files or reloading bind if the end result has not
89219732Sume# changed
90219732Sumechanged=false
91219732Sumeif [ -n "$named_options" ]; then
92219732Sume	if [ ! -f "$named_options" ] || \
93225524Shrs		[ "$(cat "$named_options")" != "$(printf %s "$newoptions")" ]
94219732Sume	then
95225524Shrs		printf %s "$newoptions" >"$named_options"
96219732Sume		changed=true
97219732Sume	fi
98219732Sumefi
99219732Sumeif [ -n "$named_zones" ]; then
100219732Sume	if [ ! -f "$named_zones" ] || \
101225524Shrs		[ "$(cat "$named_zones")" != "$(printf %s "$newzones")" ]
102219732Sume	then
103225524Shrs		printf %s "$newzones" >"$named_zones"
104219732Sume		changed=true
105219732Sume	fi
106219732Sumefi
107219732Sume
108304515Spfg# named does not seem to work with SIGHUP which is a same
109219732Sumeif $changed; then
110304515Spfg	if [ -n "$named_restart" ]; then
111304515Spfg		eval $named_restart
112304515Spfg	elif [ -n "$RESTARTCMD" ]; then
113304515Spfg		set -- ${named_service}
114304515Spfg		eval $RESTARTCMD
115304515Spfg	else
116304515Spfg		@SBINDIR@/resolvconf -r ${named_service}
117304515Spfg	fi
118219732Sumefi
119