]> arthur.barton.de Git - ax-zsh.git/commitdiff
gnupg: Setup (reuse/initialize) GnuPG agent
authorAlexander Barton <alex@barton.de>
Sat, 25 Mar 2017 21:56:54 +0000 (22:56 +0100)
committerAlexander Barton <alex@barton.de>
Sat, 25 Mar 2017 21:56:54 +0000 (22:56 +0100)
plugins/gnupg/README.md
plugins/gnupg/gnupg.zshrc

index 8ed3ae6daaba09b99953a1c542015b716544e1a1..14883353d383a0c5aff79ceadcef1c0699d64085 100644 (file)
@@ -1,9 +1,12 @@
 ## gnupg
 
-Setup *GnuPG* using *gpg*(1) or *gpg2*(1) command:
+Setup *GnuPG* using *gpg*(1) or *gpg2*(1) command and try to setup a *GnuPG
+agent* as well, either by reusing an already running agent process or by
+starting a new one.
 
 - `gpg2`: use the same completions than for `gpg`.
 - When `gpg` isn't installed but `gpg2` is, alias it to `gpg`, too.
+- Store "agent information" in `$HOME/.gnupg/agent.info-$HOST:$DISPLAY`.
 
 ### Command Aliases
 
index 39f605d5e01de7013b168ff191afa58f9083ed0e..25798fe3a82a44c61fcc73f507ee78cebf50a224 100644 (file)
@@ -11,8 +11,31 @@ if (( $+commands[gpg2] )); then
        fi
 fi
 
-if [[ -z "$GPG_AGENT_INFO" && -r ~/.gpg-agent-info ]]; then
-       # Read environment file
-       source ~/.gpg-agent-info 2>/dev/null
+# Make sure that "gpg(1)" is available.
+(( $+commands[gpg] )) || return
+
+export GPG_TTY=$(tty)
+
+agent_info_file="$HOME/.gnupg/agent.info-${HOST}"
+
+# Validate agent info ...
+if [[ -n "$GPG_AGENT_INFO" ]]; then
+       echo " *** Testing agent environment ..."
+       gpg-agent >/dev/null 2>&1 || unset GPG_AGENT_INFO
+fi
+
+# Read environment file, when available and agent info not already set.
+if [[ -z "$GPG_AGENT_INFO" && -r "$agent_info_file" ]]; then
+       source "$agent_info_file" 2>/dev/null
        [[ -n "$GPG_AGENT_INFO" ]] && export GPG_AGENT_INFO
 fi
+
+# Setup GnuPG agent when installed.
+if (( $+commands[gpg-agent] )); then
+       # Start up a new GnuPP agent, when none is running/accessible:
+       if ! gpg-agent >/dev/null 2>&1; then
+               eval $(gpg-agent --daemon --write-env-file "$agent_info_file")
+       fi
+fi
+
+unset agent_info_file