From 23538b7a1fe44e6e04e15d58b6fe809a6ad2ca31 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Mon, 25 Sep 2023 18:51:41 +0100 Subject: [PATCH] Handle failed database initialisation more gracefully Actually check the result from dbinit and exit rather than continuing if we failed to initialise it. In particular we hit this path with a missing config file. --- cgi/add.c | 8 +++++++- cgi/gpgwww.c | 6 ++++++ cgi/hashquery.c | 4 ++++ cgi/lookup.c | 6 ++++++ onak.c | 25 +++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cgi/add.c b/cgi/add.c index 5775e81..90824cc 100644 --- a/cgi/add.c +++ b/cgi/add.c @@ -87,7 +87,12 @@ int main(int argc, char *argv[]) } catchsignals(); dbctx = config.dbinit(config.backend, false); - + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + goto err; + } + count = cleankeys(dbctx, &keys, config.clean_policies); logthing(LOGTHING_INFO, "%d keys cleaned.", count); @@ -109,6 +114,7 @@ int main(int argc, char *argv[]) puts("No OpenPGP packets found in input."); end_html(); } +err: cleanuplogthing(); cleanupconfig(); } diff --git a/cgi/gpgwww.c b/cgi/gpgwww.c index 0fc58fb..c814e8f 100644 --- a/cgi/gpgwww.c +++ b/cgi/gpgwww.c @@ -180,6 +180,10 @@ int main(int argc, char *argv[]) initlogthing("gpgwww", config.logfile); catchsignals(); dbctx = config.dbinit(config.backend, true); + if (dbctx == NULL) { + puts("Failed to open key database."); + goto err; + } inithash(); logthing(LOGTHING_NOTICE, "Looking for path from 0x%016" PRIX64 " to 0x%016" @@ -193,6 +197,8 @@ int main(int argc, char *argv[]) } destroyhash(); dbctx->cleanupdb(dbctx); + +err: cleanuplogthing(); cleanupconfig(); diff --git a/cgi/hashquery.c b/cgi/hashquery.c index 7b8c6c4..d750608 100644 --- a/cgi/hashquery.c +++ b/cgi/hashquery.c @@ -94,6 +94,10 @@ int main(__unused int argc, __unused char *argv[]) catchsignals(); dbctx = config.dbinit(config.backend, false); + if (dbctx == NULL) { + doerror("Failed to open key database."); + } + if (dbctx->fetch_key_skshash == NULL) { dbctx->cleanupdb(dbctx); doerror("Can't fetch by skshash with this backend."); diff --git a/cgi/lookup.c b/cgi/lookup.c index 632a0a8..2006e47 100644 --- a/cgi/lookup.c +++ b/cgi/lookup.c @@ -228,6 +228,11 @@ int main(int argc, char *argv[]) initlogthing("lookup", config.logfile); catchsignals(); dbctx = config.dbinit(config.backend, false); + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + goto err; + } switch (op) { case OP_GET: case OP_HGET: @@ -305,6 +310,7 @@ int main(int argc, char *argv[]) puts("Unknown operation!"); } dbctx->cleanupdb(dbctx); +err: cleanuplogthing(); cleanupconfig(); } diff --git a/onak.c b/onak.c index d21ce7a..a588b51 100644 --- a/onak.c +++ b/onak.c @@ -218,6 +218,12 @@ int main(int argc, char *argv[]) usage(); } else if (!strcmp("dump", argv[optind])) { dbctx = config.dbinit(config.backend, true); + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + rc = EXIT_FAILURE; + goto err; + } dumpstate.count = dumpstate.filenum = 0; dumpstate.maxcount = 100000; dumpstate.fd = -1; @@ -245,6 +251,12 @@ int main(int argc, char *argv[]) result); dbctx = config.dbinit(config.backend, false); + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + rc = EXIT_FAILURE; + goto err; + } result = cleankeys(dbctx, &keys, config.clean_policies); logthing(LOGTHING_INFO, "%d keys cleaned.", @@ -287,6 +299,12 @@ int main(int argc, char *argv[]) } } else if (!strcmp("clean", argv[optind])) { dbctx = config.dbinit(config.backend, true); + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + rc = EXIT_FAILURE; + goto err; + } if (binary) { result = read_openpgp_stream(stdin_getchar, NULL, &packets, 0); @@ -373,6 +391,12 @@ int main(int argc, char *argv[]) } } dbctx = config.dbinit(config.backend, false); + if (dbctx == NULL) { + logthing(LOGTHING_ERROR, + "Failed to open key database."); + rc = EXIT_FAILURE; + goto err; + } if (!strcmp("index", argv[optind])) { find_keys(dbctx, search, keyid, &fingerprint, ishex, isfp, dispfp, skshash, @@ -487,6 +511,7 @@ int main(int argc, char *argv[]) usage(); } +err: cleanuplogthing(); cleanupconfig(); free(configfile); -- 2.39.5