summaryrefslogtreecommitdiff
path: root/src/providers/ldap/sdap_async_services.c
blob: 15a725b8b9db94a593e560899cb97b350f91c040 (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
    SSSD

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2012 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 "util/util.h"
#include "db/sysdb.h"
#include "db/sysdb_services.h"
#include "providers/ldap/sdap_async_private.h"
#include "providers/ldap/ldap_common.h"

struct sdap_get_services_state {
    struct tevent_context *ev;
    struct sdap_options *opts;
    struct sdap_handle *sh;
    struct sss_domain_info *dom;
    struct sysdb_ctx *sysdb;
    const char **attrs;
    const char *base_filter;
    char *filter;
    int timeout;
    bool enumeration;

    char *higher_usn;
    struct sysdb_attrs **services;
    size_t count;

    size_t base_iter;
    struct sdap_search_base **search_bases;
};

static errno_t
sdap_get_services_next_base(struct tevent_req *req);
static void
sdap_get_services_process(struct tevent_req *subreq);
static errno_t
sdap_save_services(TALLOC_CTX *memctx,
                   struct sysdb_ctx *sysdb,
                   struct sss_domain_info *dom,
                   struct sdap_options *opts,
                   struct sysdb_attrs **services,
                   size_t num_services,
                   char **_usn_value);
static errno_t
sdap_save_service(TALLOC_CTX *mem_ctx,
                  struct sysdb_ctx *sysdb,
                  struct sdap_options *opts,
                  struct sss_domain_info *dom,
                  struct sysdb_attrs *attrs,
                  char **_usn_value,
                  time_t now);

struct tevent_req *
sdap_get_services_send(TALLOC_CTX *memctx,
                       struct tevent_context *ev,
                       struct sss_domain_info *dom,
                       struct sysdb_ctx *sysdb,
                       struct sdap_options *opts,
                       struct sdap_search_base **search_bases,
                       struct sdap_handle *sh,
                       const char **attrs,
                       const char *filter,
                       int timeout,
                       bool enumeration)
{
    errno_t ret;
    struct tevent_req *req;
    struct sdap_get_services_state *state;

    req = tevent_req_create(memctx, &state, struct sdap_get_services_state);
    if (!req) return NULL;

    state->ev = ev;
    state->opts = opts;
    state->dom = dom;
    state->sh = sh;
    state->sysdb = sysdb;
    state->attrs = attrs;
    state->higher_usn = NULL;
    state->services =  NULL;
    state->count = 0;
    state->timeout = timeout;
    state->base_filter = filter;
    state->base_iter = 0;
    state->search_bases = search_bases;
    state->enumeration = enumeration;

    if (!state->search_bases) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Services lookup request without a search base\n"));
        ret = EINVAL;
        goto done;
    }

    ret = sdap_get_services_next_base(req);

done:
    if (ret != EOK) {
        tevent_req_error(req, ret);
        tevent_req_post(req, state->ev);
    }

    return req;
}

static errno_t
sdap_get_services_next_base(struct tevent_req *req)
{
    struct tevent_req *subreq;
    struct sdap_get_services_state *state;

    state = tevent_req_data(req, struct sdap_get_services_state);

    talloc_zfree(state->filter);
    state->filter = sdap_get_id_specific_filter(state,
                        state->base_filter,
                        state->search_bases[state->base_iter]->filter);
    if (!state->filter) {
        return ENOMEM;
    }

    DEBUG(SSSDBG_TRACE_FUNC,
          ("Searching for services with base [%s]\n",
           state->search_bases[state->base_iter]->basedn));

    subreq = sdap_get_generic_send(
            state, state->ev, state->opts, state->sh,
            state->search_bases[state->base_iter]->basedn,
            state->search_bases[state->base_iter]->scope,
            state->filter, state->attrs,
            state->opts->service_map, SDAP_OPTS_SERVICES,
            state->timeout,
            state->enumeration); /* If we're enumerating, we need paging */
    if (!subreq) {
        return ENOMEM;
    }
    tevent_req_set_callback(subreq, sdap_get_services_process, req);

    return EOK;
}

static void
sdap_get_services_process(struct tevent_req *subreq)
{
    struct tevent_req *req =
            tevent_req_callback_data(subreq, struct tevent_req);
    struct sdap_get_services_state *state =
            tevent_req_data(req, struct sdap_get_services_state);
    int ret;
    size_t count, i;
    struct sysdb_attrs **services;
    bool next_base = false;

    ret = sdap_get_generic_recv(subreq, state,
                                &count, &services);
    talloc_zfree(subreq);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }

    DEBUG(SSSDBG_TRACE_FUNC,
          ("Search for services, returned %d results.\n",
           count));

    if (state->enumeration || count == 0) {
        /* No services found in this search or enumerating */
        next_base = true;
    }

    /* Add this batch of sevices to the list */
    if (count > 0) {
        state->services =
                talloc_realloc(state,
                               state->services,
                               struct sysdb_attrs *,
                               state->count + count + 1);
        if (!state->services) {
            tevent_req_error(req, ENOMEM);
            return;
        }

        /* Copy the new services into the list
         */
        for (i = 0; i < count; i++) {
            state->services[state->count + i] =
                talloc_steal(state->services, services[i]);
        }

        state->count += count;
        state->services[state->count] = NULL;
    }

    if (next_base) {
        state->base_iter++;
        if (state->search_bases[state->base_iter]) {
            /* There are more search bases to try */
            ret = sdap_get_services_next_base(req);
            if (ret != EOK) {
                tevent_req_error(req, ret);
            }
            return;
        }
    }

    /* No more search bases
     * Return ENOENT if no services were found
     */
    if (state->count == 0) {
        tevent_req_error(req, ENOENT);
        return;
    }

    ret = sdap_save_services(state, state->sysdb,
                             state->dom, state->opts,
                             state->services, state->count,
                             &state->higher_usn);
    if (ret) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to store services.\n"));
        tevent_req_error(req, ret);
        return;
    }

    DEBUG(SSSDBG_TRACE_INTERNAL,
          ("Saving %d services - Done\n", state->count));

    tevent_req_done(req);
}

static errno_t
sdap_save_services(TALLOC_CTX *mem_ctx,
                   struct sysdb_ctx *sysdb,
                   struct sss_domain_info *dom,
                   struct sdap_options *opts,
                   struct sysdb_attrs **services,
                   size_t num_services,
                   char **_usn_value)
{
    errno_t ret, sret;
    time_t now;
    size_t i;
    bool in_transaction = false;
    char *higher_usn = NULL;
    char *usn_value;
    TALLOC_CTX *tmp_ctx;

    if (num_services == 0) {
        /* Nothing to do */
        return ENOENT;
    }

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        return ENOMEM;
    }

    ret = sysdb_transaction_start(sysdb);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to start transaction\n"));
        goto done;
    }

    in_transaction = true;

    now = time(NULL);
    for (i = 0; i < num_services; i++) {
        usn_value = NULL;

        ret = sdap_save_service(tmp_ctx, sysdb, opts, dom,
                                services[i],
                                &usn_value, now);

        /* Do not fail completely on errors.
         * Just report the failure to save and go on */
        if (ret) {
            DEBUG(SSSDBG_MINOR_FAILURE,
                  ("Failed to store service %d. Ignoring.\n", i));
        } else {
            DEBUG(SSSDBG_TRACE_INTERNAL,
                  ("Service [%lu/%lu] processed!\n", i, num_services));
        }

        if (usn_value) {
            if (higher_usn) {
                if ((strlen(usn_value) > strlen(higher_usn)) ||
                    (strcmp(usn_value, higher_usn) > 0)) {
                    talloc_zfree(higher_usn);
                    higher_usn = usn_value;
                } else {
                    talloc_zfree(usn_value);
                }
            } else {
                higher_usn = usn_value;
            }
        }
    }

    ret = sysdb_transaction_commit(sysdb);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Failed to commit transaction!\n"));
        goto done;
    }
    in_transaction = false;

    if (_usn_value) {
        *_usn_value = talloc_steal(mem_ctx, higher_usn);
    }

done:
    if (in_transaction) {
        sret = sysdb_transaction_cancel(sysdb);
        if (sret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("Failed to cancel transaction!\n"));
        }
    }
    talloc_free(tmp_ctx);
    return ret;
}

static errno_t
sdap_save_service(TALLOC_CTX *mem_ctx,
                  struct sysdb_ctx *sysdb,
                  struct sdap_options *opts,
                  struct sss_domain_info *dom,
                  struct sysdb_attrs *attrs,
                  char **_usn_value,
                  time_t now)
{
    errno_t ret;
    TALLOC_CTX *tmp_ctx = NULL;
    struct sysdb_attrs *svc_attrs;
    struct ldb_message_element *el;
    char *usn_value = NULL;
    const char *name = NULL;
    const char **aliases;
    const char **protocols;
    const char **cased_protocols;
    const char **store_protocols;
    char **missing;
    uint16_t port;
    uint64_t cache_timeout;

    DEBUG(SSSDBG_TRACE_ALL, ("Saving service\n"));

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        ret = ENOMEM;
        goto done;
    }

    svc_attrs = sysdb_new_attrs(tmp_ctx);
    if (!svc_attrs) {
        ret = ENOMEM;
        goto done;
    }

    /* Identify the primary name of this services */
    ret = sysdb_attrs_primary_name(
            sysdb, attrs,
            opts->service_map[SDAP_AT_SERVICE_NAME].name,
            &name);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not determine the primary name of the service\n"));
        goto done;
    }

    DEBUG(SSSDBG_TRACE_INTERNAL, ("Primary name: [%s]\n", name));


    /* Handle any available aliases */
    ret = sysdb_attrs_get_aliases(tmp_ctx, attrs, name,
                                  !dom->case_sensitive,
                                  &aliases);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to identify service aliases\n"));
        goto done;
    }

    /* Get the port number */
    ret = sysdb_attrs_get_uint16_t(attrs, SYSDB_SVC_PORT, &port);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to identify service port: [%s]\n",
               strerror(ret)));
        goto done;
    }

    /* Get the protocols this service offers on that port */
    ret = sysdb_attrs_get_string_array(attrs, SYSDB_SVC_PROTO,
                                       tmp_ctx, &protocols);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to identify service protocols: [%s]\n",
               strerror(ret)));
        goto done;
    }

    if (dom->case_sensitive == false) {
        /* Don't perform the extra mallocs if not necessary */
        ret = sss_get_cased_name_list(tmp_ctx, protocols,
                                      dom->case_sensitive, &cased_protocols);
        if (ret != EOK) {
            DEBUG(SSSDBG_MINOR_FAILURE,
                ("Failed to get case_sensitive protocols names: [%s]\n",
                strerror(ret)));
            goto done;
        }
    }

    store_protocols = dom->case_sensitive ? protocols : cased_protocols;

    /* Get the USN value, if available */
    ret = sysdb_attrs_get_el(attrs,
                      opts->service_map[SDAP_AT_SERVICE_USN].sys_name, &el);
    if (ret && ret != ENOENT) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to retrieve USN value: [%s]\n",
               strerror(ret)));
        goto done;
    }
    if (ret == ENOENT || el->num_values == 0) {
        DEBUG(SSSDBG_TRACE_LIBS,
              ("Original USN value is not available for [%s].\n",
               name));
    } else {
        ret = sysdb_attrs_add_string(svc_attrs,
                          opts->service_map[SDAP_AT_SERVICE_USN].sys_name,
                          (const char*)el->values[0].data);
        if (ret) {
            DEBUG(SSSDBG_MINOR_FAILURE,
                  ("Failed to add USN value: [%s]\n",
                   strerror(ret)));
            goto done;
        }
        usn_value = talloc_strdup(tmp_ctx, (const char*)el->values[0].data);
        if (!usn_value) {
            ret = ENOMEM;
            goto done;
        }
    }

    /* Make sure to remove any extra attributes from the sysdb
     * that have been removed from LDAP
     */
    ret = list_missing_attrs(svc_attrs, opts->service_map, SDAP_OPTS_SERVICES,
                             attrs, &missing);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to identify removed attributes: [%s]\n",
               strerror(ret)));
        goto done;
    }

    cache_timeout = dom->service_timeout;

    ret = sysdb_store_service(sysdb, dom, name, port, aliases, store_protocols,
                              svc_attrs, missing, cache_timeout, now);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to store service in the sysdb: [%s]\n",
               strerror(ret)));
        goto done;
    }

    *_usn_value = talloc_steal(mem_ctx, usn_value);

done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t
sdap_get_services_recv(TALLOC_CTX *mem_ctx,
                       struct tevent_req *req,
                       char **usn_value)
{
    struct sdap_get_services_state *state =
            tevent_req_data(req, struct sdap_get_services_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    if (usn_value) {
        *usn_value = talloc_steal(mem_ctx, state->higher_usn);
    }

    return EOK;
}


/* Enumeration routines */

struct enum_services_state {
    struct tevent_context *ev;
    struct sdap_id_ctx *id_ctx;
    struct sdap_id_op *op;
    struct sss_domain_info *domain;
    struct sysdb_ctx *sysdb;

    char *filter;
    const char **attrs;
};

static void
enum_services_op_done(struct tevent_req *subreq);

struct tevent_req *
enum_services_send(TALLOC_CTX *memctx,
                   struct tevent_context *ev,
                   struct sdap_id_ctx *id_ctx,
                   struct sdap_id_op *op,
                   bool purge)
{
    errno_t ret;
    struct tevent_req *req;
    struct tevent_req *subreq;
    struct enum_services_state *state;

    req = tevent_req_create(memctx, &state, struct enum_services_state);
    if (!req) return NULL;

    state->ev = ev;
    state->id_ctx = id_ctx;
    state->domain = id_ctx->be->domain;
    state->sysdb = id_ctx->be->domain->sysdb;
    state->op = op;

    if (id_ctx->srv_opts && id_ctx->srv_opts->max_service_value && !purge) {
        state->filter = talloc_asprintf(
                state,
                "(&(objectclass=%s)(%s=*)(%s=*)(%s=*)(%s>=%s)(!(%s=%s)))",
                id_ctx->opts->service_map[SDAP_OC_SERVICE].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_NAME].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_PORT].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_PROTOCOL].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_USN].name,
                id_ctx->srv_opts->max_service_value,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_USN].name,
                id_ctx->srv_opts->max_service_value);
    } else {
        state->filter = talloc_asprintf(
                state,
                "(&(objectclass=%s)(%s=*)(%s=*)(%s=*))",
                id_ctx->opts->service_map[SDAP_OC_SERVICE].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_NAME].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_PORT].name,
                id_ctx->opts->service_map[SDAP_AT_SERVICE_PROTOCOL].name);
    }
    if (!state->filter) {
        DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to build base filter\n"));
        ret = ENOMEM;
        goto fail;
    }

    /* TODO: handle attrs_type */
    ret = build_attrs_from_map(state, id_ctx->opts->service_map,
                               SDAP_OPTS_SERVICES, NULL,
                               &state->attrs, NULL);
    if (ret != EOK) goto fail;

    subreq = sdap_get_services_send(state, state->ev,
                                    state->domain, state->sysdb,
                                    state->id_ctx->opts,
                                    state->id_ctx->opts->service_search_bases,
                                    sdap_id_op_handle(state->op),
                                    state->attrs, state->filter,
                                    dp_opt_get_int(state->id_ctx->opts->basic,
                                                   SDAP_SEARCH_TIMEOUT),
                                    true);
    if (!subreq) {
        ret = ENOMEM;
        goto fail;
    }
    tevent_req_set_callback(subreq, enum_services_op_done, req);

    return req;

fail:
    tevent_req_error(req, ret);
    tevent_req_post(req, ev);
    return req;
}

static void
enum_services_op_done(struct tevent_req *subreq)
{
    struct tevent_req *req =
            tevent_req_callback_data(subreq, struct tevent_req);
    struct enum_services_state *state =
            tevent_req_data(req, struct enum_services_state);
    char *usn_value;
    char *endptr = NULL;
    unsigned usn_number;
    int ret;

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

    if (usn_value) {
        talloc_zfree(state->id_ctx->srv_opts->max_service_value);
        state->id_ctx->srv_opts->max_service_value =
                talloc_steal(state->id_ctx, usn_value);

        usn_number = strtoul(usn_value, &endptr, 10);
        if ((endptr == NULL || (*endptr == '\0' && endptr != usn_value))
            && (usn_number > state->id_ctx->srv_opts->last_usn)) {
            state->id_ctx->srv_opts->last_usn = usn_number;
        }
    }

    DEBUG(SSSDBG_FUNC_DATA, ("Services higher USN value: [%s]\n",
              state->id_ctx->srv_opts->max_service_value));

    tevent_req_done(req);
}

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

    return EOK;
}