1
0

functions.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2015 Vitaly Protsko <villy@sft.ru>
  4. errno=0
  5. get_fieldval() {
  6. local __data="$3"
  7. local __rest
  8. test -z "$1" && return
  9. while true ; do
  10. __rest=${__data#* }
  11. test "$__rest" = "$__data" && break
  12. if [ "${__data/ *}" = "$2" ]; then
  13. eval "$1=${__rest/ *}"
  14. break
  15. fi
  16. __data="$__rest"
  17. done
  18. }
  19. manage_fw() {
  20. local cmd=/usr/sbin/iptables
  21. local mode
  22. local item
  23. if [ -z "$4" ]; then
  24. $log "Bad usage of manage_fw"
  25. errno=3; return 3
  26. fi
  27. case "$1" in
  28. add|up|1) mode=A ;;
  29. del|down|0) mode=D ;;
  30. *) return 3 ;;
  31. esac
  32. for item in $4 ; do
  33. $cmd -$mode forwarding_$2_rule -s $item -j ACCEPT
  34. $cmd -$mode output_$3_rule -d $item -j ACCEPT
  35. $cmd -$mode forwarding_$3_rule -d $item -j ACCEPT
  36. $cmd -t nat -$mode postrouting_$3_rule -d $item -j ACCEPT
  37. done
  38. }
  39. manage_sa() {
  40. local spdcmd
  41. local rtcmd
  42. local gate
  43. local litem
  44. local ritem
  45. if [ -z "$4" ]; then
  46. $log "Bad usage of manage_sa"
  47. errno=3; return 3
  48. fi
  49. case "$1" in
  50. add|up|1) spdcmd=add; rtcmd=add ;;
  51. del|down|0) spdcmd=delete; rtcmd=del ;;
  52. *) errno=3; return 3 ;;
  53. esac
  54. get_fieldval gate src "$(/usr/sbin/ip route get $4)"
  55. if [ -z "$gate" ]; then
  56. $log "Can not find outbound IP for $4"
  57. errno=3; return 3
  58. fi
  59. for litem in $2 ; do
  60. for ritem in $3 ; do
  61. echo "
  62. spd$spdcmd $litem $ritem any -P out ipsec esp/tunnel/$gate-$4/require;
  63. spd$spdcmd $ritem $litem any -P in ipsec esp/tunnel/$4-$gate/require;
  64. " | /usr/sbin/setkey -c 1>&2
  65. done
  66. done
  67. test -n "$5" && gate=$5
  68. for ritem in $3 ; do
  69. (sleep 3; /usr/sbin/ip route $rtcmd $ritem via $gate) &
  70. done
  71. }
  72. . /lib/functions/network.sh
  73. get_zoneiflist() {
  74. local item
  75. local data
  76. local addr
  77. item=0
  78. data=$(uci get firewall.@zone[0].name)
  79. while [ -n "$data" ]; do
  80. test "$data" = "$1" && break
  81. let "item=$item+1"
  82. data=$(uci get firewall.@zone[$item].name)
  83. done
  84. if [ -z "$data" ]; then
  85. errno=1
  86. return $errno
  87. fi
  88. data=$(uci get firewall.@zone[$item].network)
  89. echo "$data"
  90. }
  91. get_zoneiplist() {
  92. local item
  93. local addr
  94. local data
  95. local result
  96. data=$(get_zoneiflist $1)
  97. test $? -gt 0 -o $errno -gt 0 -o -z "$data" && return $errno
  98. for item in $data ; do
  99. if network_is_up $item ; then
  100. network_get_ipaddrs addr $item
  101. test $? -eq 0 && result="$result $addr"
  102. fi
  103. done
  104. result=$(echo $result)
  105. echo "$result"
  106. }
  107. # EOF /etc/racoon/functions.sh