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.

217 lines
5.5 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. # while useful (sh)ellcheck is pedantic and noisy
  24. # shellcheck disable=1091,2002,2004,2034,2039,2086,2094,2140,2154,2155
  25. UB_IPTOOLS_BLANK=
  26. ##############################################################################
  27. domain_ptr_ip6() {
  28. # Get the nibble rounded /CIDR ...ip6.arpa.
  29. echo "$1" | awk -F: \
  30. 'BEGIN { OFS = "" ; }
  31. { CIDR = $0 ;
  32. sub(/.*\//,"",CIDR) ;
  33. CIDR = (CIDR / 4) ;
  34. sub(/\/[0-9]+/,"",$0) ;
  35. ct_stop = 9 - NF ;
  36. for(i=1; i<=NF; i++) {
  37. if(length($i) == 0) {
  38. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  39. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  40. y = $0 ;
  41. ct_start = length(y) - 32 + CIDR ;
  42. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  43. gsub(/./,"&\.",x) ;
  44. x = (x "ip6.arpa") ;
  45. print x }'
  46. }
  47. ##############################################################################
  48. host_ptr_ip6() {
  49. # Get complete host ...ip6.arpa.
  50. echo "$1" | awk -F: \
  51. 'BEGIN { OFS = "" ; }
  52. { sub(/\/[0-9]+/,"",$0) ;
  53. ct_stop = 9 - NF ;
  54. for(i=1; i<=NF; i++) {
  55. if(length($i) == 0) {
  56. for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
  57. else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
  58. y = $0 ;
  59. ct_start = length(y);
  60. for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
  61. sub(/[0-9]+\//,"",x) ;
  62. gsub(/./,"&\.",x) ;
  63. x = (x "ip6.arpa") ;
  64. print x }'
  65. }
  66. ##############################################################################
  67. domain_ptr_ip4() {
  68. # Get the byte rounded /CIDR ...in-addr.arpa.
  69. echo "$1" | awk \
  70. '{ CIDR = $0 ;
  71. sub(/.*\//,"",CIDR) ;
  72. CIDR = (CIDR / 8) ;
  73. dtxt = $0 ;
  74. sub(/\/.*/,"",dtxt) ;
  75. split(dtxt, dtxt, ".") ;
  76. for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
  77. x = (x "in-addr.arpa") ;
  78. print x }'
  79. }
  80. ##############################################################################
  81. host_ptr_ip4() {
  82. # Get complete host ...in-addr.arpa.
  83. echo "$1" | awk -F. \
  84. '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
  85. sub(/\/[0-9]+/,"",x) ;
  86. print x }'
  87. }
  88. ##############################################################################
  89. valid_subnet6() {
  90. case "$1" in
  91. # GA
  92. [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  93. # ULA
  94. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  95. # fe80::, ::1, and such
  96. *) echo "not" ;;
  97. esac
  98. }
  99. ##############################################################################
  100. valid_subnet4() {
  101. case "$1" in
  102. # Link, Local, and Such
  103. 169"."254"."*) echo "not" ;;
  104. 127"."*) echo "not" ;;
  105. 0"."*) echo "not" ;;
  106. 255"."*) echo "not" ;;
  107. # Other Normal
  108. 25[0-4]"."[0-9]*) echo "ok" ;;
  109. 2[0-4][0-9]"."[0-9]*) echo "ok" ;;
  110. 1[0-9][0-9]"."[0-9]*) echo "ok" ;;
  111. [0-9][0-9]"."[0-9]*) echo "ok" ;;
  112. [0-9]"."[0-9]*) echo "ok" ;;
  113. # Not Right
  114. *) echo "not";;
  115. esac
  116. }
  117. ##############################################################################
  118. valid_subnet_any() {
  119. local subnet=$1
  120. local validip4=$( valid_subnet4 $subnet )
  121. local validip6=$( valid_subnet6 $subnet )
  122. if [ "$validip4" = "ok" ] || [ "$validip6" = "ok" ] ; then
  123. echo "ok"
  124. else
  125. echo "not"
  126. fi
  127. }
  128. ##############################################################################
  129. private_subnet() {
  130. case "$1" in
  131. 10"."*) echo "ok" ;;
  132. 172"."1[6-9]"."*) echo "ok" ;;
  133. 172"."2[0-9]"."*) echo "ok" ;;
  134. 172"."3[0-1]"."*) echo "ok" ;;
  135. 192"."168"."*) echo "ok" ;;
  136. f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
  137. *) echo "not" ;;
  138. esac
  139. }
  140. ##############################################################################
  141. local_subnet() {
  142. # local subnet 2nd place is limited to one digit to improve the filter
  143. case "$1" in
  144. 127"."[0-9]"."[0-9]*) echo "ok" ;;
  145. ::1) echo "ok" ;;
  146. *) echo "not" ;;
  147. esac
  148. }
  149. ##############################################################################
  150. domain_ptr_any() {
  151. local subnet=$1
  152. local arpa validip4 validip6
  153. validip4=$( valid_subnet4 $subnet )
  154. validip6=$( valid_subnet6 $subnet )
  155. if [ "$validip4" = "ok" ] ; then
  156. arpa=$( domain_ptr_ip4 "$subnet" )
  157. elif [ "$validip6" = "ok" ] ; then
  158. arpa=$( domain_ptr_ip6 "$subnet" )
  159. fi
  160. if [ -n "$arpa" ] ; then
  161. echo $arpa
  162. fi
  163. }
  164. ##############################################################################
  165. host_ptr_any() {
  166. local subnet=$1
  167. local arpa validip4 validip6
  168. validip4=$( valid_subnet4 $subnet )
  169. validip6=$( valid_subnet6 $subnet )
  170. if [ "$validip4" = "ok" ] ; then
  171. arpa=$( host_ptr_ip4 "$subnet" )
  172. elif [ "$validip6" = "ok" ] ; then
  173. arpa=$( host_ptr_ip6 "$subnet" )
  174. fi
  175. if [ -n "$arpa" ] ; then
  176. echo $arpa
  177. fi
  178. }
  179. ##############################################################################