asn_ipfire_v0.7.7_beta2_termux.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #!/usr/bin/env bash
  2. #######################################################################
  3. # IPFire network object creator for IPv4 addresses based on ASN information
  4. # Creates 'customnetworks' objects in /var/ipfire/fwhosts/customnetworks
  5. # Creates 'customgroups' objects in /var/ipfire/fwhosts/customgroups
  6. #######################################################################
  7. #######################################################################
  8. revision="asn_ipfire.sh v0.7.7_beta2_termux" # do not comment out
  9. # Last updated: March 18 2019 by maloe
  10. # Author: Mike Kuketz, maloe
  11. # Visit: www.kuketz-blog.de
  12. # Source: notabug.org/maloe/ASN_IPFire_Script
  13. # License: GNU GPL v3
  14. # Copyright 2017-2019 Mike Kuketz, maloe
  15. #######################################################################
  16. #######################################################################
  17. # Constants, Filenames, Enable/Disable Sources
  18. # Use download tool wget (default) or curl (if existent)
  19. downloadtool=wget
  20. #downloadtool=curl
  21. # Path to IPFire customnetworks|customgroups
  22. customnetworks=/var/ipfire/fwhosts/customnetworks
  23. customgroups=/var/ipfire/fwhosts/customgroups
  24. # Remark for IPFire customnetworks|customgroups. This is used to identify entries made by asn_ipfire.sh.
  25. auto_remark="entry by asn_ipfire.sh"
  26. # Define iptables path for iptable/afwall output file
  27. iptable_path="/sbin/iptables"
  28. afwall_path="/system/bin/iptables"
  29. # Output files
  30. file_network="network_list.txt" # output file for network consolidated
  31. file_network_raw="$file_network" # output file for network not consolidated
  32. file_iptable="iptable_rules.txt" # output file in iptable format
  33. file_afwall="afwall_rules.txt" # output file in afwall format
  34. file_asn="asn_list.txt" # output file for ASNs only
  35. temppath="." # path to store temporary source file (default: ".")
  36. temp1="asn_cidrreport.tmp" # Define temp file
  37. temp2="asn_bglooking.tmp" # Define temp file
  38. # Local files can be used as ASN and/or network sources. To be activated by enabling "gather_ASN0" and/or "gather_NET0" into following arrays.
  39. local_asn_file="local_asn.list" # Note: Each ASN must be in the same line as the corresponding company, e.g. 'AS1234 CompanyA' or 'CompanyA AS1234'
  40. local_net_file="local_net.list" # Note: Each network must be in the same line as the corresponding ASN, e.g. '1.2.3.4/24 AS5678' or 'AS5678 1.2.3.4/24'
  41. # Enable/disable ASN sources: Remove leading hashes (#) to enable ASN_sources.
  42. getASNfromCOMPANY=( \
  43. ASN_local \ # local source (local_asn_file)
  44. ASN_cidrreport \ # cidr-report.org
  45. # ASN_ultratools \ # ultratools.com
  46. # ASN_bglookingglass \ # bgplookingglass.com
  47. )
  48. # Enable/disable network sources: Remove leading hash (#) to enable NET_sources.
  49. getNETfromASN=( \
  50. NET_local \ # local source (local_net_file)
  51. NET_ripe \ # stat.ripe.net
  52. # NET_ipinfo \ # ipinfo.io
  53. # NET_radb \ # whois.radb.net # use only if you know what you are doing. query results may be wrong!
  54. )
  55. #######################################################################
  56. # Gather-Functions: add further sources here and activate them in above arrays getASNfromCOMPANY() and getNETfromASN()
  57. # ASN sources: function must return a list of ASNs
  58. ASN_local() # Get ASN from local file
  59. {
  60. if [[ -f $local_asn_file ]]; then
  61. echo "---[Using local ASN Source file ($local_asn_file)]---"
  62. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space
  63. asn_array=`cat $local_asn_file | grep -i "$cname" | grep -Eo 'AS[0-9]+'`
  64. fi;
  65. }
  66. ASN_cidrreport() # Get ASN from cidr-report.org
  67. {
  68. if [[ $dl != "local" ]]; then # wget or curl available?
  69. if [[ ! -f $temp1 ]] && [[ ${#company_array[@]} -gt 1 || $keeptemp ]]; then # Temp file not exist and more than one company names or option keeptemp is enabled
  70. touch $temp1 2> /dev/null # Temp file writable?
  71. if [[ -w $temp1 ]]; then # Write temp file
  72. echo "---[Downloading ASN Source List from www.cidr-report.org]---"
  73. $dl "https://www.cidr-report.org/as2.0/autnums.html" | grep -Eo '>AS[0-9]+.*' | sed 's/^>//; s/[ ]*<\/a>[ ]*/ /' >> $temp1
  74. fi
  75. fi
  76. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space and * with expression .*
  77. if [[ -f $temp1 ]]; then # Read from temp file
  78. if [[ $verbose ]]; then echo "---[Using local (temporary) ASN Source List ($temp1)]---"; fi
  79. asn_array=`cat $temp1 | grep -i "$cname" | grep -Eo '^AS[0-9]+'`
  80. else # Read from source
  81. echo "---[Downloading ASN Source List from www.cidr-report.org]---"
  82. asn_array=`$dl "https://www.cidr-report.org/as2.0/autnums.html" | grep -i "$cname" | grep -Eo '>AS[0-9]+' | grep -Eo 'AS[0-9]+'`
  83. fi;
  84. fi
  85. }
  86. ASN_ultratools() # Get ASN from ultratools.org
  87. {
  88. if [[ $dl != "local" ]]; then # wget or curl available?
  89. cname=`echo $1 | sed 's/~/ /g; s/+/%2B/g'` # Replace ~ with space and "+" with %2B
  90. asn_array=(`$dl "https://www.ultratools.com/tools/asnInfoResult?domainName=$cname" | grep -Eo 'AS[0-9]+' | uniq`)
  91. fi
  92. }
  93. ASN_bglookingglass() # Get ASN from bgplookingglass.com
  94. {
  95. if [[ $dl != "local" ]]; then # wget or curl available?
  96. if [[ ! -f $temp2 ]] && [[ ${#company_array[@]} -gt 1 || $keeptemp ]]; then # Temp file not exist and more than one company names or option keeptemp is enabled
  97. touch $temp2 2> /dev/null # Check if writable?
  98. if [[ -w $temp2 ]]; then
  99. echo "---[Downloading ASN Source List from www.bgplookingglass.com]---"
  100. $dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  101. $dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers-2" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  102. $dl "http://www.bgplookingglass.com/4-byte-asn-names-list" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  103. fi
  104. fi
  105. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space and * with expression .*
  106. if [[ -f $temp2 ]]; then # Read from temp file
  107. if [[ $verbose ]]; then echo "---[Using local (temporary) ASN Source List ($temp2)]---"; fi
  108. asn_array=`cat $temp2 | grep -i "$cname" | grep -Eo '^AS[0-9]+'`
  109. else # Temp file not writable
  110. echo "---[Downloading ASN Source List from www.bgplookingglass.com]---"
  111. asn_array=(`$dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  112. asn_array=(${asn_array[@]} `$dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers-2" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  113. asn_array=(${asn_array[@]} `$dl "http://www.bgplookingglass.com/4-byte-asn-names-list" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  114. fi
  115. fi
  116. }
  117. # Network sources: function must return a list of CIDR networks
  118. NET_local() # Get networks from local file, sorting
  119. {
  120. if [[ -f $local_net_file ]]; then
  121. cat $local_net_file | grep -i "$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -u | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4
  122. fi
  123. }
  124. NET_ripe() # Get networks from stat.ripe.net, sorting
  125. {
  126. if [[ $dl != "local" ]]; then # wget or curl available?
  127. $dl "https://stat.ripe.net/data/announced-prefixes/data.json?preferred_version=1.1&resource=$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -u | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4
  128. fi
  129. }
  130. NET_ipinfo() # Get networks from ipinfo.io, sorting
  131. {
  132. if [[ $dl != "local" ]]; then # wget or curl available?
  133. $dl "https://ipinfo.io/$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -u | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4
  134. fi
  135. }
  136. NET_radb() # Get networks from whois, sorting
  137. {
  138. if [[ -x `which whois 2>/dev/null` ]]; then # whois available?
  139. whois -h whois.radb.net -i origin $1 | grep -w "route:" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -u | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4
  140. fi
  141. }
  142. #######################################################################
  143. # NO NEED TO EDIT ANYTHING BELOW
  144. #######################################################################
  145. # Functions
  146. # Function: check existence of wget or curl
  147. chkSystem()
  148. {
  149. if [[ -d /var/ipfire ]] && [[ -f /etc/init.d/firewall ]]; then # Running on ipfire system?
  150. is_ipfire=1
  151. else
  152. is_ipfire=""
  153. fi
  154. dl=local
  155. if [[ -x `which wget 2>/dev/null` ]]; then
  156. dl="wget --quiet -O -" # Use wget if existent
  157. fi
  158. if [[ -x `which curl 2>/dev/null` ]]; then
  159. if [[ $dl == "local" ]] || [[ "$downloadtool" == "curl" ]]; then
  160. dl="curl --silent" # Use curl if existent and wget is missing
  161. fi
  162. fi
  163. if [[ $dl == "local" ]]; then
  164. echo "Warning: did not found wget nor curl. Only local sources usable." # Neither wget nor curl was found
  165. fi
  166. }
  167. # Function: get network mask
  168. cdr2mask()
  169. {
  170. # Number of args to shift, 255..255, first non-255 byte, zeroes
  171. set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
  172. [ $1 -gt 1 ] && shift $1 || shift
  173. echo ${1-0}.${2-0}.${3-0}.${4-0}
  174. }
  175. # Functions: get decimal IP values
  176. get_firstIP() { echo ${1/\//.} | awk -F"." '{ printf "%.0f", $1*2^24+$2*2^16+$3*2^8+$4 }'; } # First IP of network
  177. get_IPrange() { echo $1 | awk -F"/" '{ printf "%.0f", 2^(32-$2)}'; } # IP range of network
  178. get_lastIP() { echo ${1/\//.} | awk -F"." '{ printf "%.0f", $1*2^24+$2*2^16+$3*2^8+$4+2^(32-$5)}'; } # Last IP +1 of network
  179. # Function: transform decimal IP into dot noted IP
  180. dec2ip() { echo $1 | awk '{ printf "%i.%i.%i.%i", $1/(2^24), $1%(2^24)/(2^16), $1%(2^24)%(2^16)/(2^8), $1%(2^24)%(2^16)%(2^8)}'; }
  181. # Function: remove redundant networks
  182. rm_redundantIP() {
  183. declare -a array1=("${!1}") # Put $1 into new array
  184. declare -a array2=() # Create second array
  185. declare maxIP=0 # Initial IP for comparison
  186. declare n=0 # Counter for array
  187. for net in ${array1[@]}; do
  188. lastIP=`get_lastIP $net` # Get last IP(+1) of actual network
  189. if [[ `echo $lastIP $maxIP | awk '$1>$2 {printf 1}'` ]]; then # Comparing big integer. Only keep network if last IP is not covered by previous network
  190. array2[$n]=$net # Write actual network into second array
  191. maxIP=$lastIP # Update maximum IP(+1)
  192. n=$[n+1]
  193. fi
  194. done
  195. for net in ${array2[@]}; do # Return result
  196. if [ $net ]; then echo ${net}; fi # Skip empty lines
  197. done
  198. }
  199. # Function: consolidate adjacent networks
  200. rm_adjacentIP() {
  201. declare -a array1=("${!1}") # Put $1 into new array1
  202. declare -a array2=() # Create working array2
  203. declare n=0 # Counter for array2
  204. declare d=0 # Initial counter for adjacents
  205. declare range=0 # IP range
  206. declare prefirstIP=0 # Get decimal first IP from actual network
  207. declare prenetmask=0 # Get decimal IP range from actual network
  208. declare prelastIP=-1 # Get decimal last IP(+1) from actual network
  209. for net in ${array1[@]}; do # Loop through network list
  210. firstIP=`get_firstIP $net` # Get decimal first IP from actual network
  211. netmask=`get_IPrange $net` # Get decimal IP range from actual network
  212. lastIP=`get_lastIP $net` # Get decimal last IP(+1) from actual network
  213. range=`echo $prenetmask $netmask | awk '{printf "%d", $1+$2;}'`
  214. # If all adjecent consolidation requirements are given
  215. if [[ `echo $firstIP $prelastIP $range $prefirstIP | awk '($1==$2) && (log($3)/log(2)==int(log($3)/log(2))) && ($4%$3==0) {printf 1}'` ]]; then
  216. suffix=`echo $range | awk '{printf "%i", 32-log($1)/log(2);}'`
  217. array2[$[n-1]]=`dec2ip $prefirstIP`"/"$suffix
  218. prelastIP=$lastIP
  219. prenetmask=$range
  220. d=1
  221. else # No changes
  222. array2[$n]=$net
  223. prelastIP=$lastIP
  224. prefirstIP=$firstIP
  225. prenetmask=$netmask
  226. n=$[n+1]
  227. fi
  228. done
  229. if [[ d -eq 1 ]]; then # If changes done, then...
  230. d=0
  231. rm_adjacentIP array2[@] # ... next Iteration
  232. else
  233. for net in ${array2[@]} ; do # Return result
  234. if [ $net ]; then echo ${net}; fi # Skip empty lines
  235. done
  236. fi
  237. }
  238. # Function: print statistics
  239. show_stats() { # Requires arguments: asn_array net_array, company
  240. declare -a asn_array=("${!1}") # Put $1 (asn_list) into new array
  241. declare -a net_array=("${!2}") # Put $2 (net_list) into new array
  242. declare countIP=0 # Counter for IP
  243. for net in ${net_array[@]}; do
  244. netmask=`get_IPrange $net` # Get decimal IP range from actual network
  245. countIP=`echo $countIP $netmask | awk '{printf "%.0f", $1+$2}'` # Count IP
  246. done
  247. countIP=`printf "%'i\n" $countIP` # Point separated format
  248. echo "${#net_array[@]} networks with $countIP IPs found in ${#asn_array[@]} ASNs for $3"
  249. }
  250. #######################################################################
  251. # Main procedures
  252. addNetworks() {
  253. if [ ! $1 ]; then # Default ipfire mode
  254. # Get highest number from existing objects in [customnetworks|customgroups]
  255. if [[ -w $customnetworks ]]; then
  256. network_object_number=$(cat $customnetworks | cut -f1 -d',' | awk '{for(i=1;i<=NF;i++) if($i>maxval) maxval=$i;}; END { print maxval;}')
  257. else
  258. echo -e "File $customnetworks not found or write protected.\nCheck your IPFire installation."
  259. exit 0
  260. fi
  261. if [[ -w $customgroups ]]; then
  262. group_object_number=$(cat $customgroups | cut -f1 -d',' | awk '{for(i=1;i<=NF;i++) if($i>maxval) maxval=$i;}; END { print maxval;}')
  263. else
  264. echo -e "File $customgroups not found or write protected.\nCheck your IPFire installation."
  265. exit 0
  266. fi
  267. # Increase counter
  268. network_object_number=$[network_object_number +1]
  269. group_object_number=$[group_object_number +1]
  270. fi
  271. for company in ${company_array[@]}; do
  272. # Get all company ASNs
  273. declare asn_array=()
  274. declare asn_list=()
  275. prnt_company=`echo $company | sed 's/~/ /g; s/,//g'` # Printable company name with space (and no commas)
  276. echo "---[Get all $prnt_company ASNs]---"
  277. for asn_gather in ${getASNfromCOMPANY[@]}; do # Loop through ASN sources
  278. $asn_gather $company
  279. asn_list=(`echo ${asn_list[@]} ${asn_array[@]}`) # Append to list
  280. done
  281. if [ ! $asn_list ]; then
  282. echo "---[No ASN found for $prnt_company]---"
  283. else
  284. asn_list=(`echo ${asn_list[@]} | sed 's/ /\n/g' | sort -untS -k2,2`) # Linebreaking and Sorting
  285. declare net_array=()
  286. declare net_list=()
  287. for asn in ${asn_list[@]}; do
  288. # Store networks from ASN in file
  289. echo "---[Get $prnt_company networks for $asn]---"
  290. for net_gather in ${getNETfromASN[@]}; do # Loop through NET webservices
  291. net_array=(`$net_gather $asn`)
  292. net_list=(`echo ${net_list[@]} ${net_array[@]} | sed 's/ /\n/g' | sort -u | sort -nt. -k1,1 -k2,2 -k3,3 -k4,4`) # Append to list, sorting
  293. done
  294. done
  295. if [ ! $net_list ]; then
  296. echo "---[No networks found for $prnt_company]---"
  297. else
  298. # Consolidate adjacent and overlapping netblocks
  299. before=${#net_list[@]} # Number of network entries before consolidate
  300. if [[ $verbose ]]; then echo " $(show_stats asn_list[@] net_list[@] $company)"; fi
  301. if [ "$1" != "--network_raw" ]; then
  302. echo "---[Remove adjacent and overlapping netblocks]---"
  303. net_list=(`rm_redundantIP net_list[@]`) # Remove redundant networks
  304. net_list=(`rm_adjacentIP net_list[@]`) # Consolidate adjacent networks
  305. fi
  306. after=${#net_list[@]} # Number of network entries after consolidate
  307. if [[ $verbose ]]; then echo " $[$before - $after] of $before networks removed"; fi
  308. # Write objects to files
  309. echo "---[Creating objects for $prnt_company networks]---"
  310. case "$1" in # Check Mode
  311. "--asn") {
  312. output="### Company: ${prnt_company} ###\n"
  313. for net in ${asn_list[@]}; do
  314. output+="$net\n" # Create output text
  315. done
  316. echo -e $output >> $output_file # Write output to file
  317. };;
  318. --network|--network_raw) {
  319. output="### Company: ${prnt_company} ###\n"
  320. if [[ $verbose ]]; then output+="### $(show_stats asn_list[@] net_list[@] $company) ###\n"; fi
  321. for net in ${net_list[@]}; do
  322. output+="$net\n" # Create output text
  323. done
  324. echo -e $output >> $output_file # Write output to file
  325. };;
  326. --iptable) {
  327. output="### Company: ${prnt_company} ###\n"
  328. for net in ${net_list[@]}; do
  329. output+="$iptable_path -A OUTPUT -d $net -j REJECT\n" # Create output text
  330. done
  331. echo -e $output >> $output_file # Write output to file
  332. };;
  333. --afwall) {
  334. output="### Company: ${prnt_company} ###\n"
  335. for net in ${net_list[@]}; do
  336. output+="$afwall_path -A \"afwall\" -d $net -j REJECT\n" # Create output text
  337. done
  338. echo -e $output >> $output_file # Write output to file
  339. };;
  340. *) { # Default ipfire mode
  341. counter=1
  342. for net in ${net_list[@]}; do
  343. # Separate IP and netmask
  344. ip=${net%/*}
  345. if [ "$ip" != "0.0.0.0" ]; then # Double check for no empty lines
  346. netmask=${net#*/}
  347. # Write new objects to files [customnetworks|customgroups]
  348. ipf_company=`echo $prnt_company | sed 's/\(.*\)/\U\1/'` # Uppercase company name
  349. printf "$network_object_number,$company-Network Nr.$counter,$ip,$(cdr2mask $netmask),$auto_remark\n" >> $customnetworks
  350. printf "$group_object_number,$ipf_company,$auto_remark,$company-Network Nr.$counter,Custom Network\n" >> $customgroups
  351. # Increase counter
  352. network_object_number=$[$network_object_number +1]
  353. group_object_number=$[$group_object_number +1]
  354. counter=$[$counter +1]
  355. fi
  356. done
  357. };;
  358. esac
  359. if [[ $verbose ]]; then echo " $(show_stats asn_list[@] net_list[@] $company)"; fi
  360. echo "---[Result for ${prnt_company} written to $output_file]---" # Resultfile info
  361. fi
  362. fi
  363. done
  364. # remove temp files
  365. if [[ ! $keeptemp ]]; then
  366. echo "---[Removing temporary source files]---"
  367. if [[ -f $temp1 ]]; then rm $temp1; fi
  368. if [[ -f $temp2 ]]; then rm $temp2; fi
  369. fi
  370. }
  371. cleanupNetworks() { # Remove entries from ipfire files
  372. for ipfire_file in $customnetworks $customgroups; do
  373. if [[ -w $ipfire_file ]]; then
  374. if [[ $backup ]]; then
  375. if [[ $verbose ]]; then echo "---[Backing up $ipfire_file.bak ]---"; fi
  376. cp -f $ipfire_file $ipfire_file.bak # Create ipfire backup files
  377. fi
  378. if [[ ${company_array[0]} == "ALL" ]]; then # Remove all entries made by asn_ipfire.sh
  379. echo "---[Removing all objects from $ipfire_file ]---"
  380. sed -i "/,$auto_remark/Id" $ipfire_file;
  381. else
  382. for company in ${company_array[@]}; do
  383. prnt_company=`echo $company | sed 's/~/ /g; s/,//g; s/\(.*\)/\U\1/'` # Company name with space and "+"
  384. echo "---[Removing $prnt_company objects from $ipfire_file ]---"
  385. sed -i "/$company.*$auto_remark/Id" $ipfire_file; # Remove company entries made by asn_ipfire.sh
  386. done
  387. fi
  388. elif [[ -f $ipfire_file ]]; then
  389. echo -e "File $ipfire_file write protected.\nCheck your IPFire installation."
  390. fi
  391. done
  392. }
  393. removeBackup() { # Remove ipfire backup files
  394. for ipfire_file in $customnetworks $customgroups; do
  395. if [[ -w $ipfire_file.bak ]]; then
  396. if [[ $verbose ]]; then echo "---[Removing backup $ipfire_file.bak ]---"; fi
  397. rm -f $ipfire_file.bak
  398. fi
  399. done
  400. }
  401. renumberIpfireFiles() { # Remove entries from ipfire files
  402. for ipfire_file in $customnetworks $customgroups; do
  403. if [[ -w $ipfire_file ]]; then
  404. if [[ $verbose ]]; then echo "---[Renumbering $ipfire_file ]---"; fi
  405. sed -i '/^$/d;=' $ipfire_file # Delete empty lines and add numbered lines
  406. sed -i 'N;s/\n[0-9]\+//' $ipfire_file # Renumber lines by consolidation
  407. else
  408. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  409. fi
  410. done
  411. }
  412. sortIpfireFiles() { # Remove entries from ipfire files
  413. for ipfire_file in $customnetworks $customgroups; do
  414. if [[ -w $ipfire_file ]]; then
  415. if [[ $verbose ]]; then echo "---[Re-sorting $ipfire_file ]---"; fi
  416. cat $ipfire_file | sort -t, -k2,2V -o $ipfire_file # Sort lines by name
  417. sed -i '/^$/d;=' $ipfire_file # Delete empty lines and add numbered lines
  418. sed -i 'N;s/\n[0-9]\+//' $ipfire_file # Renumber lines by consolidation
  419. else
  420. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  421. fi
  422. done
  423. }
  424. restoreIpfireFiles() { # Restore ipfire file
  425. for ipfire_file in $customnetworks $customgroups; do
  426. if [[ -w $ipfire_file ]]; then
  427. if [[ -f "$ipfire_file.bak" ]]; then
  428. cp -f $ipfire_file.bak $ipfire_file
  429. echo "File $ipfire_file restored."
  430. else
  431. echo "No backup file $ipfire_file.bak found."
  432. fi
  433. else
  434. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  435. fi
  436. done
  437. }
  438. listIpfireFiles () { # Show companies from ipfire files
  439. for ipfire_file in $customnetworks $customgroups; do
  440. if [[ -f $ipfire_file ]]; then
  441. echo "Company names in "$ipfire_file":"
  442. cat $ipfire_file | grep "$auto_remark" | grep -Eo ',.*-Network Nr' | sort -u | cut -d, -f2 | sed 's/-Network Nr//'
  443. else
  444. echo -e "File $ipfire_file not found.\nCheck your IPFire installation."
  445. fi
  446. done
  447. }
  448. print_help() { # Help info
  449. echo "Usage: asn_ipfire.sh [OPTION] [COMPANYs | -f FILE]"
  450. echo "Add or remove networks to IPFire firewall Groups: Networks & Host Groups"
  451. echo
  452. echo "Options:"
  453. echo " -a, --add Add new company networks"
  454. echo " -r, --remove Remove company networks from customnetworks & customgroups"
  455. echo " -f, --file FILE Get company list from FILE"
  456. echo " -l, --list List of companies already added by this script"
  457. echo " -k, --keep Keep temporary source files after finish"
  458. echo " --renumber Renumber lines of customnetworks & customgroups"
  459. echo " --sort Re-sort lines of customnetworks & customgroups"
  460. echo " --backup Backup customnetworks & customgroups before change"
  461. echo " --rmbackup Remove backup files of customnetworks & customgroups"
  462. echo " --restore Restore customnetworks & customgroups from backup"
  463. echo " -v, --verbose Verbose mode"
  464. echo " -V, --version Show this script version and exit"
  465. echo " -h, --help Show this help and exit"
  466. echo
  467. echo "Create special output files (Non-IPFire-Mode):"
  468. echo " --network Create FILE '$file_network' with networks"
  469. echo " --network_raw dito, but networks not consolidated"
  470. echo " --asn Create FILE '$file_asn' with ASNs only"
  471. echo " --iptable Create FILE '$file_iptable' with iptable rules"
  472. echo " --afwall Create FILE '$file_afwall' with afwall rules"
  473. echo
  474. echo "COMPANY to be one or more company names, put into double quotes (\"...\")"
  475. echo " Multi company names must be comma separated"
  476. echo " Substitute spaces with tilde (~)"
  477. echo " Restrict to exact matches with tilde (~) before and after the name"
  478. echo " Company names are handled case insensitive."
  479. echo " example: asn_ipfire.sh --add \"CompanyA,Company~NameB,~CompanyC~\" "
  480. echo
  481. echo "FILE to be a name of a file, containing one or more company names."
  482. echo " Company names to be separated by comma or line feed."
  483. echo " examples: asn_ipfire.sh -a -f company.list "
  484. echo " asn_ipfire.sh --network -f company.list "
  485. echo
  486. echo "Option --remove only affects entries made by asn_ipfire.sh itself."
  487. echo " These entries are recognized by the 'Remark'-column in IPFire."
  488. echo " To remove all entries done by this script, use COMPANY='ALL' "
  489. echo " examples: asn_ipfire.sh -r \"CompanyA, CompanyB\" "
  490. echo " asn_ipfire.sh -r ALL "
  491. echo
  492. }
  493. #######################################################################
  494. # Main program
  495. company_array=() # Create empty company array
  496. mode="" # Initial mode
  497. verbose="" # Default verbose = OFF
  498. backup="" # Default backup of ipfire files = OFF
  499. keeptemp="" # Default Keep source temp file after finish = OFF
  500. temp1="$temppath/$temp1" # Source temp file
  501. temp2="$temppath/$temp2" # Source temp file
  502. helptext="Usage: asn_ipfire.sh [OPTION] [COMPANYs | -f FILE] \nTry 'asn_ipfire.sh --help' for more information."
  503. chkSystem # ipfire system? wget or curl available?
  504. # Check arguments and get company array
  505. if [[ $# -eq 0 ]]; then echo -e $helptext; exit 0; fi # No arguments --> exit
  506. if [[ $# -gt 6 ]]; then echo -e "Too many arguments.\n"$helptext; exit 0; fi # Too many arguments --> exit
  507. while [[ $# > 0 ]] ; do
  508. case $1 in
  509. -f | --file) {
  510. if [[ -f $2 ]]; then # File exist
  511. company_array_from_file=(`sed 's/[ ]*//g; s/,\+/ /g; s/\[//g; s/\]//g; s/[.\]*//g' <<< cat $2`) # Substitute space,comma,slash
  512. shift
  513. else # File not exist --> exit
  514. echo "Company file not found."
  515. echo -e $helptext
  516. exit 0
  517. fi
  518. };;
  519. -a|--add | -r|--remove | --asn | --network | --network_raw | --iptable | --afwall) {
  520. if [[ $mode ]]; then # Mode already set
  521. echo -e "Too many arguments.\n"$helptext
  522. exit 0
  523. else
  524. mode=$1
  525. if [[ ! $2 ]]; then
  526. echo -e "No COMPANY names given.\n"$helptext
  527. exit 0
  528. elif [[ ${2:0:1} == "-" ]]; then # followed by argument instead of company names
  529. if [[ "$2" != "-f" && "$2" != "--file" ]]; then # followed by argument instead of company names
  530. echo -e "Wrong order of arguments.\n"$helptext # Wrong order of arguments --> exit
  531. exit 0
  532. fi
  533. else
  534. company_array_from_arg=(`sed -E 's/[ ]*//g; s/[,]+/ /g; s/\[//g; s/\]//g; s/[.\]*//g' <<< $2`) # Trim empty entries
  535. shift
  536. fi
  537. fi
  538. };;
  539. -l|--list | --renumber | --sort | --restore | --rmbackup | -h|--help | -V|--version) {
  540. if [[ $mode ]] || [[ $2 ]]; then # No more arguments allowed for this option
  541. echo -e "Too many arguments.\n"$helptext # Too many parameter --> exit
  542. exit 0
  543. else
  544. mode=$1
  545. fi
  546. };;
  547. --backup ) { # Don't write backup Ipfire files
  548. if [[ ! $mode ]] && [[ ! $2 ]]; then
  549. echo -e "Missing arguments.\n"$helptext
  550. exit 0
  551. else
  552. backup=1
  553. fi
  554. };;
  555. -k|--keep ) { # Keep temporary source files
  556. if [[ ! $mode ]] && [[ ! $2 ]]; then
  557. echo -e "Missing arguments.\n"$helptext
  558. exit 0
  559. else
  560. keeptemp=1
  561. fi
  562. };;
  563. -v|--verbose ) { # Verbose mode shows stats
  564. if [[ ! $mode ]] && [[ ! $2 ]]; then
  565. echo -e "Missing arguments.\n"$helptext
  566. exit 0
  567. else
  568. verbose=1
  569. fi
  570. };;
  571. *) {
  572. echo -e "Unknown argument.\n"$helptext # Unknown arguments --> exit
  573. exit 0
  574. };;
  575. esac
  576. shift
  577. done
  578. company_array=(`echo ${company_array_from_file[@]} ${company_array_from_arg[@]} | sort -uf`)
  579. case $mode in
  580. -a|--add | -r|--remove) { # Add objects to ipfire files
  581. if [[ $is_ipfire ]]; then
  582. output_file="customnetworks & customgroups"
  583. if [ ! $company_array ]; then
  584. echo "No company names found. Nothing done!"
  585. echo "Try 'asn_ipfire.sh --help' for more information."
  586. exit 0
  587. fi
  588. cleanupNetworks # Remove existing entries
  589. renumberIpfireFiles # Renumbering
  590. if [[ $mode == "-a" || $mode == "--add" ]]; then
  591. addNetworks # Get networks and write to file
  592. fi
  593. echo "---[Restarting firewall]---"
  594. /etc/init.d/firewall restart 1> /dev/null # Restart firewall
  595. echo "---[All done!]---"
  596. else
  597. echo -e "IPFire not found.\nCheck your IPFire installation."
  598. fi
  599. };;
  600. -l|--list) { # Function: List all company names already there by asn_ipfire
  601. if [[ $is_ipfire ]]; then
  602. listIpfireFiles
  603. else
  604. echo -e "IPFire not found.\nCheck your IPFire installation."
  605. fi
  606. };;
  607. --renumber) {
  608. if [[ $is_ipfire ]]; then
  609. verbose=1
  610. renumberIpfireFiles
  611. else
  612. echo -e "IPFire not found.\nCheck your IPFire installation."
  613. fi
  614. };;
  615. --sort) {
  616. if [[ $is_ipfire ]]; then
  617. verbose=1
  618. sortIpfireFiles
  619. else
  620. echo -e "IPFire not found.\nCheck your IPFire installation."
  621. fi
  622. };;
  623. --rmbackup) {
  624. if [[ $is_ipfire ]]; then
  625. verbose=1
  626. removeBackup
  627. else
  628. echo -e "IPFire not found.\nCheck your IPFire installation."
  629. fi
  630. };;
  631. --restore) {
  632. if [[ $is_ipfire ]]; then
  633. restoreIpfireFiles
  634. else
  635. echo -e "IPFire not found.\nCheck your IPFire installation."
  636. fi
  637. };;
  638. --asn | --network | --network_raw | --iptable | --afwall ) { # Create special output files
  639. output_file="file_"${mode:2} # Get output file
  640. output_file="${!output_file}"
  641. if [ $company_array ]; then
  642. touch $output_file > $output_file
  643. addNetworks $mode # Get and add new networks
  644. echo "---[All done!]---"
  645. else
  646. echo "No company names found. Nothing done!"
  647. echo "Try 'asn_ipfire.sh --help' for more information."
  648. fi
  649. };;
  650. -V|--version ) { # Show version and quit
  651. echo $revision;
  652. };;
  653. -h|--help) {
  654. print_help # Show help and quit
  655. };;
  656. *) echo -e $helptext;; # Wrong or unknown parameter
  657. esac
  658. exit 0