1 # Get the filename to store/lookup the environment from
2 ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
4 function _start_agent() {
5 # Check if ssh-agent is already running
6 if [[ -f "$ssh_env_cache" ]]; then
7 . "$ssh_env_cache" > /dev/null
9 # Test if $SSH_AUTH_SOCK is visible
10 zmodload zsh/net/socket
11 if [[ -S "$SSH_AUTH_SOCK" ]] && zsocket "$SSH_AUTH_SOCK" 2>/dev/null; then
16 # Set a maximum lifetime for identities added to ssh-agent
18 zstyle -s :omz:plugins:ssh-agent lifetime lifetime
20 # start ssh-agent and setup environment
21 zstyle -t :omz:plugins:ssh-agent quiet || echo >&2 "Starting ssh-agent ..."
22 ssh-agent -s ${lifetime:+-t} ${lifetime} | sed '/^echo/d' >! "$ssh_env_cache"
23 chmod 600 "$ssh_env_cache"
24 . "$ssh_env_cache" > /dev/null
27 function _add_identities() {
28 local id file line sig lines
29 local -a identities loaded_sigs loaded_ids not_loaded
30 zstyle -a :omz:plugins:ssh-agent identities identities
32 # check for .ssh folder presence
33 if [[ ! -d "$HOME/.ssh" ]]; then
37 # add default keys if no identities were set up via zstyle
38 # this is to mimic the call to ssh-add with no identities
39 if [[ ${#identities} -eq 0 ]]; then
40 # key list found on `ssh-add` man page's DESCRIPTION section
41 for id in id_rsa id_dsa id_ecdsa id_ed25519 identity; do
42 # check if file exists
43 [[ -f "$HOME/.ssh/$id" ]] && identities+=($id)
47 # get list of loaded identities' signatures and filenames
48 if lines=$(ssh-add -l); then
49 for line in ${(f)lines}; do
50 loaded_sigs+=${${(z)line}[2]}
51 loaded_ids+=${${(z)line}[3]}
55 # add identities if not already loaded
56 for id in $identities; do
57 # if id is an absolute path, make file equal to id
58 [[ "$id" = /* ]] && file="$id" || file="$HOME/.ssh/$id"
59 # check for filename match, otherwise try for signature match
60 if [[ ${loaded_ids[(I)$file]} -le 0 ]]; then
61 sig="$(ssh-keygen -lf "$file" | awk '{print $2}')"
62 [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$file")
66 # abort if no identities need to be loaded
67 if [[ ${#not_loaded} -eq 0 ]]; then
71 # pass extra arguments to ssh-add
73 zstyle -a :omz:plugins:ssh-agent ssh-add-args args
75 # if ssh-agent quiet mode, pass -q to ssh-add
76 zstyle -t :omz:plugins:ssh-agent quiet && args=(-q $args)
78 # use user specified helper to ask for password (ksshaskpass, etc)
80 zstyle -s :omz:plugins:ssh-agent helper helper
82 if [[ -n "$helper" ]]; then
83 if [[ -z "${commands[$helper]}" ]]; then
84 echo >&2 "ssh-agent: the helper '$helper' has not been found."
86 SSH_ASKPASS="$helper" ssh-add "${args[@]}" ${^not_loaded} < /dev/null
91 ssh-add "${args[@]}" ${^not_loaded}
94 # Add a nifty symlink for screen/tmux if agent forwarding is enabled
95 if zstyle -t :omz:plugins:ssh-agent agent-forwarding \
96 && [[ -n "$SSH_AUTH_SOCK" && ! -L "$SSH_AUTH_SOCK" ]]; then
97 ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
102 # Don't add identities if lazy-loading is enabled
103 if ! zstyle -t :omz:plugins:ssh-agent lazy; then
107 unset agent_forwarding ssh_env_cache
108 unfunction _start_agent _add_identities