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)"
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
43219732Sume	fi
44219732Sumefi
45219732Sume: ${named_service:=named}
46219732Sume: ${named_restart:=@RESTARTCMD ${named_service}@}
47225524Shrsnewoptions="# Generated by resolvconf$NL"
48219732Sumenewzones="$newoptions"
49219732Sume
50219732Sumeforward=
51219732Sumefor n in $NAMESERVERS; do
52219732Sume	case "$forward" in
53225524Shrs	*"$NL	$n;"*);;
54225524Shrs	*) forward="$forward$NL	$n;";;
55219732Sume	esac
56219732Sumedone
57219732Sumeif [ -n "$forward" ]; then
58225524Shrs	newoptions="${newoptions}forward first;${NL}forwarders {$forward${NL}};$NL"
59219732Sumefi
60219732Sume
61219732Sumefor d in $DOMAINS; do
62225524Shrs	newzones="${newzones}zone \"${d%%:*}\" {$NL"
63225524Shrs	newzones="$newzones	type forward;$NL"
64225524Shrs	newzones="$newzones	forward first;$NL	forwarders {$NL"
65219732Sume	ns="${d#*:}"
66219732Sume	while [ -n "$ns" ]; do
67225524Shrs		newzones="$newzones		${ns%%,*};$NL"
68219732Sume		[ "$ns" = "${ns#*,}" ] && break
69219732Sume		ns="${ns#*,}"
70219732Sume	done
71225524Shrs	newzones="$newzones	};$NL};$NL"
72219732Sumedone
73219732Sume
74219732Sume# No point in changing files or reloading bind if the end result has not
75219732Sume# changed
76219732Sumechanged=false
77219732Sumeif [ -n "$named_options" ]; then
78219732Sume	if [ ! -f "$named_options" ] || \
79225524Shrs		[ "$(cat "$named_options")" != "$(printf %s "$newoptions")" ]
80219732Sume	then
81225524Shrs		printf %s "$newoptions" >"$named_options"
82219732Sume		changed=true
83219732Sume	fi
84219732Sumefi
85219732Sumeif [ -n "$named_zones" ]; then
86219732Sume	if [ ! -f "$named_zones" ] || \
87225524Shrs		[ "$(cat "$named_zones")" != "$(printf %s "$newzones")" ]
88219732Sume	then
89225524Shrs		printf %s "$newzones" >"$named_zones"
90219732Sume		changed=true
91219732Sume	fi
92219732Sumefi
93219732Sume
94219732Sumeif $changed; then
95219732Sume	eval $named_restart
96219732Sumefi
97