xref: /freebsd/contrib/openresolv/unbound.in (revision 2d004dd5bc51eaef924f55d1e2407e80a9b4bcb5)
1587392a5SHajimu UMEMOTO#!/bin/sh
2d7149f4eSGlen Barber# Copyright (c) 2009-2014 Roy Marples
3587392a5SHajimu UMEMOTO# All rights reserved
4587392a5SHajimu UMEMOTO
5587392a5SHajimu UMEMOTO# unbound subscriber for resolvconf
6587392a5SHajimu UMEMOTO
7587392a5SHajimu UMEMOTO# Redistribution and use in source and binary forms, with or without
8587392a5SHajimu UMEMOTO# modification, are permitted provided that the following conditions
9587392a5SHajimu UMEMOTO# are met:
10587392a5SHajimu UMEMOTO#     * Redistributions of source code must retain the above copyright
11587392a5SHajimu UMEMOTO#       notice, this list of conditions and the following disclaimer.
12587392a5SHajimu UMEMOTO#     * Redistributions in binary form must reproduce the above
13587392a5SHajimu UMEMOTO#       copyright notice, this list of conditions and the following
14587392a5SHajimu UMEMOTO#       disclaimer in the documentation and/or other materials provided
15587392a5SHajimu UMEMOTO#       with the distribution.
16587392a5SHajimu UMEMOTO#
17587392a5SHajimu UMEMOTO# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18587392a5SHajimu UMEMOTO# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19587392a5SHajimu UMEMOTO# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20587392a5SHajimu UMEMOTO# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21587392a5SHajimu UMEMOTO# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22587392a5SHajimu UMEMOTO# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23587392a5SHajimu UMEMOTO# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24587392a5SHajimu UMEMOTO# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25587392a5SHajimu UMEMOTO# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26587392a5SHajimu UMEMOTO# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27587392a5SHajimu UMEMOTO# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28587392a5SHajimu UMEMOTO
29d7149f4eSGlen Barberunbound_insecure=
30d7149f4eSGlen Barber
31587392a5SHajimu UMEMOTO[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
32587392a5SHajimu UMEMOTO. "@SYSCONFDIR@/resolvconf.conf" || exit 1
33587392a5SHajimu UMEMOTO[ -z "$unbound_conf" ] && exit 0
34d7149f4eSGlen Barber[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
35a02aba5fSHiroki SatoNL="
36a02aba5fSHiroki Sato"
37587392a5SHajimu UMEMOTO
38587392a5SHajimu UMEMOTO: ${unbound_pid:=/var/run/unbound.pid}
39587392a5SHajimu UMEMOTO: ${unbound_service:=unbound}
40587392a5SHajimu UMEMOTO: ${unbound_restart:=@RESTARTCMD ${unbound_service}@}
41a02aba5fSHiroki Satonewconf="# Generated by resolvconf$NL"
42587392a5SHajimu UMEMOTO
43587392a5SHajimu UMEMOTOfor d in $DOMAINS; do
44587392a5SHajimu UMEMOTO	dn="${d%%:*}"
45587392a5SHajimu UMEMOTO	ns="${d#*:}"
46d7149f4eSGlen Barber	case "$unbound_insecure" in
47d7149f4eSGlen Barber	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
48*2d004dd5SPedro F. Giffuni		newconf="$newconf${NL}server:$NL"
49*2d004dd5SPedro F. Giffuni		newconf="$newconf	domain-insecure: \"$dn\"$NL"
50d7149f4eSGlen Barber		;;
51d7149f4eSGlen Barber	esac
52a02aba5fSHiroki Sato	newconf="$newconf${NL}forward-zone:$NL	name: \"$dn\"$NL"
53587392a5SHajimu UMEMOTO	while [ -n "$ns" ]; do
54a02aba5fSHiroki Sato		newconf="$newconf	forward-addr: ${ns%%,*}$NL"
55587392a5SHajimu UMEMOTO		[ "$ns" = "${ns#*,}" ] && break
56587392a5SHajimu UMEMOTO		ns="${ns#*,}"
57587392a5SHajimu UMEMOTO	done
58587392a5SHajimu UMEMOTOdone
59587392a5SHajimu UMEMOTO
60587392a5SHajimu UMEMOTOif [ -n "$NAMESERVERS" ]; then
61a02aba5fSHiroki Sato	newconf="$newconf${NL}forward-zone:$NL	name: \".\"$NL"
62587392a5SHajimu UMEMOTO	for n in $NAMESERVERS; do
63a02aba5fSHiroki Sato		newconf="$newconf	forward-addr: $n$NL"
64587392a5SHajimu UMEMOTO	done
65587392a5SHajimu UMEMOTOfi
66587392a5SHajimu UMEMOTO
67d7149f4eSGlen Barber# Try to ensure that config dirs exist
68d7149f4eSGlen Barberif type config_mkdirs >/dev/null 2>&1; then
69d7149f4eSGlen Barber	config_mkdirs "$unbound_conf"
70d7149f4eSGlen Barberelse
71d7149f4eSGlen Barber	@SBINDIR@/resolvconf -D "$unbound_conf"
72d7149f4eSGlen Barberfi
73d7149f4eSGlen Barber
74587392a5SHajimu UMEMOTOif [ ! -f "$unbound_conf" ] || \
75a02aba5fSHiroki Sato	[ "$(cat "$unbound_conf")" != "$(printf %s "$newconf")" ]
76587392a5SHajimu UMEMOTOthen
77a02aba5fSHiroki Sato	printf %s "$newconf" >"$unbound_conf"
78587392a5SHajimu UMEMOTO	# If we can't sent a HUP then force a restart
79587392a5SHajimu UMEMOTO	if [ -s "$unbound_pid" ]; then
80587392a5SHajimu UMEMOTO		if ! kill -HUP $(cat "$unbound_pid") 2>/dev/null; then
81587392a5SHajimu UMEMOTO			eval $unbound_restart
82587392a5SHajimu UMEMOTO		fi
83587392a5SHajimu UMEMOTO	else
84587392a5SHajimu UMEMOTO		eval $unbound_restart
85587392a5SHajimu UMEMOTO	fi
86587392a5SHajimu UMEMOTOfi
87