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.

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