]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/request.c
cleanup
[openldap] / libraries / libldap / request.c
index 245c73b9486c417b57cb05aa5c1c6c515520cb11..82be383618644c4d185c0b019b555b9530e0961b 100644 (file)
@@ -1,18 +1,37 @@
+/* $OpenLDAP$ */
 /*
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1995 Regents of the University of Michigan.
  *  All rights reserved.
+ */
+/*---
+ * This notice applies to changes, created by or for Novell, Inc.,
+ * 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. 
+ *---
+ * Modification to OpenLDAP source by Novell, Inc.
+ * April 2000 sfs  Added code to chase V3 referrals
  *  request.c - sending of ldap requests; handling of referrals
  */
 
 #include "portable.h"
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/errno.h>
 #include <ac/socket.h>
@@ -21,22 +40,18 @@ static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of
 #include <ac/unistd.h>
 
 #include "ldap-int.h"
+#include "lber.h"
 
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
-static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
+static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
-static void free_servers LDAP_P(( LDAPServer *srvlist ));
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
-static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
-    int msgid, char **dnp ));
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+static BerElement *re_encode_request LDAP_P((
+       LDAP *ld,
+       BerElement *origber,
+    ber_int_t msgid,
+       char **dnp,
+       int      *type));
 
 
 BerElement *
@@ -44,12 +59,8 @@ ldap_alloc_ber_with_options( LDAP *ld )
 {
        BerElement      *ber;
 
-       if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
+    if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
                ld->ld_errno = LDAP_NO_MEMORY;
-#ifdef STR_TRANSLATION
-       } else {
-               ldap_set_ber_options( ld, ber );
-#endif /* STR_TRANSLATION */
        }
 
        return( ber );
@@ -60,81 +71,61 @@ void
 ldap_set_ber_options( LDAP *ld, BerElement *ber )
 {
        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 */
 }
 
 
-int
-ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
+ber_int_t
+ldap_send_initial_request(
+       LDAP *ld,
+       ber_tag_t msgtype,
+       const char *dn,
        BerElement *ber )
 {
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
-       LDAPServer      *servers;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       LDAPURLDesc     *servers;
+       int rc;
 
        Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
 
-#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
-       if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
-               ld->ld_errno = LDAP_SERVER_DOWN;
-               return( -1 );
-       }
-
-       ld->ld_errno = LDAP_SUCCESS;
-       return( ld->ld_msgid );
-#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
+               /* not connected yet */
+               int rc = ldap_open_defconn( ld );
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-       if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
-               && ldap_is_dns_dn( dn ) )
-       {
-               if (( servers = dn2servers( ld, dn )) == NULL ) {
+               if( rc < 0 ) {
                        ber_free( ber, 1 );
                        return( -1 );
                }
 
-#ifdef LDAP_DEBUG
-               if ( ldap_debug & LDAP_DEBUG_TRACE ) {
-                       LDAPServer      *srv;
+               Debug( LDAP_DEBUG_TRACE,
+                       "ldap_delayed_open successful, ld_host is %s\n",
+                       ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
+       }
 
-                       for ( srv = servers; srv != NULL;
-                           srv = srv->lsrv_next ) {
-                               fprintf( stderr,
-                                   "LDAP server %s:  dn %s, port %d\n",
-                                   srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
-                                   "(default)" : srv->lsrv_dn,
-                                   srv->lsrv_port );
-                       }
-               }
-#endif /* LDAP_DEBUG */
-       } else {
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       {
                /*
                 * use of DNS is turned off or this is an X.500 DN...
                 * use our default connection
                 */
                servers = NULL;
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
        }       
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-       return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
-           NULL, 0 ));
-#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+       rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
+                                                                       servers, NULL, NULL );
+       if (servers)
+               ldap_free_urllist(servers);
+       return(rc);
 }
 
 
 
-#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 int
-ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
-       *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
+ldap_send_server_request(
+       LDAP *ld,
+       BerElement *ber,
+       ber_int_t msgid,
+       LDAPRequest *parentreq,
+       LDAPURLDesc *srvlist,
+       LDAPConn *lc,
+       LDAPreqinfo *bind )
 {
        LDAPRequest     *lr;
        int incparent;
@@ -150,14 +141,13 @@ ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
                } else {
                        if (( lc = find_connection( ld, srvlist, 1 )) ==
                            NULL ) {
-                               if ( bind && (parentreq != NULL) ) {
+                               if ( (bind != NULL) && (parentreq != NULL) ) {
                                        /* Remember the bind in the parent */
                                        incparent = 1;
                                        ++parentreq->lr_outrefcnt;
                                }
-                               lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
+                               lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
                        }
-                       free_servers( srvlist );
                }
        }
 
@@ -174,7 +164,7 @@ ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
        }
 
        use_connection( ld, lc );
-       if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
+       if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
            NULL ) {
                ld->ld_errno = LDAP_NO_MEMORY;
                ldap_free_connection( ld, lc, 0, 0 );
@@ -240,58 +230,47 @@ ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
        return( msgid );
 }
 
-
 LDAPConn *
-ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
-       int connect, int bind )
+ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
+       int connect, LDAPreqinfo *bind )
 {
        LDAPConn        *lc;
-       LDAPServer      *prevsrv, *srv;
-       Sockbuf         *sb;
+       LDAPURLDesc     *srv;
+       Sockbuf         *sb = NULL;
 
+       Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
        /*
         * make a new LDAP server connection
         * XXX open connection synchronously for now
         */
-       if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
-           ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
-           == NULL )) {
+       if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
+           ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
                if ( lc != NULL ) {
-                       free( (char *)lc );
+                       LDAP_FREE( (char *)lc );
                }
                ld->ld_errno = LDAP_NO_MEMORY;
                return( NULL );
        }
 
-       lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
+       lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
 
        if ( connect ) {
-               prevsrv = NULL;
-
-               for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
-                       if ( open_ldap_connection( ld, lc->lconn_sb,
-                           srv->lsrv_host, srv->lsrv_port,
-                           &lc->lconn_krbinstance, 0 ) != -1 ) {
+               for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
+                       if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
                                break;
                        }
-                       prevsrv = srv;
                }
 
                if ( srv == NULL ) {
-                   if ( !use_ldsb ) {
-                       free( (char *)lc->lconn_sb );
-                   }
-                   free( (char *)lc );
+                       if ( !use_ldsb ) {
+                               ber_sockbuf_free( lc->lconn_sb );
+                       }
+                   LDAP_FREE( (char *)lc );
                    ld->ld_errno = LDAP_SERVER_DOWN;
                    return( NULL );
                }
 
-               if ( prevsrv == NULL ) {
-                   *srvlistp = srv->lsrv_next;
-               } else {
-                   prevsrv->lsrv_next = srv->lsrv_next;
-               }
-               lc->lconn_server = srv;
+               lc->lconn_server = ldap_url_dup(srv);
        }
 
        lc->lconn_status = LDAP_CONNST_CONNECTED;
@@ -302,71 +281,79 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
         * 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;
+       if ( bind != NULL) {
+               int             err = 0;
                LDAPConn        *savedefconn;
 
-               freepasswd = err = 0;
-
-               if ( ld->ld_rebindproc == NULL ) {
-                       binddn = passwd = "";
-                       authmethod = LDAP_AUTH_SIMPLE;
-               } else {
-                       if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
-                           &authmethod, 0 )) == LDAP_SUCCESS ) {
-                               freepasswd = 1;
+               /* Set flag to prevent additional referrals from being processed on this
+                * connection until the bind has completed
+                */
+               lc->lconn_rebind_inprogress = 1;
+               /* V3 rebind function */
+               if ( ld->ld_rebindproc != NULL) {
+                       LDAPURLDesc     *srvfunc;
+                       if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
+                               ld->ld_errno = LDAP_NO_MEMORY;
+                               err = -1;
                        } else {
-                               ld->ld_errno = err;
+                               savedefconn = ld->ld_defconn;
+                               ++lc->lconn_refcnt;     /* avoid premature free */
+                               ld->ld_defconn = lc;
+
+                               Debug( LDAP_DEBUG_TRACE, "Call application rebindproc\n", 0, 0, 0);
+                               err = (*ld->ld_rebindproc)( ld, bind->ri_url, bind->ri_request, bind->ri_msgid);
+
+                               ld->ld_defconn = savedefconn;
+                               --lc->lconn_refcnt;
+
+                               if( err != 0) {
                                err = -1;
+                                       ldap_free_connection( ld, lc, 1, 0 );
+                                       lc = NULL;
                        }
+                               ldap_free_urldesc( srvfunc);
                }
-
-
-               if ( err == 0 ) {
+               } else {
                        savedefconn = ld->ld_defconn;
-                       ld->ld_defconn = lc;
                        ++lc->lconn_refcnt;     /* avoid premature free */
+                       ld->ld_defconn = lc;
 
-                       if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
-                           LDAP_SUCCESS ) {
+                       Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
+                       if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
                                err = -1;
                        }
-                       --lc->lconn_refcnt;
                        ld->ld_defconn = savedefconn;
-               }
-
-               if ( freepasswd ) {
-                       (*ld->ld_rebindproc)( ld, &binddn, &passwd,
-                               &authmethod, 1 );
-               }
+                       --lc->lconn_refcnt;
 
                if ( err != 0 ) {
                        ldap_free_connection( ld, lc, 1, 0 );
                        lc = NULL;
                }
        }
+               if( lc != NULL)
+                       lc->lconn_rebind_inprogress = 0;
+       }
 
        return( lc );
 }
 
 
 static LDAPConn *
-find_connection( LDAP *ld, LDAPServer *srv, int any )
+find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
 /*
  * 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;
-       LDAPServer      *ls;
+       LDAPURLDesc     *ls;
 
        for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
-               for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
-                       if ( lc->lconn_server->lsrv_host != NULL &&
-                           ls->lsrv_host != NULL && strcasecmp(
-                           ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
-                           && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
+               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 ) {
                                return( lc );
                        }
                        if ( !any ) {
@@ -384,7 +371,7 @@ static void
 use_connection( LDAP *ld, LDAPConn *lc )
 {
        ++lc->lconn_refcnt;
-       lc->lconn_lastused = time( 0 );
+       lc->lconn_lastused = time( NULL );
 }
 
 
@@ -399,13 +386,16 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
                        ldap_mark_select_clear( ld, lc->lconn_sb );
                        if ( unbind ) {
-                               ldap_send_unbind( ld, lc->lconn_sb );
-                       }
-                       ldap_close_connection( lc->lconn_sb );
-                       if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
-                               free( lc->lconn_sb->sb_ber.ber_buf );
+                               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 );
+
                prevlc = NULL;
                for ( tmplc = ld->ld_conns; tmplc != NULL;
                    tmplc = tmplc->lconn_next ) {
@@ -419,18 +409,27 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                        }
                        prevlc = tmplc;
                }
-               free_servers( lc->lconn_server );
+               ldap_free_urllist( lc->lconn_server );
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
                if ( lc->lconn_krbinstance != NULL ) {
-                       free( lc->lconn_krbinstance );
+                       LDAP_FREE( lc->lconn_krbinstance );
                }
-               if ( lc->lconn_sb != &ld->ld_sb ) {
-                       free( (char *)lc->lconn_sb );
+#endif
+               if ( lc->lconn_sb != ld->ld_sb ) {
+                       ber_sockbuf_free( lc->lconn_sb );
                }
-               free( lc );
+               if( lc->lconn_rebind_queue != NULL) {
+                       int i;
+                       for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
+                               LDAP_VFREE(lc->lconn_rebind_queue[i]);
+                       }
+                       LDAP_FREE( lc->lconn_rebind_queue);
+               }
+               LDAP_FREE( lc );
                Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
                    0, 0, 0 );
        } else {
-               lc->lconn_lastused = time( 0 );
+               lc->lconn_lastused = time( NULL );
                Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
                    lc->lconn_refcnt, 0, 0 );
        }
@@ -448,17 +447,33 @@ ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
        for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
                if ( lc->lconn_server != NULL ) {
                        fprintf( stderr, "* host: %s  port: %d%s\n",
-                           ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
-                           : lc->lconn_server->lsrv_host,
-                           lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
-                           &ld->ld_sb ) ? "  (default)" : "" );
+                           ( lc->lconn_server->lud_host == NULL ) ? "(null)"
+                           : lc->lconn_server->lud_host,
+                           lc->lconn_server->lud_port, ( lc->lconn_sb ==
+                           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",
-                   ldap_int_ctime( &lc->lconn_lastused, timebuf ));
+               fprintf( stderr, "  last used: %s",
+                   ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
+               if( lc->lconn_rebind_inprogress ) {
+                       fprintf( stderr, "  rebind in progress\n");
+                       if( lc->lconn_rebind_queue != NULL) {
+                               int i = 0;
+                               for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
+                                       int j = 0;
+                                       for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
+                                               fprintf( stderr, "    queue %d entry %d - %s\n",
+                                                       i, j, lc->lconn_rebind_queue[i][j]);
+                                       }
+                               }
+                       } else {
+                               fprintf( stderr, "    queue is empty\n");
+                       }
+               }
+               fprintf(stderr, "\n");
                if ( !all ) {
                        break;
                }
@@ -478,28 +493,30 @@ ldap_dump_requests_and_responses( LDAP *ld )
        }
        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_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" );
+               ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
+               ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
            fprintf( stderr, "   outstanding referrals %d, parent count %d\n",
                    lr->lr_outrefcnt, lr->lr_parentcnt );
        }
 
        fprintf( stderr, "** Response Queue:\n" );
-       if (( lm = ld->ld_responses ) == NULLMSG ) {
+       if (( lm = ld->ld_responses ) == NULL ) {
                fprintf( stderr, "   Empty\n" );
        }
-       for ( ; lm != NULLMSG; lm = lm->lm_next ) {
-               fprintf( stderr, " * msgid %d,  type %d\n",
-                   lm->lm_msgid, lm->lm_msgtype );
+       for ( ; lm != NULL; lm = lm->lm_next ) {
+               fprintf( stderr, " * msgid %d,  type %lu\n",
+                   lm->lm_msgid, (unsigned long) lm->lm_msgtype );
                if (( l = lm->lm_chain ) != NULL ) {
                        fprintf( stderr, "   chained responses:\n" );
-                       for ( ; l != NULLMSG; l = l->lm_chain ) {
+                       for ( ; l != NULL; l = l->lm_chain ) {
                                fprintf( stderr,
-                                   "  * msgid %d,  type %d\n",
-                                   l->lm_msgid, l->lm_msgtype );
+                                   "  * msgid %d,  type %lu\n",
+                                   l->lm_msgid,
+                                   (unsigned long) l->lm_msgtype );
                        }
                }
        }
@@ -540,38 +557,213 @@ ldap_free_request( LDAP *ld, LDAPRequest *lr )
        }
 
        if ( lr->lr_res_error != NULL ) {
-               free( lr->lr_res_error );
+               LDAP_FREE( lr->lr_res_error );
        }
 
        if ( lr->lr_res_matched != NULL ) {
-               free( lr->lr_res_matched );
+               LDAP_FREE( lr->lr_res_matched );
        }
 
-       free( lr );
+       LDAP_FREE( lr );
 }
 
-
-static void
-free_servers( LDAPServer *srvlist )
+/*
+ * 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
+ *  (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
+ */
+int
+ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, int *hadrefp )
 {
-    LDAPServer *nextsrv;
+       char            *unfollowed;
+       int                      unfollowedcnt = 0;
+       LDAPRequest     *origreq;
+       LDAPURLDesc     *srv = NULL;
+       BerElement      *ber;
+       char            **refarray = NULL;
+       LDAPConn        *lc;
+       int                      rc, count, i, j;
+       LDAPreqinfo  rinfo;
 
-    while ( srvlist != NULL ) {
-       nextsrv = srvlist->lsrv_next;
-       if ( srvlist->lsrv_dn != NULL ) {
-               free( srvlist->lsrv_dn );
+       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;
        }
-       if ( srvlist->lsrv_host != NULL ) {
-               free( srvlist->lsrv_host );
+
+       /* 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;
+           rc = -1;
+               goto done;
        }
-       free( srvlist );
-       srvlist = nextsrv;
-    }
-}
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
+       /* find original request */
+       for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) {
+               ;
+       }
+
+       refarray = refs;
+       refs = NULL;
+       /* parse out & follow referrals */
+       for( i=0; refarray[i] != NULL; i++) {
+               /* Parse the referral URL */
+               if (( rc = ldap_url_parse( refarray[i], &srv)) != LDAP_SUCCESS) {
+                       ld->ld_errno = rc;
+                       rc = -1;
+                       goto done;
+               }
+
+               /* treat ldap://hostpart and ldap://hostpart/ the same */
+               if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
+                       LDAP_FREE( srv->lud_dn );
+                       srv->lud_dn = NULL;
+               }
+
+               /* check connection for re-bind in progress */
+               if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
+                       if( lc->lconn_rebind_inprogress) {
+                               /* 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.
+                                */
+                               Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals: queue referral \"%s\"\n",
+                                       refarray[i], 0, 0);
+                               if( lc->lconn_rebind_queue == NULL ) {
+                                       /* Create a referral list */
+                                       if( (lc->lconn_rebind_queue = (char ***)LDAP_MALLOC( sizeof(void *) * 2)) == NULL) {
+                                               ld->ld_errno = LDAP_NO_MEMORY;
+                                               rc = -1;
+                                               goto done;
+                                       }
+                                       lc->lconn_rebind_queue[0] = refarray;
+                                       lc->lconn_rebind_queue[1] = NULL;
+                                       refarray = NULL;
+                               } else {
+                                       /* Count how many referral arrays we already have */
+                                       for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
+                                               ;
+                                       }
+                                       /* Add the new referral to the list */
+                                       if( (lc->lconn_rebind_queue = (char ***)LDAP_REALLOC(
+                                                       lc->lconn_rebind_queue, sizeof(void *) * (j + 2))) == NULL) {
+                                               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;
+                               }
+                               /* 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
+                */
+               if (( ber = re_encode_request( ld, origreq->lr_ber,
+                           ++ld->ld_msgid, &srv->lud_dn, &rinfo.ri_request )) == NULL ) {
+                       ld->ld_errno = LDAP_ENCODING_ERROR;
+                       rc = -1;
+                       goto done;
+               }
+
+               Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
+                       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];
+               if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
+                       origreq, srv, NULL, &rinfo )) < 0 ) {
+                       /* Failure, try next referral in the list */
+                       Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n", 
+                               refarray[i], ldap_err2string( ld->ld_errno ), 0);
+                       unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
+                       ldap_free_urllist(srv);
+                       srv = NULL;
+               } 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 */
+                       if( lc == NULL) {
+                               if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
+                                       ld->ld_errno = LDAP_OPERATIONS_ERROR;
+                                       rc = -1;
+                                       goto done;
+                               }
+                       }
+
+                       if( lc->lconn_rebind_queue != NULL) {
+                               /* Release resources of previous list */
+                               LDAP_VFREE(refarray);
+                               refarray = NULL;
+                               ldap_free_urllist(srv);
+                               srv = NULL;
+
+                               /* Pull entries off end of queue so list always null terminated */
+                               for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
+                                       ;
+                               }
+                               refarray = lc->lconn_rebind_queue[j-1];
+                               lc->lconn_rebind_queue[j-1] = NULL;
+                               /* we pulled off last entry from queue, free queue */
+                               if ( j == 1 ) {
+                                       LDAP_FREE( lc->lconn_rebind_queue);
+                                       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:
+       LDAP_VFREE(refarray);
+       ldap_free_urllist(srv);
+       LDAP_FREE( *errstrp );
+       
+       if( rc == 0) {
+               *errstrp = NULL;
+               LDAP_FREE( unfollowed );
+               return count;
+       } else {
+               ld->ld_errno = LDAP_REFERRAL;
+               *errstrp = unfollowed;
+               return rc;
+       }
+}
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /*
  * XXX merging of errors in this routine needs to be improved
  */
@@ -579,13 +771,11 @@ int
 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 {
        int             rc, count, len, newdn;
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-       int             ldapref;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
        char            *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
        LDAPRequest     *origreq;
-       LDAPServer      *srv;
+       LDAPURLDesc     *srv;
        BerElement      *ber;
+       LDAPreqinfo  rinfo;
 
        Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
 
@@ -629,9 +819,6 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 
        /* parse out & follow referrals */
        for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-               ldapref = 0;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
                if (( p = strchr( ref, '\n' )) != NULL ) {
                        *p++ = '\0';
@@ -639,22 +826,16 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
                        p = NULL;
                }
 
+               ldap_pvt_hex_unescape( ref );
                len = strlen( ref );
+
+               /* FIXME: we should use the URL Parser */
+
                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 );
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-                       ldapref = 1;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
                        tmpref = ref + LDAP_LDAP_REF_STR_LEN;
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-               } 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;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
                } else {
                        Debug( LDAP_DEBUG_TRACE,
                            "ignoring unknown referral <%s>\n", ref, 0, 0 );
@@ -663,50 +844,55 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
                        continue;
                }
 
+               /* copy the complete referral for rebind process */
+               rinfo.ri_url = LDAP_STRDUP( ref );
+
                *hadrefp = 1;
+
                if (( refdn = strchr( tmpref, '/' )) != NULL ) {
                        *refdn++ = '\0';
-                       newdn = 1;
+                       newdn = refdn[0] != '?' && refdn[0] != '\0';
+                       if( !newdn ) refdn = NULL;
                } else {
                        newdn = 0;
                }
 
                if (( ber = re_encode_request( ld, origreq->lr_ber,
-                   ++ld->ld_msgid, &refdn )) == NULL ) {
+                   ++ld->ld_msgid, &refdn, &rinfo.ri_request )) == NULL ) {
                        return( -1 );
                }
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-               if ( ldapref ) {
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-                       if (( srv = (LDAPServer *)calloc( 1,
-                           sizeof( LDAPServer ))) == NULL ) {
+                       if (( srv = (LDAPURLDesc *)LDAP_CALLOC( 1,
+                           sizeof( LDAPURLDesc ))) == NULL ) {
                                ber_free( ber, 1 );
                                ld->ld_errno = LDAP_NO_MEMORY;
                                return( -1 );
                        }
 
-                       if (( srv->lsrv_host = ldap_strdup( tmpref )) == NULL ) {
-                               free( (char *)srv );
+                       if (( srv->lud_scheme = LDAP_STRDUP("ldap")) == NULL ) {
+                               LDAP_FREE( (char *)srv );
                                ber_free( ber, 1 );
                                ld->ld_errno = LDAP_NO_MEMORY;
                                return( -1 );
                        }
 
-                       if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
+                       if (( srv->lud_host = LDAP_STRDUP( tmpref )) == NULL ) {
+                               LDAP_FREE( (char *)srv );
+                               ber_free( ber, 1 );
+                               ld->ld_errno = LDAP_NO_MEMORY;
+                               return( -1 );
+                       }
+
+                       if (( ports = strchr( srv->lud_host, ':' )) != NULL ) {
                                *ports++ = '\0';
-                               srv->lsrv_port = atoi( ports );
+                               srv->lud_port = atoi( ports );
                        } else {
-                               srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
+                               srv->lud_port = (LDAP_INT_GLOBAL_OPT())->ldo_defport;
                        }
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-               } else {
-                       srv = dn2servers( ld, tmpref );
-               }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
+               rinfo.ri_msgid = origreq->lr_origid;
                if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
-                   lr, srv, NULL, 1 ) >= 0 ) {
+                   lr, srv, NULL, &rinfo ) >= 0 ) {
                        ++count;
                } else {
                        Debug( LDAP_DEBUG_ANY,
@@ -714,13 +900,17 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
                            ldap_err2string( ld->ld_errno ), 0, 0 );
                        rc = ldap_append_referral( ld, &unfollowed, ref );
                }
+               LDAP_FREE( rinfo.ri_url);
+
+               if (srv != NULL)
+                       ldap_free_urllist(srv);
 
                if ( !newdn && refdn != NULL ) {
-                       free( refdn );
+                       LDAP_FREE( refdn );
                }
        }
 
-       free( *errstrp );
+       LDAP_FREE( *errstrp );
        *errstrp = unfollowed;
 
        return(( rc == 0 ) ? count : rc );
@@ -734,11 +924,11 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s )
 
        if ( *referralsp == NULL ) {
                first = 1;
-               *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
+               *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
                    + 1 );
        } else {
                first = 0;
-               *referralsp = (char *)realloc( *referralsp,
+               *referralsp = (char *)LDAP_REALLOC( *referralsp,
                    strlen( *referralsp ) + strlen( s ) + 2 );
        }
 
@@ -760,71 +950,84 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s )
 
 
 static BerElement *
-re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
+re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, int *type )
 {
 /*
  * XXX this routine knows way too much about how the lber library works!
  */
-       unsigned long   along, tag;
-       long            ver;
+       ber_int_t       along;
+       ber_tag_t       tag;
+       ber_int_t       ver;
        int             rc;
        BerElement      tmpber, *ber;
        char            *orig_dn;
 
        Debug( LDAP_DEBUG_TRACE,
-           "re_encode_request: new msgid %d, new dn <%s>\n",
-           msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
+           "re_encode_request: new msgid %ld, new dn <%s>\n",
+           (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
 
        tmpber = *origber;
 
        /*
-        * all LDAP requests are sequences that start with a message id,
-        * followed by a sequence that is tagged with the operation code
+        * 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.
         */
-       if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
-           ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
-                ld->ld_errno = LDAP_DECODING_ERROR;
+       rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
+
+       if ( rc == LBER_ERROR ) {
+               ld->ld_errno = LDAP_DECODING_ERROR;
                return( NULL );
        }
 
-        if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
-                return( NULL );
-        }
+       assert( tag != 0);
+       if ( tag == LDAP_REQ_BIND ) {
+               /* bind requests have a version number before the DN & other stuff */
+               rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
 
-       /* bind requests have a version number before the DN & other stuff */
-       if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
-           LBER_DEFAULT ) {
-                ld->ld_errno = LDAP_DECODING_ERROR;
-               ber_free( ber, 1 );
-               return( NULL );
+       } 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 );
        }
 
-       /* the rest of the request is the DN followed by other stuff */
-       if ( ber_get_stringa( &tmpber, &orig_dn ) == LBER_DEFAULT ) {
-               ber_free( ber, 1 );
-               return( NULL );
+       if( rc == LBER_ERROR ) {
+               ld->ld_errno = LDAP_DECODING_ERROR;
+               return NULL;
        }
 
        if ( *dnp == NULL ) {
                *dnp = orig_dn;
        } else {
-               free( orig_dn );
+               LDAP_FREE( orig_dn );
+       }
+
+       if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
+               return( NULL );
        }
 
        if ( tag == LDAP_REQ_BIND ) {
-               rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
+               rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
+       } else if ( tag == LDAP_REQ_DELETE ) {
+               rc = ber_printf( ber, "{itsN}", msgid, tag, *dnp );
        } else {
-               rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
+               rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp );
        }
 
        if ( rc == -1 ) {
+               ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
                return( NULL );
        }
 
-       if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
-           tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
-           ber_printf( ber, "}}" ) == -1 ) {
+       if ( tag != LDAP_REQ_DELETE && (
+               ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
+               != ( tmpber.ber_end - tmpber.ber_ptr ) ||
+           ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
+       {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
                return( NULL );
@@ -834,20 +1037,24 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
                Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
                    0, 0, 0 );
-               ber_dump( ber, 0 );
+               ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
        }
 #endif /* LDAP_DEBUG */
 
+       *type = tag;    /* return request type */
        return( ber );
 }
 
 
 LDAPRequest *
-ldap_find_request_by_msgid( LDAP *ld, int msgid )
+ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
 {
        LDAPRequest     *lr;
 
        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
+               if( lr->lr_status == LDAP_REQST_COMPLETED ) {
+                       continue;       /* Skip completed requests */
+               }
                if ( msgid == lr->lr_msgid ) {
                        break;
                }
@@ -855,88 +1062,5 @@ ldap_find_request_by_msgid( LDAP *ld, int msgid )
 
        return( lr );
 }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-
-
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-static LDAPServer *
-dn2servers( LDAP *ld, char *dn )       /* dn can also be a domain.... */
-{
-       char            *p, *domain, *host, *server_dn, **dxs;
-       int             i, port;
-       LDAPServer      *srvlist, *prevsrv, *srv;
-
-       if (( domain = strrchr( dn, '@' )) != NULL ) {
-               ++domain;
-       } else {
-               domain = dn;
-       }
 
-       if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
-               ld->ld_errno = LDAP_NO_MEMORY;
-               return( NULL );
-       }
 
-       srvlist = NULL;
-
-       for ( i = 0; dxs[ i ] != NULL; ++i ) {
-               port = openldap_ldap_global_options.ldo_defport;
-               server_dn = NULL;
-               if ( strchr( dxs[ i ], ':' ) == NULL ) {
-                       host = dxs[ i ];
-               } else if ( strlen( dxs[ i ] ) >= 7 &&
-                   strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
-                       host = dxs[ i ] + 7;
-                       if (( p = strchr( host, ':' )) == NULL ) {
-                               p = host;
-                       } else {
-                               *p++ = '\0';
-                               port = atoi( p );
-                       }
-                       if (( p = strchr( p, '/' )) != NULL ) {
-                               server_dn = ++p;
-                               if ( *server_dn == '\0' ) {
-                                       server_dn = NULL;
-                               }
-                       }
-               } else {
-                       host = NULL;
-               }
-
-               if ( host != NULL ) {   /* found a server we can use */
-                       if (( srv = (LDAPServer *)calloc( 1,
-                           sizeof( LDAPServer ))) == NULL ) {
-                               free_servers( srvlist );
-                               srvlist = NULL;
-                               break;          /* exit loop & return */
-                       }
-
-                       /* add to end of list of servers */
-                       if ( srvlist == NULL ) {
-                               srvlist = srv;
-                       } else {
-                               prevsrv->lsrv_next = srv;
-                       }
-                       prevsrv = srv;
-                       
-                       /* copy in info. */
-                       if (( srv->lsrv_host = ldap_strdup( host )) == NULL ||
-                           ( server_dn != NULL && ( srv->lsrv_dn =
-                           ldap_strdup( server_dn )) == NULL )) {
-                               free_servers( srvlist );
-                               srvlist = NULL;
-                               break;          /* exit loop & return */
-                       }
-                       srv->lsrv_port = port;
-               }
-       }
-
-       ldap_value_free( dxs );
-
-       if ( srvlist == NULL ) {
-               ld->ld_errno = LDAP_SERVER_DOWN;
-       }
-
-       return( srvlist );
-}
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */