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 (( "request", LDAP_LEVEL_ENTRY, "ldap_send_initial_request\n" ));
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 (( "request", LDAP_LEVEL_DETAIL1,
103 "ldap_send_initial_request: ldap_open_defconn: successful\n" ));
105 Debug( LDAP_DEBUG_TRACE,
106 "ldap_open_defconn: successful\n",
113 * use of DNS is turned off or this is an X.500 DN...
114 * use our default connection
119 #ifdef LDAP_CONNECTIONLESS
120 if (LDAP_IS_UDP(ld)) {
121 if (msgtype == LDAP_REQ_BIND) {
122 if (ld->ld_options.ldo_cldapdn)
123 ldap_memfree(ld->ld_options.ldo_cldapdn);
124 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
127 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
128 return LDAP_PARAM_ERROR;
131 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
132 servers, NULL, NULL );
134 ldap_free_urllist(servers);
141 ldap_send_server_request(
145 LDAPRequest *parentreq,
146 LDAPURLDesc *srvlist,
154 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY, "ldap_send_server_request\n" ));
156 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
160 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
163 if ( srvlist == NULL ) {
166 if (( lc = find_connection( ld, srvlist, 1 )) ==
168 if ( (bind != NULL) && (parentreq != NULL) ) {
169 /* Remember the bind in the parent */
171 ++parentreq->lr_outrefcnt;
173 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
178 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
180 if ( ld->ld_errno == LDAP_SUCCESS ) {
181 ld->ld_errno = LDAP_SERVER_DOWN;
184 /* Forget about the bind */
185 --parentreq->lr_outrefcnt;
190 use_connection( ld, lc );
191 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
193 ld->ld_errno = LDAP_NO_MEMORY;
194 ldap_free_connection( ld, lc, 0, 0 );
197 /* Forget about the bind */
198 --parentreq->lr_outrefcnt;
202 lr->lr_msgid = msgid;
203 lr->lr_status = LDAP_REQST_INPROGRESS;
204 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
207 if ( parentreq != NULL ) { /* sub-request */
209 /* Increment if we didn't do it before the bind */
210 ++parentreq->lr_outrefcnt;
212 lr->lr_origid = parentreq->lr_origid;
213 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
214 lr->lr_parent = parentreq;
215 lr->lr_refnext = parentreq->lr_child;
216 parentreq->lr_child = lr;
217 } else { /* original request */
218 lr->lr_origid = lr->lr_msgid;
221 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
222 lr->lr_next->lr_prev = lr;
224 ld->ld_requests = lr;
227 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
229 if ( errno == EWOULDBLOCK ) {
230 /* need to continue write later */
231 lr->lr_status = LDAP_REQST_WRITING;
232 ldap_mark_select_write( ld, lc->lconn_sb );
235 ld->ld_errno = LDAP_SERVER_DOWN;
236 ldap_free_request( ld, lr );
237 ldap_free_connection( ld, lc, 0, 0 );
244 if ( parentreq == NULL ) {
245 ber->ber_end = ber->ber_ptr;
246 ber->ber_ptr = ber->ber_buf;
249 /* sent -- waiting for a response */
250 ldap_mark_select_read( ld, lc->lconn_sb );
253 ld->ld_errno = LDAP_SUCCESS;
258 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
259 int connect, LDAPreqinfo *bind )
266 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY, "ldap_new_connection\n" ));
268 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
271 * make a new LDAP server connection
272 * XXX open connection synchronously for now
274 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
275 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
277 LDAP_FREE( (char *)lc );
279 ld->ld_errno = LDAP_NO_MEMORY;
283 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
286 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
287 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
294 ber_sockbuf_free( lc->lconn_sb );
296 LDAP_FREE( (char *)lc );
297 ld->ld_errno = LDAP_SERVER_DOWN;
301 lc->lconn_server = ldap_url_dup(srv);
304 lc->lconn_status = LDAP_CONNST_CONNECTED;
305 lc->lconn_next = ld->ld_conns;
309 * XXX for now, we always do a synchronous bind. This will have
310 * to change in the long run...
314 LDAPConn *savedefconn;
316 /* Set flag to prevent additional referrals from being processed on this
317 * connection until the bind has completed
319 lc->lconn_rebind_inprogress = 1;
320 /* V3 rebind function */
321 if ( ld->ld_rebind_proc != NULL) {
322 LDAPURLDesc *srvfunc;
323 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
324 ld->ld_errno = LDAP_NO_MEMORY;
327 savedefconn = ld->ld_defconn;
328 ++lc->lconn_refcnt; /* avoid premature free */
332 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
333 "ldap_new_connection: Call application rebind_proc\n" ));
335 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
337 err = (*ld->ld_rebind_proc)( ld,
338 bind->ri_url, bind->ri_request, bind->ri_msgid,
339 ld->ld_rebind_params );
341 ld->ld_defconn = savedefconn;
346 ldap_free_connection( ld, lc, 1, 0 );
349 ldap_free_urldesc( srvfunc);
352 savedefconn = ld->ld_defconn;
353 ++lc->lconn_refcnt; /* avoid premature free */
357 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
358 "ldap_new_connection: anonymous rebind via ldap_bind_s\n" ));
360 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
362 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
365 ld->ld_defconn = savedefconn;
369 ldap_free_connection( ld, lc, 1, 0 );
374 lc->lconn_rebind_inprogress = 0;
382 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
384 * return an existing connection (if any) to the server srv
385 * if "any" is non-zero, check for any server in the "srv" chain
391 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
392 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
393 if ( lc->lconn_server->lud_host != NULL &&
394 *lc->lconn_server->lud_host != '\0' &&
395 ls->lud_host != NULL && *ls->lud_host != '\0' &&
396 strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0
397 && ls->lud_port == lc->lconn_server->lud_port ) {
412 use_connection( LDAP *ld, LDAPConn *lc )
415 lc->lconn_lastused = time( NULL );
420 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
422 LDAPConn *tmplc, *prevlc;
425 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY, "ldap_free_connection\n" ));
427 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
430 if ( force || --lc->lconn_refcnt <= 0 ) {
431 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
432 ldap_mark_select_clear( ld, lc->lconn_sb );
434 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
438 if( lc->lconn_ber != NULL ) {
439 ber_free( lc->lconn_ber, 1 );
442 ldap_int_sasl_close( ld, lc );
445 for ( tmplc = ld->ld_conns; tmplc != NULL;
446 tmplc = tmplc->lconn_next ) {
448 if ( prevlc == NULL ) {
449 ld->ld_conns = tmplc->lconn_next;
451 prevlc->lconn_next = tmplc->lconn_next;
457 ldap_free_urllist( lc->lconn_server );
458 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
459 if ( lc->lconn_krbinstance != NULL ) {
460 LDAP_FREE( lc->lconn_krbinstance );
463 if ( lc->lconn_sb != ld->ld_sb ) {
464 ber_sockbuf_free( lc->lconn_sb );
466 if( lc->lconn_rebind_queue != NULL) {
468 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
469 LDAP_VFREE(lc->lconn_rebind_queue[i]);
471 LDAP_FREE( lc->lconn_rebind_queue);
475 LDAP_LOG (( "request", LDAP_LEVEL_RESULTS,
476 "ldap_free_connection: actually freed\n" ));
478 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
482 lc->lconn_lastused = time( NULL );
484 LDAP_LOG (( "request", LDAP_LEVEL_RESULTS,
485 "ldap_free_connection: refcnt %d\n", lc->lconn_refcnt ));
487 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
488 lc->lconn_refcnt, 0, 0 );
496 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
501 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
502 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
503 if ( lc->lconn_server != NULL ) {
504 fprintf( stderr, "* host: %s port: %d%s\n",
505 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
506 : lc->lconn_server->lud_host,
507 lc->lconn_server->lud_port, ( lc->lconn_sb ==
508 ld->ld_sb ) ? " (default)" : "" );
510 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
511 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
512 "NeedSocket" : ( lc->lconn_status ==
513 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
514 fprintf( stderr, " last used: %s",
515 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
516 if( lc->lconn_rebind_inprogress ) {
517 fprintf( stderr, " rebind in progress\n");
518 if( lc->lconn_rebind_queue != NULL) {
520 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
522 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
523 fprintf( stderr, " queue %d entry %d - %s\n",
524 i, j, lc->lconn_rebind_queue[i][j]);
528 fprintf( stderr, " queue is empty\n");
531 fprintf(stderr, "\n");
540 ldap_dump_requests_and_responses( LDAP *ld )
545 fprintf( stderr, "** Outstanding Requests:\n" );
546 if (( lr = ld->ld_requests ) == NULL ) {
547 fprintf( stderr, " Empty\n" );
549 for ( ; lr != NULL; lr = lr->lr_next ) {
550 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
551 lr->lr_msgid, lr->lr_origid,
552 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
553 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
554 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
555 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
556 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
557 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
558 lr->lr_outrefcnt, lr->lr_parentcnt );
561 fprintf( stderr, "** Response Queue:\n" );
562 if (( lm = ld->ld_responses ) == NULL ) {
563 fprintf( stderr, " Empty\n" );
565 for ( ; lm != NULL; lm = lm->lm_next ) {
566 fprintf( stderr, " * msgid %d, type %lu\n",
567 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
568 if (( l = lm->lm_chain ) != NULL ) {
569 fprintf( stderr, " chained responses:\n" );
570 for ( ; l != NULL; l = l->lm_chain ) {
572 " * msgid %d, type %lu\n",
574 (unsigned long) l->lm_msgtype );
579 #endif /* LDAP_DEBUG */
582 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
584 if ( lr->lr_prev == NULL ) {
585 ld->ld_requests = lr->lr_next;
587 lr->lr_prev->lr_next = lr->lr_next;
590 if ( lr->lr_next != NULL ) {
591 lr->lr_next->lr_prev = lr->lr_prev;
594 if ( lr->lr_ber != NULL ) {
595 ber_free( lr->lr_ber, 1 );
598 if ( lr->lr_res_error != NULL ) {
599 LDAP_FREE( lr->lr_res_error );
602 if ( lr->lr_res_matched != NULL ) {
603 LDAP_FREE( lr->lr_res_matched );
610 ldap_free_request( LDAP *ld, LDAPRequest *lr )
612 LDAPRequest **ttmplr;
615 LDAP_LOG (( "request", LDAP_LEVEL_ARGS,
616 "ldap_free_request (origid %d, msgid %d)\n",
617 lr->lr_origid, lr->lr_msgid ));
619 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
620 lr->lr_origid, lr->lr_msgid, 0 );
623 if ( lr->lr_parent != NULL ) {
624 --lr->lr_parent->lr_outrefcnt;
625 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
627 *ttmplr = lr->lr_refnext;
629 /* free all referrals (child requests) */
630 while ( lr->lr_child )
631 ldap_free_request( ld, lr->lr_child );
633 ldap_free_request_int( ld, lr );
641 * (IN) ld = LDAP connection handle
642 * (IN) lr = LDAP Request structure
643 * (IN) refs = array of pointers to referral strings that we will chase
644 * The array will be free'd by this function when no longer needed
645 * (IN) sref != 0 if following search reference
646 * (OUT) errstrp = Place to return a string of referrals which could not be followed
647 * (OUT) hadrefp = 1 if sucessfully followed referral
649 * Return value - number of referrals followed
652 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
655 int unfollowedcnt = 0;
656 LDAPRequest *origreq;
657 LDAPURLDesc *srv = NULL;
659 char **refarray = NULL;
664 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
668 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY, "ldap_chase_v3referrals\n" ));
670 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
676 /* If no referrals in array, return */
677 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
682 /* Check for hop limit exceeded */
683 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
685 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
686 "ldap_chase_v3referrals: more than %d referral hops (dropping)\n",
687 ld->ld_refhoplimit ));
689 Debug( LDAP_DEBUG_ANY,
690 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
692 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
697 /* find original request */
699 origreq->lr_parent != NULL;
700 origreq = origreq->lr_parent )
707 /* parse out & follow referrals */
708 for( i=0; refarray[i] != NULL; i++) {
709 /* Parse the referral URL */
710 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
716 if( srv->lud_crit_exts ) {
717 /* we do not support any extensions */
718 ld->ld_errno = LDAP_NOT_SUPPORTED;
723 /* treat ldap://hostpart and ldap://hostpart/ the same */
724 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
725 LDAP_FREE( srv->lud_dn );
729 /* check connection for re-bind in progress */
730 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
731 if( lc->lconn_rebind_inprogress) {
732 /* We are already chasing a referral or search reference and a
733 * bind on that connection is in progress. We must queue
734 * referrals on that connection, so we don't get a request
735 * going out before the bind operation completes. This happens
736 * if two search references come in one behind the other
737 * for the same server with different contexts.
740 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
741 "ldap_chase_v3referrals: queue referral \"%s\"\n",
744 Debug( LDAP_DEBUG_TRACE,
745 "ldap_chase_v3referrals: queue referral \"%s\"\n",
748 if( lc->lconn_rebind_queue == NULL ) {
749 /* Create a referral list */
750 lc->lconn_rebind_queue =
751 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
753 if( lc->lconn_rebind_queue == NULL) {
754 ld->ld_errno = LDAP_NO_MEMORY;
759 lc->lconn_rebind_queue[0] = refarray;
760 lc->lconn_rebind_queue[1] = NULL;
764 /* Count how many referral arrays we already have */
765 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
769 /* Add the new referral to the list */
770 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
771 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
773 if( lc->lconn_rebind_queue == NULL ) {
774 ld->ld_errno = LDAP_NO_MEMORY;
778 lc->lconn_rebind_queue[j] = refarray;
779 lc->lconn_rebind_queue[j+1] = NULL;
783 /* We have queued the referral/reference, now just return */
786 count = 1; /* Pretend we already followed referral */
790 /* Re-encode the request with the new starting point of the search.
791 * Note: In the future we also need to replace the filter if one
792 * was provided with the search reference
795 /* For references we don't want old dn if new dn empty */
796 if ( sref && srv->lud_dn == NULL ) {
797 srv->lud_dn = LDAP_STRDUP( "" );
800 ber = re_encode_request( ld, origreq->lr_ber, ++ld->ld_msgid,
801 sref, srv, &rinfo.ri_request );
804 ld->ld_errno = LDAP_ENCODING_ERROR;
810 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
811 "ldap_chase_v3referrals: msgid %d, url \"%s\"\n",
812 lr->lr_msgid, refarray[i] ));
814 Debug( LDAP_DEBUG_TRACE,
815 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
816 lr->lr_msgid, refarray[i], 0);
819 /* Send the new request to the server - may require a bind */
820 rinfo.ri_msgid = origreq->lr_origid;
821 rinfo.ri_url = refarray[i];
822 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
823 origreq, srv, NULL, &rinfo )) < 0 ) {
824 /* Failure, try next referral in the list */
826 LDAP_LOG (( "request", LDAP_LEVEL_ERR,
827 "ldap_chase_v3referrals: Unable to chase referral \"%s\" (%s)\n",
828 refarray[i], ldap_err2string( ld->ld_errno ) ));
830 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
831 refarray[i], ldap_err2string( ld->ld_errno ), 0);
833 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
834 ldap_free_urllist(srv);
837 /* Success, no need to try this referral list further */
842 /* check if there is a queue of referrals that came in during bind */
844 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
845 ld->ld_errno = LDAP_OPERATIONS_ERROR;
851 if( lc->lconn_rebind_queue != NULL) {
852 /* Release resources of previous list */
853 LDAP_VFREE(refarray);
855 ldap_free_urllist(srv);
858 /* Pull entries off end of queue so list always null terminated */
859 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
862 refarray = lc->lconn_rebind_queue[j-1];
863 lc->lconn_rebind_queue[j-1] = NULL;
864 /* we pulled off last entry from queue, free queue */
866 LDAP_FREE( lc->lconn_rebind_queue);
867 lc->lconn_rebind_queue = NULL;
869 /* restart the loop the with new referral list */
873 break; /* referral followed, break out of for loop */
877 LDAP_VFREE(refarray);
878 ldap_free_urllist(srv);
879 LDAP_FREE( *errstrp );
883 LDAP_FREE( unfollowed );
886 ld->ld_errno = LDAP_REFERRAL;
887 *errstrp = unfollowed;
893 * XXX merging of errors in this routine needs to be improved
896 ldap_chase_referrals( LDAP *ld,
904 char *p, *ref, *unfollowed;
905 LDAPRequest *origreq;
911 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY, "ldap_chase_referrals\n" ));
913 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
916 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
919 if ( *errstrp == NULL ) {
923 len = strlen( *errstrp );
924 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
925 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
927 p += LDAP_REF_STR_LEN;
932 if ( len < LDAP_REF_STR_LEN ) {
936 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
938 LDAP_LOG (( "request", LDAP_LEVEL_ENTRY,
939 "ldap_chase_referrals: more than %d referral hops (dropping)\n",
940 ld->ld_refhoplimit ));
942 Debug( LDAP_DEBUG_ANY,
943 "more than %d referral hops (dropping)\n",
944 ld->ld_refhoplimit, 0, 0 );
946 /* XXX report as error in ld->ld_errno? */
950 /* find original request */
951 for ( origreq = lr; origreq->lr_parent != NULL;
952 origreq = origreq->lr_parent ) {
959 /* parse out & follow referrals */
960 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
961 if (( p = strchr( ref, '\n' )) != NULL ) {
967 rc = ldap_url_parse_ext( ref, &srv );
969 if ( rc != LDAP_URL_SUCCESS ) {
971 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
972 "ldap_chase_referrals: ignoring unknown referral <%s>\n",
975 Debug( LDAP_DEBUG_TRACE,
976 "ignoring unknown referral <%s>\n", ref, 0, 0 );
978 rc = ldap_append_referral( ld, &unfollowed, ref );
983 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
984 LDAP_FREE( srv->lud_dn );
989 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
990 "ldap_chase_referrals: chasing LDAP referral <%s>\n", ref ));
992 Debug( LDAP_DEBUG_TRACE,
993 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
998 ber = re_encode_request( ld, origreq->lr_ber,
999 ++ld->ld_msgid, sref, srv, &rinfo.ri_request );
1005 /* copy the complete referral for rebind process */
1006 rinfo.ri_url = LDAP_STRDUP( ref );
1008 rinfo.ri_msgid = origreq->lr_origid;
1010 rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
1011 lr, srv, NULL, &rinfo );
1013 LDAP_FREE( rinfo.ri_url );
1019 LDAP_LOG (( "request", LDAP_LEVEL_ERR,
1020 "ldap_chase_referrals: Unable to chase referral <%s>\n",
1021 ldap_err2string( ld->ld_errno) ));
1023 Debug( LDAP_DEBUG_ANY,
1024 "Unable to chase referral (%s)\n",
1025 ldap_err2string( ld->ld_errno ), 0, 0 );
1027 rc = ldap_append_referral( ld, &unfollowed, ref );
1030 ldap_free_urllist(srv);
1033 LDAP_FREE( *errstrp );
1034 *errstrp = unfollowed;
1036 return(( rc == 0 ) ? count : rc );
1041 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1045 if ( *referralsp == NULL ) {
1047 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1051 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1052 strlen( *referralsp ) + strlen( s ) + 2 );
1055 if ( *referralsp == NULL ) {
1056 ld->ld_errno = LDAP_NO_MEMORY;
1061 strcpy( *referralsp, LDAP_REF_STR );
1063 strcat( *referralsp, "\n" );
1065 strcat( *referralsp, s );
1073 re_encode_request( LDAP *ld,
1074 BerElement *origber,
1081 * XXX this routine knows way too much about how the lber library works!
1089 BerElement tmpber, *ber;
1094 LDAP_LOG (( "request", LDAP_LEVEL_ARGS,
1095 "re_encode_request: new msgid %ld, new dn <%s>\n",
1097 ( srv == NULL || srv->lud_dn == NULL ) ? "NONE" : srv->lud_dn ));
1099 Debug( LDAP_DEBUG_TRACE,
1100 "re_encode_request: new msgid %ld, new dn <%s>\n",
1102 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1108 * all LDAP requests are sequences that start with a message id.
1109 * For all except delete, this is followed by a sequence that is
1110 * tagged with the operation code. For delete, the provided DN
1111 * is not wrapped by a sequence.
1113 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1115 if ( rtag == LBER_ERROR ) {
1116 ld->ld_errno = LDAP_DECODING_ERROR;
1121 if ( tag == LDAP_REQ_BIND ) {
1122 /* bind requests have a version number before the DN & other stuff */
1123 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1125 } else if ( tag == LDAP_REQ_DELETE ) {
1126 /* delete requests don't have a DN wrapping sequence */
1127 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1129 } else if ( tag == LDAP_REQ_SEARCH ) {
1130 /* search requests need to be re-scope-ed */
1131 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1133 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1134 /* use the scope provided in reference */
1135 scope = srv->lud_scope;
1137 } else if ( sref && scope != LDAP_SCOPE_SUBTREE ) {
1138 /* use scope implied by previous operation */
1141 /* subtree -> subtree */
1142 scope = LDAP_SCOPE_BASE;
1146 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1149 if( rtag == LBER_ERROR ) {
1150 ld->ld_errno = LDAP_DECODING_ERROR;
1154 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1158 if ( srv->lud_dn == NULL ) {
1164 if ( tag == LDAP_REQ_BIND ) {
1165 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1166 } else if ( tag == LDAP_REQ_DELETE ) {
1167 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1168 } else if ( tag == LDAP_REQ_SEARCH ) {
1169 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1171 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1174 LDAP_FREE( orig_dn );
1177 ld->ld_errno = LDAP_ENCODING_ERROR;
1182 if ( tag != LDAP_REQ_DELETE && (
1183 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1184 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1185 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1187 ld->ld_errno = LDAP_ENCODING_ERROR;
1193 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1195 LDAP_LOG (( "request", LDAP_LEVEL_DETAIL1,
1196 "re_encode_request: new request is:\n" ));
1198 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1201 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1203 #endif /* LDAP_DEBUG */
1205 *type = tag; /* return request type */
1211 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1215 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1216 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1217 continue; /* Skip completed requests */
1219 if ( msgid == lr->lr_msgid ) {