1 #!/bin/sh /etc/rc.common
6 PIDFILE
="/tmp/run/sshtunnel"
10 local p
; local v
; local args
;
13 [ -n "$v" ] && args
="$args -o $p=$v"
16 ARGS_options
="${args# *}"
20 local varname
="$1"; local add
="$2"; local separator
="${3:- }"; local actual
21 eval "actual=\$$varname"
23 new
="${actual:+$actual$separator}$add"
28 config_get section_server
$1 server
29 [ "$server" = "$section_server" ] ||
return 0 # continue to read next section if this is not for the current server
30 let count
++ # count nr of valid sections to make sure there are at least one
32 config_get remoteaddress
$1 remoteaddress
"*"
33 config_get remoteport
$1 remoteport
34 config_get localaddress
$1 localaddress
35 config_get localport
$1 localport
37 [ "$remoteport" -gt 0 ] || append_string
"error" "[tunnelR: $1]remoteport must be a positive integer" "; "
38 [ "$localport" -gt 0 ] || append_string
"error" "[tunnelR: $1]localport must be a positive integer" "; "
39 [ -n "$error" ] && return 1
41 append_string
"ARGS_tunnels" "-R $remoteaddress:$remoteport:$localaddress:$localport"
45 config_get section_server
$1 server
46 [ "$server" = "$section_server" ] ||
return 0 # continue to read next section if this is not for the current server
47 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 config_get remoteaddress
$1 remoteaddress
52 config_get remoteport
$1 remoteport
54 [ "$remoteport" -gt 0 ] || append_string
"error" "[tunnelL: $1]remoteport must be a positive integer" "; "
55 [ "$localport" -gt 0 ] || append_string
"error" "[tunnelL: $1]localport must be a positive integer" "; "
56 [ -n "$error" ] && return 1
58 append_string
"ARGS_tunnels" "-L $localaddress:$localport:$remoteaddress:$remoteport"
62 config_get section_server
$1 server
63 [ "$server" = "$section_server" ] ||
return 0 # continue to read next section if this is not for the current server
64 let count
++ # count nr of valid sections to make sure there are at least one
66 config_get localaddress
$1 localaddress
"*"
67 config_get localport
$1 localport
69 [ "$remoteport" -gt 0 ] || append_string
"error" "[tunnelD: $1]remoteport must be a positive integer" "; "
70 [ "$localport" -gt 0 ] || append_string
"error" "[tunnelD: $1]localport must be a positive integer" "; "
71 [ -n "$error" ] && return 1
73 append_string
"ARGS_tunnels" "-D $localaddress:$localport"
79 config_get user
$1 user
80 config_get hostname
$1 hostname
81 config_get retrydelay
$1 retrydelay
"10"
82 config_get PKCS11Provider
$1 PKCS11Provider
83 config_get CheckHostIP
$1 CheckHostIP
84 config_get Compression
$1 Compression
85 config_get CompressionLevel
$1 CompressionLevel
86 config_get IdentityFile
$1 IdentityFile
87 config_get LogLevel
$1 LogLevel
88 config_get ServerAliveCountMax
$1 ServerAliveCountMax
89 config_get ServerAliveInterval
$1 ServerAliveInterval
90 config_get StrictHostKeyChecking
$1 StrictHostKeyChecking
91 config_get TCPKeepAlive
$1 TCPKeepAlive
92 config_get VerifyHostKeyDNS
$1 VerifyHostKeyDNS
96 || append_string
"error" "user is not set" "; "
98 || append_string
"error" "hostname is not set" "; "
99 [ "$retrydelay" -ge 1 ] \
100 || append_string
"error" "retrydelay must be a positive integer" "; "
101 [ -z "$PKCS11Provider" -o -f "$PKCS11Provider" ] \
102 || append_string
"error" "PKCS11Provider must be a pkcs11 shared library accessible" "; "
103 [ -z "$CheckHostIP" -o "$CheckHostIP"="yes" -o "$CheckHostIP"="no" ] \
104 || append_string
"error" "CheckHostIP must be 'yes' or 'no'" "; "
105 [ -z "$Compression" -o "$Compression"="yes" -o "$Compression"="no" ] \
106 || append_string
"error" "Compression must be 'yes' or 'no'" "; "
107 [ -z "$CompressionLevel" ] ||
[ "$CompressionLevel" -ge 1 -a "$CompressionLevel" -le 9 ] \
108 || append_string
"error" "CompressionLevel must be between 1 and 9" "; "
109 [ -z "$IdentityFile" -o -f "$IdentityFile" ] \
110 || append_string
"error" "IdentityFile $IdentityFile not accessible" "; "
111 [ -z "$LogLevel" -o "$LogLevel" = "QUIET" -o "$LogLevel" = "FATAL" -o "$LogLevel" = "ERROR" -o \
112 "$LogLevel" = "INFO" -o "$LogLevel" = "VERBOSE" -o "$LogLevel" = "DEBUG" -o \
113 "$LogLevel" = "DEBUG1" -o "$LogLevel" = "DEBUG2" -o "$LogLevel" = "DEBUG3" ] \
114 || append_string
"error" "LogLevel is invalid" "; "
115 [ -z "$ServerAliveCountMax" ] ||
[ "$ServerAliveCountMax" -ge 1 ] \
116 || append_string
"error" "ServerAliveCountMax must be greater or equal than 1" "; "
117 [ -z "$ServerAliveInterval" ] ||
[ "$ServerAliveInterval" -ge 0 ] \
118 || append_string
"error" "ServerAliveInterval must be greater or equal than 0" "; "
119 [ -z "$StrictHostKeyChecking" -o "$StrictHostKeyChecking" = "yes" -o "$StrictHostKeyChecking" = "ask" -o "$StrictHostKeyChecking" = "no" ] \
120 || append_string
"error" "StrictHostKeyChecking must be 'yes', 'ask' or 'no'" "; "
121 [ -z "$TCPKeepAlive" -o "$TCPKeepAlive" = "yes" -o "$TCPKeepAlive" = "no" ] \
122 || append_string
"error" "TCPKeepAlive must be 'yes' or 'no'" "; "
123 [ -z "$VerifyHostKeyDNS" -o "$VerifyHostKeyDNS" = "yes" -o "$VerifyHostKeyDNS" = "no" ] \
124 || append_string
"error" "VerifyHostKeyDNS must be 'yes' or 'no'" "; "
126 [ -n "$error" ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - $error"; return; }
134 config_foreach load_tunnelR tunnelR
&& config_foreach load_tunnelL tunnelL
&& config_foreach load_tunnelD tunnelD
135 [ -n "$error" ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - $error"; return; }
136 [ "$count" -eq 0 ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - no tunnels defined"; return; }
138 append_params CheckHostIP Compression CompressionLevel IdentityFile LogLevel PKCS11Provider ServerAliveCountMax ServerAliveInterval StrictHostKeyChecking TCPKeepAlive VerifyHostKeyDNS
139 ARGS
="$ARGS_options -o ExitOnForwardFailure=yes -o BatchMode=yes -nN $ARGS_tunnels $user@$hostname"
141 /usr
/bin
/sshtunnel.sh
"$ARGS" "$retrydelay" "$server" &
142 echo $
! >> "${PIDFILE}.pids"
143 logger
-p user.info
-t "sshtunnel" "started tunnels to $server (pid=$!;retrydelay=$retrydelay)"
147 if [ -f "$PIDFILE".pids
]
149 logger
-p user.info
-t "sshtunnel" "stopping all tunnels"
153 kill "$pid" # kill mother process first
155 [ -f "${PIDFILE}_${pid}.pid" ] && { # if ssh was running, kill it also (mother process could be in retry wait)
156 start-stop-daemon
-K -p "${PIDFILE}_${pid}.pid"
157 rm "${PIDFILE}_${pid}.pid"
160 logger
-p daemon.info
-t "sshtunnel[$pid]" "tunnel stopped"
162 done < "${PIDFILE}.pids"
166 logger
-p user.info
-t "sshtunnel" "all tunnels stopped"
168 logger
-p user.info
-t "sshtunnel" "no tunnels running"
173 [ -f "${PIDFILE}.pids" ] && stop
175 config_load sshtunnel
176 if [ -n "$(uci show sshtunnel.@server[0])" ] # at least one server section exists
178 logger
-p user.info
-t "sshtunnel" "starting all tunnels"
179 config_foreach load_server server
180 logger
-p user.info
-t "sshtunnel" "all tunnels started"
182 logger
-p user.info
-t "sshtunnel" "no servers defined"