}
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"
get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS"
# strip leading 0x, if present:
- KEY="$(printf %s "$KEY" | sed s/^0x//)"
+ KEY="${KEY#0x}"
# check if the key is in this keyring
if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
local TRUSTEDPARTS="/etc/apt/trusted.gpg.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
# 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
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
}
prepare_gpg_home() {
+ # 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 <<EOF
+Warning: The $DPKG_MAINTSCRIPT_NAME maintainerscript of the package $DPKG_MAINTSCRIPT_PACKAGE
+Warning: seems to use apt-key (provided by apt) without depending on gnupg or gnupg2.
+Warning: This will BREAK in the future and should be fixed by the package maintainer(s).
+Note: Check first if apt-key functionality is needed at all - it probably isn't!
+EOF
+ fi
+ fi
eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
-
if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then
true
elif command_available 'gpg'; then
fi
}
+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
prepare_gpg_home
fi
case "$command" in
add)
+ warn_on_script_usage
requires_root
setup_merged_keyring
aptkey_execute "$GPG" --quiet --batch --import "$@"
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
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" "$@"