dnsmasq: manage resolv.conf if when listening on 127.0.0.1#53
[openwrt/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
index 5221db46f627cfebe044bba2989188c070ed44fb..fda11401db6f8e47912809ae1b506401d0570ffb 100644 (file)
@@ -18,6 +18,7 @@ TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
 TIMEVALIDFILE="/var/state/dnsmasqsec"
 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
 RFC6761FILE="/usr/share/dnsmasq/rfc6761.conf"
+DHCPSCRIPT="/usr/lib/dnsmasq/dhcp-script.sh"
 
 DNSMASQ_DHCP_VER=4
 
@@ -88,12 +89,24 @@ log_once() {
                logger -t dnsmasq "$@"
 }
 
+has_handler() {
+       local file
+
+       for file in /etc/hotplug.d/dhcp/* /etc/hotplug.d/tftp/* /etc/hotplug.d/neigh/*; do
+               [ -f "$file" ] && return 0
+       done
+
+       return 1
+}
+
 append_bool() {
        local section="$1"
        local option="$2"
        local value="$3"
+       local default="$4"
        local _loctmp
-       config_get_bool _loctmp "$section" "$option" 0
+       [ -z "$default" ] && default="0"
+       config_get_bool _loctmp "$section" "$option" "$default"
        [ $_loctmp -gt 0 ] && xappend "$value"
 }
 
@@ -694,9 +707,24 @@ dhcp_relay_add() {
        fi
 }
 
+dnsmasq_ismain()
+{
+       local cfg="$1"
+       local port notinterfaces
+
+       config_get port "$cfg" port "53"
+       [ $port = "53" ] || return 1
+
+       config_get notinterfaces "$cfg" notinterface ""
+       [ -n $notinterfaces ] && list_contains $notinterfaces "loopback" || return 1
+
+       # dnsmasq instance is designated to listen on 127.0.0.1#53.
+       return 0
+}
+
 dnsmasq_start()
 {
-       local cfg="$1" disabled resolvfile
+       local cfg="$1" disabled resolvfile user_dhcpscript
 
        config_get_bool disabled "$cfg" disabled 0
        [ "$disabled" -gt 0 ] && return 0
@@ -780,7 +808,7 @@ dnsmasq_start()
        config_get tftp_root "$cfg" "tftp_root"
        [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
        append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
-       append_bool "$cfg" nonwildcard "--bind-dynamic"
+       append_bool "$cfg" nonwildcard "--bind-dynamic" 1
        append_bool "$cfg" fqdn "--dhcp-fqdn"
        append_bool "$cfg" proxydnssec "--proxy-dnssec"
        append_bool "$cfg" localservice "--local-service"
@@ -791,7 +819,7 @@ dnsmasq_start()
        append_bool "$cfg" noping "--no-ping"
 
        append_parm "$cfg" logfacility "--log-facility"
-       append_parm "$cfg" dhcpscript "--dhcp-script"
+
        append_parm "$cfg" cachesize "--cache-size"
        append_parm "$cfg" dnsforwardmax "--dns-forward-max"
        append_parm "$cfg" port "--port"
@@ -831,20 +859,19 @@ dnsmasq_start()
        config_get_bool readethers "$cfg" readethers
        [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
 
-       config_get dhcpscript $cfg dhcpscript
+       config_get user_dhcpscript $cfg dhcpscript
+       if has_handler || [ -n "$user_dhcpscript" ]; then
+               xappend "--dhcp-script=$DHCPSCRIPT"
+       fi
 
        config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
        [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
        config_get_bool cachelocal "$cfg" cachelocal 1
 
-       config_get_bool noresolv "$cfg" noresolv 0
-       if [ "$noresolv" != "1" ]; then
-               config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
-               # So jail doesn't complain if file missing
-               [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
-       fi
-
-       [ -n "$resolvfile" ] && xappend "--resolv-file=$resolvfile"
+       config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
+       xappend "--resolv-file=$resolvfile"
+       # So jail doesn't complain if file missing
+       [ \! -e "$resolvfile" ] && touch "$resolvfile"
 
        config_get hostsfile "$cfg" dhcphostsfile
        [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
@@ -957,20 +984,11 @@ dnsmasq_start()
        echo >> $CONFIGFILE_TMP
        mv -f $CONFIGFILE_TMP $CONFIGFILE
 
-       [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
-               rm -f /tmp/resolv.conf
-               [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
-                       echo "search $DOMAIN" >> /tmp/resolv.conf
-               }
-               DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
-               for DNS_SERVER in $DNS_SERVERS ; do
-                       echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
-               done
-       }
 
        procd_open_instance $cfg
        procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
        procd_set_param file $CONFIGFILE
+       [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
        procd_set_param respawn
 
        local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
@@ -979,24 +997,33 @@ dnsmasq_start()
        fi
 
        procd_add_jail dnsmasq ubus log
-       procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $dhcpscript /etc/hosts /etc/ethers $EXTRA_MOUNT
+       procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $user_dhcpscript /etc/hosts /etc/ethers /sbin/hotplug-call $EXTRA_MOUNT $DHCPSCRIPT
        procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
 
        procd_close_instance
+
+
+       # write /tmp/resolve.conf only for main instance
+       dnsmasq_ismain $cfg && {
+               rm -f /tmp/resolv.conf
+               [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
+                       echo "search $DOMAIN" >> /tmp/resolv.conf
+               }
+               DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
+               for DNS_SERVER in $DNS_SERVERS ; do
+                       echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
+               done
+       }
 }
 
 dnsmasq_stop()
 {
        local cfg="$1"
 
-       config_get resolvfile "$cfg" "resolvfile"
-
        #relink /tmp/resolve.conf only for main instance
-       [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
-               [ -f /tmp/resolv.conf ] && {
-                       rm -f /tmp/resolv.conf
-                       ln -s "$resolvfile" /tmp/resolv.conf
-               }
+       dnsmasq_ismain $cfg && {
+               [ -f /tmp/resolv.conf ] && rm -f /tmp/resolv.conf
+               ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
        }
 
        rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp