Blame SOURCES/0199-SDAP-Extract-filtering-AD-group-to-function.patch

e543c9
From 64eb7055b640e9c92701886effc36f74fe9e709f Mon Sep 17 00:00:00 2001
e543c9
From: Lukas Slebodnik <lslebodn@redhat.com>
e543c9
Date: Mon, 13 Apr 2015 09:44:35 +0200
e543c9
Subject: [PATCH 199/200] SDAP: Extract filtering AD group to function
e543c9
e543c9
Patch remove code duplication.
e543c9
e543c9
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
e543c9
(cherry picked from commit bad2fc8133d941e5a6c8d8016c9689e039265c61)
e543c9
---
e543c9
 Makefile.am                                   |  2 +
e543c9
 src/providers/ldap/sdap_ad_groups.c           | 68 +++++++++++++++++++++++++++
e543c9
 src/providers/ldap/sdap_async_groups.c        | 40 ++++++----------
e543c9
 src/providers/ldap/sdap_async_nested_groups.c | 31 ++++--------
e543c9
 src/providers/ldap/sdap_async_private.h       |  7 +++
e543c9
 5 files changed, 101 insertions(+), 47 deletions(-)
e543c9
 create mode 100644 src/providers/ldap/sdap_ad_groups.c
e543c9
e543c9
diff --git a/Makefile.am b/Makefile.am
e543c9
index 8202659e0933529ca7911952bbf1476dbb4a76fc..f402239af2cfaf77dde1ce6ff261015f5d9bfacc 100644
e543c9
--- a/Makefile.am
e543c9
+++ b/Makefile.am
e543c9
@@ -1858,6 +1858,7 @@ nestedgroups_tests_SOURCES = \
e543c9
     src/providers/ldap/sdap_idmap.c \
e543c9
     src/tests/cmocka/test_nested_groups.c \
e543c9
     src/providers/ldap/sdap_async_nested_groups.c \
e543c9
+    src/providers/ldap/sdap_ad_groups.c \
e543c9
     $(NULL)
e543c9
 nestedgroups_tests_CFLAGS = \
e543c9
     $(AM_CFLAGS) \
e543c9
@@ -2307,6 +2308,7 @@ libsss_ldap_common_la_SOURCES = \
e543c9
     src/providers/ldap/sdap_async_connection.c \
e543c9
     src/providers/ldap/sdap_async_netgroups.c \
e543c9
     src/providers/ldap/sdap_async_services.c \
e543c9
+    src/providers/ldap/sdap_ad_groups.c \
e543c9
     src/providers/ldap/sdap_child_helpers.c \
e543c9
     src/providers/ldap/sdap_fd_events.c \
e543c9
     src/providers/ldap/sdap_id_op.c \
e543c9
diff --git a/src/providers/ldap/sdap_ad_groups.c b/src/providers/ldap/sdap_ad_groups.c
e543c9
new file mode 100644
e543c9
index 0000000000000000000000000000000000000000..0e36328b9b52643a2ec698b2a41f2a56a8ff69b6
e543c9
--- /dev/null
e543c9
+++ b/src/providers/ldap/sdap_ad_groups.c
e543c9
@@ -0,0 +1,68 @@
e543c9
+/*
e543c9
+    SSSD
e543c9
+
e543c9
+    AD groups helper routines
e543c9
+
e543c9
+    Authors:
e543c9
+        Lukas Slebodnik <lslebodn@redhat.com>
e543c9
+
e543c9
+    Copyright (C) 2013 Red Hat
e543c9
+
e543c9
+    This program is free software; you can redistribute it and/or modify
e543c9
+    it under the terms of the GNU General Public License as published by
e543c9
+    the Free Software Foundation; either version 3 of the License, or
e543c9
+    (at your option) any later version.
e543c9
+
e543c9
+    This program is distributed in the hope that it will be useful,
e543c9
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
e543c9
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e543c9
+    GNU General Public License for more details.
e543c9
+
e543c9
+    You should have received a copy of the GNU General Public License
e543c9
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
e543c9
+*/
e543c9
+
e543c9
+#include "db/sysdb.h"
e543c9
+#include "providers/ldap/sdap.h"
e543c9
+#include "providers/ldap/sdap_async_private.h"
e543c9
+
e543c9
+/* ==Group-Parsing Routines=============================================== */
e543c9
+
e543c9
+errno_t sdap_check_ad_group_type(struct sss_domain_info *dom,
e543c9
+                                 struct sdap_options *opts,
e543c9
+                                 struct sysdb_attrs *group_attrs,
e543c9
+                                 const char *group_name,
e543c9
+                                 bool *_need_filter)
e543c9
+{
e543c9
+    int32_t ad_group_type;
e543c9
+    errno_t ret = EOK;
e543c9
+    *_need_filter = false;
e543c9
+
e543c9
+    if (opts->schema_type == SDAP_SCHEMA_AD) {
e543c9
+        ret = sysdb_attrs_get_int32_t(group_attrs, SYSDB_GROUP_TYPE,
e543c9
+                                      &ad_group_type);
e543c9
+        if (ret != EOK) {
e543c9
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_int32_t failed.\n");
e543c9
+            return ret;
e543c9
+        }
e543c9
+
e543c9
+        DEBUG(SSSDBG_TRACE_ALL,
e543c9
+              "AD group [%s] has type flags %#x.\n",
e543c9
+              group_name, ad_group_type);
e543c9
+
e543c9
+        /* Only security groups from AD are considered for POSIX groups.
e543c9
+         * Additionally only global and universal group are taken to account
e543c9
+         * for trusted domains. */
e543c9
+        if (!(ad_group_type & SDAP_AD_GROUP_TYPE_SECURITY)
e543c9
+            || (IS_SUBDOMAIN(dom)
e543c9
+                && (!((ad_group_type & SDAP_AD_GROUP_TYPE_GLOBAL)
e543c9
+                      || (ad_group_type & SDAP_AD_GROUP_TYPE_UNIVERSAL))))) {
e543c9
+            DEBUG(SSSDBG_TRACE_FUNC,
e543c9
+                  "Filtering AD group [%s].\n", group_name);
e543c9
+
e543c9
+            *_need_filter = true;
e543c9
+        }
e543c9
+    }
e543c9
+
e543c9
+    return ret;
e543c9
+}
e543c9
diff --git a/src/providers/ldap/sdap_async_groups.c b/src/providers/ldap/sdap_async_groups.c
e543c9
index 4be8c502ea77a3913ddac2a24fbacbc522b2ef6b..00a676372fa042dfc2d57e5799261f9a45ed4a73 100644
e543c9
--- a/src/providers/ldap/sdap_async_groups.c
e543c9
+++ b/src/providers/ldap/sdap_async_groups.c
e543c9
@@ -510,10 +510,10 @@ static int sdap_save_group(TALLOC_CTX *memctx,
e543c9
     TALLOC_CTX *tmpctx = NULL;
e543c9
     bool posix_group;
e543c9
     bool use_id_mapping;
e543c9
+    bool need_filter;
e543c9
     char *sid_str;
e543c9
     const char *uuid;
e543c9
     struct sss_domain_info *subdomain;
e543c9
-    int32_t ad_group_type;
e543c9
 
e543c9
     tmpctx = talloc_new(NULL);
e543c9
     if (!tmpctx) {
e543c9
@@ -588,32 +588,20 @@ static int sdap_save_group(TALLOC_CTX *memctx,
e543c9
     DEBUG(SSSDBG_TRACE_FUNC, "Processing group %s\n", group_name);
e543c9
 
e543c9
     posix_group = true;
e543c9
-    if (opts->schema_type == SDAP_SCHEMA_AD) {
e543c9
-        ret = sysdb_attrs_get_int32_t(attrs, SYSDB_GROUP_TYPE, &ad_group_type);
e543c9
-        if (ret != EOK) {
e543c9
-            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_int32_t failed.\n");
e543c9
-            goto done;
e543c9
-        }
e543c9
+    ret = sdap_check_ad_group_type(dom, opts, attrs, group_name,
e543c9
+                                   &need_filter);
e543c9
+    if (ret != EOK) {
e543c9
+        goto done;
e543c9
+    }
e543c9
+    if (need_filter) {
e543c9
+        posix_group = false;
e543c9
+        gid = 0;
e543c9
 
e543c9
-        DEBUG(SSSDBG_TRACE_ALL, "AD group [%s] has type flags %#x.\n",
e543c9
-                                 group_name, ad_group_type);
e543c9
-        /* Only security groups from AD are considered for POSIX groups.
e543c9
-         * Additionally only global and universal group are taken to account
e543c9
-         * for trusted domains. */
e543c9
-        if (!(ad_group_type & SDAP_AD_GROUP_TYPE_SECURITY)
e543c9
-                || (IS_SUBDOMAIN(dom)
e543c9
-                    && (!((ad_group_type & SDAP_AD_GROUP_TYPE_GLOBAL)
e543c9
-                        || (ad_group_type & SDAP_AD_GROUP_TYPE_UNIVERSAL))))) {
e543c9
-            posix_group = false;
e543c9
-            gid = 0;
e543c9
-            DEBUG(SSSDBG_TRACE_FUNC, "Filtering AD group [%s].\n",
e543c9
-                                      group_name);
e543c9
-            ret = sysdb_attrs_add_bool(group_attrs, SYSDB_POSIX, false);
e543c9
-            if (ret != EOK) {
e543c9
-                DEBUG(SSSDBG_OP_FAILURE,
e543c9
-                      "Error: Failed to mark group as non-posix!\n");
e543c9
-                return ret;
e543c9
-            }
e543c9
+        ret = sysdb_attrs_add_bool(group_attrs, SYSDB_POSIX, false);
e543c9
+        if (ret != EOK) {
e543c9
+            DEBUG(SSSDBG_OP_FAILURE,
e543c9
+                  "Error: Failed to mark group as non-posix!\n");
e543c9
+            return ret;
e543c9
         }
e543c9
     }
e543c9
 
e543c9
diff --git a/src/providers/ldap/sdap_async_nested_groups.c b/src/providers/ldap/sdap_async_nested_groups.c
e543c9
index 1eba35ae8ac90acac8a2d46e8cc5f2b57e3a9256..08e199869ad16c3b19d998a2a28eae9a0dd0a371 100644
e543c9
--- a/src/providers/ldap/sdap_async_nested_groups.c
e543c9
+++ b/src/providers/ldap/sdap_async_nested_groups.c
e543c9
@@ -240,32 +240,21 @@ sdap_nested_group_hash_group(struct sdap_nested_group_ctx *group_ctx,
e543c9
 {
e543c9
     struct sdap_attr_map *map = group_ctx->opts->group_map;
e543c9
     gid_t gid;
e543c9
-    errno_t ret = ENOENT;
e543c9
-    int32_t ad_group_type;
e543c9
+    errno_t ret;
e543c9
     bool posix_group = true;
e543c9
     bool use_id_mapping;
e543c9
     bool can_find_gid;
e543c9
+    bool need_filter;
e543c9
 
e543c9
-    if (group_ctx->opts->schema_type == SDAP_SCHEMA_AD) {
e543c9
-        ret = sysdb_attrs_get_int32_t(group, SYSDB_GROUP_TYPE, &ad_group_type);
e543c9
-        if (ret != EOK) {
e543c9
-            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_int32_t failed.\n");
e543c9
-            return ret;
e543c9
-        }
e543c9
+    ret = sdap_check_ad_group_type(group_ctx->domain, group_ctx->opts,
e543c9
+                                   group, "", &need_filter);
e543c9
+    if (ret != EOK) {
e543c9
+        return ret;
e543c9
+    }
e543c9
 
e543c9
-        DEBUG(SSSDBG_TRACE_ALL, "AD group has type flags %#x.\n",
e543c9
-                                 ad_group_type);
e543c9
-        /* Only security groups from AD are considered for POSIX groups.
e543c9
-         * Additionally only global and universal group are taken to account
e543c9
-         * for trusted domains. */
e543c9
-        if (!(ad_group_type & SDAP_AD_GROUP_TYPE_SECURITY)
e543c9
-                || (IS_SUBDOMAIN(group_ctx->domain)
e543c9
-                    && (!((ad_group_type & SDAP_AD_GROUP_TYPE_GLOBAL)
e543c9
-                        || (ad_group_type & SDAP_AD_GROUP_TYPE_UNIVERSAL))))) {
e543c9
-            posix_group = false;
e543c9
-            gid = 0;
e543c9
-            DEBUG(SSSDBG_TRACE_FUNC, "Filtering AD group.\n");
e543c9
-        }
e543c9
+    if (need_filter) {
e543c9
+        posix_group = false;
e543c9
+        gid = 0;
e543c9
     }
e543c9
 
e543c9
     use_id_mapping = sdap_idmap_domain_has_algorithmic_mapping(
e543c9
diff --git a/src/providers/ldap/sdap_async_private.h b/src/providers/ldap/sdap_async_private.h
e543c9
index 3995a2ac357c52f546696284d71d2127d0302409..db542eaf869efcd53d0937bef3fc6e99cc78b938 100644
e543c9
--- a/src/providers/ldap/sdap_async_private.h
e543c9
+++ b/src/providers/ldap/sdap_async_private.h
e543c9
@@ -138,4 +138,11 @@ errno_t sdap_add_incomplete_groups(struct sysdb_ctx *sysdb,
e543c9
                                    char **groupnames,
e543c9
                                    struct sysdb_attrs **ldap_groups,
e543c9
                                    int ldap_groups_count);
e543c9
+
e543c9
+/* from sdap_async_nested_groups.c */
e543c9
+errno_t sdap_check_ad_group_type(struct sss_domain_info *dom,
e543c9
+                                 struct sdap_options *opts,
e543c9
+                                 struct sysdb_attrs *group_attrs,
e543c9
+                                 const char *group_name,
e543c9
+                                 bool *_need_filter);
e543c9
 #endif /* _SDAP_ASYNC_PRIVATE_H_ */
e543c9
-- 
e543c9
2.1.0
e543c9