summaryrefslogtreecommitdiffstats
path: root/src/sbus/connection/sbus_connection_connect.c
blob: edc090e156f0c33c4ea10568747d617b0ec5af01 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>
        Stephen Gallagher <sgallagh@redhat.com>
        Simo Sorce <ssorce@redhat.com>

    Copyright (C) 2017 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <errno.h>
#include <tevent.h>
#include <talloc.h>
#include <dbus/dbus.h>

#include "util/util.h"
#include "sbus/connection/sbus_dbus_private.h"
#include "sbus/sbus_private.h"
#include "sbus/interface_dbus/sbus_dbus_client_async.h"

struct sbus_connect_init_state {
    struct sbus_connection *conn;
    const char *name;
};

static void sbus_connect_init_hello_done(struct tevent_req *subreq);
static void sbus_connect_init_done(struct tevent_req *subreq);

struct tevent_req *
sbus_connect_init_send(TALLOC_CTX *mem_ctx,
                       struct sbus_connection *conn,
                       const char *name)
{
    struct sbus_connect_init_state *state;
    struct tevent_req *subreq;
    struct tevent_req *req;

    req = tevent_req_create(mem_ctx, &state, struct sbus_connect_init_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        return NULL;
    }

    state->conn = conn;
    state->name = name;

    subreq = sbus_call_DBus_Hello_send(state, conn, DBUS_SERVICE_DBUS,
                                       DBUS_PATH_DBUS);
    if (subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
        tevent_req_error(req, ENOMEM);
        tevent_req_post(req, conn->ev);
        return req;
    }

    tevent_req_set_callback(subreq, sbus_connect_init_hello_done, req);

    arm_watchdog();

    return req;
}

static void sbus_connect_init_hello_done(struct tevent_req *subreq)
{
    struct sbus_connect_init_state *state;
    const char *unique_name;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_callback_data(subreq, struct tevent_req);
    state = tevent_req_data(req, struct sbus_connect_init_state);

    ret = sbus_call_DBus_Hello_recv(state, subreq, &unique_name);
    talloc_zfree(subreq);
    if (ret != EOK) {
        tevent_req_error(req, ret);
        return;
    }

    if (state->name == NULL) {
        tevent_req_done(req);
        return;
    }

    subreq = sbus_call_DBus_RequestName_send(state, state->conn,
                 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, state->name,
                 DBUS_NAME_FLAG_DO_NOT_QUEUE);
    if (subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
        tevent_req_error(req, ENOMEM);
        return;
    }

    tevent_req_set_callback(subreq, sbus_connect_init_done, req);
    return;
}

static void sbus_connect_init_done(struct tevent_req *subreq)
{
    struct tevent_req *req;
    uint32_t res;
    errno_t ret;

    disarm_watchdog();

    req = tevent_req_callback_data(subreq, struct tevent_req);

    ret = sbus_call_DBus_RequestName_recv(subreq, &res);
    talloc_zfree(subreq);
    if (ret != EOK) {
        tevent_req_error(req, ret);
        return;
    }

    switch (res) {
    case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
    case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
        tevent_req_done(req);
        return;
    case DBUS_REQUEST_NAME_REPLY_EXISTS:
        tevent_req_error(req, EEXIST);
        return;
    default:
        tevent_req_error(req, EIO);
        return;
    }
}

errno_t sbus_connect_init_recv(struct tevent_req *req)
{
    TEVENT_REQ_RETURN_ON_ERROR(req);

    return EOK;
}

struct sbus_connection *
sbus_connect_system(TALLOC_CTX *mem_ctx,
                    struct tevent_context *ev,
                    const char *dbus_name,
                    time_t *last_activity_time)
{
    struct sbus_connection *sbus_conn;
    DBusConnection *dbus_conn;
    errno_t ret;

    dbus_conn = sbus_dbus_connect_bus(DBUS_BUS_SYSTEM, dbus_name);
    if (dbus_conn == NULL) {
        return NULL;
    }

    sbus_conn = sbus_connection_init(mem_ctx, ev, dbus_conn, NULL, dbus_name,
                                     SBUS_CONNECTION_SYSBUS,
                                     last_activity_time);
    dbus_connection_unref(dbus_conn);
    if (sbus_conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create connection context!\n");
        return NULL;
    }

    ret = sbus_register_standard_signals(sbus_conn);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register signal listeners "
              "[%d]: %s\n", ret, sss_strerror(ret));
        talloc_free(sbus_conn);
        return NULL;
    }

    return sbus_conn;
}

struct sbus_connection *
sbus_connect_private(TALLOC_CTX *mem_ctx,
                     struct tevent_context *ev,
                     const char *address,
                     const char *dbus_name,
                     time_t *last_activity_time)
{
    struct sbus_connection *sbus_conn;
    DBusConnection *dbus_conn;
    errno_t ret;

    dbus_conn = sbus_dbus_connect_address(address, dbus_name, true);
    if (dbus_conn == NULL) {
        return NULL;
    }

    sbus_conn = sbus_connection_init(mem_ctx, ev, dbus_conn, address, dbus_name,
                                     SBUS_CONNECTION_ADDRESS,
                                     last_activity_time);
    dbus_connection_unref(dbus_conn);
    if (sbus_conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create connection context!\n");
        return NULL;
    }

    ret = sbus_register_standard_signals(sbus_conn);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register signal listeners "
              "[%d]: %s\n", ret, sss_strerror(ret));
        talloc_free(sbus_conn);
        return NULL;
    }

    return sbus_conn;
}

struct sbus_connect_private_state {
    struct sbus_connection *conn;
};

static void sbus_connect_private_done(struct tevent_req *subreq);

struct tevent_req *
sbus_connect_private_send(TALLOC_CTX *mem_ctx,
                          struct tevent_context *ev,
                          const char *address,
                          const char *dbus_name,
                          time_t *last_activity_time)
{
    struct sbus_connect_private_state *state;
    DBusConnection *dbus_conn;
    struct tevent_req *subreq;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state,
                            struct sbus_connect_private_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        return NULL;
    }

    dbus_conn = sbus_dbus_connect_address(address, dbus_name, false);
    if (dbus_conn == NULL) {
        ret = ENOMEM;
        goto done;
    }

    state->conn = sbus_connection_init(state, ev, dbus_conn, address,
                                       dbus_name, SBUS_CONNECTION_ADDRESS,
                                       last_activity_time);
    dbus_connection_unref(dbus_conn);
    if (state->conn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create connection context!\n");
        ret = ENOMEM;
        goto done;
    }

    subreq = sbus_connect_init_send(state, state->conn, dbus_name);
    if (subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
        ret = ENOMEM;
        goto done;
    }

    tevent_req_set_callback(subreq, sbus_connect_private_done, req);

    ret = EAGAIN;

done:
    if (ret != EAGAIN) {
        tevent_req_error(req, ret);
        tevent_req_post(req, ev);
    }

    return req;
}

static void sbus_connect_private_done(struct tevent_req *subreq)
{
    struct sbus_connect_private_state *state;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_callback_data(subreq, struct tevent_req);
    state = tevent_req_data(req, struct sbus_connect_private_state);

    ret = sbus_connect_init_recv(subreq);
    talloc_zfree(subreq);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to initialize connection "
              "[%d]: %s\n", ret, sss_strerror(ret));
        talloc_free(state->conn);
        tevent_req_error(req, ret);
        return;
    }

    ret = sbus_register_standard_signals(state->conn);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register signal listeners "
              "[%d]: %s\n", ret, sss_strerror(ret));
        talloc_zfree(state->conn);
        tevent_req_error(req, ret);
        return;
    }

    if (state->conn->wellknown_name == NULL) {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as anonymous\n",
              state->conn->address);
    } else {
        DEBUG(SSSDBG_TRACE_FUNC, "Connected to %s bus as %s\n",
              state->conn->address, state->conn->wellknown_name);
    }

    tevent_req_done(req);
    return;
}

errno_t sbus_connect_private_recv(TALLOC_CTX *mem_ctx,
                                  struct tevent_req *req,
                                  struct sbus_connection **_conn)
{
    struct sbus_connect_private_state *state;
    state = tevent_req_data(req, struct sbus_connect_private_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *_conn = talloc_steal(mem_ctx, state->conn);

    return EOK;
}

struct sbus_server_create_and_connect_state {
    struct sbus_server *server;
    struct sbus_connection *conn;
};

static void sbus_server_create_and_connect_done(struct tevent_req *subreq);

struct tevent_req *
sbus_server_create_and_connect_send(TALLOC_CTX *mem_ctx,
                                    struct tevent_context *ev,
                                    const char *dbus_name,
                                    time_t *last_activity_time,
                                    const char *address,
                                    bool use_symlink,
                                    uint32_t max_connections,
                                    uid_t uid,
                                    gid_t gid,
                                    sbus_server_on_connection_cb on_conn_cb,
                                    sbus_server_on_connection_data on_conn_data)
{
    struct sbus_server_create_and_connect_state *state;
    struct tevent_req *subreq;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state, struct sbus_server_create_and_connect_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        return NULL;
    }

    state->server = sbus_server_create(state, ev, address, use_symlink,
                                       max_connections, uid, gid,
                                       on_conn_cb, on_conn_data);
    if (state->server == NULL) {
        ret = ENOMEM;
        goto done;
    }

    subreq = sbus_connect_private_send(state, ev, address, dbus_name,
                                       last_activity_time);
    if (subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
        ret = ENOMEM;
        goto done;
    }

    tevent_req_set_callback(subreq, sbus_server_create_and_connect_done, req);

    ret = EAGAIN;

done:
    if (ret != EAGAIN) {
        tevent_req_error(req, ret);
        tevent_req_post(req, ev);
    }

    return req;
}

static void sbus_server_create_and_connect_done(struct tevent_req *subreq)
{
    struct sbus_server_create_and_connect_state *state;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_callback_data(subreq, struct tevent_req);
    state = tevent_req_data(req, struct sbus_server_create_and_connect_state);

    ret = sbus_connect_private_recv(state, subreq, &state->conn);
    talloc_zfree(subreq);
    if (ret != EOK) {
        tevent_req_error(req, ret);
        return;
    }

    tevent_req_done(req);
    return;
}

errno_t
sbus_server_create_and_connect_recv(TALLOC_CTX *mem_ctx,
                                    struct tevent_req *req,
                                    struct sbus_server **_server,
                                    struct sbus_connection **_conn)
{
    struct sbus_server_create_and_connect_state *state;
    state = tevent_req_data(req, struct sbus_server_create_and_connect_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *_server = talloc_steal(mem_ctx, state->server);
    *_conn = talloc_steal(mem_ctx, state->conn);

    return EOK;
}