summaryrefslogtreecommitdiff
path: root/src/providers/ldap/sdap_sudo_timer.c
blob: 81b6ae88994210bfc7d4eca3c10d3b3a1d141995 (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
/*
    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 "util/util.h"
#include "providers/ldap/ldap_common.h"
#include "providers/ldap/sdap_sudo_timer.h"
#include "providers/ldap/sdap_sudo.h"
#include "db/sysdb_sudo.h"

struct sdap_sudo_refresh_ctx {
    struct be_ctx *be_ctx;
    struct sdap_id_ctx *id_ctx;
    struct sdap_options *opts;

    struct timeval last_refresh;
};

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

static void sdap_sudo_refresh_reschedule(struct tevent_req *req);

static void sdap_sudo_refresh_timeout(struct tevent_context *ev,
                                      struct tevent_timer *te,
                                      struct timeval tv, void *pvt);

struct sdap_sudo_refresh_ctx *
sdap_sudo_refresh_ctx_init(TALLOC_CTX *mem_ctx,
                           struct be_ctx *be_ctx,
                           struct sdap_id_ctx *id_ctx,
                           struct sdap_options *opts,
                           struct timeval last_refresh)
{
    struct sdap_sudo_refresh_ctx *refresh_ctx = NULL;

    refresh_ctx = talloc_zero(mem_ctx, struct sdap_sudo_refresh_ctx);
    if (refresh_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_zero() failed!\n"));
        return NULL;
    }

    refresh_ctx->be_ctx = be_ctx;
    refresh_ctx->id_ctx = id_ctx;
    refresh_ctx->opts = opts;
    refresh_ctx->last_refresh = last_refresh;

    return refresh_ctx;
}

int sdap_sudo_refresh_set_timer(struct sdap_sudo_refresh_ctx *ctx,
                                struct timeval tv)
{
    struct tevent_timer *enum_task;

    DEBUG(SSSDBG_TRACE_FUNC, ("Scheduling next refresh of SUDO rules at "
          "%ld.%ld\n", (long)tv.tv_sec, (long)tv.tv_usec));

    enum_task = tevent_add_timer(ctx->be_ctx->ev, ctx,
                                 tv, sdap_sudo_refresh_timer, ctx);
    if (!enum_task) {
        DEBUG(SSSDBG_FATAL_FAILURE,
              ("FATAL: failed to setup SUDO rules refresh task!\n"));
        return EFAULT;
    }

    return EOK;
}

static void sdap_sudo_refresh_timer(struct tevent_context *ev,
                                    struct tevent_timer *tt,
                                    struct timeval tv, void *pvt)
{
    struct sdap_sudo_refresh_ctx *refresh_ctx = NULL;
    struct be_sudo_req *sudo_req = NULL;
    struct tevent_timer *timeout = NULL;
    struct tevent_req *req = NULL;
    int delay = 0;
    int ret;

    refresh_ctx = talloc_get_type(pvt, struct sdap_sudo_refresh_ctx);

    delay = dp_opt_get_int(refresh_ctx->opts->basic, SDAP_SUDO_REFRESH_TIMEOUT);

    if (be_is_offline(refresh_ctx->be_ctx)) {
        DEBUG(SSSDBG_TRACE_FUNC, ("Backend is marked offline, retry later!\n"));
        tv = tevent_timeval_current_ofs(delay, 0);
        ret = sdap_sudo_refresh_set_timer(refresh_ctx, tv);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Error setting up SUDO refresh timer\n"));
        }
        return;
    }

    /* create sudo context */
    sudo_req = talloc_zero(refresh_ctx, struct be_sudo_req);
    if (sudo_req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_zero() failed!\n"));
        tv = tevent_timeval_current_ofs(delay, 0);
        ret = sdap_sudo_refresh_set_timer(refresh_ctx, tv);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Error setting up SUDO refresh timer\n"));
        }

        return;
    }

    sudo_req->type = BE_REQ_SUDO_ALL;
    sudo_req->username = NULL;

    /* send request */
    req = sdap_sudo_refresh_send(refresh_ctx, refresh_ctx->id_ctx->be, sudo_req,
                                 refresh_ctx->id_ctx->opts,
                                 refresh_ctx->id_ctx->conn_cache);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to schedule refresh of SUDO rules, "
              "retrying later!\n"));
        tv = tevent_timeval_current_ofs(delay, 0);
        ret = sdap_sudo_refresh_set_timer(refresh_ctx, tv);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Error setting up SUDO refresh timer\n"));
        }

        talloc_free(sudo_req);
        return;
    }
    refresh_ctx->last_refresh = tevent_timeval_current();
    talloc_steal(req, sudo_req); /* make it free with req */

    tevent_req_set_callback(req, sdap_sudo_refresh_reschedule, refresh_ctx);

    /* schedule timeout */
    tv = tevent_timeval_current_ofs(delay, 0);
    timeout = tevent_add_timer(refresh_ctx->be_ctx->ev, req, tv,
                               sdap_sudo_refresh_timeout, req);
    if (timeout == NULL) {
        /* If we can't guarantee a timeout, we
         * need to cancel the request, to avoid
         * the possibility of starting another
         * concurrently
         */
        talloc_zfree(req);

        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to schedule refresh of SUDO rules, "
              "retrying later!\n"));
        tv = tevent_timeval_current_ofs(delay, 0);
        ret = sdap_sudo_refresh_set_timer(refresh_ctx, tv);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Error setting up SUDO refresh timer\n"));
        }
    }

    return;
}

static void sdap_sudo_refresh_reschedule(struct tevent_req *req)
{
    struct sdap_sudo_refresh_ctx *refresh_ctx = NULL;
    struct timeval tv;
    int delay;
    int dp_error;
    int error;
    int ret;

    refresh_ctx = tevent_req_callback_data(req, struct sdap_sudo_refresh_ctx);
    ret = sdap_sudo_refresh_recv(req, &dp_error, &error);
    talloc_zfree(req);
    if (ret != EOK) {
        tv = tevent_timeval_current();
    } else {
        tv = refresh_ctx->last_refresh;

        /* Ok, we've completed a refresh. Save this to the
         * sysdb so we can postpone starting up the refresh
         * process on the next SSSD service restart (to avoid
         * slowing down system boot-up
         */
        ret = sysdb_sudo_set_refreshed(refresh_ctx->be_ctx->sysdb, true);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("Could not mark domain as having refreshed.\n"));
            /* This error is non-fatal, so continue */
        }
    }

    delay = dp_opt_get_int(refresh_ctx->opts->basic, SDAP_SUDO_REFRESH_TIMEOUT);
    tv = tevent_timeval_add(&tv, delay, 0);
    ret = sdap_sudo_refresh_set_timer(refresh_ctx, tv);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Error setting up SUDO refresh timer\n"));
    }
}

static void sdap_sudo_refresh_timeout(struct tevent_context *ev,
                                      struct tevent_timer *te,
                                      struct timeval tv, void *pvt)
{
    struct tevent_req *req = talloc_get_type(pvt, struct tevent_req);
    struct sdap_sudo_refresh_ctx *refresh_ctx = NULL;
    int delay;

    refresh_ctx = tevent_req_callback_data(req, struct sdap_sudo_refresh_ctx);

    delay = dp_opt_get_int(refresh_ctx->opts->basic, SDAP_SUDO_REFRESH_TIMEOUT);
    DEBUG(SSSDBG_CRIT_FAILURE, ("Refreshing SUDO rules timed out!"
          " Timeout too small? (%ds)!\n", delay));

    tv = tevent_timeval_current_ofs(delay, 0);
    sdap_sudo_refresh_set_timer(refresh_ctx, tv);

    talloc_zfree(req);
}