2016-08-21 07:08:34 +08:00
|
|
|
typeset _agent_forwarding _ssh_env_cache
|
2010-09-25 08:46:52 +08:00
|
|
|
|
2016-08-21 07:08:34 +08:00
|
|
|
function _start_agent() {
|
|
|
|
local lifetime
|
|
|
|
zstyle -s :omz:plugins:ssh-agent lifetime lifetime
|
2011-06-05 10:16:10 +08:00
|
|
|
|
2019-01-10 04:19:52 +08:00
|
|
|
# start ssh-agent and setup environment
|
|
|
|
echo starting ssh-agent...
|
2014-09-30 17:33:42 +08:00
|
|
|
ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
|
2016-08-21 07:08:34 +08:00
|
|
|
chmod 600 $_ssh_env_cache
|
|
|
|
. $_ssh_env_cache > /dev/null
|
2019-01-10 04:19:52 +08:00
|
|
|
}
|
2013-03-16 12:06:46 +08:00
|
|
|
|
2019-01-10 04:19:52 +08:00
|
|
|
function _add_identities() {
|
2019-01-14 23:38:45 +08:00
|
|
|
local id line sig
|
2019-01-14 23:42:14 +08:00
|
|
|
local -a identities loaded not_loaded signatures
|
2016-08-21 07:08:34 +08:00
|
|
|
zstyle -a :omz:plugins:ssh-agent identities identities
|
|
|
|
|
2019-01-14 23:38:45 +08:00
|
|
|
# check for .ssh folder presence
|
|
|
|
if [[ ! -d $HOME/.ssh ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get list of loaded identities' signatures
|
|
|
|
for line in ${(f)"$(ssh-add -l)"}; do loaded+=${${(z)line}[2]}; done
|
|
|
|
|
|
|
|
# get signatures of private keys
|
|
|
|
for id in $identities; do
|
|
|
|
signatures+="$(ssh-keygen -lf "$HOME/.ssh/$id" | awk '{print $2}') $id"
|
|
|
|
done
|
2019-01-10 04:19:52 +08:00
|
|
|
|
|
|
|
# add identities if not already loaded
|
2019-01-14 23:38:45 +08:00
|
|
|
for sig in $signatures; do
|
|
|
|
id="$(cut -f2 <<< $sig)"
|
|
|
|
sig="$(cut -f1 <<< $sig)"
|
2019-01-14 23:42:14 +08:00
|
|
|
[[ ${loaded[(I)$sig]} -le 0 ]] && not_loaded+="$HOME/.ssh/$id"
|
2019-01-10 04:19:52 +08:00
|
|
|
done
|
2019-01-14 23:42:14 +08:00
|
|
|
|
|
|
|
if [[ -n "$not_loaded" ]] && ssh-add ${^not_loaded}
|
2010-09-25 08:46:52 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 02:44:58 +08:00
|
|
|
# Get the filename to store/lookup the environment from
|
2016-08-21 07:08:34 +08:00
|
|
|
_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
2014-03-14 02:44:58 +08:00
|
|
|
|
2011-06-05 09:07:55 +08:00
|
|
|
# test if agent-forwarding is enabled
|
2016-08-21 07:08:34 +08:00
|
|
|
zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
|
|
|
|
|
|
|
|
if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
|
|
|
|
# Add a nifty symlink for screen/tmux if agent forwarding
|
|
|
|
[[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
|
|
|
|
elif [[ -f "$_ssh_env_cache" ]]; then
|
|
|
|
# Source SSH settings, if applicable
|
|
|
|
. $_ssh_env_cache > /dev/null
|
2018-07-02 00:20:34 +08:00
|
|
|
if [[ $USER == "root" ]]; then
|
|
|
|
FILTER="ax"
|
|
|
|
else
|
|
|
|
FILTER="x"
|
|
|
|
fi
|
|
|
|
ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
|
2016-08-21 07:08:34 +08:00
|
|
|
_start_agent
|
|
|
|
}
|
2010-09-25 08:46:52 +08:00
|
|
|
else
|
2016-08-21 07:08:34 +08:00
|
|
|
_start_agent
|
2010-09-25 08:46:52 +08:00
|
|
|
fi
|
|
|
|
|
2019-01-10 04:19:52 +08:00
|
|
|
_add_identities
|
|
|
|
|
2011-06-05 09:07:55 +08:00
|
|
|
# tidy up after ourselves
|
2016-08-21 07:08:34 +08:00
|
|
|
unset _agent_forwarding _ssh_env_cache
|
2019-01-10 04:19:52 +08:00
|
|
|
unfunction _start_agent _add_identities
|