b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | ############################################################################## |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License version 2 as |
| 6 | # published by the Free Software Foundation. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # Copyright (C) 2016 Eric Luehrsen |
| 14 | # |
| 15 | ############################################################################## |
| 16 | # |
| 17 | # This script facilitates alternate installation of Unbound+odhcpd and no |
| 18 | # need for dnsmasq. There are some limitations, but it works and is small. |
| 19 | # The lease file is parsed to make "zone-data:" and "local-data:" entries. |
| 20 | # |
| 21 | # config odhcpd 'odhcpd' |
| 22 | # option leasetrigger '/usr/lib/unbound/odhcpd.sh' |
| 23 | # |
| 24 | ############################################################################## |
| 25 | |
| 26 | # while useful (sh)ellcheck is pedantic and noisy |
| 27 | # shellcheck disable=1091,2002,2004,2034,2039,2086,2094,2140,2154,2155 |
| 28 | |
| 29 | UB_ODHCPD_BLANK= |
| 30 | |
| 31 | ############################################################################## |
| 32 | |
| 33 | odhcpd_zonedata() { |
| 34 | . /lib/functions.sh |
| 35 | . /usr/lib/unbound/defaults.sh |
| 36 | |
| 37 | local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link ) |
| 38 | local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 ) |
| 39 | local dhcp_domain=$( uci_get unbound.@unbound[0].domain ) |
| 40 | local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile ) |
| 41 | |
| 42 | |
| 43 | if [ -f "$UB_TOTAL_CONF" ] && [ -f "$dhcp_origin" ] \ |
| 44 | && [ "$dhcp_link" = "odhcpd" ] && [ -n "$dhcp_domain" ] ; then |
| 45 | local longconf dateconf dateoldf |
| 46 | local dns_ls_add=$UB_VARDIR/dhcp_dns.add |
| 47 | local dns_ls_del=$UB_VARDIR/dhcp_dns.del |
| 48 | local dns_ls_new=$UB_VARDIR/dhcp_dns.new |
| 49 | local dns_ls_old=$UB_VARDIR/dhcp_dns.old |
| 50 | local dhcp_ls_new=$UB_VARDIR/dhcp_lease.new |
| 51 | |
| 52 | |
| 53 | if [ ! -f $UB_DHCP_CONF ] || [ ! -f $dns_ls_old ] ; then |
| 54 | # no old files laying around |
| 55 | touch $dns_ls_old |
| 56 | sort $dhcp_origin > $dhcp_ls_new |
| 57 | longconf=freshstart |
| 58 | |
| 59 | else |
| 60 | # incremental at high load or full refresh about each 5 minutes |
| 61 | dateconf=$(( $( date +%s ) - $( date -r $UB_DHCP_CONF +%s ) )) |
| 62 | dateoldf=$(( $( date +%s ) - $( date -r $dns_ls_old +%s ) )) |
| 63 | |
| 64 | |
| 65 | if [ $dateconf -gt 300 ] ; then |
| 66 | touch $dns_ls_old |
| 67 | sort $dhcp_origin > $dhcp_ls_new |
| 68 | longconf=longtime |
| 69 | |
| 70 | elif [ $dateoldf -gt 1 ] ; then |
| 71 | touch $dns_ls_old |
| 72 | sort $dhcp_origin > $dhcp_ls_new |
| 73 | longconf=increment |
| 74 | |
| 75 | else |
| 76 | # odhcpd is rapidly updating leases a race condition could occur |
| 77 | longconf=skip |
| 78 | fi |
| 79 | fi |
| 80 | |
| 81 | |
| 82 | case $longconf in |
| 83 | freshstart) |
| 84 | awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \ |
| 85 | -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \ |
| 86 | -v bisolt=0 -v bconf=1 \ |
| 87 | -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new |
| 88 | |
| 89 | cp $dns_ls_new $dns_ls_add |
| 90 | cp $dns_ls_new $dns_ls_old |
| 91 | cat $dns_ls_add | $UB_CONTROL_CFG local_datas |
| 92 | rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new |
| 93 | ;; |
| 94 | |
| 95 | longtime) |
| 96 | awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \ |
| 97 | -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \ |
| 98 | -v bisolt=0 -v bconf=1 \ |
| 99 | -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new |
| 100 | |
| 101 | awk '{ print $1 }' $dns_ls_old | sort | uniq > $dns_ls_del |
| 102 | cp $dns_ls_new $dns_ls_add |
| 103 | cp $dns_ls_new $dns_ls_old |
| 104 | cat $dns_ls_del | $UB_CONTROL_CFG local_datas_remove |
| 105 | cat $dns_ls_add | $UB_CONTROL_CFG local_datas |
| 106 | rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new |
| 107 | ;; |
| 108 | |
| 109 | increment) |
| 110 | # incremental add and prepare the old list for delete later |
| 111 | # unbound-control can be slow so high DHCP rates cannot run a full list |
| 112 | awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \ |
| 113 | -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \ |
| 114 | -v bisolt=0 -v bconf=0 \ |
| 115 | -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new |
| 116 | |
| 117 | sort $dns_ls_new $dns_ls_old $dns_ls_old | uniq -u > $dns_ls_add |
| 118 | sort $dns_ls_new $dns_ls_old | uniq > $dns_ls_old |
| 119 | cat $dns_ls_add | $UB_CONTROL_CFG local_datas |
| 120 | rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new |
| 121 | ;; |
| 122 | |
| 123 | *) |
| 124 | echo "do nothing" >/dev/null |
| 125 | ;; |
| 126 | esac |
| 127 | fi |
| 128 | } |
| 129 | |
| 130 | ############################################################################## |
| 131 | |
| 132 | UB_ODHPCD_LOCK=/tmp/unbound_odhcpd.lock |
| 133 | |
| 134 | if [ ! -f $UB_ODHPCD_LOCK ] ; then |
| 135 | # imperfect but it should avoid collisions |
| 136 | touch $UB_ODHPCD_LOCK |
| 137 | odhcpd_zonedata |
| 138 | rm -f $UB_ODHPCD_LOCK |
| 139 | |
| 140 | else |
| 141 | UB_ODHCPD_LOCK_AGE=$(( $( date +%s ) - $( date -r $UB_ODHPCD_LOCK +%s ) )) |
| 142 | |
| 143 | if [ $UB_ODHCPD_LOCK_AGE -gt 100 ] ; then |
| 144 | # unlock because something likely broke but do not write this time through |
| 145 | rm -f $UB_ODHPCD_LOCK |
| 146 | fi |
| 147 | fi |
| 148 | |
| 149 | ############################################################################## |
| 150 | |