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