Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-doc
Path: blob/main/documentation/tools/addkey.sh
18081 views
1
#!/bin/sh
2
#
3
4
progname=$(basename $(realpath $0))
5
6
# Print an informational message
7
info() {
8
echo "$@" >&2
9
}
10
11
# Print a warning message
12
warning() {
13
echo "WARNING: $@" >&2
14
}
15
16
# Print an error message and exit
17
error() {
18
echo "ERROR: $@" >&2
19
exit 1
20
}
21
22
# Print usage message and exit
23
usage() {
24
echo "usage: ${progname} [user] [keyid ...]\n" >&2
25
exit 1
26
}
27
28
# Look for gpg
29
gpg=$(which gpg)
30
if [ -z "${gpg}" -o ! -x "${gpg}" ] ; then
31
error "gpg does not seem to be installed"
32
fi
33
gpg() {
34
LANG=C "${gpg}" \
35
--display-charset utf-8 \
36
--no-greeting \
37
--no-secmem-warning \
38
--keyid-format long \
39
--list-options no-show-uid-validity \
40
"$@"
41
}
42
43
# Look up key by key ID
44
getkeybyid() {
45
gpg --with-colons --list-keys "$1" 2>/dev/null | awk -F: \
46
'$5 ~ /^\([0-9A-F]{8}\)?'"$1"'$/i && $12 ~ /ESC/ { print $5 }'
47
}
48
49
# Look up key by email
50
getkeybyemail() {
51
gpg --with-colons --list-keys "$1" 2>/dev/null | awk -F: \
52
'$10 ~ /<'"$1"'>/i && $12 ~ /ESC/ { print $5 }'
53
}
54
55
# The first command-line argument can be a user name or a key ID.
56
if [ $# -gt 0 ] && expr "$1" : '^[a-z][0-9a-z-]*$' >/dev/null ; then
57
me="$1"
58
shift
59
fi
60
if [ -z "${me}" ] ; then
61
me=$(id -nu)
62
fi
63
if [ -z "${me}" ] ; then
64
error "Unable to determine user name."
65
fi
66
if ! expr "${me}" : '^[0-9a-z][0-9a-z-]*$' >/dev/null ; then
67
error "${me} does not seem like a valid user name."
68
fi
69
70
if [ $# -ne 0 ] ; then
71
# Verify the keys that were specified on the command line
72
for arg ; do
73
case $(expr "${arg}" : '^[0-9A-Fa-f]\{8,16\}$') in
74
8)
75
warning "${arg}: recommend using 16-digit keyid"
76
;;
77
16)
78
;;
79
*)
80
warning "${arg} does not appear to be a valid key ID"
81
continue
82
;;
83
esac
84
keyid=$(getkeybyid "${arg}")
85
if [ -n "${keyid}" ] ; then
86
keyids="${keyids} ${keyid}"
87
else
88
warning "${arg} not found"
89
fi
90
done
91
else
92
# Search for keys by freebsd.org email
93
email="${me}@FreeBSD.org"
94
keyids=$(getkeybyemail "${email}")
95
case $(echo "${keyids}" | wc -w) in
96
0)
97
error "no keys found for ${email}"
98
;;
99
1)
100
;;
101
*)
102
warning "Multiple keys found for <${email}>; exporting all."
103
warning "If this is not what you want, specify a key ID" \
104
"on the command line."
105
;;
106
esac
107
fi
108
109
# :(
110
if [ -z "${keyids}" ] ; then
111
error "no valid keys were found"
112
fi
113
114
# Generate key file
115
keyfile="${me}.key"
116
info "Generating ${keyfile}..."
117
(
118
echo "// sh ${progname} ${me}" ${keyids} ";"
119
echo ''
120
echo '[.literal-block-margin]'
121
echo '....'
122
gpg --fingerprint ${keyids}
123
echo '....'
124
echo ''
125
echo '[.literal-block-margin]'
126
echo '....'
127
gpg --no-version --armor --export ${keyids}
128
echo '....'
129
) >"${keyfile}"
130
131
cat <<EOF
132
133
Remember to move ${keyfile} to documentation/static/pgpkeys/
134
135
If this is a new entry, add a relevant entry to
136
documentation/content/en/articles/pgpkeys/_index.adoc and
137
don't forget to run the following commands before committing:
138
139
% git add ${keyfile}
140
141
EOF
142
143