-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-sock-twiddling
120 lines (98 loc) · 2.74 KB
/
bash-sock-twiddling
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#
# ssh-agent + screen = mess
_sa_fixed_sock=${HOME}/.ssh/auth_sock
# check if agent socket is live (0 ==> live)
_sa_sock_is_live ()
{
local sock=${1}
env SSH_AUTH_SOCK=${sock} /usr/bin/ssh-add -l &>/dev/null
}
_sa_link_fixed_sock () { /bin/ln -fns "$1" "$_sa_fixed_sock" ; }
# extract from an agent's socket the sshd pid for that session
_sa_pid_of_sock ()
{
local sock="$1"
[ -L "$sock" ] && sock=$(readlink $sock)
sock=$(echo $sock |egrep 'agent\.[0-9]+$')
if [ -z "$sock" ]; then
# empty suggests no agent was forwarded to this sussion
return 1
fi
echo $sock |sed -e 's/.*\.//'
return 0
}
_sa_current_session_pid ()
{
if [ -n "$SA_ORIG_SOCK" ]; then
_sa_pid_of_sock "$SA_ORIG_SOCK"
return 0
else
local sshd=$(ps h -o ppid $(ps h -o sess $$))
if ps h -o command $sshd |fgrep sshd >/dev/null; then
echo $sshd
return 0
fi
return 1
fi
}
_sa_should_link_fixed_sock ()
{
# interactive? sock defined?
[ -n "$PS1" -a -n "$SSH_AUTH_SOCK" ] || return 1
[ "$SSH_AUTH_SOCK" != "$_sa_fixed_sock" ] || return 1
# don't replace a live socket
_sa_sock_is_live $_sa_fixed_sock && return 1
# don't repoint to a dead socket
_sa_sock_is_live $SSH_AUTH_SOCK || return 1
return 0
}
_sa_link_sock_if_appropriate () {
if _sa_should_link_fixed_sock ; then
_sa_link_fixed_sock "$SSH_AUTH_SOCK"
fi
}
_sa_use_fixed_sock_if_appropriate ()
{
# interactive? sock defined?
[ -n "$PS1" -a -n "$SSH_AUTH_SOCK" ] || return 1
[ "$SSH_AUTH_SOCK" != "$_sa_fixed_sock" ] || return 1
_sa_sock_is_live "$_sa_fixed_sock" || return 1
export SA_ORIG_SOCK="$SSH_AUTH_SOCK"
export SSH_AUTH_SOCK="$_sa_fixed_sock"
}
_sa_find_alternate_sock ()
{
[ $# -eq 0 ] && return 1
local sock= file_clauses=''
# all remaining params are PIDs to ignore
while [ $# -gt 0 ]; do
file_clauses="$file_clauses -not -name agent.$1"
shift
done
# search in directories that look like ssh-agent dirs
find $( find /tmp -maxdepth 1 -type d -name ssh-\* -user $(id -un) ) -type s $file_clauses |\
while read sock; do
# verify socket is live
if _sa_sock_is_live $sock; then
echo $sock
return 0
fi
done
# no alternate found
return 1
}
# called at logout to repoint the symlink at some socket that's not getting
# nuked when this session ends
_sa_repoint_sock_link ()
{
[ ! -e $_sa_fixed_sock ] && return 0
local curr_sock_pid='' curr_fixed_pid='' alt_sock=''
curr_sock_pid=$(_sa_current_session_pid) || return 1
curr_fixed_pid=$(_sa_pid_of_sock $_sa_fixed_sock) || return 1
# no-op if socket didn't come from current session
[ -n "$curr_sock_pid" -a "$curr_sock_pid" -ne "$curr_fixed_pid" ] && return 0
# find some other socket to use, relink
alt_sock=$(_sa_find_alternate_sock $curr_sock_pid) || return 1
_sa_link_fixed_sock "$alt_sock"
}
# done