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 ( open_ldap_connection( ld, lc->lconn_sb,
260 srv, &lc->lconn_krbinstance, 0 ) != -1 )
268 ber_sockbuf_free( lc->lconn_sb );
270 LDAP_FREE( (char *)lc );
271 ld->ld_errno = LDAP_SERVER_DOWN;
275 lc->lconn_server = ldap_url_dup(srv);
278 lc->lconn_status = LDAP_CONNST_CONNECTED;
279 lc->lconn_next = ld->ld_conns;
283 * XXX for now, we always do a synchronous bind. This will have
284 * to change in the long run...
288 LDAPConn *savedefconn;
290 /* Set flag to prevent additional referrals from being processed on this
291 * connection until the bind has completed
293 lc->lconn_rebind_inprogress = 1;
294 /* V3 rebind function */
295 if ( ld->ld_rebindproc != NULL) {
296 LDAPURLDesc *srvfunc;
297 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
298 ld->ld_errno = LDAP_NO_MEMORY;
301 savedefconn = ld->ld_defconn;
302 ++lc->lconn_refcnt; /* avoid premature free */
305 Debug( LDAP_DEBUG_TRACE, "Call application rebindproc\n", 0, 0, 0);
306 err = (*ld->ld_rebindproc)( ld, bind->ri_url, bind->ri_request, bind->ri_msgid);
308 ld->ld_defconn = savedefconn;
313 ldap_free_connection( ld, lc, 1, 0 );
316 ldap_free_urldesc( srvfunc);
319 savedefconn = ld->ld_defconn;
320 ++lc->lconn_refcnt; /* avoid premature free */
323 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
324 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
327 ld->ld_defconn = savedefconn;
331 ldap_free_connection( ld, lc, 1, 0 );
336 lc->lconn_rebind_inprogress = 0;
344 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
346 * return an existing connection (if any) to the server srv
347 * if "any" is non-zero, check for any server in the "srv" chain
353 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
354 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
355 if ( lc->lconn_server->lud_host != NULL &&
356 ls->lud_host != NULL && strcasecmp(
357 ls->lud_host, lc->lconn_server->lud_host ) == 0
358 && ls->lud_port == lc->lconn_server->lud_port ) {
373 use_connection( LDAP *ld, LDAPConn *lc )
376 lc->lconn_lastused = time( NULL );
381 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
383 LDAPConn *tmplc, *prevlc;
385 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
387 if ( force || --lc->lconn_refcnt <= 0 ) {
388 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
389 ldap_mark_select_clear( ld, lc->lconn_sb );
391 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
395 if( lc->lconn_ber != NULL ) {
396 ber_free( lc->lconn_ber, 1 );
400 for ( tmplc = ld->ld_conns; tmplc != NULL;
401 tmplc = tmplc->lconn_next ) {
403 if ( prevlc == NULL ) {
404 ld->ld_conns = tmplc->lconn_next;
406 prevlc->lconn_next = tmplc->lconn_next;
412 ldap_free_urllist( lc->lconn_server );
413 if ( lc->lconn_krbinstance != NULL ) {
414 LDAP_FREE( lc->lconn_krbinstance );
416 if ( lc->lconn_sb != ld->ld_sb ) {
417 ber_sockbuf_free( lc->lconn_sb );
419 if( lc->lconn_rebind_queue != NULL) {
421 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
422 LDAP_VFREE(lc->lconn_rebind_queue[i]);
424 LDAP_FREE( lc->lconn_rebind_queue);
427 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
430 lc->lconn_lastused = time( NULL );
431 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
432 lc->lconn_refcnt, 0, 0 );
439 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
444 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
445 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
446 if ( lc->lconn_server != NULL ) {
447 fprintf( stderr, "* host: %s port: %d%s\n",
448 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
449 : lc->lconn_server->lud_host,
450 lc->lconn_server->lud_port, ( lc->lconn_sb ==
451 ld->ld_sb ) ? " (default)" : "" );
453 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
454 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
455 "NeedSocket" : ( lc->lconn_status ==
456 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
457 fprintf( stderr, " last used: %s",
458 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
459 if( lc->lconn_rebind_inprogress ) {
460 fprintf( stderr, " rebind in progress\n");
461 if( lc->lconn_rebind_queue != NULL) {
463 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
465 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
466 fprintf( stderr, " queue %d entry %d - %s\n",
467 i, j, lc->lconn_rebind_queue[i][j]);
471 fprintf( stderr, " queue is empty\n");
474 fprintf(stderr, "\n");
483 ldap_dump_requests_and_responses( LDAP *ld )
488 fprintf( stderr, "** Outstanding Requests:\n" );
489 if (( lr = ld->ld_requests ) == NULL ) {
490 fprintf( stderr, " Empty\n" );
492 for ( ; lr != NULL; lr = lr->lr_next ) {
493 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
494 lr->lr_msgid, lr->lr_origid,
495 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
496 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
497 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
498 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
499 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
500 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
501 lr->lr_outrefcnt, lr->lr_parentcnt );
504 fprintf( stderr, "** Response Queue:\n" );
505 if (( lm = ld->ld_responses ) == NULL ) {
506 fprintf( stderr, " Empty\n" );
508 for ( ; lm != NULL; lm = lm->lm_next ) {
509 fprintf( stderr, " * msgid %d, type %lu\n",
510 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
511 if (( l = lm->lm_chain ) != NULL ) {
512 fprintf( stderr, " chained responses:\n" );
513 for ( ; l != NULL; l = l->lm_chain ) {
515 " * msgid %d, type %lu\n",
517 (unsigned long) l->lm_msgtype );
522 #endif /* LDAP_DEBUG */
526 ldap_free_request( LDAP *ld, LDAPRequest *lr )
528 LDAPRequest *tmplr, *nextlr;
530 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
531 lr->lr_origid, lr->lr_msgid, 0 );
533 if ( lr->lr_parent != NULL ) {
534 --lr->lr_parent->lr_outrefcnt;
536 /* free all referrals (child requests) */
537 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
538 nextlr = tmplr->lr_refnext;
539 ldap_free_request( ld, tmplr );
543 if ( lr->lr_prev == NULL ) {
544 ld->ld_requests = lr->lr_next;
546 lr->lr_prev->lr_next = lr->lr_next;
549 if ( lr->lr_next != NULL ) {
550 lr->lr_next->lr_prev = lr->lr_prev;
553 if ( lr->lr_ber != NULL ) {
554 ber_free( lr->lr_ber, 1 );
557 if ( lr->lr_res_error != NULL ) {
558 LDAP_FREE( lr->lr_res_error );
561 if ( lr->lr_res_matched != NULL ) {
562 LDAP_FREE( lr->lr_res_matched );
572 * (IN) ld = LDAP connection handle
573 * (IN) lr = LDAP Request structure
574 * (IN) refs = array of pointers to referral strings that we will chase
575 * The array will be free'd by this function when no longer needed
576 * (OUT) errstrp = Place to return a string of referrals which could not be followed
577 * (OUT) hadrefp = 1 if sucessfully followed referral
579 * Return value - number of referrals followed
582 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, int *hadrefp )
585 int unfollowedcnt = 0;
586 LDAPRequest *origreq;
587 LDAPURLDesc *srv = NULL;
589 char **refarray = NULL;
594 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
597 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
602 /* If no referrals in array, return */
603 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
608 /* Check for hop limit exceeded */
609 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
610 Debug( LDAP_DEBUG_ANY,
611 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
612 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
617 /* find original request */
618 for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) {
624 /* parse out & follow referrals */
625 for( i=0; refarray[i] != NULL; i++) {
626 /* Parse the referral URL */
627 if (( rc = ldap_url_parse( refarray[i], &srv)) != LDAP_SUCCESS) {
633 /* treat ldap://hostpart and ldap://hostpart/ the same */
634 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
635 LDAP_FREE( srv->lud_dn );
639 /* check connection for re-bind in progress */
640 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
641 if( lc->lconn_rebind_inprogress) {
642 /* We are already chasing a referral or search reference and a
643 * bind on that connection is in progress. We must queue
644 * referrals on that connection, so we don't get a request
645 * going out before the bind operation completes. This happens
646 * if two search references come in one behind the other
647 * for the same server with different contexts.
649 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals: queue referral \"%s\"\n",
651 if( lc->lconn_rebind_queue == NULL ) {
652 /* Create a referral list */
653 if( (lc->lconn_rebind_queue = (char ***)LDAP_MALLOC( sizeof(void *) * 2)) == NULL) {
654 ld->ld_errno = LDAP_NO_MEMORY;
658 lc->lconn_rebind_queue[0] = refarray;
659 lc->lconn_rebind_queue[1] = NULL;
662 /* Count how many referral arrays we already have */
663 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
666 /* Add the new referral to the list */
667 if( (lc->lconn_rebind_queue = (char ***)LDAP_REALLOC(
668 lc->lconn_rebind_queue, sizeof(void *) * (j + 2))) == NULL) {
669 ld->ld_errno = LDAP_NO_MEMORY;
673 lc->lconn_rebind_queue[j] = refarray;
674 lc->lconn_rebind_queue[j+1] = NULL;
677 /* We have queued the referral/reference, now just return */
680 count = 1; /* Pretend we already followed referral */
684 /* Re-encode the request with the new starting point of the search.
685 * Note: In the future we also need to replace the filter if one
686 * was provided with the search reference
688 if (( ber = re_encode_request( ld, origreq->lr_ber,
689 ++ld->ld_msgid, &srv->lud_dn, &rinfo.ri_request )) == NULL ) {
690 ld->ld_errno = LDAP_ENCODING_ERROR;
695 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
696 lr->lr_msgid, refarray[i], 0);
698 /* Send the new request to the server - may require a bind */
699 rinfo.ri_msgid = origreq->lr_origid;
700 rinfo.ri_url = refarray[i];
701 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
702 origreq, srv, NULL, &rinfo )) < 0 ) {
703 /* Failure, try next referral in the list */
704 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
705 refarray[i], ldap_err2string( ld->ld_errno ), 0);
706 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
707 ldap_free_urllist(srv);
710 /* Success, no need to try this referral list further */
715 /* check if there is a queue of referrals that came in during bind */
717 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
718 ld->ld_errno = LDAP_OPERATIONS_ERROR;
724 if( lc->lconn_rebind_queue != NULL) {
725 /* Release resources of previous list */
726 LDAP_VFREE(refarray);
728 ldap_free_urllist(srv);
731 /* Pull entries off end of queue so list always null terminated */
732 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
735 refarray = lc->lconn_rebind_queue[j-1];
736 lc->lconn_rebind_queue[j-1] = NULL;
737 /* we pulled off last entry from queue, free queue */
739 LDAP_FREE( lc->lconn_rebind_queue);
740 lc->lconn_rebind_queue = NULL;
742 /* restart the loop the with new referral list */
746 break; /* referral followed, break out of for loop */
750 LDAP_VFREE(refarray);
751 ldap_free_urllist(srv);
752 LDAP_FREE( *errstrp );
756 LDAP_FREE( unfollowed );
759 ld->ld_errno = LDAP_REFERRAL;
760 *errstrp = unfollowed;
766 * XXX merging of errors in this routine needs to be improved
769 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
771 int rc, count, len, newdn;
772 char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
773 LDAPRequest *origreq;
778 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
780 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
783 if ( *errstrp == NULL ) {
787 len = strlen( *errstrp );
788 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
789 if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
790 LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
792 p += LDAP_REF_STR_LEN;
797 if ( len < LDAP_REF_STR_LEN ) {
801 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
802 Debug( LDAP_DEBUG_ANY,
803 "more than %d referral hops (dropping)\n",
804 ld->ld_refhoplimit, 0, 0 );
805 /* XXX report as error in ld->ld_errno? */
809 /* find original request */
810 for ( origreq = lr; origreq->lr_parent != NULL;
811 origreq = origreq->lr_parent ) {
818 /* parse out & follow referrals */
819 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
821 if (( p = strchr( ref, '\n' )) != NULL ) {
827 ldap_pvt_hex_unescape( ref );
830 /* FIXME: we should use the URL Parser */
832 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
833 LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
834 Debug( LDAP_DEBUG_TRACE,
835 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
836 tmpref = ref + LDAP_LDAP_REF_STR_LEN;
838 Debug( LDAP_DEBUG_TRACE,
839 "ignoring unknown referral <%s>\n", ref, 0, 0 );
840 rc = ldap_append_referral( ld, &unfollowed, ref );
845 /* copy the complete referral for rebind process */
846 rinfo.ri_url = LDAP_STRDUP( ref );
850 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
852 newdn = refdn[0] != '?' && refdn[0] != '\0';
853 if( !newdn ) refdn = NULL;
858 if (( ber = re_encode_request( ld, origreq->lr_ber,
859 ++ld->ld_msgid, &refdn, &rinfo.ri_request )) == NULL ) {
863 if (( srv = (LDAPURLDesc *)LDAP_CALLOC( 1,
864 sizeof( LDAPURLDesc ))) == NULL ) {
866 ld->ld_errno = LDAP_NO_MEMORY;
870 if (( srv->lud_scheme = LDAP_STRDUP("ldap")) == NULL ) {
871 LDAP_FREE( (char *)srv );
873 ld->ld_errno = LDAP_NO_MEMORY;
877 if (( srv->lud_host = LDAP_STRDUP( tmpref )) == NULL ) {
878 LDAP_FREE( (char *)srv );
880 ld->ld_errno = LDAP_NO_MEMORY;
884 if (( ports = strchr( srv->lud_host, ':' )) != NULL ) {
886 srv->lud_port = atoi( ports );
888 srv->lud_port = (LDAP_INT_GLOBAL_OPT())->ldo_defport;
891 rinfo.ri_msgid = origreq->lr_origid;
892 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
893 lr, srv, NULL, &rinfo ) >= 0 ) {
896 Debug( LDAP_DEBUG_ANY,
897 "Unable to chase referral (%s)\n",
898 ldap_err2string( ld->ld_errno ), 0, 0 );
899 rc = ldap_append_referral( ld, &unfollowed, ref );
901 LDAP_FREE( rinfo.ri_url);
904 ldap_free_urllist(srv);
906 if ( !newdn && refdn != NULL ) {
911 LDAP_FREE( *errstrp );
912 *errstrp = unfollowed;
914 return(( rc == 0 ) ? count : rc );
919 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
923 if ( *referralsp == NULL ) {
925 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
929 *referralsp = (char *)LDAP_REALLOC( *referralsp,
930 strlen( *referralsp ) + strlen( s ) + 2 );
933 if ( *referralsp == NULL ) {
934 ld->ld_errno = LDAP_NO_MEMORY;
939 strcpy( *referralsp, LDAP_REF_STR );
941 strcat( *referralsp, "\n" );
943 strcat( *referralsp, s );
951 re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, int *type )
954 * XXX this routine knows way too much about how the lber library works!
960 BerElement tmpber, *ber;
963 Debug( LDAP_DEBUG_TRACE,
964 "re_encode_request: new msgid %ld, new dn <%s>\n",
965 (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
970 * all LDAP requests are sequences that start with a message id.
971 * For all except delete, this is followed by a sequence that is
972 * tagged with the operation code. For delete, the provided DN
973 * is not wrapped by a sequence.
975 rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
977 if ( rc == LBER_ERROR ) {
978 ld->ld_errno = LDAP_DECODING_ERROR;
983 if ( tag == LDAP_REQ_BIND ) {
984 /* bind requests have a version number before the DN & other stuff */
985 rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
987 } else if ( tag == LDAP_REQ_DELETE ) {
988 /* delete requests don't have a DN wrapping sequence */
989 rc = ber_scanf( &tmpber, "a", &orig_dn );
992 rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
995 if( rc == LBER_ERROR ) {
996 ld->ld_errno = LDAP_DECODING_ERROR;
1000 if ( *dnp == NULL ) {
1003 LDAP_FREE( orig_dn );
1006 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1010 if ( tag == LDAP_REQ_BIND ) {
1011 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
1012 } else if ( tag == LDAP_REQ_DELETE ) {
1013 rc = ber_printf( ber, "{itsN}", msgid, tag, *dnp );
1015 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp );
1019 ld->ld_errno = LDAP_ENCODING_ERROR;
1024 if ( tag != LDAP_REQ_DELETE && (
1025 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1026 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1027 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1029 ld->ld_errno = LDAP_ENCODING_ERROR;
1035 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1036 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1038 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1040 #endif /* LDAP_DEBUG */
1042 *type = tag; /* return request type */
1048 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1052 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1053 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1054 continue; /* Skip completed requests */
1056 if ( msgid == lr->lr_msgid ) {