From: David Kalnischkies Date: Fri, 1 Jul 2016 20:00:52 +0000 (+0200) Subject: warn if apt-key is used in scripts/its output parsed X-Git-Tag: 1.3_pre1~17 X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/08fcf9628806af202e555bd02b3611e4e9a3d757 warn if apt-key is used in scripts/its output parsed apt-key needs gnupg for most of its operations, but depending on it isn't very efficient as apt-key is hardly used by users – and scripts shouldn't use it to begin with as it is just a silly wrapper. To draw more attention on the fact that e.g. 'apt-key add' should not be used in favor of "just" dropping a keyring file into the trusted.gpg.d directory this commit implements the display of warnings. --- diff --git a/cmdline/apt-key.in b/cmdline/apt-key.in index 5bd8d1249..49056f2a6 100644 --- a/cmdline/apt-key.in +++ b/cmdline/apt-key.in @@ -494,7 +494,7 @@ prepare_gpg_home() { # 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 [ -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 "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 prepare_gpg_home fi case "$command" in add) + warn_on_script_usage requires_root setup_merged_keyring aptkey_execute "$GPG" --quiet --batch --import "$@" @@ -567,11 +580,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 @@ -584,13 +599,16 @@ case "$command" in merge_back_changes ;; 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" "$@" diff --git a/doc/apt-key.8.xml b/doc/apt-key.8.xml index a847060c0..e0d9f5b95 100644 --- a/doc/apt-key.8.xml +++ b/doc/apt-key.8.xml @@ -13,7 +13,7 @@ &apt-email; &apt-product; - 2015-10-15T00:00:00Z + 2016-07-01T00:00:00Z @@ -36,6 +36,15 @@ by apt to authenticate packages. Packages which have been authenticated using these keys will be considered trusted. + + Note that if usage of apt-key is desired the additional + installation of the GNU Privacy Guard suite (packaged in + gnupg) is required. For this reason alone the programatic + usage (especially in package maintainerscripts!) is strongly discouraged. + Further more the output format of all commands is undefined and can and does + change whenever the underlying commands change. apt-key will + try to detect such usage and generates warnings on stderr in these cases. + Commands @@ -53,6 +62,12 @@ verified to belong to the owner of the repositories they claim to be for otherwise the &apt-secure; infrastructure is completely undermined. + + Instead of using this command a keyring can be placed directly in the + /etc/apt/trusted.gpg.d/ directory with a descriptive name + (same rules for filename apply as for &apt-conf; files) and "gpg" + as file extension. + diff --git a/test/integration/framework b/test/integration/framework index bebcbf342..8ca878148 100644 --- a/test/integration/framework +++ b/test/integration/framework @@ -403,6 +403,7 @@ EOF fi echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary + export APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=no echo 'Acquire::Connect::AddrConfig "false";' > rootdir/etc/apt/apt.conf.d/connect-addrconfig configcompression '.' 'gz' #'bz2' 'lzma' 'xz' diff --git a/test/integration/test-apt-key-used-in-maintainerscript b/test/integration/test-apt-key-used-in-maintainerscript index e5e96963f..9faae19de 100755 --- a/test/integration/test-apt-key-used-in-maintainerscript +++ b/test/integration/test-apt-key-used-in-maintainerscript @@ -5,6 +5,7 @@ TESTDIR="$(readlink -f "$(dirname "$0")")" . "$TESTDIR/framework" setupenvironment +unset APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE configarchitecture 'native' configdpkgnoopchroot @@ -14,7 +15,7 @@ buildingpkg() { setupsimplenativepackage "$PKG" 'native' '1' 'unstable' "$@" BUILDDIR="incoming/${PKG}-1" echo '#!/bin/sh -apt-key list' > "${BUILDDIR}/debian/postinst" +apt-key list >/dev/null' > "${BUILDDIR}/debian/postinst" buildpackage "$BUILDDIR" 'unstable' 'main' 'native' rm -rf "$BUILDDIR" } @@ -27,8 +28,15 @@ insertinstalledpackage 'unrelated' 'native' '1' insertinstalledpackage 'gnupg' 'native' '1' testsuccess apt install aptkeyuser-depends -y -testfailure grep '^Warning: This will BREAK' rootdir/tmp/testsuccess.output +cp rootdir/tmp/testsuccess.output apt.output +testfailure grep '^Warning: This will BREAK' apt.output +testsuccess grep '^Warning: apt-key' apt.output testsuccess apt install aptkeyuser-nodepends -y cp rootdir/tmp/testsuccess.output apt.output testsuccess grep '^Warning: This will BREAK' apt.output +testsuccess grep '^Warning: apt-key' apt.output + +testsuccess aptkey list +cp rootdir/tmp/testsuccess.output aptkey.list +testsuccess grep '^Warning: apt-key' aptkey.list