dnsmasq: use local option for local domain parameter
[openwrt/staging/hauke.git] / package / network / services / dnsmasq / files / dnsmasq.init
index 1be60de860ad39fd76caa975c9b21a32f242a859..f86b4b04f3678bc2c68d5871ead6c7e0fc90384a 100644 (file)
@@ -42,9 +42,14 @@ dnsmasq_ignore_opt() {
                bootp-*|\
                pxe-*)
                        [ -z "$dnsmasq_has_dhcp" ] ;;
-               dnssec-*|\
+               dnssec*|\
                trust-anchor)
-                       [ -z "$dnsmasq_has_dnssec" ] ;;
+                       if [ -z "$dnsmasq_has_dnssec" ]; then
+                               echo "dnsmasq: \"$opt\" requested, but dnssec support is not available" >&2
+                               exit 1
+                       fi
+                       return 1
+                       ;;
                tftp-*)
                        [ -z "$dnsmasq_has_tftp" ] ;;
                ipset)
@@ -102,19 +107,14 @@ dhcp_check() {
 
        [ -s "$stamp" ] && return $(cat "$stamp")
 
-       # If there's no carrier yet, skip this interface.
+       # If interface is down, skip it.
        # The init script will be called again once the link is up
-       case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
+       case "$(devstatus "$ifname" | jsonfilter -e @.up)" in
                false) return 1;;
        esac
 
        udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
 
-       [ $rv -eq 1 ] && \
-               logger -t dnsmasq \
-                       "found already running DHCP-server on interface '$ifname'" \
-                       "refusing to start, use 'option force 1' to override"
-
        echo $rv > "$stamp"
        return $rv
 }
@@ -161,7 +161,7 @@ append_server() {
 }
 
 append_rev_server() {
-        xappend "--rev-server=$1"
+       xappend "--rev-server=$1"
 }
 
 append_address() {
@@ -517,7 +517,12 @@ dhcp_add() {
 
        #check for an already active dhcp server on the interface, unless 'force' is set
        config_get_bool force "$cfg" force 0
-       [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
+       [ $force -gt 0 ] || dhcp_check "$ifname" || {
+               logger -t dnsmasq \
+                       "found already running DHCP-server on interface '$ifname'" \
+                       "refusing to start, use 'option force 1' to override"
+               return 0
+       }
 
        config_get start "$cfg" start 100
        config_get limit "$cfg" limit 150
@@ -764,6 +769,29 @@ dhcp_relay_add() {
        fi
 }
 
+dnsmasq_ipset_add() {
+       local cfg="$1"
+       local ipsets domains
+
+       add_ipset() {
+               ipsets="${ipsets:+$ipsets,}$1"
+       }
+
+       add_domain() {
+               # leading '/' is expected
+               domains="$domains/$1"
+       }
+
+       config_list_foreach "$cfg" "name" add_ipset
+       config_list_foreach "$cfg" "domain" add_domain
+
+       if [ -z "$ipsets" ] || [ -z "$domains" ]; then
+               return 0
+       fi
+
+       xappend "--ipset=$domains/$ipsets"
+}
+
 dnsmasq_start()
 {
        local cfg="$1"
@@ -850,8 +878,16 @@ dnsmasq_start()
        append_bool "$cfg" noresolv "--no-resolv"
        append_bool "$cfg" localise_queries "--localise-queries"
        append_bool "$cfg" readethers "--read-ethers"
-       append_bool "$cfg" dbus "--enable-dbus"
-       append_bool "$cfg" ubus "--enable-ubus" 1
+
+       local instance_name="dnsmasq.$cfg"
+       if [ "$cfg" = "$DEFAULT_INSTANCE" ]; then
+               instance_name="dnsmasq"
+       fi
+       config_get_bool dbus "$cfg" "dbus" 0
+       [ $dbus -gt 0 ] && xappend "--enable-dbus=uk.org.thekelleys.$instance_name"
+       config_get_bool ubus "$cfg" "ubus" 1
+       [ $ubus -gt 0 ] && xappend "--enable-ubus=$instance_name"
+
        append_bool "$cfg" expandhosts "--expand-hosts"
        config_get tftp_root "$cfg" "tftp_root"
        [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
@@ -879,7 +915,7 @@ dnsmasq_start()
        append_parm "$cfg" "minport" "--min-port"
        append_parm "$cfg" "maxport" "--max-port"
        append_parm "$cfg" "domain" "--domain"
-       append_parm "$cfg" "local" "--server"
+       append_parm "$cfg" "local" "--local"
        config_list_foreach "$cfg" "listen_address" append_listenaddress
        config_list_foreach "$cfg" "server" append_server
        config_list_foreach "$cfg" "rev_server" append_rev_server
@@ -900,6 +936,7 @@ dnsmasq_start()
        append_parm "$cfg" "min_cache_ttl" "--min-cache-ttl"
        append_parm "$cfg" "max_cache_ttl" "--max-cache-ttl"
        append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
+       append_parm "$cfg" "tftp_unique_root" "--tftp-unique-root"
        config_list_foreach "$cfg" "pxe_service" append_pxe_service
        config_get DOMAIN "$cfg" domain
 
@@ -964,10 +1001,9 @@ dnsmasq_start()
                xappend "--conf-file=$TRUSTANCHORSFILE"
                xappend "--dnssec"
                [ -x /etc/init.d/sysntpd ] && {
-                       /etc/init.d/sysntpd enabled
-                       [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
+                       if /etc/init.d/sysntpd enabled || [ "$(uci_get system.ntp.enabled)" = "1" ] ; then
                                [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
-                       }
+                       fi
                }
                config_get_bool dnsseccheckunsigned "$cfg" dnsseccheckunsigned 1
                [ "$dnsseccheckunsigned" -eq 0 ] && xappend "--dnssec-check-unsigned=no"
@@ -984,7 +1020,12 @@ dnsmasq_start()
 
        xappend "--dhcp-broadcast=tag:needs-broadcast"
 
-       xappend "--addn-hosts=$(dirname $HOSTFILE)"
+       config_get_bool ignore_hosts_dir "$cfg" ignore_hosts_dir 0
+       if [ "$ignore_hosts_dir" = "1" ]; then
+               xappend "--addn-hosts=$HOSTFILE"
+       else
+               xappend "--addn-hosts=$(dirname $HOSTFILE)"
+       fi
 
        config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
        xappend "--conf-dir=$dnsmasqconfdir"
@@ -1051,6 +1092,10 @@ dnsmasq_start()
        config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
        echo >> $CONFIGFILE_TMP
 
+       echo >> $CONFIGFILE_TMP
+       config_foreach filter_dnsmasq ipset dnsmasq_ipset_add "$cfg"
+       echo >> $CONFIGFILE_TMP
+
        echo >> $CONFIGFILE_TMP
        mv -f $CONFIGFILE_TMP $CONFIGFILE
        mv -f $HOSTFILE_TMP $HOSTFILE
@@ -1061,6 +1106,7 @@ dnsmasq_start()
                        echo "search $DOMAIN" >> /tmp/resolv.conf
                }
                DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
+               [ -e /proc/sys/net/ipv6 ] && DNS_SERVERS="$DNS_SERVERS ::1"
                for DNS_SERVER in $DNS_SERVERS ; do
                        echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
                done
@@ -1122,6 +1168,7 @@ boot()
 start_service() {
        local instance="$1"
        local instance_found=0
+       local first_instance=""
 
        . /lib/functions/network.sh
 
@@ -1132,10 +1179,27 @@ start_service() {
                        if [ -n "$instance" ] && [ "$instance" = "$name" ]; then
                                instance_found=1
                        fi
+                       if [ -z "$DEFAULT_INSTANCE" ]; then
+                               local disabled
+                               config_get_bool disabled "$name" disabled 0
+                               if [ "$disabled" -eq 0 ]; then
+                                       # First enabled section will be assigned default instance name.
+                                       # Unnamed sections get precedence over named sections.
+                                       if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev/null = "9"; then # See uci_fixup_section.
+                                               DEFAULT_INSTANCE="$name" # Unnamed config section.
+                                       elif [ -z "$first_instance" ]; then
+                                               first_instance="$name"
+                                       fi
+                               fi
+                       fi
                fi
        }
 
+       DEFAULT_INSTANCE=""
        config_load dhcp
+       if [ -z "$DEFAULT_INSTANCE" ]; then
+               DEFAULT_INSTANCE="$first_instance" # No unnamed config section was found.
+       fi
 
        if [ -n "$instance" ]; then
                [ "$instance_found" -gt 0 ] || return