tmux-pinentry/pinentry-tmux.sh
2025-07-19 19:06:52 +02:00

55 lines
1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eo pipefail
# fallback if empty
if [[ -z "$PINENTRY_USER_DATA" ]]; then
# TODO: add your fallback pinentry here
exec <fallback-pinentry> "$@"
fi
if [[ "$PINENTRY_USER_DATA" != "tmux" ]]; then
echo "Unsupported pinentry type in PINENTRY_USER_DATA"
exit 1
fi
DESCFILE=$(mktemp)
cleanup() {
rm -f "$DESCFILE"
}
trap cleanup EXIT
prompt="PIN:"
echo "OK Please go ahead"
while read cmd rest; do
case "$cmd" in
SETDESC)
echo "$(tput setaf 8)$(echo "$rest" | sed -e 's/%0A/\n/g' -e 's/%22/"/g')$(tput sgr0)" > $DESCFILE
echo "OK"
;;
SETPROMPT)
prompt="$(tput smul)$rest$(tput sgr0)"
echo "OK"
;;
GETPIN)
PIPE=$(mktemp -u)
mkfifo "$PIPE"
chmod 600 "$PIPE"
tmux display-popup -w 70 -h 7 -E "bash -c 'cat $DESCFILE; echo -n \"$prompt \"; read -s pw; echo \$pw > $PIPE'" &
PIN=$(<"$PIPE")
rm -f "$PIPE"
echo "D $PIN"
echo "OK"
;;
BYE)
echo "OK"
exit 0
;;
*)
echo "OK"
;;
esac
done