bind.c 30.1 KB
Newer Older
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2020 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

#include "portable.h"

#include <ac/socket.h>
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>

#include "lutil.h"
25
#include "lload.h"
Ondřej Kuzník's avatar
Ondřej Kuzník committed
26

Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
struct berval mech_external = BER_BVC("EXTERNAL");

int
bind_mech_external(
        LloadConnection *client,
        LloadOperation *op,
        struct berval *credentials )
{
    BerValue binddn;
    void *ssl;
    char *ptr;

    client->c_state = LLOAD_C_READY;
    client->c_type = LLOAD_C_OPEN;

    /*
     * We only support implicit assertion.
     *
     * Although RFC 4513 says the credentials field must be missing, RFC 4422
     * doesn't and libsasl2 will pass a zero-length string to send. We have to
     * allow that.
     */
    if ( !BER_BVISEMPTY( credentials ) ) {
        return operation_send_reject_locked( op, LDAP_UNWILLING_TO_PERFORM,
                "proxy authorization is not supported", 1 );
    }

    ssl = ldap_pvt_tls_sb_ctx( client->c_sb );
    if ( !ssl || ldap_pvt_tls_get_peer_dn( ssl, &binddn, NULL, 0 ) ) {
        return operation_send_reject_locked( op, LDAP_INVALID_CREDENTIALS,
                "no externally negotiated identity", 1 );
    }
    client->c_auth.bv_len = binddn.bv_len + STRLENOF("dn:");
    client->c_auth.bv_val = ch_malloc( client->c_auth.bv_len + 1 );

    ptr = lutil_strcopy( client->c_auth.bv_val, "dn:" );
    ptr = lutil_strncopy( ptr, binddn.bv_val, binddn.bv_len );
    *ptr = '\0';

    ber_memfree( binddn.bv_val );

    if ( !ber_bvstrcasecmp( &client->c_auth, &lloadd_identity ) ) {
        client->c_type = LLOAD_C_PRIVILEGED;
    }

    return operation_send_reject_locked( op, LDAP_SUCCESS, "", 1 );
}

Ondřej Kuzník's avatar
Ondřej Kuzník committed
75
/*
76
77
 * On entering the function, we've put a reference on both connections and hold
 * upstream's c_io_mutex.
Ondřej Kuzník's avatar
Ondřej Kuzník committed
78
 */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
79
static int
80
81
82
83
84
client_bind(
        LloadOperation *op,
        struct berval *binddn,
        ber_tag_t tag,
        struct berval *auth )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
85
{
86
    LloadConnection *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
87

88
    ber_printf( upstream->c_pendingber, "t{titOtO}", LDAP_TAG_MESSAGE,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
89
90
91
92
93
94
95
            LDAP_TAG_MSGID, op->o_upstream_msgid,
            LDAP_REQ_BIND, &op->o_request,
            LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );

    return 0;
}

96
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
97
/*
98
99
 * On entering the function, we've put a reference on both connections and hold
 * upstream's c_io_mutex.
Ondřej Kuzník's avatar
Ondřej Kuzník committed
100
 */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
101
static int
102
103
104
105
106
client_bind_as_vc(
        LloadOperation *op,
        struct berval *binddn,
        ber_tag_t tag,
        struct berval *auth )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
107
{
108
    LloadConnection *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
109

110
    CONNECTION_LOCK(upstream);
111
    ber_printf( upstream->c_pendingber, "t{tit{tst{{tOOtOtO}}}}", LDAP_TAG_MESSAGE,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
112
113
114
115
            LDAP_TAG_MSGID, op->o_upstream_msgid,
            LDAP_REQ_EXTENDED,
            LDAP_TAG_EXOP_REQ_OID, LDAP_EXOP_VERIFY_CREDENTIALS,
            LDAP_TAG_EXOP_REQ_VALUE,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
116
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, BER_BV_OPTIONAL( &upstream->c_vc_cookie ),
Ondřej Kuzník's avatar
Ondřej Kuzník committed
117
118
            &binddn, tag, &auth,
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );
119
    CONNECTION_UNLOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
120
121
    return 0;
}
122
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
/*
 * The client connection can be in the following states:
 * 1) there are betwee zero and many non-bind operations pending
 *    client->c_state == LLOAD_C_READY && client->c_pin_id == 0
 * 2) there is one bind operation pending (waiting on an upstream response)
 *    a) It is a simple bind
 *    b) It is a SASL bind
 * 3) there is one SASL bind in progress (received a LDAP_SASL_BIND_IN_PROGRESS
 *    response)
 *
 * In cases 2 and 3, client->c_state == LLOAD_C_BINDING, a SASL bind is in
 * progress/pending if c_sasl_bind_mech is set.
 *
 * In the first case, client_reset abandons all operations on the respective
 * upstreams, case 2a has client_reset send an anonymous bind to upstream to
 * terminate the bind. In cases 2b and 3, c_pin_id is set and we retrieve the
 * op. The rest is the same for both.
 *
 * If c_pin_id is unset, we request an upstream connection assigned, otherwise,
 * we try to reuse the pinned upstream. In the case of no upstream, we reject
 * the request. A SASL bind request means we acquire a new pin_id if we don't
 * have one already.
 *
 * We have to reset c_auth (which holds the current or pending identity) and
 * make sure we set it up eventually:
 * - In the case of a simple bind, we already know the final identity being
 *   requested so we set it up immediately
 * - In SASL binds, for mechanisms we implement ourselves (EXTERNAL), we set it
 *   up at some point
 * - Otherwise, we have to ask the upstream what it thinks as the bind
 *   succeeds, we send an LDAP "Who Am I?" exop, this is one of the few
 *   requests we send on our own. If we implement the mechanism, we provide the
 *   identity (EXTERNAL uses the client certificate DN)
 *
 * At the end of the request processing, if nothing goes wrong, we're in state
 * 2b (with c_pin_id set to the op's o_pin_id), or state 2a (we could reset
 * c_pin_id/o_pin_id if we wanted but we don't always do that at the moment).
 * If something does go wrong, we're either tearing down the client or we
 * reject the request and switch to state 1 (clearing c_pin_id).
 *
 * As usual, we have to make any changes to the target connection before we've
 * sent the PDU over it - while we are in charge of the read side and nothing
 * happens there without our ceding control, the other read side could wake up
 * at any time and pre-empt us.
 *
 * On a response (in handle_bind_response):
 * - to a simple bind, clear c_auth on a failure otherwise keep it while we
 *   just reset the client to state 1
 * - failure response to a SASL bind - reset client to state 1
 * - LDAP_SASL_BIND_IN_PROGRESS - clear o_*_msgid from the op (have to
 *   remove+reinsert it from the respective c_ops!), we need it since it is the
 *   vessel maintaining the pin between client and upstream
 * - all of the above forward the response immediately
 * - LDAP_SUCCESS for a SASL bind - we send a "Who Am I?" request to retrieve
 *   the client's DN, only on receiving the response do we finalise the
 *   exchange by forwarding the successful bind response
 *
 * We can't do the same for VC Exop since the exchange is finished at the end
 * and we need a change to the VC Exop spec to have the server (optionally?)
 * respond with the final authzid (saving us a roundtrip as well).
 */
185
int
186
request_bind( LloadConnection *client, LloadOperation *op )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
187
{
188
189
    LloadConnection *upstream = NULL;
    BerElement *ber, *copy;
190
    struct berval binddn, auth, mech = BER_BVNULL;
191
192
    ber_int_t version;
    ber_tag_t tag;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
193
    unsigned long pin = client->c_pin_id;
194
    int res, rc = LDAP_SUCCESS;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
195

Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
    if ( pin ) {
        LloadOperation *pinned_op, needle = {
            .o_client_connid = client->c_connid,
            .o_client_msgid = 0,
            .o_pin_id = client->c_pin_id,
        };

        Debug( LDAP_DEBUG_CONNS, "request_bind: "
                "client connid=%lu is pinned pin=%lu\n",
                client->c_connid, pin );

        pinned_op =
                tavl_delete( &client->c_ops, &needle, operation_client_cmp );
        if ( pinned_op ) {
            assert( op->o_tag == pinned_op->o_tag );

            pinned_op->o_client_msgid = op->o_client_msgid;

            /* Preserve the new BerElement and its pointers, reclaim the old
             * one in operation_destroy_from_client if it's still there */
            needle.o_ber = pinned_op->o_ber;
            pinned_op->o_ber = op->o_ber;
            op->o_ber = needle.o_ber;

            pinned_op->o_request = op->o_request;
            pinned_op->o_ctrls = op->o_ctrls;

            /*
             * pinned_op is accessible from the upstream, protect it since we
             * lose the client lock in operation_destroy_from_client temporarily
             */
            pinned_op->o_client_refcnt++;
            operation_destroy_from_client( op );
            pinned_op->o_client_refcnt--;

            op = pinned_op;
        }
    }

235
    /* protect the Bind operation */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
236
    op->o_client_refcnt++;
237
    tavl_delete( &client->c_ops, op, operation_client_cmp );
238

239
    client_reset( client );
240

241
242
    client->c_state = LLOAD_C_BINDING;
    client->c_type = LLOAD_C_OPEN;
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
    if ( (copy = ber_alloc()) == NULL ) {
        goto fail;
    }
    ber_init2( copy, &op->o_request, 0 );

    tag = ber_get_int( copy, &version );
    if ( tag == LBER_ERROR ) {
        Debug( LDAP_DEBUG_PACKETS, "request_bind: "
                "failed to parse version field\n" );
        goto fail;
    } else if ( version != LDAP_VERSION3 ) {
        operation_send_reject_locked(
                op, LDAP_PROTOCOL_ERROR, "LDAP version unsupported", 1 );
        ber_free( copy, 0 );
        return LDAP_SUCCESS;
    }

    tag = ber_get_stringbv( copy, &binddn, LBER_BV_NOTERM );
    if ( tag == LBER_ERROR ) {
        Debug( LDAP_DEBUG_PACKETS, "request_bind: "
                "failed to parse bind name field\n" );
        goto fail;
    }

268
269
270
271
272
    if ( !BER_BVISNULL( &client->c_auth ) ) {
        ch_free( client->c_auth.bv_val );
        BER_BVZERO( &client->c_auth );
    }

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    tag = ber_skip_element( copy, &auth );
    if ( tag == LDAP_AUTH_SIMPLE ) {
        if ( !BER_BVISEMPTY( &binddn ) ) {
            char *ptr;
            client->c_auth.bv_len = STRLENOF("dn:") + binddn.bv_len;
            client->c_auth.bv_val = ch_malloc( client->c_auth.bv_len + 1 );

            ptr = lutil_strcopy( client->c_auth.bv_val, "dn:" );
            ptr = lutil_strncopy( ptr, binddn.bv_val, binddn.bv_len );
            *ptr = '\0';
        }

        if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
            ber_memfree( client->c_sasl_bind_mech.bv_val );
            BER_BVZERO( &client->c_sasl_bind_mech );
        }
    } else if ( tag == LDAP_AUTH_SASL ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
290
291
292
293
294
        ber_init2( copy, &auth, 0 );

        if ( ber_get_stringbv( copy, &mech, LBER_BV_NOTERM ) == LBER_ERROR ) {
            goto fail;
        }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        if ( !ber_bvcmp( &mech, &mech_external ) ) {
            struct berval credentials = BER_BVNULL;

            ber_get_stringbv( copy, &credentials, LBER_BV_NOTERM );
            rc = bind_mech_external( client, op, &credentials );

            /* terminate the upstream side if client switched mechanisms */
            if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
                op->o_client_refcnt++;
                CONNECTION_UNLOCK_INCREF(client);
                operation_abandon( op );
                CONNECTION_LOCK_DECREF(client);

                ber_memfree( client->c_sasl_bind_mech.bv_val );
                BER_BVZERO( &client->c_sasl_bind_mech );
            }

            ber_free( copy, 0 );
            return rc;
        } else if ( BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
315
            ber_dupbv( &client->c_sasl_bind_mech, &mech );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
316
317
        } else if ( ber_bvcmp( &mech, &client->c_sasl_bind_mech ) ) {
            ber_bvreplace( &client->c_sasl_bind_mech, &mech );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
318
        }
319
320
321
322
    } else {
        goto fail;
    }

323
324
    rc = tavl_insert( &client->c_ops, op, operation_client_cmp, avl_dup_error );
    assert( rc == LDAP_SUCCESS );
325
    CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
326

Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
    if ( pin ) {
        ldap_pvt_thread_mutex_lock( &op->o_link_mutex );
        upstream = op->o_upstream;
        if ( upstream ) {
            CONNECTION_LOCK(upstream);
            if ( !upstream->c_live ) {
                CONNECTION_UNLOCK(upstream);
                upstream = NULL;
            }
        }
        ldap_pvt_thread_mutex_unlock( &op->o_link_mutex );
    }

    /* If we were pinned but lost the link, don't look for a new upstream, we
     * have to reject the op and clear pin */
    if ( upstream ) {
        CONNECTION_UNLOCK_INCREF(upstream);
        ldap_pvt_thread_mutex_lock( &upstream->c_io_mutex );
    } else if ( !pin ) {
        upstream = backend_select( op, &res );
    } else {
        Debug( LDAP_DEBUG_STATS, "request_bind: "
                "connid=%lu, msgid=%d pinned upstream lost\n",
                op->o_client_connid, op->o_client_msgid );
        operation_send_reject( op, LDAP_UNAVAILABLE,
                "connection to the remote server has been severed", 1 );
        pin = 0;
        goto done;
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
357
    if ( !upstream ) {
358
        Debug( LDAP_DEBUG_STATS, "request_bind: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
359
360
                "connid=%lu, msgid=%d no available connection found\n",
                op->o_client_connid, op->o_client_msgid );
361
        operation_send_reject( op, res, "no connections available", 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
362
        assert( client->c_pin_id == 0 );
363
364
365
366
367
368
369
370
371
        goto done;
    }

    ber = upstream->c_pendingber;
    if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
        Debug( LDAP_DEBUG_ANY, "request_bind: "
                "ber_alloc failed\n" );
        ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
        CONNECTION_LOCK_DECREF(upstream);
372
373
374
375
        if ( !BER_BVISNULL( &upstream->c_sasl_bind_mech ) ) {
            ber_memfree( upstream->c_sasl_bind_mech.bv_val );
            BER_BVZERO( &upstream->c_sasl_bind_mech );
        }
376
377
        CONNECTION_UNLOCK_OR_DESTROY(upstream);

378
        CONNECTION_LOCK_DECREF(client);
379
        goto fail;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
380
    }
381
    upstream->c_pendingber = ber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
382

383
    CONNECTION_LOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
384
385
    if ( pin ) {
        tavl_delete( &upstream->c_ops, op, operation_upstream_cmp );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
386
387
388
        if ( tag == LDAP_AUTH_SIMPLE ) {
            pin = op->o_pin_id = 0;
        }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
389
390
391
392
393
394
395
396
397
398
    } else if ( tag == LDAP_AUTH_SASL && !op->o_pin_id ) {
        ldap_pvt_thread_mutex_lock( &lload_pin_mutex );
        pin = op->o_pin_id = lload_next_pin++;
        Debug( LDAP_DEBUG_CONNS, "request_bind: "
                "client connid=%lu allocated pin=%lu linking it to upstream "
                "connid=%lu\n",
                op->o_client_connid, pin, upstream->c_connid );
        ldap_pvt_thread_mutex_unlock( &lload_pin_mutex );
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
399
    op->o_upstream = upstream;
400
    op->o_upstream_connid = upstream->c_connid;
401
402
    op->o_upstream_msgid = upstream->c_next_msgid++;

403
404
405
406
407
408
409
410
411
    if ( BER_BVISNULL( &mech ) ) {
        if ( !BER_BVISNULL( &upstream->c_sasl_bind_mech ) ) {
            ber_memfree( upstream->c_sasl_bind_mech.bv_val );
            BER_BVZERO( &upstream->c_sasl_bind_mech );
        }
    } else if ( ber_bvcmp( &upstream->c_sasl_bind_mech, &mech ) ) {
        ber_bvreplace( &upstream->c_sasl_bind_mech, &mech );
    }

412
413
414
415
416
417
418
419
420
421
    Debug( LDAP_DEBUG_TRACE, "request_bind: "
            "added bind from client connid=%lu to upstream connid=%lu "
            "as msgid=%d\n",
            op->o_client_connid, op->o_upstream_connid, op->o_upstream_msgid );
    if ( tavl_insert( &upstream->c_ops, op, operation_upstream_cmp,
                 avl_dup_error ) ) {
        assert(0);
    }
    upstream->c_state = LLOAD_C_BINDING;
    CONNECTION_UNLOCK(upstream);
422
423

#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
424
    if ( lload_features & LLOAD_FEATURE_VC ) {
425
        rc = client_bind_as_vc( op, &binddn, tag, &auth );
426
427
428
    } else
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
    {
429
        rc = client_bind( op, &binddn, tag, &auth );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
430
431
    }

432
433
434
435
436
437
438
done:
    if ( rc == LDAP_SUCCESS ) {
        CONNECTION_LOCK(client);
        if ( upstream ) {
            ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
        }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
439
        client->c_pin_id = pin;
440
441
442
443
444
        if ( !--op->o_client_refcnt || !upstream ) {
            operation_destroy_from_client( op );
            if ( client->c_state == LLOAD_C_BINDING ) {
                client->c_state = LLOAD_C_READY;
                client->c_type = LLOAD_C_OPEN;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
445
                client->c_pin_id = 0;
446
447
448
449
450
451
452
453
454
455
456
                if ( !BER_BVISNULL( &client->c_auth ) ) {
                    ch_free( client->c_auth.bv_val );
                    BER_BVZERO( &client->c_auth );
                }
                if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
                    ber_memfree( client->c_sasl_bind_mech.bv_val );
                    BER_BVZERO( &client->c_sasl_bind_mech );
                }
            }
        }
        CONNECTION_UNLOCK(client);
457

458
459
460
461
462
463
464
465
466
467
468
        if ( upstream ) {
            connection_write_cb( -1, 0, upstream );
            CONNECTION_LOCK_DECREF(upstream);
            CONNECTION_UNLOCK_OR_DESTROY(upstream);
        }
        CONNECTION_LOCK_DECREF(client);
    } else {
fail:
        rc = -1;

        CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
469
470
        op->o_client_refcnt--;
        operation_destroy_from_client( op );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
471
        client->c_pin_id = 0;
472
        CONNECTION_DESTROY(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
473
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
474

475
    ber_free( copy, 0 );
476
    return rc;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
477
}
Ondřej Kuzník's avatar
Ondřej Kuzník committed
478

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * Remember the response, but first ask the server what
 * authorization identity has been negotiated.
 *
 * Also, this request will fail if the server thinks a SASL
 * confidentiality/integrity layer has been negotiated so we catch
 * it early and no other clients are affected.
 */
int
finish_sasl_bind(
        LloadConnection *upstream,
        LloadOperation *op,
        BerElement *ber )
{
    LloadConnection *client = op->o_client;
    BerElement *output;
    LloadOperation *removed;
    ber_int_t msgid;
    int rc;

    if ( !(lload_features & LLOAD_FEATURE_PROXYAUTHZ) ) {
        Debug( LDAP_DEBUG_TRACE, "finish_sasl_bind: "
                "connid=%lu not configured to do proxyauthz, making no "
                "attempt to resolve final authzid name\n",
                op->o_client_connid );
        CONNECTION_UNLOCK(upstream);
        return forward_final_response( client, op, ber );
    }

    removed = tavl_delete( &upstream->c_ops, op, operation_upstream_cmp );
    if ( !removed ) {
        assert( upstream->c_state != LLOAD_C_BINDING );
        /* FIXME: has client replaced this bind since? */
        assert(0);

        operation_destroy_from_upstream( op );
    }
    assert( removed == op && upstream->c_state == LLOAD_C_BINDING );

    CONNECTION_UNLOCK(upstream);

    Debug( LDAP_DEBUG_TRACE, "finish_sasl_bind: "
            "SASL exchange in lieu of client connid=%lu to upstream "
            "connid=%lu finished, resolving final authzid name\n",
            op->o_client_connid, op->o_upstream_connid );

    ldap_pvt_thread_mutex_lock( &upstream->c_io_mutex );
    output = upstream->c_pendingber;
    if ( output == NULL && (output = ber_alloc()) == NULL ) {
        ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
        return -1;
    }
    upstream->c_pendingber = output;

    msgid = upstream->c_next_msgid++;
    ber_printf( output, "t{tit{ts}}", LDAP_TAG_MESSAGE,
            LDAP_TAG_MSGID, msgid,
            LDAP_REQ_EXTENDED,
            LDAP_TAG_EXOP_REQ_OID, LDAP_EXOP_WHO_AM_I );

    /* Make sure noone flushes the buffer before we re-insert the operation */
    CONNECTION_LOCK(upstream);
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );

    op->o_upstream_msgid = msgid;

    /* remember the response for later */
    ber_free( op->o_ber, 1 );
    op->o_ber = ber;

    rc = tavl_insert(
            &upstream->c_ops, op, operation_upstream_cmp, avl_dup_error );
    assert( rc == LDAP_SUCCESS );

    CONNECTION_UNLOCK(upstream);

    connection_write_cb( -1, 0, upstream );
    return LDAP_SUCCESS;
}

Ondřej Kuzník's avatar
Ondřej Kuzník committed
559
int
560
561
562
563
handle_bind_response(
        LloadConnection *client,
        LloadOperation *op,
        BerElement *ber )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
564
{
565
    LloadConnection *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
566
567
    BerValue response;
    BerElement *copy;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
568
    LloadOperation *removed;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
    ber_int_t result;
    ber_tag_t tag;
    int rc = LDAP_SUCCESS;

    if ( (copy = ber_alloc()) == NULL ) {
        rc = -1;
        goto done;
    }

    tag = ber_peek_element( ber, &response );
    assert( tag == LDAP_RES_BIND );

    ber_init2( copy, &response, 0 );

    tag = ber_get_enum( copy, &result );
    ber_free( copy, 0 );

    if ( tag == LBER_ERROR ) {
        rc = -1;
        goto done;
    }

    Debug( LDAP_DEBUG_STATS, "handle_bind_response: "
            "received response for bind request msgid=%d by client "
            "connid=%lu, result=%d\n",
            op->o_client_msgid, op->o_client_connid, result );

    CONNECTION_LOCK(upstream);
    if ( result != LDAP_SASL_BIND_IN_PROGRESS ) {
598
        int sasl_finished = 0;
599
        if ( !BER_BVISNULL( &upstream->c_sasl_bind_mech ) ) {
600
            sasl_finished = 1;
601
602
603
604
            ber_memfree( upstream->c_sasl_bind_mech.bv_val );
            BER_BVZERO( &upstream->c_sasl_bind_mech );
        }

605
        assert( op->o_client_msgid && op->o_upstream_msgid );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
606
        op->o_pin_id = 0;
607
608
609
610
611

        if ( sasl_finished && result == LDAP_SUCCESS ) {
            return finish_sasl_bind( upstream, op, ber );
        }
        upstream->c_state = LLOAD_C_READY;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
612
613
614
615
616
617
618
619
    } else {
        if ( tavl_delete( &upstream->c_ops, op, operation_upstream_cmp ) ) {
            op->o_upstream_msgid = 0;
            op->o_upstream_refcnt++;
            rc = tavl_insert( &upstream->c_ops, op, operation_upstream_cmp,
                    avl_dup_error );
            assert( rc == LDAP_SUCCESS );
        }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
620
621
622
623
    }
    CONNECTION_UNLOCK(upstream);

    CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
624
625
626
    removed = tavl_delete( &client->c_ops, op, operation_client_cmp );
    assert( !removed || op == removed );

Ondřej Kuzník's avatar
Ondřej Kuzník committed
627
628
629
    if ( client->c_state == LLOAD_C_BINDING ) {
        switch ( result ) {
            case LDAP_SASL_BIND_IN_PROGRESS:
630
                op->o_saved_msgid = op->o_client_msgid;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
631
632
633
634
                op->o_client_msgid = 0;
                rc = tavl_insert( &client->c_ops, op, operation_client_cmp,
                        avl_dup_error );
                assert( rc == LDAP_SUCCESS );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
635
636
637
                break;
            case LDAP_SUCCESS:
            default: {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
638
                op->o_client = NULL;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
639
640
                client->c_state = LLOAD_C_READY;
                client->c_type = LLOAD_C_OPEN;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
641
                client->c_pin_id = 0;
642
643
644
645
646
647
648
649
                if ( !BER_BVISNULL( &client->c_auth ) ) {
                    if ( result != LDAP_SUCCESS ) {
                        ber_memfree( client->c_auth.bv_val );
                        BER_BVZERO( &client->c_auth );
                    } else if ( !ber_bvstrcasecmp(
                                        &client->c_auth, &lloadd_identity ) ) {
                        client->c_type = LLOAD_C_PRIVILEGED;
                    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
                }
                if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
                    ber_memfree( client->c_sasl_bind_mech.bv_val );
                    BER_BVZERO( &client->c_sasl_bind_mech );
                }
                break;
            }
        }
    } else {
        assert( client->c_state == LLOAD_C_INVALID ||
                client->c_state == LLOAD_C_CLOSING );
    }
    CONNECTION_UNLOCK(client);

done:
    if ( rc ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
666
        operation_send_reject( op, LDAP_OTHER, "internal error", 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
667
668
669
670

        ber_free( ber, 1 );
        return LDAP_SUCCESS;
    }
671
    return forward_final_response( client, op, ber );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
672
673
}

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
int
handle_whoami_response(
        LloadConnection *client,
        LloadOperation *op,
        BerElement *ber )
{
    LloadConnection *upstream = op->o_upstream;
    BerValue matched, diagmsg;
    BerElement *saved_response = op->o_ber;
    LloadOperation *removed;
    ber_int_t result;
    ber_tag_t tag;
    ber_len_t len;

    Debug( LDAP_DEBUG_TRACE, "handle_whoami_response: "
            "connid=%ld received whoami response in lieu of connid=%ld\n",
            upstream->c_connid, client->c_connid );

    tag = ber_scanf( ber, "{emm" /* "}" */,
            &result, &matched, &diagmsg );
    if ( tag == LBER_ERROR ) {
        operation_send_reject( op, LDAP_OTHER, "upstream protocol error", 0 );
        return -1;
    }

    CONNECTION_LOCK_DECREF(upstream);
    if ( result == LDAP_PROTOCOL_ERROR ) {
        LloadBackend *b;

        b = (LloadBackend *)upstream->c_private;
        Debug( LDAP_DEBUG_ANY, "handle_whoami_response: "
                "Who Am I? extended operation not supported on backend %s, "
                "proxyauthz with clients that do SASL binds will not work "
                "msg=%s!\n",
                b->b_uri.bv_val, diagmsg.bv_val );
        CONNECTION_UNLOCK_INCREF(upstream);
        operation_send_reject( op, LDAP_OTHER, "upstream protocol error", 0 );
        return -1;
    }
    upstream->c_state = LLOAD_C_READY;

    CONNECTION_UNLOCK_INCREF(upstream);

    tag = ber_peek_tag( ber, &len );

    CONNECTION_LOCK_DECREF(client);

    assert( client->c_state == LLOAD_C_BINDING &&
            BER_BVISNULL( &client->c_auth ) );
    if ( !BER_BVISNULL( &client->c_auth ) ) {
        ber_memfree( client->c_auth.bv_val );
        BER_BVZERO( &client->c_auth );
    }

    if ( tag == LDAP_TAG_EXOP_RES_VALUE ) {
        tag = ber_scanf( ber, "o", &client->c_auth );
        if ( tag == LBER_ERROR ) {
            operation_send_reject_locked(
                    op, LDAP_OTHER, "upstream protocol error", 0 );
            CONNECTION_DESTROY(client);
            return -1;
        }
    }

    removed = tavl_delete( &client->c_ops, op, operation_client_cmp );
    assert( !removed || op == removed );

    Debug( LDAP_DEBUG_TRACE, "handle_whoami_response: "
            "connid=%ld new authid=%s\n",
            client->c_connid, client->c_auth.bv_val );

    if ( client->c_state == LLOAD_C_BINDING ) {
        op->o_client = NULL;
        client->c_state = LLOAD_C_READY;
        client->c_type = LLOAD_C_OPEN;
        client->c_pin_id = 0;
        if ( !BER_BVISNULL( &client->c_auth ) &&
                !ber_bvstrcasecmp( &client->c_auth, &lloadd_identity ) ) {
            client->c_type = LLOAD_C_PRIVILEGED;
        }
        if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
            ber_memfree( client->c_sasl_bind_mech.bv_val );
            BER_BVZERO( &client->c_sasl_bind_mech );
        }
    }

    CONNECTION_UNLOCK_INCREF(client);

    /* defer the disposal of ber to operation_destroy_* */
    op->o_ber = ber;
    return forward_final_response( client, op, saved_response );
}

Ondřej Kuzník's avatar
Ondřej Kuzník committed
767
768
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
int
769
770
771
772
handle_vc_bind_response(
        LloadConnection *client,
        LloadOperation *op,
        BerElement *ber )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
{
    BerElement *output;
    BerValue matched, diagmsg, creds = BER_BVNULL, controls = BER_BVNULL;
    ber_int_t result;
    ber_tag_t tag;
    ber_len_t len;
    int rc = 0;

    tag = ber_scanf( ber, "{emm" /* "}" */,
            &result, &matched, &diagmsg );
    if ( tag == LBER_ERROR ) {
        rc = -1;
        goto done;
    }

    tag = ber_peek_tag( ber, &len );
    if ( result == LDAP_PROTOCOL_ERROR ) {
790
791
        LloadConnection *upstream = op->o_upstream;
        LloadBackend *b;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
792
793

        CONNECTION_LOCK(upstream);
794
        b = (LloadBackend *)upstream->c_private;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
795
796
797
798
799
800
801
802
803
804
805
        Debug( LDAP_DEBUG_ANY, "handle_vc_bind_response: "
                "VC extended operation not supported on backend %s\n",
                b->b_uri.bv_val );
        CONNECTION_UNLOCK(upstream);
    }

    Debug( LDAP_DEBUG_STATS, "handle_vc_bind_response: "
            "received response for bind request msgid=%d by client "
            "connid=%lu, result=%d\n",
            op->o_client_msgid, op->o_client_connid, result );

806
    CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
807
808

    if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE ) {
809
810
        if ( !BER_BVISNULL( &client->c_vc_cookie ) ) {
            ber_memfree( client->c_vc_cookie.bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
811
        }
812
        tag = ber_scanf( ber, "o", &client->c_vc_cookie );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
813
814
        if ( tag == LBER_ERROR ) {
            rc = -1;
815
            CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
816
817
818
819
820
821
822
823
824
            goto done;
        }
        tag = ber_peek_tag( ber, &len );
    }

    if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_SCREDS ) {
        tag = ber_scanf( ber, "m", &creds );
        if ( tag == LBER_ERROR ) {
            rc = -1;
825
            CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
826
827
828
829
830
831
832
833
834
            goto done;
        }
        tag = ber_peek_tag( ber, &len );
    }

    if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS ) {
        tag = ber_scanf( ber, "m", &controls );
        if ( tag == LBER_ERROR ) {
            rc = -1;
835
            CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
836
837
838
839
            goto done;
        }
    }

840
    if ( client->c_state == LLOAD_C_BINDING ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
841
842
843
844
845
        switch ( result ) {
            case LDAP_SASL_BIND_IN_PROGRESS:
                break;
            case LDAP_SUCCESS:
            default: {
846
847
                client->c_state = LLOAD_C_READY;
                client->c_type = LLOAD_C_OPEN;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
848
                client->c_pin_id = 0;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
849
                if ( result != LDAP_SUCCESS ) {
850
851
                    ber_memfree( client->c_auth.bv_val );
                    BER_BVZERO( &client->c_auth );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
852
                } else if ( !ber_bvstrcasecmp(
853
854
                                    &client->c_auth, &lloadd_identity ) ) {
                    client->c_type = LLOAD_C_PRIVILEGED;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
855
                }
856
857
858
                if ( !BER_BVISNULL( &client->c_vc_cookie ) ) {
                    ber_memfree( client->c_vc_cookie.bv_val );
                    BER_BVZERO( &client->c_vc_cookie );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
859
                }
860
861
862
                if ( !BER_BVISNULL( &client->c_sasl_bind_mech ) ) {
                    ber_memfree( client->c_sasl_bind_mech.bv_val );
                    BER_BVZERO( &client->c_sasl_bind_mech );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
863
864
865
866
867
                }
                break;
            }
        }
    } else {
868
869
        assert( client->c_state == LLOAD_C_INVALID ||
                client->c_state == LLOAD_C_CLOSING );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
870
    }
871
    CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
872

873
874
    ldap_pvt_thread_mutex_lock( &client->c_io_mutex );
    output = client->c_pendingber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
875
876
    if ( output == NULL && (output = ber_alloc()) == NULL ) {
        rc = -1;
877
        ldap_pvt_thread_mutex_unlock( &client->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
878
879
        goto done;
    }
880
    client->c_pendingber = output;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
881
882
883
884
885
886
887

    rc = ber_printf( output, "t{tit{eOOtO}tO}", LDAP_TAG_MESSAGE,
            LDAP_TAG_MSGID, op->o_client_msgid, LDAP_RES_BIND,
            result, &matched, &diagmsg,
            LDAP_TAG_SASL_RES_CREDS, BER_BV_OPTIONAL( &creds ),
            LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &controls ) );

888
    ldap_pvt_thread_mutex_unlock( &client->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
889
    if ( rc >= 0 ) {
890
        connection_write_cb( -1, 0, client );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
891
892
893
894
        rc = 0;
    }

done:
895
    CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
896
    operation_destroy_from_client( op );
897
    CONNECTION_UNLOCK_OR_DESTROY(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
898
899
900
901
    ber_free( ber, 1 );
    return rc;
}
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */