summaryrefslogtreecommitdiffstats
path: root/src/responder/ifp/ifp_cache.c
blob: a4dd393aac251be433b42cdaa2b8b5c9fd7149ac (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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2015 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 <tevent.h>

#include "db/sysdb.h"
#include "util/util.h"
#include "responder/common/responder.h"
#include "responder/ifp/ifp_cache.h"
#include "responder/ifp/ifp_users.h"
#include "responder/ifp/ifp_groups.h"

static struct ldb_dn *
ifp_cache_build_base_dn(TALLOC_CTX *mem_ctx,
                        enum ifp_cache_type type,
                        struct sss_domain_info *domain)
{
    struct ldb_dn *base_dn = NULL;

    switch (type) {
    case IFP_CACHE_USER:
        base_dn = sysdb_user_base_dn(mem_ctx, domain);
        break;
    case IFP_CACHE_GROUP:
        base_dn = sysdb_group_base_dn(mem_ctx, domain);
        break;
    }

    return base_dn;
}

static char *
ifp_cache_build_path(TALLOC_CTX *mem_ctx,
                     enum ifp_cache_type type,
                     struct sss_domain_info *domain,
                     struct ldb_message *msg)
{
    char *path = NULL;

    switch (type) {
    case IFP_CACHE_USER:
        path = ifp_users_build_path_from_msg(mem_ctx, domain, msg);
        break;
    case IFP_CACHE_GROUP:
        path = ifp_groups_build_path_from_msg(mem_ctx, domain, msg);
        break;
    }

    return path;
}

static const char *
ifp_cache_object_class(enum ifp_cache_type type)
{
    const char *class = NULL;

    switch (type) {
    case IFP_CACHE_USER:
        class = SYSDB_USER_CLASS;
        break;
    case IFP_CACHE_GROUP:
        class = SYSDB_GROUP_CLASS;
        break;
    }

    return class;
}

static errno_t
ifp_cache_get_cached_objects(TALLOC_CTX *mem_ctx,
                             enum ifp_cache_type type,
                             struct sss_domain_info *domain,
                             const char ***_paths)
{
    TALLOC_CTX *tmp_ctx;
    struct ldb_dn *base_dn;
    struct ldb_result *result;
    const char *class = ifp_cache_object_class(type);
    const char **paths;
    errno_t ret;
    int ldb_ret;
    int i;
    const char *attrs[] = {SYSDB_OBJECTCATEGORY, SYSDB_UIDNUM,
                           SYSDB_GIDNUM, NULL};

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    base_dn = ifp_cache_build_base_dn(tmp_ctx, type, domain);
    if (base_dn == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create base dn\n");
        ret = ENOMEM;
        goto done;
    }

    ldb_ret = ldb_search(sysdb_ctx_get_ldb(domain->sysdb), tmp_ctx, &result,
                         base_dn, LDB_SCOPE_SUBTREE, attrs,
                         "(&(%s=%s)(%s=TRUE))", SYSDB_OBJECTCATEGORY, class,
                         SYSDB_IFP_CACHED);
    if (ldb_ret != LDB_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to search the cache\n");
        ret = sss_ldb_error_to_errno(ldb_ret);
        goto done;
    }

    paths = talloc_zero_array(tmp_ctx, const char *, result->count + 1);
    if (paths == NULL) {
        ret = ENOMEM;
        goto done;
    }

    for (i = 0; i < result->count; i++) {
        paths[i] = ifp_cache_build_path(paths, type, domain, result->msgs[i]);
        if (paths[i] == NULL) {
            ret = ENOMEM;
            goto done;
        }
    }

    *_paths = talloc_steal(mem_ctx, paths);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t
ifp_cache_list_domains(TALLOC_CTX *mem_ctx,
                       struct sss_domain_info *domains,
                       enum ifp_cache_type type,
                       const char ***_paths)
{
    TALLOC_CTX *tmp_ctx;
    struct sss_domain_info *domain;
    const char **tmp_paths = NULL;
    const char **paths;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    domain = domains;
    paths = NULL;
    while (domain != NULL) {
        ret = ifp_cache_get_cached_objects(tmp_ctx, type, domain, &tmp_paths);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build object list "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            goto done;
        }

        ret = add_strings_lists(tmp_ctx, paths, tmp_paths, true, &paths);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to build object list "
                  "[%d]: %s\n", ret, sss_strerror(ret));
            goto done;
        }

        domain = get_next_domain(domain, SSS_GND_DESCEND);
    }

    if (_paths != NULL) {
        *_paths = talloc_steal(mem_ctx, paths);
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

errno_t
ifp_cache_list(TALLOC_CTX *mem_ctx,
               struct ifp_ctx *ifp_ctx,
               enum ifp_cache_type type,
               const char ***_paths)
{
    return ifp_cache_list_domains(mem_ctx, ifp_ctx->rctx->domains,
                                  type, _paths);
}

errno_t
ifp_cache_list_by_domain(TALLOC_CTX *mem_ctx,
                         struct ifp_ctx *ifp_ctx,
                         const char *domainname,
                         enum ifp_cache_type type,
                         const char ***_paths)
{
    struct sss_domain_info *domain;

    domain = find_domain_by_name(ifp_ctx->rctx->domains, domainname, true);
    if (domain == NULL) {
        return ERR_DOMAIN_NOT_FOUND;
    }

    return ifp_cache_get_cached_objects(mem_ctx, type, domain, _paths);
}

static errno_t ifp_cache_object_set(struct sss_domain_info *domain,
                                    struct ldb_dn *dn,
                                    bool value)
{
    struct sysdb_attrs *attrs;
    errno_t ret;

    attrs = sysdb_new_attrs(NULL);
    if (attrs == NULL) {
        return ENOMEM;
    }

    ret = sysdb_attrs_add_bool(attrs, SYSDB_IFP_CACHED, value);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add attribute [%d]: %s\n",
              ret, sss_strerror(ret));
        goto done;
    }

    ret = sysdb_set_entry_attr(domain->sysdb, dn, attrs, SYSDB_MOD_REP);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to modify entry [%d]: %s\n",
              ret, sss_strerror(ret));
        goto done;
    }

    ret = EOK;

done:
    talloc_free(attrs);

    return ret;
}

errno_t
ifp_cache_object_store(struct sss_domain_info *domain,
                       struct ldb_dn *dn)
{
    return ifp_cache_object_set(domain, dn, true);
}

errno_t
ifp_cache_object_remove(struct sss_domain_info *domain,
                        struct ldb_dn *dn)
{
    return ifp_cache_object_set(domain, dn, false);
}