summaryrefslogtreecommitdiff
blob: 62fc73f4118f2baf73a0a0e2cb03eecd6b59f99e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
# Copyright 2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# dnsmasq-resolv.conf updater
# Written by Roy Marples (uberlord@gentoo.org)

# This is very important!
# We assume that we are a local dns cache - after all, why would a server
# use resolvconf?
# Now that we have assumed this, we also assume that generic DHCP clients
# will enter their domains and search domains ONLY in the "search" field
# in their resolv.confs and VPN clients will put the domain they are for
# into the domain field only.
# This allows dnsmasq to forward domains for a specific VPN domain to the
# VPN nameserver and everything else to the standard name servers.

# A sample dnsmasq config that works as above is like so
#domain-needed
#interface=lo
#resolv-file=/etc/dnsmasq-resolv.conf
#conf-file=/etc/dnsmasq-resolvconf.conf

# The last step is to configure dns configuration for /etc/resolv.conf
# for the lo interface. In Gentoo we set it up like so in /etc/conf.d/net
#dns_servers_lo="127.0.0.1"

# Load our variables from resolvconf
VARS="$(resolvconf -v)"
eval "${VARS}"

DNSMASQRESOLV="/etc/dnsmasq-resolv.conf"
DNSMASQCONF="/etc/dnsmasq-resolvconf.conf"

NEWCONF="# Generated by resolvconf"
NEWRESOLV="${NEWCONF}\nsearch"

# Using DBUS means that we never have to restart the daemon
# This is important as it means we should not drop DNS queries
# whilst changing DNS options around. However, DBUS support is optional
# so we need to validate a few things first.
# Check for DBus support in the binary
DBUS=no
if dnsmasq --version 2>/dev/null | \
grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]" \
; then
	# Sanity - check that dnsmasq and dbus are running
	if [ -x /etc/init.d/dbus -a -x /etc/init.d/dnsmasq ] ; then
		if /etc/init.d/dbus --quiet status && /etc/init.d/dnsmasq --quiet status ; then
			DBUS=yes
			NEWCONF="${NEWCONF}\n# Domain specific servers will be sent over dbus\n\nenable-dbus"
		fi
	fi
fi

uniqify() {
    local result=
    while [ -n "$1" ] ; do
		case " ${result} " in
			*" $1 "*) ;;
			*) result="${result} $1" ;;
		esac
		shift
	done
    echo "${result# *}"
}

# If we only have domain information then put it in search too
[ -z "${NEWSEARCH}" -a -z "${NEWNS}" ] && NEWSEARCH="${NEWDOMAIN}"

for N in ${NEWSEARCH} ; do
	case " ${NEWSL} " in
		*" ${N%,*} "*) ;;
		*) NEWSL="${NEWSL} ${N%,*}" ;;
	esac
	case "\n${NEWRESOLV}\n" in
		*"\nnameserver ${N#*,}\n"*) ;;
		*) NEWRESOLV="${NEWRESOLV}\nnameserver ${N#*,}" ;;
	esac
done
for N in ${NEWNS} ; do
	case "\n${NEWRESOLV}\n" in
		*"\nnameserver ${N}\n") ;;
		*) NEWRESOLV="${NEWRESOLV}\nnameserver ${N}" ;;
	esac
done
NEWRESOLV="$(echo "${NEWRESOLV}" | sed -e "s/^search/${NEWSL:+search${NEWSL}}/g")"

DBUSDEST=
for DN in $(uniqify ${NEWDOMAIN}) ; do
	if [ "${DBUS}" = "yes" ] ; then
		IP=${DN#*,}
		SIFS=${IFS-y} OIFS=$IFS
		IFS=.
		set -- ${IP}
		NUM="0x$(printf "%02x" $1 $2 $3 $4)"
		if [ "${SIFS}" = "y" ] ; then
			IFS=$OIFS
		else
			unset IFS
		fi
		DBUSDEST="${DBUSDEST} uint32:$(printf "%d" ${NUM}) string:${DN%,*}"
	else
		NEWCONF="${NEWCONF}\nserver=/${DN%,*}/${DN#*,}"
	fi
done

RELOAD="no"
if [ -e "${DNSMASQCONF}" ] ; then
	if [ "$(cat "${DNSMASQCONF}")" != "$(printf "${NEWCONF}")" ] ; then
		RELOAD="yes"
		printf "${NEWCONF}" > "${DNSMASQCONF}"
	fi
else
	RELOAD="yes"
	printf "${NEWCONF}" > "${DNSMASQCONF}"
fi
if [ -e "${DNSMASQRESOLV}" ] ; then
	if [ "$(cat "${DNSMASQRESOLV}")" != "$(printf "${NEWRESOLV}")" ] ; then
		RELOAD="yes"
		printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
	fi
else
	# dnsmasq polls this file so no need to set RELOAD="yes"
	printf "${NEWRESOLV}" > "${DNSMASQRESOLV}"
fi

if [ "${RELOAD}" = "yes" -a -x /etc/init.d/dnsmasq ] ; then
	/etc/init.d/dnsmasq --quiet --nodeps conditionalrestart
fi

if [ "${DBUS}" = "yes" ] ; then
	if [ "${RELOAD}" != "yes" -a -x /etc/init.d/dnsmasq ] ; then
		/etc/init.d/dnsmasq --quiet reload
	fi
	# Send even if emtpy so old servers are cleared
	dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
 		/uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
  		${DBUSDEST}
fi

# vim: ts=4 :