1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2007-01-03 04:00:42 +08:00
|
|
|
* Copyright 1998-2007 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* 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>.
|
1998-12-29 04:53:15 +08:00
|
|
|
*/
|
2003-11-26 15:16:36 +08:00
|
|
|
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
2000-06-01 01:47:33 +08:00
|
|
|
*/
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This notice applies to changes, created by or for Novell, Inc.,
|
2000-06-01 01:34:43 +08:00
|
|
|
* to preexisting works for which notices appear elsewhere in this file.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
|
|
|
|
* USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
|
|
|
|
* 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
|
|
|
|
* HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
|
|
|
|
* TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
|
|
|
|
* WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
|
|
|
|
* LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
|
|
|
|
* PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
|
2000-06-01 01:47:33 +08:00
|
|
|
*---
|
2000-06-01 01:34:43 +08:00
|
|
|
* Modification to OpenLDAP source by Novell, Inc.
|
|
|
|
* April 2000 sfs Added code to chase V3 referrals
|
1998-08-09 08:43:13 +08:00
|
|
|
* request.c - sending of ldap requests; handling of referrals
|
2003-11-26 15:16:36 +08:00
|
|
|
*---
|
|
|
|
* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
|
2002-10-16 07:22:20 +08:00
|
|
|
* can be found in the file "build/LICENSE-2.0.1" in this distribution
|
|
|
|
* of OpenLDAP Software.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "ldap-int.h"
|
1999-03-04 00:38:56 +08:00
|
|
|
#include "lber.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
|
1998-10-25 11:13:43 +08:00
|
|
|
static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
|
2005-08-02 07:35:44 +08:00
|
|
|
static void ldap_free_request_int LDAP_P(( LDAP *ld, LDAPRequest *lr ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
static BerElement *
|
|
|
|
re_encode_request( LDAP *ld,
|
1999-06-19 07:53:05 +08:00
|
|
|
BerElement *origber,
|
2000-11-29 03:56:55 +08:00
|
|
|
ber_int_t msgid,
|
2001-01-11 07:48:24 +08:00
|
|
|
int sref,
|
2000-11-29 03:56:55 +08:00
|
|
|
LDAPURLDesc *srv,
|
|
|
|
int *type );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
BerElement *
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_alloc_ber_with_options( LDAP *ld )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
|
2006-05-18 08:37:56 +08:00
|
|
|
ber = ber_alloc_t( ld->ld_lberoptions );
|
|
|
|
if ( ber == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ber );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_set_ber_options( LDAP *ld, BerElement *ber )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
ber->ber_options = ld->ld_lberoptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_send_initial_request(
|
|
|
|
LDAP *ld,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t msgtype,
|
1999-05-19 09:12:33 +08:00
|
|
|
const char *dn,
|
2003-02-14 07:02:31 +08:00
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t msgid)
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2006-05-12 14:33:27 +08:00
|
|
|
int rc = 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2006-05-12 14:33:27 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
2000-06-02 04:59:21 +08:00
|
|
|
if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
|
1999-01-31 09:09:00 +08:00
|
|
|
/* not connected yet */
|
2006-05-12 14:33:27 +08:00
|
|
|
rc = ldap_open_defconn( ld );
|
1999-01-31 09:09:00 +08:00
|
|
|
|
2006-05-12 14:33:27 +08:00
|
|
|
}
|
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
|
|
|
if( rc < 0 ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return( -1 );
|
|
|
|
} else if ( rc == 0 ) {
|
1999-01-31 09:09:00 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
2001-06-25 15:33:42 +08:00
|
|
|
"ldap_open_defconn: successful\n",
|
|
|
|
0, 0, 0 );
|
1999-01-31 09:09:00 +08:00
|
|
|
}
|
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
|
|
|
if (LDAP_IS_UDP(ld)) {
|
|
|
|
if (msgtype == LDAP_REQ_BIND) {
|
|
|
|
if (ld->ld_options.ldo_cldapdn)
|
|
|
|
ldap_memfree(ld->ld_options.ldo_cldapdn);
|
|
|
|
ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
|
|
|
|
return LDAP_PARAM_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
|
|
|
rc = ldap_send_server_request( ld, ber, msgid, NULL,
|
2006-04-01 05:07:53 +08:00
|
|
|
NULL, NULL, NULL );
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
1999-12-08 02:52:05 +08:00
|
|
|
return(rc);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-29 20:12:36 +08:00
|
|
|
int
|
|
|
|
ldap_int_flush_request(
|
|
|
|
LDAP *ld,
|
2004-07-10 01:50:22 +08:00
|
|
|
LDAPRequest *lr )
|
2002-08-29 20:12:36 +08:00
|
|
|
{
|
|
|
|
LDAPConn *lc = lr->lr_conn;
|
|
|
|
|
2006-04-10 06:29:42 +08:00
|
|
|
if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
|
2006-09-14 14:35:34 +08:00
|
|
|
if ( sock_errno() == EAGAIN ) {
|
2002-08-29 20:12:36 +08:00
|
|
|
/* need to continue write later */
|
|
|
|
lr->lr_status = LDAP_REQST_WRITING;
|
|
|
|
ldap_mark_select_write( ld, lc->lconn_sb );
|
|
|
|
ld->ld_errno = LDAP_BUSY;
|
|
|
|
return -2;
|
|
|
|
} else {
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
ldap_free_request( ld, lr );
|
|
|
|
ldap_free_connection( ld, lc, 0, 0 );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( lr->lr_parent == NULL ) {
|
|
|
|
lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
|
|
|
|
lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
|
|
|
|
}
|
|
|
|
lr->lr_status = LDAP_REQST_INPROGRESS;
|
|
|
|
|
|
|
|
/* sent -- waiting for a response */
|
|
|
|
ldap_mark_select_read( ld, lc->lconn_sb );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ldap_send_server_request(
|
|
|
|
LDAP *ld,
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t msgid,
|
|
|
|
LDAPRequest *parentreq,
|
2006-05-07 00:15:25 +08:00
|
|
|
LDAPURLDesc **srvlist,
|
1999-06-19 07:53:05 +08:00
|
|
|
LDAPConn *lc,
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAPreqinfo *bind )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPRequest *lr;
|
2006-04-08 19:01:19 +08:00
|
|
|
int incparent, rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 10:56:52 +08:00
|
|
|
incparent = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_SUCCESS; /* optimistic */
|
|
|
|
|
|
|
|
if ( lc == NULL ) {
|
|
|
|
if ( srvlist == NULL ) {
|
|
|
|
lc = ld->ld_defconn;
|
|
|
|
} else {
|
2006-05-07 00:15:25 +08:00
|
|
|
lc = find_connection( ld, *srvlist, 1 );
|
2005-08-11 20:13:43 +08:00
|
|
|
if ( lc == NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
if ( (bind != NULL) && (parentreq != NULL) ) {
|
1998-08-09 10:56:52 +08:00
|
|
|
/* Remember the bind in the parent */
|
|
|
|
incparent = 1;
|
|
|
|
++parentreq->lr_outrefcnt;
|
|
|
|
}
|
1999-12-08 02:52:05 +08:00
|
|
|
lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-08 03:20:46 +08:00
|
|
|
/* async connect... */
|
|
|
|
if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
|
|
|
|
ber_socket_t sd = AC_SOCKET_ERROR;
|
|
|
|
struct timeval tv = { 0 };
|
|
|
|
|
|
|
|
ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
|
|
|
|
|
|
|
|
/* poll ... */
|
|
|
|
switch ( ldap_int_poll( ld, sd, &tv ) ) {
|
|
|
|
case 0:
|
|
|
|
/* go on! */
|
|
|
|
lc->lconn_status = LDAP_CONNST_CONNECTED;
|
|
|
|
break;
|
|
|
|
|
2007-02-06 04:32:44 +08:00
|
|
|
case -2:
|
2007-01-08 06:05:33 +08:00
|
|
|
/* async only occurs if a network timeout is set */
|
2007-02-06 04:32:44 +08:00
|
|
|
|
|
|
|
/* honor network timeout */
|
|
|
|
if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
|
|
|
|
{
|
|
|
|
/* caller will have to call again */
|
|
|
|
ld->ld_errno = LDAP_X_CONNECTING;
|
|
|
|
}
|
|
|
|
/* fallthru */
|
2007-01-08 03:20:46 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* error */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
|
|
|
|
if ( ld->ld_errno == LDAP_SUCCESS ) {
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
}
|
2007-01-08 03:20:46 +08:00
|
|
|
|
|
|
|
ber_free( ber, 1 );
|
1998-08-09 10:56:52 +08:00
|
|
|
if ( incparent ) {
|
|
|
|
/* Forget about the bind */
|
|
|
|
--parentreq->lr_outrefcnt;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
use_connection( ld, lc );
|
2002-08-29 20:12:36 +08:00
|
|
|
|
|
|
|
/* If we still have an incomplete write, try to finish it before
|
|
|
|
* dealing with the new request. If we don't finish here, return
|
|
|
|
* LDAP_BUSY and let the caller retry later. We only allow a single
|
|
|
|
* request to be in WRITING state.
|
|
|
|
*/
|
2003-02-13 18:43:16 +08:00
|
|
|
rc = 0;
|
2002-08-29 20:12:36 +08:00
|
|
|
if ( ld->ld_requests &&
|
|
|
|
ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
|
2005-08-02 07:35:44 +08:00
|
|
|
ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
|
|
|
|
{
|
2003-02-13 18:43:16 +08:00
|
|
|
rc = -1;
|
2002-08-29 20:12:36 +08:00
|
|
|
}
|
2003-02-13 18:43:16 +08:00
|
|
|
if ( rc ) return rc;
|
2002-08-29 20:12:36 +08:00
|
|
|
|
2006-05-18 08:37:56 +08:00
|
|
|
lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( lr == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_connection( ld, lc, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ber, 1 );
|
1998-08-09 10:56:52 +08:00
|
|
|
if ( incparent ) {
|
|
|
|
/* Forget about the bind */
|
|
|
|
--parentreq->lr_outrefcnt;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
lr->lr_msgid = msgid;
|
|
|
|
lr->lr_status = LDAP_REQST_INPROGRESS;
|
|
|
|
lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
|
|
|
|
lr->lr_ber = ber;
|
|
|
|
lr->lr_conn = lc;
|
|
|
|
if ( parentreq != NULL ) { /* sub-request */
|
1998-08-09 10:56:52 +08:00
|
|
|
if ( !incparent ) {
|
|
|
|
/* Increment if we didn't do it before the bind */
|
|
|
|
++parentreq->lr_outrefcnt;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
lr->lr_origid = parentreq->lr_origid;
|
2005-08-02 19:24:52 +08:00
|
|
|
lr->lr_parentcnt = ++parentreq->lr_parentcnt;
|
1998-08-09 08:43:13 +08:00
|
|
|
lr->lr_parent = parentreq;
|
2001-09-25 02:50:44 +08:00
|
|
|
lr->lr_refnext = parentreq->lr_child;
|
|
|
|
parentreq->lr_child = lr;
|
1998-08-09 08:43:13 +08:00
|
|
|
} else { /* original request */
|
|
|
|
lr->lr_origid = lr->lr_msgid;
|
|
|
|
}
|
|
|
|
|
2005-10-09 18:01:38 +08:00
|
|
|
/* Extract requestDN for future reference */
|
|
|
|
{
|
|
|
|
BerElement tmpber = *ber;
|
|
|
|
ber_int_t bint;
|
|
|
|
ber_tag_t tag, rtag;
|
|
|
|
|
|
|
|
ber_reset( &tmpber, 1 );
|
|
|
|
rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
|
|
|
|
switch ( tag ) {
|
|
|
|
case LDAP_REQ_BIND:
|
|
|
|
rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
|
|
|
|
break;
|
|
|
|
case LDAP_REQ_DELETE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rtag = ber_scanf( &tmpber, "{" /*}*/ );
|
|
|
|
case LDAP_REQ_ABANDON:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( tag != LDAP_REQ_ABANDON ) {
|
|
|
|
ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
|
|
|
|
lr->lr_dn.bv_val = tmpber.ber_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-31 00:26:25 +08:00
|
|
|
lr->lr_prev = NULL;
|
2006-09-01 22:00:58 +08:00
|
|
|
lr->lr_next = ld->ld_requests;
|
|
|
|
if ( lr->lr_next != NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
lr->lr_next->lr_prev = lr;
|
|
|
|
}
|
|
|
|
ld->ld_requests = lr;
|
|
|
|
|
2003-02-13 18:43:16 +08:00
|
|
|
ld->ld_errno = LDAP_SUCCESS;
|
2002-08-29 20:12:36 +08:00
|
|
|
if ( ldap_int_flush_request( ld, lr ) == -1 ) {
|
2003-02-13 18:43:16 +08:00
|
|
|
msgid = -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return( msgid );
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAPConn *
|
2006-05-07 00:15:25 +08:00
|
|
|
ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
|
2000-06-01 01:34:43 +08:00
|
|
|
int connect, LDAPreqinfo *bind )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPConn *lc;
|
2007-01-08 03:20:46 +08:00
|
|
|
int async = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
|
|
|
|
use_ldsb, connect, (bind != NULL) );
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* make a new LDAP server connection
|
|
|
|
* XXX open connection synchronously for now
|
|
|
|
*/
|
2005-08-02 07:35:44 +08:00
|
|
|
lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
|
|
|
|
if ( lc == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
2005-08-02 07:35:44 +08:00
|
|
|
|
|
|
|
if ( use_ldsb ) {
|
2005-08-02 18:57:42 +08:00
|
|
|
assert( ld->ld_sb != NULL );
|
2005-08-02 07:35:44 +08:00
|
|
|
lc->lconn_sb = ld->ld_sb;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
} else {
|
|
|
|
lc->lconn_sb = ber_sockbuf_alloc();
|
|
|
|
if ( lc->lconn_sb == NULL ) {
|
|
|
|
LDAP_FREE( (char *)lc );
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( connect ) {
|
2006-05-07 00:15:25 +08:00
|
|
|
LDAPURLDesc **srvp, *srv = NULL;
|
|
|
|
|
2007-01-08 03:20:46 +08:00
|
|
|
async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
|
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
|
2007-02-22 06:26:26 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ldap_int_open_connection( ld, lc, *srvp, async );
|
|
|
|
if ( rc != -1 ) {
|
2006-05-07 00:15:25 +08:00
|
|
|
srv = *srvp;
|
|
|
|
|
2007-02-22 06:26:26 +08:00
|
|
|
if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
|
2006-05-07 00:15:25 +08:00
|
|
|
ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( srv == NULL ) {
|
1999-12-08 02:52:05 +08:00
|
|
|
if ( !use_ldsb ) {
|
|
|
|
ber_sockbuf_free( lc->lconn_sb );
|
|
|
|
}
|
2005-08-02 07:35:44 +08:00
|
|
|
LDAP_FREE( (char *)lc );
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
return( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
lc->lconn_server = ldap_url_dup( srv );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2007-01-08 03:20:46 +08:00
|
|
|
lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
lc->lconn_next = ld->ld_conns;
|
|
|
|
ld->ld_conns = lc;
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
if ( bind != NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
int err = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
LDAPConn *savedefconn;
|
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
/* Set flag to prevent additional referrals
|
|
|
|
* from being processed on this
|
2000-06-01 01:34:43 +08:00
|
|
|
* connection until the bind has completed
|
|
|
|
*/
|
|
|
|
lc->lconn_rebind_inprogress = 1;
|
|
|
|
/* V3 rebind function */
|
2001-06-15 08:14:56 +08:00
|
|
|
if ( ld->ld_rebind_proc != NULL) {
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAPURLDesc *srvfunc;
|
2005-08-11 20:13:43 +08:00
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
srvfunc = ldap_url_dup( *srvlist );
|
2005-08-11 20:13:43 +08:00
|
|
|
if ( srvfunc == NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
err = -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
2000-06-01 01:34:43 +08:00
|
|
|
savedefconn = ld->ld_defconn;
|
|
|
|
++lc->lconn_refcnt; /* avoid premature free */
|
|
|
|
ld->ld_defconn = lc;
|
|
|
|
|
2001-06-15 08:14:56 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
|
2003-03-06 22:53:01 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2005-08-02 07:35:44 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
|
2002-03-30 01:46:28 +08:00
|
|
|
#endif
|
2001-06-15 08:14:56 +08:00
|
|
|
err = (*ld->ld_rebind_proc)( ld,
|
|
|
|
bind->ri_url, bind->ri_request, bind->ri_msgid,
|
2001-06-15 12:49:38 +08:00
|
|
|
ld->ld_rebind_params );
|
2003-03-06 22:53:01 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2005-08-02 07:35:44 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
2003-03-06 22:53:01 +08:00
|
|
|
#endif
|
2000-06-01 01:34:43 +08:00
|
|
|
|
|
|
|
ld->ld_defconn = savedefconn;
|
|
|
|
--lc->lconn_refcnt;
|
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( err != 0 ) {
|
|
|
|
err = -1;
|
2000-06-01 01:34:43 +08:00
|
|
|
ldap_free_connection( ld, lc, 1, 0 );
|
|
|
|
lc = NULL;
|
2005-08-02 07:35:44 +08:00
|
|
|
}
|
|
|
|
ldap_free_urldesc( srvfunc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-09-01 18:01:36 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
} else {
|
2006-03-25 16:16:19 +08:00
|
|
|
int msgid, rc;
|
|
|
|
struct berval passwd = BER_BVNULL;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
savedefconn = ld->ld_defconn;
|
|
|
|
++lc->lconn_refcnt; /* avoid premature free */
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_defconn = lc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
|
2003-03-06 22:53:01 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
|
2002-03-30 01:46:28 +08:00
|
|
|
#endif
|
2006-03-25 16:16:19 +08:00
|
|
|
rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
|
|
|
|
NULL, NULL, &msgid );
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
err = -1;
|
2006-03-25 16:16:19 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
for ( err = 1; err > 0; ) {
|
|
|
|
struct timeval tv = { 0, 100000 };
|
|
|
|
LDAPMessage *res = NULL;
|
|
|
|
|
|
|
|
switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
|
|
|
|
case -1:
|
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_yield();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_RES_BIND:
|
|
|
|
rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
err = -1;
|
|
|
|
|
|
|
|
} else if ( err != LDAP_SUCCESS ) {
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
/* else err == LDAP_SUCCESS == 0 */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2003-03-06 22:53:01 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_defconn = savedefconn;
|
2000-06-01 01:34:43 +08:00
|
|
|
--lc->lconn_refcnt;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( err != 0 ) {
|
|
|
|
ldap_free_connection( ld, lc, 1, 0 );
|
|
|
|
lc = NULL;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( lc != NULL )
|
2000-06-01 01:34:43 +08:00
|
|
|
lc->lconn_rebind_inprogress = 0;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( lc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static LDAPConn *
|
1999-12-08 02:52:05 +08:00
|
|
|
find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* return an existing connection (if any) to the server srv
|
|
|
|
* if "any" is non-zero, check for any server in the "srv" chain
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
LDAPConn *lc;
|
2004-08-28 11:25:59 +08:00
|
|
|
LDAPURLDesc *lcu, *lsu;
|
|
|
|
int lcu_port, lsu_port;
|
2005-10-12 08:05:43 +08:00
|
|
|
int found = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
|
2004-08-28 11:25:59 +08:00
|
|
|
lcu = lc->lconn_server;
|
2004-08-28 11:53:31 +08:00
|
|
|
lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
|
|
|
|
lcu->lud_port );
|
|
|
|
|
2004-08-28 11:25:59 +08:00
|
|
|
for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
|
2004-08-28 11:53:31 +08:00
|
|
|
lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
|
|
|
|
lsu->lud_port );
|
|
|
|
|
2005-10-12 08:05:43 +08:00
|
|
|
if ( lsu_port == lcu_port
|
|
|
|
&& strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
|
2004-08-28 11:55:02 +08:00
|
|
|
&& lcu->lud_host != NULL && *lcu->lud_host != '\0'
|
2006-05-18 08:37:56 +08:00
|
|
|
&& lsu->lud_host != NULL && *lsu->lud_host != '\0'
|
2005-10-12 08:05:43 +08:00
|
|
|
&& strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
|
2004-08-28 11:55:02 +08:00
|
|
|
{
|
2005-10-12 08:05:43 +08:00
|
|
|
found = 1;
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2004-08-28 11:25:59 +08:00
|
|
|
|
|
|
|
if ( !any ) break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2005-10-12 08:05:43 +08:00
|
|
|
if ( found )
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
|
|
|
|
#endif
|
|
|
|
return lc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
use_connection( LDAP *ld, LDAPConn *lc )
|
|
|
|
{
|
|
|
|
++lc->lconn_refcnt;
|
1999-07-20 03:28:21 +08:00
|
|
|
lc->lconn_lastused = time( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPConn *tmplc, *prevlc;
|
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ldap_free_connection %d %d\n",
|
|
|
|
force, unbind, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( force || --lc->lconn_refcnt <= 0 ) {
|
2006-04-08 19:01:19 +08:00
|
|
|
/* remove from connections list first */
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2006-04-08 19:01:19 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
|
2005-10-12 08:05:43 +08:00
|
|
|
#endif
|
2006-04-08 19:01:19 +08:00
|
|
|
|
|
|
|
for ( prevlc = NULL, tmplc = ld->ld_conns;
|
2005-08-04 00:54:08 +08:00
|
|
|
tmplc != NULL;
|
|
|
|
tmplc = tmplc->lconn_next )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( tmplc == lc ) {
|
|
|
|
if ( prevlc == NULL ) {
|
|
|
|
ld->ld_conns = tmplc->lconn_next;
|
|
|
|
} else {
|
|
|
|
prevlc->lconn_next = tmplc->lconn_next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1998-08-09 10:56:52 +08:00
|
|
|
prevlc = tmplc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2005-10-12 08:05:43 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2006-04-08 19:01:19 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
|
2005-10-12 08:05:43 +08:00
|
|
|
#endif
|
2006-04-08 19:01:19 +08:00
|
|
|
|
|
|
|
if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
|
|
|
|
ldap_mark_select_clear( ld, lc->lconn_sb );
|
|
|
|
if ( unbind ) {
|
|
|
|
ldap_send_unbind( ld, lc->lconn_sb,
|
|
|
|
NULL, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lc->lconn_ber != NULL ) {
|
|
|
|
ber_free( lc->lconn_ber, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_int_sasl_close( ld, lc );
|
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
ldap_free_urllist( lc->lconn_server );
|
2005-08-02 07:35:44 +08:00
|
|
|
|
2006-09-01 18:01:36 +08:00
|
|
|
/* FIXME: is this at all possible?
|
|
|
|
* ldap_ld_free() in unbind.c calls ldap_free_connection()
|
|
|
|
* with force == 1 __after__ explicitly calling
|
|
|
|
* ldap_free_request() on all requests */
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( force ) {
|
|
|
|
LDAPRequest *lr;
|
|
|
|
|
|
|
|
for ( lr = ld->ld_requests; lr; ) {
|
|
|
|
LDAPRequest *lr_next = lr->lr_next;
|
|
|
|
|
|
|
|
if ( lr->lr_conn == lc ) {
|
|
|
|
ldap_free_request_int( ld, lr );
|
|
|
|
}
|
|
|
|
|
|
|
|
lr = lr_next;
|
|
|
|
}
|
|
|
|
}
|
2006-04-08 19:01:19 +08:00
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
if ( lc->lconn_sb != ld->ld_sb ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_sockbuf_free( lc->lconn_sb );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-04-08 19:01:19 +08:00
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
if ( lc->lconn_rebind_queue != NULL) {
|
2000-06-01 01:34:43 +08:00
|
|
|
int i;
|
2005-08-02 07:35:44 +08:00
|
|
|
for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
|
|
|
|
LDAP_VFREE( lc->lconn_rebind_queue[i] );
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
2005-08-02 07:35:44 +08:00
|
|
|
LDAP_FREE( lc->lconn_rebind_queue );
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
2006-04-08 19:01:19 +08:00
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lc );
|
2006-04-08 19:01:19 +08:00
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ldap_free_connection: actually freed\n",
|
|
|
|
0, 0, 0 );
|
2006-04-08 19:01:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
1999-07-20 03:28:21 +08:00
|
|
|
lc->lconn_lastused = time( NULL );
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
|
2005-08-02 07:35:44 +08:00
|
|
|
lc->lconn_refcnt, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPConn *lc;
|
1998-12-19 09:27:20 +08:00
|
|
|
char timebuf[32];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
|
|
|
|
if ( lc->lconn_server != NULL ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
|
2006-05-18 08:37:56 +08:00
|
|
|
( lc->lconn_server->lud_host == NULL ) ? "(null)"
|
|
|
|
: lc->lconn_server->lud_host,
|
|
|
|
lc->lconn_server->lud_port, ( lc->lconn_sb ==
|
|
|
|
ld->ld_sb ) ? " (default)" : "" );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
|
|
|
|
( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
|
2007-01-08 03:20:46 +08:00
|
|
|
? "NeedSocket" :
|
|
|
|
( lc->lconn_status == LDAP_CONNST_CONNECTING )
|
|
|
|
? "Connecting" : "Connected", 0 );
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
|
|
|
|
ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
|
|
|
|
lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
|
|
|
|
if ( lc->lconn_rebind_inprogress ) {
|
|
|
|
if ( lc->lconn_rebind_queue != NULL) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
|
|
|
|
int j;
|
|
|
|
for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
|
|
|
|
i, j, lc->lconn_rebind_queue[i][j] );
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
|
|
|
}
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( !all ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_dump_requests_and_responses( LDAP *ld )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPRequest *lr;
|
|
|
|
LDAPMessage *lm, *l;
|
2006-05-27 18:17:34 +08:00
|
|
|
int i;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
|
|
|
|
(void *)ld, 0, 0 );
|
2006-05-27 18:17:34 +08:00
|
|
|
lr = ld->ld_requests;
|
|
|
|
if ( lr == NULL ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-05-27 18:17:34 +08:00
|
|
|
for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
|
2006-05-18 08:37:56 +08:00
|
|
|
lr->lr_msgid, lr->lr_origid,
|
|
|
|
( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
|
|
|
|
( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
|
|
|
|
( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
|
|
|
|
( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
|
|
|
|
( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
|
2006-05-18 08:59:13 +08:00
|
|
|
: "InvalidStatus" );
|
|
|
|
Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
|
|
|
|
lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-12-04 08:11:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
|
2006-11-03 03:11:18 +08:00
|
|
|
(void *)ld, i, ld->ld_nabandoned );
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
|
2006-05-18 08:37:56 +08:00
|
|
|
if ( ( lm = ld->ld_responses ) == NULL ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2006-05-27 18:17:34 +08:00
|
|
|
for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
|
|
|
|
lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
|
2006-11-12 22:22:24 +08:00
|
|
|
if ( lm->lm_chain != NULL ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
|
2006-11-12 22:22:24 +08:00
|
|
|
for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
|
2006-05-18 08:59:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
2006-05-18 08:37:56 +08:00
|
|
|
" * msgid %d, type %lu\n",
|
|
|
|
l->lm_msgid,
|
2006-05-18 08:59:13 +08:00
|
|
|
(unsigned long)l->lm_msgtype, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-27 18:17:34 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
2005-07-31 00:26:25 +08:00
|
|
|
static void
|
2001-09-25 02:50:44 +08:00
|
|
|
ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2006-09-01 22:00:58 +08:00
|
|
|
/* if lr_refcnt > 0, the request has been looked up
|
|
|
|
* by ldap_find_request_by_msgid(); if in the meanwhile
|
|
|
|
* the request is free()'d by someone else, just decrease
|
|
|
|
* the reference count and extract it from the request
|
|
|
|
* list; later on, it will be freed. */
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( lr->lr_prev == NULL ) {
|
2006-09-01 22:00:58 +08:00
|
|
|
if ( lr->lr_refcnt == 0 ) {
|
|
|
|
/* free'ing the first request? */
|
|
|
|
assert( ld->ld_requests == lr );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_requests == lr ) {
|
|
|
|
ld->ld_requests = lr->lr_next;
|
|
|
|
}
|
2005-07-24 06:04:04 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
lr->lr_prev->lr_next = lr->lr_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_next != NULL ) {
|
|
|
|
lr->lr_next->lr_prev = lr->lr_prev;
|
|
|
|
}
|
|
|
|
|
2006-09-01 22:00:58 +08:00
|
|
|
if ( lr->lr_refcnt > 0 ) {
|
|
|
|
lr->lr_refcnt = -lr->lr_refcnt;
|
|
|
|
|
|
|
|
lr->lr_prev = NULL;
|
|
|
|
lr->lr_next = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( lr->lr_ber != NULL ) {
|
|
|
|
ber_free( lr->lr_ber, 1 );
|
2006-04-24 20:12:14 +08:00
|
|
|
lr->lr_ber = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_res_error != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lr->lr_res_error );
|
2006-04-24 20:12:14 +08:00
|
|
|
lr->lr_res_error = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_res_matched != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lr->lr_res_matched );
|
2006-04-24 20:12:14 +08:00
|
|
|
lr->lr_res_matched = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lr );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-09-25 02:50:44 +08:00
|
|
|
void
|
|
|
|
ldap_free_request( LDAP *ld, LDAPRequest *lr )
|
|
|
|
{
|
2006-04-08 19:01:19 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
|
|
|
|
#endif
|
2001-09-25 02:50:44 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
|
|
|
|
lr->lr_origid, lr->lr_msgid, 0 );
|
|
|
|
|
2002-11-27 11:57:36 +08:00
|
|
|
/* free all referrals (child requests) */
|
2006-11-12 22:22:24 +08:00
|
|
|
while ( lr->lr_child ) {
|
2002-11-27 11:57:36 +08:00
|
|
|
ldap_free_request( ld, lr->lr_child );
|
2006-11-12 22:22:24 +08:00
|
|
|
}
|
2002-11-27 11:57:36 +08:00
|
|
|
|
2001-09-25 02:50:44 +08:00
|
|
|
if ( lr->lr_parent != NULL ) {
|
2006-11-12 22:22:24 +08:00
|
|
|
LDAPRequest **lrp;
|
|
|
|
|
2001-09-25 02:50:44 +08:00
|
|
|
--lr->lr_parent->lr_outrefcnt;
|
2006-11-12 22:22:24 +08:00
|
|
|
for ( lrp = &lr->lr_parent->lr_child;
|
|
|
|
*lrp && *lrp != lr;
|
|
|
|
lrp = &(*lrp)->lr_refnext );
|
|
|
|
|
|
|
|
if ( *lrp == lr ) {
|
|
|
|
*lrp = lr->lr_refnext;
|
|
|
|
}
|
2001-09-25 02:50:44 +08:00
|
|
|
}
|
|
|
|
ldap_free_request_int( ld, lr );
|
|
|
|
}
|
|
|
|
|
2005-05-28 22:29:08 +08:00
|
|
|
/*
|
|
|
|
* call first time with *cntp = -1
|
|
|
|
* when returns *cntp == -1, no referrals are left
|
|
|
|
*
|
|
|
|
* NOTE: may replace *refsp, or shuffle the contents
|
|
|
|
* of the original array.
|
|
|
|
*/
|
|
|
|
static int ldap_int_nextref(
|
|
|
|
LDAP *ld,
|
|
|
|
char ***refsp,
|
|
|
|
int *cntp,
|
|
|
|
void *params )
|
|
|
|
{
|
|
|
|
assert( refsp != NULL );
|
|
|
|
assert( *refsp != NULL );
|
|
|
|
assert( cntp != NULL );
|
|
|
|
|
|
|
|
if ( *cntp < -1 ) {
|
|
|
|
*cntp = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*cntp)++;
|
|
|
|
|
|
|
|
if ( (*refsp)[ *cntp ] == NULL ) {
|
|
|
|
*cntp = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-25 02:50:44 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/*
|
|
|
|
* Chase v3 referrals
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (IN) ld = LDAP connection handle
|
|
|
|
* (IN) lr = LDAP Request structure
|
|
|
|
* (IN) refs = array of pointers to referral strings that we will chase
|
|
|
|
* The array will be free'd by this function when no longer needed
|
2000-10-11 12:23:23 +08:00
|
|
|
* (IN) sref != 0 if following search reference
|
2000-06-01 01:34:43 +08:00
|
|
|
* (OUT) errstrp = Place to return a string of referrals which could not be followed
|
|
|
|
* (OUT) hadrefp = 1 if sucessfully followed referral
|
|
|
|
*
|
|
|
|
* Return value - number of referrals followed
|
|
|
|
*/
|
2000-06-19 03:48:07 +08:00
|
|
|
int
|
2000-10-11 12:23:23 +08:00
|
|
|
ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
|
2000-06-01 01:34:43 +08:00
|
|
|
{
|
|
|
|
char *unfollowed;
|
2005-08-02 07:35:44 +08:00
|
|
|
int unfollowedcnt = 0;
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAPRequest *origreq;
|
|
|
|
LDAPURLDesc *srv = NULL;
|
|
|
|
BerElement *ber;
|
|
|
|
char **refarray = NULL;
|
|
|
|
LDAPConn *lc;
|
2003-03-06 22:53:01 +08:00
|
|
|
int rc, count, i, j, id;
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAPreqinfo rinfo;
|
|
|
|
|
|
|
|
ld->ld_errno = LDAP_SUCCESS; /* optimistic */
|
|
|
|
*hadrefp = 0;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
unfollowed = NULL;
|
|
|
|
rc = count = 0;
|
|
|
|
|
|
|
|
/* If no referrals in array, return */
|
|
|
|
if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
|
|
|
|
rc = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for hop limit exceeded */
|
|
|
|
if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
|
|
|
|
ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
|
2006-04-08 19:01:19 +08:00
|
|
|
rc = -1;
|
2000-06-01 01:34:43 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find original request */
|
2001-01-20 09:17:15 +08:00
|
|
|
for ( origreq = lr;
|
|
|
|
origreq->lr_parent != NULL;
|
|
|
|
origreq = origreq->lr_parent )
|
|
|
|
{
|
|
|
|
/* empty */ ;
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
refarray = refs;
|
|
|
|
refs = NULL;
|
2005-05-28 22:29:08 +08:00
|
|
|
|
|
|
|
if ( ld->ld_nextref_proc == NULL ) {
|
|
|
|
ld->ld_nextref_proc = ldap_int_nextref;
|
|
|
|
}
|
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/* parse out & follow referrals */
|
2005-05-28 22:29:08 +08:00
|
|
|
i = -1;
|
|
|
|
for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
|
|
|
|
i != -1;
|
|
|
|
ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
|
|
|
|
{
|
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/* Parse the referral URL */
|
2006-05-07 00:15:25 +08:00
|
|
|
rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
|
2006-04-24 20:12:14 +08:00
|
|
|
if ( rc != LDAP_URL_SUCCESS ) {
|
|
|
|
/* ldap_url_parse_ext() returns LDAP_URL_* errors
|
|
|
|
* which do not map on API errors */
|
|
|
|
ld->ld_errno = LDAP_PARAM_ERROR;
|
2000-06-01 01:34:43 +08:00
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2000-10-26 14:17:14 +08:00
|
|
|
if( srv->lud_crit_exts ) {
|
|
|
|
/* we do not support any extensions */
|
|
|
|
ld->ld_errno = LDAP_NOT_SUPPORTED;
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/* check connection for re-bind in progress */
|
|
|
|
if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
|
2005-10-09 18:01:38 +08:00
|
|
|
/* See if we've already requested this DN with this conn */
|
|
|
|
LDAPRequest *lp;
|
|
|
|
int looped = 0;
|
|
|
|
int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
|
2006-04-03 08:34:35 +08:00
|
|
|
for ( lp = origreq; lp; ) {
|
2006-04-08 19:01:19 +08:00
|
|
|
if ( lp->lr_conn == lc
|
|
|
|
&& len == lp->lr_dn.bv_len
|
|
|
|
&& len
|
|
|
|
&& strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
|
|
|
|
{
|
|
|
|
looped = 1;
|
|
|
|
break;
|
2005-10-09 18:01:38 +08:00
|
|
|
}
|
2006-04-03 08:34:35 +08:00
|
|
|
if ( lp == origreq ) {
|
2005-10-09 18:01:38 +08:00
|
|
|
lp = lp->lr_child;
|
2006-04-03 08:34:35 +08:00
|
|
|
} else {
|
2005-10-09 18:01:38 +08:00
|
|
|
lp = lr->lr_refnext;
|
2006-04-03 08:34:35 +08:00
|
|
|
}
|
2005-10-09 18:01:38 +08:00
|
|
|
}
|
|
|
|
if ( looped ) {
|
|
|
|
ldap_free_urllist( srv );
|
|
|
|
srv = NULL;
|
|
|
|
ld->ld_errno = LDAP_CLIENT_LOOP;
|
|
|
|
rc = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-04-03 08:34:35 +08:00
|
|
|
if ( lc->lconn_rebind_inprogress ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
/* We are already chasing a referral or search reference and a
|
|
|
|
* bind on that connection is in progress. We must queue
|
|
|
|
* referrals on that connection, so we don't get a request
|
|
|
|
* going out before the bind operation completes. This happens
|
|
|
|
* if two search references come in one behind the other
|
|
|
|
* for the same server with different contexts.
|
|
|
|
*/
|
2001-01-11 07:48:24 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ldap_chase_v3referrals: queue referral \"%s\"\n",
|
2000-06-01 01:34:43 +08:00
|
|
|
refarray[i], 0, 0);
|
|
|
|
if( lc->lconn_rebind_queue == NULL ) {
|
|
|
|
/* Create a referral list */
|
2001-01-20 09:17:15 +08:00
|
|
|
lc->lconn_rebind_queue =
|
2001-01-22 15:42:13 +08:00
|
|
|
(char ***) LDAP_MALLOC( sizeof(void *) * 2);
|
2001-01-20 09:17:15 +08:00
|
|
|
|
|
|
|
if( lc->lconn_rebind_queue == NULL) {
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
2001-01-20 09:17:15 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
lc->lconn_rebind_queue[0] = refarray;
|
|
|
|
lc->lconn_rebind_queue[1] = NULL;
|
|
|
|
refarray = NULL;
|
2001-01-20 09:17:15 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
} else {
|
|
|
|
/* Count how many referral arrays we already have */
|
|
|
|
for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
|
2001-01-20 09:17:15 +08:00
|
|
|
/* empty */;
|
2000-06-01 01:34:43 +08:00
|
|
|
}
|
2001-01-20 09:17:15 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/* Add the new referral to the list */
|
2001-01-20 09:17:15 +08:00
|
|
|
lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
|
|
|
|
lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
|
|
|
|
|
|
|
|
if( lc->lconn_rebind_queue == NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
lc->lconn_rebind_queue[j] = refarray;
|
|
|
|
lc->lconn_rebind_queue[j+1] = NULL;
|
|
|
|
refarray = NULL;
|
|
|
|
}
|
2001-01-20 09:17:15 +08:00
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
/* We have queued the referral/reference, now just return */
|
|
|
|
rc = 0;
|
|
|
|
*hadrefp = 1;
|
|
|
|
count = 1; /* Pretend we already followed referral */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Re-encode the request with the new starting point of the search.
|
|
|
|
* Note: In the future we also need to replace the filter if one
|
|
|
|
* was provided with the search reference
|
|
|
|
*/
|
2000-10-11 12:23:23 +08:00
|
|
|
|
|
|
|
/* For references we don't want old dn if new dn empty */
|
2000-11-29 03:56:55 +08:00
|
|
|
if ( sref && srv->lud_dn == NULL ) {
|
2000-10-11 12:23:23 +08:00
|
|
|
srv->lud_dn = LDAP_STRDUP( "" );
|
2000-11-29 03:56:55 +08:00
|
|
|
}
|
2000-10-11 12:23:23 +08:00
|
|
|
|
2003-03-06 22:53:01 +08:00
|
|
|
LDAP_NEXT_MSGID( ld, id );
|
|
|
|
ber = re_encode_request( ld, origreq->lr_ber, id,
|
2001-01-11 07:48:24 +08:00
|
|
|
sref, srv, &rinfo.ri_request );
|
|
|
|
|
|
|
|
if( ber == NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2001-01-11 07:48:24 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ldap_chase_v3referral: msgid %d, url \"%s\"\n",
|
2000-06-01 01:34:43 +08:00
|
|
|
lr->lr_msgid, refarray[i], 0);
|
|
|
|
|
|
|
|
/* Send the new request to the server - may require a bind */
|
|
|
|
rinfo.ri_msgid = origreq->lr_origid;
|
|
|
|
rinfo.ri_url = refarray[i];
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
2003-03-06 22:53:01 +08:00
|
|
|
rc = ldap_send_server_request( ld, ber, id,
|
2006-05-07 00:15:25 +08:00
|
|
|
origreq, &srv, NULL, &rinfo );
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
|
|
|
if ( rc < 0 ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
/* Failure, try next referral in the list */
|
2005-08-12 01:11:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
|
|
|
|
refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
|
|
|
|
unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
|
|
|
|
ldap_free_urllist( srv );
|
2000-06-01 01:34:43 +08:00
|
|
|
srv = NULL;
|
2005-10-09 18:01:38 +08:00
|
|
|
ld->ld_errno = LDAP_REFERRAL;
|
2000-06-01 01:34:43 +08:00
|
|
|
} else {
|
|
|
|
/* Success, no need to try this referral list further */
|
|
|
|
rc = 0;
|
|
|
|
++count;
|
|
|
|
*hadrefp = 1;
|
|
|
|
|
|
|
|
/* check if there is a queue of referrals that came in during bind */
|
2005-08-12 01:11:41 +08:00
|
|
|
if ( lc == NULL) {
|
|
|
|
lc = find_connection( ld, srv, 1 );
|
|
|
|
if ( lc == NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
ld->ld_errno = LDAP_OPERATIONS_ERROR;
|
|
|
|
rc = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-12 01:11:41 +08:00
|
|
|
if ( lc->lconn_rebind_queue != NULL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
/* Release resources of previous list */
|
2005-08-12 01:11:41 +08:00
|
|
|
LDAP_VFREE( refarray );
|
2000-06-01 01:34:43 +08:00
|
|
|
refarray = NULL;
|
2005-08-12 01:11:41 +08:00
|
|
|
ldap_free_urllist( srv );
|
2000-06-01 01:34:43 +08:00
|
|
|
srv = NULL;
|
|
|
|
|
|
|
|
/* Pull entries off end of queue so list always null terminated */
|
2005-08-12 01:11:41 +08:00
|
|
|
for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
|
2000-06-01 01:34:43 +08:00
|
|
|
;
|
2005-08-12 01:11:41 +08:00
|
|
|
refarray = lc->lconn_rebind_queue[j - 1];
|
2000-06-01 01:34:43 +08:00
|
|
|
lc->lconn_rebind_queue[j-1] = NULL;
|
|
|
|
/* we pulled off last entry from queue, free queue */
|
|
|
|
if ( j == 1 ) {
|
2005-08-12 01:11:41 +08:00
|
|
|
LDAP_FREE( lc->lconn_rebind_queue );
|
2000-06-01 01:34:43 +08:00
|
|
|
lc->lconn_rebind_queue = NULL;
|
|
|
|
}
|
|
|
|
/* restart the loop the with new referral list */
|
|
|
|
i = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break; /* referral followed, break out of for loop */
|
|
|
|
}
|
|
|
|
} /* end for loop */
|
|
|
|
done:
|
2005-08-02 07:35:44 +08:00
|
|
|
LDAP_VFREE( refarray );
|
|
|
|
ldap_free_urllist( srv );
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAP_FREE( *errstrp );
|
|
|
|
|
2005-08-02 07:35:44 +08:00
|
|
|
if( rc == 0 ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
*errstrp = NULL;
|
|
|
|
LDAP_FREE( unfollowed );
|
|
|
|
return count;
|
|
|
|
} else {
|
|
|
|
*errstrp = unfollowed;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX merging of errors in this routine needs to be improved
|
|
|
|
*/
|
|
|
|
int
|
2001-01-11 07:48:24 +08:00
|
|
|
ldap_chase_referrals( LDAP *ld,
|
|
|
|
LDAPRequest *lr,
|
|
|
|
char **errstrp,
|
|
|
|
int sref,
|
|
|
|
int *hadrefp )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2003-03-06 22:53:01 +08:00
|
|
|
int rc, count, id;
|
2002-04-11 16:47:37 +08:00
|
|
|
unsigned len;
|
2000-11-29 03:56:55 +08:00
|
|
|
char *p, *ref, *unfollowed;
|
1998-08-09 08:43:13 +08:00
|
|
|
LDAPRequest *origreq;
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
BerElement *ber;
|
2000-06-01 01:34:43 +08:00
|
|
|
LDAPreqinfo rinfo;
|
2005-10-09 18:01:38 +08:00
|
|
|
LDAPConn *lc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
ld->ld_errno = LDAP_SUCCESS; /* optimistic */
|
|
|
|
*hadrefp = 0;
|
|
|
|
|
|
|
|
if ( *errstrp == NULL ) {
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen( *errstrp );
|
|
|
|
for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
|
2000-09-30 10:37:05 +08:00
|
|
|
if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
*p = '\0';
|
|
|
|
p += LDAP_REF_STR_LEN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( len < LDAP_REF_STR_LEN ) {
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"more than %d referral hops (dropping)\n",
|
|
|
|
ld->ld_refhoplimit, 0, 0 );
|
|
|
|
/* XXX report as error in ld->ld_errno? */
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find original request */
|
|
|
|
for ( origreq = lr; origreq->lr_parent != NULL;
|
|
|
|
origreq = origreq->lr_parent ) {
|
2000-11-29 03:56:55 +08:00
|
|
|
/* empty */;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unfollowed = NULL;
|
|
|
|
rc = count = 0;
|
|
|
|
|
|
|
|
/* parse out & follow referrals */
|
|
|
|
for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
|
2005-08-12 01:11:41 +08:00
|
|
|
p = strchr( ref, '\n' );
|
|
|
|
if ( p != NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
|
2000-11-29 03:56:55 +08:00
|
|
|
if ( rc != LDAP_URL_SUCCESS ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
2006-04-24 20:12:14 +08:00
|
|
|
"ignoring %s referral <%s>\n",
|
|
|
|
ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
|
1998-08-09 11:04:12 +08:00
|
|
|
rc = ldap_append_referral( ld, &unfollowed, ref );
|
1998-08-09 08:43:13 +08:00
|
|
|
*hadrefp = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"chasing LDAP referral: <%s>\n", ref, 0, 0 );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
*hadrefp = 1;
|
2000-05-31 02:32:10 +08:00
|
|
|
|
2005-10-09 18:01:38 +08:00
|
|
|
/* See if we've already been here */
|
|
|
|
if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
|
|
|
|
LDAPRequest *lp;
|
|
|
|
int looped = 0;
|
|
|
|
int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
|
2006-05-18 06:13:39 +08:00
|
|
|
for ( lp = lr; lp; lp = lp->lr_parent ) {
|
2006-04-08 19:01:19 +08:00
|
|
|
if ( lp->lr_conn == lc
|
2006-05-18 06:13:39 +08:00
|
|
|
&& len == lp->lr_dn.bv_len )
|
2006-04-08 19:01:19 +08:00
|
|
|
{
|
2006-05-18 06:13:39 +08:00
|
|
|
if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
|
|
|
|
continue;
|
2006-04-08 19:01:19 +08:00
|
|
|
looped = 1;
|
|
|
|
break;
|
2005-10-09 18:01:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( looped ) {
|
2006-12-04 04:56:25 +08:00
|
|
|
ldap_free_urllist( srv );
|
2005-10-09 18:01:38 +08:00
|
|
|
ld->ld_errno = LDAP_CLIENT_LOOP;
|
|
|
|
rc = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 22:53:01 +08:00
|
|
|
LDAP_NEXT_MSGID( ld, id );
|
2000-11-29 03:56:55 +08:00
|
|
|
ber = re_encode_request( ld, origreq->lr_ber,
|
2003-03-06 22:53:01 +08:00
|
|
|
id, sref, srv, &rinfo.ri_request );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-08-12 01:11:41 +08:00
|
|
|
if ( ber == NULL ) {
|
2000-11-29 03:56:55 +08:00
|
|
|
return -1 ;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
/* copy the complete referral for rebind process */
|
|
|
|
rinfo.ri_url = LDAP_STRDUP( ref );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
rinfo.ri_msgid = origreq->lr_origid;
|
2000-06-07 09:09:40 +08:00
|
|
|
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2005-07-24 06:04:04 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
2003-02-14 07:02:31 +08:00
|
|
|
#endif
|
2003-03-06 22:53:01 +08:00
|
|
|
rc = ldap_send_server_request( ld, ber, id,
|
2006-05-07 00:15:25 +08:00
|
|
|
lr, &srv, NULL, &rinfo );
|
2003-02-14 07:02:31 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
2005-07-24 06:04:04 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
2003-02-14 07:02:31 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
LDAP_FREE( rinfo.ri_url );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
if( rc >= 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
++count;
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2005-08-12 01:11:41 +08:00
|
|
|
"Unable to chase referral \"%s\" (%d: %s)\n",
|
|
|
|
ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
|
1998-08-09 11:04:12 +08:00
|
|
|
rc = ldap_append_referral( ld, &unfollowed, ref );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-12-08 02:52:05 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
ldap_free_urllist(srv);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( *errstrp );
|
1998-08-09 08:43:13 +08:00
|
|
|
*errstrp = unfollowed;
|
|
|
|
|
|
|
|
return(( rc == 0 ) ? count : rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_append_referral( LDAP *ld, char **referralsp, char *s )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
int first;
|
|
|
|
|
|
|
|
if ( *referralsp == NULL ) {
|
|
|
|
first = 1;
|
1999-05-29 09:19:14 +08:00
|
|
|
*referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
|
1998-08-09 08:43:13 +08:00
|
|
|
+ 1 );
|
|
|
|
} else {
|
|
|
|
first = 0;
|
1999-05-29 09:19:14 +08:00
|
|
|
*referralsp = (char *)LDAP_REALLOC( *referralsp,
|
1998-08-09 08:43:13 +08:00
|
|
|
strlen( *referralsp ) + strlen( s ) + 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( *referralsp == NULL ) {
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( first ) {
|
|
|
|
strcpy( *referralsp, LDAP_REF_STR );
|
|
|
|
} else {
|
|
|
|
strcat( *referralsp, "\n" );
|
|
|
|
}
|
|
|
|
strcat( *referralsp, s );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static BerElement *
|
2000-11-29 03:56:55 +08:00
|
|
|
re_encode_request( LDAP *ld,
|
|
|
|
BerElement *origber,
|
|
|
|
ber_int_t msgid,
|
2001-01-11 07:48:24 +08:00
|
|
|
int sref,
|
2000-11-29 03:56:55 +08:00
|
|
|
LDAPURLDesc *srv,
|
|
|
|
int *type )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2000-09-30 13:35:44 +08:00
|
|
|
/*
|
|
|
|
* XXX this routine knows way too much about how the lber library works!
|
|
|
|
*/
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t along;
|
|
|
|
ber_tag_t tag;
|
2002-04-03 02:40:04 +08:00
|
|
|
ber_tag_t rtag;
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t ver;
|
2000-11-29 03:56:55 +08:00
|
|
|
ber_int_t scope;
|
1998-08-09 08:43:13 +08:00
|
|
|
int rc;
|
|
|
|
BerElement tmpber, *ber;
|
2006-05-10 23:01:50 +08:00
|
|
|
struct berval dn;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
1999-06-19 07:53:05 +08:00
|
|
|
"re_encode_request: new msgid %ld, new dn <%s>\n",
|
2000-11-29 03:56:55 +08:00
|
|
|
(long) msgid,
|
|
|
|
( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
tmpber = *origber;
|
|
|
|
|
|
|
|
/*
|
2000-03-09 02:56:22 +08:00
|
|
|
* all LDAP requests are sequences that start with a message id.
|
|
|
|
* For all except delete, this is followed by a sequence that is
|
|
|
|
* tagged with the operation code. For delete, the provided DN
|
|
|
|
* is not wrapped by a sequence.
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
2002-04-03 02:40:04 +08:00
|
|
|
rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
|
2000-03-09 02:56:22 +08:00
|
|
|
|
2002-04-03 02:40:04 +08:00
|
|
|
if ( rtag == LBER_ERROR ) {
|
2000-03-09 02:56:22 +08:00
|
|
|
ld->ld_errno = LDAP_DECODING_ERROR;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
assert( tag != 0);
|
2000-03-09 02:56:22 +08:00
|
|
|
if ( tag == LDAP_REQ_BIND ) {
|
|
|
|
/* bind requests have a version number before the DN & other stuff */
|
2006-05-10 23:01:50 +08:00
|
|
|
rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-03-09 02:56:22 +08:00
|
|
|
} else if ( tag == LDAP_REQ_DELETE ) {
|
|
|
|
/* delete requests don't have a DN wrapping sequence */
|
2006-05-10 23:01:50 +08:00
|
|
|
rtag = ber_scanf( &tmpber, "m", &dn );
|
2000-03-09 02:56:22 +08:00
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
} else if ( tag == LDAP_REQ_SEARCH ) {
|
|
|
|
/* search requests need to be re-scope-ed */
|
2006-05-10 23:01:50 +08:00
|
|
|
rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
|
2000-11-29 03:56:55 +08:00
|
|
|
|
2000-12-09 03:45:04 +08:00
|
|
|
if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
|
|
|
|
/* use the scope provided in reference */
|
|
|
|
scope = srv->lud_scope;
|
|
|
|
|
2003-12-18 01:55:27 +08:00
|
|
|
} else if ( sref ) {
|
|
|
|
/* use scope implied by previous operation
|
|
|
|
* base -> base
|
|
|
|
* one -> base
|
|
|
|
* subtree -> subtree
|
|
|
|
* subordinate -> subtree
|
|
|
|
*/
|
|
|
|
switch( scope ) {
|
|
|
|
default:
|
|
|
|
case LDAP_SCOPE_BASE:
|
|
|
|
case LDAP_SCOPE_ONELEVEL:
|
|
|
|
scope = LDAP_SCOPE_BASE;
|
|
|
|
break;
|
|
|
|
case LDAP_SCOPE_SUBTREE:
|
|
|
|
case LDAP_SCOPE_SUBORDINATE:
|
|
|
|
scope = LDAP_SCOPE_SUBTREE;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-09 03:45:04 +08:00
|
|
|
}
|
2000-11-29 03:56:55 +08:00
|
|
|
|
2000-03-09 02:56:22 +08:00
|
|
|
} else {
|
2006-05-10 23:01:50 +08:00
|
|
|
rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2002-04-03 02:40:04 +08:00
|
|
|
if( rtag == LBER_ERROR ) {
|
2000-03-09 02:56:22 +08:00
|
|
|
ld->ld_errno = LDAP_DECODING_ERROR;
|
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2006-05-10 23:01:50 +08:00
|
|
|
/* restore character zero'd out by ber_scanf*/
|
|
|
|
dn.bv_val[dn.bv_len] = tmpber.ber_tag;
|
|
|
|
|
2000-11-29 03:56:55 +08:00
|
|
|
if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2006-05-10 23:01:50 +08:00
|
|
|
if ( srv->lud_dn ) {
|
|
|
|
ber_str2bv( srv->lud_dn, 0, 0, &dn );
|
2000-03-09 02:56:22 +08:00
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( tag == LDAP_REQ_BIND ) {
|
2006-05-10 23:01:50 +08:00
|
|
|
rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
|
2000-03-09 02:56:22 +08:00
|
|
|
} else if ( tag == LDAP_REQ_DELETE ) {
|
2006-05-10 23:01:50 +08:00
|
|
|
rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
|
2000-11-29 03:56:55 +08:00
|
|
|
} else if ( tag == LDAP_REQ_SEARCH ) {
|
2006-05-10 23:01:50 +08:00
|
|
|
rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
2006-05-10 23:01:50 +08:00
|
|
|
rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc == -1 ) {
|
2000-03-09 02:56:22 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ber, 1 );
|
2000-11-29 03:56:55 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-03-11 02:16:22 +08:00
|
|
|
if ( tag != LDAP_REQ_DELETE && (
|
2000-03-09 02:56:22 +08:00
|
|
|
ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
|
|
|
|
!= ( tmpber.ber_end - tmpber.ber_ptr ) ||
|
2000-06-24 09:40:39 +08:00
|
|
|
ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
|
2000-03-09 02:56:22 +08:00
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
2000-11-29 03:56:55 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
|
|
|
|
0, 0, 0 );
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
2000-06-01 01:34:43 +08:00
|
|
|
*type = tag; /* return request type */
|
2000-11-29 03:56:55 +08:00
|
|
|
return ber;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LDAPRequest *
|
1999-06-19 07:53:05 +08:00
|
|
|
ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2000-09-30 13:35:44 +08:00
|
|
|
LDAPRequest *lr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2003-02-13 18:43:16 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
|
2006-05-18 08:37:56 +08:00
|
|
|
if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
continue; /* Skip completed requests */
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( msgid == lr->lr_msgid ) {
|
2006-09-01 22:00:58 +08:00
|
|
|
lr->lr_refcnt++;
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-02-13 18:43:16 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( lr );
|
|
|
|
}
|
|
|
|
|
2006-09-01 22:00:58 +08:00
|
|
|
void
|
2006-09-02 00:21:20 +08:00
|
|
|
ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
|
2006-09-01 22:00:58 +08:00
|
|
|
{
|
|
|
|
LDAPRequest *lr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2006-09-01 22:00:58 +08:00
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
|
|
|
for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
|
|
|
|
if ( lr == lrx ) {
|
|
|
|
if ( lr->lr_refcnt > 0 ) {
|
|
|
|
lr->lr_refcnt--;
|
|
|
|
|
|
|
|
} else if ( lr->lr_refcnt < 0 ) {
|
|
|
|
lr->lr_refcnt++;
|
|
|
|
if ( lr->lr_refcnt == 0 ) {
|
|
|
|
lr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( lr == NULL ) {
|
|
|
|
ldap_free_request_int( ld, lrx );
|
|
|
|
|
|
|
|
} else if ( freeit ) {
|
|
|
|
ldap_free_request( ld, lrx );
|
|
|
|
}
|
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
|
|
|
|
#endif
|
|
|
|
}
|