blob: 808c6c12a86cf750aaba633f18ccf3e0bbb0e522 [file] [log] [blame]
Simon Kelleyc72daea2012-01-05 21:33:27 +00001#!/bin/sh
2### BEGIN INIT INFO
3# Provides: dnsmasq
4# Required-Start: $network $remote_fs $syslog
5# Required-Stop: $network $remote_fs $syslog
6# Default-Start: 2 3 4 5
7# Default-Stop: 0 1 6
8# Description: DHCP and DNS server
9### END INIT INFO
10
11set +e # Don't exit on error status
12
13PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
14DAEMON=/usr/sbin/dnsmasq
15NAME=dnsmasq
16DESC="DNS forwarder and DHCP server"
17
18# Most configuration options in /etc/default/dnsmasq are deprecated
19# but still honoured.
20ENABLED=1
21if [ -r /etc/default/$NAME ]; then
22 . /etc/default/$NAME
23fi
24
25# Get the system locale, so that messages are in the correct language, and the
26# charset for IDN is correct
27if [ -r /etc/default/locale ]; then
28 . /etc/default/locale
29 export LANG
30fi
31
Simon Kelley8aa999e2014-05-04 21:45:26 +010032# /etc/dnsmasq.d/README is a non-conffile installed by the dnsmasq package.
33# Should the dnsmasq package be removed, the following test ensures that
34# the daemon is no longer started, even if the dnsmasq-base package is
35# still in place.
36test -e /etc/dnsmasq.d/README || exit 0
37
Simon Kelleyc72daea2012-01-05 21:33:27 +000038test -x $DAEMON || exit 0
39
40# Provide skeleton LSB log functions for backports which don't have LSB functions.
41if [ -f /lib/lsb/init-functions ]; then
42 . /lib/lsb/init-functions
43else
44 log_warning_msg () {
45 echo "${@}."
46 }
47
48 log_success_msg () {
49 echo "${@}."
50 }
51
52 log_daemon_msg () {
53 echo -n "${1}: $2"
54 }
55
56 log_end_msg () {
57 if [ $1 -eq 0 ]; then
58 echo "."
59 elif [ $1 -eq 255 ]; then
60 /bin/echo -e " (warning)."
61 else
62 /bin/echo -e " failed!"
63 fi
64 }
65fi
66
67# RESOLV_CONF:
68# If the resolvconf package is installed then use the resolv conf file
69# that it provides as the default. Otherwise use /etc/resolv.conf as
70# the default.
71#
72# If IGNORE_RESOLVCONF is set in /etc/default/dnsmasq or an explicit
73# filename is set there then this inhibits the use of the resolvconf-provided
74# information.
75#
76# Note that if the resolvconf package is installed it is not possible to
77# override it just by configuration in /etc/dnsmasq.conf, it is necessary
78# to set IGNORE_RESOLVCONF=yes in /etc/default/dnsmasq.
79
80if [ ! "$RESOLV_CONF" ] &&
81 [ "$IGNORE_RESOLVCONF" != "yes" ] &&
82 [ -x /sbin/resolvconf ]
83then
84 RESOLV_CONF=/var/run/dnsmasq/resolv.conf
85fi
86
87for INTERFACE in $DNSMASQ_INTERFACE; do
88 DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -i $INTERFACE"
89done
90
91for INTERFACE in $DNSMASQ_EXCEPT; do
92 DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -I $INTERFACE"
93done
94
95if [ ! "$DNSMASQ_USER" ]; then
96 DNSMASQ_USER="dnsmasq"
97fi
98
Simon Kelley1a9a3482014-03-05 15:01:08 +000099# This tells dnsmasq to ignore DNS requests that don't come from a local network.
100# It's automatically ignored if --interface --except-interface, --listen-address
101# or --auth-server exist in the configuration, so for most installations, it will
102# have no effect, but for otherwise-unconfigured installations, it stops dnsmasq
103# from being vulnerable to DNS-reflection attacks.
104
105DNSMASQ_OPTS="$DNSMASQ_OPTS --local-service"
106
Simon Kelleyc72daea2012-01-05 21:33:27 +0000107start()
108{
109 # Return
110 # 0 if daemon has been started
111 # 1 if daemon was already running
112 # 2 if daemon could not be started
113
114 # /var/run may be volatile, so we need to ensure that
115 # /var/run/dnsmasq exists here as well as in postinst
116 if [ ! -d /var/run/dnsmasq ]; then
117 mkdir /var/run/dnsmasq || return 2
118 chown dnsmasq:nogroup /var/run/dnsmasq || return 2
119 fi
120
121 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null || return 1
122 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON -- \
123 -x /var/run/dnsmasq/$NAME.pid \
124 ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
125 ${MAILTARGET:+ -t $MAILTARGET} \
126 ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
127 ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
128 ${DHCP_LEASE:+ -l $DHCP_LEASE} \
129 ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
130 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
131 ${CACHESIZE:+ -c $CACHESIZE} \
132 ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
133 ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} \
134 || return 2
135}
136
137start_resolvconf()
138{
139# If interface "lo" is explicitly disabled in /etc/default/dnsmasq
140# Then dnsmasq won't be providing local DNS, so don't add it to
141# the resolvconf server set.
142 for interface in $DNSMASQ_EXCEPT
143 do
144 [ $interface = lo ] && return
145 done
146
147 if [ -x /sbin/resolvconf ] ; then
148 echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.$NAME
149 fi
150 return 0
151}
152
153stop()
154{
155 # Return
156 # 0 if daemon has been stopped
157 # 1 if daemon was already stopped
158 # 2 if daemon could not be stopped
159 # other if a failure occurred
160 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile /var/run/dnsmasq/$NAME.pid --name $NAME
Simon Kelleyc72daea2012-01-05 21:33:27 +0000161}
162
163stop_resolvconf()
164{
165 if [ -x /sbin/resolvconf ] ; then
166 /sbin/resolvconf -d lo.$NAME
167 fi
168 return 0
169}
170
171status()
172{
173 # Return
174 # 0 if daemon is running
175 # 1 if daemon is dead and pid file exists
176 # 3 if daemon is not running
177 # 4 if daemon status is unknown
178 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null
179 case "$?" in
180 0) [ -e "/var/run/dnsmasq/$NAME.pid" ] && return 1 ; return 3 ;;
181 1) return 0 ;;
182 *) return 4 ;;
183 esac
184}
185
186case "$1" in
187 start)
188 test "$ENABLED" != "0" || exit 0
189 log_daemon_msg "Starting $DESC" "$NAME"
190 start
191 case "$?" in
192 0)
193 log_end_msg 0
194 start_resolvconf
195 exit 0
196 ;;
197 1)
198 log_success_msg "(already running)"
199 exit 0
200 ;;
201 *)
202 log_end_msg 1
203 exit 1
204 ;;
205 esac
206 ;;
207 stop)
208 stop_resolvconf
209 if [ "$ENABLED" != "0" ]; then
210 log_daemon_msg "Stopping $DESC" "$NAME"
211 fi
212 stop
213 RETVAL="$?"
214 if [ "$ENABLED" = "0" ]; then
215 case "$RETVAL" in
216 0) log_daemon_msg "Stopping $DESC" "$NAME"; log_end_msg 0 ;;
217 esac
218 exit 0
219 fi
220 case "$RETVAL" in
221 0) log_end_msg 0 ; exit 0 ;;
222 1) log_warning_msg "(not running)" ; exit 0 ;;
223 *) log_end_msg 1; exit 1 ;;
224 esac
225 ;;
226 restart|force-reload)
227 test "$ENABLED" != "0" || exit 1
228 $DAEMON --test ${CONFIG_DIR:+ -7 $CONFIG_DIR} ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} >/dev/null 2>&1
229 if [ $? -ne 0 ]; then
230 NAME="configuration syntax check"
231 RETVAL="2"
232 else
233 stop_resolvconf
234 stop
235 RETVAL="$?"
236 fi
237 log_daemon_msg "Restarting $DESC" "$NAME"
238 case "$RETVAL" in
239 0|1)
240 sleep 2
241 start
242 case "$?" in
243 0)
244 log_end_msg 0
245 start_resolvconf
246 exit 0
247 ;;
248 *)
249 log_end_msg 1
250 exit 1
251 ;;
252 esac
253 ;;
254 *)
255 log_end_msg 1
256 exit 1
257 ;;
258 esac
259 ;;
260 status)
261 log_daemon_msg "Checking $DESC" "$NAME"
262 status
263 case "$?" in
264 0) log_success_msg "(running)" ; exit 0 ;;
265 1) log_success_msg "(dead, pid file exists)" ; exit 1 ;;
266 3) log_success_msg "(not running)" ; exit 3 ;;
267 *) log_success_msg "(unknown)" ; exit 4 ;;
268 esac
269 ;;
Simon Kelley760169f2012-03-09 14:27:49 +0000270 dump-stats)
271 kill -s USR1 `cat /var/run/dnsmasq/$NAME.pid`
272 ;;
Simon Kelley2cd9a0d2012-06-11 21:56:10 +0100273 systemd-start-resolvconf)
274 start_resolvconf
275 ;;
276 systemd-stop-resolvconf)
277 stop_resolvconf
278 ;;
279 systemd-exec)
Simon Kelleyd92c53e2014-05-20 21:00:02 +0100280# /var/run may be volatile, so we need to ensure that
281 # /var/run/dnsmasq exists here as well as in postinst
282 if [ ! -d /var/run/dnsmasq ]; then
283 mkdir /var/run/dnsmasq || return 2
284 chown dnsmasq:nogroup /var/run/dnsmasq || return 2
285 fi
Simon Kelley2cd9a0d2012-06-11 21:56:10 +0100286# Enable DBus by default because we use DBus activation with systemd.
Simon Kelleya23949d2014-05-09 20:47:09 +0100287 exec $DAEMON --keep-in-foreground --enable-dbus \
Simon Kelleya754e1d2014-05-20 20:56:55 +0100288 -x /var/run/dnsmasq/$NAME.pid \
Simon Kelley2cd9a0d2012-06-11 21:56:10 +0100289 ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
290 ${MAILTARGET:+ -t $MAILTARGET} \
291 ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
292 ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
293 ${DHCP_LEASE:+ -l $DHCP_LEASE} \
294 ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
295 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
296 ${CACHESIZE:+ -c $CACHESIZE} \
297 ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
298 ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
299 ;;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000300 *)
Simon Kelley760169f2012-03-09 14:27:49 +0000301 echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|dump-stats|status}" >&2
Simon Kelleyc72daea2012-01-05 21:33:27 +0000302 exit 3
303 ;;
304esac
305
306exit 0
307