dnsmasq: Add feature to forward domains to certain DNS servers.
[ipfire-2.x.git] / src / initscripts / init.d / dnsmasq
CommitLineData
d1e90efc
MT
1#!/bin/sh
2########################################################################
3# Begin $rc_base/init.d/dnsmasq
4#
5# Description : dnsmasq init script
6#
7# Authors : Michael Tremer - mitch@ipfire.org
8#
9# Version : 01.00
10#
11# Notes :
12#
13########################################################################
14
15. /etc/sysconfig/rc
16. ${rc_functions}
17
16a61778
MT
18# Pull custom configuration file
19if [ -e "/etc/sysconfig/dnsmasq" ]; then
20 . /etc/sysconfig/dnsmasq
21fi
22
f480386f
MT
23SHOW_SRV=1
24
e4ba53ed
SS
25function dns_forward_args() {
26 local file="${1}"
27
28 # Do nothing if file is empty.
29 [ -s "${file}" ] || return
30
31 local cmdline
32
33 local enabled zone server remark
34 while IFS="," read -r enabled zone server remark; do
35 # Line must be enabled.
36 [ "${enabled}" = "on" ] || continue
37
38 cmdline="${cmdline} --server=/${zone}/${server}"
39 done < ${file}
40
41 echo "${cmdline}"
42}
43
44
d1e90efc
MT
45case "${1}" in
46 start)
fcfd54ba
AF
47 # kill already running copy of dnsmasq...
48 killproc /usr/sbin/dnsmasq 2>&1 > /dev/null
49
d1e90efc
MT
50 boot_mesg "Starting Domain Name Service Proxy..."
51
52 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
53 ARGS=
f480386f
MT
54 [ "$DOMAIN_NAME_GREEN" != "" ] && ARGS="-s $DOMAIN_NAME_GREEN"
55
56 echo > /var/ipfire/red/resolv.conf # Clear it
57 if [ -e "/var/ipfire/red/dns1" ]; then
58 DNS1=$(cat /var/ipfire/red/dns1 2>/dev/null)
2789f357
AF
59 if [ ! -z ${DNS1} ]; then
60 echo "nameserver ${DNS1}" >> /var/ipfire/red/resolv.conf
61 fi
f480386f
MT
62 fi
63 if [ -e "/var/ipfire/red/dns2" ]; then
64 DNS2=$(cat /var/ipfire/red/dns2 2>/dev/null)
2789f357
AF
65 if [ ! -z ${DNS2} ]; then
66 echo "nameserver ${DNS2}" >> /var/ipfire/red/resolv.conf
67 fi
f480386f
MT
68 fi
69 [ -e "/var/ipfire/red/active" ] && ARGS="$ARGS -r /var/ipfire/red/resolv.conf"
e4ba53ed 70
63aceb22 71 ARGS="$ARGS --domain=`cat /var/ipfire/main/settings |grep DOMAIN |cut -d = -f 2`"
e4ba53ed
SS
72
73 # Add custom forward dns zones.
74 ARGS="${ARGS} $(dns_forward_args /var/ipfire/dnsforward/config)"
75
16a61778
MT
76 ARGS="$ARGS $CUSTOM_ARGS"
77
d1e90efc 78 loadproc /usr/sbin/dnsmasq -l /var/state/dhcp/dhcpd.leases $ARGS
f480386f
MT
79
80 if [ "${SHOW_SRV}" -eq 1 ] && [ "${DNS1}" != "" -o "${DNS2}" != "" ]; then
81 boot_mesg "Using DNS server(s): ${DNS1} ${DNS2}"
82 boot_mesg_flush
83 fi
d1e90efc
MT
84 ;;
85
86 stop)
87 boot_mesg "Stopping Domain Name Service Proxy..."
88 killproc /usr/sbin/dnsmasq
89 ;;
90
91 restart)
92 ${0} stop
93 sleep 1
94 ${0} start
95 ;;
96
97 status)
98 statusproc /usr/sbin/dnsmasq
99 ;;
100
101 *)
102 echo "Usage: ${0} {start|stop|restart|status}"
103 exit 1
104 ;;
105esac
106
107# End $rc_base/init.d/dnsmasq