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(
98 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
100 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
101 /* not connected yet */
102 int rc = ldap_open_defconn( ld );
109 Debug( LDAP_DEBUG_TRACE,
110 "ldap_open_defconn: successful\n",
114 #ifdef LDAP_CONNECTIONLESS
115 if (LDAP_IS_UDP(ld)) {
116 if (msgtype == LDAP_REQ_BIND) {
117 if (ld->ld_options.ldo_cldapdn)
118 ldap_memfree(ld->ld_options.ldo_cldapdn);
119 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
122 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
123 return LDAP_PARAM_ERROR;
126 #ifdef LDAP_R_COMPILE
127 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
129 rc = ldap_send_server_request( ld, ber, msgid, NULL,
131 #ifdef LDAP_R_COMPILE
132 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
139 ldap_int_flush_request(
143 LDAPConn *lc = lr->lr_conn;
145 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
146 if ( errno == EAGAIN ) {
147 /* need to continue write later */
148 lr->lr_status = LDAP_REQST_WRITING;
149 ldap_mark_select_write( ld, lc->lconn_sb );
150 ld->ld_errno = LDAP_BUSY;
153 ld->ld_errno = LDAP_SERVER_DOWN;
154 ldap_free_request( ld, lr );
155 ldap_free_connection( ld, lc, 0, 0 );
159 if ( lr->lr_parent == NULL ) {
160 lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
161 lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
163 lr->lr_status = LDAP_REQST_INPROGRESS;
165 /* sent -- waiting for a response */
166 ldap_mark_select_read( ld, lc->lconn_sb );
172 ldap_send_server_request(
176 LDAPRequest *parentreq,
177 LDAPURLDesc *srvlist,
184 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
187 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
190 if ( srvlist == NULL ) {
193 lc = find_connection( ld, srvlist, 1 );
195 if ( (bind != NULL) && (parentreq != NULL) ) {
196 /* Remember the bind in the parent */
198 ++parentreq->lr_outrefcnt;
200 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
205 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
207 if ( ld->ld_errno == LDAP_SUCCESS ) {
208 ld->ld_errno = LDAP_SERVER_DOWN;
211 /* Forget about the bind */
212 --parentreq->lr_outrefcnt;
217 use_connection( ld, lc );
219 /* If we still have an incomplete write, try to finish it before
220 * dealing with the new request. If we don't finish here, return
221 * LDAP_BUSY and let the caller retry later. We only allow a single
222 * request to be in WRITING state.
225 if ( ld->ld_requests &&
226 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
227 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
233 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
235 ld->ld_errno = LDAP_NO_MEMORY;
236 ldap_free_connection( ld, lc, 0, 0 );
239 /* Forget about the bind */
240 --parentreq->lr_outrefcnt;
244 lr->lr_msgid = msgid;
245 lr->lr_status = LDAP_REQST_INPROGRESS;
246 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
249 if ( parentreq != NULL ) { /* sub-request */
251 /* Increment if we didn't do it before the bind */
252 ++parentreq->lr_outrefcnt;
254 lr->lr_origid = parentreq->lr_origid;
255 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
256 lr->lr_parent = parentreq;
257 lr->lr_refnext = parentreq->lr_child;
258 parentreq->lr_child = lr;
259 } else { /* original request */
260 lr->lr_origid = lr->lr_msgid;
263 /* Extract requestDN for future reference */
265 BerElement tmpber = *ber;
269 ber_reset( &tmpber, 1 );
270 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
273 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
275 case LDAP_REQ_DELETE:
278 rtag = ber_scanf( &tmpber, "{" /*}*/ );
279 case LDAP_REQ_ABANDON:
282 if ( tag != LDAP_REQ_ABANDON ) {
283 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
284 lr->lr_dn.bv_val = tmpber.ber_ptr;
289 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
290 lr->lr_next->lr_prev = lr;
292 ld->ld_requests = lr;
294 ld->ld_errno = LDAP_SUCCESS;
295 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
303 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
304 int connect, LDAPreqinfo *bind )
309 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
310 use_ldsb, connect, (bind != NULL) );
312 * make a new LDAP server connection
313 * XXX open connection synchronously for now
315 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
317 ld->ld_errno = LDAP_NO_MEMORY;
322 assert( ld->ld_sb != NULL );
323 lc->lconn_sb = ld->ld_sb;
326 lc->lconn_sb = ber_sockbuf_alloc();
327 if ( lc->lconn_sb == NULL ) {
328 LDAP_FREE( (char *)lc );
329 ld->ld_errno = LDAP_NO_MEMORY;
335 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
336 if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 )
344 ber_sockbuf_free( lc->lconn_sb );
346 LDAP_FREE( (char *)lc );
347 ld->ld_errno = LDAP_SERVER_DOWN;
351 lc->lconn_server = ldap_url_dup( srv );
354 lc->lconn_status = LDAP_CONNST_CONNECTED;
355 #ifdef LDAP_R_COMPILE
356 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
358 lc->lconn_next = ld->ld_conns;
360 #ifdef LDAP_R_COMPILE
361 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
365 * XXX for now, we always do a synchronous bind. This will have
366 * to change in the long run...
370 LDAPConn *savedefconn;
372 /* Set flag to prevent additional referrals
373 * from being processed on this
374 * connection until the bind has completed
376 lc->lconn_rebind_inprogress = 1;
377 /* V3 rebind function */
378 if ( ld->ld_rebind_proc != NULL) {
379 LDAPURLDesc *srvfunc;
381 srvfunc = ldap_url_dup( srvlist );
382 if ( srvfunc == NULL ) {
383 ld->ld_errno = LDAP_NO_MEMORY;
386 savedefconn = ld->ld_defconn;
387 ++lc->lconn_refcnt; /* avoid premature free */
390 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
391 #ifdef LDAP_R_COMPILE
392 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
393 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
395 err = (*ld->ld_rebind_proc)( ld,
396 bind->ri_url, bind->ri_request, bind->ri_msgid,
397 ld->ld_rebind_params );
398 #ifdef LDAP_R_COMPILE
399 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
400 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
403 ld->ld_defconn = savedefconn;
408 ldap_free_connection( ld, lc, 1, 0 );
411 ldap_free_urldesc( srvfunc );
415 struct berval passwd = BER_BVNULL;
417 savedefconn = ld->ld_defconn;
418 ++lc->lconn_refcnt; /* avoid premature free */
421 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
422 #ifdef LDAP_R_COMPILE
423 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
424 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
426 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
427 NULL, NULL, &msgid );
428 if ( rc != LDAP_SUCCESS ) {
432 for ( err = 1; err > 0; ) {
433 struct timeval tv = { 0, 100000 };
434 LDAPMessage *res = NULL;
436 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
442 #ifdef LDAP_R_COMPILE
443 ldap_pvt_thread_yield();
448 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
449 if ( rc != LDAP_SUCCESS ) {
452 } else if ( err != LDAP_SUCCESS ) {
455 /* else err == LDAP_SUCCESS == 0 */
463 #ifdef LDAP_R_COMPILE
464 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
465 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
467 ld->ld_defconn = savedefconn;
471 ldap_free_connection( ld, lc, 1, 0 );
476 lc->lconn_rebind_inprogress = 0;
484 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
486 * return an existing connection (if any) to the server srv
487 * if "any" is non-zero, check for any server in the "srv" chain
491 LDAPURLDesc *lcu, *lsu;
492 int lcu_port, lsu_port;
495 #ifdef LDAP_R_COMPILE
496 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
498 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
499 lcu = lc->lconn_server;
500 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
503 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
504 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
507 if ( lsu_port == lcu_port
508 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
509 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
510 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
511 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
522 #ifdef LDAP_R_COMPILE
523 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
531 use_connection( LDAP *ld, LDAPConn *lc )
534 lc->lconn_lastused = time( NULL );
539 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
541 LDAPConn *tmplc, *prevlc;
543 Debug( LDAP_DEBUG_TRACE,
544 "ldap_free_connection %d %d\n",
547 if ( force || --lc->lconn_refcnt <= 0 ) {
548 /* remove from connections list first */
549 #ifdef LDAP_R_COMPILE
550 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
553 for ( prevlc = NULL, tmplc = ld->ld_conns;
555 tmplc = tmplc->lconn_next )
558 if ( prevlc == NULL ) {
559 ld->ld_conns = tmplc->lconn_next;
561 prevlc->lconn_next = tmplc->lconn_next;
567 #ifdef LDAP_R_COMPILE
568 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
571 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
572 ldap_mark_select_clear( ld, lc->lconn_sb );
574 ldap_send_unbind( ld, lc->lconn_sb,
579 if ( lc->lconn_ber != NULL ) {
580 ber_free( lc->lconn_ber, 1 );
583 ldap_int_sasl_close( ld, lc );
585 ldap_free_urllist( lc->lconn_server );
586 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
587 if ( lc->lconn_krbinstance != NULL ) {
588 LDAP_FREE( lc->lconn_krbinstance );
592 /* FIXME: is this at all possible? */
596 for ( lr = ld->ld_requests; lr; ) {
597 LDAPRequest *lr_next = lr->lr_next;
599 if ( lr->lr_conn == lc ) {
600 ldap_free_request_int( ld, lr );
607 if ( lc->lconn_sb != ld->ld_sb ) {
608 ber_sockbuf_free( lc->lconn_sb );
611 if ( lc->lconn_rebind_queue != NULL) {
613 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
614 LDAP_VFREE( lc->lconn_rebind_queue[i] );
616 LDAP_FREE( lc->lconn_rebind_queue );
621 Debug( LDAP_DEBUG_TRACE,
622 "ldap_free_connection: actually freed\n",
626 lc->lconn_lastused = time( NULL );
627 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
628 lc->lconn_refcnt, 0, 0 );
635 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
640 fprintf( stderr, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "" );
641 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
642 if ( lc->lconn_server != NULL ) {
643 fprintf( stderr, "* host: %s port: %d%s\n",
644 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
645 : lc->lconn_server->lud_host,
646 lc->lconn_server->lud_port, ( lc->lconn_sb ==
647 ld->ld_sb ) ? " (default)" : "" );
649 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
650 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
651 "NeedSocket" : ( lc->lconn_status ==
652 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
653 fprintf( stderr, " last used: %s",
654 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
655 if( lc->lconn_rebind_inprogress ) {
656 fprintf( stderr, " rebind in progress\n");
657 if( lc->lconn_rebind_queue != NULL) {
659 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
661 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
662 fprintf( stderr, " queue %d entry %d - %s\n",
663 i, j, lc->lconn_rebind_queue[i][j]);
667 fprintf( stderr, " queue is empty\n");
670 fprintf(stderr, "\n");
679 ldap_dump_requests_and_responses( LDAP *ld )
684 #ifdef LDAP_R_COMPILE
685 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
687 fprintf( stderr, "** ld %p Outstanding Requests:\n", (void *)ld );
688 if (( lr = ld->ld_requests ) == NULL ) {
689 fprintf( stderr, " Empty\n" );
691 for ( ; lr != NULL; lr = lr->lr_next ) {
692 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
693 lr->lr_msgid, lr->lr_origid,
694 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
695 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
696 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
697 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
698 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
700 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
701 lr->lr_outrefcnt, lr->lr_parentcnt );
703 #ifdef LDAP_R_COMPILE
704 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
706 fprintf( stderr, "** ld %p Response Queue:\n", (void *)ld );
707 if (( lm = ld->ld_responses ) == NULL ) {
708 fprintf( stderr, " Empty\n" );
710 for ( ; lm != NULL; lm = lm->lm_next ) {
711 fprintf( stderr, " * msgid %d, type %lu\n",
712 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
713 if (( l = lm->lm_chain ) != NULL ) {
714 fprintf( stderr, " chained responses:\n" );
715 for ( ; l != NULL; l = l->lm_chain ) {
717 " * msgid %d, type %lu\n",
719 (unsigned long) l->lm_msgtype );
724 #endif /* LDAP_DEBUG */
727 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
729 if ( lr->lr_prev == NULL ) {
730 /* free'ing the first request? */
731 assert( ld->ld_requests == lr );
732 ld->ld_requests = lr->lr_next;
735 lr->lr_prev->lr_next = lr->lr_next;
738 if ( lr->lr_next != NULL ) {
739 lr->lr_next->lr_prev = lr->lr_prev;
742 if ( lr->lr_ber != NULL ) {
743 ber_free( lr->lr_ber, 1 );
747 if ( lr->lr_res_error != NULL ) {
748 LDAP_FREE( lr->lr_res_error );
749 lr->lr_res_error = NULL;
752 if ( lr->lr_res_matched != NULL ) {
753 LDAP_FREE( lr->lr_res_matched );
754 lr->lr_res_matched = NULL;
761 ldap_free_request( LDAP *ld, LDAPRequest *lr )
763 LDAPRequest **ttmplr;
764 #ifdef LDAP_R_COMPILE
765 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
768 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
769 lr->lr_origid, lr->lr_msgid, 0 );
771 /* free all referrals (child requests) */
772 while ( lr->lr_child )
773 ldap_free_request( ld, lr->lr_child );
775 if ( lr->lr_parent != NULL ) {
776 --lr->lr_parent->lr_outrefcnt;
777 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
779 *ttmplr = lr->lr_refnext;
781 ldap_free_request_int( ld, lr );
785 * call first time with *cntp = -1
786 * when returns *cntp == -1, no referrals are left
788 * NOTE: may replace *refsp, or shuffle the contents
789 * of the original array.
791 static int ldap_int_nextref(
797 assert( refsp != NULL );
798 assert( *refsp != NULL );
799 assert( cntp != NULL );
808 if ( (*refsp)[ *cntp ] == NULL ) {
819 * (IN) ld = LDAP connection handle
820 * (IN) lr = LDAP Request structure
821 * (IN) refs = array of pointers to referral strings that we will chase
822 * The array will be free'd by this function when no longer needed
823 * (IN) sref != 0 if following search reference
824 * (OUT) errstrp = Place to return a string of referrals which could not be followed
825 * (OUT) hadrefp = 1 if sucessfully followed referral
827 * Return value - number of referrals followed
830 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
833 int unfollowedcnt = 0;
834 LDAPRequest *origreq;
835 LDAPURLDesc *srv = NULL;
837 char **refarray = NULL;
839 int rc, count, i, j, id;
842 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
845 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
850 /* If no referrals in array, return */
851 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
856 /* Check for hop limit exceeded */
857 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
858 Debug( LDAP_DEBUG_ANY,
859 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
860 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
865 /* find original request */
867 origreq->lr_parent != NULL;
868 origreq = origreq->lr_parent )
876 if ( ld->ld_nextref_proc == NULL ) {
877 ld->ld_nextref_proc = ldap_int_nextref;
880 /* parse out & follow referrals */
882 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
884 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
887 /* Parse the referral URL */
888 rc = ldap_url_parse_ext( refarray[i], &srv );
889 if ( rc != LDAP_URL_SUCCESS ) {
890 /* ldap_url_parse_ext() returns LDAP_URL_* errors
891 * which do not map on API errors */
892 ld->ld_errno = LDAP_PARAM_ERROR;
897 if( srv->lud_crit_exts ) {
898 /* we do not support any extensions */
899 ld->ld_errno = LDAP_NOT_SUPPORTED;
904 /* treat ldap://hostpart and ldap://hostpart/ the same */
905 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
906 LDAP_FREE( srv->lud_dn );
910 /* check connection for re-bind in progress */
911 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
912 /* See if we've already requested this DN with this conn */
915 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
916 for ( lp = origreq; lp; ) {
917 if ( lp->lr_conn == lc
918 && len == lp->lr_dn.bv_len
920 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
925 if ( lp == origreq ) {
932 ldap_free_urllist( srv );
934 ld->ld_errno = LDAP_CLIENT_LOOP;
939 if ( lc->lconn_rebind_inprogress ) {
940 /* We are already chasing a referral or search reference and a
941 * bind on that connection is in progress. We must queue
942 * referrals on that connection, so we don't get a request
943 * going out before the bind operation completes. This happens
944 * if two search references come in one behind the other
945 * for the same server with different contexts.
947 Debug( LDAP_DEBUG_TRACE,
948 "ldap_chase_v3referrals: queue referral \"%s\"\n",
950 if( lc->lconn_rebind_queue == NULL ) {
951 /* Create a referral list */
952 lc->lconn_rebind_queue =
953 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
955 if( lc->lconn_rebind_queue == NULL) {
956 ld->ld_errno = LDAP_NO_MEMORY;
961 lc->lconn_rebind_queue[0] = refarray;
962 lc->lconn_rebind_queue[1] = NULL;
966 /* Count how many referral arrays we already have */
967 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
971 /* Add the new referral to the list */
972 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
973 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
975 if( lc->lconn_rebind_queue == NULL ) {
976 ld->ld_errno = LDAP_NO_MEMORY;
980 lc->lconn_rebind_queue[j] = refarray;
981 lc->lconn_rebind_queue[j+1] = NULL;
985 /* We have queued the referral/reference, now just return */
988 count = 1; /* Pretend we already followed referral */
992 /* Re-encode the request with the new starting point of the search.
993 * Note: In the future we also need to replace the filter if one
994 * was provided with the search reference
997 /* For references we don't want old dn if new dn empty */
998 if ( sref && srv->lud_dn == NULL ) {
999 srv->lud_dn = LDAP_STRDUP( "" );
1002 LDAP_NEXT_MSGID( ld, id );
1003 ber = re_encode_request( ld, origreq->lr_ber, id,
1004 sref, srv, &rinfo.ri_request );
1007 ld->ld_errno = LDAP_ENCODING_ERROR;
1012 Debug( LDAP_DEBUG_TRACE,
1013 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1014 lr->lr_msgid, refarray[i], 0);
1016 /* Send the new request to the server - may require a bind */
1017 rinfo.ri_msgid = origreq->lr_origid;
1018 rinfo.ri_url = refarray[i];
1019 #ifdef LDAP_R_COMPILE
1020 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1022 rc = ldap_send_server_request( ld, ber, id,
1023 origreq, srv, NULL, &rinfo );
1024 #ifdef LDAP_R_COMPILE
1025 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1028 /* Failure, try next referral in the list */
1029 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1030 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1031 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1032 ldap_free_urllist( srv );
1034 ld->ld_errno = LDAP_REFERRAL;
1036 /* Success, no need to try this referral list further */
1041 /* check if there is a queue of referrals that came in during bind */
1043 lc = find_connection( ld, srv, 1 );
1045 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1051 if ( lc->lconn_rebind_queue != NULL ) {
1052 /* Release resources of previous list */
1053 LDAP_VFREE( refarray );
1055 ldap_free_urllist( srv );
1058 /* Pull entries off end of queue so list always null terminated */
1059 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1061 refarray = lc->lconn_rebind_queue[j - 1];
1062 lc->lconn_rebind_queue[j-1] = NULL;
1063 /* we pulled off last entry from queue, free queue */
1065 LDAP_FREE( lc->lconn_rebind_queue );
1066 lc->lconn_rebind_queue = NULL;
1068 /* restart the loop the with new referral list */
1072 break; /* referral followed, break out of for loop */
1074 } /* end for loop */
1076 LDAP_VFREE( refarray );
1077 ldap_free_urllist( srv );
1078 LDAP_FREE( *errstrp );
1082 LDAP_FREE( unfollowed );
1085 *errstrp = unfollowed;
1091 * XXX merging of errors in this routine needs to be improved
1094 ldap_chase_referrals( LDAP *ld,
1102 char *p, *ref, *unfollowed;
1103 LDAPRequest *origreq;
1109 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1111 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1114 if ( *errstrp == NULL ) {
1118 len = strlen( *errstrp );
1119 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1120 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1122 p += LDAP_REF_STR_LEN;
1127 if ( len < LDAP_REF_STR_LEN ) {
1131 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1132 Debug( LDAP_DEBUG_ANY,
1133 "more than %d referral hops (dropping)\n",
1134 ld->ld_refhoplimit, 0, 0 );
1135 /* XXX report as error in ld->ld_errno? */
1139 /* find original request */
1140 for ( origreq = lr; origreq->lr_parent != NULL;
1141 origreq = origreq->lr_parent ) {
1148 /* parse out & follow referrals */
1149 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1150 p = strchr( ref, '\n' );
1155 rc = ldap_url_parse_ext( ref, &srv );
1156 if ( rc != LDAP_URL_SUCCESS ) {
1157 Debug( LDAP_DEBUG_TRACE,
1158 "ignoring %s referral <%s>\n",
1159 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1160 rc = ldap_append_referral( ld, &unfollowed, ref );
1165 if ( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1166 LDAP_FREE( srv->lud_dn );
1170 Debug( LDAP_DEBUG_TRACE,
1171 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1175 /* See if we've already been here */
1176 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1179 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1180 for (lp = lr; lp; lp = lp->lr_parent ) {
1181 if ( lp->lr_conn == lc
1182 && len == lp->lr_dn.bv_len
1184 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1191 ldap_free_urllist(srv);
1192 ld->ld_errno = LDAP_CLIENT_LOOP;
1198 LDAP_NEXT_MSGID( ld, id );
1199 ber = re_encode_request( ld, origreq->lr_ber,
1200 id, sref, srv, &rinfo.ri_request );
1202 if ( ber == NULL ) {
1206 /* copy the complete referral for rebind process */
1207 rinfo.ri_url = LDAP_STRDUP( ref );
1209 rinfo.ri_msgid = origreq->lr_origid;
1211 #ifdef LDAP_R_COMPILE
1212 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1214 rc = ldap_send_server_request( ld, ber, id,
1215 lr, srv, NULL, &rinfo );
1216 #ifdef LDAP_R_COMPILE
1217 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1220 LDAP_FREE( rinfo.ri_url );
1225 Debug( LDAP_DEBUG_ANY,
1226 "Unable to chase referral \"%s\" (%d: %s)\n",
1227 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1228 rc = ldap_append_referral( ld, &unfollowed, ref );
1231 ldap_free_urllist(srv);
1234 LDAP_FREE( *errstrp );
1235 *errstrp = unfollowed;
1237 return(( rc == 0 ) ? count : rc );
1242 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1246 if ( *referralsp == NULL ) {
1248 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1252 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1253 strlen( *referralsp ) + strlen( s ) + 2 );
1256 if ( *referralsp == NULL ) {
1257 ld->ld_errno = LDAP_NO_MEMORY;
1262 strcpy( *referralsp, LDAP_REF_STR );
1264 strcat( *referralsp, "\n" );
1266 strcat( *referralsp, s );
1274 re_encode_request( LDAP *ld,
1275 BerElement *origber,
1282 * XXX this routine knows way too much about how the lber library works!
1290 BerElement tmpber, *ber;
1291 struct berval orig_dn;
1294 Debug( LDAP_DEBUG_TRACE,
1295 "re_encode_request: new msgid %ld, new dn <%s>\n",
1297 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1302 * all LDAP requests are sequences that start with a message id.
1303 * For all except delete, this is followed by a sequence that is
1304 * tagged with the operation code. For delete, the provided DN
1305 * is not wrapped by a sequence.
1307 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1309 if ( rtag == LBER_ERROR ) {
1310 ld->ld_errno = LDAP_DECODING_ERROR;
1315 if ( tag == LDAP_REQ_BIND ) {
1316 /* bind requests have a version number before the DN & other stuff */
1317 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &orig_dn );
1319 } else if ( tag == LDAP_REQ_DELETE ) {
1320 /* delete requests don't have a DN wrapping sequence */
1321 rtag = ber_scanf( &tmpber, "m", &orig_dn );
1323 } else if ( tag == LDAP_REQ_SEARCH ) {
1324 /* search requests need to be re-scope-ed */
1325 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &orig_dn, &scope );
1327 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1328 /* use the scope provided in reference */
1329 scope = srv->lud_scope;
1331 } else if ( sref ) {
1332 /* use scope implied by previous operation
1335 * subtree -> subtree
1336 * subordinate -> subtree
1340 case LDAP_SCOPE_BASE:
1341 case LDAP_SCOPE_ONELEVEL:
1342 scope = LDAP_SCOPE_BASE;
1344 case LDAP_SCOPE_SUBTREE:
1345 case LDAP_SCOPE_SUBORDINATE:
1346 scope = LDAP_SCOPE_SUBTREE;
1352 rtag = ber_scanf( &tmpber, "{m" /*}*/, &orig_dn );
1355 if( rtag == LBER_ERROR ) {
1356 ld->ld_errno = LDAP_DECODING_ERROR;
1360 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1364 if ( srv->lud_dn == NULL ) {
1365 dn = orig_dn.bv_val;
1370 if ( tag == LDAP_REQ_BIND ) {
1371 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1372 } else if ( tag == LDAP_REQ_DELETE ) {
1373 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1374 } else if ( tag == LDAP_REQ_SEARCH ) {
1375 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1377 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1381 ld->ld_errno = LDAP_ENCODING_ERROR;
1386 if ( tag != LDAP_REQ_DELETE && (
1387 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1388 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1389 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1391 ld->ld_errno = LDAP_ENCODING_ERROR;
1397 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1398 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1400 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1402 #endif /* LDAP_DEBUG */
1404 *type = tag; /* return request type */
1410 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1414 #ifdef LDAP_R_COMPILE
1415 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1417 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1418 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1419 continue; /* Skip completed requests */
1421 if ( msgid == lr->lr_msgid ) {
1425 #ifdef LDAP_R_COMPILE
1426 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );