>From 0fa8b678e03fb99f73ffc1684092360ffa54f781 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Thu, 19 Feb 2015 12:10:23 +0100
Subject: [PATCH 6/8] be_refresh: support groups

Resolves:
https://fedorahosted.org/sssd/ticket/2346
---
 src/db/sysdb.c                    |  8 ++++++++
 src/db/sysdb.h                    |  2 ++
 src/providers/dp_refresh.c        | 23 +++++++++++++++++++++++
 src/providers/dp_refresh.h        |  1 +
 src/providers/ldap/sdap_refresh.c | 29 +++++++++++++++++++++++++++++
 5 files changed, 63 insertions(+)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index 2bb4a41aa4a9e6201ac27ac8d9a1803c1fb5c43e..6be5e8026c6d24c03f133242a62acec759a24780 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -198,6 +198,14 @@ struct ldb_dn *sysdb_group_dn(TALLOC_CTX *mem_ctx,
     return dn;
 }
 
+struct ldb_dn *sysdb_group_base_dn(TALLOC_CTX *mem_ctx,
+                                   struct sss_domain_info *dom)
+{
+    return ldb_dn_new_fmt(mem_ctx, dom->sysdb->ldb,
+                          SYSDB_TMPL_GROUP_BASE, dom->name);
+}
+
+
 struct ldb_dn *sysdb_netgroup_dn(TALLOC_CTX *mem_ctx,
                                  struct sss_domain_info *dom, const char *name)
 {
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 2ee6ee6e6ae7f4b1fe2b0d37ac8b979f4b0126e5..84c84a49f1080fc7c3e8b5a129677598ee6760d8 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -387,6 +387,8 @@ struct ldb_dn *sysdb_user_base_dn(TALLOC_CTX *mem_ctx,
                                   struct sss_domain_info *dom);
 struct ldb_dn *sysdb_group_dn(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
                               const char *name);
+struct ldb_dn *sysdb_group_base_dn(TALLOC_CTX *mem_ctx,
+                                   struct sss_domain_info *dom);
 struct ldb_dn *sysdb_netgroup_dn(TALLOC_CTX *mem_ctx,
                                  struct sss_domain_info *dom, const char *name);
 struct ldb_dn *sysdb_netgroup_base_dn(TALLOC_CTX *mem_ctx,
diff --git a/src/providers/dp_refresh.c b/src/providers/dp_refresh.c
index f599618e074e6c0ef29363f014ec0ec422de3a55..169ef71edfa4aaf59a199bd1f005f62323e58b87 100644
--- a/src/providers/dp_refresh.c
+++ b/src/providers/dp_refresh.c
@@ -109,6 +109,26 @@ static errno_t be_refresh_get_users(TALLOC_CTX *mem_ctx,
     return ret;
 }
 
+static errno_t be_refresh_get_groups(TALLOC_CTX *mem_ctx,
+                                     struct sss_domain_info *domain,
+                                     time_t period,
+                                     char ***_values)
+{
+    struct ldb_dn *base_dn = NULL;
+    errno_t ret;
+
+    base_dn = sysdb_group_base_dn(mem_ctx, domain);
+    if (base_dn == NULL) {
+        return ENOMEM;
+    }
+
+    ret = be_refresh_get_values(mem_ctx, domain, period, SYSDB_GROUP_CLASS,
+                                base_dn, SYSDB_NAME, _values);
+
+    talloc_free(base_dn);
+    return ret;
+}
+
 static errno_t be_refresh_get_netgroups(TALLOC_CTX *mem_ctx,
                                         struct sss_domain_info *domain,
                                         time_t period,
@@ -161,6 +181,9 @@ struct be_refresh_ctx *be_refresh_ctx_init(TALLOC_CTX *mem_ctx)
     ctx->callbacks[BE_REFRESH_TYPE_USERS].name = "users";
     ctx->callbacks[BE_REFRESH_TYPE_USERS].get_values = be_refresh_get_users;
 
+    ctx->callbacks[BE_REFRESH_TYPE_GROUPS].name = "groups";
+    ctx->callbacks[BE_REFRESH_TYPE_GROUPS].get_values = be_refresh_get_groups;
+
     ctx->callbacks[BE_REFRESH_TYPE_NETGROUPS].name = "netgroups";
     ctx->callbacks[BE_REFRESH_TYPE_NETGROUPS].get_values \
         = be_refresh_get_netgroups;
diff --git a/src/providers/dp_refresh.h b/src/providers/dp_refresh.h
index a0d42734b64e16184a3a2cdfe45057003ddec6cc..89ccfcfb49c4cee18d3fa0f83ce6314a49ee1302 100644
--- a/src/providers/dp_refresh.h
+++ b/src/providers/dp_refresh.h
@@ -45,6 +45,7 @@ typedef errno_t
 
 enum be_refresh_type {
     BE_REFRESH_TYPE_USERS,
+    BE_REFRESH_TYPE_GROUPS,
     BE_REFRESH_TYPE_NETGROUPS,
     BE_REFRESH_TYPE_SENTINEL
 };
diff --git a/src/providers/ldap/sdap_refresh.c b/src/providers/ldap/sdap_refresh.c
index 8fff9d49297b4efe3f4673003859242dd9192070..bda50aeb3e44510f3afc1a10cb09336a6eec26ec 100644
--- a/src/providers/ldap/sdap_refresh.c
+++ b/src/providers/ldap/sdap_refresh.c
@@ -78,6 +78,9 @@ static struct tevent_req *sdap_refresh_send(TALLOC_CTX *mem_ctx,
     case BE_REQ_USER:
         state->type = "user";
         break;
+    case BE_REQ_GROUP:
+        state->type = "group";
+        break;
     case BE_REQ_NETGROUP:
         state->type = "netgroup";
         break;
@@ -220,6 +223,23 @@ static errno_t sdap_refresh_users_recv(struct tevent_req *req)
 }
 
 static struct tevent_req *
+sdap_refresh_groups_send(TALLOC_CTX *mem_ctx,
+                         struct tevent_context *ev,
+                         struct be_ctx *be_ctx,
+                         struct sss_domain_info *domain,
+                         char **names,
+                         void *pvt)
+{
+    return sdap_refresh_send(mem_ctx, ev, be_ctx, domain,
+                             BE_REQ_GROUP, names, pvt);
+}
+
+static errno_t sdap_refresh_groups_recv(struct tevent_req *req)
+{
+    return sdap_refresh_recv(req);
+}
+
+static struct tevent_req *
 sdap_refresh_netgroups_send(TALLOC_CTX *mem_ctx,
                             struct tevent_context *ev,
                             struct be_ctx *be_ctx,
@@ -250,6 +270,15 @@ errno_t sdap_refresh_init(struct be_refresh_ctx *refresh_ctx,
               "will not work [%d]: %s\n", ret, strerror(ret));
     }
 
+    ret = be_refresh_add_cb(refresh_ctx, BE_REFRESH_TYPE_GROUPS,
+                            sdap_refresh_groups_send,
+                            sdap_refresh_groups_recv,
+                            id_ctx);
+    if (ret != EOK && ret != EEXIST) {
+        DEBUG(SSSDBG_MINOR_FAILURE, "Periodical refresh of groups "
+              "will not work [%d]: %s\n", ret, strerror(ret));
+    }
+
     ret = be_refresh_add_cb(refresh_ctx, BE_REFRESH_TYPE_NETGROUPS,
                             sdap_refresh_netgroups_send,
                             sdap_refresh_netgroups_recv,
-- 
1.9.3

