X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/b53c9cea2902572822bbbece5bac236c1bbf846e..ae2a6be8a2155c136f9535abfbcc750c8c395cd2:/cmdline/apt-key.in diff --git a/cmdline/apt-key.in b/cmdline/apt-key.in index 881f8a990..7c12c72a3 100644 --- a/cmdline/apt-key.in +++ b/cmdline/apt-key.in @@ -2,18 +2,16 @@ set -e unset GREP_OPTIONS - -APT_DIR="/" -eval $(apt-config shell APT_DIR Dir) +export IFS="$(printf "\n\b")" 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 "$@"; } @@ -24,8 +22,28 @@ requires_root() { fi } +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" + IFS=: + for p in $PATH; do + if [ -x "${p}/${1}" ]; then + IFS="$OLDIFS" + return 0 + fi + done + IFS="$OLDIFS" + return 1 +} + +escape_shell() { + echo "$@" | sed -e "s#'#'\"'\"'#g" +} + get_fingerprints_of_keyring() { - $GPG_CMD --keyring "$1" --with-colons --fingerprint | while read publine; do + aptkey_execute "$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine; do # search for a public key if [ "${publine%%:*}" != 'pub' ]; then continue; fi # search for the associated fingerprint (should be the very next line) @@ -39,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" @@ -56,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=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5` - master_keys=`$GPG_CMD --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 @@ -72,16 +90,18 @@ add_keys_with_verify_against_master_keyring() { for add_key in $add_keys; do # export the add keyring one-by-one local TMP_KEYRING="${GPGHOMEDIR}/tmp-keyring.gpg" - $GPG_CMD --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key" - if ! $GPG_CMD --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then + aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key" + if ! aptkey_execute "$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then cat >&2 "${GPGHOMEDIR}/gpgoutput.log" false fi # check if signed with the master key and only add in this case ADDED=0 for master_key in $master_keys; do - if $GPG_CMD --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then - $GPG_CMD --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" | $GPG --batch --yes --import + if aptkey_execute "$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \ + | grep '^sig:!:' | cut -d: -f5 | grep -q "$master_key"; then + aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \ + | aptkey_execute "$GPG" --batch --yes --import ADDED=1 fi done @@ -96,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) @@ -109,31 +132,31 @@ net_update() { fi # in theory we would need to depend on wget for this, but this feature # isn't useable in debian anyway as we have no keyring uri nor a master key - if ! which wget >/dev/null 2>&1; then + if ! command_available 'wget'; then echo >&2 "ERROR: an installed wget is required for a network-based update" exit 1 fi - if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then - mkdir -p ${APT_DIR}/var/lib/apt/keyrings + if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then + mkdir -p "${APT_DIR}/var/lib/apt/keyrings" fi - keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI) + 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 + (cd "${APT_DIR}/var/lib/apt/keyrings"; wget --timeout=90 -q -N "$ARCHIVE_KEYRING_URI") + if [ ! -e "$keyring" ]; then return fi - new_mtime=$(stat -c %Y $keyring) + new_mtime=$(stat -c %Y "$keyring") if [ $new_mtime -ne $old_mtime ]; then aptkey_echo "Checking for new archive signing keys now" - add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING + add_keys_with_verify_against_master_keyring "$keyring" "$MASTER_KEYRING" fi } update() { - if [ ! -f $ARCHIVE_KEYRING ]; then + if [ ! -f "$ARCHIVE_KEYRING" ]; then echo >&2 "ERROR: Can't find the archive-keyring" echo >&2 "Is the &keyring-package; package installed?" exit 1 @@ -166,10 +189,13 @@ remove_key_from_keyring() { return fi - local GPG="$GPG_CMD --keyring $KEYRINGFILE" for KEY in "$@"; do local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst" get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS" + + # strip leading 0x, if present: + KEY="${KEY#0x}" + # check if the key is in this keyring if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then continue @@ -191,7 +217,7 @@ remove_key_from_keyring() { cp -a "$REALTARGET" "$KEYRINGFILE" fi # delete the key from the keyring - $GPG --batch --delete-keys --yes "$KEY" + aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY" if [ -n "$REALTARGET" ]; then # the real backup is the old link, not the copy we made mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~" @@ -211,14 +237,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 $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); 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 @@ -231,7 +254,7 @@ run_cmd_on_keyring() { local KEYRINGFILE="$1" shift # fingerprint and co will fail if key isn't in this keyring - $GPG_CMD --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true + aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true } import_keyring_into_keyring() { @@ -240,7 +263,7 @@ import_keyring_into_keyring() { shift 2 rm -f "${GPGHOMEDIR}/gpgoutput.log" # the idea is simple: We take keys from one keyring and copy it to another - # we do this with so many checks inbetween to ensure that WE control the + # we do this with so many checks in between to ensure that WE control the # creation, so we know that the (potentially) created $TO keyring is a # simple keyring rather than a keybox as gpg2 would create it which in turn # can't be read by gpgv. @@ -249,7 +272,7 @@ import_keyring_into_keyring() { if [ ! -s "$TO" ]; then if [ -s "$FROM" ]; then if [ -z "$2" ]; then - if ! $GPG_CMD --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then + if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then cat >&2 "${GPGHOMEDIR}/gpgoutput.log" false else @@ -264,7 +287,8 @@ import_keyring_into_keyring() { elif [ -s "$FROM" ]; then local EXPORTLIMIT="$1" if [ -n "$1$2" ]; then shift; fi - if ! $GPG_CMD --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} | $GPG_CMD --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then + if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \ + | aptkey_execute "$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then cat >&2 "${GPGHOMEDIR}/gpgoutput.log" false fi @@ -275,7 +299,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 @@ -286,8 +310,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 /dev/null "$TRUSTEDFILE" $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg') > "$PUBRING" 2>/dev/null || true + 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 @@ -336,7 +364,9 @@ setup_merged_keyring() { merge_all_trusted_keyrings_into_pubring FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg" TRUSTEDFILE="${FORCED_KEYRING}" - GPG="$GPG --keyring $TRUSTEDFILE" + echo "#!/bin/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 elif [ -z "$FORCED_KEYRING" ]; then @@ -346,10 +376,14 @@ setup_merged_keyring() { else touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg" fi - GPG="$GPG --keyring ${GPGHOMEDIR}/pubring.gpg" + echo "#!/bin/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" - GPG="$GPG --keyring $TRUSTEDFILE" + echo "#!/bin/sh +exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh" + GPG="${GPGHOMEDIR}/gpg.1.sh" fi } @@ -363,6 +397,8 @@ create_new_keyring() { fi } +aptkey_execute() { sh "$@"; } + usage() { echo "Usage: apt-key [--keyring file] [command] [arguments]" echo @@ -398,7 +434,7 @@ while [ -n "$1" ]; do ;; --readonly) merge_back_changes() { true; } - create_new_keyring() { if [ ! -r $FORCED_KEYRING ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; } + create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; } ;; --fakeroot) requires_root() { true; } @@ -408,11 +444,11 @@ while [ -n "$1" ]; do ;; --debug1) # some cmds like finger redirect stderr to /dev/null … - aptkey_execute() { echo 'EXEC:' "$@"; "$@"; } + aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; } ;; --debug2) # … other more complicated ones pipe gpg into gpg. - aptkey_execute() { echo >&2 'EXEC:' "$@"; "$@"; } + aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; } ;; --*) echo >&2 "Unknown option: $1" @@ -448,19 +484,32 @@ create_gpg_home() { fi fi GPGHOMEDIR="$(mktemp -d)" - CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';" + CURRENTTRAP="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';" trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM chmod 700 "$GPGHOMEDIR" } prepare_gpg_home() { - eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand) - - if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev/null 2>&1; then + # 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" ]; then + if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then + cat >&2 </dev/null 2>&1; then + elif command_available 'gpg'; then GPG_EXE="gpg" - elif which gpg2 >/dev/null 2>&1; then + elif command_available 'gpg2'; then GPG_EXE="gpg2" else echo >&2 "Error: gnupg or gnupg2 do not seem to be installed," @@ -469,38 +518,40 @@ prepare_gpg_home() { exit 255 fi - if type aptkey_execute >/dev/null 2>&1; then - GPG_EXE="aptkey_execute $GPG_EXE" - fi - GPG_CMD="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring" - 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 - GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR" # 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 - $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1 - # tell gpg that it shouldn't try to maintain a trustdb file - GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always" - GPG="$GPG_CMD" + touch "${GPGHOMEDIR}/empty.gpg" + if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \ + --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then + cat >&2 "${GPGHOMEDIR}/gpgoutput.log" + false + fi + # now tell gpg that it shouldn't try to maintain this trustdb file + echo "#!/bin/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 | $GPG --batch --import >/dev/null 2>&1 || true } if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then @@ -511,7 +562,7 @@ case "$command" in add) requires_root setup_merged_keyring - $GPG --quiet --batch --import "$@" + aptkey_execute "$GPG" --quiet --batch --import "$@" merge_back_changes aptkey_echo "OK" ;; @@ -540,20 +591,20 @@ case "$command" in ;; export|exportall) merge_all_trusted_keyrings_into_pubring - $GPG_CMD --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@" + aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@" ;; adv*) setup_merged_keyring aptkey_echo "Executing: $GPG $*" - $GPG "$@" + aptkey_execute "$GPG" "$@" merge_back_changes ;; verify) GPGV='' eval $(apt-config shell GPGV Apt::Key::gpgvcommand) - if [ -n "$GPGV" ] && which "$GPGV" >/dev/null 2>&1; then true; - elif which gpgv >/dev/null 2>&1; then GPGV='gpgv'; - elif which gpgv2 >/dev/null 2>&1; then GPGV='gpgv2'; + if [ -n "$GPGV" ] && command_available "$GPGV"; then true; + elif command_available 'gpgv'; then GPGV='gpgv'; + elif command_available 'gpgv2'; then GPGV='gpgv2'; else echo >&2 'ERROR: gpgv or gpgv2 required for verification' exit 29 @@ -566,9 +617,9 @@ case "$command" in fi setup_merged_keyring if [ -n "$FORCED_KEYRING" ]; then - $GPGV --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@" + "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@" else - $GPGV --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@" + "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@" fi ;; help)