2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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 ber = ber_alloc_t( ld->ld_lberoptions );
75 ld->ld_errno = LDAP_NO_MEMORY;
83 ldap_set_ber_options( LDAP *ld, BerElement *ber )
85 ber->ber_options = ld->ld_lberoptions;
90 ldap_send_initial_request(
99 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
101 #ifdef LDAP_R_COMPILE
102 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
104 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
105 /* not connected yet */
106 rc = ldap_open_defconn( ld );
109 #ifdef LDAP_R_COMPILE
110 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
115 } else if ( rc == 0 ) {
116 Debug( LDAP_DEBUG_TRACE,
117 "ldap_open_defconn: successful\n",
121 #ifdef LDAP_CONNECTIONLESS
122 if (LDAP_IS_UDP(ld)) {
123 if (msgtype == LDAP_REQ_BIND) {
124 if (ld->ld_options.ldo_cldapdn)
125 ldap_memfree(ld->ld_options.ldo_cldapdn);
126 ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
129 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
130 return LDAP_PARAM_ERROR;
133 #ifdef LDAP_R_COMPILE
134 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
136 rc = ldap_send_server_request( ld, ber, msgid, NULL,
138 #ifdef LDAP_R_COMPILE
139 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
146 ldap_int_flush_request(
150 LDAPConn *lc = lr->lr_conn;
152 if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
153 if ( sock_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,
191 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
194 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
197 if ( srvlist == NULL ) {
200 lc = find_connection( ld, *srvlist, 1 );
202 if ( (bind != NULL) && (parentreq != NULL) ) {
203 /* Remember the bind in the parent */
205 ++parentreq->lr_outrefcnt;
207 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
212 /* async connect... */
213 if ( lc != NULL && lc->lconn_status == LDAP_CONNST_CONNECTING ) {
214 ber_socket_t sd = AC_SOCKET_ERROR;
215 struct timeval tv = { 0 };
217 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
220 switch ( ldap_int_poll( ld, sd, &tv ) ) {
223 lc->lconn_status = LDAP_CONNST_CONNECTED;
227 /* async only occurs if a network timeout is set */
229 /* honor network timeout */
230 if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
232 /* caller will have to call again */
233 ld->ld_errno = LDAP_X_CONNECTING;
243 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
244 if ( ld->ld_errno == LDAP_SUCCESS ) {
245 ld->ld_errno = LDAP_SERVER_DOWN;
250 /* Forget about the bind */
251 --parentreq->lr_outrefcnt;
256 use_connection( ld, lc );
258 #ifdef LDAP_CONNECTIONLESS
259 if ( LDAP_IS_UDP( ld )) {
260 BerElement tmpber = *ber;
261 ber_rewind( &tmpber );
262 rc = ber_write( &tmpber, ld->ld_options.ldo_peer,
263 sizeof( struct sockaddr ), 0 );
265 ld->ld_errno = LDAP_ENCODING_ERROR;
271 /* If we still have an incomplete write, try to finish it before
272 * dealing with the new request. If we don't finish here, return
273 * LDAP_BUSY and let the caller retry later. We only allow a single
274 * request to be in WRITING state.
277 if ( ld->ld_requests &&
278 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
279 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
285 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
287 ld->ld_errno = LDAP_NO_MEMORY;
288 ldap_free_connection( ld, lc, 0, 0 );
291 /* Forget about the bind */
292 --parentreq->lr_outrefcnt;
296 lr->lr_msgid = msgid;
297 lr->lr_status = LDAP_REQST_INPROGRESS;
298 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
301 if ( parentreq != NULL ) { /* sub-request */
303 /* Increment if we didn't do it before the bind */
304 ++parentreq->lr_outrefcnt;
306 lr->lr_origid = parentreq->lr_origid;
307 lr->lr_parentcnt = ++parentreq->lr_parentcnt;
308 lr->lr_parent = parentreq;
309 lr->lr_refnext = parentreq->lr_child;
310 parentreq->lr_child = lr;
311 } else { /* original request */
312 lr->lr_origid = lr->lr_msgid;
315 /* Extract requestDN for future reference */
317 BerElement tmpber = *ber;
321 ber_reset( &tmpber, 1 );
322 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
325 rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
327 case LDAP_REQ_DELETE:
330 rtag = ber_scanf( &tmpber, "{" /*}*/ );
331 case LDAP_REQ_ABANDON:
334 if ( tag != LDAP_REQ_ABANDON ) {
335 ber_skip_tag( &tmpber, &lr->lr_dn.bv_len );
336 lr->lr_dn.bv_val = tmpber.ber_ptr;
341 lr->lr_next = ld->ld_requests;
342 if ( lr->lr_next != NULL ) {
343 lr->lr_next->lr_prev = lr;
345 ld->ld_requests = lr;
347 ld->ld_errno = LDAP_SUCCESS;
348 if ( ldap_int_flush_request( ld, lr ) == -1 ) {
356 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
357 int connect, LDAPreqinfo *bind )
362 Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
363 use_ldsb, connect, (bind != NULL) );
365 * make a new LDAP server connection
366 * XXX open connection synchronously for now
368 lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
370 ld->ld_errno = LDAP_NO_MEMORY;
375 assert( ld->ld_sb != NULL );
376 lc->lconn_sb = ld->ld_sb;
379 lc->lconn_sb = ber_sockbuf_alloc();
380 if ( lc->lconn_sb == NULL ) {
381 LDAP_FREE( (char *)lc );
382 ld->ld_errno = LDAP_NO_MEMORY;
388 LDAPURLDesc **srvp, *srv = NULL;
390 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
392 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
395 rc = ldap_int_open_connection( ld, lc, *srvp, async );
399 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
400 ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
409 ber_sockbuf_free( lc->lconn_sb );
411 LDAP_FREE( (char *)lc );
412 ld->ld_errno = LDAP_SERVER_DOWN;
416 lc->lconn_server = ldap_url_dup( srv );
419 lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
420 #ifdef LDAP_R_COMPILE
421 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
423 lc->lconn_next = ld->ld_conns;
425 #ifdef LDAP_R_COMPILE
426 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
429 if ( bind != NULL ) {
431 LDAPConn *savedefconn;
433 /* Set flag to prevent additional referrals
434 * from being processed on this
435 * connection until the bind has completed
437 lc->lconn_rebind_inprogress = 1;
438 /* V3 rebind function */
439 if ( ld->ld_rebind_proc != NULL) {
440 LDAPURLDesc *srvfunc;
442 srvfunc = ldap_url_dup( *srvlist );
443 if ( srvfunc == NULL ) {
444 ld->ld_errno = LDAP_NO_MEMORY;
447 savedefconn = ld->ld_defconn;
448 ++lc->lconn_refcnt; /* avoid premature free */
451 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
452 #ifdef LDAP_R_COMPILE
453 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
454 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
456 err = (*ld->ld_rebind_proc)( ld,
457 bind->ri_url, bind->ri_request, bind->ri_msgid,
458 ld->ld_rebind_params );
459 #ifdef LDAP_R_COMPILE
460 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
461 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
464 ld->ld_defconn = savedefconn;
469 ldap_free_connection( ld, lc, 1, 0 );
472 ldap_free_urldesc( srvfunc );
477 struct berval passwd = BER_BVNULL;
479 savedefconn = ld->ld_defconn;
480 ++lc->lconn_refcnt; /* avoid premature free */
483 Debug( LDAP_DEBUG_TRACE,
484 "anonymous rebind via ldap_sasl_bind(\"\")\n",
487 #ifdef LDAP_R_COMPILE
488 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
489 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
491 rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
492 NULL, NULL, &msgid );
493 if ( rc != LDAP_SUCCESS ) {
497 for ( err = 1; err > 0; ) {
498 struct timeval tv = { 0, 100000 };
499 LDAPMessage *res = NULL;
501 switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
507 #ifdef LDAP_R_COMPILE
508 ldap_pvt_thread_yield();
513 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
514 if ( rc != LDAP_SUCCESS ) {
517 } else if ( err != LDAP_SUCCESS ) {
520 /* else err == LDAP_SUCCESS == 0 */
524 Debug( LDAP_DEBUG_TRACE,
525 "ldap_new_connection %p: "
526 "unexpected response %d "
527 "from BIND request id=%d\n",
528 (void *) ld, ldap_msgtype( res ), msgid );
534 #ifdef LDAP_R_COMPILE
535 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
536 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
538 ld->ld_defconn = savedefconn;
542 ldap_free_connection( ld, lc, 1, 0 );
547 lc->lconn_rebind_inprogress = 0;
555 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
557 * return an existing connection (if any) to the server srv
558 * if "any" is non-zero, check for any server in the "srv" chain
562 LDAPURLDesc *lcu, *lsu;
563 int lcu_port, lsu_port;
566 #ifdef LDAP_R_COMPILE
567 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
569 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
570 lcu = lc->lconn_server;
571 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
574 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
575 lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
578 if ( lsu_port == lcu_port
579 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
580 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
581 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
582 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
593 #ifdef LDAP_R_COMPILE
594 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
602 use_connection( LDAP *ld, LDAPConn *lc )
605 lc->lconn_lastused = time( NULL );
610 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
612 LDAPConn *tmplc, *prevlc;
614 Debug( LDAP_DEBUG_TRACE,
615 "ldap_free_connection %d %d\n",
618 if ( force || --lc->lconn_refcnt <= 0 ) {
619 /* remove from connections list first */
620 #ifdef LDAP_R_COMPILE
621 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
624 for ( prevlc = NULL, tmplc = ld->ld_conns;
626 tmplc = tmplc->lconn_next )
629 if ( prevlc == NULL ) {
630 ld->ld_conns = tmplc->lconn_next;
632 prevlc->lconn_next = tmplc->lconn_next;
634 if ( ld->ld_defconn == lc ) {
635 ld->ld_defconn = NULL;
641 #ifdef LDAP_R_COMPILE
642 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
645 /* process connection callbacks */
647 struct ldapoptions *lo;
651 lo = &ld->ld_options;
652 if ( lo->ldo_conn_cbs ) {
653 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
655 cb->lc_del( ld, lc->lconn_sb, cb );
658 lo = LDAP_INT_GLOBAL_OPT();
659 if ( lo->ldo_conn_cbs ) {
660 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
662 cb->lc_del( ld, lc->lconn_sb, cb );
667 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
668 ldap_mark_select_clear( ld, lc->lconn_sb );
670 ldap_send_unbind( ld, lc->lconn_sb,
675 if ( lc->lconn_ber != NULL ) {
676 ber_free( lc->lconn_ber, 1 );
679 ldap_int_sasl_close( ld, lc );
681 ldap_free_urllist( lc->lconn_server );
683 /* FIXME: is this at all possible?
684 * ldap_ld_free() in unbind.c calls ldap_free_connection()
685 * with force == 1 __after__ explicitly calling
686 * ldap_free_request() on all requests */
690 for ( lr = ld->ld_requests; lr; ) {
691 LDAPRequest *lr_next = lr->lr_next;
693 if ( lr->lr_conn == lc ) {
694 ldap_free_request_int( ld, lr );
701 if ( lc->lconn_sb != ld->ld_sb ) {
702 ber_sockbuf_free( lc->lconn_sb );
704 ber_int_sb_close( lc->lconn_sb );
707 if ( lc->lconn_rebind_queue != NULL) {
709 for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
710 LDAP_VFREE( lc->lconn_rebind_queue[i] );
712 LDAP_FREE( lc->lconn_rebind_queue );
717 Debug( LDAP_DEBUG_TRACE,
718 "ldap_free_connection: actually freed\n",
722 lc->lconn_lastused = time( NULL );
723 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
724 lc->lconn_refcnt, 0, 0 );
731 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
736 Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
737 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
738 if ( lc->lconn_server != NULL ) {
739 Debug( LDAP_DEBUG_TRACE, "* host: %s port: %d%s\n",
740 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
741 : lc->lconn_server->lud_host,
742 lc->lconn_server->lud_port, ( lc->lconn_sb ==
743 ld->ld_sb ) ? " (default)" : "" );
745 Debug( LDAP_DEBUG_TRACE, " refcnt: %d status: %s\n", lc->lconn_refcnt,
746 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
748 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
749 ? "Connecting" : "Connected", 0 );
750 Debug( LDAP_DEBUG_TRACE, " last used: %s%s\n",
751 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
752 lc->lconn_rebind_inprogress ? " rebind in progress" : "", 0 );
753 if ( lc->lconn_rebind_inprogress ) {
754 if ( lc->lconn_rebind_queue != NULL) {
757 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
759 for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
760 Debug( LDAP_DEBUG_TRACE, " queue %d entry %d - %s\n",
761 i, j, lc->lconn_rebind_queue[i][j] );
765 Debug( LDAP_DEBUG_TRACE, " queue is empty\n", 0, 0, 0 );
768 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
777 ldap_dump_requests_and_responses( LDAP *ld )
783 Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
785 lr = ld->ld_requests;
787 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
789 for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
790 Debug( LDAP_DEBUG_TRACE, " * msgid %d, origid %d, status %s\n",
791 lr->lr_msgid, lr->lr_origid,
792 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
793 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
794 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
795 ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
796 ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
798 Debug( LDAP_DEBUG_TRACE, " outstanding referrals %d, parent count %d\n",
799 lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
801 Debug( LDAP_DEBUG_TRACE, " ld %p request count %d (abandoned %lu)\n",
802 (void *)ld, i, ld->ld_nabandoned );
803 Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
804 if ( ( lm = ld->ld_responses ) == NULL ) {
805 Debug( LDAP_DEBUG_TRACE, " Empty\n", 0, 0, 0 );
807 for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
808 Debug( LDAP_DEBUG_TRACE, " * msgid %d, type %lu\n",
809 lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
810 if ( lm->lm_chain != NULL ) {
811 Debug( LDAP_DEBUG_TRACE, " chained responses:\n", 0, 0, 0 );
812 for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
813 Debug( LDAP_DEBUG_TRACE,
814 " * msgid %d, type %lu\n",
816 (unsigned long)l->lm_msgtype, 0 );
820 Debug( LDAP_DEBUG_TRACE, " ld %p response count %d\n", (void *)ld, i, 0 );
822 #endif /* LDAP_DEBUG */
825 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
827 /* if lr_refcnt > 0, the request has been looked up
828 * by ldap_find_request_by_msgid(); if in the meanwhile
829 * the request is free()'d by someone else, just decrease
830 * the reference count and extract it from the request
831 * list; later on, it will be freed. */
832 if ( lr->lr_prev == NULL ) {
833 if ( lr->lr_refcnt == 0 ) {
834 /* free'ing the first request? */
835 assert( ld->ld_requests == lr );
838 if ( ld->ld_requests == lr ) {
839 ld->ld_requests = lr->lr_next;
843 lr->lr_prev->lr_next = lr->lr_next;
846 if ( lr->lr_next != NULL ) {
847 lr->lr_next->lr_prev = lr->lr_prev;
850 if ( lr->lr_refcnt > 0 ) {
851 lr->lr_refcnt = -lr->lr_refcnt;
859 if ( lr->lr_ber != NULL ) {
860 ber_free( lr->lr_ber, 1 );
864 if ( lr->lr_res_error != NULL ) {
865 LDAP_FREE( lr->lr_res_error );
866 lr->lr_res_error = NULL;
869 if ( lr->lr_res_matched != NULL ) {
870 LDAP_FREE( lr->lr_res_matched );
871 lr->lr_res_matched = NULL;
878 ldap_free_request( LDAP *ld, LDAPRequest *lr )
880 #ifdef LDAP_R_COMPILE
881 LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
884 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
885 lr->lr_origid, lr->lr_msgid, 0 );
887 /* free all referrals (child requests) */
888 while ( lr->lr_child ) {
889 ldap_free_request( ld, lr->lr_child );
892 if ( lr->lr_parent != NULL ) {
895 --lr->lr_parent->lr_outrefcnt;
896 for ( lrp = &lr->lr_parent->lr_child;
898 lrp = &(*lrp)->lr_refnext );
901 *lrp = lr->lr_refnext;
904 ldap_free_request_int( ld, lr );
908 * call first time with *cntp = -1
909 * when returns *cntp == -1, no referrals are left
911 * NOTE: may replace *refsp, or shuffle the contents
912 * of the original array.
914 static int ldap_int_nextref(
920 assert( refsp != NULL );
921 assert( *refsp != NULL );
922 assert( cntp != NULL );
931 if ( (*refsp)[ *cntp ] == NULL ) {
942 * (IN) ld = LDAP connection handle
943 * (IN) lr = LDAP Request structure
944 * (IN) refs = array of pointers to referral strings that we will chase
945 * The array will be free'd by this function when no longer needed
946 * (IN) sref != 0 if following search reference
947 * (OUT) errstrp = Place to return a string of referrals which could not be followed
948 * (OUT) hadrefp = 1 if sucessfully followed referral
950 * Return value - number of referrals followed
953 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
956 int unfollowedcnt = 0;
957 LDAPRequest *origreq;
958 LDAPURLDesc *srv = NULL;
960 char **refarray = NULL;
962 int rc, count, i, j, id;
965 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
968 Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
973 /* If no referrals in array, return */
974 if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
979 /* Check for hop limit exceeded */
980 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
981 Debug( LDAP_DEBUG_ANY,
982 "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
983 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
988 /* find original request */
990 origreq->lr_parent != NULL;
991 origreq = origreq->lr_parent )
999 if ( ld->ld_nextref_proc == NULL ) {
1000 ld->ld_nextref_proc = ldap_int_nextref;
1003 /* parse out & follow referrals */
1005 for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1007 ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1010 /* Parse the referral URL */
1011 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1012 if ( rc != LDAP_URL_SUCCESS ) {
1013 /* ldap_url_parse_ext() returns LDAP_URL_* errors
1014 * which do not map on API errors */
1015 ld->ld_errno = LDAP_PARAM_ERROR;
1020 if( srv->lud_crit_exts ) {
1021 /* we do not support any extensions */
1022 ld->ld_errno = LDAP_NOT_SUPPORTED;
1027 /* check connection for re-bind in progress */
1028 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1029 /* See if we've already requested this DN with this conn */
1032 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1033 for ( lp = origreq; lp; ) {
1034 if ( lp->lr_conn == lc
1035 && len == lp->lr_dn.bv_len
1037 && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1042 if ( lp == origreq ) {
1045 lp = lp->lr_refnext;
1049 ldap_free_urllist( srv );
1051 ld->ld_errno = LDAP_CLIENT_LOOP;
1056 if ( lc->lconn_rebind_inprogress ) {
1057 /* We are already chasing a referral or search reference and a
1058 * bind on that connection is in progress. We must queue
1059 * referrals on that connection, so we don't get a request
1060 * going out before the bind operation completes. This happens
1061 * if two search references come in one behind the other
1062 * for the same server with different contexts.
1064 Debug( LDAP_DEBUG_TRACE,
1065 "ldap_chase_v3referrals: queue referral \"%s\"\n",
1067 if( lc->lconn_rebind_queue == NULL ) {
1068 /* Create a referral list */
1069 lc->lconn_rebind_queue =
1070 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1072 if( lc->lconn_rebind_queue == NULL) {
1073 ld->ld_errno = LDAP_NO_MEMORY;
1078 lc->lconn_rebind_queue[0] = refarray;
1079 lc->lconn_rebind_queue[1] = NULL;
1083 /* Count how many referral arrays we already have */
1084 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1088 /* Add the new referral to the list */
1089 lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1090 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1092 if( lc->lconn_rebind_queue == NULL ) {
1093 ld->ld_errno = LDAP_NO_MEMORY;
1097 lc->lconn_rebind_queue[j] = refarray;
1098 lc->lconn_rebind_queue[j+1] = NULL;
1102 /* We have queued the referral/reference, now just return */
1105 count = 1; /* Pretend we already followed referral */
1109 /* Re-encode the request with the new starting point of the search.
1110 * Note: In the future we also need to replace the filter if one
1111 * was provided with the search reference
1114 /* For references we don't want old dn if new dn empty */
1115 if ( sref && srv->lud_dn == NULL ) {
1116 srv->lud_dn = LDAP_STRDUP( "" );
1119 LDAP_NEXT_MSGID( ld, id );
1120 ber = re_encode_request( ld, origreq->lr_ber, id,
1121 sref, srv, &rinfo.ri_request );
1124 ld->ld_errno = LDAP_ENCODING_ERROR;
1129 Debug( LDAP_DEBUG_TRACE,
1130 "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1131 lr->lr_msgid, refarray[i], 0);
1133 /* Send the new request to the server - may require a bind */
1134 rinfo.ri_msgid = origreq->lr_origid;
1135 rinfo.ri_url = refarray[i];
1136 #ifdef LDAP_R_COMPILE
1137 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1139 rc = ldap_send_server_request( ld, ber, id,
1140 origreq, &srv, NULL, &rinfo );
1141 #ifdef LDAP_R_COMPILE
1142 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1145 /* Failure, try next referral in the list */
1146 Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n",
1147 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1148 unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1149 ldap_free_urllist( srv );
1151 ld->ld_errno = LDAP_REFERRAL;
1153 /* Success, no need to try this referral list further */
1158 /* check if there is a queue of referrals that came in during bind */
1160 lc = find_connection( ld, srv, 1 );
1162 ld->ld_errno = LDAP_OPERATIONS_ERROR;
1168 if ( lc->lconn_rebind_queue != NULL ) {
1169 /* Release resources of previous list */
1170 LDAP_VFREE( refarray );
1172 ldap_free_urllist( srv );
1175 /* Pull entries off end of queue so list always null terminated */
1176 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1178 refarray = lc->lconn_rebind_queue[j - 1];
1179 lc->lconn_rebind_queue[j-1] = NULL;
1180 /* we pulled off last entry from queue, free queue */
1182 LDAP_FREE( lc->lconn_rebind_queue );
1183 lc->lconn_rebind_queue = NULL;
1185 /* restart the loop the with new referral list */
1189 break; /* referral followed, break out of for loop */
1191 } /* end for loop */
1193 LDAP_VFREE( refarray );
1194 ldap_free_urllist( srv );
1195 LDAP_FREE( *errstrp );
1199 LDAP_FREE( unfollowed );
1202 *errstrp = unfollowed;
1208 * XXX merging of errors in this routine needs to be improved
1211 ldap_chase_referrals( LDAP *ld,
1219 char *p, *ref, *unfollowed;
1220 LDAPRequest *origreq;
1226 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1228 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
1231 if ( *errstrp == NULL ) {
1235 len = strlen( *errstrp );
1236 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1237 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1239 p += LDAP_REF_STR_LEN;
1244 if ( len < LDAP_REF_STR_LEN ) {
1248 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1249 Debug( LDAP_DEBUG_ANY,
1250 "more than %d referral hops (dropping)\n",
1251 ld->ld_refhoplimit, 0, 0 );
1252 /* XXX report as error in ld->ld_errno? */
1256 /* find original request */
1257 for ( origreq = lr; origreq->lr_parent != NULL;
1258 origreq = origreq->lr_parent ) {
1265 /* parse out & follow referrals */
1266 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1267 p = strchr( ref, '\n' );
1272 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1273 if ( rc != LDAP_URL_SUCCESS ) {
1274 Debug( LDAP_DEBUG_TRACE,
1275 "ignoring %s referral <%s>\n",
1276 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1277 rc = ldap_append_referral( ld, &unfollowed, ref );
1282 Debug( LDAP_DEBUG_TRACE,
1283 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1287 /* See if we've already been here */
1288 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1291 int len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1292 for ( lp = lr; lp; lp = lp->lr_parent ) {
1293 if ( lp->lr_conn == lc
1294 && len == lp->lr_dn.bv_len )
1296 if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1303 ldap_free_urllist( srv );
1304 ld->ld_errno = LDAP_CLIENT_LOOP;
1310 LDAP_NEXT_MSGID( ld, id );
1311 ber = re_encode_request( ld, origreq->lr_ber,
1312 id, sref, srv, &rinfo.ri_request );
1314 if ( ber == NULL ) {
1318 /* copy the complete referral for rebind process */
1319 rinfo.ri_url = LDAP_STRDUP( ref );
1321 rinfo.ri_msgid = origreq->lr_origid;
1323 #ifdef LDAP_R_COMPILE
1324 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1326 rc = ldap_send_server_request( ld, ber, id,
1327 lr, &srv, NULL, &rinfo );
1328 #ifdef LDAP_R_COMPILE
1329 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1332 LDAP_FREE( rinfo.ri_url );
1337 Debug( LDAP_DEBUG_ANY,
1338 "Unable to chase referral \"%s\" (%d: %s)\n",
1339 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1340 rc = ldap_append_referral( ld, &unfollowed, ref );
1343 ldap_free_urllist(srv);
1346 LDAP_FREE( *errstrp );
1347 *errstrp = unfollowed;
1349 return(( rc == 0 ) ? count : rc );
1354 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1358 if ( *referralsp == NULL ) {
1360 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1364 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1365 strlen( *referralsp ) + strlen( s ) + 2 );
1368 if ( *referralsp == NULL ) {
1369 ld->ld_errno = LDAP_NO_MEMORY;
1374 strcpy( *referralsp, LDAP_REF_STR );
1376 strcat( *referralsp, "\n" );
1378 strcat( *referralsp, s );
1386 re_encode_request( LDAP *ld,
1387 BerElement *origber,
1394 * XXX this routine knows way too much about how the lber library works!
1402 BerElement tmpber, *ber;
1405 Debug( LDAP_DEBUG_TRACE,
1406 "re_encode_request: new msgid %ld, new dn <%s>\n",
1408 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1413 * all LDAP requests are sequences that start with a message id.
1414 * For all except delete, this is followed by a sequence that is
1415 * tagged with the operation code. For delete, the provided DN
1416 * is not wrapped by a sequence.
1418 rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1420 if ( rtag == LBER_ERROR ) {
1421 ld->ld_errno = LDAP_DECODING_ERROR;
1426 if ( tag == LDAP_REQ_BIND ) {
1427 /* bind requests have a version number before the DN & other stuff */
1428 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1430 } else if ( tag == LDAP_REQ_DELETE ) {
1431 /* delete requests don't have a DN wrapping sequence */
1432 rtag = ber_scanf( &tmpber, "m", &dn );
1434 } else if ( tag == LDAP_REQ_SEARCH ) {
1435 /* search requests need to be re-scope-ed */
1436 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1438 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1439 /* use the scope provided in reference */
1440 scope = srv->lud_scope;
1442 } else if ( sref ) {
1443 /* use scope implied by previous operation
1446 * subtree -> subtree
1447 * subordinate -> subtree
1451 case LDAP_SCOPE_BASE:
1452 case LDAP_SCOPE_ONELEVEL:
1453 scope = LDAP_SCOPE_BASE;
1455 case LDAP_SCOPE_SUBTREE:
1456 case LDAP_SCOPE_SUBORDINATE:
1457 scope = LDAP_SCOPE_SUBTREE;
1463 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1466 if( rtag == LBER_ERROR ) {
1467 ld->ld_errno = LDAP_DECODING_ERROR;
1471 /* restore character zero'd out by ber_scanf*/
1472 dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1474 if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1478 if ( srv->lud_dn ) {
1479 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1482 if ( tag == LDAP_REQ_BIND ) {
1483 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1484 } else if ( tag == LDAP_REQ_DELETE ) {
1485 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1486 } else if ( tag == LDAP_REQ_SEARCH ) {
1487 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1489 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1493 ld->ld_errno = LDAP_ENCODING_ERROR;
1498 if ( tag != LDAP_REQ_DELETE && (
1499 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1500 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1501 ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1503 ld->ld_errno = LDAP_ENCODING_ERROR;
1509 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1510 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1512 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1514 #endif /* LDAP_DEBUG */
1516 *type = tag; /* return request type */
1522 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1526 #ifdef LDAP_R_COMPILE
1527 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1529 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1530 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1531 continue; /* Skip completed requests */
1533 if ( msgid == lr->lr_msgid ) {
1538 #ifdef LDAP_R_COMPILE
1539 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1546 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1550 #ifdef LDAP_R_COMPILE
1551 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1553 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1555 if ( lr->lr_refcnt > 0 ) {
1558 } else if ( lr->lr_refcnt < 0 ) {
1560 if ( lr->lr_refcnt == 0 ) {
1568 ldap_free_request_int( ld, lrx );
1570 } else if ( freeit ) {
1571 ldap_free_request( ld, lrx );
1573 #ifdef LDAP_R_COMPILE
1574 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );