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 ));
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(
99 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
101 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
102 /* not connected yet */
103 int rc = ldap_open_defconn( ld );
110 Debug( LDAP_DEBUG_TRACE,
111 "ldap_open_defconn: successful\n",
117 * use of DNS is turned off or this is an X.500 DN...
118 * use our default connection
123 #ifdef LDAP_CONNECTIONLESS
124 if (LDAP_IS_UDP(ld)) {
125 if (msgtype == LDAP_REQ_BIND) {
126 if (ld->ld_options.ldo_cldapdn)
127 ldap_memfree(ld->ld_options.ldo_cldapdn);
128 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
131 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
132 return LDAP_PARAM_ERROR;
135 #ifdef LDAP_R_COMPILE
136 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
138 rc = ldap_send_server_request( ld, ber, msgid, NULL,
139 servers, NULL, NULL );
140 #ifdef LDAP_R_COMPILE
141 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
144 ldap_free_urllist(servers);
150 ldap_int_flush_request(
154 LDAPConn *lc = lr->lr_conn;
156 if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
157 if ( errno == EAGAIN ) {
158 /* need to continue write later */
159 lr->lr_status = LDAP_REQST_WRITING;
160 ldap_mark_select_write( ld, lc->lconn_sb );
161 ld->ld_errno = LDAP_BUSY;
164 ld->ld_errno = LDAP_SERVER_DOWN;
165 ldap_free_request( ld, lr );
166 ldap_free_connection( ld, lc, 0, 0 );
170 if ( lr->lr_parent == NULL ) {
171 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
172 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
174 lr->lr_status = LDAP_REQST_INPROGRESS;
176 /* sent -- waiting for a response */
177 ldap_mark_select_read( ld, lc->lconn_sb );
183 ldap_send_server_request(
187 LDAPRequest *parentreq,
188 LDAPURLDesc *srvlist,
195 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
198 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
201 if ( srvlist == NULL ) {
204 lc = find_connection( ld, srvlist, 1 );
206 if ( (bind != NULL) && (parentreq != NULL) ) {
207 /* Remember the bind in the parent */
209 ++parentreq->lr_outrefcnt;
211 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
216 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
218 if ( ld->ld_errno == LDAP_SUCCESS ) {
219 ld->ld_errno = LDAP_SERVER_DOWN;
222 /* Forget about the bind */
223 --parentreq->lr_outrefcnt;
228 use_connection( ld, lc );
230 /* If we still have an incomplete write, try to finish it before
231 * dealing with the new request. If we don't finish here, return
232 * LDAP_BUSY and let the caller retry later. We only allow a single
233 * request to be in WRITING state.
236 if ( ld->ld_requests &&
237 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
238 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
244 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
246 ld->ld_errno = LDAP_NO_MEMORY;
247 ldap_free_connection( ld, lc, 0, 0 );
250 /* Forget about the bind */
251 --parentreq->lr_outrefcnt;
255 lr->lr_msgid = msgid;
256 lr->lr_status = LDAP_REQST_INPROGRESS;
257 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
260 if ( parentreq != NULL ) { /* sub-request */
262 /* Increment if we didn't do it before the bind */
263 ++parentreq->lr_outrefcnt;
265 lr->lr_origid = parentreq->lr_origid;
266 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
267 lr->lr_parent = parentreq;
268 lr->lr_refnext = parentreq->lr_child;
269 parentreq->lr_child = lr;
270 } else { /* original request */
271 lr->lr_origid = lr->lr_msgid;
275 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
276 lr->lr_next->lr_prev = lr;
278 ld->ld_requests = lr;
280 ld->ld_errno = LDAP_SUCCESS;
281 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
289 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
290 int connect, LDAPreqinfo *bind )
295 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
296 use_ldsb, connect, (bind != NULL) );
298 * make a new LDAP server connection
299 * XXX open connection synchronously for now
301 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
303 ld->ld_errno = LDAP_NO_MEMORY;
308 assert( ld->ld_sb != NULL );
309 lc->lconn_sb = ld->ld_sb;
312 lc->lconn_sb = ber_sockbuf_alloc();
313 if ( lc->lconn_sb == NULL ) {
314 LDAP_FREE( (char *)lc );
315 ld->ld_errno = LDAP_NO_MEMORY;
321 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
322 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 )
330 ber_sockbuf_free( lc->lconn_sb );
332 LDAP_FREE( (char *)lc );
333 ld->ld_errno = LDAP_SERVER_DOWN;
337 lc->lconn_server = ldap_url_dup( srv );
340 lc->lconn_status = LDAP_CONNST_CONNECTED;
341 lc->lconn_next = ld->ld_conns;
345 * XXX for now, we always do a synchronous bind. This will have
346 * to change in the long run...
350 LDAPConn *savedefconn;
352 /* Set flag to prevent additional referrals
353 * from being processed on this
354 * connection until the bind has completed
356 lc->lconn_rebind_inprogress = 1;
357 /* V3 rebind function */
358 if ( ld->ld_rebind_proc != NULL) {
359 LDAPURLDesc *srvfunc;
361 srvfunc = ldap_url_dup( srvlist );
362 if ( srvfunc == NULL ) {
363 ld->ld_errno = LDAP_NO_MEMORY;
366 savedefconn = ld->ld_defconn;
367 ++lc->lconn_refcnt; /* avoid premature free */
370 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
371 #ifdef LDAP_R_COMPILE
372 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
373 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
375 err = (*ld->ld_rebind_proc)( ld,
376 bind->ri_url, bind->ri_request, bind->ri_msgid,
377 ld->ld_rebind_params );
378 #ifdef LDAP_R_COMPILE
379 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
380 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
383 ld->ld_defconn = savedefconn;
388 ldap_free_connection( ld, lc, 1, 0 );
391 ldap_free_urldesc( srvfunc );
394 savedefconn = ld->ld_defconn;
395 ++lc->lconn_refcnt; /* avoid premature free */
398 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
399 #ifdef LDAP_R_COMPILE
400 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
401 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
403 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
406 #ifdef LDAP_R_COMPILE
407 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
408 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
410 ld->ld_defconn = savedefconn;
414 ldap_free_connection( ld, lc, 1, 0 );
419 lc->lconn_rebind_inprogress = 0;
427 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
429 * return an existing connection (if any) to the server srv
430 * if "any" is non-zero, check for any server in the "srv" chain
434 LDAPURLDesc *lcu, *lsu;
435 int lcu_port, lsu_port;
437 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
438 lcu = lc->lconn_server;
439 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
442 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
443 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
446 if ( strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
447 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
448 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
449 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0
450 && lsu_port == lcu_port )
465 use_connection( LDAP *ld, LDAPConn *lc )
468 lc->lconn_lastused = time( NULL );
473 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
475 LDAPConn *tmplc, *prevlc;
477 Debug( LDAP_DEBUG_TRACE,
478 "ldap_free_connection %d %d\n",
481 if ( force || --lc->lconn_refcnt <= 0 ) {
482 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
483 ldap_mark_select_clear( ld, lc->lconn_sb );
485 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
489 if ( lc->lconn_ber != NULL ) {
490 ber_free( lc->lconn_ber, 1 );
493 ldap_int_sasl_close( ld, lc );
496 for ( tmplc = ld->ld_conns;
498 tmplc = tmplc->lconn_next )
501 if ( prevlc == NULL ) {
502 ld->ld_conns = tmplc->lconn_next;
504 prevlc->lconn_next = tmplc->lconn_next;
510 ldap_free_urllist( lc->lconn_server );
511 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
512 if ( lc->lconn_krbinstance != NULL ) {
513 LDAP_FREE( lc->lconn_krbinstance );
517 /* FIXME: is this at all possible? */
521 for ( lr = ld->ld_requests; lr; ) {
522 LDAPRequest *lr_next = lr->lr_next;
524 if ( lr->lr_conn == lc ) {
525 ldap_free_request_int( ld, lr );
531 if ( lc->lconn_sb != ld->ld_sb ) {
532 ber_sockbuf_free( lc->lconn_sb );
534 if ( lc->lconn_rebind_queue != NULL) {
536 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
537 LDAP_VFREE( lc->lconn_rebind_queue[i] );
539 LDAP_FREE( lc->lconn_rebind_queue );
542 Debug( LDAP_DEBUG_TRACE,
543 "ldap_free_connection: actually freed\n",
546 lc->lconn_lastused = time( NULL );
547 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
548 lc->lconn_refcnt, 0, 0 );
555 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
560 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
561 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
562 if ( lc->lconn_server != NULL ) {
563 fprintf( stderr, "* host: %s port: %d%s\n",
564 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
565 : lc->lconn_server->lud_host,
566 lc->lconn_server->lud_port, ( lc->lconn_sb ==
567 ld->ld_sb ) ? " (default)" : "" );
569 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
570 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
571 "NeedSocket" : ( lc->lconn_status ==
572 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
573 fprintf( stderr, " last used: %s",
574 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
575 if( lc->lconn_rebind_inprogress ) {
576 fprintf( stderr, " rebind in progress\n");
577 if( lc->lconn_rebind_queue != NULL) {
579 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
581 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
582 fprintf( stderr, " queue %d entry %d - %s\n",
583 i, j, lc->lconn_rebind_queue[i][j]);
587 fprintf( stderr, " queue is empty\n");
590 fprintf(stderr, "\n");
599 ldap_dump_requests_and_responses( LDAP *ld )
604 #ifdef LDAP_R_COMPILE
605 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
607 fprintf( stderr, "** Outstanding Requests:\n" );
608 if (( lr = ld->ld_requests ) == NULL ) {
609 fprintf( stderr, " Empty\n" );
611 for ( ; lr != NULL; lr = lr->lr_next ) {
612 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
613 lr->lr_msgid, lr->lr_origid,
614 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
615 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
616 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
617 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
618 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
619 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
620 lr->lr_outrefcnt, lr->lr_parentcnt );
622 #ifdef LDAP_R_COMPILE
623 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
625 fprintf( stderr, "** Response Queue:\n" );
626 if (( lm = ld->ld_responses ) == NULL ) {
627 fprintf( stderr, " Empty\n" );
629 for ( ; lm != NULL; lm = lm->lm_next ) {
630 fprintf( stderr, " * msgid %d, type %lu\n",
631 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
632 if (( l = lm->lm_chain ) != NULL ) {
633 fprintf( stderr, " chained responses:\n" );
634 for ( ; l != NULL; l = l->lm_chain ) {
636 " * msgid %d, type %lu\n",
638 (unsigned long) l->lm_msgtype );
643 #endif /* LDAP_DEBUG */
646 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
648 if ( lr->lr_prev == NULL ) {
649 /* free'ing the first request? */
650 assert( ld->ld_requests == lr );
651 ld->ld_requests = lr->lr_next;
654 lr->lr_prev->lr_next = lr->lr_next;
657 if ( lr->lr_next != NULL ) {
658 lr->lr_next->lr_prev = lr->lr_prev;
661 if ( lr->lr_ber != NULL ) {
662 ber_free( lr->lr_ber, 1 );
665 if ( lr->lr_res_error != NULL ) {
666 LDAP_FREE( lr->lr_res_error );
669 if ( lr->lr_res_matched != NULL ) {
670 LDAP_FREE( lr->lr_res_matched );
677 ldap_free_request( LDAP *ld, LDAPRequest *lr )
679 LDAPRequest **ttmplr;
681 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
682 lr->lr_origid, lr->lr_msgid, 0 );
684 /* free all referrals (child requests) */
685 while ( lr->lr_child )
686 ldap_free_request( ld, lr->lr_child );
688 if ( lr->lr_parent != NULL ) {
689 --lr->lr_parent->lr_outrefcnt;
690 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
692 *ttmplr = lr->lr_refnext;
694 ldap_free_request_int( ld, lr );
698 * call first time with *cntp = -1
699 * when returns *cntp == -1, no referrals are left
701 * NOTE: may replace *refsp, or shuffle the contents
702 * of the original array.
704 static int ldap_int_nextref(
710 assert( refsp != NULL );
711 assert( *refsp != NULL );
712 assert( cntp != NULL );
721 if ( (*refsp)[ *cntp ] == NULL ) {
732 * (IN) ld = LDAP connection handle
733 * (IN) lr = LDAP Request structure
734 * (IN) refs = array of pointers to referral strings that we will chase
735 * The array will be free'd by this function when no longer needed
736 * (IN) sref != 0 if following search reference
737 * (OUT) errstrp = Place to return a string of referrals which could not be followed
738 * (OUT) hadrefp = 1 if sucessfully followed referral
740 * Return value - number of referrals followed
743 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
746 int unfollowedcnt = 0;
747 LDAPRequest *origreq;
748 LDAPURLDesc *srv = NULL;
750 char **refarray = NULL;
752 int rc, count, i, j, id;
755 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
758 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
763 /* If no referrals in array, return */
764 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
769 /* Check for hop limit exceeded */
770 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
771 Debug( LDAP_DEBUG_ANY,
772 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
773 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
778 /* find original request */
780 origreq->lr_parent != NULL;
781 origreq = origreq->lr_parent )
789 if ( ld->ld_nextref_proc == NULL ) {
790 ld->ld_nextref_proc = ldap_int_nextref;
793 /* parse out & follow referrals */
795 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
797 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
800 /* Parse the referral URL */
801 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
807 if( srv->lud_crit_exts ) {
808 /* we do not support any extensions */
809 ld->ld_errno = LDAP_NOT_SUPPORTED;
814 /* treat ldap://hostpart and ldap://hostpart/ the same */
815 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
816 LDAP_FREE( srv->lud_dn );
820 /* check connection for re-bind in progress */
821 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
822 if( lc->lconn_rebind_inprogress) {
823 /* We are already chasing a referral or search reference and a
824 * bind on that connection is in progress. We must queue
825 * referrals on that connection, so we don't get a request
826 * going out before the bind operation completes. This happens
827 * if two search references come in one behind the other
828 * for the same server with different contexts.
830 Debug( LDAP_DEBUG_TRACE,
831 "ldap_chase_v3referrals: queue referral \"%s\"\n",
833 if( lc->lconn_rebind_queue == NULL ) {
834 /* Create a referral list */
835 lc->lconn_rebind_queue =
836 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
838 if( lc->lconn_rebind_queue == NULL) {
839 ld->ld_errno = LDAP_NO_MEMORY;
844 lc->lconn_rebind_queue[0] = refarray;
845 lc->lconn_rebind_queue[1] = NULL;
849 /* Count how many referral arrays we already have */
850 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
854 /* Add the new referral to the list */
855 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
856 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
858 if( lc->lconn_rebind_queue == NULL ) {
859 ld->ld_errno = LDAP_NO_MEMORY;
863 lc->lconn_rebind_queue[j] = refarray;
864 lc->lconn_rebind_queue[j+1] = NULL;
868 /* We have queued the referral/reference, now just return */
871 count = 1; /* Pretend we already followed referral */
875 /* Re-encode the request with the new starting point of the search.
876 * Note: In the future we also need to replace the filter if one
877 * was provided with the search reference
880 /* For references we don't want old dn if new dn empty */
881 if ( sref && srv->lud_dn == NULL ) {
882 srv->lud_dn = LDAP_STRDUP( "" );
885 LDAP_NEXT_MSGID( ld, id );
886 ber = re_encode_request( ld, origreq->lr_ber, id,
887 sref, srv, &rinfo.ri_request );
890 ld->ld_errno = LDAP_ENCODING_ERROR;
895 Debug( LDAP_DEBUG_TRACE,
896 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
897 lr->lr_msgid, refarray[i], 0);
899 /* Send the new request to the server - may require a bind */
900 rinfo.ri_msgid = origreq->lr_origid;
901 rinfo.ri_url = refarray[i];
902 #ifdef LDAP_R_COMPILE
903 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
905 rc = ldap_send_server_request( ld, ber, id,
906 origreq, srv, NULL, &rinfo );
907 #ifdef LDAP_R_COMPILE
908 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
911 /* Failure, try next referral in the list */
912 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
913 refarray[i], ldap_err2string( ld->ld_errno ), 0);
914 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
915 ldap_free_urllist(srv);
918 /* Success, no need to try this referral list further */
923 /* check if there is a queue of referrals that came in during bind */
925 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
926 ld->ld_errno = LDAP_OPERATIONS_ERROR;
932 if( lc->lconn_rebind_queue != NULL) {
933 /* Release resources of previous list */
934 LDAP_VFREE(refarray);
936 ldap_free_urllist(srv);
939 /* Pull entries off end of queue so list always null terminated */
940 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
943 refarray = lc->lconn_rebind_queue[j-1];
944 lc->lconn_rebind_queue[j-1] = NULL;
945 /* we pulled off last entry from queue, free queue */
947 LDAP_FREE( lc->lconn_rebind_queue);
948 lc->lconn_rebind_queue = NULL;
950 /* restart the loop the with new referral list */
954 break; /* referral followed, break out of for loop */
958 LDAP_VFREE( refarray );
959 ldap_free_urllist( srv );
960 LDAP_FREE( *errstrp );
964 LDAP_FREE( unfollowed );
967 ld->ld_errno = LDAP_REFERRAL;
968 *errstrp = unfollowed;
974 * XXX merging of errors in this routine needs to be improved
977 ldap_chase_referrals( LDAP *ld,
985 char *p, *ref, *unfollowed;
986 LDAPRequest *origreq;
991 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
993 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
996 if ( *errstrp == NULL ) {
1000 len = strlen( *errstrp );
1001 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1002 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1004 p += LDAP_REF_STR_LEN;
1009 if ( len < LDAP_REF_STR_LEN ) {
1013 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1014 Debug( LDAP_DEBUG_ANY,
1015 "more than %d referral hops (dropping)\n",
1016 ld->ld_refhoplimit, 0, 0 );
1017 /* XXX report as error in ld->ld_errno? */
1021 /* find original request */
1022 for ( origreq = lr; origreq->lr_parent != NULL;
1023 origreq = origreq->lr_parent ) {
1030 /* parse out & follow referrals */
1031 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1032 if (( p = strchr( ref, '\n' )) != NULL ) {
1038 rc = ldap_url_parse_ext( ref, &srv );
1040 if ( rc != LDAP_URL_SUCCESS ) {
1041 Debug( LDAP_DEBUG_TRACE,
1042 "ignoring unknown referral <%s>\n", ref, 0, 0 );
1043 rc = ldap_append_referral( ld, &unfollowed, ref );
1048 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1049 LDAP_FREE( srv->lud_dn );
1053 Debug( LDAP_DEBUG_TRACE,
1054 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1058 LDAP_NEXT_MSGID( ld, id );
1059 ber = re_encode_request( ld, origreq->lr_ber,
1060 id, sref, srv, &rinfo.ri_request );
1066 /* copy the complete referral for rebind process */
1067 rinfo.ri_url = LDAP_STRDUP( ref );
1069 rinfo.ri_msgid = origreq->lr_origid;
1071 #ifdef LDAP_R_COMPILE
1072 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1074 rc = ldap_send_server_request( ld, ber, id,
1075 lr, srv, NULL, &rinfo );
1076 #ifdef LDAP_R_COMPILE
1077 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1080 LDAP_FREE( rinfo.ri_url );
1085 Debug( LDAP_DEBUG_ANY,
1086 "Unable to chase referral (%s)\n",
1087 ldap_err2string( ld->ld_errno ), 0, 0 );
1088 rc = ldap_append_referral( ld, &unfollowed, ref );
1091 ldap_free_urllist(srv);
1094 LDAP_FREE( *errstrp );
1095 *errstrp = unfollowed;
1097 return(( rc == 0 ) ? count : rc );
1102 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1106 if ( *referralsp == NULL ) {
1108 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1112 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1113 strlen( *referralsp ) + strlen( s ) + 2 );
1116 if ( *referralsp == NULL ) {
1117 ld->ld_errno = LDAP_NO_MEMORY;
1122 strcpy( *referralsp, LDAP_REF_STR );
1124 strcat( *referralsp, "\n" );
1126 strcat( *referralsp, s );
1134 re_encode_request( LDAP *ld,
1135 BerElement *origber,
1142 * XXX this routine knows way too much about how the lber library works!
1150 BerElement tmpber, *ber;
1154 Debug( LDAP_DEBUG_TRACE,
1155 "re_encode_request: new msgid %ld, new dn <%s>\n",
1157 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1162 * all LDAP requests are sequences that start with a message id.
1163 * For all except delete, this is followed by a sequence that is
1164 * tagged with the operation code. For delete, the provided DN
1165 * is not wrapped by a sequence.
1167 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1169 if ( rtag == LBER_ERROR ) {
1170 ld->ld_errno = LDAP_DECODING_ERROR;
1175 if ( tag == LDAP_REQ_BIND ) {
1176 /* bind requests have a version number before the DN & other stuff */
1177 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1179 } else if ( tag == LDAP_REQ_DELETE ) {
1180 /* delete requests don't have a DN wrapping sequence */
1181 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1183 } else if ( tag == LDAP_REQ_SEARCH ) {
1184 /* search requests need to be re-scope-ed */
1185 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1187 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1188 /* use the scope provided in reference */
1189 scope = srv->lud_scope;
1191 } else if ( sref ) {
1192 /* use scope implied by previous operation
1195 * subtree -> subtree
1196 * subordinate -> subtree
1200 case LDAP_SCOPE_BASE:
1201 case LDAP_SCOPE_ONELEVEL:
1202 scope = LDAP_SCOPE_BASE;
1204 case LDAP_SCOPE_SUBTREE:
1205 #ifdef LDAP_SCOPE_SUBORDINATE
1206 case LDAP_SCOPE_SUBORDINATE:
1208 scope = LDAP_SCOPE_SUBTREE;
1214 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1217 if( rtag == LBER_ERROR ) {
1218 ld->ld_errno = LDAP_DECODING_ERROR;
1222 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1226 if ( srv->lud_dn == NULL ) {
1232 if ( tag == LDAP_REQ_BIND ) {
1233 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1234 } else if ( tag == LDAP_REQ_DELETE ) {
1235 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1236 } else if ( tag == LDAP_REQ_SEARCH ) {
1237 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1239 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1242 LDAP_FREE( orig_dn );
1245 ld->ld_errno = LDAP_ENCODING_ERROR;
1250 if ( tag != LDAP_REQ_DELETE && (
1251 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1252 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1253 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1255 ld->ld_errno = LDAP_ENCODING_ERROR;
1261 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1262 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1264 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1266 #endif /* LDAP_DEBUG */
1268 *type = tag; /* return request type */
1274 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1278 #ifdef LDAP_R_COMPILE
1279 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1281 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1282 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1283 continue; /* Skip completed requests */
1285 if ( msgid == lr->lr_msgid ) {
1289 #ifdef LDAP_R_COMPILE
1290 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );