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

Resolves:
https://fedorahosted.org/sssd/ticket/2150
---
 src/responder/ifp/ifp_groups.c                     | 162 +++++++++++++++++++++
 src/responder/ifp/ifp_groups.h                     |  20 +++
 src/responder/ifp/ifp_iface.c                      |  10 ++
 src/responder/ifp/ifp_iface.xml                    |  24 +++
 src/responder/ifp/ifp_iface_generated.c            | 121 +++++++++++++++
 src/responder/ifp/ifp_iface_generated.h            |  31 ++++
 .../ifp/org.freedesktop.sssd.infopipe.conf         |   1 +
 7 files changed, 369 insertions(+)

diff --git a/src/responder/ifp/ifp_groups.c b/src/responder/ifp/ifp_groups.c
index 533e6806df11f4b569fca0e7a78694c3cd576001..e362002d35b6aff6f35f71c0dfc37015a2fc89e5 100644
--- a/src/responder/ifp/ifp_groups.c
+++ b/src/responder/ifp/ifp_groups.c
@@ -19,8 +19,13 @@
 */
 
 #include <talloc.h>
+#include <tevent.h>
 
+#include "util/util.h"
 #include "db/sysdb.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"
 
 char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
@@ -37,3 +42,160 @@ char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
 
     return sbus_opath_compose(mem_ctx, IFP_PATH_GROUPS, domain->name, gid);
 }
+
+static void ifp_groups_find_by_name_done(struct tevent_req *req);
+
+int ifp_groups_find_by_name(struct sbus_request *sbus_req,
+                           void *data,
+                           const char *name)
+{
+    struct ifp_ctx *ctx;
+    struct tevent_req *req;
+
+    ctx = talloc_get_type(data, struct ifp_ctx);
+    if (ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
+        return ERR_INTERNAL;
+    }
+
+    req = cache_req_group_by_name_send(sbus_req, ctx->rctx->ev, ctx->rctx,
+                                       ctx->ncache, ctx->neg_timeout, 0,
+                                       NULL, name);
+    if (req == NULL) {
+        return ENOMEM;
+    }
+
+    tevent_req_set_callback(req, ifp_groups_find_by_name_done, sbus_req);
+
+    return EOK;
+}
+
+static void
+ifp_groups_find_by_name_done(struct tevent_req *req)
+{
+    DBusError *error;
+    struct sbus_request *sbus_req;
+    struct sss_domain_info *domain;
+    struct ldb_result *result;
+    char *object_path;
+    errno_t ret;
+
+    sbus_req = tevent_req_callback_data(req, struct sbus_request);
+
+    ret = cache_req_group_by_name_recv(sbus_req, req, &result, &domain, NULL);
+    talloc_zfree(req);
+    if (ret == ENOENT) {
+        error = sbus_error_new(sbus_req, SBUS_ERROR_NOT_FOUND,
+                               "Group not found");
+        goto done;
+    } else if (ret != EOK) {
+        error = sbus_error_new(sbus_req, DBUS_ERROR_FAILED, "Failed to fetch "
+                               "group [%d]: %s\n", ret, sss_strerror(ret));
+        goto done;
+    }
+
+    object_path = ifp_groups_build_path_from_msg(sbus_req, domain,
+                                                 result->msgs[0]);
+    if (object_path == NULL) {
+        error = sbus_error_new(sbus_req, SBUS_ERROR_INTERNAL,
+                               "Failed to compose object path");
+        goto done;
+    }
+
+    ret = EOK;
+
+done:
+    if (ret != EOK) {
+        sbus_request_fail_and_finish(sbus_req, error);
+        return;
+    }
+
+    iface_ifp_groups_FindByName_finish(sbus_req, object_path);
+    return;
+}
+
+static void ifp_groups_find_by_id_done(struct tevent_req *req);
+
+int ifp_groups_find_by_id(struct sbus_request *sbus_req,
+                          void *data,
+                          uint32_t id)
+{
+    struct ifp_ctx *ctx;
+    struct tevent_req *req;
+
+    ctx = talloc_get_type(data, struct ifp_ctx);
+    if (ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
+        return ERR_INTERNAL;
+    }
+
+    req = cache_req_group_by_id_send(sbus_req, ctx->rctx->ev, ctx->rctx,
+                                     ctx->ncache, ctx->neg_timeout, 0,
+                                     NULL, id);
+    if (req == NULL) {
+        return ENOMEM;
+    }
+
+    tevent_req_set_callback(req, ifp_groups_find_by_id_done, sbus_req);
+
+    return EOK;
+}
+
+static void
+ifp_groups_find_by_id_done(struct tevent_req *req)
+{
+    DBusError *error;
+    struct sbus_request *sbus_req;
+    struct sss_domain_info *domain;
+    struct ldb_result *result;
+    char *object_path;
+    errno_t ret;
+
+    sbus_req = tevent_req_callback_data(req, struct sbus_request);
+
+    ret = cache_req_group_by_id_recv(sbus_req, req, &result, &domain);
+    talloc_zfree(req);
+    if (ret == ENOENT) {
+        error = sbus_error_new(sbus_req, SBUS_ERROR_NOT_FOUND,
+                               "Group not found");
+        goto done;
+    } else if (ret != EOK) {
+        error = sbus_error_new(sbus_req, DBUS_ERROR_FAILED, "Failed to fetch "
+                               "group [%d]: %s\n", ret, sss_strerror(ret));
+        goto done;
+    }
+
+    object_path = ifp_groups_build_path_from_msg(sbus_req, domain,
+                                                 result->msgs[0]);
+    if (object_path == NULL) {
+        error = sbus_error_new(sbus_req, SBUS_ERROR_INTERNAL,
+                               "Failed to compose object path");
+        goto done;
+    }
+
+done:
+    if (ret != EOK) {
+        sbus_request_fail_and_finish(sbus_req, error);
+        return;
+    }
+
+    iface_ifp_groups_FindByID_finish(sbus_req, object_path);
+    return;
+}
+
+int ifp_groups_list_by_name(struct sbus_request *sbus_req,
+                            void *data,
+                            const char *filter,
+                            uint32_t limit)
+{
+    return EOK;
+}
+
+int ifp_groups_list_by_domain_and_name(struct sbus_request *sbus_req,
+                                       void *data,
+                                       const char *domain,
+                                       const char *filter,
+                                       uint32_t limit)
+{
+    return EOK;
+}
diff --git a/src/responder/ifp/ifp_groups.h b/src/responder/ifp/ifp_groups.h
index 1bdaf3e0c21e5ca53d9839264f87b8d10d3afbf1..b93757326d2db31f80ffc44797cd70fbcd97995f 100644
--- a/src/responder/ifp/ifp_groups.h
+++ b/src/responder/ifp/ifp_groups.h
@@ -33,5 +33,25 @@ char * ifp_groups_build_path_from_msg(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *domain,
                                       struct ldb_message *msg);
 
+/* org.freedesktop.sssd.infopipe.Groups */
+
+int ifp_groups_find_by_name(struct sbus_request *sbus_req,
+                            void *data,
+                            const char *name);
+
+int ifp_groups_find_by_id(struct sbus_request *sbus_req,
+                          void *data,
+                          uint32_t id);
+
+int ifp_groups_list_by_name(struct sbus_request *sbus_req,
+                            void *data,
+                            const char *filter,
+                            uint32_t limit);
+
+int ifp_groups_list_by_domain_and_name(struct sbus_request *sbus_req,
+                                       void *data,
+                                       const char *domain,
+                                       const char *filter,
+                                       uint32_t limit);
 
 #endif /* IFP_GROUPS_H_ */
diff --git a/src/responder/ifp/ifp_iface.c b/src/responder/ifp/ifp_iface.c
index 1878263c2b4dd20b4d3f5931d56eb39e952bad50..e762ffa2833a0cf43ba0a96a23054e599a52ba74 100644
--- a/src/responder/ifp/ifp_iface.c
+++ b/src/responder/ifp/ifp_iface.c
@@ -25,6 +25,7 @@
 #include "responder/ifp/ifp_domains.h"
 #include "responder/ifp/ifp_components.h"
 #include "responder/ifp/ifp_users.h"
+#include "responder/ifp/ifp_groups.h"
 
 struct iface_ifp iface_ifp = {
     { &iface_ifp_meta, 0 },
@@ -98,6 +99,14 @@ struct iface_ifp_users_user iface_ifp_users_user = {
     .get_extraAttributes = ifp_users_user_get_extra_attributes
 };
 
+struct iface_ifp_groups iface_ifp_groups = {
+    { &iface_ifp_groups_meta, 0 },
+    .FindByName = ifp_groups_find_by_name,
+    .FindByID = ifp_groups_find_by_id,
+    .ListByName = ifp_groups_list_by_name,
+    .ListByDomainAndName = ifp_groups_list_by_domain_and_name
+};
+
 struct iface_map {
     const char *path;
     struct sbus_vtable *vtable;
@@ -109,6 +118,7 @@ static struct iface_map iface_map[] = {
     { IFP_PATH_COMPONENTS_TREE, &iface_ifp_components.vtable },
     { IFP_PATH_USERS, &iface_ifp_users.vtable },
     { IFP_PATH_USERS_TREE, &iface_ifp_users_user.vtable },
+    { IFP_PATH_GROUPS, &iface_ifp_groups.vtable },
     { NULL, NULL },
 };
 
diff --git a/src/responder/ifp/ifp_iface.xml b/src/responder/ifp/ifp_iface.xml
index 2c15fddea678b87acc7591442c4e0190da0abaee..31fb529fe650d1f2776ca648943acab49be10914 100644
--- a/src/responder/ifp/ifp_iface.xml
+++ b/src/responder/ifp/ifp_iface.xml
@@ -140,4 +140,28 @@
         <property name="groups" type="ao" access="read" />
         <property name="extraAttributes" type="a{sas}" access="read" />
     </interface>
+
+    <interface name="org.freedesktop.sssd.infopipe.Groups">
+        <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="iface_ifp_groups"/>
+
+        <method name="FindByName">
+            <arg name="name" type="s" direction="in" />
+            <arg name="result" type="o" direction="out" />
+        </method>
+        <method name="FindByID">
+            <arg name="id" type="u" direction="in" />
+            <arg name="result" type="o" direction="out" />
+        </method>
+        <method name="ListByName">
+            <arg name="name_filter" type="s" direction="in" />
+            <arg name="limit" type="u" direction="in" />
+            <arg name="result" type="ao" direction="out" />
+        </method>
+        <method name="ListByDomainAndName">
+            <arg name="domain_name" type="s" direction="in" />
+            <arg name="name_filter" type="s" direction="in" />
+            <arg name="limit" type="u" direction="in" />
+            <arg name="result" type="ao" direction="out"/>
+        </method>
+    </interface>
 </node>
diff --git a/src/responder/ifp/ifp_iface_generated.c b/src/responder/ifp/ifp_iface_generated.c
index 1f6c7ba1d1e884dc2be5124b78fc4c775ce17ffe..355db6e84d7ee439bde0a23656c9600da4005778 100644
--- a/src/responder/ifp/ifp_iface_generated.c
+++ b/src/responder/ifp/ifp_iface_generated.c
@@ -756,6 +756,127 @@ const struct sbus_interface_meta iface_ifp_users_user_meta = {
     sbus_invoke_get_all, /* GetAll invoker */
 };
 
+/* arguments for org.freedesktop.sssd.infopipe.Groups.FindByName */
+const struct sbus_arg_meta iface_ifp_groups_FindByName__in[] = {
+    { "name", "s" },
+    { NULL, }
+};
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.FindByName */
+const struct sbus_arg_meta iface_ifp_groups_FindByName__out[] = {
+    { "result", "o" },
+    { NULL, }
+};
+
+int iface_ifp_groups_FindByName_finish(struct sbus_request *req, const char *arg_result)
+{
+   return sbus_request_return_and_finish(req,
+                                         DBUS_TYPE_OBJECT_PATH, &arg_result,
+                                         DBUS_TYPE_INVALID);
+}
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.FindByID */
+const struct sbus_arg_meta iface_ifp_groups_FindByID__in[] = {
+    { "id", "u" },
+    { NULL, }
+};
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.FindByID */
+const struct sbus_arg_meta iface_ifp_groups_FindByID__out[] = {
+    { "result", "o" },
+    { NULL, }
+};
+
+int iface_ifp_groups_FindByID_finish(struct sbus_request *req, const char *arg_result)
+{
+   return sbus_request_return_and_finish(req,
+                                         DBUS_TYPE_OBJECT_PATH, &arg_result,
+                                         DBUS_TYPE_INVALID);
+}
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.ListByName */
+const struct sbus_arg_meta iface_ifp_groups_ListByName__in[] = {
+    { "name_filter", "s" },
+    { "limit", "u" },
+    { NULL, }
+};
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.ListByName */
+const struct sbus_arg_meta iface_ifp_groups_ListByName__out[] = {
+    { "result", "ao" },
+    { NULL, }
+};
+
+int iface_ifp_groups_ListByName_finish(struct sbus_request *req, const char *arg_result[], int len_result)
+{
+   return sbus_request_return_and_finish(req,
+                                         DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &arg_result, len_result,
+                                         DBUS_TYPE_INVALID);
+}
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.ListByDomainAndName */
+const struct sbus_arg_meta iface_ifp_groups_ListByDomainAndName__in[] = {
+    { "domain_name", "s" },
+    { "name_filter", "s" },
+    { "limit", "u" },
+    { NULL, }
+};
+
+/* arguments for org.freedesktop.sssd.infopipe.Groups.ListByDomainAndName */
+const struct sbus_arg_meta iface_ifp_groups_ListByDomainAndName__out[] = {
+    { "result", "ao" },
+    { NULL, }
+};
+
+int iface_ifp_groups_ListByDomainAndName_finish(struct sbus_request *req, const char *arg_result[], int len_result)
+{
+   return sbus_request_return_and_finish(req,
+                                         DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &arg_result, len_result,
+                                         DBUS_TYPE_INVALID);
+}
+
+/* methods for org.freedesktop.sssd.infopipe.Groups */
+const struct sbus_method_meta iface_ifp_groups__methods[] = {
+    {
+        "FindByName", /* name */
+        iface_ifp_groups_FindByName__in,
+        iface_ifp_groups_FindByName__out,
+        offsetof(struct iface_ifp_groups, FindByName),
+        invoke_s_method,
+    },
+    {
+        "FindByID", /* name */
+        iface_ifp_groups_FindByID__in,
+        iface_ifp_groups_FindByID__out,
+        offsetof(struct iface_ifp_groups, FindByID),
+        invoke_u_method,
+    },
+    {
+        "ListByName", /* name */
+        iface_ifp_groups_ListByName__in,
+        iface_ifp_groups_ListByName__out,
+        offsetof(struct iface_ifp_groups, ListByName),
+        invoke_su_method,
+    },
+    {
+        "ListByDomainAndName", /* name */
+        iface_ifp_groups_ListByDomainAndName__in,
+        iface_ifp_groups_ListByDomainAndName__out,
+        offsetof(struct iface_ifp_groups, ListByDomainAndName),
+        invoke_ssu_method,
+    },
+    { NULL, }
+};
+
+/* interface info for org.freedesktop.sssd.infopipe.Groups */
+const struct sbus_interface_meta iface_ifp_groups_meta = {
+    "org.freedesktop.sssd.infopipe.Groups", /* name */
+    iface_ifp_groups__methods,
+    NULL, /* no signals */
+    NULL, /* no 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 609bd40e6593a3f893774cafbe166e7b7bfd67d0..09db24b7319fbb3823dd742233e9dd55965511c1 100644
--- a/src/responder/ifp/ifp_iface_generated.h
+++ b/src/responder/ifp/ifp_iface_generated.h
@@ -73,6 +73,13 @@
 #define IFACE_IFP_USERS_USER_GROUPS "groups"
 #define IFACE_IFP_USERS_USER_EXTRAATTRIBUTES "extraAttributes"
 
+/* constants for org.freedesktop.sssd.infopipe.Groups */
+#define IFACE_IFP_GROUPS "org.freedesktop.sssd.infopipe.Groups"
+#define IFACE_IFP_GROUPS_FINDBYNAME "FindByName"
+#define IFACE_IFP_GROUPS_FINDBYID "FindByID"
+#define IFACE_IFP_GROUPS_LISTBYNAME "ListByName"
+#define IFACE_IFP_GROUPS_LISTBYDOMAINANDNAME "ListByDomainAndName"
+
 /* ------------------------------------------------------------------------
  * DBus handlers
  *
@@ -217,6 +224,27 @@ struct iface_ifp_users_user {
 /* finish function for UpdateGroupsList */
 int iface_ifp_users_user_UpdateGroupsList_finish(struct sbus_request *req);
 
+/* vtable for org.freedesktop.sssd.infopipe.Groups */
+struct iface_ifp_groups {
+    struct sbus_vtable vtable; /* derive from sbus_vtable */
+    int (*FindByName)(struct sbus_request *req, void *data, const char *arg_name);
+    int (*FindByID)(struct sbus_request *req, void *data, uint32_t arg_id);
+    int (*ListByName)(struct sbus_request *req, void *data, const char *arg_name_filter, uint32_t arg_limit);
+    int (*ListByDomainAndName)(struct sbus_request *req, void *data, const char *arg_domain_name, const char *arg_name_filter, uint32_t arg_limit);
+};
+
+/* finish function for FindByName */
+int iface_ifp_groups_FindByName_finish(struct sbus_request *req, const char *arg_result);
+
+/* finish function for FindByID */
+int iface_ifp_groups_FindByID_finish(struct sbus_request *req, const char *arg_result);
+
+/* finish function for ListByName */
+int iface_ifp_groups_ListByName_finish(struct sbus_request *req, const char *arg_result[], int len_result);
+
+/* finish function for ListByDomainAndName */
+int iface_ifp_groups_ListByDomainAndName_finish(struct sbus_request *req, const char *arg_result[], int len_result);
+
 /* ------------------------------------------------------------------------
  * DBus Interface Metadata
  *
@@ -242,4 +270,7 @@ extern const struct sbus_interface_meta iface_ifp_users_meta;
 /* interface info for org.freedesktop.sssd.infopipe.Users.User */
 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;
+
 #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 79160bbcc2800cfb98dfa79dbf5e445f2e42a6e0..916218c8b70d3989a0e619d4f50080c0dd9521bd 100644
--- a/src/responder/ifp/org.freedesktop.sssd.infopipe.conf
+++ b/src/responder/ifp/org.freedesktop.sssd.infopipe.conf
@@ -32,6 +32,7 @@
     <allow send_interface="org.freedesktop.sssd.infopipe"/>
     <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"/>
   </policy>
 
   <policy user="root">
-- 
2.1.0

