Move libexpat, unbound into core and introduce hnsd
[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 longconf dateconf
33   local dns_ls_add=$UB_VARDIR/dhcp_dns.add
34   local dns_ls_del=$UB_VARDIR/dhcp_dns.del
35   local dhcp_ls_new=$UB_VARDIR/dhcp_lease.new
36   local dhcp_ls_old=$UB_VARDIR/dhcp_lease.old
37   local dhcp_ls_add=$UB_VARDIR/dhcp_lease.add
38   local dhcp_ls_del=$UB_VARDIR/dhcp_lease.del
39
40   local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link )
41   local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 )
42   local dhcp_domain=$( uci_get unbound.@unbound[0].domain )
43   local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
44
45
46   if [ -f "$UB_TOTAL_CONF" -a -f "$dhcp_origin" \
47        -a "$dhcp_link" = "odhcpd" -a -n "$dhcp_domain" ] ; then
48     # Capture the lease file which could be changing often
49     sort $dhcp_origin > $dhcp_ls_new
50
51
52     if [ ! -f $UB_DHCP_CONF -o ! -f $dhcp_ls_old ] ; then
53       longconf=2
54
55     else
56       dateconf=$(( $( date +%s ) - $( date -r $UB_DHCP_CONF +%s ) ))
57
58
59       if [ $dateconf > 150 ] ; then
60         longconf=1
61       else
62         longconf=0
63       fi
64     fi
65
66
67     if [ $longconf -gt 0 ] ; then
68       # Go through the messy business of coding up A, AAAA, and PTR records
69       # This static conf will be available if Unbound restarts asynchronously
70       awk -v hostfile=$UB_DHCP_CONF -v domain=$dhcp_domain \
71           -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=1 \
72           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
73     fi
74
75
76     if [ $longconf -lt 2 ] ; then
77       # Deleting and adding all records into Unbound can be a burden in a
78       # high density environment. Use unbound-control incrementally.
79       sort $dhcp_ls_old $dhcp_ls_new $dhcp_ls_new | uniq -u > $dhcp_ls_del
80       awk -v hostfile=$dns_ls_del -v domain=$dhcp_domain \
81           -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
82           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_del
83
84       sort $dhcp_ls_new $dhcp_ls_old $dhcp_ls_old | uniq -u > $dhcp_ls_add
85       awk -v hostfile=$dns_ls_add -v domain=$dhcp_domain \
86           -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
87           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_add
88
89     else
90       awk -v hostfile=$dns_ls_add -v domain=$dhcp_domain \
91           -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
92           -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
93     fi
94
95
96     if [ -f "$dns_ls_del" ] ; then
97       cat $dns_ls_del | $UB_CONTROL_CFG local_datas_remove
98     fi
99
100
101     if [ -f "$dns_ls_add" ] ; then
102       cat $dns_ls_add | $UB_CONTROL_CFG local_datas
103     fi
104
105
106     # prepare next round
107     mv $dhcp_ls_new $dhcp_ls_old
108     rm -f $dns_ls_del $dns_ls_add $dhcp_ls_del $dhcp_ls_add
109   fi
110 }
111
112 ##############################################################################
113
114 odhcpd_zonedata
115
116 ##############################################################################
117