summaryrefslogtreecommitdiff
path: root/server/providers/ipa/ipa_auth.c
blob: 86b72e49531b5141bfdc8fa9bafe76d1f3624331 (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
/*
    SSSD

    IPA Backend Module -- Authentication

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2009 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 "util/util.h"
#include "providers/ldap/ldap_common.h"
#include "providers/ldap/sdap_async.h"
#include "providers/krb5/krb5_auth.h"
#include "providers/ipa/ipa_common.h"

struct ipa_auth_ctx {
    struct sdap_auth_ctx *sdap_auth_ctx;
    struct krb5_ctx *krb5_ctx;
    struct be_req *be_req;
    be_async_callback_t callback;
    void *pvt;
    bool password_migration;

    int dp_err_type;
    int errnum;
    char *errstr;
};

static void ipa_auth_reply(struct ipa_auth_ctx *ipa_auth_ctx)
{
    struct pam_data *pd;
    struct be_req *be_req = ipa_auth_ctx->be_req;
    be_req->fn = ipa_auth_ctx->callback;
    be_req->pvt = ipa_auth_ctx->pvt;
    be_req->be_ctx->bet_info[BET_AUTH].pvt_bet_data = ipa_auth_ctx->krb5_ctx;
    pd = talloc_get_type(be_req->req_data, struct pam_data);
    int dp_err_type = ipa_auth_ctx->dp_err_type;
    char *errstr = ipa_auth_ctx->errstr;

    talloc_zfree(ipa_auth_ctx);
    DEBUG(9, ("sending [%d] [%d] [%s].\n", dp_err_type, pd->pam_status,
                                           errstr));

    be_req->fn(be_req, dp_err_type, pd->pam_status, errstr);
}

struct ipa_auth_handler_state {
    struct tevent_context *ev;

    int dp_err_type;
    int errnum;
    char *errstr;
};

static void ipa_auth_handler_callback(struct be_req *be_req,
                                   int dp_err_type,
                                   int errnum,
                                   const char *errstr);

static struct tevent_req *ipa_auth_handler_send(TALLOC_CTX *memctx,
                                            struct tevent_context *ev,
                                            struct be_req *be_req,
                                            be_req_fn_t auth_handler)
{
    struct ipa_auth_handler_state *state;
    struct tevent_req *req;

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

    state->ev = ev;

    be_req->fn = ipa_auth_handler_callback;
    be_req->pvt = req;

    auth_handler(be_req);

    return req;
}

static void ipa_auth_handler_callback(struct be_req *be_req,
                                   int dp_err_type,
                                   int errnum,
                                   const char *errstr)
{
    struct tevent_req *req = talloc_get_type(be_req->pvt, struct tevent_req);
    struct ipa_auth_handler_state *state = tevent_req_data(req,
                                                 struct ipa_auth_handler_state);

    DEBUG(9, ("received from handler [%d] [%d] [%s].\n", dp_err_type, errnum,
                                                         errstr));
    state->dp_err_type = dp_err_type;
    state->errnum = errnum;
    state->errstr = talloc_strdup(state, errstr);

    tevent_req_post(req, state->ev);
    tevent_req_done(req);
    return;
}

static int ipa_auth_handler_recv(struct tevent_req *req, TALLOC_CTX *memctx,
                              int *dp_err_type, int *errnum,
                              char **errstr)
{
    struct ipa_auth_handler_state *state = tevent_req_data(req,
                                                 struct ipa_auth_handler_state);
    enum tevent_req_state tstate;
    uint64_t err;

    if (tevent_req_is_error(req, &tstate, &err)) {
        if (err) return err;
        return EIO;
    }

    *dp_err_type = state->dp_err_type;
    *errnum = state->errnum;
    *errstr = talloc_steal(memctx, state->errstr);

    return EOK;
}


static void ipa_auth_handler_done(struct tevent_req *req);
static void ipa_auth_ldap_done(struct tevent_req *req);
static void ipa_auth_handler_retry_done(struct tevent_req *req);

void ipa_auth(struct be_req *be_req)
{
    struct tevent_req *req;
    struct ipa_auth_ctx *ipa_auth_ctx;
    struct sdap_id_ctx *sdap_id_ctx;

    ipa_auth_ctx = talloc_zero(be_req, struct ipa_auth_ctx);
    if (ipa_auth_ctx == NULL) {
        DEBUG(1, ("talloc failed.\n"));
        be_req->fn(be_req, DP_ERR_FATAL, PAM_SYSTEM_ERR, NULL);
    }

    ipa_auth_ctx->callback = be_req->fn;
    ipa_auth_ctx->pvt = be_req->pvt;

    ipa_auth_ctx->be_req = be_req;

    ipa_auth_ctx->sdap_auth_ctx = talloc_zero(ipa_auth_ctx,
                                              struct sdap_auth_ctx);
    if (ipa_auth_ctx->sdap_auth_ctx == NULL) {
        DEBUG(1, ("talloc failed.\n"));
        goto fail;
    }

    sdap_id_ctx = talloc_get_type(
                              be_req->be_ctx->bet_info[BET_ID].pvt_bet_data,
                              struct sdap_id_ctx);
    ipa_auth_ctx->sdap_auth_ctx->be = sdap_id_ctx->be;
    ipa_auth_ctx->sdap_auth_ctx->opts = sdap_id_ctx->opts;

    ipa_auth_ctx->krb5_ctx = talloc_get_type(
                              be_req->be_ctx->bet_info[BET_AUTH].pvt_bet_data,
                              struct krb5_ctx);

/* TODO: test and activate when server side support is available */
    ipa_auth_ctx->password_migration = false;

    ipa_auth_ctx->dp_err_type = DP_ERR_FATAL;
    ipa_auth_ctx->errnum = EIO;
    ipa_auth_ctx->errstr = NULL;

    req = ipa_auth_handler_send(ipa_auth_ctx, be_req->be_ctx->ev, be_req,
                                krb5_pam_handler);
    if (req == NULL) {
        DEBUG(1, ("ipa_auth_handler_send failed.\n"));
        goto fail;
    }

    tevent_req_set_callback(req, ipa_auth_handler_done, ipa_auth_ctx);
    return;

fail:
    ipa_auth_reply(ipa_auth_ctx);
}

static void ipa_auth_handler_done(struct tevent_req *req)
{
    struct ipa_auth_ctx *ipa_auth_ctx = tevent_req_callback_data(req,
                                                           struct ipa_auth_ctx);
    struct pam_data *pd;
    struct be_req *be_req;
    int ret;

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

    ret = ipa_auth_handler_recv(req, ipa_auth_ctx, &ipa_auth_ctx->dp_err_type,
                                &ipa_auth_ctx->errnum, &ipa_auth_ctx->errstr);
    talloc_zfree(req);
    if (ret != EOK) {
        DEBUG(1, ("ipa_auth_handler request failed.\n"));
        pd->pam_status = PAM_SYSTEM_ERR;
        goto done;
    }
    if (ipa_auth_ctx->dp_err_type != DP_ERR_OK) {
        pd->pam_status = ipa_auth_ctx->errnum;
        goto done;
    }

    if (ipa_auth_ctx->password_migration && pd->pam_status == PAM_CRED_ERR) {
        DEBUG(1, ("Assuming Kerberos password is missing, "
                  "starting password migration.\n"));
        be_req->be_ctx->bet_info[BET_AUTH].pvt_bet_data =
                                                    ipa_auth_ctx->sdap_auth_ctx;
        req = ipa_auth_handler_send(ipa_auth_ctx, be_req->be_ctx->ev, be_req,
                                    sdap_pam_auth_handler);
        if (req == NULL) {
            DEBUG(1, ("ipa_auth_ldap_send failed.\n"));
            goto done;
        }

        tevent_req_set_callback(req, ipa_auth_ldap_done, ipa_auth_ctx);
        return;
    }

done:
    ipa_auth_reply(ipa_auth_ctx);
}

static void ipa_auth_ldap_done(struct tevent_req *req)
{
    struct ipa_auth_ctx *ipa_auth_ctx = tevent_req_callback_data(req,
                                                           struct ipa_auth_ctx);
    struct pam_data *pd;
    struct be_req *be_req;
    int ret;

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

    ret = ipa_auth_handler_recv(req, ipa_auth_ctx, &ipa_auth_ctx->dp_err_type,
                                &ipa_auth_ctx->errnum, &ipa_auth_ctx->errstr);
    talloc_zfree(req);
    if (ret != EOK) {
        DEBUG(1, ("ipa_auth_handler request failed.\n"));
        pd->pam_status = PAM_SYSTEM_ERR;
        goto done;
    }
    if (ipa_auth_ctx->dp_err_type != DP_ERR_OK) {
        pd->pam_status = ipa_auth_ctx->errnum;
        goto done;
    }

    if (pd->pam_status == PAM_SUCCESS) {
        DEBUG(1, ("LDAP authentication succeded, "
                  "trying Kerberos authentication again.\n"));
        be_req->be_ctx->bet_info[BET_AUTH].pvt_bet_data = ipa_auth_ctx->krb5_ctx;
        req = ipa_auth_handler_send(ipa_auth_ctx, be_req->be_ctx->ev, be_req,
                                    krb5_pam_handler);
        if (req == NULL) {
            DEBUG(1, ("ipa_auth_ldap_send failed.\n"));
            goto done;
        }

        tevent_req_set_callback(req, ipa_auth_handler_retry_done, ipa_auth_ctx);
        return;
    }

done:
    ipa_auth_reply(ipa_auth_ctx);
}

static void ipa_auth_handler_retry_done(struct tevent_req *req)
{
    struct ipa_auth_ctx *ipa_auth_ctx = tevent_req_callback_data(req,
                                                           struct ipa_auth_ctx);
    struct pam_data *pd;
    struct be_req *be_req;
    int ret;

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

    ret = ipa_auth_handler_recv(req, ipa_auth_ctx, &ipa_auth_ctx->dp_err_type,
                                &ipa_auth_ctx->errnum, &ipa_auth_ctx->errstr);
    talloc_zfree(req);
    if (ret != EOK) {
        DEBUG(1, ("ipa_auth_handler request failed.\n"));
        pd->pam_status = PAM_SYSTEM_ERR;
    }
    if (ipa_auth_ctx->dp_err_type != DP_ERR_OK) {
        pd->pam_status = ipa_auth_ctx->errnum;
    }

    ipa_auth_reply(ipa_auth_ctx);
}