Add gpg-agent plumbing for zsh

This commit is contained in:
Alan Orth 2016-11-19 17:59:36 +02:00
parent bf8ce9aadb
commit 9b63f48e31
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
2 changed files with 62 additions and 0 deletions

4
.zshrc
View File

@ -120,5 +120,9 @@ genpass() {
openssl rand -base64 18 openssl rand -base64 18
} }
# Activate GPG agent and cache the password after first use
# See: https://github.com/axtl/gpg-agent.zsh
source ~/src/git/dotfiles/gpg-agent.plugin.zsh
export PATH export PATH
export MANPATH export MANPATH

58
gpg-agent.plugin.zsh Normal file
View File

@ -0,0 +1,58 @@
local GPG_DIR="${HOME}/.gnupg"
local GPG_ENV="${GPG_DIR}/gpg-agent.env"
local SSH_SOCK=""
local GPG_SOCK=""
if [[ ! -z "${SSH_AUTH_SOCK}" ]]
then
SSH_SOCK="${GPG_DIR}/$(basename ${SSH_AUTH_SOCK})"
fi
if [[ ! -z "${GPG_AGENT_INFO}" ]]
then
GPG_SOCK="${GPG_DIR}/$(basename ${GPG_AGENT_INFO} | cut -d : -f1)"
fi
function _gpg_agent_start() {
emulate -L zsh
if [[ ! ( -f "${GPG_ENV}" && -S "${SSH_SOCK}" && -S "${GPG_SOCK}" ) ]]; then
_gpg_agent_clean
# start and source the script
eval "$(/usr/bin/env gpg-agent \
--quiet \
--daemon \
--enable-ssh-support \
--use-standard-socket \
--write-env-file ${GPG_ENV} \
2> /dev/null)"
chmod 600 "${GPG_ENV}"
fi
source "${GPG_ENV}" 2> /dev/null
export GPG_AGENT_INFO
export SSH_AUTH_SOCK
export SSH_AGENT_PID
GPG_TTY=$(tty)
export GPG_TTY
}
function _gpg_agent_reset() {
emulate -L zsh
_gpg_agent_clean
_gpg_agent_start
}
function _gpg_agent_clean () {
emulate -L zsh
# clear possibly stale things
rm "${SSH_SOCK}" 2> /dev/null
rm "${GPG_SOCK}" 2> /dev/null
rm "${GPG_ENV}" 2> /dev/null
killall -9 gpg-agent 2> /dev/null
killall -9 ssh-agent 2> /dev/null
}
_gpg_agent_start