summaryrefslogtreecommitdiff
path: root/rapid-tunneling
blob: 49c565673e4deb7b12f0b0e7a30af211f53dae87 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh

set -e

base_dir="$HOME/.rapid-tunneling"

usage() {
	me=`basename $0`
	echo >&2 "$me: Initiate a Rapid-Tunneling tunnel."
	echo >&2 "Usage:"
	echo >&2 "  $me [options] remote-access-NAME.tar.gz"
	echo >&2 ""
	echo >&2 "Options:"
	echo >&2 "  -b DIR   Override Base Directory (Default: $base_dir)"
	echo >&2 "  -h HOST  Connect to HOST rather the host from the tarball"
	echo >&2 "  -p PORT  Connect to PORT rather the host from the tarball"
	echo >&2 "  -u USER  Connect to USER rather the host from the tarball"
	echo >&2 ""
}

if [ -z "$1" ]; then
	usage
	exit 1
fi

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

opt_host=''
opt_port=''
opt_tunnelport='' # For completeness
opt_user=''

while getopts b:h:p:u: opt; do
	case "$opt" in
		b) base_dir="$OPTARG";;
		h) opt_host="$OPTARG";;
		p) opt_port="$OPTARG";;
		u) opt_user="$OPTARG";;
		\?) usage; exit 1;;
	esac
done

shift $((OPTIND - 1))

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

for arg in host port user tunnelport; do
	opt_arg=opt_$arg
	if [ "${!opt_arg}" ]; then
		eval $arg="${!opt_arg}"
	else
		eval $arg=`cat $tmpdir/recv/$arg`
	fi
done

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
mkdir -p $base_dir
cp $tmpdir/recv/key $base_dir/key
tar czf $base_dir/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 $base_dir/pid -x /bin/sh -- -c "exec env SSH_AUTH_SOCK= ssh -o 'StrictHostKeyChecking no' -o 'BatchMode yes' -o 'ServerAliveInterval 60' -T -p $port -i $base_dir/key -R $tunnelport:127.0.0.1:22 -l $user $host < $base_dir/ra-params.tar.gz"