]> git.saurik.com Git - apt.git/blame - cmdline/apt-key.in
support gpg 2.1.x in apt-key
[apt.git] / cmdline / apt-key.in
CommitLineData
b3d44315
MV
1#!/bin/sh
2
3set -e
dac074b0 4unset GREP_OPTIONS
b3d44315 5
5acf154d
MV
6APT_DIR="/"
7eval $(apt-config shell APT_DIR Dir)
8
5b2c6ddc 9MASTER_KEYRING='&keyring-master-filename;'
80f3aeb0 10eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
5b2c6ddc 11ARCHIVE_KEYRING='&keyring-filename;'
80f3aeb0 12eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
5b2c6ddc 13REMOVED_KEYS='&keyring-removed-filename;'
80f3aeb0 14eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
5b2c6ddc 15ARCHIVE_KEYRING_URI='&keyring-uri;'
f87338d2 16eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
4a242c5b 17
3d0def05
DK
18aptkey_echo() { echo "$@"; }
19
00c6e1a3
MV
20requires_root() {
21 if [ "$(id -u)" -ne 0 ]; then
84b286f6 22 echo >&2 "ERROR: This command can only be used by root."
00c6e1a3
MV
23 exit 1
24 fi
25}
26
ba72845c
DK
27get_fingerprints_of_keyring() {
28 $GPG_CMD --keyring "$1" --with-colons --fingerprint | while read publine; do
29 # search for a public key
30 if [ "${publine%%:*}" != 'pub' ]; then continue; fi
31 # search for the associated fingerprint (should be the very next line)
32 while read fprline; do
33 if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen
34 elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi
35 echo "$fprline" | cut -d':' -f 10
36 done
37 done
38}
39
7fbe42c0 40add_keys_with_verify_against_master_keyring() {
8c56b1e0
MV
41 ADD_KEYRING=$1
42 MASTER=$2
f87338d2 43
8c56b1e0 44 if [ ! -f "$ADD_KEYRING" ]; then
84b286f6 45 echo >&2 "ERROR: '$ADD_KEYRING' not found"
8c56b1e0 46 return
84b286f6 47 fi
8c56b1e0 48 if [ ! -f "$MASTER" ]; then
84b286f6 49 echo >&2 "ERROR: '$MASTER' not found"
8c56b1e0
MV
50 return
51 fi
52
53 # when adding new keys, make sure that the archive-master-keyring
54 # is honored. so:
3a341a1d
MV
55 # all keys that are exported must have a valid signature
56 # from a key in the $distro-master-keyring
ba72845c 57 add_keys="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
f87338d2 58 all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
8c56b1e0 59 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
f87338d2
DK
60
61 # ensure there are no colisions LP: #857472
62 for all_add_key in $all_add_keys; do
63 for master_key in $master_keys; do
64 if [ "$all_add_key" = "$master_key" ]; then
65 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
66 return 1
67 fi
68 done
69 done
0dae96a2 70
8c56b1e0 71 for add_key in $add_keys; do
f87338d2 72 # export the add keyring one-by-one
0dae96a2
DK
73 local TMP_KEYRING="${GPGHOMEDIR}/tmp-keyring.gpg"
74 $GPG_CMD --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key"
75 if ! $GPG_CMD --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
c1642be5 76 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
0dae96a2
DK
77 false
78 fi
79 # check if signed with the master key and only add in this case
80 ADDED=0
8c56b1e0 81 for master_key in $master_keys; do
0dae96a2
DK
82 if $GPG_CMD --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then
83 $GPG_CMD --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" | $GPG --batch --yes --import
c9bb37c7 84 ADDED=1
8c56b1e0 85 fi
7fbe42c0 86 done
c9bb37c7
MV
87 if [ $ADDED = 0 ]; then
88 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
89 fi
0dae96a2 90 rm -f "${TMP_KEYRING}"
8c56b1e0 91 done
7fbe42c0 92}
4a242c5b 93
848ecfa4
MV
94# update the current archive signing keyring from a network URI
95# the archive-keyring keys needs to be signed with the master key
96# (otherwise it does not make sense from a security POV)
97net_update() {
f87338d2 98 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
5acf154d
MV
99 APT_KEY_NET_UPDATE_ENABLED=""
100 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
101 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
102 exit 1
103 fi
f87338d2 104
848ecfa4 105 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
00c6e1a3 106 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
46e39c8e
MV
107 exit 1
108 fi
109 # in theory we would need to depend on wget for this, but this feature
110 # isn't useable in debian anyway as we have no keyring uri nor a master key
111 if ! which wget >/dev/null 2>&1; then
00c6e1a3 112 echo >&2 "ERROR: an installed wget is required for a network-based update"
46e39c8e 113 exit 1
848ecfa4 114 fi
5acf154d
MV
115 if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
116 mkdir -p ${APT_DIR}/var/lib/apt/keyrings
848ecfa4 117 fi
e5543ea5 118 keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
93886541
MV
119 old_mtime=0
120 if [ -e $keyring ]; then
20ba2505 121 old_mtime=$(stat -c %Y $keyring)
93886541 122 fi
5acf154d 123 (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
93886541
MV
124 if [ ! -e $keyring ]; then
125 return
126 fi
20ba2505 127 new_mtime=$(stat -c %Y $keyring)
93886541 128 if [ $new_mtime -ne $old_mtime ]; then
3d0def05 129 aptkey_echo "Checking for new archive signing keys now"
93886541
MV
130 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
131 fi
848ecfa4
MV
132}
133
4a242c5b
MV
134update() {
135 if [ ! -f $ARCHIVE_KEYRING ]; then
136 echo >&2 "ERROR: Can't find the archive-keyring"
5b2c6ddc 137 echo >&2 "Is the &keyring-package; package installed?"
4a242c5b
MV
138 exit 1
139 fi
140
3a341a1d
MV
141 # add new keys from the package;
142
143 # we do not use add_keys_with_verify_against_master_keyring here,
1171258a 144 # because "update" is run on regular package updates. A
3a341a1d
MV
145 # attacker might as well replace the master-archive-keyring file
146 # in the package and add his own keys. so this check wouldn't
147 # add any security. we *need* this check on net-update though
f14cde2c 148 import_keyring_into_keyring "$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
4a242c5b 149
364af2ef
MV
150 if [ -r "$REMOVED_KEYS" ]; then
151 # remove no-longer supported/used keys
ba72845c 152 get_fingerprints_of_keyring "$REMOVED_KEYS" | while read key; do
5beb682d 153 foreach_keyring_do 'remove_key_from_keyring' "$key"
364af2ef
MV
154 done
155 else
84b286f6 156 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
364af2ef 157 fi
4a242c5b
MV
158}
159
04937adc 160remove_key_from_keyring() {
4b30c1dc
DK
161 local KEYRINGFILE="$1"
162 shift
5beb682d
DK
163 # non-existent keyrings have by definition no keys
164 if [ ! -e "$KEYRINGFILE" ]; then
165 return
166 fi
167
4b30c1dc 168 local GPG="$GPG_CMD --keyring $KEYRINGFILE"
0b94a7bc 169 for KEY in "$@"; do
4b30c1dc 170 # check if the key is in this keyring: the key id is in the 5 column at the end
05f64ca2 171 if ! get_fingerprints_of_keyring "$KEYRINGFILE" | grep -iq "^[0-9A-F]*${KEY}$"; then
4b30c1dc
DK
172 continue
173 fi
174 if [ ! -w "$KEYRINGFILE" ]; then
175 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
176 continue
177 fi
178 # check if it is the only key in the keyring and if so remove the keyring altogether
ba72845c 179 if [ '1' = "$(get_fingerprints_of_keyring "$KEYRINGFILE" | wc -l)" ]; then
4b30c1dc
DK
180 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
181 return
182 fi
183 # we can't just modify pointed to files as these might be in /usr or something
184 local REALTARGET
185 if [ -L "$KEYRINGFILE" ]; then
186 REALTARGET="$(readlink -f "$KEYRINGFILE")"
187 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
188 cp -a "$REALTARGET" "$KEYRINGFILE"
189 fi
190 # delete the key from the keyring
191 $GPG --batch --delete-key --yes "$KEY"
192 if [ -n "$REALTARGET" ]; then
193 # the real backup is the old link, not the copy we made
194 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
195 fi
196 done
04937adc
DK
197}
198
4b30c1dc
DK
199foreach_keyring_do() {
200 local ACTION="$1"
201 shift
b0d40854 202 # if a --keyring was given, just work on this one
4b30c1dc
DK
203 if [ -n "$FORCED_KEYRING" ]; then
204 $ACTION "$FORCED_KEYRING" "$@"
205 else
206 # otherwise all known keyrings are up for inspection
5beb682d
DK
207 if [ -s "$TRUSTEDFILE" ]; then
208 $ACTION "$TRUSTEDFILE" "$@"
209 fi
4b30c1dc
DK
210 local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
211 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
212 if [ -d "$TRUSTEDPARTS" ]; then
5beb682d
DK
213 # strip / suffix as gpg will double-slash in that case (#665411)
214 local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
215 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
216 TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
217 fi
4b30c1dc 218 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
5beb682d
DK
219 if [ -s "$trusted" ]; then
220 $ACTION "$trusted" "$@"
221 fi
4b30c1dc 222 done
04937adc 223 fi
4b30c1dc 224 fi
04937adc
DK
225}
226
0b94a7bc 227run_cmd_on_keyring() {
5beb682d
DK
228 local KEYRINGFILE="$1"
229 shift
0b94a7bc
DK
230 # fingerprint and co will fail if key isn't in this keyring
231 $GPG_CMD --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true
5beb682d
DK
232}
233
f14cde2c
DK
234import_keyring_into_keyring() {
235 local FROM="${1:-${GPGHOMEDIR}/pubring.gpg}"
236 local TO="${2:-${GPGHOMEDIR}/pubring.gpg}"
237 shift 2
238 rm -f "${GPGHOMEDIR}/gpgoutput.log"
239 # the idea is simple: We take keys from one keyring and copy it to another
240 # we do this with so many checks inbetween to ensure that WE control the
241 # creation, so we know that the (potentially) created $TO keyring is a
242 # simple keyring rather than a keybox as gpg2 would create it which in turn
243 # can't be read by gpgv.
244 # BEWARE: This is designed more in the way to work with the current
245 # callers, than to have a well defined it would be easy to add new callers to.
246 if [ ! -s "$TO" ]; then
247 if [ -s "$FROM" ]; then
248 if [ -z "$2" ]; then
249 if ! $GPG_CMD --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
250 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
251 false
252 else
253 chmod 0644 -- "$TO"
254 fi
255 else
256 create_new_keyring "$TO"
257 fi
258 else
259 create_new_keyring "$TO"
260 fi
261 elif [ -s "$FROM" ]; then
262 local EXPORTLIMIT="$1"
263 if [ -n "$1$2" ]; then shift; fi
264 if ! $GPG_CMD --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} | $GPG_CMD --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
265 cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
266 false
267 fi
0dae96a2
DK
268 fi
269}
270
f14cde2c
DK
271import_keys_from_keyring() {
272 import_keyring_into_keyring "$1" "$2"
273}
274
0dae96a2 275merge_keys_into_keyrings() {
f14cde2c 276 import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only'
0dae96a2
DK
277}
278
279merge_back_changes() {
280 if [ -n "$FORCED_KEYRING" ]; then
281 # if the keyring was forced merge is already done
282 return
283 fi
284 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
285 # merge all updated keys
286 foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
287 fi
0b94a7bc 288 # look for keys which were added or removed
0dae96a2
DK
289 get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
290 get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
0dae96a2
DK
291 sort "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" | uniq --unique | while read key; do
292 if grep -q "^${key}$" "${GPGHOMEDIR}/pubring.orig.keylst"; then
293 # key isn't part of new keyring, so remove
294 foreach_keyring_do 'remove_key_from_keyring' "$key"
295 elif grep -q "^${key}$" "${GPGHOMEDIR}/pubring.keylst"; then
296 # key is part of new keyring, so we need to import it
f14cde2c 297 import_keyring_into_keyring '' "$TRUSTEDFILE" "$key"
0dae96a2
DK
298 else
299 echo >&2 "Errror: Key ${key} (dis)appeared out of nowhere"
300 fi
301 done
5beb682d
DK
302}
303
304setup_merged_keyring() {
b0d40854 305 if [ -n "$FORCED_KEYID" ]; then
f14cde2c 306 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
b0d40854
DK
307 FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg"
308 TRUSTEDFILE="${FORCED_KEYRING}"
309 GPG="$GPG --keyring $TRUSTEDFILE"
310 # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
f14cde2c 311 import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true
b0d40854 312 elif [ -z "$FORCED_KEYRING" ]; then
0740a310 313 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
0dae96a2
DK
314 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
315 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
316 else
317 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
0740a310 318 fi
0dae96a2
DK
319 GPG="$GPG --keyring ${GPGHOMEDIR}/pubring.gpg"
320 else
0dae96a2 321 create_new_keyring "$TRUSTEDFILE"
f14cde2c 322 GPG="$GPG --keyring $TRUSTEDFILE"
5beb682d
DK
323 fi
324}
325
0dae96a2
DK
326create_new_keyring() {
327 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
328 if ! [ -e "$TRUSTEDFILE" ]; then
329 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
330 touch -- "$TRUSTEDFILE"
331 chmod 0644 -- "$TRUSTEDFILE"
332 fi
333 fi
334}
5beb682d 335
b3d44315 336usage() {
46e39c8e 337 echo "Usage: apt-key [--keyring file] [command] [arguments]"
b3d44315
MV
338 echo
339 echo "Manage apt's list of trusted keys"
340 echo
341 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
342 echo " apt-key del <keyid> - remove the key <keyid>"
bf6d5b42
OS
343 echo " apt-key export <keyid> - output the key <keyid>"
344 echo " apt-key exportall - output all trusted keys"
4a242c5b 345 echo " apt-key update - update keys using the keyring package"
848ecfa4 346 echo " apt-key net-update - update keys using the network"
b3d44315 347 echo " apt-key list - list keys"
a8cabc8f
LB
348 echo " apt-key finger - list fingerprints"
349 echo " apt-key adv - pass advanced options to gpg (download key)"
b3d44315 350 echo
46e39c8e 351 echo "If no specific keyring file is given the command applies to all keyring files."
b3d44315
MV
352}
353
3dc55197
DK
354while [ -n "$1" ]; do
355 case "$1" in
356 --keyring)
357 shift
358 TRUSTEDFILE="$1"
04937adc 359 FORCED_KEYRING="$1"
3dc55197 360 ;;
b0d40854
DK
361 --keyid)
362 shift
363 FORCED_KEYID="$1"
364 ;;
bd7fb5aa
DK
365 --secret-keyring)
366 shift
367 FORCED_SECRET_KEYRING="$1"
33a22672
DK
368 ;;
369 --readonly)
370 merge_back_changes() { true; }
f14cde2c 371 create_new_keyring() { if [ ! -r $FORCED_KEYRING ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; }
bd7fb5aa 372 ;;
3dc55197
DK
373 --fakeroot)
374 requires_root() { true; }
3dc55197 375 ;;
3d0def05
DK
376 --quiet)
377 aptkey_echo() { true; }
3d0def05 378 ;;
c1642be5
DK
379 --debug1)
380 # some cmds like finger redirect stderr to /dev/null …
381 aptkey_execute() { echo 'EXEC:' "$@"; "$@"; }
382 ;;
383 --debug2)
384 # … other more complicated ones pipe gpg into gpg.
385 aptkey_execute() { echo >&2 'EXEC:' "$@"; "$@"; }
386 ;;
3dc55197
DK
387 --*)
388 echo >&2 "Unknown option: $1"
389 usage
390 exit 1;;
391 *)
392 break;;
393 esac
33a22672 394 shift
3dc55197
DK
395done
396
397if [ -z "$TRUSTEDFILE" ]; then
398 TRUSTEDFILE="/etc/apt/trusted.gpg"
399 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
400 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
46e39c8e 401fi
46e39c8e 402
b3d44315
MV
403command="$1"
404if [ -z "$command" ]; then
405 usage
406 exit 1
407fi
408shift
409
9fda3be1 410if [ "$command" != "help" ]; then
93d0d08c
DK
411 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
412
413 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev/null 2>&1; then
414 true
415 elif which gpg >/dev/null 2>&1; then
416 GPG_EXE="gpg"
417 elif which gpg2 >/dev/null 2>&1; then
418 GPG_EXE="gpg2"
419 else
420 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
421 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
9fda3be1 422 echo >&2
93d0d08c 423 exit 255
9fda3be1
DK
424 fi
425
c1642be5
DK
426 if type aptkey_execute >/dev/null 2>&1; then
427 GPG_EXE="aptkey_execute $GPG_EXE"
428 fi
93d0d08c 429 GPG_CMD="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
05991180
DK
430
431 # gpg needs (in different versions more or less) files to function correctly,
432 # so we give it its own homedir and generate some valid content for it
0d303f17
DK
433 if [ -n "$TMPDIR" ]; then
434 # tmpdir is a directory and current user has rwx access to it
435 # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
436 if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
437 unset TMPDIR
438 fi
7ffac4c1 439 fi
05991180
DK
440 GPGHOMEDIR="$(mktemp -d)"
441 CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
442 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
443 chmod 700 "$GPGHOMEDIR"
444 # We don't use a secret keyring, of course, but gpg panics and
445 # implodes if there isn't one available - and writeable for imports
446 SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
447 touch $SECRETKEYRING
448 GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR"
449 # create the trustdb with an (empty) dummy keyring
450 # older gpgs required it, newer gpgs even warn that it isn't needed,
451 # but require it nonetheless for some commands, so we just play safe
452 # here for the foreseeable future and create a dummy one
453 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1
454 # tell gpg that it shouldn't try to maintain a trustdb file
455 GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always"
456 GPG="$GPG_CMD"
457
bd7fb5aa
DK
458 # for advanced operations, we might really need a secret keyring after all
459 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
460 rm -f "$SECRETKEYRING"
461 cp -a "$FORCED_SECRET_KEYRING" "$SECRETKEYRING"
462 fi
f14cde2c
DK
463
464 # older gpg versions need a secring file, but newer versions take it as
465 # a hint to start a migration from earlier versions. The file is empty
466 # anyhow, so nothing actually happens, but its three lines of output
467 # nobody expects to see in apt-key context, so trigger it in silence
468 echo -n | $GPG --batch --import >/dev/null 2>&1 || true
b3d44315
MV
469fi
470
b3d44315
MV
471case "$command" in
472 add)
5beb682d
DK
473 requires_root
474 setup_merged_keyring
475 $GPG --quiet --batch --import "$@"
0dae96a2 476 merge_back_changes
5beb682d 477 aptkey_echo "OK"
b3d44315
MV
478 ;;
479 del|rm|remove)
5beb682d
DK
480 requires_root
481 foreach_keyring_do 'remove_key_from_keyring' "$@"
482 aptkey_echo "OK"
b3d44315 483 ;;
4a242c5b 484 update)
5beb682d
DK
485 requires_root
486 setup_merged_keyring
4a242c5b 487 update
0dae96a2 488 merge_back_changes
4a242c5b 489 ;;
848ecfa4 490 net-update)
5beb682d
DK
491 requires_root
492 setup_merged_keyring
848ecfa4 493 net_update
0dae96a2 494 merge_back_changes
848ecfa4 495 ;;
b3d44315 496 list)
0b94a7bc 497 foreach_keyring_do 'run_cmd_on_keyring' --list-keys "$@"
5beb682d 498 ;;
b3d44315 499 finger*)
0b94a7bc 500 foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@"
5beb682d 501 ;;
4f51a496 502 export|exportall)
38005d8b
DK
503 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
504 $GPG_CMD --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
5beb682d 505 ;;
b3d44315 506 adv*)
5beb682d
DK
507 setup_merged_keyring
508 aptkey_echo "Executing: $GPG $*"
509 $GPG "$@"
0dae96a2 510 merge_back_changes
5beb682d 511 ;;
c46a36ad
DK
512 verify)
513 setup_merged_keyring
f14cde2c
DK
514 GPGV=''
515 eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
516 if [ -n "$GPGV" ] && ! which "$GPGV" >/dev/null 2>&1; then GPGV='';
517 elif which gpgv >/dev/null 2>&1; then GPGV='gpgv';
518 elif which gpgv2 >/dev/null 2>&1; then GPGV='gpgv2';
519 fi
520 if [ -n "$GPGV" ]; then
b0d40854 521 if [ -n "$FORCED_KEYRING" ]; then
f14cde2c 522 $GPGV --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
b0d40854 523 else
f14cde2c 524 $GPGV --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"
b0d40854 525 fi
c46a36ad
DK
526 else
527 $GPG --verify "$@"
528 fi
529 ;;
b3d44315
MV
530 help)
531 usage
532 ;;
533 *)
534 usage
535 exit 1
536 ;;
537esac