summaryrefslogtreecommitdiffstats
path: root/src/providers/ldap/sdap_online_check.c
blob: f721a5f45d54d3b0b80630abf3172652b50a5bbd (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
/*
    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 <errno.h>
#include <talloc.h>
#include <tevent.h>
#include "util/util.h"
#include "providers/backend.h"
#include "providers/ldap/sdap_async.h"
#include "providers/ldap/ldap_common.h"

struct sdap_online_check_state {
    struct sdap_id_ctx *id_ctx;
    struct be_ctx *be_ctx;
};

static void sdap_online_check_connect_done(struct tevent_req *subreq);
static void sdap_online_check_reinit_done(struct tevent_req *subreq);

static struct tevent_req *sdap_online_check_send(TALLOC_CTX *mem_ctx,
                                                 struct sdap_id_ctx *id_ctx)
{
    struct sdap_online_check_state *state;
    struct tevent_req *subreq;
    struct tevent_req *req;
    struct be_ctx *be_ctx;
    errno_t ret;

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

    state->id_ctx = id_ctx;
    state->be_ctx = be_ctx = id_ctx->be;

    subreq = sdap_cli_connect_send(state, be_ctx->ev, id_ctx->opts, be_ctx,
                                   id_ctx->conn->service, false,
                                   CON_TLS_DFL, false);
    if (subreq == NULL) {
        ret = ENOMEM;
        goto immediately;
    }

    tevent_req_set_callback(subreq, sdap_online_check_connect_done, req);

    return req;

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

    return req;
}

static void sdap_online_check_connect_done(struct tevent_req *subreq)
{
    struct sdap_online_check_state *state;
    struct sdap_server_opts *srv_opts;
    struct sdap_id_ctx *id_ctx;
    struct tevent_req *req;
    bool can_retry;
    bool reinit = false;
    errno_t ret;

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

    id_ctx = state->id_ctx;

    ret = sdap_cli_connect_recv(subreq, state, &can_retry, NULL, &srv_opts);
    talloc_zfree(subreq);
    if (ret != EOK) {
        if (can_retry == false) {
            ret = ERR_OFFLINE;
        }

        goto done;
    } else {
        if (id_ctx->srv_opts == NULL) {
            srv_opts->max_user_value = 0;
            srv_opts->max_group_value = 0;
            srv_opts->max_service_value = 0;
            srv_opts->max_sudo_value = 0;
        } else if (strcmp(srv_opts->server_id, id_ctx->srv_opts->server_id) == 0
                   && srv_opts->supports_usn
                   && id_ctx->srv_opts->last_usn > srv_opts->last_usn) {
            id_ctx->srv_opts->max_user_value = 0;
            id_ctx->srv_opts->max_group_value = 0;
            id_ctx->srv_opts->max_service_value = 0;
            id_ctx->srv_opts->max_sudo_value = 0;
            id_ctx->srv_opts->last_usn = srv_opts->last_usn;

            reinit = true;
        }

        sdap_steal_server_opts(id_ctx, &srv_opts);
    }

    if (reinit) {
        DEBUG(SSSDBG_TRACE_FUNC, "Server reinitialization detected. "
              "Cleaning cache.\n");
        subreq = sdap_reinit_cleanup_send(state, state->be_ctx, id_ctx);
        if (subreq == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to perform reinitialization "
                  "clean up.\n");
            /* not fatal */
            goto done;
        }

        tevent_req_set_callback(subreq, sdap_online_check_reinit_done, req);
        return;
    }

    ret = EOK;

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

    tevent_req_done(req);
}

static void sdap_online_check_reinit_done(struct tevent_req *subreq)
{
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_callback_data(subreq, struct tevent_req);

    ret = sdap_reinit_cleanup_recv(subreq);
    talloc_zfree(subreq);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to perform reinitialization "
              "clean up [%d]: %s\n", ret, strerror(ret));
        /* not fatal */
    } else {
        DEBUG(SSSDBG_TRACE_FUNC, "Reinitialization clean up completed\n");
    }

    tevent_req_done(req);
}

static errno_t sdap_online_check_recv(struct tevent_req *req)
{
    TEVENT_REQ_RETURN_ON_ERROR(req);

    return EOK;
}

struct sdap_online_check_handler_state {
    struct dp_reply_std reply;
};

static void sdap_online_check_handler_done(struct tevent_req *subreq);

struct tevent_req *
sdap_online_check_handler_send(TALLOC_CTX *mem_ctx,
                               struct sdap_id_ctx *id_ctx,
                               void *data,
                               struct dp_req_params *params)
{
    struct sdap_online_check_handler_state *state;
    struct tevent_req *subreq;
    struct tevent_req *req;
    errno_t ret;

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

    subreq = sdap_online_check_send(state, id_ctx);
    if (subreq == NULL) {
        ret = ENOMEM;
        goto immediately;
    }

    tevent_req_set_callback(subreq, sdap_online_check_handler_done, req);

    return req;

immediately:
    dp_reply_std_set(&state->reply, DP_ERR_DECIDE, ret, NULL);

    /* TODO For backward compatibility we always return EOK to DP now. */
    tevent_req_done(req);
    tevent_req_post(req, params->ev);

    return req;
}

static void sdap_online_check_handler_done(struct tevent_req *subreq)
{
    struct sdap_online_check_handler_state *state;
    struct tevent_req *req;
    errno_t ret;

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

    ret = sdap_online_check_recv(subreq);
    talloc_zfree(subreq);

    /* TODO For backward compatibility we always return EOK to DP now. */
    dp_reply_std_set(&state->reply, DP_ERR_DECIDE, ret, NULL);
    tevent_req_done(req);
}

errno_t sdap_online_check_handler_recv(TALLOC_CTX *mem_ctx,
                                       struct tevent_req *req,
                                       struct dp_reply_std *data)
{
    struct sdap_online_check_handler_state *state = NULL;

    state = tevent_req_data(req, struct sdap_online_check_handler_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *data = state->reply;

    return EOK;
}