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.

203 lines
8.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=99
  3. STOP=01
  4. PIDFILE="/tmp/run/sshtunnel"
  5. append_params() {
  6. local p; local v; local args;
  7. for p in $*; do
  8. eval "v=\$$p"
  9. [ -n "$v" ] && args="$args -o $p=$v"
  10. done
  11. ARGS_options="${args# *}"
  12. }
  13. append_string() {
  14. local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
  15. eval "actual=\$$varname"
  16. new="${actual:+$actual$separator}$add"
  17. eval "$varname=\$new"
  18. }
  19. load_tunnelR() {
  20. config_get section_server $1 server
  21. [ "$server" = "$section_server" ] || return 0 # continue to read next section if this is not for the current server
  22. let count++ # count nr of valid sections to make sure there are at least one
  23. config_get remoteaddress $1 remoteaddress "*"
  24. config_get remoteport $1 remoteport
  25. config_get localaddress $1 localaddress
  26. config_get localport $1 localport
  27. [ "$remoteport" -gt 0 ] || append_string "error" "[tunnelR: $1]remoteport must be a positive integer" "; "
  28. [ "$localport" -gt 0 ] || append_string "error" "[tunnelR: $1]localport must be a positive integer" "; "
  29. [ -n "$error" ] && return 1
  30. append_string "ARGS_tunnels" "-R $remoteaddress:$remoteport:$localaddress:$localport"
  31. }
  32. load_tunnelL() {
  33. config_get section_server $1 server
  34. [ "$server" = "$section_server" ] || return 0 # continue to read next section if this is not for the current server
  35. let count++ # count nr of valid sections to make sure there are at least one
  36. config_get localaddress $1 localaddress "*"
  37. config_get localport $1 localport
  38. config_get remoteaddress $1 remoteaddress
  39. config_get remoteport $1 remoteport
  40. [ "$remoteport" -gt 0 ] || append_string "error" "[tunnelL: $1]remoteport must be a positive integer" "; "
  41. [ "$localport" -gt 0 ] || append_string "error" "[tunnelL: $1]localport must be a positive integer" "; "
  42. [ -n "$error" ] && return 1
  43. append_string "ARGS_tunnels" "-L $localaddress:$localport:$remoteaddress:$remoteport"
  44. }
  45. load_tunnelD() {
  46. config_get section_server $1 server
  47. [ "$server" = "$section_server" ] || return 0 # continue to read next section if this is not for the current server
  48. let count++ # count nr of valid sections to make sure there are at least one
  49. config_get localaddress $1 localaddress "*"
  50. config_get localport $1 localport
  51. [ "$remoteport" -gt 0 ] || append_string "error" "[tunnelD: $1]remoteport must be a positive integer" "; "
  52. [ "$localport" -gt 0 ] || append_string "error" "[tunnelD: $1]localport must be a positive integer" "; "
  53. [ -n "$error" ] && return 1
  54. append_string "ARGS_tunnels" "-D $localaddress:$localport"
  55. }
  56. load_tunnelW() {
  57. config_get section_server $1 server
  58. [ "$server" = "$section_server" ] || return 0 # continue to read next section if this is not for the current server
  59. let count++ # count nr of valid sections to make sure there are at least one
  60. config_get localdev $1 localdev "*"
  61. config_get remotedev $1 remotedev "*"
  62. config_get vpntype $1 vpntype "*"
  63. [ "$vpntype" == "ethernet" ] || [ "$vpntype" == "point-to-point" ] || append_string "error" "[tunnelW: $1] vpntype must be \"ethernet\" (tap) or \"pointopoint\" (tun)" "; "
  64. [ "$localdev" == "any" ] || [ "$localdev" -ge 0 ] || append_string "error" "[tunnelW: $1] localdev must be an integer or \"any\"" "; "
  65. [ "$remotedev" == "any" ] || [ "$remotedev" -ge 0 ] || append_string "error" "[tunnelW: $1] remotedev must be an integer or \"any\"" "; "
  66. [ "$user" == "root" ] || logger -p user.warn -t "sshtunnel" "warning: root is required unless the tunnel device has been created manually"
  67. [ -n "$error" ] && return 1
  68. append_string "ARGS_tunnels" "-w $localdev:$remotedev -o Tunnel=$vpntype"
  69. }
  70. load_server() {
  71. server="$1"
  72. config_get user $1 user
  73. config_get hostname $1 hostname
  74. config_get port $1 port "22"
  75. config_get retrydelay $1 retrydelay "60"
  76. config_get PKCS11Provider $1 PKCS11Provider
  77. config_get CheckHostIP $1 CheckHostIP
  78. config_get Compression $1 Compression
  79. config_get CompressionLevel $1 CompressionLevel
  80. config_get IdentityFile $1 IdentityFile
  81. config_get LogLevel $1 LogLevel
  82. config_get ServerAliveCountMax $1 ServerAliveCountMax
  83. config_get ServerAliveInterval $1 ServerAliveInterval
  84. config_get StrictHostKeyChecking $1 StrictHostKeyChecking
  85. config_get TCPKeepAlive $1 TCPKeepAlive
  86. config_get VerifyHostKeyDNS $1 VerifyHostKeyDNS
  87. error=""
  88. [ -n "$user" ] \
  89. || append_string "error" "user is not set" "; "
  90. [ -n "$hostname" ] \
  91. || append_string "error" "hostname is not set" "; "
  92. [ "$retrydelay" -ge 1 ] \
  93. || append_string "error" "retrydelay must be a positive integer" "; "
  94. [ -z "$PKCS11Provider" -o -f "$PKCS11Provider" ] \
  95. || append_string "error" "PKCS11Provider must be a pkcs11 shared library accessible" "; "
  96. [ -z "$CheckHostIP" -o "$CheckHostIP"="yes" -o "$CheckHostIP"="no" ] \
  97. || append_string "error" "CheckHostIP must be 'yes' or 'no'" "; "
  98. [ -z "$Compression" -o "$Compression"="yes" -o "$Compression"="no" ] \
  99. || append_string "error" "Compression must be 'yes' or 'no'" "; "
  100. [ -z "$CompressionLevel" ] || [ "$CompressionLevel" -ge 1 -a "$CompressionLevel" -le 9 ] \
  101. || append_string "error" "CompressionLevel must be between 1 and 9" "; "
  102. [ -z "$IdentityFile" -o -f "$IdentityFile" ] \
  103. || append_string "error" "IdentityFile $IdentityFile not accessible" "; "
  104. [ -z "$LogLevel" -o "$LogLevel" = "QUIET" -o "$LogLevel" = "FATAL" -o "$LogLevel" = "ERROR" -o \
  105. "$LogLevel" = "INFO" -o "$LogLevel" = "VERBOSE" -o "$LogLevel" = "DEBUG" -o \
  106. "$LogLevel" = "DEBUG1" -o "$LogLevel" = "DEBUG2" -o "$LogLevel" = "DEBUG3" ] \
  107. || append_string "error" "LogLevel is invalid" "; "
  108. [ -z "$ServerAliveCountMax" ] || [ "$ServerAliveCountMax" -ge 1 ] \
  109. || append_string "error" "ServerAliveCountMax must be greater or equal than 1" "; "
  110. [ -z "$ServerAliveInterval" ] || [ "$ServerAliveInterval" -ge 0 ] \
  111. || append_string "error" "ServerAliveInterval must be greater or equal than 0" "; "
  112. [ -z "$StrictHostKeyChecking" -o "$StrictHostKeyChecking" = "yes" -o "$StrictHostKeyChecking" = "ask" -o "$StrictHostKeyChecking" = "no" ] \
  113. || append_string "error" "StrictHostKeyChecking must be 'yes', 'ask' or 'no'" "; "
  114. [ -z "$TCPKeepAlive" -o "$TCPKeepAlive" = "yes" -o "$TCPKeepAlive" = "no" ] \
  115. || append_string "error" "TCPKeepAlive must be 'yes' or 'no'" "; "
  116. [ -z "$VerifyHostKeyDNS" -o "$VerifyHostKeyDNS" = "yes" -o "$VerifyHostKeyDNS" = "no" ] \
  117. || append_string "error" "VerifyHostKeyDNS must be 'yes' or 'no'" "; "
  118. [ -n "$error" ] && { logger -p user.err -t "sshtunnel" "tunnels to $server not started - $error"; return; }
  119. ARGS=""
  120. ARGS_options=""
  121. ARGS_tunnels=""
  122. count=0
  123. config_foreach load_tunnelR tunnelR && config_foreach load_tunnelL tunnelL && config_foreach load_tunnelD tunnelD
  124. [ -n "$error" ] && { logger -p user.err -t "sshtunnel" "tunnels to $server not started - $error"; return; }
  125. [ "$count" -eq 0 ] && { logger -p user.err -t "sshtunnel" "tunnels to $server not started - no tunnels defined"; return; }
  126. append_params CheckHostIP Compression CompressionLevel IdentityFile LogLevel PKCS11Provider ServerAliveCountMax ServerAliveInterval StrictHostKeyChecking TCPKeepAlive VerifyHostKeyDNS
  127. ARGS="$ARGS_options -o ExitOnForwardFailure=yes -o BatchMode=yes -nN $ARGS_tunnels -p $port $user@$hostname"
  128. /usr/bin/sshtunnel.sh "$ARGS" "$retrydelay" "$server" &
  129. echo $! >> "${PIDFILE}.pids"
  130. logger -p user.info -t "sshtunnel" "started tunnels to $server (pid=$!;retrydelay=$retrydelay)"
  131. }
  132. stop() {
  133. if [ -f "$PIDFILE".pids ]
  134. then
  135. logger -p user.info -t "sshtunnel" "stopping all tunnels"
  136. while read pid
  137. do
  138. kill "$pid" # kill mother process first
  139. [ -f "${PIDFILE}_${pid}.pid" ] && { # if ssh was running, kill it also (mother process could be in retry wait)
  140. start-stop-daemon -K -p "${PIDFILE}_${pid}.pid"
  141. rm "${PIDFILE}_${pid}.pid"
  142. }
  143. logger -p daemon.info -t "sshtunnel[$pid]" "tunnel stopped"
  144. done < "${PIDFILE}.pids"
  145. rm "${PIDFILE}.pids"
  146. logger -p user.info -t "sshtunnel" "all tunnels stopped"
  147. else
  148. logger -p user.info -t "sshtunnel" "no tunnels running"
  149. fi
  150. }
  151. start() {
  152. [ -f "${PIDFILE}.pids" ] && stop
  153. config_load sshtunnel
  154. if [ -n "$(uci show sshtunnel.@server[0])" ] # at least one server section exists
  155. then
  156. logger -p user.info -t "sshtunnel" "starting all tunnels"
  157. config_foreach load_server server
  158. logger -p user.info -t "sshtunnel" "all tunnels started"
  159. else
  160. logger -p user.info -t "sshtunnel" "no servers defined"
  161. fi
  162. }