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