2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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(
123 ber_socket_t sd = AC_SOCKET_INVALID;
125 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
127 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
128 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ) == -1 ) {
129 /* not connected yet */
130 rc = ldap_open_defconn( ld );
133 if ( ld->ld_defconn && ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING )
134 rc = ldap_int_check_async_open( ld, sd );
137 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
139 } else if ( rc == 0 ) {
140 Debug( LDAP_DEBUG_TRACE,
141 "ldap_open_defconn: successful\n",
145 #ifdef LDAP_CONNECTIONLESS
146 if (LDAP_IS_UDP(ld)) {
147 if (msgtype == LDAP_REQ_BIND) {
148 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
149 if (ld->ld_options.ldo_cldapdn)
150 ldap_memfree(ld->ld_options.ldo_cldapdn);
151 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
153 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
154 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
157 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
160 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
161 return LDAP_PARAM_ERROR;
165 LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
166 rc = ldap_send_server_request( ld, ber, msgid, NULL,
167 NULL, NULL, NULL, 0, 0 );
168 LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
169 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
174 /* protected by conn_mutex */
176 ldap_int_flush_request(
180 LDAPConn *lc = lr->lr_conn;
182 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
183 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
184 if ( sock_errno() == EAGAIN ) {
185 /* need to continue write later */
186 lr->lr_status = LDAP_REQST_WRITING;
187 ldap_mark_select_write( ld, lc->lconn_sb );
188 ld->ld_errno = LDAP_BUSY;
191 ld->ld_errno = LDAP_SERVER_DOWN;
192 ldap_free_request( ld, lr );
193 ldap_free_connection( ld, lc, 0, 0 );
197 if ( lr->lr_parent == NULL ) {
198 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
199 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
201 lr->lr_status = LDAP_REQST_INPROGRESS;
203 /* sent -- waiting for a response */
204 ldap_mark_select_read( ld, lc->lconn_sb );
205 ldap_clear_select_write( ld, lc->lconn_sb );
211 * protected by req_mutex
212 * if m_noconn then protect using conn_lock
213 * else already protected with conn_lock
214 * if m_res then also protected by res_mutex
218 ldap_send_server_request(
222 LDAPRequest *parentreq,
223 LDAPURLDesc **srvlist,
232 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
233 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
236 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
238 LDAP_CONN_LOCK_IF(m_noconn);
240 if ( srvlist == NULL ) {
243 lc = find_connection( ld, *srvlist, 1 );
245 if ( (bind != NULL) && (parentreq != NULL) ) {
246 /* Remember the bind in the parent */
248 ++parentreq->lr_outrefcnt;
250 lc = ldap_new_connection( ld, srvlist, 0,
256 /* async connect... */
257 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
258 ber_socket_t sd = AC_SOCKET_ERROR;
259 struct timeval tv = { 0 };
261 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
264 switch ( ldap_int_poll( ld, sd, &tv, 1 ) ) {
267 lc->lconn_status = LDAP_CONNST_CONNECTED;
271 /* async only occurs if a network timeout is set */
273 /* honor network timeout */
274 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
275 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
277 /* caller will have to call again */
278 ld->ld_errno = LDAP_X_CONNECTING;
280 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
289 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
290 if ( ld->ld_errno == LDAP_SUCCESS ) {
291 ld->ld_errno = LDAP_SERVER_DOWN;
296 /* Forget about the bind */
297 --parentreq->lr_outrefcnt;
299 LDAP_CONN_UNLOCK_IF(m_noconn);
303 use_connection( ld, lc );
305 #ifdef LDAP_CONNECTIONLESS
306 if ( LDAP_IS_UDP( ld )) {
307 BerElement tmpber = *ber;
308 ber_rewind( &tmpber );
309 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
310 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
311 sizeof( struct sockaddr ), 0 );
312 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
314 ld->ld_errno = LDAP_ENCODING_ERROR;
315 LDAP_CONN_UNLOCK_IF(m_noconn);
321 /* If we still have an incomplete write, try to finish it before
322 * dealing with the new request. If we don't finish here, return
323 * LDAP_BUSY and let the caller retry later. We only allow a single
324 * request to be in WRITING state.
327 if ( ld->ld_requests &&
328 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
329 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
334 LDAP_CONN_UNLOCK_IF(m_noconn);
338 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
340 ld->ld_errno = LDAP_NO_MEMORY;
341 ldap_free_connection( ld, lc, 0, 0 );
344 /* Forget about the bind */
345 --parentreq->lr_outrefcnt;
347 LDAP_CONN_UNLOCK_IF(m_noconn);
350 lr->lr_msgid = msgid;
351 lr->lr_status = LDAP_REQST_INPROGRESS;
352 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
355 if ( parentreq != NULL ) { /* sub-request */
357 /* Increment if we didn't do it before the bind */
358 ++parentreq->lr_outrefcnt;
360 lr->lr_origid = parentreq->lr_origid;
361 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
362 lr->lr_parent = parentreq;
363 lr->lr_refnext = parentreq->lr_child;
364 parentreq->lr_child = lr;
365 } else { /* original request */
366 lr->lr_origid = lr->lr_msgid;
369 /* Extract requestDN for future reference */
371 BerElement tmpber = *ber;
375 ber_reset( &tmpber, 1 );
376 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
379 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
381 case LDAP_REQ_DELETE:
384 rtag = ber_scanf( &tmpber, "{" /*}*/ );
385 case LDAP_REQ_ABANDON:
388 if ( tag != LDAP_REQ_ABANDON ) {
389 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
390 lr->lr_dn.bv_val = tmpber.ber_ptr;
395 lr->lr_next = ld->ld_requests;
396 if ( lr->lr_next != NULL ) {
397 lr->lr_next->lr_prev = lr;
399 ld->ld_requests = lr;
401 ld->ld_errno = LDAP_SUCCESS;
402 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
406 LDAP_CONN_UNLOCK_IF(m_noconn);
410 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
412 find_tls_ext( LDAPURLDesc *srv )
417 if ( !srv->lud_exts )
420 for (i=0; srv->lud_exts[i]; i++) {
422 ext = srv->lud_exts[i];
423 if ( ext[0] == '!') {
427 if ( !strcasecmp( ext, "StartTLS" ) ||
428 !strcasecmp( ext, "X-StartTLS" ) ||
429 !strcmp( ext, LDAP_EXOP_START_TLS )) {
437 * always protected by conn_mutex
438 * optionally protected by req_mutex and res_mutex
441 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
442 int connect, LDAPreqinfo *bind, int m_req, int m_res )
447 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
448 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
449 use_ldsb, connect, (bind != NULL) );
451 * make a new LDAP server connection
452 * XXX open connection synchronously for now
454 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
456 ld->ld_errno = LDAP_NO_MEMORY;
461 assert( ld->ld_sb != NULL );
462 lc->lconn_sb = ld->ld_sb;
465 lc->lconn_sb = ber_sockbuf_alloc();
466 if ( lc->lconn_sb == NULL ) {
467 LDAP_FREE( (char *)lc );
468 ld->ld_errno = LDAP_NO_MEMORY;
474 LDAPURLDesc **srvp, *srv = NULL;
476 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
478 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
481 rc = ldap_int_open_connection( ld, lc, *srvp, async );
485 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
486 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
495 ber_sockbuf_free( lc->lconn_sb );
497 LDAP_FREE( (char *)lc );
498 ld->ld_errno = LDAP_SERVER_DOWN;
502 lc->lconn_server = ldap_url_dup( srv );
505 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
506 lc->lconn_next = ld->ld_conns;
511 if ( lc->lconn_server->lud_exts ) {
512 int rc, ext = find_tls_ext( lc->lconn_server );
514 LDAPConn *savedefconn;
516 savedefconn = ld->ld_defconn;
517 ++lc->lconn_refcnt; /* avoid premature free */
520 LDAP_REQ_UNLOCK_IF(m_req);
521 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
522 LDAP_RES_UNLOCK_IF(m_res);
523 rc = ldap_start_tls_s( ld, NULL, NULL );
524 LDAP_RES_LOCK_IF(m_res);
525 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
526 LDAP_REQ_LOCK_IF(m_req);
527 ld->ld_defconn = savedefconn;
530 if ( rc != LDAP_SUCCESS && ext == 2 ) {
531 ldap_free_connection( ld, lc, 1, 0 );
539 if ( bind != NULL ) {
541 LDAPConn *savedefconn;
543 /* Set flag to prevent additional referrals
544 * from being processed on this
545 * connection until the bind has completed
547 lc->lconn_rebind_inprogress = 1;
548 /* V3 rebind function */
549 if ( ld->ld_rebind_proc != NULL) {
550 LDAPURLDesc *srvfunc;
552 srvfunc = ldap_url_dup( *srvlist );
553 if ( srvfunc == NULL ) {
554 ld->ld_errno = LDAP_NO_MEMORY;
557 savedefconn = ld->ld_defconn;
558 ++lc->lconn_refcnt; /* avoid premature free */
561 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
562 LDAP_REQ_UNLOCK_IF(m_req);
563 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
564 LDAP_RES_UNLOCK_IF(m_res);
565 err = (*ld->ld_rebind_proc)( ld,
566 bind->ri_url, bind->ri_request, bind->ri_msgid,
567 ld->ld_rebind_params );
568 LDAP_RES_LOCK_IF(m_res);
569 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
570 LDAP_REQ_LOCK_IF(m_req);
572 ld->ld_defconn = savedefconn;
577 ldap_free_connection( ld, lc, 1, 0 );
580 ldap_free_urldesc( srvfunc );
585 struct berval passwd = BER_BVNULL;
587 savedefconn = ld->ld_defconn;
588 ++lc->lconn_refcnt; /* avoid premature free */
591 Debug( LDAP_DEBUG_TRACE,
592 "anonymous rebind via ldap_sasl_bind(\"\")\n",
595 LDAP_REQ_UNLOCK_IF(m_req);
596 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
597 LDAP_RES_UNLOCK_IF(m_res);
598 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
599 NULL, NULL, &msgid );
600 if ( rc != LDAP_SUCCESS ) {
604 for ( err = 1; err > 0; ) {
605 struct timeval tv = { 0, 100000 };
606 LDAPMessage *res = NULL;
608 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
614 #ifdef LDAP_R_COMPILE
615 ldap_pvt_thread_yield();
620 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
621 if ( rc != LDAP_SUCCESS ) {
624 } else if ( err != LDAP_SUCCESS ) {
627 /* else err == LDAP_SUCCESS == 0 */
631 Debug( LDAP_DEBUG_TRACE,
632 "ldap_new_connection %p: "
633 "unexpected response %d "
634 "from BIND request id=%d\n",
635 (void *) ld, ldap_msgtype( res ), msgid );
641 LDAP_RES_LOCK_IF(m_res);
642 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
643 LDAP_REQ_LOCK_IF(m_req);
644 ld->ld_defconn = savedefconn;
648 ldap_free_connection( ld, lc, 1, 0 );
653 lc->lconn_rebind_inprogress = 0;
659 /* protected by ld_conn_mutex */
661 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
663 * return an existing connection (if any) to the server srv
664 * if "any" is non-zero, check for any server in the "srv" chain
668 LDAPURLDesc *lcu, *lsu;
669 int lcu_port, lsu_port;
672 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
673 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
674 lcu = lc->lconn_server;
675 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
678 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
679 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
682 if ( lsu_port == lcu_port
683 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
684 && lcu->lud_host != NULL && lsu->lud_host != NULL
685 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
701 /* protected by ld_conn_mutex */
703 use_connection( LDAP *ld, LDAPConn *lc )
705 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
707 lc->lconn_lastused = time( NULL );
711 /* protected by ld_conn_mutex */
713 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
715 LDAPConn *tmplc, *prevlc;
717 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
718 Debug( LDAP_DEBUG_TRACE,
719 "ldap_free_connection %d %d\n",
722 if ( force || --lc->lconn_refcnt <= 0 ) {
723 /* remove from connections list first */
725 for ( prevlc = NULL, tmplc = ld->ld_conns;
727 tmplc = tmplc->lconn_next )
730 if ( prevlc == NULL ) {
731 ld->ld_conns = tmplc->lconn_next;
733 prevlc->lconn_next = tmplc->lconn_next;
735 if ( ld->ld_defconn == lc ) {
736 ld->ld_defconn = NULL;
743 /* process connection callbacks */
745 struct ldapoptions *lo;
749 lo = &ld->ld_options;
750 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
751 if ( lo->ldo_conn_cbs ) {
752 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
754 cb->lc_del( ld, lc->lconn_sb, cb );
757 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
758 lo = LDAP_INT_GLOBAL_OPT();
759 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
760 if ( lo->ldo_conn_cbs ) {
761 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
763 cb->lc_del( ld, lc->lconn_sb, cb );
766 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
769 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
770 ldap_mark_select_clear( ld, lc->lconn_sb );
772 ldap_send_unbind( ld, lc->lconn_sb,
777 if ( lc->lconn_ber != NULL ) {
778 ber_free( lc->lconn_ber, 1 );
781 ldap_int_sasl_close( ld, lc );
783 ldap_int_gssapi_close( ld, lc );
786 ldap_free_urllist( lc->lconn_server );
788 /* FIXME: is this at all possible?
789 * ldap_ld_free() in unbind.c calls ldap_free_connection()
790 * with force == 1 __after__ explicitly calling
791 * ldap_free_request() on all requests */
795 for ( lr = ld->ld_requests; lr; ) {
796 LDAPRequest *lr_next = lr->lr_next;
798 if ( lr->lr_conn == lc ) {
799 ldap_free_request_int( ld, lr );
806 if ( lc->lconn_sb != ld->ld_sb ) {
807 ber_sockbuf_free( lc->lconn_sb );
809 ber_int_sb_close( lc->lconn_sb );
812 if ( lc->lconn_rebind_queue != NULL) {
814 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
815 LDAP_VFREE( lc->lconn_rebind_queue[i] );
817 LDAP_FREE( lc->lconn_rebind_queue );
822 Debug( LDAP_DEBUG_TRACE,
823 "ldap_free_connection: actually freed\n",
827 lc->lconn_lastused = time( NULL );
828 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
829 lc->lconn_refcnt, 0, 0 );
834 /* Protects self with ld_conn_mutex */
837 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
842 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
843 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
844 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
845 if ( lc->lconn_server != NULL ) {
846 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
847 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
848 : lc->lconn_server->lud_host,
849 lc->lconn_server->lud_port, ( lc->lconn_sb ==
850 ld->ld_sb ) ? " (default)" : "" );
852 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
853 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
855 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
856 ? "Connecting" : "Connected", 0 );
857 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
858 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
859 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
860 if ( lc->lconn_rebind_inprogress ) {
861 if ( lc->lconn_rebind_queue != NULL) {
864 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
866 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
867 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
868 i, j, lc->lconn_rebind_queue[i][j] );
872 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
875 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
880 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
884 /* protected by req_mutex and res_mutex */
886 ldap_dump_requests_and_responses( LDAP *ld )
892 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
894 lr = ld->ld_requests;
896 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
898 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
899 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
900 lr->lr_msgid, lr->lr_origid,
901 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
902 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
903 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
904 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
905 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
907 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
908 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
910 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
911 (void *)ld, i, ld->ld_nabandoned );
912 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
913 if ( ( lm = ld->ld_responses ) == NULL ) {
914 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
916 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
917 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
918 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
919 if ( lm->lm_chain != NULL ) {
920 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
921 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
922 Debug( LDAP_DEBUG_TRACE,
923 " * msgid %d, type %lu\n",
925 (unsigned long)l->lm_msgtype, 0 );
929 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
931 #endif /* LDAP_DEBUG */
933 /* protected by req_mutex */
935 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
937 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
938 /* if lr_refcnt > 0, the request has been looked up
939 * by ldap_find_request_by_msgid(); if in the meanwhile
940 * the request is free()'d by someone else, just decrease
941 * the reference count and extract it from the request
942 * list; later on, it will be freed. */
943 if ( lr->lr_prev == NULL ) {
944 if ( lr->lr_refcnt == 0 ) {
945 /* free'ing the first request? */
946 assert( ld->ld_requests == lr );
949 if ( ld->ld_requests == lr ) {
950 ld->ld_requests = lr->lr_next;
954 lr->lr_prev->lr_next = lr->lr_next;
957 if ( lr->lr_next != NULL ) {
958 lr->lr_next->lr_prev = lr->lr_prev;
961 if ( lr->lr_refcnt > 0 ) {
962 lr->lr_refcnt = -lr->lr_refcnt;
970 if ( lr->lr_ber != NULL ) {
971 ber_free( lr->lr_ber, 1 );
975 if ( lr->lr_res_error != NULL ) {
976 LDAP_FREE( lr->lr_res_error );
977 lr->lr_res_error = NULL;
980 if ( lr->lr_res_matched != NULL ) {
981 LDAP_FREE( lr->lr_res_matched );
982 lr->lr_res_matched = NULL;
988 /* protected by req_mutex */
990 ldap_free_request( LDAP *ld, LDAPRequest *lr )
992 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
993 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
994 lr->lr_origid, lr->lr_msgid, 0 );
996 /* free all referrals (child requests) */
997 while ( lr->lr_child ) {
998 ldap_free_request( ld, lr->lr_child );
1001 if ( lr->lr_parent != NULL ) {
1004 --lr->lr_parent->lr_outrefcnt;
1005 for ( lrp = &lr->lr_parent->lr_child;
1007 lrp = &(*lrp)->lr_refnext );
1010 *lrp = lr->lr_refnext;
1013 ldap_free_request_int( ld, lr );
1017 * call first time with *cntp = -1
1018 * when returns *cntp == -1, no referrals are left
1020 * NOTE: may replace *refsp, or shuffle the contents
1021 * of the original array.
1023 static int ldap_int_nextref(
1029 assert( refsp != NULL );
1030 assert( *refsp != NULL );
1031 assert( cntp != NULL );
1040 if ( (*refsp)[ *cntp ] == NULL ) {
1048 * Chase v3 referrals
1051 * (IN) ld = LDAP connection handle
1052 * (IN) lr = LDAP Request structure
1053 * (IN) refs = array of pointers to referral strings that we will chase
1054 * The array will be free'd by this function when no longer needed
1055 * (IN) sref != 0 if following search reference
1056 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1057 * (OUT) hadrefp = 1 if sucessfully followed referral
1059 * Return value - number of referrals followed
1061 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1064 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1067 int unfollowedcnt = 0;
1068 LDAPRequest *origreq;
1069 LDAPURLDesc *srv = NULL;
1071 char **refarray = NULL;
1073 int rc, count, i, j, id;
1075 LDAP_NEXTREF_PROC *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
1077 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1078 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1079 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1080 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1082 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1088 /* If no referrals in array, return */
1089 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1094 /* Check for hop limit exceeded */
1095 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1096 Debug( LDAP_DEBUG_ANY,
1097 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1098 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1103 /* find original request */
1105 origreq->lr_parent != NULL;
1106 origreq = origreq->lr_parent )
1114 /* parse out & follow referrals */
1115 /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
1117 for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1119 nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1122 /* Parse the referral URL */
1123 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1124 if ( rc != LDAP_URL_SUCCESS ) {
1125 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1126 * which do not map on API errors */
1127 ld->ld_errno = LDAP_PARAM_ERROR;
1132 if( srv->lud_crit_exts ) {
1135 /* If StartTLS is the only critical ext, OK. */
1136 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1140 /* we do not support any other extensions */
1141 ld->ld_errno = LDAP_NOT_SUPPORTED;
1147 /* check connection for re-bind in progress */
1148 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1149 /* See if we've already requested this DN with this conn */
1152 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1153 for ( lp = origreq; lp; ) {
1154 if ( lp->lr_conn == lc
1155 && len == lp->lr_dn.bv_len
1157 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1162 if ( lp == origreq ) {
1165 lp = lp->lr_refnext;
1169 ldap_free_urllist( srv );
1171 ld->ld_errno = LDAP_CLIENT_LOOP;
1176 if ( lc->lconn_rebind_inprogress ) {
1177 /* We are already chasing a referral or search reference and a
1178 * bind on that connection is in progress. We must queue
1179 * referrals on that connection, so we don't get a request
1180 * going out before the bind operation completes. This happens
1181 * if two search references come in one behind the other
1182 * for the same server with different contexts.
1184 Debug( LDAP_DEBUG_TRACE,
1185 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1187 if( lc->lconn_rebind_queue == NULL ) {
1188 /* Create a referral list */
1189 lc->lconn_rebind_queue =
1190 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1192 if( lc->lconn_rebind_queue == NULL) {
1193 ld->ld_errno = LDAP_NO_MEMORY;
1198 lc->lconn_rebind_queue[0] = refarray;
1199 lc->lconn_rebind_queue[1] = NULL;
1203 /* Count how many referral arrays we already have */
1204 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1208 /* Add the new referral to the list */
1209 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1210 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1212 if( lc->lconn_rebind_queue == NULL ) {
1213 ld->ld_errno = LDAP_NO_MEMORY;
1217 lc->lconn_rebind_queue[j] = refarray;
1218 lc->lconn_rebind_queue[j+1] = NULL;
1222 /* We have queued the referral/reference, now just return */
1225 count = 1; /* Pretend we already followed referral */
1229 /* Re-encode the request with the new starting point of the search.
1230 * Note: In the future we also need to replace the filter if one
1231 * was provided with the search reference
1234 /* For references we don't want old dn if new dn empty */
1235 if ( sref && srv->lud_dn == NULL ) {
1236 srv->lud_dn = LDAP_STRDUP( "" );
1239 LDAP_NEXT_MSGID( ld, id );
1240 ber = re_encode_request( ld, origreq->lr_ber, id,
1241 sref, srv, &rinfo.ri_request );
1244 ld->ld_errno = LDAP_ENCODING_ERROR;
1249 Debug( LDAP_DEBUG_TRACE,
1250 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1251 lr->lr_msgid, refarray[i], 0);
1253 /* Send the new request to the server - may require a bind */
1254 rinfo.ri_msgid = origreq->lr_origid;
1255 rinfo.ri_url = refarray[i];
1256 rc = ldap_send_server_request( ld, ber, id,
1257 origreq, &srv, NULL, &rinfo, 0, 1 );
1259 /* Failure, try next referral in the list */
1260 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1261 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1262 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1263 ldap_free_urllist( srv );
1265 ld->ld_errno = LDAP_REFERRAL;
1267 /* Success, no need to try this referral list further */
1272 /* check if there is a queue of referrals that came in during bind */
1274 lc = find_connection( ld, srv, 1 );
1276 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1278 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
1283 if ( lc->lconn_rebind_queue != NULL ) {
1284 /* Release resources of previous list */
1285 LDAP_VFREE( refarray );
1287 ldap_free_urllist( srv );
1290 /* Pull entries off end of queue so list always null terminated */
1291 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1293 refarray = lc->lconn_rebind_queue[j - 1];
1294 lc->lconn_rebind_queue[j-1] = NULL;
1295 /* we pulled off last entry from queue, free queue */
1297 LDAP_FREE( lc->lconn_rebind_queue );
1298 lc->lconn_rebind_queue = NULL;
1300 /* restart the loop the with new referral list */
1304 break; /* referral followed, break out of for loop */
1306 } /* end for loop */
1308 LDAP_VFREE( refarray );
1309 ldap_free_urllist( srv );
1310 LDAP_FREE( *errstrp );
1314 LDAP_FREE( unfollowed );
1317 *errstrp = unfollowed;
1323 * XXX merging of errors in this routine needs to be improved
1324 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1327 ldap_chase_referrals( LDAP *ld,
1335 char *p, *ref, *unfollowed;
1336 LDAPRequest *origreq;
1342 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1343 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1344 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1345 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1347 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1350 if ( *errstrp == NULL ) {
1354 len = strlen( *errstrp );
1355 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1356 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1358 p += LDAP_REF_STR_LEN;
1363 if ( len < LDAP_REF_STR_LEN ) {
1367 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1368 Debug( LDAP_DEBUG_ANY,
1369 "more than %d referral hops (dropping)\n",
1370 ld->ld_refhoplimit, 0, 0 );
1371 /* XXX report as error in ld->ld_errno? */
1375 /* find original request */
1376 for ( origreq = lr; origreq->lr_parent != NULL;
1377 origreq = origreq->lr_parent ) {
1384 /* parse out & follow referrals */
1385 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1386 p = strchr( ref, '\n' );
1391 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1392 if ( rc != LDAP_URL_SUCCESS ) {
1393 Debug( LDAP_DEBUG_TRACE,
1394 "ignoring %s referral <%s>\n",
1395 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1396 rc = ldap_append_referral( ld, &unfollowed, ref );
1401 Debug( LDAP_DEBUG_TRACE,
1402 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1406 /* See if we've already been here */
1407 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1410 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1411 for ( lp = lr; lp; lp = lp->lr_parent ) {
1412 if ( lp->lr_conn == lc
1413 && len == lp->lr_dn.bv_len )
1415 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1422 ldap_free_urllist( srv );
1423 ld->ld_errno = LDAP_CLIENT_LOOP;
1429 LDAP_NEXT_MSGID( ld, id );
1430 ber = re_encode_request( ld, origreq->lr_ber,
1431 id, sref, srv, &rinfo.ri_request );
1433 if ( ber == NULL ) {
1437 /* copy the complete referral for rebind process */
1438 rinfo.ri_url = LDAP_STRDUP( ref );
1440 rinfo.ri_msgid = origreq->lr_origid;
1442 rc = ldap_send_server_request( ld, ber, id,
1443 lr, &srv, NULL, &rinfo, 0, 1 );
1444 LDAP_FREE( rinfo.ri_url );
1449 Debug( LDAP_DEBUG_ANY,
1450 "Unable to chase referral \"%s\" (%d: %s)\n",
1451 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1452 rc = ldap_append_referral( ld, &unfollowed, ref );
1455 ldap_free_urllist(srv);
1458 LDAP_FREE( *errstrp );
1459 *errstrp = unfollowed;
1461 return(( rc == 0 ) ? count : rc );
1466 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1470 if ( *referralsp == NULL ) {
1472 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1476 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1477 strlen( *referralsp ) + strlen( s ) + 2 );
1480 if ( *referralsp == NULL ) {
1481 ld->ld_errno = LDAP_NO_MEMORY;
1486 strcpy( *referralsp, LDAP_REF_STR );
1488 strcat( *referralsp, "\n" );
1490 strcat( *referralsp, s );
1498 re_encode_request( LDAP *ld,
1499 BerElement *origber,
1506 * XXX this routine knows way too much about how the lber library works!
1514 BerElement tmpber, *ber;
1517 Debug( LDAP_DEBUG_TRACE,
1518 "re_encode_request: new msgid %ld, new dn <%s>\n",
1520 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1525 * all LDAP requests are sequences that start with a message id.
1526 * For all except delete, this is followed by a sequence that is
1527 * tagged with the operation code. For delete, the provided DN
1528 * is not wrapped by a sequence.
1530 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1532 if ( rtag == LBER_ERROR ) {
1533 ld->ld_errno = LDAP_DECODING_ERROR;
1538 if ( tag == LDAP_REQ_BIND ) {
1539 /* bind requests have a version number before the DN & other stuff */
1540 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1542 } else if ( tag == LDAP_REQ_DELETE ) {
1543 /* delete requests don't have a DN wrapping sequence */
1544 rtag = ber_scanf( &tmpber, "m", &dn );
1546 } else if ( tag == LDAP_REQ_SEARCH ) {
1547 /* search requests need to be re-scope-ed */
1548 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1550 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1551 /* use the scope provided in reference */
1552 scope = srv->lud_scope;
1554 } else if ( sref ) {
1555 /* use scope implied by previous operation
1558 * subtree -> subtree
1559 * subordinate -> subtree
1563 case LDAP_SCOPE_BASE:
1564 case LDAP_SCOPE_ONELEVEL:
1565 scope = LDAP_SCOPE_BASE;
1567 case LDAP_SCOPE_SUBTREE:
1568 case LDAP_SCOPE_SUBORDINATE:
1569 scope = LDAP_SCOPE_SUBTREE;
1575 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1578 if( rtag == LBER_ERROR ) {
1579 ld->ld_errno = LDAP_DECODING_ERROR;
1583 /* restore character zero'd out by ber_scanf*/
1584 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1586 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1590 if ( srv->lud_dn ) {
1591 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1594 if ( tag == LDAP_REQ_BIND ) {
1595 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1596 } else if ( tag == LDAP_REQ_DELETE ) {
1597 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1598 } else if ( tag == LDAP_REQ_SEARCH ) {
1599 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1601 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1605 ld->ld_errno = LDAP_ENCODING_ERROR;
1610 if ( tag != LDAP_REQ_DELETE && (
1611 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1612 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1613 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1615 ld->ld_errno = LDAP_ENCODING_ERROR;
1621 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1622 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1624 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1626 #endif /* LDAP_DEBUG */
1628 *type = tag; /* return request type */
1633 /* protected by req_mutex */
1635 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1639 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1640 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1641 continue; /* Skip completed requests */
1643 if ( msgid == lr->lr_msgid ) {
1652 /* protected by req_mutex */
1654 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1658 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1660 if ( lr->lr_refcnt > 0 ) {
1663 } else if ( lr->lr_refcnt < 0 ) {
1665 if ( lr->lr_refcnt == 0 ) {
1673 ldap_free_request_int( ld, lrx );
1675 } else if ( freeit ) {
1676 ldap_free_request( ld, lrx );