You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.1 KiB

  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. # "conffile" = Unbound configuration left for a restart
  22. # "pipefile" = DNS entries for unbound-control standard input
  23. # "domain" = text domain suffix
  24. # "bslaac" = boolean, use DHCPv4 MAC to find GA and ULA IPV6 SLAAC
  25. # "bisolt" = boolean, format <host>.<network>.<domain>. so you can isolate
  26. # "bconf" = boolean, write conf file with pipe records
  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. if ( hst !~ /^[[:alnum:]]([-[:alnum:]]*[[:alnum:]])?$/ ) {
  39. # that is not a valid host name (RFC1123)
  40. hst = "-" ;
  41. }
  42. if ( bisolt == 1 ) {
  43. # TODO: this might be better with a substituion option,
  44. # or per DHCP pool do-not-DNS option, but its getting busy here.
  45. fqdn = net
  46. fqdn = sub( /\./, "-", fqdn ) ;
  47. fqdn = tolower( hst "." fqdn "." domain ) ;
  48. }
  49. else {
  50. fqdn = tolower( hst "." domain ) ;
  51. }
  52. if ((cls == "ipv4") && (hst != "-") && (cdr == 32) && (NF == 9)) {
  53. # IPV4 ; only for provided hostnames and full /32 assignments
  54. # NF=9 ; odhcpd errata in field format without host name
  55. ptr = adr ; qpr = "" ; split( ptr, ptr, "." ) ;
  56. slaac = slaac_eui64( id ) ;
  57. if ( bconf == 1 ) {
  58. x = ( "local-data: \"" fqdn ". 300 IN A " adr "\"" ) ;
  59. y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
  60. print ( x "\n" y "\n" ) > conffile ;
  61. }
  62. # always create the pipe file
  63. for( i=1; i<=4; i++ ) { qpr = ( ptr[i] "." qpr) ; }
  64. x = ( fqdn ". 300 IN A " adr ) ;
  65. y = ( qpr "in-addr.arpa. 300 IN PTR " fqdn ) ;
  66. print ( x "\n" y ) > pipefile ;
  67. if (( bslaac == 1 ) && ( slaac != 0 )) {
  68. # UCI option to discover IPV6 routed SLAAC addresses
  69. # NOT TODO - ping probe take too long when added in awk-rule loop
  70. cmd = ( "ip -6 --oneline route show dev " net ) ;
  71. while ( ( cmd | getline adr ) > 0 ) {
  72. if (( substr( adr, 1, 5 ) <= "fdff:" ) \
  73. && ( index( adr, "anycast" ) == 0 ) \
  74. && ( index( adr, "via" ) == 0 )) {
  75. # GA or ULA routed addresses only (not LL or MC)
  76. sub( /\/.*/, "", adr ) ;
  77. adr = ( adr slaac ) ;
  78. if ( split( adr, tmp0, ":" ) > 8 ) {
  79. sub( "::", ":", adr ) ;
  80. }
  81. if ( bconf == 1 ) {
  82. x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr "\"" ) ;
  83. y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
  84. print ( x "\n" y "\n" ) > conffile ;
  85. }
  86. # always create the pipe file
  87. qpr = ipv6_ptr( adr ) ;
  88. x = ( fqdn ". 300 IN AAAA " adr ) ;
  89. y = ( qpr ". 300 IN PTR " fqdn ) ;
  90. print ( x "\n" y ) > pipefile ;
  91. }
  92. }
  93. close( cmd ) ;
  94. }
  95. }
  96. else if ((cls != "ipv4") && (hst != "-") && (9 <= NF) && (NF <= 10)) {
  97. if (cdr == 128) {
  98. if ( bconf == 1 ) {
  99. x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr "\"" ) ;
  100. y = ( "local-data-ptr: \"" adr " 300 " fqdn "\"" ) ;
  101. print ( x "\n" y "\n" ) > conffile ;
  102. }
  103. # only for provided hostnames and full /128 assignments
  104. qpr = ipv6_ptr( adr ) ;
  105. x = ( fqdn ". 300 IN AAAA " adr ) ;
  106. y = ( qpr ". 300 IN PTR " fqdn ) ;
  107. print ( x "\n" y ) > pipefile ;
  108. }
  109. if (cdr2 == 128) {
  110. if ( bconf == 1 ) {
  111. x = ( "local-data: \"" fqdn ". 300 IN AAAA " adr2 "\"" ) ;
  112. y = ( "local-data-ptr: \"" adr2 " 300 " fqdn "\"" ) ;
  113. print ( x "\n" y "\n" ) > conffile ;
  114. }
  115. # odhcp puts GA and ULA on the same line (position 9 and 10)
  116. qpr2 = ipv6_ptr( adr2 ) ;
  117. x = ( fqdn ". 300 IN AAAA " adr2 ) ;
  118. y = ( qpr2 ". 300 IN PTR " fqdn ) ;
  119. print ( x "\n" y ) > pipefile ;
  120. }
  121. }
  122. else {
  123. # dump non-conforming lease records
  124. }
  125. }
  126. ##############################################################################
  127. function ipv6_ptr( ipv6, arpa, ary, end, i, j, new6, sz, start ) {
  128. # IPV6 colon flexibility is a challenge when creating [ptr].ip6.arpa.
  129. sz = split( ipv6, ary, ":" ) ; end = 9 - sz ;
  130. for( i=1; i<=sz; i++ ) {
  131. if( length(ary[i]) == 0 ) {
  132. for( j=1; j<=end; j++ ) { ary[i] = ( ary[i] "0000" ) ; }
  133. }
  134. else {
  135. ary[i] = substr( ( "0000" ary[i] ), length( ary[i] )+5-4 ) ;
  136. }
  137. }
  138. new6 = ary[1] ;
  139. for( i = 2; i <= sz; i++ ) { new6 = ( new6 ary[i] ) ; }
  140. start = length( new6 ) ;
  141. for( i=start; i>0; i-- ) { arpa = ( arpa substr( new6, i, 1 ) ) ; } ;
  142. gsub( /./, "&\.", arpa ) ; arpa = ( arpa "ip6.arpa" ) ;
  143. return arpa ;
  144. }
  145. ##############################################################################
  146. function slaac_eui64( mac, ary, glbit, eui64 ) {
  147. if ( length(mac) >= 12 ) {
  148. # RFC2373 and use DHCPv4 registered MAC to find SLAAC addresses
  149. split( mac , ary , "" ) ;
  150. glbit = ( "0x" ary[2] ) ;
  151. glbit = sprintf( "%d", glbit ) ;
  152. glbit = or( glbit, 2 ) ;
  153. ary[2] = sprintf( "%x", glbit ) ;
  154. eui64 = ( ary[1] ary[2] ary[3] ary[4] ":" ary[5] ary[6] "ff:fe" ) ;
  155. eui64 = ( eui64 ary[7] ary[8] ":" ary[9] ary[10] ary[11] ary[12] ) ;
  156. }
  157. else {
  158. eui64 = 0 ;
  159. }
  160. return eui64 ;
  161. }
  162. ##############################################################################