dnsmasq: make tftp root if not existing
[openwrt/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
index cec4b1290a472beee1be572f44766bdc093e8cff..5b4de32181ab04b1088c94ca40f7bce5f276a0bc 100644 (file)
@@ -23,22 +23,22 @@ xappend() {
 }
 
 hex_to_hostid() {
-        local var="$1"
-        local hex="${2#0x}"     # strip optional "0x" prefix
+       local var="$1"
+       local hex="${2#0x}" # strip optional "0x" prefix
 
-        if [ -n "${hex//[0-9a-fA-F]/}" ]; then
-                # is invalid hex literal
-                return 1
-        fi
+       if [ -n "${hex//[0-9a-fA-F]/}" ]; then
+               # is invalid hex literal
+               return 1
+       fi
 
-        # convert into host id
-        export "$var=$(
-                printf "%0x:%0x"  \
-                        $(((0x$hex >> 16) % 65536)) \
-                        $(( 0x$hex        % 65536))
-        )"
+       # convert into host id
+       export "$var=$(
+               printf "%0x:%0x" \
+               $(((0x$hex >> 16) % 65536)) \
+               $(( 0x$hex        % 65536))
+               )"
 
-        return 0
+       return 0
 }
 
 dhcp_calc() {
@@ -278,6 +278,7 @@ dhcp_host_add() {
        config_get tag "$cfg" tag
 
        if [ "$DHCPv6CAPABLE" -eq 1 ]; then
+               config_get duid "$cfg" duid
                config_get hostid "$cfg" hostid
                if [ -n "$hostid" ]; then
                        hex_to_hostid hostid "$hostid"
@@ -289,7 +290,7 @@ dhcp_host_add() {
 
        config_get leasetime "$cfg" leasetime
 
-       xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
+       xappend "--dhcp-host=$macs${duid:+,id:$duid}${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
 }
 
 dhcp_tag_add() {
@@ -391,6 +392,14 @@ dhcp_add() {
        dhcp_option_add "$cfg" "$networkid"
 }
 
+dhcp_option_append() {
+       local option="$1"
+       local networkid="$2"
+       local force="$3"
+
+       xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
+}
+
 dhcp_option_add() {
        local cfg="$1"
        local networkid="$2"
@@ -398,11 +407,21 @@ dhcp_option_add() {
 
        [ "$force" = "0" ] && force=
 
-       config_get dhcp_option "$cfg" dhcp_option
-       for o in $dhcp_option; do
-               xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
-       done
+       local list_len
+       config_get list_len "$cfg" dhcp_option_LENGTH
+
+       if [ -n "$list_len" ]; then
+               config_list_foreach "$cfg" dhcp_option dhcp_option_append "$networkid" "$force"
+       else
+               config_get dhcp_option "$cfg" dhcp_option
+
+               [ -n "$dhcp_option" ] && echo "Warning: the 'option dhcp_option' syntax is deprecated, use 'list dhcp_option'" >&2
 
+               local option
+               for option in $dhcp_option; do
+                       dhcp_option_append "$option" "$networkid" "$force"
+               done
+       fi
 }
 
 dhcp_domain_add() {
@@ -507,13 +526,14 @@ dhcp_relay_add() {
        if [ -z "$interface" ]; then
                xappend "--dhcp-relay=$local_addr,$server_addr"
        else
-               xappend "--dhcp-relay=$local_addr,$server_addr,$interface"
+               network_get_device ifname "$interface" || return
+               xappend "--dhcp-relay=$local_addr,$server_addr,$ifname"
        fi
 }
 
 dnsmasq_start()
 {
-       local cfg="$1" disabled
+       local cfg="$1" disabled resolvfile
 
        config_get_bool disabled "$cfg" disabled 0
        [ "$disabled" -gt 0 ] && return 0
@@ -561,7 +581,7 @@ dnsmasq_start()
        append_bool "$cfg" boguspriv "--bogus-priv"
        append_bool "$cfg" expandhosts "--expand-hosts"
        config_get tftp_root "$cfg" "tftp_root"
-       [ -d "$tftp_root" ] && append_bool "$cfg" enable_tftp "--enable-tftp"
+       [ -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" fqdn "--dhcp-fqdn"
@@ -593,7 +613,6 @@ dnsmasq_start()
        config_list_foreach "$cfg" "addnhosts" append_addnhosts
        config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
        append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
-       append_parm "$cfg" "resolvfile" "--resolv-file" "/tmp/resolv.conf.auto"
        append_parm "$cfg" "serversfile" "--servers-file"
        append_parm "$cfg" "tftp_root" "--tftp-root"
        append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
@@ -608,7 +627,6 @@ dnsmasq_start()
        config_get_bool readethers "$cfg" readethers
        [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
 
-       config_get resolvfile $cfg resolvfile
        config_get dhcpscript $cfg dhcpscript
 
        config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
@@ -622,6 +640,8 @@ dnsmasq_start()
                [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
        fi
 
+       [ -n "$resolvfile" ] && xappend "--resolv-file=$resolvfile"
+
        config_get hostsfile "$cfg" dhcphostsfile
        [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"