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

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