2017-03-16 20:11:45 +08:00
|
|
|
/* $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"
|
2017-12-18 18:53:39 +08:00
|
|
|
#include "lload.h"
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-09-27 18:49:39 +08:00
|
|
|
int
|
2017-12-14 00:09:26 +08:00
|
|
|
forward_response( LloadConnection *client, LloadOperation *op, BerElement *ber )
|
2017-03-29 01:40:20 +08:00
|
|
|
{
|
|
|
|
BerElement *output;
|
|
|
|
BerValue response, controls = BER_BVNULL;
|
|
|
|
ber_tag_t tag, response_tag;
|
|
|
|
ber_len_t len;
|
|
|
|
|
|
|
|
response_tag = ber_skip_element( ber, &response );
|
|
|
|
|
|
|
|
tag = ber_peek_tag( ber, &len );
|
|
|
|
if ( tag == LDAP_TAG_CONTROLS ) {
|
|
|
|
ber_skip_element( ber, &controls );
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:35:21 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "forward_response: "
|
2017-06-20 20:00:31 +08:00
|
|
|
"%s to client connid=%lu request msgid=%d\n",
|
2017-12-18 18:53:39 +08:00
|
|
|
lload_msgtype2str( response_tag ), op->o_client_connid,
|
2017-04-14 16:43:37 +08:00
|
|
|
op->o_client_msgid );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-12-14 00:09:26 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &client->c_io_mutex );
|
|
|
|
output = client->c_pendingber;
|
2017-03-29 01:40:20 +08:00
|
|
|
if ( output == NULL && (output = ber_alloc()) == NULL ) {
|
|
|
|
ber_free( ber, 1 );
|
2017-12-14 00:09:26 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &client->c_io_mutex );
|
2017-03-29 01:40:20 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2017-12-14 00:09:26 +08:00
|
|
|
client->c_pendingber = output;
|
2017-03-29 01:40:20 +08:00
|
|
|
|
|
|
|
ber_printf( output, "t{titOtO}", LDAP_TAG_MESSAGE,
|
|
|
|
LDAP_TAG_MSGID, op->o_client_msgid,
|
|
|
|
response_tag, &response,
|
|
|
|
LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &controls ) );
|
|
|
|
|
2017-12-14 00:09:26 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &client->c_io_mutex );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
|
|
|
ber_free( ber, 1 );
|
2017-12-14 00:09:26 +08:00
|
|
|
connection_write_cb( -1, 0, client );
|
2017-03-29 01:40:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:49:39 +08:00
|
|
|
int
|
2017-12-14 00:09:26 +08:00
|
|
|
forward_final_response(
|
|
|
|
LloadConnection *client,
|
|
|
|
LloadOperation *op,
|
|
|
|
BerElement *ber )
|
2017-03-29 01:40:20 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-06-27 23:35:21 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS, "forward_final_response: "
|
2017-06-20 20:00:31 +08:00
|
|
|
"connid=%lu msgid=%d finishing up with a request for "
|
|
|
|
"client connid=%lu\n",
|
|
|
|
op->o_upstream_connid, op->o_upstream_msgid, op->o_client_connid );
|
2017-12-14 00:09:26 +08:00
|
|
|
rc = forward_response( client, op, ber );
|
2017-12-14 01:39:42 +08:00
|
|
|
CONNECTION_LOCK(op->o_upstream);
|
|
|
|
if ( !op->o_pin_id || !op->o_upstream_refcnt-- ) {
|
|
|
|
operation_destroy_from_upstream( op );
|
|
|
|
}
|
|
|
|
CONNECTION_UNLOCK(op->o_upstream);
|
2017-03-29 01:40:20 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-12-18 18:53:39 +08:00
|
|
|
handle_unsolicited( LloadConnection *c, BerElement *ber )
|
2017-03-29 01:40:20 +08:00
|
|
|
{
|
2017-09-28 17:13:24 +08:00
|
|
|
if ( c->c_state == LLOAD_C_READY ) {
|
|
|
|
c->c_state = LLOAD_C_CLOSING;
|
2017-09-25 17:45:07 +08:00
|
|
|
}
|
2017-04-14 16:45:18 +08:00
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
Debug( LDAP_DEBUG_CONNS, "handle_unsolicited: "
|
2017-06-20 20:00:31 +08:00
|
|
|
"teardown for upstream connection connid=%lu\n",
|
2017-03-29 01:40:20 +08:00
|
|
|
c->c_connid );
|
|
|
|
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_DESTROY(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull c->c_currentber from the connection and try to look up the operation on
|
|
|
|
* the upstream.
|
|
|
|
*
|
|
|
|
* If it's a notice of disconnection, we won't find it and need to tear down
|
|
|
|
* the connection and tell the clients, if we can't find the operation, ignore
|
|
|
|
* the message (either client already disconnected/abandoned it or the upstream
|
|
|
|
* is pulling our leg).
|
|
|
|
*
|
|
|
|
* Some responses need special handling:
|
|
|
|
* - Bind response
|
|
|
|
* - VC response where the client requested a Bind (both need to update the
|
|
|
|
* client's bind status)
|
|
|
|
* - search entries/referrals and intermediate responses (will not trigger
|
|
|
|
* operation to be removed)
|
|
|
|
*
|
|
|
|
* If the worker pool is overloaded, we might be called directly from
|
2017-09-22 17:24:52 +08:00
|
|
|
* the read callback, at that point, the connection hasn't been muted.
|
2017-03-29 01:40:20 +08:00
|
|
|
*
|
|
|
|
* TODO: when the client already has data pending on write, we should mute the
|
|
|
|
* upstream.
|
|
|
|
* - should record the BerElement on the Op and the Op on the client
|
2017-06-14 02:33:31 +08:00
|
|
|
*
|
|
|
|
* The following hold on entering any of the handlers:
|
|
|
|
* - op->o_upstream_refcnt > 0
|
|
|
|
* - op->o_upstream->c_refcnt > 0
|
|
|
|
* - op->o_client->c_refcnt > 0
|
2017-03-29 01:40:20 +08:00
|
|
|
*/
|
|
|
|
static int
|
2017-12-18 18:53:39 +08:00
|
|
|
handle_one_response( LloadConnection *c )
|
2017-03-29 01:40:20 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadOperation *op = NULL, needle = { .o_upstream_connid = c->c_connid };
|
|
|
|
LloadOperationHandler handler = NULL;
|
2017-03-29 01:40:20 +08:00
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
2017-04-21 18:07:43 +08:00
|
|
|
int rc = LDAP_SUCCESS;
|
2017-03-29 01:40:20 +08:00
|
|
|
|
|
|
|
ber = c->c_currentber;
|
|
|
|
c->c_currentber = NULL;
|
|
|
|
|
|
|
|
tag = ber_get_int( ber, &needle.o_upstream_msgid );
|
|
|
|
if ( tag != LDAP_TAG_MSGID ) {
|
|
|
|
rc = -1;
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( needle.o_upstream_msgid == 0 ) {
|
|
|
|
return handle_unsolicited( c, ber );
|
|
|
|
} else if ( !( op = tavl_find(
|
|
|
|
c->c_ops, &needle, operation_upstream_cmp ) ) ) {
|
|
|
|
/* Already abandoned, do nothing */
|
2017-05-03 19:18:49 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
return rc;
|
2017-03-29 01:40:20 +08:00
|
|
|
/*
|
|
|
|
} else if ( op->o_response_pending ) {
|
|
|
|
c->c_pendingop = op;
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
op->o_response_pending = ber;
|
|
|
|
*/
|
|
|
|
|
|
|
|
tag = ber_peek_tag( ber, &len );
|
|
|
|
switch ( tag ) {
|
|
|
|
case LDAP_RES_SEARCH_ENTRY:
|
|
|
|
case LDAP_RES_SEARCH_REFERENCE:
|
|
|
|
case LDAP_RES_INTERMEDIATE:
|
|
|
|
handler = forward_response;
|
|
|
|
break;
|
|
|
|
case LDAP_RES_BIND:
|
|
|
|
handler = handle_bind_response;
|
|
|
|
break;
|
|
|
|
case LDAP_RES_EXTENDED:
|
2017-05-23 17:04:10 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
|
2017-03-29 01:40:20 +08:00
|
|
|
if ( op->o_tag == LDAP_REQ_BIND ) {
|
|
|
|
handler = handle_vc_bind_response;
|
|
|
|
}
|
2017-05-23 17:04:10 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
|
2017-03-29 01:40:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( !handler ) {
|
|
|
|
handler = forward_final_response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( op ) {
|
2017-11-22 20:56:53 +08:00
|
|
|
op->o_last_response = slap_get_time();
|
2017-06-27 23:35:21 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS2, "handle_one_response: "
|
2017-06-20 20:00:31 +08:00
|
|
|
"upstream connid=%lu, processing response for "
|
|
|
|
"client connid=%lu, msgid=%d\n",
|
2017-04-14 16:43:37 +08:00
|
|
|
c->c_connid, op->o_client_connid, op->o_client_msgid );
|
2017-03-29 01:40:20 +08:00
|
|
|
} else {
|
|
|
|
tag = ber_peek_tag( ber, &len );
|
2017-06-20 20:00:31 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS2, "handle_one_response: "
|
|
|
|
"upstream connid=%lu, %s, msgid=%d not for a pending "
|
|
|
|
"operation\n",
|
2017-12-18 18:53:39 +08:00
|
|
|
c->c_connid, lload_msgtype2str( tag ),
|
|
|
|
needle.o_upstream_msgid );
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( handler ) {
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *client;
|
2017-05-10 01:03:49 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_upstream_refcnt++;
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
2017-05-10 01:03:49 +08:00
|
|
|
|
2017-07-17 17:56:06 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_link_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
client = op->o_client;
|
|
|
|
if ( client ) {
|
|
|
|
CONNECTION_LOCK(client);
|
2017-07-11 20:08:21 +08:00
|
|
|
if ( client->c_live ) {
|
|
|
|
op->o_client_refcnt++;
|
|
|
|
CONNECTION_UNLOCK_INCREF(client);
|
|
|
|
} else {
|
|
|
|
CONNECTION_UNLOCK(client);
|
|
|
|
client = NULL;
|
|
|
|
}
|
2017-05-10 01:03:49 +08:00
|
|
|
}
|
2017-07-17 17:56:06 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_link_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
|
|
|
|
if ( client ) {
|
2017-12-14 00:09:26 +08:00
|
|
|
rc = handler( client, op, ber );
|
2017-05-10 01:03:49 +08:00
|
|
|
CONNECTION_LOCK_DECREF(client);
|
2017-06-23 18:09:03 +08:00
|
|
|
op->o_client_refcnt--;
|
|
|
|
if ( !op->o_client_refcnt ) {
|
|
|
|
operation_destroy_from_client( op );
|
|
|
|
}
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_UNLOCK_OR_DESTROY(client);
|
2017-05-10 01:03:49 +08:00
|
|
|
} else {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
op->o_upstream_refcnt--;
|
2017-06-23 18:09:03 +08:00
|
|
|
if ( !client || !op->o_upstream_refcnt ) {
|
2017-09-28 17:13:24 +08:00
|
|
|
if ( c->c_state == LLOAD_C_BINDING ) {
|
|
|
|
c->c_state = LLOAD_C_READY;
|
2017-07-11 20:08:53 +08:00
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
operation_destroy_from_upstream( op );
|
|
|
|
}
|
2017-05-10 01:03:49 +08:00
|
|
|
} else {
|
2017-11-22 21:15:54 +08:00
|
|
|
assert(0);
|
2017-05-10 01:03:49 +08:00
|
|
|
ber_free( ber, 1 );
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if ( rc ) {
|
2017-06-20 20:00:31 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS, "handle_one_response: "
|
|
|
|
"error on processing a response (%s) on upstream connection "
|
|
|
|
"connid=%ld, tag=%lx\n",
|
2017-12-18 18:53:39 +08:00
|
|
|
lload_msgtype2str( tag ), c->c_connid, tag );
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_DESTROY(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
2017-04-21 18:07:43 +08:00
|
|
|
/* We leave the connection locked */
|
2017-03-29 01:40:20 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
int
|
2017-12-18 18:53:39 +08:00
|
|
|
upstream_bind_cb( LloadConnection *c )
|
2017-03-17 22:32:06 +08:00
|
|
|
{
|
2017-09-25 17:45:07 +08:00
|
|
|
BerElement *ber = c->c_currentber;
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadBackend *b = c->c_private;
|
2017-04-19 16:51:47 +08:00
|
|
|
BerValue matcheddn, message;
|
2017-03-17 22:32:06 +08:00
|
|
|
ber_tag_t tag;
|
|
|
|
ber_int_t msgid, result;
|
|
|
|
|
|
|
|
c->c_currentber = NULL;
|
|
|
|
|
|
|
|
if ( ber_scanf( ber, "it", &msgid, &tag ) == LBER_ERROR ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_bind_cb: "
|
|
|
|
"protocol violation from server\n" );
|
2017-03-17 22:32:06 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( msgid != ( c->c_next_msgid - 1 ) || tag != LDAP_RES_BIND ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_bind_cb: "
|
|
|
|
"unexpected %s from server, msgid=%d\n",
|
2017-12-18 18:53:39 +08:00
|
|
|
lload_msgtype2str( tag ), msgid );
|
2017-03-17 22:32:06 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:51:47 +08:00
|
|
|
if ( ber_scanf( ber, "{emm" /* "}" */, &result, &matcheddn, &message ) ==
|
2017-03-17 22:32:06 +08:00
|
|
|
LBER_ERROR ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_bind_cb: "
|
|
|
|
"response does not conform with a bind response\n" );
|
2017-03-17 22:32:06 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( result ) {
|
2017-09-25 17:45:07 +08:00
|
|
|
case LDAP_SUCCESS: {
|
|
|
|
c->c_pdu_cb = handle_one_response;
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_READY;
|
|
|
|
c->c_type = LLOAD_C_OPEN;
|
2017-11-16 21:34:40 +08:00
|
|
|
c->c_read_timeout = NULL;
|
|
|
|
event_add( c->c_read_event, c->c_read_timeout );
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_bind_cb: "
|
|
|
|
"connid=%lu finished binding, now active\n",
|
|
|
|
c->c_connid );
|
2017-09-25 17:45:07 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_preparing, c, c_next );
|
|
|
|
b->b_active++;
|
|
|
|
b->b_opening--;
|
|
|
|
b->b_failed = 0;
|
2017-11-22 20:55:20 +08:00
|
|
|
if ( b->b_last_conn ) {
|
|
|
|
LDAP_CIRCLEQ_INSERT_AFTER(
|
|
|
|
&b->b_conns, b->b_last_conn, c, c_next );
|
|
|
|
} else {
|
|
|
|
LDAP_CIRCLEQ_INSERT_HEAD( &b->b_conns, c, c_next );
|
|
|
|
}
|
|
|
|
b->b_last_conn = c;
|
2017-09-25 17:45:07 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
backend_retry( b );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
} break;
|
2017-03-17 22:32:06 +08:00
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
case LDAP_SASL_BIND_IN_PROGRESS:
|
|
|
|
/* TODO: fallthrough until we implement SASL */
|
|
|
|
#endif /* HAVE_CYRUS_SASL */
|
|
|
|
default:
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_bind_cb: "
|
|
|
|
"upstream bind failed, rc=%d, message='%s'\n",
|
2017-04-19 16:51:47 +08:00
|
|
|
result, message.bv_val );
|
2017-03-17 22:32:06 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:51:47 +08:00
|
|
|
ber_free( ber, 1 );
|
2017-09-25 17:45:07 +08:00
|
|
|
return LDAP_SUCCESS;
|
2017-03-17 22:32:06 +08:00
|
|
|
|
2017-04-19 16:51:47 +08:00
|
|
|
fail:
|
2017-03-17 22:32:06 +08:00
|
|
|
ber_free( ber, 1 );
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_DESTROY(c);
|
2017-09-25 17:45:07 +08:00
|
|
|
return -1;
|
2017-03-16 20:11:45 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:32:06 +08:00
|
|
|
void *
|
|
|
|
upstream_bind( void *ctx, void *arg )
|
2017-03-16 20:11:45 +08:00
|
|
|
{
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *c = arg;
|
2017-09-25 17:45:07 +08:00
|
|
|
BerElement *ber;
|
2017-03-17 22:32:06 +08:00
|
|
|
ber_int_t msgid;
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-09-25 17:45:07 +08:00
|
|
|
c->c_pdu_cb = upstream_bind_cb;
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-09-25 17:45:07 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
|
|
|
|
ber = c->c_pendingber;
|
|
|
|
if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
|
|
|
CONNECTION_LOCK_DESTROY(c);
|
2017-03-17 22:32:06 +08:00
|
|
|
return NULL;
|
2017-03-16 20:11:45 +08:00
|
|
|
}
|
2017-09-25 17:45:07 +08:00
|
|
|
c->c_pendingber = ber;
|
2017-03-17 22:32:06 +08:00
|
|
|
msgid = c->c_next_msgid++;
|
|
|
|
|
2017-06-14 02:46:31 +08:00
|
|
|
if ( bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
|
2017-03-17 22:32:06 +08:00
|
|
|
/* simple bind */
|
2017-03-29 01:07:09 +08:00
|
|
|
ber_printf( ber, "{it{iOtON}}",
|
2017-03-17 22:32:06 +08:00
|
|
|
msgid, LDAP_REQ_BIND, LDAP_VERSION3,
|
2017-06-14 02:46:31 +08:00
|
|
|
&bindconf.sb_binddn, LDAP_AUTH_SIMPLE,
|
|
|
|
&bindconf.sb_cred );
|
2017-03-17 22:32:06 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CYRUS_SASL
|
|
|
|
} else {
|
2017-03-29 01:07:09 +08:00
|
|
|
BerValue cred = BER_BVNULL;
|
|
|
|
ber_printf( ber, "{it{iOt{OON}N}}",
|
|
|
|
msgid, LDAP_REQ_BIND, LDAP_VERSION3,
|
2017-06-14 02:46:31 +08:00
|
|
|
&bindconf.sb_binddn, LDAP_AUTH_SASL,
|
|
|
|
&bindconf.sb_saslmech, BER_BV_OPTIONAL( &cred ) );
|
2017-03-17 22:32:06 +08:00
|
|
|
#endif /* HAVE_CYRUS_SASL */
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
|
|
|
|
2017-09-22 17:24:52 +08:00
|
|
|
connection_write_cb( -1, 0, c );
|
2017-03-17 22:32:06 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
2017-11-16 21:34:40 +08:00
|
|
|
c->c_read_timeout = lload_timeout_net;
|
|
|
|
event_add( c->c_read_event, c->c_read_timeout );
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_UNLOCK_OR_DESTROY(c);
|
2017-04-21 18:07:43 +08:00
|
|
|
|
2017-03-17 22:32:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-25 17:45:07 +08:00
|
|
|
/*
|
|
|
|
* The backend is already locked when entering the function.
|
|
|
|
*/
|
|
|
|
static int
|
2017-12-18 18:53:39 +08:00
|
|
|
upstream_finish( LloadConnection *c )
|
2017-09-25 17:45:07 +08:00
|
|
|
{
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadBackend *b = c->c_private;
|
2017-09-25 17:45:07 +08:00
|
|
|
int is_bindconn = 0, rc = 0;
|
|
|
|
|
|
|
|
c->c_pdu_cb = handle_one_response;
|
|
|
|
|
|
|
|
/* Unless we are configured to use the VC exop, consider allocating the
|
|
|
|
* connection into the bind conn pool. Start off by allocating one for
|
|
|
|
* general use, then one for binds, then we start filling up the general
|
|
|
|
* connection pool, finally the bind pool */
|
|
|
|
if (
|
|
|
|
#ifdef LDAP_API_FEATURE_VERIFY_CREDENTIALS
|
|
|
|
!(lload_features & LLOAD_FEATURE_VC) &&
|
|
|
|
#endif /* LDAP_API_FEATURE_VERIFY_CREDENTIALS */
|
|
|
|
b->b_active && b->b_numbindconns ) {
|
|
|
|
if ( !b->b_bindavail ) {
|
|
|
|
is_bindconn = 1;
|
|
|
|
} else if ( b->b_active >= b->b_numconns &&
|
|
|
|
b->b_bindavail < b->b_numbindconns ) {
|
|
|
|
is_bindconn = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_bindconn ) {
|
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_preparing, c, c_next );
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_READY;
|
|
|
|
c->c_type = LLOAD_C_BIND;
|
2017-09-25 17:45:07 +08:00
|
|
|
b->b_bindavail++;
|
|
|
|
b->b_opening--;
|
|
|
|
b->b_failed = 0;
|
2017-11-22 20:55:20 +08:00
|
|
|
if ( b->b_last_bindconn ) {
|
|
|
|
LDAP_CIRCLEQ_INSERT_AFTER(
|
|
|
|
&b->b_bindconns, b->b_last_bindconn, c, c_next );
|
|
|
|
} else {
|
|
|
|
LDAP_CIRCLEQ_INSERT_HEAD( &b->b_bindconns, c, c_next );
|
|
|
|
}
|
|
|
|
b->b_last_bindconn = c;
|
2017-09-25 17:45:07 +08:00
|
|
|
} else if ( bindconf.sb_method == LDAP_AUTH_NONE ) {
|
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_preparing, c, c_next );
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_READY;
|
|
|
|
c->c_type = LLOAD_C_OPEN;
|
2017-09-25 17:45:07 +08:00
|
|
|
b->b_active++;
|
|
|
|
b->b_opening--;
|
|
|
|
b->b_failed = 0;
|
2017-11-22 20:55:20 +08:00
|
|
|
if ( b->b_last_conn ) {
|
|
|
|
LDAP_CIRCLEQ_INSERT_AFTER( &b->b_conns, b->b_last_conn, c, c_next );
|
|
|
|
} else {
|
|
|
|
LDAP_CIRCLEQ_INSERT_HEAD( &b->b_conns, c, c_next );
|
|
|
|
}
|
|
|
|
b->b_last_conn = c;
|
2017-09-25 17:45:07 +08:00
|
|
|
} else {
|
|
|
|
rc = 1;
|
|
|
|
ldap_pvt_thread_pool_submit( &connection_pool, upstream_bind, c );
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_finish: "
|
|
|
|
"%sconnection connid=%lu is%s ready for use\n",
|
|
|
|
is_bindconn ? "bind " : "", c->c_connid, rc ? " almost" : "" );
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-09-25 18:15:59 +08:00
|
|
|
static void
|
|
|
|
upstream_tls_handshake_cb( evutil_socket_t s, short what, void *arg )
|
|
|
|
{
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *c = arg;
|
|
|
|
LloadBackend *b;
|
2017-09-25 18:15:59 +08:00
|
|
|
int rc = LDAP_SUCCESS;
|
|
|
|
|
|
|
|
CONNECTION_LOCK(c);
|
|
|
|
if ( what & EV_TIMEOUT ) {
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_tls_handshake_cb: "
|
|
|
|
"connid=%lu, timeout reached, destroying\n",
|
|
|
|
c->c_connid );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
b = c->c_private;
|
|
|
|
|
2017-12-18 18:53:39 +08:00
|
|
|
rc = ldap_pvt_tls_connect( lload_tls_backend_ld, c->c_sb, b->b_host );
|
2017-09-25 18:15:59 +08:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc == 0 ) {
|
|
|
|
struct event_base *base = event_get_base( c->c_read_event );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're finished, replace the callbacks
|
|
|
|
*
|
|
|
|
* This is deadlock-safe, since both share the same base - the one
|
|
|
|
* that's just running us.
|
|
|
|
*/
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
event_del( c->c_write_event );
|
|
|
|
|
2017-11-16 21:34:40 +08:00
|
|
|
c->c_read_timeout = NULL;
|
2017-09-25 18:15:59 +08:00
|
|
|
event_assign( c->c_read_event, base, c->c_fd, EV_READ|EV_PERSIST,
|
|
|
|
connection_read_cb, c );
|
2017-11-16 21:34:40 +08:00
|
|
|
event_add( c->c_read_event, c->c_read_timeout );
|
2017-09-25 18:15:59 +08:00
|
|
|
|
|
|
|
event_assign( c->c_write_event, base, c->c_fd, EV_WRITE,
|
|
|
|
connection_write_cb, c );
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_tls_handshake_cb: "
|
|
|
|
"connid=%lu finished\n",
|
|
|
|
c->c_connid );
|
|
|
|
c->c_is_tls = LLOAD_TLS_ESTABLISHED;
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
|
|
|
|
rc = upstream_finish( c );
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
|
|
|
|
if ( rc == LDAP_SUCCESS ) {
|
|
|
|
backend_retry( b );
|
|
|
|
}
|
|
|
|
} else if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
|
|
|
|
event_add( c->c_write_event, lload_write_timeout );
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_tls_handshake_cb: "
|
|
|
|
"connid=%lu need write rc=%d\n",
|
|
|
|
c->c_connid, rc );
|
|
|
|
}
|
|
|
|
CONNECTION_UNLOCK_OR_DESTROY(c);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_tls_handshake_cb: "
|
|
|
|
"connid=%lu failed rc=%d\n",
|
|
|
|
c->c_connid, rc );
|
|
|
|
CONNECTION_DESTROY(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-12-18 18:53:39 +08:00
|
|
|
upstream_starttls( LloadConnection *c )
|
2017-09-25 18:15:59 +08:00
|
|
|
{
|
|
|
|
BerValue matcheddn, message, responseOid,
|
|
|
|
startTLSOid = BER_BVC(LDAP_EXOP_START_TLS);
|
|
|
|
BerElement *ber = c->c_currentber;
|
|
|
|
struct event_base *base;
|
|
|
|
ber_int_t msgid, result;
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
|
|
|
c->c_currentber = NULL;
|
|
|
|
|
|
|
|
if ( ber_scanf( ber, "it", &msgid, &tag ) == LBER_ERROR ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_starttls: "
|
|
|
|
"protocol violation from server\n" );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( msgid != ( c->c_next_msgid - 1 ) || tag != LDAP_RES_EXTENDED ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_starttls: "
|
|
|
|
"unexpected %s from server, msgid=%d\n",
|
2017-12-18 18:53:39 +08:00
|
|
|
lload_msgtype2str( tag ), msgid );
|
2017-09-25 18:15:59 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ber_scanf( ber, "{emm}", &result, &matcheddn, &message ) ==
|
|
|
|
LBER_ERROR ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_starttls: "
|
|
|
|
"protocol violation on StartTLS response\n" );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (tag = ber_get_tag( ber )) != LBER_DEFAULT ) {
|
|
|
|
if ( tag != LDAP_TAG_EXOP_RES_OID ||
|
|
|
|
ber_scanf( ber, "{m}", &responseOid ) == LBER_DEFAULT ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_starttls: "
|
|
|
|
"protocol violation on StartTLS response\n" );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ber_bvcmp( &responseOid, &startTLSOid ) ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_starttls: "
|
|
|
|
"oid=%s not a StartTLS response\n",
|
|
|
|
responseOid.bv_val );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( result != LDAP_SUCCESS ) {
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadBackend *b = c->c_private;
|
2017-09-25 18:15:59 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_STATS, "upstream_starttls: "
|
|
|
|
"server doesn't support StartTLS rc=%d message='%s'%s\n",
|
|
|
|
result, message.bv_val,
|
|
|
|
(c->c_is_tls == LLOAD_STARTTLS_OPTIONAL) ? ", ignored" : "" );
|
|
|
|
if ( c->c_is_tls != LLOAD_STARTTLS_OPTIONAL ) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
c->c_is_tls = LLOAD_CLEARTEXT;
|
|
|
|
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
|
|
|
|
rc = upstream_finish( c );
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
|
|
|
|
if ( rc == LDAP_SUCCESS ) {
|
|
|
|
backend_retry( b );
|
|
|
|
}
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK_OR_DESTROY(c);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = event_get_base( c->c_read_event );
|
|
|
|
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
event_del( c->c_write_event );
|
|
|
|
|
2017-11-16 21:34:40 +08:00
|
|
|
c->c_read_timeout = lload_timeout_net;
|
2017-09-25 18:15:59 +08:00
|
|
|
event_assign( c->c_read_event, base, c->c_fd, EV_READ|EV_PERSIST,
|
|
|
|
upstream_tls_handshake_cb, c );
|
|
|
|
event_assign( c->c_write_event, base, c->c_fd, EV_WRITE,
|
|
|
|
upstream_tls_handshake_cb, c );
|
|
|
|
|
2017-11-16 21:34:40 +08:00
|
|
|
event_add( c->c_read_event, c->c_read_timeout );
|
2017-09-25 18:15:59 +08:00
|
|
|
event_add( c->c_write_event, lload_write_timeout );
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
CONNECTION_DESTROY(c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:15:46 +08:00
|
|
|
/*
|
|
|
|
* We must already hold b->b_mutex when called.
|
|
|
|
*/
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *
|
|
|
|
upstream_init( ber_socket_t s, LloadBackend *b )
|
2017-03-17 22:32:06 +08:00
|
|
|
{
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *c;
|
|
|
|
struct event_base *base = lload_get_base( s );
|
2017-03-17 22:32:06 +08:00
|
|
|
struct event *event;
|
2017-09-25 17:45:07 +08:00
|
|
|
int flags, rc = -1;
|
2017-03-17 22:32:06 +08:00
|
|
|
|
|
|
|
assert( b != NULL );
|
|
|
|
|
2017-09-25 18:15:59 +08:00
|
|
|
flags = (b->b_proto == LDAP_PROTO_IPC) ? CONN_IS_IPC : 0;
|
2017-12-18 18:53:39 +08:00
|
|
|
if ( (c = lload_connection_init( s, b->b_host, flags )) == NULL ) {
|
2017-09-22 17:24:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:32:06 +08:00
|
|
|
c->c_private = b;
|
2017-09-22 17:24:52 +08:00
|
|
|
c->c_is_tls = b->b_tls;
|
|
|
|
c->c_pdu_cb = handle_one_response;
|
2017-03-17 22:32:06 +08:00
|
|
|
|
2017-09-25 17:45:07 +08:00
|
|
|
LDAP_CIRCLEQ_INSERT_HEAD( &b->b_preparing, c, c_next );
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_type = LLOAD_C_PREPARING;
|
2017-09-25 17:45:07 +08:00
|
|
|
|
2017-06-14 02:32:35 +08:00
|
|
|
{
|
|
|
|
ber_len_t max = sockbuf_max_incoming_upstream;
|
|
|
|
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:24:52 +08:00
|
|
|
event = event_new( base, s, EV_READ|EV_PERSIST, connection_read_cb, c );
|
|
|
|
if ( !event ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_init: "
|
|
|
|
"Read event could not be allocated\n" );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
c->c_read_event = event;
|
|
|
|
|
|
|
|
event = event_new( base, s, EV_WRITE, connection_write_cb, c );
|
2017-03-16 20:11:45 +08:00
|
|
|
if ( !event ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "upstream_init: "
|
|
|
|
"Write event could not be allocated\n" );
|
2017-03-16 20:11:45 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
2017-09-22 17:24:52 +08:00
|
|
|
/* We only add the write event when we have data pending */
|
2017-03-16 20:11:45 +08:00
|
|
|
c->c_write_event = event;
|
|
|
|
|
2017-09-25 18:15:59 +08:00
|
|
|
if ( c->c_is_tls == LLOAD_CLEARTEXT ) {
|
|
|
|
rc = upstream_finish( c );
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else if ( c->c_is_tls == LLOAD_LDAPS ) {
|
|
|
|
event_assign( c->c_read_event, base, s, EV_READ|EV_PERSIST,
|
|
|
|
upstream_tls_handshake_cb, c );
|
|
|
|
event_assign( c->c_write_event, base, s, EV_WRITE,
|
|
|
|
upstream_tls_handshake_cb, c );
|
|
|
|
event_add( c->c_write_event, lload_write_timeout );
|
|
|
|
} else if ( c->c_is_tls == LLOAD_STARTTLS ||
|
|
|
|
c->c_is_tls == LLOAD_STARTTLS_OPTIONAL ) {
|
|
|
|
BerElement *output;
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
|
|
|
|
if ( (output = c->c_pendingber = ber_alloc()) == NULL ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ber_printf( output, "t{tit{ts}}", LDAP_TAG_MESSAGE,
|
|
|
|
LDAP_TAG_MSGID, c->c_next_msgid++,
|
|
|
|
LDAP_REQ_EXTENDED,
|
|
|
|
LDAP_TAG_EXOP_REQ_OID, LDAP_EXOP_START_TLS );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
2017-04-11 21:15:46 +08:00
|
|
|
|
2017-09-25 18:15:59 +08:00
|
|
|
c->c_pdu_cb = upstream_starttls;
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
connection_write_cb( s, 0, c );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
}
|
2017-11-16 21:34:40 +08:00
|
|
|
event_add( c->c_read_event, c->c_read_timeout );
|
2017-03-17 22:29:38 +08:00
|
|
|
|
2017-09-25 17:45:07 +08:00
|
|
|
c->c_destroy = upstream_destroy;
|
|
|
|
CONNECTION_UNLOCK_OR_DESTROY(c);
|
|
|
|
|
|
|
|
/* has upstream_finish() finished? */
|
|
|
|
if ( rc == LDAP_SUCCESS ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
backend_retry( b );
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
2017-04-11 21:15:46 +08:00
|
|
|
}
|
2017-03-16 20:11:45 +08:00
|
|
|
|
|
|
|
return c;
|
2017-04-11 21:15:46 +08:00
|
|
|
|
2017-03-16 20:11:45 +08:00
|
|
|
fail:
|
|
|
|
if ( c->c_write_event ) {
|
|
|
|
event_del( c->c_write_event );
|
|
|
|
event_free( c->c_write_event );
|
|
|
|
}
|
|
|
|
if ( c->c_read_event ) {
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
event_free( c->c_read_event );
|
|
|
|
}
|
2017-09-22 17:24:52 +08:00
|
|
|
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_INVALID;
|
2017-09-22 17:24:52 +08:00
|
|
|
CONNECTION_DESTROY(c);
|
|
|
|
assert( c == NULL );
|
|
|
|
|
2017-03-16 20:11:45 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
void
|
2017-12-18 18:53:39 +08:00
|
|
|
upstream_destroy( LloadConnection *c )
|
2017-03-16 20:11:45 +08:00
|
|
|
{
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadBackend *b = c->c_private;
|
2017-04-21 18:07:43 +08:00
|
|
|
struct event *read_event, *write_event;
|
2017-06-14 02:40:50 +08:00
|
|
|
TAvlnode *root;
|
2017-06-15 01:24:53 +08:00
|
|
|
long freed, executing;
|
2017-09-22 17:24:52 +08:00
|
|
|
enum sc_state state;
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-04-12 22:57:21 +08:00
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_destroy: "
|
2017-06-20 20:00:31 +08:00
|
|
|
"freeing connection connid=%lu\n",
|
2017-04-11 21:15:46 +08:00
|
|
|
c->c_connid );
|
|
|
|
|
2017-09-28 17:13:24 +08:00
|
|
|
assert( c->c_state != LLOAD_C_INVALID );
|
2017-09-22 17:24:52 +08:00
|
|
|
state = c->c_state;
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_INVALID;
|
2017-04-21 18:07:43 +08:00
|
|
|
|
2017-06-14 02:40:50 +08:00
|
|
|
root = c->c_ops;
|
|
|
|
c->c_ops = NULL;
|
2017-06-15 01:24:53 +08:00
|
|
|
executing = c->c_n_ops_executing;
|
2017-06-23 17:30:38 +08:00
|
|
|
c->c_n_ops_executing = 0;
|
2017-06-14 02:40:50 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
read_event = c->c_read_event;
|
|
|
|
write_event = c->c_write_event;
|
2017-06-23 17:30:38 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
|
2017-06-14 02:40:50 +08:00
|
|
|
freed = tavl_free( root, (AVL_FREE)operation_lost_upstream );
|
2017-06-15 01:24:53 +08:00
|
|
|
assert( freed == executing );
|
2017-06-14 02:40:50 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
/*
|
|
|
|
* Avoid a deadlock:
|
|
|
|
* event_del will block if the event is currently executing its callback,
|
|
|
|
* that callback might be waiting to lock c->c_mutex
|
|
|
|
*/
|
2017-05-10 01:02:28 +08:00
|
|
|
if ( read_event ) {
|
|
|
|
event_del( read_event );
|
|
|
|
}
|
2017-04-21 18:07:43 +08:00
|
|
|
|
2017-05-10 01:02:28 +08:00
|
|
|
if ( write_event ) {
|
|
|
|
event_del( write_event );
|
|
|
|
}
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-09-22 17:24:52 +08:00
|
|
|
/* Remove from the backend on first pass */
|
2017-09-28 17:13:24 +08:00
|
|
|
if ( state != LLOAD_C_CLOSING ) {
|
2017-09-22 17:24:52 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
2017-09-28 17:13:24 +08:00
|
|
|
if ( c->c_type == LLOAD_C_PREPARING ) {
|
2017-09-25 17:45:07 +08:00
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_preparing, c, c_next );
|
|
|
|
b->b_opening--;
|
|
|
|
b->b_failed++;
|
2017-09-28 17:13:24 +08:00
|
|
|
} else if ( c->c_type == LLOAD_C_BIND ) {
|
2017-11-22 20:55:20 +08:00
|
|
|
if ( c == b->b_last_bindconn ) {
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *prev =
|
2017-11-22 20:55:20 +08:00
|
|
|
LDAP_CIRCLEQ_LOOP_PREV( &b->b_bindconns, c, c_next );
|
|
|
|
if ( prev == c ) {
|
|
|
|
b->b_last_bindconn = NULL;
|
|
|
|
} else {
|
|
|
|
b->b_last_bindconn = prev;
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 17:24:52 +08:00
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_bindconns, c, c_next );
|
|
|
|
b->b_bindavail--;
|
|
|
|
} else {
|
2017-11-22 20:55:20 +08:00
|
|
|
if ( c == b->b_last_conn ) {
|
2017-12-18 18:53:39 +08:00
|
|
|
LloadConnection *prev =
|
2017-11-22 20:55:20 +08:00
|
|
|
LDAP_CIRCLEQ_LOOP_PREV( &b->b_conns, c, c_next );
|
|
|
|
if ( prev == c ) {
|
|
|
|
b->b_last_conn = NULL;
|
|
|
|
} else {
|
|
|
|
b->b_last_conn = prev;
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 17:24:52 +08:00
|
|
|
LDAP_CIRCLEQ_REMOVE( &b->b_conns, c, c_next );
|
|
|
|
b->b_active--;
|
|
|
|
}
|
|
|
|
b->b_n_ops_executing -= executing;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
backend_retry( b );
|
2017-03-16 20:11:45 +08:00
|
|
|
}
|
2017-03-16 20:14:22 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
2017-03-16 20:11:45 +08:00
|
|
|
|
2017-05-10 01:02:28 +08:00
|
|
|
if ( c->c_read_event ) {
|
|
|
|
event_free( c->c_read_event );
|
|
|
|
c->c_read_event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( c->c_write_event ) {
|
|
|
|
event_free( c->c_write_event );
|
|
|
|
c->c_write_event = NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/*
|
|
|
|
* If we attempted to destroy any operations, we might have lent a new
|
|
|
|
* refcnt token for a thread that raced us to that, let them call us again
|
|
|
|
* later
|
|
|
|
*/
|
|
|
|
assert( c->c_refcnt >= 0 );
|
|
|
|
if ( c->c_refcnt ) {
|
2017-09-28 17:13:24 +08:00
|
|
|
c->c_state = LLOAD_C_CLOSING;
|
2017-05-03 18:31:40 +08:00
|
|
|
Debug( LDAP_DEBUG_CONNS, "upstream_destroy: "
|
|
|
|
"connid=%lu aborting with refcnt=%d\n",
|
|
|
|
c->c_connid, c->c_refcnt );
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 20:11:45 +08:00
|
|
|
connection_destroy( c );
|
|
|
|
}
|