]> git.saurik.com Git - apt.git/blob - cmdline/apt-key.in
use only one --keyring in gpg interactions
[apt.git] / cmdline / apt-key.in
1 #!/bin/sh
2
3 set -e
4 unset GREP_OPTIONS
5
6 APT_DIR="/"
7 eval $(apt-config shell APT_DIR Dir)
8
9 MASTER_KEYRING='&keyring-master-filename;'
10 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
11 ARCHIVE_KEYRING='&keyring-filename;'
12 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
13 REMOVED_KEYS='&keyring-removed-filename;'
14 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
15 ARCHIVE_KEYRING_URI='&keyring-uri;'
16 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
17
18 aptkey_echo() { echo "$@"; }
19
20 requires_root() {
21 if [ "$(id -u)" -ne 0 ]; then
22 echo >&2 "ERROR: This command can only be used by root."
23 exit 1
24 fi
25 }
26
27 get_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
40 add_keys_with_verify_against_master_keyring() {
41 ADD_KEYRING=$1
42 MASTER=$2
43
44 if [ ! -f "$ADD_KEYRING" ]; then
45 echo >&2 "ERROR: '$ADD_KEYRING' not found"
46 return
47 fi
48 if [ ! -f "$MASTER" ]; then
49 echo >&2 "ERROR: '$MASTER' not found"
50 return
51 fi
52
53 # when adding new keys, make sure that the archive-master-keyring
54 # is honored. so:
55 # all keys that are exported must have a valid signature
56 # from a key in the $distro-master-keyring
57 add_keys="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
58 all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
59 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
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
70
71 for add_key in $add_keys; do
72 # export the add keyring one-by-one
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
76 cat "${GPGHOMEDIR}/gpgoutput.log"
77 false
78 fi
79 # check if signed with the master key and only add in this case
80 ADDED=0
81 for master_key in $master_keys; do
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
84 ADDED=1
85 fi
86 done
87 if [ $ADDED = 0 ]; then
88 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
89 fi
90 rm -f "${TMP_KEYRING}"
91 done
92 }
93
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)
97 net_update() {
98 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
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
104
105 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
106 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
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
112 echo >&2 "ERROR: an installed wget is required for a network-based update"
113 exit 1
114 fi
115 if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
116 mkdir -p ${APT_DIR}/var/lib/apt/keyrings
117 fi
118 keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
119 old_mtime=0
120 if [ -e $keyring ]; then
121 old_mtime=$(stat -c %Y $keyring)
122 fi
123 (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
124 if [ ! -e $keyring ]; then
125 return
126 fi
127 new_mtime=$(stat -c %Y $keyring)
128 if [ $new_mtime -ne $old_mtime ]; then
129 aptkey_echo "Checking for new archive signing keys now"
130 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
131 fi
132 }
133
134 update() {
135 if [ ! -f $ARCHIVE_KEYRING ]; then
136 echo >&2 "ERROR: Can't find the archive-keyring"
137 echo >&2 "Is the &keyring-package; package installed?"
138 exit 1
139 fi
140
141 # add new keys from the package;
142
143 # we do not use add_keys_with_verify_against_master_keyring here,
144 # because "update" is run on regular package updates. A
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
148 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
149
150 if [ -r "$REMOVED_KEYS" ]; then
151 # remove no-longer supported/used keys
152 get_fingerprints_of_keyring "$REMOVED_KEYS" | while read key; do
153 foreach_keyring_do 'remove_key_from_keyring' "$key"
154 done
155 else
156 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
157 fi
158 }
159
160 remove_key_from_keyring() {
161 local KEYRINGFILE="$1"
162 shift
163 # non-existent keyrings have by definition no keys
164 if [ ! -e "$KEYRINGFILE" ]; then
165 return
166 fi
167
168 local GPG="$GPG_CMD --keyring $KEYRINGFILE"
169 while [ -n "$1" ]; do
170 local KEY="$1"
171 shift
172 # check if the key is in this keyring: the key id is in the 5 column at the end
173 if ! get_fingerprints_of_keyring "$KEYRINGFILE" | grep -q "^[0-9A-F]*${KEY}$"; then
174 continue
175 fi
176 if [ ! -w "$KEYRINGFILE" ]; then
177 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
178 continue
179 fi
180 # check if it is the only key in the keyring and if so remove the keyring altogether
181 if [ '1' = "$(get_fingerprints_of_keyring "$KEYRINGFILE" | wc -l)" ]; then
182 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
183 return
184 fi
185 # we can't just modify pointed to files as these might be in /usr or something
186 local REALTARGET
187 if [ -L "$KEYRINGFILE" ]; then
188 REALTARGET="$(readlink -f "$KEYRINGFILE")"
189 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
190 cp -a "$REALTARGET" "$KEYRINGFILE"
191 fi
192 # delete the key from the keyring
193 $GPG --batch --delete-key --yes "$KEY"
194 if [ -n "$REALTARGET" ]; then
195 # the real backup is the old link, not the copy we made
196 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
197 fi
198 done
199 }
200
201 foreach_keyring_do() {
202 local ACTION="$1"
203 shift
204 # if a --keyring was given, just remove from there
205 if [ -n "$FORCED_KEYRING" ]; then
206 $ACTION "$FORCED_KEYRING" "$@"
207 else
208 # otherwise all known keyrings are up for inspection
209 if [ -s "$TRUSTEDFILE" ]; then
210 $ACTION "$TRUSTEDFILE" "$@"
211 fi
212 local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
213 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
214 if [ -d "$TRUSTEDPARTS" ]; then
215 # strip / suffix as gpg will double-slash in that case (#665411)
216 local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
217 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
218 TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
219 fi
220 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
221 if [ -s "$trusted" ]; then
222 $ACTION "$trusted" "$@"
223 fi
224 done
225 fi
226 fi
227 }
228
229 list_keys_from_keyring() {
230 local KEYRINGFILE="$1"
231 shift
232 # don't show the error message if this keyring doesn't include the key
233 $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev/null || true
234 }
235
236 fingerprint_keys_from_keyring() {
237 local KEYRINGFILE="$1"
238 shift
239 # don't show the error message if this keyring doesn't include the fingerprint
240 $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev/null || true
241 }
242
243 import_keys_from_keyring() {
244 local IMPORT="$1"
245 local KEYRINGFILE="$2"
246 if ! $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
247 cat "${GPGHOMEDIR}/gpgoutput.log"
248 false
249 fi
250 }
251
252 merge_keys_into_keyrings() {
253 local KEYRINGFILE="$1"
254 local IMPORT="$2"
255 if ! $GPG_CMD --keyring "$KEYRINGFILE" --batch --import --import-options 'merge-only' "$IMPORT" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
256 cat "${GPGHOMEDIR}/gpgoutput.log"
257 false
258 fi
259 }
260
261 merge_back_changes() {
262 if [ -n "$FORCED_KEYRING" ]; then
263 # if the keyring was forced merge is already done
264 return
265 fi
266 if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
267 # merge all updated keys
268 foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
269 fi
270 # no look for keys which were added or removed
271 get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
272 get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
273 #echo >&2 "MERGE BACK"
274 sort "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" | uniq --unique | while read key; do
275 if grep -q "^${key}$" "${GPGHOMEDIR}/pubring.orig.keylst"; then
276 # key isn't part of new keyring, so remove
277 foreach_keyring_do 'remove_key_from_keyring' "$key"
278 elif grep -q "^${key}$" "${GPGHOMEDIR}/pubring.keylst"; then
279 # key is part of new keyring, so we need to import it
280 create_new_keyring "$TRUSTEDFILE"
281 if ! $GPG --batch --yes --export "$key" | $GPG_CMD --keyring "$TRUSTEDFILE" --batch --yes --import > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
282 cat "${GPGHOMEDIR}/gpgoutput.log"
283 false
284 fi
285 else
286 echo >&2 "Errror: Key ${key} (dis)appeared out of nowhere"
287 fi
288 done
289 }
290
291 setup_merged_keyring() {
292 if [ -z "$FORCED_KEYRING" ]; then
293 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
294 if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
295 cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
296 else
297 touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
298 fi
299 GPG="$GPG --keyring ${GPGHOMEDIR}/pubring.gpg"
300 else
301 GPG="$GPG --keyring $TRUSTEDFILE"
302 create_new_keyring "$TRUSTEDFILE"
303 fi
304 }
305
306 create_new_keyring() {
307 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
308 if ! [ -e "$TRUSTEDFILE" ]; then
309 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
310 touch -- "$TRUSTEDFILE"
311 chmod 0644 -- "$TRUSTEDFILE"
312 fi
313 fi
314 }
315
316 usage() {
317 echo "Usage: apt-key [--keyring file] [command] [arguments]"
318 echo
319 echo "Manage apt's list of trusted keys"
320 echo
321 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
322 echo " apt-key del <keyid> - remove the key <keyid>"
323 echo " apt-key export <keyid> - output the key <keyid>"
324 echo " apt-key exportall - output all trusted keys"
325 echo " apt-key update - update keys using the keyring package"
326 echo " apt-key net-update - update keys using the network"
327 echo " apt-key list - list keys"
328 echo " apt-key finger - list fingerprints"
329 echo " apt-key adv - pass advanced options to gpg (download key)"
330 echo
331 echo "If no specific keyring file is given the command applies to all keyring files."
332 }
333
334 while [ -n "$1" ]; do
335 case "$1" in
336 --keyring)
337 shift
338 TRUSTEDFILE="$1"
339 FORCED_KEYRING="$1"
340 shift
341 ;;
342 --secret-keyring)
343 shift
344 FORCED_SECRET_KEYRING="$1"
345 shift
346 ;;
347 --fakeroot)
348 requires_root() { true; }
349 shift
350 ;;
351 --quiet)
352 aptkey_echo() { true; }
353 shift
354 ;;
355 --*)
356 echo >&2 "Unknown option: $1"
357 usage
358 exit 1;;
359 *)
360 break;;
361 esac
362 done
363
364 if [ -z "$TRUSTEDFILE" ]; then
365 TRUSTEDFILE="/etc/apt/trusted.gpg"
366 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
367 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
368 fi
369
370 command="$1"
371 if [ -z "$command" ]; then
372 usage
373 exit 1
374 fi
375 shift
376
377 if [ "$command" != "help" ]; then
378 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
379
380 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev/null 2>&1; then
381 true
382 elif which gpg >/dev/null 2>&1; then
383 GPG_EXE="gpg"
384 elif which gpg2 >/dev/null 2>&1; then
385 GPG_EXE="gpg2"
386 else
387 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
388 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
389 echo >&2
390 exit 255
391 fi
392
393 GPG_CMD="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
394
395 # gpg needs (in different versions more or less) files to function correctly,
396 # so we give it its own homedir and generate some valid content for it
397 GPGHOMEDIR="$(mktemp -d)"
398 CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
399 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
400 chmod 700 "$GPGHOMEDIR"
401 # We don't use a secret keyring, of course, but gpg panics and
402 # implodes if there isn't one available - and writeable for imports
403 SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
404 touch $SECRETKEYRING
405 GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR"
406 # create the trustdb with an (empty) dummy keyring
407 # older gpgs required it, newer gpgs even warn that it isn't needed,
408 # but require it nonetheless for some commands, so we just play safe
409 # here for the foreseeable future and create a dummy one
410 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1
411 # tell gpg that it shouldn't try to maintain a trustdb file
412 GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always"
413 GPG="$GPG_CMD"
414
415 # for advanced operations, we might really need a secret keyring after all
416 if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
417 rm -f "$SECRETKEYRING"
418 cp -a "$FORCED_SECRET_KEYRING" "$SECRETKEYRING"
419 fi
420 fi
421
422 case "$command" in
423 add)
424 requires_root
425 setup_merged_keyring
426 $GPG --quiet --batch --import "$@"
427 merge_back_changes
428 aptkey_echo "OK"
429 ;;
430 del|rm|remove)
431 requires_root
432 foreach_keyring_do 'remove_key_from_keyring' "$@"
433 merge_back_changes
434 aptkey_echo "OK"
435 ;;
436 update)
437 requires_root
438 setup_merged_keyring
439 update
440 merge_back_changes
441 ;;
442 net-update)
443 requires_root
444 setup_merged_keyring
445 net_update
446 merge_back_changes
447 ;;
448 list)
449 foreach_keyring_do 'list_keys_from_keyring' "$@"
450 ;;
451 finger*)
452 foreach_keyring_do 'fingerprint_keys_from_keyring' "$@"
453 ;;
454 export|exportall)
455 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
456 $GPG_CMD --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
457 ;;
458 adv*)
459 setup_merged_keyring
460 aptkey_echo "Executing: $GPG $*"
461 $GPG "$@"
462 merge_back_changes
463 ;;
464 help)
465 usage
466 ;;
467 *)
468 usage
469 exit 1
470 ;;
471 esac