openldap/servers/lloadd/client.c

544 lines
15 KiB
C
Raw Normal View History

2017-03-08 17:59:57 -05: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"
#include "lload.h"
2017-03-08 17:59:57 -05:00
lload_c_head clients = LDAP_CIRCLEQ_HEAD_INITIALIZER( clients );
ldap_pvt_thread_mutex_t clients_mutex;
static void client_unlink( LloadConnection *upstream );
int
request_abandon( LloadConnection *c, LloadOperation *op )
{
LloadOperation *request, needle = { .o_client_connid = c->c_connid };
int rc = LDAP_SUCCESS;
op->o_res = LLOAD_OP_COMPLETED;
if ( ber_decode_int( &op->o_request, &needle.o_client_msgid ) ) {
Debug( LDAP_DEBUG_STATS, "request_abandon: "
"connid=%lu msgid=%d invalid integer sent in abandon request\n",
c->c_connid, op->o_client_msgid );
operation_unlink( op );
CONNECTION_LOCK_DESTROY(c);
return -1;
}
CONNECTION_LOCK(c);
request = tavl_find( c->c_ops, &needle, operation_client_cmp );
if ( !request ) {
Debug( LDAP_DEBUG_STATS, "request_abandon: "
"connid=%lu msgid=%d requests abandon of an operation "
"msgid=%d not being processed anymore\n",
c->c_connid, op->o_client_msgid, needle.o_client_msgid );
CONNECTION_UNLOCK(c);
goto done;
} else if ( request->o_tag == LDAP_REQ_BIND ) {
/* RFC 4511 states we must not allow Abandon on Binds */
Debug( LDAP_DEBUG_STATS, "request_abandon: "
"connid=%lu msgid=%d requests abandon of a bind operation "
"msgid=%d\n",
c->c_connid, op->o_client_msgid, needle.o_client_msgid );
CONNECTION_UNLOCK(c);
goto done;
}
Debug( LDAP_DEBUG_STATS, "request_abandon: "
"connid=%lu msgid=%d abandoning %s msgid=%d\n",
c->c_connid, op->o_client_msgid,
lload_msgtype2str( request->o_tag ), needle.o_client_msgid );
if ( c->c_state == LLOAD_C_BINDING ) {
assert(0);
}
CONNECTION_UNLOCK(c);
operation_abandon( request );
done:
operation_unlink( op );
return rc;
}
int
request_process( LloadConnection *client, LloadOperation *op )
{
BerElement *output;
LloadConnection *upstream;
ber_int_t msgid;
int res, rc = LDAP_SUCCESS;
upstream = backend_select( op, &res );
if ( !upstream ) {
Debug( LDAP_DEBUG_STATS, "request_process: "
"connid=%lu, msgid=%d no available connection found\n",
op->o_client_connid, op->o_client_msgid );
operation_send_reject( op, res, "no connections available", 1 );
goto fail;
}
op->o_upstream = upstream;
op->o_upstream_connid = upstream->c_connid;
op->o_res = LLOAD_OP_FAILED;
output = upstream->c_pendingber;
if ( output == NULL && (output = ber_alloc()) == NULL ) {
LloadBackend *b = upstream->c_private;
upstream->c_n_ops_executing--;
CONNECTION_UNLOCK(upstream);
ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
ldap_pvt_thread_mutex_lock( &b->b_mutex );
b->b_n_ops_executing--;
operation_update_backend_counters( op, b );
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
Debug( LDAP_DEBUG_ANY, "request_process: "
"ber_alloc failed\n" );
rc = -1;
goto fail;
}
upstream->c_pendingber = output;
op->o_upstream_msgid = msgid = upstream->c_next_msgid++;
rc = tavl_insert(
&upstream->c_ops, op, operation_upstream_cmp, avl_dup_error );
CONNECTION_UNLOCK(upstream);
Debug( LDAP_DEBUG_TRACE, "request_process: "
"client connid=%lu added %s msgid=%d to upstream connid=%lu as "
"msgid=%d\n",
op->o_client_connid, lload_msgtype2str( op->o_tag ),
op->o_client_msgid, op->o_upstream_connid, op->o_upstream_msgid );
assert( rc == LDAP_SUCCESS );
lload_stats.counters[LLOAD_STATS_OPS_OTHER].lc_ops_forwarded++;
if ( (lload_features & LLOAD_FEATURE_PROXYAUTHZ) &&
client->c_type != LLOAD_C_PRIVILEGED ) {
CONNECTION_LOCK(client);
Debug( LDAP_DEBUG_TRACE, "request_process: "
"proxying identity %s to upstream\n",
client->c_auth.bv_val );
ber_printf( output, "t{titOt{{sbO}" /* "}}" */, LDAP_TAG_MESSAGE,
LDAP_TAG_MSGID, msgid,
op->o_tag, &op->o_request,
LDAP_TAG_CONTROLS,
LDAP_CONTROL_PROXY_AUTHZ, 1, &client->c_auth );
CONNECTION_UNLOCK(client);
if ( !BER_BVISNULL( &op->o_ctrls ) ) {
ber_write( output, op->o_ctrls.bv_val, op->o_ctrls.bv_len, 0 );
}
ber_printf( output, /* "{{" */ "}}" );
} else {
ber_printf( output, "t{titOtO}", LDAP_TAG_MESSAGE,
LDAP_TAG_MSGID, msgid,
op->o_tag, &op->o_request,
LDAP_TAG_CONTROLS, BER_BV_OPTIONAL( &op->o_ctrls ) );
}
ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
connection_write_cb( -1, 0, upstream );
return rc;
fail:
if ( upstream ) {
CONNECTION_LOCK_DESTROY(upstream);
operation_send_reject( op, LDAP_OTHER, "internal error", 0 );
}
operation_unlink( op );
if ( rc ) {
CONNECTION_LOCK_DESTROY(client);
}
return rc;
}
int
handle_one_request( LloadConnection *c )
{
BerElement *ber;
LloadOperation *op = NULL;
RequestHandler handler = NULL;
ber = c->c_currentber;
2017-03-16 08:25:01 -04:00
c->c_currentber = NULL;
CONNECTION_LOCK(c);
2017-03-16 08:25:01 -04:00
op = operation_init( c, ber );
if ( !op ) {
Debug( LDAP_DEBUG_ANY, "handle_one_request: "
2017-07-03 11:37:35 -04:00
"connid=%lu, operation_init failed\n",
c->c_connid );
2017-09-22 05:24:52 -04:00
CONNECTION_DESTROY(c);
ber_free( ber, 1 );
return -1;
2017-03-16 08:25:01 -04:00
}
CONNECTION_UNLOCK(c);
2017-03-16 08:25:01 -04:00
2017-03-28 13:40:20 -04:00
switch ( op->o_tag ) {
case LDAP_REQ_UNBIND:
/* There is never a response for this operation */
op->o_res = LLOAD_OP_COMPLETED;
operation_unlink( op );
2017-05-18 11:48:12 -04:00
Debug( LDAP_DEBUG_STATS, "handle_one_request: "
"received unbind, closing client connid=%lu\n",
c->c_connid );
CONNECTION_LOCK_DESTROY(c);
return -1;
2017-03-28 13:40:20 -04:00
case LDAP_REQ_BIND:
2017-09-27 06:01:17 -04:00
handler = request_bind;
break;
case LDAP_REQ_ABANDON:
2017-12-13 07:17:23 -05:00
/* We can't send a response to abandon requests even if a bind is
* currently in progress */
return request_abandon( c, op );
case LDAP_REQ_EXTENDED:
handler = request_extended;
break;
2017-03-28 13:40:20 -04:00
default:
2017-09-28 05:13:24 -04:00
if ( c->c_state == LLOAD_C_BINDING ) {
operation_send_reject(
2017-04-14 04:41:57 -04:00
op, LDAP_PROTOCOL_ERROR, "bind in progress", 0 );
return LDAP_SUCCESS;
2017-04-14 04:41:57 -04:00
}
handler = request_process;
2017-03-28 13:40:20 -04:00
break;
}
if ( c->c_state == LLOAD_C_CLOSING ) {
operation_send_reject(
op, LDAP_UNAVAILABLE, "connection is shutting down", 0 );
return LDAP_SUCCESS;
}
return handler( c, op );
2017-03-08 17:59:57 -05:00
}
2017-09-25 06:17:04 -04:00
/*
* The connection has a token assigned to it when the callback is set up.
*/
void
client_tls_handshake_cb( evutil_socket_t s, short what, void *arg )
{
LloadConnection *c = arg;
epoch_t epoch;
2017-09-25 06:17:04 -04:00
int rc = 0;
if ( what & EV_TIMEOUT ) {
Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
"connid=%lu, timeout reached, destroying\n",
c->c_connid );
goto fail;
}
/*
* In case of StartTLS, make sure we flush the response first.
* Also before we try to read anything from the connection, it isn't
* permitted to Abandon a StartTLS exop per RFC4511 anyway.
*/
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
if ( c->c_pendingber ) {
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
connection_write_cb( s, what, arg );
CONNECTION_LOCK(c);
2017-09-25 06:17:04 -04:00
if ( !c->c_live ) {
CONNECTION_UNLOCK(c);
2017-09-25 06:17:04 -04:00
goto fail;
}
CONNECTION_UNLOCK(c);
2017-09-25 06:17:04 -04:00
/* Do we still have data pending? If so, connection_write_cb would
* already have arranged the write callback to trigger again */
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
2017-09-25 06:17:04 -04:00
if ( c->c_pendingber ) {
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
return;
}
}
rc = ldap_pvt_tls_accept( c->c_sb, LLOAD_TLS_CTX );
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
2017-09-25 06:17:04 -04: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.
*/
CONNECTION_LOCK(c);
2017-09-25 06:17:04 -04:00
event_del( c->c_read_event );
event_del( c->c_write_event );
c->c_read_timeout = NULL;
2017-09-25 06:17:04 -04:00
event_assign( c->c_read_event, base, c->c_fd, EV_READ|EV_PERSIST,
connection_read_cb, c );
if ( c->c_live ) {
event_add( c->c_read_event, c->c_read_timeout );
}
2017-09-25 06:17:04 -04:00
event_assign( c->c_write_event, base, c->c_fd, EV_WRITE,
connection_write_cb, c );
Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
"connid=%lu finished\n",
c->c_connid );
c->c_is_tls = LLOAD_TLS_ESTABLISHED;
CONNECTION_UNLOCK(c);
2017-09-25 06:17:04 -04:00
return;
} else if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
CONNECTION_LOCK(c);
if ( c->c_live ) {
event_add( c->c_write_event, lload_write_timeout );
}
CONNECTION_UNLOCK(c);
2017-09-25 06:17:04 -04:00
Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
"connid=%lu need write rc=%d\n",
c->c_connid, rc );
}
return;
fail:
Debug( LDAP_DEBUG_CONNS, "client_tls_handshake_cb: "
"connid=%lu failed rc=%d\n",
c->c_connid, rc );
assert( c->c_ops == NULL );
epoch = epoch_join();
CONNECTION_LOCK_DESTROY(c);
epoch_leave( epoch );
2017-09-25 06:17:04 -04:00
}
LloadConnection *
2017-03-08 17:59:57 -05:00
client_init(
ber_socket_t s,
LloadListener *listener,
2017-03-08 17:59:57 -05:00
const char *peername,
struct event_base *base,
int flags )
{
LloadConnection *c;
2017-03-08 17:59:57 -05:00
struct event *event;
2017-09-22 05:24:52 -04:00
event_callback_fn read_cb = connection_read_cb,
write_cb = connection_write_cb;
2017-03-08 17:59:57 -05:00
assert( listener != NULL );
if ( (c = lload_connection_init( s, peername, flags) ) == NULL ) {
2017-09-22 05:24:52 -04:00
return NULL;
}
2017-03-08 17:59:57 -05:00
2017-06-13 14:32:35 -04:00
{
ber_len_t max = sockbuf_max_incoming_client;
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
}
2017-09-28 05:13:24 -04:00
c->c_state = LLOAD_C_READY;
2017-09-25 06:17:04 -04:00
if ( flags & CONN_IS_TLS ) {
int rc;
c->c_is_tls = LLOAD_LDAPS;
rc = ldap_pvt_tls_accept( c->c_sb, LLOAD_TLS_CTX );
2017-09-25 06:17:04 -04:00
if ( rc < 0 ) {
Debug( LDAP_DEBUG_CONNS, "client_init: "
"connid=%lu failed initial TLS accept rc=%d\n",
c->c_connid, rc );
CONNECTION_LOCK(c);
2017-09-25 06:17:04 -04:00
goto fail;
}
if ( rc ) {
c->c_read_timeout = lload_timeout_net;
2017-09-25 06:17:04 -04:00
read_cb = write_cb = client_tls_handshake_cb;
}
}
2017-09-22 05:24:52 -04:00
event = event_new( base, s, EV_READ|EV_PERSIST, read_cb, c );
2017-03-08 17:59:57 -05:00
if ( !event ) {
2017-03-28 13:12:27 -04:00
Debug( LDAP_DEBUG_ANY, "client_init: "
"Read event could not be allocated\n" );
CONNECTION_LOCK(c);
2017-03-08 17:59:57 -05:00
goto fail;
}
c->c_read_event = event;
2017-09-22 05:24:52 -04:00
event = event_new( base, s, EV_WRITE, write_cb, c );
2017-03-08 17:59:57 -05:00
if ( !event ) {
2017-03-28 13:12:27 -04:00
Debug( LDAP_DEBUG_ANY, "client_init: "
"Write event could not be allocated\n" );
CONNECTION_LOCK(c);
2017-03-08 17:59:57 -05:00
goto fail;
}
c->c_write_event = event;
c->c_private = listener;
2017-09-22 05:24:52 -04:00
c->c_destroy = client_destroy;
c->c_unlink = client_unlink;
2017-09-22 05:24:52 -04:00
c->c_pdu_cb = handle_one_request;
CONNECTION_LOCK(c);
/* We only register the write event when we have data pending */
event_add( c->c_read_event, c->c_read_timeout );
ldap_pvt_thread_mutex_lock( &clients_mutex );
LDAP_CIRCLEQ_INSERT_TAIL( &clients, c, c_next );
ldap_pvt_thread_mutex_unlock( &clients_mutex );
2017-04-21 06:07:43 -04:00
CONNECTION_UNLOCK(c);
2017-03-08 17:59:57 -05:00
return c;
fail:
if ( c->c_write_event ) {
event_free( c->c_write_event );
2017-05-09 13:02:28 -04:00
c->c_write_event = NULL;
2017-03-08 17:59:57 -05:00
}
if ( c->c_read_event ) {
event_free( c->c_read_event );
2017-05-09 13:02:28 -04:00
c->c_read_event = NULL;
2017-03-08 17:59:57 -05:00
}
2017-09-22 05:24:52 -04:00
2017-09-28 05:13:24 -04:00
c->c_state = LLOAD_C_INVALID;
c->c_live--;
c->c_refcnt--;
connection_destroy( c );
2017-03-08 17:59:57 -05:00
return NULL;
}
void
client_reset( LloadConnection *c )
{
TAvlnode *root;
root = c->c_ops;
c->c_ops = NULL;
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 );
}
CONNECTION_UNLOCK(c);
if ( root ) {
int freed;
freed = tavl_free( root, (AVL_FREE)operation_abandon );
Debug( LDAP_DEBUG_TRACE, "client_reset: "
"dropped %d operations\n",
freed );
}
CONNECTION_LOCK(c);
}
2017-03-28 13:40:20 -04:00
void
client_unlink( LloadConnection *c )
2017-03-08 17:59:57 -05:00
{
enum sc_state state;
2017-07-03 05:41:37 -04:00
struct event *read_event, *write_event;
Debug( LDAP_DEBUG_CONNS, "client_unlink: "
"removing client connid=%lu\n",
c->c_connid );
2017-09-28 05:13:24 -04:00
assert( c->c_state != LLOAD_C_INVALID );
assert( c->c_state != LLOAD_C_DYING );
state = c->c_state;
c->c_state = LLOAD_C_DYING;
2017-07-03 05:41:37 -04:00
read_event = c->c_read_event;
write_event = c->c_write_event;
CONNECTION_UNLOCK(c);
2017-07-03 05:41:37 -04:00
if ( read_event ) {
event_del( read_event );
2017-05-03 06:31:40 -04:00
}
2017-07-03 05:41:37 -04:00
if ( write_event ) {
event_del( write_event );
}
if ( state != LLOAD_C_DYING ) {
ldap_pvt_thread_mutex_lock( &clients_mutex );
LDAP_CIRCLEQ_REMOVE( &clients, c, c_next );
ldap_pvt_thread_mutex_unlock( &clients_mutex );
}
CONNECTION_LOCK(c);
client_reset( c );
}
void
client_destroy( LloadConnection *c )
{
Debug( LDAP_DEBUG_CONNS, "client_destroy: "
"destroying client connid=%lu\n",
c->c_connid );
CONNECTION_LOCK(c);
assert( c->c_state == LLOAD_C_DYING );
c->c_state = LLOAD_C_INVALID;
2017-07-03 05:41:37 -04: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;
}
assert( c->c_refcnt == 0 );
2017-03-08 17:59:57 -05:00
connection_destroy( c );
}
void
clients_destroy( int gentle )
{
ldap_pvt_thread_mutex_lock( &clients_mutex );
connections_walk(
&clients_mutex, &clients, lload_connection_close, &gentle );
ldap_pvt_thread_mutex_unlock( &clients_mutex );
}