summaryrefslogtreecommitdiff
path: root/src/providers/ldap/sdap_async_groups_ad.c
blob: 9b61c697d5789c3ec3467ec52a7171f6a640ce9e (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
/*
    SSSD

    Authors:
        Stephen Gallagher <sgallagh@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 "util/util.h"
#include "providers/ldap/sdap_async.h"
#include "providers/ldap/ldap_common.h"

struct sdap_ad_match_rule_state {
    struct tevent_context *ev;
    struct sdap_handle *sh;
    const char **attrs;

    struct sdap_options *opts;
    const char *base_filter;
    char *filter;
    int timeout;

    size_t base_iter;
    struct sdap_search_base **search_bases;

    size_t count;
    struct sysdb_attrs **users;
};

static errno_t
sdap_get_ad_match_rule_members_next_base(struct tevent_req *req);
static void
sdap_get_ad_match_rule_members_step(struct tevent_req *subreq);

struct tevent_req *
sdap_get_ad_match_rule_members_send(TALLOC_CTX *mem_ctx,
                                    struct tevent_context *ev,
                                    struct sdap_options *opts,
                                    struct sdap_handle *sh,
                                    struct sysdb_attrs *group,
                                    int timeout)
{
    errno_t ret;
    struct tevent_req *req;
    struct sdap_ad_match_rule_state *state;
    const char *group_dn;
    char *sanitized_group_dn;

    req = tevent_req_create(mem_ctx, &state, struct sdap_ad_match_rule_state);
    if (!req) return NULL;

    state->ev = ev;
    state->opts = opts;
    state->sh = sh;
    state->timeout = timeout;
    state->count = 0;
    state->base_iter = 0;
    state->search_bases = opts->sdom->user_search_bases;

    /* Request all of the user attributes that we know about. */
    ret = build_attrs_from_map(state, opts->user_map, SDAP_OPTS_USER,
                               NULL, &state->attrs, NULL);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not build attribute map: [%s]\n",
               strerror(ret)));
        goto immediate;
    }

    /* Get the DN of the group */
    ret = sysdb_attrs_get_string(group, SYSDB_ORIG_DN, &group_dn);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not retrieve originalDN for group: %s\n",
               strerror(ret)));
        goto immediate;
    }

    /* Sanitize it in case we have special characters in DN */
    ret = sss_filter_sanitize(state, group_dn, &sanitized_group_dn);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Could not sanitize group DN: %s\n",
               strerror(ret)));
        goto immediate;
    }

    /* Craft a special filter according to
     * http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475%28v=vs.85%29.aspx
     */
    state->base_filter =
            talloc_asprintf(state,
                            "(&(%s:%s:=%s)(objectClass=%s))",
                            state->opts->user_map[SDAP_AT_USER_MEMBEROF].name,
                            SDAP_MATCHING_RULE_IN_CHAIN,
                            sanitized_group_dn,
                            state->opts->user_map[SDAP_OC_USER].name);
    talloc_zfree(sanitized_group_dn);
    if (!state->base_filter) {
        ret = ENOMEM;
        goto immediate;
    }

    /* Start the loop through the search bases to get all of the users */
    ret = sdap_get_ad_match_rule_members_next_base(req);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("sdap_get_ad_match_rule_members_next_base failed: [%s]\n",
               strerror(ret)));
        goto immediate;
    }

    return req;

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

static errno_t
sdap_get_ad_match_rule_members_next_base(struct tevent_req *req)
{
    struct tevent_req *subreq;
    struct sdap_ad_match_rule_state *state;

    state = tevent_req_data(req, struct sdap_ad_match_rule_state);

    talloc_zfree(state->filter);
    state->filter = sdap_get_id_specific_filter(state,
                        state->base_filter,
                        state->search_bases[state->base_iter]->filter);
    if (!state->filter) {
        return ENOMEM;
    }

    DEBUG(SSSDBG_TRACE_FUNC,
          ("Searching for users with base [%s]\n",
           state->search_bases[state->base_iter]->basedn));

    subreq = sdap_get_generic_send(
            state, state->ev, state->opts, state->sh,
            state->search_bases[state->base_iter]->basedn,
            state->search_bases[state->base_iter]->scope,
            state->filter, state->attrs,
            state->opts->user_map, SDAP_OPTS_USER,
            state->timeout, true);
    if (!subreq) {
        return ENOMEM;
    }

    tevent_req_set_callback(subreq, sdap_get_ad_match_rule_members_step, req);

    return EOK;
}

static void
sdap_get_ad_match_rule_members_step(struct tevent_req *subreq)
{
    errno_t ret;
    struct tevent_req *req =
            tevent_req_callback_data(subreq, struct tevent_req);
    struct sdap_ad_match_rule_state *state =
            tevent_req_data(req, struct sdap_ad_match_rule_state);
    size_t count, i;
    struct sysdb_attrs **users;

    ret = sdap_get_generic_recv(subreq, state, &count, &users);
    talloc_zfree(subreq);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("LDAP search failed: [%s]\n", strerror(ret)));
        tevent_req_error(req, ret);
        return;
    }

    DEBUG(SSSDBG_TRACE_LIBS,
          ("Search for users returned %zu results\n", count));

    /* Add this batch of users to the list */
    if (count > 0) {
        state->users = talloc_realloc(state, state->users,
                                      struct sysdb_attrs *,
                                      state->count + count + 1);
        if (!state->users) {
            tevent_req_error(req, ENOMEM);
            return;
        }

        /* Copy the new users into the list */
        for (i = 0; i < count; i++) {
            state->users[state->count + i] =
                    talloc_steal(state->users, users[i]);
        }

        state->count += count;
        state->users[state->count] = NULL;
    }

    /* Continue checking other search bases */
    state->base_iter++;
    if (state->search_bases[state->base_iter]) {
        /* There are more search bases to try */
        ret = sdap_get_ad_match_rule_members_next_base(req);
        if (ret != EOK) {
            tevent_req_error(req, ret);
        }
        return;
    }

    /* No more search bases. We're done here. */
    if (state->count == 0) {
        DEBUG(SSSDBG_TRACE_LIBS,
              ("No users matched in any search base\n"));
        tevent_req_error(req, ENOENT);
        return;
    }

    tevent_req_done(req);
}

errno_t
sdap_get_ad_match_rule_members_recv(struct tevent_req *req,
                                    TALLOC_CTX *mem_ctx,
                                    size_t *num_users,
                                    struct sysdb_attrs ***users)
{
    struct sdap_ad_match_rule_state *state =
            tevent_req_data(req, struct sdap_ad_match_rule_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *num_users = state->count;
    *users = talloc_steal(mem_ctx, state->users);
    return EOK;
}