bind.c 16.5 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
25
26
/* $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"
#include "slap.h"

Ondřej Kuzník's avatar
Ondřej Kuzník committed
27
/*
28
29
 * 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
30
 */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
31
static int
Ondřej Kuzník's avatar
Ondřej Kuzník committed
32
client_bind( Operation *op )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
33
{
Ondřej Kuzník's avatar
Ondřej Kuzník committed
34
    Connection *client = op->o_client, *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
35
36
37
38
39
    BerElement *ber, *copy = NULL;
    BerValue binddn;
    ber_tag_t tag;
    ber_int_t version;

Ondřej Kuzník's avatar
Ondřej Kuzník committed
40
    ber = upstream->c_pendingber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
41
42
43
44
45
    if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
        Debug( LDAP_DEBUG_ANY, "request_bind: "
                "ber_alloc failed\n" );
        goto fail;
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
46
    upstream->c_pendingber = ber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
47
48
49
50
51
52
53
54

    if ( (copy = ber_alloc()) == NULL ) {
        goto fail;
    }
    ber_init2( copy, &op->o_request, 0 );

    tag = ber_get_int( copy, &version );
    if ( tag == LBER_ERROR ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
55
56
        Debug( LDAP_DEBUG_PACKETS, "request_bind: "
                "failed to parse version field\n" );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
57
58
        goto fail;
    } else if ( version != LDAP_VERSION3 ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
59
        ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
60
        operation_send_reject(
61
                op, LDAP_PROTOCOL_ERROR, "LDAP version unsupported", 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
62
63
64
65
66
67
        ber_free( copy, 0 );
        return 0;
    }

    tag = ber_get_stringbv( copy, &binddn, LBER_BV_NOTERM );
    if ( tag == LBER_ERROR ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
68
69
        Debug( LDAP_DEBUG_PACKETS, "request_bind: "
                "failed to parse bind name field\n" );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
70
71
72
        goto fail;
    }

73
    CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
74
75
    if ( !BER_BVISNULL( &client->c_auth ) ) {
        ch_free( client->c_auth.bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
76
77
78
79
    }

    if ( !BER_BVISEMPTY( &binddn ) ) {
        char *ptr;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
80
81
        client->c_auth.bv_len = STRLENOF("dn:") + binddn.bv_len;
        client->c_auth.bv_val = ch_malloc( client->c_auth.bv_len + 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
82

Ondřej Kuzník's avatar
Ondřej Kuzník committed
83
        ptr = lutil_strcopy( client->c_auth.bv_val, "dn:" );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
84
85
86
        ptr = lutil_strncopy( ptr, binddn.bv_val, binddn.bv_len );
        *ptr = '\0';
    } else {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
87
        BER_BVZERO( &client->c_auth );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
88
    }
89
    CONNECTION_UNLOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
90

91
    CONNECTION_LOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
92
    op->o_upstream_msgid = upstream->c_next_msgid++;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
93
94
95
96
97
98

    ber_printf( ber, "t{titOtO}", LDAP_TAG_MESSAGE,
            LDAP_TAG_MSGID, op->o_upstream_msgid,
            LDAP_REQ_BIND, &op->o_request,
            LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );

99
    Debug( LDAP_DEBUG_TRACE, "request_bind: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
100
101
            "added bind from client connid=%lu to upstream connid=%lu "
            "as msgid=%d\n",
102
            op->o_client_connid, op->o_upstream_connid, op->o_upstream_msgid );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
103
104
    if ( tavl_insert( &upstream->c_ops, op, operation_upstream_cmp,
                 avl_dup_error ) ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
105
106
        assert(0);
    }
107
    upstream->c_state = LLOAD_C_ACTIVE;
108
    CONNECTION_UNLOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
109

Ondřej Kuzník's avatar
Ondřej Kuzník committed
110
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
111
112

    ber_free( copy, 0 );
113
    connection_write_cb( -1, 0, upstream );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
114
115
116
117
118
119
    return 0;

fail:
    if ( copy ) {
        ber_free( copy, 0 );
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
120
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
121
122
123
    Debug( LDAP_DEBUG_STATS, "request_bind: "
            "connid=%lu bind request processing failed, closing\n",
            client->c_connid );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
124
125
126
    return 1;
}

127
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
128
/*
129
130
 * 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
131
 */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
132
static int
Ondřej Kuzník's avatar
Ondřej Kuzník committed
133
client_bind_as_vc( Operation *op )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
134
{
Ondřej Kuzník's avatar
Ondřej Kuzník committed
135
    Connection *client = op->o_client, *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
136
137
    BerElement *ber, *request, *copy = NULL;
    BerValue binddn, auth, mech;
138
139
    char *msg = "internal error";
    int result = LDAP_OTHER;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
140
141
142
143
144
145
146
147
148
149
150
    ber_int_t version;
    ber_tag_t tag;
    ber_len_t len;

    if ( (request = ber_alloc()) == NULL ) {
        goto fail;
    }
    ber_init2( request, &op->o_request, 0 );

    tag = ber_scanf( request, "im", &version, &binddn );
    if ( tag == LBER_ERROR || version != LDAP_VERSION3 ) {
151
152
        result = LDAP_PROTOCOL_ERROR;
        msg = "version not recognised";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
153
154
155
156
157
158
159
160
161
162
        goto fail;
    }

    copy = ber_dup( request );
    if ( !copy ) {
        goto fail;
    }

    tag = ber_skip_element( request, &auth );
    if ( tag == LBER_ERROR ) {
163
164
        result = LDAP_PROTOCOL_ERROR;
        msg = "malformed bind request";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
165
166
167
        goto fail;
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
168
    ber = upstream->c_pendingber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
169
170
171
172
173
    if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
        Debug( LDAP_DEBUG_ANY, "request_bind_as_vc: "
                "ber_alloc failed\n" );
        goto fail;
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
174
    upstream->c_pendingber = ber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
175

Ondřej Kuzník's avatar
Ondřej Kuzník committed
176
    op->o_upstream_msgid = upstream->c_next_msgid++;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
177

178
    CONNECTION_LOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
179
180
181
182
183
    ber_printf( ber, "t{tit{tst{{tOOtOtO}}}}", LDAP_TAG_MESSAGE,
            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
184
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, BER_BV_OPTIONAL( &upstream->c_vc_cookie ),
Ondřej Kuzník's avatar
Ondřej Kuzník committed
185
186
            &binddn, tag, &auth,
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );
187
    CONNECTION_UNLOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
188
189
190
191
192

    tag = ber_peek_tag( copy, &len );
    switch ( tag ) {
        case LDAP_AUTH_SASL:
            ber_get_stringbv( copy, &mech, LBER_BV_NOTERM );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
193

194
            CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
195
196
197
            if ( ber_bvcmp( &mech, &client->c_sasl_bind_mech ) ) {
                ber_memfree( client->c_sasl_bind_mech.bv_val );
                ber_dupbv( &client->c_sasl_bind_mech, &mech );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
198
            }
199
            CONNECTION_UNLOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
200
201
202
            /* TODO: extract authzdn from the message */
            break;
        case LDAP_AUTH_SIMPLE:
203
            CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
204
205
            if ( !BER_BVISNULL( &client->c_auth ) ) {
                ch_free( client->c_auth.bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
206
            }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
207
208
            if ( !BER_BVISEMPTY( &binddn ) ) {
                char *ptr;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
209
210
                client->c_auth.bv_len = STRLENOF("dn:") + binddn.bv_len;
                client->c_auth.bv_val = ch_malloc( client->c_auth.bv_len + 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
211

Ondřej Kuzník's avatar
Ondřej Kuzník committed
212
                ptr = lutil_strcopy( client->c_auth.bv_val, "dn:" );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
213
214
215
                ptr = lutil_strncopy( ptr, binddn.bv_val, binddn.bv_len );
                *ptr = '\0';
            } else {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
216
                BER_BVZERO( &client->c_auth );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
217
218
            }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
219
220
221
            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
222
            }
223
            CONNECTION_UNLOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
224
225
            break;
        default:
226
227
            result = LDAP_PROTOCOL_ERROR;
            msg = "malformed bind request";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
228
229
            goto fail;
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
230

231
    CONNECTION_LOCK(upstream);
232
    Debug( LDAP_DEBUG_TRACE, "request_bind_as_vc: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
233
234
            "added bind from client connid=%lu to upstream connid=%lu "
            "as VC exop msgid=%d\n",
235
            op->o_client_connid, op->o_upstream_connid, op->o_upstream_msgid );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
236
237
    if ( tavl_insert( &upstream->c_ops, op, operation_upstream_cmp,
                 avl_dup_error ) ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
238
239
        assert(0);
    }
240
    CONNECTION_UNLOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
241

Ondřej Kuzník's avatar
Ondřej Kuzník committed
242
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
243
244

    ber_free( copy, 0 );
245
    connection_write_cb( -1, 0, upstream );
246

Ondřej Kuzník's avatar
Ondřej Kuzník committed
247
248
249
250
251
252
    return 0;

fail:
    if ( copy ) {
        ber_free( copy, 0 );
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
253
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
254
255
256
    Debug( LDAP_DEBUG_STATS, "request_bind_as_vc: "
            "connid=%lu bind request processing failed, closing\n",
            client->c_connid );
257
    operation_send_reject( op, result, msg, 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
258
259
    return 1;
}
260
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
261

262
int
Ondřej Kuzník's avatar
Ondřej Kuzník committed
263
request_bind( Connection *client, Operation *op )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
264
{
265
266
    Connection *upstream;
    int rc = LDAP_SUCCESS;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
267

268
    /* protect the Bind operation */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
269
    op->o_client_refcnt++;
270
    tavl_delete( &client->c_ops, op, operation_client_cmp );
271

272
    client_reset( client );
273

274
275
    client->c_state = LLOAD_C_BINDING;
    client->c_type = LLOAD_C_OPEN;
276

277
278
    rc = tavl_insert( &client->c_ops, op, operation_client_cmp, avl_dup_error );
    assert( rc == LDAP_SUCCESS );
279
    CONNECTION_UNLOCK_INCREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
280
281
282
283

    upstream = backend_select( op );
    if ( !upstream ) {
        Debug( LDAP_DEBUG_STATS, "client_bind: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
284
285
                "connid=%lu, msgid=%d no available connection found\n",
                op->o_client_connid, op->o_client_msgid );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
286
        operation_send_reject(
287
                op, LDAP_UNAVAILABLE, "no connections available", 1 );
288
        CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
289
290
        op->o_client_refcnt--;
        operation_destroy_from_client( op );
291
        return rc;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
292
293
294
    }

    op->o_upstream = upstream;
295
    op->o_upstream_connid = upstream->c_connid;
296
297

#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
298
    if ( lload_features & LLOAD_FEATURE_VC ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
299
        rc = client_bind_as_vc( op );
300
301
302
    } else
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
    {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
303
        rc = client_bind( op );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
304
305
    }

306
    CONNECTION_LOCK_DECREF(upstream);
307
    CONNECTION_UNLOCK_OR_DESTROY(upstream);
308

309
    CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
310
    if ( rc ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
311
312
        op->o_client_refcnt--;
        operation_destroy_from_client( op );
313
        CONNECTION_DESTROY(client);
314
        return -1;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
315
316
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
317
318
    if ( !--op->o_client_refcnt ) {
        operation_destroy_from_client( op );
319
320
321
        if ( client->c_state == LLOAD_C_BINDING ) {
            client->c_state = LLOAD_C_READY;
            client->c_type = LLOAD_C_OPEN;
322
323
324
325
326
327
328
329
330
            if ( !BER_BVISNULL( &client->c_auth ) ) {
                ber_memfree( 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 );
            }
        }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
331
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
332

333
    return rc;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
334
}
Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
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

int
handle_bind_response( Operation *op, BerElement *ber )
{
    Connection *client = op->o_client, *upstream = op->o_upstream;
    BerValue response;
    BerElement *copy;
    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 ) {
        upstream->c_state = LLOAD_C_READY;
    }
    CONNECTION_UNLOCK(upstream);

    CONNECTION_LOCK(client);
    if ( client->c_state == LLOAD_C_BINDING ) {
        switch ( result ) {
            case LDAP_SASL_BIND_IN_PROGRESS:
                break;
            case LDAP_SUCCESS:
            default: {
                client->c_state = LLOAD_C_READY;
                client->c_type = LLOAD_C_OPEN;
                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;
                }
                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 ) {
        operation_send_reject( op, LDAP_OTHER, "internal error", 0 );

        ber_free( ber, 1 );
        return LDAP_SUCCESS;
    }
    return forward_final_response( op, ber );
}

#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
int
handle_vc_bind_response( Operation *op, BerElement *ber )
{
    Connection *c = op->o_client;
    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 ) {
        Connection *upstream = op->o_upstream;
        Backend *b;

        CONNECTION_LOCK(upstream);
        b = (Backend *)upstream->c_private;
        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 );

    CONNECTION_LOCK(c);

    if ( tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE ) {
        if ( !BER_BVISNULL( &c->c_vc_cookie ) ) {
            ber_memfree( c->c_vc_cookie.bv_val );
        }
        tag = ber_scanf( ber, "o", &c->c_vc_cookie );
        if ( tag == LBER_ERROR ) {
            rc = -1;
            CONNECTION_UNLOCK_INCREF(c);
            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;
            CONNECTION_UNLOCK_INCREF(c);
            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;
            CONNECTION_UNLOCK_INCREF(c);
            goto done;
        }
    }

    if ( c->c_state == LLOAD_C_BINDING ) {
        switch ( result ) {
            case LDAP_SASL_BIND_IN_PROGRESS:
                break;
            case LDAP_SUCCESS:
            default: {
                c->c_state = LLOAD_C_READY;
                c->c_type = LLOAD_C_OPEN;
                if ( result != LDAP_SUCCESS ) {
                    ber_memfree( c->c_auth.bv_val );
                    BER_BVZERO( &c->c_auth );
                } else if ( !ber_bvstrcasecmp(
                                    &c->c_auth, &lloadd_identity ) ) {
                    c->c_type = LLOAD_C_PRIVILEGED;
                }
                if ( !BER_BVISNULL( &c->c_vc_cookie ) ) {
                    ber_memfree( c->c_vc_cookie.bv_val );
                    BER_BVZERO( &c->c_vc_cookie );
                }
                if ( !BER_BVISNULL( &c->c_sasl_bind_mech ) ) {
                    ber_memfree( c->c_sasl_bind_mech.bv_val );
                    BER_BVZERO( &c->c_sasl_bind_mech );
                }
                break;
            }
        }
    } else {
        assert( c->c_state == LLOAD_C_INVALID ||
                c->c_state == LLOAD_C_CLOSING );
    }
    CONNECTION_UNLOCK_INCREF(c);

    ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
    output = c->c_pendingber;
    if ( output == NULL && (output = ber_alloc()) == NULL ) {
        rc = -1;
        ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
        goto done;
    }
    c->c_pendingber = output;

    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 ) );

    ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
    if ( rc >= 0 ) {
        connection_write_cb( -1, 0, c );
        rc = 0;
    }

done:
    CONNECTION_LOCK_DECREF(c);
    operation_destroy_from_client( op );
    CONNECTION_UNLOCK_OR_DESTROY(c);
    ber_free( ber, 1 );
    return rc;
}
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */