2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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 );
132 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
133 LBER_SB_OPT_GET_FD, &sd );
136 if ( ld->ld_defconn && ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING )
137 rc = ldap_int_check_async_open( ld, sd );
140 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
142 } else if ( rc == 0 ) {
143 Debug( LDAP_DEBUG_TRACE,
144 "ldap_open_defconn: successful\n",
148 #ifdef LDAP_CONNECTIONLESS
149 if (LDAP_IS_UDP(ld)) {
150 if (msgtype == LDAP_REQ_BIND) {
151 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
152 if (ld->ld_options.ldo_cldapdn)
153 ldap_memfree(ld->ld_options.ldo_cldapdn);
154 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
156 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
157 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
160 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
163 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
164 return LDAP_PARAM_ERROR;
168 LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
169 rc = ldap_send_server_request( ld, ber, msgid, NULL,
170 NULL, NULL, NULL, 0, 0 );
171 LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
172 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
177 /* protected by conn_mutex */
179 ldap_int_flush_request(
183 LDAPConn *lc = lr->lr_conn;
185 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
186 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
187 if ( sock_errno() == EAGAIN ) {
188 /* need to continue write later */
189 lr->lr_status = LDAP_REQST_WRITING;
190 ldap_mark_select_write( ld, lc->lconn_sb );
191 ld->ld_errno = LDAP_BUSY;
194 ld->ld_errno = LDAP_SERVER_DOWN;
195 ldap_free_request( ld, lr );
196 ldap_free_connection( ld, lc, 0, 0 );
200 if ( lr->lr_parent == NULL ) {
201 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
202 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
204 lr->lr_status = LDAP_REQST_INPROGRESS;
206 /* sent -- waiting for a response */
207 ldap_mark_select_read( ld, lc->lconn_sb );
208 ldap_clear_select_write( ld, lc->lconn_sb );
214 * protected by req_mutex
215 * if m_noconn then protect using conn_lock
216 * else already protected with conn_lock
217 * if m_res then also protected by res_mutex
221 ldap_send_server_request(
225 LDAPRequest *parentreq,
226 LDAPURLDesc **srvlist,
235 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
236 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
239 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
241 LDAP_CONN_LOCK_IF(m_noconn);
243 if ( srvlist == NULL ) {
246 lc = find_connection( ld, *srvlist, 1 );
248 if ( (bind != NULL) && (parentreq != NULL) ) {
249 /* Remember the bind in the parent */
251 ++parentreq->lr_outrefcnt;
253 lc = ldap_new_connection( ld, srvlist, 0,
259 /* async connect... */
260 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
261 ber_socket_t sd = AC_SOCKET_ERROR;
262 struct timeval tv = { 0 };
264 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
267 switch ( ldap_int_poll( ld, sd, &tv, 1 ) ) {
270 lc->lconn_status = LDAP_CONNST_CONNECTED;
274 /* async only occurs if a network timeout is set */
276 /* honor network timeout */
277 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
278 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
280 /* caller will have to call again */
281 ld->ld_errno = LDAP_X_CONNECTING;
283 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
292 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
293 if ( ld->ld_errno == LDAP_SUCCESS ) {
294 ld->ld_errno = LDAP_SERVER_DOWN;
299 /* Forget about the bind */
300 --parentreq->lr_outrefcnt;
302 LDAP_CONN_UNLOCK_IF(m_noconn);
306 use_connection( ld, lc );
308 #ifdef LDAP_CONNECTIONLESS
309 if ( LDAP_IS_UDP( ld )) {
310 BerElement tmpber = *ber;
311 ber_rewind( &tmpber );
312 LDAP_MUTEX_LOCK( &ld->ld_options.ldo_mutex );
313 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
314 sizeof( struct sockaddr_storage ), 0 );
315 LDAP_MUTEX_UNLOCK( &ld->ld_options.ldo_mutex );
317 ld->ld_errno = LDAP_ENCODING_ERROR;
319 LDAP_CONN_UNLOCK_IF(m_noconn);
325 /* If we still have an incomplete write, try to finish it before
326 * dealing with the new request. If we don't finish here, return
327 * LDAP_BUSY and let the caller retry later. We only allow a single
328 * request to be in WRITING state.
331 if ( ld->ld_requests &&
332 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
333 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
339 LDAP_CONN_UNLOCK_IF(m_noconn);
343 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
345 ld->ld_errno = LDAP_NO_MEMORY;
346 ldap_free_connection( ld, lc, 0, 0 );
349 /* Forget about the bind */
350 --parentreq->lr_outrefcnt;
352 LDAP_CONN_UNLOCK_IF(m_noconn);
355 lr->lr_msgid = msgid;
356 lr->lr_status = LDAP_REQST_INPROGRESS;
357 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
360 if ( parentreq != NULL ) { /* sub-request */
362 /* Increment if we didn't do it before the bind */
363 ++parentreq->lr_outrefcnt;
365 lr->lr_origid = parentreq->lr_origid;
366 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
367 lr->lr_parent = parentreq;
368 lr->lr_refnext = parentreq->lr_child;
369 parentreq->lr_child = lr;
370 } else { /* original request */
371 lr->lr_origid = lr->lr_msgid;
374 /* Extract requestDN for future reference */
375 #ifdef LDAP_CONNECTIONLESS
376 if ( !LDAP_IS_UDP(ld) )
379 BerElement tmpber = *ber;
383 ber_reset( &tmpber, 1 );
384 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
387 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
389 case LDAP_REQ_DELETE:
392 rtag = ber_scanf( &tmpber, "{" /*}*/ );
393 case LDAP_REQ_ABANDON:
396 if ( tag != LDAP_REQ_ABANDON ) {
397 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
398 lr->lr_dn.bv_val = tmpber.ber_ptr;
403 lr->lr_next = ld->ld_requests;
404 if ( lr->lr_next != NULL ) {
405 lr->lr_next->lr_prev = lr;
407 ld->ld_requests = lr;
409 ld->ld_errno = LDAP_SUCCESS;
410 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
414 LDAP_CONN_UNLOCK_IF(m_noconn);
418 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
420 find_tls_ext( LDAPURLDesc *srv )
425 if ( !srv->lud_exts )
428 for (i=0; srv->lud_exts[i]; i++) {
430 ext = srv->lud_exts[i];
431 if ( ext[0] == '!') {
435 if ( !strcasecmp( ext, "StartTLS" ) ||
436 !strcasecmp( ext, "X-StartTLS" ) ||
437 !strcmp( ext, LDAP_EXOP_START_TLS )) {
445 * always protected by conn_mutex
446 * optionally protected by req_mutex and res_mutex
449 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
450 int connect, LDAPreqinfo *bind, int m_req, int m_res )
455 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
456 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
457 use_ldsb, connect, (bind != NULL) );
459 * make a new LDAP server connection
460 * XXX open connection synchronously for now
462 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
464 ld->ld_errno = LDAP_NO_MEMORY;
469 assert( ld->ld_sb != NULL );
470 lc->lconn_sb = ld->ld_sb;
473 lc->lconn_sb = ber_sockbuf_alloc();
474 if ( lc->lconn_sb == NULL ) {
475 LDAP_FREE( (char *)lc );
476 ld->ld_errno = LDAP_NO_MEMORY;
482 LDAPURLDesc **srvp, *srv = NULL;
484 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
486 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
489 rc = ldap_int_open_connection( ld, lc, *srvp, async );
493 /* If we fully connected, async is moot */
497 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
498 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
507 ber_sockbuf_free( lc->lconn_sb );
509 LDAP_FREE( (char *)lc );
510 ld->ld_errno = LDAP_SERVER_DOWN;
514 lc->lconn_server = ldap_url_dup( srv );
515 if ( !lc->lconn_server ) {
517 ber_sockbuf_free( lc->lconn_sb );
518 LDAP_FREE( (char *)lc );
519 ld->ld_errno = LDAP_NO_MEMORY;
524 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
525 lc->lconn_next = ld->ld_conns;
530 if ( lc->lconn_server->lud_exts ) {
531 int rc, ext = find_tls_ext( lc->lconn_server );
533 LDAPConn *savedefconn;
535 savedefconn = ld->ld_defconn;
536 ++lc->lconn_refcnt; /* avoid premature free */
539 LDAP_REQ_UNLOCK_IF(m_req);
540 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
541 LDAP_RES_UNLOCK_IF(m_res);
542 rc = ldap_start_tls_s( ld, NULL, NULL );
543 LDAP_RES_LOCK_IF(m_res);
544 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
545 LDAP_REQ_LOCK_IF(m_req);
546 ld->ld_defconn = savedefconn;
549 if ( rc != LDAP_SUCCESS && ext == 2 ) {
550 ldap_free_connection( ld, lc, 1, 0 );
558 if ( bind != NULL ) {
560 LDAPConn *savedefconn;
562 /* Set flag to prevent additional referrals
563 * from being processed on this
564 * connection until the bind has completed
566 lc->lconn_rebind_inprogress = 1;
567 /* V3 rebind function */
568 if ( ld->ld_rebind_proc != NULL) {
569 LDAPURLDesc *srvfunc;
571 srvfunc = ldap_url_dup( *srvlist );
572 if ( srvfunc == NULL ) {
573 ld->ld_errno = LDAP_NO_MEMORY;
576 savedefconn = ld->ld_defconn;
577 ++lc->lconn_refcnt; /* avoid premature free */
580 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
581 LDAP_REQ_UNLOCK_IF(m_req);
582 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
583 LDAP_RES_UNLOCK_IF(m_res);
584 err = (*ld->ld_rebind_proc)( ld,
585 bind->ri_url, bind->ri_request, bind->ri_msgid,
586 ld->ld_rebind_params );
587 LDAP_RES_LOCK_IF(m_res);
588 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
589 LDAP_REQ_LOCK_IF(m_req);
591 ld->ld_defconn = savedefconn;
596 ldap_free_connection( ld, lc, 1, 0 );
599 ldap_free_urldesc( srvfunc );
604 struct berval passwd = BER_BVNULL;
606 savedefconn = ld->ld_defconn;
607 ++lc->lconn_refcnt; /* avoid premature free */
610 Debug( LDAP_DEBUG_TRACE,
611 "anonymous rebind via ldap_sasl_bind(\"\")\n",
614 LDAP_REQ_UNLOCK_IF(m_req);
615 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
616 LDAP_RES_UNLOCK_IF(m_res);
617 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
618 NULL, NULL, &msgid );
619 if ( rc != LDAP_SUCCESS ) {
623 for ( err = 1; err > 0; ) {
624 struct timeval tv = { 0, 100000 };
625 LDAPMessage *res = NULL;
627 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
633 #ifdef LDAP_R_COMPILE
634 ldap_pvt_thread_yield();
639 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
640 if ( rc != LDAP_SUCCESS ) {
643 } else if ( err != LDAP_SUCCESS ) {
646 /* else err == LDAP_SUCCESS == 0 */
650 Debug( LDAP_DEBUG_TRACE,
651 "ldap_new_connection %p: "
652 "unexpected response %d "
653 "from BIND request id=%d\n",
654 (void *) ld, ldap_msgtype( res ), msgid );
660 LDAP_RES_LOCK_IF(m_res);
661 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
662 LDAP_REQ_LOCK_IF(m_req);
663 ld->ld_defconn = savedefconn;
667 ldap_free_connection( ld, lc, 1, 0 );
672 lc->lconn_rebind_inprogress = 0;
678 /* protected by ld_conn_mutex */
680 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
682 * return an existing connection (if any) to the server srv
683 * if "any" is non-zero, check for any server in the "srv" chain
687 LDAPURLDesc *lcu, *lsu;
688 int lcu_port, lsu_port;
691 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
692 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
693 lcu = lc->lconn_server;
694 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
697 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
698 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
701 if ( lsu_port == lcu_port
702 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
703 && lcu->lud_host != NULL && lsu->lud_host != NULL
704 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
720 /* protected by ld_conn_mutex */
722 use_connection( LDAP *ld, LDAPConn *lc )
724 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
726 lc->lconn_lastused = time( NULL );
730 /* protected by ld_conn_mutex */
732 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
734 LDAPConn *tmplc, *prevlc;
736 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
737 Debug( LDAP_DEBUG_TRACE,
738 "ldap_free_connection %d %d\n",
741 if ( force || --lc->lconn_refcnt <= 0 ) {
742 /* remove from connections list first */
744 for ( prevlc = NULL, tmplc = ld->ld_conns;
746 tmplc = tmplc->lconn_next )
749 if ( prevlc == NULL ) {
750 ld->ld_conns = tmplc->lconn_next;
752 prevlc->lconn_next = tmplc->lconn_next;
754 if ( ld->ld_defconn == lc ) {
755 ld->ld_defconn = NULL;
762 /* process connection callbacks */
764 struct ldapoptions *lo;
768 lo = &ld->ld_options;
769 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
770 if ( lo->ldo_conn_cbs ) {
771 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
773 cb->lc_del( ld, lc->lconn_sb, cb );
776 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
777 lo = LDAP_INT_GLOBAL_OPT();
778 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
779 if ( lo->ldo_conn_cbs ) {
780 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
782 cb->lc_del( ld, lc->lconn_sb, cb );
785 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
788 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
789 ldap_mark_select_clear( ld, lc->lconn_sb );
791 ldap_send_unbind( ld, lc->lconn_sb,
796 if ( lc->lconn_ber != NULL ) {
797 ber_free( lc->lconn_ber, 1 );
800 ldap_int_sasl_close( ld, lc );
802 ldap_int_gssapi_close( ld, lc );
805 ldap_free_urllist( lc->lconn_server );
807 /* FIXME: is this at all possible?
808 * ldap_ld_free() in unbind.c calls ldap_free_connection()
809 * with force == 1 __after__ explicitly calling
810 * ldap_free_request() on all requests */
814 for ( lr = ld->ld_requests; lr; ) {
815 LDAPRequest *lr_next = lr->lr_next;
817 if ( lr->lr_conn == lc ) {
818 ldap_free_request_int( ld, lr );
825 if ( lc->lconn_sb != ld->ld_sb ) {
826 ber_sockbuf_free( lc->lconn_sb );
828 ber_int_sb_close( lc->lconn_sb );
831 if ( lc->lconn_rebind_queue != NULL) {
833 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
834 LDAP_VFREE( lc->lconn_rebind_queue[i] );
836 LDAP_FREE( lc->lconn_rebind_queue );
841 Debug( LDAP_DEBUG_TRACE,
842 "ldap_free_connection: actually freed\n",
846 lc->lconn_lastused = time( NULL );
847 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
848 lc->lconn_refcnt, 0, 0 );
853 /* Protects self with ld_conn_mutex */
856 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
861 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
862 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
863 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
864 if ( lc->lconn_server != NULL ) {
865 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
866 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
867 : lc->lconn_server->lud_host,
868 lc->lconn_server->lud_port, ( lc->lconn_sb ==
869 ld->ld_sb ) ? " (default)" : "" );
871 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
872 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
874 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
875 ? "Connecting" : "Connected", 0 );
876 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
877 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
878 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
879 if ( lc->lconn_rebind_inprogress ) {
880 if ( lc->lconn_rebind_queue != NULL) {
883 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
885 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
886 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
887 i, j, lc->lconn_rebind_queue[i][j] );
891 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
894 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
899 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
903 /* protected by req_mutex and res_mutex */
905 ldap_dump_requests_and_responses( LDAP *ld )
911 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
913 lr = ld->ld_requests;
915 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
917 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
918 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
919 lr->lr_msgid, lr->lr_origid,
920 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
921 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
922 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
923 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
924 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
926 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
927 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
929 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
930 (void *)ld, i, ld->ld_nabandoned );
931 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
932 if ( ( lm = ld->ld_responses ) == NULL ) {
933 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
935 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
936 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
937 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
938 if ( lm->lm_chain != NULL ) {
939 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
940 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
941 Debug( LDAP_DEBUG_TRACE,
942 " * msgid %d, type %lu\n",
944 (unsigned long)l->lm_msgtype, 0 );
948 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
950 #endif /* LDAP_DEBUG */
952 /* protected by req_mutex */
954 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
956 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
957 /* if lr_refcnt > 0, the request has been looked up
958 * by ldap_find_request_by_msgid(); if in the meanwhile
959 * the request is free()'d by someone else, just decrease
960 * the reference count and extract it from the request
961 * list; later on, it will be freed. */
962 if ( lr->lr_prev == NULL ) {
963 if ( lr->lr_refcnt == 0 ) {
964 /* free'ing the first request? */
965 assert( ld->ld_requests == lr );
968 if ( ld->ld_requests == lr ) {
969 ld->ld_requests = lr->lr_next;
973 lr->lr_prev->lr_next = lr->lr_next;
976 if ( lr->lr_next != NULL ) {
977 lr->lr_next->lr_prev = lr->lr_prev;
980 if ( lr->lr_refcnt > 0 ) {
981 lr->lr_refcnt = -lr->lr_refcnt;
989 if ( lr->lr_ber != NULL ) {
990 ber_free( lr->lr_ber, 1 );
994 if ( lr->lr_res_error != NULL ) {
995 LDAP_FREE( lr->lr_res_error );
996 lr->lr_res_error = NULL;
999 if ( lr->lr_res_matched != NULL ) {
1000 LDAP_FREE( lr->lr_res_matched );
1001 lr->lr_res_matched = NULL;
1007 /* protected by req_mutex */
1009 ldap_free_request( LDAP *ld, LDAPRequest *lr )
1011 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1012 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
1013 lr->lr_origid, lr->lr_msgid, 0 );
1015 /* free all referrals (child requests) */
1016 while ( lr->lr_child ) {
1017 ldap_free_request( ld, lr->lr_child );
1020 if ( lr->lr_parent != NULL ) {
1023 --lr->lr_parent->lr_outrefcnt;
1024 for ( lrp = &lr->lr_parent->lr_child;
1026 lrp = &(*lrp)->lr_refnext );
1029 *lrp = lr->lr_refnext;
1032 ldap_free_request_int( ld, lr );
1036 * call first time with *cntp = -1
1037 * when returns *cntp == -1, no referrals are left
1039 * NOTE: may replace *refsp, or shuffle the contents
1040 * of the original array.
1042 static int ldap_int_nextref(
1048 assert( refsp != NULL );
1049 assert( *refsp != NULL );
1050 assert( cntp != NULL );
1059 if ( (*refsp)[ *cntp ] == NULL ) {
1067 * Chase v3 referrals
1070 * (IN) ld = LDAP connection handle
1071 * (IN) lr = LDAP Request structure
1072 * (IN) refs = array of pointers to referral strings that we will chase
1073 * The array will be free'd by this function when no longer needed
1074 * (IN) sref != 0 if following search reference
1075 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1076 * (OUT) hadrefp = 1 if sucessfully followed referral
1078 * Return value - number of referrals followed
1080 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1083 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1086 int unfollowedcnt = 0;
1087 LDAPRequest *origreq;
1088 LDAPURLDesc *srv = NULL;
1090 char **refarray = NULL;
1092 int rc, count, i, j, id;
1094 LDAP_NEXTREF_PROC *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
1096 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1097 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1098 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1099 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1101 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1107 /* If no referrals in array, return */
1108 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1113 /* Check for hop limit exceeded */
1114 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1115 Debug( LDAP_DEBUG_ANY,
1116 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1117 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1122 /* find original request */
1124 origreq->lr_parent != NULL;
1125 origreq = origreq->lr_parent )
1133 /* parse out & follow referrals */
1134 /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
1136 for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1138 nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1141 /* Parse the referral URL */
1142 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1143 if ( rc != LDAP_URL_SUCCESS ) {
1144 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1145 * which do not map on API errors */
1146 ld->ld_errno = LDAP_PARAM_ERROR;
1151 if( srv->lud_crit_exts ) {
1154 /* If StartTLS is the only critical ext, OK. */
1155 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1159 /* we do not support any other extensions */
1160 ld->ld_errno = LDAP_NOT_SUPPORTED;
1166 /* check connection for re-bind in progress */
1167 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1168 /* See if we've already requested this DN with this conn */
1171 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1172 for ( lp = origreq; lp; ) {
1173 if ( lp->lr_conn == lc
1174 && len == lp->lr_dn.bv_len
1176 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1181 if ( lp == origreq ) {
1184 lp = lp->lr_refnext;
1188 ldap_free_urllist( srv );
1190 ld->ld_errno = LDAP_CLIENT_LOOP;
1195 if ( lc->lconn_rebind_inprogress ) {
1196 /* We are already chasing a referral or search reference and a
1197 * bind on that connection is in progress. We must queue
1198 * referrals on that connection, so we don't get a request
1199 * going out before the bind operation completes. This happens
1200 * if two search references come in one behind the other
1201 * for the same server with different contexts.
1203 Debug( LDAP_DEBUG_TRACE,
1204 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1206 if( lc->lconn_rebind_queue == NULL ) {
1207 /* Create a referral list */
1208 lc->lconn_rebind_queue =
1209 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1211 if( lc->lconn_rebind_queue == NULL) {
1212 ld->ld_errno = LDAP_NO_MEMORY;
1217 lc->lconn_rebind_queue[0] = refarray;
1218 lc->lconn_rebind_queue[1] = NULL;
1222 /* Count how many referral arrays we already have */
1223 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1227 /* Add the new referral to the list */
1228 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1229 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1231 if( lc->lconn_rebind_queue == NULL ) {
1232 ld->ld_errno = LDAP_NO_MEMORY;
1236 lc->lconn_rebind_queue[j] = refarray;
1237 lc->lconn_rebind_queue[j+1] = NULL;
1241 /* We have queued the referral/reference, now just return */
1244 count = 1; /* Pretend we already followed referral */
1248 /* Re-encode the request with the new starting point of the search.
1249 * Note: In the future we also need to replace the filter if one
1250 * was provided with the search reference
1253 /* For references we don't want old dn if new dn empty */
1254 if ( sref && srv->lud_dn == NULL ) {
1255 srv->lud_dn = LDAP_STRDUP( "" );
1258 LDAP_NEXT_MSGID( ld, id );
1259 ber = re_encode_request( ld, origreq->lr_ber, id,
1260 sref, srv, &rinfo.ri_request );
1263 ld->ld_errno = LDAP_ENCODING_ERROR;
1268 Debug( LDAP_DEBUG_TRACE,
1269 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1270 lr->lr_msgid, refarray[i], 0);
1272 /* Send the new request to the server - may require a bind */
1273 rinfo.ri_msgid = origreq->lr_origid;
1274 rinfo.ri_url = refarray[i];
1275 rc = ldap_send_server_request( ld, ber, id,
1276 origreq, &srv, NULL, &rinfo, 0, 1 );
1278 /* Failure, try next referral in the list */
1279 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1280 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1281 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1282 ldap_free_urllist( srv );
1284 ld->ld_errno = LDAP_REFERRAL;
1286 /* Success, no need to try this referral list further */
1291 /* check if there is a queue of referrals that came in during bind */
1293 lc = find_connection( ld, srv, 1 );
1295 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1297 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
1302 if ( lc->lconn_rebind_queue != NULL ) {
1303 /* Release resources of previous list */
1304 LDAP_VFREE( refarray );
1306 ldap_free_urllist( srv );
1309 /* Pull entries off end of queue so list always null terminated */
1310 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1312 refarray = lc->lconn_rebind_queue[j - 1];
1313 lc->lconn_rebind_queue[j-1] = NULL;
1314 /* we pulled off last entry from queue, free queue */
1316 LDAP_FREE( lc->lconn_rebind_queue );
1317 lc->lconn_rebind_queue = NULL;
1319 /* restart the loop the with new referral list */
1323 break; /* referral followed, break out of for loop */
1325 } /* end for loop */
1327 LDAP_VFREE( refarray );
1328 ldap_free_urllist( srv );
1329 LDAP_FREE( *errstrp );
1333 LDAP_FREE( unfollowed );
1336 *errstrp = unfollowed;
1342 * XXX merging of errors in this routine needs to be improved
1343 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1346 ldap_chase_referrals( LDAP *ld,
1354 char *p, *ref, *unfollowed;
1355 LDAPRequest *origreq;
1361 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1362 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1363 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1364 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1366 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1369 if ( *errstrp == NULL ) {
1373 len = strlen( *errstrp );
1374 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1375 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1377 p += LDAP_REF_STR_LEN;
1382 if ( len < LDAP_REF_STR_LEN ) {
1386 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1387 Debug( LDAP_DEBUG_ANY,
1388 "more than %d referral hops (dropping)\n",
1389 ld->ld_refhoplimit, 0, 0 );
1390 /* XXX report as error in ld->ld_errno? */
1394 /* find original request */
1395 for ( origreq = lr; origreq->lr_parent != NULL;
1396 origreq = origreq->lr_parent ) {
1403 /* parse out & follow referrals */
1404 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1405 p = strchr( ref, '\n' );
1410 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1411 if ( rc != LDAP_URL_SUCCESS ) {
1412 Debug( LDAP_DEBUG_TRACE,
1413 "ignoring %s referral <%s>\n",
1414 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1415 rc = ldap_append_referral( ld, &unfollowed, ref );
1420 Debug( LDAP_DEBUG_TRACE,
1421 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1425 /* See if we've already been here */
1426 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1429 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1430 for ( lp = lr; lp; lp = lp->lr_parent ) {
1431 if ( lp->lr_conn == lc
1432 && len == lp->lr_dn.bv_len )
1434 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1441 ldap_free_urllist( srv );
1442 ld->ld_errno = LDAP_CLIENT_LOOP;
1448 LDAP_NEXT_MSGID( ld, id );
1449 ber = re_encode_request( ld, origreq->lr_ber,
1450 id, sref, srv, &rinfo.ri_request );
1452 if ( ber == NULL ) {
1453 ldap_free_urllist( srv );
1457 /* copy the complete referral for rebind process */
1458 rinfo.ri_url = LDAP_STRDUP( ref );
1460 rinfo.ri_msgid = origreq->lr_origid;
1462 rc = ldap_send_server_request( ld, ber, id,
1463 lr, &srv, NULL, &rinfo, 0, 1 );
1464 LDAP_FREE( rinfo.ri_url );
1469 Debug( LDAP_DEBUG_ANY,
1470 "Unable to chase referral \"%s\" (%d: %s)\n",
1471 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1472 rc = ldap_append_referral( ld, &unfollowed, ref );
1475 ldap_free_urllist(srv);
1478 LDAP_FREE( *errstrp );
1479 *errstrp = unfollowed;
1481 return(( rc == 0 ) ? count : rc );
1486 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1490 if ( *referralsp == NULL ) {
1492 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1496 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1497 strlen( *referralsp ) + strlen( s ) + 2 );
1500 if ( *referralsp == NULL ) {
1501 ld->ld_errno = LDAP_NO_MEMORY;
1506 strcpy( *referralsp, LDAP_REF_STR );
1508 strcat( *referralsp, "\n" );
1510 strcat( *referralsp, s );
1518 re_encode_request( LDAP *ld,
1519 BerElement *origber,
1526 * XXX this routine knows way too much about how the lber library works!
1534 BerElement tmpber, *ber;
1537 Debug( LDAP_DEBUG_TRACE,
1538 "re_encode_request: new msgid %ld, new dn <%s>\n",
1540 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1545 * all LDAP requests are sequences that start with a message id.
1546 * For all except delete, this is followed by a sequence that is
1547 * tagged with the operation code. For delete, the provided DN
1548 * is not wrapped by a sequence.
1550 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1552 if ( rtag == LBER_ERROR ) {
1553 ld->ld_errno = LDAP_DECODING_ERROR;
1558 if ( tag == LDAP_REQ_BIND ) {
1559 /* bind requests have a version number before the DN & other stuff */
1560 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1562 } else if ( tag == LDAP_REQ_DELETE ) {
1563 /* delete requests don't have a DN wrapping sequence */
1564 rtag = ber_scanf( &tmpber, "m", &dn );
1566 } else if ( tag == LDAP_REQ_SEARCH ) {
1567 /* search requests need to be re-scope-ed */
1568 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1570 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1571 /* use the scope provided in reference */
1572 scope = srv->lud_scope;
1574 } else if ( sref ) {
1575 /* use scope implied by previous operation
1578 * subtree -> subtree
1579 * subordinate -> subtree
1583 case LDAP_SCOPE_BASE:
1584 case LDAP_SCOPE_ONELEVEL:
1585 scope = LDAP_SCOPE_BASE;
1587 case LDAP_SCOPE_SUBTREE:
1588 case LDAP_SCOPE_SUBORDINATE:
1589 scope = LDAP_SCOPE_SUBTREE;
1595 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1598 if( rtag == LBER_ERROR ) {
1599 ld->ld_errno = LDAP_DECODING_ERROR;
1603 /* restore character zero'd out by ber_scanf*/
1604 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1606 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1610 if ( srv->lud_dn ) {
1611 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1614 if ( tag == LDAP_REQ_BIND ) {
1615 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1616 } else if ( tag == LDAP_REQ_DELETE ) {
1617 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1618 } else if ( tag == LDAP_REQ_SEARCH ) {
1619 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1621 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1625 ld->ld_errno = LDAP_ENCODING_ERROR;
1630 if ( tag != LDAP_REQ_DELETE && (
1631 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1632 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1633 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1635 ld->ld_errno = LDAP_ENCODING_ERROR;
1641 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1642 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1644 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1646 #endif /* LDAP_DEBUG */
1648 *type = tag; /* return request type */
1653 /* protected by req_mutex */
1655 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1659 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1660 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1661 continue; /* Skip completed requests */
1663 if ( msgid == lr->lr_msgid ) {
1672 /* protected by req_mutex */
1674 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1678 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1680 if ( lr->lr_refcnt > 0 ) {
1683 } else if ( lr->lr_refcnt < 0 ) {
1685 if ( lr->lr_refcnt == 0 ) {
1693 ldap_free_request_int( ld, lrx );
1695 } else if ( freeit ) {
1696 ldap_free_request( ld, lrx );