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.

198 lines
5.1 KiB

  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. # These are iptools that might be useful in a larger package, if provided
  18. # elsewhere for common use. One example that many may find useful is turning
  19. # flexible IPV6 colon dividers into PTR. Otherwise these are incomplete and
  20. # would need robustness improvements for more generic applications.
  21. #
  22. ##############################################################################
  23. domain_ptr_ip6() {
  24. # Get the nibble rounded /CIDR ...ip6.arpa.
  25. echo "$1" | awk -F: \
  26. 'BEGIN { OFS = "" ; }
  27. { CIDR = $0 ;
  28. sub(/.*\//,"",CIDR) ;
  29. CIDR = (CIDR / 4) ;
  30. sub(/\/[0-9]+/,"",$0) ;
  31. ct_stop = 9 - NF ;
  32. for(i=1; i<=NF; i++) {
  33. if(length($i) == 0) {
  34. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  35. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  36. y = $0 ;
  37. ct_start = length(y) - 32 + CIDR ;
  38. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  39. gsub(/./,"&\.",x) ;
  40. x = (x "ip6.arpa") ;
  41. print x }'
  42. }
  43. ##############################################################################
  44. host_ptr_ip6() {
  45. # Get complete host ...ip6.arpa.
  46. echo "$1" | awk -F: \
  47. 'BEGIN { OFS = "" ; }
  48. { sub(/\/[0-9]+/,"",$0) ;
  49. ct_stop = 9 - NF ;
  50. for(i=1; i<=NF; i++) {
  51. if(length($i) == 0) {
  52. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  53. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  54. y = $0 ;
  55. ct_start = length(y);
  56. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  57. sub(/[0-9]+\//,"",x) ;
  58. gsub(/./,"&\.",x) ;
  59. x = (x "ip6.arpa") ;
  60. print x }'
  61. }
  62. ##############################################################################
  63. domain_ptr_ip4() {
  64. # Get the byte rounded /CIDR ...in-addr.arpa.
  65. echo "$1" | awk \
  66. '{ CIDR = $0 ;
  67. sub(/.*\//,"",CIDR) ;
  68. CIDR = (CIDR / 8) ;
  69. dtxt = $0 ;
  70. sub(/\/.*/,"",dtxt) ;
  71. split(dtxt, dtxt, ".") ;
  72. for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
  73. x = (x "in-addr.arpa") ;
  74. print x }'
  75. }
  76. ##############################################################################
  77. host_ptr_ip4() {
  78. # Get omplete host ...in-addr.arpa.
  79. echo "$1" | awk -F. \
  80. '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
  81. sub(/\/[0-9]+/,"",x) ;
  82. print x }'
  83. }
  84. ##############################################################################
  85. valid_subnet6() {
  86. case "$1" in
  87. # GA
  88. [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  89. # ULA
  90. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  91. # fe80::, ::1, and such
  92. *) echo "not" ;;
  93. esac
  94. }
  95. ##############################################################################
  96. valid_subnet4() {
  97. case "$1" in
  98. # Link, Local, and Such
  99. 169"."254"."*) echo "not" ;;
  100. 127"."*) echo "not" ;;
  101. 0"."*) echo "not" ;;
  102. 255"."*) echo "not" ;;
  103. # Other Normal
  104. 25[0-4]"."[0-9]*) echo "ok" ;;
  105. 2[0-4][0-9]"."[0-9]*) echo "ok" ;;
  106. 1[0-9][0-9]"."[0-9]*) echo "ok" ;;
  107. [0-9][0-9]"."[0-9]*) echo "ok" ;;
  108. [0-9]"."[0-9]*) echo "ok" ;;
  109. # Not Right
  110. *) echo "not";;
  111. esac
  112. }
  113. ##############################################################################
  114. valid_subnet_any() {
  115. local subnet=$1
  116. local validip4=$( valid_subnet4 $subnet )
  117. local validip6=$( valid_subnet6 $subnet )
  118. if [ "$validip4" = "ok" ] || [ "$validip6" = "ok" ] ; then
  119. echo "ok"
  120. else
  121. echo "not"
  122. fi
  123. }
  124. ##############################################################################
  125. private_subnet() {
  126. case "$1" in
  127. 10"."*) echo "ok" ;;
  128. 172"."1[6-9]"."*) echo "ok" ;;
  129. 172"."2[0-9]"."*) echo "ok" ;;
  130. 172"."3[0-1]"."*) echo "ok" ;;
  131. 192"."168"."*) echo "ok" ;;
  132. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  133. *) echo "not" ;;
  134. esac
  135. }
  136. ##############################################################################
  137. domain_ptr_any() {
  138. local subnet=$1
  139. local arpa validip4 validip6
  140. validip4=$( valid_subnet4 $subnet )
  141. validip6=$( valid_subnet6 $subnet )
  142. if [ "$validip4" = "ok" ] ; then
  143. arpa=$( domain_ptr_ip4 "$subnet" )
  144. elif [ "$validip6" = "ok" ] ; then
  145. arpa=$( domain_ptr_ip6 "$subnet" )
  146. fi
  147. if [ -n "$arpa" ] ; then
  148. echo $arpa
  149. fi
  150. }
  151. ##############################################################################
  152. host_ptr_any() {
  153. local subnet=$1
  154. local arpa validip4 validip6
  155. validip4=$( valid_subnet4 $subnet )
  156. validip6=$( valid_subnet6 $subnet )
  157. if [ "$validip4" = "ok" ] ; then
  158. arpa=$( host_ptr_ip4 "$subnet" )
  159. elif [ "$validip6" = "ok" ] ; then
  160. arpa=$( host_ptr_ip6 "$subnet" )
  161. fi
  162. if [ -n "$arpa" ] ; then
  163. echo $arpa
  164. fi
  165. }
  166. ##############################################################################