]>
Commit | Line | Data |
---|---|---|
b3d44315 MV |
1 | #!/bin/sh |
2 | ||
3 | set -e | |
dac074b0 | 4 | unset GREP_OPTIONS |
b3d44315 | 5 | |
f9e64e7b | 6 | GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring" |
248ec5ab | 7 | |
f9e64e7b DK |
8 | # gpg needs a trustdb to function, but it can't be invalid (not even empty) |
9 | # so we create a temporary directory to store our fresh readable trustdb in | |
10 | TRUSTDBDIR="$(mktemp -d)" | |
11 | CURRENTTRAP="${CURRENTTRAP} rm -rf '${TRUSTDBDIR}';" | |
12 | trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM | |
13 | chmod 700 "$TRUSTDBDIR" | |
14 | # We also don't use a secret keyring, of course, but gpg panics and | |
15 | # implodes if there isn't one available - and writeable for imports | |
16 | SECRETKEYRING="${TRUSTDBDIR}/secring.gpg" | |
17 | touch $SECRETKEYRING | |
18 | GPG_CMD="$GPG_CMD --secret-keyring $SECRETKEYRING" | |
c0a01322 | 19 | GPG_CMD="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg" |
f9e64e7b DK |
20 | |
21 | # now create the trustdb with an (empty) dummy keyring | |
22 | $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING | |
23 | # and make sure that gpg isn't trying to update the file | |
24 | GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always" | |
25 | ||
46e39c8e | 26 | GPG="$GPG_CMD" |
4a242c5b | 27 | |
7fbe42c0 MV |
28 | MASTER_KEYRING="" |
29 | #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg | |
80f3aeb0 DK |
30 | eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring) |
31 | ARCHIVE_KEYRING_URI="" | |
848ecfa4 | 32 | #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg |
80f3aeb0 | 33 | eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI) |
848ecfa4 | 34 | |
7ef96224 | 35 | ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg |
80f3aeb0 | 36 | eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring) |
7ef96224 | 37 | REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg |
80f3aeb0 | 38 | eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys) |
4a242c5b | 39 | |
00c6e1a3 MV |
40 | requires_root() { |
41 | if [ "$(id -u)" -ne 0 ]; then | |
42 | echo >&1 "ERROR: This command can only be used by root." | |
43 | exit 1 | |
44 | fi | |
45 | } | |
46 | ||
5de34668 JK |
47 | # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead. |
48 | init_keyring() { | |
49 | for path; do | |
50 | if ! [ -e "$path" ]; then | |
51 | touch -- "$path" | |
52 | chmod 0644 -- "$path" | |
53 | fi | |
54 | done | |
55 | } | |
56 | ||
7fbe42c0 | 57 | add_keys_with_verify_against_master_keyring() { |
8c56b1e0 MV |
58 | ADD_KEYRING=$1 |
59 | MASTER=$2 | |
60 | ||
61 | if [ ! -f "$ADD_KEYRING" ]; then | |
62 | echo "ERROR: '$ADD_KEYRING' not found" | |
63 | return | |
64 | fi | |
65 | if [ ! -f "$MASTER" ]; then | |
66 | echo "ERROR: '$MASTER' not found" | |
67 | return | |
68 | fi | |
69 | ||
70 | # when adding new keys, make sure that the archive-master-keyring | |
71 | # is honored. so: | |
3a341a1d MV |
72 | # all keys that are exported must have a valid signature |
73 | # from a key in the $distro-master-keyring | |
8c56b1e0 MV |
74 | add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5` |
75 | master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
76 | for add_key in $add_keys; do | |
c9bb37c7 | 77 | ADDED=0 |
8c56b1e0 | 78 | for master_key in $master_keys; do |
c9bb37c7 | 79 | if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then |
3916f941 | 80 | $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import |
c9bb37c7 | 81 | ADDED=1 |
8c56b1e0 | 82 | fi |
7fbe42c0 | 83 | done |
c9bb37c7 MV |
84 | if [ $ADDED = 0 ]; then |
85 | echo >&2 "Key '$add_key' not added. It is not signed with a master key" | |
86 | fi | |
8c56b1e0 | 87 | done |
7fbe42c0 | 88 | } |
4a242c5b | 89 | |
848ecfa4 MV |
90 | # update the current archive signing keyring from a network URI |
91 | # the archive-keyring keys needs to be signed with the master key | |
92 | # (otherwise it does not make sense from a security POV) | |
93 | net_update() { | |
94 | if [ -z "$ARCHIVE_KEYRING_URI" ]; then | |
00c6e1a3 | 95 | echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set" |
46e39c8e MV |
96 | exit 1 |
97 | fi | |
00c6e1a3 | 98 | requires_root |
46e39c8e MV |
99 | # in theory we would need to depend on wget for this, but this feature |
100 | # isn't useable in debian anyway as we have no keyring uri nor a master key | |
101 | if ! which wget >/dev/null 2>&1; then | |
00c6e1a3 | 102 | echo >&2 "ERROR: an installed wget is required for a network-based update" |
46e39c8e | 103 | exit 1 |
848ecfa4 MV |
104 | fi |
105 | if [ ! -d /var/lib/apt/keyrings ]; then | |
106 | mkdir -p /var/lib/apt/keyrings | |
107 | fi | |
20ba2505 | 108 | keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING) |
93886541 MV |
109 | old_mtime=0 |
110 | if [ -e $keyring ]; then | |
20ba2505 | 111 | old_mtime=$(stat -c %Y $keyring) |
93886541 | 112 | fi |
848ecfa4 | 113 | (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI) |
93886541 MV |
114 | if [ ! -e $keyring ]; then |
115 | return | |
116 | fi | |
20ba2505 | 117 | new_mtime=$(stat -c %Y $keyring) |
93886541 | 118 | if [ $new_mtime -ne $old_mtime ]; then |
8dd0686e | 119 | echo "Checking for new archive signing keys now" |
93886541 MV |
120 | add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING |
121 | fi | |
848ecfa4 MV |
122 | } |
123 | ||
4a242c5b MV |
124 | update() { |
125 | if [ ! -f $ARCHIVE_KEYRING ]; then | |
126 | echo >&2 "ERROR: Can't find the archive-keyring" | |
5fdd72f2 | 127 | echo >&2 "Is the debian-archive-keyring package installed?" |
4a242c5b MV |
128 | exit 1 |
129 | fi | |
00c6e1a3 | 130 | requires_root |
4a242c5b | 131 | |
3a341a1d MV |
132 | # add new keys from the package; |
133 | ||
134 | # we do not use add_keys_with_verify_against_master_keyring here, | |
1171258a | 135 | # because "update" is run on regular package updates. A |
3a341a1d MV |
136 | # attacker might as well replace the master-archive-keyring file |
137 | # in the package and add his own keys. so this check wouldn't | |
138 | # add any security. we *need* this check on net-update though | |
139 | $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import | |
4a242c5b | 140 | |
364af2ef MV |
141 | if [ -r "$REMOVED_KEYS" ]; then |
142 | # remove no-longer supported/used keys | |
143 | keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
144 | for key in $keys; do | |
145 | if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then | |
146 | $GPG --quiet --batch --delete-key --yes ${key} | |
147 | fi | |
148 | done | |
149 | else | |
150 | echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2 | |
151 | fi | |
4a242c5b MV |
152 | } |
153 | ||
04937adc DK |
154 | remove_key_from_keyring() { |
155 | local GPG="$GPG_CMD --keyring $1" | |
156 | # check if the key is in this keyring: the key id is in the 5 column at the end | |
157 | if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+$2:"; then | |
158 | return | |
159 | fi | |
160 | if [ ! -w "$1" ]; then | |
161 | echo >&2 "Key ${2} is in keyring ${1}, but can't be removed as it is read only." | |
162 | return | |
163 | fi | |
164 | # check if it is the only key in the keyring and if so remove the keyring alltogether | |
165 | if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then | |
166 | mv -f "$1" "${1}~" # behave like gpg | |
167 | return | |
168 | fi | |
169 | # we can't just modify pointed to files as these might be in /usr or something | |
170 | local REALTARGET | |
171 | if [ -L "$1" ]; then | |
172 | REALTARGET="$(readlink -f "$1")" | |
173 | mv -f "$1" "${1}.dpkg-tmp" | |
174 | cp -a "$REALTARGET" "$1" | |
175 | ls "$(dirname $1)" | |
176 | fi | |
177 | # delete the key from the keyring | |
178 | $GPG --batch --delete-key --yes "$2" | |
179 | if [ -n "$REALTARGET" ]; then | |
180 | # the real backup is the old link, not the copy we made | |
181 | mv -f "${1}.dpkg-tmp" "${1}~" | |
182 | fi | |
183 | } | |
184 | ||
185 | remove_key() { | |
186 | requires_root | |
187 | ||
188 | # if a --keyring was given, just remove from there | |
189 | if [ -n "$FORCED_KEYRING" ]; then | |
190 | remove_key_from_keyring "$FORCED_KEYRING" "$1" | |
191 | else | |
192 | # otherwise all known keyrings are up for inspection | |
193 | local TRUSTEDFILE="/etc/apt/trusted.gpg" | |
194 | eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring) | |
195 | eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f) | |
196 | remove_key_from_keyring "$TRUSTEDFILE" "$1" | |
197 | TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
198 | eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) | |
199 | if [ -d "$TRUSTEDPARTS" ]; then | |
200 | for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do | |
201 | remove_key_from_keyring "$trusted" "$1" | |
202 | done | |
203 | fi | |
204 | fi | |
205 | echo "OK" | |
206 | } | |
207 | ||
7fbe42c0 | 208 | |
b3d44315 | 209 | usage() { |
46e39c8e | 210 | echo "Usage: apt-key [--keyring file] [command] [arguments]" |
b3d44315 MV |
211 | echo |
212 | echo "Manage apt's list of trusted keys" | |
213 | echo | |
214 | echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)" | |
215 | echo " apt-key del <keyid> - remove the key <keyid>" | |
bf6d5b42 OS |
216 | echo " apt-key export <keyid> - output the key <keyid>" |
217 | echo " apt-key exportall - output all trusted keys" | |
4a242c5b | 218 | echo " apt-key update - update keys using the keyring package" |
848ecfa4 | 219 | echo " apt-key net-update - update keys using the network" |
b3d44315 | 220 | echo " apt-key list - list keys" |
a8cabc8f LB |
221 | echo " apt-key finger - list fingerprints" |
222 | echo " apt-key adv - pass advanced options to gpg (download key)" | |
b3d44315 | 223 | echo |
46e39c8e | 224 | echo "If no specific keyring file is given the command applies to all keyring files." |
b3d44315 MV |
225 | } |
226 | ||
3dc55197 DK |
227 | while [ -n "$1" ]; do |
228 | case "$1" in | |
229 | --keyring) | |
230 | shift | |
231 | TRUSTEDFILE="$1" | |
04937adc | 232 | FORCED_KEYRING="$1" |
3dc55197 DK |
233 | if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then |
234 | GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE" | |
235 | else | |
236 | echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable" | |
237 | exit 1 | |
238 | fi | |
239 | shift | |
240 | ;; | |
241 | --fakeroot) | |
242 | requires_root() { true; } | |
243 | shift | |
244 | ;; | |
245 | --*) | |
246 | echo >&2 "Unknown option: $1" | |
247 | usage | |
248 | exit 1;; | |
249 | *) | |
250 | break;; | |
251 | esac | |
252 | done | |
253 | ||
254 | if [ -z "$TRUSTEDFILE" ]; then | |
255 | TRUSTEDFILE="/etc/apt/trusted.gpg" | |
256 | eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring) | |
257 | eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f) | |
258 | if [ -r "$TRUSTEDFILE" ]; then | |
259 | GPG="$GPG --keyring $TRUSTEDFILE" | |
260 | fi | |
261 | GPG="$GPG --primary-keyring $TRUSTEDFILE" | |
262 | TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
263 | eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) | |
264 | if [ -d "$TRUSTEDPARTS" ]; then | |
59f46f3a DK |
265 | # strip / suffix as gpg will double-slash in that case (#665411) |
266 | STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}" | |
267 | if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then | |
268 | TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS" | |
269 | fi | |
270 | for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do | |
3dc55197 DK |
271 | GPG="$GPG --keyring $trusted" |
272 | done | |
273 | fi | |
46e39c8e | 274 | fi |
46e39c8e | 275 | |
b3d44315 MV |
276 | command="$1" |
277 | if [ -z "$command" ]; then | |
278 | usage | |
279 | exit 1 | |
280 | fi | |
281 | shift | |
282 | ||
283 | if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then | |
284 | echo >&2 "Warning: gnupg does not seem to be installed." | |
285 | echo >&2 "Warning: apt-key requires gnupg for most operations." | |
286 | echo >&2 | |
287 | fi | |
288 | ||
b3d44315 MV |
289 | case "$command" in |
290 | add) | |
00c6e1a3 | 291 | requires_root |
5de34668 | 292 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
293 | $GPG --quiet --batch --import "$1" |
294 | echo "OK" | |
295 | ;; | |
296 | del|rm|remove) | |
5de34668 | 297 | init_keyring "$TRUSTEDFILE" |
04937adc | 298 | remove_key "$1" |
b3d44315 | 299 | ;; |
4a242c5b | 300 | update) |
5de34668 | 301 | init_keyring "$TRUSTEDFILE" |
4a242c5b MV |
302 | update |
303 | ;; | |
848ecfa4 | 304 | net-update) |
5de34668 | 305 | init_keyring "$TRUSTEDFILE" |
848ecfa4 MV |
306 | net_update |
307 | ;; | |
b3d44315 | 308 | list) |
5de34668 | 309 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
310 | $GPG --batch --list-keys |
311 | ;; | |
312 | finger*) | |
5de34668 | 313 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
314 | $GPG --batch --fingerprint |
315 | ;; | |
bf6d5b42 | 316 | export) |
5de34668 | 317 | init_keyring "$TRUSTEDFILE" |
bf6d5b42 OS |
318 | $GPG --armor --export "$1" |
319 | ;; | |
320 | exportall) | |
5de34668 | 321 | init_keyring "$TRUSTEDFILE" |
bf6d5b42 OS |
322 | $GPG --armor --export |
323 | ;; | |
b3d44315 | 324 | adv*) |
5de34668 | 325 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
326 | echo "Executing: $GPG $*" |
327 | $GPG $* | |
328 | ;; | |
329 | help) | |
330 | usage | |
331 | ;; | |
332 | *) | |
333 | usage | |
334 | exit 1 | |
335 | ;; | |
336 | esac |