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,
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(
86 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
88 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
89 /* not connected yet */
90 int rc = ldap_open_defconn( ld );
97 Debug( LDAP_DEBUG_TRACE,
98 "ldap_delayed_open successful, ld_host is %s\n",
99 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
104 * use of DNS is turned off or this is an X.500 DN...
105 * use our default connection
110 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
111 servers, NULL, NULL );
113 ldap_free_urllist(servers);
120 ldap_send_server_request(
124 LDAPRequest *parentreq,
125 LDAPURLDesc *srvlist,
132 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
135 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
138 if ( srvlist == NULL ) {
141 if (( lc = find_connection( ld, srvlist, 1 )) ==
143 if ( (bind != NULL) && (parentreq != NULL) ) {
144 /* Remember the bind in the parent */
146 ++parentreq->lr_outrefcnt;
148 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
153 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
155 if ( ld->ld_errno == LDAP_SUCCESS ) {
156 ld->ld_errno = LDAP_SERVER_DOWN;
159 /* Forget about the bind */
160 --parentreq->lr_outrefcnt;
165 use_connection( ld, lc );
166 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
168 ld->ld_errno = LDAP_NO_MEMORY;
169 ldap_free_connection( ld, lc, 0, 0 );
172 /* Forget about the bind */
173 --parentreq->lr_outrefcnt;
177 lr->lr_msgid = msgid;
178 lr->lr_status = LDAP_REQST_INPROGRESS;
179 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
182 if ( parentreq != NULL ) { /* sub-request */
184 /* Increment if we didn't do it before the bind */
185 ++parentreq->lr_outrefcnt;
187 lr->lr_origid = parentreq->lr_origid;
188 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
189 lr->lr_parent = parentreq;
190 lr->lr_refnext = parentreq->lr_refnext;
191 parentreq->lr_refnext = lr;
192 } else { /* original request */
193 lr->lr_origid = lr->lr_msgid;
196 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
197 lr->lr_next->lr_prev = lr;
199 ld->ld_requests = lr;
202 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
204 if ( errno == EWOULDBLOCK ) {
205 /* need to continue write later */
206 lr->lr_status = LDAP_REQST_WRITING;
207 ldap_mark_select_write( ld, lc->lconn_sb );
210 ld->ld_errno = LDAP_SERVER_DOWN;
211 ldap_free_request( ld, lr );
212 ldap_free_connection( ld, lc, 0, 0 );
219 if ( parentreq == NULL ) {
220 ber->ber_end = ber->ber_ptr;
221 ber->ber_ptr = ber->ber_buf;
224 /* sent -- waiting for a response */
225 ldap_mark_select_read( ld, lc->lconn_sb );
228 ld->ld_errno = LDAP_SUCCESS;
233 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
234 int connect, LDAPreqinfo *bind )
240 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
242 * make a new LDAP server connection
243 * XXX open connection synchronously for now
245 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
246 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
248 LDAP_FREE( (char *)lc );
250 ld->ld_errno = LDAP_NO_MEMORY;
254 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
257 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
258 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
265 ber_sockbuf_free( lc->lconn_sb );
267 LDAP_FREE( (char *)lc );
268 ld->ld_errno = LDAP_SERVER_DOWN;
272 lc->lconn_server = ldap_url_dup(srv);
275 lc->lconn_status = LDAP_CONNST_CONNECTED;
276 lc->lconn_next = ld->ld_conns;
280 * XXX for now, we always do a synchronous bind. This will have
281 * to change in the long run...
285 LDAPConn *savedefconn;
287 /* Set flag to prevent additional referrals from being processed on this
288 * connection until the bind has completed
290 lc->lconn_rebind_inprogress = 1;
291 /* V3 rebind function */
292 if ( ld->ld_rebind_proc != NULL) {
293 LDAPURLDesc *srvfunc;
294 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
295 ld->ld_errno = LDAP_NO_MEMORY;
298 savedefconn = ld->ld_defconn;
299 ++lc->lconn_refcnt; /* avoid premature free */
302 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
303 err = (*ld->ld_rebind_proc)( ld,
304 bind->ri_url, bind->ri_request, bind->ri_msgid,
305 ld->ld_rebind_params );
307 ld->ld_defconn = savedefconn;
312 ldap_free_connection( ld, lc, 1, 0 );
315 ldap_free_urldesc( srvfunc);
318 savedefconn = ld->ld_defconn;
319 ++lc->lconn_refcnt; /* avoid premature free */
322 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
323 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
326 ld->ld_defconn = savedefconn;
330 ldap_free_connection( ld, lc, 1, 0 );
335 lc->lconn_rebind_inprogress = 0;
343 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
345 * return an existing connection (if any) to the server srv
346 * if "any" is non-zero, check for any server in the "srv" chain
352 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
353 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
354 if ( lc->lconn_server->lud_host != NULL &&
355 *lc->lconn_server->lud_host != '\0' &&
356 ls->lud_host != NULL && *ls->lud_host != '\0' &&
357 strcasecmp( 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 );
399 ldap_int_sasl_close( ld, lc );
402 for ( tmplc = ld->ld_conns; tmplc != NULL;
403 tmplc = tmplc->lconn_next ) {
405 if ( prevlc == NULL ) {
406 ld->ld_conns = tmplc->lconn_next;
408 prevlc->lconn_next = tmplc->lconn_next;
414 ldap_free_urllist( lc->lconn_server );
415 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
416 if ( lc->lconn_krbinstance != NULL ) {
417 LDAP_FREE( lc->lconn_krbinstance );
420 if ( lc->lconn_sb != ld->ld_sb ) {
421 ber_sockbuf_free( lc->lconn_sb );
423 if( lc->lconn_rebind_queue != NULL) {
425 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
426 LDAP_VFREE(lc->lconn_rebind_queue[i]);
428 LDAP_FREE( lc->lconn_rebind_queue);
431 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
434 lc->lconn_lastused = time( NULL );
435 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
436 lc->lconn_refcnt, 0, 0 );
443 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
448 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
449 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
450 if ( lc->lconn_server != NULL ) {
451 fprintf( stderr, "* host: %s port: %d%s\n",
452 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
453 : lc->lconn_server->lud_host,
454 lc->lconn_server->lud_port, ( lc->lconn_sb ==
455 ld->ld_sb ) ? " (default)" : "" );
457 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
458 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
459 "NeedSocket" : ( lc->lconn_status ==
460 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
461 fprintf( stderr, " last used: %s",
462 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
463 if( lc->lconn_rebind_inprogress ) {
464 fprintf( stderr, " rebind in progress\n");
465 if( lc->lconn_rebind_queue != NULL) {
467 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
469 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
470 fprintf( stderr, " queue %d entry %d - %s\n",
471 i, j, lc->lconn_rebind_queue[i][j]);
475 fprintf( stderr, " queue is empty\n");
478 fprintf(stderr, "\n");
487 ldap_dump_requests_and_responses( LDAP *ld )
492 fprintf( stderr, "** Outstanding Requests:\n" );
493 if (( lr = ld->ld_requests ) == NULL ) {
494 fprintf( stderr, " Empty\n" );
496 for ( ; lr != NULL; lr = lr->lr_next ) {
497 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
498 lr->lr_msgid, lr->lr_origid,
499 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
500 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
501 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
502 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
503 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
504 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
505 lr->lr_outrefcnt, lr->lr_parentcnt );
508 fprintf( stderr, "** Response Queue:\n" );
509 if (( lm = ld->ld_responses ) == NULL ) {
510 fprintf( stderr, " Empty\n" );
512 for ( ; lm != NULL; lm = lm->lm_next ) {
513 fprintf( stderr, " * msgid %d, type %lu\n",
514 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
515 if (( l = lm->lm_chain ) != NULL ) {
516 fprintf( stderr, " chained responses:\n" );
517 for ( ; l != NULL; l = l->lm_chain ) {
519 " * msgid %d, type %lu\n",
521 (unsigned long) l->lm_msgtype );
526 #endif /* LDAP_DEBUG */
530 ldap_free_request( LDAP *ld, LDAPRequest *lr )
532 LDAPRequest *tmplr, *nextlr;
534 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
535 lr->lr_origid, lr->lr_msgid, 0 );
537 if ( lr->lr_parent != NULL ) {
538 --lr->lr_parent->lr_outrefcnt;
540 /* free all referrals (child requests) */
541 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
542 nextlr = tmplr->lr_refnext;
543 ldap_free_request( ld, tmplr );
547 if ( lr->lr_prev == NULL ) {
548 ld->ld_requests = lr->lr_next;
550 lr->lr_prev->lr_next = lr->lr_next;
553 if ( lr->lr_next != NULL ) {
554 lr->lr_next->lr_prev = lr->lr_prev;
557 if ( lr->lr_ber != NULL ) {
558 ber_free( lr->lr_ber, 1 );
561 if ( lr->lr_res_error != NULL ) {
562 LDAP_FREE( lr->lr_res_error );
565 if ( lr->lr_res_matched != NULL ) {
566 LDAP_FREE( lr->lr_res_matched );
576 * (IN) ld = LDAP connection handle
577 * (IN) lr = LDAP Request structure
578 * (IN) refs = array of pointers to referral strings that we will chase
579 * The array will be free'd by this function when no longer needed
580 * (IN) sref != 0 if following search reference
581 * (OUT) errstrp = Place to return a string of referrals which could not be followed
582 * (OUT) hadrefp = 1 if sucessfully followed referral
584 * Return value - number of referrals followed
587 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
590 int unfollowedcnt = 0;
591 LDAPRequest *origreq;
592 LDAPURLDesc *srv = NULL;
594 char **refarray = NULL;
599 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
602 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
607 /* If no referrals in array, return */
608 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
613 /* Check for hop limit exceeded */
614 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
615 Debug( LDAP_DEBUG_ANY,
616 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
617 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
622 /* find original request */
624 origreq->lr_parent != NULL;
625 origreq = origreq->lr_parent )
632 /* parse out & follow referrals */
633 for( i=0; refarray[i] != NULL; i++) {
634 /* Parse the referral URL */
635 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
641 if( srv->lud_crit_exts ) {
642 /* we do not support any extensions */
643 ld->ld_errno = LDAP_NOT_SUPPORTED;
648 /* treat ldap://hostpart and ldap://hostpart/ the same */
649 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
650 LDAP_FREE( srv->lud_dn );
654 /* check connection for re-bind in progress */
655 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
656 if( lc->lconn_rebind_inprogress) {
657 /* We are already chasing a referral or search reference and a
658 * bind on that connection is in progress. We must queue
659 * referrals on that connection, so we don't get a request
660 * going out before the bind operation completes. This happens
661 * if two search references come in one behind the other
662 * for the same server with different contexts.
664 Debug( LDAP_DEBUG_TRACE,
665 "ldap_chase_v3referrals: queue referral \"%s\"\n",
667 if( lc->lconn_rebind_queue == NULL ) {
668 /* Create a referral list */
669 lc->lconn_rebind_queue =
670 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
672 if( lc->lconn_rebind_queue == NULL) {
673 ld->ld_errno = LDAP_NO_MEMORY;
678 lc->lconn_rebind_queue[0] = refarray;
679 lc->lconn_rebind_queue[1] = NULL;
683 /* Count how many referral arrays we already have */
684 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
688 /* Add the new referral to the list */
689 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
690 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
692 if( lc->lconn_rebind_queue == NULL ) {
693 ld->ld_errno = LDAP_NO_MEMORY;
697 lc->lconn_rebind_queue[j] = refarray;
698 lc->lconn_rebind_queue[j+1] = NULL;
702 /* We have queued the referral/reference, now just return */
705 count = 1; /* Pretend we already followed referral */
709 /* Re-encode the request with the new starting point of the search.
710 * Note: In the future we also need to replace the filter if one
711 * was provided with the search reference
714 /* For references we don't want old dn if new dn empty */
715 if ( sref && srv->lud_dn == NULL ) {
716 srv->lud_dn = LDAP_STRDUP( "" );
719 ber = re_encode_request( ld, origreq->lr_ber, ++ld->ld_msgid,
720 sref, srv, &rinfo.ri_request );
723 ld->ld_errno = LDAP_ENCODING_ERROR;
728 Debug( LDAP_DEBUG_TRACE,
729 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
730 lr->lr_msgid, refarray[i], 0);
732 /* Send the new request to the server - may require a bind */
733 rinfo.ri_msgid = origreq->lr_origid;
734 rinfo.ri_url = refarray[i];
735 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
736 origreq, srv, NULL, &rinfo )) < 0 ) {
737 /* Failure, try next referral in the list */
738 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
739 refarray[i], ldap_err2string( ld->ld_errno ), 0);
740 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
741 ldap_free_urllist(srv);
744 /* Success, no need to try this referral list further */
749 /* check if there is a queue of referrals that came in during bind */
751 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
752 ld->ld_errno = LDAP_OPERATIONS_ERROR;
758 if( lc->lconn_rebind_queue != NULL) {
759 /* Release resources of previous list */
760 LDAP_VFREE(refarray);
762 ldap_free_urllist(srv);
765 /* Pull entries off end of queue so list always null terminated */
766 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
769 refarray = lc->lconn_rebind_queue[j-1];
770 lc->lconn_rebind_queue[j-1] = NULL;
771 /* we pulled off last entry from queue, free queue */
773 LDAP_FREE( lc->lconn_rebind_queue);
774 lc->lconn_rebind_queue = NULL;
776 /* restart the loop the with new referral list */
780 break; /* referral followed, break out of for loop */
784 LDAP_VFREE(refarray);
785 ldap_free_urllist(srv);
786 LDAP_FREE( *errstrp );
790 LDAP_FREE( unfollowed );
793 ld->ld_errno = LDAP_REFERRAL;
794 *errstrp = unfollowed;
800 * XXX merging of errors in this routine needs to be improved
803 ldap_chase_referrals( LDAP *ld,
810 char *p, *ref, *unfollowed;
811 LDAPRequest *origreq;
816 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
818 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
821 if ( *errstrp == NULL ) {
825 len = strlen( *errstrp );
826 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
827 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
829 p += LDAP_REF_STR_LEN;
834 if ( len < LDAP_REF_STR_LEN ) {
838 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
839 Debug( LDAP_DEBUG_ANY,
840 "more than %d referral hops (dropping)\n",
841 ld->ld_refhoplimit, 0, 0 );
842 /* XXX report as error in ld->ld_errno? */
846 /* find original request */
847 for ( origreq = lr; origreq->lr_parent != NULL;
848 origreq = origreq->lr_parent ) {
855 /* parse out & follow referrals */
856 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
857 if (( p = strchr( ref, '\n' )) != NULL ) {
863 rc = ldap_url_parse_ext( ref, &srv );
865 if ( rc != LDAP_URL_SUCCESS ) {
866 Debug( LDAP_DEBUG_TRACE,
867 "ignoring unknown referral <%s>\n", ref, 0, 0 );
868 rc = ldap_append_referral( ld, &unfollowed, ref );
873 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
874 LDAP_FREE( srv->lud_dn );
878 Debug( LDAP_DEBUG_TRACE,
879 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
883 ber = re_encode_request( ld, origreq->lr_ber,
884 ++ld->ld_msgid, sref, srv, &rinfo.ri_request );
890 /* copy the complete referral for rebind process */
891 rinfo.ri_url = LDAP_STRDUP( ref );
893 rinfo.ri_msgid = origreq->lr_origid;
895 rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
896 lr, srv, NULL, &rinfo );
898 LDAP_FREE( rinfo.ri_url );
903 Debug( LDAP_DEBUG_ANY,
904 "Unable to chase referral (%s)\n",
905 ldap_err2string( ld->ld_errno ), 0, 0 );
906 rc = ldap_append_referral( ld, &unfollowed, ref );
909 ldap_free_urllist(srv);
912 LDAP_FREE( *errstrp );
913 *errstrp = unfollowed;
915 return(( rc == 0 ) ? count : rc );
920 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
924 if ( *referralsp == NULL ) {
926 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
930 *referralsp = (char *)LDAP_REALLOC( *referralsp,
931 strlen( *referralsp ) + strlen( s ) + 2 );
934 if ( *referralsp == NULL ) {
935 ld->ld_errno = LDAP_NO_MEMORY;
940 strcpy( *referralsp, LDAP_REF_STR );
942 strcat( *referralsp, "\n" );
944 strcat( *referralsp, s );
952 re_encode_request( LDAP *ld,
960 * XXX this routine knows way too much about how the lber library works!
967 BerElement tmpber, *ber;
971 Debug( LDAP_DEBUG_TRACE,
972 "re_encode_request: new msgid %ld, new dn <%s>\n",
974 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
979 * all LDAP requests are sequences that start with a message id.
980 * For all except delete, this is followed by a sequence that is
981 * tagged with the operation code. For delete, the provided DN
982 * is not wrapped by a sequence.
984 rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
986 if ( rc == LBER_ERROR ) {
987 ld->ld_errno = LDAP_DECODING_ERROR;
992 if ( tag == LDAP_REQ_BIND ) {
993 /* bind requests have a version number before the DN & other stuff */
994 rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
996 } else if ( tag == LDAP_REQ_DELETE ) {
997 /* delete requests don't have a DN wrapping sequence */
998 rc = ber_scanf( &tmpber, "a", &orig_dn );
1000 } else if ( tag == LDAP_REQ_SEARCH ) {
1001 /* search requests need to be re-scope-ed */
1002 rc = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1004 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1005 /* use the scope provided in reference */
1006 scope = srv->lud_scope;
1008 } else if ( sref && scope != LDAP_SCOPE_SUBTREE ) {
1009 /* use scope implied by previous operation */
1012 /* subtree -> subtree */
1013 scope = LDAP_SCOPE_BASE;
1017 rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1020 if( rc == LBER_ERROR ) {
1021 ld->ld_errno = LDAP_DECODING_ERROR;
1025 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1029 if ( srv->lud_dn == NULL ) {
1035 if ( tag == LDAP_REQ_BIND ) {
1036 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1037 } else if ( tag == LDAP_REQ_DELETE ) {
1038 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1039 } else if ( tag == LDAP_REQ_SEARCH ) {
1040 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1042 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1045 LDAP_FREE( orig_dn );
1048 ld->ld_errno = LDAP_ENCODING_ERROR;
1053 if ( tag != LDAP_REQ_DELETE && (
1054 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1055 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1056 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1058 ld->ld_errno = LDAP_ENCODING_ERROR;
1064 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1065 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1067 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1069 #endif /* LDAP_DEBUG */
1071 *type = tag; /* return request type */
1077 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1081 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1082 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1083 continue; /* Skip completed requests */
1085 if ( msgid == lr->lr_msgid ) {