From 2b879f461f83c1938e1787f0f1cbdd1233fac41e Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Mon, 21 Sep 2020 12:27:21 -0400 Subject: change gpg/sudo interaction --- selfpublish.sh | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/selfpublish.sh b/selfpublish.sh index 6ebf36b..ac5ad09 100644 --- a/selfpublish.sh +++ b/selfpublish.sh @@ -192,41 +192,52 @@ get_home() [ "$1" ] && getent passwd "$1" | (IFS=: read _ _ _ _ _ h _; echo $h) } +GPG() +{ + set -- gpg "$@" + if [ "$SUDO_USER" ] + then + su "$SUDO_USER" -c "$(bash -c 'printf "%q " "$@"' bash "$@")" + else + "$@" + fi +} set -e # 1. GET CRYPTONOMIC UID cryptonomic_hostname=$(cryptonomic hostname) [ "$cryptonomic_hostname" ] -username=$(id -un) + +if [ "$SUDO_USER" ] +then + username=$SUDO_USER +else + username=$(id -un) +fi [ "$username" ] uid=${username}@${cryptonomic_hostname} # 2. CHECK IF EXISTING SECRET KEY -if [ "$SUDO_USER" -a ! "$GNUPGHOME" ] -then - GNUPGHOME=$(get_home "$SUDO_USER")/.gnupg - export GNUPGHOME -fi -t=$(gpg -K --with-colons) +t=$(GPG -K --with-colons) if [ "$t" ] then # 3. CHECK IF EXISTING KEY HAS UID - if gpg -K --with-colons | find_secret_key_with_domain "${cryptonomic_hostname#*.}" | grep -q . && ! force + if GPG -K --with-colons | find_secret_key_with_domain "${cryptonomic_hostname#*.}" | grep -q . && ! force then exit fi # 4. ADD UID TO EXISTING KEY - gpg_default_key=$(gpg -K --with-colons | process_colons match_first_secret_key show_fpr) + gpg_default_key=$(GPG -K --with-colons | process_colons match_first_secret_key show_fpr) [ "$gpg_default_key" ] - gpg --quick-add-uid "$gpg_default_key" "$uid" || force - verbose gpg -K "$gpg_default_key" + GPG --quick-add-uid "$gpg_default_key" "$uid" || force + verbose GPG -K "$gpg_default_key" exit 0 else # 2.5 GENERATE NEW KEY - gpg --batch --passphrase '' --quick-generate-key "$uid" - verbose gpg -K "$uid" + GPG --batch --passphrase '' --quick-generate-key "$uid" + verbose GPG -K "$uid" exit 0 fi EOF -- cgit v1.2.3