xref: /dragonfly/contrib/openresolv/unbound.in (revision f0e61bb7)
1#!/bin/sh
2# Copyright (c) 2009-2023 Roy Marples
3# All rights reserved
4
5# unbound subscriber for resolvconf
6
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#     * Redistributions of source code must retain the above copyright
11#       notice, this list of conditions and the following disclaimer.
12#     * Redistributions in binary form must reproduce the above
13#       copyright notice, this list of conditions and the following
14#       disclaimer in the documentation and/or other materials provided
15#       with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29unbound_insecure=
30
31[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
32. "@SYSCONFDIR@/resolvconf.conf" || exit 1
33[ -z "$unbound_conf" ] && exit 0
34[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
35NL="
36"
37
38: ${unbound_pid:=/var/run/unbound.pid}
39: ${unbound_service:=unbound}
40newconf="# Generated by resolvconf$NL"
41
42for d in $DOMAINS; do
43	dn="${d%%:*}"
44	ns="${d#*:}"
45	case "$unbound_insecure" in
46	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
47		newconf="$newconf${NL}server:$NL"
48		newconf="$newconf	domain-insecure: \"$dn\"$NL"
49		;;
50	esac
51	newconf="$newconf${NL}forward-zone:$NL	name: \"$dn\"$NL"
52	if [ -n "$unbound_forward_zone_options" ]; then
53		newconf="$newconf	$unbound_forward_zone_options${NL}"
54	fi
55	while [ -n "$ns" ]; do
56		newconf="$newconf	forward-addr: ${ns%%,*}$NL"
57		[ "$ns" = "${ns#*,}" ] && break
58		ns="${ns#*,}"
59	done
60done
61
62if [ -n "$NAMESERVERS" ]; then
63	newconf="$newconf${NL}forward-zone:$NL	name: \".\"$NL"
64	if [ -n "$unbound_forward_zone_options" ]; then
65		newconf="$newconf	$unbound_forward_zone_options${NL}"
66	fi
67	for n in $NAMESERVERS; do
68		newconf="$newconf	forward-addr: $n$NL"
69	done
70fi
71
72# Try to ensure that config dirs exist
73if command -v config_mkdirs >/dev/null 2>&1; then
74	config_mkdirs "$unbound_conf"
75else
76	@SBINDIR@/resolvconf -D "$unbound_conf"
77fi
78
79restart_unbound()
80{
81	if [ -n "$unbound_restart" ]; then
82		eval $unbound_restart
83	elif [ -n "$RESTARTCMD" ]; then
84		set -- ${unbound_service}
85		eval "$RESTARTCMD"
86	else
87		@SBINDIR@/resolvconf -r ${unbound_service}
88	fi
89}
90
91if [ ! -f "$unbound_conf" ] || \
92	[ "$(cat "$unbound_conf")" != "$(printf %s "$newconf")" ]
93then
94	printf %s "$newconf" >"$unbound_conf"
95	# If we can't sent a HUP then force a restart
96	if [ -s "$unbound_pid" ]; then
97		if ! kill -HUP $(cat "$unbound_pid") 2>/dev/null; then
98			restart_unbound
99		fi
100	else
101		restart_unbound
102	fi
103fi
104