Move libexpat, unbound into core and introduce hnsd
[librecmc/librecmc.git] / package / network / services / 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 #   "bconf"  = boolean, write conf file format rather than pipe records
26 #
27 ##############################################################################
28
29 /^#/ {
30   # We need to pick out DHCP v4 or v6 records
31   net = $2 ; id = $3 ; cls = $4 ; hst = $5 ; adr = $9 ; adr2 = $10
32   cdr = adr ;
33   cdr2 = adr2 ;
34   sub( /\/.*/, "", adr ) ;
35   sub( /.*\//, "", cdr ) ;
36   sub( /\/.*/, "", adr2 ) ;
37   sub( /.*\//, "", cdr2 ) ;
38
39
40   if ( hst !~ /^[[:alnum:]]([-[:alnum:]]*[[:alnum:]])?$/ ) {
41     # that is not a valid host name (RFC1123)
42     hst = "-" ;
43   }
44
45
46   if ( bisolt == 1 ) {
47     # TODO: this might be better with a substituion option,
48     # or per DHCP pool do-not-DNS option, but its getting busy here.
49     fqdn = net
50     fqdn = sub( /\./, "-", fqdn ) ;
51     fqdn = tolower( hst "." fqdn "." domain ) ;
52   }
53
54   else {
55     fqdn = tolower( hst "." domain ) ;
56   }
57
58
59   if ((cls == "ipv4") && (hst != "-") && (cdr == 32) && (NF == 9)) {
60     # IPV4 ; only for provided hostnames and full /32 assignments
61     # NF=9 ; odhcpd errata in field format without host name
62     ptr = adr ; qpr = "" ; split( ptr, ptr, "." ) ;
63     slaac = slaac_eui64( id ) ;
64
65
66     if ( bconf == 1 ) {
67       x = ( "local-data: \"" fqdn ". 300 IN A " adr "\"" ) ;
68       y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
69       print ( x "\n" y "\n" ) > hostfile ;
70     }
71
72     else {
73       for( i=1; i<=4; i++ ) { qpr = ( ptr[i] "." qpr) ; }
74       x = ( fqdn ". 300 IN A " adr ) ;
75       y = ( qpr "in-addr.arpa. 300 IN PTR " fqdn ) ;
76       print ( x "\n" y ) > hostfile ;
77     }
78
79
80     if (( bslaac == 1 ) && ( slaac != 0 )) {
81       # UCI option to discover IPV6 routed SLAAC addresses
82       # NOT TODO - ping probe take too long when added in awk-rule loop
83       cmd = ( "ip -6 --oneline route show dev " net ) ;
84
85
86       while ( ( cmd | getline adr ) > 0 ) {
87         if (( substr( adr, 1, 5 ) <= "fdff:" ) \
88         && ( index( adr, "anycast" ) == 0 ) \
89         && ( index( adr, "via" ) == 0 )) {
90           # GA or ULA routed addresses only (not LL or MC)
91           sub( /\/.*/, "", adr ) ;
92           adr = ( adr slaac ) ;
93
94
95           if ( split( adr, tmp0, ":" ) > 8 ) {
96             sub( "::", ":", adr ) ;
97           }
98
99
100           if ( bconf == 1 ) {
101             x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr "\"" ) ;
102             y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
103             print ( x "\n" y "\n" ) > hostfile ;
104           }
105
106           else {
107             qpr = ipv6_ptr( adr ) ;
108             x = ( fqdn ". 300 IN AAAA " adr ) ;
109             y = ( qpr ". 300 IN PTR " fqdn ) ;
110             print ( x "\n" y ) > hostfile ;
111           }
112         }
113       }
114
115
116       close( cmd ) ;
117     }
118   }
119
120   else if ((cls != "ipv4") && (hst != "-") && (9 <= NF) && (NF <= 10)) {
121     if (cdr == 128) {
122       if ( bconf == 1 ) {
123         x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr "\"" ) ;
124         y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
125         print ( x "\n" y "\n" ) > hostfile ;
126       }
127
128       else {
129         # only for provided hostnames and full /128 assignments
130         qpr = ipv6_ptr( adr ) ;
131         x = ( fqdn ". 300 IN AAAA " adr ) ;
132         y = ( qpr ". 300 IN PTR " fqdn ) ;
133         print ( x "\n" y ) > hostfile ;
134       }
135     }
136
137     if (cdr2 == 128) {
138       if ( bconf == 1 ) {
139         x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr2 "\"" ) ;
140         y = ( "local-data-ptr: \"" adr2 " 300 " fqdn "\"" ) ;
141         print ( x "\n" y "\n" ) > hostfile ;
142       }
143
144       else {
145         # odhcp puts GA and ULA on the same line (position 9 and 10)
146         qpr2 = ipv6_ptr( adr2 ) ;
147         x = ( fqdn ". 300 IN AAAA " adr2 ) ;
148         y = ( qpr2 ". 300 IN PTR " fqdn ) ;
149         print ( x "\n" y ) > hostfile ;
150       }
151     }
152   }
153
154   else {
155     # dump non-conforming lease records
156   }
157 }
158
159 ##############################################################################
160
161 function ipv6_ptr( ipv6,    arpa, ary, end, i, j, new6, sz, start ) {
162   # IPV6 colon flexibility is a challenge when creating [ptr].ip6.arpa.
163   sz = split( ipv6, ary, ":" ) ; end = 9 - sz ;
164
165
166   for( i=1; i<=sz; i++ ) {
167     if( length(ary[i]) == 0 ) {
168       for( j=1; j<=end; j++ ) { ary[i] = ( ary[i] "0000" ) ; }
169     }
170
171     else {
172       ary[i] = substr( ( "0000" ary[i] ), length( ary[i] )+5-4 ) ;
173     }
174   }
175
176
177   new6 = ary[1] ;
178   for( i = 2; i <= sz; i++ ) { new6 = ( new6 ary[i] ) ; }
179   start = length( new6 ) ;
180   for( i=start; i>0; i-- ) { arpa = ( arpa substr( new6, i, 1 ) ) ; } ;
181   gsub( /./, "&\.", arpa ) ; arpa = ( arpa "ip6.arpa" ) ;
182
183   return arpa ;
184 }
185
186 ##############################################################################
187
188 function slaac_eui64( mac,    ary, glbit, eui64 ) {
189   if ( length(mac) >= 12 ) {
190     # RFC2373 and use DHCPv4 registered MAC to find SLAAC addresses
191     split( mac , ary , "" ) ;
192     glbit = ( "0x" ary[2] ) ;
193     glbit = sprintf( "%d", glbit ) ;
194     glbit = or( glbit, 2 ) ;
195     ary[2] = sprintf( "%x", glbit ) ;
196     eui64 = ( ary[1] ary[2] ary[3] ary[4] ":" ary[5] ary[6] "ff:fe" ) ;
197     eui64 = ( eui64 ary[7] ary[8] ":" ary[9] ary[10]  ary[11] ary[12] ) ;
198   }
199
200   else {
201     eui64 = 0 ;
202   }
203
204
205   return eui64 ;
206 }
207
208 ##############################################################################
209