3 * Copyright 1998-2003 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
29 /* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
30 * can be found in the file "build/LICENSE-2.0.1" in this distribution
31 * of OpenLDAP Software.
38 #include <ac/stdlib.h>
41 #include <ac/socket.h>
42 #include <ac/string.h>
44 #include <ac/unistd.h>
49 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
50 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
53 re_encode_request( LDAP *ld,
61 ldap_alloc_ber_with_options( LDAP *ld )
65 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
66 ld->ld_errno = LDAP_NO_MEMORY;
74 ldap_set_ber_options( LDAP *ld, BerElement *ber )
76 ber->ber_options = ld->ld_lberoptions;
81 ldap_send_initial_request(
91 LDAP_LOG ( OPERATION, ENTRY, "ldap_send_initial_request\n", 0, 0, 0 );
93 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
96 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
97 /* not connected yet */
98 int rc = ldap_open_defconn( ld );
106 LDAP_LOG ( OPERATION, DETAIL1,
107 "ldap_send_initial_request: ldap_open_defconn: successful\n" ,
110 Debug( LDAP_DEBUG_TRACE,
111 "ldap_open_defconn: successful\n",
118 * use of DNS is turned off or this is an X.500 DN...
119 * use our default connection
124 #ifdef LDAP_CONNECTIONLESS
125 if (LDAP_IS_UDP(ld)) {
126 if (msgtype == LDAP_REQ_BIND) {
127 if (ld->ld_options.ldo_cldapdn)
128 ldap_memfree(ld->ld_options.ldo_cldapdn);
129 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
132 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
133 return LDAP_PARAM_ERROR;
136 rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL,
137 servers, NULL, NULL );
139 ldap_free_urllist(servers);
145 ldap_int_flush_request(
150 LDAPConn *lc = lr->lr_conn;
152 if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
153 if ( errno == EAGAIN ) {
154 /* need to continue write later */
155 lr->lr_status = LDAP_REQST_WRITING;
156 ldap_mark_select_write( ld, lc->lconn_sb );
157 ld->ld_errno = LDAP_BUSY;
160 ld->ld_errno = LDAP_SERVER_DOWN;
161 ldap_free_request( ld, lr );
162 ldap_free_connection( ld, lc, 0, 0 );
166 if ( lr->lr_parent == NULL ) {
167 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
168 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
170 lr->lr_status = LDAP_REQST_INPROGRESS;
172 /* sent -- waiting for a response */
173 ldap_mark_select_read( ld, lc->lconn_sb );
179 ldap_send_server_request(
183 LDAPRequest *parentreq,
184 LDAPURLDesc *srvlist,
192 LDAP_LOG ( OPERATION, ENTRY, "ldap_send_server_request\n", 0, 0, 0 );
194 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
198 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
201 if ( srvlist == NULL ) {
204 if (( lc = find_connection( ld, srvlist, 1 )) ==
206 if ( (bind != NULL) && (parentreq != NULL) ) {
207 /* Remember the bind in the parent */
209 ++parentreq->lr_outrefcnt;
211 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
216 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
218 if ( ld->ld_errno == LDAP_SUCCESS ) {
219 ld->ld_errno = LDAP_SERVER_DOWN;
222 /* Forget about the bind */
223 --parentreq->lr_outrefcnt;
228 use_connection( ld, lc );
230 /* If we still have an incomplete write, try to finish it before
231 * dealing with the new request. If we don't finish here, return
232 * LDAP_BUSY and let the caller retry later. We only allow a single
233 * request to be in WRITING state.
236 #ifdef LDAP_R_COMPILE
237 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
239 if ( ld->ld_requests &&
240 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
241 ldap_int_flush_request( ld, ld->ld_requests ) < 0 ) {
244 #ifdef LDAP_R_COMPILE
245 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
249 if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) ==
251 ld->ld_errno = LDAP_NO_MEMORY;
252 ldap_free_connection( ld, lc, 0, 0 );
255 /* Forget about the bind */
256 --parentreq->lr_outrefcnt;
260 lr->lr_msgid = msgid;
261 lr->lr_status = LDAP_REQST_INPROGRESS;
262 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
265 if ( parentreq != NULL ) { /* sub-request */
267 /* Increment if we didn't do it before the bind */
268 ++parentreq->lr_outrefcnt;
270 lr->lr_origid = parentreq->lr_origid;
271 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
272 lr->lr_parent = parentreq;
273 lr->lr_refnext = parentreq->lr_child;
274 parentreq->lr_child = lr;
275 } else { /* original request */
276 lr->lr_origid = lr->lr_msgid;
279 #ifdef LDAP_R_COMPILE
280 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
282 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
283 lr->lr_next->lr_prev = lr;
285 ld->ld_requests = lr;
288 ld->ld_errno = LDAP_SUCCESS;
289 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
292 #ifdef LDAP_R_COMPILE
293 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
300 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
301 int connect, LDAPreqinfo *bind )
308 LDAP_LOG ( OPERATION, ENTRY, "ldap_new_connection\n", 0, 0, 0 );
310 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 );
313 * make a new LDAP server connection
314 * XXX open connection synchronously for now
316 if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL ||
317 ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) {
319 LDAP_FREE( (char *)lc );
321 ld->ld_errno = LDAP_NO_MEMORY;
325 lc->lconn_sb = ( use_ldsb ) ? ld->ld_sb : sb;
328 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
329 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) {
336 ber_sockbuf_free( lc->lconn_sb );
338 LDAP_FREE( (char *)lc );
339 ld->ld_errno = LDAP_SERVER_DOWN;
343 lc->lconn_server = ldap_url_dup(srv);
346 lc->lconn_status = LDAP_CONNST_CONNECTED;
347 lc->lconn_next = ld->ld_conns;
351 * XXX for now, we always do a synchronous bind. This will have
352 * to change in the long run...
356 LDAPConn *savedefconn;
358 /* Set flag to prevent additional referrals from being processed on this
359 * connection until the bind has completed
361 lc->lconn_rebind_inprogress = 1;
362 /* V3 rebind function */
363 if ( ld->ld_rebind_proc != NULL) {
364 LDAPURLDesc *srvfunc;
365 if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
366 ld->ld_errno = LDAP_NO_MEMORY;
369 savedefconn = ld->ld_defconn;
370 ++lc->lconn_refcnt; /* avoid premature free */
374 LDAP_LOG ( OPERATION, DETAIL1,
375 "ldap_new_connection: Call application rebind_proc\n",
378 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
380 err = (*ld->ld_rebind_proc)( ld,
381 bind->ri_url, bind->ri_request, bind->ri_msgid,
382 ld->ld_rebind_params );
384 ld->ld_defconn = savedefconn;
389 ldap_free_connection( ld, lc, 1, 0 );
392 ldap_free_urldesc( srvfunc);
395 savedefconn = ld->ld_defconn;
396 ++lc->lconn_refcnt; /* avoid premature free */
400 LDAP_LOG ( OPERATION, DETAIL1,
401 "ldap_new_connection: anonymous rebind via ldap_bind_s\n",
404 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
406 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
409 ld->ld_defconn = savedefconn;
413 ldap_free_connection( ld, lc, 1, 0 );
418 lc->lconn_rebind_inprogress = 0;
426 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
428 * return an existing connection (if any) to the server srv
429 * if "any" is non-zero, check for any server in the "srv" chain
435 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
436 for ( ls = srv; ls != NULL; ls = ls->lud_next ) {
437 if ( lc->lconn_server->lud_host != NULL &&
438 *lc->lconn_server->lud_host != '\0' &&
439 ls->lud_host != NULL && *ls->lud_host != '\0' &&
440 strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0
441 && ls->lud_port == lc->lconn_server->lud_port ) {
456 use_connection( LDAP *ld, LDAPConn *lc )
459 lc->lconn_lastused = time( NULL );
464 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
466 LDAPConn *tmplc, *prevlc;
469 LDAP_LOG ( OPERATION, ENTRY, "ldap_free_connection\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
474 if ( force || --lc->lconn_refcnt <= 0 ) {
475 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
476 ldap_mark_select_clear( ld, lc->lconn_sb );
478 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
482 if( lc->lconn_ber != NULL ) {
483 ber_free( lc->lconn_ber, 1 );
486 ldap_int_sasl_close( ld, lc );
489 for ( tmplc = ld->ld_conns; tmplc != NULL;
490 tmplc = tmplc->lconn_next ) {
492 if ( prevlc == NULL ) {
493 ld->ld_conns = tmplc->lconn_next;
495 prevlc->lconn_next = tmplc->lconn_next;
501 ldap_free_urllist( lc->lconn_server );
502 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
503 if ( lc->lconn_krbinstance != NULL ) {
504 LDAP_FREE( lc->lconn_krbinstance );
507 if ( lc->lconn_sb != ld->ld_sb ) {
508 ber_sockbuf_free( lc->lconn_sb );
510 if( lc->lconn_rebind_queue != NULL) {
512 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++) {
513 LDAP_VFREE(lc->lconn_rebind_queue[i]);
515 LDAP_FREE( lc->lconn_rebind_queue);
519 LDAP_LOG ( OPERATION, RESULTS,
520 "ldap_free_connection: actually freed\n", 0, 0, 0 );
522 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
526 lc->lconn_lastused = time( NULL );
528 LDAP_LOG ( OPERATION, RESULTS, "ldap_free_connection: refcnt %d\n",
529 lc->lconn_refcnt, 0, 0 );
531 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
532 lc->lconn_refcnt, 0, 0 );
540 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
545 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
546 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
547 if ( lc->lconn_server != NULL ) {
548 fprintf( stderr, "* host: %s port: %d%s\n",
549 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
550 : lc->lconn_server->lud_host,
551 lc->lconn_server->lud_port, ( lc->lconn_sb ==
552 ld->ld_sb ) ? " (default)" : "" );
554 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
555 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
556 "NeedSocket" : ( lc->lconn_status ==
557 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
558 fprintf( stderr, " last used: %s",
559 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
560 if( lc->lconn_rebind_inprogress ) {
561 fprintf( stderr, " rebind in progress\n");
562 if( lc->lconn_rebind_queue != NULL) {
564 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
566 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
567 fprintf( stderr, " queue %d entry %d - %s\n",
568 i, j, lc->lconn_rebind_queue[i][j]);
572 fprintf( stderr, " queue is empty\n");
575 fprintf(stderr, "\n");
584 ldap_dump_requests_and_responses( LDAP *ld )
589 #ifdef LDAP_R_COMPILE
590 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
592 fprintf( stderr, "** Outstanding Requests:\n" );
593 if (( lr = ld->ld_requests ) == NULL ) {
594 fprintf( stderr, " Empty\n" );
596 for ( ; lr != NULL; lr = lr->lr_next ) {
597 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
598 lr->lr_msgid, lr->lr_origid,
599 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
600 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
601 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
602 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
603 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
604 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
605 lr->lr_outrefcnt, lr->lr_parentcnt );
607 #ifdef LDAP_R_COMPILE
608 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
609 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
611 fprintf( stderr, "** Response Queue:\n" );
612 if (( lm = ld->ld_responses ) == NULL ) {
613 fprintf( stderr, " Empty\n" );
615 for ( ; lm != NULL; lm = lm->lm_next ) {
616 fprintf( stderr, " * msgid %d, type %lu\n",
617 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
618 if (( l = lm->lm_chain ) != NULL ) {
619 fprintf( stderr, " chained responses:\n" );
620 for ( ; l != NULL; l = l->lm_chain ) {
622 " * msgid %d, type %lu\n",
624 (unsigned long) l->lm_msgtype );
628 #ifdef LDAP_R_COMPILE
629 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
632 #endif /* LDAP_DEBUG */
635 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
637 if ( lr->lr_prev == NULL ) {
638 ld->ld_requests = lr->lr_next;
640 lr->lr_prev->lr_next = lr->lr_next;
643 if ( lr->lr_next != NULL ) {
644 lr->lr_next->lr_prev = lr->lr_prev;
647 if ( lr->lr_ber != NULL ) {
648 ber_free( lr->lr_ber, 1 );
651 if ( lr->lr_res_error != NULL ) {
652 LDAP_FREE( lr->lr_res_error );
655 if ( lr->lr_res_matched != NULL ) {
656 LDAP_FREE( lr->lr_res_matched );
663 ldap_free_request( LDAP *ld, LDAPRequest *lr )
665 LDAPRequest **ttmplr;
668 LDAP_LOG ( OPERATION, ARGS, "ldap_free_request (origid %d, msgid %d)\n",
669 lr->lr_origid, lr->lr_msgid, 0 );
671 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
672 lr->lr_origid, lr->lr_msgid, 0 );
675 /* free all referrals (child requests) */
676 while ( lr->lr_child )
677 ldap_free_request( ld, lr->lr_child );
679 if ( lr->lr_parent != NULL ) {
680 --lr->lr_parent->lr_outrefcnt;
681 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
683 *ttmplr = lr->lr_refnext;
685 ldap_free_request_int( ld, lr );
693 * (IN) ld = LDAP connection handle
694 * (IN) lr = LDAP Request structure
695 * (IN) refs = array of pointers to referral strings that we will chase
696 * The array will be free'd by this function when no longer needed
697 * (IN) sref != 0 if following search reference
698 * (OUT) errstrp = Place to return a string of referrals which could not be followed
699 * (OUT) hadrefp = 1 if sucessfully followed referral
701 * Return value - number of referrals followed
704 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
707 int unfollowedcnt = 0;
708 LDAPRequest *origreq;
709 LDAPURLDesc *srv = NULL;
711 char **refarray = NULL;
716 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
720 LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_v3referrals\n", 0, 0, 0 );
722 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
728 /* If no referrals in array, return */
729 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
734 /* Check for hop limit exceeded */
735 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
737 LDAP_LOG ( OPERATION, DETAIL1,
738 "ldap_chase_v3referrals: more than %d referral hops (dropping)\n",
739 ld->ld_refhoplimit, 0, 0 );
741 Debug( LDAP_DEBUG_ANY,
742 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
744 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
749 /* find original request */
751 origreq->lr_parent != NULL;
752 origreq = origreq->lr_parent )
759 /* parse out & follow referrals */
760 for( i=0; refarray[i] != NULL; i++) {
761 /* Parse the referral URL */
762 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
768 if( srv->lud_crit_exts ) {
769 /* we do not support any extensions */
770 ld->ld_errno = LDAP_NOT_SUPPORTED;
775 /* treat ldap://hostpart and ldap://hostpart/ the same */
776 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
777 LDAP_FREE( srv->lud_dn );
781 /* check connection for re-bind in progress */
782 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
783 if( lc->lconn_rebind_inprogress) {
784 /* We are already chasing a referral or search reference and a
785 * bind on that connection is in progress. We must queue
786 * referrals on that connection, so we don't get a request
787 * going out before the bind operation completes. This happens
788 * if two search references come in one behind the other
789 * for the same server with different contexts.
792 LDAP_LOG ( OPERATION, DETAIL1,
793 "ldap_chase_v3referrals: queue referral \"%s\"\n",
796 Debug( LDAP_DEBUG_TRACE,
797 "ldap_chase_v3referrals: queue referral \"%s\"\n",
800 if( lc->lconn_rebind_queue == NULL ) {
801 /* Create a referral list */
802 lc->lconn_rebind_queue =
803 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
805 if( lc->lconn_rebind_queue == NULL) {
806 ld->ld_errno = LDAP_NO_MEMORY;
811 lc->lconn_rebind_queue[0] = refarray;
812 lc->lconn_rebind_queue[1] = NULL;
816 /* Count how many referral arrays we already have */
817 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
821 /* Add the new referral to the list */
822 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
823 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
825 if( lc->lconn_rebind_queue == NULL ) {
826 ld->ld_errno = LDAP_NO_MEMORY;
830 lc->lconn_rebind_queue[j] = refarray;
831 lc->lconn_rebind_queue[j+1] = NULL;
835 /* We have queued the referral/reference, now just return */
838 count = 1; /* Pretend we already followed referral */
842 /* Re-encode the request with the new starting point of the search.
843 * Note: In the future we also need to replace the filter if one
844 * was provided with the search reference
847 /* For references we don't want old dn if new dn empty */
848 if ( sref && srv->lud_dn == NULL ) {
849 srv->lud_dn = LDAP_STRDUP( "" );
852 ber = re_encode_request( ld, origreq->lr_ber, ++ld->ld_msgid,
853 sref, srv, &rinfo.ri_request );
856 ld->ld_errno = LDAP_ENCODING_ERROR;
862 LDAP_LOG ( OPERATION, DETAIL1,
863 "ldap_chase_v3referrals: msgid %d, url \"%s\"\n",
864 lr->lr_msgid, refarray[i], 0 );
866 Debug( LDAP_DEBUG_TRACE,
867 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
868 lr->lr_msgid, refarray[i], 0);
871 /* Send the new request to the server - may require a bind */
872 rinfo.ri_msgid = origreq->lr_origid;
873 rinfo.ri_url = refarray[i];
874 if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
875 origreq, srv, NULL, &rinfo )) < 0 ) {
876 /* Failure, try next referral in the list */
878 LDAP_LOG ( OPERATION, ERR,
879 "ldap_chase_v3referrals: Unable to chase referral \"%s\" (%s)\n",
880 refarray[i], ldap_err2string( ld->ld_errno ), 0 );
882 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n",
883 refarray[i], ldap_err2string( ld->ld_errno ), 0);
885 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
886 ldap_free_urllist(srv);
889 /* Success, no need to try this referral list further */
894 /* check if there is a queue of referrals that came in during bind */
896 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
897 ld->ld_errno = LDAP_OPERATIONS_ERROR;
903 if( lc->lconn_rebind_queue != NULL) {
904 /* Release resources of previous list */
905 LDAP_VFREE(refarray);
907 ldap_free_urllist(srv);
910 /* Pull entries off end of queue so list always null terminated */
911 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
914 refarray = lc->lconn_rebind_queue[j-1];
915 lc->lconn_rebind_queue[j-1] = NULL;
916 /* we pulled off last entry from queue, free queue */
918 LDAP_FREE( lc->lconn_rebind_queue);
919 lc->lconn_rebind_queue = NULL;
921 /* restart the loop the with new referral list */
925 break; /* referral followed, break out of for loop */
929 LDAP_VFREE(refarray);
930 ldap_free_urllist(srv);
931 LDAP_FREE( *errstrp );
935 LDAP_FREE( unfollowed );
938 ld->ld_errno = LDAP_REFERRAL;
939 *errstrp = unfollowed;
945 * XXX merging of errors in this routine needs to be improved
948 ldap_chase_referrals( LDAP *ld,
956 char *p, *ref, *unfollowed;
957 LDAPRequest *origreq;
963 LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_referrals\n", 0, 0, 0 );
965 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
968 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
971 if ( *errstrp == NULL ) {
975 len = strlen( *errstrp );
976 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
977 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
979 p += LDAP_REF_STR_LEN;
984 if ( len < LDAP_REF_STR_LEN ) {
988 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
990 LDAP_LOG ( OPERATION, ENTRY,
991 "ldap_chase_referrals: more than %d referral hops (dropping)\n",
992 ld->ld_refhoplimit, 0, 0 );
994 Debug( LDAP_DEBUG_ANY,
995 "more than %d referral hops (dropping)\n",
996 ld->ld_refhoplimit, 0, 0 );
998 /* XXX report as error in ld->ld_errno? */
1002 /* find original request */
1003 for ( origreq = lr; origreq->lr_parent != NULL;
1004 origreq = origreq->lr_parent ) {
1011 /* parse out & follow referrals */
1012 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1013 if (( p = strchr( ref, '\n' )) != NULL ) {
1019 rc = ldap_url_parse_ext( ref, &srv );
1021 if ( rc != LDAP_URL_SUCCESS ) {
1023 LDAP_LOG ( OPERATION, DETAIL1,
1024 "ldap_chase_referrals: ignoring unknown referral <%s>\n",
1027 Debug( LDAP_DEBUG_TRACE,
1028 "ignoring unknown referral <%s>\n", ref, 0, 0 );
1030 rc = ldap_append_referral( ld, &unfollowed, ref );
1035 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1036 LDAP_FREE( srv->lud_dn );
1041 LDAP_LOG ( OPERATION, DETAIL1,
1042 "ldap_chase_referrals: chasing LDAP referral <%s>\n", ref, 0, 0 );
1044 Debug( LDAP_DEBUG_TRACE,
1045 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1050 ber = re_encode_request( ld, origreq->lr_ber,
1051 ++ld->ld_msgid, sref, srv, &rinfo.ri_request );
1057 /* copy the complete referral for rebind process */
1058 rinfo.ri_url = LDAP_STRDUP( ref );
1060 rinfo.ri_msgid = origreq->lr_origid;
1062 rc = ldap_send_server_request( ld, ber, ld->ld_msgid,
1063 lr, srv, NULL, &rinfo );
1065 LDAP_FREE( rinfo.ri_url );
1071 LDAP_LOG ( OPERATION, ERR,
1072 "ldap_chase_referrals: Unable to chase referral <%s>\n",
1073 ldap_err2string( ld->ld_errno), 0, 0 );
1075 Debug( LDAP_DEBUG_ANY,
1076 "Unable to chase referral (%s)\n",
1077 ldap_err2string( ld->ld_errno ), 0, 0 );
1079 rc = ldap_append_referral( ld, &unfollowed, ref );
1082 ldap_free_urllist(srv);
1085 LDAP_FREE( *errstrp );
1086 *errstrp = unfollowed;
1088 return(( rc == 0 ) ? count : rc );
1093 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1097 if ( *referralsp == NULL ) {
1099 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1103 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1104 strlen( *referralsp ) + strlen( s ) + 2 );
1107 if ( *referralsp == NULL ) {
1108 ld->ld_errno = LDAP_NO_MEMORY;
1113 strcpy( *referralsp, LDAP_REF_STR );
1115 strcat( *referralsp, "\n" );
1117 strcat( *referralsp, s );
1125 re_encode_request( LDAP *ld,
1126 BerElement *origber,
1133 * XXX this routine knows way too much about how the lber library works!
1141 BerElement tmpber, *ber;
1146 LDAP_LOG ( OPERATION, ARGS,
1147 "re_encode_request: new msgid %ld, new dn <%s>\n",
1149 ( srv == NULL || srv->lud_dn == NULL ) ? "NONE" : srv->lud_dn, 0 );
1151 Debug( LDAP_DEBUG_TRACE,
1152 "re_encode_request: new msgid %ld, new dn <%s>\n",
1154 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1160 * all LDAP requests are sequences that start with a message id.
1161 * For all except delete, this is followed by a sequence that is
1162 * tagged with the operation code. For delete, the provided DN
1163 * is not wrapped by a sequence.
1165 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1167 if ( rtag == LBER_ERROR ) {
1168 ld->ld_errno = LDAP_DECODING_ERROR;
1173 if ( tag == LDAP_REQ_BIND ) {
1174 /* bind requests have a version number before the DN & other stuff */
1175 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1177 } else if ( tag == LDAP_REQ_DELETE ) {
1178 /* delete requests don't have a DN wrapping sequence */
1179 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1181 } else if ( tag == LDAP_REQ_SEARCH ) {
1182 /* search requests need to be re-scope-ed */
1183 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1185 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1186 /* use the scope provided in reference */
1187 scope = srv->lud_scope;
1189 } else if ( sref && scope != LDAP_SCOPE_SUBTREE ) {
1190 /* use scope implied by previous operation */
1193 /* subtree -> subtree */
1194 scope = LDAP_SCOPE_BASE;
1198 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1201 if( rtag == LBER_ERROR ) {
1202 ld->ld_errno = LDAP_DECODING_ERROR;
1206 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1210 if ( srv->lud_dn == NULL ) {
1216 if ( tag == LDAP_REQ_BIND ) {
1217 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1218 } else if ( tag == LDAP_REQ_DELETE ) {
1219 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1220 } else if ( tag == LDAP_REQ_SEARCH ) {
1221 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1223 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1226 LDAP_FREE( orig_dn );
1229 ld->ld_errno = LDAP_ENCODING_ERROR;
1234 if ( tag != LDAP_REQ_DELETE && (
1235 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1236 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1237 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1239 ld->ld_errno = LDAP_ENCODING_ERROR;
1245 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1247 LDAP_LOG ( OPERATION, DETAIL1,
1248 "re_encode_request: new request is:\n", 0, 0, 0 );
1250 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1253 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1255 #endif /* LDAP_DEBUG */
1257 *type = tag; /* return request type */
1263 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1267 #ifdef LDAP_R_COMPILE
1268 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1270 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1271 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1272 continue; /* Skip completed requests */
1274 if ( msgid == lr->lr_msgid ) {
1278 #ifdef LDAP_R_COMPILE
1279 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );