make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / autosshfs-mount
blobb88bb7db4dd31300e2b41792501232f2fb861777
1 #!/bin/bash
3 set -e
4 set -o pipefail
6 mountpoint_basedir=$1
8 if [ ! -d "$mountpoint_basedir" ]
9 then
10 echo "$0: supposed basedir is not a directory: $mountpoint_basedir" >&2
11 exit 1
14 connection_params=$2
15 connection_params=${connection_params##*/}
17 # connection_params is in form of:
18 # %r@%n(%h):%p
19 # relevant ssh config option: ControlPath /home/.../somewhere/%r@%n(%h):%p
20 # see man ssh_config(5) for details.
22 # example incrontab:
23 # /home/.../somewhere IN_CREATE autosshfs-mount mnt/ssh $#
25 if [[ $connection_params =~ ^([^@]+)@([^\(]+)\(([^\)]+)\):([0-9]+) ]]
26 then
27 user=${BASH_REMATCH[1]}
28 host=${BASH_REMATCH[2]}
29 hostname=${BASH_REMATCH[3]}
30 port=${BASH_REMATCH[4]}
32 hash=`echo -n "$user$host$hostname$port" | md5sum | cut -c 1-32`
33 exec {lockfd}>> /tmp/autosshfs-mount-$hash.lock
35 if flock --no-fork --exclusive --timeout 10 $lockfd
36 then
37 mountname="$user@$host($hostname,$port)"
38 mountpoint="$mountpoint_basedir/$mountname"
39 mkdir -p "$mountpoint"
40 ln -snfT "$mountname" "$mountpoint_basedir/$host"
41 if ! mountpoint "$mountpoint"
42 then
43 sshfs -o allow_root,auto_unmount,Port=$port,ControlMaster=no,transform_symlinks,reconnect,Hostname="$hostname" "$user@$host:/" "$mountpoint" {lockfd}>&-
44 err=$?
45 if [ $err != 0 ]
46 then
47 echo "$0: sshfs error $err" >&2
50 else
51 echo "$0: can not acquire lock" >&2
52 exit 3
54 else
55 echo "$0: can not parse connection params: $connection_params" >&2
56 exit 2