ssh-agent.plugin.zsh 3.4 KB

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