apt-key del: Ignore case when checking if a keyid exists in a keyring.
[ntk/apt.git] / cmdline / apt-key.in
index 779872b..751cc5c 100644 (file)
@@ -5,26 +5,30 @@ unset GREP_OPTIONS
 
 GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring"
 
-# gpg needs a trustdb to function, but it can't be invalid (not even empty)
-# so we create a temporary directory to store our fresh readable trustdb in
-TRUSTDBDIR="$(mktemp -d)"
-CURRENTTRAP="${CURRENTTRAP} rm -rf '${TRUSTDBDIR}';"
+# 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
+GPGHOMEDIR="$(mktemp -d)"
+CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
 trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
-chmod 700 "$TRUSTDBDIR"
-# We also don't use a secret keyring, of course, but gpg panics and
+chmod 700 "$GPGHOMEDIR"
+# 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="${TRUSTDBDIR}/secring.gpg"
+SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
 touch $SECRETKEYRING
-GPG_CMD="$GPG_CMD --secret-keyring $SECRETKEYRING"
-GPG_CMD="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg"
-
-# now create the trustdb with an (empty) dummy keyring
-$GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING
-# and make sure that gpg isn't trying to update the file
+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"
 
+APT_DIR="/"
+eval $(apt-config shell APT_DIR Dir)
+
 MASTER_KEYRING='&keyring-master-filename;'
 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
 ARCHIVE_KEYRING='&keyring-filename;'
@@ -33,7 +37,7 @@ REMOVED_KEYS='&keyring-removed-filename;'
 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
 ARCHIVE_KEYRING_URI='&keyring-uri;'
 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
-TMP_KEYRING=/var/lib/apt/keyrings/maybe-import-keyring.gpg
+TMP_KEYRING=${APT_DIR}/var/lib/apt/keyrings/maybe-import-keyring.gpg
 
 requires_root() {
        if [ "$(id -u)" -ne 0 ]; then
@@ -107,7 +111,11 @@ add_keys_with_verify_against_master_keyring() {
 # (otherwise it does not make sense from a security POV)
 net_update() {
     # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
-    exit 1
+    APT_KEY_NET_UPDATE_ENABLED=""
+    eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
+    if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
+        exit 1
+    fi
 
     if [ -z "$ARCHIVE_KEYRING_URI" ]; then
        echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
@@ -120,15 +128,15 @@ net_update() {
        echo >&2 "ERROR: an installed wget is required for a network-based update"
        exit 1
     fi
-    if [ ! -d /var/lib/apt/keyrings ]; then
-       mkdir -p /var/lib/apt/keyrings
+    if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
+       mkdir -p ${APT_DIR}/var/lib/apt/keyrings
     fi
-    keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
+    keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
     old_mtime=0
     if [ -e $keyring ]; then
        old_mtime=$(stat -c %Y $keyring)
     fi
-    (cd  /var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
+    (cd  ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
     if [ ! -e $keyring ]; then
        return
     fi
@@ -172,14 +180,14 @@ update() {
 remove_key_from_keyring() {
     local GPG="$GPG_CMD --keyring $1"
     # check if the key is in this keyring: the key id is in the 5 column at the end
-    if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+$2:"; then
-       return
+    if ! $GPG --with-colons --list-keys 2>&1 | grep -qi "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]*$2:"; then
+       return 1
     fi
     if [ ! -w "$1" ]; then
        echo >&2 "Key ${2} is in keyring ${1}, but can't be removed as it is read only."
        return
     fi
-    # check if it is the only key in the keyring and if so remove the keyring alltogether
+    # check if it is the only key in the keyring and if so remove the keyring altogether
     if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
        mv -f "$1" "${1}~" # behave like gpg
        return
@@ -203,23 +211,35 @@ remove_key_from_keyring() {
 remove_key() {
     requires_root
 
+    local NOTFOUND=1
+    local RET=0
     # if a --keyring was given, just remove from there
     if [ -n "$FORCED_KEYRING" ]; then
-       remove_key_from_keyring "$FORCED_KEYRING" "$1"
+       remove_key_from_keyring "$FORCED_KEYRING" "$1" || RET=$?
+       NOTFOUND=$RET
     else
        # otherwise all known keyrings are up for inspection
        local TRUSTEDFILE="/etc/apt/trusted.gpg"
        eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
        eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
-       remove_key_from_keyring "$TRUSTEDFILE" "$1"
+       remove_key_from_keyring "$TRUSTEDFILE" "$1" || RET=$?
+       NOTFOUND=$RET
        TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
        eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
        if [ -d "$TRUSTEDPARTS" ]; then
            for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
-               remove_key_from_keyring "$trusted" "$1"
+               RET=0
+               remove_key_from_keyring "$trusted" "$1" || RET=$?
+               if [ $RET -eq 0 ]; then
+                   NOTFOUND=0
+               fi
            done
        fi
     fi
+    if [ $NOTFOUND -ne 0 ]; then
+       echo >&2 "ERROR: The specified keyid '$1' was not found"
+       return 1
+    fi
     echo "OK"
 }