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;
318 LDAP_CONN_UNLOCK_IF(m_noconn);
324 /* If we still have an incomplete write, try to finish it before
325 * dealing with the new request. If we don't finish here, return
326 * LDAP_BUSY and let the caller retry later. We only allow a single
327 * request to be in WRITING state.
330 if ( ld->ld_requests &&
331 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
332 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
337 LDAP_CONN_UNLOCK_IF(m_noconn);
341 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
343 ld->ld_errno = LDAP_NO_MEMORY;
344 ldap_free_connection( ld, lc, 0, 0 );
347 /* Forget about the bind */
348 --parentreq->lr_outrefcnt;
350 LDAP_CONN_UNLOCK_IF(m_noconn);
353 lr->lr_msgid = msgid;
354 lr->lr_status = LDAP_REQST_INPROGRESS;
355 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
358 if ( parentreq != NULL ) { /* sub-request */
360 /* Increment if we didn't do it before the bind */
361 ++parentreq->lr_outrefcnt;
363 lr->lr_origid = parentreq->lr_origid;
364 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
365 lr->lr_parent = parentreq;
366 lr->lr_refnext = parentreq->lr_child;
367 parentreq->lr_child = lr;
368 } else { /* original request */
369 lr->lr_origid = lr->lr_msgid;
372 /* Extract requestDN for future reference */
373 #ifdef LDAP_CONNECTIONLESS
374 if ( !LDAP_IS_UDP(ld) )
377 BerElement tmpber = *ber;
381 ber_reset( &tmpber, 1 );
382 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
385 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
387 case LDAP_REQ_DELETE:
390 rtag = ber_scanf( &tmpber, "{" /*}*/ );
391 case LDAP_REQ_ABANDON:
394 if ( tag != LDAP_REQ_ABANDON ) {
395 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
396 lr->lr_dn.bv_val = tmpber.ber_ptr;
401 lr->lr_next = ld->ld_requests;
402 if ( lr->lr_next != NULL ) {
403 lr->lr_next->lr_prev = lr;
405 ld->ld_requests = lr;
407 ld->ld_errno = LDAP_SUCCESS;
408 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
412 LDAP_CONN_UNLOCK_IF(m_noconn);
416 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
418 find_tls_ext( LDAPURLDesc *srv )
423 if ( !srv->lud_exts )
426 for (i=0; srv->lud_exts[i]; i++) {
428 ext = srv->lud_exts[i];
429 if ( ext[0] == '!') {
433 if ( !strcasecmp( ext, "StartTLS" ) ||
434 !strcasecmp( ext, "X-StartTLS" ) ||
435 !strcmp( ext, LDAP_EXOP_START_TLS )) {
443 * always protected by conn_mutex
444 * optionally protected by req_mutex and res_mutex
447 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
448 int connect, LDAPreqinfo *bind, int m_req, int m_res )
453 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
454 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
455 use_ldsb, connect, (bind != NULL) );
457 * make a new LDAP server connection
458 * XXX open connection synchronously for now
460 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
462 ld->ld_errno = LDAP_NO_MEMORY;
467 assert( ld->ld_sb != NULL );
468 lc->lconn_sb = ld->ld_sb;
471 lc->lconn_sb = ber_sockbuf_alloc();
472 if ( lc->lconn_sb == NULL ) {
473 LDAP_FREE( (char *)lc );
474 ld->ld_errno = LDAP_NO_MEMORY;
480 LDAPURLDesc **srvp, *srv = NULL;
482 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
484 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
487 rc = ldap_int_open_connection( ld, lc, *srvp, async );
491 /* If we fully connected, async is moot */
495 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
496 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
505 ber_sockbuf_free( lc->lconn_sb );
507 LDAP_FREE( (char *)lc );
508 ld->ld_errno = LDAP_SERVER_DOWN;
512 lc->lconn_server = ldap_url_dup( srv );
513 if ( !lc->lconn_server ) {
515 ber_sockbuf_free( lc->lconn_sb );
516 LDAP_FREE( (char *)lc );
517 ld->ld_errno = LDAP_NO_MEMORY;
522 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
523 lc->lconn_next = ld->ld_conns;
528 if ( lc->lconn_server->lud_exts ) {
529 int rc, ext = find_tls_ext( lc->lconn_server );
531 LDAPConn *savedefconn;
533 savedefconn = ld->ld_defconn;
534 ++lc->lconn_refcnt; /* avoid premature free */
537 LDAP_REQ_UNLOCK_IF(m_req);
538 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
539 LDAP_RES_UNLOCK_IF(m_res);
540 rc = ldap_start_tls_s( ld, NULL, NULL );
541 LDAP_RES_LOCK_IF(m_res);
542 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
543 LDAP_REQ_LOCK_IF(m_req);
544 ld->ld_defconn = savedefconn;
547 if ( rc != LDAP_SUCCESS && ext == 2 ) {
548 ldap_free_connection( ld, lc, 1, 0 );
556 if ( bind != NULL ) {
558 LDAPConn *savedefconn;
560 /* Set flag to prevent additional referrals
561 * from being processed on this
562 * connection until the bind has completed
564 lc->lconn_rebind_inprogress = 1;
565 /* V3 rebind function */
566 if ( ld->ld_rebind_proc != NULL) {
567 LDAPURLDesc *srvfunc;
569 srvfunc = ldap_url_dup( *srvlist );
570 if ( srvfunc == NULL ) {
571 ld->ld_errno = LDAP_NO_MEMORY;
574 savedefconn = ld->ld_defconn;
575 ++lc->lconn_refcnt; /* avoid premature free */
578 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
579 LDAP_REQ_UNLOCK_IF(m_req);
580 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
581 LDAP_RES_UNLOCK_IF(m_res);
582 err = (*ld->ld_rebind_proc)( ld,
583 bind->ri_url, bind->ri_request, bind->ri_msgid,
584 ld->ld_rebind_params );
585 LDAP_RES_LOCK_IF(m_res);
586 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
587 LDAP_REQ_LOCK_IF(m_req);
589 ld->ld_defconn = savedefconn;
594 ldap_free_connection( ld, lc, 1, 0 );
597 ldap_free_urldesc( srvfunc );
602 struct berval passwd = BER_BVNULL;
604 savedefconn = ld->ld_defconn;
605 ++lc->lconn_refcnt; /* avoid premature free */
608 Debug( LDAP_DEBUG_TRACE,
609 "anonymous rebind via ldap_sasl_bind(\"\")\n",
612 LDAP_REQ_UNLOCK_IF(m_req);
613 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
614 LDAP_RES_UNLOCK_IF(m_res);
615 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
616 NULL, NULL, &msgid );
617 if ( rc != LDAP_SUCCESS ) {
621 for ( err = 1; err > 0; ) {
622 struct timeval tv = { 0, 100000 };
623 LDAPMessage *res = NULL;
625 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
631 #ifdef LDAP_R_COMPILE
632 ldap_pvt_thread_yield();
637 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
638 if ( rc != LDAP_SUCCESS ) {
641 } else if ( err != LDAP_SUCCESS ) {
644 /* else err == LDAP_SUCCESS == 0 */
648 Debug( LDAP_DEBUG_TRACE,
649 "ldap_new_connection %p: "
650 "unexpected response %d "
651 "from BIND request id=%d\n",
652 (void *) ld, ldap_msgtype( res ), msgid );
658 LDAP_RES_LOCK_IF(m_res);
659 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
660 LDAP_REQ_LOCK_IF(m_req);
661 ld->ld_defconn = savedefconn;
665 ldap_free_connection( ld, lc, 1, 0 );
670 lc->lconn_rebind_inprogress = 0;
676 /* protected by ld_conn_mutex */
678 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
680 * return an existing connection (if any) to the server srv
681 * if "any" is non-zero, check for any server in the "srv" chain
685 LDAPURLDesc *lcu, *lsu;
686 int lcu_port, lsu_port;
689 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
690 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
691 lcu = lc->lconn_server;
692 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
695 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
696 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
699 if ( lsu_port == lcu_port
700 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
701 && lcu->lud_host != NULL && lsu->lud_host != NULL
702 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
718 /* protected by ld_conn_mutex */
720 use_connection( LDAP *ld, LDAPConn *lc )
722 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
724 lc->lconn_lastused = time( NULL );
728 /* protected by ld_conn_mutex */
730 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
732 LDAPConn *tmplc, *prevlc;
734 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
735 Debug( LDAP_DEBUG_TRACE,
736 "ldap_free_connection %d %d\n",
739 if ( force || --lc->lconn_refcnt <= 0 ) {
740 /* remove from connections list first */
742 for ( prevlc = NULL, tmplc = ld->ld_conns;
744 tmplc = tmplc->lconn_next )
747 if ( prevlc == NULL ) {
748 ld->ld_conns = tmplc->lconn_next;
750 prevlc->lconn_next = tmplc->lconn_next;
752 if ( ld->ld_defconn == lc ) {
753 ld->ld_defconn = NULL;
760 /* process connection callbacks */
762 struct ldapoptions *lo;
766 lo = &ld->ld_options;
767 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
768 if ( lo->ldo_conn_cbs ) {
769 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
771 cb->lc_del( ld, lc->lconn_sb, cb );
774 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
775 lo = LDAP_INT_GLOBAL_OPT();
776 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
777 if ( lo->ldo_conn_cbs ) {
778 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
780 cb->lc_del( ld, lc->lconn_sb, cb );
783 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
786 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
787 ldap_mark_select_clear( ld, lc->lconn_sb );
789 ldap_send_unbind( ld, lc->lconn_sb,
794 if ( lc->lconn_ber != NULL ) {
795 ber_free( lc->lconn_ber, 1 );
798 ldap_int_sasl_close( ld, lc );
800 ldap_int_gssapi_close( ld, lc );
803 ldap_free_urllist( lc->lconn_server );
805 /* FIXME: is this at all possible?
806 * ldap_ld_free() in unbind.c calls ldap_free_connection()
807 * with force == 1 __after__ explicitly calling
808 * ldap_free_request() on all requests */
812 for ( lr = ld->ld_requests; lr; ) {
813 LDAPRequest *lr_next = lr->lr_next;
815 if ( lr->lr_conn == lc ) {
816 ldap_free_request_int( ld, lr );
823 if ( lc->lconn_sb != ld->ld_sb ) {
824 ber_sockbuf_free( lc->lconn_sb );
826 ber_int_sb_close( lc->lconn_sb );
829 if ( lc->lconn_rebind_queue != NULL) {
831 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
832 LDAP_VFREE( lc->lconn_rebind_queue[i] );
834 LDAP_FREE( lc->lconn_rebind_queue );
839 Debug( LDAP_DEBUG_TRACE,
840 "ldap_free_connection: actually freed\n",
844 lc->lconn_lastused = time( NULL );
845 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
846 lc->lconn_refcnt, 0, 0 );
851 /* Protects self with ld_conn_mutex */
854 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
859 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
860 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
861 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
862 if ( lc->lconn_server != NULL ) {
863 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
864 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
865 : lc->lconn_server->lud_host,
866 lc->lconn_server->lud_port, ( lc->lconn_sb ==
867 ld->ld_sb ) ? " (default)" : "" );
869 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
870 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
872 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
873 ? "Connecting" : "Connected", 0 );
874 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
875 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
876 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
877 if ( lc->lconn_rebind_inprogress ) {
878 if ( lc->lconn_rebind_queue != NULL) {
881 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
883 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
884 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
885 i, j, lc->lconn_rebind_queue[i][j] );
889 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
892 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
897 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
901 /* protected by req_mutex and res_mutex */
903 ldap_dump_requests_and_responses( LDAP *ld )
909 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
911 lr = ld->ld_requests;
913 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
915 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
916 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
917 lr->lr_msgid, lr->lr_origid,
918 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
919 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
920 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
921 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
922 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
924 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
925 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
927 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
928 (void *)ld, i, ld->ld_nabandoned );
929 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
930 if ( ( lm = ld->ld_responses ) == NULL ) {
931 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
933 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
934 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
935 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
936 if ( lm->lm_chain != NULL ) {
937 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
938 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
939 Debug( LDAP_DEBUG_TRACE,
940 " * msgid %d, type %lu\n",
942 (unsigned long)l->lm_msgtype, 0 );
946 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
948 #endif /* LDAP_DEBUG */
950 /* protected by req_mutex */
952 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
954 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
955 /* if lr_refcnt > 0, the request has been looked up
956 * by ldap_find_request_by_msgid(); if in the meanwhile
957 * the request is free()'d by someone else, just decrease
958 * the reference count and extract it from the request
959 * list; later on, it will be freed. */
960 if ( lr->lr_prev == NULL ) {
961 if ( lr->lr_refcnt == 0 ) {
962 /* free'ing the first request? */
963 assert( ld->ld_requests == lr );
966 if ( ld->ld_requests == lr ) {
967 ld->ld_requests = lr->lr_next;
971 lr->lr_prev->lr_next = lr->lr_next;
974 if ( lr->lr_next != NULL ) {
975 lr->lr_next->lr_prev = lr->lr_prev;
978 if ( lr->lr_refcnt > 0 ) {
979 lr->lr_refcnt = -lr->lr_refcnt;
987 if ( lr->lr_ber != NULL ) {
988 ber_free( lr->lr_ber, 1 );
992 if ( lr->lr_res_error != NULL ) {
993 LDAP_FREE( lr->lr_res_error );
994 lr->lr_res_error = NULL;
997 if ( lr->lr_res_matched != NULL ) {
998 LDAP_FREE( lr->lr_res_matched );
999 lr->lr_res_matched = NULL;
1005 /* protected by req_mutex */
1007 ldap_free_request( LDAP *ld, LDAPRequest *lr )
1009 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1010 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
1011 lr->lr_origid, lr->lr_msgid, 0 );
1013 /* free all referrals (child requests) */
1014 while ( lr->lr_child ) {
1015 ldap_free_request( ld, lr->lr_child );
1018 if ( lr->lr_parent != NULL ) {
1021 --lr->lr_parent->lr_outrefcnt;
1022 for ( lrp = &lr->lr_parent->lr_child;
1024 lrp = &(*lrp)->lr_refnext );
1027 *lrp = lr->lr_refnext;
1030 ldap_free_request_int( ld, lr );
1034 * call first time with *cntp = -1
1035 * when returns *cntp == -1, no referrals are left
1037 * NOTE: may replace *refsp, or shuffle the contents
1038 * of the original array.
1040 static int ldap_int_nextref(
1046 assert( refsp != NULL );
1047 assert( *refsp != NULL );
1048 assert( cntp != NULL );
1057 if ( (*refsp)[ *cntp ] == NULL ) {
1065 * Chase v3 referrals
1068 * (IN) ld = LDAP connection handle
1069 * (IN) lr = LDAP Request structure
1070 * (IN) refs = array of pointers to referral strings that we will chase
1071 * The array will be free'd by this function when no longer needed
1072 * (IN) sref != 0 if following search reference
1073 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1074 * (OUT) hadrefp = 1 if sucessfully followed referral
1076 * Return value - number of referrals followed
1078 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1081 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1084 int unfollowedcnt = 0;
1085 LDAPRequest *origreq;
1086 LDAPURLDesc *srv = NULL;
1088 char **refarray = NULL;
1090 int rc, count, i, j, id;
1092 LDAP_NEXTREF_PROC *nextref_proc = ld->ld_nextref_proc ? ld->ld_nextref_proc : ldap_int_nextref;
1094 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1095 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1096 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1097 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1099 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1105 /* If no referrals in array, return */
1106 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1111 /* Check for hop limit exceeded */
1112 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1113 Debug( LDAP_DEBUG_ANY,
1114 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1115 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1120 /* find original request */
1122 origreq->lr_parent != NULL;
1123 origreq = origreq->lr_parent )
1131 /* parse out & follow referrals */
1132 /* NOTE: if nextref_proc == ldap_int_nextref, params is ignored */
1134 for ( nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1136 nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1139 /* Parse the referral URL */
1140 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1141 if ( rc != LDAP_URL_SUCCESS ) {
1142 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1143 * which do not map on API errors */
1144 ld->ld_errno = LDAP_PARAM_ERROR;
1149 if( srv->lud_crit_exts ) {
1152 /* If StartTLS is the only critical ext, OK. */
1153 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1157 /* we do not support any other extensions */
1158 ld->ld_errno = LDAP_NOT_SUPPORTED;
1164 /* check connection for re-bind in progress */
1165 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1166 /* See if we've already requested this DN with this conn */
1169 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1170 for ( lp = origreq; lp; ) {
1171 if ( lp->lr_conn == lc
1172 && len == lp->lr_dn.bv_len
1174 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1179 if ( lp == origreq ) {
1182 lp = lp->lr_refnext;
1186 ldap_free_urllist( srv );
1188 ld->ld_errno = LDAP_CLIENT_LOOP;
1193 if ( lc->lconn_rebind_inprogress ) {
1194 /* We are already chasing a referral or search reference and a
1195 * bind on that connection is in progress. We must queue
1196 * referrals on that connection, so we don't get a request
1197 * going out before the bind operation completes. This happens
1198 * if two search references come in one behind the other
1199 * for the same server with different contexts.
1201 Debug( LDAP_DEBUG_TRACE,
1202 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1204 if( lc->lconn_rebind_queue == NULL ) {
1205 /* Create a referral list */
1206 lc->lconn_rebind_queue =
1207 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1209 if( lc->lconn_rebind_queue == NULL) {
1210 ld->ld_errno = LDAP_NO_MEMORY;
1215 lc->lconn_rebind_queue[0] = refarray;
1216 lc->lconn_rebind_queue[1] = NULL;
1220 /* Count how many referral arrays we already have */
1221 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1225 /* Add the new referral to the list */
1226 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1227 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1229 if( lc->lconn_rebind_queue == NULL ) {
1230 ld->ld_errno = LDAP_NO_MEMORY;
1234 lc->lconn_rebind_queue[j] = refarray;
1235 lc->lconn_rebind_queue[j+1] = NULL;
1239 /* We have queued the referral/reference, now just return */
1242 count = 1; /* Pretend we already followed referral */
1246 /* Re-encode the request with the new starting point of the search.
1247 * Note: In the future we also need to replace the filter if one
1248 * was provided with the search reference
1251 /* For references we don't want old dn if new dn empty */
1252 if ( sref && srv->lud_dn == NULL ) {
1253 srv->lud_dn = LDAP_STRDUP( "" );
1256 LDAP_NEXT_MSGID( ld, id );
1257 ber = re_encode_request( ld, origreq->lr_ber, id,
1258 sref, srv, &rinfo.ri_request );
1261 ld->ld_errno = LDAP_ENCODING_ERROR;
1266 Debug( LDAP_DEBUG_TRACE,
1267 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1268 lr->lr_msgid, refarray[i], 0);
1270 /* Send the new request to the server - may require a bind */
1271 rinfo.ri_msgid = origreq->lr_origid;
1272 rinfo.ri_url = refarray[i];
1273 rc = ldap_send_server_request( ld, ber, id,
1274 origreq, &srv, NULL, &rinfo, 0, 1 );
1276 /* Failure, try next referral in the list */
1277 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1278 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1279 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1280 ldap_free_urllist( srv );
1282 ld->ld_errno = LDAP_REFERRAL;
1284 /* Success, no need to try this referral list further */
1289 /* check if there is a queue of referrals that came in during bind */
1291 lc = find_connection( ld, srv, 1 );
1293 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1295 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
1300 if ( lc->lconn_rebind_queue != NULL ) {
1301 /* Release resources of previous list */
1302 LDAP_VFREE( refarray );
1304 ldap_free_urllist( srv );
1307 /* Pull entries off end of queue so list always null terminated */
1308 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1310 refarray = lc->lconn_rebind_queue[j - 1];
1311 lc->lconn_rebind_queue[j-1] = NULL;
1312 /* we pulled off last entry from queue, free queue */
1314 LDAP_FREE( lc->lconn_rebind_queue );
1315 lc->lconn_rebind_queue = NULL;
1317 /* restart the loop the with new referral list */
1321 break; /* referral followed, break out of for loop */
1323 } /* end for loop */
1325 LDAP_VFREE( refarray );
1326 ldap_free_urllist( srv );
1327 LDAP_FREE( *errstrp );
1331 LDAP_FREE( unfollowed );
1334 *errstrp = unfollowed;
1340 * XXX merging of errors in this routine needs to be improved
1341 * Protected by res_mutex, conn_mutex and req_mutex (try_read1msg)
1344 ldap_chase_referrals( LDAP *ld,
1352 char *p, *ref, *unfollowed;
1353 LDAPRequest *origreq;
1359 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
1360 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_conn_mutex );
1361 LDAP_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
1362 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1364 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1367 if ( *errstrp == NULL ) {
1371 len = strlen( *errstrp );
1372 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1373 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1375 p += LDAP_REF_STR_LEN;
1380 if ( len < LDAP_REF_STR_LEN ) {
1384 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1385 Debug( LDAP_DEBUG_ANY,
1386 "more than %d referral hops (dropping)\n",
1387 ld->ld_refhoplimit, 0, 0 );
1388 /* XXX report as error in ld->ld_errno? */
1392 /* find original request */
1393 for ( origreq = lr; origreq->lr_parent != NULL;
1394 origreq = origreq->lr_parent ) {
1401 /* parse out & follow referrals */
1402 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1403 p = strchr( ref, '\n' );
1408 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1409 if ( rc != LDAP_URL_SUCCESS ) {
1410 Debug( LDAP_DEBUG_TRACE,
1411 "ignoring %s referral <%s>\n",
1412 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1413 rc = ldap_append_referral( ld, &unfollowed, ref );
1418 Debug( LDAP_DEBUG_TRACE,
1419 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1423 /* See if we've already been here */
1424 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1427 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1428 for ( lp = lr; lp; lp = lp->lr_parent ) {
1429 if ( lp->lr_conn == lc
1430 && len == lp->lr_dn.bv_len )
1432 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1439 ldap_free_urllist( srv );
1440 ld->ld_errno = LDAP_CLIENT_LOOP;
1446 LDAP_NEXT_MSGID( ld, id );
1447 ber = re_encode_request( ld, origreq->lr_ber,
1448 id, sref, srv, &rinfo.ri_request );
1450 if ( ber == NULL ) {
1451 ldap_free_urllist( srv );
1455 /* copy the complete referral for rebind process */
1456 rinfo.ri_url = LDAP_STRDUP( ref );
1458 rinfo.ri_msgid = origreq->lr_origid;
1460 rc = ldap_send_server_request( ld, ber, id,
1461 lr, &srv, NULL, &rinfo, 0, 1 );
1462 LDAP_FREE( rinfo.ri_url );
1467 Debug( LDAP_DEBUG_ANY,
1468 "Unable to chase referral \"%s\" (%d: %s)\n",
1469 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1470 rc = ldap_append_referral( ld, &unfollowed, ref );
1473 ldap_free_urllist(srv);
1476 LDAP_FREE( *errstrp );
1477 *errstrp = unfollowed;
1479 return(( rc == 0 ) ? count : rc );
1484 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1488 if ( *referralsp == NULL ) {
1490 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1494 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1495 strlen( *referralsp ) + strlen( s ) + 2 );
1498 if ( *referralsp == NULL ) {
1499 ld->ld_errno = LDAP_NO_MEMORY;
1504 strcpy( *referralsp, LDAP_REF_STR );
1506 strcat( *referralsp, "\n" );
1508 strcat( *referralsp, s );
1516 re_encode_request( LDAP *ld,
1517 BerElement *origber,
1524 * XXX this routine knows way too much about how the lber library works!
1532 BerElement tmpber, *ber;
1535 Debug( LDAP_DEBUG_TRACE,
1536 "re_encode_request: new msgid %ld, new dn <%s>\n",
1538 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1543 * all LDAP requests are sequences that start with a message id.
1544 * For all except delete, this is followed by a sequence that is
1545 * tagged with the operation code. For delete, the provided DN
1546 * is not wrapped by a sequence.
1548 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1550 if ( rtag == LBER_ERROR ) {
1551 ld->ld_errno = LDAP_DECODING_ERROR;
1556 if ( tag == LDAP_REQ_BIND ) {
1557 /* bind requests have a version number before the DN & other stuff */
1558 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1560 } else if ( tag == LDAP_REQ_DELETE ) {
1561 /* delete requests don't have a DN wrapping sequence */
1562 rtag = ber_scanf( &tmpber, "m", &dn );
1564 } else if ( tag == LDAP_REQ_SEARCH ) {
1565 /* search requests need to be re-scope-ed */
1566 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1568 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1569 /* use the scope provided in reference */
1570 scope = srv->lud_scope;
1572 } else if ( sref ) {
1573 /* use scope implied by previous operation
1576 * subtree -> subtree
1577 * subordinate -> subtree
1581 case LDAP_SCOPE_BASE:
1582 case LDAP_SCOPE_ONELEVEL:
1583 scope = LDAP_SCOPE_BASE;
1585 case LDAP_SCOPE_SUBTREE:
1586 case LDAP_SCOPE_SUBORDINATE:
1587 scope = LDAP_SCOPE_SUBTREE;
1593 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1596 if( rtag == LBER_ERROR ) {
1597 ld->ld_errno = LDAP_DECODING_ERROR;
1601 /* restore character zero'd out by ber_scanf*/
1602 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1604 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1608 if ( srv->lud_dn ) {
1609 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1612 if ( tag == LDAP_REQ_BIND ) {
1613 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1614 } else if ( tag == LDAP_REQ_DELETE ) {
1615 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1616 } else if ( tag == LDAP_REQ_SEARCH ) {
1617 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1619 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1623 ld->ld_errno = LDAP_ENCODING_ERROR;
1628 if ( tag != LDAP_REQ_DELETE && (
1629 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1630 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1631 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1633 ld->ld_errno = LDAP_ENCODING_ERROR;
1639 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1640 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1642 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1644 #endif /* LDAP_DEBUG */
1646 *type = tag; /* return request type */
1651 /* protected by req_mutex */
1653 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1657 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1658 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1659 continue; /* Skip completed requests */
1661 if ( msgid == lr->lr_msgid ) {
1670 /* protected by req_mutex */
1672 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1676 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1678 if ( lr->lr_refcnt > 0 ) {
1681 } else if ( lr->lr_refcnt < 0 ) {
1683 if ( lr->lr_refcnt == 0 ) {
1691 ldap_free_request_int( ld, lrx );
1693 } else if ( freeit ) {
1694 ldap_free_request( ld, lrx );