summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c
blob: db4a3d0611a713feb30843bb01b7645afcb2b695 (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
/** BEGIN COPYRIGHT BLOCK
 * 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/>.
 *
 * Additional permission under GPLv3 section 7:
 *
 * In the following paragraph, "GPL" means the GNU General Public
 * License, version 3 or any later version, and "Non-GPL Code" means
 * code that is governed neither by the GPL nor a license
 * compatible with the GPL.
 *
 * You may link the code of this Program with Non-GPL Code and convey
 * linked combinations including the two, provided that such Non-GPL
 * Code only links to the code of this Program through those well
 * defined interfaces identified in the file named EXCEPTION found in
 * the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline
 * functions from the Approved Interfaces without causing the resulting
 * work to be covered by the GPL. Only the copyright holders of this
 * Program may make changes or additions to the list of Approved
 * Interfaces.
 *
 * Authors:
 * Simo Sorce <ssorce@redhat.com>
 *
 * Copyright (C) 2011 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include "ipa_cldap.h"
#include <poll.h>
#include <lber.h>

/* pre allocate some space for answers, default to increment 3 at a time */
static int ipa_cldap_more_kvps(struct kvp_list *kvps)
{
    struct kvp *pairs;

    if (kvps->allocated - kvps->top > 0) {
        return 0;
    }

    pairs = realloc(kvps->pairs, (kvps->allocated + 3) * sizeof(struct kvp));
    if (!pairs) {
        return ENOMEM;
    }
    kvps->pairs = pairs;
    kvps->allocated += 3;

    return 0;
}

static void ipa_cldap_free_kvps(struct kvp_list *kvps)
{
    free(kvps->pairs);
    kvps->pairs = NULL;
    kvps->allocated = 0;
    kvps->top = 0;
}

static int ipa_cldap_get_kvp(BerElement *be, struct kvp_list *kvps)
{
    ber_tag_t tag;
    int ret;

    ret = ipa_cldap_more_kvps(kvps);
    if (ret) {
        return ret;
    }

    tag = ber_scanf(be, "{mm}",
                    &(kvps->pairs[kvps->top].attr),
                    &(kvps->pairs[kvps->top].value));
    if (tag == LBER_ERROR) {
        LOG_TRACE("Invalid filter\n");
        ret = EINVAL;
    } else {
        kvps->top++;
    }

    return ret;
}

static int ipa_cldap_get_tree(BerElement *be, struct kvp_list *kvps)
{
    ber_tag_t tag;
    ber_tag_t len;
    char *cookie;
    int ret;

    tag = ber_peek_tag(be, &len);
    if (tag == LDAP_FILTER_EQUALITY) {
        /* Special case of a single clause filter, eg. (NtVer=\06\00\00\00) */
        ret = ipa_cldap_get_kvp(be, kvps);
        if (ret == 0) {
            return 0;
        }
    }

    tag = ber_first_element(be, &len, &cookie);
    while (tag != LBER_DEFAULT) {
        tag = ber_peek_tag(be, &len);
        switch (tag) {
        case LDAP_FILTER_EQUALITY:
            ret = ipa_cldap_get_kvp(be, kvps);
            break;
        case LDAP_FILTER_AND:
            ret = ipa_cldap_get_tree(be, kvps);
            break;
        default:
            LOG_TRACE("Unsupported filter\n");
            ret = EINVAL;
            break;
        }

        if (ret) {
            return ret;
        }

        tag = ber_next_element(be, &len, cookie);
    }

    return 0;
}

static int ipa_cldap_decode(struct ipa_cldap_req *req)
{
    struct berval bv;
    BerElement *be;
    ber_tag_t tag;
    ber_len_t len;
    ber_int_t scope;
    ber_int_t deref;
    ber_int_t sizelimit;
    ber_int_t timelimit;
    ber_int_t typesonly;
    struct berval base;
    struct berval attr;
    int ret = EINVAL;

    bv.bv_val = req->dgram;
    bv.bv_len = req->dgsize;

    be = ber_alloc_t(0);
    if (!be) {
        LOG_FATAL("Out of memory!\n");
        goto done;
    }

    ber_init2(be, &bv, 0);

    tag = ber_skip_tag(be, &len);
    if (tag != LDAP_TAG_MESSAGE) {
        LOG_TRACE("Invalid message (%d)\n", (int)tag);
        goto done;
    }

    tag = ber_get_int(be, &req->id);
    if (tag != LDAP_TAG_MSGID) {
        LOG_TRACE("Failed to get id\n");
        goto done;
    }

    tag = ber_peek_tag(be, &len);
    if (tag != LDAP_REQ_SEARCH) {
        LOG_TRACE("Unexpected message type (%d)\n", (int)tag);
        goto done;
    }

    tag = ber_scanf(be, "{meeiib",
                    &base, &scope, &deref, &sizelimit, &timelimit, &typesonly);
    if (tag == LBER_ERROR) {
        LOG_TRACE("Failed to parse message\n");
        goto done;
    }

    if ((base.bv_len != 0) ||
        (scope != 0) ||
        (typesonly != 0)){
        LOG_TRACE("Unexpected request\n");
        goto done;
    }

    ret = ipa_cldap_get_tree(be, &req->kvps);
    if (ret) {
        LOG_TRACE("Failed to parse filter\n");
        goto done;
    }

    tag = ber_scanf(be, "{m}}", &attr);
    if (tag == LBER_ERROR) {
        LOG_TRACE("Failed to parse message\n");
        goto done;
    }

    if (strncasecmp(attr.bv_val, "netlogon", attr.bv_len) != 0) {
        LOG_TRACE("Unexpected request\n");
        goto done;
    }

done:
    ber_free(be, 0);
    return ret;
}

static void ipa_cldap_respond(struct ipa_cldap_ctx *ctx,
                              struct ipa_cldap_req *req,
                              struct berval *nbtblob)
{
    struct berval *bv = NULL;
    BerElement *be;
    int ret;

    be = ber_alloc_t(0);
    if (!be) {
        LOG_OOM();
        return;
    }

    if (nbtblob->bv_len != 0) {
        /* result */
        ret = ber_printf(be, "{it{s{{s[O]}}}}", req->id,
                         LDAP_RES_SEARCH_ENTRY, "", "netlogon", nbtblob);
        if (ret == LBER_ERROR) {
            LOG("Failed to encode CLDAP reply\n");
            goto done;
        }
    }
    /* done */
    /* As per MS-ADTS 6.3.3.3 always return SUCCESS even for invalid filters */
    ret = ber_printf(be, "{it{ess}}", req->id,
                         LDAP_RES_SEARCH_RESULT, 0, "", "");
    if (ret == LBER_ERROR) {
        LOG("Failed to encode CLDAP reply\n");
        goto done;
    }
    /* get data blob */
    ret = ber_flatten(be, &bv);
    if (ret == LBER_ERROR) {
        LOG("Failed to encode CLDAP reply\n");
        goto done;
    }

    ret = sendto(ctx->sd, bv->bv_val, bv->bv_len, 0,
                 (struct sockaddr *)&req->ss, req->ss_len);
    if (ret == -1) {
        LOG("Failed to send CLDAP reply (%d, %s)\n", errno, strerror(errno));
    }

done:
    ber_bvfree(bv);
    ber_free(be, 1);
}

static void ipa_cldap_process(struct ipa_cldap_ctx *ctx,
                              struct ipa_cldap_req *req)
{
    struct berval reply;
    int ret;

    ret = ipa_cldap_decode(req);
    if (ret) {
        goto done;
    }

    LOG_TRACE("CLDAP Request received");

    ret = ipa_cldap_netlogon(ctx, req, &reply);

done:
    if (ret != 0) {
        /* bad request, or internal error, return empty reply */
        /* as Windows does per MS-ADTS 6.3.3.3 */
        memset(&reply, 0, sizeof(struct berval));
    }

    ipa_cldap_respond(ctx, req, &reply);

    ipa_cldap_free_kvps(&req->kvps);
    free(req);
    return;
}

static struct ipa_cldap_req *ipa_cldap_recv_dgram(struct ipa_cldap_ctx *ctx)
{
    struct ipa_cldap_req *req;

    req = calloc(1, sizeof(struct ipa_cldap_req));
    if (!req) {
        LOG("Failed to allocate memory for req");
        return NULL;
    }

    req->fd = ctx->sd;
    req->ss_len = sizeof(struct sockaddr_storage);

    req->dgsize = recvfrom(req->fd, req->dgram, MAX_DG_SIZE, 0,
                           (struct sockaddr *)&req->ss, &req->ss_len);
    if (req->dgsize == -1) {
        LOG_TRACE("Failed to get datagram\n");
        free(req);
        return NULL;
    }

    return req;
}

void *ipa_cldap_worker(void *arg)
{
    struct ipa_cldap_req *req;
    struct pollfd fds[2];
    bool stop = false;
    struct ipa_cldap_ctx *ctx = (struct ipa_cldap_ctx *) arg;
    int ret;

    while (!stop) {

        fds[0].fd = ctx->stopfd[0];
        fds[0].events = POLLIN;
        fds[0].revents = 0;
        fds[1].fd = ctx->sd;
        fds[1].events = POLLIN;
        fds[1].revents = 0;

        /* wait until a request comes in */
        ret = poll(fds, 2, -1);
        if (ret == -1) {
            if (errno != EINTR) {
                LOG_FATAL("poll() failed with [%d, %s]. Can't continue.\n",
                          errno, strerror(errno));
                stop = true;
            }
        }
        if (ret <= 0) {
            continue;
        }

        /* got a stop signal, exit the loop */
        if (fds[0].revents & POLLIN) {
            stop = true;
            continue;
        }

        /* got a CLDAP packet, handle it */
        if (fds[1].revents & POLLIN) {
            req = ipa_cldap_recv_dgram(ctx);
            if (req) {
                ipa_cldap_process(ctx, req);
            }
        }
    }
    return NULL;
}