isc-dhcpd: Redux of convenience functions avoiding sed/cut 22415/head
authorPhilip Prindeville <philipp@redfish-solutions.com>
Thu, 21 Sep 2023 23:40:18 +0000 (17:40 -0600)
committerPhilip Prindeville <philipp@redfish-solutions.com>
Mon, 16 Oct 2023 04:18:09 +0000 (22:18 -0600)
Also minimize messing with $IFS.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
net/isc-dhcp/files/dhcpd.init

index 498cf78820d43766312556a8f92ccad9b6d940a5..927924dbc69ac2cc0d06f0febefa045654fdd518 100755 (executable)
@@ -4,6 +4,7 @@ START=25
 USE_PROCD=1
 PROG=/usr/sbin/dhcpd
 
+WS=$'[\t ]'
 TTL=3600
 PREFIX="update add"
 
@@ -46,23 +47,35 @@ time2seconds() {
        echo $(( number * multiplier ))
 }
 
+explode() {
+       echo "${1//\./ }"
+}
+
 trim() {
-       local arg="$1"
+       local str="$1" prev
 
-       echo "$arg" | sed -e 's/^ *//' -e 's/ *$//'
+       while true; do
+               prev="$str"
+               str="${str%%$WS}"
+               [ "$str" = "$prev" ] && break
+       done
+       while true; do
+               prev="$str"
+               str="${str##$WS}"
+               [ "$str" = "$prev" ] && break
+       done
+       echo "$str"
 }
 
 rfc1918_prefix() {
-       local octets="$(echo "${1%%/*}" | cut -d. -f1)"
-
-       [ "$octets" = "10" ] && { echo "$octets"; return; }
+       local subnet="${1%/*}"
+       set -- $(explode "$subnet")
 
-       octets="$(echo "${1%%/*}" | cut -d. -f1-2)"
-
-       case "$octets" in
+       case "$1.$2" in
+       10.*)
+               echo "$1" ;;
        172.1[6789]|172.2[0-9]|172.3[01]|192.168)
-               echo "$octets"
-               ;;
+               echo "$1.$2" ;;
        esac
 }
 
@@ -110,12 +123,6 @@ update() {
                echo -e "$PREFIX" "$lhs $family $type $@\nsend" >> $dyn_file
 }
 
-explode() {
-       local arg="$1"
-
-       echo "$arg" | sed -e 's/\./, /g'
-}
-
 rev_str() {
        local str="$1" delim="$2"
        local frag result="" IFS="$delim"
@@ -138,20 +145,35 @@ create_empty_zone() {
 }
 
 append_routes() {
-       local tuple tuple="$(trim "$1")"
-       local network prefix router save octets compacted
+       local tuple="$(trim "$1")"
+       local network prefix router subnet compacted octet
 
-       save="${tuple% *}"
-       router="$(trim "${tuple#${save} }")"
+       subnet="${tuple%%$WS*}"
 
-       network="$(trim "${save%/[0-9]*}")"
+       network="${subnet%/[0-9]*}"
 
-       prefix="$(trim "${save##${network}/}")"
+       prefix="${subnet#*/}"
 
-       octets=$((($prefix + 7) / 8))
-       compacted="$(echo "$network" | cut -d. -f1-$octets)"
+       set -- $(explode "$network")
 
-       append routes "$(explode "$prefix${compacted:+.$compacted}.$router")" ", "
+       case $((($prefix + 7) / 8)) in
+       0)
+               compacted= ;;
+       1)
+               compacted="$1" ;;
+       2)
+               compacted="$1 $2" ;;
+       3)
+               compacted="$1 $2 $3" ;;
+       4)
+               compacted="$1 $2 $3 $4" ;;
+       esac
+
+       router="${tuple#${subnet}$WS}"
+
+       for octet in $prefix $compacted $(explode "$router"); do
+               append routes "$octet" ", "
+       done
 }
 
 append_dhcp_options() {
@@ -280,7 +302,7 @@ static_host_add() {
        config_get leasetime "$cfg" "leasetime"
        if [ -n "$leasetime" ] ; then
                leasetime="$(time2seconds "$leasetime")"
-               [ "$?" -ne 0 ] && return 1
+               [ $? -ne 0 ] && return 1
        fi
 
        config_get hostid "$cfg" "hostid"
@@ -290,8 +312,7 @@ static_host_add() {
 
        config_get force_send "$cfg" "force_send"
        extra_options=
-       local _IFS="$IFS" IFS=','
-       for option in $force_send; do
+       for option in ${force_send//,/ }; do
                case "$option" in
                hostname)
                        append extra_options "0c" "," ;;
@@ -309,7 +330,6 @@ static_host_add() {
                        echo "unknown option: $option" >&2 ;;
                esac
        done
-       IFS="$_IFS"
 
        macn=0
        for mac in $macs; do
@@ -325,7 +345,7 @@ static_host_add() {
                echo " hardware ethernet $mac;"
                echo " fixed-address $ip;"
                echo " option host-name \"$name\";"
-               if [ "$broadcast" -eq 1 ] ; then
+               if [ $broadcast -eq 1 ] ; then
                        echo " always-broadcast true;"
                fi
                if [ -n "$leasetime" ] ; then
@@ -379,8 +399,8 @@ gen_dhcp_subnet() {
        if [ -n "$BROADCAST" ] && [ "$BROADCAST" != "0.0.0.0" ] ; then
                echo " option broadcast-address $BROADCAST;"
        fi
-       if [ "$dynamicdhcp" -eq 0 ] ; then
-               if [ "$authoritative" -eq 1 ] ; then
+       if [ $dynamicdhcp -eq 0 ] ; then
+               if [ $authoritative -eq 1 ] ; then
                        echo " deny unknown-clients;"
                else
                        echo " ignore unknown-clients;"
@@ -390,7 +410,7 @@ gen_dhcp_subnet() {
                echo " default-lease-time $leasetime;"
                echo " max-lease-time $leasetime;"
        fi
-       if [ "$defaultroute" -eq 1 ] ; then
+       if [ $defaultroute -eq 1 ] ; then
                echo " option routers $gateway;"
        fi
        echo " option domain-name-servers $DNS;"
@@ -413,7 +433,7 @@ dhcpd_add() {
        local IP NETMASK BROADCAST NETWORK PREFIX DNS START END
 
        config_get_bool ignore "$cfg" "ignore" 0
-       [ "$ignore" = "0" ] || return 0
+       [ $ignore -eq 1 ] && return 0
 
        config_get net "$cfg" "interface"
        [ -n "$net" ] || return 0
@@ -431,19 +451,19 @@ dhcpd_add() {
        network_get_device ifname "$net" || return 0
        network_get_protocol proto "$net" || return 0
 
-       [ static = "$proto" ] || return 0
+       [ "$proto" != "static" ] && return 0
 
        local octets="$(rfc1918_prefix "$subnet")"
 
        [ -n "$octets" ] && append rfc1918_nets "$octets"
 
-       [ $synthesize -eq 0 ] && return
+       [ $synthesize -eq 0 ] && return 0
 
        config_get_bool dynamicdhcp "$cfg" "dynamicdhcp" 1
 
        config_get_bool defaultroute "$cfg" "default_route" 1
 
-       dhcp_ifs="$dhcp_ifs $ifname"
+       append dhcp_ifs "$ifname"
 
        if ! ipcalc "$subnet" "$start" "$limit"; then
                echo "invalid range params: $subnet start: $start limit $limit" >&2
@@ -454,7 +474,7 @@ dhcpd_add() {
        config_get leasetime "$cfg" "leasetime"
        if [ -n "$leasetime" ] ; then
                leasetime="$(time2seconds "$leasetime")"
-               [ "$?" -ne 0 ] && return 1
+               [ $? -ne 0 ] && return 1
        fi
 
        if network_get_dnsserver dnsserver "$net" ; then
@@ -493,9 +513,9 @@ general_config() {
        [ $boot_unknown_clients -eq 0 ] && echo "boot-unknown-clients false;"
 
        default_lease_time="$(time2seconds "$default_lease_time")"
-       [ "$?" -ne 0 ] && return 1
+       [ $? -ne 0 ] && return 1
        max_lease_time="$(time2seconds "$max_lease_time")"
-       [ "$?" -ne 0 ] && return 1
+       [ $? -ne 0 ] && return 1
 
        if [ $dynamicdns -eq 1 ]; then
                create_empty_zone "$domain"
@@ -620,6 +640,7 @@ start_service() {
                local rfc1918_nets=""
 
                # alas we have to make 2 passes...
+               dhcp_ifs=
                config_foreach dhcpd_add dhcp 0
 
                rfc1918_nets="$(echo "$rfc1918_nets" | tr ' ' $'\n' | sort | uniq | tr $'\n' ' ')"
@@ -637,6 +658,7 @@ EOF
 
                rfc1918_nets=
 
+               dhcp_ifs=
                config_foreach dhcpd_add dhcp 1 >> $config_file
 
                static_hosts >> $config_file