summaryrefslogtreecommitdiffstats
path: root/src/responder/nss/nss_enum.c
blob: b1cce2cde43ece735285c132d0127c142ee83cb0 (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
/*
    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 <tevent.h>
#include <talloc.h>

#include "util/util.h"
#include "util/sss_ptr_hash.h"
#include "responder/nss/nss_private.h"

typedef errno_t (*nss_setent_set_timeout_fn)(struct tevent_context *ev,
                                             struct nss_ctx *nss_ctx,
                                             struct nss_enum_ctx *enum_ctx);

struct nss_setent_internal_state {
    struct tevent_context *ev;
    struct nss_ctx *nss_ctx;
    struct nss_enum_ctx *enum_ctx;
    nss_setent_set_timeout_fn timeout_handler;
    enum cache_req_type type;
};

static void nss_setent_internal_done(struct tevent_req *subreq);

/* Cache request data is stealed on internal state. */
static struct tevent_req *
nss_setent_internal_send(TALLOC_CTX *mem_ctx,
                         struct tevent_context *ev,
                         struct cli_ctx *cli_ctx,
                         struct cache_req_data *data,
                         enum cache_req_type type,
                         struct nss_enum_ctx *enum_ctx,
                         nss_setent_set_timeout_fn timeout_handler)
{
    struct nss_setent_internal_state *state;
    struct tevent_req *subreq;
    struct tevent_req *req;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state, struct nss_setent_internal_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        return NULL;
    }

    talloc_steal(state, data);

    state->ev = ev;
    state->nss_ctx = talloc_get_type(cli_ctx->rctx->pvt_ctx, struct nss_ctx);
    state->enum_ctx = enum_ctx;
    state->type = type;
    state->timeout_handler = timeout_handler;

    if (state->enum_ctx->is_ready) {
        /* Object is already constructed, just return here. */
        talloc_free(data);
        ret = EOK;
        goto done;
    }

    if (state->enum_ctx->ongoing != NULL) {
        /* Object is being constructed. Register ourselves for
         * notification when it is finished. */
        ret = setent_add_ref(state, &state->enum_ctx->notify_list, req);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unable to register setent reference [%d]: %s!\n",
                  ret, sss_strerror(ret));
            goto done;
        }

        ret = EAGAIN;
        goto done;
    }

    /* Create new object. */
    state->enum_ctx->is_ready = false;
    subreq = cache_req_send(req, ev, cli_ctx->rctx, cli_ctx->rctx->ncache,
                            0, NULL, data);
    if (subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to send cache request!\n");
        ret = ENOMEM;
        goto done;
    }

    tevent_req_set_callback(subreq, nss_setent_internal_done, req);
    state->enum_ctx->ongoing = subreq;

    ret = EAGAIN;

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

    return req;
}

static void nss_setent_internal_done(struct tevent_req *subreq)
{
    struct cache_req_result **result;
    struct nss_setent_internal_state *state;
    struct setent_req_list **notify_list;
    struct tevent_req *req;
    errno_t ret;
    errno_t tret;

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

    /* This is the ongoing request and it is finished. Remove it. */
    state->enum_ctx->ongoing = NULL;

    ret = cache_req_recv(state, subreq, &result);
    talloc_zfree(subreq);

    switch (ret) {
    case EOK:
        talloc_zfree(state->enum_ctx->result);
        state->enum_ctx->result = talloc_steal(state->nss_ctx, result);

        if (state->type == CACHE_REQ_NETGROUP_BY_NAME) {
            /* We need to expand the netgroup into triples and members. */
            ret = sysdb_netgr_to_entries(state->enum_ctx,
                                         result[0]->ldb_result,
                                         &state->enum_ctx->netgroup);
            if (ret != EOK) {
                goto done;
            }
        }
        break;
    case ENOENT:
        /* Reset the result but build it again next time setent is called. */
        talloc_zfree(state->enum_ctx->result);
        talloc_zfree(state->enum_ctx->netgroup);
        goto done;
    default:
        /* In case of an error, we do not touch the enumeration context. */
        goto done;
    }

    /* Expire the result object after its timeout is reached. */
    tret = state->timeout_handler(state->ev, state->nss_ctx, state->enum_ctx);
    if (tret != EOK) {
        ret = ENOMEM;
        goto done;
    }

    /* The object is ready now. */
    state->enum_ctx->is_ready = true;

    ret = EOK;

done:
    /* We want to finish the requests in correct order, this was the
     * first request, notify_list contain the subsequent request.
     *
     * Because callback invoked from tevent_req_done will free state,
     * we must remember notify_list explicitly to avoid segfault.
     */
    notify_list = &state->enum_ctx->notify_list;

    if (ret == EOK) {
        tevent_req_done(req);
        setent_notify_done(notify_list);
    } else {
        tevent_req_error(req, ret);
        setent_notify(notify_list, ret);
    }
}

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

    return EOK;
}

static void
nss_setent_timeout(struct tevent_context *ev,
                   struct tevent_timer *te,
                   struct timeval current_time,
                   void *pvt)
{
    struct nss_enum_ctx *enum_ctx = pvt;

    DEBUG(SSSDBG_TRACE_FUNC, "Enumeration result object has expired.\n");

    /* Reset enumeration context. */
    talloc_zfree(enum_ctx->result);
    enum_ctx->is_ready = false;
}

static errno_t
nss_setent_set_timeout(struct tevent_context *ev,
                       struct nss_ctx *nss_ctx,
                       struct nss_enum_ctx *enum_ctx)
{
    struct tevent_timer *te;
    struct timeval tv;

    tv = tevent_timeval_current_ofs(nss_ctx->enum_cache_timeout, 0);
    te = tevent_add_timer(ev, nss_ctx, tv, nss_setent_timeout, enum_ctx);
    if (te == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Could not set up life timer for enumeration object.\n");
        return ENOMEM;
    }

    return EOK;
}

struct tevent_req *
nss_setent_send(TALLOC_CTX *mem_ctx,
                struct tevent_context *ev,
                struct cli_ctx *cli_ctx,
                enum cache_req_type type,
                struct nss_enum_ctx *enum_ctx)
{
    struct cache_req_data *data;

    data = cache_req_data_enum(mem_ctx, type);
    if (data == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set cache request data!\n");
        return NULL;
    }

    return nss_setent_internal_send(mem_ctx, ev, cli_ctx, data, type, enum_ctx,
                                    nss_setent_set_timeout);
}

errno_t nss_setent_recv(struct tevent_req *req)
{
    return nss_setent_internal_recv(req);
}

static void
nss_setnetgrent_timeout(struct tevent_context *ev,
                        struct tevent_timer *te,
                        struct timeval current_time,
                        void *pvt)
{
    struct nss_enum_ctx *enum_ctx;

    DEBUG(SSSDBG_TRACE_FUNC, "Enumeration result object has expired.\n");

    /* Free enumeration context. This will also remove it from the table. */
    enum_ctx = talloc_get_type(pvt, struct nss_enum_ctx);
    talloc_free(enum_ctx);
}

static errno_t
nss_setnetgrent_set_timeout(struct tevent_context *ev,
                            struct nss_ctx *nss_ctx,
                            struct nss_enum_ctx *enum_ctx)
{
    struct tevent_timer *te;
    struct timeval tv;
    uint32_t timeout;

    timeout = enum_ctx->result[0]->domain->netgroup_timeout;

    tv = tevent_timeval_current_ofs(timeout, 0);
    te = tevent_add_timer(ev, nss_ctx, tv, nss_setnetgrent_timeout, enum_ctx);
    if (te == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Could not set up life timer for enumeration object.\n");
        return ENOMEM;
    }

    return EOK;
}

static struct nss_enum_ctx *
nss_setnetgrent_set_enum_ctx(hash_table_t *table,
                             const char *netgroup)
{
    struct nss_enum_ctx *enum_ctx;
    errno_t ret;

    enum_ctx = sss_ptr_hash_lookup(table, netgroup, struct nss_enum_ctx);
    if (enum_ctx != NULL) {
        return enum_ctx;
    }

    enum_ctx = talloc_zero(table, struct nss_enum_ctx);
    if (enum_ctx == NULL) {
        return NULL;
    }

    ret = sss_ptr_hash_add(table, netgroup, enum_ctx, struct nss_enum_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Unable to add enumeration context into table [%d]: %s\n",
              ret, sss_strerror(ret));
        talloc_free(enum_ctx);
        return NULL;
    }

    return enum_ctx;
}

struct tevent_req *
nss_setnetgrent_send(TALLOC_CTX *mem_ctx,
                     struct tevent_context *ev,
                     struct cli_ctx *cli_ctx,
                     enum cache_req_type type,
                     hash_table_t *table,
                     const char *netgroup)
{
    struct nss_enum_ctx *enum_ctx;
    struct cache_req_data *data;

    enum_ctx = nss_setnetgrent_set_enum_ctx(table, netgroup);
    if (enum_ctx == NULL) {
        return NULL;
    }

    data = cache_req_data_name(mem_ctx, type, netgroup);
    if (data == NULL) {
        return NULL;
    }

    return nss_setent_internal_send(mem_ctx, ev, cli_ctx, data, type, enum_ctx,
                                    nss_setnetgrent_set_timeout);
}

errno_t nss_setnetgrent_recv(struct tevent_req *req)
{
    return nss_setent_internal_recv(req);
}