3 * Copyright 1998-2000 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 static BerElement *re_encode_request LDAP_P((
58 ldap_alloc_ber_with_options( LDAP *ld )
62 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
63 ld->ld_errno = LDAP_NO_MEMORY;
71 ldap_set_ber_options( LDAP *ld, BerElement *ber )
73 ber->ber_options = ld->ld_lberoptions;
78 ldap_send_initial_request(
87 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
89 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
90 /* not connected yet */
91 int rc = ldap_open_defconn( ld );
98 Debug( LDAP_DEBUG_TRACE,
99 "ldap_delayed_open successful, ld_host is %s\n",
100 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
105 * use of DNS is turned off or this is an X.500 DN...
106 * use our default connection
111 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
112 servers, NULL, NULL );
114 ldap_free_urllist(servers);
121 ldap_send_server_request(
125 LDAPRequest *parentreq,
126 LDAPURLDesc *srvlist,
133 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
136 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
139 if ( srvlist == NULL ) {
142 if (( lc = find_connection( ld, srvlist, 1 )) ==
144 if ( (bind != NULL) && (parentreq != NULL) ) {
145 /* Remember the bind in the parent */
147 ++parentreq->lr_outrefcnt;
149 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
154 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
156 if ( ld->ld_errno == LDAP_SUCCESS ) {
157 ld->ld_errno = LDAP_SERVER_DOWN;
160 /* Forget about the bind */
161 --parentreq->lr_outrefcnt;
166 use_connection( ld, lc );
167 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
169 ld->ld_errno = LDAP_NO_MEMORY;
170 ldap_free_connection( ld, lc, 0, 0 );
173 /* Forget about the bind */
174 --parentreq->lr_outrefcnt;
178 lr->lr_msgid = msgid;
179 lr->lr_status = LDAP_REQST_INPROGRESS;
180 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
183 if ( parentreq != NULL ) { /* sub-request */
185 /* Increment if we didn't do it before the bind */
186 ++parentreq->lr_outrefcnt;
188 lr->lr_origid = parentreq->lr_origid;
189 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
190 lr->lr_parent = parentreq;
191 lr->lr_refnext = parentreq->lr_refnext;
192 parentreq->lr_refnext = lr;
193 } else { /* original request */
194 lr->lr_origid = lr->lr_msgid;
197 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
198 lr->lr_next->lr_prev = lr;
200 ld->ld_requests = lr;
203 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
205 if ( errno == EWOULDBLOCK ) {
206 /* need to continue write later */
207 lr->lr_status = LDAP_REQST_WRITING;
208 ldap_mark_select_write( ld, lc->lconn_sb );
211 ld->ld_errno = LDAP_SERVER_DOWN;
212 ldap_free_request( ld, lr );
213 ldap_free_connection( ld, lc, 0, 0 );
220 if ( parentreq == NULL ) {
221 ber->ber_end = ber->ber_ptr;
222 ber->ber_ptr = ber->ber_buf;
225 /* sent -- waiting for a response */
226 ldap_mark_select_read( ld, lc->lconn_sb );
229 ld->ld_errno = LDAP_SUCCESS;
234 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
235 int connect, LDAPreqinfo *bind )
241 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
243 * make a new LDAP server connection
244 * XXX open connection synchronously for now
246 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
247 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
249 LDAP_FREE( (char *)lc );
251 ld->ld_errno = LDAP_NO_MEMORY;
255 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
258 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
259 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
266 ber_sockbuf_free( lc->lconn_sb );
268 LDAP_FREE( (char *)lc );
269 ld->ld_errno = LDAP_SERVER_DOWN;
273 lc->lconn_server = ldap_url_dup(srv);
276 lc->lconn_status = LDAP_CONNST_CONNECTED;
277 lc->lconn_next = ld->ld_conns;
281 * XXX for now, we always do a synchronous bind. This will have
282 * to change in the long run...
286 LDAPConn *savedefconn;
288 /* Set flag to prevent additional referrals from being processed on this
289 * connection until the bind has completed
291 lc->lconn_rebind_inprogress = 1;
292 /* V3 rebind function */
293 if ( ld->ld_rebindproc != NULL) {
294 LDAPURLDesc *srvfunc;
295 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
296 ld->ld_errno = LDAP_NO_MEMORY;
299 savedefconn = ld->ld_defconn;
300 ++lc->lconn_refcnt; /* avoid premature free */
303 Debug( LDAP_DEBUG_TRACE, "Call application rebindproc\n", 0, 0, 0);
304 err = (*ld->ld_rebindproc)( ld, bind->ri_url, bind->ri_request, bind->ri_msgid);
306 ld->ld_defconn = savedefconn;
311 ldap_free_connection( ld, lc, 1, 0 );
314 ldap_free_urldesc( srvfunc);
317 savedefconn = ld->ld_defconn;
318 ++lc->lconn_refcnt; /* avoid premature free */
321 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
322 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
325 ld->ld_defconn = savedefconn;
329 ldap_free_connection( ld, lc, 1, 0 );
334 lc->lconn_rebind_inprogress = 0;
342 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
344 * return an existing connection (if any) to the server srv
345 * if "any" is non-zero, check for any server in the "srv" chain
351 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
352 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
353 if ( lc->lconn_server->lud_host != NULL &&
354 *lc->lconn_server->lud_host != '\0' &&
355 ls->lud_host != NULL && *ls->lud_host != '\0' &&
356 strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0
357 && ls->lud_port == lc->lconn_server->lud_port ) {
372 use_connection( LDAP *ld, LDAPConn *lc )
375 lc->lconn_lastused = time( NULL );
380 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
382 LDAPConn *tmplc, *prevlc;
384 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
386 if ( force || --lc->lconn_refcnt <= 0 ) {
387 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
388 ldap_mark_select_clear( ld, lc->lconn_sb );
390 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
394 if( lc->lconn_ber != NULL ) {
395 ber_free( lc->lconn_ber, 1 );
398 ldap_int_sasl_close( ld, lc );
401 for ( tmplc = ld->ld_conns; tmplc != NULL;
402 tmplc = tmplc->lconn_next ) {
404 if ( prevlc == NULL ) {
405 ld->ld_conns = tmplc->lconn_next;
407 prevlc->lconn_next = tmplc->lconn_next;
413 ldap_free_urllist( lc->lconn_server );
414 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
415 if ( lc->lconn_krbinstance != NULL ) {
416 LDAP_FREE( lc->lconn_krbinstance );
419 if ( lc->lconn_sb != ld->ld_sb ) {
420 ber_sockbuf_free( lc->lconn_sb );
422 if( lc->lconn_rebind_queue != NULL) {
424 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
425 LDAP_VFREE(lc->lconn_rebind_queue[i]);
427 LDAP_FREE( lc->lconn_rebind_queue);
430 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
433 lc->lconn_lastused = time( NULL );
434 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
435 lc->lconn_refcnt, 0, 0 );
442 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
447 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
448 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
449 if ( lc->lconn_server != NULL ) {
450 fprintf( stderr, "* host: %s port: %d%s\n",
451 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
452 : lc->lconn_server->lud_host,
453 lc->lconn_server->lud_port, ( lc->lconn_sb ==
454 ld->ld_sb ) ? " (default)" : "" );
456 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
457 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
458 "NeedSocket" : ( lc->lconn_status ==
459 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
460 fprintf( stderr, " last used: %s",
461 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
462 if( lc->lconn_rebind_inprogress ) {
463 fprintf( stderr, " rebind in progress\n");
464 if( lc->lconn_rebind_queue != NULL) {
466 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
468 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
469 fprintf( stderr, " queue %d entry %d - %s\n",
470 i, j, lc->lconn_rebind_queue[i][j]);
474 fprintf( stderr, " queue is empty\n");
477 fprintf(stderr, "\n");
486 ldap_dump_requests_and_responses( LDAP *ld )
491 fprintf( stderr, "** Outstanding Requests:\n" );
492 if (( lr = ld->ld_requests ) == NULL ) {
493 fprintf( stderr, " Empty\n" );
495 for ( ; lr != NULL; lr = lr->lr_next ) {
496 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
497 lr->lr_msgid, lr->lr_origid,
498 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
499 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
500 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
501 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
502 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
503 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
504 lr->lr_outrefcnt, lr->lr_parentcnt );
507 fprintf( stderr, "** Response Queue:\n" );
508 if (( lm = ld->ld_responses ) == NULL ) {
509 fprintf( stderr, " Empty\n" );
511 for ( ; lm != NULL; lm = lm->lm_next ) {
512 fprintf( stderr, " * msgid %d, type %lu\n",
513 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
514 if (( l = lm->lm_chain ) != NULL ) {
515 fprintf( stderr, " chained responses:\n" );
516 for ( ; l != NULL; l = l->lm_chain ) {
518 " * msgid %d, type %lu\n",
520 (unsigned long) l->lm_msgtype );
525 #endif /* LDAP_DEBUG */
529 ldap_free_request( LDAP *ld, LDAPRequest *lr )
531 LDAPRequest *tmplr, *nextlr;
533 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
534 lr->lr_origid, lr->lr_msgid, 0 );
536 if ( lr->lr_parent != NULL ) {
537 --lr->lr_parent->lr_outrefcnt;
539 /* free all referrals (child requests) */
540 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
541 nextlr = tmplr->lr_refnext;
542 ldap_free_request( ld, tmplr );
546 if ( lr->lr_prev == NULL ) {
547 ld->ld_requests = lr->lr_next;
549 lr->lr_prev->lr_next = lr->lr_next;
552 if ( lr->lr_next != NULL ) {
553 lr->lr_next->lr_prev = lr->lr_prev;
556 if ( lr->lr_ber != NULL ) {
557 ber_free( lr->lr_ber, 1 );
560 if ( lr->lr_res_error != NULL ) {
561 LDAP_FREE( lr->lr_res_error );
564 if ( lr->lr_res_matched != NULL ) {
565 LDAP_FREE( lr->lr_res_matched );
575 * (IN) ld = LDAP connection handle
576 * (IN) lr = LDAP Request structure
577 * (IN) refs = array of pointers to referral strings that we will chase
578 * The array will be free'd by this function when no longer needed
579 * (IN) sref != 0 if following search reference
580 * (OUT) errstrp = Place to return a string of referrals which could not be followed
581 * (OUT) hadrefp = 1 if sucessfully followed referral
583 * Return value - number of referrals followed
586 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
589 int unfollowedcnt = 0;
590 LDAPRequest *origreq;
591 LDAPURLDesc *srv = NULL;
593 char **refarray = NULL;
598 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
601 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
606 /* If no referrals in array, return */
607 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
612 /* Check for hop limit exceeded */
613 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
614 Debug( LDAP_DEBUG_ANY,
615 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
616 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
621 /* find original request */
622 for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) {
628 /* parse out & follow referrals */
629 for( i=0; refarray[i] != NULL; i++) {
630 /* Parse the referral URL */
631 if (( rc = ldap_url_parse( refarray[i], &srv)) != LDAP_SUCCESS) {
637 /* treat ldap://hostpart and ldap://hostpart/ the same */
638 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
639 LDAP_FREE( srv->lud_dn );
643 /* check connection for re-bind in progress */
644 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
645 if( lc->lconn_rebind_inprogress) {
646 /* We are already chasing a referral or search reference and a
647 * bind on that connection is in progress. We must queue
648 * referrals on that connection, so we don't get a request
649 * going out before the bind operation completes. This happens
650 * if two search references come in one behind the other
651 * for the same server with different contexts.
653 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals: queue referral \"%s\"\n",
655 if( lc->lconn_rebind_queue == NULL ) {
656 /* Create a referral list */
657 if( (lc->lconn_rebind_queue = (char ***)LDAP_MALLOC( sizeof(void *) * 2)) == NULL) {
658 ld->ld_errno = LDAP_NO_MEMORY;
662 lc->lconn_rebind_queue[0] = refarray;
663 lc->lconn_rebind_queue[1] = NULL;
666 /* Count how many referral arrays we already have */
667 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
670 /* Add the new referral to the list */
671 if( (lc->lconn_rebind_queue = (char ***)LDAP_REALLOC(
672 lc->lconn_rebind_queue, sizeof(void *) * (j + 2))) == NULL) {
673 ld->ld_errno = LDAP_NO_MEMORY;
677 lc->lconn_rebind_queue[j] = refarray;
678 lc->lconn_rebind_queue[j+1] = NULL;
681 /* We have queued the referral/reference, now just return */
684 count = 1; /* Pretend we already followed referral */
688 /* Re-encode the request with the new starting point of the search.
689 * Note: In the future we also need to replace the filter if one
690 * was provided with the search reference
693 /* For references we don't want old dn if new dn empty */
694 if ( sref && srv->lud_dn == NULL )
695 srv->lud_dn = LDAP_STRDUP( "" );
697 if (( ber = re_encode_request( ld, origreq->lr_ber,
698 ++ld->ld_msgid, &srv->lud_dn, &rinfo.ri_request )) == NULL ) {
699 ld->ld_errno = LDAP_ENCODING_ERROR;
704 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
705 lr->lr_msgid, refarray[i], 0);
707 /* Send the new request to the server - may require a bind */
708 rinfo.ri_msgid = origreq->lr_origid;
709 rinfo.ri_url = refarray[i];
710 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
711 origreq, srv, NULL, &rinfo )) < 0 ) {
712 /* Failure, try next referral in the list */
713 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
714 refarray[i], ldap_err2string( ld->ld_errno ), 0);
715 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
716 ldap_free_urllist(srv);
719 /* Success, no need to try this referral list further */
724 /* check if there is a queue of referrals that came in during bind */
726 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
727 ld->ld_errno = LDAP_OPERATIONS_ERROR;
733 if( lc->lconn_rebind_queue != NULL) {
734 /* Release resources of previous list */
735 LDAP_VFREE(refarray);
737 ldap_free_urllist(srv);
740 /* Pull entries off end of queue so list always null terminated */
741 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
744 refarray = lc->lconn_rebind_queue[j-1];
745 lc->lconn_rebind_queue[j-1] = NULL;
746 /* we pulled off last entry from queue, free queue */
748 LDAP_FREE( lc->lconn_rebind_queue);
749 lc->lconn_rebind_queue = NULL;
751 /* restart the loop the with new referral list */
755 break; /* referral followed, break out of for loop */
759 LDAP_VFREE(refarray);
760 ldap_free_urllist(srv);
761 LDAP_FREE( *errstrp );
765 LDAP_FREE( unfollowed );
768 ld->ld_errno = LDAP_REFERRAL;
769 *errstrp = unfollowed;
775 * XXX merging of errors in this routine needs to be improved
778 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
780 int rc, count, len, newdn;
781 char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
782 LDAPRequest *origreq;
787 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
789 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
792 if ( *errstrp == NULL ) {
796 len = strlen( *errstrp );
797 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
798 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
800 p += LDAP_REF_STR_LEN;
805 if ( len < LDAP_REF_STR_LEN ) {
809 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
810 Debug( LDAP_DEBUG_ANY,
811 "more than %d referral hops (dropping)\n",
812 ld->ld_refhoplimit, 0, 0 );
813 /* XXX report as error in ld->ld_errno? */
817 /* find original request */
818 for ( origreq = lr; origreq->lr_parent != NULL;
819 origreq = origreq->lr_parent ) {
826 /* parse out & follow referrals */
827 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
829 if (( p = strchr( ref, '\n' )) != NULL ) {
835 /* copy the complete referral for rebind process */
836 rinfo.ri_url = LDAP_STRDUP( ref );
838 ldap_pvt_hex_unescape( ref );
841 /* FIXME: we should use the URL Parser */
843 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
844 LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
845 Debug( LDAP_DEBUG_TRACE,
846 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
847 tmpref = ref + LDAP_LDAP_REF_STR_LEN;
849 Debug( LDAP_DEBUG_TRACE,
850 "ignoring unknown referral <%s>\n", ref, 0, 0 );
851 rc = ldap_append_referral( ld, &unfollowed, ref );
853 LDAP_FREE( rinfo.ri_url );
860 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
862 newdn = refdn[0] != '?' && refdn[0] != '\0';
863 if( !newdn ) refdn = NULL;
868 if (( ber = re_encode_request( ld, origreq->lr_ber,
869 ++ld->ld_msgid, &refdn, &rinfo.ri_request )) == NULL ) {
873 if (( srv = (LDAPURLDesc *)LDAP_CALLOC( 1,
874 sizeof( LDAPURLDesc ))) == NULL ) {
876 ld->ld_errno = LDAP_NO_MEMORY;
880 if (( srv->lud_scheme = LDAP_STRDUP("ldap")) == NULL ) {
881 LDAP_FREE( (char *)srv );
883 ld->ld_errno = LDAP_NO_MEMORY;
887 if (( srv->lud_host = LDAP_STRDUP( tmpref )) == NULL ) {
888 LDAP_FREE( (char *)srv );
890 ld->ld_errno = LDAP_NO_MEMORY;
894 if (( ports = strchr( srv->lud_host, ':' )) != NULL ) {
896 srv->lud_port = atoi( ports );
898 srv->lud_port = (LDAP_INT_GLOBAL_OPT())->ldo_defport;
901 rinfo.ri_msgid = origreq->lr_origid;
902 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
903 lr, srv, NULL, &rinfo ) >= 0 ) {
906 Debug( LDAP_DEBUG_ANY,
907 "Unable to chase referral (%s)\n",
908 ldap_err2string( ld->ld_errno ), 0, 0 );
909 rc = ldap_append_referral( ld, &unfollowed, ref );
911 LDAP_FREE( rinfo.ri_url);
914 ldap_free_urllist(srv);
916 if ( !newdn && refdn != NULL ) {
921 LDAP_FREE( *errstrp );
922 *errstrp = unfollowed;
924 return(( rc == 0 ) ? count : rc );
929 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
933 if ( *referralsp == NULL ) {
935 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
939 *referralsp = (char *)LDAP_REALLOC( *referralsp,
940 strlen( *referralsp ) + strlen( s ) + 2 );
943 if ( *referralsp == NULL ) {
944 ld->ld_errno = LDAP_NO_MEMORY;
949 strcpy( *referralsp, LDAP_REF_STR );
951 strcat( *referralsp, "\n" );
953 strcat( *referralsp, s );
961 re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, int *type )
964 * XXX this routine knows way too much about how the lber library works!
970 BerElement tmpber, *ber;
973 Debug( LDAP_DEBUG_TRACE,
974 "re_encode_request: new msgid %ld, new dn <%s>\n",
975 (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
980 * all LDAP requests are sequences that start with a message id.
981 * For all except delete, this is followed by a sequence that is
982 * tagged with the operation code. For delete, the provided DN
983 * is not wrapped by a sequence.
985 rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
987 if ( rc == LBER_ERROR ) {
988 ld->ld_errno = LDAP_DECODING_ERROR;
993 if ( tag == LDAP_REQ_BIND ) {
994 /* bind requests have a version number before the DN & other stuff */
995 rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
997 } else if ( tag == LDAP_REQ_DELETE ) {
998 /* delete requests don't have a DN wrapping sequence */
999 rc = ber_scanf( &tmpber, "a", &orig_dn );
1002 rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1005 if( rc == LBER_ERROR ) {
1006 ld->ld_errno = LDAP_DECODING_ERROR;
1010 if ( *dnp == NULL ) {
1013 LDAP_FREE( orig_dn );
1016 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1020 if ( tag == LDAP_REQ_BIND ) {
1021 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
1022 } else if ( tag == LDAP_REQ_DELETE ) {
1023 rc = ber_printf( ber, "{itsN}", msgid, tag, *dnp );
1025 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp );
1029 ld->ld_errno = LDAP_ENCODING_ERROR;
1034 if ( tag != LDAP_REQ_DELETE && (
1035 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1036 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1037 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1039 ld->ld_errno = LDAP_ENCODING_ERROR;
1045 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1046 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1048 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1050 #endif /* LDAP_DEBUG */
1052 *type = tag; /* return request type */
1058 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1062 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1063 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1064 continue; /* Skip completed requests */
1066 if ( msgid == lr->lr_msgid ) {