Add core additional core pkgs feed/master commit : f564008b9d6b458d2e5291414ef4ac05cc...
[librecmc/librecmc.git] / package / network / services / unbound / files / odhcpd.sh
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 . /lib/functions.sh
27 . /usr/lib/unbound/defaults.sh
28
29 ##############################################################################
30
31 odhcpd_zonedata() {
32   local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link )
33   local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 )
34   local dhcp_domain=$( uci_get unbound.@unbound[0].domain )
35   local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
36
37
38   if [ -f "$UB_TOTAL_CONF" ] && [ -f "$dhcp_origin" ] \
39   && [ "$dhcp_link" = "odhcpd" ] && [ -n "$dhcp_domain" ] ; then
40     local longconf dateconf
41     local dns_ls_add=$UB_VARDIR/dhcp_dns.add
42     local dns_ls_del=$UB_VARDIR/dhcp_dns.del
43     local dns_ls_new=$UB_VARDIR/dhcp_dns.new
44     local dns_ls_old=$UB_VARDIR/dhcp_dns.old
45     local dhcp_ls_new=$UB_VARDIR/dhcp_lease.new
46
47     # Capture the lease file which could be changing often
48     sort $dhcp_origin > $dhcp_ls_new
49
50
51     if [ ! -f $UB_DHCP_CONF ] || [ ! -f $dns_ls_old ] ; then
52       # no old files laying around
53       longconf=freshstart
54
55     else
56       # incremental at high load or full refresh about each 5 minutes
57       dateconf=$(( $( date +%s ) - $( date -r $UB_DHCP_CONF +%s ) ))
58
59
60       if [ $dateconf -gt 300 ] ; then
61         longconf=longtime
62       else
63         longconf=increment
64       fi
65     fi
66
67
68     case $longconf in
69     freshstart)
70       awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
71           -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
72           -v bisolt=0 -v bconf=1 \
73           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
74
75       cp $dns_ls_new $dns_ls_add
76       cp $dns_ls_new $dns_ls_old
77       ;;
78
79     longtime)
80       awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
81           -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
82           -v bisolt=0 -v bconf=1 \
83           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
84
85       awk '{ print $1 }' $dns_ls_old | sort | uniq > $dns_ls_del
86       cp $dns_ls_new $dns_ls_add
87       cp $dns_ls_new $dns_ls_old
88       ;;
89
90     *)
91       # incremental add and prepare the old list for delete later
92       # unbound-control can be slow so high DHCP rates cannot run a full list
93       awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
94           -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
95           -v bisolt=0 -v bconf=0 \
96           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
97
98       sort $dns_ls_new $dns_ls_old $dns_ls_old | uniq -u > $dns_ls_add
99       sort $dns_ls_new $dns_ls_old | uniq > $dns_ls_old
100       ;;
101     esac
102
103
104     if [ -f "$dns_ls_del" ] ; then
105       cat $dns_ls_del | $UB_CONTROL_CFG local_datas_remove
106     fi
107
108
109     if [ -f "$dns_ls_add" ] ; then
110       cat $dns_ls_add | $UB_CONTROL_CFG local_datas
111     fi
112
113
114     # prepare next round
115     rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new
116   fi
117 }
118
119 ##############################################################################
120
121 odhcpd_zonedata
122
123 ##############################################################################
124