2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 /* This notice applies to changes, created by or for Novell, Inc.,
19 * to preexisting works for which notices appear elsewhere in this file.
21 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
23 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
24 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
25 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
26 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
27 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
28 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
29 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
30 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
32 * Modification to OpenLDAP source by Novell, Inc.
33 * April 2000 sfs Added code to chase V3 referrals
34 * request.c - sending of ldap requests; handling of referrals
36 * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
37 * can be found in the file "build/LICENSE-2.0.1" in this distribution
38 * of OpenLDAP Software.
45 #include <ac/stdlib.h>
48 #include <ac/socket.h>
49 #include <ac/string.h>
51 #include <ac/unistd.h>
56 /* used by ldap_send_server_request and ldap_new_connection */
58 #define LDAP_CONN_LOCK_IF(nolock) \
59 { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_conn_mutex ); }
60 #define LDAP_CONN_UNLOCK_IF(nolock) \
61 { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex ); }
62 #define LDAP_REQ_LOCK_IF(nolock) \
63 { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_req_mutex ); }
64 #define LDAP_REQ_UNLOCK_IF(nolock) \
65 { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex ); }
66 #define LDAP_RES_LOCK_IF(nolock) \
67 { if (nolock) LDAP_MUTEX_LOCK( &ld->ld_res_mutex ); }
68 #define LDAP_RES_UNLOCK_IF(nolock) \
69 { if (nolock) LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex ); }
71 #define LDAP_CONN_LOCK_IF(nolock)
72 #define LDAP_CONN_UNLOCK_IF(nolock)
73 #define LDAP_REQ_LOCK_IF(nolock)
74 #define LDAP_REQ_UNLOCK_IF(nolock)
75 #define LDAP_RES_LOCK_IF(nolock)
76 #define LDAP_RES_UNLOCK_IF(nolock)
79 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
80 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
81 static void ldap_free_request_int LDAP_P(( LDAP *ld, LDAPRequest *lr ));
84 re_encode_request( LDAP *ld,
92 ldap_alloc_ber_with_options( LDAP *ld )
96 ber = ber_alloc_t( ld->ld_lberoptions );
98 ld->ld_errno = LDAP_NO_MEMORY;
106 ldap_set_ber_options( LDAP *ld, BerElement *ber )
108 /* ld_lberoptions is constant, hence no lock */
109 ber->ber_options = ld->ld_lberoptions;
113 /* sets needed mutexes - no mutexes set to this point */
115 ldap_send_initial_request(
124 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
126 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
127 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
128 /* not connected yet */
129 rc = ldap_open_defconn( ld );
134 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
136 } else if ( rc == 0 ) {
137 Debug( LDAP_DEBUG_TRACE,
138 "ldap_open_defconn: successful\n",
142 #ifdef LDAP_CONNECTIONLESS
143 if (LDAP_IS_UDP(ld)) {
144 if (msgtype == LDAP_REQ_BIND) {
145 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
146 if (ld->ld_options.ldo_cldapdn)
147 ldap_memfree(ld->ld_options.ldo_cldapdn);
148 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
150 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
151 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
154 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
157 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
158 return LDAP_PARAM_ERROR;
162 LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
163 rc = ldap_send_server_request( ld, ber, msgid, NULL,
164 NULL, NULL, NULL, 0, 0 );
165 LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
166 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
171 /* protected by conn_mutex */
173 ldap_int_flush_request(
177 LDAPConn *lc = lr->lr_conn;
179 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
180 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
181 if ( sock_errno() == EAGAIN ) {
182 /* need to continue write later */
183 lr->lr_status = LDAP_REQST_WRITING;
184 ldap_mark_select_write( ld, lc->lconn_sb );
185 ld->ld_errno = LDAP_BUSY;
188 ld->ld_errno = LDAP_SERVER_DOWN;
189 ldap_free_request( ld, lr );
190 ldap_free_connection( ld, lc, 0, 0 );
194 if ( lr->lr_parent == NULL ) {
195 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
196 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
198 lr->lr_status = LDAP_REQST_INPROGRESS;
200 /* sent -- waiting for a response */
201 ldap_mark_select_read( ld, lc->lconn_sb );
207 * protected by req_mutex
208 * if m_noconn then protect using conn_lock
209 * else already protected with conn_lock
210 * if m_res then also protected by res_mutex
214 ldap_send_server_request(
218 LDAPRequest *parentreq,
219 LDAPURLDesc **srvlist,
228 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
229 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
232 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
234 LDAP_CONN_LOCK_IF(m_noconn);
236 if ( srvlist == NULL ) {
239 lc = find_connection( ld, *srvlist, 1 );
241 if ( (bind != NULL) && (parentreq != NULL) ) {
242 /* Remember the bind in the parent */
244 ++parentreq->lr_outrefcnt;
246 lc = ldap_new_connection( ld, srvlist, 0,
252 /* async connect... */
253 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
254 ber_socket_t sd = AC_SOCKET_ERROR;
255 struct timeval tv = { 0 };
257 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
260 switch ( ldap_int_poll( ld, sd, &tv ) ) {
263 lc->lconn_status = LDAP_CONNST_CONNECTED;
267 /* async only occurs if a network timeout is set */
269 /* honor network timeout */
270 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
271 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
273 /* caller will have to call again */
274 ld->ld_errno = LDAP_X_CONNECTING;
276 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
285 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
286 if ( ld->ld_errno == LDAP_SUCCESS ) {
287 ld->ld_errno = LDAP_SERVER_DOWN;
292 /* Forget about the bind */
293 --parentreq->lr_outrefcnt;
295 LDAP_CONN_UNLOCK_IF(m_noconn);
299 use_connection( ld, lc );
301 #ifdef LDAP_CONNECTIONLESS
302 if ( LDAP_IS_UDP( ld )) {
303 BerElement tmpber = *ber;
304 ber_rewind( &tmpber );
305 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
306 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
307 sizeof( struct sockaddr ), 0 );
308 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
310 ld->ld_errno = LDAP_ENCODING_ERROR;
311 LDAP_CONN_UNLOCK_IF(m_noconn);
317 /* If we still have an incomplete write, try to finish it before
318 * dealing with the new request. If we don't finish here, return
319 * LDAP_BUSY and let the caller retry later. We only allow a single
320 * request to be in WRITING state.
323 if ( ld->ld_requests &&
324 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
325 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
330 LDAP_CONN_UNLOCK_IF(m_noconn);
334 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
336 ld->ld_errno = LDAP_NO_MEMORY;
337 ldap_free_connection( ld, lc, 0, 0 );
340 /* Forget about the bind */
341 --parentreq->lr_outrefcnt;
343 LDAP_CONN_UNLOCK_IF(m_noconn);
346 lr->lr_msgid = msgid;
347 lr->lr_status = LDAP_REQST_INPROGRESS;
348 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
351 if ( parentreq != NULL ) { /* sub-request */
353 /* Increment if we didn't do it before the bind */
354 ++parentreq->lr_outrefcnt;
356 lr->lr_origid = parentreq->lr_origid;
357 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
358 lr->lr_parent = parentreq;
359 lr->lr_refnext = parentreq->lr_child;
360 parentreq->lr_child = lr;
361 } else { /* original request */
362 lr->lr_origid = lr->lr_msgid;
365 /* Extract requestDN for future reference */
367 BerElement tmpber = *ber;
371 ber_reset( &tmpber, 1 );
372 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
375 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
377 case LDAP_REQ_DELETE:
380 rtag = ber_scanf( &tmpber, "{" /*}*/ );
381 case LDAP_REQ_ABANDON:
384 if ( tag != LDAP_REQ_ABANDON ) {
385 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
386 lr->lr_dn.bv_val = tmpber.ber_ptr;
391 lr->lr_next = ld->ld_requests;
392 if ( lr->lr_next != NULL ) {
393 lr->lr_next->lr_prev = lr;
395 ld->ld_requests = lr;
397 ld->ld_errno = LDAP_SUCCESS;
398 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
402 LDAP_CONN_UNLOCK_IF(m_noconn);
406 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
408 find_tls_ext( LDAPURLDesc *srv )
413 if ( !srv->lud_exts )
416 for (i=0; srv->lud_exts[i]; i++) {
418 ext = srv->lud_exts[i];
419 if ( ext[0] == '!') {
423 if ( !strcasecmp( ext, "StartTLS" ) ||
424 !strcasecmp( ext, "X-StartTLS" ) ||
425 !strcmp( ext, LDAP_EXOP_START_TLS )) {
433 * always protected by conn_mutex
434 * optionally protected by req_mutex and res_mutex
437 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
438 int connect, LDAPreqinfo *bind, int m_req, int m_res )
443 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
444 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
445 use_ldsb, connect, (bind != NULL) );
447 * make a new LDAP server connection
448 * XXX open connection synchronously for now
450 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
452 ld->ld_errno = LDAP_NO_MEMORY;
457 assert( ld->ld_sb != NULL );
458 lc->lconn_sb = ld->ld_sb;
461 lc->lconn_sb = ber_sockbuf_alloc();
462 if ( lc->lconn_sb == NULL ) {
463 LDAP_FREE( (char *)lc );
464 ld->ld_errno = LDAP_NO_MEMORY;
470 LDAPURLDesc **srvp, *srv = NULL;
472 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
474 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
477 rc = ldap_int_open_connection( ld, lc, *srvp, async );
481 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
482 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
491 ber_sockbuf_free( lc->lconn_sb );
493 LDAP_FREE( (char *)lc );
494 ld->ld_errno = LDAP_SERVER_DOWN;
498 lc->lconn_server = ldap_url_dup( srv );
501 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
502 lc->lconn_next = ld->ld_conns;
507 if ( lc->lconn_server->lud_exts ) {
508 int rc, ext = find_tls_ext( lc->lconn_server );
510 LDAPConn *savedefconn;
512 savedefconn = ld->ld_defconn;
513 ++lc->lconn_refcnt; /* avoid premature free */
516 LDAP_REQ_UNLOCK_IF(m_req);
517 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
518 LDAP_RES_UNLOCK_IF(m_res);
519 rc = ldap_start_tls_s( ld, NULL, NULL );
520 LDAP_RES_LOCK_IF(m_res);
521 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
522 LDAP_REQ_LOCK_IF(m_req);
523 ld->ld_defconn = savedefconn;
526 if ( rc != LDAP_SUCCESS && ext == 2 ) {
527 ldap_free_connection( ld, lc, 1, 0 );
535 if ( bind != NULL ) {
537 LDAPConn *savedefconn;
539 /* Set flag to prevent additional referrals
540 * from being processed on this
541 * connection until the bind has completed
543 lc->lconn_rebind_inprogress = 1;
544 /* V3 rebind function */
545 if ( ld->ld_rebind_proc != NULL) {
546 LDAPURLDesc *srvfunc;
548 srvfunc = ldap_url_dup( *srvlist );
549 if ( srvfunc == NULL ) {
550 ld->ld_errno = LDAP_NO_MEMORY;
553 savedefconn = ld->ld_defconn;
554 ++lc->lconn_refcnt; /* avoid premature free */
557 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
558 LDAP_REQ_UNLOCK_IF(m_req);
559 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
560 LDAP_RES_UNLOCK_IF(m_res);
561 err = (*ld->ld_rebind_proc)( ld,
562 bind->ri_url, bind->ri_request, bind->ri_msgid,
563 ld->ld_rebind_params );
564 LDAP_RES_LOCK_IF(m_res);
565 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
566 LDAP_REQ_LOCK_IF(m_req);
568 ld->ld_defconn = savedefconn;
573 ldap_free_connection( ld, lc, 1, 0 );
576 ldap_free_urldesc( srvfunc );
581 struct berval passwd = BER_BVNULL;
583 savedefconn = ld->ld_defconn;
584 ++lc->lconn_refcnt; /* avoid premature free */
587 Debug( LDAP_DEBUG_TRACE,
588 "anonymous rebind via ldap_sasl_bind(\"\")\n",
591 LDAP_REQ_UNLOCK_IF(m_req);
592 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
593 LDAP_RES_UNLOCK_IF(m_res);
594 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
595 NULL, NULL, &msgid );
596 if ( rc != LDAP_SUCCESS ) {
600 for ( err = 1; err > 0; ) {
601 struct timeval tv = { 0, 100000 };
602 LDAPMessage *res = NULL;
604 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
610 #ifdef LDAP_R_COMPILE
611 ldap_pvt_thread_yield();
616 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
617 if ( rc != LDAP_SUCCESS ) {
620 } else if ( err != LDAP_SUCCESS ) {
623 /* else err == LDAP_SUCCESS == 0 */
627 Debug( LDAP_DEBUG_TRACE,
628 "ldap_new_connection %p: "
629 "unexpected response %d "
630 "from BIND request id=%d\n",
631 (void *) ld, ldap_msgtype( res ), msgid );
637 LDAP_RES_LOCK_IF(m_res);
638 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
639 LDAP_REQ_LOCK_IF(m_req);
640 ld->ld_defconn = savedefconn;
644 ldap_free_connection( ld, lc, 1, 0 );
649 lc->lconn_rebind_inprogress = 0;
655 /* protected by ld_conn_mutex */
657 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
659 * return an existing connection (if any) to the server srv
660 * if "any" is non-zero, check for any server in the "srv" chain
664 LDAPURLDesc *lcu, *lsu;
665 int lcu_port, lsu_port;
668 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
669 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
670 lcu = lc->lconn_server;
671 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
674 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
675 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
678 if ( lsu_port == lcu_port
679 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
680 && lcu->lud_host != NULL && lsu->lud_host != NULL
681 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
697 /* protected by ld_conn_mutex */
699 use_connection( LDAP *ld, LDAPConn *lc )
701 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
703 lc->lconn_lastused = time( NULL );
707 /* protected by ld_conn_mutex */
709 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
711 LDAPConn *tmplc, *prevlc;
713 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
714 Debug( LDAP_DEBUG_TRACE,
715 "ldap_free_connection %d %d\n",
718 if ( force || --lc->lconn_refcnt <= 0 ) {
719 /* remove from connections list first */
721 for ( prevlc = NULL, tmplc = ld->ld_conns;
723 tmplc = tmplc->lconn_next )
726 if ( prevlc == NULL ) {
727 ld->ld_conns = tmplc->lconn_next;
729 prevlc->lconn_next = tmplc->lconn_next;
731 if ( ld->ld_defconn == lc ) {
732 ld->ld_defconn = NULL;
739 /* process connection callbacks */
741 struct ldapoptions *lo;
745 lo = &ld->ld_options;
746 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
747 if ( lo->ldo_conn_cbs ) {
748 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
750 cb->lc_del( ld, lc->lconn_sb, cb );
753 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
754 lo = LDAP_INT_GLOBAL_OPT();
755 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
756 if ( lo->ldo_conn_cbs ) {
757 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
759 cb->lc_del( ld, lc->lconn_sb, cb );
762 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
765 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
766 ldap_mark_select_clear( ld, lc->lconn_sb );
768 ldap_send_unbind( ld, lc->lconn_sb,
773 if ( lc->lconn_ber != NULL ) {
774 ber_free( lc->lconn_ber, 1 );
777 ldap_int_sasl_close( ld, lc );
779 ldap_int_gssapi_close( ld, lc );
782 ldap_free_urllist( lc->lconn_server );
784 /* FIXME: is this at all possible?
785 * ldap_ld_free() in unbind.c calls ldap_free_connection()
786 * with force == 1 __after__ explicitly calling
787 * ldap_free_request() on all requests */
791 for ( lr = ld->ld_requests; lr; ) {
792 LDAPRequest *lr_next = lr->lr_next;
794 if ( lr->lr_conn == lc ) {
795 ldap_free_request_int( ld, lr );
802 if ( lc->lconn_sb != ld->ld_sb ) {
803 ber_sockbuf_free( lc->lconn_sb );
805 ber_int_sb_close( lc->lconn_sb );
808 if ( lc->lconn_rebind_queue != NULL) {
810 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
811 LDAP_VFREE( lc->lconn_rebind_queue[i] );
813 LDAP_FREE( lc->lconn_rebind_queue );
818 Debug( LDAP_DEBUG_TRACE,
819 "ldap_free_connection: actually freed\n",
823 lc->lconn_lastused = time( NULL );
824 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
825 lc->lconn_refcnt, 0, 0 );
830 /* Protects self with ld_conn_mutex */
833 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
838 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
839 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
840 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
841 if ( lc->lconn_server != NULL ) {
842 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
843 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
844 : lc->lconn_server->lud_host,
845 lc->lconn_server->lud_port, ( lc->lconn_sb ==
846 ld->ld_sb ) ? " (default)" : "" );
848 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
849 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
851 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
852 ? "Connecting" : "Connected", 0 );
853 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
854 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
855 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
856 if ( lc->lconn_rebind_inprogress ) {
857 if ( lc->lconn_rebind_queue != NULL) {
860 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
862 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
863 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
864 i, j, lc->lconn_rebind_queue[i][j] );
868 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
871 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
876 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
880 /* protected by req_mutex and res_mutex */
882 ldap_dump_requests_and_responses( LDAP *ld )
888 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
890 lr = ld->ld_requests;
892 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
894 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
895 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
896 lr->lr_msgid, lr->lr_origid,
897 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
898 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
899 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
900 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
901 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
903 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
904 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
906 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
907 (void *)ld, i, ld->ld_nabandoned );
908 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
909 if ( ( lm = ld->ld_responses ) == NULL ) {
910 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
912 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
913 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
914 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
915 if ( lm->lm_chain != NULL ) {
916 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
917 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
918 Debug( LDAP_DEBUG_TRACE,
919 " * msgid %d, type %lu\n",
921 (unsigned long)l->lm_msgtype, 0 );
925 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
927 #endif /* LDAP_DEBUG */
929 /* protected by req_mutex */
931 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
933 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
934 /* if lr_refcnt > 0, the request has been looked up
935 * by ldap_find_request_by_msgid(); if in the meanwhile
936 * the request is free()'d by someone else, just decrease
937 * the reference count and extract it from the request
938 * list; later on, it will be freed. */
939 if ( lr->lr_prev == NULL ) {
940 if ( lr->lr_refcnt == 0 ) {
941 /* free'ing the first request? */
942 assert( ld->ld_requests == lr );
945 if ( ld->ld_requests == lr ) {
946 ld->ld_requests = lr->lr_next;
950 lr->lr_prev->lr_next = lr->lr_next;
953 if ( lr->lr_next != NULL ) {
954 lr->lr_next->lr_prev = lr->lr_prev;
957 if ( lr->lr_refcnt > 0 ) {
958 lr->lr_refcnt = -lr->lr_refcnt;
966 if ( lr->lr_ber != NULL ) {
967 ber_free( lr->lr_ber, 1 );
971 if ( lr->lr_res_error != NULL ) {
972 LDAP_FREE( lr->lr_res_error );
973 lr->lr_res_error = NULL;
976 if ( lr->lr_res_matched != NULL ) {
977 LDAP_FREE( lr->lr_res_matched );
978 lr->lr_res_matched = NULL;
984 /* protected by req_mutex */
986 ldap_free_request( LDAP *ld, LDAPRequest *lr )
988 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
989 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
990 lr->lr_origid, lr->lr_msgid, 0 );
992 /* free all referrals (child requests) */
993 while ( lr->lr_child ) {
994 ldap_free_request( ld, lr->lr_child );
997 if ( lr->lr_parent != NULL ) {
1000 --lr->lr_parent->lr_outrefcnt;
1001 for ( lrp = &lr->lr_parent->lr_child;
1003 lrp = &(*lrp)->lr_refnext );
1006 *lrp = lr->lr_refnext;
1009 ldap_free_request_int( ld, lr );
1013 * call first time with *cntp = -1
1014 * when returns *cntp == -1, no referrals are left
1016 * NOTE: may replace *refsp, or shuffle the contents
1017 * of the original array.
1019 static int ldap_int_nextref(
1025 assert( refsp != NULL );
1026 assert( *refsp != NULL );
1027 assert( cntp != NULL );
1036 if ( (*refsp)[ *cntp ] == NULL ) {
1044 * Chase v3 referrals
1047 * (IN) ld = LDAP connection handle
1048 * (IN) lr = LDAP Request structure
1049 * (IN) refs = array of pointers to referral strings that we will chase
1050 * The array will be free'd by this function when no longer needed
1051 * (IN) sref != 0 if following search reference
1052 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1053 * (OUT) hadrefp = 1 if sucessfully followed referral
1055 * Return value - number of referrals followed
1057 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1060 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1063 int unfollowedcnt = 0;
1064 LDAPRequest *origreq;
1065 LDAPURLDesc *srv = NULL;
1067 char **refarray = NULL;
1069 int rc, count, i, j, id;
1071 LDAP_NEXTREF_PROC *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
1073 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1074 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1075 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1076 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1078 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1084 /* If no referrals in array, return */
1085 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1090 /* Check for hop limit exceeded */
1091 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1092 Debug( LDAP_DEBUG_ANY,
1093 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1094 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1099 /* find original request */
1101 origreq->lr_parent != NULL;
1102 origreq = origreq->lr_parent )
1110 /* parse out & follow referrals */
1111 /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
1113 for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1115 nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1118 /* Parse the referral URL */
1119 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1120 if ( rc != LDAP_URL_SUCCESS ) {
1121 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1122 * which do not map on API errors */
1123 ld->ld_errno = LDAP_PARAM_ERROR;
1128 if( srv->lud_crit_exts ) {
1131 /* If StartTLS is the only critical ext, OK. */
1132 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1136 /* we do not support any other extensions */
1137 ld->ld_errno = LDAP_NOT_SUPPORTED;
1143 /* check connection for re-bind in progress */
1144 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1145 /* See if we've already requested this DN with this conn */
1148 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1149 for ( lp = origreq; lp; ) {
1150 if ( lp->lr_conn == lc
1151 && len == lp->lr_dn.bv_len
1153 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1158 if ( lp == origreq ) {
1161 lp = lp->lr_refnext;
1165 ldap_free_urllist( srv );
1167 ld->ld_errno = LDAP_CLIENT_LOOP;
1172 if ( lc->lconn_rebind_inprogress ) {
1173 /* We are already chasing a referral or search reference and a
1174 * bind on that connection is in progress. We must queue
1175 * referrals on that connection, so we don't get a request
1176 * going out before the bind operation completes. This happens
1177 * if two search references come in one behind the other
1178 * for the same server with different contexts.
1180 Debug( LDAP_DEBUG_TRACE,
1181 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1183 if( lc->lconn_rebind_queue == NULL ) {
1184 /* Create a referral list */
1185 lc->lconn_rebind_queue =
1186 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1188 if( lc->lconn_rebind_queue == NULL) {
1189 ld->ld_errno = LDAP_NO_MEMORY;
1194 lc->lconn_rebind_queue[0] = refarray;
1195 lc->lconn_rebind_queue[1] = NULL;
1199 /* Count how many referral arrays we already have */
1200 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1204 /* Add the new referral to the list */
1205 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1206 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1208 if( lc->lconn_rebind_queue == NULL ) {
1209 ld->ld_errno = LDAP_NO_MEMORY;
1213 lc->lconn_rebind_queue[j] = refarray;
1214 lc->lconn_rebind_queue[j+1] = NULL;
1218 /* We have queued the referral/reference, now just return */
1221 count = 1; /* Pretend we already followed referral */
1225 /* Re-encode the request with the new starting point of the search.
1226 * Note: In the future we also need to replace the filter if one
1227 * was provided with the search reference
1230 /* For references we don't want old dn if new dn empty */
1231 if ( sref && srv->lud_dn == NULL ) {
1232 srv->lud_dn = LDAP_STRDUP( "" );
1235 LDAP_NEXT_MSGID( ld, id );
1236 ber = re_encode_request( ld, origreq->lr_ber, id,
1237 sref, srv, &rinfo.ri_request );
1240 ld->ld_errno = LDAP_ENCODING_ERROR;
1245 Debug( LDAP_DEBUG_TRACE,
1246 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1247 lr->lr_msgid, refarray[i], 0);
1249 /* Send the new request to the server - may require a bind */
1250 rinfo.ri_msgid = origreq->lr_origid;
1251 rinfo.ri_url = refarray[i];
1252 rc = ldap_send_server_request( ld, ber, id,
1253 origreq, &srv, NULL, &rinfo, 0, 1 );
1255 /* Failure, try next referral in the list */
1256 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1257 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1258 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1259 ldap_free_urllist( srv );
1261 ld->ld_errno = LDAP_REFERRAL;
1263 /* Success, no need to try this referral list further */
1268 /* check if there is a queue of referrals that came in during bind */
1270 lc = find_connection( ld, srv, 1 );
1272 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1274 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
1279 if ( lc->lconn_rebind_queue != NULL ) {
1280 /* Release resources of previous list */
1281 LDAP_VFREE( refarray );
1283 ldap_free_urllist( srv );
1286 /* Pull entries off end of queue so list always null terminated */
1287 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1289 refarray = lc->lconn_rebind_queue[j - 1];
1290 lc->lconn_rebind_queue[j-1] = NULL;
1291 /* we pulled off last entry from queue, free queue */
1293 LDAP_FREE( lc->lconn_rebind_queue );
1294 lc->lconn_rebind_queue = NULL;
1296 /* restart the loop the with new referral list */
1300 break; /* referral followed, break out of for loop */
1302 } /* end for loop */
1304 LDAP_VFREE( refarray );
1305 ldap_free_urllist( srv );
1306 LDAP_FREE( *errstrp );
1310 LDAP_FREE( unfollowed );
1313 *errstrp = unfollowed;
1319 * XXX merging of errors in this routine needs to be improved
1320 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1323 ldap_chase_referrals( LDAP *ld,
1331 char *p, *ref, *unfollowed;
1332 LDAPRequest *origreq;
1338 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1339 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1340 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1341 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1343 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1346 if ( *errstrp == NULL ) {
1350 len = strlen( *errstrp );
1351 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1352 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1354 p += LDAP_REF_STR_LEN;
1359 if ( len < LDAP_REF_STR_LEN ) {
1363 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1364 Debug( LDAP_DEBUG_ANY,
1365 "more than %d referral hops (dropping)\n",
1366 ld->ld_refhoplimit, 0, 0 );
1367 /* XXX report as error in ld->ld_errno? */
1371 /* find original request */
1372 for ( origreq = lr; origreq->lr_parent != NULL;
1373 origreq = origreq->lr_parent ) {
1380 /* parse out & follow referrals */
1381 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1382 p = strchr( ref, '\n' );
1387 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1388 if ( rc != LDAP_URL_SUCCESS ) {
1389 Debug( LDAP_DEBUG_TRACE,
1390 "ignoring %s referral <%s>\n",
1391 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1392 rc = ldap_append_referral( ld, &unfollowed, ref );
1397 Debug( LDAP_DEBUG_TRACE,
1398 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1402 /* See if we've already been here */
1403 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1406 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1407 for ( lp = lr; lp; lp = lp->lr_parent ) {
1408 if ( lp->lr_conn == lc
1409 && len == lp->lr_dn.bv_len )
1411 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1418 ldap_free_urllist( srv );
1419 ld->ld_errno = LDAP_CLIENT_LOOP;
1425 LDAP_NEXT_MSGID( ld, id );
1426 ber = re_encode_request( ld, origreq->lr_ber,
1427 id, sref, srv, &rinfo.ri_request );
1429 if ( ber == NULL ) {
1433 /* copy the complete referral for rebind process */
1434 rinfo.ri_url = LDAP_STRDUP( ref );
1436 rinfo.ri_msgid = origreq->lr_origid;
1438 rc = ldap_send_server_request( ld, ber, id,
1439 lr, &srv, NULL, &rinfo, 0, 1 );
1440 LDAP_FREE( rinfo.ri_url );
1445 Debug( LDAP_DEBUG_ANY,
1446 "Unable to chase referral \"%s\" (%d: %s)\n",
1447 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1448 rc = ldap_append_referral( ld, &unfollowed, ref );
1451 ldap_free_urllist(srv);
1454 LDAP_FREE( *errstrp );
1455 *errstrp = unfollowed;
1457 return(( rc == 0 ) ? count : rc );
1462 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1466 if ( *referralsp == NULL ) {
1468 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1472 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1473 strlen( *referralsp ) + strlen( s ) + 2 );
1476 if ( *referralsp == NULL ) {
1477 ld->ld_errno = LDAP_NO_MEMORY;
1482 strcpy( *referralsp, LDAP_REF_STR );
1484 strcat( *referralsp, "\n" );
1486 strcat( *referralsp, s );
1494 re_encode_request( LDAP *ld,
1495 BerElement *origber,
1502 * XXX this routine knows way too much about how the lber library works!
1510 BerElement tmpber, *ber;
1513 Debug( LDAP_DEBUG_TRACE,
1514 "re_encode_request: new msgid %ld, new dn <%s>\n",
1516 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1521 * all LDAP requests are sequences that start with a message id.
1522 * For all except delete, this is followed by a sequence that is
1523 * tagged with the operation code. For delete, the provided DN
1524 * is not wrapped by a sequence.
1526 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1528 if ( rtag == LBER_ERROR ) {
1529 ld->ld_errno = LDAP_DECODING_ERROR;
1534 if ( tag == LDAP_REQ_BIND ) {
1535 /* bind requests have a version number before the DN & other stuff */
1536 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1538 } else if ( tag == LDAP_REQ_DELETE ) {
1539 /* delete requests don't have a DN wrapping sequence */
1540 rtag = ber_scanf( &tmpber, "m", &dn );
1542 } else if ( tag == LDAP_REQ_SEARCH ) {
1543 /* search requests need to be re-scope-ed */
1544 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1546 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1547 /* use the scope provided in reference */
1548 scope = srv->lud_scope;
1550 } else if ( sref ) {
1551 /* use scope implied by previous operation
1554 * subtree -> subtree
1555 * subordinate -> subtree
1559 case LDAP_SCOPE_BASE:
1560 case LDAP_SCOPE_ONELEVEL:
1561 scope = LDAP_SCOPE_BASE;
1563 case LDAP_SCOPE_SUBTREE:
1564 case LDAP_SCOPE_SUBORDINATE:
1565 scope = LDAP_SCOPE_SUBTREE;
1571 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1574 if( rtag == LBER_ERROR ) {
1575 ld->ld_errno = LDAP_DECODING_ERROR;
1579 /* restore character zero'd out by ber_scanf*/
1580 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1582 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1586 if ( srv->lud_dn ) {
1587 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1590 if ( tag == LDAP_REQ_BIND ) {
1591 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1592 } else if ( tag == LDAP_REQ_DELETE ) {
1593 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1594 } else if ( tag == LDAP_REQ_SEARCH ) {
1595 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1597 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1601 ld->ld_errno = LDAP_ENCODING_ERROR;
1606 if ( tag != LDAP_REQ_DELETE && (
1607 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1608 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1609 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1611 ld->ld_errno = LDAP_ENCODING_ERROR;
1617 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1618 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1620 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1622 #endif /* LDAP_DEBUG */
1624 *type = tag; /* return request type */
1629 /* protected by req_mutex */
1631 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1635 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1636 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1637 continue; /* Skip completed requests */
1639 if ( msgid == lr->lr_msgid ) {
1648 /* protected by req_mutex */
1650 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1654 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1656 if ( lr->lr_refcnt > 0 ) {
1659 } else if ( lr->lr_refcnt < 0 ) {
1661 if ( lr->lr_refcnt == 0 ) {
1669 ldap_free_request_int( ld, lrx );
1671 } else if ( freeit ) {
1672 ldap_free_request( ld, lrx );