| 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 |
| 11 | force=nil chosen= commands= |
| 12 | while [ $# -gt 0 ]; do |
| 13 | case "$1" in |
| 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 ;; |
| 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 | |
| 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 | |
| 38 | ## Some useful variables. |
| 39 | hostname=${HOST-$(hostname)} |
| 40 | user=${USER-${LOGNAME-$(id -un)}} uid=${UID-$(id -u)} |
| 41 | dir=$TMPDIR/.ssh-agent.$hostname.$user |
| 42 | socket=$dir/sock; pid=$dir/pid |
| 43 | export SSH_AUTH_SOCK |
| 44 | |
| 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 |
| 69 | ;; |
| 70 | esac |
| 71 | |
| 72 | ### If so, do that |
| 73 | case $foundp in |
| 74 | nil) |
| 75 | mkdir -p -m700 "$dir" |
| 76 | SSH_AUTH_SOCK=$socket |
| 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 |
| 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" |
| 90 | (cd /; exec $runes ssh-agent -d -a "$socket" </dev/null >/dev/null 2>&1)& |
| 91 | echo $! >"$pid" |
| 92 | SSH_AUTH_SOCK=$socket |
| 93 | fi |
| 94 | ;; |
| 95 | esac |
| 96 | |
| 97 | ### Run a program, or export the details |
| 98 | case $style in |
| 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 "$@" ;; |
| 102 | esac |