From a9d0c396598b767b32dada744603225e517bc845 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                     | 143 +++++++++++++++++++++
 src/responder/ifp/ifp_groups.h                     |  25 ++++
 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, 256 insertions(+)

diff --git a/src/responder/ifp/ifp_groups.c b/src/responder/ifp/ifp_groups.c
index 9a0e2e2244cb3dd8b203cc2858c3be7c6449e988..f1a93a03675e7583c063ae0adfd434e7d27c708c 100644
--- a/src/responder/ifp/ifp_groups.c
+++ b/src/responder/ifp/ifp_groups.c
@@ -23,6 +23,7 @@
 
 #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"
@@ -43,6 +44,41 @@ char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
     return sbus_opath_compose(mem_ctx, IFP_PATH_GROUPS, domain->name, gid);
 }
 
+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,
@@ -191,3 +227,110 @@ 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;
+}
+
+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)
+{
+
+}
diff --git a/src/responder/ifp/ifp_groups.h b/src/responder/ifp/ifp_groups.h
index b93757326d2db31f80ffc44797cd70fbcd97995f..888b3cd6962a86f6d676a2cddc4273ee66d9eb59 100644
--- a/src/responder/ifp/ifp_groups.h
+++ b/src/responder/ifp/ifp_groups.h
@@ -33,6 +33,11 @@ char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *domain,
                                       struct ldb_message *msg);
 
+errno_t ifp_groups_decompose_path(struct sss_domain_info *domains,
+                                  const char *path,
+                                  struct sss_domain_info **_domain,
+                                  gid_t *_gid);
+
 /* org.freedesktop.sssd.infopipe.Groups */
 
 int ifp_groups_find_by_name(struct sbus_request *sbus_req,
@@ -54,4 +59,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 f6e482099a1a9c5dd1a944ad4ee899f7ddc35bd3..f0b0e3aedc9e756ad4d590f423cd19f9e41b3923 100644
--- a/src/responder/ifp/ifp_iface.c
+++ b/src/responder/ifp/ifp_iface.c
@@ -106,6 +106,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;
@@ -118,6 +126,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 4fa150b9a5432f4ead29819c29df0f025e8f9174..5e05ab4b8988f8016d3a219e9e00f7f35d972727 100644
--- a/src/responder/ifp/ifp_iface.xml
+++ b/src/responder/ifp/ifp_iface.xml
@@ -163,4 +163,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 d74c94c37dc26aec51bb8de4cd4a7e6aa462c35a..68ca2a6a6f4110b1d3a7ad116f86c3d040a5a5a0 100644
--- a/src/responder/ifp/ifp_iface_generated.c
+++ b/src/responder/ifp/ifp_iface_generated.c
@@ -868,6 +868,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 ee10963d464756cd555e363284bc4f94f8f0da2c..af478f7d03ef5bedf9c5a24de762745264da64a1 100644
--- a/src/responder/ifp/ifp_iface_generated.h
+++ b/src/responder/ifp/ifp_iface_generated.h
@@ -79,6 +79,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
  *
@@ -243,6 +250,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
  *
@@ -271,4 +287,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

