2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
74 ld->ld_errno = LDAP_NO_MEMORY;
82 ldap_set_ber_options( LDAP *ld, BerElement *ber )
84 ber->ber_options = ld->ld_lberoptions;
89 ldap_send_initial_request(
98 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
100 #ifdef LDAP_R_COMPILE
101 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
103 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
104 /* not connected yet */
105 rc = ldap_open_defconn( ld );
108 #ifdef LDAP_R_COMPILE
109 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
114 } else if ( rc == 0 ) {
115 Debug( LDAP_DEBUG_TRACE,
116 "ldap_open_defconn: successful\n",
120 #ifdef LDAP_CONNECTIONLESS
121 if (LDAP_IS_UDP(ld)) {
122 if (msgtype == LDAP_REQ_BIND) {
123 if (ld->ld_options.ldo_cldapdn)
124 ldap_memfree(ld->ld_options.ldo_cldapdn);
125 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
128 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
129 return LDAP_PARAM_ERROR;
132 #ifdef LDAP_R_COMPILE
133 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
135 rc = ldap_send_server_request( ld, ber, msgid, NULL,
137 #ifdef LDAP_R_COMPILE
138 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
145 ldap_int_flush_request(
149 LDAPConn *lc = lr->lr_conn;
151 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
152 if ( errno == EAGAIN ) {
153 /* need to continue write later */
154 lr->lr_status = LDAP_REQST_WRITING;
155 ldap_mark_select_write( ld, lc->lconn_sb );
156 ld->ld_errno = LDAP_BUSY;
159 ld->ld_errno = LDAP_SERVER_DOWN;
160 ldap_free_request( ld, lr );
161 ldap_free_connection( ld, lc, 0, 0 );
165 if ( lr->lr_parent == NULL ) {
166 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
167 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
169 lr->lr_status = LDAP_REQST_INPROGRESS;
171 /* sent -- waiting for a response */
172 ldap_mark_select_read( ld, lc->lconn_sb );
178 ldap_send_server_request(
182 LDAPRequest *parentreq,
183 LDAPURLDesc **srvlist,
190 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
193 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
196 if ( srvlist == NULL ) {
199 lc = find_connection( ld, *srvlist, 1 );
201 if ( (bind != NULL) && (parentreq != NULL) ) {
202 /* Remember the bind in the parent */
204 ++parentreq->lr_outrefcnt;
206 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
211 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
213 if ( ld->ld_errno == LDAP_SUCCESS ) {
214 ld->ld_errno = LDAP_SERVER_DOWN;
217 /* Forget about the bind */
218 --parentreq->lr_outrefcnt;
223 use_connection( ld, lc );
225 /* If we still have an incomplete write, try to finish it before
226 * dealing with the new request. If we don't finish here, return
227 * LDAP_BUSY and let the caller retry later. We only allow a single
228 * request to be in WRITING state.
231 if ( ld->ld_requests &&
232 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
233 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
239 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
241 ld->ld_errno = LDAP_NO_MEMORY;
242 ldap_free_connection( ld, lc, 0, 0 );
245 /* Forget about the bind */
246 --parentreq->lr_outrefcnt;
250 lr->lr_msgid = msgid;
251 lr->lr_status = LDAP_REQST_INPROGRESS;
252 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
255 if ( parentreq != NULL ) { /* sub-request */
257 /* Increment if we didn't do it before the bind */
258 ++parentreq->lr_outrefcnt;
260 lr->lr_origid = parentreq->lr_origid;
261 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
262 lr->lr_parent = parentreq;
263 lr->lr_refnext = parentreq->lr_child;
264 parentreq->lr_child = lr;
265 } else { /* original request */
266 lr->lr_origid = lr->lr_msgid;
269 /* Extract requestDN for future reference */
271 BerElement tmpber = *ber;
275 ber_reset( &tmpber, 1 );
276 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
279 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
281 case LDAP_REQ_DELETE:
284 rtag = ber_scanf( &tmpber, "{" /*}*/ );
285 case LDAP_REQ_ABANDON:
288 if ( tag != LDAP_REQ_ABANDON ) {
289 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
290 lr->lr_dn.bv_val = tmpber.ber_ptr;
295 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
296 lr->lr_next->lr_prev = lr;
298 ld->ld_requests = lr;
300 ld->ld_errno = LDAP_SUCCESS;
301 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
309 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
310 int connect, LDAPreqinfo *bind )
314 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
315 use_ldsb, connect, (bind != NULL) );
317 * make a new LDAP server connection
318 * XXX open connection synchronously for now
320 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
322 ld->ld_errno = LDAP_NO_MEMORY;
327 assert( ld->ld_sb != NULL );
328 lc->lconn_sb = ld->ld_sb;
331 lc->lconn_sb = ber_sockbuf_alloc();
332 if ( lc->lconn_sb == NULL ) {
333 LDAP_FREE( (char *)lc );
334 ld->ld_errno = LDAP_NO_MEMORY;
340 LDAPURLDesc **srvp, *srv = NULL;
342 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
343 if ( ldap_int_open_connection( ld, lc, *srvp, 0 ) != -1 )
347 if ( ld->ld_urllist_proc ) {
348 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
357 ber_sockbuf_free( lc->lconn_sb );
359 LDAP_FREE( (char *)lc );
360 ld->ld_errno = LDAP_SERVER_DOWN;
364 lc->lconn_server = ldap_url_dup( srv );
367 lc->lconn_status = LDAP_CONNST_CONNECTED;
368 #ifdef LDAP_R_COMPILE
369 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
371 lc->lconn_next = ld->ld_conns;
373 #ifdef LDAP_R_COMPILE
374 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
377 if ( bind != NULL ) {
379 LDAPConn *savedefconn;
381 /* Set flag to prevent additional referrals
382 * from being processed on this
383 * connection until the bind has completed
385 lc->lconn_rebind_inprogress = 1;
386 /* V3 rebind function */
387 if ( ld->ld_rebind_proc != NULL) {
388 LDAPURLDesc *srvfunc;
390 srvfunc = ldap_url_dup( *srvlist );
391 if ( srvfunc == NULL ) {
392 ld->ld_errno = LDAP_NO_MEMORY;
395 savedefconn = ld->ld_defconn;
396 ++lc->lconn_refcnt; /* avoid premature free */
399 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
400 #ifdef LDAP_R_COMPILE
401 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
402 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
404 err = (*ld->ld_rebind_proc)( ld,
405 bind->ri_url, bind->ri_request, bind->ri_msgid,
406 ld->ld_rebind_params );
407 #ifdef LDAP_R_COMPILE
408 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
409 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
412 ld->ld_defconn = savedefconn;
417 ldap_free_connection( ld, lc, 1, 0 );
420 ldap_free_urldesc( srvfunc );
424 struct berval passwd = BER_BVNULL;
426 savedefconn = ld->ld_defconn;
427 ++lc->lconn_refcnt; /* avoid premature free */
430 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
431 #ifdef LDAP_R_COMPILE
432 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
433 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
435 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
436 NULL, NULL, &msgid );
437 if ( rc != LDAP_SUCCESS ) {
441 for ( err = 1; err > 0; ) {
442 struct timeval tv = { 0, 100000 };
443 LDAPMessage *res = NULL;
445 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
451 #ifdef LDAP_R_COMPILE
452 ldap_pvt_thread_yield();
457 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
458 if ( rc != LDAP_SUCCESS ) {
461 } else if ( err != LDAP_SUCCESS ) {
464 /* else err == LDAP_SUCCESS == 0 */
472 #ifdef LDAP_R_COMPILE
473 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
474 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
476 ld->ld_defconn = savedefconn;
480 ldap_free_connection( ld, lc, 1, 0 );
485 lc->lconn_rebind_inprogress = 0;
493 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
495 * return an existing connection (if any) to the server srv
496 * if "any" is non-zero, check for any server in the "srv" chain
500 LDAPURLDesc *lcu, *lsu;
501 int lcu_port, lsu_port;
504 #ifdef LDAP_R_COMPILE
505 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
507 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
508 lcu = lc->lconn_server;
509 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
512 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
513 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
516 if ( lsu_port == lcu_port
517 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
518 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
519 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
520 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
531 #ifdef LDAP_R_COMPILE
532 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
540 use_connection( LDAP *ld, LDAPConn *lc )
543 lc->lconn_lastused = time( NULL );
548 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
550 LDAPConn *tmplc, *prevlc;
552 Debug( LDAP_DEBUG_TRACE,
553 "ldap_free_connection %d %d\n",
556 if ( force || --lc->lconn_refcnt <= 0 ) {
557 /* remove from connections list first */
558 #ifdef LDAP_R_COMPILE
559 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
562 for ( prevlc = NULL, tmplc = ld->ld_conns;
564 tmplc = tmplc->lconn_next )
567 if ( prevlc == NULL ) {
568 ld->ld_conns = tmplc->lconn_next;
570 prevlc->lconn_next = tmplc->lconn_next;
576 #ifdef LDAP_R_COMPILE
577 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
580 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
581 ldap_mark_select_clear( ld, lc->lconn_sb );
583 ldap_send_unbind( ld, lc->lconn_sb,
588 if ( lc->lconn_ber != NULL ) {
589 ber_free( lc->lconn_ber, 1 );
592 ldap_int_sasl_close( ld, lc );
594 ldap_free_urllist( lc->lconn_server );
595 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
596 if ( lc->lconn_krbinstance != NULL ) {
597 LDAP_FREE( lc->lconn_krbinstance );
601 /* FIXME: is this at all possible? */
605 for ( lr = ld->ld_requests; lr; ) {
606 LDAPRequest *lr_next = lr->lr_next;
608 if ( lr->lr_conn == lc ) {
609 ldap_free_request_int( ld, lr );
616 if ( lc->lconn_sb != ld->ld_sb ) {
617 ber_sockbuf_free( lc->lconn_sb );
620 if ( lc->lconn_rebind_queue != NULL) {
622 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
623 LDAP_VFREE( lc->lconn_rebind_queue[i] );
625 LDAP_FREE( lc->lconn_rebind_queue );
630 Debug( LDAP_DEBUG_TRACE,
631 "ldap_free_connection: actually freed\n",
635 lc->lconn_lastused = time( NULL );
636 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
637 lc->lconn_refcnt, 0, 0 );
644 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
649 fprintf( stderr, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "" );
650 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
651 if ( lc->lconn_server != NULL ) {
652 fprintf( stderr, "* host: %s port: %d%s\n",
653 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
654 : lc->lconn_server->lud_host,
655 lc->lconn_server->lud_port, ( lc->lconn_sb ==
656 ld->ld_sb ) ? " (default)" : "" );
658 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
659 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
660 "NeedSocket" : ( lc->lconn_status ==
661 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
662 fprintf( stderr, " last used: %s",
663 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
664 if( lc->lconn_rebind_inprogress ) {
665 fprintf( stderr, " rebind in progress\n");
666 if( lc->lconn_rebind_queue != NULL) {
668 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
670 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
671 fprintf( stderr, " queue %d entry %d - %s\n",
672 i, j, lc->lconn_rebind_queue[i][j]);
676 fprintf( stderr, " queue is empty\n");
679 fprintf(stderr, "\n");
688 ldap_dump_requests_and_responses( LDAP *ld )
693 #ifdef LDAP_R_COMPILE
694 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
696 fprintf( stderr, "** ld %p Outstanding Requests:\n", (void *)ld );
697 if (( lr = ld->ld_requests ) == NULL ) {
698 fprintf( stderr, " Empty\n" );
700 for ( ; lr != NULL; lr = lr->lr_next ) {
701 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
702 lr->lr_msgid, lr->lr_origid,
703 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
704 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
705 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
706 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
707 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
709 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
710 lr->lr_outrefcnt, lr->lr_parentcnt );
712 #ifdef LDAP_R_COMPILE
713 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
715 fprintf( stderr, "** ld %p Response Queue:\n", (void *)ld );
716 if (( lm = ld->ld_responses ) == NULL ) {
717 fprintf( stderr, " Empty\n" );
719 for ( ; lm != NULL; lm = lm->lm_next ) {
720 fprintf( stderr, " * msgid %d, type %lu\n",
721 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
722 if (( l = lm->lm_chain ) != NULL ) {
723 fprintf( stderr, " chained responses:\n" );
724 for ( ; l != NULL; l = l->lm_chain ) {
726 " * msgid %d, type %lu\n",
728 (unsigned long) l->lm_msgtype );
733 #endif /* LDAP_DEBUG */
736 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
738 if ( lr->lr_prev == NULL ) {
739 /* free'ing the first request? */
740 assert( ld->ld_requests == lr );
741 ld->ld_requests = lr->lr_next;
744 lr->lr_prev->lr_next = lr->lr_next;
747 if ( lr->lr_next != NULL ) {
748 lr->lr_next->lr_prev = lr->lr_prev;
751 if ( lr->lr_ber != NULL ) {
752 ber_free( lr->lr_ber, 1 );
756 if ( lr->lr_res_error != NULL ) {
757 LDAP_FREE( lr->lr_res_error );
758 lr->lr_res_error = NULL;
761 if ( lr->lr_res_matched != NULL ) {
762 LDAP_FREE( lr->lr_res_matched );
763 lr->lr_res_matched = NULL;
770 ldap_free_request( LDAP *ld, LDAPRequest *lr )
772 LDAPRequest **ttmplr;
773 #ifdef LDAP_R_COMPILE
774 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
777 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
778 lr->lr_origid, lr->lr_msgid, 0 );
780 /* free all referrals (child requests) */
781 while ( lr->lr_child )
782 ldap_free_request( ld, lr->lr_child );
784 if ( lr->lr_parent != NULL ) {
785 --lr->lr_parent->lr_outrefcnt;
786 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
788 *ttmplr = lr->lr_refnext;
790 ldap_free_request_int( ld, lr );
794 * call first time with *cntp = -1
795 * when returns *cntp == -1, no referrals are left
797 * NOTE: may replace *refsp, or shuffle the contents
798 * of the original array.
800 static int ldap_int_nextref(
806 assert( refsp != NULL );
807 assert( *refsp != NULL );
808 assert( cntp != NULL );
817 if ( (*refsp)[ *cntp ] == NULL ) {
828 * (IN) ld = LDAP connection handle
829 * (IN) lr = LDAP Request structure
830 * (IN) refs = array of pointers to referral strings that we will chase
831 * The array will be free'd by this function when no longer needed
832 * (IN) sref != 0 if following search reference
833 * (OUT) errstrp = Place to return a string of referrals which could not be followed
834 * (OUT) hadrefp = 1 if sucessfully followed referral
836 * Return value - number of referrals followed
839 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
842 int unfollowedcnt = 0;
843 LDAPRequest *origreq;
844 LDAPURLDesc *srv = NULL;
846 char **refarray = NULL;
848 int rc, count, i, j, id;
851 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
854 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
859 /* If no referrals in array, return */
860 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
865 /* Check for hop limit exceeded */
866 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
867 Debug( LDAP_DEBUG_ANY,
868 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
869 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
874 /* find original request */
876 origreq->lr_parent != NULL;
877 origreq = origreq->lr_parent )
885 if ( ld->ld_nextref_proc == NULL ) {
886 ld->ld_nextref_proc = ldap_int_nextref;
889 /* parse out & follow referrals */
891 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
893 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
896 /* Parse the referral URL */
897 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
898 if ( rc != LDAP_URL_SUCCESS ) {
899 /* ldap_url_parse_ext() returns LDAP_URL_* errors
900 * which do not map on API errors */
901 ld->ld_errno = LDAP_PARAM_ERROR;
906 if( srv->lud_crit_exts ) {
907 /* we do not support any extensions */
908 ld->ld_errno = LDAP_NOT_SUPPORTED;
913 /* check connection for re-bind in progress */
914 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
915 /* See if we've already requested this DN with this conn */
918 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
919 for ( lp = origreq; lp; ) {
920 if ( lp->lr_conn == lc
921 && len == lp->lr_dn.bv_len
923 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
928 if ( lp == origreq ) {
935 ldap_free_urllist( srv );
937 ld->ld_errno = LDAP_CLIENT_LOOP;
942 if ( lc->lconn_rebind_inprogress ) {
943 /* We are already chasing a referral or search reference and a
944 * bind on that connection is in progress. We must queue
945 * referrals on that connection, so we don't get a request
946 * going out before the bind operation completes. This happens
947 * if two search references come in one behind the other
948 * for the same server with different contexts.
950 Debug( LDAP_DEBUG_TRACE,
951 "ldap_chase_v3referrals: queue referral \"%s\"\n",
953 if( lc->lconn_rebind_queue == NULL ) {
954 /* Create a referral list */
955 lc->lconn_rebind_queue =
956 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
958 if( lc->lconn_rebind_queue == NULL) {
959 ld->ld_errno = LDAP_NO_MEMORY;
964 lc->lconn_rebind_queue[0] = refarray;
965 lc->lconn_rebind_queue[1] = NULL;
969 /* Count how many referral arrays we already have */
970 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
974 /* Add the new referral to the list */
975 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
976 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
978 if( lc->lconn_rebind_queue == NULL ) {
979 ld->ld_errno = LDAP_NO_MEMORY;
983 lc->lconn_rebind_queue[j] = refarray;
984 lc->lconn_rebind_queue[j+1] = NULL;
988 /* We have queued the referral/reference, now just return */
991 count = 1; /* Pretend we already followed referral */
995 /* Re-encode the request with the new starting point of the search.
996 * Note: In the future we also need to replace the filter if one
997 * was provided with the search reference
1000 /* For references we don't want old dn if new dn empty */
1001 if ( sref && srv->lud_dn == NULL ) {
1002 srv->lud_dn = LDAP_STRDUP( "" );
1005 LDAP_NEXT_MSGID( ld, id );
1006 ber = re_encode_request( ld, origreq->lr_ber, id,
1007 sref, srv, &rinfo.ri_request );
1010 ld->ld_errno = LDAP_ENCODING_ERROR;
1015 Debug( LDAP_DEBUG_TRACE,
1016 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1017 lr->lr_msgid, refarray[i], 0);
1019 /* Send the new request to the server - may require a bind */
1020 rinfo.ri_msgid = origreq->lr_origid;
1021 rinfo.ri_url = refarray[i];
1022 #ifdef LDAP_R_COMPILE
1023 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1025 rc = ldap_send_server_request( ld, ber, id,
1026 origreq, &srv, NULL, &rinfo );
1027 #ifdef LDAP_R_COMPILE
1028 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1031 /* Failure, try next referral in the list */
1032 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1033 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1034 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1035 ldap_free_urllist( srv );
1037 ld->ld_errno = LDAP_REFERRAL;
1039 /* Success, no need to try this referral list further */
1044 /* check if there is a queue of referrals that came in during bind */
1046 lc = find_connection( ld, srv, 1 );
1048 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1054 if ( lc->lconn_rebind_queue != NULL ) {
1055 /* Release resources of previous list */
1056 LDAP_VFREE( refarray );
1058 ldap_free_urllist( srv );
1061 /* Pull entries off end of queue so list always null terminated */
1062 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1064 refarray = lc->lconn_rebind_queue[j - 1];
1065 lc->lconn_rebind_queue[j-1] = NULL;
1066 /* we pulled off last entry from queue, free queue */
1068 LDAP_FREE( lc->lconn_rebind_queue );
1069 lc->lconn_rebind_queue = NULL;
1071 /* restart the loop the with new referral list */
1075 break; /* referral followed, break out of for loop */
1077 } /* end for loop */
1079 LDAP_VFREE( refarray );
1080 ldap_free_urllist( srv );
1081 LDAP_FREE( *errstrp );
1085 LDAP_FREE( unfollowed );
1088 *errstrp = unfollowed;
1094 * XXX merging of errors in this routine needs to be improved
1097 ldap_chase_referrals( LDAP *ld,
1105 char *p, *ref, *unfollowed;
1106 LDAPRequest *origreq;
1112 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1114 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1117 if ( *errstrp == NULL ) {
1121 len = strlen( *errstrp );
1122 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1123 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1125 p += LDAP_REF_STR_LEN;
1130 if ( len < LDAP_REF_STR_LEN ) {
1134 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1135 Debug( LDAP_DEBUG_ANY,
1136 "more than %d referral hops (dropping)\n",
1137 ld->ld_refhoplimit, 0, 0 );
1138 /* XXX report as error in ld->ld_errno? */
1142 /* find original request */
1143 for ( origreq = lr; origreq->lr_parent != NULL;
1144 origreq = origreq->lr_parent ) {
1151 /* parse out & follow referrals */
1152 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1153 p = strchr( ref, '\n' );
1158 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1159 if ( rc != LDAP_URL_SUCCESS ) {
1160 Debug( LDAP_DEBUG_TRACE,
1161 "ignoring %s referral <%s>\n",
1162 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1163 rc = ldap_append_referral( ld, &unfollowed, ref );
1168 Debug( LDAP_DEBUG_TRACE,
1169 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1173 /* See if we've already been here */
1174 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1177 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1178 for ( lp = lr; lp; lp = lp->lr_parent ) {
1179 if ( lp->lr_conn == lc
1180 && len == lp->lr_dn.bv_len )
1182 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1189 ldap_free_urllist(srv);
1190 ld->ld_errno = LDAP_CLIENT_LOOP;
1196 LDAP_NEXT_MSGID( ld, id );
1197 ber = re_encode_request( ld, origreq->lr_ber,
1198 id, sref, srv, &rinfo.ri_request );
1200 if ( ber == NULL ) {
1204 /* copy the complete referral for rebind process */
1205 rinfo.ri_url = LDAP_STRDUP( ref );
1207 rinfo.ri_msgid = origreq->lr_origid;
1209 #ifdef LDAP_R_COMPILE
1210 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1212 rc = ldap_send_server_request( ld, ber, id,
1213 lr, &srv, NULL, &rinfo );
1214 #ifdef LDAP_R_COMPILE
1215 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1218 LDAP_FREE( rinfo.ri_url );
1223 Debug( LDAP_DEBUG_ANY,
1224 "Unable to chase referral \"%s\" (%d: %s)\n",
1225 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1226 rc = ldap_append_referral( ld, &unfollowed, ref );
1229 ldap_free_urllist(srv);
1232 LDAP_FREE( *errstrp );
1233 *errstrp = unfollowed;
1235 return(( rc == 0 ) ? count : rc );
1240 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1244 if ( *referralsp == NULL ) {
1246 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1250 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1251 strlen( *referralsp ) + strlen( s ) + 2 );
1254 if ( *referralsp == NULL ) {
1255 ld->ld_errno = LDAP_NO_MEMORY;
1260 strcpy( *referralsp, LDAP_REF_STR );
1262 strcat( *referralsp, "\n" );
1264 strcat( *referralsp, s );
1272 re_encode_request( LDAP *ld,
1273 BerElement *origber,
1280 * XXX this routine knows way too much about how the lber library works!
1288 BerElement tmpber, *ber;
1291 Debug( LDAP_DEBUG_TRACE,
1292 "re_encode_request: new msgid %ld, new dn <%s>\n",
1294 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1299 * all LDAP requests are sequences that start with a message id.
1300 * For all except delete, this is followed by a sequence that is
1301 * tagged with the operation code. For delete, the provided DN
1302 * is not wrapped by a sequence.
1304 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1306 if ( rtag == LBER_ERROR ) {
1307 ld->ld_errno = LDAP_DECODING_ERROR;
1312 if ( tag == LDAP_REQ_BIND ) {
1313 /* bind requests have a version number before the DN & other stuff */
1314 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1316 } else if ( tag == LDAP_REQ_DELETE ) {
1317 /* delete requests don't have a DN wrapping sequence */
1318 rtag = ber_scanf( &tmpber, "m", &dn );
1320 } else if ( tag == LDAP_REQ_SEARCH ) {
1321 /* search requests need to be re-scope-ed */
1322 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1324 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1325 /* use the scope provided in reference */
1326 scope = srv->lud_scope;
1328 } else if ( sref ) {
1329 /* use scope implied by previous operation
1332 * subtree -> subtree
1333 * subordinate -> subtree
1337 case LDAP_SCOPE_BASE:
1338 case LDAP_SCOPE_ONELEVEL:
1339 scope = LDAP_SCOPE_BASE;
1341 case LDAP_SCOPE_SUBTREE:
1342 case LDAP_SCOPE_SUBORDINATE:
1343 scope = LDAP_SCOPE_SUBTREE;
1349 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1352 if( rtag == LBER_ERROR ) {
1353 ld->ld_errno = LDAP_DECODING_ERROR;
1357 /* restore character zero'd out by ber_scanf*/
1358 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1360 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1364 if ( srv->lud_dn ) {
1365 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1368 if ( tag == LDAP_REQ_BIND ) {
1369 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1370 } else if ( tag == LDAP_REQ_DELETE ) {
1371 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1372 } else if ( tag == LDAP_REQ_SEARCH ) {
1373 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1375 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1379 ld->ld_errno = LDAP_ENCODING_ERROR;
1384 if ( tag != LDAP_REQ_DELETE && (
1385 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1386 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1387 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1389 ld->ld_errno = LDAP_ENCODING_ERROR;
1395 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1396 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1398 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1400 #endif /* LDAP_DEBUG */
1402 *type = tag; /* return request type */
1408 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1412 #ifdef LDAP_R_COMPILE
1413 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1415 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1416 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1417 continue; /* Skip completed requests */
1419 if ( msgid == lr->lr_msgid ) {
1423 #ifdef LDAP_R_COMPILE
1424 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );