2017-03-09 06:59:57 +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"
|
|
|
|
#include "slap.h"
|
|
|
|
|
2017-05-25 22:04:08 +08:00
|
|
|
slap_c_head clients = LDAP_CIRCLEQ_HEAD_INITIALIZER( clients );
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_t clients_mutex;
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
typedef int (*RequestHandler)( Connection *c, Operation *op );
|
|
|
|
|
2017-03-09 06:59:57 +08:00
|
|
|
static void
|
|
|
|
client_read_cb( evutil_socket_t s, short what, void *arg )
|
|
|
|
{
|
|
|
|
Connection *c = arg;
|
2017-03-16 20:25:01 +08:00
|
|
|
BerElement *ber;
|
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
2017-03-16 19:41:07 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-05-09 23:24:14 +08:00
|
|
|
if ( !c->c_live ) {
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 18:07:43 +08:00
|
|
|
|
2017-03-09 06:59:57 +08:00
|
|
|
Debug( LDAP_DEBUG_CONNS, "client_read_cb: "
|
|
|
|
"connection %lu ready to read\n",
|
|
|
|
c->c_connid );
|
2017-03-16 20:25:01 +08:00
|
|
|
|
|
|
|
ber = c->c_currentber;
|
|
|
|
if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "client_read_cb: "
|
|
|
|
"ber_alloc failed\n" );
|
2017-04-21 18:07:43 +08:00
|
|
|
CLIENT_DESTROY(c);
|
2017-05-03 17:14:19 +08:00
|
|
|
return;
|
2017-03-16 20:25:01 +08:00
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
c->c_currentber = ber;
|
2017-03-16 20:25:01 +08:00
|
|
|
|
|
|
|
tag = ber_get_next( c->c_sb, &len, ber );
|
|
|
|
if ( tag != LDAP_TAG_MESSAGE ) {
|
|
|
|
int err = sock_errno();
|
|
|
|
|
|
|
|
if ( err != EWOULDBLOCK && err != EAGAIN ) {
|
2017-05-18 23:24:36 +08:00
|
|
|
if ( err ) {
|
|
|
|
char ebuf[128];
|
|
|
|
Debug( LDAP_DEBUG_ANY, "client_read_cb: "
|
|
|
|
"ber_get_next on fd %d failed errno=%d (%s)\n",
|
|
|
|
c->c_fd, err,
|
|
|
|
sock_errstr( err, ebuf, sizeof(ebuf) ) );
|
|
|
|
}
|
2017-03-16 20:25:01 +08:00
|
|
|
|
|
|
|
c->c_currentber = NULL;
|
2017-05-03 17:14:19 +08:00
|
|
|
ber_free( ber, 1 );
|
2017-05-09 23:24:14 +08:00
|
|
|
|
|
|
|
event_del( c->c_read_event );
|
2017-04-21 18:07:43 +08:00
|
|
|
CLIENT_DESTROY(c);
|
2017-05-03 17:14:19 +08:00
|
|
|
return;
|
2017-03-16 20:25:01 +08:00
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
event_add( c->c_read_event, NULL );
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK(c);
|
2017-03-16 20:25:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
if ( !slap_conn_max_pdus_per_cycle ||
|
|
|
|
ldap_pvt_thread_pool_submit(
|
|
|
|
&connection_pool, handle_requests, c ) ) {
|
|
|
|
/* If we're overloaded or configured as such, process one and resume in
|
|
|
|
* the next cycle.
|
|
|
|
*
|
|
|
|
* handle_one_request re-locks the mutex in the
|
|
|
|
* process, need to test it's still alive */
|
|
|
|
if ( handle_one_request( c ) == LDAP_SUCCESS ) {
|
|
|
|
CLIENT_UNLOCK_OR_DESTROY(c);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event_del( c->c_read_event );
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
handle_requests( void *ctx, void *arg )
|
|
|
|
{
|
|
|
|
Connection *c = arg;
|
|
|
|
int requests_handled = 0;
|
|
|
|
|
|
|
|
CONNECTION_LOCK(c);
|
|
|
|
for ( ; requests_handled < slap_conn_max_pdus_per_cycle;
|
|
|
|
requests_handled++ ) {
|
|
|
|
BerElement *ber;
|
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
|
|
|
|
|
|
|
/* handle_one_response may unlock the connection in the process, we
|
|
|
|
* need to expect that might be our responsibility to destroy it */
|
|
|
|
if ( handle_one_request( c ) ) {
|
|
|
|
/* Error, connection is unlocked and might already have been
|
|
|
|
* destroyed */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Otherwise, handle_one_request leaves the connection locked */
|
|
|
|
|
|
|
|
if ( (ber = ber_alloc()) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "client_read_cb: "
|
|
|
|
"ber_alloc failed\n" );
|
|
|
|
CLIENT_DESTROY(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->c_currentber = ber;
|
|
|
|
|
|
|
|
tag = ber_get_next( c->c_sb, &len, ber );
|
|
|
|
if ( tag != LDAP_TAG_MESSAGE ) {
|
|
|
|
int err = sock_errno();
|
|
|
|
|
|
|
|
if ( err != EWOULDBLOCK && err != EAGAIN ) {
|
2017-05-18 23:24:36 +08:00
|
|
|
if ( err ) {
|
|
|
|
char ebuf[128];
|
|
|
|
Debug( LDAP_DEBUG_ANY, "handle_requests: "
|
|
|
|
"ber_get_next on fd %d failed errno=%d (%s)\n",
|
|
|
|
c->c_fd, err,
|
|
|
|
sock_errstr( err, ebuf, sizeof(ebuf) ) );
|
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
|
|
|
|
c->c_currentber = NULL;
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
CLIENT_DESTROY(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event_add( c->c_read_event, NULL );
|
|
|
|
CLIENT_UNLOCK_OR_DESTROY(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
handle_one_request( Connection *c )
|
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
Operation *op = NULL;
|
|
|
|
RequestHandler handler = NULL;
|
|
|
|
|
|
|
|
ber = c->c_currentber;
|
2017-03-16 20:25:01 +08:00
|
|
|
c->c_currentber = NULL;
|
|
|
|
|
|
|
|
op = operation_init( c, ber );
|
|
|
|
if ( !op ) {
|
2017-05-03 17:14:19 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "handle_one_request: "
|
2017-03-29 01:12:27 +08:00
|
|
|
"operation_init failed\n" );
|
2017-04-21 18:07:43 +08:00
|
|
|
CLIENT_DESTROY(c);
|
2017-05-03 17:14:19 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
return -1;
|
2017-03-16 20:25:01 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
switch ( op->o_tag ) {
|
|
|
|
case LDAP_REQ_UNBIND:
|
2017-05-03 19:04:03 +08:00
|
|
|
/* There is never a response for this operation */
|
|
|
|
operation_destroy_from_client( op );
|
2017-05-18 23:48:12 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS, "handle_one_request: "
|
|
|
|
"received unbind, closing client connid=%lu\n",
|
|
|
|
c->c_connid );
|
2017-05-03 17:14:19 +08:00
|
|
|
CLIENT_DESTROY(c);
|
|
|
|
return -1;
|
2017-03-29 01:40:20 +08:00
|
|
|
case LDAP_REQ_BIND:
|
2017-05-03 17:14:19 +08:00
|
|
|
handler = client_bind;
|
|
|
|
break;
|
|
|
|
case LDAP_REQ_ABANDON:
|
|
|
|
/* FIXME: We need to be able to abandon a Bind request, handling
|
|
|
|
* ExOps (esp. Cancel) will be different */
|
|
|
|
handler = request_abandon;
|
2017-03-29 01:40:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-14 16:41:57 +08:00
|
|
|
if ( c->c_state == SLAP_C_BINDING ) {
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_client_refcnt++;
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
2017-04-14 16:41:57 +08:00
|
|
|
operation_send_reject(
|
|
|
|
op, LDAP_PROTOCOL_ERROR, "bind in progress", 0 );
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_client_refcnt--;
|
|
|
|
operation_destroy_from_client( op );
|
2017-05-03 17:14:19 +08:00
|
|
|
return LDAP_SUCCESS;
|
2017-04-14 16:41:57 +08:00
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
handler = request_process;
|
2017-03-29 01:40:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
return handler( c, op );
|
2017-03-09 06:59:57 +08:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:11:45 +08:00
|
|
|
void
|
2017-03-09 06:59:57 +08:00
|
|
|
client_write_cb( evutil_socket_t s, short what, void *arg )
|
|
|
|
{
|
|
|
|
Connection *c = arg;
|
2017-03-16 20:19:31 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-05-09 23:22:37 +08:00
|
|
|
if ( !c->c_live ) {
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
|
2017-03-16 20:19:31 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "client_write_cb: "
|
|
|
|
"have something to write to client %lu\n",
|
|
|
|
c->c_connid );
|
|
|
|
|
2017-05-09 23:19:14 +08:00
|
|
|
/* We might have been beaten to flushing the data by another thread */
|
|
|
|
if ( c->c_pendingber && ber_flush( c->c_sb, c->c_pendingber, 1 ) ) {
|
2017-03-16 20:19:31 +08:00
|
|
|
int err = sock_errno();
|
2017-05-18 23:24:36 +08:00
|
|
|
|
2017-03-16 20:19:31 +08:00
|
|
|
if ( err != EWOULDBLOCK && err != EAGAIN ) {
|
2017-05-18 23:24:36 +08:00
|
|
|
char ebuf[128];
|
2017-03-16 20:19:31 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
2017-05-18 23:24:36 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "client_write_cb: "
|
|
|
|
"ber_flush on fd %d failed errno=%d (%s)\n",
|
|
|
|
c->c_fd, err, sock_errstr( err, ebuf, sizeof(ebuf) ) );
|
2017-04-21 18:07:43 +08:00
|
|
|
CLIENT_LOCK_DESTROY(c);
|
2017-03-16 20:19:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event_add( c->c_write_event, NULL );
|
2017-04-21 18:09:14 +08:00
|
|
|
} else {
|
|
|
|
c->c_pendingber = NULL;
|
2017-03-16 20:19:31 +08:00
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
2017-04-21 18:07:43 +08:00
|
|
|
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
CLIENT_UNLOCK_OR_DESTROY(c);
|
2017-03-09 06:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection *
|
|
|
|
client_init(
|
|
|
|
ber_socket_t s,
|
|
|
|
Listener *listener,
|
|
|
|
const char *peername,
|
|
|
|
struct event_base *base,
|
|
|
|
int flags )
|
|
|
|
{
|
|
|
|
Connection *c;
|
|
|
|
struct event *event;
|
|
|
|
|
|
|
|
assert( listener != NULL );
|
|
|
|
|
|
|
|
c = connection_init( s, peername, flags );
|
|
|
|
|
2017-06-14 02:32:35 +08:00
|
|
|
{
|
|
|
|
ber_len_t max = sockbuf_max_incoming_client;
|
|
|
|
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:29:38 +08:00
|
|
|
c->c_state = SLAP_C_READY;
|
|
|
|
|
2017-03-09 06:59:57 +08:00
|
|
|
event = event_new( base, s, EV_READ|EV_PERSIST, client_read_cb, c );
|
|
|
|
if ( !event ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "client_init: "
|
|
|
|
"Read event could not be allocated\n" );
|
2017-03-09 06:59:57 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
event_add( event, NULL );
|
|
|
|
c->c_read_event = event;
|
|
|
|
|
|
|
|
event = event_new( base, s, EV_WRITE, client_write_cb, c );
|
|
|
|
if ( !event ) {
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "client_init: "
|
|
|
|
"Write event could not be allocated\n" );
|
2017-03-09 06:59:57 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* We only register the write event when we have data pending */
|
|
|
|
c->c_write_event = event;
|
|
|
|
|
|
|
|
c->c_private = listener;
|
2017-05-25 22:04:08 +08:00
|
|
|
|
|
|
|
/* There should be no lock inversion yet since no other thread could
|
|
|
|
* approach it from clients side */
|
|
|
|
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 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK(c);
|
2017-03-09 06:59:57 +08:00
|
|
|
|
|
|
|
return c;
|
|
|
|
fail:
|
|
|
|
if ( c->c_write_event ) {
|
|
|
|
event_free( c->c_write_event );
|
2017-05-10 01:02:28 +08:00
|
|
|
c->c_write_event = NULL;
|
2017-03-09 06:59:57 +08:00
|
|
|
}
|
|
|
|
if ( c->c_read_event ) {
|
|
|
|
event_free( c->c_read_event );
|
2017-05-10 01:02:28 +08:00
|
|
|
c->c_read_event = NULL;
|
2017-03-09 06:59:57 +08:00
|
|
|
}
|
2017-03-17 22:29:38 +08:00
|
|
|
c->c_state = SLAP_C_INVALID;
|
2017-03-09 06:59:57 +08:00
|
|
|
connection_destroy( c );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
void
|
2017-03-09 06:59:57 +08:00
|
|
|
client_destroy( Connection *c )
|
|
|
|
{
|
2017-05-25 22:04:08 +08:00
|
|
|
enum sc_state state;
|
|
|
|
|
2017-04-14 16:45:18 +08:00
|
|
|
Debug( LDAP_DEBUG_CONNS, "client_destroy: "
|
2017-05-18 23:48:12 +08:00
|
|
|
"destroying client connid=%lu\n",
|
2017-04-14 16:45:18 +08:00
|
|
|
c->c_connid );
|
|
|
|
|
2017-05-25 22:04:08 +08:00
|
|
|
assert( c->c_state != SLAP_C_INVALID );
|
|
|
|
state = c->c_state;
|
|
|
|
c->c_state = SLAP_C_INVALID;
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
if ( c->c_read_event ) {
|
|
|
|
event_free( c->c_read_event );
|
2017-05-10 01:02:28 +08:00
|
|
|
c->c_read_event = NULL;
|
2017-05-03 18:31:40 +08:00
|
|
|
}
|
2017-04-14 16:45:18 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
if ( c->c_write_event ) {
|
|
|
|
event_free( c->c_write_event );
|
2017-05-10 01:02:28 +08:00
|
|
|
c->c_write_event = NULL;
|
2017-04-14 16:45:18 +08:00
|
|
|
}
|
|
|
|
|
2017-05-25 22:04:08 +08:00
|
|
|
if ( state != SLAP_C_CLOSING ) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &clients_mutex );
|
|
|
|
LDAP_CIRCLEQ_REMOVE( &clients, c, c_next );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &clients_mutex );
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
client_reset( c );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 ) {
|
|
|
|
c->c_state = SLAP_C_CLOSING;
|
|
|
|
Debug( LDAP_DEBUG_CONNS, "client_destroy: "
|
|
|
|
"connid=%lu aborting with refcnt=%d\n",
|
|
|
|
c->c_connid, c->c_refcnt );
|
|
|
|
CONNECTION_UNLOCK(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-25 22:04:08 +08:00
|
|
|
|
2017-03-09 06:59:57 +08:00
|
|
|
connection_destroy( c );
|
|
|
|
}
|
2017-05-25 22:04:08 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
clients_destroy( void )
|
|
|
|
{
|
|
|
|
ldap_pvt_thread_mutex_lock( &clients_mutex );
|
|
|
|
while ( !LDAP_CIRCLEQ_EMPTY( &clients ) ) {
|
|
|
|
Connection *c = LDAP_CIRCLEQ_FIRST( &clients );
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_unlock( &clients_mutex );
|
|
|
|
CONNECTION_LOCK(c);
|
|
|
|
/* We have shut down all processing, a dying connection connection
|
|
|
|
* should have been reclaimed by now! */
|
|
|
|
assert( c->c_live );
|
|
|
|
/* Upstream connections have already been destroyed, there should be no
|
|
|
|
* ops left */
|
|
|
|
assert( !c->c_ops );
|
|
|
|
CLIENT_DESTROY(c);
|
|
|
|
ldap_pvt_thread_mutex_lock( &clients_mutex );
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &clients_mutex );
|
|
|
|
}
|