From 7e91d7e095b545297741efb65eec41f158a158a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 15 Jun 2016 15:41:34 +0200
Subject: [PATCH 14/23] sss_tools: create confdb if not exist

So tools (especially sssctl) may be run even when databases where
removed.
---
 Makefile.am                  |  5 ++-
 src/confdb/confdb_setup.c    | 81 +++++++++++++++++++++++++++++++++++++++++---
 src/confdb/confdb_setup.h    |  8 +++--
 src/monitor/monitor.c        | 48 ++------------------------
 src/tests/sysdb-tests.c      |  1 -
 src/tests/sysdb_ssh-tests.c  |  1 -
 src/tools/common/sss_tools.c | 10 +++---
 7 files changed, 95 insertions(+), 59 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 3b2ebbb374f72cb6aafb6039aa3832e3ec853b79..5a2672b1cadb84955932ae597c4be28937cc2424 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -492,6 +492,7 @@ SSSD_TOOLS_OBJ = \
     src/tools/selinux.c \
     src/tools/common/sss_tools.c \
     src/tools/common/sss_process.c \
+    src/confdb/confdb_setup.c \
     src/util/nscd.c \
     $(NULL)
 
@@ -1202,7 +1203,9 @@ sssd_SOURCES = \
     src/monitor/monitor_netlink.c \
     src/confdb/confdb_setup.c \
     src/monitor/monitor_iface_generated.c \
-    $(SSSD_TOOLS_OBJ) \
+    src/util/nscd.c \
+    src/tools/files.c \
+    src/tools/selinux.c \
     $(NULL)
 sssd_LDADD = \
     $(SSSD_LIBS) \
diff --git a/src/confdb/confdb_setup.c b/src/confdb/confdb_setup.c
index dfdcae56697123c414968cfaaabe3e1cd68ca21f..c1db9965ffe3b13fce88e09b01777f5d686bce06 100644
--- a/src/confdb/confdb_setup.c
+++ b/src/confdb/confdb_setup.c
@@ -30,8 +30,7 @@
 #include "util/sss_ini.h"
 #include "tools/tools_util.h"
 
-
-int confdb_test(struct confdb_ctx *cdb)
+static int confdb_test(struct confdb_ctx *cdb)
 {
     char **values;
     int ret;
@@ -106,7 +105,7 @@ done:
     return ret;
 }
 
-int confdb_create_base(struct confdb_ctx *cdb)
+static int confdb_create_base(struct confdb_ctx *cdb)
 {
     int ret;
     struct ldb_ldif *ldif;
@@ -127,7 +126,7 @@ int confdb_create_base(struct confdb_ctx *cdb)
     return EOK;
 }
 
-int confdb_init_db(const char *config_file, struct confdb_ctx *cdb)
+static int confdb_init_db(const char *config_file, struct confdb_ctx *cdb)
 {
     TALLOC_CTX *tmp_ctx;
     int ret;
@@ -354,3 +353,77 @@ done:
     talloc_zfree(tmp_ctx);
     return ret;
 }
+
+errno_t confdb_setup(TALLOC_CTX *mem_ctx,
+                     const char *cdb_file,
+                     const char *config_file,
+                     struct confdb_ctx **_cdb)
+{
+    TALLOC_CTX *tmp_ctx;
+    struct confdb_ctx *cdb;
+    errno_t ret;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
+        return ENOMEM;
+    }
+
+    ret = confdb_init(tmp_ctx, &cdb, cdb_file);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "The confdb initialization failed "
+              "[%d]: %s\n", ret, sss_strerror(ret));
+        goto done;
+    }
+
+    /* Initialize the CDB from the configuration file */
+    ret = confdb_test(cdb);
+    if (ret == ENOENT) {
+        /* First-time setup */
+
+        /* Purge any existing confdb in case an old
+         * misconfiguration gets in the way
+         */
+        talloc_zfree(cdb);
+        ret = unlink(cdb_file);
+        if (ret != EOK && errno != ENOENT) {
+            ret = errno;
+            DEBUG(SSSDBG_MINOR_FAILURE,
+                  "Purging existing confdb failed: %d [%s].\n",
+                  ret, sss_strerror(ret));
+            goto done;
+        }
+
+        ret = confdb_init(tmp_ctx, &cdb, cdb_file);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_FATAL_FAILURE, "The confdb initialization failed "
+                  "[%d]: %s\n", ret, sss_strerror(ret));
+        }
+
+        /* Load special entries */
+        ret = confdb_create_base(cdb);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_FATAL_FAILURE,
+                  "Unable to load special entries into confdb\n");
+            goto done;
+        }
+    } else if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "Fatal error initializing confdb\n");
+        goto done;

+    }
+
+    ret = confdb_init_db(config_file, cdb);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "ConfDB initialization has failed "
+              "[%d]: %s\n", ret, sss_strerror(ret));
+        goto done;
+    }
+
+    *_cdb = talloc_steal(mem_ctx, cdb);
+
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
diff --git a/src/confdb/confdb_setup.h b/src/confdb/confdb_setup.h
index 2b8802f6fd56ebf84379b46080e3563104598271..d995fc98cf3cf109fcbe08b44e25908b81ac5bd4 100644
--- a/src/confdb/confdb_setup.h
+++ b/src/confdb/confdb_setup.h
@@ -45,8 +45,10 @@
      "version: "CONFDB_VERSION"\n" \
      "\n"
 
-int confdb_create_base(struct confdb_ctx *cdb);
-int confdb_test(struct confdb_ctx *cdb);
-int confdb_init_db(const char *config_file, struct confdb_ctx *cdb);
+errno_t confdb_setup(TALLOC_CTX *mem_ctx,
+                     const char *cdb_file,
+                     const char *config_file,
+                     struct confdb_ctx **_cdb);
+
 
 #endif /* CONFDB_SETUP_H_ */
diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index 6b2cb55d68429d4a8776642e84a5dc1dcd7205f3..8aec39656046bb4a2e6be0bf01f3c66d82414c6d 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -1892,52 +1892,10 @@ errno_t load_configuration(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-    ret = confdb_init(ctx, &ctx->cdb, cdb_file);
+    ret = confdb_setup(ctx, cdb_file, config_file, &ctx->cdb);
     if (ret != EOK) {
-        DEBUG(SSSDBG_FATAL_FAILURE,"The confdb initialization failed\n");
-        goto done;
-    }
-
-    /* Initialize the CDB from the configuration file */
-    ret = confdb_test(ctx->cdb);
-    if (ret == ENOENT) {
-        /* First-time setup */
-
-        /* Purge any existing confdb in case an old
-         * misconfiguration gets in the way
-         */
-        talloc_zfree(ctx->cdb);
-        ret = unlink(cdb_file);
-        if (ret != EOK && errno != ENOENT) {
-            ret = errno;
-            DEBUG(SSSDBG_MINOR_FAILURE,
-                  "Purging existing confdb failed: %d [%s].\n",
-                  ret, sss_strerror(ret));
-            goto done;
-        }
-
-        ret = confdb_init(ctx, &ctx->cdb, cdb_file);
-        if (ret != EOK) {
-            DEBUG(SSSDBG_FATAL_FAILURE,"The confdb initialization failed\n");
-            goto done;
-        }
-
-        /* Load special entries */
-        ret = confdb_create_base(ctx->cdb);
-        if (ret != EOK) {
-            DEBUG(SSSDBG_FATAL_FAILURE,
-                  "Unable to load special entries into confdb\n");
-            goto done;
-        }
-    } else if (ret != EOK) {
-        DEBUG(SSSDBG_FATAL_FAILURE, "Fatal error initializing confdb\n");
-        goto done;
-    }
-
-    ret = confdb_init_db(config_file, ctx->cdb);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_FATAL_FAILURE, "ConfDB initialization has failed [%s]\n",
-              sss_strerror(ret));
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to setup ConfDB [%d]: %s\n",
+              ret, sss_strerror(ret));
         goto done;
     }
 
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 3c3e30a6458cda6624ad60e6f05bdca3fe6c1684..05017e4c0b5b5e98b810cdf3d3bd6b6cdbc3e364 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -28,7 +28,6 @@
 #include <sys/types.h>
 #include "util/util.h"
 #include "util/crypto/sss_crypto.h"
-#include "confdb/confdb_setup.h"
 #include "db/sysdb_private.h"
 #include "db/sysdb_services.h"
 #include "db/sysdb_autofs.h"
diff --git a/src/tests/sysdb_ssh-tests.c b/src/tests/sysdb_ssh-tests.c
index bb93672e04dfaab12c6ecbeab6a8909d2fdc588a..d23f4df310a2f0fa7be17fa05cc52f90cf89f5ae 100644
--- a/src/tests/sysdb_ssh-tests.c
+++ b/src/tests/sysdb_ssh-tests.c
@@ -29,7 +29,6 @@
 #include "tests/common.h"
 #include "util/util.h"
 #include "confdb/confdb.h"
-#include "confdb/confdb_setup.h"
 #include "db/sysdb.h"
 #include "db/sysdb_services.h"
 #include "db/sysdb_ssh.h"
diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c
index d0d28cf0efc01ef1d9b36f9d2e6ca7aeb07374cd..6080bb0ac3c6dff5cc6159139d5a8fd01d6ce133 100644
--- a/src/tools/common/sss_tools.c
+++ b/src/tools/common/sss_tools.c
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "util/util.h"
 #include "confdb/confdb.h"
+#include "confdb/confdb_setup.h"
 #include "db/sysdb.h"
 #include "tools/common/sss_tools.h"
 
@@ -102,14 +103,15 @@ static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
         return ENOMEM;
     }
 
-    ret = confdb_init(mem_ctx, &confdb, path);
+    ret = confdb_setup(mem_ctx, path, SSSD_CONFIG_FILE, &confdb);
+    talloc_zfree(path);
     if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "Could not initialize connection to the confdb\n");
-        talloc_free(path);
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to setup ConfDB [%d]: %s\n",
+              ret, sss_strerror(ret));
         return ret;
     }
 
+
     if (_confdb != NULL) {
         *_confdb = confdb;
     }
-- 
2.4.11

