summaryrefslogtreecommitdiff
path: root/src/providers/ipa/ipa_selinux_maps.c
blob: b0a2c8f727be2af0a4eb878f4b5b56a7b0ef11bd (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
/*
    SSSD

    IPA Backend Module -- SELinux user maps (maps retrieval)

    Authors:
        Jan Zeleny <jzeleny@redhat.com>

    Copyright (C) 2012 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 "providers/ipa/ipa_common.h"
#include "providers/ipa/ipa_selinux_maps.h"

struct ipa_selinux_get_maps_state {
    struct tevent_context *ev;
    struct sysdb_ctx *sysdb;
    struct sdap_handle *sh;
    struct sdap_options *opts;
    struct ipa_options *ipa_opts;
    const char **attrs;

    struct sdap_search_base **search_bases;
    int search_base_iter;

    char *cur_filter;
    char *maps_filter;

    size_t map_count;
    struct sysdb_attrs **maps;
};

static errno_t
ipa_selinux_get_maps_next(struct tevent_req *req,
                          struct ipa_selinux_get_maps_state *state);
static void
ipa_selinux_get_maps_done(struct tevent_req *subreq);

struct tevent_req *ipa_selinux_get_maps_send(TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct sysdb_ctx *sysdb,
                                             struct sdap_handle *sh,
                                             struct sdap_options *opts,
                                             struct ipa_options *ipa_opts,
                                             struct sdap_search_base **search_bases)
{
    struct tevent_req *req;
    struct ipa_selinux_get_maps_state *state;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state, struct ipa_selinux_get_maps_state);
    if (req == NULL) {
        return NULL;
    }

    state->ev = ev;
    state->sysdb = sysdb;
    state->sh = sh;
    state->opts = opts;
    state->ipa_opts = ipa_opts;
    state->search_bases = search_bases;
    state->search_base_iter = 0;
    state->map_count = 0;
    state->maps = NULL;

    ret = build_attrs_from_map(state, ipa_opts->selinuxuser_map,
                               IPA_OPTS_SELINUX_USERMAP, NULL,
                               &state->attrs, NULL);
    if (ret != EOK) goto fail;

    state->cur_filter = NULL;
    state->maps_filter = talloc_asprintf(state,
                        "(&(objectclass=%s)(%s=TRUE))",
                        ipa_opts->selinuxuser_map[IPA_OC_SELINUX_USERMAP].name,
                        ipa_opts->selinuxuser_map[IPA_AT_SELINUX_USERMAP_ENABLED].name);
    if (state->maps_filter == NULL) {
        ret = ENOMEM;
        goto fail;
    }

    ret = ipa_selinux_get_maps_next(req, state);
    if (ret == EOK) {
        ret = EINVAL;
    }

    if (ret != EAGAIN) {
        goto fail;
    }

    return req;

fail:
    tevent_req_error(req, ret);
    tevent_req_post(req, ev);
    return req;
}

static errno_t
ipa_selinux_get_maps_next(struct tevent_req *req,
                          struct ipa_selinux_get_maps_state *state)
{
    struct sdap_search_base *base;
    struct tevent_req *subreq;

    base = state->search_bases[state->search_base_iter];
    if (base == NULL) {
        return EOK;
    }

    talloc_zfree(state->cur_filter);
    state->cur_filter = sdap_get_id_specific_filter(state, state->maps_filter,
                                                    base->filter);
    if (state->cur_filter == NULL) {
        return ENOMEM;
    }

    DEBUG(SSSDBG_TRACE_FUNC, ("Trying to fetch SELinux maps with following "
                              "parameters: [%d][%s][%s]\n", base->scope,
                              state->cur_filter, base->basedn));
    subreq = sdap_get_generic_send(state, state->ev, state->opts,
                                   state->sh, base->basedn,
                                   base->scope, state->cur_filter,
                                   state->attrs,
                                   state->ipa_opts->selinuxuser_map,
                                   IPA_OPTS_SELINUX_USERMAP,
                                   dp_opt_get_int(state->opts->basic,
                                                  SDAP_ENUM_SEARCH_TIMEOUT),
                                   true);
    if (subreq == NULL) {
        return ENOMEM;
    }

    tevent_req_set_callback(subreq, ipa_selinux_get_maps_done, req);
    return EAGAIN;
}

static void ipa_selinux_get_maps_done(struct tevent_req *subreq)
{
    errno_t ret;
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                  struct tevent_req);
    struct ipa_selinux_get_maps_state *state = tevent_req_data(req,
                                                  struct ipa_selinux_get_maps_state);
    struct sysdb_attrs **results;
    size_t total_count;
    size_t count;
    int i;

    ret = sdap_get_generic_recv(subreq, state, &count, &results);
    if (ret != EOK) {
        goto done;
    }

    if (count > 0) {
        DEBUG(SSSDBG_TRACE_FUNC, ("Found %d user maps in current search base\n",
                                  count));

        total_count = count + state->map_count;
        state->maps = talloc_realloc(state, state->maps, struct sysdb_attrs *, total_count);
        if (state->maps == NULL) {
            ret = ENOMEM;
            goto done;
        }

        i = 0;
        while (state->map_count < total_count) {
            state->maps[state->map_count] = talloc_steal(state->maps, results[i]);
            state->map_count++;
            i++;
        }
    }

    state->search_base_iter++;
    ret = ipa_selinux_get_maps_next(req, state);
    if (ret == EAGAIN) {
        return;
    } else if (ret != EOK) {
        goto done;
    }

    if (state->map_count == 0) {
        DEBUG(SSSDBG_TRACE_FUNC, ("No SELinux user maps found!\n"));
        ret = ENOENT;
    }

done:
    if (ret != EOK) {
        tevent_req_error(req, ret);
    } else {
        tevent_req_done(req);
    }
}

errno_t
ipa_selinux_get_maps_recv(struct tevent_req *req,
                          TALLOC_CTX *mem_ctx,
                          size_t *count,
                          struct sysdb_attrs ***maps)
{
    struct ipa_selinux_get_maps_state *state =
            tevent_req_data(req, struct ipa_selinux_get_maps_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *count = state->map_count;
    *maps = talloc_steal(mem_ctx, state->maps);

    return EOK;
}