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