Skip to content

Commit ab98488

Browse files
committed
gpg < 2.4 limits path to 180 chars, symlink gpg dir during import
The actual error occurs when the gpg-agent tries to create a unix socket Per default a gpg-agent is not started for things like --verify, --list etc. only when needing access to a private key (such as importing one). Hence, we apply the workaround only for importGpgKey and not in other cases where --homedir is used This also means we need to rewrite trustGpgKey as it currently uses --edit-key which again needs access to the private key => use --import-ownertrust instead, which again should not use a gpg-agent.
1 parent c775963 commit ab98488

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/utility/gpg-utils.sh

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

99102
function 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+
}

src/utility/io.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function withCustomOutputInput() {
6969
exitIfArgIsNotFunction "$withCustomOutputInput_fun" 3
7070

7171
local withCustomOutputInput_tmpFile
72-
withCustomOutputInput_tmpFile=$(mktemp /tmp/tegonal-scripts-io.XXXXXXXXX) || traceAndDie "could not create a temporary directory"
72+
withCustomOutputInput_tmpFile=$(mktemp -t tegonal-scripts-io.XXXXXXXXX) || traceAndDie "could not create a temporary directory"
7373
eval "exec ${withCustomOutputInput_outputNr}>\"$withCustomOutputInput_tmpFile\"" || traceAndDie "could not create output file descriptor %s" "$withCustomOutputInput_outputNr"
7474
eval "exec ${withCustomOutputInput_inputNr}<\"$withCustomOutputInput_tmpFile\"" || traceAndDie "could not create input file descriptor %s" "$withCustomOutputInput_inputNr"
7575
# don't fail if we cannot delete the tmp file, if this should happen, then the system should clean-up the file when the process ends

0 commit comments

Comments
 (0)