X-Git-Url: http://git.openwrt.org/?p=openwrt%2Fstaging%2Fchunkeey.git;a=blobdiff_plain;f=package%2Fnetwork%2Fservices%2Fdnsmasq%2Ffiles%2Fdnsmasq.init;h=209952b5e68e3944f9e6c3a075e292049fe30603;hp=630c07d1d8ef405867a8dfb30c28115623c64339;hb=d9011ad6beb377fe66ce8fb4924db2c856887fff;hpb=dfea3bae11dd5c207182371ce1fdca763fb5bbe0 diff --git a/package/network/services/dnsmasq/files/dnsmasq.init b/package/network/services/dnsmasq/files/dnsmasq.init index 630c07d1d8..209952b5e6 100644 --- a/package/network/services/dnsmasq/files/dnsmasq.init +++ b/package/network/services/dnsmasq/files/dnsmasq.init @@ -3,7 +3,8 @@ START=60 -SERVICE_USE_PID=1 +USE_PROCD=1 +PROG=/usr/sbin/dnsmasq DNS_SERVERS="" DOMAIN="" @@ -12,6 +13,8 @@ ADD_LOCAL_DOMAIN=1 ADD_LOCAL_HOSTNAME=1 CONFIGFILE="/var/etc/dnsmasq.conf" +HOSTFILE="/tmp/hosts/dhcp" +TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf" xappend() { local value="$1" @@ -32,6 +35,29 @@ dhcp_calc() { echo "$res" } +dhcp_check() { + local ifname="$1" + local stamp="/var/run/dnsmasq.$ifname.dhcp" + local rv=0 + + [ -s "$stamp" ] && return $(cat "$stamp") + + 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 +} + +log_once() { + pidof dnsmasq >/dev/null || \ + logger -t dnsmasq "$@" +} + append_bool() { local section="$1" local option="$2" @@ -59,6 +85,10 @@ append_address() { xappend "--address=$1" } +append_ipset() { + xappend "--ipset=$1" +} + append_interface() { local ifname=$(uci_get_state network "$1" ifname "$1") xappend "--interface=$ifname" @@ -74,7 +104,7 @@ append_addnhosts() { } append_bogusnxdomain() { - xappend "--bogus-nxdomain=$1" + xappend "--bogus-nxdomain=$1" } dnsmasq() { @@ -109,6 +139,7 @@ dnsmasq() { append_parm "$cfg" "local" "--server" config_list_foreach "$cfg" "server" append_server config_list_foreach "$cfg" "address" append_address + config_list_foreach "$cfg" "ipset" append_ipset config_list_foreach "$cfg" "interface" append_interface config_list_foreach "$cfg" "notinterface" append_notinterface config_list_foreach "$cfg" "addnhosts" append_addnhosts @@ -134,13 +165,14 @@ dnsmasq() { config_get hostsfile "$cfg" dhcphostsfile [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile" - mkdir -p /tmp/hosts + mkdir -p /tmp/hosts /tmp/dnsmasq.d xappend "--addn-hosts=/tmp/hosts" + xappend "--conf-dir=/tmp/dnsmasq.d" local rebind config_get_bool rebind "$cfg" rebind_protection 1 [ $rebind -gt 0 ] && { - logger -t dnsmasq \ + log_once \ "DNS rebinding protection is active," \ "will discard upstream RFC1918 responses!" xappend "--stop-dns-rebind" @@ -148,20 +180,29 @@ dnsmasq() { local rebind_localhost config_get_bool rebind_localhost "$cfg" rebind_localhost 0 [ $rebind_localhost -gt 0 ] && { - logger -t dnsmasq "Allowing 127.0.0.0/8 responses" + log_once "Allowing 127.0.0.0/8 responses" xappend "--rebind-localhost-ok" } append_rebind_domain() { - logger -t dnsmasq "Allowing RFC1918 responses for domain $1" + log_once "Allowing RFC1918 responses for domain $1" xappend "--rebind-domain-ok=$1" } config_list_foreach "$cfg" rebind_domain append_rebind_domain } + config_get_bool dnssec "$cfg" dnssec 0 + [ "$dnssec" -gt 0 ] && { + xappend "--conf-file=$TRUSTANCHORSFILE" + xappend "--dnssec" + append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned" + } + dhcp_option_add "$cfg" "" 0 + xappend "--dhcp-broadcast=tag:needs-broadcast" + echo >> $CONFIGFILE } @@ -257,19 +298,29 @@ dhcp_host_add() { config_get ip "$cfg" ip [ -n "$ip" -o -n "$name" ] || return 0 + config_get_bool dns "$cfg" dns 0 + [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && { + echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE + } + config_get mac "$cfg" mac - [ -z "$mac" ] && { + if [ -n "$mac" ]; then + # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap + macs="" + for m in $mac; do append macs "$m" ","; done + else + # --dhcp-host=lap,192.168.0.199 [ -n "$name" ] || return 0 - mac="$name" + macs="$name" name="" - } - - macs="" - for m in $mac; do append macs "$m" ","; done + fi config_get tag "$cfg" tag - xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}" + config_get_bool broadcast "$cfg" broadcast 0 + [ "$broadcast" = "0" ] && broadcast= + + xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}" } dhcp_tag_add() { @@ -329,36 +380,31 @@ dhcp_add() { config_get net "$cfg" interface [ -n "$net" ] || return 0 + config_get dhcpv4 "$cfg" dhcpv4 + [ "$dhcpv4" != "disabled" ] || return 0 + config_get networkid "$cfg" networkid [ -n "$networkid" ] || networkid="$net" - config_get ifname "$net" ifname - [ -n "$ifname" ] || return 0 + network_get_subnet subnet "$net" || return 0 + network_get_device ifname "$net" || return 0 + network_get_protocol proto "$net" || return 0 - config_get dnsserver "$net" dns - [ "$cachelocal" = "0" -a -n "$dnsserver" ] && { + [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && { DNS_SERVERS="$DNS_SERVERS $dnsserver" } append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0 - config_get proto "$net" proto + # Do not support non-static interfaces for now [ static = "$proto" ] || return 0 - config_get ipaddr "$net" ipaddr - config_get netmask "$cfg" netmask - [ -n "$netmask" ] || config_get netmask "$net" netmask + # Override interface netmask with dhcp config if applicable + config_get netmask "$cfg" netmask "${subnet##*/}" #check for an already active dhcp server on the interface, unless 'force' is set config_get_bool force "$cfg" force 0 - [ $force -gt 0 ] || { - udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && { - logger -t dnsmasq \ - "found already running DHCP-server on interface '$ifname'" \ - "refusing to start, use 'option force 1' to override" - return 0 - } - } + [ $force -gt 0 ] || dhcp_check "$ifname" || return 0 config_get start "$cfg" start config_get limit "$cfg" limit @@ -370,7 +416,7 @@ dhcp_add() { start="$(dhcp_calc "${start:-100}")" limit="${limit:-150}" [ "$limit" -gt 0 ] && limit=$((limit-1)) - eval "$(ipcalc.sh $ipaddr $netmask $start $limit)" + eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)" if [ "$dynamicdhcp" = "0" ]; then END="static"; fi xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}" @@ -402,10 +448,10 @@ dhcp_domain_add() { [ -n "$ip" ] || return 0 for name in $names; do - record="${record:+$record/}$name" + record="${record:+$record }$name" done - xappend "--address=/$record/$ip" + echo "$ip $record" >> $HOSTFILE } dhcp_srv_add() { @@ -479,19 +525,32 @@ dhcp_hostrecord_add() { xappend "--host-record=$record" } -start() { - include /lib/network - scan_interfaces +service_triggers() +{ + procd_add_reload_trigger "dhcp" +} + +boot() { + # Will be launched through hotplug + return 0 +} - local lanaddr - config_get lanaddr "lan" ipaddr +start_service() { + include /lib/functions config_load dhcp + procd_open_instance + procd_set_param command $PROG -C $CONFIGFILE -k + procd_set_param file $CONFIGFILE + procd_set_param respawn + procd_close_instance + # before we can call xappend mkdir -p $(dirname $CONFIGFILE) echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE + echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE # if we did this last, we could override auto-generated config [ -f /etc/dnsmasq.conf ] && { @@ -514,37 +573,45 @@ start() { config_foreach dhcp_hostrecord_add hostrecord # add own hostname - [ $ADD_LOCAL_HOSTNAME -eq 1 ] && [ -n "$lanaddr" ] && { + local lanaddr + [ $ADD_LOCAL_HOSTNAME -eq 1 ] && network_get_ipaddr lanaddr "lan" && { local hostname="$(uci_get system @system[0] hostname OpenWrt)" - dhcp_hostrecord_add "" "$hostname${DOMAIN:+.$DOMAIN $hostname}" "$lanaddr" + dhcp_domain_add "" "$hostname" "$lanaddr" } echo >> $CONFIGFILE config_foreach dhcp_srv_add srvhost config_foreach dhcp_mx_add mxhost echo >> $CONFIGFILE - config_foreach dhcp_add dhcp + + config_get odhcpd_is_active odhcpd maindhcp + if [ "$odhcpd_is_active" != "1" ]; then + config_foreach dhcp_add dhcp + fi + echo >> $CONFIGFILE config_foreach dhcp_cname_add cname echo >> $CONFIGFILE - service_start /usr/sbin/dnsmasq -C $CONFIGFILE && { - 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 + 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 } -stop() { - service_stop /usr/sbin/dnsmasq && { - [ -f /tmp/resolv.conf ] && { - rm -f /tmp/resolv.conf - ln -s /tmp/resolv.conf.auto /tmp/resolv.conf - } +reload_service() { + rc_procd start_service "$@" + return 0 +} + +stop_service() { + [ -f /tmp/resolv.conf ] && { + rm -f /tmp/resolv.conf + ln -s /tmp/resolv.conf.auto /tmp/resolv.conf } + rm -f /var/run/dnsmasq.*.dhcp }