summaryrefslogtreecommitdiffstats
path: root/src/providers/ldap/sdap_async_sudo_timer.c
blob: ed32cd901bc145b10f3613366b24f29e9100bc11 (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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2011 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 <tevent.h>
#include <talloc.h>

#include "util/util.h"
#include "providers/ldap/sdap.h"
#include "providers/ldap/sdap_id_op.h"
#include "providers/ldap/sdap_sudo.h"

struct sdap_sudo_timer_state {
    struct tevent_context *ev;
    struct sdap_sudo_ctx *sudo_ctx;
    time_t timeout;          /* relative time how many seconds wait before
                                canceling fn request */
    sdap_sudo_timer_fn_t fn; /* request executed on 'when' */

    struct tevent_req *subreq;
    struct tevent_timer *timer_timeout;
};

static void sdap_sudo_timer(struct tevent_context *ev,
                            struct tevent_timer *tt,
                            struct timeval tv, void *pvt);

static void sdap_sudo_timer_done(struct tevent_req *subreq);

static void sdap_sudo_timer_timeout(struct tevent_context *ev,
                                    struct tevent_timer *tt,
                                    struct timeval tv, void *pvt);

struct tevent_req * sdap_sudo_timer_send(TALLOC_CTX *mem_ctx,
                                         struct tevent_context *ev,
                                         struct sdap_sudo_ctx *sudo_ctx,
                                         struct timeval when,
                                         time_t timeout,
                                         sdap_sudo_timer_fn_t fn)
{
    struct tevent_req *req = NULL;
    struct tevent_timer *timer = NULL;
    struct sdap_sudo_timer_state *state = NULL;
    int ret;

    /* create request */
    req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_timer_state);
    if (req == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, ("tevent_req_create() failed\n"));
        return NULL;
    }

    state->ev = ev;
    state->sudo_ctx = sudo_ctx;
    state->timeout = timeout;
    state->fn = fn;

    /* set timer */
    timer = tevent_add_timer(ev, req, when, sdap_sudo_timer, req);
    if (timer == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, ("tevent_add_timer() failed\n"));
        ret = ENOMEM;
        goto immediately;
    }

    return req;

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

    return req;
}

int sdap_sudo_timer_recv(TALLOC_CTX *mem_ctx,
                         struct tevent_req *req,
                         struct tevent_req **_subreq)
{
    struct sdap_sudo_timer_state *state = NULL;

    state = tevent_req_data(req, struct sdap_sudo_timer_state);
    TEVENT_REQ_RETURN_ON_ERROR(req);

    *_subreq = talloc_steal(mem_ctx, state->subreq);

    return EOK;
}

static void sdap_sudo_timer(struct tevent_context *ev,
                            struct tevent_timer *tt,
                            struct timeval tv, void *pvt)
{
    struct tevent_req *req = NULL;
    struct sdap_sudo_timer_state *state = NULL;

    req = talloc_get_type(pvt, struct tevent_req);
    state = tevent_req_data(req, struct sdap_sudo_timer_state);

    /* issue request */
    state->subreq = state->fn(state, state->sudo_ctx);
    if (state->subreq == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to issue timed request!\n"));
        tevent_req_error(req, ENOMEM);
        return;
    }

    tevent_req_set_callback(state->subreq, sdap_sudo_timer_done, req);

    /* schedule timeout */
    tv = tevent_timeval_current_ofs(state->timeout, 0);
    state->timer_timeout = tevent_add_timer(state->ev, state->subreq, tv,
                                            sdap_sudo_timer_timeout, req);
    if (state->timer_timeout == NULL) {
        /* If we can't guarantee a timeout, we
         * need to cancel the request, to avoid
         * the possibility of starting another
         * concurrently
         */
        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to set timeout, "
                                    "canceling request!\n"));
        talloc_zfree(state->subreq);
        tevent_req_error(req, ENOMEM);
    }
}

static void sdap_sudo_timer_done(struct tevent_req *subreq)
{
    struct tevent_req *req = NULL;
    struct sdap_sudo_timer_state *state = NULL;

    req = tevent_req_callback_data(subreq, struct tevent_req);
    /* do not free subreq, it is returned in recv */

    /* cancel timeout */
    state = tevent_req_data(req, struct sdap_sudo_timer_state);
    talloc_zfree(state->timer_timeout);

    tevent_req_done(req);
}

static void sdap_sudo_timer_timeout(struct tevent_context *ev,
                                    struct tevent_timer *tt,
                                    struct timeval tv, void *pvt)
{
    struct tevent_req *req = NULL;
    struct sdap_sudo_timer_state *state = NULL;

    req = talloc_get_type(pvt, struct tevent_req);
    state = tevent_req_data(req, struct sdap_sudo_timer_state);

    DEBUG(SSSDBG_CRIT_FAILURE, ("Request timed out. Is timeout too small?"
                                " (%ds)!\n", state->timeout));

    talloc_zfree(state->subreq);

    tevent_req_error(req, EAGAIN);
}