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.

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