[packages] add sshtunnel, Manages Local and Remote openssh ssh(1) tunnels
[openwrt/svn-archive/archive.git] / net / sshtunnel / files / etc / init.d / sshtunnel
1 #!/bin/sh /etc/rc.common
2
3 START=99
4 STOP=01
5
6 PIDFILE="/tmp/run/sshtunnel"
7
8 load_tunnel() {
9 config_get user $1 user
10 config_get hostname $1 hostname
11 config_get identity $1 identity
12 config_get remoteport $1 remoteport
13 config_get localport $1 localport
14 config_get options $1 options '-o ServerAliveCountMax=3 -o ServerAliveInterval=20 -o StrictHostKeyChecking=false'
15 config_get retrydelay $1 retrydelay "10"
16 [ "$cfgtype" = "tunnelL" ] && {
17 config_get localaddress $1 localaddress "127.0.0.1"
18 config_get remoteaddress $1 remoteaddress "*"
19 }
20 [ "$cfgtype" = "tunnelR" ] && {
21 config_get localaddress $1 localaddress "*"
22 config_get remoteaddress $1 remoteaddress "127.0.0.1"
23 }
24
25 local error
26 [ -f "$identity" ] || error="Identity file $identity not accessible"
27 [ -n "$user" ] || error="please set user option"
28 [ -n "$hostname" ] || error="please set hostname option"
29 [ "$remoteport" -gt 0 -a "$localport" -gt 0 -a "$retrydelay" -ge 0 ] || error="invalid configuration"
30 [ -n "$error" ] && { logger -p user.err -t "sshtunnel" "$cfgtype $1 not started - $error"; return; }
31
32 [ "$cfgtype" = "tunnelL" ] && {
33 args="-N -i $identity -o PasswordAuthentication=no -o ExitOnForwardFailure=yes $options -L $localaddress:$localport:$remoteaddress:$remoteport $user@$hostname"
34 }
35 [ "$cfgtype" = "tunnelR" ] && {
36 args="-N -i $identity -o PasswordAuthentication=no -o ExitOnForwardFailure=yes $options -R $remoteaddress:$remoteport:$localaddress:$localport $user@$hostname"
37 }
38
39 /usr/bin/sshtunnel.sh "$args" "$retrydelay" &
40 echo $! >> "$PIDFILE".pids
41 logger -p user.info -t "sshtunnel" "started new $cfgtype $1 (pid=$!;retrydelay=$retrydelay)"
42 }
43
44 stop() {
45 if [ -f "$PIDFILE".pids ]
46 then
47 logger -p user.info -t "sshtunnel" "stopping all tunnels"
48
49 while read pid
50 do
51 start-stop-daemon -K -p "$PIDFILE"_"$pid".pid
52 kill $pid
53 logger -p daemon.info -t "sshtunnel[$pid]" "tunnel stopped"
54 done < "$PIDFILE".pids
55
56 rm "$PIDFILE".pids
57
58 logger -p user.info -t "sshtunnel" "all tunnels stopped"
59 else
60 logger -p user.info -t "sshtunnel" "no tunnels running"
61 fi
62 }
63
64 start() {
65 [ -f "$PIDFILE".pids ] && stop
66
67 logger -p user.info -t "sshtunnel" "starting all tunnels"
68
69 config_load sshtunnel
70 config_foreach load_tunnel tunnelR
71 config_foreach load_tunnel tunnelL
72
73 logger -p user.info -t "sshtunnel" "all tunnels started"
74 }