]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/ssh-agent/ssh-agent.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / ssh-agent / ssh-agent.plugin.zsh
1 # Get the filename to store/lookup the environment from
2 ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
3
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
8
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
12       return 0
13     fi
14   fi
15
16   # Set a maximum lifetime for identities added to ssh-agent
17   local lifetime
18   zstyle -s :omz:plugins:ssh-agent lifetime lifetime
19
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
25 }
26
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
31
32   # check for .ssh folder presence
33   if [[ ! -d "$HOME/.ssh" ]]; then
34     return
35   fi
36
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)
44     done
45   fi
46
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]}
52     done
53   fi
54
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")
63     fi
64   done
65
66   # abort if no identities need to be loaded
67   if [[ ${#not_loaded} -eq 0 ]]; then
68     return
69   fi
70
71   # pass extra arguments to ssh-add
72   local args
73   zstyle -a :omz:plugins:ssh-agent ssh-add-args args
74
75   # if ssh-agent quiet mode, pass -q to ssh-add
76   zstyle -t :omz:plugins:ssh-agent quiet && args=(-q $args)
77
78   # use user specified helper to ask for password (ksshaskpass, etc)
79   local helper
80   zstyle -s :omz:plugins:ssh-agent helper helper
81
82   if [[ -n "$helper" ]]; then
83     if [[ -z "${commands[$helper]}" ]]; then
84       echo >&2 "ssh-agent: the helper '$helper' has not been found."
85     else
86       SSH_ASKPASS="$helper" ssh-add "${args[@]}" ${^not_loaded} < /dev/null
87       return $?
88     fi
89   fi
90
91   ssh-add "${args[@]}" ${^not_loaded}
92 }
93
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
98 else
99   _start_agent
100 fi
101
102 # Don't add identities if lazy-loading is enabled
103 if ! zstyle -t :omz:plugins:ssh-agent lazy; then
104   _add_identities
105 fi
106
107 unset agent_forwarding ssh_env_cache
108 unfunction _start_agent _add_identities