1219732Sume#!/bin/sh
2304515Spfg# Copyright (c) 2009-2016 Roy Marples
3219732Sume# All rights reserved
4219732Sume
5219732Sume# unbound 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
29282434Sgjbunbound_insecure=
30282434Sgjb
31219732Sume[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
32219732Sume. "@SYSCONFDIR@/resolvconf.conf" || exit 1
33219732Sume[ -z "$unbound_conf" ] && exit 0
34282434Sgjb[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
35225524ShrsNL="
36225524Shrs"
37219732Sume
38219732Sume: ${unbound_pid:=/var/run/unbound.pid}
39219732Sume: ${unbound_service:=unbound}
40225524Shrsnewconf="# Generated by resolvconf$NL"
41219732Sume
42219732Sumefor d in $DOMAINS; do
43219732Sume	dn="${d%%:*}"
44219732Sume	ns="${d#*:}"
45282434Sgjb	case "$unbound_insecure" in
46282434Sgjb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
47295165Spfg		newconf="$newconf${NL}server:$NL"
48295165Spfg		newconf="$newconf	domain-insecure: \"$dn\"$NL"
49282434Sgjb		;;
50282434Sgjb	esac
51225524Shrs	newconf="$newconf${NL}forward-zone:$NL	name: \"$dn\"$NL"
52219732Sume	while [ -n "$ns" ]; do
53225524Shrs		newconf="$newconf	forward-addr: ${ns%%,*}$NL"
54219732Sume		[ "$ns" = "${ns#*,}" ] && break
55219732Sume		ns="${ns#*,}"
56219732Sume	done
57219732Sumedone
58219732Sume
59219732Sumeif [ -n "$NAMESERVERS" ]; then
60225524Shrs	newconf="$newconf${NL}forward-zone:$NL	name: \".\"$NL"
61219732Sume	for n in $NAMESERVERS; do
62225524Shrs		newconf="$newconf	forward-addr: $n$NL"
63219732Sume	done
64219732Sumefi
65219732Sume
66282434Sgjb# Try to ensure that config dirs exist
67282434Sgjbif type config_mkdirs >/dev/null 2>&1; then
68282434Sgjb	config_mkdirs "$unbound_conf"
69282434Sgjbelse
70282434Sgjb	@SBINDIR@/resolvconf -D "$unbound_conf"
71282434Sgjbfi
72282434Sgjb
73304515Spfgrestart_unbound()
74304515Spfg{
75304515Spfg	if [ -n "$unbound_restart" ]; then
76304515Spfg		eval $unbound_restart
77304515Spfg	elif [ -n "$RESTARTCMD" ]; then
78304515Spfg		set -- ${unbound_service}
79304515Spfg		eval $RESTARTCMD
80304515Spfg	else
81304515Spfg		@SBINDIR@/resolvconf -r ${unbound_service}
82304515Spfg	fi
83304515Spfg}
84304515Spfg
85219732Sumeif [ ! -f "$unbound_conf" ] || \
86225524Shrs	[ "$(cat "$unbound_conf")" != "$(printf %s "$newconf")" ]
87219732Sumethen
88225524Shrs	printf %s "$newconf" >"$unbound_conf"
89219732Sume	# If we can't sent a HUP then force a restart
90219732Sume	if [ -s "$unbound_pid" ]; then
91219732Sume		if ! kill -HUP $(cat "$unbound_pid") 2>/dev/null; then
92304515Spfg			restart_unbound
93219732Sume		fi
94219732Sume	else
95304515Spfg		restart_unbound
96219732Sume	fi
97219732Sumefi
98