summaryrefslogtreecommitdiff
path: root/src/responder/sudo/sudosrv_cache.c
blob: ee44a5ad6b7e94ecc0bd28c83af57bfd5f3f92f5 (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
/*
    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 <talloc.h>
#include <dhash.h>
#include <time.h>

#include "util/util.h"
#include "confdb/confdb.h"
#include "db/sysdb.h"
#include "responder/sudo/sudosrv_private.h"

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

struct sudo_cache_entry {
    hash_table_t *table;
    hash_key_t *key;
    size_t num_rules;
    struct sysdb_attrs **rules;

    struct sudo_ctx *sudo_ctx;
};

errno_t sudosrv_cache_init(TALLOC_CTX *mem_ctx,
                           unsigned long count,
                           hash_table_t **table)
{
    return sss_hash_create(mem_ctx, count, table);
}

static errno_t
sudosrv_cache_reinit(struct sudo_ctx *sudo_ctx)
{
    errno_t ret;

    talloc_free(sudo_ctx->cache);

    ret = sudosrv_cache_init(sudo_ctx, 10, &sudo_ctx->cache);
    if (ret != EOK) {
        DEBUG(SSSDBG_FATAL_FAILURE,
                ("Could not re-initialize hash table: [%s]", strerror(ret)));
    }
    return ret;
}

static hash_key_t *sudosrv_cache_create_key(TALLOC_CTX *mem_ctx,
                                            struct sss_domain_info *domain,
                                            const char *username)
{
    hash_key_t *key = talloc_zero(NULL, hash_key_t);
    if (key == NULL) {
        return NULL;
    }

    key->type = HASH_KEY_STRING;
    if (username == NULL) {
        key->str = talloc_strdup(key, domain->name);
    } else {
        key->str = talloc_asprintf(key, "%s:%s", domain->name, username);
    }

    if (key->str == NULL) {
        talloc_free(key);
        return NULL;
    }

    return talloc_steal(mem_ctx, key);
}

errno_t sudosrv_cache_set_entry(struct tevent_context *ev,
                                struct sudo_ctx *sudo_ctx,
                                hash_table_t *table,
                                struct sss_domain_info *domain,
                                const char *username,
                                size_t num_rules,
                                struct sysdb_attrs **rules,
                                time_t timeout)
{
    struct sudo_cache_entry *cache_entry = NULL;
    hash_key_t *key = NULL;
    hash_value_t value;
    TALLOC_CTX *tmp_ctx = NULL;
    struct tevent_timer *timer = NULL;
    struct timeval tv;
    errno_t ret;
    int hret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        goto done;
    }

    /* create key */
    key = sudosrv_cache_create_key(tmp_ctx, domain, username);
    if (key == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to create hash key.\n"));
        ret = ENOMEM;
        goto done;
    }

    /* create value */
    cache_entry = talloc_zero(tmp_ctx, struct sudo_cache_entry);
    if (cache_entry == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to create hash value.\n"));
        ret = ENOMEM;
        goto done;
    }
    cache_entry->table = table;
    cache_entry->key = key;
    cache_entry->num_rules = num_rules;
    cache_entry->rules = rules;
    cache_entry->sudo_ctx = sudo_ctx;

    value.type = HASH_VALUE_PTR;
    value.ptr = cache_entry;

    /* insert value */
    hret = hash_enter(table, key, &value);
    if (hret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Unable to add [%s] to SUDO cache", key->str));
        DEBUG(SSSDBG_TRACE_LIBS,
              ("Hash error [%d][%s]", hret, hash_error_string(hret)));
        ret = EIO;
        goto done;
    }

    /* Create a timer event to remove the entry from the cache */
    tv = tevent_timeval_current_ofs(timeout, 0);
    timer = tevent_add_timer(ev, cache_entry, tv,
                             sudosrv_cache_remove,
                             cache_entry);
    if (timer == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* everythig is ok, steal the pointers */
    talloc_steal(cache_entry, key);
    talloc_steal(cache_entry, rules);
    talloc_steal(table, cache_entry);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

static void free_cache_entry_cb(struct tevent_context *ev,
                                struct tevent_immediate *imm,
                                void *pvt)
{
    struct sudo_cache_entry *cache_entry =
        talloc_get_type(pvt, struct sudo_cache_entry);
    talloc_free(cache_entry);
}

static void sudosrv_cache_remove(struct tevent_context *ev,
                                 struct tevent_timer *te,
                                 struct timeval tv,
                                 void *pvt)
{
    int hret;
    hash_key_t *key;
    struct sudo_cache_entry *cache_entry;
    struct tevent_immediate *imm;

    cache_entry = talloc_get_type(pvt, struct sudo_cache_entry);
    key = cache_entry->key;

    hret = hash_delete(cache_entry->table, key);
    if (hret != HASH_SUCCESS && hret != HASH_ERROR_KEY_NOT_FOUND
        && hret != HASH_ERROR_BAD_KEY_TYPE) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not clear [%s] from SUDO cache.\n", key->str));
        DEBUG(SSSDBG_TRACE_LIBS,
               ("Hash error [%d][%s]", hret, hash_error_string(hret)));

        /* corrupted memory, re-initialize table */
        sudosrv_cache_reinit(cache_entry->sudo_ctx);
    } else {
        DEBUG(SSSDBG_TRACE_INTERNAL,
              ("[%s] removed from SUDO cache\n", key->str));

        imm = tevent_create_immediate(ev);
        if (imm == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Out of memory\n"));
            return;
        }
        tevent_schedule_immediate(imm, ev, free_cache_entry_cb, cache_entry);
    }
}

static errno_t sudosrv_cache_lookup_internal(hash_table_t *table,
                                             struct sss_domain_info *domain,
                                             const char *username,
                                             size_t *num_rules,
                                             struct sysdb_attrs ***rules)
{
    struct sudo_cache_entry *cache_entry = NULL;
    hash_key_t *key = NULL;
    hash_value_t value;
    TALLOC_CTX *tmp_ctx = NULL;
    errno_t ret;
    int hret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        goto done;
    }

    /* create key */
    key = sudosrv_cache_create_key(tmp_ctx, domain, username);
    if (key == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to create hash key.\n"));
        ret = ENOMEM;
        goto done;
    }

    hret = hash_lookup(table, key, &value);
    if (hret == HASH_SUCCESS) {
        /* cache hit */
        cache_entry = value.ptr;
        *num_rules = cache_entry->num_rules;
        *rules = cache_entry->rules;
        ret = EOK;
    } else if (hret == HASH_ERROR_KEY_NOT_FOUND) {
        /* cache miss */
        ret = ENOENT;
    } else {
        /* error */
        ret = EIO;
    }

done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t sudosrv_cache_lookup(hash_table_t *table,
                             struct sudo_dom_ctx *dctx,
                             bool check_next,
                             const char *username,
                             size_t *num_rules,
                             struct sysdb_attrs ***rules)
{
    struct sss_domain_info *domain = dctx->domain;
    char *name = NULL;
    errno_t ret;

    if (!check_next) {
        if (username != NULL) {
            name = sss_get_cased_name(NULL, username,
                                      dctx->domain->case_sensitive);
            if (name == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, ("Out of memory\n"));
                ret = ENOMEM;
                goto done;
            }
        }

        ret = sudosrv_cache_lookup_internal(table, dctx->domain, name,
                                            num_rules, rules);
        goto done;
    }

    while (domain != NULL) {
        if (domain->fqnames) {
            domain = domain->next;
            continue;
        }

        if (username != NULL) {
            talloc_free(name);
            name = sss_get_cased_name(NULL, username,
                                      dctx->domain->case_sensitive);
            if (name == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, ("Out of memory\n"));
                ret = ENOMEM;
                goto done;
            }
        }

        ret = sudosrv_cache_lookup_internal(table, domain, name,
                                            num_rules, rules);
        if (ret == EOK) {
            /* user is in this domain */
            dctx->domain = domain;
            goto done;
        } else if (ret != ENOENT) {
            /* error */
            goto done;
        }

        /* user is not in this domain cache, check next */
        domain = domain->next;
    }

    /* user is not in cache */
    ret = ENOENT;

done:
    talloc_free(name);
    return ret;
}