1d7149f4eSGlen Barber#!/bin/sh
2*9af6c78cSPedro F. Giffuni# Copyright (c) 2009-2019 Roy Marples
3d7149f4eSGlen Barber# All rights reserved
4d7149f4eSGlen Barber
5d7149f4eSGlen Barber# PowerDNS Recursor subscriber for resolvconf
6d7149f4eSGlen Barber
7d7149f4eSGlen Barber# Redistribution and use in source and binary forms, with or without
8d7149f4eSGlen Barber# modification, are permitted provided that the following conditions
9d7149f4eSGlen Barber# are met:
10d7149f4eSGlen Barber#     * Redistributions of source code must retain the above copyright
11d7149f4eSGlen Barber#       notice, this list of conditions and the following disclaimer.
12d7149f4eSGlen Barber#     * Redistributions in binary form must reproduce the above
13d7149f4eSGlen Barber#       copyright notice, this list of conditions and the following
14d7149f4eSGlen Barber#       disclaimer in the documentation and/or other materials provided
15d7149f4eSGlen Barber#       with the distribution.
16d7149f4eSGlen Barber#
17d7149f4eSGlen Barber# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18d7149f4eSGlen Barber# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19d7149f4eSGlen Barber# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20d7149f4eSGlen Barber# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21d7149f4eSGlen Barber# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22d7149f4eSGlen Barber# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23d7149f4eSGlen Barber# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d7149f4eSGlen Barber# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d7149f4eSGlen Barber# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d7149f4eSGlen Barber# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27d7149f4eSGlen Barber# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d7149f4eSGlen Barber
29d7149f4eSGlen Barber[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30d7149f4eSGlen Barber. "@SYSCONFDIR@/resolvconf.conf" || exit 1
31d7149f4eSGlen Barber[ -z "$pdns_zones" ] && exit 0
32d7149f4eSGlen Barber[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33d7149f4eSGlen BarberNL="
34d7149f4eSGlen Barber"
35d7149f4eSGlen Barber
36*9af6c78cSPedro F. Giffuni: ${pdns_service:=pdns-recursor}
37d7149f4eSGlen Barber
38d7149f4eSGlen Barbernewzones=
39d7149f4eSGlen Barber
40*9af6c78cSPedro F. Giffunifor n in $NAMESERVERS; do
41*9af6c78cSPedro F. Giffuni	newzones="$newzones${newzones:+,}$n"
42*9af6c78cSPedro F. Giffunidone
43*9af6c78cSPedro F. Giffuni[ -n "$newzones" ] && newzones="+.=$newzones$NL"
44d7149f4eSGlen Barber
45d7149f4eSGlen Barberfor d in $DOMAINS; do
46d7149f4eSGlen Barber	newns=
47d7149f4eSGlen Barber	ns="${d#*:}"
48d7149f4eSGlen Barber	while [ -n "$ns" ]; do
49d7149f4eSGlen Barber		newns="$newns${newns:+,}${ns%%,*}"
50d7149f4eSGlen Barber		[ "$ns" = "${ns#*,}" ] && break
51d7149f4eSGlen Barber		ns="${ns#*,}"
52d7149f4eSGlen Barber	done
53d7149f4eSGlen Barber	[ -n "$newns" ] && newzones="$newzones${d%%:*}=$newns$NL"
54d7149f4eSGlen Barberdone
55d7149f4eSGlen Barber
56d7149f4eSGlen Barber# Try to ensure that config dirs exist
57d7149f4eSGlen Barberif type config_mkdirs >/dev/null 2>&1; then
58d7149f4eSGlen Barber	config_mkdirs "$pdnsd_zones"
59d7149f4eSGlen Barberelse
60d7149f4eSGlen Barber	@SBINDIR@/resolvconf -D "$pdnsd_zones"
61d7149f4eSGlen Barberfi
62d7149f4eSGlen Barber
63d7149f4eSGlen Barberif [ ! -f "$pdns_zones" ] || \
64d7149f4eSGlen Barber	[ "$(cat "$pdns_zones")" != "$(printf %s "$newzones")" ]
65d7149f4eSGlen Barberthen
66d7149f4eSGlen Barber	printf %s "$newzones" >"$pdns_zones"
6787b2cfceSPedro F. Giffuni	if [ -n "$pdns_restart" ]; then
68d7149f4eSGlen Barber		eval $pdns_restart
6987b2cfceSPedro F. Giffuni	elif [ -n "$RESTARTCMD" ]; then
7087b2cfceSPedro F. Giffuni		set -- ${pdns_service}
71*9af6c78cSPedro F. Giffuni		eval "$RESTARTCMD"
7287b2cfceSPedro F. Giffuni	else
7387b2cfceSPedro F. Giffuni		@SBINDIR@/resolvconf -r ${pdns_service}
7487b2cfceSPedro F. Giffuni	fi
75d7149f4eSGlen Barberfi
76