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

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