Blame SOURCES/0046-SYSDB-Add-methods-to-deal-with-the-domain-s-resoluti.patch

ecf709
From b601cae66c441163a00f73c64d00a29e0840d44e Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
ecf709
Date: Fri, 24 Mar 2017 15:29:23 +0100
ecf709
Subject: [PATCH 46/54] SYSDB: Add methods to deal with the domain's resolution
ecf709
 order
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
In the following-up patches those newly introduced methods will be used
ecf709
to deal with the domainResolutionOrder attribute.
ecf709
ecf709
The sysdb_update_domain_resolution_order() method is purposely not
ecf709
checking whether a value has changed or not before writing to sysdb and
ecf709
while may not be optimal, the readability of the code has increased a
ecf709
lot by keeping it as simple as possible.
ecf709
ecf709
Tests for these new methods are part of the next commit.
ecf709
ecf709
Related:
ecf709
https://pagure.io/SSSD/sssd/issue/3001
ecf709
ecf709
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
ecf709
ecf709
Reviewed-by: Sumit Bose <sbose@redhat.com>
ecf709
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ecf709
---
ecf709
 Makefile.am                            |   2 +
ecf709
 src/db/sysdb.h                         |   2 +
ecf709
 src/db/sysdb_domain_resolution_order.c | 169 +++++++++++++++++++++++++++++++++
ecf709
 src/db/sysdb_domain_resolution_order.h |  37 ++++++++
ecf709
 4 files changed, 210 insertions(+)
ecf709
 create mode 100644 src/db/sysdb_domain_resolution_order.c
ecf709
 create mode 100644 src/db/sysdb_domain_resolution_order.h
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index 359feddef298b0013c726409b7ba8b86504abf09..8052150be32d89813764e9bc436dfcb211a738d6 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -736,6 +736,7 @@ dist_noinst_HEADERS = \
ecf709
     src/db/sysdb_private.h \
ecf709
     src/db/sysdb_services.h \
ecf709
     src/db/sysdb_ssh.h \
ecf709
+    src/db/sysdb_domain_resolution_order.h \
ecf709
     src/confdb/confdb.h \
ecf709
     src/confdb/confdb_private.h \
ecf709
     src/confdb/confdb_setup.h \
ecf709
@@ -995,6 +996,7 @@ libsss_util_la_SOURCES = \
ecf709
     src/db/sysdb_idmap.c \
ecf709
     src/db/sysdb_gpo.c \
ecf709
     src/db/sysdb_certmap.c \
ecf709
+    src/db/sysdb_domain_resolution_order.c \
ecf709
     src/monitor/monitor_sbus.c \
ecf709
     src/providers/dp_auth_util.c \
ecf709
     src/providers/dp_pam_data_util.c \
ecf709
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
ecf709
index 6762b51bee02911fb97d5d393fad2495504ee5ad..42d2857ed7765c17e7d84b0da93ed07758fbe012 100644
ecf709
--- a/src/db/sysdb.h
ecf709
+++ b/src/db/sysdb.h
ecf709
@@ -184,6 +184,8 @@
ecf709
 #define SYSDB_OVERRIDE_GROUP_CLASS "groupOverride"
ecf709
 #define SYSDB_OVERRIDE_DN "overrideDN"
ecf709
 #define SYSDB_OVERRIDE_OBJECT_DN "overrideObjectDN"
ecf709
+#define SYSDB_USE_DOMAIN_RESOLUTION_ORDER "useDomainResolutionOrder"
ecf709
+#define SYSDB_DOMAIN_RESOLUTION_ORDER "domainResolutionOrder"
ecf709
 
ecf709
 #define SYSDB_NEXTID_FILTER "("SYSDB_NEXTID"=*)"
ecf709
 
ecf709
diff --git a/src/db/sysdb_domain_resolution_order.c b/src/db/sysdb_domain_resolution_order.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..63774461a1e9f3dc863220d418e29e06d6e6e6df
ecf709
--- /dev/null
ecf709
+++ b/src/db/sysdb_domain_resolution_order.c
ecf709
@@ -0,0 +1,169 @@
ecf709
+/*
ecf709
+    Authors:
ecf709
+        Fabiano Fidêncio <fidencio@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#include <ldb.h>
ecf709
+
ecf709
+#include "db/sysdb.h"
ecf709
+#include "db/sysdb_private.h"
ecf709
+
ecf709
+static errno_t
ecf709
+sysdb_get_domain_resolution_order_string_attr(TALLOC_CTX *mem_ctx,
ecf709
+                                              struct sysdb_ctx *sysdb,
ecf709
+                                              struct ldb_dn *dn,
ecf709
+                                              const char *const *attrs,
ecf709
+                                              const char **_attr)
ecf709
+{
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    struct ldb_result *res;
ecf709
+    const char *attr;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs,
ecf709
+                     NULL);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        ret = EIO;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    if (res->count > 1) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE,
ecf709
+              "Base search returned [%d] results, expected 1.\n", res->count);
ecf709
+        ret = EINVAL;
ecf709
+        goto done;
ecf709
+    } else if (res->count == 0) {
ecf709
+        ret = ENOENT;
ecf709
+        goto done;
ecf709
+    } else {
ecf709
+        /* res->count == 1 */
ecf709
+        attr = ldb_msg_find_attr_as_string(res->msgs[0], attrs[0], NULL);
ecf709
+        if (attr == NULL) {
ecf709
+            ret = ENOENT;
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    *_attr = talloc_steal(mem_ctx, attr);
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+errno_t
ecf709
+sysdb_get_domain_resolution_order(TALLOC_CTX *mem_ctx,
ecf709
+                                  struct sysdb_ctx *sysdb,
ecf709
+                                  struct ldb_dn *dn,
ecf709
+                                  const char **_domain_resolution_order)
ecf709
+{
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    const char *domain_resolution_order = NULL;
ecf709
+    const char *attrs[] = { SYSDB_DOMAIN_RESOLUTION_ORDER, NULL };
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    ret = sysdb_get_domain_resolution_order_string_attr(
ecf709
+            tmp_ctx, sysdb, dn, attrs, &domain_resolution_order);
ecf709
+    if (ret != EOK && ret != ENOENT) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE,
ecf709
+              "sysdb_get_domain_resolution_order_string_attr() failed "
ecf709
+              "[%d]: [%s]",
ecf709
+              ret, sss_strerror(ret));
ecf709
+        goto done;
ecf709
+    } else if (ret == ENOENT) {
ecf709
+        *_domain_resolution_order = NULL;
ecf709
+        goto done;
ecf709
+    } else {
ecf709
+        /* ret == EOK */
ecf709
+        *_domain_resolution_order = talloc_steal(mem_ctx,
ecf709
+                                                 domain_resolution_order);
ecf709
+    }
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+errno_t
ecf709
+sysdb_update_domain_resolution_order(struct sysdb_ctx *sysdb,
ecf709
+                                     struct ldb_dn *dn,
ecf709
+                                     const char *domain_resolution_order)
ecf709
+{
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    struct ldb_message *msg;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    msg = ldb_msg_new(tmp_ctx);
ecf709
+    if (msg == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    msg->dn = dn;
ecf709
+
ecf709
+    ret = ldb_msg_add_empty(msg, SYSDB_DOMAIN_RESOLUTION_ORDER,
ecf709
+                            LDB_FLAG_MOD_REPLACE, NULL);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        ret = sysdb_error_to_errno(ret);
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    if (domain_resolution_order != NULL) {
ecf709
+        ret = ldb_msg_add_string(msg, SYSDB_DOMAIN_RESOLUTION_ORDER,
ecf709
+                                 domain_resolution_order);
ecf709
+        if (ret != LDB_SUCCESS) {
ecf709
+            ret = sysdb_error_to_errno(ret);
ecf709
+            goto done;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    ret = ldb_modify(sysdb->ldb, msg);
ecf709
+    if (ret != LDB_SUCCESS) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE,
ecf709
+              "ldb_modify()_failed: [%s][%d][%s]\n",
ecf709
+              ldb_strerror(ret), ret, ldb_errstring(sysdb->ldb));
ecf709
+        ret = sysdb_error_to_errno(ret);
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
diff --git a/src/db/sysdb_domain_resolution_order.h b/src/db/sysdb_domain_resolution_order.h
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..45d2ea63f6bc14cd3184994530846ee6f762d4d0
ecf709
--- /dev/null
ecf709
+++ b/src/db/sysdb_domain_resolution_order.h
ecf709
@@ -0,0 +1,37 @@
ecf709
+/*
ecf709
+    Authors:
ecf709
+        Fabiano Fidêncio <fidencio@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#ifndef _SYSDB_DOMAIN_RESOLUTION_ORDER_H_
ecf709
+#define _SYSDB_DOMAIN_RESOLUTION_ORDER_H_
ecf709
+
ecf709
+#include "db/sysdb.h"
ecf709
+
ecf709
+errno_t
ecf709
+sysdb_get_domain_resolution_order(TALLOC_CTX *mem_ctx,
ecf709
+                                  struct sysdb_ctx *sysdb,
ecf709
+                                  struct ldb_dn *dn,
ecf709
+                                  const char **_domain_resolution_order);
ecf709
+
ecf709
+errno_t
ecf709
+sysdb_update_domain_resolution_order(struct sysdb_ctx *sysdb,
ecf709
+                                     struct ldb_dn *dn,
ecf709
+                                     const char *domain_resolution_order);
ecf709
+
ecf709
+#endif /* _SYSDB_DOMAIN_RESOLUTION_ORDER_H_ */
ecf709
-- 
ecf709
2.9.3
ecf709