Blame SOURCES/0064-sss_override-decompose-code-better.patch

6cf099
From fdc64e169e988c4d1bc105ad6ccdfe4817db4c53 Mon Sep 17 00:00:00 2001
6cf099
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
6cf099
Date: Wed, 19 Aug 2015 12:43:15 +0200
6cf099
Subject: [PATCH 64/66] sss_override: decompose code better
6cf099
6cf099
Preparation for:
6cf099
https://fedorahosted.org/sssd/ticket/2737
6cf099
6cf099
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
6cf099
---
6cf099
 src/tools/sss_override.c | 290 +++++++++++++++++++++++++++++------------------
6cf099
 1 file changed, 181 insertions(+), 109 deletions(-)
6cf099
6cf099
diff --git a/src/tools/sss_override.c b/src/tools/sss_override.c
6cf099
index 84b51c537842b7281a523f58c2cfbdfd38e54c72..9e2ce3325c0bfa33fadb970f725098d7d12ac432 100644
6cf099
--- a/src/tools/sss_override.c
6cf099
+++ b/src/tools/sss_override.c
6cf099
@@ -171,6 +171,22 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+errno_t prepare_view_msg(struct sss_domain_info *domain)
6cf099
+{
6cf099
+    errno_t ret;
6cf099
+
6cf099
+    ret = prepare_view(domain);
6cf099
+    if (ret == EEXIST) {
6cf099
+        fprintf(stderr, _("Other than " LOCALVIEW " view already exist "
6cf099
+                "in domain %s.\n"), domain->name);
6cf099
+    } else if (ret != EOK) {
6cf099
+        fprintf(stderr, _("Unable to prepare " LOCALVIEW
6cf099
+                " view in domain %s.\n"), domain->name);
6cf099
+    }
6cf099
+
6cf099
+    return ret;
6cf099
+}
6cf099
+
6cf099
 static char *build_anchor(TALLOC_CTX *mem_ctx, const char *obj_dn)
6cf099
 {
6cf099
     char *anchor;
6cf099
@@ -320,17 +336,15 @@ static char *get_sysname(TALLOC_CTX *mem_ctx,
6cf099
     return get_fqname(mem_ctx, domain, name);
6cf099
 }
6cf099
 
6cf099
-static const char *get_object_dn_and_domain(TALLOC_CTX *mem_ctx,
6cf099
-                                         enum sysdb_member_type type,
6cf099
-                                         const char *name,
6cf099
-                                         struct sss_domain_info *domain,
6cf099
-                                         struct sss_domain_info *domains,
6cf099
-                                         struct sss_domain_info **_new_domain)
6cf099
+static struct sss_domain_info *
6cf099
+get_object_domain(enum sysdb_member_type type,
6cf099
+                  const char *name,
6cf099
+                  struct sss_domain_info *domain,
6cf099
+                  struct sss_domain_info *domains)
6cf099
 {
6cf099
     TALLOC_CTX *tmp_ctx;
6cf099
     struct sss_domain_info *dom;
6cf099
     struct ldb_result *res;
6cf099
-    const char *dn;
6cf099
     const char *strtype;
6cf099
     char *sysname;
6cf099
     bool check_next;
6cf099
@@ -427,18 +441,6 @@ static const char *get_object_dn_and_domain(TALLOC_CTX *mem_ctx,
6cf099
     DEBUG(SSSDBG_TRACE_FUNC, "Domain of %s %s is %s\n",
6cf099
           strtype, name, dom->name);
6cf099
 
6cf099
-    dn = ldb_dn_get_linearized(res->msgs[0]->dn);
6cf099
-    if (dn == NULL) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "ldb_dn_get_linearized() failed.\n");
6cf099
-        ret = ENOMEM;
6cf099
-        goto done;
6cf099
-    }
6cf099
-
6cf099
-    talloc_steal(mem_ctx, dn);
6cf099
-    *_new_domain = dom;
6cf099
-
6cf099
-    ret = EOK;
6cf099
-
6cf099
 done:
6cf099
     talloc_free(tmp_ctx);
6cf099
 
6cf099
@@ -446,35 +448,94 @@ done:
6cf099
         return NULL;
6cf099
     }
6cf099
 
6cf099
-    return dn;
6cf099
+    return dom;
6cf099
 }
6cf099
 
6cf099
-static const char * get_user_dn_and_domain(TALLOC_CTX *mem_ctx,
6cf099
-                                           struct sss_domain_info *domains,
6cf099
-                                           struct override_user *user)
6cf099
+static errno_t get_user_domain_msg(struct sss_tool_ctx *tool_ctx,
6cf099
+                                   struct override_user *user)
6cf099
 {
6cf099
-    return get_object_dn_and_domain(mem_ctx, SYSDB_MEMBER_USER,
6cf099
-                         user->orig_name, user->domain, domains,
6cf099
-                         &user->domain);
6cf099
+    struct sss_domain_info *newdom;
6cf099
+    const char *domname;
6cf099
+
6cf099
+    newdom = get_object_domain(SYSDB_MEMBER_USER, user->orig_name,
6cf099
+                               user->domain, tool_ctx->domains);
6cf099
+    if (newdom == NULL) {
6cf099
+        domname = user->domain == NULL ? "[unknown]" : user->domain->name;
6cf099
+        fprintf(stderr, _("Unable to find user %s@%s.\n"),
6cf099
+                user->orig_name, domname);
6cf099
+        return ENOENT;
6cf099
+    }
6cf099
+
6cf099
+    user->domain = newdom;
6cf099
+    return EOK;
6cf099
+}
6cf099
+
6cf099
+static errno_t get_group_domain_msg(struct sss_tool_ctx *tool_ctx,
6cf099
+                                    struct override_group *group)
6cf099
+{
6cf099
+    struct sss_domain_info *newdom;
6cf099
+    const char *domname;
6cf099
+
6cf099
+    newdom = get_object_domain(SYSDB_MEMBER_GROUP, group->orig_name,
6cf099
+                               group->domain, tool_ctx->domains);
6cf099
+    if (newdom == NULL) {
6cf099
+        domname = group->domain == NULL ? "[unknown]" : group->domain->name;
6cf099
+        fprintf(stderr, _("Unable to find group %s@%s.\n"),
6cf099
+                group->orig_name, domname);
6cf099
+        return ENOENT;
6cf099
+    }
6cf099
+
6cf099
+    group->domain = newdom;
6cf099
+    return EOK;
6cf099
 }
6cf099
 
6cf099
-static const char * get_group_dn_and_domain(TALLOC_CTX *mem_ctx,
6cf099
-                                            struct sss_domain_info *domains,
6cf099
-                                            struct override_group *group)
6cf099
+static errno_t get_object_dn(TALLOC_CTX *mem_ctx,
6cf099
+                             struct sss_domain_info *domain,
6cf099
+                             enum sysdb_member_type type,
6cf099
+                             const char *name,
6cf099
+                             struct ldb_dn **_ldb_dn,
6cf099
+                             const char **_str_dn)
6cf099
 {
6cf099
-    return get_object_dn_and_domain(mem_ctx, SYSDB_MEMBER_GROUP,
6cf099
-                         group->orig_name, group->domain, domains,
6cf099
-                         &group->domain);
6cf099
+    struct ldb_dn *ldb_dn;
6cf099
+
6cf099
+    switch (type) {
6cf099
+    case SYSDB_MEMBER_USER:
6cf099
+       ldb_dn = sysdb_user_dn(mem_ctx, domain, name);
6cf099
+       break;
6cf099
+    case SYSDB_MEMBER_GROUP:
6cf099
+       ldb_dn = sysdb_group_dn(mem_ctx, domain, name);
6cf099
+       break;
6cf099
+    default:
6cf099
+       DEBUG(SSSDBG_CRIT_FAILURE, "Unsupported member type %d\n", type);
6cf099
+       return ERR_INTERNAL;
6cf099
+    }
6cf099
+
6cf099
+    if (ldb_dn == NULL) {
6cf099
+        return ENOMEM;
6cf099
+    }
6cf099
+
6cf099
+    if (_str_dn != NULL) {
6cf099
+        *_str_dn = ldb_dn_get_linearized(ldb_dn);
6cf099
+    }
6cf099
+
6cf099
+    if (_ldb_dn != NULL) {
6cf099
+        *_ldb_dn = ldb_dn;
6cf099
+    } else {
6cf099
+        talloc_free(ldb_dn);
6cf099
+    }
6cf099
+
6cf099
+    return EOK;
6cf099
 }
6cf099
 
6cf099
 static errno_t override_object_add(struct sss_domain_info *domain,
6cf099
                                    enum sysdb_member_type type,
6cf099
                                    struct sysdb_attrs *attrs,
6cf099
-                                   const char *obj_dn)
6cf099
+                                   const char *name)
6cf099
 {
6cf099
     TALLOC_CTX *tmp_ctx;
6cf099
     const char *anchor;
6cf099
     struct ldb_dn *ldb_dn;
6cf099
+    const char *str_dn;
6cf099
     errno_t ret;
6cf099
 
6cf099
     tmp_ctx = talloc_new(NULL);
6cf099
@@ -482,13 +543,12 @@ static errno_t override_object_add(struct sss_domain_info *domain,
6cf099
         return ENOMEM;
6cf099
     }
6cf099
 
6cf099
-    ldb_dn = ldb_dn_new(tmp_ctx, sysdb_ctx_get_ldb(domain->sysdb), obj_dn);
6cf099
-    if (ldb_dn == NULL) {
6cf099
-        ret = ENOMEM;
6cf099
+    ret = get_object_dn(tmp_ctx, domain, type, name, &ldb_dn, &str_dn);
6cf099
+    if (ret != EOK) {
6cf099
         goto done;
6cf099
     }
6cf099
 
6cf099
-    anchor = build_anchor(tmp_ctx, obj_dn);
6cf099
+    anchor = build_anchor(tmp_ctx, str_dn);
6cf099
     if (anchor == NULL) {
6cf099
         ret = ENOMEM;
6cf099
         goto done;
6cf099
@@ -499,7 +559,7 @@ static errno_t override_object_add(struct sss_domain_info *domain,
6cf099
         goto done;
6cf099
     }
6cf099
 
6cf099
-    DEBUG(SSSDBG_TRACE_FUNC, "Creating override for %s\n", obj_dn);
6cf099
+    DEBUG(SSSDBG_TRACE_FUNC, "Creating override for %s\n", str_dn);
6cf099
 
6cf099
     ret = sysdb_store_override(domain, LOCALVIEW, type, attrs, ldb_dn);
6cf099
 
6cf099
@@ -508,13 +568,70 @@ done:
6cf099
     return ret;
6cf099
 }
6cf099
 
6cf099
+static errno_t override_user(struct sss_tool_ctx *tool_ctx,
6cf099
+                             struct override_user *user)
6cf099
+{
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+    errno_t ret;
6cf099
+
6cf099
+    ret = prepare_view_msg(user->domain);
6cf099
+    if (ret != EOK) {
6cf099
+        return ret;
6cf099
+    }
6cf099
+
6cf099
+    attrs = build_user_attrs(tool_ctx, user);
6cf099
+    if (attrs == NULL) {
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
6cf099
+        return ENOMEM;
6cf099
+    }
6cf099
+
6cf099
+    ret = override_object_add(user->domain, SYSDB_MEMBER_USER, attrs,
6cf099
+                              user->orig_name);
6cf099
+    if (ret != EOK) {
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
+        return ret;
6cf099
+    }
6cf099
+
6cf099
+    return EOK;
6cf099
+}
6cf099
+
6cf099
+static errno_t override_group(struct sss_tool_ctx *tool_ctx,
6cf099
+                              struct override_group *group)
6cf099
+{
6cf099
+    struct sysdb_attrs *attrs;
6cf099
+    errno_t ret;
6cf099
+
6cf099
+    ret = prepare_view_msg(group->domain);
6cf099
+    if (ret != EOK) {
6cf099
+        return ret;
6cf099
+    }
6cf099
+
6cf099
+    attrs = build_group_attrs(tool_ctx, group);
6cf099
+    if (attrs == NULL) {
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
6cf099
+        return ENOMEM;
6cf099
+    }
6cf099
+
6cf099
+    ret = override_object_add(group->domain, SYSDB_MEMBER_GROUP, attrs,
6cf099
+                              group->orig_name);
6cf099
+    if (ret != EOK) {
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
+        return ret;
6cf099
+    }
6cf099
+
6cf099
+    return EOK;
6cf099
+}
6cf099
+
6cf099
 static errno_t override_object_del(struct sss_domain_info *domain,
6cf099
-                                   const char *obj_dn)
6cf099
+                                   enum sysdb_member_type type,
6cf099
+                                   const char *name)
6cf099
 {
6cf099
     TALLOC_CTX *tmp_ctx;
6cf099
-    const char *anchor;
6cf099
-    struct ldb_dn *override_dn;
6cf099
     struct ldb_message *msg;
6cf099
+    struct ldb_dn *override_dn;
6cf099
+    struct ldb_dn *ldb_dn;
6cf099
+    const char *str_dn;
6cf099
+    const char *anchor;
6cf099
     errno_t ret;
6cf099
     int sret;
6cf099
     bool in_transaction = false;
6cf099
@@ -525,7 +642,12 @@ static errno_t override_object_del(struct sss_domain_info *domain,
6cf099
         return ENOMEM;
6cf099
     }
6cf099
 
6cf099
-    anchor = build_anchor(tmp_ctx, obj_dn);
6cf099
+    ret = get_object_dn(tmp_ctx, domain, type, name, &ldb_dn, &str_dn);
6cf099
+    if (ret != EOK) {
6cf099
+        goto done;
6cf099
+    }
6cf099
+
6cf099
+    anchor = build_anchor(tmp_ctx, str_dn);
6cf099
     if (anchor == NULL) {
6cf099
         ret = ENOMEM;
6cf099
         goto done;
6cf099
@@ -538,7 +660,7 @@ static errno_t override_object_del(struct sss_domain_info *domain,
6cf099
         goto done;
6cf099
     }
6cf099
 
6cf099
-    DEBUG(SSSDBG_TRACE_FUNC, "Removing override for %s\n", obj_dn);
6cf099
+    DEBUG(SSSDBG_TRACE_FUNC, "Removing override for %s\n", str_dn);
6cf099
 
6cf099
     ret = sysdb_transaction_start(domain->sysdb);
6cf099
     if (ret != EOK) {
6cf099
@@ -559,7 +681,7 @@ static errno_t override_object_del(struct sss_domain_info *domain,
6cf099
         goto done;
6cf099
     }
6cf099
 
6cf099
-    msg->dn = ldb_dn_new(msg, ldb, obj_dn);
6cf099
+    msg->dn = talloc_steal(msg, ldb_dn);
6cf099
     if (msg->dn == NULL) {
6cf099
         ret = ENOMEM;
6cf099
         goto done;
6cf099
@@ -607,8 +729,6 @@ static int override_user_add(struct sss_cmdline *cmdline,
6cf099
                              void *pvt)
6cf099
 {
6cf099
     struct override_user user = {NULL};
6cf099
-    struct sysdb_attrs *attrs;
6cf099
-    const char *dn;
6cf099
     int ret;
6cf099
 
6cf099
     ret = parse_cmdline_user_add(cmdline, tool_ctx, &user);
6cf099
@@ -617,34 +737,13 @@ static int override_user_add(struct sss_cmdline *cmdline,
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    dn = get_user_dn_and_domain(tool_ctx, tool_ctx->domains, &user);
6cf099
-    if (dn == NULL) {
6cf099
-        fprintf(stderr, _("Unable to find user %s@%s.\n"),
6cf099
-                user.orig_name,
6cf099
-                user.domain == NULL ? "[unknown]" : user.domain->name);
6cf099
-        return EXIT_FAILURE;
6cf099
-    }
6cf099
-
6cf099
-    ret = prepare_view(user.domain);
6cf099
-    if (ret == EEXIST) {
6cf099
-        fprintf(stderr, _("Other than LOCAL view already exist in "
6cf099
-                "domain %s.\n"), user.domain->name);
6cf099
-        return EXIT_FAILURE;
6cf099
-    } else if (ret != EOK) {
6cf099
-        fprintf(stderr, _("Unable to prepare view [%d]: %s.\n"),
6cf099
-                ret, sss_strerror(ret));
6cf099
-        return EXIT_FAILURE;
6cf099
-    }
6cf099
-
6cf099
-    attrs = build_user_attrs(tool_ctx, &user);
6cf099
-    if (attrs == NULL) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
6cf099
+    ret = get_user_domain_msg(tool_ctx, &user);
6cf099
+    if (ret != EOK) {
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    ret = override_object_add(user.domain, SYSDB_MEMBER_USER, attrs, dn);
6cf099
+    ret = override_user(tool_ctx, &user);
6cf099
     if (ret != EOK) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
@@ -656,7 +755,6 @@ static int override_user_del(struct sss_cmdline *cmdline,
6cf099
                              void *pvt)
6cf099
 {
6cf099
     struct override_user user = {NULL};
6cf099
-    const char *dn;
6cf099
     int ret;
6cf099
 
6cf099
     ret = parse_cmdline_user_del(cmdline, tool_ctx, &user);
6cf099
@@ -665,16 +763,14 @@ static int override_user_del(struct sss_cmdline *cmdline,
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    dn = get_user_dn_and_domain(tool_ctx, tool_ctx->domains, &user);
6cf099
-    if (dn == NULL) {
6cf099
-        fprintf(stderr, _("Unable to find user %s@%s.\n"),
6cf099
-                user.orig_name, user.domain->name);
6cf099
+    ret = get_user_domain_msg(tool_ctx, &user);
6cf099
+    if (ret != EOK) {
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    ret = override_object_del(user.domain, dn);
6cf099
+    ret = override_object_del(user.domain, SYSDB_MEMBER_USER, user.orig_name);
6cf099
     if (ret != EOK) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to delete override object.\n");
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
@@ -686,8 +782,6 @@ static int override_group_add(struct sss_cmdline *cmdline,
6cf099
                               void *pvt)
6cf099
 {
6cf099
     struct override_group group = {NULL};
6cf099
-    struct sysdb_attrs *attrs;
6cf099
-    const char *dn;
6cf099
     int ret;
6cf099
 
6cf099
     ret = parse_cmdline_group_add(cmdline, tool_ctx, &group);
6cf099
@@ -696,33 +790,13 @@ static int override_group_add(struct sss_cmdline *cmdline,
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    dn = get_group_dn_and_domain(tool_ctx, tool_ctx->domains, &group);
6cf099
-    if (dn == NULL) {
6cf099
-        fprintf(stderr, _("Unable to find group %s@%s.\n"),
6cf099
-                group.orig_name, group.domain->name);
6cf099
-        return EXIT_FAILURE;
6cf099
-    }
6cf099
-
6cf099
-    ret = prepare_view(group.domain);
6cf099
-    if (ret == EEXIST) {
6cf099
-        fprintf(stderr, _("Other than LOCAL view already exist in "
6cf099
-                "domain %s.\n"), group.domain->name);
6cf099
-        return EXIT_FAILURE;
6cf099
-    } else if (ret != EOK) {
6cf099
-        fprintf(stderr, _("Unable to prepare view [%d]: %s.\n"),
6cf099
-                ret, sss_strerror(ret));
6cf099
-        return EXIT_FAILURE;
6cf099
-    }
6cf099
-
6cf099
-    attrs = build_group_attrs(tool_ctx, &group);
6cf099
-    if (attrs == NULL) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build sysdb attrs.\n");
6cf099
+    ret = get_group_domain_msg(tool_ctx, &group);
6cf099
+    if (ret != EOK) {
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    ret = override_object_add(group.domain, SYSDB_MEMBER_GROUP, attrs, dn);
6cf099
+    ret = override_group(tool_ctx, &group);
6cf099
     if (ret != EOK) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
@@ -734,7 +808,6 @@ static int override_group_del(struct sss_cmdline *cmdline,
6cf099
                               void *pvt)
6cf099
 {
6cf099
     struct override_group group = {NULL};
6cf099
-    const char *dn;
6cf099
     int ret;
6cf099
 
6cf099
     ret = parse_cmdline_group_del(cmdline, tool_ctx, &group);
6cf099
@@ -743,16 +816,15 @@ static int override_group_del(struct sss_cmdline *cmdline,
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    dn = get_group_dn_and_domain(tool_ctx, tool_ctx->domains, &group);
6cf099
-    if (dn == NULL) {
6cf099
-        fprintf(stderr, _("Unable to find group %s@%s.\n"),
6cf099
-                group.orig_name, group.domain->name);
6cf099
+    ret = get_group_domain_msg(tool_ctx, &group);
6cf099
+    if (ret != EOK) {
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-    ret = override_object_del(group.domain, dn);
6cf099
+    ret = override_object_del(group.domain, SYSDB_MEMBER_GROUP,
6cf099
+                              group.orig_name);
6cf099
     if (ret != EOK) {
6cf099
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add override object.\n");
6cf099
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to delete override object.\n");
6cf099
         return EXIT_FAILURE;
6cf099
     }
6cf099
 
6cf099
-- 
6cf099
2.4.3
6cf099