3e585634d139cd62a4be37624bbd00e636e59787
[openwrt/svn-archive/archive.git] / net / sshtunnel / files / initd_sshtunnel
1 #!/bin/sh /etc/rc.common
2
3 START=99
4 STOP=01
5
6 PIDFILE="/tmp/run/sshtunnel"
7
8
9 append_params() {
10 local p; local v; local args;
11 for p in $*; do
12 eval "v=\$$p"
13 [ -n "$v" ] && args="$args -o $p=$v"
14 done
15
16 ARGS_options="${args# *}"
17 }
18
19 append_string() {
20 local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
21 eval "actual=\$$varname"
22
23 new="${actual:+$actual$separator}$add"
24 eval "$varname=\$new"
25 }
26
27 load_tunnelR() {
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
31
32 config_get remoteaddress $1 remoteaddress "*"
33 config_get remoteport $1 remoteport
34 config_get localaddress $1 localaddress
35 config_get localport $1 localport
36
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
40
41 append_string "ARGS_tunnels" "-R $remoteaddress:$remoteport:$localaddress:$localport"
42 }
43
44 load_tunnelL() {
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
48
49 config_get localaddress $1 localaddress "*"
50 config_get localport $1 localport
51 config_get remoteaddress $1 remoteaddress
52 config_get remoteport $1 remoteport
53
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
57
58 append_string "ARGS_tunnels" "-L $localaddress:$localport:$remoteaddress:$remoteport"
59 }
60
61 load_tunnelD() {
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
65
66 config_get localaddress $1 localaddress "*"
67 config_get localport $1 localport
68
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
72
73 append_string "ARGS_tunnels" "-D $localaddress:$localport"
74 }
75
76 load_server() {
77 server="$1"
78
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
93
94 error=""
95 [ -n "$user" ] \
96 || append_string "error" "user is not set" "; "
97 [ -n "$hostname" ] \
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'" "; "
125
126 [ -n "$error" ] && { logger -p user.err -t "sshtunnel" "tunnels to $server not started - $error"; return; }
127
128
129 ARGS=""
130 ARGS_options=""
131 ARGS_tunnels=""
132
133 count=0
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; }
137
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"
140
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)"
144 }
145
146 stop() {
147 if [ -f "$PIDFILE".pids ]
148 then
149 logger -p user.info -t "sshtunnel" "stopping all tunnels"
150
151 while read pid
152 do
153 kill "$pid" # kill mother process first
154
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"
158 }
159
160 logger -p daemon.info -t "sshtunnel[$pid]" "tunnel stopped"
161
162 done < "${PIDFILE}.pids"
163
164 rm "${PIDFILE}.pids"
165
166 logger -p user.info -t "sshtunnel" "all tunnels stopped"
167 else
168 logger -p user.info -t "sshtunnel" "no tunnels running"
169 fi
170 }
171
172 start() {
173 [ -f "${PIDFILE}.pids" ] && stop
174
175 config_load sshtunnel
176 if [ -n "$(uci show sshtunnel.@server[0])" ] # at least one server section exists
177 then
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"
181 else
182 logger -p user.info -t "sshtunnel" "no servers defined"
183 fi
184 }