2017-03-16 20:24:42 +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 "lutil.h"
|
|
|
|
#include "slap.h"
|
|
|
|
|
2017-03-29 01:07:09 +08:00
|
|
|
ber_tag_t
|
|
|
|
slap_req2res( ber_tag_t tag )
|
|
|
|
{
|
|
|
|
switch ( tag ) {
|
|
|
|
case LDAP_REQ_ADD:
|
|
|
|
case LDAP_REQ_BIND:
|
|
|
|
case LDAP_REQ_COMPARE:
|
|
|
|
case LDAP_REQ_EXTENDED:
|
|
|
|
case LDAP_REQ_MODIFY:
|
|
|
|
case LDAP_REQ_MODRDN:
|
|
|
|
tag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_REQ_DELETE:
|
|
|
|
tag = LDAP_RES_DELETE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_REQ_ABANDON:
|
|
|
|
case LDAP_REQ_UNBIND:
|
|
|
|
tag = LBER_SEQUENCE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_REQ_SEARCH:
|
|
|
|
tag = LDAP_RES_SEARCH_RESULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tag = LBER_SEQUENCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
slap_msgtype2str( ber_tag_t tag )
|
|
|
|
{
|
|
|
|
switch ( tag ) {
|
|
|
|
case LDAP_REQ_ABANDON: return "abandon request";
|
|
|
|
case LDAP_REQ_ADD: return "add request";
|
|
|
|
case LDAP_REQ_BIND: return "bind request";
|
|
|
|
case LDAP_REQ_COMPARE: return "compare request";
|
|
|
|
case LDAP_REQ_DELETE: return "delete request";
|
|
|
|
case LDAP_REQ_EXTENDED: return "extended request";
|
|
|
|
case LDAP_REQ_MODIFY: return "modify request";
|
|
|
|
case LDAP_REQ_RENAME: return "rename request";
|
|
|
|
case LDAP_REQ_SEARCH: return "search request";
|
|
|
|
case LDAP_REQ_UNBIND: return "unbind request";
|
|
|
|
|
|
|
|
case LDAP_RES_ADD: return "add result";
|
|
|
|
case LDAP_RES_BIND: return "bind result";
|
|
|
|
case LDAP_RES_COMPARE: return "compare result";
|
|
|
|
case LDAP_RES_DELETE: return "delete result";
|
|
|
|
case LDAP_RES_EXTENDED: return "extended result";
|
|
|
|
case LDAP_RES_INTERMEDIATE: return "intermediate response";
|
|
|
|
case LDAP_RES_MODIFY: return "modify result";
|
|
|
|
case LDAP_RES_RENAME: return "rename result";
|
|
|
|
case LDAP_RES_SEARCH_ENTRY: return "search-entry response";
|
|
|
|
case LDAP_RES_SEARCH_REFERENCE: return "search-reference response";
|
|
|
|
case LDAP_RES_SEARCH_RESULT: return "search result";
|
|
|
|
}
|
|
|
|
return "unknown message";
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:24:42 +08:00
|
|
|
int
|
|
|
|
operation_client_cmp( const void *left, const void *right )
|
|
|
|
{
|
|
|
|
const Operation *l = left, *r = right;
|
|
|
|
|
2017-05-18 23:28:55 +08:00
|
|
|
assert( l->o_client_connid == r->o_client_connid );
|
2017-03-16 20:24:42 +08:00
|
|
|
return ( l->o_client_msgid < r->o_client_msgid ) ?
|
|
|
|
-1 :
|
|
|
|
( l->o_client_msgid > r->o_client_msgid );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
operation_upstream_cmp( const void *left, const void *right )
|
|
|
|
{
|
|
|
|
const Operation *l = left, *r = right;
|
|
|
|
|
2017-05-18 23:28:55 +08:00
|
|
|
assert( l->o_upstream_connid == r->o_upstream_connid );
|
2017-03-16 20:24:42 +08:00
|
|
|
return ( l->o_upstream_msgid < r->o_upstream_msgid ) ?
|
|
|
|
-1 :
|
|
|
|
( l->o_upstream_msgid > r->o_upstream_msgid );
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/*
|
|
|
|
* Free the operation, subject to there being noone else holding a reference
|
|
|
|
* to it.
|
|
|
|
*
|
|
|
|
* Both operation_destroy_from_* functions are the same, two implementations
|
|
|
|
* exist to cater for the fact that either side (client or upstream) might
|
|
|
|
* decide to destroy it and each holds a different mutex.
|
|
|
|
*
|
|
|
|
* Due to the fact that we rely on mutexes on both connections which have a
|
|
|
|
* different timespan from the operation, we have to take the following race
|
|
|
|
* into account:
|
|
|
|
*
|
|
|
|
* Trigger
|
|
|
|
* - both operation_destroy_from_client and operation_destroy_from_upstream
|
|
|
|
* are called at the same time (each holding its mutex), several times
|
|
|
|
* before one of them finishes
|
|
|
|
* - either or both connections might have started the process of being
|
|
|
|
* destroyed
|
|
|
|
*
|
|
|
|
* We need to detect that the race has happened and only allow one of them to
|
|
|
|
* free the operation (we use o_freeing != 0 to announce+detect that).
|
|
|
|
*
|
|
|
|
* In case the caller was in the process of destroying the connection and the
|
|
|
|
* race had been won by the mirror caller, it will increment c_refcnt on its
|
|
|
|
* connection and make sure to postpone the final step in
|
|
|
|
* client/upstream_destroy(). Testing o_freeing for the mirror side's token
|
|
|
|
* allows the winner to detect that it has been a party to the race and a token
|
|
|
|
* in c_refcnt has been deposited on its behalf.
|
2017-05-10 01:03:49 +08:00
|
|
|
*
|
|
|
|
* Beware! This widget really touches all the mutexes we have and showcases the
|
|
|
|
* issues with maintaining so many mutex ordering restrictions.
|
2017-05-03 18:31:40 +08:00
|
|
|
*/
|
2017-03-16 20:24:42 +08:00
|
|
|
void
|
2017-05-03 18:31:40 +08:00
|
|
|
operation_destroy_from_client( Operation *op )
|
2017-03-16 20:24:42 +08:00
|
|
|
{
|
2017-05-10 01:03:49 +08:00
|
|
|
Connection *upstream, *client = op->o_client;
|
2017-05-03 18:31:40 +08:00
|
|
|
Backend *b = NULL;
|
2017-05-10 01:03:49 +08:00
|
|
|
int race_state, detach_client = !client->c_live;
|
2017-05-03 18:31:40 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p attempting to release operation\n",
|
|
|
|
op );
|
|
|
|
|
|
|
|
/* 1. liveness/refcnt adjustment and test */
|
|
|
|
op->o_client_refcnt -= op->o_client_live;
|
|
|
|
op->o_client_live = 0;
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
assert( op->o_client_refcnt <= client->c_refcnt );
|
2017-05-03 18:31:40 +08:00
|
|
|
if ( op->o_client_refcnt ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p not dead yet\n",
|
|
|
|
op );
|
|
|
|
return;
|
|
|
|
}
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 2. Remove from the operation map and TODO adjust the pending op count */
|
|
|
|
tavl_delete( &client->c_ops, op, operation_client_cmp );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 3. Detect whether we entered a race to free op and indicate that to any
|
|
|
|
* others */
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
2017-05-03 18:31:40 +08:00
|
|
|
race_state = op->o_freeing;
|
|
|
|
op->o_freeing |= SLAP_OP_FREEING_CLIENT;
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK_INCREF(client);
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( detach_client ) {
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
op->o_client = NULL;
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
|
|
|
|
/* 4. If we lost the race, deal with it */
|
|
|
|
if ( race_state ) {
|
|
|
|
/*
|
|
|
|
* We have raced to destroy op and the first one to lose on this side,
|
|
|
|
* leave a refcnt token on client so we don't destroy it before the
|
|
|
|
* other side has finished (it knows we did that when it examines
|
|
|
|
* o_freeing again).
|
|
|
|
*/
|
2017-05-18 23:51:57 +08:00
|
|
|
if ( !detach_client && race_state == SLAP_OP_FREEING_UPSTREAM ) {
|
2017-05-10 01:03:49 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
2017-05-18 23:51:57 +08:00
|
|
|
"op=%p lost race, increasing client refcnt c=%p\n",
|
|
|
|
op, client );
|
2017-05-10 01:03:49 +08:00
|
|
|
CONNECTION_LOCK(client);
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p lost race with another "
|
|
|
|
"operation_destroy_from_client\n",
|
|
|
|
op );
|
|
|
|
CONNECTION_LOCK_DECREF(client);
|
2017-04-14 16:45:18 +08:00
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. If we raced the upstream side and won, reclaim the token */
|
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
upstream = op->o_upstream;
|
|
|
|
if ( upstream ) {
|
2017-05-18 23:51:57 +08:00
|
|
|
CONNECTION_LOCK(upstream);
|
2017-05-03 18:31:40 +08:00
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
|
|
|
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
|
|
|
if ( upstream && ( op->o_freeing & SLAP_OP_FREEING_UPSTREAM ) ) {
|
|
|
|
/*
|
|
|
|
* We have raced to destroy op and won. To avoid freeing the connection
|
|
|
|
* under us, a refcnt token has been left over for us on the upstream,
|
|
|
|
* decref and see whether we are in charge of freeing it
|
|
|
|
*/
|
|
|
|
upstream->c_refcnt--;
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p other side lost race with us\n",
|
|
|
|
op );
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 6. liveness/refcnt adjustment and test */
|
|
|
|
op->o_upstream_refcnt -= op->o_upstream_live;
|
|
|
|
op->o_upstream_live = 0;
|
|
|
|
if ( op->o_upstream_refcnt ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p other side still alive, refcnt=%d\n",
|
|
|
|
op, op->o_upstream_refcnt );
|
2017-05-18 23:51:57 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* There must have been no race if op is still alive */
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_freeing &= ~SLAP_OP_FREEING_CLIENT;
|
|
|
|
assert( op->o_freeing == 0 );
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
|
|
|
assert( upstream != NULL );
|
|
|
|
CONNECTION_UNLOCK(upstream);
|
2017-05-03 18:31:40 +08:00
|
|
|
CONNECTION_LOCK_DECREF(client);
|
|
|
|
return;
|
2017-03-16 20:24:42 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 7. Remove from the operation map and adjust the pending op count */
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( upstream ) {
|
|
|
|
if ( tavl_delete( &upstream->c_ops, op, operation_upstream_cmp ) ) {
|
|
|
|
upstream->c_n_ops_executing--;
|
|
|
|
b = (Backend *)upstream->c_private;
|
|
|
|
}
|
|
|
|
UPSTREAM_UNLOCK_OR_DESTROY(upstream);
|
2017-04-14 16:45:18 +08:00
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( b ) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
b->b_n_ops_executing--;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
}
|
2017-04-14 16:45:18 +08:00
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 8. Release the operation */
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_client: "
|
|
|
|
"op=%p destroyed operation from client connid=%lu, "
|
|
|
|
"client msgid=%d\n",
|
|
|
|
op, op->o_client_connid, op->o_client_msgid );
|
|
|
|
ber_free( op->o_ber, 1 );
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &op->o_mutex );
|
2017-05-03 18:31:40 +08:00
|
|
|
ch_free( op );
|
2017-05-10 01:03:49 +08:00
|
|
|
|
|
|
|
CONNECTION_LOCK_DECREF(client);
|
2017-05-03 18:31:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See operation_destroy_from_client.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
operation_destroy_from_upstream( Operation *op )
|
|
|
|
{
|
2017-05-10 01:03:49 +08:00
|
|
|
Connection *client, *upstream = op->o_upstream;
|
2017-05-03 18:31:40 +08:00
|
|
|
Backend *b = NULL;
|
2017-05-10 01:03:49 +08:00
|
|
|
int race_state, detach_upstream = !upstream->c_live;
|
2017-05-03 18:31:40 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p attempting to release operation\n",
|
|
|
|
op );
|
|
|
|
|
|
|
|
/* 1. liveness/refcnt adjustment and test */
|
|
|
|
op->o_upstream_refcnt -= op->o_upstream_live;
|
|
|
|
op->o_upstream_live = 0;
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
assert( op->o_upstream_refcnt <= upstream->c_refcnt );
|
2017-05-03 18:31:40 +08:00
|
|
|
if ( op->o_upstream_refcnt ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p not dead yet\n",
|
|
|
|
op );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Remove from the operation map and adjust the pending op count */
|
|
|
|
if ( tavl_delete( &upstream->c_ops, op, operation_upstream_cmp ) ) {
|
|
|
|
upstream->c_n_ops_executing--;
|
|
|
|
b = (Backend *)upstream->c_private;
|
|
|
|
}
|
|
|
|
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
|
|
|
race_state = op->o_freeing;
|
|
|
|
op->o_freeing |= SLAP_OP_FREEING_UPSTREAM;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(upstream);
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 3. Detect whether we entered a race to free op */
|
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( detach_upstream ) {
|
|
|
|
op->o_upstream = NULL;
|
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( b ) {
|
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
b->b_n_ops_executing--;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 4. If we lost the race, deal with it */
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( race_state ) {
|
2017-05-03 18:31:40 +08:00
|
|
|
/*
|
|
|
|
* We have raced to destroy op and the first one to lose on this side,
|
|
|
|
* leave a refcnt token on upstream so we don't destroy it before the
|
|
|
|
* other side has finished (it knows we did that when it examines
|
|
|
|
* o_freeing again).
|
|
|
|
*/
|
2017-05-18 23:51:57 +08:00
|
|
|
if ( !detach_upstream && race_state == SLAP_OP_FREEING_CLIENT ) {
|
2017-05-10 01:03:49 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
2017-05-18 23:51:57 +08:00
|
|
|
"op=%p lost race, increasing upstream refcnt c=%p\n",
|
|
|
|
op, upstream );
|
2017-05-10 01:03:49 +08:00
|
|
|
CONNECTION_LOCK(upstream);
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p lost race with another "
|
|
|
|
"operation_destroy_from_upstream\n",
|
|
|
|
op );
|
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
return;
|
2017-03-16 20:24:42 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 5. If we raced the client side and won, reclaim the token */
|
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
2017-05-10 01:03:49 +08:00
|
|
|
client = op->o_client;
|
|
|
|
if ( client ) {
|
2017-05-18 23:51:57 +08:00
|
|
|
CONNECTION_LOCK(client);
|
2017-05-03 18:31:40 +08:00
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
|
|
|
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
|
|
|
if ( client && ( op->o_freeing & SLAP_OP_FREEING_CLIENT ) ) {
|
|
|
|
/*
|
|
|
|
* We have raced to destroy op and won. To avoid freeing the connection
|
|
|
|
* under us, a refcnt token has been left over for us on the client,
|
|
|
|
* decref and see whether we are in charge of freeing it
|
|
|
|
*/
|
|
|
|
client->c_refcnt--;
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p other side lost race with us\n",
|
|
|
|
op );
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
/* 6. liveness/refcnt adjustment and test */
|
|
|
|
op->o_client_refcnt -= op->o_client_live;
|
|
|
|
op->o_client_live = 0;
|
|
|
|
if ( op->o_client_refcnt ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p other side still alive, refcnt=%d\n",
|
|
|
|
op, op->o_client_refcnt );
|
|
|
|
/* There must have been no race if op is still alive */
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &op->o_mutex );
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_freeing &= ~SLAP_OP_FREEING_UPSTREAM;
|
|
|
|
assert( op->o_freeing == 0 );
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &op->o_mutex );
|
|
|
|
|
|
|
|
assert( client != NULL );
|
|
|
|
CONNECTION_UNLOCK(client);
|
2017-05-03 18:31:40 +08:00
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 7. Remove from the operation map and TODO adjust the pending op count */
|
2017-05-10 01:03:49 +08:00
|
|
|
if ( client ) {
|
|
|
|
tavl_delete( &client->c_ops, op, operation_client_cmp );
|
|
|
|
CLIENT_UNLOCK_OR_DESTROY(client);
|
|
|
|
}
|
2017-05-03 18:31:40 +08:00
|
|
|
|
|
|
|
/* 8. Release the operation */
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_destroy_from_upstream: "
|
|
|
|
"op=%p destroyed operation from client connid=%lu, "
|
|
|
|
"client msgid=%d\n",
|
|
|
|
op, op->o_client_connid, op->o_client_msgid );
|
|
|
|
ber_free( op->o_ber, 1 );
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &op->o_mutex );
|
2017-03-16 20:24:42 +08:00
|
|
|
ch_free( op );
|
2017-05-03 18:31:40 +08:00
|
|
|
|
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
2017-03-16 20:24:42 +08:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
/*
|
|
|
|
* Entered holding c_mutex for now.
|
|
|
|
*/
|
2017-03-16 20:24:42 +08:00
|
|
|
Operation *
|
|
|
|
operation_init( Connection *c, BerElement *ber )
|
|
|
|
{
|
|
|
|
Operation *op;
|
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
op = ch_calloc( 1, sizeof(Operation) );
|
|
|
|
op->o_client = c;
|
2017-04-14 16:43:37 +08:00
|
|
|
op->o_client_connid = c->c_connid;
|
2017-03-29 01:40:20 +08:00
|
|
|
op->o_ber = ber;
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-05-18 23:51:57 +08:00
|
|
|
ldap_pvt_thread_mutex_init( &op->o_mutex );
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_client_live = op->o_client_refcnt = 1;
|
|
|
|
op->o_upstream_live = op->o_upstream_refcnt = 1;
|
|
|
|
|
2017-03-16 20:24:42 +08:00
|
|
|
tag = ber_get_int( ber, &op->o_client_msgid );
|
|
|
|
if ( tag != LDAP_TAG_MSGID ) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = tavl_insert( &c->c_ops, op, operation_client_cmp, avl_dup_error );
|
|
|
|
if ( rc ) {
|
|
|
|
Debug( LDAP_DEBUG_PACKETS, "operation_init: "
|
2017-04-14 16:43:37 +08:00
|
|
|
"several operations with same msgid=%d in-flight from client "
|
|
|
|
"%lu\n",
|
|
|
|
op->o_client_msgid, op->o_client_connid );
|
2017-03-16 20:24:42 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = op->o_tag = ber_skip_element( ber, &op->o_request );
|
|
|
|
switch ( tag ) {
|
|
|
|
case LBER_ERROR:
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( rc ) {
|
|
|
|
tavl_delete( &c->c_ops, op, operation_client_cmp );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = ber_peek_tag( ber, &len );
|
|
|
|
if ( tag == LDAP_TAG_CONTROLS ) {
|
|
|
|
ber_skip_element( ber, &op->o_ctrls );
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:12:27 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_init: "
|
2017-05-18 23:48:12 +08:00
|
|
|
"set up a new operation, %s with msgid=%d for client connid=%lu\n",
|
2017-04-14 16:43:37 +08:00
|
|
|
slap_msgtype2str( op->o_tag ), op->o_client_msgid,
|
|
|
|
op->o_client_connid );
|
2017-03-29 01:12:27 +08:00
|
|
|
|
2017-04-14 16:45:18 +08:00
|
|
|
c->c_n_ops_executing++;
|
2017-03-16 20:24:42 +08:00
|
|
|
return op;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ch_free( op );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
void
|
|
|
|
operation_abandon( Operation *op )
|
|
|
|
{
|
2017-05-10 01:03:49 +08:00
|
|
|
Connection *c;
|
2017-05-03 17:14:19 +08:00
|
|
|
BerElement *ber;
|
|
|
|
Backend *b;
|
2017-03-29 01:40:20 +08:00
|
|
|
int rc;
|
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
|
|
|
c = op->o_upstream;
|
2017-05-03 17:14:19 +08:00
|
|
|
if ( !c ) {
|
|
|
|
c = op->o_client;
|
2017-05-10 01:03:49 +08:00
|
|
|
assert( c );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
/* Caller should hold a reference on client */
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-05-10 01:03:49 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
2017-05-03 18:31:40 +08:00
|
|
|
operation_destroy_from_client( op );
|
2017-05-03 17:14:19 +08:00
|
|
|
CLIENT_UNLOCK_OR_DESTROY(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-05-10 01:03:49 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
2017-05-03 17:14:19 +08:00
|
|
|
if ( tavl_delete( &c->c_ops, op, operation_upstream_cmp ) == NULL ) {
|
|
|
|
/* The operation has already been abandoned or finished */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
c->c_n_ops_executing--;
|
|
|
|
b = (Backend *)c->c_private;
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &b->b_mutex );
|
|
|
|
b->b_n_ops_executing--;
|
|
|
|
ldap_pvt_thread_mutex_unlock( &b->b_mutex );
|
2017-04-14 16:45:18 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &c->c_io_mutex );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
ber = c->c_pendingber;
|
|
|
|
if ( ber == NULL && (ber = ber_alloc()) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "operation_abandon: "
|
|
|
|
"ber_alloc failed\n" );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
c->c_pendingber = ber;
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
rc = ber_printf( ber, "t{titi}", LDAP_TAG_MESSAGE,
|
|
|
|
LDAP_TAG_MSGID, c->c_next_msgid++,
|
|
|
|
LDAP_REQ_ABANDON, op->o_upstream_msgid );
|
2017-03-29 01:40:20 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
if ( rc == -1 ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
c->c_pendingber = NULL;
|
|
|
|
}
|
2017-04-19 16:51:47 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
2017-04-19 16:51:47 +08:00
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
if ( rc != -1 ) {
|
|
|
|
upstream_write_cb( -1, 0, c );
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
done:
|
2017-05-03 18:31:40 +08:00
|
|
|
operation_destroy_from_upstream( op );
|
2017-05-03 17:14:19 +08:00
|
|
|
UPSTREAM_UNLOCK_OR_DESTROY(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
int
|
|
|
|
request_abandon( Connection *c, Operation *op )
|
|
|
|
{
|
2017-05-18 23:28:55 +08:00
|
|
|
Operation *request, needle = { .o_client_connid = c->c_connid };
|
2017-05-18 23:12:06 +08:00
|
|
|
int rc = LDAP_SUCCESS;
|
2017-05-03 17:14:19 +08:00
|
|
|
|
2017-06-14 02:44:03 +08:00
|
|
|
/* parse two's complement integer */
|
|
|
|
if ( !BER_BVISEMPTY( &op->o_request ) ) {
|
|
|
|
unsigned char *buf = (unsigned char *)op->o_request.bv_val;
|
|
|
|
ber_len_t i;
|
|
|
|
ber_int_t netnum = buf[0] & 0xff;
|
|
|
|
|
|
|
|
/* sign extend */
|
|
|
|
netnum = ( netnum ^ 0x80 ) - 0x80;
|
|
|
|
|
|
|
|
/* shift in the bytes */
|
|
|
|
for ( i = 1; i < op->o_request.bv_len; i++ ) {
|
|
|
|
netnum = ( netnum << 8 ) | buf[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
needle.o_client_msgid = netnum;
|
2017-05-03 17:14:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
request = tavl_find( c->c_ops, &needle, operation_client_cmp );
|
|
|
|
if ( !request ) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
|
|
|
operation_abandon( request );
|
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
|
|
|
|
done:
|
2017-05-03 18:31:40 +08:00
|
|
|
operation_destroy_from_client( op );
|
2017-05-03 17:14:19 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
void
|
2017-04-14 16:39:24 +08:00
|
|
|
operation_send_reject(
|
|
|
|
Operation *op,
|
|
|
|
int result,
|
|
|
|
const char *msg,
|
|
|
|
int send_anyway )
|
2017-03-29 01:40:20 +08:00
|
|
|
{
|
2017-05-10 01:03:49 +08:00
|
|
|
Connection *c;
|
2017-03-29 01:40:20 +08:00
|
|
|
BerElement *ber;
|
|
|
|
int found;
|
|
|
|
|
2017-04-14 16:39:24 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "operation_send_reject: "
|
|
|
|
"rejecting %s from client %lu with message: \"%s\"\n",
|
2017-04-14 16:43:37 +08:00
|
|
|
slap_msgtype2str( op->o_tag ), op->o_client_connid, msg );
|
2017-04-14 16:39:24 +08:00
|
|
|
|
2017-05-10 01:03:49 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &operation_mutex );
|
|
|
|
c = op->o_client;
|
|
|
|
if ( !c ) {
|
|
|
|
c = op->o_upstream;
|
|
|
|
/* One of the connections has initiated this and keeps a reference, if
|
|
|
|
* client is dead, it must have been the upstream */
|
|
|
|
assert( c );
|
|
|
|
CONNECTION_LOCK(c);
|
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
|
|
|
operation_destroy_from_upstream( op );
|
|
|
|
UPSTREAM_UNLOCK_OR_DESTROY(c);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK(c);
|
2017-05-10 01:03:49 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &operation_mutex );
|
|
|
|
|
2017-03-29 01:40:20 +08:00
|
|
|
found = ( tavl_delete( &c->c_ops, op, operation_client_cmp ) == op );
|
2017-04-14 16:39:24 +08:00
|
|
|
if ( !found && !send_anyway ) {
|
2017-05-03 18:31:40 +08:00
|
|
|
goto done;
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(c);
|
2017-03-29 01:40:20 +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 );
|
2017-05-03 18:31:40 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
|
|
|
operation_destroy_from_client( op );
|
|
|
|
CLIENT_DESTROY(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->c_pendingber = ber;
|
|
|
|
|
|
|
|
ber_printf( ber, "t{tit{ess}}", LDAP_TAG_MESSAGE,
|
|
|
|
LDAP_TAG_MSGID, op->o_client_msgid,
|
|
|
|
slap_req2res( op->o_tag ), result, "", msg );
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_unlock( &c->c_io_mutex );
|
|
|
|
|
|
|
|
client_write_cb( -1, 0, c );
|
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(c);
|
2017-05-03 18:31:40 +08:00
|
|
|
done:
|
|
|
|
operation_destroy_from_client( op );
|
2017-04-21 18:07:43 +08:00
|
|
|
CLIENT_UNLOCK_OR_DESTROY(c);
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
operation_lost_upstream( Operation *op )
|
|
|
|
{
|
|
|
|
operation_send_reject( op, LDAP_UNAVAILABLE,
|
2017-04-14 16:39:24 +08:00
|
|
|
"connection to the remote server has been severed", 0 );
|
2017-03-29 01:40:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
int
|
|
|
|
request_process( Connection *client, Operation *op )
|
2017-03-16 20:24:42 +08:00
|
|
|
{
|
|
|
|
BerElement *output;
|
2017-05-03 17:14:19 +08:00
|
|
|
Connection *upstream;
|
2017-03-16 20:24:42 +08:00
|
|
|
ber_int_t msgid;
|
2017-05-03 17:14:19 +08:00
|
|
|
int rc = LDAP_SUCCESS;
|
|
|
|
|
2017-05-03 18:31:40 +08:00
|
|
|
op->o_client_refcnt++;
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(client);
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-04-14 16:36:42 +08:00
|
|
|
upstream = backend_select( op );
|
|
|
|
if ( !upstream ) {
|
2017-03-29 01:40:20 +08:00
|
|
|
Debug( LDAP_DEBUG_STATS, "request_process: "
|
2017-05-18 23:48:12 +08:00
|
|
|
"connid=%lu, msgid=%d no available connection found\n",
|
|
|
|
op->o_client_connid, op->o_client_msgid );
|
2017-06-14 02:36:23 +08:00
|
|
|
|
|
|
|
operation_send_reject(
|
|
|
|
op, LDAP_UNAVAILABLE, "no connections available", 1 );
|
2017-03-16 20:24:42 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
2017-04-14 16:36:42 +08:00
|
|
|
op->o_upstream = upstream;
|
2017-04-14 16:43:37 +08:00
|
|
|
op->o_upstream_connid = upstream->c_connid;
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-04-14 16:36:42 +08:00
|
|
|
output = upstream->c_pendingber;
|
2017-03-29 01:40:20 +08:00
|
|
|
if ( output == NULL && (output = ber_alloc()) == NULL ) {
|
2017-03-16 20:24:42 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
2017-04-14 16:36:42 +08:00
|
|
|
upstream->c_pendingber = output;
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
2017-04-14 16:36:42 +08:00
|
|
|
op->o_upstream_msgid = msgid = upstream->c_next_msgid++;
|
|
|
|
rc = tavl_insert(
|
|
|
|
&upstream->c_ops, op, operation_upstream_cmp, avl_dup_error );
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(upstream);
|
2017-05-03 17:14:19 +08:00
|
|
|
|
|
|
|
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, slap_msgtype2str( op->o_tag ),
|
|
|
|
op->o_client_msgid, op->o_upstream_connid, op->o_upstream_msgid );
|
2017-03-16 20:24:42 +08:00
|
|
|
assert( rc == LDAP_SUCCESS );
|
|
|
|
|
2017-06-14 02:48:05 +08:00
|
|
|
if ( (lload_features & LLOAD_FEATURE_PROXYAUTHZ) &&
|
|
|
|
client->c_type != SLAP_C_PRIVILEGED ) {
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK_DECREF(client);
|
2017-04-04 22:11:48 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "request_process: "
|
|
|
|
"proxying identity %s to upstream\n",
|
2017-04-14 16:36:42 +08:00
|
|
|
client->c_auth.bv_val );
|
2017-04-04 22:11:48 +08:00
|
|
|
ber_printf( output, "t{titOt{{sbO}" /* "}}" */, LDAP_TAG_MESSAGE,
|
|
|
|
LDAP_TAG_MSGID, msgid,
|
|
|
|
op->o_tag, &op->o_request,
|
|
|
|
LDAP_TAG_CONTROLS,
|
2017-04-14 16:36:42 +08:00
|
|
|
LDAP_CONTROL_PROXY_AUTHZ, 1, &client->c_auth );
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_UNLOCK_INCREF(client);
|
2017-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
if ( !BER_BVISNULL( &op->o_ctrls ) ) {
|
2017-06-14 02:42:40 +08:00
|
|
|
ber_write( output, op->o_ctrls.bv_val, op->o_ctrls.bv_len, 0 );
|
2017-04-04 22:11:48 +08:00
|
|
|
}
|
2017-06-14 02:42:40 +08:00
|
|
|
|
2017-04-04 22:11:48 +08:00
|
|
|
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 ) );
|
|
|
|
}
|
2017-04-14 16:36:42 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-04-14 16:36:42 +08:00
|
|
|
upstream_write_cb( -1, 0, upstream );
|
2017-03-16 20:24:42 +08:00
|
|
|
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
|
|
|
UPSTREAM_UNLOCK_OR_DESTROY(upstream);
|
|
|
|
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK_DECREF(client);
|
2017-05-03 18:31:40 +08:00
|
|
|
if ( !--op->o_client_refcnt ) {
|
|
|
|
operation_destroy_from_client( op );
|
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
return rc;
|
2017-04-14 16:36:42 +08:00
|
|
|
|
2017-03-16 20:24:42 +08:00
|
|
|
fail:
|
2017-04-14 16:36:42 +08:00
|
|
|
if ( upstream ) {
|
|
|
|
ldap_pvt_thread_mutex_unlock( &upstream->c_io_mutex );
|
2017-04-21 18:07:43 +08:00
|
|
|
CONNECTION_LOCK_DECREF(upstream);
|
|
|
|
UPSTREAM_UNLOCK_OR_DESTROY(upstream);
|
2017-06-14 02:36:23 +08:00
|
|
|
operation_send_reject( op, LDAP_OTHER, "internal error", 0 );
|
2017-04-14 16:36:42 +08:00
|
|
|
}
|
2017-05-03 17:14:19 +08:00
|
|
|
CONNECTION_LOCK_DECREF(client);
|
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 rc;
|
2017-03-16 20:24:42 +08:00
|
|
|
}
|