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