ssh-agent.plugin.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. typeset _agent_forwarding _ssh_env_cache
  2. function _start_agent() {
  3. local lifetime
  4. zstyle -s :omz:plugins:ssh-agent lifetime lifetime
  5. # start ssh-agent and setup environment
  6. echo Starting ssh-agent...
  7. ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
  8. chmod 600 $_ssh_env_cache
  9. . $_ssh_env_cache > /dev/null
  10. }
  11. function _add_identities() {
  12. local id line sig lines
  13. local -a identities loaded_sigs loaded_ids not_loaded
  14. zstyle -a :omz:plugins:ssh-agent identities identities
  15. # check for .ssh folder presence
  16. if [[ ! -d $HOME/.ssh ]]; then
  17. return
  18. fi
  19. # add default keys if no identities were set up via zstyle
  20. # this is to mimic the call to ssh-add with no identities
  21. if [[ ${#identities} -eq 0 ]]; then
  22. # key list found on `ssh-add` man page's DESCRIPTION section
  23. for id in id_rsa id_dsa id_ecdsa id_ed25519 identity; do
  24. # check if file exists
  25. [[ -f "$HOME/.ssh/$id" ]] && identities+=($id)
  26. done
  27. fi
  28. # get list of loaded identities' signatures and filenames
  29. if lines=$(ssh-add -l); then
  30. for line in ${(f)lines}; do
  31. loaded_sigs+=${${(z)line}[2]}
  32. loaded_ids+=${${(z)line}[3]}
  33. done
  34. fi
  35. # add identities if not already loaded
  36. for id in $identities; do
  37. # check for filename match, otherwise try for signature match
  38. if [[ ${loaded_ids[(I)$HOME/.ssh/$id]} -le 0 ]]; then
  39. sig="$(ssh-keygen -lf "$HOME/.ssh/$id" | awk '{print $2}')"
  40. [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$HOME/.ssh/$id")
  41. fi
  42. done
  43. # abort if no identities need to be loaded
  44. if [[ ${#not_loaded} -eq 0 ]]; then
  45. return
  46. fi
  47. # pass extra arguments to ssh-add
  48. local args
  49. zstyle -a :omz:plugins:ssh-agent ssh-add-args args
  50. # use user specified helper to ask for password (ksshaskpass, etc)
  51. local helper
  52. zstyle -s :omz:plugins:ssh-agent helper helper
  53. if [[ -n "$helper" ]]; then
  54. if [[ -z "${commands[$helper]}" ]]; then
  55. echo "ssh-agent: the helper '$helper' has not been found."
  56. else
  57. SSH_ASKPASS="$helper" ssh-add "${args[@]}" ${^not_loaded} < /dev/null
  58. return $?
  59. fi
  60. fi
  61. ssh-add "${args[@]}" ${^not_loaded}
  62. }
  63. # Get the filename to store/lookup the environment from
  64. _ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
  65. # test if agent-forwarding is enabled
  66. zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
  67. if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  68. # Add a nifty symlink for screen/tmux if agent forwarding
  69. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
  70. elif [[ -f "$_ssh_env_cache" ]]; then
  71. # Source SSH settings, if applicable
  72. . $_ssh_env_cache > /dev/null
  73. if [[ $USERNAME == "root" ]]; then
  74. FILTER="ax"
  75. else
  76. FILTER="x"
  77. fi
  78. ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
  79. _start_agent
  80. }
  81. else
  82. _start_agent
  83. fi
  84. _add_identities
  85. # tidy up after ourselves
  86. unset _agent_forwarding _ssh_env_cache
  87. unfunction _start_agent _add_identities