summaryrefslogtreecommitdiff
path: root/rapid-tunneling
blob: cf1b2776ae59896dca6e7a209bd917af693263db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

set -e

if [ -z "$1" ]; then
	echo "Usage: $0 <remote-access-file-name>" 1>&2
	exit 1
fi

if [ -r /etc/rapid-tunneling/client ]; then . /etc/rapid-tunneling/client; fi

tmpdir=`mktemp -d rapid-tunneling.XXXXXX`
mkdir $tmpdir/recv $tmpdir/send
tar xzf "$1" -C $tmpdir/recv

user=`cat $tmpdir/recv/user`
host=`cat $tmpdir/recv/host`
port=`cat $tmpdir/recv/port`
tunnelport=`cat $tmpdir/recv/tunnelport`

ssh-keygen -q -t rsa -C rapid-tunneling -N "" -f $tmpdir/send/key	# public will be in key.pub
cp $tmpdir/recv/key $tmpdir/send/origkey
cp $tmpdir/recv/key $HOME/key
tar czf $HOME/ra-params.tar.gz -C $tmpdir/send key origkey

mkdir -p -m 700 $HOME/.ssh
awk '$NF != "rapid-tunneling" {print}' $HOME/.ssh/authorized_keys > $HOME/.ssh/authorized_keys.new 2>/dev/null || true
cat $tmpdir/send/key.pub >> $HOME/.ssh/authorized_keys.new
mv -f $HOME/.ssh/authorized_keys.new $HOME/.ssh/authorized_keys
chmod 644 $HOME/.ssh/authorized_keys
rm -rf $tmpdir
# Remove host key of $host (in case it has changed)
ssh-keygen -R $host 2>/dev/null || true
# Run in background
if [ -x /sbin/start-stop-daemon ]; then
	SSD=/sbin/start-stop-daemon
else
	SSD="/sbin/busybox start-stop-daemon"
fi

$SSD -S -b -m -p $HOME/pid -x /bin/sh -- -c "exec ssh -o 'StrictHostKeyChecking no' -o 'BatchMode yes' -o 'ServerAliveInterval 60' -T -p $port -i $HOME/key -R $tunnelport:127.0.0.1:22 -l $user $host < $HOME/ra-params.tar.gz"