From ef9b32fb1d870f38bb5db1d28f997092fb402b9d Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Mon, 28 Aug 2017 12:46:14 -0500 Subject: [PATCH] cachedpass: Script to cache a password in gpg-agent --- cachedpass.sh | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 cachedpass.sh diff --git a/cachedpass.sh b/cachedpass.sh new file mode 100755 index 0000000..a5a7d56 --- /dev/null +++ b/cachedpass.sh @@ -0,0 +1,59 @@ +#!/bin/sh + + +PROMPT='Password:' + +usage() { + printf '%s: key [description]\n' "${0##*/}" +} + + +escape() { + printf '%s' "${1}" | tr ' ' '+' +} + + +key="${1}" +description="${2}" + +if [ -z "${key}" ]; then + usage >&2 + exit 2 +elif [ "${key}" = '-h' ] || [ "${key}" = '--help' ]; then + usage + exit 0 +fi +if [ -z "${description}" ]; then + description=$(printf 'Enter the password for %s' "${key}") +fi + + +sleep .125 +umask 0077 + +agentpipe=$(mktemp -u) +mkfifo ${agentpipe} +trap 'rm ${agentpipe}' EXIT + +printf 'GET_PASSPHRASE --data cachedpass:%s X %s %s' \ + "$(escape "${key}")" \ + "${PROMPT}" \ + "$(escape "${description}")" \ + | gpg-connect-agent > ${agentpipe} & +while read msg args; do + case "${msg}" in + ERR) + printf '%s\n' "${args#* }" >&2 + exit 1 + ;; + OK) + ;; + D) + printf '%s' "${args}" | xclip -i -l 1 -sel clip + ;; + *) + printf 'Unexpected response from gpg-agent: %s %s' \ + "${msg}" "${args}" >&2 + exit 1 + esac +done < ${agentpipe}