This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.sh
79 lines (63 loc) · 2.35 KB
/
pull.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
# Prevent tainting variables via environment
# See: https://gist.github.com/duxsco/fad211d5828e09d0391f018834f955c9
unset choice keyserver mechanism number_regex pka success temp_gpg_homedir
if [[ $# -ne 1 ]]; then
cat <<EOF
Please, provide the ID of the key whose public key you want to retrieve.
Example:
$ bash ${0##*/} 0xABCDEFGH01234567
$ bash ${0##*/} [email protected]
Aborting...
EOF
exit 1
fi
declare -a success
temp_gpg_homedir="$(mktemp -d)"
if grep -q '^gpg (GnuPG) 2\.2\.' < <(gpg --homedir "${temp_gpg_homedir}" --version); then
pka="pka"
else
pka=""
fi
# if e-mail...
if grep -q "@" <<<"$1"; then
for mechanism in "dane" "wkd" ${pka} "cert" "hkps://keys.openpgp.org" "hkps://keys.mailvelope.com" "hkps://keys.gentoo.org" "hkps://keyring.debian.org" "hkps://keyserver.ubuntu.com"; do
if gpg --homedir "${temp_gpg_homedir}" --auto-key-locate "clear,${mechanism}" --locate-external-key "$1" >/dev/null 2>&1; then
success+=("${mechanism}")
fi
done
else
for keyserver in "hkps://keys.openpgp.org" "hkps://keys.mailvelope.com" "hkps://keys.gentoo.org" "hkps://keyserver.ubuntu.com"; do
if gpg --homedir "${temp_gpg_homedir}" --keyserver "${keyserver}" --recv-keys "$1" >/dev/null 2>&1; then
success+=("${keyserver}")
fi
done
fi
gpgconf --homedir "${temp_gpg_homedir}" --kill all
if [[ ${#success[@]} -eq 0 ]]; then
echo -e "\nNo working mechanism found! Aborting...\n"
else
echo -e "\nFollowing mechanism(s) are working for public key retrieval.\nWhat do you want to use?\n 0) Abort/Quit"
for index in "${!success[@]}"; do
echo " $((index+1))) ${success[$index]}"
done
echo ""
read -r -p "Please, select by number: " choice
echo ""
number_regex='^[0-9]+$'
if ! [[ ${choice} =~ ${number_regex} ]] || [[ ${choice} -gt ${#success[@]} ]]; then
echo -e "Invalid choice! Aborting...\n"
exit 1
fi
if [[ ${choice} -eq 0 ]]; then
echo -e "Public key retrieval aborted!\n"
else
((choice--))
echo -e "Mechanism \"${success[$choice]}\" chosen...\n"
if grep -q "@" <<<"$1"; then
gpg --auto-key-locate "clear,${success[$choice]}" --locate-external-key "$1"
else
gpg --keyserver "${success[$choice]}" --recv-keys "$1"
fi
fi
fi