22a470b4cd6f2c970c42d2d3a09a95f82690c337
[librecmc/package-feed.git] / net / 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 # Common file location definitions
27 . /usr/lib/unbound/unbound.sh
28
29 ##############################################################################
30
31 odhcpd_settings() {
32   # This trigger is out of normal init context, so we need to read some UCI.
33   local cfg="$1"
34   config_get UNBOUND_D_DHCP_LINK  "$cfg" dhcp_link none
35   config_get_bool UNBOUND_B_SLAAC6_MAC "$cfg" dhcp4_slaac6 0
36   config_get UNBOUND_TXT_DOMAIN "$cfg" domain lan
37 }
38
39 ##############################################################################
40
41 odhcpd_zonedata() {
42   local dns_ls_add=$UNBOUND_VARDIR/dhcp_dns.add
43   local dns_ls_del=$UNBOUND_VARDIR/dhcp_dns.del
44   local dhcp_ls_new=$UNBOUND_VARDIR/dhcp_lease.new
45   local dhcp_ls_old=$UNBOUND_VARDIR/dhcp_lease.old
46   local dhcp_ls_add=$UNBOUND_VARDIR/dhcp_lease.add
47   local dhcp_ls_del=$UNBOUND_VARDIR/dhcp_lease.del
48   local dhcp_origin=$( uci get dhcp.@odhcpd[0].leasefile )
49
50   config_load unbound
51   config_foreach odhcpd_settings unbound
52
53
54   if [ "$UNBOUND_D_DHCP_LINK" = "odhcpd" -a -f "$dhcp_origin" ] ; then
55     # Capture the lease file which could be changing often
56     cat $dhcp_origin | sort > $dhcp_ls_new
57     touch $dhcp_ls_old
58     sort $dhcp_ls_new $dhcp_ls_old $dhcp_ls_old | uniq -u > $dhcp_ls_add
59     sort $dhcp_ls_old $dhcp_ls_new $dhcp_ls_new | uniq -u > $dhcp_ls_del
60
61     # Go through the messy business of coding up A, AAAA, and PTR records
62     # This static conf will be available if Unbound restarts asynchronously
63     awk -v hostfile=$UNBOUND_DHCP_CONF -v domain=$UNBOUND_TXT_DOMAIN \
64         -v bslaac=$UNBOUND_B_SLAAC6_MAC -v bisolt=0 -v bconf=1 \
65         -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
66
67     # Deleting and adding all records into Unbound can be a burden in a
68     # high density environment. Use unbound-control incrementally.
69     awk -v hostfile=$dns_ls_del -v domain=$UNBOUND_TXT_DOMAIN \
70         -v bslaac=$UNBOUND_B_SLAAC6_MAC -v bisolt=0 -v bconf=0 \
71         -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_del
72
73     awk -v hostfile=$dns_ls_add -v domain=$UNBOUND_TXT_DOMAIN \
74         -v bslaac=$UNBOUND_B_SLAAC6_MAC -v bisolt=0 -v bconf=0 \
75         -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_add
76
77
78     if [ -f "$dns_ls_del" ] ; then
79       cat $dns_ls_del | $UNBOUND_CONTROL_CFG local_datas_remove
80     fi
81
82
83     if [ -f "$dns_ls_add" ] ; then
84       cat $dns_ls_add | $UNBOUND_CONTROL_CFG local_datas
85     fi
86
87
88     # prepare next round
89     mv $dhcp_ls_new $dhcp_ls_old
90     rm -f $dns_ls_del $dns_ls_add $dhcp_ls_del $dhcp_ls_add
91   fi
92 }
93
94 ##############################################################################
95
96 odhcpd_zonedata
97
98 ##############################################################################
99