@@ -93,7 +93,10 @@ function trustGpgKey() {
9393 # shellcheck disable=SC2034 # is passed by name to parseFnArgs
9494 local -ra params=(gpgDir keyId)
9595 parseFnArgs params " $@ "
96- echo -e " 5\ny\n" | gpg --homedir " $gpgDir " --no-tty --command-fd 0 --edit-key " $keyId " trust
96+
97+ local fingerprint
98+ fingerprint=" $( gpg --homedir " $gpgDir " --with-colons --fingerprint " $keyId " | grep ' ^fpr:' | cut -d: -f10 | head -n1) "
99+ echo " $fingerprint :5:" | gpg --homedir " $gpgDir " --import-ownertrust
97100}
98101
99102function importGpgKey() {
@@ -123,14 +126,26 @@ function importGpgKey() {
123126 fi
124127
125128 if [[ $isTrusting == y ]]; then
129+ local maybeSymlinkedGpgDir
130+ maybeSymlinkedGpgDir=" $( getSaveGpgHomedir " $gpgDir " ) "
131+
126132 echo " importing key $file "
127- gpg --homedir " $gpgDir " --batch --no-tty --import " $file " || die " failed to import $file "
133+ gpg --homedir " $maybeSymlinkedGpgDir " --batch --no-tty --import " $file " || {
134+ cleanupMaybeSymlinkedGpgDir " $gpgDir " " $maybeSymlinkedGpgDir "
135+ die " failed to import $file "
136+ }
137+
128138 local keyId
129139 grep pub <<< " $outputKey" | perl -0777 -pe " s#pub\s+[^/]+/([0-9A-Z]+).*#\$ 1#g" |
130140 while read -r keyId; do
131141 echo " establishing trust for key $keyId "
132- trustGpgKey " $gpgDir " " $keyId "
133- done
142+ # shellcheck disable=SC2310 # we are aware of that set -e has no effect for trustGpgKey that's why we use || return $?
143+ trustGpgKey " $maybeSymlinkedGpgDir " " $keyId " || return $?
144+ done || {
145+ local exitCode=$?
146+ cleanupMaybeSymlinkedGpgDir " $gpgDir " " $maybeSymlinkedGpgDir "
147+ return " $exitCode "
148+ }
134149 else
135150 return 1
136151 fi
@@ -219,7 +234,7 @@ function getRevocationData() {
219234 --list-options show-sig-expire,show-unusable-subkeys,show-unusable-uids \
220235 --with-colons " $keyId " ) || returnDying " could not list signatures for key %s" " $keyId " || return $?
221236 revData=$( perl -0777 -ne ' while (/(sub|pub):r:.*?:' " $keyId " ' :[\S\s]+?(rev:.*)/g) { print "$2\n"; }' <<< " $sigs" )
222- [[ -n $revData ]] || returnDying " was not able to extract the revocation data from the signatures (maybe it was not revoked?):\n%" " $sigs " || return $?
237+ [[ -n $revData ]] || returnDying " was not able to extract the revocation data from the signatures (maybe it was not revoked?):\n%s " " $sigs " || return $?
223238 echo " $revData "
224239}
225240
@@ -255,3 +270,31 @@ function listSignaturesAndHighlightKey() {
255270 # shellcheck disable=SC2001
256271 sed " s/$keyId /\x1b[0;31m&\x1b[0m/g" <<< " $signatures"
257272}
273+
274+ function getSaveGpgHomedir() {
275+ local gpgDir
276+ # shellcheck disable=SC2034 # is passed by name to parseFnArgs
277+ local -ra params=(gpgDir)
278+ parseFnArgs params " $@ "
279+
280+ if (( ${# gpgDir} < 100 )) ; then
281+ echo " $gpgDir "
282+ else
283+ local tmpDir
284+ tmpDir=$( mktemp -d -t gpg-homedir-XXXXXXXXXX)
285+ ln -s " $gpgDir " " $tmpDir /gpg"
286+ echo " $tmpDir /gpg"
287+ fi
288+ }
289+
290+ function cleanupMaybeSymlinkedGpgDir() {
291+ local gpgDir maybeSymlinkedGpgDir
292+ # shellcheck disable=SC2034 # is passed by name to parseFnArgs
293+ local -ra params=(gpgDir maybeSymlinkedGpgDir)
294+ parseFnArgs params " $@ "
295+
296+ if [[ $maybeSymlinkedGpgDir != " $gpgDir " ]]; then
297+ # if cleanup fails then well... let's hope the system cleans it up at some point
298+ rm -r " $maybeSymlinkedGpgDir " || true
299+ fi
300+ }
0 commit comments