From 4cbbea453cbe366239fb69c64f0b3dbde82f3b4c 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/7] SYSDB: Adding wrappers for ldb_* operations

This patch adds 4 wrappers:
* sss_ldb_add()
* sss_ldb_delete()
* sss_ldb_modify()
* sss_ldb_rename()

Those wrappers write ldif message to log if
debug_level = SSSDBG_LDB.

Adding and modifying produce full ldif.
Deleting and renaming produce only short message.

If SSSDBG_LDB is not set, wrappers collapse to normal
ldb_* functions without additonial memory consumption.

Resolves:
https://fedorahosted.org/sssd/ticket/3060
---
 Makefile.am                |   2 +
 src/db/sysdb_ldb_wrapper.c | 138 +++++++++++++++++++++++++++++++++++++++++++++
 src/db/sysdb_ldb_wrapper.h |  35 ++++++++++++
 src/util/debug.h           |   1 +
 4 files changed, 176 insertions(+)
 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 f792ed6a6b531d9e6e2c886c2fbe64e1e2345b73..591bd513a544d383d5812225bf0e4ab01389dd9a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -644,6 +644,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 \
@@ -892,6 +893,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..9aa32648987a241088d2701298e7404a76db2d52
--- /dev/null
+++ b/src/db/sysdb_ldb_wrapper.c
@@ -0,0 +1,138 @@
+/*
+   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 <string.h>
+#include <ldb.h>
+#include "util/util.h"
+
+struct sss_ldif_vprint_ctx {
+    char *ldif;
+};
+
+static int ldif_vprintf_fn(void *private_data, const char *fmt, ...)
+{
+    struct sss_ldif_vprint_ctx *print_ctx;
+    va_list ap;
+    int lenght = 0;
+
+    /* Note that the function should return the number of
+     * bytes written, or a negative error code.
+     */
+
+    print_ctx = talloc_get_type(private_data, struct sss_ldif_vprint_ctx);
+
+    if (print_ctx == NULL) {
+        return (-1 * ENOMEM);
+    }
+
+    if (fmt != NULL) {
+        va_start(ap, fmt);
+
+        if (print_ctx->ldif != NULL) {
+            lenght = strlen(print_ctx->ldif);
+        }
+
+        print_ctx->ldif = talloc_vasprintf_append_buffer(print_ctx->ldif,
+                                                         fmt, ap);
+        if (print_ctx->ldif == NULL) {
+            return (-1 * ENOENT);
+        }
+
+        lenght = strlen(print_ctx->ldif) - lenght;
+        va_end(ap);
+    }
+
+    return lenght;
+}
+
+static void sss_ldb_ldif2log(enum ldb_changetype changetype,
+                             struct ldb_context *ldb,
+                             const struct ldb_message *message)
+{
+    int ret;
+    struct ldb_ldif ldif;
+    struct sss_ldif_vprint_ctx *ldb_print_ctx;
+
+    ldb_print_ctx = talloc_zero(ldb, struct sss_ldif_vprint_ctx);
+    if (ldb_print_ctx == NULL) {
+        return;
+    }
+    ldb_print_ctx->ldif = NULL;
+
+    ldif.changetype = changetype;
+    ldif.msg = discard_const_p(struct ldb_message, message);
+
+    ret = ldb_ldif_write(ldb, ldif_vprintf_fn, ldb_print_ctx, &ldif);
+    if (ret < 0) {
+        ret = -1 * ret;
+        DEBUG(SSSDBG_MINOR_FAILURE, "ldb_ldif_write() failed with [%d][%s].\n",
+                                    ret, sss_strerror(ret));
+        goto done;
+    }
+
+    DEBUG(SSSDBG_LDB, "ldif\n[\n%s\n]\n", ldb_print_ctx->ldif);
+
+done:
+    talloc_free(ldb_print_ctx->ldif);
+    talloc_free(ldb_print_ctx);
+
+    return;
+}
+
+int sss_ldb_add(struct ldb_context *ldb, const struct ldb_message *message)
+{
+    if (DEBUG_IS_SET(SSSDBG_LDB) == true) {
+        sss_ldb_ldif2log(LDB_CHANGETYPE_ADD, ldb, message);
+    }
+
+    return ldb_add(ldb, message);
+}
+
+int sss_ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
+{
+    if (DEBUG_IS_SET(SSSDBG_LDB) == true) {
+        DEBUG(SSSDBG_LDB, "Deleting [%s]\n", ldb_dn_get_rdn_name(dn));
+    }
+
+    return ldb_delete(ldb, dn);
+}
+
+int sss_ldb_modify(struct ldb_context *ldb, const struct ldb_message *message)
+{
+    if (DEBUG_IS_SET(SSSDBG_LDB) == true) {
+        sss_ldb_ldif2log(LDB_CHANGETYPE_MODIFY, ldb, message);
+    }
+
+    return ldb_modify(ldb, message);
+}
+
+int sss_ldb_rename(struct ldb_context *ldb,
+                   struct ldb_dn *olddn,
+                   struct ldb_dn *newdn)
+{
+    if (DEBUG_IS_SET(SSSDBG_LDB) == true) {
+        DEBUG(SSSDBG_LDB, "Renaming [%s] to [%s]\n",
+                          ldb_dn_get_rdn_name(olddn),
+                          ldb_dn_get_rdn_name(newdn));
+    }
+
+    return ldb_rename(ldb, olddn, newdn);
+}
diff --git a/src/db/sysdb_ldb_wrapper.h b/src/db/sysdb_ldb_wrapper.h
new file mode 100644
index 0000000000000000000000000000000000000000..07a42f0f110ded75c4ad5048e23dd13429a449c1
--- /dev/null
+++ b/src/db/sysdb_ldb_wrapper.h
@@ -0,0 +1,35 @@
+/*
+   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/>.
+ */
+
+#ifndef __SYSDB_LDB_WRAPPER_H__
+#define __SYSDB_LDB_WRAPPER_H__
+
+int sss_ldb_add(struct ldb_context *ldb, const struct ldb_message *message);
+
+int sss_ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn);
+
+int sss_ldb_modify(struct ldb_context *ldb, const struct ldb_message *message);
+
+int sss_ldb_rename(struct ldb_context *ldb,
+                   struct ldb_dn *olddn,
+                   struct ldb_dn *newdn);
+
+#endif /* __SYSDB_LDB_WRAPPER_H__ */
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

