summaryrefslogtreecommitdiffstats
path: root/src/util/sss_ptr_hash.c
blob: 0f884c8b6b70c2348cd386c6fee5e0c90395475b (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
    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 <talloc.h>
#include <dhash.h>

#include "util/util.h"
#include "util/sss_ptr_hash.h"

static bool sss_ptr_hash_check_type(void *ptr, const char *type)
{
    void *type_ptr;

    type_ptr = talloc_check_name(ptr, type);
    if (type_ptr == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Invalid data type detected. Expected [%s], got [%s].\n",
              type, talloc_get_name(ptr));
        return false;
    }

    return true;
}

struct sss_ptr_hash_delete_data {
    hash_delete_callback *callback;
    void *pvt;
};

struct sss_ptr_hash_value {
    struct sss_ptr_hash_spy *spy;
    void *ptr;
};

struct sss_ptr_hash_spy {
    struct sss_ptr_hash_value *value;
    hash_table_t *table;
    const char *key;
};

static int
sss_ptr_hash_spy_destructor(struct sss_ptr_hash_spy *spy)
{
    spy->value->spy = NULL;
    spy->value->ptr = NULL;

    /* This results in removing entry from hash table and freeing the value. */
    sss_ptr_hash_delete(spy->table, spy->key, false);
    return 0;
}

static struct sss_ptr_hash_spy *
sss_ptr_hash_spy_create(TALLOC_CTX *mem_ctx,
                        hash_table_t *table,
                        const char *key,
                        struct sss_ptr_hash_value *value)
{
    struct sss_ptr_hash_spy *spy;

    spy = talloc_zero(mem_ctx, struct sss_ptr_hash_spy);
    if (spy == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
        return NULL;
    }

    spy->key = talloc_strdup(spy, key);
    if (spy->key == NULL) {
        talloc_free(spy);
        return NULL;
    }

    spy->table = table;
    spy->value = value;
    talloc_set_destructor(spy, sss_ptr_hash_spy_destructor);

    return spy;
}

static int
sss_ptr_hash_value_destructor(struct sss_ptr_hash_value *value)
{
    if (value->spy != NULL) {
        /* Disable spy destructor and free it. */
        talloc_set_destructor(value->spy, NULL);
        talloc_zfree(value->spy);
    }

    return 0;
}

static struct sss_ptr_hash_value *
sss_ptr_hash_value_create(hash_table_t *table,
                          const char *key,
                          void *talloc_ptr)
{
    struct sss_ptr_hash_value *value;

    value = talloc_zero(table, struct sss_ptr_hash_value);
    if (value == NULL) {
        return NULL;
    }

    value->spy = sss_ptr_hash_spy_create(talloc_ptr, table, key, value);
    if (value->spy == NULL) {
        talloc_free(value);
        return NULL;
    }

    value->ptr = talloc_ptr;
    talloc_set_destructor(value, sss_ptr_hash_value_destructor);

    return value;
}

static void
sss_ptr_hash_delete_cb(hash_entry_t *item,
                       hash_destroy_enum deltype,
                       void *pvt)
{
    struct sss_ptr_hash_delete_data *data;
    struct sss_ptr_hash_value *value;
    void *ptr;

    data = talloc_get_type(pvt, struct sss_ptr_hash_delete_data);
    if (data == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid data!\n");
        return;
    }

    value = talloc_get_type(item->value.ptr, struct sss_ptr_hash_value);
    if (value == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid value!\n");
        return;
    }

    ptr = value->ptr;

    /* Free value. */
    talloc_free(value);

    /* Switch to the input value and call custom callback. */
    if (data->callback != NULL) {
        item->value.ptr = ptr;
        data->callback(item, deltype, data->pvt);
    }
}

hash_table_t *sss_ptr_hash_create(TALLOC_CTX *mem_ctx,
                                  hash_delete_callback *del_cb,
                                  void *del_cb_pvt)
{
    struct sss_ptr_hash_delete_data *data;
    hash_table_t *table;
    errno_t ret;

    data = talloc_zero(NULL, struct sss_ptr_hash_delete_data);
    if (data == NULL) {
        return NULL;
    }

    data->callback = del_cb;
    data->pvt = del_cb_pvt;

    ret = sss_hash_create_ex(mem_ctx, 10, &table, 0, 0, 0, 0,
                             sss_ptr_hash_delete_cb, data);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create hash table [%d]: %s\n",
              ret, sss_strerror(ret));
        talloc_free(data);
        return NULL;
    }

    talloc_steal(table, data);

    return table;
}

errno_t _sss_ptr_hash_add(hash_table_t *table,
                          const char *key,
                          void *talloc_ptr,
                          const char *type,
                          bool override)
{
    struct sss_ptr_hash_value *value;
    hash_value_t table_value;
    hash_key_t table_key;
    int hret;

    if (table == NULL || key == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid input!\n");
        return EINVAL;
    }

    if (!sss_ptr_hash_check_type(talloc_ptr, type)) {
        return ERR_INVALID_DATA_TYPE;
    }

    value = sss_ptr_hash_value_create(table, key, talloc_ptr);
    if (value == NULL) {
        return ENOMEM;
    }

    table_key.type = HASH_KEY_STRING;
    table_key.str = discard_const_p(char, key);

    table_value.type = HASH_VALUE_PTR;
    table_value.ptr = value;

    if (override == false && hash_has_key(table, &table_key)) {
        return EEXIST;
    }

    hret = hash_enter(table, &table_key, &table_value);
    if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add key %s!\n", key);
        talloc_free(value);
        return EIO;
    }

    return EOK;
}

static struct sss_ptr_hash_value *
sss_ptr_hash_lookup_internal(hash_table_t *table,
                             const char *key)
{
    hash_value_t table_value;
    hash_key_t table_key;
    int hret;

    table_key.type = HASH_KEY_STRING;
    table_key.str = discard_const_p(char, key);

    hret = hash_lookup(table, &table_key, &table_value);
    if (hret == HASH_ERROR_KEY_NOT_FOUND) {
        return NULL;
    } else if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to search hash table [%d]\n", hret);
        return NULL;
    }

    /* Check value type. */
    if (table_value.type != HASH_VALUE_PTR) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid value type found: %d\n",
              table_value.type);
        return NULL;
    }

    if (!sss_ptr_hash_check_type(table_value.ptr, "struct sss_ptr_hash_value")) {
        return NULL;
    }

    return table_value.ptr;
}

void *_sss_ptr_hash_lookup(hash_table_t *table,
                           const char *key,
                           const char *type)
{
    struct sss_ptr_hash_value *value;

    value = sss_ptr_hash_lookup_internal(table, key);
    if (value == NULL || value->ptr == NULL) {
        return NULL;
    }

    if (!sss_ptr_hash_check_type(value->ptr, type)) {
        return NULL;
    }

    return value->ptr;
}

void sss_ptr_hash_delete(hash_table_t *table,
                         const char *key,
                         bool free_value)
{
    struct sss_ptr_hash_value *value;
    hash_key_t table_key;
    int hret;
    void *ptr;

    if (table == NULL || key == NULL) {
        return;
    }

    value = sss_ptr_hash_lookup_internal(table, key);
    if (value == NULL) {
        /* Value not found. */
        return;
    }

    ptr = value->ptr;

    table_key.type = HASH_KEY_STRING;
    table_key.str = discard_const_p(char, key);

    /* Delete table entry. This will free value and spy in delete callback. */
    hret = hash_delete(table, &table_key);
    if (hret != HASH_SUCCESS && hret != HASH_ERROR_KEY_NOT_FOUND) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to remove key from table [%d]\n",
              hret);
    }

    /* Also free the original value if requested. */
    if (free_value) {
        talloc_free(ptr);
    }

    return;
}

void sss_ptr_hash_delete_all(hash_table_t *table,
                             bool free_values)
{
    struct sss_ptr_hash_value *value;
    hash_value_t *values;
    unsigned long count;
    unsigned long i;
    int hret;
    void *ptr;

    if (table == NULL) {
        return;
    }

    hret = hash_values(table, &count, &values);
    if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get values [%d]\n", hret);
        return;
    }

    for (i = 0; i < count; i++) {
        value = values[i].ptr;
        ptr = value->ptr;

        /* This will remove the entry from hash table and free value. */
        talloc_free(value->spy);

        if (free_values) {
            /* Also free the original value. */
            talloc_free(ptr);
        }
    }

    return;
}

bool sss_ptr_hash_has_key(hash_table_t *table,
                          const char *key)
{
    hash_key_t table_key;

    table_key.type = HASH_KEY_STRING;
    table_key.str = discard_const_p(char, key);

    return hash_has_key(table, &table_key);
}