scripts/cachedpass.sh

60 lines
931 B
Bash
Executable File

#!/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}