summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 2daf92798b99da81486177138d2d25933b572c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <talloc.h>
#include <dbus/dbus.h>

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 DBusMessage* sss_dbus_infopipe_call(DBusConnection *conn,
                                           const char *method)
{
    return dbus_message_new_method_call(INFOPIPE_DBUS_NAME,
                                        INFOPIPE_PATH,
                                        INFOPIPE_INTERFACE,
                                        method);
}

static DBusMessage* sss_dbus_property_call(DBusConnection *conn,
                                           const char *object_path,
                                           const char *method)
{
    return dbus_message_new_method_call(INFOPIPE_DBUS_NAME,
                                        object_path,
                                        DBUS_PROP_IFACE,
                                        method);
}

errno_t sss_dbus_get_string_property(TALLOC_CTX *mem_ctx,
                                     DBusConnection *conn,
                                     DBusError *error,
                                     const char *object_path,
                                     const char *property,
                                     char **_out)
{
    DBusMessage *msg = NULL;
    DBusMessage *reply = NULL;
    DBusMessageIter iter;
    DBusMessageIter subiter;
    char *prop = NULL;
    dbus_bool_t dbret;
    errno_t ret;

    msg = sss_dbus_property_call(conn, object_path, "Get");
    if (msg == NULL) {
        ret = ENOMEM;
        goto done;
    }

    dbret = dbus_message_append_args(msg,
                                     DBUS_TYPE_STRING, &property,
                                     DBUS_TYPE_INVALID);
    if (!dbret) {
        ret = ENOMEM;
        goto done;
    }

    reply = dbus_connection_send_with_reply_and_block(conn, msg, 5000, error);
    if (dbus_error_is_set(error)) {
        ret = EIO;
        goto done;
    }

    dbus_message_iter_init(reply, &iter);

    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
        ret = EINVAL;
        goto done;
    }

    dbus_message_iter_recurse(&iter, &subiter);

    if (dbus_message_iter_get_arg_type(&subiter) != DBUS_TYPE_STRING) {
        ret = EINVAL;
        goto done;
    }

    dbus_message_iter_get_basic(&subiter, &prop);

    *_out = talloc_strdup(mem_ctx, prop);
    if (*_out == NULL) {
        ret = ENOMEM;
        goto done;
    }

    ret = EOK;

done:
    if (msg != NULL) {
        dbus_message_unref(msg);
    }

    if (reply != NULL) {
        dbus_message_unref(reply);
    }

    return ret;
}

errno_t sss_dbus_list_domains(TALLOC_CTX *mem_ctx,
                              DBusConnection *conn,
                              DBusError *error,
                              char ***_domains)
{
    DBusMessage *msg = NULL;
    DBusMessage *reply = NULL;
    char **domains = NULL;
    char **objects = NULL;
    int num_objects;
    dbus_bool_t dbret;
    errno_t ret;
    int i;

    if (conn == NULL || error == NULL) {
        ret = EINVAL;
        goto done;
    }

    msg = sss_dbus_infopipe_call(conn, SSS_METHOD_DOMAIN_LIST);
    if (msg == NULL) {
        ret = ENOMEM;
        goto done;
    }

    reply = dbus_connection_send_with_reply_and_block(conn, msg, 5000, error);
    if (dbus_error_is_set(error)) {
        ret = EIO;
        goto done;
    }

    dbret = dbus_message_get_args(reply, error,
                                  DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
                                  &objects, &num_objects,
                                  DBUS_TYPE_INVALID);
    if (!dbret) {
        ret = EIO;
        goto done;
    }

    if (objects == NULL || num_objects == 0) {
        ret = ENOENT;
        goto done;
    }

    domains = talloc_zero_array(mem_ctx, char*, num_objects + 1);
    if (domains == NULL) {
        ret = ENOMEM;
        goto done;
    }

    for (i = 0; i < num_objects; i++) {
        ret = sss_dbus_get_string_property(domains, conn, error, objects[i],
                                           "name", &domains[i]);
        if (ret != EOK) {
            goto done;
        }
    }

    *_domains = domains;

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(domains);
    }

    if (msg != NULL) {
        dbus_message_unref(msg);
    }

    if (reply != NULL) {
        dbus_message_unref(reply);
    }

    return ret;
}

int main(int argc, const char **argv)
{
    DBusConnection *conn = NULL;
    DBusError error;
    char **domains = NULL;
    errno_t ret;
    int i;

    dbus_error_init(&error);

    conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
    if (dbus_error_is_set(&error)) {
        fprintf(stderr, "Failed to open connection to bus: %s\n",
                error.message);
        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_connection_unref(conn);

    return 0;
}