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(
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 );
210 * protected by req_mutex
211 * if m_noconn then protect using conn_lock
212 * else already protected with conn_lock
213 * if m_res then also protected by res_mutex
217 ldap_send_server_request(
221 LDAPRequest *parentreq,
222 LDAPURLDesc **srvlist,
231 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
232 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
235 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
237 LDAP_CONN_LOCK_IF(m_noconn);
239 if ( srvlist == NULL ) {
242 lc = find_connection( ld, *srvlist, 1 );
244 if ( (bind != NULL) && (parentreq != NULL) ) {
245 /* Remember the bind in the parent */
247 ++parentreq->lr_outrefcnt;
249 lc = ldap_new_connection( ld, srvlist, 0,
255 /* async connect... */
256 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
257 ber_socket_t sd = AC_SOCKET_ERROR;
258 struct timeval tv = { 0 };
260 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
263 switch ( ldap_int_poll( ld, sd, &tv ) ) {
266 lc->lconn_status = LDAP_CONNST_CONNECTED;
270 /* async only occurs if a network timeout is set */
272 /* honor network timeout */
273 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
274 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
276 /* caller will have to call again */
277 ld->ld_errno = LDAP_X_CONNECTING;
279 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
288 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
289 if ( ld->ld_errno == LDAP_SUCCESS ) {
290 ld->ld_errno = LDAP_SERVER_DOWN;
295 /* Forget about the bind */
296 --parentreq->lr_outrefcnt;
298 LDAP_CONN_UNLOCK_IF(m_noconn);
302 use_connection( ld, lc );
304 #ifdef LDAP_CONNECTIONLESS
305 if ( LDAP_IS_UDP( ld )) {
306 BerElement tmpber = *ber;
307 ber_rewind( &tmpber );
308 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
309 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
310 sizeof( struct sockaddr ), 0 );
311 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
313 ld->ld_errno = LDAP_ENCODING_ERROR;
314 LDAP_CONN_UNLOCK_IF(m_noconn);
320 /* If we still have an incomplete write, try to finish it before
321 * dealing with the new request. If we don't finish here, return
322 * LDAP_BUSY and let the caller retry later. We only allow a single
323 * request to be in WRITING state.
326 if ( ld->ld_requests &&
327 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
328 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
333 LDAP_CONN_UNLOCK_IF(m_noconn);
337 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
339 ld->ld_errno = LDAP_NO_MEMORY;
340 ldap_free_connection( ld, lc, 0, 0 );
343 /* Forget about the bind */
344 --parentreq->lr_outrefcnt;
346 LDAP_CONN_UNLOCK_IF(m_noconn);
349 lr->lr_msgid = msgid;
350 lr->lr_status = LDAP_REQST_INPROGRESS;
351 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
354 if ( parentreq != NULL ) { /* sub-request */
356 /* Increment if we didn't do it before the bind */
357 ++parentreq->lr_outrefcnt;
359 lr->lr_origid = parentreq->lr_origid;
360 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
361 lr->lr_parent = parentreq;
362 lr->lr_refnext = parentreq->lr_child;
363 parentreq->lr_child = lr;
364 } else { /* original request */
365 lr->lr_origid = lr->lr_msgid;
368 /* Extract requestDN for future reference */
370 BerElement tmpber = *ber;
374 ber_reset( &tmpber, 1 );
375 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
378 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
380 case LDAP_REQ_DELETE:
383 rtag = ber_scanf( &tmpber, "{" /*}*/ );
384 case LDAP_REQ_ABANDON:
387 if ( tag != LDAP_REQ_ABANDON ) {
388 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
389 lr->lr_dn.bv_val = tmpber.ber_ptr;
394 lr->lr_next = ld->ld_requests;
395 if ( lr->lr_next != NULL ) {
396 lr->lr_next->lr_prev = lr;
398 ld->ld_requests = lr;
400 ld->ld_errno = LDAP_SUCCESS;
401 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
405 LDAP_CONN_UNLOCK_IF(m_noconn);
409 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
411 find_tls_ext( LDAPURLDesc *srv )
416 if ( !srv->lud_exts )
419 for (i=0; srv->lud_exts[i]; i++) {
421 ext = srv->lud_exts[i];
422 if ( ext[0] == '!') {
426 if ( !strcasecmp( ext, "StartTLS" ) ||
427 !strcasecmp( ext, "X-StartTLS" ) ||
428 !strcmp( ext, LDAP_EXOP_START_TLS )) {
436 * always protected by conn_mutex
437 * optionally protected by req_mutex and res_mutex
440 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
441 int connect, LDAPreqinfo *bind, int m_req, int m_res )
446 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
447 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
448 use_ldsb, connect, (bind != NULL) );
450 * make a new LDAP server connection
451 * XXX open connection synchronously for now
453 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
455 ld->ld_errno = LDAP_NO_MEMORY;
460 assert( ld->ld_sb != NULL );
461 lc->lconn_sb = ld->ld_sb;
464 lc->lconn_sb = ber_sockbuf_alloc();
465 if ( lc->lconn_sb == NULL ) {
466 LDAP_FREE( (char *)lc );
467 ld->ld_errno = LDAP_NO_MEMORY;
473 LDAPURLDesc **srvp, *srv = NULL;
475 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
477 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
480 rc = ldap_int_open_connection( ld, lc, *srvp, async );
484 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
485 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
494 ber_sockbuf_free( lc->lconn_sb );
496 LDAP_FREE( (char *)lc );
497 ld->ld_errno = LDAP_SERVER_DOWN;
501 lc->lconn_server = ldap_url_dup( srv );
504 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
505 lc->lconn_next = ld->ld_conns;
510 if ( lc->lconn_server->lud_exts ) {
511 int rc, ext = find_tls_ext( lc->lconn_server );
513 LDAPConn *savedefconn;
515 savedefconn = ld->ld_defconn;
516 ++lc->lconn_refcnt; /* avoid premature free */
519 LDAP_REQ_UNLOCK_IF(m_req);
520 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
521 LDAP_RES_UNLOCK_IF(m_res);
522 rc = ldap_start_tls_s( ld, NULL, NULL );
523 LDAP_RES_LOCK_IF(m_res);
524 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
525 LDAP_REQ_LOCK_IF(m_req);
526 ld->ld_defconn = savedefconn;
529 if ( rc != LDAP_SUCCESS && ext == 2 ) {
530 ldap_free_connection( ld, lc, 1, 0 );
538 if ( bind != NULL ) {
540 LDAPConn *savedefconn;
542 /* Set flag to prevent additional referrals
543 * from being processed on this
544 * connection until the bind has completed
546 lc->lconn_rebind_inprogress = 1;
547 /* V3 rebind function */
548 if ( ld->ld_rebind_proc != NULL) {
549 LDAPURLDesc *srvfunc;
551 srvfunc = ldap_url_dup( *srvlist );
552 if ( srvfunc == NULL ) {
553 ld->ld_errno = LDAP_NO_MEMORY;
556 savedefconn = ld->ld_defconn;
557 ++lc->lconn_refcnt; /* avoid premature free */
560 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
561 LDAP_REQ_UNLOCK_IF(m_req);
562 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
563 LDAP_RES_UNLOCK_IF(m_res);
564 err = (*ld->ld_rebind_proc)( ld,
565 bind->ri_url, bind->ri_request, bind->ri_msgid,
566 ld->ld_rebind_params );
567 LDAP_RES_LOCK_IF(m_res);
568 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
569 LDAP_REQ_LOCK_IF(m_req);
571 ld->ld_defconn = savedefconn;
576 ldap_free_connection( ld, lc, 1, 0 );
579 ldap_free_urldesc( srvfunc );
584 struct berval passwd = BER_BVNULL;
586 savedefconn = ld->ld_defconn;
587 ++lc->lconn_refcnt; /* avoid premature free */
590 Debug( LDAP_DEBUG_TRACE,
591 "anonymous rebind via ldap_sasl_bind(\"\")\n",
594 LDAP_REQ_UNLOCK_IF(m_req);
595 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
596 LDAP_RES_UNLOCK_IF(m_res);
597 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
598 NULL, NULL, &msgid );
599 if ( rc != LDAP_SUCCESS ) {
603 for ( err = 1; err > 0; ) {
604 struct timeval tv = { 0, 100000 };
605 LDAPMessage *res = NULL;
607 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
613 #ifdef LDAP_R_COMPILE
614 ldap_pvt_thread_yield();
619 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
620 if ( rc != LDAP_SUCCESS ) {
623 } else if ( err != LDAP_SUCCESS ) {
626 /* else err == LDAP_SUCCESS == 0 */
630 Debug( LDAP_DEBUG_TRACE,
631 "ldap_new_connection %p: "
632 "unexpected response %d "
633 "from BIND request id=%d\n",
634 (void *) ld, ldap_msgtype( res ), msgid );
640 LDAP_RES_LOCK_IF(m_res);
641 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
642 LDAP_REQ_LOCK_IF(m_req);
643 ld->ld_defconn = savedefconn;
647 ldap_free_connection( ld, lc, 1, 0 );
652 lc->lconn_rebind_inprogress = 0;
658 /* protected by ld_conn_mutex */
660 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
662 * return an existing connection (if any) to the server srv
663 * if "any" is non-zero, check for any server in the "srv" chain
667 LDAPURLDesc *lcu, *lsu;
668 int lcu_port, lsu_port;
671 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
672 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
673 lcu = lc->lconn_server;
674 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
677 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
678 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
681 if ( lsu_port == lcu_port
682 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
683 && lcu->lud_host != NULL && lsu->lud_host != NULL
684 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
700 /* protected by ld_conn_mutex */
702 use_connection( LDAP *ld, LDAPConn *lc )
704 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
706 lc->lconn_lastused = time( NULL );
710 /* protected by ld_conn_mutex */
712 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
714 LDAPConn *tmplc, *prevlc;
716 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
717 Debug( LDAP_DEBUG_TRACE,
718 "ldap_free_connection %d %d\n",
721 if ( force || --lc->lconn_refcnt <= 0 ) {
722 /* remove from connections list first */
724 for ( prevlc = NULL, tmplc = ld->ld_conns;
726 tmplc = tmplc->lconn_next )
729 if ( prevlc == NULL ) {
730 ld->ld_conns = tmplc->lconn_next;
732 prevlc->lconn_next = tmplc->lconn_next;
734 if ( ld->ld_defconn == lc ) {
735 ld->ld_defconn = NULL;
742 /* process connection callbacks */
744 struct ldapoptions *lo;
748 lo = &ld->ld_options;
749 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
750 if ( lo->ldo_conn_cbs ) {
751 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
753 cb->lc_del( ld, lc->lconn_sb, cb );
756 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
757 lo = LDAP_INT_GLOBAL_OPT();
758 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
759 if ( lo->ldo_conn_cbs ) {
760 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
762 cb->lc_del( ld, lc->lconn_sb, cb );
765 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
768 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
769 ldap_mark_select_clear( ld, lc->lconn_sb );
771 ldap_send_unbind( ld, lc->lconn_sb,
776 if ( lc->lconn_ber != NULL ) {
777 ber_free( lc->lconn_ber, 1 );
780 ldap_int_sasl_close( ld, lc );
782 ldap_int_gssapi_close( ld, lc );
785 ldap_free_urllist( lc->lconn_server );
787 /* FIXME: is this at all possible?
788 * ldap_ld_free() in unbind.c calls ldap_free_connection()
789 * with force == 1 __after__ explicitly calling
790 * ldap_free_request() on all requests */
794 for ( lr = ld->ld_requests; lr; ) {
795 LDAPRequest *lr_next = lr->lr_next;
797 if ( lr->lr_conn == lc ) {
798 ldap_free_request_int( ld, lr );
805 if ( lc->lconn_sb != ld->ld_sb ) {
806 ber_sockbuf_free( lc->lconn_sb );
808 ber_int_sb_close( lc->lconn_sb );
811 if ( lc->lconn_rebind_queue != NULL) {
813 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
814 LDAP_VFREE( lc->lconn_rebind_queue[i] );
816 LDAP_FREE( lc->lconn_rebind_queue );
821 Debug( LDAP_DEBUG_TRACE,
822 "ldap_free_connection: actually freed\n",
826 lc->lconn_lastused = time( NULL );
827 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
828 lc->lconn_refcnt, 0, 0 );
833 /* Protects self with ld_conn_mutex */
836 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
841 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
842 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
843 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
844 if ( lc->lconn_server != NULL ) {
845 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
846 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
847 : lc->lconn_server->lud_host,
848 lc->lconn_server->lud_port, ( lc->lconn_sb ==
849 ld->ld_sb ) ? " (default)" : "" );
851 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
852 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
854 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
855 ? "Connecting" : "Connected", 0 );
856 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
857 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
858 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
859 if ( lc->lconn_rebind_inprogress ) {
860 if ( lc->lconn_rebind_queue != NULL) {
863 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
865 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
866 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
867 i, j, lc->lconn_rebind_queue[i][j] );
871 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
874 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
879 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
883 /* protected by req_mutex and res_mutex */
885 ldap_dump_requests_and_responses( LDAP *ld )
891 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
893 lr = ld->ld_requests;
895 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
897 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
898 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
899 lr->lr_msgid, lr->lr_origid,
900 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
901 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
902 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
903 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
904 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
906 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
907 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
909 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
910 (void *)ld, i, ld->ld_nabandoned );
911 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
912 if ( ( lm = ld->ld_responses ) == NULL ) {
913 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
915 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
916 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
917 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
918 if ( lm->lm_chain != NULL ) {
919 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
920 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
921 Debug( LDAP_DEBUG_TRACE,
922 " * msgid %d, type %lu\n",
924 (unsigned long)l->lm_msgtype, 0 );
928 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
930 #endif /* LDAP_DEBUG */
932 /* protected by req_mutex */
934 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
936 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
937 /* if lr_refcnt > 0, the request has been looked up
938 * by ldap_find_request_by_msgid(); if in the meanwhile
939 * the request is free()'d by someone else, just decrease
940 * the reference count and extract it from the request
941 * list; later on, it will be freed. */
942 if ( lr->lr_prev == NULL ) {
943 if ( lr->lr_refcnt == 0 ) {
944 /* free'ing the first request? */
945 assert( ld->ld_requests == lr );
948 if ( ld->ld_requests == lr ) {
949 ld->ld_requests = lr->lr_next;
953 lr->lr_prev->lr_next = lr->lr_next;
956 if ( lr->lr_next != NULL ) {
957 lr->lr_next->lr_prev = lr->lr_prev;
960 if ( lr->lr_refcnt > 0 ) {
961 lr->lr_refcnt = -lr->lr_refcnt;
969 if ( lr->lr_ber != NULL ) {
970 ber_free( lr->lr_ber, 1 );
974 if ( lr->lr_res_error != NULL ) {
975 LDAP_FREE( lr->lr_res_error );
976 lr->lr_res_error = NULL;
979 if ( lr->lr_res_matched != NULL ) {
980 LDAP_FREE( lr->lr_res_matched );
981 lr->lr_res_matched = NULL;
987 /* protected by req_mutex */
989 ldap_free_request( LDAP *ld, LDAPRequest *lr )
991 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
992 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
993 lr->lr_origid, lr->lr_msgid, 0 );
995 /* free all referrals (child requests) */
996 while ( lr->lr_child ) {
997 ldap_free_request( ld, lr->lr_child );
1000 if ( lr->lr_parent != NULL ) {
1003 --lr->lr_parent->lr_outrefcnt;
1004 for ( lrp = &lr->lr_parent->lr_child;
1006 lrp = &(*lrp)->lr_refnext );
1009 *lrp = lr->lr_refnext;
1012 ldap_free_request_int( ld, lr );
1016 * call first time with *cntp = -1
1017 * when returns *cntp == -1, no referrals are left
1019 * NOTE: may replace *refsp, or shuffle the contents
1020 * of the original array.
1022 static int ldap_int_nextref(
1028 assert( refsp != NULL );
1029 assert( *refsp != NULL );
1030 assert( cntp != NULL );
1039 if ( (*refsp)[ *cntp ] == NULL ) {
1047 * Chase v3 referrals
1050 * (IN) ld = LDAP connection handle
1051 * (IN) lr = LDAP Request structure
1052 * (IN) refs = array of pointers to referral strings that we will chase
1053 * The array will be free'd by this function when no longer needed
1054 * (IN) sref != 0 if following search reference
1055 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1056 * (OUT) hadrefp = 1 if sucessfully followed referral
1058 * Return value - number of referrals followed
1060 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1063 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1066 int unfollowedcnt = 0;
1067 LDAPRequest *origreq;
1068 LDAPURLDesc *srv = NULL;
1070 char **refarray = NULL;
1072 int rc, count, i, j, id;
1074 LDAP_NEXTREF_PROC *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
1076 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1077 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1078 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1079 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1081 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1087 /* If no referrals in array, return */
1088 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1093 /* Check for hop limit exceeded */
1094 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1095 Debug( LDAP_DEBUG_ANY,
1096 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1097 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1102 /* find original request */
1104 origreq->lr_parent != NULL;
1105 origreq = origreq->lr_parent )
1113 /* parse out & follow referrals */
1114 /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
1116 for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1118 nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1121 /* Parse the referral URL */
1122 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1123 if ( rc != LDAP_URL_SUCCESS ) {
1124 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1125 * which do not map on API errors */
1126 ld->ld_errno = LDAP_PARAM_ERROR;
1131 if( srv->lud_crit_exts ) {
1134 /* If StartTLS is the only critical ext, OK. */
1135 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1139 /* we do not support any other extensions */
1140 ld->ld_errno = LDAP_NOT_SUPPORTED;
1146 /* check connection for re-bind in progress */
1147 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1148 /* See if we've already requested this DN with this conn */
1151 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1152 for ( lp = origreq; lp; ) {
1153 if ( lp->lr_conn == lc
1154 && len == lp->lr_dn.bv_len
1156 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1161 if ( lp == origreq ) {
1164 lp = lp->lr_refnext;
1168 ldap_free_urllist( srv );
1170 ld->ld_errno = LDAP_CLIENT_LOOP;
1175 if ( lc->lconn_rebind_inprogress ) {
1176 /* We are already chasing a referral or search reference and a
1177 * bind on that connection is in progress. We must queue
1178 * referrals on that connection, so we don't get a request
1179 * going out before the bind operation completes. This happens
1180 * if two search references come in one behind the other
1181 * for the same server with different contexts.
1183 Debug( LDAP_DEBUG_TRACE,
1184 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1186 if( lc->lconn_rebind_queue == NULL ) {
1187 /* Create a referral list */
1188 lc->lconn_rebind_queue =
1189 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1191 if( lc->lconn_rebind_queue == NULL) {
1192 ld->ld_errno = LDAP_NO_MEMORY;
1197 lc->lconn_rebind_queue[0] = refarray;
1198 lc->lconn_rebind_queue[1] = NULL;
1202 /* Count how many referral arrays we already have */
1203 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1207 /* Add the new referral to the list */
1208 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1209 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1211 if( lc->lconn_rebind_queue == NULL ) {
1212 ld->ld_errno = LDAP_NO_MEMORY;
1216 lc->lconn_rebind_queue[j] = refarray;
1217 lc->lconn_rebind_queue[j+1] = NULL;
1221 /* We have queued the referral/reference, now just return */
1224 count = 1; /* Pretend we already followed referral */
1228 /* Re-encode the request with the new starting point of the search.
1229 * Note: In the future we also need to replace the filter if one
1230 * was provided with the search reference
1233 /* For references we don't want old dn if new dn empty */
1234 if ( sref && srv->lud_dn == NULL ) {
1235 srv->lud_dn = LDAP_STRDUP( "" );
1238 LDAP_NEXT_MSGID( ld, id );
1239 ber = re_encode_request( ld, origreq->lr_ber, id,
1240 sref, srv, &rinfo.ri_request );
1243 ld->ld_errno = LDAP_ENCODING_ERROR;
1248 Debug( LDAP_DEBUG_TRACE,
1249 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1250 lr->lr_msgid, refarray[i], 0);
1252 /* Send the new request to the server - may require a bind */
1253 rinfo.ri_msgid = origreq->lr_origid;
1254 rinfo.ri_url = refarray[i];
1255 rc = ldap_send_server_request( ld, ber, id,
1256 origreq, &srv, NULL, &rinfo, 0, 1 );
1258 /* Failure, try next referral in the list */
1259 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1260 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1261 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1262 ldap_free_urllist( srv );
1264 ld->ld_errno = LDAP_REFERRAL;
1266 /* Success, no need to try this referral list further */
1271 /* check if there is a queue of referrals that came in during bind */
1273 lc = find_connection( ld, srv, 1 );
1275 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1277 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
1282 if ( lc->lconn_rebind_queue != NULL ) {
1283 /* Release resources of previous list */
1284 LDAP_VFREE( refarray );
1286 ldap_free_urllist( srv );
1289 /* Pull entries off end of queue so list always null terminated */
1290 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1292 refarray = lc->lconn_rebind_queue[j - 1];
1293 lc->lconn_rebind_queue[j-1] = NULL;
1294 /* we pulled off last entry from queue, free queue */
1296 LDAP_FREE( lc->lconn_rebind_queue );
1297 lc->lconn_rebind_queue = NULL;
1299 /* restart the loop the with new referral list */
1303 break; /* referral followed, break out of for loop */
1305 } /* end for loop */
1307 LDAP_VFREE( refarray );
1308 ldap_free_urllist( srv );
1309 LDAP_FREE( *errstrp );
1313 LDAP_FREE( unfollowed );
1316 *errstrp = unfollowed;
1322 * XXX merging of errors in this routine needs to be improved
1323 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1326 ldap_chase_referrals( LDAP *ld,
1334 char *p, *ref, *unfollowed;
1335 LDAPRequest *origreq;
1341 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1342 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1343 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1344 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1346 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1349 if ( *errstrp == NULL ) {
1353 len = strlen( *errstrp );
1354 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1355 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1357 p += LDAP_REF_STR_LEN;
1362 if ( len < LDAP_REF_STR_LEN ) {
1366 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1367 Debug( LDAP_DEBUG_ANY,
1368 "more than %d referral hops (dropping)\n",
1369 ld->ld_refhoplimit, 0, 0 );
1370 /* XXX report as error in ld->ld_errno? */
1374 /* find original request */
1375 for ( origreq = lr; origreq->lr_parent != NULL;
1376 origreq = origreq->lr_parent ) {
1383 /* parse out & follow referrals */
1384 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1385 p = strchr( ref, '\n' );
1390 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1391 if ( rc != LDAP_URL_SUCCESS ) {
1392 Debug( LDAP_DEBUG_TRACE,
1393 "ignoring %s referral <%s>\n",
1394 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1395 rc = ldap_append_referral( ld, &unfollowed, ref );
1400 Debug( LDAP_DEBUG_TRACE,
1401 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1405 /* See if we've already been here */
1406 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1409 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1410 for ( lp = lr; lp; lp = lp->lr_parent ) {
1411 if ( lp->lr_conn == lc
1412 && len == lp->lr_dn.bv_len )
1414 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1421 ldap_free_urllist( srv );
1422 ld->ld_errno = LDAP_CLIENT_LOOP;
1428 LDAP_NEXT_MSGID( ld, id );
1429 ber = re_encode_request( ld, origreq->lr_ber,
1430 id, sref, srv, &rinfo.ri_request );
1432 if ( ber == NULL ) {
1436 /* copy the complete referral for rebind process */
1437 rinfo.ri_url = LDAP_STRDUP( ref );
1439 rinfo.ri_msgid = origreq->lr_origid;
1441 rc = ldap_send_server_request( ld, ber, id,
1442 lr, &srv, NULL, &rinfo, 0, 1 );
1443 LDAP_FREE( rinfo.ri_url );
1448 Debug( LDAP_DEBUG_ANY,
1449 "Unable to chase referral \"%s\" (%d: %s)\n",
1450 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1451 rc = ldap_append_referral( ld, &unfollowed, ref );
1454 ldap_free_urllist(srv);
1457 LDAP_FREE( *errstrp );
1458 *errstrp = unfollowed;
1460 return(( rc == 0 ) ? count : rc );
1465 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1469 if ( *referralsp == NULL ) {
1471 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1475 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1476 strlen( *referralsp ) + strlen( s ) + 2 );
1479 if ( *referralsp == NULL ) {
1480 ld->ld_errno = LDAP_NO_MEMORY;
1485 strcpy( *referralsp, LDAP_REF_STR );
1487 strcat( *referralsp, "\n" );
1489 strcat( *referralsp, s );
1497 re_encode_request( LDAP *ld,
1498 BerElement *origber,
1505 * XXX this routine knows way too much about how the lber library works!
1513 BerElement tmpber, *ber;
1516 Debug( LDAP_DEBUG_TRACE,
1517 "re_encode_request: new msgid %ld, new dn <%s>\n",
1519 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1524 * all LDAP requests are sequences that start with a message id.
1525 * For all except delete, this is followed by a sequence that is
1526 * tagged with the operation code. For delete, the provided DN
1527 * is not wrapped by a sequence.
1529 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1531 if ( rtag == LBER_ERROR ) {
1532 ld->ld_errno = LDAP_DECODING_ERROR;
1537 if ( tag == LDAP_REQ_BIND ) {
1538 /* bind requests have a version number before the DN & other stuff */
1539 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1541 } else if ( tag == LDAP_REQ_DELETE ) {
1542 /* delete requests don't have a DN wrapping sequence */
1543 rtag = ber_scanf( &tmpber, "m", &dn );
1545 } else if ( tag == LDAP_REQ_SEARCH ) {
1546 /* search requests need to be re-scope-ed */
1547 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1549 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1550 /* use the scope provided in reference */
1551 scope = srv->lud_scope;
1553 } else if ( sref ) {
1554 /* use scope implied by previous operation
1557 * subtree -> subtree
1558 * subordinate -> subtree
1562 case LDAP_SCOPE_BASE:
1563 case LDAP_SCOPE_ONELEVEL:
1564 scope = LDAP_SCOPE_BASE;
1566 case LDAP_SCOPE_SUBTREE:
1567 case LDAP_SCOPE_SUBORDINATE:
1568 scope = LDAP_SCOPE_SUBTREE;
1574 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1577 if( rtag == LBER_ERROR ) {
1578 ld->ld_errno = LDAP_DECODING_ERROR;
1582 /* restore character zero'd out by ber_scanf*/
1583 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1585 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1589 if ( srv->lud_dn ) {
1590 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1593 if ( tag == LDAP_REQ_BIND ) {
1594 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1595 } else if ( tag == LDAP_REQ_DELETE ) {
1596 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1597 } else if ( tag == LDAP_REQ_SEARCH ) {
1598 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1600 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1604 ld->ld_errno = LDAP_ENCODING_ERROR;
1609 if ( tag != LDAP_REQ_DELETE && (
1610 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1611 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1612 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1614 ld->ld_errno = LDAP_ENCODING_ERROR;
1620 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1621 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1623 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1625 #endif /* LDAP_DEBUG */
1627 *type = tag; /* return request type */
1632 /* protected by req_mutex */
1634 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1638 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1639 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1640 continue; /* Skip completed requests */
1642 if ( msgid == lr->lr_msgid ) {
1651 /* protected by req_mutex */
1653 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1657 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1659 if ( lr->lr_refcnt > 0 ) {
1662 } else if ( lr->lr_refcnt < 0 ) {
1664 if ( lr->lr_refcnt == 0 ) {
1672 ldap_free_request_int( ld, lrx );
1674 } else if ( freeit ) {
1675 ldap_free_request( ld, lrx );