dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0057-sysdb-remove-IDXONE-and-objectClass-from-users-and-g.patch

9f2ebf
From 0f907d8501387ec32dbb00e1c38d5da25e698f90 Mon Sep 17 00:00:00 2001
9f2ebf
From: Sumit Bose <sbose@redhat.com>
9f2ebf
Date: Tue, 14 Nov 2017 13:14:14 +0100
9f2ebf
Subject: [PATCH 57/57] sysdb: remove IDXONE and objectClass from users and
9f2ebf
 groups
9f2ebf
MIME-Version: 1.0
9f2ebf
Content-Type: text/plain; charset=UTF-8
9f2ebf
Content-Transfer-Encoding: 8bit
9f2ebf
9f2ebf
This patch does the needed sysdb update for the previous to patches. It
9f2ebf
removes the one-level search index IDXONE and replaces objectClass with
9f2ebf
objectCategory in the user and group objects.
9f2ebf
9f2ebf
Related to https://pagure.io/SSSD/sssd/issue/3503
9f2ebf
9f2ebf
Reviewed-by: Fabiano FidĂȘncio <fidencio@redhat.com>
9f2ebf
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
9f2ebf
(cherry picked from commit 2927da49dd8a16fff6312d89ad43cc355655800c)
9f2ebf
---
9f2ebf
 src/db/sysdb_init.c    |  52 +++++++++++-
9f2ebf
 src/db/sysdb_private.h |  11 ++-
9f2ebf
 src/db/sysdb_upgrade.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++
9f2ebf
 3 files changed, 274 insertions(+), 6 deletions(-)
9f2ebf
9f2ebf
diff --git a/src/db/sysdb_init.c b/src/db/sysdb_init.c
9f2ebf
index 44a7918f603fe1368b7d81738666de6bb47b83d0..74ad23f3050da0ae14fa495d2302f4a858fcd3c5 100644
9f2ebf
--- a/src/db/sysdb_init.c
9f2ebf
+++ b/src/db/sysdb_init.c
9f2ebf
@@ -359,8 +359,48 @@ static errno_t sysdb_ts_cache_upgrade(TALLOC_CTX *mem_ctx,
9f2ebf
                                       const char *cur_version,
9f2ebf
                                       const char **_new_version)
9f2ebf
 {
9f2ebf
-    /* Currently the sysdb cache only has one version */
9f2ebf
-    return EFAULT;
9f2ebf
+    errno_t ret;
9f2ebf
+    TALLOC_CTX *tmp_ctx;
9f2ebf
+    const char *version;
9f2ebf
+    struct ldb_context *save_ldb;
9f2ebf
+
9f2ebf
+    tmp_ctx = talloc_new(NULL);
9f2ebf
+    if (tmp_ctx == NULL) {
9f2ebf
+        return ENOMEM;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    /* The upgrade process depends on having ldb around, yet the upgrade
9f2ebf
+     * function shouldn't set the ldb pointer, only the connect function
9f2ebf
+     * should after it's successful. To avoid hard refactoring, save the
9f2ebf
+     * ldb pointer here and restore in the 'done' handler
9f2ebf
+     */
9f2ebf
+    save_ldb = sysdb->ldb;
9f2ebf
+    sysdb->ldb = ldb;
9f2ebf
+
9f2ebf
+    version = talloc_strdup(tmp_ctx, cur_version);
9f2ebf
+    if (version == NULL) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    DEBUG(SSSDBG_CONF_SETTINGS,
9f2ebf
+          "Upgrading timstamp cache of DB [%s] from version: %s\n",
9f2ebf
+          domain->name, version);
9f2ebf
+
9f2ebf
+    if (strcmp(version, SYSDB_TS_VERSION_0_1) == 0) {
9f2ebf
+        ret = sysdb_ts_upgrade_01(sysdb, &version);
9f2ebf
+        if (ret != EOK) {
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = EOK;
9f2ebf
+
9f2ebf
+done:
9f2ebf
+    sysdb->ldb = save_ldb;
9f2ebf
+    *_new_version = version;
9f2ebf
+    talloc_free(tmp_ctx);
9f2ebf
+    return ret;
9f2ebf
 }
9f2ebf
 
9f2ebf
 static errno_t sysdb_domain_cache_upgrade(TALLOC_CTX *mem_ctx,
9f2ebf
@@ -511,6 +551,14 @@ static errno_t sysdb_domain_cache_upgrade(TALLOC_CTX *mem_ctx,
9f2ebf
         }
9f2ebf
     }
9f2ebf
 
9f2ebf
+    if (strcmp(version, SYSDB_VERSION_0_19) == 0) {
9f2ebf
+        ret = sysdb_upgrade_19(sysdb, &version);
9f2ebf
+        if (ret != EOK) {
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+    }
9f2ebf
+
9f2ebf
+
9f2ebf
     ret = EOK;
9f2ebf
 done:
9f2ebf
     sysdb->ldb = save_ldb;
9f2ebf
diff --git a/src/db/sysdb_private.h b/src/db/sysdb_private.h
9f2ebf
index dbd75615bc212e73c4338a76dceaa68a5889ed1d..cac06ba46da23080d1ab661502d0792bd37b9291 100644
9f2ebf
--- a/src/db/sysdb_private.h
9f2ebf
+++ b/src/db/sysdb_private.h
9f2ebf
@@ -23,6 +23,7 @@
9f2ebf
 #ifndef __INT_SYS_DB_H__
9f2ebf
 #define __INT_SYS_DB_H__
9f2ebf
 
9f2ebf
+#define SYSDB_VERSION_0_20 "0.20"
9f2ebf
 #define SYSDB_VERSION_0_19 "0.19"
9f2ebf
 #define SYSDB_VERSION_0_18 "0.18"
9f2ebf
 #define SYSDB_VERSION_0_17 "0.17"
9f2ebf
@@ -43,7 +44,7 @@
9f2ebf
 #define SYSDB_VERSION_0_2 "0.2"
9f2ebf
 #define SYSDB_VERSION_0_1 "0.1"
9f2ebf
 
9f2ebf
-#define SYSDB_VERSION SYSDB_VERSION_0_19
9f2ebf
+#define SYSDB_VERSION SYSDB_VERSION_0_20
9f2ebf
 
9f2ebf
 #define SYSDB_BASE_LDIF \
9f2ebf
      "dn: @ATTRIBUTES\n" \
9f2ebf
@@ -72,7 +73,6 @@
9f2ebf
      "@IDXATTR: sudoUser\n" \
9f2ebf
      "@IDXATTR: sshKnownHostsExpire\n" \
9f2ebf
      "@IDXATTR: objectSIDString\n" \
9f2ebf
-     "@IDXONE: 1\n" \
9f2ebf
      "@IDXATTR: ghost\n" \
9f2ebf
      "@IDXATTR: userPrincipalName\n" \
9f2ebf
      "@IDXATTR: canonicalUserPrincipalName\n" \
9f2ebf
@@ -92,9 +92,10 @@
9f2ebf
      "\n"
9f2ebf
 
9f2ebf
 /* The timestamp cache has its own versioning */
9f2ebf
+#define SYSDB_TS_VERSION_0_2 "0.2"
9f2ebf
 #define SYSDB_TS_VERSION_0_1 "0.1"
9f2ebf
 
9f2ebf
-#define SYSDB_TS_VERSION SYSDB_TS_VERSION_0_1
9f2ebf
+#define SYSDB_TS_VERSION SYSDB_TS_VERSION_0_2
9f2ebf
 
9f2ebf
 #define SYSDB_TS_BASE_LDIF \
9f2ebf
      "dn: @ATTRIBUTES\n" \
9f2ebf
@@ -103,7 +104,6 @@
9f2ebf
      "dn: @INDEXLIST\n" \
9f2ebf
      "@IDXATTR: lastUpdate\n" \
9f2ebf
      "@IDXATTR: dataExpireTimestamp\n" \
9f2ebf
-     "@IDXONE: 1\n" \
9f2ebf
      "\n" \
9f2ebf
      "dn: cn=sysdb\n" \
9f2ebf
      "cn: sysdb\n" \
9f2ebf
@@ -169,6 +169,9 @@ int sysdb_upgrade_17(struct sysdb_ctx *sysdb,
9f2ebf
                      struct sysdb_dom_upgrade_ctx *upgrade_ctx,
9f2ebf
                      const char **ver);
9f2ebf
 int sysdb_upgrade_18(struct sysdb_ctx *sysdb, const char **ver);
9f2ebf
+int sysdb_upgrade_19(struct sysdb_ctx *sysdb, const char **ver);
9f2ebf
+
9f2ebf
+int sysdb_ts_upgrade_01(struct sysdb_ctx *sysdb, const char **ver);
9f2ebf
 
9f2ebf
 int sysdb_add_string(struct ldb_message *msg,
9f2ebf
                      const char *attr, const char *value);
9f2ebf
diff --git a/src/db/sysdb_upgrade.c b/src/db/sysdb_upgrade.c
9f2ebf
index 365d45f7ebd78523ca9ec4b9c2158cc09acb5489..bc157a24664239bc1255e49a1825243a07acc90f 100644
9f2ebf
--- a/src/db/sysdb_upgrade.c
9f2ebf
+++ b/src/db/sysdb_upgrade.c
9f2ebf
@@ -2317,6 +2317,223 @@ done:
9f2ebf
     return ret;
9f2ebf
 }
9f2ebf
 
9f2ebf
+static errno_t add_object_category(struct ldb_context *ldb,
9f2ebf
+                                   struct upgrade_ctx *ctx)
9f2ebf
+{
9f2ebf
+    errno_t ret;
9f2ebf
+    struct ldb_result *objects = NULL;
9f2ebf
+    const char *attrs[] = { SYSDB_OBJECTCLASS, NULL };
9f2ebf
+    struct ldb_dn *base_dn;
9f2ebf
+    size_t c;
9f2ebf
+    const char *class_name;
9f2ebf
+    struct ldb_message *msg = NULL;
9f2ebf
+    struct ldb_message *del_msg = NULL;
9f2ebf
+
9f2ebf
+    base_dn = ldb_dn_new(ctx, ldb, SYSDB_BASE);
9f2ebf
+    if (base_dn == NULL) {
9f2ebf
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed create base dn.\n");
9f2ebf
+        return ENOMEM;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = ldb_search(ldb, ctx, &objects, base_dn,
9f2ebf
+                     LDB_SCOPE_SUBTREE, attrs,
9f2ebf
+                     "(|("SYSDB_OBJECTCLASS"="SYSDB_USER_CLASS")"
9f2ebf
+                       "("SYSDB_OBJECTCLASS"="SYSDB_GROUP_CLASS"))");
9f2ebf
+    talloc_free(base_dn);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to search objects: %d\n", ret);
9f2ebf
+        ret = sysdb_error_to_errno(ret);
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    if (objects == NULL || objects->count == 0) {
9f2ebf
+        DEBUG(SSSDBG_TRACE_LIBS, "No objects found, nothing to do.");
9f2ebf
+        ret = EOK;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    del_msg = ldb_msg_new(ctx);
9f2ebf
+    if (del_msg == NULL) {
9f2ebf
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_new failed.\n");
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+    ret = ldb_msg_add_empty(del_msg, SYSDB_OBJECTCLASS, LDB_FLAG_MOD_DELETE,
9f2ebf
+                            NULL);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add_empty failed.\n");
9f2ebf
+        ret = sysdb_error_to_errno(ret);
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    DEBUG(SSSDBG_TRACE_ALL, "Found [%d] objects.\n", objects->count);
9f2ebf
+    for (c = 0; c < objects->count; c++) {
9f2ebf
+        DEBUG(SSSDBG_TRACE_ALL, "Updating [%s].\n",
9f2ebf
+              ldb_dn_get_linearized(objects->msgs[c]->dn));
9f2ebf
+
9f2ebf
+        class_name = ldb_msg_find_attr_as_string(objects->msgs[c],
9f2ebf
+                                                 SYSDB_OBJECTCLASS, NULL);
9f2ebf
+        if (class_name == NULL) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE, "Searched objects by objectClass, "
9f2ebf
+                                     "but result does not have one.\n");
9f2ebf
+            ret = EINVAL;
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+
9f2ebf
+        talloc_free(msg);
9f2ebf
+        msg = ldb_msg_new(ctx);
9f2ebf
+        if (msg == NULL) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_new failed.\n");
9f2ebf
+            ret = ENOMEM;
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+
9f2ebf
+        msg->dn = objects->msgs[c]->dn;
9f2ebf
+        del_msg->dn = objects->msgs[c]->dn;
9f2ebf
+
9f2ebf
+        ret = ldb_msg_add_empty(msg, SYSDB_OBJECTCATEGORY, LDB_FLAG_MOD_ADD,
9f2ebf
+                                NULL);
9f2ebf
+        if (ret != LDB_SUCCESS) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add_empty failed.\n");
9f2ebf
+            ret = sysdb_error_to_errno(ret);
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+
9f2ebf
+        ret = ldb_msg_add_string(msg, SYSDB_OBJECTCATEGORY, class_name);
9f2ebf
+        if (ret != LDB_SUCCESS) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE, "ldb_msg_add_string failed.\n");
9f2ebf
+            ret = sysdb_error_to_errno(ret);
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+
9f2ebf
+        DEBUG(SSSDBG_TRACE_ALL, "Adding [%s] to [%s].\n", class_name,
9f2ebf
+              ldb_dn_get_linearized(objects->msgs[c]->dn));
9f2ebf
+        ret = ldb_modify(ldb, msg);
9f2ebf
+        if (ret != LDB_SUCCESS) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE,
9f2ebf
+                  "Failed to add objectCategory to %s: %d.\n",
9f2ebf
+                  ldb_dn_get_linearized(objects->msgs[c]->dn),
9f2ebf
+                  sysdb_error_to_errno(ret));
9f2ebf
+            ret = sysdb_error_to_errno(ret);
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+
9f2ebf
+        ret = ldb_modify(ldb, del_msg);
9f2ebf
+        if (ret != LDB_SUCCESS) {
9f2ebf
+            DEBUG(SSSDBG_OP_FAILURE,
9f2ebf
+                  "Failed to remove objectClass from %s: %d.\n",
9f2ebf
+                  ldb_dn_get_linearized(objects->msgs[c]->dn),
9f2ebf
+                  sysdb_error_to_errno(ret));
9f2ebf
+            ret = sysdb_error_to_errno(ret);
9f2ebf
+            goto done;
9f2ebf
+        }
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = EOK;
9f2ebf
+
9f2ebf
+done:
9f2ebf
+    talloc_free(msg);
9f2ebf
+    talloc_free(del_msg);
9f2ebf
+    talloc_free(objects);
9f2ebf
+
9f2ebf
+    return ret;
9f2ebf
+}
9f2ebf
+
9f2ebf
+int sysdb_upgrade_19(struct sysdb_ctx *sysdb, const char **ver)
9f2ebf
+{
9f2ebf
+    struct upgrade_ctx *ctx;
9f2ebf
+    errno_t ret;
9f2ebf
+    struct ldb_message *msg = NULL;
9f2ebf
+
9f2ebf
+    ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_VERSION_0_20, &ctx;;
9f2ebf
+    if (ret) {
9f2ebf
+        return ret;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = add_object_category(sysdb->ldb, ctx);
9f2ebf
+    if (ret != EOK) {
9f2ebf
+        DEBUG(SSSDBG_CRIT_FAILURE, "add_object_category failed.\n");
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    /* Remove @IDXONE from index */
9f2ebf
+    msg = ldb_msg_new(ctx);
9f2ebf
+    if (msg == NULL) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    msg->dn = ldb_dn_new(msg, sysdb->ldb, "@INDEXLIST");
9f2ebf
+    if (msg->dn == NULL) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = ldb_msg_add_empty(msg, "@IDXONE", LDB_FLAG_MOD_DELETE, NULL);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = ldb_modify(sysdb->ldb, msg);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        ret = sysdb_error_to_errno(ret);
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    /* conversion done, update version number */
9f2ebf
+    ret = update_version(ctx);
9f2ebf
+
9f2ebf
+done:
9f2ebf
+    ret = finish_upgrade(ret, &ctx, ver);
9f2ebf
+    return ret;
9f2ebf
+}
9f2ebf
+
9f2ebf
+int sysdb_ts_upgrade_01(struct sysdb_ctx *sysdb, const char **ver)
9f2ebf
+{
9f2ebf
+    struct upgrade_ctx *ctx;
9f2ebf
+    errno_t ret;
9f2ebf
+    struct ldb_message *msg = NULL;
9f2ebf
+
9f2ebf
+    ret = commence_upgrade(sysdb, sysdb->ldb, SYSDB_TS_VERSION_0_2, &ctx;;
9f2ebf
+    if (ret) {
9f2ebf
+        return ret;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    /* Remove @IDXONE from index */
9f2ebf
+    talloc_free(msg);
9f2ebf
+    msg = ldb_msg_new(ctx);
9f2ebf
+    if (msg == NULL) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    msg->dn = ldb_dn_new(msg, sysdb->ldb, "@INDEXLIST");
9f2ebf
+    if (msg->dn == NULL) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = ldb_msg_add_empty(msg, "@IDXONE", LDB_FLAG_MOD_DELETE, NULL);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        ret = ENOMEM;
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    ret = ldb_modify(sysdb->ldb, msg);
9f2ebf
+    if (ret != LDB_SUCCESS) {
9f2ebf
+        ret = sysdb_error_to_errno(ret);
9f2ebf
+        goto done;
9f2ebf
+    }
9f2ebf
+
9f2ebf
+    /* conversion done, update version number */
9f2ebf
+    ret = update_version(ctx);
9f2ebf
+
9f2ebf
+done:
9f2ebf
+    ret = finish_upgrade(ret, &ctx, ver);
9f2ebf
+    return ret;
9f2ebf
+}
9f2ebf
+
9f2ebf
 /*
9f2ebf
  * Example template for future upgrades.
9f2ebf
  * Copy and change version numbers as appropriate.
9f2ebf
-- 
9f2ebf
2.14.3
9f2ebf