Commit | Line | Data |
---|---|---|
285a1386 MW |
1 | #! /bin/sh |
2 | ||
3 | set -e | |
4 | usage="Usage: $0 [-bc] [COMMAND [ARGS...]]" | |
5 | ||
6 | ### Parse options | |
7 | case "${SHELL-/bin/sh}" in | |
8 | *csh*) style=csh;; | |
9 | *) style=sh;; | |
10 | esac | |
af254ca9 | 11 | force=nil chosen= commands= |
285a1386 MW |
12 | while [ $# -gt 0 ]; do |
13 | case "$1" in | |
c388de2b MW |
14 | -h | --help) echo "$usage"; exit 0 ;; |
15 | -c | --cshell | --tcsh) style=csh chosen=t ;; | |
16 | -b | --bourne | --bash | --zsh) style=sh chosen=t ;; | |
17 | -f | --force) force=t ;; | |
18 | --) shift; break ;; | |
19 | -*) echo >&2 "$usage"; exit 1 ;; | |
20 | *) break ;; | |
285a1386 MW |
21 | esac |
22 | shift | |
23 | done | |
24 | [ $# -gt 0 ] && style=commands | |
25 | case $chosen,$style in | |
26 | t,commands) | |
27 | echo >&2 "$0: output style and commands? you're odd" | |
28 | exit 1 | |
29 | ;; | |
30 | esac | |
31 | ||
d3e29c31 MW |
32 | ## Check that this stands a chance of working. |
33 | if ! type >/dev/null 2>&1 ssh-agent || ! type >/dev/null >&1 ssh-add; then | |
34 | echo >&2 "$0: ssh not installed; chickening out" | |
35 | exit 1 | |
36 | fi | |
37 | ||
b46a2fb2 MW |
38 | ## Some useful variables. |
39 | hostname=${HOST-$(hostname)} | |
3feef411 | 40 | user=${USER-${LOGNAME-$(id -un)}} uid=${UID-$(id -u)} |
b46a2fb2 MW |
41 | dir=$TMPDIR/.ssh-agent.$hostname.$user |
42 | socket=$dir/sock; pid=$dir/pid | |
239bb58e | 43 | export SSH_AUTH_SOCK |
b46a2fb2 | 44 | |
3feef411 MW |
45 | ## Should I start a new agent? |
46 | foundp=nil | |
47 | case $force in | |
48 | nil) | |
49 | case ${SSH_AUTH_SOCK+t} in | |
50 | t) | |
51 | set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e | |
52 | if [ $rc -lt 2 ]; then foundp=t; fi | |
53 | ;; | |
54 | esac | |
55 | case $foundp in | |
56 | t) ;; | |
57 | *) | |
58 | for i in \ | |
59 | "$HOME/.cache/keyring-"*"/ssh" \ | |
60 | "/run/user/$uid/keyring/ssh" \ | |
61 | "$socket" | |
62 | do | |
63 | SSH_AUTH_SOCK=$i | |
64 | set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e | |
65 | if [ $rc -lt 2 ]; then foundp=t; break; fi | |
66 | done | |
67 | ;; | |
68 | esac | |
285a1386 MW |
69 | ;; |
70 | esac | |
71 | ||
72 | ### If so, do that | |
af254ca9 MW |
73 | case $foundp in |
74 | nil) | |
af254ca9 | 75 | mkdir -p -m700 "$dir" |
239bb58e | 76 | SSH_AUTH_SOCK=$socket |
f9f7e9df MW |
77 | p=$PATH |
78 | runes= | |
79 | while :; do | |
80 | case $p in *:*) d=${p%%:*} p=${p#*:} ;; *) d=$p p= ;; esac | |
81 | if [ -x "${d+$d/}setsid" ]; then runes="$runes setsid"; break; fi | |
82 | case $p in "") break ;; esac | |
83 | done | |
af254ca9 MW |
84 | set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e |
85 | if [ $rc -ge 2 ]; then | |
86 | if [ -f "$pid" ]; then | |
87 | kill $(cat "$pid") >/dev/null 2>&1 || : | |
88 | fi | |
89 | rm -f "$socket" "$pid" | |
f9f7e9df | 90 | (cd /; exec $runes ssh-agent -d -a "$socket" </dev/null >/dev/null 2>&1)& |
af254ca9 | 91 | echo $! >"$pid" |
239bb58e | 92 | SSH_AUTH_SOCK=$socket |
af254ca9 MW |
93 | fi |
94 | ;; | |
95 | esac | |
285a1386 MW |
96 | |
97 | ### Run a program, or export the details | |
98 | case $style in | |
c388de2b MW |
99 | sh) echo "SSH_AUTH_SOCK='$SSH_AUTH_SOCK'; export SSH_AUTH_SOCK" ;; |
100 | csh) echo "setenv SSH_AUTH_SOCK '$SSH_AUTH_SOCK'" ;; | |
101 | commands) exec "$@" ;; | |
285a1386 | 102 | esac |