summaryrefslogtreecommitdiffstats
path: root/src/providers/krb5/krb5_wait_queue.c
blob: 126209620dcd22889cf7bcb1cbec1890b8891b41 (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
/*
    SSSD

    Kerberos 5 Backend Module - Serialize the request of a user

    Authors:
        Sumit Bose <sbose@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 <tevent.h>
#include <dhash.h>

#include <security/pam_modules.h>

#include "src/providers/krb5/krb5_auth.h"

#define INIT_HASH_SIZE 5

struct queue_entry {
    struct queue_entry *prev;
    struct queue_entry *next;

    struct be_ctx *be_ctx;
    struct be_req *be_req;
    struct tevent_req *parent_req;
    struct pam_data *pd;
    struct krb5_ctx *krb5_ctx;
};

static void wait_queue_auth_done(struct tevent_req *req);

static void krb5_auth_queue_finish(struct tevent_req *req, errno_t ret,
                                   int pam_status, int dp_err);

static void wait_queue_auth(struct tevent_context *ev, struct tevent_timer *te,
                            struct timeval current_time, void *private_data)
{
    struct queue_entry *qe = talloc_get_type(private_data, struct queue_entry);
    struct tevent_req *req;

    req = krb5_auth_send(qe->parent_req, qe->be_ctx->ev,
                         qe->be_ctx, qe->pd, qe->krb5_ctx);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "krb5_auth_send failed.\n");
    } else {
        tevent_req_set_callback(req, wait_queue_auth_done,
                                qe->parent_req);
    }

    talloc_zfree(qe);
}

static void wait_queue_auth_done(struct tevent_req *req)
{
    struct tevent_req *parent_req = \
                tevent_req_callback_data(req, struct tevent_req);
    int pam_status;
    int dp_err;
    errno_t ret;

    ret = krb5_auth_recv(req, &pam_status, &dp_err);
    talloc_zfree(req);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "krb5_auth_recv failed: %d\n", ret);
    }

    krb5_auth_queue_finish(parent_req, ret, pam_status, dp_err);
}

static void wait_queue_del_cb(hash_entry_t *entry, hash_destroy_enum type,
                              void *pvt)
{
    struct queue_entry *head;

    if (entry->value.type == HASH_VALUE_PTR) {
        head = talloc_get_type(entry->value.ptr, struct queue_entry);
        talloc_zfree(head);
        return;
    }

    DEBUG(SSSDBG_CRIT_FAILURE,
          "Unexpected value type [%d].\n", entry->value.type);
}

static errno_t add_to_wait_queue(struct be_ctx *be_ctx,
                                 struct tevent_req *parent_req,
                                 struct pam_data *pd,
                                 struct krb5_ctx *krb5_ctx)
{
    int ret;
    hash_key_t key;
    hash_value_t value;
    struct queue_entry *head;
    struct queue_entry *queue_entry;

    if (krb5_ctx->wait_queue_hash == NULL) {
        ret = sss_hash_create_ex(krb5_ctx, INIT_HASH_SIZE,
                                 &krb5_ctx->wait_queue_hash, 0, 0, 0, 0,
                                 wait_queue_del_cb, NULL);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "sss_hash_create failed\n");
            return ret;
        }
    }

    key.type = HASH_KEY_STRING;
    key.str = pd->user;

    ret = hash_lookup(krb5_ctx->wait_queue_hash, &key, &value);
    switch (ret) {
        case HASH_SUCCESS:
            if (value.type != HASH_VALUE_PTR) {
                DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected hash value type.\n");
                return EINVAL;
            }

            head = talloc_get_type(value.ptr, struct queue_entry);

            queue_entry = talloc_zero(head, struct queue_entry);
            if (queue_entry == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
                return ENOMEM;
            }

            queue_entry->be_ctx = be_ctx;
            queue_entry->parent_req = parent_req;
            queue_entry->pd = pd;
            queue_entry->krb5_ctx = krb5_ctx;

            DLIST_ADD_END(head, queue_entry, struct queue_entry *);

            break;
        case HASH_ERROR_KEY_NOT_FOUND:
            value.type = HASH_VALUE_PTR;
            head = talloc_zero(krb5_ctx->wait_queue_hash, struct queue_entry);
            if (head == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
                return ENOMEM;
            }
            value.ptr = head;

            ret = hash_enter(krb5_ctx->wait_queue_hash, &key, &value);
            if (ret != HASH_SUCCESS) {
                DEBUG(SSSDBG_CRIT_FAILURE, "hash_enter failed.\n");
                talloc_free(head);
                return EIO;
            }

            break;
        default:
            DEBUG(SSSDBG_CRIT_FAILURE, "hash_lookup failed.\n");
            return EIO;
    }

    if (head->next == NULL) {
        return ENOENT;
    } else {
        return EOK;
    }
}

static void check_wait_queue(struct krb5_ctx *krb5_ctx, char *username)
{
    int ret;
    hash_key_t key;
    hash_value_t value;
    struct queue_entry *head;
    struct queue_entry *queue_entry;
    struct tevent_timer *te;

    if (krb5_ctx->wait_queue_hash == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "No wait queue available.\n");
        return;
    }

    key.type = HASH_KEY_STRING;
    key.str = username;

    ret = hash_lookup(krb5_ctx->wait_queue_hash, &key, &value);

    switch (ret) {
        case HASH_SUCCESS:
            if (value.type != HASH_VALUE_PTR) {
                DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected hash value type.\n");
                return;
            }

            head = talloc_get_type(value.ptr, struct queue_entry);

            if (head->next == NULL) {
                DEBUG(SSSDBG_TRACE_LIBS,
                      "Wait queue for user [%s] is empty.\n", username);
            } else {
                queue_entry = head->next;

                DLIST_REMOVE(head, queue_entry);

                te = tevent_add_timer(queue_entry->be_ctx->ev, krb5_ctx,
                                      tevent_timeval_current(), wait_queue_auth,
                                      queue_entry);
                if (te == NULL) {
                    DEBUG(SSSDBG_CRIT_FAILURE, "tevent_add_timer failed.\n");
                } else {
                    return;
                }
            }

            ret = hash_delete(krb5_ctx->wait_queue_hash, &key);
            if (ret != HASH_SUCCESS) {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      "Failed to remove wait queue for user [%s].\n",
                          username);
            }

            break;
        case HASH_ERROR_KEY_NOT_FOUND:
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "No wait queue for user [%s] found.\n", username);
            break;
        default:
            DEBUG(SSSDBG_CRIT_FAILURE, "hash_lookup failed.\n");
    }

    return;
}

struct krb5_auth_queue_state {
    struct krb5_ctx *krb5_ctx;
    struct pam_data *pd;

    int pam_status;
    int dp_err;
};

static void krb5_auth_queue_done(struct tevent_req *subreq);

struct tevent_req *krb5_auth_queue_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct be_ctx *be_ctx,
                                        struct pam_data *pd,
                                        struct krb5_ctx *krb5_ctx)
{
    errno_t ret;
    struct tevent_req *req;
    struct tevent_req *subreq;
    struct krb5_auth_queue_state *state;

    req = tevent_req_create(mem_ctx, &state, struct krb5_auth_queue_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create failed.\n");
        return NULL;
    }
    state->krb5_ctx = krb5_ctx;
    state->pd = pd;

    ret = add_to_wait_queue(be_ctx, req, pd, krb5_ctx);
    if (ret == EOK) {
        DEBUG(SSSDBG_TRACE_LIBS,
              "Request [%p] successfully added to wait queue "
              "of user [%s].\n", req, pd->user);
        ret = EOK;
        goto immediate;
    } else if (ret == ENOENT) {
        DEBUG(SSSDBG_TRACE_LIBS, "Wait queue of user [%s] is empty, "
              "running request [%p] immediately.\n", pd->user, req);
    } else {
        DEBUG(SSSDBG_MINOR_FAILURE,
              "Failed to add request to wait queue of user [%s], "
              "running request [%p] immediately.\n", pd->user, req);
    }

    subreq = krb5_auth_send(req, ev, be_ctx, pd, krb5_ctx);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "krb5_auth_send failed.\n");
        ret = ENOMEM;
        goto immediate;
    }

    tevent_req_set_callback(subreq, krb5_auth_queue_done, req);

    ret = EOK;

immediate:
    if (ret != EOK) {
        tevent_req_error(req, ret);
        tevent_req_post(req, ev);
    }
    return req;
}

static void krb5_auth_queue_done(struct tevent_req *subreq)
{
    struct tevent_req *req = \
                tevent_req_callback_data(subreq, struct tevent_req);
    struct krb5_auth_queue_state *state = \
                tevent_req_data(req, struct krb5_auth_queue_state);
    errno_t ret;

    ret = krb5_auth_recv(subreq, &state->pam_status, &state->dp_err);
    talloc_zfree(subreq);

    check_wait_queue(state->krb5_ctx, state->pd->user);

    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "krb5_auth_recv failed with: %d\n", ret);
        tevent_req_error(req, ret);
        return;
    }

    DEBUG(SSSDBG_TRACE_LIBS, "krb5_auth_queue request [%p] done.\n", req);
    tevent_req_done(req);
}

/* This is a violation of the tevent_req style. Ideally, the wait queue would
 * be rewritten to the tevent_req style in the future, expose per-request recv
 * and not hide the request underneath. But this function allows us to expose
 * a tevent_req API for users of this module
 */
static void krb5_auth_queue_finish(struct tevent_req *req,
                                   errno_t ret,
                                   int pam_status,
                                   int dp_err)
{
    struct krb5_auth_queue_state *state = \
                tevent_req_data(req, struct krb5_auth_queue_state);

    check_wait_queue(state->krb5_ctx, state->pd->user);

    state->pam_status = pam_status;
    state->dp_err = dp_err;
    if (ret != EOK) {
        tevent_req_error(req, ret);
    } else {
        DEBUG(SSSDBG_TRACE_LIBS, "krb5_auth_queue request [%p] done.\n", req);
        tevent_req_done(req);
    }
}

int krb5_auth_queue_recv(struct tevent_req *req,
                         int *_pam_status,
                         int *_dp_err)
{
    struct krb5_auth_queue_state *state = \
                tevent_req_data(req, struct krb5_auth_queue_state);

    /* Returning values even on failure is not typical, but IPA password migration
     * relies on receiving PAM_CRED_ERR even if the request fails..
     */
    if (_pam_status) {
        *_pam_status = state->pam_status;
    }

    if (_dp_err) {
        *_dp_err = state->pam_status;
    }

    TEVENT_REQ_RETURN_ON_ERROR(req);

    return EOK;
}