]> the.earth.li Git - onak.git/commitdiff
Handle failed database initialisation more gracefully
authorJonathan McDowell <noodles@earth.li>
Mon, 25 Sep 2023 17:51:41 +0000 (18:51 +0100)
committerJonathan McDowell <noodles@earth.li>
Mon, 25 Sep 2023 17:52:43 +0000 (18:52 +0100)
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
cgi/gpgwww.c
cgi/hashquery.c
cgi/lookup.c
onak.c

index 5775e81ce3f209702ac25a5620920e612360a212..90824ccce7b5715b9c350f693e42ab6a05f06029 100644 (file)
--- 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();
        }
index 0fc58fb3762f1b40b539e8bf80cd1352a8f7a149..c814e8f0d0b920648854567aacfc6171a12a38cc 100644 (file)
@@ -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();
 
index 7b8c6c4fa57360cee56e2d5c8e2d6a4d59b7cb1d..d750608b812342cc9160c70bc574ab73b42e0294 100644 (file)
@@ -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.");
index 632a0a8cd58dd9974c3a47e510fc4c2615d5a849..2006e475cea9d70dba5c90aef601a93fc4a0f51e 100644 (file)
@@ -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 d21ce7afb683c99cb675aa5c453d1f1006bb55de..a588b51d2fc667c668b4aa3106dabc9a2d7794be 100644 (file)
--- 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);