3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
11 * This notice applies to changes, created by or for Novell, Inc.,
12 * to preexisting works for which notices appear elsewhere in this file.
14 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
16 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
17 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
18 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
19 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
20 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
21 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
22 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
23 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
25 * Modification to OpenLDAP source by Novell, Inc.
26 * April 2000 sfs Added code to chase V3 referrals
27 * request.c - sending of ldap requests; handling of referrals
34 #include <ac/stdlib.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
40 #include <ac/unistd.h>
45 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
46 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
49 re_encode_request( LDAP *ld,
57 ldap_alloc_ber_with_options( LDAP *ld )
61 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
62 ld->ld_errno = LDAP_NO_MEMORY;
70 ldap_set_ber_options( LDAP *ld, BerElement *ber )
72 ber->ber_options = ld->ld_lberoptions;
77 ldap_send_initial_request(
87 LDAP_LOG ( OPERATION, ENTRY, "ldap_send_initial_request\n", 0, 0, 0 );
89 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
92 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
93 /* not connected yet */
94 int rc = ldap_open_defconn( ld );
102 LDAP_LOG ( OPERATION, DETAIL1,
103 "ldap_send_initial_request: ldap_open_defconn: successful\n" ,
106 Debug( LDAP_DEBUG_TRACE,
107 "ldap_open_defconn: successful\n",
114 * use of DNS is turned off or this is an X.500 DN...
115 * use our default connection
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 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
133 servers, NULL, NULL );
135 ldap_free_urllist(servers);
142 ldap_send_server_request(
146 LDAPRequest *parentreq,
147 LDAPURLDesc *srvlist,
155 LDAP_LOG ( OPERATION, ENTRY, "ldap_send_server_request\n", 0, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
161 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
164 if ( srvlist == NULL ) {
167 if (( lc = find_connection( ld, srvlist, 1 )) ==
169 if ( (bind != NULL) && (parentreq != NULL) ) {
170 /* Remember the bind in the parent */
172 ++parentreq->lr_outrefcnt;
174 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
179 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
181 if ( ld->ld_errno == LDAP_SUCCESS ) {
182 ld->ld_errno = LDAP_SERVER_DOWN;
185 /* Forget about the bind */
186 --parentreq->lr_outrefcnt;
191 use_connection( ld, lc );
192 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
194 ld->ld_errno = LDAP_NO_MEMORY;
195 ldap_free_connection( ld, lc, 0, 0 );
198 /* Forget about the bind */
199 --parentreq->lr_outrefcnt;
203 lr->lr_msgid = msgid;
204 lr->lr_status = LDAP_REQST_INPROGRESS;
205 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
208 if ( parentreq != NULL ) { /* sub-request */
210 /* Increment if we didn't do it before the bind */
211 ++parentreq->lr_outrefcnt;
213 lr->lr_origid = parentreq->lr_origid;
214 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
215 lr->lr_parent = parentreq;
216 lr->lr_refnext = parentreq->lr_child;
217 parentreq->lr_child = lr;
218 } else { /* original request */
219 lr->lr_origid = lr->lr_msgid;
222 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
223 lr->lr_next->lr_prev = lr;
225 ld->ld_requests = lr;
228 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
230 if ( errno == EWOULDBLOCK ) {
231 /* need to continue write later */
232 lr->lr_status = LDAP_REQST_WRITING;
233 ldap_mark_select_write( ld, lc->lconn_sb );
236 ld->ld_errno = LDAP_SERVER_DOWN;
237 ldap_free_request( ld, lr );
238 ldap_free_connection( ld, lc, 0, 0 );
245 if ( parentreq == NULL ) {
246 ber->ber_end = ber->ber_ptr;
247 ber->ber_ptr = ber->ber_buf;
250 /* sent -- waiting for a response */
251 ldap_mark_select_read( ld, lc->lconn_sb );
254 ld->ld_errno = LDAP_SUCCESS;
259 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
260 int connect, LDAPreqinfo *bind )
267 LDAP_LOG ( OPERATION, ENTRY, "ldap_new_connection\n", 0, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
272 * make a new LDAP server connection
273 * XXX open connection synchronously for now
275 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
276 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
278 LDAP_FREE( (char *)lc );
280 ld->ld_errno = LDAP_NO_MEMORY;
284 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
287 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
288 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
295 ber_sockbuf_free( lc->lconn_sb );
297 LDAP_FREE( (char *)lc );
298 ld->ld_errno = LDAP_SERVER_DOWN;
302 lc->lconn_server = ldap_url_dup(srv);
305 lc->lconn_status = LDAP_CONNST_CONNECTED;
306 lc->lconn_next = ld->ld_conns;
310 * XXX for now, we always do a synchronous bind. This will have
311 * to change in the long run...
315 LDAPConn *savedefconn;
317 /* Set flag to prevent additional referrals from being processed on this
318 * connection until the bind has completed
320 lc->lconn_rebind_inprogress = 1;
321 /* V3 rebind function */
322 if ( ld->ld_rebind_proc != NULL) {
323 LDAPURLDesc *srvfunc;
324 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
325 ld->ld_errno = LDAP_NO_MEMORY;
328 savedefconn = ld->ld_defconn;
329 ++lc->lconn_refcnt; /* avoid premature free */
333 LDAP_LOG ( OPERATION, DETAIL1,
334 "ldap_new_connection: Call application rebind_proc\n",
337 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
339 err = (*ld->ld_rebind_proc)( ld,
340 bind->ri_url, bind->ri_request, bind->ri_msgid,
341 ld->ld_rebind_params );
343 ld->ld_defconn = savedefconn;
348 ldap_free_connection( ld, lc, 1, 0 );
351 ldap_free_urldesc( srvfunc);
354 savedefconn = ld->ld_defconn;
355 ++lc->lconn_refcnt; /* avoid premature free */
359 LDAP_LOG ( OPERATION, DETAIL1,
360 "ldap_new_connection: anonymous rebind via ldap_bind_s\n",
363 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
365 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
368 ld->ld_defconn = savedefconn;
372 ldap_free_connection( ld, lc, 1, 0 );
377 lc->lconn_rebind_inprogress = 0;
385 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
387 * return an existing connection (if any) to the server srv
388 * if "any" is non-zero, check for any server in the "srv" chain
394 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
395 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
396 if ( lc->lconn_server->lud_host != NULL &&
397 *lc->lconn_server->lud_host != '\0' &&
398 ls->lud_host != NULL && *ls->lud_host != '\0' &&
399 strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0
400 && ls->lud_port == lc->lconn_server->lud_port ) {
415 use_connection( LDAP *ld, LDAPConn *lc )
418 lc->lconn_lastused = time( NULL );
423 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
425 LDAPConn *tmplc, *prevlc;
428 LDAP_LOG ( OPERATION, ENTRY, "ldap_free_connection\n", 0, 0, 0 );
430 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
433 if ( force || --lc->lconn_refcnt <= 0 ) {
434 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
435 ldap_mark_select_clear( ld, lc->lconn_sb );
437 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
441 if( lc->lconn_ber != NULL ) {
442 ber_free( lc->lconn_ber, 1 );
445 ldap_int_sasl_close( ld, lc );
448 for ( tmplc = ld->ld_conns; tmplc != NULL;
449 tmplc = tmplc->lconn_next ) {
451 if ( prevlc == NULL ) {
452 ld->ld_conns = tmplc->lconn_next;
454 prevlc->lconn_next = tmplc->lconn_next;
460 ldap_free_urllist( lc->lconn_server );
461 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
462 if ( lc->lconn_krbinstance != NULL ) {
463 LDAP_FREE( lc->lconn_krbinstance );
466 if ( lc->lconn_sb != ld->ld_sb ) {
467 ber_sockbuf_free( lc->lconn_sb );
469 if( lc->lconn_rebind_queue != NULL) {
471 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
472 LDAP_VFREE(lc->lconn_rebind_queue[i]);
474 LDAP_FREE( lc->lconn_rebind_queue);
478 LDAP_LOG ( OPERATION, RESULTS,
479 "ldap_free_connection: actually freed\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
485 lc->lconn_lastused = time( NULL );
487 LDAP_LOG ( OPERATION, RESULTS, "ldap_free_connection: refcnt %d\n",
488 lc->lconn_refcnt, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
491 lc->lconn_refcnt, 0, 0 );
499 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
504 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
505 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
506 if ( lc->lconn_server != NULL ) {
507 fprintf( stderr, "* host: %s port: %d%s\n",
508 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
509 : lc->lconn_server->lud_host,
510 lc->lconn_server->lud_port, ( lc->lconn_sb ==
511 ld->ld_sb ) ? " (default)" : "" );
513 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
514 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
515 "NeedSocket" : ( lc->lconn_status ==
516 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
517 fprintf( stderr, " last used: %s",
518 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
519 if( lc->lconn_rebind_inprogress ) {
520 fprintf( stderr, " rebind in progress\n");
521 if( lc->lconn_rebind_queue != NULL) {
523 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
525 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
526 fprintf( stderr, " queue %d entry %d - %s\n",
527 i, j, lc->lconn_rebind_queue[i][j]);
531 fprintf( stderr, " queue is empty\n");
534 fprintf(stderr, "\n");
543 ldap_dump_requests_and_responses( LDAP *ld )
548 fprintf( stderr, "** Outstanding Requests:\n" );
549 if (( lr = ld->ld_requests ) == NULL ) {
550 fprintf( stderr, " Empty\n" );
552 for ( ; lr != NULL; lr = lr->lr_next ) {
553 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
554 lr->lr_msgid, lr->lr_origid,
555 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
556 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
557 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
558 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
559 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
560 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
561 lr->lr_outrefcnt, lr->lr_parentcnt );
564 fprintf( stderr, "** Response Queue:\n" );
565 if (( lm = ld->ld_responses ) == NULL ) {
566 fprintf( stderr, " Empty\n" );
568 for ( ; lm != NULL; lm = lm->lm_next ) {
569 fprintf( stderr, " * msgid %d, type %lu\n",
570 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
571 if (( l = lm->lm_chain ) != NULL ) {
572 fprintf( stderr, " chained responses:\n" );
573 for ( ; l != NULL; l = l->lm_chain ) {
575 " * msgid %d, type %lu\n",
577 (unsigned long) l->lm_msgtype );
582 #endif /* LDAP_DEBUG */
585 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
587 if ( lr->lr_prev == NULL ) {
588 ld->ld_requests = lr->lr_next;
590 lr->lr_prev->lr_next = lr->lr_next;
593 if ( lr->lr_next != NULL ) {
594 lr->lr_next->lr_prev = lr->lr_prev;
597 if ( lr->lr_ber != NULL ) {
598 ber_free( lr->lr_ber, 1 );
601 if ( lr->lr_res_error != NULL ) {
602 LDAP_FREE( lr->lr_res_error );
605 if ( lr->lr_res_matched != NULL ) {
606 LDAP_FREE( lr->lr_res_matched );
613 ldap_free_request( LDAP *ld, LDAPRequest *lr )
615 LDAPRequest **ttmplr;
618 LDAP_LOG ( OPERATION, ARGS, "ldap_free_request (origid %d, msgid %d)\n",
619 lr->lr_origid, lr->lr_msgid, 0 );
621 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
622 lr->lr_origid, lr->lr_msgid, 0 );
625 if ( lr->lr_parent != NULL ) {
626 --lr->lr_parent->lr_outrefcnt;
627 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
629 *ttmplr = lr->lr_refnext;
631 /* free all referrals (child requests) */
632 while ( lr->lr_child )
633 ldap_free_request( ld, lr->lr_child );
635 ldap_free_request_int( ld, lr );
643 * (IN) ld = LDAP connection handle
644 * (IN) lr = LDAP Request structure
645 * (IN) refs = array of pointers to referral strings that we will chase
646 * The array will be free'd by this function when no longer needed
647 * (IN) sref != 0 if following search reference
648 * (OUT) errstrp = Place to return a string of referrals which could not be followed
649 * (OUT) hadrefp = 1 if sucessfully followed referral
651 * Return value - number of referrals followed
654 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
657 int unfollowedcnt = 0;
658 LDAPRequest *origreq;
659 LDAPURLDesc *srv = NULL;
661 char **refarray = NULL;
666 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
670 LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_v3referrals\n", 0, 0, 0 );
672 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
678 /* If no referrals in array, return */
679 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
684 /* Check for hop limit exceeded */
685 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
687 LDAP_LOG ( OPERATION, DETAIL1,
688 "ldap_chase_v3referrals: more than %d referral hops (dropping)\n",
689 ld->ld_refhoplimit, 0, 0 );
691 Debug( LDAP_DEBUG_ANY,
692 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
694 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
699 /* find original request */
701 origreq->lr_parent != NULL;
702 origreq = origreq->lr_parent )
709 /* parse out & follow referrals */
710 for( i=0; refarray[i] != NULL; i++) {
711 /* Parse the referral URL */
712 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
718 if( srv->lud_crit_exts ) {
719 /* we do not support any extensions */
720 ld->ld_errno = LDAP_NOT_SUPPORTED;
725 /* treat ldap://hostpart and ldap://hostpart/ the same */
726 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
727 LDAP_FREE( srv->lud_dn );
731 /* check connection for re-bind in progress */
732 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
733 if( lc->lconn_rebind_inprogress) {
734 /* We are already chasing a referral or search reference and a
735 * bind on that connection is in progress. We must queue
736 * referrals on that connection, so we don't get a request
737 * going out before the bind operation completes. This happens
738 * if two search references come in one behind the other
739 * for the same server with different contexts.
742 LDAP_LOG ( OPERATION, DETAIL1,
743 "ldap_chase_v3referrals: queue referral \"%s\"\n",
746 Debug( LDAP_DEBUG_TRACE,
747 "ldap_chase_v3referrals: queue referral \"%s\"\n",
750 if( lc->lconn_rebind_queue == NULL ) {
751 /* Create a referral list */
752 lc->lconn_rebind_queue =
753 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
755 if( lc->lconn_rebind_queue == NULL) {
756 ld->ld_errno = LDAP_NO_MEMORY;
761 lc->lconn_rebind_queue[0] = refarray;
762 lc->lconn_rebind_queue[1] = NULL;
766 /* Count how many referral arrays we already have */
767 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
771 /* Add the new referral to the list */
772 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
773 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
775 if( lc->lconn_rebind_queue == NULL ) {
776 ld->ld_errno = LDAP_NO_MEMORY;
780 lc->lconn_rebind_queue[j] = refarray;
781 lc->lconn_rebind_queue[j+1] = NULL;
785 /* We have queued the referral/reference, now just return */
788 count = 1; /* Pretend we already followed referral */
792 /* Re-encode the request with the new starting point of the search.
793 * Note: In the future we also need to replace the filter if one
794 * was provided with the search reference
797 /* For references we don't want old dn if new dn empty */
798 if ( sref && srv->lud_dn == NULL ) {
799 srv->lud_dn = LDAP_STRDUP( "" );
802 ber = re_encode_request( ld, origreq->lr_ber, ++ld->ld_msgid,
803 sref, srv, &rinfo.ri_request );
806 ld->ld_errno = LDAP_ENCODING_ERROR;
812 LDAP_LOG ( OPERATION, DETAIL1,
813 "ldap_chase_v3referrals: msgid %d, url \"%s\"\n",
814 lr->lr_msgid, refarray[i], 0 );
816 Debug( LDAP_DEBUG_TRACE,
817 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
818 lr->lr_msgid, refarray[i], 0);
821 /* Send the new request to the server - may require a bind */
822 rinfo.ri_msgid = origreq->lr_origid;
823 rinfo.ri_url = refarray[i];
824 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
825 origreq, srv, NULL, &rinfo )) < 0 ) {
826 /* Failure, try next referral in the list */
828 LDAP_LOG ( OPERATION, ERR,
829 "ldap_chase_v3referrals: Unable to chase referral \"%s\" (%s)\n",
830 refarray[i], ldap_err2string( ld->ld_errno ), 0 );
832 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
833 refarray[i], ldap_err2string( ld->ld_errno ), 0);
835 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
836 ldap_free_urllist(srv);
839 /* Success, no need to try this referral list further */
844 /* check if there is a queue of referrals that came in during bind */
846 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
847 ld->ld_errno = LDAP_OPERATIONS_ERROR;
853 if( lc->lconn_rebind_queue != NULL) {
854 /* Release resources of previous list */
855 LDAP_VFREE(refarray);
857 ldap_free_urllist(srv);
860 /* Pull entries off end of queue so list always null terminated */
861 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
864 refarray = lc->lconn_rebind_queue[j-1];
865 lc->lconn_rebind_queue[j-1] = NULL;
866 /* we pulled off last entry from queue, free queue */
868 LDAP_FREE( lc->lconn_rebind_queue);
869 lc->lconn_rebind_queue = NULL;
871 /* restart the loop the with new referral list */
875 break; /* referral followed, break out of for loop */
879 LDAP_VFREE(refarray);
880 ldap_free_urllist(srv);
881 LDAP_FREE( *errstrp );
885 LDAP_FREE( unfollowed );
888 ld->ld_errno = LDAP_REFERRAL;
889 *errstrp = unfollowed;
895 * XXX merging of errors in this routine needs to be improved
898 ldap_chase_referrals( LDAP *ld,
906 char *p, *ref, *unfollowed;
907 LDAPRequest *origreq;
913 LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_referrals\n", 0, 0, 0 );
915 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
918 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
921 if ( *errstrp == NULL ) {
925 len = strlen( *errstrp );
926 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
927 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
929 p += LDAP_REF_STR_LEN;
934 if ( len < LDAP_REF_STR_LEN ) {
938 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
940 LDAP_LOG ( OPERATION, ENTRY,
941 "ldap_chase_referrals: more than %d referral hops (dropping)\n",
942 ld->ld_refhoplimit, 0, 0 );
944 Debug( LDAP_DEBUG_ANY,
945 "more than %d referral hops (dropping)\n",
946 ld->ld_refhoplimit, 0, 0 );
948 /* XXX report as error in ld->ld_errno? */
952 /* find original request */
953 for ( origreq = lr; origreq->lr_parent != NULL;
954 origreq = origreq->lr_parent ) {
961 /* parse out & follow referrals */
962 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
963 if (( p = strchr( ref, '\n' )) != NULL ) {
969 rc = ldap_url_parse_ext( ref, &srv );
971 if ( rc != LDAP_URL_SUCCESS ) {
973 LDAP_LOG ( OPERATION, DETAIL1,
974 "ldap_chase_referrals: ignoring unknown referral <%s>\n",
977 Debug( LDAP_DEBUG_TRACE,
978 "ignoring unknown referral <%s>\n", ref, 0, 0 );
980 rc = ldap_append_referral( ld, &unfollowed, ref );
985 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
986 LDAP_FREE( srv->lud_dn );
991 LDAP_LOG ( OPERATION, DETAIL1,
992 "ldap_chase_referrals: chasing LDAP referral <%s>\n", ref, 0, 0 );
994 Debug( LDAP_DEBUG_TRACE,
995 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1000 ber = re_encode_request( ld, origreq->lr_ber,
1001 ++ld->ld_msgid, sref, srv, &rinfo.ri_request );
1007 /* copy the complete referral for rebind process */
1008 rinfo.ri_url = LDAP_STRDUP( ref );
1010 rinfo.ri_msgid = origreq->lr_origid;
1012 rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
1013 lr, srv, NULL, &rinfo );
1015 LDAP_FREE( rinfo.ri_url );
1021 LDAP_LOG ( OPERATION, ERR,
1022 "ldap_chase_referrals: Unable to chase referral <%s>\n",
1023 ldap_err2string( ld->ld_errno), 0, 0 );
1025 Debug( LDAP_DEBUG_ANY,
1026 "Unable to chase referral (%s)\n",
1027 ldap_err2string( ld->ld_errno ), 0, 0 );
1029 rc = ldap_append_referral( ld, &unfollowed, ref );
1032 ldap_free_urllist(srv);
1035 LDAP_FREE( *errstrp );
1036 *errstrp = unfollowed;
1038 return(( rc == 0 ) ? count : rc );
1043 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1047 if ( *referralsp == NULL ) {
1049 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1053 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1054 strlen( *referralsp ) + strlen( s ) + 2 );
1057 if ( *referralsp == NULL ) {
1058 ld->ld_errno = LDAP_NO_MEMORY;
1063 strcpy( *referralsp, LDAP_REF_STR );
1065 strcat( *referralsp, "\n" );
1067 strcat( *referralsp, s );
1075 re_encode_request( LDAP *ld,
1076 BerElement *origber,
1083 * XXX this routine knows way too much about how the lber library works!
1091 BerElement tmpber, *ber;
1096 LDAP_LOG ( OPERATION, ARGS,
1097 "re_encode_request: new msgid %ld, new dn <%s>\n",
1099 ( srv == NULL || srv->lud_dn == NULL ) ? "NONE" : srv->lud_dn, 0 );
1101 Debug( LDAP_DEBUG_TRACE,
1102 "re_encode_request: new msgid %ld, new dn <%s>\n",
1104 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1110 * all LDAP requests are sequences that start with a message id.
1111 * For all except delete, this is followed by a sequence that is
1112 * tagged with the operation code. For delete, the provided DN
1113 * is not wrapped by a sequence.
1115 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1117 if ( rtag == LBER_ERROR ) {
1118 ld->ld_errno = LDAP_DECODING_ERROR;
1123 if ( tag == LDAP_REQ_BIND ) {
1124 /* bind requests have a version number before the DN & other stuff */
1125 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1127 } else if ( tag == LDAP_REQ_DELETE ) {
1128 /* delete requests don't have a DN wrapping sequence */
1129 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1131 } else if ( tag == LDAP_REQ_SEARCH ) {
1132 /* search requests need to be re-scope-ed */
1133 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1135 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1136 /* use the scope provided in reference */
1137 scope = srv->lud_scope;
1139 } else if ( sref && scope != LDAP_SCOPE_SUBTREE ) {
1140 /* use scope implied by previous operation */
1143 /* subtree -> subtree */
1144 scope = LDAP_SCOPE_BASE;
1148 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1151 if( rtag == LBER_ERROR ) {
1152 ld->ld_errno = LDAP_DECODING_ERROR;
1156 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1160 if ( srv->lud_dn == NULL ) {
1166 if ( tag == LDAP_REQ_BIND ) {
1167 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1168 } else if ( tag == LDAP_REQ_DELETE ) {
1169 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1170 } else if ( tag == LDAP_REQ_SEARCH ) {
1171 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1173 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1176 LDAP_FREE( orig_dn );
1179 ld->ld_errno = LDAP_ENCODING_ERROR;
1184 if ( tag != LDAP_REQ_DELETE && (
1185 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1186 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1187 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1189 ld->ld_errno = LDAP_ENCODING_ERROR;
1195 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1197 LDAP_LOG ( OPERATION, DETAIL1,
1198 "re_encode_request: new request is:\n", 0, 0, 0 );
1200 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1203 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1205 #endif /* LDAP_DEBUG */
1207 *type = tag; /* return request type */
1213 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1217 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1218 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1219 continue; /* Skip completed requests */
1221 if ( msgid == lr->lr_msgid ) {