1282431Sgjb#!/bin/sh
2282431Sgjb# Copyright (c) 2009-2011 Roy Marples
3282431Sgjb# All rights reserved
4282431Sgjb
5282431Sgjb# PowerDNS Recursor subscriber for resolvconf
6282431Sgjb
7282431Sgjb# Redistribution and use in source and binary forms, with or without
8282431Sgjb# modification, are permitted provided that the following conditions
9282431Sgjb# are met:
10282431Sgjb#     * Redistributions of source code must retain the above copyright
11282431Sgjb#       notice, this list of conditions and the following disclaimer.
12282431Sgjb#     * Redistributions in binary form must reproduce the above
13282431Sgjb#       copyright notice, this list of conditions and the following
14282431Sgjb#       disclaimer in the documentation and/or other materials provided
15282431Sgjb#       with the distribution.
16282431Sgjb#
17282431Sgjb# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18282431Sgjb# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19282431Sgjb# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20282431Sgjb# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21282431Sgjb# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22282431Sgjb# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23282431Sgjb# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24282431Sgjb# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25282431Sgjb# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26282431Sgjb# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27282431Sgjb# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28282431Sgjb
29282431Sgjb[ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
30282431Sgjb. "@SYSCONFDIR@/resolvconf.conf" || exit 1
31282431Sgjb[ -z "$pdns_zones" ] && exit 0
32282431Sgjb[ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
33282431SgjbNL="
34282431Sgjb"
35282431Sgjb
36282431Sgjb: ${pdns_service:=pdns_recursor}
37282431Sgjb: ${pdns_restart:=@RESTARTCMD ${pdns_service}@}
38282431Sgjb
39282431Sgjbnewzones=
40282431Sgjb
41282431Sgjb# pds_recursor does not present support global forward servers, which
42282431Sgjb# does limit it's usefulness somewhat.
43282431Sgjb# If it did, the below code can be enabled, or something like it.
44282431Sgjb#for n in $NAMESERVERS; do
45282431Sgjb#	newzones="$newzones${newzones:+,}$n"
46282431Sgjb#done
47282431Sgjb#[ -n "$newzones" ] && newzones=".=$newzones$NL"
48282431Sgjb
49282431Sgjbfor d in $DOMAINS; do
50282431Sgjb	newns=
51282431Sgjb	ns="${d#*:}"
52282431Sgjb	while [ -n "$ns" ]; do
53282431Sgjb		newns="$newns${newns:+,}${ns%%,*}"
54282431Sgjb		[ "$ns" = "${ns#*,}" ] && break
55282431Sgjb		ns="${ns#*,}"
56282431Sgjb	done
57282431Sgjb	[ -n "$newns" ] && newzones="$newzones${d%%:*}=$newns$NL"
58282431Sgjbdone
59282431Sgjb
60282431Sgjb# Try to ensure that config dirs exist
61282431Sgjbif type config_mkdirs >/dev/null 2>&1; then
62282431Sgjb	config_mkdirs "$pdnsd_zones"
63282431Sgjbelse
64282431Sgjb	@SBINDIR@/resolvconf -D "$pdnsd_zones"
65282431Sgjbfi
66282431Sgjb
67282431Sgjbif [ ! -f "$pdns_zones" ] || \
68282431Sgjb	[ "$(cat "$pdns_zones")" != "$(printf %s "$newzones")" ]
69282431Sgjbthen
70282431Sgjb	printf %s "$newzones" >"$pdns_zones"
71282431Sgjb	eval $pdns_restart
72282431Sgjbfi
73