X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/e23ee4c21c6d8045ab020526aa864a48dc16ddd9..8757a0f:/cmdline/apt-key.in diff --git a/cmdline/apt-key.in b/cmdline/apt-key.in index f1d021e8a..21df37ffd 100644 --- a/cmdline/apt-key.in +++ b/cmdline/apt-key.in @@ -4,17 +4,14 @@ set -e unset GREP_OPTIONS export IFS="$(printf "\n\b")" -APT_DIR="/" -eval $(apt-config shell APT_DIR Dir) - MASTER_KEYRING='&keyring-master-filename;' -eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring) +eval "$(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)" ARCHIVE_KEYRING='&keyring-filename;' -eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring) +eval "$(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)" REMOVED_KEYS='&keyring-removed-filename;' -eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys) +eval "$(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)" ARCHIVE_KEYRING_URI='&keyring-uri;' -eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI) +eval "$(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)" aptkey_echo() { echo "$@"; } @@ -26,6 +23,7 @@ requires_root() { } command_available() { + if [ -x "$1" ]; then return 0; fi # command -v "$1" >/dev/null 2>&1 # not required by policy, see #747320 # which "$1" >/dev/null 2>&1 # is in debianutils (essential) but not on non-debian systems local OLDIFS="$IFS" @@ -40,6 +38,10 @@ command_available() { return 1 } +escape_shell() { + echo "$@" | sed -e "s#'#'\"'\"'#g" +} + get_fingerprints_of_keyring() { aptkey_execute "$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine; do # search for a public key @@ -55,8 +57,8 @@ get_fingerprints_of_keyring() { } add_keys_with_verify_against_master_keyring() { - ADD_KEYRING=$1 - MASTER=$2 + ADD_KEYRING="$1" + MASTER="$2" if [ ! -f "$ADD_KEYRING" ]; then echo >&2 "ERROR: '$ADD_KEYRING' not found" @@ -72,8 +74,8 @@ add_keys_with_verify_against_master_keyring() { # all keys that are exported must have a valid signature # from a key in the $distro-master-keyring add_keys="$(get_fingerprints_of_keyring "$ADD_KEYRING")" - all_add_keys=`aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5` - master_keys=`aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5` + all_add_keys="$(aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5)" + master_keys="$(aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5)" # ensure there are no colisions LP: #857472 for all_add_key in $all_add_keys; do @@ -114,6 +116,9 @@ add_keys_with_verify_against_master_keyring() { # the archive-keyring keys needs to be signed with the master key # (otherwise it does not make sense from a security POV) net_update() { + local APT_DIR='/' + eval $(apt-config shell APT_DIR Dir) + # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128)) APT_KEY_NET_UPDATE_ENABLED="" eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled) @@ -137,7 +142,7 @@ net_update() { keyring="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")" old_mtime=0 if [ -e $keyring ]; then - old_mtime=$(stat -c %Y $keyring) + old_mtime=$(stat -c %Y "$keyring") fi (cd "${APT_DIR}/var/lib/apt/keyrings"; wget --timeout=90 -q -N "$ARCHIVE_KEYRING_URI") if [ ! -e "$keyring" ]; then @@ -151,6 +156,13 @@ net_update() { } update() { + if [ -z "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then + echo >&2 "Warning: 'apt-key update' is deprecated and should not be used anymore!" + if [ -z "$ARCHIVE_KEYRING" ]; then + echo >&2 "Note: In your distribution this command is a no-op and can therefore be removed safely." + exit 0 + fi + fi if [ ! -f "$ARCHIVE_KEYRING" ]; then echo >&2 "ERROR: Can't find the archive-keyring" echo >&2 "Is the &keyring-package; package installed?" @@ -187,6 +199,10 @@ remove_key_from_keyring() { for KEY in "$@"; do local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst" get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS" + + # strip leading 0x, if present: + KEY="$(echo "${KEY#0x}" | tr -d ' ')" + # check if the key is in this keyring if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then continue @@ -228,14 +244,11 @@ foreach_keyring_do() { $ACTION "$TRUSTEDFILE" "$@" fi local TRUSTEDPARTS="/etc/apt/trusted.gpg.d" - eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) + eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)" if [ -d "$TRUSTEDPARTS" ]; then - # strip / suffix as gpg will double-slash in that case (#665411) - local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}" - if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then - TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS" - fi - for trusted in $(find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -regex '^.*\.gpg$' | sort); do + TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")" + local TRUSTEDPARTSLIST="$(cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg')" + for trusted in $(echo "$TRUSTEDPARTSLIST" | sort); do if [ -s "$trusted" ]; then $ACTION "$trusted" "$@" fi @@ -293,7 +306,7 @@ merge_all_trusted_keyrings_into_pubring() { # does the same as: # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg" # but without using gpg, just cat and find - local PUBRING="${GPGHOMEDIR}/pubring.gpg" + local PUBRING="$(readlink -f "${GPGHOMEDIR}")/pubring.gpg" # if a --keyring was given, just use this one if [ -n "$FORCED_KEYRING" ]; then if [ -s "$FORCED_KEYRING" ]; then @@ -304,13 +317,12 @@ merge_all_trusted_keyrings_into_pubring() { local TRUSTEDPARTS="/etc/apt/trusted.gpg.d" eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) if [ -d "$TRUSTEDPARTS" ]; then - # ignore errors mostly for non-existing $TRUSTEDFILE - { - cat "$TRUSTEDFILE" || true - for parts in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do - cat "$parts" || true - done - } > "$PUBRING" 2>/dev/null + rm -f "$PUBRING" + if [ -s "$TRUSTEDFILE" ]; then + cat "$TRUSTEDFILE" > "$PUBRING" + fi + TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")" + (cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg' -exec cat {} + >> "$PUBRING";) elif [ -s "$TRUSTEDFILE" ]; then cp --dereference "$TRUSTEDFILE" "$PUBRING" fi @@ -360,7 +372,7 @@ setup_merged_keyring() { FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg" TRUSTEDFILE="${FORCED_KEYRING}" echo "#!/bin/sh -exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" +exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" GPG="${GPGHOMEDIR}/gpg.1.sh" # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true @@ -372,12 +384,12 @@ exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1. touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg" fi echo "#!/bin/sh -exec sh \"${GPG}\" --keyring \"${GPGHOMEDIR}/pubring.gpg\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" +exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" GPG="${GPGHOMEDIR}/gpg.1.sh" else create_new_keyring "$TRUSTEDFILE" echo "#!/bin/sh -exec sh \"${GPG}\" --keyring \"${TRUSTEDFILE}\" \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" +exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" GPG="${GPGHOMEDIR}/gpg.1.sh" fi } @@ -468,6 +480,14 @@ if [ -z "$command" ]; then fi shift +cleanup_gpg_home() { + if [ -z "$GPGHOMEDIR" ]; then return; fi + if command_available 'gpgconf'; then + GNUPGHOME="${GPGHOMEDIR}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true + fi + rm -rf "$GPGHOMEDIR" +} + create_gpg_home() { # gpg needs (in different versions more or less) files to function correctly, # so we give it its own homedir and generate some valid content for it later on @@ -479,61 +499,92 @@ create_gpg_home() { fi fi GPGHOMEDIR="$(mktemp -d)" - CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';" + CURRENTTRAP="${CURRENTTRAP} cleanup_gpg_home;" trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM + if [ -z "$GPGHOMEDIR" ]; then + echo "ERROR: Could not create temporary gpg home directory in apt-key ($TMPDIR)" + exit 28 + fi chmod 700 "$GPGHOMEDIR" } prepare_gpg_home() { - eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand) - + # crude detection if we are called from a maintainerscript where the + # package depends on gnupg or not. We accept recommends here as + # well as the script hopefully uses apt-key optionally then like e.g. + # debian-archive-keyring for (upgrade) cleanup did + if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ] && [ -z "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then + if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then + cat >&2 <&2 "Error: gnupg or gnupg2 do not seem to be installed," - echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation." + echo >&2 "Error: gnupg, gnupg2 and gnupg1 do not seem to be installed," + echo >&2 "Error: but apt-key requires gnupg, gnupg2 or gnupg1 for this operation." echo >&2 exit 255 fi create_gpg_home - # We don't use a secret keyring, of course, but gpg panics and - # implodes if there isn't one available - and writeable for imports - SECRETKEYRING="${GPGHOMEDIR}/secring.gpg" - touch "$SECRETKEYRING" - # create the trustdb with an (empty) dummy keyring # older gpgs required it, newer gpgs even warn that it isn't needed, # but require it nonetheless for some commands, so we just play safe # here for the foreseeable future and create a dummy one + touch "${GPGHOMEDIR}/empty.gpg" if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \ - --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "$SECRETKEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then + --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then cat >&2 "${GPGHOMEDIR}/gpgoutput.log" false fi - # tell gpg that it shouldn't try to maintain a trustdb file + + # now tell gpg that it shouldn't try to maintain this trustdb file echo "#!/bin/sh -exec \"${GPG_EXE}\" --ignore-time-conflict --no-options --no-default-keyring \\ - --homedir \"${GPGHOMEDIR}\" --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh" +exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\ +--homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh" GPG_SH="${GPGHOMEDIR}/gpg.0.sh" GPG="$GPG_SH" + # We don't usually need a secret keyring, of course, but # for advanced operations, we might really need a secret keyring after all if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then - rm -f "$SECRETKEYRING" - cp -a "$FORCED_SECRET_KEYRING" "$SECRETKEYRING" + if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then + cat >&2 "${GPGHOMEDIR}/gpgoutput.log" + false + fi + else + # and then, there are older versions of gpg which panic and implode + # if there isn't one available - and writeable for imports + # and even if not output is littered with the creation of a secring, + # so lets call import once to have it create what it wants in silence + echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true fi +} - # older gpg versions need a secring file, but newer versions take it as - # a hint to start a migration from earlier versions. The file is empty - # anyhow, so nothing actually happens, but its three lines of output - # nobody expects to see in apt-key context, so trigger it in silence - echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true +warn_on_script_usage() { + if [ -n "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then + return + fi + # (Maintainer) scripts should not be using apt-key + if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ]; then + echo >&2 "Warning: apt-key should not be used in scripts (called from $DPKG_MAINTSCRIPT_NAME maintainerscript of the package ${DPKG_MAINTSCRIPT_PACKAGE})" + elif [ ! -t 1 ]; then + echo >&2 "Warning: apt-key output should not be parsed (stdout is not a terminal)" + fi } if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then @@ -542,6 +593,7 @@ fi case "$command" in add) + warn_on_script_usage requires_root setup_merged_keyring aptkey_execute "$GPG" --quiet --batch --import "$@" @@ -549,11 +601,13 @@ case "$command" in aptkey_echo "OK" ;; del|rm|remove) + # no script warning here as removing 'add' usage needs 'del' for cleanup requires_root foreach_keyring_do 'remove_key_from_keyring' "$@" aptkey_echo "OK" ;; update) + warn_on_script_usage requires_root setup_merged_keyring update @@ -565,17 +619,17 @@ case "$command" in net_update merge_back_changes ;; - list) - foreach_keyring_do 'run_cmd_on_keyring' --list-keys "$@" - ;; - finger*) + list|finger*) + warn_on_script_usage foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@" ;; export|exportall) + warn_on_script_usage merge_all_trusted_keyrings_into_pubring aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@" ;; adv*) + warn_on_script_usage setup_merged_keyring aptkey_echo "Executing: $GPG $*" aptkey_execute "$GPG" "$@" @@ -587,8 +641,9 @@ case "$command" in if [ -n "$GPGV" ] && command_available "$GPGV"; then true; elif command_available 'gpgv'; then GPGV='gpgv'; elif command_available 'gpgv2'; then GPGV='gpgv2'; + elif command_available 'gpgv1'; then GPGV='gpgv1'; else - echo >&2 'ERROR: gpgv or gpgv2 required for verification' + echo >&2 'ERROR: gpgv, gpgv2 or gpgv1 required for verification' exit 29 fi # for a forced keyid we need gpg --export, so full wrapping required