ssh-agent.plugin.zsh 3.2 KB

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