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.

172 lines
3.0 KiB

  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. manage_nonesa() {
  73. local spdcmd
  74. local item
  75. local cout cin
  76. if [ -z "$4" ]; then
  77. $log "Bad usage of manage_nonesa"
  78. errno=3; return 3
  79. fi
  80. case "$1" in
  81. add|up|1) spdcmd=add ;;
  82. del|down|0) spdcmd=delete ;;
  83. *) errno=3; return 3 ;;
  84. esac
  85. case "$2" in
  86. local|remote) ;;
  87. *) errno=3; return 3 ;;
  88. esac
  89. for item in $3 ; do
  90. if [ "$2" = "local" ]; then
  91. cout="$4 $item"
  92. cin="$item $4"
  93. else
  94. cout="$item $4"
  95. cin="$4 $item"
  96. fi
  97. echo "
  98. spd$spdcmd $cout any -P out none;
  99. spd$spdcmd $cin any -P in none;
  100. " | /usr/sbin/setkey -c 1>&2
  101. done
  102. }
  103. . /lib/functions/network.sh
  104. get_zoneiflist() {
  105. local item
  106. local data
  107. local addr
  108. item=0
  109. data=$(uci get firewall.@zone[0].name)
  110. while [ -n "$data" ]; do
  111. test "$data" = "$1" && break
  112. let "item=$item+1"
  113. data=$(uci get firewall.@zone[$item].name)
  114. done
  115. if [ -z "$data" ]; then
  116. errno=1
  117. return $errno
  118. fi
  119. data=$(uci get firewall.@zone[$item].network)
  120. echo "$data"
  121. }
  122. get_zoneiplist() {
  123. local item
  124. local addr
  125. local data
  126. local result
  127. data=$(get_zoneiflist $1)
  128. test $? -gt 0 -o $errno -gt 0 -o -z "$data" && return $errno
  129. for item in $data ; do
  130. if network_is_up $item ; then
  131. network_get_ipaddrs addr $item
  132. test $? -eq 0 && result="$result $addr"
  133. fi
  134. done
  135. result=$(echo $result)
  136. echo "$result"
  137. }
  138. # EOF /etc/racoon/functions.sh