bind.c 11.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
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
32
33
static int
request_bind( Operation *op )
{
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
55
56

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

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

    tag = ber_get_stringbv( copy, &binddn, LBER_BV_NOTERM );
    if ( tag == LBER_ERROR ) {
        goto fail;
    }

69
    CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
70
71
    if ( !BER_BVISNULL( &client->c_auth ) ) {
        ch_free( client->c_auth.bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
72
73
74
75
    }

    if ( !BER_BVISEMPTY( &binddn ) ) {
        char *ptr;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
76
77
        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
78

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

87
    CONNECTION_LOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
88
    op->o_upstream_msgid = upstream->c_next_msgid++;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
89
90
91
92
93
94

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

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

Ondřej Kuzník's avatar
Ondřej Kuzník committed
106
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
107
108

    ber_free( copy, 0 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
109
    upstream_write_cb( -1, 0, upstream );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
110
111
112
113
114
115
    return 0;

fail:
    if ( copy ) {
        ber_free( copy, 0 );
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
116
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
117
118
119
    return 1;
}

120
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
121
/*
122
123
 * 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
124
 */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
125
126
127
static int
request_bind_as_vc( Operation *op )
{
Ondřej Kuzník's avatar
Ondřej Kuzník committed
128
    Connection *client = op->o_client, *upstream = op->o_upstream;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
129
130
    BerElement *ber, *request, *copy = NULL;
    BerValue binddn, auth, mech;
131
132
    char *msg = "internal error";
    int result = LDAP_OTHER;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
133
134
135
136
137
138
139
140
141
142
143
    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 ) {
144
145
        result = LDAP_PROTOCOL_ERROR;
        msg = "version not recognised";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
146
147
148
149
150
151
152
153
154
155
        goto fail;
    }

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

    tag = ber_skip_element( request, &auth );
    if ( tag == LBER_ERROR ) {
156
157
        result = LDAP_PROTOCOL_ERROR;
        msg = "malformed bind request";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
158
159
160
        goto fail;
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
161
    ber = upstream->c_pendingber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
162
163
164
165
166
    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
167
    upstream->c_pendingber = ber;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
168

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

171
    CONNECTION_LOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
172
173
174
175
176
    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
177
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE, BER_BV_OPTIONAL( &upstream->c_vc_cookie ),
Ondřej Kuzník's avatar
Ondřej Kuzník committed
178
179
            &binddn, tag, &auth,
            LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );
180
    CONNECTION_UNLOCK(upstream);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
181
182
183
184
185

    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
186

187
            CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
188
189
190
            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
191
            }
192
            CONNECTION_UNLOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
193
194
195
            /* TODO: extract authzdn from the message */
            break;
        case LDAP_AUTH_SIMPLE:
196
            CONNECTION_LOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
197
198
            if ( !BER_BVISNULL( &client->c_auth ) ) {
                ch_free( client->c_auth.bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
199
            }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
200
201
            if ( !BER_BVISEMPTY( &binddn ) ) {
                char *ptr;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
202
203
                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
204

Ondřej Kuzník's avatar
Ondřej Kuzník committed
205
                ptr = lutil_strcopy( client->c_auth.bv_val, "dn:" );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
206
207
208
                ptr = lutil_strncopy( ptr, binddn.bv_val, binddn.bv_len );
                *ptr = '\0';
            } else {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
209
                BER_BVZERO( &client->c_auth );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
210
211
            }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
212
213
214
            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
215
            }
216
            CONNECTION_UNLOCK(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
217
218
            break;
        default:
219
220
            result = LDAP_PROTOCOL_ERROR;
            msg = "malformed bind request";
Ondřej Kuzník's avatar
Ondřej Kuzník committed
221
222
            goto fail;
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
223

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

Ondřej Kuzník's avatar
Ondřej Kuzník committed
235
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
236
237

    ber_free( copy, 0 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
238
    upstream_write_cb( -1, 0, upstream );
239

Ondřej Kuzník's avatar
Ondřej Kuzník committed
240
241
242
243
244
245
    return 0;

fail:
    if ( copy ) {
        ber_free( copy, 0 );
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
246
    ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
247
    operation_send_reject( op, result, msg, 1 );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
248
249
    return 1;
}
250
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
251

252
253
void
client_reset( Connection *c )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
254
255
{
    TAvlnode *root;
256
    int freed;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
257
258
259

    root = c->c_ops;
    c->c_ops = NULL;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273

    /* unless op->o_client_refcnt > op->o_client_live, there is noone using the
     * operation from the client side and noone new will now that we've removed
     * it from client's c_ops */
    if ( root ) {
        TAvlnode *node = tavl_end( root, TAVL_DIR_LEFT );
        do {
            Operation *op = node->avl_data;

            /* make sure it's useable after we've unlocked the connection */
            op->o_client_refcnt++;
        } while ( (node = tavl_next( node, TAVL_DIR_RIGHT )) );
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
274
275
276
277
278
279
280
281
    if ( !BER_BVISNULL( &c->c_auth ) ) {
        ch_free( c->c_auth.bv_val );
        BER_BVZERO( &c->c_auth );
    }
    if ( !BER_BVISNULL( &c->c_sasl_bind_mech ) ) {
        ch_free( c->c_sasl_bind_mech.bv_val );
        BER_BVZERO( &c->c_sasl_bind_mech );
    }
282
    CONNECTION_UNLOCK_INCREF(c);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
283

Ondřej Kuzník's avatar
Ondřej Kuzník committed
284
285
286
287
288
289
    if ( root ) {
        TAvlnode *node = tavl_end( root, TAVL_DIR_LEFT );
        do {
            Operation *op = node->avl_data;

            operation_abandon( op );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
290

Ondřej Kuzník's avatar
Ondřej Kuzník committed
291
292
293
294
295
296
297
298
299
300
301
            CONNECTION_LOCK(c);
            op->o_client_refcnt--;
            operation_destroy_from_client( op );
            CONNECTION_UNLOCK(c);
        } while ( (node = tavl_next( node, TAVL_DIR_RIGHT )) );

        freed = tavl_free( root, NULL );
        Debug( LDAP_DEBUG_TRACE, "client_reset: "
                "dropped %d operations\n",
                freed );
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
302

303
    CONNECTION_LOCK_DECREF(c);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
304
305
}

306
307
int
client_bind( Connection *client, Operation *op )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
308
{
309
310
    Connection *upstream;
    int rc = LDAP_SUCCESS;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
311

312
    /* protect the Bind operation */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
313
    op->o_client_refcnt++;
314
315
    tavl_delete( &client->c_ops, op, operation_client_cmp );
    client->c_state = SLAP_C_BINDING;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
316
    client->c_type = SLAP_C_OPEN;
317

318
    client_reset( client );
319
320
321

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

    upstream = backend_select( op );
    if ( !upstream ) {
        Debug( LDAP_DEBUG_STATS, "client_bind: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
327
328
                "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
329
        operation_send_reject(
330
                op, LDAP_UNAVAILABLE, "no connections available", 1 );
331
        CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
332
333
        op->o_client_refcnt--;
        operation_destroy_from_client( op );
334
        return rc;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
335
336
337
    }

    op->o_upstream = upstream;
338
    op->o_upstream_connid = upstream->c_connid;
339
340

#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
Ondřej Kuzník's avatar
Ondřej Kuzník committed
341
    if ( lload_features & LLOAD_FEATURE_VC ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
342
        rc = request_bind_as_vc( op );
343
344
345
    } else
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
    {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
346
347
348
        rc = request_bind( op );
    }

349
350
351
    CONNECTION_LOCK_DECREF(upstream);
    UPSTREAM_UNLOCK_OR_DESTROY(upstream);

352
    CONNECTION_LOCK_DECREF(client);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
353
    if ( rc ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
354
355
        op->o_client_refcnt--;
        operation_destroy_from_client( op );
356
357
        CLIENT_DESTROY(client);
        return -1;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
358
359
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
360
361
    if ( !--op->o_client_refcnt ) {
        operation_destroy_from_client( op );
362
363
364
365
366
367
368
369
370
371
372
373
        if ( client->c_state == SLAP_C_BINDING ) {
            client->c_state = SLAP_C_READY;
            client->c_type = SLAP_C_OPEN;
            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
374
    }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
375

376
    return rc;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
377
}