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