summaryrefslogtreecommitdiffstats
path: root/src/providers/data_provider/dp_request.c
blob: a6bc020e0649760c46637d6f90569248792f7f04 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2016 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 <talloc.h>
#include <tevent.h>
#include <dbus/dbus.h>

#include "sbus/sssd_dbus_errors.h"
#include "providers/data_provider/dp_private.h"
#include "providers/backend.h"
#include "util/dlinklist.h"
#include "util/util.h"
#include "util/probes.h"

struct dp_req {
    struct data_provider *provider;
    struct dp_client *client;
    uint32_t dp_flags;

    struct sss_domain_info *domain;

    enum dp_targets target;
    enum dp_methods method;
    struct dp_method *execute;
    const char *name;
    uint32_t num;

    struct tevent_req *req;
    struct tevent_req *handler_req;
    void *request_data;

    /* Active request list. */
    struct dp_req *prev;
    struct dp_req *next;
};

static bool check_data_type(const char *expected,
                            const char *description,
                            void *ptr)
{
    void *tmp;

    /* If ptr is NULL we still return true since it is valid case. */
    tmp = talloc_check_name(ptr, expected);
    if (tmp != NULL || ptr == NULL) {
        return true;
    }

    DEBUG(SSSDBG_CRIT_FAILURE, "Invalid %s data type provided. Expected [%s], "
          "got [%s].\n", description, expected, talloc_get_name(ptr));

    return false;
}

static bool check_method_data(struct dp_method *method,
                              void *request_data)
{
    if (!check_data_type(method->method_dtype, "method", method->method_data)) {
        return false;
    }

    if (!check_data_type(method->request_dtype, "request", request_data)) {
        return false;
    }

    return true;
}

static int dp_req_destructor(struct dp_req *dp_req)
{
    DLIST_REMOVE(dp_req->provider->requests.active, dp_req);

    if (dp_req->provider->requests.num_active == 0) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Bug: there are no active requests!\n");
        return 0;
    }

    dp_req->provider->requests.num_active--;

    DP_REQ_DEBUG(SSSDBG_TRACE_FUNC, dp_req->name, "Request removed.");

    DEBUG(SSSDBG_TRACE_FUNC, "Number of active DP request: %u\n",
          dp_req->provider->requests.num_active);

    return 0;
}

static errno_t dp_attach_req(struct dp_req *dp_req,
                             struct data_provider *provider,
                             const char *name,
                             uint32_t dp_flags)
{
    /* If we run out of numbers we simply overflow. */
    dp_req->num = provider->requests.index++;
    dp_req->name = talloc_asprintf(dp_req, "%s #%u", name, dp_req->num);
    if (dp_req->name == NULL) {
        return ENOMEM;
    }

    /* Attach this request to active request list. */
    DLIST_ADD(provider->requests.active, dp_req);
    provider->requests.num_active++;

    talloc_set_destructor(dp_req, dp_req_destructor);

    DP_REQ_DEBUG(SSSDBG_TRACE_FUNC, dp_req->name,
                 "New request. Flags [%#.4x].", dp_flags);

    DEBUG(SSSDBG_TRACE_FUNC, "Number of active DP request: %u\n",
          provider->requests.num_active);

    return EOK;
}

static errno_t
dp_req_new(TALLOC_CTX *mem_ctx,
           struct data_provider *provider,
           struct dp_client *dp_cli,
           const char *domainname,
           const char *name,
           enum dp_targets target,
           enum dp_methods method,
           uint32_t dp_flags,
           void *request_data,
           struct tevent_req *req,
           struct dp_req **_dp_req)
{
    struct dp_req *dp_req;
    struct be_ctx *be_ctx;
    errno_t ret;

    /* We set output even for error to simplify code flow in the caller. */
    *_dp_req = NULL;

    dp_req = talloc_zero(mem_ctx, struct dp_req);
    if (dp_req == NULL) {
        return ENOMEM;
    }

    dp_req->provider = provider;
    dp_req->client = dp_cli;
    dp_req->dp_flags = dp_flags;
    dp_req->target = target;
    dp_req->method = method;
    dp_req->request_data = request_data;
    dp_req->req = req;

    ret = dp_attach_req(dp_req, provider, name, dp_flags);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create DP request "
              "[%s] [%d]: %s\n", name, ret, sss_strerror(ret));
        talloc_free(dp_req);
        return ret;
    }

    /* Now the request is created. We will return it even in case of error
     * so we can get better debug messages. */

    talloc_steal(dp_req, dp_req->request_data);
    *_dp_req = dp_req;

    be_ctx = provider->be_ctx;
    dp_req->domain = be_ctx->domain;
    if (domainname != NULL) {
        dp_req->domain = find_domain_by_name(be_ctx->domain, domainname, true);
        if (dp_req->domain == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unknown domain: %s\n", domainname);
            return ERR_DOMAIN_NOT_FOUND;
        }
    }

    ret = dp_find_method(provider, target, method, &dp_req->execute);

    return ret;
}

static errno_t
file_dp_request(TALLOC_CTX *mem_ctx,
                struct data_provider *provider,
                struct dp_client *dp_cli,
                const char *domainname,
                const char *name,
                enum dp_targets target,
                enum dp_methods method,
                uint32_t dp_flags,
                void *request_data,
                struct tevent_req *req,
                struct dp_req **_dp_req)
{
    struct dp_req_params *dp_params;
    dp_req_send_fn send_fn;
    struct dp_req *dp_req;
    struct be_ctx *be_ctx;
    errno_t ret;

    be_ctx = provider->be_ctx;

    ret = dp_req_new(mem_ctx, provider, dp_cli, domainname, name, target,
                     method, dp_flags, request_data, req, &dp_req);
    if (ret != EOK) {
        *_dp_req = dp_req;
        goto done;
    }

    /* DP request is already created. We will always return it to get nice
     * debug messages. */
    *_dp_req = dp_req;

    /* Check that provided data are of correct type. */

    if (!check_method_data(dp_req->execute, dp_req->request_data)) {
        ret = ERR_INVALID_DATA_TYPE;
        goto done;
    }

    /* Process data provider flags */

    if (dp_flags & DP_FAST_REPLY && be_is_offline(be_ctx)) {
        ret = ERR_OFFLINE;
        goto done;
    }

    /* File request */

    dp_params = talloc_zero(dp_req, struct dp_req_params);
    if (dp_params == NULL) {
        ret = ENOMEM;
        goto done;
    }

    dp_params->ev = provider->ev;
    dp_params->be_ctx = be_ctx;
    dp_params->domain = dp_req->domain;
    dp_params->target = dp_req->target;
    dp_params->method = dp_req->method;

    send_fn = dp_req->execute->send_fn;
    dp_req->handler_req = send_fn(dp_req, dp_req->execute->method_data,
                                  dp_req->request_data, dp_params);
    if (dp_req->handler_req == NULL) {
        ret = ENOMEM;
        goto done;
    }

    *_dp_req = dp_req;

    ret = EOK;

done:
    return ret;
}

struct dp_req_state {
    struct dp_req *dp_req;
    dp_req_recv_fn recv_fn;
    void *output_data;
};

static void dp_req_done(struct tevent_req *subreq);

struct tevent_req *dp_req_send(TALLOC_CTX *mem_ctx,
                               struct data_provider *provider,
                               struct dp_client *dp_cli,
                               const char *domain,
                               const char *name,
                               enum dp_targets target,
                               enum dp_methods method,
                               uint32_t dp_flags,
                               void *request_data,
                               const char **_request_name)
{
    struct dp_req_state *state;
    const char *request_name;
    struct tevent_req *req;
    struct dp_req *dp_req;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state, struct dp_req_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
        return NULL;
    }

    ret = file_dp_request(state, provider, dp_cli, domain, name, target,
                          method, dp_flags, request_data, req, &dp_req);

    if (dp_req == NULL) {
        /* An error ocurred before request could be created. */
        if (_request_name != NULL) {
            *_request_name = "Request Not Yet Created";
        }

        goto immediately;
    }

    PROBE(DP_REQ_SEND, domain, dp_req->name, target, method);
    state->dp_req = dp_req;
    if (_request_name != NULL) {
        request_name = talloc_strdup(mem_ctx, dp_req->name);
        if (request_name == NULL) {
            *_request_name = "Request Not Yet Created";
            ret = ENOMEM;
            goto immediately;
        }
        *_request_name = request_name;
    }

    if (ret != EOK) {
        goto immediately;
    }

    state->recv_fn = dp_req->execute->recv_fn;
    state->output_data = talloc_zero_size(state, dp_req->execute->output_size);
    if (state->output_data == NULL) {
        ret = ENOMEM;
        goto immediately;
    }

    talloc_set_name_const(state->output_data, dp_req->execute->output_dtype);

    tevent_req_set_callback(dp_req->handler_req, dp_req_done, req);

    return req;

immediately:
    if (ret == EOK) {
        tevent_req_done(req);
    } else {
        tevent_req_error(req, ret);
    }
    tevent_req_post(req, provider->ev);

    return req;
}

static void dp_req_done(struct tevent_req *subreq)
{
    struct dp_req_state *state;
    struct tevent_req *req;
    errno_t ret;

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

    ret = state->recv_fn(state->output_data, subreq, state->output_data);

    /* subreq is the same as dp_req->handler_req */
    talloc_zfree(subreq);
    state->dp_req->handler_req = NULL;

    PROBE(DP_REQ_DONE, state->dp_req->name, state->dp_req->target,
          state->dp_req->method, ret, sss_strerror(ret));

    DP_REQ_DEBUG(SSSDBG_TRACE_FUNC, state->dp_req->name,
                 "Request handler finished [%d]: %s", ret, sss_strerror(ret));

    if (ret != EOK) {
        tevent_req_error(req, ret);
        return;
    }

    tevent_req_done(req);
}

errno_t _dp_req_recv(TALLOC_CTX *mem_ctx,
                     struct tevent_req *req,
                     const char *output_dtype,
                     void **_output_data)
{
    struct dp_req_state *state;

    state = tevent_req_data(req, struct dp_req_state);

    if (state->dp_req != NULL) {
        DP_REQ_DEBUG(SSSDBG_TRACE_FUNC, state->dp_req->name,
                     "Receiving request data.");
    } else {
        /* dp_req may be NULL in case we error when filing request */
        DEBUG(SSSDBG_TRACE_FUNC,
              "Receiving data of prematurely interrupted request!\n");
    }

    TEVENT_REQ_RETURN_ON_ERROR(req);

    if (!check_data_type(output_dtype, "output", state->output_data)) {
        return ERR_INVALID_DATA_TYPE;
    }

    *_output_data = talloc_steal(mem_ctx, state->output_data);

    return EOK;
}

static void dp_terminate_request(struct dp_req *dp_req)
{
    if (dp_req->handler_req == NULL) {
        /* This may occur when the handler already finished but the caller
         * of dp request did not yet recieved data/free dp_req. We just
         * return here. */
        return;
    }

    /* We will end the handler request and mark dp request as terminated. */

    DP_REQ_DEBUG(SSSDBG_TRACE_ALL, dp_req->name, "Terminating.");

    talloc_zfree(dp_req->handler_req);
    tevent_req_error(dp_req->req, ERR_TERMINATED);
}

static void dp_terminate_request_list(struct data_provider *provider,
                                      const char *domain)
{
    struct dp_req *next;
    struct dp_req *cur;

    if (provider == NULL || provider->requests.active == NULL) {
        return;
    }

    for (cur = provider->requests.active; cur != NULL; cur = next) {
        next = cur->next;
        if (domain == NULL || strcmp(cur->domain->name, domain) == 0) {
            dp_terminate_request(cur);
        }
    }
}

void dp_terminate_active_requests(struct data_provider *provider)
{
    DEBUG(SSSDBG_TRACE_FUNC, "Terminating active data provider requests\n");

    dp_terminate_request_list(provider, NULL);
}

void dp_terminate_domain_requests(struct data_provider *provider,
                                  const char *domain)
{
    DEBUG(SSSDBG_TRACE_FUNC, "Terminating active data provider requests "
          "for domain [%s]\n", domain);

    if (domain == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Bug: domain is NULL!\n");
        return;
    }

    dp_terminate_request_list(provider, domain);
}