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