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"
77 config_get section_server
$1 server
78 [ "$server" = "$section_server" ] ||
return 0 # continue to read next section if this is not for the current server
79 let count
++ # count nr of valid sections to make sure there are at least one
81 config_get localdev
$1 localdev
"*"
82 config_get remotedev
$1 remotedev
"*"
83 config_get vpntype
$1 vpntype
"*"
85 [ "$vpntype" == "ethernet" ] ||
[ "$vpntype" == "point-to-point" ] || append_string
"error" "[tunnelW: $1] vpntype must be \"ethernet\" (tap) or \"pointopoint\" (tun)" "; "
86 [ "$localdev" == "any" ] ||
[ "$localdev" -ge 0 ] || append_string
"error" "[tunnelW: $1] localdev must be an integer or \"any\"" "; "
87 [ "$remotedev" == "any" ] ||
[ "$remotedev" -ge 0 ] || append_string
"error" "[tunnelW: $1] remotedev must be an integer or \"any\"" "; "
88 [ "$user" == "root" ] || logger
-p user.warn
-t "sshtunnel" "warning: root is required unless the tunnel device has been created manually"
89 [ -n "$error" ] && return 1
91 append_string
"ARGS_tunnels" "-w $localdev:$remotedev -o Tunnel=$vpntype"
97 config_get user
$1 user
98 config_get hostname
$1 hostname
99 config_get retrydelay
$1 retrydelay
"60"
100 config_get PKCS11Provider
$1 PKCS11Provider
101 config_get CheckHostIP
$1 CheckHostIP
102 config_get Compression
$1 Compression
103 config_get CompressionLevel
$1 CompressionLevel
104 config_get IdentityFile
$1 IdentityFile
105 config_get LogLevel
$1 LogLevel
106 config_get ServerAliveCountMax
$1 ServerAliveCountMax
107 config_get ServerAliveInterval
$1 ServerAliveInterval
108 config_get StrictHostKeyChecking
$1 StrictHostKeyChecking
109 config_get TCPKeepAlive
$1 TCPKeepAlive
110 config_get VerifyHostKeyDNS
$1 VerifyHostKeyDNS
114 || append_string
"error" "user is not set" "; "
116 || append_string
"error" "hostname is not set" "; "
117 [ "$retrydelay" -ge 1 ] \
118 || append_string
"error" "retrydelay must be a positive integer" "; "
119 [ -z "$PKCS11Provider" -o -f "$PKCS11Provider" ] \
120 || append_string
"error" "PKCS11Provider must be a pkcs11 shared library accessible" "; "
121 [ -z "$CheckHostIP" -o "$CheckHostIP"="yes" -o "$CheckHostIP"="no" ] \
122 || append_string
"error" "CheckHostIP must be 'yes' or 'no'" "; "
123 [ -z "$Compression" -o "$Compression"="yes" -o "$Compression"="no" ] \
124 || append_string
"error" "Compression must be 'yes' or 'no'" "; "
125 [ -z "$CompressionLevel" ] ||
[ "$CompressionLevel" -ge 1 -a "$CompressionLevel" -le 9 ] \
126 || append_string
"error" "CompressionLevel must be between 1 and 9" "; "
127 [ -z "$IdentityFile" -o -f "$IdentityFile" ] \
128 || append_string
"error" "IdentityFile $IdentityFile not accessible" "; "
129 [ -z "$LogLevel" -o "$LogLevel" = "QUIET" -o "$LogLevel" = "FATAL" -o "$LogLevel" = "ERROR" -o \
130 "$LogLevel" = "INFO" -o "$LogLevel" = "VERBOSE" -o "$LogLevel" = "DEBUG" -o \
131 "$LogLevel" = "DEBUG1" -o "$LogLevel" = "DEBUG2" -o "$LogLevel" = "DEBUG3" ] \
132 || append_string
"error" "LogLevel is invalid" "; "
133 [ -z "$ServerAliveCountMax" ] ||
[ "$ServerAliveCountMax" -ge 1 ] \
134 || append_string
"error" "ServerAliveCountMax must be greater or equal than 1" "; "
135 [ -z "$ServerAliveInterval" ] ||
[ "$ServerAliveInterval" -ge 0 ] \
136 || append_string
"error" "ServerAliveInterval must be greater or equal than 0" "; "
137 [ -z "$StrictHostKeyChecking" -o "$StrictHostKeyChecking" = "yes" -o "$StrictHostKeyChecking" = "ask" -o "$StrictHostKeyChecking" = "no" ] \
138 || append_string
"error" "StrictHostKeyChecking must be 'yes', 'ask' or 'no'" "; "
139 [ -z "$TCPKeepAlive" -o "$TCPKeepAlive" = "yes" -o "$TCPKeepAlive" = "no" ] \
140 || append_string
"error" "TCPKeepAlive must be 'yes' or 'no'" "; "
141 [ -z "$VerifyHostKeyDNS" -o "$VerifyHostKeyDNS" = "yes" -o "$VerifyHostKeyDNS" = "no" ] \
142 || append_string
"error" "VerifyHostKeyDNS must be 'yes' or 'no'" "; "
144 [ -n "$error" ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - $error"; return; }
152 config_foreach load_tunnelR tunnelR
&& config_foreach load_tunnelL tunnelL
&& config_foreach load_tunnelD tunnelD
153 [ -n "$error" ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - $error"; return; }
154 [ "$count" -eq 0 ] && { logger
-p user.err
-t "sshtunnel" "tunnels to $server not started - no tunnels defined"; return; }
156 append_params CheckHostIP Compression CompressionLevel IdentityFile LogLevel PKCS11Provider ServerAliveCountMax ServerAliveInterval StrictHostKeyChecking TCPKeepAlive VerifyHostKeyDNS
157 ARGS
="$ARGS_options -o ExitOnForwardFailure=yes -o BatchMode=yes -nN $ARGS_tunnels $user@$hostname"
159 /usr
/bin
/sshtunnel.sh
"$ARGS" "$retrydelay" "$server" &
160 echo $
! >> "${PIDFILE}.pids"
161 logger
-p user.info
-t "sshtunnel" "started tunnels to $server (pid=$!;retrydelay=$retrydelay)"
165 if [ -f "$PIDFILE".pids
]
167 logger
-p user.info
-t "sshtunnel" "stopping all tunnels"
171 kill "$pid" # kill mother process first
173 [ -f "${PIDFILE}_${pid}.pid" ] && { # if ssh was running, kill it also (mother process could be in retry wait)
174 start-stop-daemon
-K -p "${PIDFILE}_${pid}.pid"
175 rm "${PIDFILE}_${pid}.pid"
178 logger
-p daemon.info
-t "sshtunnel[$pid]" "tunnel stopped"
180 done < "${PIDFILE}.pids"
184 logger
-p user.info
-t "sshtunnel" "all tunnels stopped"
186 logger
-p user.info
-t "sshtunnel" "no tunnels running"
191 [ -f "${PIDFILE}.pids" ] && stop
193 config_load sshtunnel
194 if [ -n "$(uci show sshtunnel.@server[0])" ] # at least one server section exists
196 logger
-p user.info
-t "sshtunnel" "starting all tunnels"
197 config_foreach load_server server
198 logger
-p user.info
-t "sshtunnel" "all tunnels started"
200 logger
-p user.info
-t "sshtunnel" "no servers defined"