2013-05-18 23:27:40 +00:00
|
|
|
#
|
|
|
|
# Provides for an easier use of SSH by setting up ssh-agent.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
|
|
|
|
|
|
|
# Return if requirements are not found.
|
2017-05-05 16:58:19 +00:00
|
|
|
if (( ! $+commands[ssh-agent] )); then
|
2013-05-18 23:27:40 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set the path to the SSH directory.
|
|
|
|
_ssh_dir="$HOME/.ssh"
|
|
|
|
|
|
|
|
# Set the path to the environment file if not set by another module.
|
2016-03-08 16:14:47 +00:00
|
|
|
_ssh_agent_env="${_ssh_agent_env:-${TMPDIR:-/tmp}/ssh-agent.env.$UID}"
|
2013-05-18 23:27:40 +00:00
|
|
|
|
|
|
|
# Set the path to the persistent authentication socket.
|
2016-03-08 16:14:47 +00:00
|
|
|
_ssh_agent_sock="${TMPDIR:-/tmp}/ssh-agent.sock.$UID"
|
2013-05-18 23:27:40 +00:00
|
|
|
|
|
|
|
# Start ssh-agent if not started.
|
|
|
|
if [[ ! -S "$SSH_AUTH_SOCK" ]]; then
|
|
|
|
# Export environment variables.
|
|
|
|
source "$_ssh_agent_env" 2> /dev/null
|
2013-09-26 18:46:21 +00:00
|
|
|
|
|
|
|
# Start ssh-agent if not started.
|
2015-02-13 05:00:27 +00:00
|
|
|
if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "${SSH_AGENT_PID:--1} ssh-agent"; then
|
2013-09-26 18:46:21 +00:00
|
|
|
eval "$(ssh-agent | sed '/^echo /d' | tee "$_ssh_agent_env")"
|
|
|
|
fi
|
2013-05-18 23:27:40 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create a persistent SSH authentication socket.
|
|
|
|
if [[ -S "$SSH_AUTH_SOCK" && "$SSH_AUTH_SOCK" != "$_ssh_agent_sock" ]]; then
|
|
|
|
ln -sf "$SSH_AUTH_SOCK" "$_ssh_agent_sock"
|
|
|
|
export SSH_AUTH_SOCK="$_ssh_agent_sock"
|
|
|
|
fi
|
|
|
|
|
2013-08-09 00:26:34 +00:00
|
|
|
# Load identities.
|
|
|
|
if ssh-add -l 2>&1 | grep -q 'The agent has no identities'; then
|
|
|
|
zstyle -a ':prezto:module:ssh:load' identities '_ssh_identities'
|
2017-10-04 18:27:55 +00:00
|
|
|
# ssh-add has strange requirements for running SSH_ASKPASS, so we duplicate
|
|
|
|
# them here. Essentially, if the other requirements are met, we redirect stdin
|
|
|
|
# from /dev/null in order to meet the final requirement.
|
|
|
|
#
|
|
|
|
# From ssh-add(1):
|
|
|
|
# If ssh-add needs a passphrase, it will read the passphrase from the current
|
|
|
|
# terminal if it was run from a terminal. If ssh-add does not have a terminal
|
|
|
|
# associated with it but DISPLAY and SSH_ASKPASS are set, it will execute the
|
|
|
|
# program specified by SSH_ASKPASS and open an X11 window to read the
|
|
|
|
# passphrase.
|
|
|
|
if [[ -n "$DISPLAY" && -x "$SSH_ASKPASS" ]]; then
|
2017-10-19 00:30:16 +00:00
|
|
|
ssh-add ${_ssh_identities:+$_ssh_dir/${^_ssh_identities[@]}} < /dev/null 2> /dev/null
|
2013-08-09 00:26:34 +00:00
|
|
|
else
|
2017-10-19 00:30:16 +00:00
|
|
|
ssh-add ${_ssh_identities:+$_ssh_dir/${^_ssh_identities[@]}} 2> /dev/null
|
2013-08-09 00:26:34 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-05-18 23:27:40 +00:00
|
|
|
# Clean up.
|
|
|
|
unset _ssh_{dir,identities} _ssh_agent_{env,sock}
|