2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 /* This notice applies to changes, created by or for Novell, Inc.,
19 * to preexisting works for which notices appear elsewhere in this file.
21 * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
23 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
24 * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
25 * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
26 * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
27 * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
28 * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
29 * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
30 * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
32 * Modification to OpenLDAP source by Novell, Inc.
33 * April 2000 sfs Added code to chase V3 referrals
34 * request.c - sending of ldap requests; handling of referrals
36 * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
37 * can be found in the file "build/LICENSE-2.0.1" in this distribution
38 * of OpenLDAP Software.
45 #include <ac/stdlib.h>
48 #include <ac/socket.h>
49 #include <ac/string.h>
51 #include <ac/unistd.h>
56 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any ));
57 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
58 static void ldap_free_request_int LDAP_P(( LDAP *ld, LDAPRequest *lr ));
61 re_encode_request( LDAP *ld,
69 ldap_alloc_ber_with_options( LDAP *ld )
73 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
74 ld->ld_errno = LDAP_NO_MEMORY;
82 ldap_set_ber_options( LDAP *ld, BerElement *ber )
84 ber->ber_options = ld->ld_lberoptions;
89 ldap_send_initial_request(
99 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
101 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
102 /* not connected yet */
103 int rc = ldap_open_defconn( ld );
110 Debug( LDAP_DEBUG_TRACE,
111 "ldap_open_defconn: successful\n",
117 * use of DNS is turned off or this is an X.500 DN...
118 * use our default connection
123 #ifdef LDAP_CONNECTIONLESS
124 if (LDAP_IS_UDP(ld)) {
125 if (msgtype == LDAP_REQ_BIND) {
126 if (ld->ld_options.ldo_cldapdn)
127 ldap_memfree(ld->ld_options.ldo_cldapdn);
128 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
131 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
132 return LDAP_PARAM_ERROR;
135 #ifdef LDAP_R_COMPILE
136 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
138 rc = ldap_send_server_request( ld, ber, msgid, NULL,
139 servers, NULL, NULL );
140 #ifdef LDAP_R_COMPILE
141 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
144 ldap_free_urllist(servers);
150 ldap_int_flush_request(
154 LDAPConn *lc = lr->lr_conn;
156 if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
157 if ( errno == EAGAIN ) {
158 /* need to continue write later */
159 lr->lr_status = LDAP_REQST_WRITING;
160 ldap_mark_select_write( ld, lc->lconn_sb );
161 ld->ld_errno = LDAP_BUSY;
164 ld->ld_errno = LDAP_SERVER_DOWN;
165 ldap_free_request( ld, lr );
166 ldap_free_connection( ld, lc, 0, 0 );
170 if ( lr->lr_parent == NULL ) {
171 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
172 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
174 lr->lr_status = LDAP_REQST_INPROGRESS;
176 /* sent -- waiting for a response */
177 ldap_mark_select_read( ld, lc->lconn_sb );
183 ldap_send_server_request(
187 LDAPRequest *parentreq,
188 LDAPURLDesc *srvlist,
195 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
198 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
201 if ( srvlist == NULL ) {
204 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 if ( ld->ld_requests &&
237 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
238 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
244 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
246 ld->ld_errno = LDAP_NO_MEMORY;
247 ldap_free_connection( ld, lc, 0, 0 );
250 /* Forget about the bind */
251 --parentreq->lr_outrefcnt;
255 lr->lr_msgid = msgid;
256 lr->lr_status = LDAP_REQST_INPROGRESS;
257 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
260 if ( parentreq != NULL ) { /* sub-request */
262 /* Increment if we didn't do it before the bind */
263 ++parentreq->lr_outrefcnt;
265 lr->lr_origid = parentreq->lr_origid;
266 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
267 lr->lr_parent = parentreq;
268 lr->lr_refnext = parentreq->lr_child;
269 parentreq->lr_child = lr;
270 } else { /* original request */
271 lr->lr_origid = lr->lr_msgid;
274 /* Extract requestDN for future reference */
276 BerElement tmpber = *ber;
280 ber_reset( &tmpber, 1 );
281 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
284 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
286 case LDAP_REQ_DELETE:
289 rtag = ber_scanf( &tmpber, "{" /*}*/ );
290 case LDAP_REQ_ABANDON:
293 if ( tag != LDAP_REQ_ABANDON ) {
294 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
295 lr->lr_dn.bv_val = tmpber.ber_ptr;
300 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
301 lr->lr_next->lr_prev = lr;
303 ld->ld_requests = lr;
305 ld->ld_errno = LDAP_SUCCESS;
306 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
314 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
315 int connect, LDAPreqinfo *bind )
320 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
321 use_ldsb, connect, (bind != NULL) );
323 * make a new LDAP server connection
324 * XXX open connection synchronously for now
326 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
328 ld->ld_errno = LDAP_NO_MEMORY;
333 assert( ld->ld_sb != NULL );
334 lc->lconn_sb = ld->ld_sb;
337 lc->lconn_sb = ber_sockbuf_alloc();
338 if ( lc->lconn_sb == NULL ) {
339 LDAP_FREE( (char *)lc );
340 ld->ld_errno = LDAP_NO_MEMORY;
346 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
347 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 )
355 ber_sockbuf_free( lc->lconn_sb );
357 LDAP_FREE( (char *)lc );
358 ld->ld_errno = LDAP_SERVER_DOWN;
362 lc->lconn_server = ldap_url_dup( srv );
365 lc->lconn_status = LDAP_CONNST_CONNECTED;
366 #ifdef LDAP_R_COMPILE
367 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
369 lc->lconn_next = ld->ld_conns;
371 #ifdef LDAP_R_COMPILE
372 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
376 * XXX for now, we always do a synchronous bind. This will have
377 * to change in the long run...
381 LDAPConn *savedefconn;
383 /* Set flag to prevent additional referrals
384 * from being processed on this
385 * connection until the bind has completed
387 lc->lconn_rebind_inprogress = 1;
388 /* V3 rebind function */
389 if ( ld->ld_rebind_proc != NULL) {
390 LDAPURLDesc *srvfunc;
392 srvfunc = ldap_url_dup( srvlist );
393 if ( srvfunc == NULL ) {
394 ld->ld_errno = LDAP_NO_MEMORY;
397 savedefconn = ld->ld_defconn;
398 ++lc->lconn_refcnt; /* avoid premature free */
401 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
402 #ifdef LDAP_R_COMPILE
403 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
404 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
406 err = (*ld->ld_rebind_proc)( ld,
407 bind->ri_url, bind->ri_request, bind->ri_msgid,
408 ld->ld_rebind_params );
409 #ifdef LDAP_R_COMPILE
410 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
411 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
414 ld->ld_defconn = savedefconn;
419 ldap_free_connection( ld, lc, 1, 0 );
422 ldap_free_urldesc( srvfunc );
425 savedefconn = ld->ld_defconn;
426 ++lc->lconn_refcnt; /* avoid premature free */
429 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
430 #ifdef LDAP_R_COMPILE
431 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
432 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
434 if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
437 #ifdef LDAP_R_COMPILE
438 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
439 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
441 ld->ld_defconn = savedefconn;
445 ldap_free_connection( ld, lc, 1, 0 );
450 lc->lconn_rebind_inprogress = 0;
458 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
460 * return an existing connection (if any) to the server srv
461 * if "any" is non-zero, check for any server in the "srv" chain
465 LDAPURLDesc *lcu, *lsu;
466 int lcu_port, lsu_port;
469 #ifdef LDAP_R_COMPILE
470 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
472 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
473 lcu = lc->lconn_server;
474 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
477 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
478 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
481 if ( lsu_port == lcu_port
482 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
483 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
484 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
485 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
496 #ifdef LDAP_R_COMPILE
497 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
505 use_connection( LDAP *ld, LDAPConn *lc )
508 lc->lconn_lastused = time( NULL );
513 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
515 LDAPConn *tmplc, *prevlc;
517 Debug( LDAP_DEBUG_TRACE,
518 "ldap_free_connection %d %d\n",
521 if ( force || --lc->lconn_refcnt <= 0 ) {
522 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
523 ldap_mark_select_clear( ld, lc->lconn_sb );
525 ldap_send_unbind( ld, lc->lconn_sb,
530 if ( lc->lconn_ber != NULL ) {
531 ber_free( lc->lconn_ber, 1 );
534 ldap_int_sasl_close( ld, lc );
537 #ifdef LDAP_R_COMPILE
538 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
540 for ( tmplc = ld->ld_conns;
542 tmplc = tmplc->lconn_next )
545 if ( prevlc == NULL ) {
546 ld->ld_conns = tmplc->lconn_next;
548 prevlc->lconn_next = tmplc->lconn_next;
554 #ifdef LDAP_R_COMPILE
555 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
557 ldap_free_urllist( lc->lconn_server );
558 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
559 if ( lc->lconn_krbinstance != NULL ) {
560 LDAP_FREE( lc->lconn_krbinstance );
564 /* FIXME: is this at all possible? */
568 for ( lr = ld->ld_requests; lr; ) {
569 LDAPRequest *lr_next = lr->lr_next;
571 if ( lr->lr_conn == lc ) {
572 ldap_free_request_int( ld, lr );
578 if ( lc->lconn_sb != ld->ld_sb ) {
579 ber_sockbuf_free( lc->lconn_sb );
581 if ( lc->lconn_rebind_queue != NULL) {
583 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
584 LDAP_VFREE( lc->lconn_rebind_queue[i] );
586 LDAP_FREE( lc->lconn_rebind_queue );
589 Debug( LDAP_DEBUG_TRACE,
590 "ldap_free_connection: actually freed\n",
593 lc->lconn_lastused = time( NULL );
594 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
595 lc->lconn_refcnt, 0, 0 );
602 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
607 fprintf( stderr, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "" );
608 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
609 if ( lc->lconn_server != NULL ) {
610 fprintf( stderr, "* host: %s port: %d%s\n",
611 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
612 : lc->lconn_server->lud_host,
613 lc->lconn_server->lud_port, ( lc->lconn_sb ==
614 ld->ld_sb ) ? " (default)" : "" );
616 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
617 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
618 "NeedSocket" : ( lc->lconn_status ==
619 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
620 fprintf( stderr, " last used: %s",
621 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
622 if( lc->lconn_rebind_inprogress ) {
623 fprintf( stderr, " rebind in progress\n");
624 if( lc->lconn_rebind_queue != NULL) {
626 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
628 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
629 fprintf( stderr, " queue %d entry %d - %s\n",
630 i, j, lc->lconn_rebind_queue[i][j]);
634 fprintf( stderr, " queue is empty\n");
637 fprintf(stderr, "\n");
646 ldap_dump_requests_and_responses( LDAP *ld )
651 #ifdef LDAP_R_COMPILE
652 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
654 fprintf( stderr, "** ld %p Outstanding Requests:\n", (void *)ld );
655 if (( lr = ld->ld_requests ) == NULL ) {
656 fprintf( stderr, " Empty\n" );
658 for ( ; lr != NULL; lr = lr->lr_next ) {
659 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
660 lr->lr_msgid, lr->lr_origid,
661 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
662 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
663 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
664 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
665 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
667 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
668 lr->lr_outrefcnt, lr->lr_parentcnt );
670 #ifdef LDAP_R_COMPILE
671 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
673 fprintf( stderr, "** ld %p Response Queue:\n", (void *)ld );
674 if (( lm = ld->ld_responses ) == NULL ) {
675 fprintf( stderr, " Empty\n" );
677 for ( ; lm != NULL; lm = lm->lm_next ) {
678 fprintf( stderr, " * msgid %d, type %lu\n",
679 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
680 if (( l = lm->lm_chain ) != NULL ) {
681 fprintf( stderr, " chained responses:\n" );
682 for ( ; l != NULL; l = l->lm_chain ) {
684 " * msgid %d, type %lu\n",
686 (unsigned long) l->lm_msgtype );
691 #endif /* LDAP_DEBUG */
694 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
696 if ( lr->lr_prev == NULL ) {
697 /* free'ing the first request? */
698 assert( ld->ld_requests == lr );
699 ld->ld_requests = lr->lr_next;
702 lr->lr_prev->lr_next = lr->lr_next;
705 if ( lr->lr_next != NULL ) {
706 lr->lr_next->lr_prev = lr->lr_prev;
709 if ( lr->lr_ber != NULL ) {
710 ber_free( lr->lr_ber, 1 );
713 if ( lr->lr_res_error != NULL ) {
714 LDAP_FREE( lr->lr_res_error );
717 if ( lr->lr_res_matched != NULL ) {
718 LDAP_FREE( lr->lr_res_matched );
725 ldap_free_request( LDAP *ld, LDAPRequest *lr )
727 LDAPRequest **ttmplr;
729 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
730 lr->lr_origid, lr->lr_msgid, 0 );
732 /* free all referrals (child requests) */
733 while ( lr->lr_child )
734 ldap_free_request( ld, lr->lr_child );
736 if ( lr->lr_parent != NULL ) {
737 --lr->lr_parent->lr_outrefcnt;
738 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
740 *ttmplr = lr->lr_refnext;
742 ldap_free_request_int( ld, lr );
746 * call first time with *cntp = -1
747 * when returns *cntp == -1, no referrals are left
749 * NOTE: may replace *refsp, or shuffle the contents
750 * of the original array.
752 static int ldap_int_nextref(
758 assert( refsp != NULL );
759 assert( *refsp != NULL );
760 assert( cntp != NULL );
769 if ( (*refsp)[ *cntp ] == NULL ) {
780 * (IN) ld = LDAP connection handle
781 * (IN) lr = LDAP Request structure
782 * (IN) refs = array of pointers to referral strings that we will chase
783 * The array will be free'd by this function when no longer needed
784 * (IN) sref != 0 if following search reference
785 * (OUT) errstrp = Place to return a string of referrals which could not be followed
786 * (OUT) hadrefp = 1 if sucessfully followed referral
788 * Return value - number of referrals followed
791 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
794 int unfollowedcnt = 0;
795 LDAPRequest *origreq;
796 LDAPURLDesc *srv = NULL;
798 char **refarray = NULL;
800 int rc, count, i, j, id;
803 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
806 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
811 /* If no referrals in array, return */
812 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
817 /* Check for hop limit exceeded */
818 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
819 Debug( LDAP_DEBUG_ANY,
820 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
821 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
826 /* find original request */
828 origreq->lr_parent != NULL;
829 origreq = origreq->lr_parent )
837 if ( ld->ld_nextref_proc == NULL ) {
838 ld->ld_nextref_proc = ldap_int_nextref;
841 /* parse out & follow referrals */
843 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
845 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
848 /* Parse the referral URL */
849 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
855 if( srv->lud_crit_exts ) {
856 /* we do not support any extensions */
857 ld->ld_errno = LDAP_NOT_SUPPORTED;
862 /* treat ldap://hostpart and ldap://hostpart/ the same */
863 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
864 LDAP_FREE( srv->lud_dn );
868 /* check connection for re-bind in progress */
869 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
870 /* See if we've already requested this DN with this conn */
873 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
874 for (lp = origreq; lp; ) {
875 if ( lp->lr_conn == lc ) {
876 if ( len == lp->lr_dn.bv_len ) {
877 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val,
890 ldap_free_urllist( srv );
892 ld->ld_errno = LDAP_CLIENT_LOOP;
897 if( lc->lconn_rebind_inprogress) {
898 /* We are already chasing a referral or search reference and a
899 * bind on that connection is in progress. We must queue
900 * referrals on that connection, so we don't get a request
901 * going out before the bind operation completes. This happens
902 * if two search references come in one behind the other
903 * for the same server with different contexts.
905 Debug( LDAP_DEBUG_TRACE,
906 "ldap_chase_v3referrals: queue referral \"%s\"\n",
908 if( lc->lconn_rebind_queue == NULL ) {
909 /* Create a referral list */
910 lc->lconn_rebind_queue =
911 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
913 if( lc->lconn_rebind_queue == NULL) {
914 ld->ld_errno = LDAP_NO_MEMORY;
919 lc->lconn_rebind_queue[0] = refarray;
920 lc->lconn_rebind_queue[1] = NULL;
924 /* Count how many referral arrays we already have */
925 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
929 /* Add the new referral to the list */
930 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
931 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
933 if( lc->lconn_rebind_queue == NULL ) {
934 ld->ld_errno = LDAP_NO_MEMORY;
938 lc->lconn_rebind_queue[j] = refarray;
939 lc->lconn_rebind_queue[j+1] = NULL;
943 /* We have queued the referral/reference, now just return */
946 count = 1; /* Pretend we already followed referral */
950 /* Re-encode the request with the new starting point of the search.
951 * Note: In the future we also need to replace the filter if one
952 * was provided with the search reference
955 /* For references we don't want old dn if new dn empty */
956 if ( sref && srv->lud_dn == NULL ) {
957 srv->lud_dn = LDAP_STRDUP( "" );
960 LDAP_NEXT_MSGID( ld, id );
961 ber = re_encode_request( ld, origreq->lr_ber, id,
962 sref, srv, &rinfo.ri_request );
965 ld->ld_errno = LDAP_ENCODING_ERROR;
970 Debug( LDAP_DEBUG_TRACE,
971 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
972 lr->lr_msgid, refarray[i], 0);
974 /* Send the new request to the server - may require a bind */
975 rinfo.ri_msgid = origreq->lr_origid;
976 rinfo.ri_url = refarray[i];
977 #ifdef LDAP_R_COMPILE
978 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
980 rc = ldap_send_server_request( ld, ber, id,
981 origreq, srv, NULL, &rinfo );
982 #ifdef LDAP_R_COMPILE
983 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
986 /* Failure, try next referral in the list */
987 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
988 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
989 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
990 ldap_free_urllist( srv );
992 ld->ld_errno = LDAP_REFERRAL;
994 /* Success, no need to try this referral list further */
999 /* check if there is a queue of referrals that came in during bind */
1001 lc = find_connection( ld, srv, 1 );
1003 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1009 if ( lc->lconn_rebind_queue != NULL ) {
1010 /* Release resources of previous list */
1011 LDAP_VFREE( refarray );
1013 ldap_free_urllist( srv );
1016 /* Pull entries off end of queue so list always null terminated */
1017 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1019 refarray = lc->lconn_rebind_queue[j - 1];
1020 lc->lconn_rebind_queue[j-1] = NULL;
1021 /* we pulled off last entry from queue, free queue */
1023 LDAP_FREE( lc->lconn_rebind_queue );
1024 lc->lconn_rebind_queue = NULL;
1026 /* restart the loop the with new referral list */
1030 break; /* referral followed, break out of for loop */
1032 } /* end for loop */
1034 LDAP_VFREE( refarray );
1035 ldap_free_urllist( srv );
1036 LDAP_FREE( *errstrp );
1040 LDAP_FREE( unfollowed );
1043 *errstrp = unfollowed;
1049 * XXX merging of errors in this routine needs to be improved
1052 ldap_chase_referrals( LDAP *ld,
1060 char *p, *ref, *unfollowed;
1061 LDAPRequest *origreq;
1067 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1069 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1072 if ( *errstrp == NULL ) {
1076 len = strlen( *errstrp );
1077 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1078 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1080 p += LDAP_REF_STR_LEN;
1085 if ( len < LDAP_REF_STR_LEN ) {
1089 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1090 Debug( LDAP_DEBUG_ANY,
1091 "more than %d referral hops (dropping)\n",
1092 ld->ld_refhoplimit, 0, 0 );
1093 /* XXX report as error in ld->ld_errno? */
1097 /* find original request */
1098 for ( origreq = lr; origreq->lr_parent != NULL;
1099 origreq = origreq->lr_parent ) {
1106 /* parse out & follow referrals */
1107 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1108 p = strchr( ref, '\n' );
1113 rc = ldap_url_parse_ext( ref, &srv );
1115 if ( rc != LDAP_URL_SUCCESS ) {
1116 Debug( LDAP_DEBUG_TRACE,
1117 "ignoring unknown referral <%s>\n", ref, 0, 0 );
1118 rc = ldap_append_referral( ld, &unfollowed, ref );
1123 if ( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1124 LDAP_FREE( srv->lud_dn );
1128 Debug( LDAP_DEBUG_TRACE,
1129 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1133 /* See if we've already been here */
1134 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1137 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1138 for (lp = lr; lp; lp = lp->lr_parent ) {
1139 if ( lp->lr_conn == lc ) {
1140 if ( len == lp->lr_dn.bv_len ) {
1141 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val,
1150 ldap_free_urllist(srv);
1151 ld->ld_errno = LDAP_CLIENT_LOOP;
1157 LDAP_NEXT_MSGID( ld, id );
1158 ber = re_encode_request( ld, origreq->lr_ber,
1159 id, sref, srv, &rinfo.ri_request );
1161 if ( ber == NULL ) {
1165 /* copy the complete referral for rebind process */
1166 rinfo.ri_url = LDAP_STRDUP( ref );
1168 rinfo.ri_msgid = origreq->lr_origid;
1170 #ifdef LDAP_R_COMPILE
1171 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1173 rc = ldap_send_server_request( ld, ber, id,
1174 lr, srv, NULL, &rinfo );
1175 #ifdef LDAP_R_COMPILE
1176 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1179 LDAP_FREE( rinfo.ri_url );
1184 Debug( LDAP_DEBUG_ANY,
1185 "Unable to chase referral \"%s\" (%d: %s)\n",
1186 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1187 rc = ldap_append_referral( ld, &unfollowed, ref );
1190 ldap_free_urllist(srv);
1193 LDAP_FREE( *errstrp );
1194 *errstrp = unfollowed;
1196 return(( rc == 0 ) ? count : rc );
1201 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1205 if ( *referralsp == NULL ) {
1207 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1211 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1212 strlen( *referralsp ) + strlen( s ) + 2 );
1215 if ( *referralsp == NULL ) {
1216 ld->ld_errno = LDAP_NO_MEMORY;
1221 strcpy( *referralsp, LDAP_REF_STR );
1223 strcat( *referralsp, "\n" );
1225 strcat( *referralsp, s );
1233 re_encode_request( LDAP *ld,
1234 BerElement *origber,
1241 * XXX this routine knows way too much about how the lber library works!
1249 BerElement tmpber, *ber;
1250 struct berval orig_dn;
1253 Debug( LDAP_DEBUG_TRACE,
1254 "re_encode_request: new msgid %ld, new dn <%s>\n",
1256 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1261 * all LDAP requests are sequences that start with a message id.
1262 * For all except delete, this is followed by a sequence that is
1263 * tagged with the operation code. For delete, the provided DN
1264 * is not wrapped by a sequence.
1266 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1268 if ( rtag == LBER_ERROR ) {
1269 ld->ld_errno = LDAP_DECODING_ERROR;
1274 if ( tag == LDAP_REQ_BIND ) {
1275 /* bind requests have a version number before the DN & other stuff */
1276 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &orig_dn );
1278 } else if ( tag == LDAP_REQ_DELETE ) {
1279 /* delete requests don't have a DN wrapping sequence */
1280 rtag = ber_scanf( &tmpber, "m", &orig_dn );
1282 } else if ( tag == LDAP_REQ_SEARCH ) {
1283 /* search requests need to be re-scope-ed */
1284 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &orig_dn, &scope );
1286 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1287 /* use the scope provided in reference */
1288 scope = srv->lud_scope;
1290 } else if ( sref ) {
1291 /* use scope implied by previous operation
1294 * subtree -> subtree
1295 * subordinate -> subtree
1299 case LDAP_SCOPE_BASE:
1300 case LDAP_SCOPE_ONELEVEL:
1301 scope = LDAP_SCOPE_BASE;
1303 case LDAP_SCOPE_SUBTREE:
1304 case LDAP_SCOPE_SUBORDINATE:
1305 scope = LDAP_SCOPE_SUBTREE;
1311 rtag = ber_scanf( &tmpber, "{m" /*}*/, &orig_dn );
1314 if( rtag == LBER_ERROR ) {
1315 ld->ld_errno = LDAP_DECODING_ERROR;
1319 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1323 if ( srv->lud_dn == NULL ) {
1324 dn = orig_dn.bv_val;
1329 if ( tag == LDAP_REQ_BIND ) {
1330 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1331 } else if ( tag == LDAP_REQ_DELETE ) {
1332 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1333 } else if ( tag == LDAP_REQ_SEARCH ) {
1334 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1336 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1340 ld->ld_errno = LDAP_ENCODING_ERROR;
1345 if ( tag != LDAP_REQ_DELETE && (
1346 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1347 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1348 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1350 ld->ld_errno = LDAP_ENCODING_ERROR;
1356 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1357 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1359 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1361 #endif /* LDAP_DEBUG */
1363 *type = tag; /* return request type */
1369 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1373 #ifdef LDAP_R_COMPILE
1374 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1376 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1377 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1378 continue; /* Skip completed requests */
1380 if ( msgid == lr->lr_msgid ) {
1384 #ifdef LDAP_R_COMPILE
1385 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );