From 471b7be4bd5cc7f245f9aa00c7784a7056e439b7 Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Thu, 27 Aug 2020 10:43:37 +0300
Subject: [PATCH] Warn and fall back to dummy database on unknown database
 backend config

The rpmdb is our most precious piece of data, don't make assumptions on
invalid configuration. Together with our crazy create-db-on-read behavior,
total database loss is just one 'rpmdb --rebuilddb' away in some scenarios
with the former behavior: access an sqlite/ndb database with older
version not supporting those, silently fallback to creating empty bdb,
and if db is now rebuilt, poof the data is gone.

Detect and warn on unknown/invalid %_db_backend configuration and fall
back to using dummy backend where no damage can occur. Doesn't help with
the old versions out there, but lets at least be saner going forward.
---
 lib/backend/dbi.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/backend/dbi.c b/lib/backend/dbi.c
index 94823b14c..8fbe5f374 100644
--- a/lib/backend/dbi.c
+++ b/lib/backend/dbi.c
@@ -77,6 +77,11 @@ dbDetectBackend(rpmdb rdb)
 	}
     }
 
+    if (!cfg) {
+	rpmlog(RPMLOG_WARNING, _("invalid %%_db_backend: %s\n"), db_backend);
+	goto exit;
+    }
+
     /* If configured database doesn't exist, try autodetection */
     if (!tryBackend(dbhome, cfg)) {
 	for (ops = backends; ops && *ops; ops++) {
@@ -106,6 +111,7 @@ dbDetectBackend(rpmdb rdb)
     if (rdb->db_ops == NULL && cfg)
 	rdb->db_ops = cfg;
 
+exit:
     /* If all else fails... */
     if (rdb->db_ops == NULL) {
 	rdb->db_ops = &dummydb_dbops;
-- 
2.27.0

