Back to index

lightning-sunbird  0.9+nobinonly
request.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1995 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  request.c - sending of ldap requests; handling of referrals
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 static LDAPConn *find_connection( LDAP *ld, LDAPServer *srv, int any );
00054 static void use_connection( LDAP *ld, LDAPConn *lc );
00055 static void free_servers( LDAPServer *srvlist );
00056 static int chase_one_referral( LDAP *ld, LDAPRequest *lr, LDAPRequest *origreq,
00057     char *refurl, char *desc, int *unknownp );
00058 static int re_encode_request( LDAP *ld, BerElement *origber,
00059     int msgid, LDAPURLDesc *ludp, BerElement **berp );
00060 
00061 #ifdef LDAP_DNS
00062 static LDAPServer *dn2servers( LDAP *ld, char *dn );
00063 #endif /* LDAP_DNS */
00064 
00065 
00066 /* returns an LDAP error code and also sets error inside LDAP * */
00067 int
00068 nsldapi_alloc_ber_with_options( LDAP *ld, BerElement **berp )
00069 {
00070        int    err;
00071 
00072        LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
00073        if (( *berp = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
00074               err = LDAP_NO_MEMORY;
00075               LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00076        } else {
00077               err = LDAP_SUCCESS;
00078 #ifdef STR_TRANSLATION
00079               nsldapi_set_ber_options( ld, *berp );
00080 #endif /* STR_TRANSLATION */
00081        }
00082        LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00083 
00084        return( err );
00085 }
00086 
00087 
00088 void
00089 nsldapi_set_ber_options( LDAP *ld, BerElement *ber )
00090 {
00091        ber->ber_options = ld->ld_lberoptions;
00092 #ifdef STR_TRANSLATION
00093        if (( ld->ld_lberoptions & LBER_OPT_TRANSLATE_STRINGS ) != 0 ) {
00094               ber_set_string_translators( ber,
00095                   ld->ld_lber_encode_translate_proc,
00096                   ld->ld_lber_decode_translate_proc );
00097        }
00098 #endif /* STR_TRANSLATION */
00099 }
00100 
00101 
00102 /* returns the message id of the request or -1 if an error occurs */
00103 int
00104 nsldapi_send_initial_request( LDAP *ld, int msgid, unsigned long msgtype,
00105        char *dn, BerElement *ber )
00106 {
00107        LDAPServer    *servers;
00108        
00109        LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_initial_request\n", 0,0,0 );
00110 
00111 #ifdef LDAP_DNS
00112        LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
00113        if (( ld->ld_options & LDAP_BITOPT_DNS ) != 0 && ldap_is_dns_dn( dn )) {
00114               if (( servers = dn2servers( ld, dn )) == NULL ) {
00115                      ber_free( ber, 1 );
00116                      LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00117                      return( -1 );
00118               }
00119 
00120 #ifdef LDAP_DEBUG
00121               if ( ldap_debug & LDAP_DEBUG_TRACE ) {
00122                      LDAPServer    *srv;
00123                      char    msg[256];
00124 
00125                      for ( srv = servers; srv != NULL;
00126                          srv = srv->lsrv_next ) {
00127                             sprintf( msg,
00128                                 "LDAP server %s:  dn %s, port %d\n",
00129                                 srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
00130                                 "(default)" : srv->lsrv_dn,
00131                                 srv->lsrv_port );
00132                             ber_err_print( msg );
00133                      }
00134               }
00135 #endif /* LDAP_DEBUG */
00136        } else {
00137 #endif /* LDAP_DNS */
00138               /*
00139                * use of DNS is turned off or this is an LDAP DN...
00140                * use our default connection
00141                */
00142               servers = NULL;
00143 #ifdef LDAP_DNS
00144        }      
00145        LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00146 #endif /* LDAP_DNS */
00147 
00148        return( nsldapi_send_server_request( ld, ber, msgid, NULL,
00149            servers, NULL, ( msgtype == LDAP_REQ_BIND ) ? dn : NULL, 0 ));
00150 }
00151 
00152 
00153 /* returns the message id of the request or -1 if an error occurs */
00154 int
00155 nsldapi_send_server_request(
00156     LDAP *ld,               /* session handle */
00157     BerElement *ber,        /* message to send */
00158     int msgid,                     /* ID of message to send */
00159     LDAPRequest *parentreq, /* non-NULL for referred requests */
00160     LDAPServer *srvlist,    /* servers to connect to (NULL for default) */
00161     LDAPConn *lc,           /* connection to use (NULL for default) */
00162     char *bindreqdn,        /* non-NULL for bind requests */
00163     int bind                /* perform a bind after opening new conn.? */
00164 )
00165 {
00166        LDAPRequest   *lr;
00167        int           err;
00168        int           incparent;    /* did we bump parent's ref count? */
00169 
00170        LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_server_request\n", 0, 0, 0 );
00171 
00172        incparent = 0;
00173        LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
00174        if ( lc == NULL ) {
00175               if ( srvlist == NULL ) {
00176                      if ( ld->ld_defconn == NULL ) {
00177                             LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
00178                             if ( bindreqdn == NULL && ( ld->ld_options
00179                                 & LDAP_BITOPT_RECONNECT ) != 0 ) {
00180                                    LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN,
00181                                        NULL, NULL );
00182                                    ber_free( ber, 1 );
00183                                    LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00184                                    LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00185                                    return( -1 );
00186                             }
00187                             LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00188 
00189                             if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
00190                                    ber_free( ber, 1 );
00191                                    LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00192                                    return( -1 );
00193                             }
00194                      }
00195                      lc = ld->ld_defconn;
00196               } else {
00197                      if (( lc = find_connection( ld, srvlist, 1 )) ==
00198                          NULL ) {
00199                             if ( bind && (parentreq != NULL) ) {
00200                                    /* Remember the bind in the parent */
00201                                    incparent = 1;
00202                                    ++parentreq->lr_outrefcnt;
00203                             }
00204 
00205                             lc = nsldapi_new_connection( ld, &srvlist, 0,
00206                                    1, bind );
00207                      }
00208                      free_servers( srvlist );
00209               }
00210        }
00211 
00212 
00213     /*
00214      * the logic here is:
00215      * if 
00216      * 1. no connections exists, 
00217      * or 
00218      * 2. if the connection is either not in the connected 
00219      *     or connecting state in an async io model
00220      * or 
00221      * 3. the connection is notin a connected state with normal (non async io)
00222      */
00223        if (   lc == NULL
00224               || (  (ld->ld_options & LDAP_BITOPT_ASYNC 
00225                && lc->lconn_status != LDAP_CONNST_CONNECTING
00226                   && lc->lconn_status != LDAP_CONNST_CONNECTED)
00227               || (!(ld->ld_options & LDAP_BITOPT_ASYNC )
00228                   && lc->lconn_status != LDAP_CONNST_CONNECTED) ) ) {
00229 
00230               ber_free( ber, 1 );
00231               if ( lc != NULL ) {
00232                      LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL );
00233               }
00234               if ( incparent ) {
00235                      /* Forget about the bind */
00236                      --parentreq->lr_outrefcnt; 
00237               }
00238               LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00239               return( -1 );
00240        }
00241 
00242        use_connection( ld, lc );
00243        if (( lr = (LDAPRequest *)NSLDAPI_CALLOC( 1, sizeof( LDAPRequest ))) ==
00244            NULL || ( bindreqdn != NULL && ( bindreqdn =
00245            nsldapi_strdup( bindreqdn )) == NULL )) {
00246               if ( lr != NULL ) {
00247                      NSLDAPI_FREE( lr );
00248               }
00249               LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
00250               nsldapi_free_connection( ld, lc, NULL, NULL, 0, 0 );
00251               ber_free( ber, 1 );
00252               if ( incparent ) {
00253                      /* Forget about the bind */
00254                      --parentreq->lr_outrefcnt; 
00255               }
00256               LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00257               return( -1 );
00258        } 
00259        lr->lr_binddn = bindreqdn;
00260        lr->lr_msgid = msgid;
00261        lr->lr_status = LDAP_REQST_INPROGRESS;
00262        lr->lr_res_errno = LDAP_SUCCESS;   /* optimistic */
00263        lr->lr_ber = ber;
00264        lr->lr_conn = lc;
00265 
00266        if ( parentreq != NULL ) {  /* sub-request */
00267               if ( !incparent ) { 
00268                      /* Increment if we didn't do it before the bind */
00269                      ++parentreq->lr_outrefcnt;
00270               }
00271               lr->lr_origid = parentreq->lr_origid;
00272               lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
00273               lr->lr_parent = parentreq;
00274               if ( parentreq->lr_child != NULL ) {
00275                      lr->lr_sibling = parentreq->lr_child;
00276               }
00277               parentreq->lr_child = lr;
00278        } else {                    /* original request */
00279               lr->lr_origid = lr->lr_msgid;
00280        }
00281 
00282        LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
00283        if (( lr->lr_next = ld->ld_requests ) != NULL ) {
00284               lr->lr_next->lr_prev = lr;
00285        }
00286        ld->ld_requests = lr;
00287        lr->lr_prev = NULL;
00288 
00289        if (( err = nsldapi_ber_flush( ld, lc->lconn_sb, ber, 0, 1 )) != 0 ) {
00290 
00291               /* need to continue write later */
00292               if (ld->ld_options & LDAP_BITOPT_ASYNC && err == -2 ) { 
00293                      lr->lr_status = LDAP_REQST_WRITING;
00294                      nsldapi_iostatus_interest_write( ld, lc->lconn_sb );
00295               } else {
00296 
00297                      LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL );
00298                      nsldapi_free_request( ld, lr, 0 );
00299                      nsldapi_free_connection( ld, lc, NULL, NULL, 0, 0 );
00300                      LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
00301                      LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00302                      return( -1 );
00303               }
00304 
00305        } else {
00306               if ( parentreq == NULL ) {
00307                      ber->ber_end = ber->ber_ptr;
00308                      ber->ber_ptr = ber->ber_buf;
00309               }
00310 
00311               /* sent -- waiting for a response */
00312               if (ld->ld_options & LDAP_BITOPT_ASYNC) {
00313                      lc->lconn_status = LDAP_CONNST_CONNECTED;
00314               }
00315 
00316               nsldapi_iostatus_interest_read( ld, lc->lconn_sb );
00317        }
00318        LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
00319        LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00320 
00321        LDAP_SET_LDERRNO( ld, LDAP_SUCCESS, NULL, NULL );
00322        return( msgid );
00323 }
00324 
00325 
00326 /*
00327  * returns -1 if a fatal error occurs.  If async is non-zero and the flush
00328  * would block, -2 is returned.
00329  */
00330 int
00331 nsldapi_ber_flush( LDAP *ld, Sockbuf *sb, BerElement *ber, int freeit,
00332        int async )
00333 {
00334        int    terrno;
00335 
00336        for ( ;; ) {
00337                /*
00338                 * ber_flush() doesn't set errno on EOF, so we pre-set it to
00339                 * zero to avoid getting tricked by leftover "EAGAIN" errors
00340                 */
00341               LDAP_SET_ERRNO( ld, 0 );
00342 
00343               if ( ber_flush( sb, ber, freeit ) == 0 ) {
00344                      return( 0 );  /* success */
00345               }
00346 
00347               terrno = LDAP_GET_ERRNO( ld );
00348 
00349         if (ld->ld_options & LDAP_BITOPT_ASYNC) {
00350             if ( terrno != 0 && !NSLDAPI_ERRNO_IO_INPROGRESS( terrno )) {
00351                 nsldapi_connection_lost_nolock( ld, sb );
00352                 return( -1 );      /* fatal error */
00353             }
00354         }
00355         else if ( !NSLDAPI_ERRNO_IO_INPROGRESS( terrno )) {
00356 
00357                      nsldapi_connection_lost_nolock( ld, sb );
00358                      return( -1 ); /* fatal error */
00359               }
00360 
00361               if ( async ) {
00362                      return( -2 ); /* would block */
00363               }
00364        }
00365 }
00366 
00367 LDAPConn *
00368 nsldapi_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
00369        int connect, int bind )
00370 {
00371     int       rc;
00372     
00373        LDAPConn      *lc;
00374        LDAPServer    *prevsrv, *srv;
00375        Sockbuf              *sb = NULL;
00376 
00377        /*
00378         * make a new LDAP server connection
00379         */
00380        if (( lc = (LDAPConn *)NSLDAPI_CALLOC( 1, sizeof( LDAPConn ))) == NULL
00381            || ( !use_ldsb && ( sb = ber_sockbuf_alloc()) == NULL )) {
00382               if ( lc != NULL ) {
00383                      NSLDAPI_FREE( (char *)lc );
00384               }
00385               LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
00386               return( NULL );
00387        }
00388 
00389        LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
00390        if ( !use_ldsb ) {
00391               /*
00392                * we have allocated a new sockbuf
00393                * set I/O routines to match those in default LDAP sockbuf
00394                */
00395               IFP                         sb_fn;
00396               struct lber_x_ext_io_fns    extiofns;
00397               
00398               extiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
00399 
00400               if ( ber_sockbuf_get_option( ld->ld_sbp,
00401                   LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns ) == 0 ) {
00402                      ber_sockbuf_set_option( sb,
00403                          LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns );
00404               }
00405               if ( ber_sockbuf_get_option( ld->ld_sbp,
00406                   LBER_SOCKBUF_OPT_READ_FN, (void *)&sb_fn ) == 0
00407                   && sb_fn != NULL ) {
00408                      ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_READ_FN,
00409                          (void *)sb_fn );
00410               }
00411               if ( ber_sockbuf_get_option( ld->ld_sbp,
00412                   LBER_SOCKBUF_OPT_WRITE_FN, (void *)&sb_fn ) == 0
00413                   && sb_fn != NULL ) {
00414                      ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_WRITE_FN,
00415                          (void *)sb_fn );
00416               }
00417        }
00418 
00419        lc->lconn_sb = ( use_ldsb ) ? ld->ld_sbp : sb;
00420        lc->lconn_version = ld->ld_version;       /* inherited */
00421        LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
00422 
00423        if ( connect ) {
00424               prevsrv = NULL;
00425         /* 
00426          * save the return code for later
00427          */ 
00428               for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
00429                      rc = nsldapi_connect_to_host( ld, lc->lconn_sb,
00430                                srv->lsrv_host, srv->lsrv_port,
00431                             (  srv->lsrv_options & LDAP_SRV_OPT_SECURE ) != 0,
00432                                    &lc->lconn_krbinstance );
00433                      if (rc != -1) {
00434                             break;
00435                      }
00436                      prevsrv = srv;
00437               }
00438 
00439               if ( srv == NULL ) {
00440                   if ( !use_ldsb ) {
00441                      NSLDAPI_FREE( (char *)lc->lconn_sb );
00442                   }
00443                   NSLDAPI_FREE( (char *)lc );
00444                   /* nsldapi_open_ldap_connection has already set ld_errno */
00445                   return( NULL );
00446               }
00447 
00448               if ( prevsrv == NULL ) {
00449                   *srvlistp = srv->lsrv_next;
00450               } else {
00451                   prevsrv->lsrv_next = srv->lsrv_next;
00452               }
00453               lc->lconn_server = srv;
00454        }
00455 
00456        if (ld->ld_options & LDAP_BITOPT_ASYNC && rc == -2)
00457     {
00458         lc->lconn_status = LDAP_CONNST_CONNECTING;
00459     }
00460     else {
00461         lc->lconn_status = LDAP_CONNST_CONNECTED;
00462     }
00463     
00464        lc->lconn_next = ld->ld_conns;
00465        ld->ld_conns = lc;
00466 
00467        /*
00468         * XXX for now, we always do a synchronous bind.  This will have
00469         * to change in the long run...
00470         */
00471        if ( bind ) {
00472               int           err, lderr, freepasswd, authmethod;
00473               char          *binddn, *passwd;
00474               LDAPConn      *savedefconn;
00475 
00476               freepasswd = err = 0;
00477 
00478               if ( ld->ld_rebind_fn == NULL ) {
00479                      binddn = passwd = "";
00480                      authmethod = LDAP_AUTH_SIMPLE;
00481               } else {
00482                      if (( lderr = (*ld->ld_rebind_fn)( ld, &binddn, &passwd,
00483                          &authmethod, 0, ld->ld_rebind_arg ))
00484                          == LDAP_SUCCESS ) {
00485                             freepasswd = 1;
00486                      } else {
00487                             LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00488                             err = -1;
00489                      }
00490               }
00491 
00492 
00493               if ( err == 0 ) {
00494                      savedefconn = ld->ld_defconn;
00495                      ld->ld_defconn = lc;
00496                      ++lc->lconn_refcnt;  /* avoid premature free */
00497 
00498                      /*
00499                       * when binding, we will back down as low as LDAPv2
00500                       * if we get back "protocol error" from bind attempts
00501                       */
00502                      for ( ;; ) {
00503                             /* LDAP_MUTEX_UNLOCK(ld, LDAP_CONN_LOCK); */
00504                             if (( lderr = ldap_bind_s( ld, binddn, passwd,
00505                                 authmethod )) == LDAP_SUCCESS ) {
00506                                    /* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */
00507                                    break;
00508                             }
00509                             /* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */
00510                             if ( lc->lconn_version <= LDAP_VERSION2
00511                                 || lderr != LDAP_PROTOCOL_ERROR ) {
00512                                    err = -1;
00513                                    break;
00514                             }
00515                             --lc->lconn_version; /* try lower version */
00516                      }
00517                      --lc->lconn_refcnt;
00518                      ld->ld_defconn = savedefconn;
00519               }
00520 
00521               if ( freepasswd ) {
00522                      (*ld->ld_rebind_fn)( ld, &binddn, &passwd,
00523                             &authmethod, 1, ld->ld_rebind_arg );
00524               }
00525 
00526               if ( err != 0 ) {
00527                      nsldapi_free_connection( ld, lc, NULL, NULL, 1, 0 );
00528                      lc = NULL;
00529               }
00530        }
00531 
00532        return( lc );
00533 }
00534 
00535 
00536 #define LDAP_CONN_SAMEHOST( h1, h2 ) \
00537        (( (h1) == NULL && (h2) == NULL ) || \
00538        ( (h1) != NULL && (h2) != NULL && strcasecmp( (h1), (h2) ) == 0 ))
00539 
00540 static LDAPConn *
00541 find_connection( LDAP *ld, LDAPServer *srv, int any )
00542 /*
00543  * return an existing connection (if any) to the server srv
00544  * if "any" is non-zero, check for any server in the "srv" chain
00545  */
00546 {
00547        LDAPConn      *lc;
00548        LDAPServer    *ls;
00549 
00550        for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
00551               for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
00552                      if ( LDAP_CONN_SAMEHOST( ls->lsrv_host,
00553                          lc->lconn_server->lsrv_host )
00554                          && ls->lsrv_port == lc->lconn_server->lsrv_port
00555                          && ls->lsrv_options ==
00556                          lc->lconn_server->lsrv_options ) {
00557                             return( lc );
00558                      }
00559                      if ( !any ) {
00560                             break;
00561                      }
00562               }
00563        }
00564 
00565        return( NULL );
00566 }
00567 
00568 
00569 
00570 static void
00571 use_connection( LDAP *ld, LDAPConn *lc )
00572 {
00573        ++lc->lconn_refcnt;
00574        lc->lconn_lastused = time( 0 );
00575 }
00576 
00577 
00578 void
00579 nsldapi_free_connection( LDAP *ld, LDAPConn *lc, LDAPControl **serverctrls,
00580     LDAPControl **clientctrls, int force, int unbind )
00581 {
00582        LDAPConn      *tmplc, *prevlc;
00583 
00584        LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection\n", 0, 0, 0 );
00585 
00586        if ( force || --lc->lconn_refcnt <= 0 ) {
00587               if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
00588                      nsldapi_iostatus_interest_clear( ld, lc->lconn_sb );
00589                      if ( unbind ) {
00590                             nsldapi_send_unbind( ld, lc->lconn_sb,
00591                                 serverctrls, clientctrls );
00592                      }
00593               }
00594               nsldapi_close_connection( ld, lc->lconn_sb );
00595               prevlc = NULL;
00596               for ( tmplc = ld->ld_conns; tmplc != NULL;
00597                   tmplc = tmplc->lconn_next ) {
00598                      if ( tmplc == lc ) {
00599                             if ( prevlc == NULL ) {
00600                                 ld->ld_conns = tmplc->lconn_next;
00601                             } else {
00602                                 prevlc->lconn_next = tmplc->lconn_next;
00603                             }
00604                             break;
00605                      }
00606                      prevlc = tmplc;
00607               }
00608               free_servers( lc->lconn_server );
00609               if ( lc->lconn_krbinstance != NULL ) {
00610                      NSLDAPI_FREE( lc->lconn_krbinstance );
00611               }
00612               /*
00613                * if this is the default connection (lc->lconn_sb==ld->ld_sbp)
00614                * we do not free the Sockbuf here since it will be freed
00615                * later inside ldap_unbind().
00616                */
00617               if ( lc->lconn_sb != ld->ld_sbp ) {
00618                      ber_sockbuf_free( lc->lconn_sb );
00619                      lc->lconn_sb = NULL;
00620               }
00621               if ( lc->lconn_ber != NULLBER ) {
00622                      ber_free( lc->lconn_ber, 1 );
00623               }
00624               if ( lc->lconn_binddn != NULL ) {
00625                      NSLDAPI_FREE( lc->lconn_binddn );
00626               }
00627               NSLDAPI_FREE( lc );
00628               LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection: actually freed\n",
00629                   0, 0, 0 );
00630        } else {
00631               lc->lconn_lastused = time( 0 );
00632               LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection: refcnt %d\n",
00633                   lc->lconn_refcnt, 0, 0 );
00634        }
00635 }
00636 
00637 
00638 #ifdef LDAP_DEBUG
00639 void
00640 nsldapi_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
00641 {
00642        LDAPConn      *lc;
00643        char        msg[256];
00644 /* CTIME for this platform doesn't use this. */
00645 #if !defined(SUNOS4) && !defined(_WIN32) && !defined(LINUX) && !defined(macintosh)
00646        char          buf[26];
00647 #endif
00648 
00649        sprintf( msg, "** Connection%s:\n", all ? "s" : "" );
00650        ber_err_print( msg );
00651        for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
00652               if ( lc->lconn_server != NULL ) {
00653                      sprintf( msg, "* host: %s  port: %d  secure: %s%s\n",
00654                          ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
00655                          : lc->lconn_server->lsrv_host,
00656                          lc->lconn_server->lsrv_port,
00657                          ( lc->lconn_server->lsrv_options &
00658                          LDAP_SRV_OPT_SECURE ) ? "Yes" :
00659                          "No", ( lc->lconn_sb == ld->ld_sbp ) ?
00660                          "  (default)" : "" );
00661                      ber_err_print( msg );
00662               }
00663               sprintf( msg, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
00664                   ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
00665                   "NeedSocket" : ( lc->lconn_status ==
00666                   LDAP_CONNST_CONNECTING ) ? "Connecting" :
00667                   ( lc->lconn_status == LDAP_CONNST_DEAD ) ? "Dead" :
00668                   "Connected" );
00669               ber_err_print( msg );
00670               sprintf( msg, "  last used: %s",
00671                   NSLDAPI_CTIME( (time_t *) &lc->lconn_lastused, buf,
00672                             sizeof(buf) ));
00673               ber_err_print( msg );
00674               if ( lc->lconn_ber != NULLBER ) {
00675                      ber_err_print( "  partial response has been received:\n" );
00676                      ber_dump( lc->lconn_ber, 1 );
00677               }
00678               ber_err_print( "\n" );
00679 
00680               if ( !all ) {
00681                      break;
00682               }
00683        }
00684 }
00685 
00686 
00687 void
00688 nsldapi_dump_requests_and_responses( LDAP *ld )
00689 {
00690        LDAPRequest   *lr;
00691        LDAPMessage   *lm, *l;
00692        char        msg[256];
00693 
00694        ber_err_print( "** Outstanding Requests:\n" );
00695        LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
00696        if (( lr = ld->ld_requests ) == NULL ) {
00697               ber_err_print( "   Empty\n" );
00698        }
00699        for ( ; lr != NULL; lr = lr->lr_next ) {
00700            sprintf( msg, " * msgid %d,  origid %d, status %s\n",
00701               lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
00702               LDAP_REQST_INPROGRESS ) ? "InProgress" :
00703               ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
00704               ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
00705               ( lr->lr_status == LDAP_REQST_CONNDEAD ) ? "Dead" :
00706               "Writing" );
00707            ber_err_print( msg );
00708            sprintf( msg, "   outstanding referrals %d, parent count %d\n",
00709                   lr->lr_outrefcnt, lr->lr_parentcnt );
00710            ber_err_print( msg );
00711            if ( lr->lr_binddn != NULL ) {
00712                   sprintf( msg, "   pending bind DN: <%s>\n", lr->lr_binddn );
00713                   ber_err_print( msg );
00714            }
00715        }
00716        LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
00717 
00718        ber_err_print( "** Response Queue:\n" );
00719        LDAP_MUTEX_LOCK( ld, LDAP_RESP_LOCK );
00720        if (( lm = ld->ld_responses ) == NULLMSG ) {
00721               ber_err_print( "   Empty\n" );
00722        }
00723        for ( ; lm != NULLMSG; lm = lm->lm_next ) {
00724               sprintf( msg, " * msgid %d,  type %d\n",
00725                   lm->lm_msgid, lm->lm_msgtype );
00726               ber_err_print( msg );
00727               if (( l = lm->lm_chain ) != NULL ) {
00728                      ber_err_print( "   chained responses:\n" );
00729                      for ( ; l != NULLMSG; l = l->lm_chain ) {
00730                             sprintf( msg,
00731                                 "  * msgid %d,  type %d\n",
00732                                 l->lm_msgid, l->lm_msgtype );
00733                             ber_err_print( msg );
00734                      }
00735               }
00736        }
00737        LDAP_MUTEX_UNLOCK( ld, LDAP_RESP_LOCK );
00738 }
00739 #endif /* LDAP_DEBUG */
00740 
00741 
00742 void
00743 nsldapi_free_request( LDAP *ld, LDAPRequest *lr, int free_conn )
00744 {
00745        LDAPRequest   *tmplr, *nextlr;
00746 
00747        LDAPDebug( LDAP_DEBUG_TRACE,
00748               "nsldapi_free_request 0x%x (origid %d, msgid %d)\n",
00749               lr, lr->lr_origid, lr->lr_msgid );
00750 
00751        if ( lr->lr_parent != NULL ) {
00752               --lr->lr_parent->lr_outrefcnt;
00753        }
00754 
00755        /* free all of our spawned referrals (child requests) */
00756        for ( tmplr = lr->lr_child; tmplr != NULL; tmplr = nextlr ) {
00757               nextlr = tmplr->lr_sibling;
00758               nsldapi_free_request( ld, tmplr, free_conn );
00759        }
00760 
00761        if ( free_conn ) {
00762               nsldapi_free_connection( ld, lr->lr_conn, NULL, NULL, 0, 1 );
00763        }
00764 
00765        if ( lr->lr_prev == NULL ) {
00766               ld->ld_requests = lr->lr_next;
00767        } else {
00768               lr->lr_prev->lr_next = lr->lr_next;
00769        }
00770 
00771        if ( lr->lr_next != NULL ) {
00772               lr->lr_next->lr_prev = lr->lr_prev;
00773        }
00774 
00775        if ( lr->lr_ber != NULL ) {
00776               ber_free( lr->lr_ber, 1 );
00777        }
00778 
00779        if ( lr->lr_res_error != NULL ) {
00780               NSLDAPI_FREE( lr->lr_res_error );
00781        }
00782 
00783        if ( lr->lr_res_matched != NULL ) {
00784               NSLDAPI_FREE( lr->lr_res_matched );
00785        }
00786 
00787        if ( lr->lr_binddn != NULL ) {
00788               NSLDAPI_FREE( lr->lr_binddn );
00789        }
00790        NSLDAPI_FREE( lr );
00791 }
00792 
00793 
00794 static void
00795 free_servers( LDAPServer *srvlist )
00796 {
00797     LDAPServer       *nextsrv;
00798 
00799     while ( srvlist != NULL ) {
00800        nextsrv = srvlist->lsrv_next;
00801        if ( srvlist->lsrv_dn != NULL ) {
00802               NSLDAPI_FREE( srvlist->lsrv_dn );
00803        }
00804        if ( srvlist->lsrv_host != NULL ) {
00805               NSLDAPI_FREE( srvlist->lsrv_host );
00806        }
00807        NSLDAPI_FREE( srvlist );
00808        srvlist = nextsrv;
00809     }
00810 }
00811 
00812 
00813 /*
00814  * Initiate chasing of LDAPv2+ (Umich extension) referrals.
00815  *
00816  * Returns an LDAP error code.
00817  *
00818  * Note that *hadrefp will be set to 1 if one or more referrals were found in
00819  * "*errstrp" (even if we can't chase them) and zero if none were found.
00820  * 
00821  * XXX merging of errors in this routine needs to be improved.
00822  */
00823 int
00824 nsldapi_chase_v2_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp,
00825     int *totalcountp, int *chasingcountp )
00826 {
00827        char          *p, *ref, *unfollowed;
00828        LDAPRequest   *origreq;
00829        int           rc, tmprc, len, unknown;
00830 
00831        LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_chase_v2_referrals\n", 0, 0, 0 );
00832 
00833        *totalcountp = *chasingcountp = 0;
00834 
00835        if ( *errstrp == NULL ) {
00836               return( LDAP_SUCCESS );
00837        }
00838 
00839        len = strlen( *errstrp );
00840        for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
00841               if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
00842                   LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
00843                      *p = '\0';
00844                      p += LDAP_REF_STR_LEN;
00845                      break;
00846               }
00847        }
00848 
00849        if ( len < LDAP_REF_STR_LEN ) {
00850               return( LDAP_SUCCESS );
00851        }
00852 
00853        if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
00854               LDAPDebug( LDAP_DEBUG_TRACE,
00855                   "more than %d referral hops (dropping)\n",
00856                   ld->ld_refhoplimit, 0, 0 );
00857               return( LDAP_REFERRAL_LIMIT_EXCEEDED );
00858        }
00859 
00860        /* find original request */
00861        for ( origreq = lr; origreq->lr_parent != NULL;
00862             origreq = origreq->lr_parent ) {
00863               ;
00864        }
00865 
00866        unfollowed = NULL;
00867        rc = LDAP_SUCCESS;
00868 
00869        /* parse out & follow referrals */
00870        for ( ref = p; rc == LDAP_SUCCESS && ref != NULL; ref = p ) {
00871               if (( p = strchr( ref, '\n' )) != NULL ) {
00872                      *p++ = '\0';
00873               } else {
00874                      p = NULL;
00875               }
00876 
00877               ++*totalcountp;
00878 
00879               rc = chase_one_referral( ld, lr, origreq, ref, "v2 referral",
00880                   &unknown );
00881 
00882               if ( rc != LDAP_SUCCESS || unknown ) {
00883                      if (( tmprc = nsldapi_append_referral( ld, &unfollowed,
00884                          ref )) != LDAP_SUCCESS ) {
00885                             rc = tmprc;
00886                      }
00887               } else {
00888                      ++*chasingcountp;
00889               }
00890        }
00891 
00892        NSLDAPI_FREE( *errstrp );
00893        *errstrp = unfollowed;
00894 
00895        return( rc );
00896 }
00897 
00898 
00899 /* returns an LDAP error code */
00900 int
00901 nsldapi_chase_v3_refs( LDAP *ld, LDAPRequest *lr, char **v3refs,
00902     int is_reference, int *totalcountp, int *chasingcountp )
00903 {
00904        int           i, rc, unknown;
00905        LDAPRequest   *origreq;
00906 
00907        *totalcountp = *chasingcountp = 0;
00908 
00909        if ( v3refs == NULL || v3refs[0] == NULL ) {
00910               return( LDAP_SUCCESS );
00911        }
00912 
00913        *totalcountp = 1;
00914 
00915        if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
00916               LDAPDebug( LDAP_DEBUG_TRACE,
00917                   "more than %d referral hops (dropping)\n",
00918                   ld->ld_refhoplimit, 0, 0 );
00919               return( LDAP_REFERRAL_LIMIT_EXCEEDED );
00920        }
00921 
00922        /* find original request */
00923        for ( origreq = lr; origreq->lr_parent != NULL;
00924            origreq = origreq->lr_parent ) {
00925               ;
00926        }
00927 
00928        /*
00929         * in LDAPv3, we just need to follow one referral in the set.
00930         * we dp this by stopping as soon as we succeed in initiating a
00931         * chase on any referral (basically this means we were able to connect
00932         * to the server and bind).
00933         */
00934        for ( i = 0; v3refs[i] != NULL; ++i ) {
00935               rc = chase_one_referral( ld, lr, origreq, v3refs[i],
00936                   is_reference ? "v3 reference" : "v3 referral", &unknown );
00937               if ( rc == LDAP_SUCCESS && !unknown ) {
00938                      *chasingcountp = 1;
00939                      break;
00940               }
00941        }
00942 
00943        /* XXXmcs: should we save unfollowed referrals somewhere? */
00944 
00945        return( rc ); /* last error is as good as any other I guess... */
00946 }
00947 
00948 
00949 /*
00950  * returns an LDAP error code
00951  *
00952  * XXXmcs: this function used to have #ifdef LDAP_DNS code in it but I
00953  *     removed it when I improved the parsing (we don't define LDAP_DNS
00954  *     here at Netscape).
00955  */
00956 static int
00957 chase_one_referral( LDAP *ld, LDAPRequest *lr, LDAPRequest *origreq,
00958     char *refurl, char *desc, int *unknownp )
00959 {
00960        int           rc, tmprc, secure, msgid;
00961        LDAPServer    *srv;
00962        BerElement    *ber;
00963        LDAPURLDesc   *ludp;
00964 
00965        *unknownp = 0;
00966        ludp = NULLLDAPURLDESC;
00967 
00968        if ( nsldapi_url_parse( refurl, &ludp, 0 ) != 0 ) {
00969               LDAPDebug( LDAP_DEBUG_TRACE,
00970                   "ignoring unknown %s <%s>\n", desc, refurl, 0 );
00971               *unknownp = 1;
00972               rc = LDAP_SUCCESS;
00973               goto cleanup_and_return;
00974        }
00975 
00976        secure = (( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 );
00977 
00978 /* XXXmcs: can't tell if secure is supported by connect callback */
00979        if ( secure && ld->ld_extconnect_fn == NULL ) {
00980               LDAPDebug( LDAP_DEBUG_TRACE,
00981                   "ignoring LDAPS %s <%s>\n", desc, refurl, 0 );
00982               *unknownp = 1;
00983               rc = LDAP_SUCCESS;
00984               goto cleanup_and_return;
00985        }
00986 
00987        LDAPDebug( LDAP_DEBUG_TRACE, "chasing LDAP%s %s: <%s>\n",
00988            secure ? "S" : "", desc, refurl );
00989 
00990        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00991        msgid = ++ld->ld_msgid;
00992        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00993 
00994        if (( tmprc = re_encode_request( ld, origreq->lr_ber, msgid,
00995            ludp, &ber )) != LDAP_SUCCESS ) {
00996               rc = tmprc;
00997               goto cleanup_and_return;
00998        }
00999 
01000        if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer )))
01001            == NULL ) {
01002               ber_free( ber, 1 );
01003               rc = LDAP_NO_MEMORY;
01004               goto cleanup_and_return;
01005        }
01006 
01007        if ( ludp->lud_host == NULL && ld->ld_defhost == NULL ) {
01008               srv->lsrv_host = NULL;
01009        } else {
01010               if ( ludp->lud_host == NULL ) {
01011                 srv->lsrv_host =
01012                   nsldapi_strdup( origreq->lr_conn->lconn_server->lsrv_host );
01013                 LDAPDebug( LDAP_DEBUG_TRACE,
01014                   "chase_one_referral: using hostname '%s' from original "
01015                   "request on new request\n",
01016                   srv->lsrv_host, 0, 0);
01017               } else {
01018                 srv->lsrv_host = nsldapi_strdup( ludp->lud_host );
01019                 LDAPDebug( LDAP_DEBUG_TRACE,
01020                   "chase_one_referral: using hostname '%s' as specified "
01021                   "on new request\n",
01022                   srv->lsrv_host, 0, 0);
01023               }
01024 
01025               if ( srv->lsrv_host == NULL ) {
01026                      NSLDAPI_FREE( (char *)srv );
01027                      ber_free( ber, 1 );
01028                      rc = LDAP_NO_MEMORY;
01029                      goto cleanup_and_return;
01030               }
01031        }
01032 
01033        if ( ludp->lud_port == 0 && ludp->lud_host == NULL ) {
01034               srv->lsrv_port = origreq->lr_conn->lconn_server->lsrv_port;
01035               LDAPDebug( LDAP_DEBUG_TRACE,
01036                   "chase_one_referral: using port (%d) from original "
01037                   "request on new request\n",
01038                   srv->lsrv_port, 0, 0);
01039        } else {
01040               srv->lsrv_port = ludp->lud_port;
01041               LDAPDebug( LDAP_DEBUG_TRACE,
01042                   "chase_one_referral: using port (%d) as specified on "
01043                   "new request\n",
01044                   srv->lsrv_port, 0, 0);
01045        }
01046 
01047        if ( secure ) {
01048               srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
01049        }
01050 
01051        if ( nsldapi_send_server_request( ld, ber, msgid,
01052            lr, srv, NULL, NULL, 1 ) < 0 ) {
01053               rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
01054               LDAPDebug( LDAP_DEBUG_ANY, "Unable to chase %s %s (%s)\n",
01055                   desc, refurl, ldap_err2string( rc ));
01056        } else {
01057               rc = LDAP_SUCCESS;
01058        }
01059 
01060 cleanup_and_return:
01061        if ( ludp != NULLLDAPURLDESC ) {
01062               ldap_free_urldesc( ludp );
01063        }
01064 
01065        return( rc );
01066 }
01067 
01068 
01069 /* returns an LDAP error code */
01070 int
01071 nsldapi_append_referral( LDAP *ld, char **referralsp, char *s )
01072 {
01073        int    first;
01074 
01075        if ( *referralsp == NULL ) {
01076               first = 1;
01077               *referralsp = (char *)NSLDAPI_MALLOC( strlen( s ) +
01078                   LDAP_REF_STR_LEN + 1 );
01079        } else {
01080               first = 0;
01081               *referralsp = (char *)NSLDAPI_REALLOC( *referralsp,
01082                   strlen( *referralsp ) + strlen( s ) + 2 );
01083        }
01084 
01085        if ( *referralsp == NULL ) {
01086               return( LDAP_NO_MEMORY );
01087        }
01088 
01089        if ( first ) {
01090               strcpy( *referralsp, LDAP_REF_STR );
01091        } else {
01092               strcat( *referralsp, "\n" );
01093        }
01094        strcat( *referralsp, s );
01095 
01096        return( LDAP_SUCCESS );
01097 }
01098 
01099 
01100 
01101 /* returns an LDAP error code */
01102 static int
01103 re_encode_request( LDAP *ld, BerElement *origber, int msgid, LDAPURLDesc *ludp,
01104     BerElement **berp )
01105 {
01106 /*
01107  * XXX this routine knows way too much about how the lber library works!
01108  */
01109        unsigned long        along, tag;
01110        long                 ver;
01111        int                  rc;
01112        BerElement           *ber;
01113        struct berelement    tmpber;
01114        char                 *dn, *orig_dn;
01115 
01116        LDAPDebug( LDAP_DEBUG_TRACE,
01117            "re_encode_request: new msgid %d, new dn <%s>\n",
01118            msgid, ( ludp->lud_dn == NULL ) ? "NONE" : ludp->lud_dn, 0 );
01119 
01120        tmpber = *origber;
01121 
01122        /*
01123         * All LDAP requests are sequences that start with a message id.  For
01124         * everything except delete requests, this is followed by a sequence
01125         * that is tagged with the operation code.  For deletes, there is just
01126         * a DN that is tagged with the operation code.
01127         */
01128 
01129        /* skip past msgid and get operation tag */
01130        if ( ber_scanf( &tmpber, "{it", &along, &tag ) == LBER_ERROR ) {
01131               return( LDAP_DECODING_ERROR );
01132        }
01133 
01134        /*
01135         * XXXmcs: we don't support scope or filters in search referrals yet,
01136         * so if either were present we return an error which is probably
01137         * better than just ignoring the extra info.
01138         */
01139        if ( tag == LDAP_REQ_SEARCH &&
01140            ( ludp->lud_scope != -1 || ludp->lud_filter != NULL )) {
01141               return( LDAP_LOCAL_ERROR );
01142        }
01143 
01144        if ( tag == LDAP_REQ_BIND ) {
01145               /* bind requests have a version number before the DN */
01146               rc = ber_scanf( &tmpber, "{ia", &ver, &orig_dn );
01147        } else if ( tag == LDAP_REQ_DELETE ) {
01148               /* delete requests DNs are not within a sequence */
01149               rc = ber_scanf( &tmpber, "a", &orig_dn );
01150        } else {
01151               rc = ber_scanf( &tmpber, "{a", &orig_dn );
01152        }
01153 
01154        if ( rc == LBER_ERROR ) {
01155               return( LDAP_DECODING_ERROR );
01156        }
01157 
01158        if ( ludp->lud_dn == NULL ) {
01159               dn = orig_dn;
01160        } else {
01161               dn = ludp->lud_dn;
01162               NSLDAPI_FREE( orig_dn );
01163               orig_dn = NULL;
01164        }
01165 
01166        /* allocate and build the new request */
01167         if (( rc = nsldapi_alloc_ber_with_options( ld, &ber ))
01168            != LDAP_SUCCESS ) {
01169               if ( orig_dn != NULL ) {
01170                      NSLDAPI_FREE( orig_dn );
01171               }
01172                 return( rc );
01173         }
01174 
01175        if ( tag == LDAP_REQ_BIND ) {
01176               rc = ber_printf( ber, "{it{is", msgid, tag,
01177                   (int)ver /* XXX lossy cast */, dn );
01178        } else if ( tag == LDAP_REQ_DELETE ) {
01179               rc = ber_printf( ber, "{its}", msgid, tag, dn );
01180        } else {
01181               rc = ber_printf( ber, "{it{s", msgid, tag, dn );
01182        }
01183 
01184        if ( orig_dn != NULL ) {
01185               NSLDAPI_FREE( orig_dn );
01186        }
01187 /*
01188  * can't use "dn" or "orig_dn" from this point on (they've been freed)
01189  */
01190 
01191        if ( rc == -1 ) {
01192               ber_free( ber, 1 );
01193               return( LDAP_ENCODING_ERROR );
01194        }
01195 
01196        if ( tag != LDAP_REQ_DELETE &&
01197            ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
01198            tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr )
01199            || ber_printf( ber, "}}" ) == -1 )) {
01200               ber_free( ber, 1 );
01201               return( LDAP_ENCODING_ERROR );
01202        }
01203 
01204 #ifdef LDAP_DEBUG
01205        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
01206               LDAPDebug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
01207                   0, 0, 0 );
01208               ber_dump( ber, 0 );
01209        }
01210 #endif /* LDAP_DEBUG */
01211 
01212        *berp = ber;
01213        return( LDAP_SUCCESS );
01214 }
01215 
01216 
01217 LDAPRequest *
01218 nsldapi_find_request_by_msgid( LDAP *ld, int msgid )
01219 {
01220        LDAPRequest   *lr;
01221 
01222        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
01223               if ( msgid == lr->lr_msgid ) {
01224                      break;
01225               }
01226        }
01227 
01228        return( lr );
01229 }
01230 
01231 
01232 /*
01233  * nsldapi_connection_lost_nolock() resets "ld" to a non-connected, known
01234  * state.  It should be called whenever a fatal error occurs on the
01235  * Sockbuf "sb."  sb == NULL means we don't know specifically where
01236  * the problem was so we assume all connections are bad.
01237  */
01238 void
01239 nsldapi_connection_lost_nolock( LDAP *ld, Sockbuf *sb )
01240 {
01241        LDAPRequest   *lr;
01242 
01243        /*
01244         * change status of all pending requests that are associated with "sb
01245         *     to "connection dead."
01246         * also change the connection status to "dead" and remove it from
01247         *     the list of sockets we are interested in.
01248         */
01249        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
01250               if ( sb == NULL ||
01251                   ( lr->lr_conn != NULL && lr->lr_conn->lconn_sb == sb )) {
01252                      lr->lr_status = LDAP_REQST_CONNDEAD;
01253                      if ( lr->lr_conn != NULL ) {
01254                             lr->lr_conn->lconn_status = LDAP_CONNST_DEAD;
01255                             nsldapi_iostatus_interest_clear( ld,
01256                                 lr->lr_conn->lconn_sb );
01257                      }
01258               }
01259        }
01260 }
01261 
01262 
01263 #ifdef LDAP_DNS
01264 static LDAPServer *
01265 dn2servers( LDAP *ld, char *dn )   /* dn can also be a domain.... */
01266 {
01267        char          *p, *domain, *host, *server_dn, **dxs;
01268        int           i, port;
01269        LDAPServer    *srvlist, *prevsrv, *srv;
01270 
01271        if (( domain = strrchr( dn, '@' )) != NULL ) {
01272               ++domain;
01273        } else {
01274               domain = dn;
01275        }
01276 
01277        if (( dxs = nsldapi_getdxbyname( domain )) == NULL ) {
01278               LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
01279               return( NULL );
01280        }
01281 
01282        srvlist = NULL;
01283 
01284        for ( i = 0; dxs[ i ] != NULL; ++i ) {
01285               port = LDAP_PORT;
01286               server_dn = NULL;
01287               if ( strchr( dxs[ i ], ':' ) == NULL ) {
01288                      host = dxs[ i ];
01289               } else if ( strlen( dxs[ i ] ) >= 7 &&
01290                   strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
01291                      host = dxs[ i ] + 7;
01292                      if (( p = strchr( host, ':' )) == NULL ) {
01293                             p = host;
01294                      } else {
01295                             *p++ = '\0';
01296                             port = atoi( p );
01297                      }
01298                      if (( p = strchr( p, '/' )) != NULL ) {
01299                             server_dn = ++p;
01300                             if ( *server_dn == '\0' ) {
01301                                    server_dn = NULL;
01302                             }
01303                      }
01304               } else {
01305                      host = NULL;
01306               }
01307 
01308               if ( host != NULL ) {       /* found a server we can use */
01309                      if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1,
01310                          sizeof( LDAPServer ))) == NULL ) {
01311                             free_servers( srvlist );
01312                             srvlist = NULL;
01313                             break;        /* exit loop & return */
01314                      }
01315 
01316                      /* add to end of list of servers */
01317                      if ( srvlist == NULL ) {
01318                             srvlist = srv;
01319                      } else {
01320                             prevsrv->lsrv_next = srv;
01321                      }
01322                      prevsrv = srv;
01323                      
01324                      /* copy in info. */
01325                      if (( srv->lsrv_host = nsldapi_strdup( host )) == NULL
01326                          || ( server_dn != NULL && ( srv->lsrv_dn =
01327                          nsldapi_strdup( server_dn )) == NULL )) {
01328                             free_servers( srvlist );
01329                             srvlist = NULL;
01330                             break;        /* exit loop & return */
01331                      }
01332                      srv->lsrv_port = port;
01333               }
01334        }
01335 
01336        ldap_value_free( dxs );
01337 
01338        if ( srvlist == NULL ) {
01339               LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL );
01340        }
01341 
01342        return( srvlist );
01343 }
01344 #endif /* LDAP_DNS */