]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/sh | |
2 | ||
3 | set -e | |
4 | unset GREP_OPTIONS | |
5 | export IFS="$(printf "\n\b")" | |
6 | ||
7 | MASTER_KEYRING='&keyring-master-filename;' | |
8 | eval "$(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)" | |
9 | ARCHIVE_KEYRING='&keyring-filename;' | |
10 | eval "$(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)" | |
11 | REMOVED_KEYS='&keyring-removed-filename;' | |
12 | eval "$(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)" | |
13 | ARCHIVE_KEYRING_URI='&keyring-uri;' | |
14 | eval "$(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)" | |
15 | ||
16 | aptkey_echo() { echo "$@"; } | |
17 | ||
18 | requires_root() { | |
19 | if [ "$(id -u)" -ne 0 ]; then | |
20 | echo >&2 "ERROR: This command can only be used by root." | |
21 | exit 1 | |
22 | fi | |
23 | } | |
24 | ||
25 | command_available() { | |
26 | # command -v "$1" >/dev/null 2>&1 # not required by policy, see #747320 | |
27 | # which "$1" >/dev/null 2>&1 # is in debianutils (essential) but not on non-debian systems | |
28 | local OLDIFS="$IFS" | |
29 | IFS=: | |
30 | for p in $PATH; do | |
31 | if [ -x "${p}/${1}" ]; then | |
32 | IFS="$OLDIFS" | |
33 | return 0 | |
34 | fi | |
35 | done | |
36 | IFS="$OLDIFS" | |
37 | return 1 | |
38 | } | |
39 | ||
40 | escape_shell() { | |
41 | echo "$@" | sed -e "s#'#'\"'\"'#g" | |
42 | } | |
43 | ||
44 | get_fingerprints_of_keyring() { | |
45 | aptkey_execute "$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine; do | |
46 | # search for a public key | |
47 | if [ "${publine%%:*}" != 'pub' ]; then continue; fi | |
48 | # search for the associated fingerprint (should be the very next line) | |
49 | while read fprline; do | |
50 | if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen | |
51 | elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi | |
52 | echo "$fprline" | cut -d':' -f 10 | |
53 | done | |
54 | # order in the keyring shouldn't be important | |
55 | done | sort | |
56 | } | |
57 | ||
58 | add_keys_with_verify_against_master_keyring() { | |
59 | ADD_KEYRING="$1" | |
60 | MASTER="$2" | |
61 | ||
62 | if [ ! -f "$ADD_KEYRING" ]; then | |
63 | echo >&2 "ERROR: '$ADD_KEYRING' not found" | |
64 | return | |
65 | fi | |
66 | if [ ! -f "$MASTER" ]; then | |
67 | echo >&2 "ERROR: '$MASTER' not found" | |
68 | return | |
69 | fi | |
70 | ||
71 | # when adding new keys, make sure that the archive-master-keyring | |
72 | # is honored. so: | |
73 | # all keys that are exported must have a valid signature | |
74 | # from a key in the $distro-master-keyring | |
75 | add_keys="$(get_fingerprints_of_keyring "$ADD_KEYRING")" | |
76 | all_add_keys="$(aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5)" | |
77 | master_keys="$(aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5)" | |
78 | ||
79 | # ensure there are no colisions LP: #857472 | |
80 | for all_add_key in $all_add_keys; do | |
81 | for master_key in $master_keys; do | |
82 | if [ "$all_add_key" = "$master_key" ]; then | |
83 | echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted" | |
84 | return 1 | |
85 | fi | |
86 | done | |
87 | done | |
88 | ||
89 | for add_key in $add_keys; do | |
90 | # export the add keyring one-by-one | |
91 | local TMP_KEYRING="${GPGHOMEDIR}/tmp-keyring.gpg" | |
92 | aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key" | |
93 | if ! aptkey_execute "$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then | |
94 | cat >&2 "${GPGHOMEDIR}/gpgoutput.log" | |
95 | false | |
96 | fi | |
97 | # check if signed with the master key and only add in this case | |
98 | ADDED=0 | |
99 | for master_key in $master_keys; do | |
100 | if aptkey_execute "$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \ | |
101 | | grep '^sig:!:' | cut -d: -f5 | grep -q "$master_key"; then | |
102 | aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \ | |
103 | | aptkey_execute "$GPG" --batch --yes --import | |
104 | ADDED=1 | |
105 | fi | |
106 | done | |
107 | if [ $ADDED = 0 ]; then | |
108 | echo >&2 "Key '$add_key' not added. It is not signed with a master key" | |
109 | fi | |
110 | rm -f "${TMP_KEYRING}" | |
111 | done | |
112 | } | |
113 | ||
114 | # update the current archive signing keyring from a network URI | |
115 | # the archive-keyring keys needs to be signed with the master key | |
116 | # (otherwise it does not make sense from a security POV) | |
117 | net_update() { | |
118 | local APT_DIR='/' | |
119 | eval $(apt-config shell APT_DIR Dir) | |
120 | ||
121 | # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128)) | |
122 | APT_KEY_NET_UPDATE_ENABLED="" | |
123 | eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled) | |
124 | if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then | |
125 | exit 1 | |
126 | fi | |
127 | ||
128 | if [ -z "$ARCHIVE_KEYRING_URI" ]; then | |
129 | echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set" | |
130 | exit 1 | |
131 | fi | |
132 | # in theory we would need to depend on wget for this, but this feature | |
133 | # isn't useable in debian anyway as we have no keyring uri nor a master key | |
134 | if ! command_available 'wget'; then | |
135 | echo >&2 "ERROR: an installed wget is required for a network-based update" | |
136 | exit 1 | |
137 | fi | |
138 | if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then | |
139 | mkdir -p "${APT_DIR}/var/lib/apt/keyrings" | |
140 | fi | |
141 | keyring="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")" | |
142 | old_mtime=0 | |
143 | if [ -e $keyring ]; then | |
144 | old_mtime=$(stat -c %Y "$keyring") | |
145 | fi | |
146 | (cd "${APT_DIR}/var/lib/apt/keyrings"; wget --timeout=90 -q -N "$ARCHIVE_KEYRING_URI") | |
147 | if [ ! -e "$keyring" ]; then | |
148 | return | |
149 | fi | |
150 | new_mtime=$(stat -c %Y "$keyring") | |
151 | if [ $new_mtime -ne $old_mtime ]; then | |
152 | aptkey_echo "Checking for new archive signing keys now" | |
153 | add_keys_with_verify_against_master_keyring "$keyring" "$MASTER_KEYRING" | |
154 | fi | |
155 | } | |
156 | ||
157 | update() { | |
158 | if [ ! -f "$ARCHIVE_KEYRING" ]; then | |
159 | echo >&2 "ERROR: Can't find the archive-keyring" | |
160 | echo >&2 "Is the &keyring-package; package installed?" | |
161 | exit 1 | |
162 | fi | |
163 | ||
164 | # add new keys from the package; | |
165 | ||
166 | # we do not use add_keys_with_verify_against_master_keyring here, | |
167 | # because "update" is run on regular package updates. A | |
168 | # attacker might as well replace the master-archive-keyring file | |
169 | # in the package and add his own keys. so this check wouldn't | |
170 | # add any security. we *need* this check on net-update though | |
171 | import_keyring_into_keyring "$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log" | |
172 | ||
173 | if [ -r "$REMOVED_KEYS" ]; then | |
174 | # remove no-longer supported/used keys | |
175 | get_fingerprints_of_keyring "$REMOVED_KEYS" | while read key; do | |
176 | foreach_keyring_do 'remove_key_from_keyring' "$key" | |
177 | done | |
178 | else | |
179 | echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" | |
180 | fi | |
181 | } | |
182 | ||
183 | remove_key_from_keyring() { | |
184 | local KEYRINGFILE="$1" | |
185 | shift | |
186 | # non-existent keyrings have by definition no keys | |
187 | if [ ! -e "$KEYRINGFILE" ]; then | |
188 | return | |
189 | fi | |
190 | ||
191 | for KEY in "$@"; do | |
192 | local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst" | |
193 | get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS" | |
194 | ||
195 | # strip leading 0x, if present: | |
196 | KEY="${KEY#0x}" | |
197 | ||
198 | # check if the key is in this keyring | |
199 | if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then | |
200 | continue | |
201 | fi | |
202 | if [ ! -w "$KEYRINGFILE" ]; then | |
203 | echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only." | |
204 | continue | |
205 | fi | |
206 | # check if it is the only key in the keyring and if so remove the keyring altogether | |
207 | if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then | |
208 | mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg | |
209 | return | |
210 | fi | |
211 | # we can't just modify pointed to files as these might be in /usr or something | |
212 | local REALTARGET | |
213 | if [ -L "$KEYRINGFILE" ]; then | |
214 | REALTARGET="$(readlink -f "$KEYRINGFILE")" | |
215 | mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp" | |
216 | cp -a "$REALTARGET" "$KEYRINGFILE" | |
217 | fi | |
218 | # delete the key from the keyring | |
219 | aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY" | |
220 | if [ -n "$REALTARGET" ]; then | |
221 | # the real backup is the old link, not the copy we made | |
222 | mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~" | |
223 | fi | |
224 | done | |
225 | } | |
226 | ||
227 | foreach_keyring_do() { | |
228 | local ACTION="$1" | |
229 | shift | |
230 | # if a --keyring was given, just work on this one | |
231 | if [ -n "$FORCED_KEYRING" ]; then | |
232 | $ACTION "$FORCED_KEYRING" "$@" | |
233 | else | |
234 | # otherwise all known keyrings are up for inspection | |
235 | if [ -s "$TRUSTEDFILE" ]; then | |
236 | $ACTION "$TRUSTEDFILE" "$@" | |
237 | fi | |
238 | local TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
239 | eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)" | |
240 | if [ -d "$TRUSTEDPARTS" ]; then | |
241 | TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")" | |
242 | local TRUSTEDPARTSLIST="$(cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg')" | |
243 | for trusted in $(echo "$TRUSTEDPARTSLIST" | sort); do | |
244 | if [ -s "$trusted" ]; then | |
245 | $ACTION "$trusted" "$@" | |
246 | fi | |
247 | done | |
248 | fi | |
249 | fi | |
250 | } | |
251 | ||
252 | run_cmd_on_keyring() { | |
253 | local KEYRINGFILE="$1" | |
254 | shift | |
255 | # fingerprint and co will fail if key isn't in this keyring | |
256 | aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true | |
257 | } | |
258 | ||
259 | import_keyring_into_keyring() { | |
260 | local FROM="${1:-${GPGHOMEDIR}/pubring.gpg}" | |
261 | local TO="${2:-${GPGHOMEDIR}/pubring.gpg}" | |
262 | shift 2 | |
263 | rm -f "${GPGHOMEDIR}/gpgoutput.log" | |
264 | # the idea is simple: We take keys from one keyring and copy it to another | |
265 | # we do this with so many checks in between to ensure that WE control the | |
266 | # creation, so we know that the (potentially) created $TO keyring is a | |
267 | # simple keyring rather than a keybox as gpg2 would create it which in turn | |
268 | # can't be read by gpgv. | |
269 | # BEWARE: This is designed more in the way to work with the current | |
270 | # callers, than to have a well defined it would be easy to add new callers to. | |
271 | if [ ! -s "$TO" ]; then | |
272 | if [ -s "$FROM" ]; then | |
273 | if [ -z "$2" ]; then | |
274 | if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then | |
275 | cat >&2 "${GPGHOMEDIR}/gpgoutput.log" | |
276 | false | |
277 | else | |
278 | chmod 0644 -- "$TO" | |
279 | fi | |
280 | else | |
281 | create_new_keyring "$TO" | |
282 | fi | |
283 | else | |
284 | create_new_keyring "$TO" | |
285 | fi | |
286 | elif [ -s "$FROM" ]; then | |
287 | local EXPORTLIMIT="$1" | |
288 | if [ -n "$1$2" ]; then shift; fi | |
289 | if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \ | |
290 | | aptkey_execute "$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then | |
291 | cat >&2 "${GPGHOMEDIR}/gpgoutput.log" | |
292 | false | |
293 | fi | |
294 | fi | |
295 | } | |
296 | ||
297 | merge_all_trusted_keyrings_into_pubring() { | |
298 | # does the same as: | |
299 | # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg" | |
300 | # but without using gpg, just cat and find | |
301 | local PUBRING="$(readlink -f "${GPGHOMEDIR}/pubring.gpg")" | |
302 | # if a --keyring was given, just use this one | |
303 | if [ -n "$FORCED_KEYRING" ]; then | |
304 | if [ -s "$FORCED_KEYRING" ]; then | |
305 | cp --dereference "$FORCED_KEYRING" "$PUBRING" | |
306 | fi | |
307 | else | |
308 | # otherwise all known keyrings are merged | |
309 | local TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
310 | eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) | |
311 | if [ -d "$TRUSTEDPARTS" ]; then | |
312 | rm -f "$PUBRING" | |
313 | if [ -s "$TRUSTEDFILE" ]; then | |
314 | cat "$TRUSTEDFILE" > "$PUBRING" | |
315 | fi | |
316 | TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")" | |
317 | (cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg' -exec cat {} + >> "$PUBRING";) | |
318 | elif [ -s "$TRUSTEDFILE" ]; then | |
319 | cp --dereference "$TRUSTEDFILE" "$PUBRING" | |
320 | fi | |
321 | fi | |
322 | ||
323 | if [ ! -s "$PUBRING" ]; then | |
324 | touch "$PUBRING" | |
325 | fi | |
326 | } | |
327 | ||
328 | import_keys_from_keyring() { | |
329 | import_keyring_into_keyring "$1" "$2" | |
330 | } | |
331 | ||
332 | merge_keys_into_keyrings() { | |
333 | import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only' | |
334 | } | |
335 | ||
336 | merge_back_changes() { | |
337 | if [ -n "$FORCED_KEYRING" ]; then | |
338 | # if the keyring was forced merge is already done | |
339 | return | |
340 | fi | |
341 | if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then | |
342 | # merge all updated keys | |
343 | foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg" | |
344 | fi | |
345 | # look for keys which were added or removed | |
346 | get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst" | |
347 | get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst" | |
348 | comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff" | |
349 | # key isn't part of new keyring, so remove | |
350 | cut -f 2 "${GPGHOMEDIR}/pubring.diff" | while read key; do | |
351 | if [ -z "$key" ]; then continue; fi | |
352 | foreach_keyring_do 'remove_key_from_keyring' "$key" | |
353 | done | |
354 | # key is only part of new keyring, so we need to import it | |
355 | cut -f 1 "${GPGHOMEDIR}/pubring.diff" | while read key; do | |
356 | if [ -z "$key" ]; then continue; fi | |
357 | import_keyring_into_keyring '' "$TRUSTEDFILE" "$key" | |
358 | done | |
359 | } | |
360 | ||
361 | setup_merged_keyring() { | |
362 | if [ -n "$FORCED_KEYID" ]; then | |
363 | merge_all_trusted_keyrings_into_pubring | |
364 | FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg" | |
365 | TRUSTEDFILE="${FORCED_KEYRING}" | |
366 | echo "#!/bin/sh | |
367 | exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" | |
368 | GPG="${GPGHOMEDIR}/gpg.1.sh" | |
369 | # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty | |
370 | import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true | |
371 | elif [ -z "$FORCED_KEYRING" ]; then | |
372 | merge_all_trusted_keyrings_into_pubring | |
373 | if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then | |
374 | cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg" | |
375 | else | |
376 | touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg" | |
377 | fi | |
378 | echo "#!/bin/sh | |
379 | exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" | |
380 | GPG="${GPGHOMEDIR}/gpg.1.sh" | |
381 | else | |
382 | create_new_keyring "$TRUSTEDFILE" | |
383 | echo "#!/bin/sh | |
384 | exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" | |
385 | GPG="${GPGHOMEDIR}/gpg.1.sh" | |
386 | fi | |
387 | } | |
388 | ||
389 | create_new_keyring() { | |
390 | # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead. | |
391 | if ! [ -e "$TRUSTEDFILE" ]; then | |
392 | if [ -w "$(dirname "$TRUSTEDFILE")" ]; then | |
393 | touch -- "$TRUSTEDFILE" | |
394 | chmod 0644 -- "$TRUSTEDFILE" | |
395 | fi | |
396 | fi | |
397 | } | |
398 | ||
399 | aptkey_execute() { sh "$@"; } | |
400 | ||
401 | usage() { | |
402 | echo "Usage: apt-key [--keyring file] [command] [arguments]" | |
403 | echo | |
404 | echo "Manage apt's list of trusted keys" | |
405 | echo | |
406 | echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)" | |
407 | echo " apt-key del <keyid> - remove the key <keyid>" | |
408 | echo " apt-key export <keyid> - output the key <keyid>" | |
409 | echo " apt-key exportall - output all trusted keys" | |
410 | echo " apt-key update - update keys using the keyring package" | |
411 | echo " apt-key net-update - update keys using the network" | |
412 | echo " apt-key list - list keys" | |
413 | echo " apt-key finger - list fingerprints" | |
414 | echo " apt-key adv - pass advanced options to gpg (download key)" | |
415 | echo | |
416 | echo "If no specific keyring file is given the command applies to all keyring files." | |
417 | } | |
418 | ||
419 | while [ -n "$1" ]; do | |
420 | case "$1" in | |
421 | --keyring) | |
422 | shift | |
423 | TRUSTEDFILE="$1" | |
424 | FORCED_KEYRING="$1" | |
425 | ;; | |
426 | --keyid) | |
427 | shift | |
428 | FORCED_KEYID="$1" | |
429 | ;; | |
430 | --secret-keyring) | |
431 | shift | |
432 | FORCED_SECRET_KEYRING="$1" | |
433 | ;; | |
434 | --readonly) | |
435 | merge_back_changes() { true; } | |
436 | create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; } | |
437 | ;; | |
438 | --fakeroot) | |
439 | requires_root() { true; } | |
440 | ;; | |
441 | --quiet) | |
442 | aptkey_echo() { true; } | |
443 | ;; | |
444 | --debug1) | |
445 | # some cmds like finger redirect stderr to /dev/null … | |
446 | aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; } | |
447 | ;; | |
448 | --debug2) | |
449 | # … other more complicated ones pipe gpg into gpg. | |
450 | aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; } | |
451 | ;; | |
452 | --*) | |
453 | echo >&2 "Unknown option: $1" | |
454 | usage | |
455 | exit 1;; | |
456 | *) | |
457 | break;; | |
458 | esac | |
459 | shift | |
460 | done | |
461 | ||
462 | if [ -z "$TRUSTEDFILE" ]; then | |
463 | TRUSTEDFILE="/etc/apt/trusted.gpg" | |
464 | eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring) | |
465 | eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f) | |
466 | fi | |
467 | ||
468 | command="$1" | |
469 | if [ -z "$command" ]; then | |
470 | usage | |
471 | exit 1 | |
472 | fi | |
473 | shift | |
474 | ||
475 | create_gpg_home() { | |
476 | # gpg needs (in different versions more or less) files to function correctly, | |
477 | # so we give it its own homedir and generate some valid content for it later on | |
478 | if [ -n "$TMPDIR" ]; then | |
479 | # tmpdir is a directory and current user has rwx access to it | |
480 | # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir() | |
481 | if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then | |
482 | unset TMPDIR | |
483 | fi | |
484 | fi | |
485 | GPGHOMEDIR="$(mktemp -d)" | |
486 | CURRENTTRAP="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';" | |
487 | trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM | |
488 | chmod 700 "$GPGHOMEDIR" | |
489 | } | |
490 | ||
491 | prepare_gpg_home() { | |
492 | # crude detection if we are called from a maintainerscript where the | |
493 | # package depends on gnupg or not. We accept recommends here as | |
494 | # well as the script hopefully uses apt-key optionally then like e.g. | |
495 | # debian-archive-keyring for (upgrade) cleanup did | |
496 | if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ]; then | |
497 | if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then | |
498 | cat >&2 <<EOF | |
499 | Warning: The $DPKG_MAINTSCRIPT_NAME maintainerscript of the package $DPKG_MAINTSCRIPT_PACKAGE | |
500 | Warning: seems to use apt-key (provided by apt) without depending on gnupg or gnupg2. | |
501 | Warning: This will BREAK in the future and should be fixed by the package maintainer(s). | |
502 | Note: Check first if apt-key functionality is needed at all - it probably isn't! | |
503 | EOF | |
504 | fi | |
505 | fi | |
506 | eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)" | |
507 | if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then | |
508 | true | |
509 | elif command_available 'gpg'; then | |
510 | GPG_EXE="gpg" | |
511 | elif command_available 'gpg2'; then | |
512 | GPG_EXE="gpg2" | |
513 | else | |
514 | echo >&2 "Error: gnupg or gnupg2 do not seem to be installed," | |
515 | echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation." | |
516 | echo >&2 | |
517 | exit 255 | |
518 | fi | |
519 | ||
520 | create_gpg_home | |
521 | ||
522 | # create the trustdb with an (empty) dummy keyring | |
523 | # older gpgs required it, newer gpgs even warn that it isn't needed, | |
524 | # but require it nonetheless for some commands, so we just play safe | |
525 | # here for the foreseeable future and create a dummy one | |
526 | touch "${GPGHOMEDIR}/empty.gpg" | |
527 | if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \ | |
528 | --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then | |
529 | cat >&2 "${GPGHOMEDIR}/gpgoutput.log" | |
530 | false | |
531 | fi | |
532 | ||
533 | # now tell gpg that it shouldn't try to maintain this trustdb file | |
534 | echo "#!/bin/sh | |
535 | exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\ | |
536 | --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh" | |
537 | GPG_SH="${GPGHOMEDIR}/gpg.0.sh" | |
538 | GPG="$GPG_SH" | |
539 | ||
540 | # We don't usually need a secret keyring, of course, but | |
541 | # for advanced operations, we might really need a secret keyring after all | |
542 | if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then | |
543 | if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then | |
544 | cat >&2 "${GPGHOMEDIR}/gpgoutput.log" | |
545 | false | |
546 | fi | |
547 | else | |
548 | # and then, there are older versions of gpg which panic and implode | |
549 | # if there isn't one available - and writeable for imports | |
550 | # and even if not output is littered with the creation of a secring, | |
551 | # so lets call import once to have it create what it wants in silence | |
552 | echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true | |
553 | fi | |
554 | } | |
555 | ||
556 | if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then | |
557 | prepare_gpg_home | |
558 | fi | |
559 | ||
560 | case "$command" in | |
561 | add) | |
562 | requires_root | |
563 | setup_merged_keyring | |
564 | aptkey_execute "$GPG" --quiet --batch --import "$@" | |
565 | merge_back_changes | |
566 | aptkey_echo "OK" | |
567 | ;; | |
568 | del|rm|remove) | |
569 | requires_root | |
570 | foreach_keyring_do 'remove_key_from_keyring' "$@" | |
571 | aptkey_echo "OK" | |
572 | ;; | |
573 | update) | |
574 | requires_root | |
575 | setup_merged_keyring | |
576 | update | |
577 | merge_back_changes | |
578 | ;; | |
579 | net-update) | |
580 | requires_root | |
581 | setup_merged_keyring | |
582 | net_update | |
583 | merge_back_changes | |
584 | ;; | |
585 | list) | |
586 | foreach_keyring_do 'run_cmd_on_keyring' --list-keys "$@" | |
587 | ;; | |
588 | finger*) | |
589 | foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@" | |
590 | ;; | |
591 | export|exportall) | |
592 | merge_all_trusted_keyrings_into_pubring | |
593 | aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@" | |
594 | ;; | |
595 | adv*) | |
596 | setup_merged_keyring | |
597 | aptkey_echo "Executing: $GPG $*" | |
598 | aptkey_execute "$GPG" "$@" | |
599 | merge_back_changes | |
600 | ;; | |
601 | verify) | |
602 | GPGV='' | |
603 | eval $(apt-config shell GPGV Apt::Key::gpgvcommand) | |
604 | if [ -n "$GPGV" ] && command_available "$GPGV"; then true; | |
605 | elif command_available 'gpgv'; then GPGV='gpgv'; | |
606 | elif command_available 'gpgv2'; then GPGV='gpgv2'; | |
607 | else | |
608 | echo >&2 'ERROR: gpgv or gpgv2 required for verification' | |
609 | exit 29 | |
610 | fi | |
611 | # for a forced keyid we need gpg --export, so full wrapping required | |
612 | if [ -n "$FORCED_KEYID" ]; then | |
613 | prepare_gpg_home | |
614 | else | |
615 | create_gpg_home | |
616 | fi | |
617 | setup_merged_keyring | |
618 | if [ -n "$FORCED_KEYRING" ]; then | |
619 | "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@" | |
620 | else | |
621 | "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@" | |
622 | fi | |
623 | ;; | |
624 | help) | |
625 | usage | |
626 | ;; | |
627 | *) | |
628 | usage | |
629 | exit 1 | |
630 | ;; | |
631 | esac |