ssh-agent.plugin.zsh 3.1 KB

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