From d34e814e67bcedeb5491df51fc9d291a223a27cd Mon Sep 17 00:00:00 2001
From: Petr Cech <pcech@redhat.com>
Date: Tue, 16 Aug 2016 14:59:06 +0200
Subject: [PATCH 3/3] WIP: Adding sss_ldb_add()

This patch will be split into:
a) adding support for printing ldif
b) replace of ldb_*() with sss_ldb_*()

Resolves:
https://fedorahosted.org/sssd/ticket/3060
---
 Makefile.am                |   2 +
 src/db/sysdb_ldb_wrapper.c | 105 +++++++++++++++++++++++++++++++++++++++++++++
 src/db/sysdb_ldb_wrapper.h |  22 ++++++++++
 src/db/sysdb_ops.c         |   3 +-
 src/util/debug.h           |   1 +
 5 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 src/db/sysdb_ldb_wrapper.c
 create mode 100644 src/db/sysdb_ldb_wrapper.h

diff --git a/Makefile.am b/Makefile.am
index 5d1d671096f986d9387e6199112c017e9bf30e1b..42ad0c67c2142f10f290a4a6b77938155b5120bd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -641,6 +641,7 @@ dist_noinst_HEADERS = \
     src/db/sysdb_private.h \
     src/db/sysdb_services.h \
     src/db/sysdb_ssh.h \
+    src/db/sysdb_ldb_wrapper.h \
     src/confdb/confdb.h \
     src/confdb/confdb_private.h \
     src/confdb/confdb_setup.h \
@@ -887,6 +888,7 @@ pkglib_LTLIBRARIES += libsss_util.la
 libsss_util_la_SOURCES = \
     src/confdb/confdb.c \
     src/db/sysdb.c \
+    src/db/sysdb_ldb_wrapper.c \
     src/db/sysdb_ops.c \
     src/db/sysdb_search.c \
     src/db/sysdb_selinux.c \
diff --git a/src/db/sysdb_ldb_wrapper.c b/src/db/sysdb_ldb_wrapper.c
new file mode 100644
index 0000000000000000000000000000000000000000..8d63e1dece1bb07313c1c8300027a9991e84e2cc
--- /dev/null
+++ b/src/db/sysdb_ldb_wrapper.c
@@ -0,0 +1,105 @@
+/*
+   SSSD
+
+   System Database -- ldb wrappers
+
+   Copyright (C) Petr Cech <pcech@redhat.com>	2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <ldb.h>
+#include "util/util.h"
+
+/* version A */
+static void sss_ldb_print(struct ldb_context *ldb,
+                          const struct ldb_message *message,
+                          const char *method_name)
+{
+    DEBUG(SSSDBG_LDB, "Method [%s] was called on entry [%s]:\n",
+                      method_name, ldb_dn_get_linearized(message->dn));
+
+    for (int i = 0; i < message->num_elements; i++) {
+        DEBUG(SSSDBG_LDB, "  element [%s] with flag [%d] contains:\n",
+                          message->elements[i].name,
+                          message->elements[i].flags);
+
+        for (int j=0; j < message->elements[i].num_values; j++) {
+            DEBUG(SSSDBG_LDB, "    value: [%s]\n",
+                  ldb_binary_encode(ldb, message->elements[i].values[j]));
+        }
+    }
+}
+
+/* version B */
+
+struct sss_ldb_print_ctx {
+    char *ldif;
+};
+
+static int my_vprintf_fn(void *private_data, const char *fmt, ...)
+{
+    struct sss_ldb_print_ctx *ldb_print_ctx;
+    va_list ap;
+
+    ldb_print_ctx = talloc_get_type(private_data, struct sss_ldb_print_ctx);
+
+    if (ldb_print_ctx == NULL) {
+        return -1;
+    }
+
+    if (fmt != NULL) {
+        va_start(ap, fmt);
+        ldb_print_ctx->ldif = talloc_vasprintf_append_buffer(ldb_print_ctx->ldif, fmt, ap);
+        va_end(ap);
+        if (ldb_print_ctx->ldif == NULL) {
+            return -2;
+        }
+    }
+
+    /* Note that the function should return the number of
+       bytes written, or a negative error code */
+    return 1;
+}
+
+int sss_ldb_add(struct ldb_context *ldb, const struct ldb_message *message)
+{
+    int ret;
+    struct ldb_ldif ldif;
+    struct sss_ldb_print_ctx *ldb_print_ctx;
+
+    /* version A */
+    DEBUG(SSSDBG_LDB, ">>> --- version A ---\n");
+    sss_ldb_print(ldb, message, "ldb_add");
+
+
+    /* version B */
+    DEBUG(SSSDBG_LDB, ">>> --- version B ---\n");
+
+    ldb_print_ctx = talloc_zero(ldb, struct sss_ldb_print_ctx);
+    if (ldb_print_ctx == NULL) {
+        return ENOMEM;
+    }
+    ldb_print_ctx->ldif = NULL;
+
+    ldif.changetype = LDB_CHANGETYPE_ADD;
+    ldif.msg = discard_const_p(struct ldb_message, message);
+
+    ret = ldb_ldif_write(ldb, my_vprintf_fn, ldb_print_ctx, &ldif);
+    DEBUG(SSSDBG_LDB, "LDIF [%s]\n", ldb_print_ctx->ldif);
+    talloc_free(ldb_print_ctx);
+
+
+    return ldb_add(ldb, message);
+}
\ No newline at end of file
diff --git a/src/db/sysdb_ldb_wrapper.h b/src/db/sysdb_ldb_wrapper.h
new file mode 100644
index 0000000000000000000000000000000000000000..6a8a60468f51318c528e83648bae6e20825993d9
--- /dev/null
+++ b/src/db/sysdb_ldb_wrapper.h
@@ -0,0 +1,22 @@
+/*
+   SSSD
+
+   System Database -- ldb wrappers
+
+   Copyright (C) Petr Cech <pcech@redhat.com>	2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+int sss_ldb_add(struct ldb_context *ldb, const struct ldb_message *message);
\ No newline at end of file
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index a81840b2515d09f91d1dfa783bcf08f0fad112b4..852e1eea41db507fe0fd8234f69b702a4a7a147c 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -23,6 +23,7 @@
 #include "db/sysdb_private.h"
 #include "db/sysdb_services.h"
 #include "db/sysdb_autofs.h"
+#include "db/sysdb_ldb_wrapper.h"
 #include "util/crypto/sss_crypto.h"
 #include "util/cert.h"
 #include <time.h>
@@ -846,7 +847,7 @@ static errno_t sysdb_create_ts_entry(struct sysdb_ctx *sysdb,
         goto done;
     }
 
-    lret = ldb_add(sysdb->ldb_ts, msg);
+    lret = sss_ldb_add(sysdb->ldb_ts, msg);
     if (lret != LDB_SUCCESS) {
         DEBUG(SSSDBG_OP_FAILURE,
               "ldb_add failed: [%s](%d)[%s]\n",
diff --git a/src/util/debug.h b/src/util/debug.h
index 2a1bd4ffd30817d7128805996c21105fe40982a2..610a65b6b6b9a41c9d7062b118abe5f015e10d68 100644
--- a/src/util/debug.h
+++ b/src/util/debug.h
@@ -67,6 +67,7 @@ int get_fd_from_debug_file(void);
 #define SSSDBG_TRACE_INTERNAL 0x2000   /* level 8 */
 #define SSSDBG_TRACE_ALL      0x4000   /* level 9 */
 #define SSSDBG_BE_FO          0x8000   /* level 9 */
+#define SSSDBG_LDB            0x10000  /* level 9 */
 #define SSSDBG_IMPORTANT_INFO SSSDBG_OP_FAILURE
 
 #define SSSDBG_INVALID        -1
-- 
2.7.4

