gpg-agent.plugin.zsh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. local GPG_ENV=$HOME/.gnupg/gpg-agent.env
  2. function start_agent_nossh {
  3. eval $(/usr/bin/env gpg-agent --quiet --daemon --write-env-file ${GPG_ENV} 2> /dev/null)
  4. chmod 600 ${GPG_ENV}
  5. export GPG_AGENT_INFO
  6. }
  7. function start_agent_withssh {
  8. eval $(/usr/bin/env gpg-agent --quiet --daemon --enable-ssh-support --write-env-file ${GPG_ENV} 2> /dev/null)
  9. chmod 600 ${GPG_ENV}
  10. export GPG_AGENT_INFO
  11. export SSH_AUTH_SOCK
  12. export SSH_AGENT_PID
  13. }
  14. # check if another agent is running
  15. if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
  16. # source settings of old agent, if applicable
  17. if [ -f "${GPG_ENV}" ]; then
  18. . ${GPG_ENV} > /dev/null
  19. export GPG_AGENT_INFO
  20. export SSH_AUTH_SOCK
  21. export SSH_AGENT_PID
  22. fi
  23. # check again if another agent is running using the newly sourced settings
  24. if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
  25. # check for existing ssh-agent
  26. if ssh-add -l > /dev/null 2> /dev/null; then
  27. # ssh-agent running, start gpg-agent without ssh support
  28. start_agent_nossh;
  29. else
  30. # otherwise start gpg-agent with ssh support
  31. start_agent_withssh;
  32. fi
  33. fi
  34. fi
  35. GPG_TTY=$(tty)
  36. export GPG_TTY