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 )
308 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
309 use_ldsb, connect, (bind != NULL) );
311 * make a new LDAP server connection
312 * XXX open connection synchronously for now
314 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
316 ld->ld_errno = LDAP_NO_MEMORY;
321 assert( ld->ld_sb != NULL );
322 lc->lconn_sb = ld->ld_sb;
325 lc->lconn_sb = ber_sockbuf_alloc();
326 if ( lc->lconn_sb == NULL ) {
327 LDAP_FREE( (char *)lc );
328 ld->ld_errno = LDAP_NO_MEMORY;
334 LDAPURLDesc **srvp, *srv = NULL;
336 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
337 if ( ldap_int_open_connection( ld, lc, *srvp, 0 ) != -1 )
341 if ( ld->ld_urllist_proc ) {
342 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
351 ber_sockbuf_free( lc->lconn_sb );
353 LDAP_FREE( (char *)lc );
354 ld->ld_errno = LDAP_SERVER_DOWN;
358 lc->lconn_server = ldap_url_dup( srv );
361 lc->lconn_status = LDAP_CONNST_CONNECTED;
362 #ifdef LDAP_R_COMPILE
363 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
365 lc->lconn_next = ld->ld_conns;
367 #ifdef LDAP_R_COMPILE
368 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
371 if ( bind != NULL ) {
373 LDAPConn *savedefconn;
375 /* Set flag to prevent additional referrals
376 * from being processed on this
377 * connection until the bind has completed
379 lc->lconn_rebind_inprogress = 1;
380 /* V3 rebind function */
381 if ( ld->ld_rebind_proc != NULL) {
382 LDAPURLDesc *srvfunc;
384 srvfunc = ldap_url_dup( *srvlist );
385 if ( srvfunc == NULL ) {
386 ld->ld_errno = LDAP_NO_MEMORY;
389 savedefconn = ld->ld_defconn;
390 ++lc->lconn_refcnt; /* avoid premature free */
393 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
394 #ifdef LDAP_R_COMPILE
395 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
396 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
398 err = (*ld->ld_rebind_proc)( ld,
399 bind->ri_url, bind->ri_request, bind->ri_msgid,
400 ld->ld_rebind_params );
401 #ifdef LDAP_R_COMPILE
402 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
403 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
406 ld->ld_defconn = savedefconn;
411 ldap_free_connection( ld, lc, 1, 0 );
414 ldap_free_urldesc( srvfunc );
418 struct berval passwd = BER_BVNULL;
420 savedefconn = ld->ld_defconn;
421 ++lc->lconn_refcnt; /* avoid premature free */
424 Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
425 #ifdef LDAP_R_COMPILE
426 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
427 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
429 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
430 NULL, NULL, &msgid );
431 if ( rc != LDAP_SUCCESS ) {
435 for ( err = 1; err > 0; ) {
436 struct timeval tv = { 0, 100000 };
437 LDAPMessage *res = NULL;
439 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
445 #ifdef LDAP_R_COMPILE
446 ldap_pvt_thread_yield();
451 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
452 if ( rc != LDAP_SUCCESS ) {
455 } else if ( err != LDAP_SUCCESS ) {
458 /* else err == LDAP_SUCCESS == 0 */
466 #ifdef LDAP_R_COMPILE
467 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
468 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
470 ld->ld_defconn = savedefconn;
474 ldap_free_connection( ld, lc, 1, 0 );
479 lc->lconn_rebind_inprogress = 0;
487 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
489 * return an existing connection (if any) to the server srv
490 * if "any" is non-zero, check for any server in the "srv" chain
494 LDAPURLDesc *lcu, *lsu;
495 int lcu_port, lsu_port;
498 #ifdef LDAP_R_COMPILE
499 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
501 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
502 lcu = lc->lconn_server;
503 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
506 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
507 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
510 if ( lsu_port == lcu_port
511 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
512 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
513 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
514 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
525 #ifdef LDAP_R_COMPILE
526 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
534 use_connection( LDAP *ld, LDAPConn *lc )
537 lc->lconn_lastused = time( NULL );
542 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
544 LDAPConn *tmplc, *prevlc;
546 Debug( LDAP_DEBUG_TRACE,
547 "ldap_free_connection %d %d\n",
550 if ( force || --lc->lconn_refcnt <= 0 ) {
551 /* remove from connections list first */
552 #ifdef LDAP_R_COMPILE
553 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
556 for ( prevlc = NULL, tmplc = ld->ld_conns;
558 tmplc = tmplc->lconn_next )
561 if ( prevlc == NULL ) {
562 ld->ld_conns = tmplc->lconn_next;
564 prevlc->lconn_next = tmplc->lconn_next;
570 #ifdef LDAP_R_COMPILE
571 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
574 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
575 ldap_mark_select_clear( ld, lc->lconn_sb );
577 ldap_send_unbind( ld, lc->lconn_sb,
582 if ( lc->lconn_ber != NULL ) {
583 ber_free( lc->lconn_ber, 1 );
586 ldap_int_sasl_close( ld, lc );
588 ldap_free_urllist( lc->lconn_server );
589 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
590 if ( lc->lconn_krbinstance != NULL ) {
591 LDAP_FREE( lc->lconn_krbinstance );
595 /* FIXME: is this at all possible? */
599 for ( lr = ld->ld_requests; lr; ) {
600 LDAPRequest *lr_next = lr->lr_next;
602 if ( lr->lr_conn == lc ) {
603 ldap_free_request_int( ld, lr );
610 if ( lc->lconn_sb != ld->ld_sb ) {
611 ber_sockbuf_free( lc->lconn_sb );
614 if ( lc->lconn_rebind_queue != NULL) {
616 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
617 LDAP_VFREE( lc->lconn_rebind_queue[i] );
619 LDAP_FREE( lc->lconn_rebind_queue );
624 Debug( LDAP_DEBUG_TRACE,
625 "ldap_free_connection: actually freed\n",
629 lc->lconn_lastused = time( NULL );
630 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
631 lc->lconn_refcnt, 0, 0 );
638 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
643 fprintf( stderr, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "" );
644 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
645 if ( lc->lconn_server != NULL ) {
646 fprintf( stderr, "* host: %s port: %d%s\n",
647 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
648 : lc->lconn_server->lud_host,
649 lc->lconn_server->lud_port, ( lc->lconn_sb ==
650 ld->ld_sb ) ? " (default)" : "" );
652 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
653 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
654 "NeedSocket" : ( lc->lconn_status ==
655 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
656 fprintf( stderr, " last used: %s",
657 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
658 if( lc->lconn_rebind_inprogress ) {
659 fprintf( stderr, " rebind in progress\n");
660 if( lc->lconn_rebind_queue != NULL) {
662 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
664 for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
665 fprintf( stderr, " queue %d entry %d - %s\n",
666 i, j, lc->lconn_rebind_queue[i][j]);
670 fprintf( stderr, " queue is empty\n");
673 fprintf(stderr, "\n");
682 ldap_dump_requests_and_responses( LDAP *ld )
687 #ifdef LDAP_R_COMPILE
688 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
690 fprintf( stderr, "** ld %p Outstanding Requests:\n", (void *)ld );
691 if (( lr = ld->ld_requests ) == NULL ) {
692 fprintf( stderr, " Empty\n" );
694 for ( ; lr != NULL; lr = lr->lr_next ) {
695 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
696 lr->lr_msgid, lr->lr_origid,
697 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
698 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
699 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
700 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
701 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
703 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
704 lr->lr_outrefcnt, lr->lr_parentcnt );
706 #ifdef LDAP_R_COMPILE
707 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
709 fprintf( stderr, "** ld %p Response Queue:\n", (void *)ld );
710 if (( lm = ld->ld_responses ) == NULL ) {
711 fprintf( stderr, " Empty\n" );
713 for ( ; lm != NULL; lm = lm->lm_next ) {
714 fprintf( stderr, " * msgid %d, type %lu\n",
715 lm->lm_msgid, (unsigned long) lm->lm_msgtype );
716 if (( l = lm->lm_chain ) != NULL ) {
717 fprintf( stderr, " chained responses:\n" );
718 for ( ; l != NULL; l = l->lm_chain ) {
720 " * msgid %d, type %lu\n",
722 (unsigned long) l->lm_msgtype );
727 #endif /* LDAP_DEBUG */
730 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
732 if ( lr->lr_prev == NULL ) {
733 /* free'ing the first request? */
734 assert( ld->ld_requests == lr );
735 ld->ld_requests = lr->lr_next;
738 lr->lr_prev->lr_next = lr->lr_next;
741 if ( lr->lr_next != NULL ) {
742 lr->lr_next->lr_prev = lr->lr_prev;
745 if ( lr->lr_ber != NULL ) {
746 ber_free( lr->lr_ber, 1 );
750 if ( lr->lr_res_error != NULL ) {
751 LDAP_FREE( lr->lr_res_error );
752 lr->lr_res_error = NULL;
755 if ( lr->lr_res_matched != NULL ) {
756 LDAP_FREE( lr->lr_res_matched );
757 lr->lr_res_matched = NULL;
764 ldap_free_request( LDAP *ld, LDAPRequest *lr )
766 LDAPRequest **ttmplr;
767 #ifdef LDAP_R_COMPILE
768 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
771 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
772 lr->lr_origid, lr->lr_msgid, 0 );
774 /* free all referrals (child requests) */
775 while ( lr->lr_child )
776 ldap_free_request( ld, lr->lr_child );
778 if ( lr->lr_parent != NULL ) {
779 --lr->lr_parent->lr_outrefcnt;
780 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext );
782 *ttmplr = lr->lr_refnext;
784 ldap_free_request_int( ld, lr );
788 * call first time with *cntp = -1
789 * when returns *cntp == -1, no referrals are left
791 * NOTE: may replace *refsp, or shuffle the contents
792 * of the original array.
794 static int ldap_int_nextref(
800 assert( refsp != NULL );
801 assert( *refsp != NULL );
802 assert( cntp != NULL );
811 if ( (*refsp)[ *cntp ] == NULL ) {
822 * (IN) ld = LDAP connection handle
823 * (IN) lr = LDAP Request structure
824 * (IN) refs = array of pointers to referral strings that we will chase
825 * The array will be free'd by this function when no longer needed
826 * (IN) sref != 0 if following search reference
827 * (OUT) errstrp = Place to return a string of referrals which could not be followed
828 * (OUT) hadrefp = 1 if sucessfully followed referral
830 * Return value - number of referrals followed
833 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
836 int unfollowedcnt = 0;
837 LDAPRequest *origreq;
838 LDAPURLDesc *srv = NULL;
840 char **refarray = NULL;
842 int rc, count, i, j, id;
845 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
848 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
853 /* If no referrals in array, return */
854 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
859 /* Check for hop limit exceeded */
860 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
861 Debug( LDAP_DEBUG_ANY,
862 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
863 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
868 /* find original request */
870 origreq->lr_parent != NULL;
871 origreq = origreq->lr_parent )
879 if ( ld->ld_nextref_proc == NULL ) {
880 ld->ld_nextref_proc = ldap_int_nextref;
883 /* parse out & follow referrals */
885 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
887 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
890 /* Parse the referral URL */
891 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
892 if ( rc != LDAP_URL_SUCCESS ) {
893 /* ldap_url_parse_ext() returns LDAP_URL_* errors
894 * which do not map on API errors */
895 ld->ld_errno = LDAP_PARAM_ERROR;
900 if( srv->lud_crit_exts ) {
901 /* we do not support any extensions */
902 ld->ld_errno = LDAP_NOT_SUPPORTED;
907 /* check connection for re-bind in progress */
908 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
909 /* See if we've already requested this DN with this conn */
912 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
913 for ( lp = origreq; lp; ) {
914 if ( lp->lr_conn == lc
915 && len == lp->lr_dn.bv_len
917 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
922 if ( lp == origreq ) {
929 ldap_free_urllist( srv );
931 ld->ld_errno = LDAP_CLIENT_LOOP;
936 if ( lc->lconn_rebind_inprogress ) {
937 /* We are already chasing a referral or search reference and a
938 * bind on that connection is in progress. We must queue
939 * referrals on that connection, so we don't get a request
940 * going out before the bind operation completes. This happens
941 * if two search references come in one behind the other
942 * for the same server with different contexts.
944 Debug( LDAP_DEBUG_TRACE,
945 "ldap_chase_v3referrals: queue referral \"%s\"\n",
947 if( lc->lconn_rebind_queue == NULL ) {
948 /* Create a referral list */
949 lc->lconn_rebind_queue =
950 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
952 if( lc->lconn_rebind_queue == NULL) {
953 ld->ld_errno = LDAP_NO_MEMORY;
958 lc->lconn_rebind_queue[0] = refarray;
959 lc->lconn_rebind_queue[1] = NULL;
963 /* Count how many referral arrays we already have */
964 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
968 /* Add the new referral to the list */
969 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
970 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
972 if( lc->lconn_rebind_queue == NULL ) {
973 ld->ld_errno = LDAP_NO_MEMORY;
977 lc->lconn_rebind_queue[j] = refarray;
978 lc->lconn_rebind_queue[j+1] = NULL;
982 /* We have queued the referral/reference, now just return */
985 count = 1; /* Pretend we already followed referral */
989 /* Re-encode the request with the new starting point of the search.
990 * Note: In the future we also need to replace the filter if one
991 * was provided with the search reference
994 /* For references we don't want old dn if new dn empty */
995 if ( sref && srv->lud_dn == NULL ) {
996 srv->lud_dn = LDAP_STRDUP( "" );
999 LDAP_NEXT_MSGID( ld, id );
1000 ber = re_encode_request( ld, origreq->lr_ber, id,
1001 sref, srv, &rinfo.ri_request );
1004 ld->ld_errno = LDAP_ENCODING_ERROR;
1009 Debug( LDAP_DEBUG_TRACE,
1010 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1011 lr->lr_msgid, refarray[i], 0);
1013 /* Send the new request to the server - may require a bind */
1014 rinfo.ri_msgid = origreq->lr_origid;
1015 rinfo.ri_url = refarray[i];
1016 #ifdef LDAP_R_COMPILE
1017 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1019 rc = ldap_send_server_request( ld, ber, id,
1020 origreq, &srv, NULL, &rinfo );
1021 #ifdef LDAP_R_COMPILE
1022 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1025 /* Failure, try next referral in the list */
1026 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1027 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1028 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1029 ldap_free_urllist( srv );
1031 ld->ld_errno = LDAP_REFERRAL;
1033 /* Success, no need to try this referral list further */
1038 /* check if there is a queue of referrals that came in during bind */
1040 lc = find_connection( ld, srv, 1 );
1042 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1048 if ( lc->lconn_rebind_queue != NULL ) {
1049 /* Release resources of previous list */
1050 LDAP_VFREE( refarray );
1052 ldap_free_urllist( srv );
1055 /* Pull entries off end of queue so list always null terminated */
1056 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1058 refarray = lc->lconn_rebind_queue[j - 1];
1059 lc->lconn_rebind_queue[j-1] = NULL;
1060 /* we pulled off last entry from queue, free queue */
1062 LDAP_FREE( lc->lconn_rebind_queue );
1063 lc->lconn_rebind_queue = NULL;
1065 /* restart the loop the with new referral list */
1069 break; /* referral followed, break out of for loop */
1071 } /* end for loop */
1073 LDAP_VFREE( refarray );
1074 ldap_free_urllist( srv );
1075 LDAP_FREE( *errstrp );
1079 LDAP_FREE( unfollowed );
1082 *errstrp = unfollowed;
1088 * XXX merging of errors in this routine needs to be improved
1091 ldap_chase_referrals( LDAP *ld,
1099 char *p, *ref, *unfollowed;
1100 LDAPRequest *origreq;
1106 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1108 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1111 if ( *errstrp == NULL ) {
1115 len = strlen( *errstrp );
1116 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1117 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1119 p += LDAP_REF_STR_LEN;
1124 if ( len < LDAP_REF_STR_LEN ) {
1128 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1129 Debug( LDAP_DEBUG_ANY,
1130 "more than %d referral hops (dropping)\n",
1131 ld->ld_refhoplimit, 0, 0 );
1132 /* XXX report as error in ld->ld_errno? */
1136 /* find original request */
1137 for ( origreq = lr; origreq->lr_parent != NULL;
1138 origreq = origreq->lr_parent ) {
1145 /* parse out & follow referrals */
1146 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1147 p = strchr( ref, '\n' );
1152 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1153 if ( rc != LDAP_URL_SUCCESS ) {
1154 Debug( LDAP_DEBUG_TRACE,
1155 "ignoring %s referral <%s>\n",
1156 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1157 rc = ldap_append_referral( ld, &unfollowed, ref );
1162 Debug( LDAP_DEBUG_TRACE,
1163 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1167 /* See if we've already been here */
1168 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1171 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1172 for (lp = lr; lp; lp = lp->lr_parent ) {
1173 if ( lp->lr_conn == lc
1174 && len == lp->lr_dn.bv_len
1176 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1183 ldap_free_urllist(srv);
1184 ld->ld_errno = LDAP_CLIENT_LOOP;
1190 LDAP_NEXT_MSGID( ld, id );
1191 ber = re_encode_request( ld, origreq->lr_ber,
1192 id, sref, srv, &rinfo.ri_request );
1194 if ( ber == NULL ) {
1198 /* copy the complete referral for rebind process */
1199 rinfo.ri_url = LDAP_STRDUP( ref );
1201 rinfo.ri_msgid = origreq->lr_origid;
1203 #ifdef LDAP_R_COMPILE
1204 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1206 rc = ldap_send_server_request( ld, ber, id,
1207 lr, &srv, NULL, &rinfo );
1208 #ifdef LDAP_R_COMPILE
1209 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1212 LDAP_FREE( rinfo.ri_url );
1217 Debug( LDAP_DEBUG_ANY,
1218 "Unable to chase referral \"%s\" (%d: %s)\n",
1219 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1220 rc = ldap_append_referral( ld, &unfollowed, ref );
1223 ldap_free_urllist(srv);
1226 LDAP_FREE( *errstrp );
1227 *errstrp = unfollowed;
1229 return(( rc == 0 ) ? count : rc );
1234 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1238 if ( *referralsp == NULL ) {
1240 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1244 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1245 strlen( *referralsp ) + strlen( s ) + 2 );
1248 if ( *referralsp == NULL ) {
1249 ld->ld_errno = LDAP_NO_MEMORY;
1254 strcpy( *referralsp, LDAP_REF_STR );
1256 strcat( *referralsp, "\n" );
1258 strcat( *referralsp, s );
1266 re_encode_request( LDAP *ld,
1267 BerElement *origber,
1274 * XXX this routine knows way too much about how the lber library works!
1282 BerElement tmpber, *ber;
1285 Debug( LDAP_DEBUG_TRACE,
1286 "re_encode_request: new msgid %ld, new dn <%s>\n",
1288 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1293 * all LDAP requests are sequences that start with a message id.
1294 * For all except delete, this is followed by a sequence that is
1295 * tagged with the operation code. For delete, the provided DN
1296 * is not wrapped by a sequence.
1298 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1300 if ( rtag == LBER_ERROR ) {
1301 ld->ld_errno = LDAP_DECODING_ERROR;
1306 if ( tag == LDAP_REQ_BIND ) {
1307 /* bind requests have a version number before the DN & other stuff */
1308 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1310 } else if ( tag == LDAP_REQ_DELETE ) {
1311 /* delete requests don't have a DN wrapping sequence */
1312 rtag = ber_scanf( &tmpber, "m", &dn );
1314 } else if ( tag == LDAP_REQ_SEARCH ) {
1315 /* search requests need to be re-scope-ed */
1316 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1318 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1319 /* use the scope provided in reference */
1320 scope = srv->lud_scope;
1322 } else if ( sref ) {
1323 /* use scope implied by previous operation
1326 * subtree -> subtree
1327 * subordinate -> subtree
1331 case LDAP_SCOPE_BASE:
1332 case LDAP_SCOPE_ONELEVEL:
1333 scope = LDAP_SCOPE_BASE;
1335 case LDAP_SCOPE_SUBTREE:
1336 case LDAP_SCOPE_SUBORDINATE:
1337 scope = LDAP_SCOPE_SUBTREE;
1343 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1346 if( rtag == LBER_ERROR ) {
1347 ld->ld_errno = LDAP_DECODING_ERROR;
1351 /* restore character zero'd out by ber_scanf*/
1352 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1354 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1358 if ( srv->lud_dn ) {
1359 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1362 if ( tag == LDAP_REQ_BIND ) {
1363 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1364 } else if ( tag == LDAP_REQ_DELETE ) {
1365 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1366 } else if ( tag == LDAP_REQ_SEARCH ) {
1367 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1369 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1373 ld->ld_errno = LDAP_ENCODING_ERROR;
1378 if ( tag != LDAP_REQ_DELETE && (
1379 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1380 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1381 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1383 ld->ld_errno = LDAP_ENCODING_ERROR;
1389 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1390 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1392 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1394 #endif /* LDAP_DEBUG */
1396 *type = tag; /* return request type */
1402 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1406 #ifdef LDAP_R_COMPILE
1407 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1409 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1410 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1411 continue; /* Skip completed requests */
1413 if ( msgid == lr->lr_msgid ) {
1417 #ifdef LDAP_R_COMPILE
1418 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );