From 91b02ca0b407bca3928642525f2296e0f4e50def Mon Sep 17 00:00:00 2001 From: Pavel Březina Date: Tue, 11 Feb 2014 17:13:49 +0100 Subject: ListDomains via libdbus-glib --- .gitignore | 1 + src/main.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore index 6222d9d..69c3bbd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/* Debug/* Release/* +/Debug diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b0f18a9 --- /dev/null +++ b/src/main.c @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef int errno_t; +#define EOK 0 + +#define INFOPIPE_DBUS_NAME "org.freedesktop.sssd.infopipe" +#define INFOPIPE_INTERFACE "org.freedesktop.sssd.infopipe" +#define INFOPIPE_PATH "/org/freedesktop/sssd/infopipe" + +#define SSS_METHOD_DOMAIN_LIST "ListDomains" + +#define DBUS_PROP_IFACE "org.freedesktop.DBus.Properties" + +static DBusGProxy* sss_dbus_infopipe_proxy(DBusGConnection *conn) +{ + return dbus_g_proxy_new_for_name(conn, INFOPIPE_DBUS_NAME, + INFOPIPE_PATH, INFOPIPE_INTERFACE); +} + +static DBusGProxy* sss_dbus_property_proxy(DBusGConnection *conn, + const char *object_path) +{ + return dbus_g_proxy_new_for_name(conn, INFOPIPE_DBUS_NAME, + object_path, DBUS_PROP_IFACE); +} + +errno_t sss_dbus_get_string_property(TALLOC_CTX *mem_ctx, + DBusGConnection *conn, + GError *error, + const char *object_path, + const char *property, + char **_out) +{ + DBusGProxy *proxy = NULL; + GValue variant = {0, }; + gboolean gbret; + + proxy = sss_dbus_property_proxy(conn, object_path); + if (proxy == NULL) { + return ENOMEM; + } + + gbret = dbus_g_proxy_call(proxy, "Get", &error, + /* input args */ + G_TYPE_STRING, property, G_TYPE_INVALID, + /* output args */ + G_TYPE_VALUE, &variant, G_TYPE_INVALID); + if (!gbret) { + g_printerr("Failed to issue D-Bus call: %s\n", error->message); + return EIO; + } + + *_out = talloc_strdup(mem_ctx, g_value_get_string(&variant)); + if (*_out == NULL) { + return ENOMEM; + } + + return EOK; +} + + +errno_t sss_dbus_list_domains(TALLOC_CTX *mem_ctx, + DBusGConnection *conn, + GError **_error, + char ***_domains) +{ + DBusGProxy *proxy = NULL; + GError *error = NULL; + char **domains = NULL; + GPtrArray *objects = NULL; + const char *object = NULL; + gboolean gbret; + errno_t ret; + int i; + + if (conn == NULL) { + return EINVAL; + } + + proxy = sss_dbus_infopipe_proxy(conn); + if (proxy == NULL) { + return ENOMEM; + } + + gbret = dbus_g_proxy_call(proxy, SSS_METHOD_DOMAIN_LIST, &error, + /* input args */ + G_TYPE_INVALID, + /* output args */ + dbus_g_type_get_collection("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), + &objects, + G_TYPE_INVALID); + if (!gbret) { + g_printerr("Failed to issue D-Bus call: %s\n", error->message); + ret = EIO; + goto done; + } + + if (objects == NULL || objects->len == 0) { + ret = ENOENT; + goto done; + } + + domains = talloc_zero_array(mem_ctx, char*, objects->len + 1); + if (domains == NULL) { + ret = ENOMEM; + goto done; + } + + for (i = 0; i < objects->len; i++) { + object = (const char*)g_ptr_array_index(objects, i); + ret = sss_dbus_get_string_property(domains, conn, error, + object, "name", &domains[i]); + if (ret != EOK) { + goto done; + } + } + + *_domains = domains; + + ret = EOK; + +done: + if (error != NULL) { + if (_error != NULL) { + *_error = error; + } else { + g_error_free(error); + } + } + + if (objects != NULL) { + g_ptr_array_free(objects, TRUE); + } + + return ret; +} + +int main(int argc, const char **argv) +{ + DBusGConnection *conn = NULL; + GError *error = NULL; + char **domains = NULL; + errno_t ret; + int i; + + g_type_init(); + + conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); + if (conn == NULL) { + g_printerr("Failed to open connection to bus: %s\n", error->message); + g_error_free(error); + return 1; + } + + ret = sss_dbus_list_domains(NULL, conn, &error, &domains); + if (ret != EOK) { + fprintf(stderr, "Error [%d]: %s\n", ret, strerror(ret)); + return 1; + } + + for (i = 0; domains[i] != NULL; i++) { + printf("%s\n", domains[i]); + } + + dbus_g_connection_unref(conn); + + return 0; +} -- cgit