1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* request.c - sending of ldap requests; handling of referrals
|
|
|
|
*/
|
|
|
|
|
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 ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1999-12-08 02:52:05 +08:00
|
|
|
static LDAPURLDesc *dn2servers LDAP_P(( LDAP *ld, const char *dn ));
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static BerElement *re_encode_request LDAP_P((
|
|
|
|
LDAP *ld,
|
|
|
|
BerElement *origber,
|
|
|
|
ber_int_t msgid,
|
|
|
|
char **dnp ));
|
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;
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
} else {
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_set_ber_options( ld, ber );
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
|
|
|
|
ber_set_string_translators( ber,
|
|
|
|
ld->ld_lber_encode_translate_proc,
|
|
|
|
ld->ld_lber_decode_translate_proc );
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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,
|
1998-08-09 08:43:13 +08:00
|
|
|
BerElement *ber )
|
|
|
|
{
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *servers;
|
|
|
|
int rc;
|
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
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( ! ber_pvt_sb_in_use(&ld->ld_sb ) ) {
|
1999-01-31 09:09:00 +08:00
|
|
|
/* not connected yet */
|
1999-09-23 12:08:33 +08:00
|
|
|
int rc = ldap_open_defconn( ld );
|
1999-01-31 09:09:00 +08:00
|
|
|
|
1999-09-23 12:08:33 +08:00
|
|
|
if( rc < 0 ) {
|
1999-04-04 13:04:48 +08:00
|
|
|
ber_free( ber, 1 );
|
1999-01-31 09:09:00 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ldap_delayed_open successful, ld_host is %s\n",
|
|
|
|
( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
|
|
|
|
}
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1999-08-02 15:41:29 +08:00
|
|
|
if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS )
|
1998-11-08 09:11:09 +08:00
|
|
|
&& ldap_is_dns_dn( dn ) )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if (( servers = dn2servers( ld, dn )) == NULL ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
if ( ldap_debug & LDAP_DEBUG_TRACE ) {
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
for ( srv = servers;
|
|
|
|
srv != NULL;
|
|
|
|
srv = srv->lud_next )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"LDAP server %s: dn %s, port %d\n",
|
1999-12-08 02:52:05 +08:00
|
|
|
srv->lud_host, ( srv->lud_dn == NULL ) ?
|
|
|
|
"(default)" : srv->lud_dn,
|
|
|
|
srv->lud_port );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LDAP_DEBUG */
|
1999-05-22 14:11:48 +08:00
|
|
|
} else
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1999-05-22 14:11:48 +08:00
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* use of DNS is turned off or this is an X.500 DN...
|
|
|
|
* use our default connection
|
|
|
|
*/
|
|
|
|
servers = NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
|
|
|
|
servers, NULL, 0 );
|
|
|
|
if (servers)
|
|
|
|
ldap_free_urllist(servers);
|
|
|
|
return(rc);
|
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,
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srvlist,
|
1999-06-19 07:53:05 +08:00
|
|
|
LDAPConn *lc,
|
|
|
|
int bind )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPRequest *lr;
|
1998-08-09 10:56:52 +08:00
|
|
|
int incparent;
|
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 {
|
|
|
|
if (( lc = find_connection( ld, srvlist, 1 )) ==
|
|
|
|
NULL ) {
|
1998-08-09 10:56:52 +08:00
|
|
|
if ( bind && (parentreq != NULL) ) {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
if ( ld->ld_errno == LDAP_SUCCESS ) {
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
}
|
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 );
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
|
1998-08-09 08:43:13 +08:00
|
|
|
NULL ) {
|
|
|
|
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;
|
|
|
|
lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
|
|
|
|
lr->lr_parent = parentreq;
|
|
|
|
lr->lr_refnext = parentreq->lr_refnext;
|
|
|
|
parentreq->lr_refnext = lr;
|
|
|
|
} else { /* original request */
|
|
|
|
lr->lr_origid = lr->lr_msgid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (( lr->lr_next = ld->ld_requests ) != NULL ) {
|
|
|
|
lr->lr_next->lr_prev = lr;
|
|
|
|
}
|
|
|
|
ld->ld_requests = lr;
|
|
|
|
lr->lr_prev = NULL;
|
|
|
|
|
|
|
|
if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
|
|
|
|
#ifdef notyet
|
|
|
|
if ( errno == EWOULDBLOCK ) {
|
|
|
|
/* need to continue write later */
|
|
|
|
lr->lr_status = LDAP_REQST_WRITING;
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_write( ld, lc->lconn_sb );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
#else /* notyet */
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_request( ld, lr );
|
|
|
|
ldap_free_connection( ld, lc, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
#endif /* notyet */
|
|
|
|
#ifdef notyet
|
|
|
|
}
|
|
|
|
#endif /* notyet */
|
|
|
|
} else {
|
|
|
|
if ( parentreq == NULL ) {
|
|
|
|
ber->ber_end = ber->ber_ptr;
|
|
|
|
ber->ber_ptr = ber->ber_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sent -- waiting for a response */
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_read( ld, lc->lconn_sb );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ld->ld_errno = LDAP_SUCCESS;
|
|
|
|
return( msgid );
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAPConn *
|
1999-12-08 02:52:05 +08:00
|
|
|
ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
|
1998-08-09 08:43:13 +08:00
|
|
|
int connect, int bind )
|
|
|
|
{
|
|
|
|
LDAPConn *lc;
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
Sockbuf *sb;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make a new LDAP server connection
|
|
|
|
* XXX open connection synchronously for now
|
|
|
|
*/
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
|
1999-05-19 09:12:33 +08:00
|
|
|
( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( lc != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( (char *)lc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
|
|
|
|
|
|
|
|
if ( connect ) {
|
1999-12-08 02:52:05 +08:00
|
|
|
for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( open_ldap_connection( ld, lc->lconn_sb,
|
1999-12-08 02:52:05 +08:00
|
|
|
srv, &lc->lconn_krbinstance, 0 ) != -1 )
|
|
|
|
{
|
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 );
|
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( (char *)lc );
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
lc->lconn_server = ldap_url_dup(srv);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lc->lconn_status = LDAP_CONNST_CONNECTED;
|
|
|
|
lc->lconn_next = ld->ld_conns;
|
|
|
|
ld->ld_conns = lc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX for now, we always do a synchronous bind. This will have
|
|
|
|
* to change in the long run...
|
|
|
|
*/
|
|
|
|
if ( bind ) {
|
|
|
|
int err, freepasswd, authmethod;
|
|
|
|
char *binddn, *passwd;
|
|
|
|
LDAPConn *savedefconn;
|
|
|
|
|
|
|
|
freepasswd = err = 0;
|
|
|
|
|
1999-03-04 00:02:00 +08:00
|
|
|
if ( ld->ld_rebindproc == 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
binddn = passwd = "";
|
|
|
|
authmethod = LDAP_AUTH_SIMPLE;
|
|
|
|
} else {
|
|
|
|
if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
|
|
|
|
&authmethod, 0 )) == LDAP_SUCCESS ) {
|
|
|
|
freepasswd = 1;
|
|
|
|
} else {
|
|
|
|
ld->ld_errno = err;
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( err == 0 ) {
|
|
|
|
savedefconn = ld->ld_defconn;
|
|
|
|
ld->ld_defconn = lc;
|
|
|
|
++lc->lconn_refcnt; /* avoid premature free */
|
|
|
|
|
|
|
|
if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
|
|
|
|
LDAP_SUCCESS ) {
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
--lc->lconn_refcnt;
|
|
|
|
ld->ld_defconn = savedefconn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( freepasswd ) {
|
|
|
|
(*ld->ld_rebindproc)( ld, &binddn, &passwd,
|
|
|
|
&authmethod, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( err != 0 ) {
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_connection( ld, lc, 1, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
lc = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *ls;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
|
1999-12-08 02:52:05 +08:00
|
|
|
for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
|
|
|
|
if ( lc->lconn_server->lud_host != NULL &&
|
|
|
|
ls->lud_host != NULL && strcasecmp(
|
|
|
|
ls->lud_host, lc->lconn_server->lud_host ) == 0
|
|
|
|
&& ls->lud_port == lc->lconn_server->lud_port ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( lc );
|
|
|
|
}
|
|
|
|
if ( !any ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( force || --lc->lconn_refcnt <= 0 ) {
|
|
|
|
if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_clear( ld, lc->lconn_sb );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( unbind ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
1999-10-13 02:51:36 +08:00
|
|
|
|
|
|
|
/* force closure */
|
|
|
|
ldap_close_connection( lc->lconn_sb );
|
|
|
|
ber_pvt_sb_destroy( lc->lconn_sb );
|
|
|
|
|
|
|
|
if( lc->lconn_ber != NULL ) {
|
|
|
|
ber_free( lc->lconn_ber, 1 );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
prevlc = NULL;
|
|
|
|
for ( tmplc = ld->ld_conns; tmplc != NULL;
|
|
|
|
tmplc = tmplc->lconn_next ) {
|
|
|
|
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
|
|
|
}
|
1999-12-08 02:52:05 +08:00
|
|
|
ldap_free_urllist( lc->lconn_server );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( lc->lconn_krbinstance != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lc->lconn_krbinstance );
|
1998-08-09 08:43:13 +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
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lc );
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
|
1998-08-09 08:43:13 +08:00
|
|
|
0, 0, 0 );
|
|
|
|
} 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",
|
1998-08-09 08:43:13 +08:00
|
|
|
lc->lconn_refcnt, 0, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
|
|
|
|
for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
|
|
|
|
if ( lc->lconn_server != NULL ) {
|
|
|
|
fprintf( stderr, "* host: %s port: %d%s\n",
|
1999-12-08 02:52:05 +08:00
|
|
|
( lc->lconn_server->lud_host == NULL ) ? "(null)"
|
|
|
|
: lc->lconn_server->lud_host,
|
|
|
|
lc->lconn_server->lud_port, ( lc->lconn_sb ==
|
1998-08-09 08:43:13 +08:00
|
|
|
&ld->ld_sb ) ? " (default)" : "" );
|
|
|
|
}
|
|
|
|
fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
|
|
|
|
( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
|
|
|
|
"NeedSocket" : ( lc->lconn_status ==
|
|
|
|
LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
|
|
|
|
fprintf( stderr, " last used: %s\n",
|
1999-01-15 22:49:03 +08:00
|
|
|
ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
|
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;
|
|
|
|
|
|
|
|
fprintf( stderr, "** Outstanding Requests:\n" );
|
|
|
|
if (( lr = ld->ld_requests ) == NULL ) {
|
|
|
|
fprintf( stderr, " Empty\n" );
|
|
|
|
}
|
|
|
|
for ( ; lr != NULL; lr = lr->lr_next ) {
|
|
|
|
fprintf( stderr, " * msgid %d, origid %d, status %s\n",
|
|
|
|
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" :
|
|
|
|
"Writing" );
|
|
|
|
fprintf( stderr, " outstanding referrals %d, parent count %d\n",
|
|
|
|
lr->lr_outrefcnt, lr->lr_parentcnt );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stderr, "** Response Queue:\n" );
|
1999-06-19 07:53:05 +08:00
|
|
|
if (( lm = ld->ld_responses ) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
fprintf( stderr, " Empty\n" );
|
|
|
|
}
|
1999-06-19 07:53:05 +08:00
|
|
|
for ( ; lm != NULL; lm = lm->lm_next ) {
|
1999-08-18 05:14:03 +08:00
|
|
|
fprintf( stderr, " * msgid %d, type %lu\n",
|
|
|
|
lm->lm_msgid, (unsigned long) lm->lm_msgtype );
|
1998-08-09 08:43:13 +08:00
|
|
|
if (( l = lm->lm_chain ) != NULL ) {
|
|
|
|
fprintf( stderr, " chained responses:\n" );
|
1999-06-19 07:53:05 +08:00
|
|
|
for ( ; l != NULL; l = l->lm_chain ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
fprintf( stderr,
|
1999-08-18 05:14:03 +08:00
|
|
|
" * msgid %d, type %lu\n",
|
|
|
|
l->lm_msgid,
|
|
|
|
(unsigned long) l->lm_msgtype );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_request( LDAP *ld, LDAPRequest *lr )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
LDAPRequest *tmplr, *nextlr;
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
|
1998-08-09 08:43:13 +08:00
|
|
|
lr->lr_origid, lr->lr_msgid, 0 );
|
|
|
|
|
|
|
|
if ( lr->lr_parent != NULL ) {
|
|
|
|
--lr->lr_parent->lr_outrefcnt;
|
|
|
|
} else {
|
|
|
|
/* free all referrals (child requests) */
|
|
|
|
for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
|
|
|
|
nextlr = tmplr->lr_refnext;
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_request( ld, tmplr );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_prev == NULL ) {
|
|
|
|
ld->ld_requests = lr->lr_next;
|
|
|
|
} else {
|
|
|
|
lr->lr_prev->lr_next = lr->lr_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_next != NULL ) {
|
|
|
|
lr->lr_next->lr_prev = lr->lr_prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_ber != NULL ) {
|
|
|
|
ber_free( lr->lr_ber, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lr->lr_res_error != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( lr->lr_res_error );
|
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 );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX merging of errors in this routine needs to be improved
|
|
|
|
*/
|
|
|
|
int
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-12-09 07:50:59 +08:00
|
|
|
int rc, count, len, newdn;
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
int ldapref;
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
|
|
|
|
LDAPRequest *origreq;
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
BerElement *ber;
|
|
|
|
|
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 ) {
|
|
|
|
if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
|
|
|
|
LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
|
|
|
|
*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 ) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
unfollowed = NULL;
|
|
|
|
rc = count = 0;
|
|
|
|
|
|
|
|
/* parse out & follow referrals */
|
|
|
|
for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
ldapref = 0;
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if (( p = strchr( ref, '\n' )) != NULL ) {
|
|
|
|
*p++ = '\0';
|
|
|
|
} else {
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
|
2000-03-21 06:00:16 +08:00
|
|
|
ldap_pvt_hex_unescape( ref );
|
1998-08-09 08:43:13 +08:00
|
|
|
len = strlen( ref );
|
2000-03-21 06:00:16 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
|
|
|
|
LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"chasing LDAP referral: <%s>\n", ref, 0, 0 );
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
ldapref = 1;
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
tmpref = ref + LDAP_LDAP_REF_STR_LEN;
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
} else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
|
|
|
|
LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"chasing DX referral: <%s>\n", ref, 0, 0 );
|
|
|
|
tmpref = ref + LDAP_DX_REF_STR_LEN;
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"ignoring unknown referral <%s>\n", ref, 0, 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;
|
|
|
|
}
|
|
|
|
|
1999-12-09 07:50:59 +08:00
|
|
|
/* NOTE! This code treats "ldap://host/" differently
|
|
|
|
* from "ldap://host". The behavior is wrong, but is
|
|
|
|
* left here intentionally to maintain compatibility
|
|
|
|
* with OpenLDAP 1.x and UMich 3.3 clients.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
*hadrefp = 1;
|
|
|
|
if (( refdn = strchr( tmpref, '/' )) != NULL ) {
|
|
|
|
*refdn++ = '\0';
|
1999-12-09 07:50:59 +08:00
|
|
|
newdn = 1;
|
|
|
|
} else {
|
|
|
|
newdn = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (( ber = re_encode_request( ld, origreq->lr_ber,
|
|
|
|
++ld->ld_msgid, &refdn )) == NULL ) {
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ldapref ) {
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1999-12-08 02:52:05 +08:00
|
|
|
if (( srv = (LDAPURLDesc *)LDAP_CALLOC( 1,
|
|
|
|
sizeof( LDAPURLDesc ))) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
if (( srv->lud_host = LDAP_STRDUP( tmpref )) == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( (char *)srv );
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-12-08 02:52:05 +08:00
|
|
|
if (( ports = strchr( srv->lud_host, ':' )) != NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
*ports++ = '\0';
|
1999-12-08 02:52:05 +08:00
|
|
|
srv->lud_port = atoi( ports );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
1999-12-08 02:52:05 +08:00
|
|
|
srv->lud_port = ldap_int_global_options.ldo_defport;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
srv = dn2servers( ld, tmpref );
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
|
1998-08-09 08:43:13 +08:00
|
|
|
lr, srv, NULL, 1 ) >= 0 ) {
|
|
|
|
++count;
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Unable to chase referral (%s)\n",
|
|
|
|
ldap_err2string( ld->ld_errno ), 0, 0 );
|
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
|
|
|
if (srv != NULL)
|
|
|
|
ldap_free_urllist(srv);
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( !newdn && refdn != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( refdn );
|
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 *
|
1999-06-19 07:53:05 +08:00
|
|
|
re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp )
|
1998-08-09 08:43:13 +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;
|
|
|
|
ber_int_t ver;
|
1998-08-09 08:43:13 +08:00
|
|
|
int rc;
|
|
|
|
BerElement tmpber, *ber;
|
|
|
|
char *orig_dn;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
1999-06-19 07:53:05 +08:00
|
|
|
"re_encode_request: new msgid %ld, new dn <%s>\n",
|
|
|
|
(long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 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
|
|
|
*/
|
2000-03-09 02:56:22 +08:00
|
|
|
rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
|
|
|
|
|
|
|
|
if ( rc == LBER_ERROR ) {
|
|
|
|
ld->ld_errno = LDAP_DECODING_ERROR;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2000-03-09 02:56:22 +08:00
|
|
|
if ( tag == LDAP_REQ_BIND ) {
|
|
|
|
/* bind requests have a version number before the DN & other stuff */
|
|
|
|
rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_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 */
|
|
|
|
rc = ber_scanf( &tmpber, "a", &orig_dn );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-03-09 02:56:22 +08:00
|
|
|
if( rc == LBER_ERROR ) {
|
|
|
|
ld->ld_errno = LDAP_DECODING_ERROR;
|
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( *dnp == NULL ) {
|
|
|
|
*dnp = orig_dn;
|
|
|
|
} else {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( orig_dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-03-09 02:56:22 +08:00
|
|
|
if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( tag == LDAP_REQ_BIND ) {
|
1999-05-22 14:11:48 +08:00
|
|
|
rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
|
2000-03-09 02:56:22 +08:00
|
|
|
} else if ( tag == LDAP_REQ_DELETE ) {
|
|
|
|
rc = ber_printf( ber, "{its}", msgid, tag, *dnp );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
1999-05-22 14:11:48 +08:00
|
|
|
rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp );
|
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 );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
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-03-11 02:16:22 +08:00
|
|
|
ber_printf( ber, /*{{*/ "}}" ) == -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 );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
return( ber );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
LDAPRequest *lr;
|
|
|
|
|
|
|
|
for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
|
|
|
|
if ( msgid == lr->lr_msgid ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( lr );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
|
1999-12-08 02:52:05 +08:00
|
|
|
static LDAPURLDesc *
|
1999-08-02 15:41:29 +08:00
|
|
|
dn2servers( LDAP *ld, const char *dn ) /* dn can also be a domain.... */
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-08-02 15:41:29 +08:00
|
|
|
char *p, *host, *server_dn, **dxs;
|
|
|
|
const char *domain;
|
1998-08-09 08:43:13 +08:00
|
|
|
int i, port;
|
1999-12-08 02:52:05 +08:00
|
|
|
LDAPURLDesc *srvlist, *prevsrv, *srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if (( domain = strrchr( dn, '@' )) != NULL ) {
|
|
|
|
++domain;
|
|
|
|
} else {
|
|
|
|
domain = dn;
|
|
|
|
}
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
srvlist = NULL;
|
|
|
|
for ( i = 0; dxs[ i ] != NULL; ++i ) {
|
1999-12-08 02:52:05 +08:00
|
|
|
if (ldap_url_parselist(&srv, dxs[i]) == LDAP_SUCCESS
|
|
|
|
|| ldap_url_parsehosts(&srv, dxs[i]) == LDAP_SUCCESS)
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
/* add to end of list of servers */
|
|
|
|
if ( srvlist == NULL ) {
|
|
|
|
srvlist = srv;
|
|
|
|
} else {
|
1999-12-08 02:52:05 +08:00
|
|
|
prevsrv->lud_next = srv;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
prevsrv = srv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_value_free( dxs );
|
|
|
|
|
|
|
|
if ( srvlist == NULL ) {
|
|
|
|
ld->ld_errno = LDAP_SERVER_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( srvlist );
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
|