From b98b27d3d5d8f74f301bf1e299b069e0c138e080 Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Mon, 9 Jun 2014 10:59:57 -0700 Subject: [PATCH] save SSH_AUTH_SOCK and SSH_AGENT_PID before reading GPG_ENV GPG_ENV possibly contains SSH_AUTH_SOCK and SSH_AGENT_PID from a gpg-agent previously started with the ssh agent built-in functionality enabled. If an ssh-agent is already live, we should use the existing ssh-agent's environmental variables rather than the old variables from the GPG_ENV file. --- plugins/gpg-agent/gpg-agent.plugin.zsh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/plugins/gpg-agent/gpg-agent.plugin.zsh b/plugins/gpg-agent/gpg-agent.plugin.zsh index 3e6a34f42..9ea77903a 100644 --- a/plugins/gpg-agent/gpg-agent.plugin.zsh +++ b/plugins/gpg-agent/gpg-agent.plugin.zsh @@ -16,9 +16,24 @@ function start_agent_withssh { # check if another agent is running if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then + # check if ssh-agent is running + local SSH_RUNNING=1 + local OLD_SSH_AUTH_SOCK= + local OLD_SSH_AGENT_PID= + if [[ -n "$SSH_AGENT_PID" ]]; then + kill -0 $SSH_AGENT_PID + SSH_RUNNING=$? + if [[ $SSH_RUNNING -eq 0 ]]; then + OLD_SSH_AUTH_SOCK="$SSH_AUTH_SOCK" + OLD_SSH_AGENT_PID="$SSH_AGENT_PID" + fi + fi + # source settings of old agent, if applicable if [ -f "${GPG_ENV}" ]; then . ${GPG_ENV} > /dev/null + SSH_AUTH_SOCK="${OLD_SSH_AUTH_SOCK:-$SSH_AUTH_SOCK}" + SSH_AGENT_PID="${OLD_SSH_AGENT_PID:-$SSH_AGENT_PID}" export GPG_AGENT_INFO export SSH_AUTH_SOCK export SSH_AGENT_PID @@ -26,8 +41,7 @@ if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then # check again if another agent is running using the newly sourced settings if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then - # check for existing ssh-agent - if ssh-add -l > /dev/null 2> /dev/null; then + if [[ $SSH_RUNNING -eq 0 ]]; then # ssh-agent running, start gpg-agent without ssh support start_agent_nossh; else