Unbound: bug fix odhcpd and add auto adblock
[feed/packages.git] / net / unbound / files / odhcpd.awk
1 #!/usr/bin/awk
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 # Turn DHCP records into meaningful A, AAAA, and PTR records. Also lift a
18 # function from dnsmasq and use DHCPv4 MAC to find IPV6 SLAAC hosts.
19 #
20 # External Parameters
21 # "hostfile" = where this script will cache host DNS data
22 # "domain" = text domain suffix
23 # "bslaac" = boolean, use DHCPv4 MAC to find GA and ULA IPV6 SLAAC
24 # "bisolt" = boolean, format <host>.<network>.<domain>. so you can isolate
25 #
26 ##############################################################################
27
28 /^#/ {
29 # We need to pick out DHCP v4 or v6 records
30 net = $2 ; id = $3 ; cls = $4 ; hst = $5 ; adr = $9 ;
31 cdr = adr ;
32 sub( /\/.*/, "", adr ) ;
33 sub( /.*\//, "", cdr ) ;
34
35
36 if ( bisolt == 1 ) {
37 # TODO: this might be better with a substituion option,
38 # or per DHCP pool do-not-DNS option, but its getting busy here.
39 fqdn = net
40 fqdn = sub( /\./, "-", fqdn ) ;
41 fqdn = tolower( hst "." fqdn "." domain ) ;
42 }
43
44 else {
45 fqdn = tolower( hst "." domain ) ;
46 }
47
48
49 if ( cls == "ipv4" ) {
50 if ( NF == 8 ) {
51 # odhcpd errata in field format without host name
52 adr = $8 ; hst = "-" ; cdr = adr ;
53 sub( /\/.*/, "", adr ) ;
54 sub( /.*\//, "", cdr ) ;
55 }
56
57
58 if (( cdr == 32 ) && ( hst != "-" )) {
59 # only for provided hostnames and full /32 assignments
60 ptr = adr ; qpr = "" ; split( ptr, ptr, "." ) ;
61 slaac = slaac_eui64( id ) ;
62 for( i=1; i<=4; i++ ) { qpr = ( ptr[i] "." qpr) ; }
63
64 # DHCP A and PTR records with FQDN
65 x = ( fqdn ". 120 IN A " adr ) ;
66 y = ( qpr "in-addr.arpa. 120 IN PTR " fqdn ) ;
67 print ( x "\n" y ) > hostfile ;
68
69
70 if ((bslaac == 1) && (slaac != 0)) {
71 # UCI option to discover IPV6 routed SLAAC addresses
72 # NOT TODO - ping probe take too long when added in awk-rule loop
73 cmd = ( "ip -6 --oneline route show dev " net ) ;
74
75
76 while ( ( cmd | getline adr ) > 0 ) {
77 if (( substr( adr, 1, 5 ) <= "fd00:" ) \
78 && ( index( adr, "via" ) == 0 )) {
79 # GA or ULA routed addresses only (not LL or MC)
80 sub( /\/.*/, "", adr ) ;
81 adr = ( adr slaac ) ;
82 if ( split( adr, tmp0, ":" ) >= 8 ) { sub( "::", ":", adr ) ; }
83 qpr = ipv6_ptr( adr ) ;
84 x = ( fqdn ". 120 IN AAAA " adr ) ;
85 y = ( qpr " 120 IN PTR " fqdn ) ;
86 print ( x "\n" y ) > hostfile ;
87 }
88 }
89
90
91 close( cmd ) ;
92 }
93 }
94 }
95
96 else {
97 if (( cdr == 128 ) && ( hst != "-" )) {
98 # only for provided hostnames and full /128 assignments
99 qpr = ipv6_ptr( adr ) ;
100 x = ( fqdn ". 120 IN AAAA " adr ) ;
101 y = ( qpr " 120 IN PTR " fqdn ) ;
102 print ( x "\n" y ) > hostfile ;
103 }
104 }
105 }
106
107 ##############################################################################
108
109 function ipv6_ptr( ipv6, arpa, ary, end, i, j, new6, sz, start ) {
110 # IPV6 colon flexibility is a challenge when creating [ptr].ip6.arpa.
111 sz = split( ipv6, ary, ":" ) ; end = 9 - sz ;
112
113
114 for( i=1; i<=sz; i++ ) {
115 if( length(ary[i]) == 0 ) {
116 for( j=1; j<=end; j++ ) { ary[i] = ( ary[i] "0000" ) ; }
117 }
118
119 else {
120 ary[i] = substr( ( "0000" ary[i] ), length( ary[i] )+5-4 ) ;
121 }
122 }
123
124
125 new6 = ary[1] ;
126 for( i = 2; i <= sz; i++ ) { new6 = ( new6 ary[i] ) ; }
127 start = length( new6 ) ;
128 for( i=start; i>0; i-- ) { arpa = ( arpa substr( new6, i, 1 ) ) ; } ;
129 gsub( /./, "&\.", arpa ) ; arpa = ( arpa "ip6.arpa" ) ;
130
131 return arpa ;
132 }
133
134 ##############################################################################
135
136 function slaac_eui64( mac, ary, glbit, eui64 ) {
137 if ( length(mac) >= 12 ) {
138 # RFC2373 and use DHCPv4 registered MAC to find SLAAC addresses
139 split( mac , ary , "" ) ;
140 glbit = ( "0x" ary[2] ) ;
141 glbit = sprintf( "%d", glbit ) ;
142 glbit = or( glbit, 2 ) ;
143 ary[2] = sprintf( "%x", glbit ) ;
144 eui64 = ( ary[1] ary[2] ary[3] ary[4] ":" ary[5] ary[6] "ff:fe" ) ;
145 eui64 = ( eui64 ary[7] ary[8] ":" ary[9] ary[10] ary[11] ary[12] ) ;
146 }
147
148 else {
149 eui64 = 0 ;
150 }
151
152
153 return eui64 ;
154 }
155
156 ##############################################################################
157