summaryrefslogtreecommitdiff
path: root/src/providers/ldap/sdap_access.c
blob: 4ebd72769bcfb49e7414f8d6ee6e784f4817cd03 (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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
    SSSD

    sdap_access.c

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2010 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 <sys/param.h>
#include <security/pam_modules.h>
#include <talloc.h>
#include <tevent.h>
#include <errno.h>

#include "util/util.h"
#include "db/sysdb.h"
#include "providers/ldap/ldap_common.h"
#include "providers/ldap/sdap.h"
#include "providers/ldap/sdap_access.h"
#include "providers/ldap/sdap_async.h"
#include "providers/data_provider.h"
#include "providers/dp_backend.h"

static void sdap_access_reply(struct be_req *be_req, int pam_status)
{
    struct pam_data *pd;
    pd = talloc_get_type(be_req->req_data, struct pam_data);
    pd->pam_status = pam_status;

    if (pam_status == PAM_SUCCESS || pam_status == PAM_PERM_DENIED) {
        be_req->fn(be_req, DP_ERR_OK, pam_status, NULL);
    }

    else {
        be_req->fn(be_req, DP_ERR_FATAL, pam_status, NULL);
    }
}

static struct tevent_req *sdap_access_send(TALLOC_CTX *mem_ctx,
                                           struct tevent_context *ev,
                                           struct be_ctx *be_ctx,
                                           struct sdap_access_ctx *access_ctx,
                                           const char *username);

static struct tevent_req *sdap_access_filter_send(TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct be_ctx *be_ctx,
                                             struct sdap_access_ctx *access_ctx,
                                             const char *username,
                                             struct ldb_message *user_entry);
static void sdap_access_filter_done(struct tevent_req *subreq);

static struct tevent_req *sdap_account_expired_send(TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct be_ctx *be_ctx,
                                             struct sdap_access_ctx *access_ctx,
                                             const char *username,
                                             struct ldb_message *user_entry);
static void sdap_account_expired_done(struct tevent_req *subreq);

static void sdap_access_done(struct tevent_req *req);
void sdap_pam_access_handler(struct be_req *breq)
{
    struct pam_data *pd;
    struct tevent_req *req;
    struct sdap_access_ctx *access_ctx;

    pd = talloc_get_type(breq->req_data, struct pam_data);

    access_ctx =
            talloc_get_type(breq->be_ctx->bet_info[BET_ACCESS].pvt_bet_data,
                            struct sdap_access_ctx);

    req = sdap_access_send(breq,
                           breq->be_ctx->ev,
                           breq->be_ctx,
                           access_ctx,
                           pd->user);
    if (req == NULL) {
        DEBUG(1, ("Unable to start sdap_access request\n"));
        sdap_access_reply(breq, PAM_SYSTEM_ERR);
        return;
    }

    tevent_req_set_callback(req, sdap_access_done, breq);
}

struct sdap_access_req_ctx {
    const char *username;
    struct tevent_context *ev;
    struct sdap_access_ctx *access_ctx;
    struct be_ctx *be_ctx;
    int pam_status;
    struct ldb_message *user_entry;
    size_t current_rule;
};

static errno_t select_next_rule(struct tevent_req *req);
static struct tevent_req *sdap_access_send(TALLOC_CTX *mem_ctx,
                                           struct tevent_context *ev,
                                           struct be_ctx *be_ctx,
                                           struct sdap_access_ctx *access_ctx,
                                           const char *username)
{
    errno_t ret;
    struct sdap_access_req_ctx *state;
    struct tevent_req *req;
    struct ldb_result *res;
    const char *attrs[] = { "*", NULL };

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

    state->be_ctx = be_ctx;
    state->username = username;
    state->pam_status = PAM_SYSTEM_ERR;
    state->ev = ev;
    state->access_ctx = access_ctx;
    state->current_rule = 0;

    DEBUG(6, ("Performing access check for user [%s]\n", username));

    if (access_ctx->access_rule[0] == LDAP_ACCESS_EMPTY) {
        DEBUG(3, ("No access rules defined, access denied.\n"));
        state->pam_status = PAM_PERM_DENIED;
        ret = EOK;
        goto done;
    }

    /* Get original user DN */
    ret = sysdb_get_user_attr(state, be_ctx->sysdb, be_ctx->domain,
                              username, attrs,
                              &res);
    if (ret != EOK) {
        if (ret == ENOENT) {
            /* If we can't find the user, return permission denied */
            state->pam_status = PAM_PERM_DENIED;
            ret = EOK;
            goto done;
        }
        goto done;
    }
    else {
        if (res->count == 0) {
            /* If we can't find the user, return permission denied */
            state->pam_status = PAM_PERM_DENIED;
            ret = EOK;
            goto done;
        }

        if (res->count != 1) {
            DEBUG(1, ("Invalid response from sysdb_get_user_attr\n"));
            ret = EINVAL;
            goto done;
        }
    }

    state->user_entry = res->msgs[0];

    ret = select_next_rule(req);
    if (ret != EOK) {
        if (ret == EACCES) {
            state->pam_status = PAM_PERM_DENIED;
            ret = EOK;
            goto done;
        }
        DEBUG(1, ("select_next_rule failed.\n"));
        goto done;
    }

    return req;

done:
    if (ret == EOK) {
        tevent_req_done(req);
    } else {
        tevent_req_error(req, ret);
    }
    tevent_req_post(req, ev);
    return req;
}

static errno_t select_next_rule(struct tevent_req *req)
{
    struct sdap_access_req_ctx *state =
            tevent_req_data(req, struct sdap_access_req_ctx);
    struct tevent_req *subreq;

    switch (state->access_ctx->access_rule[state->current_rule]) {
        case LDAP_ACCESS_EMPTY:
            return ENOENT;
            break;
        case LDAP_ACCESS_FILTER:
            subreq = sdap_access_filter_send(state, state->ev, state->be_ctx,
                                             state->access_ctx, state->username,
                                             state->user_entry);
            if (subreq == NULL) {
                DEBUG(1, ("sdap_access_filter_send failed.\n"));
                return ENOMEM;
            }

            tevent_req_set_callback(subreq, sdap_access_filter_done, req);
            return EOK;
        case LDAP_ACCESS_EXPIRE:
            subreq = sdap_account_expired_send(state, state->ev, state->be_ctx,
                                               state->access_ctx,
                                               state->username,
                                               state->user_entry);
            if (subreq == NULL) {
                DEBUG(1, ("sdap_account_expired_send failed.\n"));
                return ENOMEM;
            }

            tevent_req_set_callback(subreq, sdap_account_expired_done, req);
            return EOK;
        default:
            DEBUG(1, ("Unexpected access rule type. Access denied.\n"));
    }

    return EACCES;
}

static void next_access_rule(struct tevent_req *req)
{
    struct sdap_access_req_ctx *state =
            tevent_req_data(req, struct sdap_access_req_ctx);
    int ret;

    if (state->pam_status == PAM_PERM_DENIED ||
        state->pam_status == PAM_ACCT_EXPIRED) {
        tevent_req_done(req);
        return;
    }

    state->current_rule++;

    ret = select_next_rule(req);
    if (ret != EOK) {
        if (ret == ENOENT) {
            state->pam_status = PAM_SUCCESS;
            tevent_req_done(req);
            return;
        } else if (ret == EACCES) {
            state->pam_status = PAM_PERM_DENIED;
            tevent_req_done(req);
        } else {
            tevent_req_error(req, ret);
        }
    }

    return;
}


static errno_t sdap_account_expired_shadow(const char *username,
                                           struct ldb_message *user_entry,
                                           int *pam_status)
{
    int ret;
    const char *val;
    long sp_expire;
    long today;

    DEBUG(6, ("Performing access shadow check for user [%s]\n", username));

    val = ldb_msg_find_attr_as_string(user_entry, SYSDB_SHADOWPW_EXPIRE, NULL);
    if (val == NULL) {
        DEBUG(3, ("Shadow expire attribute not found. "
                  "Access will be granted.\n"));
        *pam_status = PAM_SUCCESS;
        return EOK;
    }
    ret = string_to_shadowpw_days(val, &sp_expire);
    if (ret != EOK) {
        DEBUG(1, ("Failed to retrieve shadow expire date.\n"));
        return ret;
    }

    today = (long) (time(NULL) / (60 * 60 * 24));
    if (sp_expire > 0 && today > sp_expire) {
        *pam_status = PAM_ACCT_EXPIRED;
    } else {
        *pam_status = PAM_SUCCESS;
    }

    return EOK;
}

struct sdap_account_expired_req_ctx {
    int pam_status;
};

static struct tevent_req *sdap_account_expired_send(TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct be_ctx *be_ctx,
                                             struct sdap_access_ctx *access_ctx,
                                             const char *username,
                                             struct ldb_message *user_entry)
{
    struct tevent_req *req;
    struct sdap_account_expired_req_ctx *state;
    int ret;
    const char *expire;

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

    state->pam_status = PAM_SYSTEM_ERR;

    expire = dp_opt_get_cstring(access_ctx->id_ctx->opts->basic,
                                SDAP_ACCOUNT_EXPIRE_POLICY);
    if (expire == NULL) {
        DEBUG(1, ("Missing account expire policy. Access denied\n"));
        state->pam_status = PAM_PERM_DENIED;
        ret = EOK;
        goto done;
    } else {
        if (strcasecmp(expire, LDAP_ACCOUNT_EXPIRE_SHADOW) == 0) {
            ret = sdap_account_expired_shadow(username, user_entry,
                                              &state->pam_status);
            if (ret != EOK) {
                DEBUG(1, ("sdap_account_expired_shadow failed.\n"));
                goto done;
            }
        } else {
            DEBUG(1, ("Unsupported LDAP account expire policy [%s]. "
                      "Access denied.\n", expire));
            state->pam_status = PAM_PERM_DENIED;
            ret = EOK;
            goto done;
        }
    }

    ret = EOK;

done:
    if (ret == EOK) {
        tevent_req_done(req);
    } else {
        tevent_req_error(req, ret);
    }
    tevent_req_post(req, ev);

    return req;
}

static errno_t sdap_account_expired_recv(struct tevent_req *req, int *pam_status)
{
    struct sdap_account_expired_req_ctx *state =
            tevent_req_data(req, struct sdap_account_expired_req_ctx);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *pam_status = state->pam_status;

    return EOK;
}

static void sdap_account_expired_done(struct tevent_req *subreq)
{
    errno_t ret;
    struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
    struct sdap_access_req_ctx *state =
            tevent_req_data(req, struct sdap_access_req_ctx);

    ret = sdap_account_expired_recv(subreq, &state->pam_status);
    talloc_zfree(subreq);
    if (ret != EOK) {
        DEBUG(1, ("Error retrieving access check result.\n"));
        state->pam_status = PAM_SYSTEM_ERR;
        tevent_req_error(req, ret);
        return;
    }

    next_access_rule(req);

    return;
}



struct sdap_access_filter_req_ctx {
    const char *username;
    const char *filter;
    struct tevent_context *ev;
    struct sdap_access_ctx *access_ctx;
    struct sdap_id_ctx *sdap_ctx;
    struct sdap_id_op *sdap_op;
    struct sysdb_handle *handle;
    struct be_ctx *be_ctx;
    int pam_status;
    bool cached_access;
    char *basedn;
};

static void sdap_access_filter_decide_offline(struct tevent_req *req);
static int sdap_access_filter_retry(struct tevent_req *req);
static void sdap_access_filter_connect_done(struct tevent_req *subreq);
static void sdap_access_filter_get_access_done(struct tevent_req *req);
static struct tevent_req *sdap_access_filter_send(TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct be_ctx *be_ctx,
                                             struct sdap_access_ctx *access_ctx,
                                             const char *username,
                                             struct ldb_message *user_entry)
{
    errno_t ret;
    struct sdap_access_filter_req_ctx *state;
    struct tevent_req *req;
    const char *basedn;
    char *clean_username;

    req = tevent_req_create(mem_ctx, &state, struct sdap_access_filter_req_ctx);
    if (req == NULL) {
        return NULL;
    }

    if (access_ctx->filter == NULL || *access_ctx->filter == '\0') {
        /* If no filter is set, default to restrictive */
        DEBUG(6, ("No filter set. Access is denied.\n"));
        state->pam_status = PAM_PERM_DENIED;
        tevent_req_done(req);
        tevent_req_post(req, be_ctx->ev);
        return req;
    }

    state->filter = NULL;
    state->be_ctx = be_ctx;
    state->username = username;
    state->pam_status = PAM_SYSTEM_ERR;
    state->sdap_ctx = access_ctx->id_ctx;
    state->ev = ev;
    state->access_ctx = access_ctx;

    DEBUG(6, ("Performing access filter check for user [%s]\n", username));

    state->cached_access = ldb_msg_find_attr_as_bool(user_entry,
                                                     SYSDB_LDAP_ACCESS_FILTER,
                                                     false);
    /* Ok, we have one result, check if we are online or offline */
    if (be_is_offline(state->be_ctx)) {
        /* Ok, we're offline. Return from the cache */
        sdap_access_filter_decide_offline(req);
        goto finished;
    }

    /* Perform online operation */
    basedn = ldb_msg_find_attr_as_string(user_entry,
                                         SYSDB_ORIG_DN,
                                         NULL);
    if(basedn == NULL) {
        DEBUG(1,("Could not find originalDN for user [%s]\n",
                 state->username));
        goto failed;
    }

    state->basedn = talloc_strdup(state, basedn);
    if (state->basedn == NULL) {
        DEBUG(1, ("Could not allocate memory for originalDN\n"));
        goto failed;
    }

    /* Construct the filter */

    ret = sss_filter_sanitize(state, state->username, &clean_username);
    if (ret != EOK) {
        goto failed;
    }

    state->filter = talloc_asprintf(
        state,
        "(&(%s=%s)(objectclass=%s)%s)",
        state->sdap_ctx->opts->user_map[SDAP_AT_USER_NAME].name,
        clean_username,
        state->sdap_ctx->opts->user_map[SDAP_OC_USER].name,
        state->access_ctx->filter);
    if (state->filter == NULL) {
        DEBUG(0, ("Could not construct access filter\n"));
        goto failed;
    }
    talloc_zfree(clean_username);

    DEBUG(6, ("Checking filter against LDAP\n"));

    state->sdap_op = sdap_id_op_create(state, state->sdap_ctx->conn_cache);
    if (!state->sdap_op) {
        DEBUG(2, ("sdap_id_op_create failed\n"));
        goto failed;
    }

    ret = sdap_access_filter_retry(req);
    if (ret != EOK) {
        goto failed;
    }

    return req;

failed:
    talloc_free(req);
    return NULL;

finished:
    tevent_req_done(req);
    tevent_req_post(req, ev);
    return req;
}

static void sdap_access_filter_decide_offline(struct tevent_req *req)
{
    struct sdap_access_filter_req_ctx *state =
            tevent_req_data(req, struct sdap_access_filter_req_ctx);

    if (state->cached_access) {
        DEBUG(6, ("Access granted by cached credentials\n"));
        state->pam_status = PAM_SUCCESS;
    } else {
        DEBUG(6, ("Access denied by cached credentials\n"));
        state->pam_status = PAM_PERM_DENIED;
    }
}

static int sdap_access_filter_retry(struct tevent_req *req)
{
    struct sdap_access_filter_req_ctx *state =
            tevent_req_data(req, struct sdap_access_filter_req_ctx);
    struct tevent_req *subreq;
    int ret;

    subreq = sdap_id_op_connect_send(state->sdap_op, state, &ret);
    if (!subreq) {
        DEBUG(2, ("sdap_id_op_connect_send failed: %d (%s)\n", ret, strerror(ret)));
        return ret;
    }

    tevent_req_set_callback(subreq, sdap_access_filter_connect_done, req);
    return EOK;
}

static void sdap_access_filter_connect_done(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct sdap_access_filter_req_ctx *state =
            tevent_req_data(req, struct sdap_access_filter_req_ctx);
    int ret, dp_error;

    ret = sdap_id_op_connect_recv(subreq, &dp_error);
    talloc_zfree(subreq);

    if (ret != EOK) {
        if (dp_error == DP_ERR_OFFLINE) {
            sdap_access_filter_decide_offline(req);
            tevent_req_done(req);
            return;
        }

        tevent_req_error(req, ret);
        return;
    }

    /* Connection to LDAP succeeded
     * Send filter request
     */
    subreq = sdap_get_generic_send(state,
                                   state->ev,
                                   state->sdap_ctx->opts,
                                   sdap_id_op_handle(state->sdap_op),
                                   state->basedn,
                                   LDAP_SCOPE_BASE,
                                   state->filter, NULL,
                                   NULL, 0);
    if (subreq == NULL) {
        DEBUG(1, ("Could not start LDAP communication\n"));
        state->pam_status = PAM_SYSTEM_ERR;
        tevent_req_error(req, EIO);
        return;
    }

    tevent_req_set_callback(subreq, sdap_access_filter_get_access_done, req);
}

static void sdap_access_filter_get_access_done(struct tevent_req *subreq)
{
    int ret, dp_error;
    size_t num_results;
    bool found = false;
    struct sysdb_attrs *attrs;
    struct sysdb_attrs **results;
    struct tevent_req *req =
            tevent_req_callback_data(subreq, struct tevent_req);
    struct sdap_access_filter_req_ctx *state =
            tevent_req_data(req, struct sdap_access_filter_req_ctx);
    ret = sdap_get_generic_recv(subreq, state,
                                &num_results, &results);
    talloc_zfree(subreq);

    ret = sdap_id_op_done(state->sdap_op, ret, &dp_error);
    if (ret != EOK) {
        if (dp_error == DP_ERR_OK) {
            /* retry */
            ret = sdap_access_filter_retry(req);
            if (ret == EOK) {
                return;
            }
            state->pam_status = PAM_SYSTEM_ERR;
        } else if (dp_error == DP_ERR_OFFLINE) {
            sdap_access_filter_decide_offline(req);
        } else {
            DEBUG(1, ("sdap_get_generic_send() returned error [%d][%s]\n",
                      ret, strerror(ret)));
            state->pam_status = PAM_SYSTEM_ERR;
        }

        goto done;
    }

    /* Check the number of responses we got
     * If it's exactly 1, we passed the check
     * If it's < 1, we failed the check
     * Anything else is an error
     */
    if (num_results < 1) {
        DEBUG(4, ("User [%s] was not found with the specified filter. "
                  "Denying access.\n", state->username));
        found = false;
    }
    else if (results == NULL) {
        DEBUG(1, ("num_results > 0, but results is NULL\n"));
        ret = EIO;
        state->pam_status = PAM_SYSTEM_ERR;
        goto done;
    }
    else if (num_results > 1) {
        /* It should not be possible to get more than one reply
         * here, since we're doing a base-scoped search
         */
        DEBUG(1, ("Received multiple replies\n"));
        ret = EIO;
        state->pam_status = PAM_SYSTEM_ERR;
        goto done;
    }
    else { /* Ok, we got a single reply */
        found = true;
    }

    if (found) {
        /* Save "allow" to the cache for future offline
         * access checks.
         */
        DEBUG(6, ("Access granted by online lookup\n"));
        state->pam_status = PAM_SUCCESS;
    }
    else {
        /* Save "disallow" to the cache for future offline
         * access checks.
         */
        DEBUG(6, ("Access denied by online lookup\n"));
        state->pam_status = PAM_PERM_DENIED;
    }

    attrs = sysdb_new_attrs(state);
    if (attrs == NULL) {
        ret = ENOMEM;
        DEBUG(1, ("Could not set up attrs\n"));
        goto done;
    }

    ret = sysdb_attrs_add_bool(attrs, SYSDB_LDAP_ACCESS_FILTER,
                               state->pam_status == PAM_SUCCESS ?
                                                    true :
                                                    false);
    if (ret != EOK) {
        /* Failing to save to the cache is non-fatal.
         * Just return the result.
         */
        ret = EOK;
        DEBUG(1, ("Could not set up attrs\n"));
        goto done;
    }

    ret = sysdb_set_user_attr(attrs,
                              state->be_ctx->sysdb,
                              state->be_ctx->domain,
                              state->username,
                              attrs, SYSDB_MOD_REP);
    if (ret != EOK) {
        /* Failing to save to the cache is non-fatal.
         * Just return the result.
         */
        ret = EOK;
        DEBUG(1, ("Failed to set user access attribute\n"));
        goto done;
    }

done:
    if (ret == EOK) {
        tevent_req_done(req);
    }
    else {
        tevent_req_error(req, ret);
    }
}

static errno_t sdap_access_filter_recv(struct tevent_req *req, int *pam_status)
{
    struct sdap_access_filter_req_ctx *state =
            tevent_req_data(req, struct sdap_access_filter_req_ctx);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *pam_status = state->pam_status;

    return EOK;
}

static void sdap_access_filter_done(struct tevent_req *subreq)
{
    errno_t ret;
    struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
    struct sdap_access_req_ctx *state =
            tevent_req_data(req, struct sdap_access_req_ctx);

    ret = sdap_access_filter_recv(subreq, &state->pam_status);
    talloc_zfree(subreq);
    if (ret != EOK) {
        DEBUG(1, ("Error retrieving access check result.\n"));
        state->pam_status = PAM_SYSTEM_ERR;
        tevent_req_error(req, ret);
        return;
    }

    next_access_rule(req);

    return;
}

static errno_t sdap_access_recv(struct tevent_req *req, int *pam_status)
{
    struct sdap_access_req_ctx *state =
            tevent_req_data(req, struct sdap_access_req_ctx);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *pam_status = state->pam_status;

    return EOK;
}

static void sdap_access_done(struct tevent_req *req)
{
    errno_t ret;
    int pam_status = PAM_SYSTEM_ERR;
    struct be_req *breq =
            tevent_req_callback_data(req, struct be_req);

    ret = sdap_access_recv(req, &pam_status);
    talloc_zfree(req);
    if (ret != EOK) {
        DEBUG(1, ("Error retrieving access check result.\n"));
        pam_status = PAM_SYSTEM_ERR;
    }

    sdap_access_reply(breq, pam_status);
}