2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 ));
60 re_encode_request( LDAP *ld,
68 ldap_alloc_ber_with_options( LDAP *ld )
72 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
73 ld->ld_errno = LDAP_NO_MEMORY;
81 ldap_set_ber_options( LDAP *ld, BerElement *ber )
83 ber->ber_options = ld->ld_lberoptions;
88 ldap_send_initial_request(
98 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
100 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
101 /* not connected yet */
102 int rc = ldap_open_defconn( ld );
109 Debug( LDAP_DEBUG_TRACE,
110 "ldap_open_defconn: successful\n",
116 * use of DNS is turned off or this is an X.500 DN...
117 * use our default connection
122 #ifdef LDAP_CONNECTIONLESS
123 if (LDAP_IS_UDP(ld)) {
124 if (msgtype == LDAP_REQ_BIND) {
125 if (ld->ld_options.ldo_cldapdn)
126 ldap_memfree(ld->ld_options.ldo_cldapdn);
127 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
130 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
131 return LDAP_PARAM_ERROR;
134 #ifdef LDAP_R_COMPILE
135 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
137 rc = ldap_send_server_request( ld, ber, msgid, NULL,
138 servers, NULL, NULL );
139 #ifdef LDAP_R_COMPILE
140 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
143 ldap_free_urllist(servers);
149 ldap_int_flush_request(
153 LDAPConn *lc = lr->lr_conn;
155 if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
156 if ( errno == EAGAIN ) {
157 /* need to continue write later */
158 lr->lr_status = LDAP_REQST_WRITING;
159 ldap_mark_select_write( ld, lc->lconn_sb );
160 ld->ld_errno = LDAP_BUSY;
163 ld->ld_errno = LDAP_SERVER_DOWN;
164 ldap_free_request( ld, lr );
165 ldap_free_connection( ld, lc, 0, 0 );
169 if ( lr->lr_parent == NULL ) {
170 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
171 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
173 lr->lr_status = LDAP_REQST_INPROGRESS;
175 /* sent -- waiting for a response */
176 ldap_mark_select_read( ld, lc->lconn_sb );
182 ldap_send_server_request(
186 LDAPRequest *parentreq,
187 LDAPURLDesc *srvlist,
194 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
197 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
200 if ( srvlist == NULL ) {
203 if (( lc = find_connection( ld, srvlist, 1 )) ==
205 if ( (bind != NULL) && (parentreq != NULL) ) {
206 /* Remember the bind in the parent */
208 ++parentreq->lr_outrefcnt;
210 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
215 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
217 if ( ld->ld_errno == LDAP_SUCCESS ) {
218 ld->ld_errno = LDAP_SERVER_DOWN;
221 /* Forget about the bind */
222 --parentreq->lr_outrefcnt;
227 use_connection( ld, lc );
229 /* If we still have an incomplete write, try to finish it before
230 * dealing with the new request. If we don't finish here, return
231 * LDAP_BUSY and let the caller retry later. We only allow a single
232 * request to be in WRITING state.
235 if ( ld->ld_requests &&
236 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
237 ldap_int_flush_request( ld, ld->ld_requests ) < 0 ) {
242 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
244 ld->ld_errno = LDAP_NO_MEMORY;
245 ldap_free_connection( ld, lc, 0, 0 );
248 /* Forget about the bind */
249 --parentreq->lr_outrefcnt;
253 lr->lr_msgid = msgid;
254 lr->lr_status = LDAP_REQST_INPROGRESS;
255 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
258 if ( parentreq != NULL ) { /* sub-request */
260 /* Increment if we didn't do it before the bind */
261 ++parentreq->lr_outrefcnt;
263 lr->lr_origid = parentreq->lr_origid;
264 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
265 lr->lr_parent = parentreq;
266 lr->lr_refnext = parentreq->lr_child;
267 parentreq->lr_child = lr;
268 } else { /* original request */
269 lr->lr_origid = lr->lr_msgid;
272 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
273 lr->lr_next->lr_prev = lr;
275 ld->ld_requests = lr;
278 ld->ld_errno = LDAP_SUCCESS;
279 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
287 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
288 int connect, LDAPreqinfo *bind )
294 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
296 * make a new LDAP server connection
297 * XXX open connection synchronously for now
299 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
300 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
302 LDAP_FREE( (char *)lc );
304 ld->ld_errno = LDAP_NO_MEMORY;
308 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
311 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
312 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
319 ber_sockbuf_free( lc->lconn_sb );
321 LDAP_FREE( (char *)lc );
322 ld->ld_errno = LDAP_SERVER_DOWN;
326 lc->lconn_server = ldap_url_dup(srv);
329 lc->lconn_status = LDAP_CONNST_CONNECTED;
330 lc->lconn_next = ld->ld_conns;
334 * XXX for now, we always do a synchronous bind. This will have
335 * to change in the long run...
339 LDAPConn *savedefconn;
341 /* Set flag to prevent additional referrals from being processed on this
342 * connection until the bind has completed
344 lc->lconn_rebind_inprogress = 1;
345 /* V3 rebind function */
346 if ( ld->ld_rebind_proc != NULL) {
347 LDAPURLDesc *srvfunc;
348 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
349 ld->ld_errno = LDAP_NO_MEMORY;
352 savedefconn = ld->ld_defconn;
353 ++lc->lconn_refcnt; /* avoid premature free */
356 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
357 #ifdef LDAP_R_COMPILE
358 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
359 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
361 err = (*ld->ld_rebind_proc)( ld,
362 bind->ri_url, bind->ri_request, bind->ri_msgid,
363 ld->ld_rebind_params );
364 #ifdef LDAP_R_COMPILE
365 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
366 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
369 ld->ld_defconn = savedefconn;
374 ldap_free_connection( ld, lc, 1, 0 );
377 ldap_free_urldesc( srvfunc);
380 savedefconn = ld->ld_defconn;
381 ++lc->lconn_refcnt; /* avoid premature free */
384 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
385 #ifdef LDAP_R_COMPILE
386 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
387 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
389 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
392 #ifdef LDAP_R_COMPILE
393 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
394 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
396 ld->ld_defconn = savedefconn;
400 ldap_free_connection( ld, lc, 1, 0 );
405 lc->lconn_rebind_inprogress = 0;
413 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
415 * return an existing connection (if any) to the server srv
416 * if "any" is non-zero, check for any server in the "srv" chain
420 LDAPURLDesc *lcu, *lsu;
421 int lcu_port, lsu_port;
423 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
424 lcu = lc->lconn_server;
425 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
428 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
429 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
432 if ( strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
433 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
434 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
435 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0
436 && lsu_port == lcu_port )
451 use_connection( LDAP *ld, LDAPConn *lc )
454 lc->lconn_lastused = time( NULL );
459 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
461 LDAPConn *tmplc, *prevlc;
463 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
465 if ( force || --lc->lconn_refcnt <= 0 ) {
466 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
467 ldap_mark_select_clear( ld, lc->lconn_sb );
469 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
473 if( lc->lconn_ber != NULL ) {
474 ber_free( lc->lconn_ber, 1 );
477 ldap_int_sasl_close( ld, lc );
480 for ( tmplc = ld->ld_conns; tmplc != NULL;
481 tmplc = tmplc->lconn_next ) {
483 if ( prevlc == NULL ) {
484 ld->ld_conns = tmplc->lconn_next;
486 prevlc->lconn_next = tmplc->lconn_next;
492 ldap_free_urllist( lc->lconn_server );
493 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
494 if ( lc->lconn_krbinstance != NULL ) {
495 LDAP_FREE( lc->lconn_krbinstance );
498 if ( lc->lconn_sb != ld->ld_sb ) {
499 ber_sockbuf_free( lc->lconn_sb );
501 if( lc->lconn_rebind_queue != NULL) {
503 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
504 LDAP_VFREE(lc->lconn_rebind_queue[i]);
506 LDAP_FREE( lc->lconn_rebind_queue);
509 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
512 lc->lconn_lastused = time( NULL );
513 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
514 lc->lconn_refcnt, 0, 0 );
521 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
526 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
527 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
528 if ( lc->lconn_server != NULL ) {
529 fprintf( stderr, "* host: %s port: %d%s\n",
530 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
531 : lc->lconn_server->lud_host,
532 lc->lconn_server->lud_port, ( lc->lconn_sb ==
533 ld->ld_sb ) ? " (default)" : "" );
535 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
536 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
537 "NeedSocket" : ( lc->lconn_status ==
538 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
539 fprintf( stderr, " last used: %s",
540 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
541 if( lc->lconn_rebind_inprogress ) {
542 fprintf( stderr, " rebind in progress\n");
543 if( lc->lconn_rebind_queue != NULL) {
545 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
547 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
548 fprintf( stderr, " queue %d entry %d - %s\n",
549 i, j, lc->lconn_rebind_queue[i][j]);
553 fprintf( stderr, " queue is empty\n");
556 fprintf(stderr, "\n");
565 ldap_dump_requests_and_responses( LDAP *ld )
570 #ifdef LDAP_R_COMPILE
571 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
573 fprintf( stderr, "** Outstanding Requests:\n" );
574 if (( lr = ld->ld_requests ) == NULL ) {
575 fprintf( stderr, " Empty\n" );
577 for ( ; lr != NULL; lr = lr->lr_next ) {
578 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
579 lr->lr_msgid, lr->lr_origid,
580 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
581 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
582 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
583 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
584 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
585 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
586 lr->lr_outrefcnt, lr->lr_parentcnt );
588 #ifdef LDAP_R_COMPILE
589 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
591 fprintf( stderr, "** Response Queue:\n" );
592 if (( lm = ld->ld_responses ) == NULL ) {
593 fprintf( stderr, " Empty\n" );
595 for ( ; lm != NULL; lm = lm->lm_next ) {
596 fprintf( stderr, " * msgid %d, type %lu\n",
597 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
598 if (( l = lm->lm_chain ) != NULL ) {
599 fprintf( stderr, " chained responses:\n" );
600 for ( ; l != NULL; l = l->lm_chain ) {
602 " * msgid %d, type %lu\n",
604 (unsigned long) l->lm_msgtype );
609 #endif /* LDAP_DEBUG */
612 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
614 if ( lr->lr_prev == NULL ) {
615 /* free'ing the first request? */
616 assert( ld->ld_requests == lr );
617 ld->ld_requests = lr->lr_next;
620 lr->lr_prev->lr_next = lr->lr_next;
623 if ( lr->lr_next != NULL ) {
624 lr->lr_next->lr_prev = lr->lr_prev;
627 if ( lr->lr_ber != NULL ) {
628 ber_free( lr->lr_ber, 1 );
631 if ( lr->lr_res_error != NULL ) {
632 LDAP_FREE( lr->lr_res_error );
635 if ( lr->lr_res_matched != NULL ) {
636 LDAP_FREE( lr->lr_res_matched );
643 ldap_free_request( LDAP *ld, LDAPRequest *lr )
645 LDAPRequest **ttmplr;
647 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
648 lr->lr_origid, lr->lr_msgid, 0 );
650 /* free all referrals (child requests) */
651 while ( lr->lr_child )
652 ldap_free_request( ld, lr->lr_child );
654 if ( lr->lr_parent != NULL ) {
655 --lr->lr_parent->lr_outrefcnt;
656 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
658 *ttmplr = lr->lr_refnext;
660 ldap_free_request_int( ld, lr );
664 * call first time with *cntp = -1
665 * when returns *cntp == -1, no referrals are left
667 * NOTE: may replace *refsp, or shuffle the contents
668 * of the original array.
670 static int ldap_int_nextref(
676 assert( refsp != NULL );
677 assert( *refsp != NULL );
678 assert( cntp != NULL );
687 if ( (*refsp)[ *cntp ] == NULL ) {
698 * (IN) ld = LDAP connection handle
699 * (IN) lr = LDAP Request structure
700 * (IN) refs = array of pointers to referral strings that we will chase
701 * The array will be free'd by this function when no longer needed
702 * (IN) sref != 0 if following search reference
703 * (OUT) errstrp = Place to return a string of referrals which could not be followed
704 * (OUT) hadrefp = 1 if sucessfully followed referral
706 * Return value - number of referrals followed
709 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
712 int unfollowedcnt = 0;
713 LDAPRequest *origreq;
714 LDAPURLDesc *srv = NULL;
716 char **refarray = NULL;
718 int rc, count, i, j, id;
721 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
724 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
729 /* If no referrals in array, return */
730 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
735 /* Check for hop limit exceeded */
736 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
737 Debug( LDAP_DEBUG_ANY,
738 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
739 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
744 /* find original request */
746 origreq->lr_parent != NULL;
747 origreq = origreq->lr_parent )
755 if ( ld->ld_nextref_proc == NULL ) {
756 ld->ld_nextref_proc = ldap_int_nextref;
759 /* parse out & follow referrals */
761 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
763 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
766 /* Parse the referral URL */
767 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
773 if( srv->lud_crit_exts ) {
774 /* we do not support any extensions */
775 ld->ld_errno = LDAP_NOT_SUPPORTED;
780 /* treat ldap://hostpart and ldap://hostpart/ the same */
781 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
782 LDAP_FREE( srv->lud_dn );
786 /* check connection for re-bind in progress */
787 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
788 if( lc->lconn_rebind_inprogress) {
789 /* We are already chasing a referral or search reference and a
790 * bind on that connection is in progress. We must queue
791 * referrals on that connection, so we don't get a request
792 * going out before the bind operation completes. This happens
793 * if two search references come in one behind the other
794 * for the same server with different contexts.
796 Debug( LDAP_DEBUG_TRACE,
797 "ldap_chase_v3referrals: queue referral \"%s\"\n",
799 if( lc->lconn_rebind_queue == NULL ) {
800 /* Create a referral list */
801 lc->lconn_rebind_queue =
802 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
804 if( lc->lconn_rebind_queue == NULL) {
805 ld->ld_errno = LDAP_NO_MEMORY;
810 lc->lconn_rebind_queue[0] = refarray;
811 lc->lconn_rebind_queue[1] = NULL;
815 /* Count how many referral arrays we already have */
816 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
820 /* Add the new referral to the list */
821 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
822 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
824 if( lc->lconn_rebind_queue == NULL ) {
825 ld->ld_errno = LDAP_NO_MEMORY;
829 lc->lconn_rebind_queue[j] = refarray;
830 lc->lconn_rebind_queue[j+1] = NULL;
834 /* We have queued the referral/reference, now just return */
837 count = 1; /* Pretend we already followed referral */
841 /* Re-encode the request with the new starting point of the search.
842 * Note: In the future we also need to replace the filter if one
843 * was provided with the search reference
846 /* For references we don't want old dn if new dn empty */
847 if ( sref && srv->lud_dn == NULL ) {
848 srv->lud_dn = LDAP_STRDUP( "" );
851 LDAP_NEXT_MSGID( ld, id );
852 ber = re_encode_request( ld, origreq->lr_ber, id,
853 sref, srv, &rinfo.ri_request );
856 ld->ld_errno = LDAP_ENCODING_ERROR;
861 Debug( LDAP_DEBUG_TRACE,
862 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
863 lr->lr_msgid, refarray[i], 0);
865 /* Send the new request to the server - may require a bind */
866 rinfo.ri_msgid = origreq->lr_origid;
867 rinfo.ri_url = refarray[i];
868 #ifdef LDAP_R_COMPILE
869 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
871 rc = ldap_send_server_request( ld, ber, id,
872 origreq, srv, NULL, &rinfo );
873 #ifdef LDAP_R_COMPILE
874 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
877 /* Failure, try next referral in the list */
878 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
879 refarray[i], ldap_err2string( ld->ld_errno ), 0);
880 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
881 ldap_free_urllist(srv);
884 /* Success, no need to try this referral list further */
889 /* check if there is a queue of referrals that came in during bind */
891 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
892 ld->ld_errno = LDAP_OPERATIONS_ERROR;
898 if( lc->lconn_rebind_queue != NULL) {
899 /* Release resources of previous list */
900 LDAP_VFREE(refarray);
902 ldap_free_urllist(srv);
905 /* Pull entries off end of queue so list always null terminated */
906 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
909 refarray = lc->lconn_rebind_queue[j-1];
910 lc->lconn_rebind_queue[j-1] = NULL;
911 /* we pulled off last entry from queue, free queue */
913 LDAP_FREE( lc->lconn_rebind_queue);
914 lc->lconn_rebind_queue = NULL;
916 /* restart the loop the with new referral list */
920 break; /* referral followed, break out of for loop */
924 LDAP_VFREE(refarray);
925 ldap_free_urllist(srv);
926 LDAP_FREE( *errstrp );
930 LDAP_FREE( unfollowed );
933 ld->ld_errno = LDAP_REFERRAL;
934 *errstrp = unfollowed;
940 * XXX merging of errors in this routine needs to be improved
943 ldap_chase_referrals( LDAP *ld,
951 char *p, *ref, *unfollowed;
952 LDAPRequest *origreq;
957 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
959 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
962 if ( *errstrp == NULL ) {
966 len = strlen( *errstrp );
967 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
968 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
970 p += LDAP_REF_STR_LEN;
975 if ( len < LDAP_REF_STR_LEN ) {
979 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
980 Debug( LDAP_DEBUG_ANY,
981 "more than %d referral hops (dropping)\n",
982 ld->ld_refhoplimit, 0, 0 );
983 /* XXX report as error in ld->ld_errno? */
987 /* find original request */
988 for ( origreq = lr; origreq->lr_parent != NULL;
989 origreq = origreq->lr_parent ) {
996 /* parse out & follow referrals */
997 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
998 if (( p = strchr( ref, '\n' )) != NULL ) {
1004 rc = ldap_url_parse_ext( ref, &srv );
1006 if ( rc != LDAP_URL_SUCCESS ) {
1007 Debug( LDAP_DEBUG_TRACE,
1008 "ignoring unknown referral <%s>\n", ref, 0, 0 );
1009 rc = ldap_append_referral( ld, &unfollowed, ref );
1014 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1015 LDAP_FREE( srv->lud_dn );
1019 Debug( LDAP_DEBUG_TRACE,
1020 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1024 LDAP_NEXT_MSGID( ld, id );
1025 ber = re_encode_request( ld, origreq->lr_ber,
1026 id, sref, srv, &rinfo.ri_request );
1032 /* copy the complete referral for rebind process */
1033 rinfo.ri_url = LDAP_STRDUP( ref );
1035 rinfo.ri_msgid = origreq->lr_origid;
1037 #ifdef LDAP_R_COMPILE
1038 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1040 rc = ldap_send_server_request( ld, ber, id,
1041 lr, srv, NULL, &rinfo );
1042 #ifdef LDAP_R_COMPILE
1043 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1046 LDAP_FREE( rinfo.ri_url );
1051 Debug( LDAP_DEBUG_ANY,
1052 "Unable to chase referral (%s)\n",
1053 ldap_err2string( ld->ld_errno ), 0, 0 );
1054 rc = ldap_append_referral( ld, &unfollowed, ref );
1057 ldap_free_urllist(srv);
1060 LDAP_FREE( *errstrp );
1061 *errstrp = unfollowed;
1063 return(( rc == 0 ) ? count : rc );
1068 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1072 if ( *referralsp == NULL ) {
1074 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1078 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1079 strlen( *referralsp ) + strlen( s ) + 2 );
1082 if ( *referralsp == NULL ) {
1083 ld->ld_errno = LDAP_NO_MEMORY;
1088 strcpy( *referralsp, LDAP_REF_STR );
1090 strcat( *referralsp, "\n" );
1092 strcat( *referralsp, s );
1100 re_encode_request( LDAP *ld,
1101 BerElement *origber,
1108 * XXX this routine knows way too much about how the lber library works!
1116 BerElement tmpber, *ber;
1120 Debug( LDAP_DEBUG_TRACE,
1121 "re_encode_request: new msgid %ld, new dn <%s>\n",
1123 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1128 * all LDAP requests are sequences that start with a message id.
1129 * For all except delete, this is followed by a sequence that is
1130 * tagged with the operation code. For delete, the provided DN
1131 * is not wrapped by a sequence.
1133 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1135 if ( rtag == LBER_ERROR ) {
1136 ld->ld_errno = LDAP_DECODING_ERROR;
1141 if ( tag == LDAP_REQ_BIND ) {
1142 /* bind requests have a version number before the DN & other stuff */
1143 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1145 } else if ( tag == LDAP_REQ_DELETE ) {
1146 /* delete requests don't have a DN wrapping sequence */
1147 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1149 } else if ( tag == LDAP_REQ_SEARCH ) {
1150 /* search requests need to be re-scope-ed */
1151 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1153 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1154 /* use the scope provided in reference */
1155 scope = srv->lud_scope;
1157 } else if ( sref ) {
1158 /* use scope implied by previous operation
1161 * subtree -> subtree
1162 * subordinate -> subtree
1166 case LDAP_SCOPE_BASE:
1167 case LDAP_SCOPE_ONELEVEL:
1168 scope = LDAP_SCOPE_BASE;
1170 case LDAP_SCOPE_SUBTREE:
1171 #ifdef LDAP_SCOPE_SUBORDINATE
1172 case LDAP_SCOPE_SUBORDINATE:
1174 scope = LDAP_SCOPE_SUBTREE;
1180 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1183 if( rtag == LBER_ERROR ) {
1184 ld->ld_errno = LDAP_DECODING_ERROR;
1188 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1192 if ( srv->lud_dn == NULL ) {
1198 if ( tag == LDAP_REQ_BIND ) {
1199 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1200 } else if ( tag == LDAP_REQ_DELETE ) {
1201 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1202 } else if ( tag == LDAP_REQ_SEARCH ) {
1203 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1205 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1208 LDAP_FREE( orig_dn );
1211 ld->ld_errno = LDAP_ENCODING_ERROR;
1216 if ( tag != LDAP_REQ_DELETE && (
1217 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1218 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1219 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1221 ld->ld_errno = LDAP_ENCODING_ERROR;
1227 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1228 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1230 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1232 #endif /* LDAP_DEBUG */
1234 *type = tag; /* return request type */
1240 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1244 #ifdef LDAP_R_COMPILE
1245 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1247 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1248 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1249 continue; /* Skip completed requests */
1251 if ( msgid == lr->lr_msgid ) {
1255 #ifdef LDAP_R_COMPILE
1256 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );