ssh-agent.plugin.zsh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. # If no keys specified in zstyle, add default keys.
  35. # Mimics calling ssh-add with no arguments.
  36. if [[ ${#identities[@]} -eq 0 ]]; then
  37. # Iterate over files in .ssh folder.
  38. for file in "$HOME/.ssh"/*; do
  39. # Check if file is a regular file and starts with "-----BEGIN OPENSSH PRIVATE KEY-----".
  40. if [[ -f "$file" && $(command head -n 1 "$file") =~ ^-----BEGIN\ OPENSSH\ PRIVATE\ KEY----- ]]; then
  41. # Add filename (without path) to identities array.
  42. identities+=("${file##*/}")
  43. fi
  44. done
  45. fi
  46. # get list of loaded identities' signatures and filenames
  47. if lines=$(ssh-add -l); then
  48. for line in ${(f)lines}; do
  49. loaded_sigs+=${${(z)line}[2]}
  50. loaded_ids+=${${(z)line}[3]}
  51. done
  52. fi
  53. # add identities if not already loaded
  54. for id in $identities; do
  55. # if id is an absolute path, make file equal to id
  56. [[ "$id" = /* ]] && file="$id" || file="$HOME/.ssh/$id"
  57. # check for filename match, otherwise try for signature match
  58. if [[ -f $file && ${loaded_ids[(I)$file]} -le 0 ]]; then
  59. sig="$(ssh-keygen -lf "$file" | awk '{print $2}')"
  60. [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$file")
  61. fi
  62. done
  63. # abort if no identities need to be loaded
  64. if [[ ${#not_loaded} -eq 0 ]]; then
  65. return
  66. fi
  67. # pass extra arguments to ssh-add
  68. local args
  69. zstyle -a :omz:plugins:ssh-agent ssh-add-args args
  70. # if ssh-agent quiet mode, pass -q to ssh-add
  71. zstyle -t :omz:plugins:ssh-agent quiet && args=(-q $args)
  72. # use user specified helper to ask for password (ksshaskpass, etc)
  73. local helper
  74. zstyle -s :omz:plugins:ssh-agent helper helper
  75. if [[ -n "$helper" ]]; then
  76. if [[ -z "${commands[$helper]}" ]]; then
  77. echo >&2 "ssh-agent: the helper '$helper' has not been found."
  78. else
  79. SSH_ASKPASS="$helper" ssh-add "${args[@]}" ${^not_loaded} < /dev/null
  80. return $?
  81. fi
  82. fi
  83. ssh-add "${args[@]}" ${^not_loaded}
  84. }
  85. # Add a nifty symlink for screen/tmux if agent forwarding is enabled
  86. if zstyle -t :omz:plugins:ssh-agent agent-forwarding \
  87. && [[ -n "$SSH_AUTH_SOCK" ]]; then
  88. if [[ ! -L "$SSH_AUTH_SOCK" ]]; then
  89. if [[ -n "$TERMUX_VERSION" ]]; then
  90. ln -sf "$SSH_AUTH_SOCK" "$PREFIX"/tmp/ssh-agent-$USERNAME-screen
  91. else
  92. ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
  93. fi
  94. fi
  95. else
  96. _start_agent
  97. fi
  98. # Don't add identities if lazy-loading is enabled
  99. if ! zstyle -t :omz:plugins:ssh-agent lazy; then
  100. _add_identities
  101. fi
  102. unset agent_forwarding ssh_env_cache
  103. unfunction _start_agent _add_identities