From a4c1f316f7c434e0e20f457287dda01a96a888c0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 10 Feb 2015 12:19:19 +0100
Subject: [PATCH 7/8] IFP: add org.freedesktop.sssd.infopipe.Groups.Group

Resolves:
https://fedorahosted.org/sssd/ticket/2150
---
 src/responder/ifp/ifp_groups.c                     | 311 +++++++++++++++++++++
 src/responder/ifp/ifp_groups.h                     |  20 ++
 src/responder/ifp/ifp_iface.c                      |   9 +
 src/responder/ifp/ifp_iface.xml                    |   9 +
 src/responder/ifp/ifp_iface_generated.c            |  50 ++++
 src/responder/ifp/ifp_iface_generated.h            |  19 ++
 .../ifp/org.freedesktop.sssd.infopipe.conf         |   1 +
 7 files changed, 419 insertions(+)

diff --git a/src/responder/ifp/ifp_groups.c b/src/responder/ifp/ifp_groups.c
index e362002d35b6aff6f35f71c0dfc37015a2fc89e5..2ca51f8349f01343c30f0987ccad2b070871fa0a 100644
--- a/src/responder/ifp/ifp_groups.c
+++ b/src/responder/ifp/ifp_groups.c
@@ -23,10 +23,12 @@
 
 #include "util/util.h"
 #include "db/sysdb.h"
+#include "util/strtonum.h"
 #include "sbus/sssd_dbus_errors.h"
 #include "responder/common/responder.h"
 #include "responder/common/responder_cache_req.h"
 #include "responder/ifp/ifp_groups.h"
+#include "responder/ifp/ifp_users.h"
 
 char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *domain,
@@ -43,6 +45,41 @@ char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
     return sbus_opath_compose(mem_ctx, IFP_PATH_GROUPS, domain->name, gid);
 }
 
+static errno_t ifp_groups_decompose_path(struct sss_domain_info *domains,
+                                         const char *path,
+                                         struct sss_domain_info **_domain,
+                                         gid_t *_gid)
+{
+    char **parts = NULL;
+    struct sss_domain_info *domain;
+    gid_t gid;
+    errno_t ret;
+
+    ret = sbus_opath_decompose_exact(NULL, path, IFP_PATH_GROUPS, 2, &parts);
+    if (ret != EOK) {
+        return ret;
+    }
+
+    domain = find_domain_by_name(domains, parts[0], false);
+    if (domain == NULL) {
+        ret = ERR_DOMAIN_NOT_FOUND;
+        goto done;
+    }
+
+    gid = strtouint32(parts[1], NULL, 10);
+    if (errno != 0) {
+        ret = errno;
+        goto done;
+    }
+
+    *_domain = domain;
+    *_gid = gid;
+
+done:
+    talloc_free(parts);
+    return ret;
+}
+
 static void ifp_groups_find_by_name_done(struct tevent_req *req);
 
 int ifp_groups_find_by_name(struct sbus_request *sbus_req,
@@ -199,3 +236,277 @@ int ifp_groups_list_by_domain_and_name(struct sbus_request *sbus_req,
 {
     return EOK;
 }
+
+static errno_t
+ifp_groups_group_get(struct sbus_request *sbus_req,
+                     void *data,
+                     gid_t *_gid,
+                     struct sss_domain_info **_domain,
+                     struct ldb_message **_group)
+{
+    struct ifp_ctx *ctx;
+    struct sss_domain_info *domain;
+    struct ldb_result *res;
+    uid_t gid;
+    errno_t ret;
+
+    ctx = talloc_get_type(data, struct ifp_ctx);
+    if (ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
+        return ERR_INTERNAL;
+    }
+
+    ret = ifp_groups_decompose_path(ctx->rctx->domains, sbus_req->path,
+                                    &domain, &gid);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to decompose object path"
+              "[%s] [%d]: %s\n", sbus_req->path, ret, sss_strerror(ret));
+        return ret;
+    }
+
+    if (_group != NULL) {
+        ret = sysdb_getgrgid_with_views(sbus_req, domain, gid, &res);
+        if (ret == EOK && res->count == 0) {
+            *_group = NULL;
+            ret = ENOENT;
+        }
+
+        if (ret != EOK) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to lookup group %u@%s [%d]: %s\n",
+                  gid, domain->name, ret, sss_strerror(ret));
+        } else {
+            *_group = res->msgs[0];
+        }
+    }
+
+    if (ret == EOK || ret == ENOENT) {
+        if (_gid != NULL) {
+            *_gid = gid;
+        }
+
+        if (_domain != NULL) {
+            *_domain = domain;
+        }
+    }
+
+    return ret;
+}
+
+void ifp_groups_group_get_name(struct sbus_request *sbus_req,
+                               void *data,
+                               const char **_out)
+{
+    struct ldb_message *msg;
+    errno_t ret;
+
+    ret = ifp_groups_group_get(sbus_req, data, NULL, NULL, &msg);
+    if (ret != EOK) {
+        *_out = NULL;
+        return;
+    }
+
+    *_out = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
+
+    return;
+}
+
+void ifp_groups_group_get_gid_number(struct sbus_request *sbus_req,
+                                     void *data,
+                                     uint32_t *_out)
+{
+    struct ldb_message *msg;
+    errno_t ret;
+
+    ret = ifp_groups_group_get(sbus_req, data, NULL, NULL, &msg);
+    if (ret != EOK) {
+        *_out = 0;
+        return;
+    }
+
+    *_out = ldb_msg_find_attr_as_uint(msg, SYSDB_GIDNUM, 0);
+
+    return;
+}
+
+static errno_t
+ifp_groups_group_get_members(TALLOC_CTX *mem_ctx,
+                             struct sbus_request *sbus_req,
+                             void *data,
+                             const char ***_users,
+                             int *_users_count,
+                             const char ***_groups,
+                             int *_groups_count)
+{
+    TALLOC_CTX *tmp_ctx;
+    struct ldb_message *msg;
+    struct ldb_message_element *el;
+    struct sss_domain_info *domain;
+    const char *class;
+    const char **users;
+    int users_count;
+    const char **groups;
+    int groups_count;
+    const char *dn;
+    struct ldb_dn *ldb_dn;
+    size_t count;
+    struct ldb_message **entry;
+    errno_t ret;
+    int i;
+    const char *attrs[] = {SYSDB_OBJECTCLASS, SYSDB_UIDNUM, SYSDB_GIDNUM, NULL};
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        return ENOMEM;
+    }
+
+    ret = ifp_groups_group_get(sbus_req, data, NULL, &domain, &msg);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    el = ldb_msg_find_element(msg, SYSDB_MEMBER);
+    if (el == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get member attribute!\n");
+        ret = ERR_INTERNAL;
+        goto done;
+    }
+
+    if (el->num_values == 0) {
+        *_users = NULL;
+        *_groups = NULL;
+        *_users_count = 0;
+        *_groups_count = 0;
+        ret = EOK;
+        goto done;
+    }
+
+    users = talloc_zero_array(tmp_ctx, const char *, el->num_values + 1);
+    if (users == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero_array() failed\n");
+        ret = ENOMEM;
+        goto done;
+    }
+
+    groups = talloc_zero_array(tmp_ctx, const char *, el->num_values + 1);
+    if (groups == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero_array() failed\n");
+        ret = ENOMEM;
+        goto done;
+    }
+
+    users_count = 0;
+    groups_count = 0;
+    for (i = 0; i < el->num_values; i++) {
+        dn = (const char *)el->values[i].data;
+        ldb_dn = ldb_dn_new(sbus_req, sysdb_ctx_get_ldb(domain->sysdb), dn);
+        if (ldb_dn == NULL) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "ldb_dn_new() failed\n");
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_search_entry(tmp_ctx, domain->sysdb, ldb_dn, LDB_SCOPE_BASE,
+                                 NULL, attrs, &count, &entry);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to lookup dn %s [%d]: %s\n",
+                  dn, ret, sss_strerror(ret));
+            goto done;
+        }
+
+        if (count == 0) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "No entry returned?\n");
+            ret = ERR_INTERNAL;
+            goto done;
+        } else if (count != 1) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "More than one entry returned?\n");
+            ret = ERR_INTERNAL;
+            goto done;
+        }
+
+        class = ldb_msg_find_attr_as_string(entry[0], SYSDB_OBJECTCLASS, NULL);
+        if (class == NULL) {
+            ret = ERR_INTERNAL;
+            goto done;
+        }
+
+        if (strcmp(class, SYSDB_USER_CLASS) == 0) {
+            users[users_count] = ifp_users_build_path_from_msg(users, domain,
+                                                               entry[0]);
+            if (users[users_count] == NULL) {
+                ret = ENOMEM;
+                goto done;
+            }
+
+            users_count++;
+        } else if (strcmp(class, SYSDB_GROUP_CLASS) == 0) {
+            groups[groups_count] = ifp_groups_build_path_from_msg(groups,
+                                                             domain, entry[0]);
+            if (groups[groups_count] == NULL) {
+                ret = ENOMEM;
+                goto done;
+            }
+
+            groups_count++;
+        } else {
+            DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected object class %s\n", class);
+            ret = ERR_INTERNAL;
+            goto done;
+        }
+    }
+
+    if (_users != NULL) {
+        *_users = talloc_steal(mem_ctx, users);
+    }
+
+    if (_users_count != NULL) {
+        *_users_count = users_count;
+    }
+
+    if (_groups != NULL) {
+        *_groups = talloc_steal(mem_ctx, groups);
+    }
+
+    if (_groups_count != NULL) {
+        *_groups_count = groups_count;
+    }
+
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
+void ifp_groups_group_get_users(struct sbus_request *sbus_req,
+                                void *data,
+                                const char ***_out,
+                                int *_size)
+{
+    errno_t ret;
+
+    *_out = NULL;
+    *_size = 0;
+
+    ret = ifp_groups_group_get_members(sbus_req, sbus_req, data, _out, _size,
+                                       NULL, NULL);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to acquire groups members\n");
+    }
+}
+
+void ifp_groups_group_get_groups(struct sbus_request *sbus_req,
+                                void *data,
+                                const char ***_out,
+                                int *_size)
+{
+    errno_t ret;
+
+    *_out = NULL;
+    *_size = 0;
+
+    ret = ifp_groups_group_get_members(sbus_req, sbus_req, data, NULL, NULL,
+                                       _out, _size);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to acquire groups members\n");
+    }
+}
diff --git a/src/responder/ifp/ifp_groups.h b/src/responder/ifp/ifp_groups.h
index b93757326d2db31f80ffc44797cd70fbcd97995f..2c417c8e47c81b17a46c256c6f45db4f34502d6f 100644
--- a/src/responder/ifp/ifp_groups.h
+++ b/src/responder/ifp/ifp_groups.h
@@ -54,4 +54,24 @@ int ifp_groups_list_by_domain_and_name(struct sbus_request *sbus_req,
                                        const char *filter,
                                        uint32_t limit);
 
+/* org.freedesktop.sssd.infopipe.Groups.Group */
+
+void ifp_groups_group_get_name(struct sbus_request *sbus_req,
+                               void *data,
+                               const char **_out);
+
+void ifp_groups_group_get_gid_number(struct sbus_request *sbus_req,
+                                     void *data,
+                                     uint32_t *_out);
+
+void ifp_groups_group_get_users(struct sbus_request *sbus_req,
+                                void *data,
+                                const char ***_out,
+                                int *_size);
+
+void ifp_groups_group_get_groups(struct sbus_request *sbus_req,
+                                void *data,
+                                const char ***_out,
+                                int *_size);
+
 #endif /* IFP_GROUPS_H_ */
diff --git a/src/responder/ifp/ifp_iface.c b/src/responder/ifp/ifp_iface.c
index e762ffa2833a0cf43ba0a96a23054e599a52ba74..3e1ec342b6d1428db55088af1382ba7f6b697e92 100644
--- a/src/responder/ifp/ifp_iface.c
+++ b/src/responder/ifp/ifp_iface.c
@@ -107,6 +107,14 @@ struct iface_ifp_groups iface_ifp_groups = {
     .ListByDomainAndName = ifp_groups_list_by_domain_and_name
 };
 
+struct iface_ifp_groups_group iface_ifp_groups_group = {
+    { &iface_ifp_groups_group_meta, 0 },
+    .get_name = ifp_groups_group_get_name,
+    .get_gidNumber = ifp_groups_group_get_gid_number,
+    .get_users = ifp_groups_group_get_users,
+    .get_groups = ifp_groups_group_get_groups
+};
+
 struct iface_map {
     const char *path;
     struct sbus_vtable *vtable;
@@ -119,6 +127,7 @@ static struct iface_map iface_map[] = {
     { IFP_PATH_USERS, &iface_ifp_users.vtable },
     { IFP_PATH_USERS_TREE, &iface_ifp_users_user.vtable },
     { IFP_PATH_GROUPS, &iface_ifp_groups.vtable },
+    { IFP_PATH_GROUPS_TREE, &iface_ifp_groups_group.vtable },
     { NULL, NULL },
 };
 
diff --git a/src/responder/ifp/ifp_iface.xml b/src/responder/ifp/ifp_iface.xml
index 31fb529fe650d1f2776ca648943acab49be10914..987734258a86d8ece57ba5a44ee4a9c975c0dc05 100644
--- a/src/responder/ifp/ifp_iface.xml
+++ b/src/responder/ifp/ifp_iface.xml
@@ -164,4 +164,13 @@
             <arg name="result" type="ao" direction="out"/>
         </method>
     </interface>
+
+    <interface name="org.freedesktop.sssd.infopipe.Groups.Group">
+        <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="iface_ifp_groups_group"/>
+
+        <property name="name" type="s" access="read" />
+        <property name="gidNumber" type="u" access="read" />
+        <property name="users" type="ao" access="read" />
+        <property name="groups" type="ao" access="read" />
+    </interface>
 </node>
diff --git a/src/responder/ifp/ifp_iface_generated.c b/src/responder/ifp/ifp_iface_generated.c
index 355db6e84d7ee439bde0a23656c9600da4005778..73948ecd262576b83c809b264b92fcf7df1082b8 100644
--- a/src/responder/ifp/ifp_iface_generated.c
+++ b/src/responder/ifp/ifp_iface_generated.c
@@ -877,6 +877,56 @@ const struct sbus_interface_meta iface_ifp_groups_meta = {
     sbus_invoke_get_all, /* GetAll invoker */
 };
 
+/* property info for org.freedesktop.sssd.infopipe.Groups.Group */
+const struct sbus_property_meta iface_ifp_groups_group__properties[] = {
+    {
+        "name", /* name */
+        "s", /* type */
+        SBUS_PROPERTY_READABLE,
+        offsetof(struct iface_ifp_groups_group, get_name),
+        sbus_invoke_get_s,
+        0, /* not writable */
+        NULL, /* no invoker */
+    },
+    {
+        "gidNumber", /* name */
+        "u", /* type */
+        SBUS_PROPERTY_READABLE,
+        offsetof(struct iface_ifp_groups_group, get_gidNumber),
+        sbus_invoke_get_u,
+        0, /* not writable */
+        NULL, /* no invoker */
+    },
+    {
+        "users", /* name */
+        "ao", /* type */
+        SBUS_PROPERTY_READABLE,
+        offsetof(struct iface_ifp_groups_group, get_users),
+        sbus_invoke_get_ao,
+        0, /* not writable */
+        NULL, /* no invoker */
+    },
+    {
+        "groups", /* name */
+        "ao", /* type */
+        SBUS_PROPERTY_READABLE,
+        offsetof(struct iface_ifp_groups_group, get_groups),
+        sbus_invoke_get_ao,
+        0, /* not writable */
+        NULL, /* no invoker */
+    },
+    { NULL, }
+};
+
+/* interface info for org.freedesktop.sssd.infopipe.Groups.Group */
+const struct sbus_interface_meta iface_ifp_groups_group_meta = {
+    "org.freedesktop.sssd.infopipe.Groups.Group", /* name */
+    NULL, /* no methods */
+    NULL, /* no signals */
+    iface_ifp_groups_group__properties,
+    sbus_invoke_get_all, /* GetAll invoker */
+};
+
 /* invokes a handler with a 'ssu' DBus signature */
 static int invoke_ssu_method(struct sbus_request *dbus_req, void *function_ptr)
 {
diff --git a/src/responder/ifp/ifp_iface_generated.h b/src/responder/ifp/ifp_iface_generated.h
index 09db24b7319fbb3823dd742233e9dd55965511c1..53ff336dfe9a5628536a1e6accc762994f44c90d 100644
--- a/src/responder/ifp/ifp_iface_generated.h
+++ b/src/responder/ifp/ifp_iface_generated.h
@@ -80,6 +80,13 @@
 #define IFACE_IFP_GROUPS_LISTBYNAME "ListByName"
 #define IFACE_IFP_GROUPS_LISTBYDOMAINANDNAME "ListByDomainAndName"
 
+/* constants for org.freedesktop.sssd.infopipe.Groups.Group */
+#define IFACE_IFP_GROUPS_GROUP "org.freedesktop.sssd.infopipe.Groups.Group"
+#define IFACE_IFP_GROUPS_GROUP_NAME "name"
+#define IFACE_IFP_GROUPS_GROUP_GIDNUMBER "gidNumber"
+#define IFACE_IFP_GROUPS_GROUP_USERS "users"
+#define IFACE_IFP_GROUPS_GROUP_GROUPS "groups"
+
 /* ------------------------------------------------------------------------
  * DBus handlers
  *
@@ -245,6 +252,15 @@ int iface_ifp_groups_ListByName_finish(struct sbus_request *req, const char *arg
 /* finish function for ListByDomainAndName */
 int iface_ifp_groups_ListByDomainAndName_finish(struct sbus_request *req, const char *arg_result[], int len_result);
 
+/* vtable for org.freedesktop.sssd.infopipe.Groups.Group */
+struct iface_ifp_groups_group {
+    struct sbus_vtable vtable; /* derive from sbus_vtable */
+    void (*get_name)(struct sbus_request *, void *data, const char * *);
+    void (*get_gidNumber)(struct sbus_request *, void *data, uint32_t *);
+    void (*get_users)(struct sbus_request *, void *data, const char * * *, int *);
+    void (*get_groups)(struct sbus_request *, void *data, const char * * *, int *);
+};
+
 /* ------------------------------------------------------------------------
  * DBus Interface Metadata
  *
@@ -273,4 +289,7 @@ extern const struct sbus_interface_meta iface_ifp_users_user_meta;
 /* interface info for org.freedesktop.sssd.infopipe.Groups */
 extern const struct sbus_interface_meta iface_ifp_groups_meta;
 
+/* interface info for org.freedesktop.sssd.infopipe.Groups.Group */
+extern const struct sbus_interface_meta iface_ifp_groups_group_meta;
+
 #endif /* __IFP_IFACE_XML__ */
diff --git a/src/responder/ifp/org.freedesktop.sssd.infopipe.conf b/src/responder/ifp/org.freedesktop.sssd.infopipe.conf
index 916218c8b70d3989a0e619d4f50080c0dd9521bd..b8df49350ecdd119fdde196a21771cae41dc52ef 100644
--- a/src/responder/ifp/org.freedesktop.sssd.infopipe.conf
+++ b/src/responder/ifp/org.freedesktop.sssd.infopipe.conf
@@ -33,6 +33,7 @@
     <allow send_interface="org.freedesktop.sssd.infopipe.Users"/>
     <allow send_interface="org.freedesktop.sssd.infopipe.Users.User"/>
     <allow send_interface="org.freedesktop.sssd.infopipe.Groups"/>
+    <allow send_interface="org.freedesktop.sssd.infopipe.Groups.Group"/>
   </policy>
 
   <policy user="root">
-- 
2.1.0

