2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
57 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
58 static void ldap_free_request_int LDAP_P(( LDAP *ld, LDAPRequest *lr ));
61 re_encode_request( LDAP *ld,
69 ldap_alloc_ber_with_options( LDAP *ld )
73 ber = ber_alloc_t( ld->ld_lberoptions );
75 ld->ld_errno = LDAP_NO_MEMORY;
83 ldap_set_ber_options( LDAP *ld, BerElement *ber )
85 ber->ber_options = ld->ld_lberoptions;
90 ldap_send_initial_request(
99 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
101 #ifdef LDAP_R_COMPILE
102 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
104 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
105 /* not connected yet */
106 rc = ldap_open_defconn( ld );
109 #ifdef LDAP_R_COMPILE
110 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
115 } else if ( rc == 0 ) {
116 Debug( LDAP_DEBUG_TRACE,
117 "ldap_open_defconn: successful\n",
121 #ifdef LDAP_CONNECTIONLESS
122 if (LDAP_IS_UDP(ld)) {
123 if (msgtype == LDAP_REQ_BIND) {
124 if (ld->ld_options.ldo_cldapdn)
125 ldap_memfree(ld->ld_options.ldo_cldapdn);
126 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
129 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
130 return LDAP_PARAM_ERROR;
133 #ifdef LDAP_R_COMPILE
134 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
136 rc = ldap_send_server_request( ld, ber, msgid, NULL,
138 #ifdef LDAP_R_COMPILE
139 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
146 ldap_int_flush_request(
150 LDAPConn *lc = lr->lr_conn;
152 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
153 if ( sock_errno() == EAGAIN ) {
154 /* need to continue write later */
155 lr->lr_status = LDAP_REQST_WRITING;
156 ldap_mark_select_write( ld, lc->lconn_sb );
157 ld->ld_errno = LDAP_BUSY;
160 ld->ld_errno = LDAP_SERVER_DOWN;
161 ldap_free_request( ld, lr );
162 ldap_free_connection( ld, lc, 0, 0 );
166 if ( lr->lr_parent == NULL ) {
167 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
168 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
170 lr->lr_status = LDAP_REQST_INPROGRESS;
172 /* sent -- waiting for a response */
173 ldap_mark_select_read( ld, lc->lconn_sb );
179 ldap_send_server_request(
183 LDAPRequest *parentreq,
184 LDAPURLDesc **srvlist,
191 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
194 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
197 if ( srvlist == NULL ) {
200 lc = find_connection( ld, *srvlist, 1 );
202 if ( (bind != NULL) && (parentreq != NULL) ) {
203 /* Remember the bind in the parent */
205 ++parentreq->lr_outrefcnt;
207 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
212 /* async connect... */
213 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
214 ber_socket_t sd = AC_SOCKET_ERROR;
215 struct timeval tv = { 0 };
217 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
220 switch ( ldap_int_poll( ld, sd, &tv ) ) {
223 lc->lconn_status = LDAP_CONNST_CONNECTED;
227 /* async only occurs if a network timeout is set */
229 /* honor network timeout */
230 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
232 /* caller will have to call again */
233 ld->ld_errno = LDAP_X_CONNECTING;
243 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
244 if ( ld->ld_errno == LDAP_SUCCESS ) {
245 ld->ld_errno = LDAP_SERVER_DOWN;
250 /* Forget about the bind */
251 --parentreq->lr_outrefcnt;
256 use_connection( ld, lc );
258 #ifdef LDAP_CONNECTIONLESS
259 if ( LDAP_IS_UDP( ld )) {
260 BerElement tmpber = *ber;
261 ber_rewind( &tmpber );
262 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
263 sizeof( struct sockaddr ), 0 );
265 ld->ld_errno = LDAP_ENCODING_ERROR;
271 /* If we still have an incomplete write, try to finish it before
272 * dealing with the new request. If we don't finish here, return
273 * LDAP_BUSY and let the caller retry later. We only allow a single
274 * request to be in WRITING state.
277 if ( ld->ld_requests &&
278 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
279 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
285 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
287 ld->ld_errno = LDAP_NO_MEMORY;
288 ldap_free_connection( ld, lc, 0, 0 );
291 /* Forget about the bind */
292 --parentreq->lr_outrefcnt;
296 lr->lr_msgid = msgid;
297 lr->lr_status = LDAP_REQST_INPROGRESS;
298 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
301 if ( parentreq != NULL ) { /* sub-request */
303 /* Increment if we didn't do it before the bind */
304 ++parentreq->lr_outrefcnt;
306 lr->lr_origid = parentreq->lr_origid;
307 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
308 lr->lr_parent = parentreq;
309 lr->lr_refnext = parentreq->lr_child;
310 parentreq->lr_child = lr;
311 } else { /* original request */
312 lr->lr_origid = lr->lr_msgid;
315 /* Extract requestDN for future reference */
317 BerElement tmpber = *ber;
321 ber_reset( &tmpber, 1 );
322 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
325 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
327 case LDAP_REQ_DELETE:
330 rtag = ber_scanf( &tmpber, "{" /*}*/ );
331 case LDAP_REQ_ABANDON:
334 if ( tag != LDAP_REQ_ABANDON ) {
335 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
336 lr->lr_dn.bv_val = tmpber.ber_ptr;
341 lr->lr_next = ld->ld_requests;
342 if ( lr->lr_next != NULL ) {
343 lr->lr_next->lr_prev = lr;
345 ld->ld_requests = lr;
347 ld->ld_errno = LDAP_SUCCESS;
348 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
355 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
357 find_tls_ext( LDAPURLDesc *srv )
362 if ( !srv->lud_exts )
365 for (i=0; srv->lud_exts[i]; i++) {
367 ext = srv->lud_exts[i];
368 if ( ext[0] == '!') {
372 if ( !strcasecmp( ext, "StartTLS" ) ||
373 !strcasecmp( ext, "X-StartTLS" ) ||
374 !strcmp( ext, LDAP_EXOP_START_TLS )) {
382 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
383 int connect, LDAPreqinfo *bind )
388 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
389 use_ldsb, connect, (bind != NULL) );
391 * make a new LDAP server connection
392 * XXX open connection synchronously for now
394 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
396 ld->ld_errno = LDAP_NO_MEMORY;
401 assert( ld->ld_sb != NULL );
402 lc->lconn_sb = ld->ld_sb;
405 lc->lconn_sb = ber_sockbuf_alloc();
406 if ( lc->lconn_sb == NULL ) {
407 LDAP_FREE( (char *)lc );
408 ld->ld_errno = LDAP_NO_MEMORY;
414 LDAPURLDesc **srvp, *srv = NULL;
416 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
418 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
421 rc = ldap_int_open_connection( ld, lc, *srvp, async );
425 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
426 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
435 ber_sockbuf_free( lc->lconn_sb );
437 LDAP_FREE( (char *)lc );
438 ld->ld_errno = LDAP_SERVER_DOWN;
442 lc->lconn_server = ldap_url_dup( srv );
445 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
446 #ifdef LDAP_R_COMPILE
447 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
449 lc->lconn_next = ld->ld_conns;
451 #ifdef LDAP_R_COMPILE
452 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
457 if ( lc->lconn_server->lud_exts ) {
458 int rc, ext = find_tls_ext( lc->lconn_server );
460 LDAPConn *savedefconn;
462 savedefconn = ld->ld_defconn;
463 ++lc->lconn_refcnt; /* avoid premature free */
466 #ifdef LDAP_R_COMPILE
467 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
468 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
470 rc = ldap_start_tls_s( ld, NULL, NULL );
471 #ifdef LDAP_R_COMPILE
472 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
473 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
475 ld->ld_defconn = savedefconn;
478 if ( rc != LDAP_SUCCESS && ext == 2 ) {
479 ldap_free_connection( ld, lc, 1, 0 );
487 if ( bind != NULL ) {
489 LDAPConn *savedefconn;
491 /* Set flag to prevent additional referrals
492 * from being processed on this
493 * connection until the bind has completed
495 lc->lconn_rebind_inprogress = 1;
496 /* V3 rebind function */
497 if ( ld->ld_rebind_proc != NULL) {
498 LDAPURLDesc *srvfunc;
500 srvfunc = ldap_url_dup( *srvlist );
501 if ( srvfunc == NULL ) {
502 ld->ld_errno = LDAP_NO_MEMORY;
505 savedefconn = ld->ld_defconn;
506 ++lc->lconn_refcnt; /* avoid premature free */
509 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
510 #ifdef LDAP_R_COMPILE
511 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
512 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
514 err = (*ld->ld_rebind_proc)( ld,
515 bind->ri_url, bind->ri_request, bind->ri_msgid,
516 ld->ld_rebind_params );
517 #ifdef LDAP_R_COMPILE
518 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
519 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
522 ld->ld_defconn = savedefconn;
527 ldap_free_connection( ld, lc, 1, 0 );
530 ldap_free_urldesc( srvfunc );
535 struct berval passwd = BER_BVNULL;
537 savedefconn = ld->ld_defconn;
538 ++lc->lconn_refcnt; /* avoid premature free */
541 Debug( LDAP_DEBUG_TRACE,
542 "anonymous rebind via ldap_sasl_bind(\"\")\n",
545 #ifdef LDAP_R_COMPILE
546 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
547 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
549 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
550 NULL, NULL, &msgid );
551 if ( rc != LDAP_SUCCESS ) {
555 for ( err = 1; err > 0; ) {
556 struct timeval tv = { 0, 100000 };
557 LDAPMessage *res = NULL;
559 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
565 #ifdef LDAP_R_COMPILE
566 ldap_pvt_thread_yield();
571 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
572 if ( rc != LDAP_SUCCESS ) {
575 } else if ( err != LDAP_SUCCESS ) {
578 /* else err == LDAP_SUCCESS == 0 */
582 Debug( LDAP_DEBUG_TRACE,
583 "ldap_new_connection %p: "
584 "unexpected response %d "
585 "from BIND request id=%d\n",
586 (void *) ld, ldap_msgtype( res ), msgid );
592 #ifdef LDAP_R_COMPILE
593 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
594 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
596 ld->ld_defconn = savedefconn;
600 ldap_free_connection( ld, lc, 1, 0 );
605 lc->lconn_rebind_inprogress = 0;
613 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
615 * return an existing connection (if any) to the server srv
616 * if "any" is non-zero, check for any server in the "srv" chain
620 LDAPURLDesc *lcu, *lsu;
621 int lcu_port, lsu_port;
624 #ifdef LDAP_R_COMPILE
625 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
627 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
628 lcu = lc->lconn_server;
629 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
632 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
633 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
636 if ( lsu_port == lcu_port
637 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
638 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
639 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
640 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
651 #ifdef LDAP_R_COMPILE
652 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
660 use_connection( LDAP *ld, LDAPConn *lc )
663 lc->lconn_lastused = time( NULL );
668 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
670 LDAPConn *tmplc, *prevlc;
672 Debug( LDAP_DEBUG_TRACE,
673 "ldap_free_connection %d %d\n",
676 if ( force || --lc->lconn_refcnt <= 0 ) {
677 /* remove from connections list first */
678 #ifdef LDAP_R_COMPILE
679 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
682 for ( prevlc = NULL, tmplc = ld->ld_conns;
684 tmplc = tmplc->lconn_next )
687 if ( prevlc == NULL ) {
688 ld->ld_conns = tmplc->lconn_next;
690 prevlc->lconn_next = tmplc->lconn_next;
692 if ( ld->ld_defconn == lc ) {
693 ld->ld_defconn = NULL;
699 #ifdef LDAP_R_COMPILE
700 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
703 /* process connection callbacks */
705 struct ldapoptions *lo;
709 lo = &ld->ld_options;
710 if ( lo->ldo_conn_cbs ) {
711 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
713 cb->lc_del( ld, lc->lconn_sb, cb );
716 lo = LDAP_INT_GLOBAL_OPT();
717 if ( lo->ldo_conn_cbs ) {
718 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
720 cb->lc_del( ld, lc->lconn_sb, cb );
725 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
726 ldap_mark_select_clear( ld, lc->lconn_sb );
728 ldap_send_unbind( ld, lc->lconn_sb,
733 if ( lc->lconn_ber != NULL ) {
734 ber_free( lc->lconn_ber, 1 );
737 ldap_int_sasl_close( ld, lc );
739 ldap_int_gssapi_close( ld, lc );
742 ldap_free_urllist( lc->lconn_server );
744 /* FIXME: is this at all possible?
745 * ldap_ld_free() in unbind.c calls ldap_free_connection()
746 * with force == 1 __after__ explicitly calling
747 * ldap_free_request() on all requests */
751 for ( lr = ld->ld_requests; lr; ) {
752 LDAPRequest *lr_next = lr->lr_next;
754 if ( lr->lr_conn == lc ) {
755 ldap_free_request_int( ld, lr );
762 if ( lc->lconn_sb != ld->ld_sb ) {
763 ber_sockbuf_free( lc->lconn_sb );
765 ber_int_sb_close( lc->lconn_sb );
768 if ( lc->lconn_rebind_queue != NULL) {
770 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
771 LDAP_VFREE( lc->lconn_rebind_queue[i] );
773 LDAP_FREE( lc->lconn_rebind_queue );
778 Debug( LDAP_DEBUG_TRACE,
779 "ldap_free_connection: actually freed\n",
783 lc->lconn_lastused = time( NULL );
784 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
785 lc->lconn_refcnt, 0, 0 );
792 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
797 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
798 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
799 if ( lc->lconn_server != NULL ) {
800 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
801 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
802 : lc->lconn_server->lud_host,
803 lc->lconn_server->lud_port, ( lc->lconn_sb ==
804 ld->ld_sb ) ? " (default)" : "" );
806 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
807 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
809 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
810 ? "Connecting" : "Connected", 0 );
811 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
812 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
813 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
814 if ( lc->lconn_rebind_inprogress ) {
815 if ( lc->lconn_rebind_queue != NULL) {
818 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
820 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
821 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
822 i, j, lc->lconn_rebind_queue[i][j] );
826 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
829 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
838 ldap_dump_requests_and_responses( LDAP *ld )
844 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
846 lr = ld->ld_requests;
848 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
850 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
851 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
852 lr->lr_msgid, lr->lr_origid,
853 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
854 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
855 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
856 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
857 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
859 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
860 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
862 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
863 (void *)ld, i, ld->ld_nabandoned );
864 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
865 if ( ( lm = ld->ld_responses ) == NULL ) {
866 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
868 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
869 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
870 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
871 if ( lm->lm_chain != NULL ) {
872 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
873 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
874 Debug( LDAP_DEBUG_TRACE,
875 " * msgid %d, type %lu\n",
877 (unsigned long)l->lm_msgtype, 0 );
881 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
883 #endif /* LDAP_DEBUG */
886 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
888 /* if lr_refcnt > 0, the request has been looked up
889 * by ldap_find_request_by_msgid(); if in the meanwhile
890 * the request is free()'d by someone else, just decrease
891 * the reference count and extract it from the request
892 * list; later on, it will be freed. */
893 if ( lr->lr_prev == NULL ) {
894 if ( lr->lr_refcnt == 0 ) {
895 /* free'ing the first request? */
896 assert( ld->ld_requests == lr );
899 if ( ld->ld_requests == lr ) {
900 ld->ld_requests = lr->lr_next;
904 lr->lr_prev->lr_next = lr->lr_next;
907 if ( lr->lr_next != NULL ) {
908 lr->lr_next->lr_prev = lr->lr_prev;
911 if ( lr->lr_refcnt > 0 ) {
912 lr->lr_refcnt = -lr->lr_refcnt;
920 if ( lr->lr_ber != NULL ) {
921 ber_free( lr->lr_ber, 1 );
925 if ( lr->lr_res_error != NULL ) {
926 LDAP_FREE( lr->lr_res_error );
927 lr->lr_res_error = NULL;
930 if ( lr->lr_res_matched != NULL ) {
931 LDAP_FREE( lr->lr_res_matched );
932 lr->lr_res_matched = NULL;
939 ldap_free_request( LDAP *ld, LDAPRequest *lr )
941 #ifdef LDAP_R_COMPILE
942 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
945 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
946 lr->lr_origid, lr->lr_msgid, 0 );
948 /* free all referrals (child requests) */
949 while ( lr->lr_child ) {
950 ldap_free_request( ld, lr->lr_child );
953 if ( lr->lr_parent != NULL ) {
956 --lr->lr_parent->lr_outrefcnt;
957 for ( lrp = &lr->lr_parent->lr_child;
959 lrp = &(*lrp)->lr_refnext );
962 *lrp = lr->lr_refnext;
965 ldap_free_request_int( ld, lr );
969 * call first time with *cntp = -1
970 * when returns *cntp == -1, no referrals are left
972 * NOTE: may replace *refsp, or shuffle the contents
973 * of the original array.
975 static int ldap_int_nextref(
981 assert( refsp != NULL );
982 assert( *refsp != NULL );
983 assert( cntp != NULL );
992 if ( (*refsp)[ *cntp ] == NULL ) {
1000 * Chase v3 referrals
1003 * (IN) ld = LDAP connection handle
1004 * (IN) lr = LDAP Request structure
1005 * (IN) refs = array of pointers to referral strings that we will chase
1006 * The array will be free'd by this function when no longer needed
1007 * (IN) sref != 0 if following search reference
1008 * (OUT) errstrp = Place to return a string of referrals which could not be followed
1009 * (OUT) hadrefp = 1 if sucessfully followed referral
1011 * Return value - number of referrals followed
1014 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1017 int unfollowedcnt = 0;
1018 LDAPRequest *origreq;
1019 LDAPURLDesc *srv = NULL;
1021 char **refarray = NULL;
1023 int rc, count, i, j, id;
1026 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1029 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1034 /* If no referrals in array, return */
1035 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1040 /* Check for hop limit exceeded */
1041 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1042 Debug( LDAP_DEBUG_ANY,
1043 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1044 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1049 /* find original request */
1051 origreq->lr_parent != NULL;
1052 origreq = origreq->lr_parent )
1060 if ( ld->ld_nextref_proc == NULL ) {
1061 ld->ld_nextref_proc = ldap_int_nextref;
1064 /* parse out & follow referrals */
1066 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1068 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1071 /* Parse the referral URL */
1072 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1073 if ( rc != LDAP_URL_SUCCESS ) {
1074 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1075 * which do not map on API errors */
1076 ld->ld_errno = LDAP_PARAM_ERROR;
1081 if( srv->lud_crit_exts ) {
1084 /* If StartTLS is the only critical ext, OK. */
1085 if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1089 /* we do not support any other extensions */
1090 ld->ld_errno = LDAP_NOT_SUPPORTED;
1096 /* check connection for re-bind in progress */
1097 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1098 /* See if we've already requested this DN with this conn */
1101 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1102 for ( lp = origreq; lp; ) {
1103 if ( lp->lr_conn == lc
1104 && len == lp->lr_dn.bv_len
1106 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1111 if ( lp == origreq ) {
1114 lp = lp->lr_refnext;
1118 ldap_free_urllist( srv );
1120 ld->ld_errno = LDAP_CLIENT_LOOP;
1125 if ( lc->lconn_rebind_inprogress ) {
1126 /* We are already chasing a referral or search reference and a
1127 * bind on that connection is in progress. We must queue
1128 * referrals on that connection, so we don't get a request
1129 * going out before the bind operation completes. This happens
1130 * if two search references come in one behind the other
1131 * for the same server with different contexts.
1133 Debug( LDAP_DEBUG_TRACE,
1134 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1136 if( lc->lconn_rebind_queue == NULL ) {
1137 /* Create a referral list */
1138 lc->lconn_rebind_queue =
1139 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1141 if( lc->lconn_rebind_queue == NULL) {
1142 ld->ld_errno = LDAP_NO_MEMORY;
1147 lc->lconn_rebind_queue[0] = refarray;
1148 lc->lconn_rebind_queue[1] = NULL;
1152 /* Count how many referral arrays we already have */
1153 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1157 /* Add the new referral to the list */
1158 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1159 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1161 if( lc->lconn_rebind_queue == NULL ) {
1162 ld->ld_errno = LDAP_NO_MEMORY;
1166 lc->lconn_rebind_queue[j] = refarray;
1167 lc->lconn_rebind_queue[j+1] = NULL;
1171 /* We have queued the referral/reference, now just return */
1174 count = 1; /* Pretend we already followed referral */
1178 /* Re-encode the request with the new starting point of the search.
1179 * Note: In the future we also need to replace the filter if one
1180 * was provided with the search reference
1183 /* For references we don't want old dn if new dn empty */
1184 if ( sref && srv->lud_dn == NULL ) {
1185 srv->lud_dn = LDAP_STRDUP( "" );
1188 LDAP_NEXT_MSGID( ld, id );
1189 ber = re_encode_request( ld, origreq->lr_ber, id,
1190 sref, srv, &rinfo.ri_request );
1193 ld->ld_errno = LDAP_ENCODING_ERROR;
1198 Debug( LDAP_DEBUG_TRACE,
1199 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1200 lr->lr_msgid, refarray[i], 0);
1202 /* Send the new request to the server - may require a bind */
1203 rinfo.ri_msgid = origreq->lr_origid;
1204 rinfo.ri_url = refarray[i];
1205 #ifdef LDAP_R_COMPILE
1206 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1208 rc = ldap_send_server_request( ld, ber, id,
1209 origreq, &srv, NULL, &rinfo );
1210 #ifdef LDAP_R_COMPILE
1211 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1214 /* Failure, try next referral in the list */
1215 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1216 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1217 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1218 ldap_free_urllist( srv );
1220 ld->ld_errno = LDAP_REFERRAL;
1222 /* Success, no need to try this referral list further */
1227 /* check if there is a queue of referrals that came in during bind */
1229 lc = find_connection( ld, srv, 1 );
1231 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1237 if ( lc->lconn_rebind_queue != NULL ) {
1238 /* Release resources of previous list */
1239 LDAP_VFREE( refarray );
1241 ldap_free_urllist( srv );
1244 /* Pull entries off end of queue so list always null terminated */
1245 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1247 refarray = lc->lconn_rebind_queue[j - 1];
1248 lc->lconn_rebind_queue[j-1] = NULL;
1249 /* we pulled off last entry from queue, free queue */
1251 LDAP_FREE( lc->lconn_rebind_queue );
1252 lc->lconn_rebind_queue = NULL;
1254 /* restart the loop the with new referral list */
1258 break; /* referral followed, break out of for loop */
1260 } /* end for loop */
1262 LDAP_VFREE( refarray );
1263 ldap_free_urllist( srv );
1264 LDAP_FREE( *errstrp );
1268 LDAP_FREE( unfollowed );
1271 *errstrp = unfollowed;
1277 * XXX merging of errors in this routine needs to be improved
1280 ldap_chase_referrals( LDAP *ld,
1288 char *p, *ref, *unfollowed;
1289 LDAPRequest *origreq;
1295 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1297 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1300 if ( *errstrp == NULL ) {
1304 len = strlen( *errstrp );
1305 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1306 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1308 p += LDAP_REF_STR_LEN;
1313 if ( len < LDAP_REF_STR_LEN ) {
1317 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1318 Debug( LDAP_DEBUG_ANY,
1319 "more than %d referral hops (dropping)\n",
1320 ld->ld_refhoplimit, 0, 0 );
1321 /* XXX report as error in ld->ld_errno? */
1325 /* find original request */
1326 for ( origreq = lr; origreq->lr_parent != NULL;
1327 origreq = origreq->lr_parent ) {
1334 /* parse out & follow referrals */
1335 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1336 p = strchr( ref, '\n' );
1341 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1342 if ( rc != LDAP_URL_SUCCESS ) {
1343 Debug( LDAP_DEBUG_TRACE,
1344 "ignoring %s referral <%s>\n",
1345 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1346 rc = ldap_append_referral( ld, &unfollowed, ref );
1351 Debug( LDAP_DEBUG_TRACE,
1352 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1356 /* See if we've already been here */
1357 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1360 ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1361 for ( lp = lr; lp; lp = lp->lr_parent ) {
1362 if ( lp->lr_conn == lc
1363 && len == lp->lr_dn.bv_len )
1365 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1372 ldap_free_urllist( srv );
1373 ld->ld_errno = LDAP_CLIENT_LOOP;
1379 LDAP_NEXT_MSGID( ld, id );
1380 ber = re_encode_request( ld, origreq->lr_ber,
1381 id, sref, srv, &rinfo.ri_request );
1383 if ( ber == NULL ) {
1387 /* copy the complete referral for rebind process */
1388 rinfo.ri_url = LDAP_STRDUP( ref );
1390 rinfo.ri_msgid = origreq->lr_origid;
1392 #ifdef LDAP_R_COMPILE
1393 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1395 rc = ldap_send_server_request( ld, ber, id,
1396 lr, &srv, NULL, &rinfo );
1397 #ifdef LDAP_R_COMPILE
1398 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1401 LDAP_FREE( rinfo.ri_url );
1406 Debug( LDAP_DEBUG_ANY,
1407 "Unable to chase referral \"%s\" (%d: %s)\n",
1408 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1409 rc = ldap_append_referral( ld, &unfollowed, ref );
1412 ldap_free_urllist(srv);
1415 LDAP_FREE( *errstrp );
1416 *errstrp = unfollowed;
1418 return(( rc == 0 ) ? count : rc );
1423 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1427 if ( *referralsp == NULL ) {
1429 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1433 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1434 strlen( *referralsp ) + strlen( s ) + 2 );
1437 if ( *referralsp == NULL ) {
1438 ld->ld_errno = LDAP_NO_MEMORY;
1443 strcpy( *referralsp, LDAP_REF_STR );
1445 strcat( *referralsp, "\n" );
1447 strcat( *referralsp, s );
1455 re_encode_request( LDAP *ld,
1456 BerElement *origber,
1463 * XXX this routine knows way too much about how the lber library works!
1471 BerElement tmpber, *ber;
1474 Debug( LDAP_DEBUG_TRACE,
1475 "re_encode_request: new msgid %ld, new dn <%s>\n",
1477 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1482 * all LDAP requests are sequences that start with a message id.
1483 * For all except delete, this is followed by a sequence that is
1484 * tagged with the operation code. For delete, the provided DN
1485 * is not wrapped by a sequence.
1487 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1489 if ( rtag == LBER_ERROR ) {
1490 ld->ld_errno = LDAP_DECODING_ERROR;
1495 if ( tag == LDAP_REQ_BIND ) {
1496 /* bind requests have a version number before the DN & other stuff */
1497 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1499 } else if ( tag == LDAP_REQ_DELETE ) {
1500 /* delete requests don't have a DN wrapping sequence */
1501 rtag = ber_scanf( &tmpber, "m", &dn );
1503 } else if ( tag == LDAP_REQ_SEARCH ) {
1504 /* search requests need to be re-scope-ed */
1505 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1507 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1508 /* use the scope provided in reference */
1509 scope = srv->lud_scope;
1511 } else if ( sref ) {
1512 /* use scope implied by previous operation
1515 * subtree -> subtree
1516 * subordinate -> subtree
1520 case LDAP_SCOPE_BASE:
1521 case LDAP_SCOPE_ONELEVEL:
1522 scope = LDAP_SCOPE_BASE;
1524 case LDAP_SCOPE_SUBTREE:
1525 case LDAP_SCOPE_SUBORDINATE:
1526 scope = LDAP_SCOPE_SUBTREE;
1532 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1535 if( rtag == LBER_ERROR ) {
1536 ld->ld_errno = LDAP_DECODING_ERROR;
1540 /* restore character zero'd out by ber_scanf*/
1541 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1543 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1547 if ( srv->lud_dn ) {
1548 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1551 if ( tag == LDAP_REQ_BIND ) {
1552 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1553 } else if ( tag == LDAP_REQ_DELETE ) {
1554 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1555 } else if ( tag == LDAP_REQ_SEARCH ) {
1556 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1558 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1562 ld->ld_errno = LDAP_ENCODING_ERROR;
1567 if ( tag != LDAP_REQ_DELETE && (
1568 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1569 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1570 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1572 ld->ld_errno = LDAP_ENCODING_ERROR;
1578 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1579 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1581 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1583 #endif /* LDAP_DEBUG */
1585 *type = tag; /* return request type */
1591 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1595 #ifdef LDAP_R_COMPILE
1596 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1598 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1599 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1600 continue; /* Skip completed requests */
1602 if ( msgid == lr->lr_msgid ) {
1607 #ifdef LDAP_R_COMPILE
1608 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1615 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1619 #ifdef LDAP_R_COMPILE
1620 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1622 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1624 if ( lr->lr_refcnt > 0 ) {
1627 } else if ( lr->lr_refcnt < 0 ) {
1629 if ( lr->lr_refcnt == 0 ) {
1637 ldap_free_request_int( ld, lrx );
1639 } else if ( freeit ) {
1640 ldap_free_request( ld, lrx );
1642 #ifdef LDAP_R_COMPILE
1643 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );