]> git.saurik.com Git - apt.git/blob - cmdline/apt-key.in
support gnupg2 as drop-in replacement for gnupg
[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 TMP_KEYRING=${APT_DIR}/var/lib/apt/keyrings/maybe-import-keyring.gpg
18
19 aptkey_echo() { echo "$@"; }
20
21 requires_root() {
22 if [ "$(id -u)" -ne 0 ]; then
23 echo >&2 "ERROR: This command can only be used by root."
24 exit 1
25 fi
26 }
27
28 add_keys_with_verify_against_master_keyring() {
29 ADD_KEYRING=$1
30 MASTER=$2
31
32 if [ ! -f "$ADD_KEYRING" ]; then
33 echo >&2 "ERROR: '$ADD_KEYRING' not found"
34 return
35 fi
36 if [ ! -f "$MASTER" ]; then
37 echo >&2 "ERROR: '$MASTER' not found"
38 return
39 fi
40
41 # when adding new keys, make sure that the archive-master-keyring
42 # is honored. so:
43 # all keys that are exported must have a valid signature
44 # from a key in the $distro-master-keyring
45 add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
46 all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
47 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
48
49 # ensure there are no colisions LP: #857472
50 for all_add_key in $all_add_keys; do
51 for master_key in $master_keys; do
52 if [ "$all_add_key" = "$master_key" ]; then
53 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
54 return 1
55 fi
56 done
57 done
58
59 for add_key in $add_keys; do
60 # export the add keyring one-by-one
61 rm -f $TMP_KEYRING
62 $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
63 # check if signed with the master key and only add in this case
64 ADDED=0
65 for master_key in $master_keys; do
66 if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then
67 $GPG --import $TMP_KEYRING
68 ADDED=1
69 fi
70 done
71 if [ $ADDED = 0 ]; then
72 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
73 fi
74 done
75 rm -f $TMP_KEYRING
76 }
77
78 # update the current archive signing keyring from a network URI
79 # the archive-keyring keys needs to be signed with the master key
80 # (otherwise it does not make sense from a security POV)
81 net_update() {
82 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
83 APT_KEY_NET_UPDATE_ENABLED=""
84 eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
85 if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
86 exit 1
87 fi
88
89 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
90 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
91 exit 1
92 fi
93 # in theory we would need to depend on wget for this, but this feature
94 # isn't useable in debian anyway as we have no keyring uri nor a master key
95 if ! which wget >/dev/null 2>&1; then
96 echo >&2 "ERROR: an installed wget is required for a network-based update"
97 exit 1
98 fi
99 if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
100 mkdir -p ${APT_DIR}/var/lib/apt/keyrings
101 fi
102 keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
103 old_mtime=0
104 if [ -e $keyring ]; then
105 old_mtime=$(stat -c %Y $keyring)
106 fi
107 (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
108 if [ ! -e $keyring ]; then
109 return
110 fi
111 new_mtime=$(stat -c %Y $keyring)
112 if [ $new_mtime -ne $old_mtime ]; then
113 aptkey_echo "Checking for new archive signing keys now"
114 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
115 fi
116 }
117
118 update() {
119 if [ ! -f $ARCHIVE_KEYRING ]; then
120 echo >&2 "ERROR: Can't find the archive-keyring"
121 echo >&2 "Is the &keyring-package; package installed?"
122 exit 1
123 fi
124
125 # add new keys from the package;
126
127 # we do not use add_keys_with_verify_against_master_keyring here,
128 # because "update" is run on regular package updates. A
129 # attacker might as well replace the master-archive-keyring file
130 # in the package and add his own keys. so this check wouldn't
131 # add any security. we *need* this check on net-update though
132 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
133
134 if [ -r "$REMOVED_KEYS" ]; then
135 # remove no-longer supported/used keys
136 $GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5 | while read key; do
137 foreach_keyring_do 'remove_key_from_keyring' "$key"
138 done
139 else
140 echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
141 fi
142 }
143
144 remove_key_from_keyring() {
145 local KEYRINGFILE="$1"
146 shift
147 # non-existent keyrings have by definition no keys
148 if [ ! -e "$KEYRINGFILE" ]; then
149 return
150 fi
151
152 local GPG="$GPG_CMD --keyring $KEYRINGFILE"
153 while [ -n "$1" ]; do
154 local KEY="$1"
155 shift
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]*${KEY}:"; then
158 continue
159 fi
160 if [ ! -w "$KEYRINGFILE" ]; then
161 echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
162 continue
163 fi
164 # check if it is the only key in the keyring and if so remove the keyring altogether
165 if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
166 mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # 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 "$KEYRINGFILE" ]; then
172 REALTARGET="$(readlink -f "$KEYRINGFILE")"
173 mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
174 cp -a "$REALTARGET" "$KEYRINGFILE"
175 fi
176 # delete the key from the keyring
177 $GPG --batch --delete-key --yes "$KEY"
178 if [ -n "$REALTARGET" ]; then
179 # the real backup is the old link, not the copy we made
180 mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
181 fi
182 done
183 }
184
185 foreach_keyring_do() {
186 local ACTION="$1"
187 shift
188 # if a --keyring was given, just remove from there
189 if [ -n "$FORCED_KEYRING" ]; then
190 $ACTION "$FORCED_KEYRING" "$@"
191 else
192 # otherwise all known keyrings are up for inspection
193 if [ -s "$TRUSTEDFILE" ]; then
194 $ACTION "$TRUSTEDFILE" "$@"
195 fi
196 local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
197 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
198 if [ -d "$TRUSTEDPARTS" ]; then
199 # strip / suffix as gpg will double-slash in that case (#665411)
200 local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
201 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
202 TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
203 fi
204 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
205 if [ -s "$trusted" ]; then
206 $ACTION "$trusted" "$@"
207 fi
208 done
209 fi
210 fi
211 }
212
213 list_keys_from_keyring() {
214 local KEYRINGFILE="$1"
215 shift
216 # don't show the error message if this keyring doesn't include the key
217 $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev/null || true
218 }
219
220 fingerprint_keys_from_keyring() {
221 local KEYRINGFILE="$1"
222 shift
223 # don't show the error message if this keyring doesn't include the fingerprint
224 $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev/null || true
225 }
226
227 import_keys_from_keyring() {
228 local IMPORT="$1"
229 local KEYRINGFILE="$2"
230 $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev/null 2>&1
231 }
232
233 setup_merged_keyring() {
234 local TRUSTEDFILE_BAK="$TRUSTEDFILE"
235 TRUSTEDFILE='/dev/null'
236 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
237 TRUSTEDFILE="$TRUSTEDFILE_BAK"
238 # mark it as non-writeable so users get errors if gnupg tries to modify it
239 if [ -s "${GPGHOMEDIR}/trusted.gpg" ]; then
240 chmod -w "${GPGHOMEDIR}/trusted.gpg"
241 GPG="$GPG --keyring ${GPGHOMEDIR}/trusted.gpg"
242 fi
243 if [ -r "$TRUSTEDFILE" ]; then
244 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
245 fi
246 }
247
248
249 usage() {
250 echo "Usage: apt-key [--keyring file] [command] [arguments]"
251 echo
252 echo "Manage apt's list of trusted keys"
253 echo
254 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
255 echo " apt-key del <keyid> - remove the key <keyid>"
256 echo " apt-key export <keyid> - output the key <keyid>"
257 echo " apt-key exportall - output all trusted keys"
258 echo " apt-key update - update keys using the keyring package"
259 echo " apt-key net-update - update keys using the network"
260 echo " apt-key list - list keys"
261 echo " apt-key finger - list fingerprints"
262 echo " apt-key adv - pass advanced options to gpg (download key)"
263 echo
264 echo "If no specific keyring file is given the command applies to all keyring files."
265 }
266
267 while [ -n "$1" ]; do
268 case "$1" in
269 --keyring)
270 shift
271 TRUSTEDFILE="$1"
272 FORCED_KEYRING="$1"
273 shift
274 ;;
275 --fakeroot)
276 requires_root() { true; }
277 shift
278 ;;
279 --quiet)
280 aptkey_echo() { true; }
281 shift
282 ;;
283 --*)
284 echo >&2 "Unknown option: $1"
285 usage
286 exit 1;;
287 *)
288 break;;
289 esac
290 done
291
292 if [ -z "$TRUSTEDFILE" ]; then
293 TRUSTEDFILE="/etc/apt/trusted.gpg"
294 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
295 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
296 fi
297
298 command="$1"
299 if [ -z "$command" ]; then
300 usage
301 exit 1
302 fi
303 shift
304
305 if [ "$command" != "help" ]; then
306 eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
307
308 if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev/null 2>&1; then
309 true
310 elif which gpg >/dev/null 2>&1; then
311 GPG_EXE="gpg"
312 elif which gpg2 >/dev/null 2>&1; then
313 GPG_EXE="gpg2"
314 else
315 echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
316 echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
317 echo >&2
318 exit 255
319 fi
320
321 GPG_CMD="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
322
323 # gpg needs (in different versions more or less) files to function correctly,
324 # so we give it its own homedir and generate some valid content for it
325 GPGHOMEDIR="$(mktemp -d)"
326 CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
327 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
328 chmod 700 "$GPGHOMEDIR"
329 # We don't use a secret keyring, of course, but gpg panics and
330 # implodes if there isn't one available - and writeable for imports
331 SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
332 touch $SECRETKEYRING
333 GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR"
334 # create the trustdb with an (empty) dummy keyring
335 # older gpgs required it, newer gpgs even warn that it isn't needed,
336 # but require it nonetheless for some commands, so we just play safe
337 # here for the foreseeable future and create a dummy one
338 $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1
339 # tell gpg that it shouldn't try to maintain a trustdb file
340 GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always"
341 GPG="$GPG_CMD"
342
343 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
344 if ! [ -e "$TRUSTEDFILE" ]; then
345 if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
346 touch -- "$TRUSTEDFILE"
347 chmod 0644 -- "$TRUSTEDFILE"
348 fi
349 fi
350 fi
351
352 case "$command" in
353 add)
354 requires_root
355 setup_merged_keyring
356 $GPG --quiet --batch --import "$@"
357 aptkey_echo "OK"
358 ;;
359 del|rm|remove)
360 requires_root
361 foreach_keyring_do 'remove_key_from_keyring' "$@"
362 aptkey_echo "OK"
363 ;;
364 update)
365 requires_root
366 setup_merged_keyring
367 update
368 ;;
369 net-update)
370 requires_root
371 setup_merged_keyring
372 net_update
373 ;;
374 list)
375 foreach_keyring_do 'list_keys_from_keyring' "$@"
376 ;;
377 finger*)
378 foreach_keyring_do 'fingerprint_keys_from_keyring' "$@"
379 ;;
380 export|exportall)
381 foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
382 $GPG_CMD --keyring "${GPGHOMEDIR}/trusted.gpg" --armor --export "$@"
383 ;;
384 adv*)
385 setup_merged_keyring
386 aptkey_echo "Executing: $GPG $*"
387 $GPG "$@"
388 ;;
389 help)
390 usage
391 ;;
392 *)
393 usage
394 exit 1
395 ;;
396 esac