Blame SOURCES/0038-Ticket-49298-Correct-error-codes-with-config-restore.patch

96373c
From e3dea0043973faf42f7756d840bc55aa8f143eb1 Mon Sep 17 00:00:00 2001
96373c
From: William Brown <firstyear@redhat.com>
96373c
Date: Wed, 15 Nov 2017 13:44:02 +1000
96373c
Subject: [PATCH] Ticket 49298 - Correct error codes with config restore.
96373c
96373c
Bug Description:  The piece of code uses 0 as an error - not 1,
96373c
and in some cases did not even check the codes or use the
96373c
correct logic.
96373c
96373c
Fix Description:  Cleanup dse_check_file to better check the
96373c
content of files and communicate issues to the admin. Correct
96373c
slapd_bootstrap_config to correctly handle the cases of removal
96373c
and restore.
96373c
96373c
https://pagure.io/389-ds-base/issue/49298
96373c
96373c
Author: wibrown
96373c
96373c
Review by: mreynoolds & spichugi
96373c
96373c
Signed-off-by: Mark Reynolds <mreynolds@redhat.com>
96373c
(cherry picked from commit 75e55e26579955adf058e8adcba9a28779583b7b)
96373c
---
96373c
 .../suites/config/removed_config_49298_test.py     | 81 ++++++++++++++++++++++
96373c
 ldap/servers/slapd/config.c                        | 15 ++--
96373c
 ldap/servers/slapd/dse.c                           | 42 ++++++++---
96373c
 3 files changed, 119 insertions(+), 19 deletions(-)
96373c
 create mode 100644 dirsrvtests/tests/suites/config/removed_config_49298_test.py
96373c
96373c
diff --git a/dirsrvtests/tests/suites/config/removed_config_49298_test.py b/dirsrvtests/tests/suites/config/removed_config_49298_test.py
96373c
new file mode 100644
96373c
index 000000000..e65236924
96373c
--- /dev/null
96373c
+++ b/dirsrvtests/tests/suites/config/removed_config_49298_test.py
96373c
@@ -0,0 +1,81 @@
96373c
+# --- BEGIN COPYRIGHT BLOCK ---
96373c
+# Copyright (C) 2017 Red Hat, Inc.
96373c
+# All rights reserved.
96373c
+#
96373c
+# License: GPL (version 3 or any later version).
96373c
+# See LICENSE for details.
96373c
+# --- END COPYRIGHT BLOCK ---
96373c
+#
96373c
+import pytest
96373c
+import os
96373c
+import logging
96373c
+import subprocess
96373c
+
96373c
+from lib389.topologies import topology_st as topo
96373c
+
96373c
+DEBUGGING = os.getenv("DEBUGGING", default=False)
96373c
+if DEBUGGING:
96373c
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
96373c
+else:
96373c
+    logging.getLogger(__name__).setLevel(logging.INFO)
96373c
+log = logging.getLogger(__name__)
96373c
+
96373c
+def test_restore_config(topo):
96373c
+    """
96373c
+    Check that if a dse.ldif and backup are removed, that the server still starts.
96373c
+
96373c
+    :id: e1c38fa7-30bc-46f2-a934-f8336f387581
96373c
+    :setup: Standalone instance
96373c
+    :steps:
96373c
+        1. Stop the instance
96373c
+        2. Delete 'dse.ldif'
96373c
+        3. Start the instance
96373c
+    :expectedresults:
96373c
+        1. Steps 1 and 2 succeed.
96373c
+        2. Server will succeed to start with restored cfg.
96373c
+    """
96373c
+    topo.standalone.stop()
96373c
+
96373c
+    dse_path = topo.standalone.get_config_dir()
96373c
+
96373c
+    log.info(dse_path)
96373c
+
96373c
+    for i in ('dse.ldif', 'dse.ldif.startOK'):
96373c
+        p = os.path.join(dse_path, i)
96373c
+        os.remove(p)
96373c
+
96373c
+    # This will pass.
96373c
+    topo.standalone.start()
96373c
+
96373c
+def test_removed_config(topo):
96373c
+    """
96373c
+    Check that if a dse.ldif and backup are removed, that the server
96373c
+    exits better than "segfault".
96373c
+
96373c
+    :id: b45272d1-c197-473e-872f-07257fcb2ec0
96373c
+    :setup: Standalone instance
96373c
+    :steps:
96373c
+        1. Stop the instance
96373c
+        2. Delete 'dse.ldif', 'dse.ldif.bak', 'dse.ldif.startOK'
96373c
+        3. Start the instance
96373c
+    :expectedresults:
96373c
+        1. Steps 1 and 2 succeed.
96373c
+        2. Server will fail to start, but will not crash.
96373c
+    """
96373c
+    topo.standalone.stop()
96373c
+
96373c
+    dse_path = topo.standalone.get_config_dir()
96373c
+
96373c
+    log.info(dse_path)
96373c
+
96373c
+    for i in ('dse.ldif', 'dse.ldif.bak', 'dse.ldif.startOK'):
96373c
+        p = os.path.join(dse_path, i)
96373c
+        os.remove(p)
96373c
+
96373c
+    # We actually can't check the log output, because it can't read dse.ldif,
96373c
+    # don't know where to write it yet! All we want is the server fail to
96373c
+    # start here, rather than infinite run + segfault.
96373c
+    with pytest.raises(subprocess.CalledProcessError):
96373c
+        topo.standalone.start()
96373c
+
96373c
+
96373c
diff --git a/ldap/servers/slapd/config.c b/ldap/servers/slapd/config.c
96373c
index afe07df84..c8d57e747 100644
96373c
--- a/ldap/servers/slapd/config.c
96373c
+++ b/ldap/servers/slapd/config.c
96373c
@@ -121,14 +121,13 @@ slapd_bootstrap_config(const char *configdir)
96373c
                       "Passed null config directory\n");
96373c
         return rc; /* Fail */
96373c
     }
96373c
-    PR_snprintf(configfile, sizeof(configfile), "%s/%s", configdir,
96373c
-                CONFIG_FILENAME);
96373c
-    PR_snprintf(tmpfile, sizeof(tmpfile), "%s/%s.tmp", configdir,
96373c
-                CONFIG_FILENAME);
96373c
-    if ((rc = dse_check_file(configfile, tmpfile)) == 0) {
96373c
-        PR_snprintf(tmpfile, sizeof(tmpfile), "%s/%s.bak", configdir,
96373c
-                    CONFIG_FILENAME);
96373c
-        rc = dse_check_file(configfile, tmpfile);
96373c
+    PR_snprintf(configfile, sizeof(configfile), "%s/%s", configdir, CONFIG_FILENAME);
96373c
+    PR_snprintf(tmpfile, sizeof(tmpfile), "%s/%s.bak", configdir, CONFIG_FILENAME);
96373c
+    rc = dse_check_file(configfile, tmpfile);
96373c
+    if (rc == 0) {
96373c
+        /* EVERYTHING IS GOING WRONG, ARRGHHHHHH */
96373c
+        slapi_log_err(SLAPI_LOG_ERR, "slapd_bootstrap_config", "No valid configurations can be accessed! You must restore %s from backup!\n", configfile);
96373c
+        return 0;
96373c
     }
96373c
 
96373c
     if ((rc = PR_GetFileInfo64(configfile, &prfinfo)) != PR_SUCCESS) {
96373c
diff --git a/ldap/servers/slapd/dse.c b/ldap/servers/slapd/dse.c
96373c
index 420248c24..653009f53 100644
96373c
--- a/ldap/servers/slapd/dse.c
96373c
+++ b/ldap/servers/slapd/dse.c
96373c
@@ -609,29 +609,49 @@ dse_check_file(char *filename, char *backupname)
96373c
 
96373c
     if (PR_GetFileInfo64(filename, &prfinfo) == PR_SUCCESS) {
96373c
         if (prfinfo.size > 0) {
96373c
-            return (1);
96373c
+            /* File exists and has content. */
96373c
+            return 1;
96373c
         } else {
96373c
+            slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
96373c
+                          "The config %s has zero length. Attempting restore ... \n", filename, rc);
96373c
             rc = PR_Delete(filename);
96373c
         }
96373c
+    } else {
96373c
+        slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
96373c
+                      "The config %s can not be accessed. Attempting restore ... (reason: %d)\n", filename, rc);
96373c
     }
96373c
 
96373c
     if (backupname) {
96373c
+
96373c
+        if (PR_GetFileInfo64(backupname, &prfinfo) != PR_SUCCESS) {
96373c
+            slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
96373c
+                          "The backup %s can not be accessed. Check it exists and permissions.\n", backupname);
96373c
+            return 0;
96373c
+        }
96373c
+
96373c
+        if (prfinfo.size <= 0) {
96373c
+            slapi_log_err(SLAPI_LOG_ERR, "dse_check_file",
96373c
+                      "The backup file %s has zero length, refusing to restore it.\n", backupname);
96373c
+            return 0;
96373c
+        }
96373c
+
96373c
         rc = PR_Rename(backupname, filename);
96373c
-    } else {
96373c
-        return (0);
96373c
-    }
96373c
+        if (rc != PR_SUCCESS) {
96373c
+            slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
96373c
+                      "The configuration file %s was NOT able to be restored from %s, error %d\n", filename, backupname, rc);
96373c
+            return 0;
96373c
+        }
96373c
 
96373c
-    if (PR_GetFileInfo64(filename, &prfinfo) == PR_SUCCESS && prfinfo.size > 0) {
96373c
         slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
96373c
-                      "The configuration file %s was restored from backup %s\n", filename, backupname);
96373c
-        return (1);
96373c
+                  "The configuration file %s was restored from backup %s\n", filename, backupname);
96373c
+        return 1;
96373c
+
96373c
     } else {
96373c
-        slapi_log_err(SLAPI_LOG_ERR, "dse_check_file",
96373c
-                      "The configuration file %s was not restored from backup %s, error %d\n",
96373c
-                      filename, backupname, rc);
96373c
-        return (0);
96373c
+        slapi_log_err(SLAPI_LOG_INFO, "dse_check_file", "No backup filename provided.\n");
96373c
+        return 0;
96373c
     }
96373c
 }
96373c
+
96373c
 static int
96373c
 dse_read_one_file(struct dse *pdse, const char *filename, Slapi_PBlock *pb, int primary_file)
96373c
 {
96373c
-- 
96373c
2.13.6
96373c