dnsmasq: Add feature to forward domains to certain DNS servers.
[ipfire-2.x.git] / src / initscripts / init.d / dnsmasq
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
18 # Pull custom configuration file
19 if [ -e "/etc/sysconfig/dnsmasq" ]; then
20         . /etc/sysconfig/dnsmasq
21 fi
22
23 SHOW_SRV=1
24
25 function 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
45 case "${1}" in
46         start)
47                 # kill already running copy of dnsmasq...
48                 killproc /usr/sbin/dnsmasq 2>&1 > /dev/null
49
50                 boot_mesg "Starting Domain Name Service Proxy..."
51                 
52                 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
53                 ARGS=
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)
59                     if [ ! -z ${DNS1} ]; then
60                         echo "nameserver ${DNS1}" >> /var/ipfire/red/resolv.conf
61                     fi
62                 fi
63                 if [ -e "/var/ipfire/red/dns2" ]; then
64                     DNS2=$(cat /var/ipfire/red/dns2 2>/dev/null)
65                     if [ ! -z ${DNS2} ]; then
66                         echo "nameserver ${DNS2}" >> /var/ipfire/red/resolv.conf
67                     fi
68                 fi
69                 [ -e "/var/ipfire/red/active" ] && ARGS="$ARGS -r /var/ipfire/red/resolv.conf"
70         
71                 ARGS="$ARGS --domain=`cat /var/ipfire/main/settings |grep DOMAIN |cut -d = -f 2`"
72
73                 # Add custom forward dns zones.
74                 ARGS="${ARGS} $(dns_forward_args /var/ipfire/dnsforward/config)"
75
76                 ARGS="$ARGS $CUSTOM_ARGS"
77
78                 loadproc /usr/sbin/dnsmasq -l /var/state/dhcp/dhcpd.leases $ARGS
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
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                 ;;
105 esac
106
107 # End $rc_base/init.d/dnsmasq