1219732Sume#!/bin/sh
2282746Sgjb# Copyright (c) 2009-2014 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
29282746Sgjbunbound_insecure=
30282746Sgjb
31219732Sume[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
32219732Sume. "@SYSCONFDIR@/resolvconf.conf" || exit 1
33219732Sume[ -z "$unbound_conf" ] && exit 0
34282746Sgjb[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
35225524ShrsNL="
36225524Shrs"
37219732Sume
38219732Sume: ${unbound_pid:=/var/run/unbound.pid}
39219732Sume: ${unbound_service:=unbound}
40219732Sume: ${unbound_restart:=@RESTARTCMD ${unbound_service}@}
41225524Shrsnewconf="# Generated by resolvconf$NL"
42219732Sume
43219732Sumefor d in $DOMAINS; do
44219732Sume	dn="${d%%:*}"
45219732Sume	ns="${d#*:}"
46282746Sgjb	case "$unbound_insecure" in
47282746Sgjb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
48282746Sgjb		newconf="$newconf${NL}domain-insecure: \"$dn\""
49282746Sgjb		;;
50282746Sgjb	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
66282746Sgjb# Try to ensure that config dirs exist
67282746Sgjbif type config_mkdirs >/dev/null 2>&1; then
68282746Sgjb	config_mkdirs "$unbound_conf"
69282746Sgjbelse
70282746Sgjb	@SBINDIR@/resolvconf -D "$unbound_conf"
71282746Sgjbfi
72282746Sgjb
73219732Sumeif [ ! -f "$unbound_conf" ] || \
74225524Shrs	[ "$(cat "$unbound_conf")" != "$(printf %s "$newconf")" ]
75219732Sumethen
76225524Shrs	printf %s "$newconf" >"$unbound_conf"
77219732Sume	# If we can't sent a HUP then force a restart
78219732Sume	if [ -s "$unbound_pid" ]; then
79219732Sume		if ! kill -HUP $(cat "$unbound_pid") 2>/dev/null; then
80219732Sume			eval $unbound_restart
81219732Sume		fi
82219732Sume	else
83219732Sume		eval $unbound_restart
84219732Sume	fi
85219732Sumefi
86