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 re_encode_request( LDAP *ld,
56 ldap_alloc_ber_with_options( LDAP *ld )
60 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
61 ld->ld_errno = LDAP_NO_MEMORY;
69 ldap_set_ber_options( LDAP *ld, BerElement *ber )
71 ber->ber_options = ld->ld_lberoptions;
76 ldap_send_initial_request(
85 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
87 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
88 /* not connected yet */
89 int rc = ldap_open_defconn( ld );
96 Debug( LDAP_DEBUG_TRACE,
97 "ldap_delayed_open successful, ld_host is %s\n",
98 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
103 * use of DNS is turned off or this is an X.500 DN...
104 * use our default connection
109 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
110 servers, NULL, NULL );
112 ldap_free_urllist(servers);
119 ldap_send_server_request(
123 LDAPRequest *parentreq,
124 LDAPURLDesc *srvlist,
131 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
134 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
137 if ( srvlist == NULL ) {
140 if (( lc = find_connection( ld, srvlist, 1 )) ==
142 if ( (bind != NULL) && (parentreq != NULL) ) {
143 /* Remember the bind in the parent */
145 ++parentreq->lr_outrefcnt;
147 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
152 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
154 if ( ld->ld_errno == LDAP_SUCCESS ) {
155 ld->ld_errno = LDAP_SERVER_DOWN;
158 /* Forget about the bind */
159 --parentreq->lr_outrefcnt;
164 use_connection( ld, lc );
165 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
167 ld->ld_errno = LDAP_NO_MEMORY;
168 ldap_free_connection( ld, lc, 0, 0 );
171 /* Forget about the bind */
172 --parentreq->lr_outrefcnt;
176 lr->lr_msgid = msgid;
177 lr->lr_status = LDAP_REQST_INPROGRESS;
178 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
181 if ( parentreq != NULL ) { /* sub-request */
183 /* Increment if we didn't do it before the bind */
184 ++parentreq->lr_outrefcnt;
186 lr->lr_origid = parentreq->lr_origid;
187 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
188 lr->lr_parent = parentreq;
189 lr->lr_refnext = parentreq->lr_refnext;
190 parentreq->lr_refnext = lr;
191 } else { /* original request */
192 lr->lr_origid = lr->lr_msgid;
195 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
196 lr->lr_next->lr_prev = lr;
198 ld->ld_requests = lr;
201 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
203 if ( errno == EWOULDBLOCK ) {
204 /* need to continue write later */
205 lr->lr_status = LDAP_REQST_WRITING;
206 ldap_mark_select_write( ld, lc->lconn_sb );
209 ld->ld_errno = LDAP_SERVER_DOWN;
210 ldap_free_request( ld, lr );
211 ldap_free_connection( ld, lc, 0, 0 );
218 if ( parentreq == NULL ) {
219 ber->ber_end = ber->ber_ptr;
220 ber->ber_ptr = ber->ber_buf;
223 /* sent -- waiting for a response */
224 ldap_mark_select_read( ld, lc->lconn_sb );
227 ld->ld_errno = LDAP_SUCCESS;
232 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
233 int connect, LDAPreqinfo *bind )
239 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
241 * make a new LDAP server connection
242 * XXX open connection synchronously for now
244 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
245 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
247 LDAP_FREE( (char *)lc );
249 ld->ld_errno = LDAP_NO_MEMORY;
253 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
256 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
257 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
264 ber_sockbuf_free( lc->lconn_sb );
266 LDAP_FREE( (char *)lc );
267 ld->ld_errno = LDAP_SERVER_DOWN;
271 lc->lconn_server = ldap_url_dup(srv);
274 lc->lconn_status = LDAP_CONNST_CONNECTED;
275 lc->lconn_next = ld->ld_conns;
279 * XXX for now, we always do a synchronous bind. This will have
280 * to change in the long run...
284 LDAPConn *savedefconn;
286 /* Set flag to prevent additional referrals from being processed on this
287 * connection until the bind has completed
289 lc->lconn_rebind_inprogress = 1;
290 /* V3 rebind function */
291 if ( ld->ld_rebindproc != NULL) {
292 LDAPURLDesc *srvfunc;
293 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
294 ld->ld_errno = LDAP_NO_MEMORY;
297 savedefconn = ld->ld_defconn;
298 ++lc->lconn_refcnt; /* avoid premature free */
301 Debug( LDAP_DEBUG_TRACE, "Call application rebindproc\n", 0, 0, 0);
302 err = (*ld->ld_rebindproc)( ld, bind->ri_url, bind->ri_request, bind->ri_msgid);
304 ld->ld_defconn = savedefconn;
309 ldap_free_connection( ld, lc, 1, 0 );
312 ldap_free_urldesc( srvfunc);
315 savedefconn = ld->ld_defconn;
316 ++lc->lconn_refcnt; /* avoid premature free */
319 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
320 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
323 ld->ld_defconn = savedefconn;
327 ldap_free_connection( ld, lc, 1, 0 );
332 lc->lconn_rebind_inprogress = 0;
340 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
342 * return an existing connection (if any) to the server srv
343 * if "any" is non-zero, check for any server in the "srv" chain
349 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
350 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
351 if ( lc->lconn_server->lud_host != NULL &&
352 *lc->lconn_server->lud_host != '\0' &&
353 ls->lud_host != NULL && *ls->lud_host != '\0' &&
354 strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0
355 && ls->lud_port == lc->lconn_server->lud_port ) {
370 use_connection( LDAP *ld, LDAPConn *lc )
373 lc->lconn_lastused = time( NULL );
378 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
380 LDAPConn *tmplc, *prevlc;
382 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
384 if ( force || --lc->lconn_refcnt <= 0 ) {
385 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
386 ldap_mark_select_clear( ld, lc->lconn_sb );
388 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
392 if( lc->lconn_ber != NULL ) {
393 ber_free( lc->lconn_ber, 1 );
396 ldap_int_sasl_close( ld, lc );
399 for ( tmplc = ld->ld_conns; tmplc != NULL;
400 tmplc = tmplc->lconn_next ) {
402 if ( prevlc == NULL ) {
403 ld->ld_conns = tmplc->lconn_next;
405 prevlc->lconn_next = tmplc->lconn_next;
411 ldap_free_urllist( lc->lconn_server );
412 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
413 if ( lc->lconn_krbinstance != NULL ) {
414 LDAP_FREE( lc->lconn_krbinstance );
417 if ( lc->lconn_sb != ld->ld_sb ) {
418 ber_sockbuf_free( lc->lconn_sb );
420 if( lc->lconn_rebind_queue != NULL) {
422 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
423 LDAP_VFREE(lc->lconn_rebind_queue[i]);
425 LDAP_FREE( lc->lconn_rebind_queue);
428 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
431 lc->lconn_lastused = time( NULL );
432 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
433 lc->lconn_refcnt, 0, 0 );
440 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
445 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
446 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
447 if ( lc->lconn_server != NULL ) {
448 fprintf( stderr, "* host: %s port: %d%s\n",
449 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
450 : lc->lconn_server->lud_host,
451 lc->lconn_server->lud_port, ( lc->lconn_sb ==
452 ld->ld_sb ) ? " (default)" : "" );
454 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
455 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
456 "NeedSocket" : ( lc->lconn_status ==
457 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
458 fprintf( stderr, " last used: %s",
459 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
460 if( lc->lconn_rebind_inprogress ) {
461 fprintf( stderr, " rebind in progress\n");
462 if( lc->lconn_rebind_queue != NULL) {
464 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
466 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
467 fprintf( stderr, " queue %d entry %d - %s\n",
468 i, j, lc->lconn_rebind_queue[i][j]);
472 fprintf( stderr, " queue is empty\n");
475 fprintf(stderr, "\n");
484 ldap_dump_requests_and_responses( LDAP *ld )
489 fprintf( stderr, "** Outstanding Requests:\n" );
490 if (( lr = ld->ld_requests ) == NULL ) {
491 fprintf( stderr, " Empty\n" );
493 for ( ; lr != NULL; lr = lr->lr_next ) {
494 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
495 lr->lr_msgid, lr->lr_origid,
496 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
497 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
498 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
499 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
500 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
501 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
502 lr->lr_outrefcnt, lr->lr_parentcnt );
505 fprintf( stderr, "** Response Queue:\n" );
506 if (( lm = ld->ld_responses ) == NULL ) {
507 fprintf( stderr, " Empty\n" );
509 for ( ; lm != NULL; lm = lm->lm_next ) {
510 fprintf( stderr, " * msgid %d, type %lu\n",
511 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
512 if (( l = lm->lm_chain ) != NULL ) {
513 fprintf( stderr, " chained responses:\n" );
514 for ( ; l != NULL; l = l->lm_chain ) {
516 " * msgid %d, type %lu\n",
518 (unsigned long) l->lm_msgtype );
523 #endif /* LDAP_DEBUG */
527 ldap_free_request( LDAP *ld, LDAPRequest *lr )
529 LDAPRequest *tmplr, *nextlr;
531 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
532 lr->lr_origid, lr->lr_msgid, 0 );
534 if ( lr->lr_parent != NULL ) {
535 --lr->lr_parent->lr_outrefcnt;
537 /* free all referrals (child requests) */
538 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
539 nextlr = tmplr->lr_refnext;
540 ldap_free_request( ld, tmplr );
544 if ( lr->lr_prev == NULL ) {
545 ld->ld_requests = lr->lr_next;
547 lr->lr_prev->lr_next = lr->lr_next;
550 if ( lr->lr_next != NULL ) {
551 lr->lr_next->lr_prev = lr->lr_prev;
554 if ( lr->lr_ber != NULL ) {
555 ber_free( lr->lr_ber, 1 );
558 if ( lr->lr_res_error != NULL ) {
559 LDAP_FREE( lr->lr_res_error );
562 if ( lr->lr_res_matched != NULL ) {
563 LDAP_FREE( lr->lr_res_matched );
573 * (IN) ld = LDAP connection handle
574 * (IN) lr = LDAP Request structure
575 * (IN) refs = array of pointers to referral strings that we will chase
576 * The array will be free'd by this function when no longer needed
577 * (IN) sref != 0 if following search reference
578 * (OUT) errstrp = Place to return a string of referrals which could not be followed
579 * (OUT) hadrefp = 1 if sucessfully followed referral
581 * Return value - number of referrals followed
584 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
587 int unfollowedcnt = 0;
588 LDAPRequest *origreq;
589 LDAPURLDesc *srv = NULL;
591 char **refarray = NULL;
596 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
599 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
604 /* If no referrals in array, return */
605 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
610 /* Check for hop limit exceeded */
611 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
612 Debug( LDAP_DEBUG_ANY,
613 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
614 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
619 /* find original request */
620 for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) {
626 /* parse out & follow referrals */
627 for( i=0; refarray[i] != NULL; i++) {
628 /* Parse the referral URL */
629 if (( rc = ldap_url_parse( refarray[i], &srv)) != LDAP_SUCCESS) {
635 if( srv->lud_crit_exts ) {
636 /* we do not support any extensions */
637 ld->ld_errno = LDAP_NOT_SUPPORTED;
642 /* treat ldap://hostpart and ldap://hostpart/ the same */
643 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
644 LDAP_FREE( srv->lud_dn );
648 /* check connection for re-bind in progress */
649 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
650 if( lc->lconn_rebind_inprogress) {
651 /* We are already chasing a referral or search reference and a
652 * bind on that connection is in progress. We must queue
653 * referrals on that connection, so we don't get a request
654 * going out before the bind operation completes. This happens
655 * if two search references come in one behind the other
656 * for the same server with different contexts.
658 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals: queue referral \"%s\"\n",
660 if( lc->lconn_rebind_queue == NULL ) {
661 /* Create a referral list */
662 if( (lc->lconn_rebind_queue = (char ***)LDAP_MALLOC( sizeof(void *) * 2)) == NULL) {
663 ld->ld_errno = LDAP_NO_MEMORY;
667 lc->lconn_rebind_queue[0] = refarray;
668 lc->lconn_rebind_queue[1] = NULL;
671 /* Count how many referral arrays we already have */
672 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
675 /* Add the new referral to the list */
676 if( (lc->lconn_rebind_queue = (char ***)LDAP_REALLOC(
677 lc->lconn_rebind_queue, sizeof(void *) * (j + 2))) == NULL) {
678 ld->ld_errno = LDAP_NO_MEMORY;
682 lc->lconn_rebind_queue[j] = refarray;
683 lc->lconn_rebind_queue[j+1] = NULL;
686 /* We have queued the referral/reference, now just return */
689 count = 1; /* Pretend we already followed referral */
693 /* Re-encode the request with the new starting point of the search.
694 * Note: In the future we also need to replace the filter if one
695 * was provided with the search reference
698 /* For references we don't want old dn if new dn empty */
699 if ( sref && srv->lud_dn == NULL ) {
700 srv->lud_dn = LDAP_STRDUP( "" );
703 if (( ber = re_encode_request( ld, origreq->lr_ber,
704 ++ld->ld_msgid, srv, &rinfo.ri_request )) == NULL ) {
705 ld->ld_errno = LDAP_ENCODING_ERROR;
710 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
711 lr->lr_msgid, refarray[i], 0);
713 /* Send the new request to the server - may require a bind */
714 rinfo.ri_msgid = origreq->lr_origid;
715 rinfo.ri_url = refarray[i];
716 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
717 origreq, srv, NULL, &rinfo )) < 0 ) {
718 /* Failure, try next referral in the list */
719 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
720 refarray[i], ldap_err2string( ld->ld_errno ), 0);
721 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
722 ldap_free_urllist(srv);
725 /* Success, no need to try this referral list further */
730 /* check if there is a queue of referrals that came in during bind */
732 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
733 ld->ld_errno = LDAP_OPERATIONS_ERROR;
739 if( lc->lconn_rebind_queue != NULL) {
740 /* Release resources of previous list */
741 LDAP_VFREE(refarray);
743 ldap_free_urllist(srv);
746 /* Pull entries off end of queue so list always null terminated */
747 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
750 refarray = lc->lconn_rebind_queue[j-1];
751 lc->lconn_rebind_queue[j-1] = NULL;
752 /* we pulled off last entry from queue, free queue */
754 LDAP_FREE( lc->lconn_rebind_queue);
755 lc->lconn_rebind_queue = NULL;
757 /* restart the loop the with new referral list */
761 break; /* referral followed, break out of for loop */
765 LDAP_VFREE(refarray);
766 ldap_free_urllist(srv);
767 LDAP_FREE( *errstrp );
771 LDAP_FREE( unfollowed );
774 ld->ld_errno = LDAP_REFERRAL;
775 *errstrp = unfollowed;
781 * XXX merging of errors in this routine needs to be improved
784 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
787 char *p, *ref, *unfollowed;
788 LDAPRequest *origreq;
793 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
795 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
798 if ( *errstrp == NULL ) {
802 len = strlen( *errstrp );
803 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
804 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
806 p += LDAP_REF_STR_LEN;
811 if ( len < LDAP_REF_STR_LEN ) {
815 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
816 Debug( LDAP_DEBUG_ANY,
817 "more than %d referral hops (dropping)\n",
818 ld->ld_refhoplimit, 0, 0 );
819 /* XXX report as error in ld->ld_errno? */
823 /* find original request */
824 for ( origreq = lr; origreq->lr_parent != NULL;
825 origreq = origreq->lr_parent ) {
832 /* parse out & follow referrals */
833 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
834 if (( p = strchr( ref, '\n' )) != NULL ) {
840 rc = ldap_url_parse_ext( ref, &srv );
842 if ( rc != LDAP_URL_SUCCESS ) {
843 Debug( LDAP_DEBUG_TRACE,
844 "ignoring unknown referral <%s>\n", ref, 0, 0 );
845 rc = ldap_append_referral( ld, &unfollowed, ref );
850 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
851 LDAP_FREE( srv->lud_dn );
855 Debug( LDAP_DEBUG_TRACE,
856 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
860 ber = re_encode_request( ld, origreq->lr_ber,
861 ++ld->ld_msgid, srv, &rinfo.ri_request );
867 /* copy the complete referral for rebind process */
868 rinfo.ri_url = LDAP_STRDUP( ref );
870 rinfo.ri_msgid = origreq->lr_origid;
872 rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
873 lr, srv, NULL, &rinfo );
875 LDAP_FREE( rinfo.ri_url );
880 Debug( LDAP_DEBUG_ANY,
881 "Unable to chase referral (%s)\n",
882 ldap_err2string( ld->ld_errno ), 0, 0 );
883 rc = ldap_append_referral( ld, &unfollowed, ref );
886 ldap_free_urllist(srv);
889 LDAP_FREE( *errstrp );
890 *errstrp = unfollowed;
892 return(( rc == 0 ) ? count : rc );
897 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
901 if ( *referralsp == NULL ) {
903 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
907 *referralsp = (char *)LDAP_REALLOC( *referralsp,
908 strlen( *referralsp ) + strlen( s ) + 2 );
911 if ( *referralsp == NULL ) {
912 ld->ld_errno = LDAP_NO_MEMORY;
917 strcpy( *referralsp, LDAP_REF_STR );
919 strcat( *referralsp, "\n" );
921 strcat( *referralsp, s );
929 re_encode_request( LDAP *ld,
936 * XXX this routine knows way too much about how the lber library works!
943 BerElement tmpber, *ber;
947 Debug( LDAP_DEBUG_TRACE,
948 "re_encode_request: new msgid %ld, new dn <%s>\n",
950 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
955 * all LDAP requests are sequences that start with a message id.
956 * For all except delete, this is followed by a sequence that is
957 * tagged with the operation code. For delete, the provided DN
958 * is not wrapped by a sequence.
960 rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
962 if ( rc == LBER_ERROR ) {
963 ld->ld_errno = LDAP_DECODING_ERROR;
968 if ( tag == LDAP_REQ_BIND ) {
969 /* bind requests have a version number before the DN & other stuff */
970 rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
972 } else if ( tag == LDAP_REQ_DELETE ) {
973 /* delete requests don't have a DN wrapping sequence */
974 rc = ber_scanf( &tmpber, "a", &orig_dn );
976 } else if ( tag == LDAP_REQ_SEARCH ) {
977 /* search requests need to be re-scope-ed */
978 rc = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
980 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
981 /* use the scope provided in reference */
982 scope = srv->lud_scope;
984 } else if ( scope != LDAP_SCOPE_SUBTREE ) {
985 /* use scope implied by previous operation */
988 /* subtree -> subtree */
989 scope = LDAP_SCOPE_BASE;
993 rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
996 if( rc == LBER_ERROR ) {
997 ld->ld_errno = LDAP_DECODING_ERROR;
1001 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1005 if ( srv->lud_dn == NULL ) {
1011 if ( tag == LDAP_REQ_BIND ) {
1012 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1013 } else if ( tag == LDAP_REQ_DELETE ) {
1014 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1015 } else if ( tag == LDAP_REQ_SEARCH ) {
1016 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1018 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1021 LDAP_FREE( orig_dn );
1024 ld->ld_errno = LDAP_ENCODING_ERROR;
1029 if ( tag != LDAP_REQ_DELETE && (
1030 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1031 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1032 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1034 ld->ld_errno = LDAP_ENCODING_ERROR;
1040 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1041 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1043 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1045 #endif /* LDAP_DEBUG */
1047 *type = tag; /* return request type */
1053 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1057 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1058 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1059 continue; /* Skip completed requests */
1061 if ( msgid == lr->lr_msgid ) {