]> git.sur5r.net Git - openldap/blob - libraries/libldap/request.c
e6b695a6442e6260c2cf22632fcf481b310264b5
[openldap] / libraries / libldap / request.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18 /* This notice applies to changes, created by or for Novell, Inc.,
19  * to preexisting works for which notices appear elsewhere in this file.
20  *
21  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
22  *
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. 
31  *---
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
35  *---
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.
39  */
40
41 #include "portable.h"
42
43 #include <stdio.h>
44
45 #include <ac/stdlib.h>
46
47 #include <ac/errno.h>
48 #include <ac/socket.h>
49 #include <ac/string.h>
50 #include <ac/time.h>
51 #include <ac/unistd.h>
52
53 #include "ldap-int.h"
54 #include "lber.h"
55
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 ));
59
60 static BerElement *
61 re_encode_request( LDAP *ld,
62         BerElement *origber,
63         ber_int_t msgid,
64         int sref,
65         LDAPURLDesc *srv,
66         int *type );
67
68 BerElement *
69 ldap_alloc_ber_with_options( LDAP *ld )
70 {
71         BerElement      *ber;
72
73         ber = ber_alloc_t( ld->ld_lberoptions );
74         if ( ber == NULL ) {
75                 ld->ld_errno = LDAP_NO_MEMORY;
76         }
77
78         return( ber );
79 }
80
81
82 void
83 ldap_set_ber_options( LDAP *ld, BerElement *ber )
84 {
85         ber->ber_options = ld->ld_lberoptions;
86 }
87
88
89 ber_int_t
90 ldap_send_initial_request(
91         LDAP *ld,
92         ber_tag_t msgtype,
93         const char *dn,
94         BerElement *ber,
95         ber_int_t msgid)
96 {
97         int rc = 1;
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
100
101 #ifdef LDAP_R_COMPILE
102         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
103 #endif
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 );
107
108         }
109 #ifdef LDAP_R_COMPILE
110         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
111 #endif
112         if( rc < 0 ) {
113                 ber_free( ber, 1 );
114                 return( -1 );
115         } else if ( rc == 0 ) {
116                 Debug( LDAP_DEBUG_TRACE,
117                         "ldap_open_defconn: successful\n",
118                         0, 0, 0 );
119         }
120
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);
127                         return 0;
128                 }
129                 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
130                         return LDAP_PARAM_ERROR;
131         }
132 #endif
133 #ifdef LDAP_R_COMPILE
134         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
135 #endif
136         rc = ldap_send_server_request( ld, ber, msgid, NULL,
137                 NULL, NULL, NULL );
138 #ifdef LDAP_R_COMPILE
139         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
140 #endif
141         return(rc);
142 }
143
144
145 int
146 ldap_int_flush_request(
147         LDAP *ld,
148         LDAPRequest *lr )
149 {
150         LDAPConn *lc = lr->lr_conn;
151
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;
158                         return -2;
159                 } else {
160                         ld->ld_errno = LDAP_SERVER_DOWN;
161                         ldap_free_request( ld, lr );
162                         ldap_free_connection( ld, lc, 0, 0 );
163                         return( -1 );
164                 }
165         } else {
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;
169                 }
170                 lr->lr_status = LDAP_REQST_INPROGRESS;
171
172                 /* sent -- waiting for a response */
173                 ldap_mark_select_read( ld, lc->lconn_sb );
174         }
175         return 0;
176 }
177
178 int
179 ldap_send_server_request(
180         LDAP *ld,
181         BerElement *ber,
182         ber_int_t msgid,
183         LDAPRequest *parentreq,
184         LDAPURLDesc **srvlist,
185         LDAPConn *lc,
186         LDAPreqinfo *bind )
187 {
188         LDAPRequest     *lr;
189         int             incparent, rc;
190
191         Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
192
193         incparent = 0;
194         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
195
196         if ( lc == NULL ) {
197                 if ( srvlist == NULL ) {
198                         lc = ld->ld_defconn;
199                 } else {
200                         lc = find_connection( ld, *srvlist, 1 );
201                         if ( lc == NULL ) {
202                                 if ( (bind != NULL) && (parentreq != NULL) ) {
203                                         /* Remember the bind in the parent */
204                                         incparent = 1;
205                                         ++parentreq->lr_outrefcnt;
206                                 }
207                                 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
208                         }
209                 }
210         }
211
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 };
216
217                 ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
218
219                 /* poll ... */
220                 switch ( ldap_int_poll( ld, sd, &tv ) ) {
221                 case 0:
222                         /* go on! */
223                         lc->lconn_status = LDAP_CONNST_CONNECTED;
224                         break;
225
226                 case -2:
227                         /* async only occurs if a network timeout is set */
228
229                         /* honor network timeout */
230                         if ( time( NULL ) - lc->lconn_created <= ld->ld_options.ldo_tm_net.tv_sec )
231                         {
232                                 /* caller will have to call again */
233                                 ld->ld_errno = LDAP_X_CONNECTING;
234                         }
235                         /* fallthru */
236
237                 default:
238                         /* error */
239                         break;
240                 }
241         }
242
243         if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
244                 if ( ld->ld_errno == LDAP_SUCCESS ) {
245                         ld->ld_errno = LDAP_SERVER_DOWN;
246                 }
247
248                 ber_free( ber, 1 );
249                 if ( incparent ) {
250                         /* Forget about the bind */
251                         --parentreq->lr_outrefcnt; 
252                 }
253                 return( -1 );
254         }
255
256         use_connection( ld, lc );
257
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 );
264                 if ( rc == -1 ) {
265                         ld->ld_errno = LDAP_ENCODING_ERROR;
266                         return rc;
267                 }
268         }
269 #endif
270
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.
275          */
276         rc = 0;
277         if ( ld->ld_requests &&
278                 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
279                 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
280         {
281                 rc = -1;
282         }
283         if ( rc ) return rc;
284
285         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ) );
286         if ( lr == NULL ) {
287                 ld->ld_errno = LDAP_NO_MEMORY;
288                 ldap_free_connection( ld, lc, 0, 0 );
289                 ber_free( ber, 1 );
290                 if ( incparent ) {
291                         /* Forget about the bind */
292                         --parentreq->lr_outrefcnt; 
293                 }
294                 return( -1 );
295         } 
296         lr->lr_msgid = msgid;
297         lr->lr_status = LDAP_REQST_INPROGRESS;
298         lr->lr_res_errno = LDAP_SUCCESS;        /* optimistic */
299         lr->lr_ber = ber;
300         lr->lr_conn = lc;
301         if ( parentreq != NULL ) {      /* sub-request */
302                 if ( !incparent ) { 
303                         /* Increment if we didn't do it before the bind */
304                         ++parentreq->lr_outrefcnt;
305                 }
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;
313         }
314
315         /* Extract requestDN for future reference */
316         {
317                 BerElement tmpber = *ber;
318                 ber_int_t       bint;
319                 ber_tag_t       tag, rtag;
320
321                 ber_reset( &tmpber, 1 );
322                 rtag = ber_scanf( &tmpber, "{it", /*}*/ &bint, &tag );
323                 switch ( tag ) {
324                 case LDAP_REQ_BIND:
325                         rtag = ber_scanf( &tmpber, "{i" /*}*/, &bint );
326                         break;
327                 case LDAP_REQ_DELETE:
328                         break;
329                 default:
330                         rtag = ber_scanf( &tmpber, "{" /*}*/ );
331                 case LDAP_REQ_ABANDON:
332                         break;
333                 }
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;
337                 }
338         }
339
340         lr->lr_prev = NULL;
341         lr->lr_next = ld->ld_requests;
342         if ( lr->lr_next != NULL ) {
343                 lr->lr_next->lr_prev = lr;
344         }
345         ld->ld_requests = lr;
346
347         ld->ld_errno = LDAP_SUCCESS;
348         if ( ldap_int_flush_request( ld, lr ) == -1 ) {
349                 msgid = -1;
350         }
351
352         return( msgid );
353 }
354
355 /* return 0 if no StartTLS ext, 1 if present, 2 if critical */
356 static int
357 find_tls_ext( LDAPURLDesc *srv )
358 {
359         int i, crit;
360         char *ext;
361
362         if ( !srv->lud_exts )
363                 return 0;
364
365         for (i=0; srv->lud_exts[i]; i++) {
366                 crit = 0;
367                 ext = srv->lud_exts[i];
368                 if ( ext[0] == '!') {
369                         ext++;
370                         crit = 1;
371                 }
372                 if ( !strcasecmp( ext, "StartTLS" ) ||
373                         !strcasecmp( ext, "X-StartTLS" ) ||
374                         !strcmp( ext, LDAP_EXOP_START_TLS )) {
375                         return crit + 1;
376                 }
377         }
378         return 0;
379 }
380
381 LDAPConn *
382 ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb,
383         int connect, LDAPreqinfo *bind )
384 {
385         LDAPConn        *lc;
386         int             async = 0;
387
388         Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
389                 use_ldsb, connect, (bind != NULL) );
390         /*
391          * make a new LDAP server connection
392          * XXX open connection synchronously for now
393          */
394         lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
395         if ( lc == NULL ) {
396                 ld->ld_errno = LDAP_NO_MEMORY;
397                 return( NULL );
398         }
399         
400         if ( use_ldsb ) {
401                 assert( ld->ld_sb != NULL );
402                 lc->lconn_sb = ld->ld_sb;
403
404         } else {
405                 lc->lconn_sb = ber_sockbuf_alloc();
406                 if ( lc->lconn_sb == NULL ) {
407                         LDAP_FREE( (char *)lc );
408                         ld->ld_errno = LDAP_NO_MEMORY;
409                         return( NULL );
410                 }
411         }
412
413         if ( connect ) {
414                 LDAPURLDesc     **srvp, *srv = NULL;
415
416                 async = LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_CONNECT_ASYNC );
417
418                 for ( srvp = srvlist; *srvp != NULL; srvp = &(*srvp)->lud_next ) {
419                         int             rc;
420
421                         rc = ldap_int_open_connection( ld, lc, *srvp, async );
422                         if ( rc != -1 ) {
423                                 srv = *srvp;
424
425                                 if ( ld->ld_urllist_proc && ( !async || rc != -2 ) ) {
426                                         ld->ld_urllist_proc( ld, srvlist, srvp, ld->ld_urllist_params );
427                                 }
428
429                                 break;
430                         }
431                 }
432
433                 if ( srv == NULL ) {
434                         if ( !use_ldsb ) {
435                                 ber_sockbuf_free( lc->lconn_sb );
436                         }
437                         LDAP_FREE( (char *)lc );
438                         ld->ld_errno = LDAP_SERVER_DOWN;
439                         return( NULL );
440                 }
441
442                 lc->lconn_server = ldap_url_dup( srv );
443         }
444
445         lc->lconn_status = async ? LDAP_CONNST_CONNECTING : LDAP_CONNST_CONNECTED;
446 #ifdef LDAP_R_COMPILE
447         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
448 #endif
449         lc->lconn_next = ld->ld_conns;
450         ld->ld_conns = lc;
451 #ifdef LDAP_R_COMPILE
452         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
453 #endif
454
455         if ( lc->lconn_server->lud_exts ) {
456 #ifdef HAVE_TLS
457                 if ( connect ) {
458                         int rc, ext = find_tls_ext( lc->lconn_server );
459                         if ( ext ) {
460                                 LDAPConn        *savedefconn;
461
462                                 savedefconn = ld->ld_defconn;
463                                 ++lc->lconn_refcnt;     /* avoid premature free */
464                                 ld->ld_defconn = lc;
465
466 #ifdef LDAP_R_COMPILE
467                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
468                                 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
469 #endif
470                                 rc = ldap_start_tls_s( ld, NULL, NULL );
471 #ifdef LDAP_R_COMPILE
472                                 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
473                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
474 #endif
475                                 ld->ld_defconn = savedefconn;
476                                 --lc->lconn_refcnt;
477
478                                 if ( rc != LDAP_SUCCESS && ext == 2 ) {
479                                         ldap_free_connection( ld, lc, 1, 0 );
480                                         return NULL;
481                                 }
482                         }
483                         
484                 }
485 #endif
486         }
487
488         if ( bind != NULL ) {
489                 int             err = 0;
490                 LDAPConn        *savedefconn;
491
492                 /* Set flag to prevent additional referrals
493                  * from being processed on this
494                  * connection until the bind has completed
495                  */
496                 lc->lconn_rebind_inprogress = 1;
497                 /* V3 rebind function */
498                 if ( ld->ld_rebind_proc != NULL) {
499                         LDAPURLDesc     *srvfunc;
500
501                         srvfunc = ldap_url_dup( *srvlist );
502                         if ( srvfunc == NULL ) {
503                                 ld->ld_errno = LDAP_NO_MEMORY;
504                                 err = -1;
505                         } else {
506                                 savedefconn = ld->ld_defconn;
507                                 ++lc->lconn_refcnt;     /* avoid premature free */
508                                 ld->ld_defconn = lc;
509
510                                 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
511 #ifdef LDAP_R_COMPILE
512                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
513                                 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
514 #endif
515                                 err = (*ld->ld_rebind_proc)( ld,
516                                         bind->ri_url, bind->ri_request, bind->ri_msgid,
517                                         ld->ld_rebind_params );
518 #ifdef LDAP_R_COMPILE
519                                 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
520                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
521 #endif
522
523                                 ld->ld_defconn = savedefconn;
524                                 --lc->lconn_refcnt;
525
526                                 if ( err != 0 ) {
527                                         err = -1;
528                                         ldap_free_connection( ld, lc, 1, 0 );
529                                         lc = NULL;
530                                 }
531                                 ldap_free_urldesc( srvfunc );
532                         }
533
534                 } else {
535                         int             msgid, rc;
536                         struct berval   passwd = BER_BVNULL;
537
538                         savedefconn = ld->ld_defconn;
539                         ++lc->lconn_refcnt;     /* avoid premature free */
540                         ld->ld_defconn = lc;
541
542                         Debug( LDAP_DEBUG_TRACE,
543                                 "anonymous rebind via ldap_sasl_bind(\"\")\n",
544                                 0, 0, 0);
545
546 #ifdef LDAP_R_COMPILE
547                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
548                         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
549 #endif
550                         rc = ldap_sasl_bind( ld, "", LDAP_SASL_SIMPLE, &passwd,
551                                 NULL, NULL, &msgid );
552                         if ( rc != LDAP_SUCCESS ) {
553                                 err = -1;
554
555                         } else {
556                                 for ( err = 1; err > 0; ) {
557                                         struct timeval  tv = { 0, 100000 };
558                                         LDAPMessage     *res = NULL;
559
560                                         switch ( ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
561                                         case -1:
562                                                 err = -1;
563                                                 break;
564
565                                         case 0:
566 #ifdef LDAP_R_COMPILE
567                                                 ldap_pvt_thread_yield();
568 #endif
569                                                 break;
570
571                                         case LDAP_RES_BIND:
572                                                 rc = ldap_parse_result( ld, res, &err, NULL, NULL, NULL, NULL, 1 );
573                                                 if ( rc != LDAP_SUCCESS ) {
574                                                         err = -1;
575
576                                                 } else if ( err != LDAP_SUCCESS ) {
577                                                         err = -1;
578                                                 }
579                                                 /* else err == LDAP_SUCCESS == 0 */
580                                                 break;
581
582                                         default:
583                                                 Debug( LDAP_DEBUG_TRACE,
584                                                         "ldap_new_connection %p: "
585                                                         "unexpected response %d "
586                                                         "from BIND request id=%d\n",
587                                                         (void *) ld, ldap_msgtype( res ), msgid );
588                                                 err = -1;
589                                                 break;
590                                         }
591                                 }
592                         }
593 #ifdef LDAP_R_COMPILE
594                         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
595                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
596 #endif
597                         ld->ld_defconn = savedefconn;
598                         --lc->lconn_refcnt;
599
600                         if ( err != 0 ) {
601                                 ldap_free_connection( ld, lc, 1, 0 );
602                                 lc = NULL;
603                         }
604                 }
605                 if ( lc != NULL )
606                         lc->lconn_rebind_inprogress = 0;
607         }
608
609         return( lc );
610 }
611
612
613 static LDAPConn *
614 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
615 /*
616  * return an existing connection (if any) to the server srv
617  * if "any" is non-zero, check for any server in the "srv" chain
618  */
619 {
620         LDAPConn        *lc;
621         LDAPURLDesc     *lcu, *lsu;
622         int lcu_port, lsu_port;
623         int found = 0;
624
625 #ifdef LDAP_R_COMPILE
626         ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
627 #endif
628         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
629                 lcu = lc->lconn_server;
630                 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
631                         lcu->lud_port );
632
633                 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
634                         lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
635                                 lsu->lud_port );
636
637                         if ( lsu_port == lcu_port
638                                 && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
639                                 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
640                                 && lsu->lud_host != NULL && *lsu->lud_host != '\0'
641                                 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
642                         {
643                                 found = 1;
644                                 break;
645                         }
646
647                         if ( !any ) break;
648                 }
649                 if ( found )
650                         break;
651         }
652 #ifdef LDAP_R_COMPILE
653         ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
654 #endif
655         return lc;
656 }
657
658
659
660 static void
661 use_connection( LDAP *ld, LDAPConn *lc )
662 {
663         ++lc->lconn_refcnt;
664         lc->lconn_lastused = time( NULL );
665 }
666
667
668 void
669 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
670 {
671         LDAPConn        *tmplc, *prevlc;
672
673         Debug( LDAP_DEBUG_TRACE,
674                 "ldap_free_connection %d %d\n",
675                 force, unbind, 0 );
676
677         if ( force || --lc->lconn_refcnt <= 0 ) {
678                 /* remove from connections list first */
679 #ifdef LDAP_R_COMPILE
680                 ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
681 #endif
682
683                 for ( prevlc = NULL, tmplc = ld->ld_conns;
684                         tmplc != NULL;
685                         tmplc = tmplc->lconn_next )
686                 {
687                         if ( tmplc == lc ) {
688                                 if ( prevlc == NULL ) {
689                                     ld->ld_conns = tmplc->lconn_next;
690                                 } else {
691                                     prevlc->lconn_next = tmplc->lconn_next;
692                                 }
693                                 if ( ld->ld_defconn == lc ) {
694                                         ld->ld_defconn = NULL;
695                                 }
696                                 break;
697                         }
698                         prevlc = tmplc;
699                 }
700 #ifdef LDAP_R_COMPILE
701                 ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
702 #endif
703
704                 /* process connection callbacks */
705                 {
706                         struct ldapoptions *lo;
707                         ldaplist *ll;
708                         ldap_conncb *cb;
709
710                         lo = &ld->ld_options;
711                         if ( lo->ldo_conn_cbs ) {
712                                 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
713                                         cb = ll->ll_data;
714                                         cb->lc_del( ld, lc->lconn_sb, cb );
715                                 }
716                         }
717                         lo = LDAP_INT_GLOBAL_OPT();
718                         if ( lo->ldo_conn_cbs ) {
719                                 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
720                                         cb = ll->ll_data;
721                                         cb->lc_del( ld, lc->lconn_sb, cb );
722                                 }
723                         }
724                 }
725
726                 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
727                         ldap_mark_select_clear( ld, lc->lconn_sb );
728                         if ( unbind ) {
729                                 ldap_send_unbind( ld, lc->lconn_sb,
730                                                 NULL, NULL );
731                         }
732                 }
733
734                 if ( lc->lconn_ber != NULL ) {
735                         ber_free( lc->lconn_ber, 1 );
736                 }
737
738                 ldap_int_sasl_close( ld, lc );
739 #ifdef HAVE_GSSAPI
740                 ldap_int_gssapi_close( ld, lc );
741 #endif
742
743                 ldap_free_urllist( lc->lconn_server );
744
745                 /* FIXME: is this at all possible?
746                  * ldap_ld_free() in unbind.c calls ldap_free_connection()
747                  * with force == 1 __after__ explicitly calling
748                  * ldap_free_request() on all requests */
749                 if ( force ) {
750                         LDAPRequest     *lr;
751
752                         for ( lr = ld->ld_requests; lr; ) {
753                                 LDAPRequest     *lr_next = lr->lr_next;
754
755                                 if ( lr->lr_conn == lc ) {
756                                         ldap_free_request_int( ld, lr );
757                                 }
758
759                                 lr = lr_next;
760                         }
761                 }
762
763                 if ( lc->lconn_sb != ld->ld_sb ) {
764                         ber_sockbuf_free( lc->lconn_sb );
765                 } else {
766                         ber_int_sb_close( lc->lconn_sb );
767                 }
768
769                 if ( lc->lconn_rebind_queue != NULL) {
770                         int i;
771                         for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
772                                 LDAP_VFREE( lc->lconn_rebind_queue[i] );
773                         }
774                         LDAP_FREE( lc->lconn_rebind_queue );
775                 }
776
777                 LDAP_FREE( lc );
778
779                 Debug( LDAP_DEBUG_TRACE,
780                         "ldap_free_connection: actually freed\n",
781                         0, 0, 0 );
782
783         } else {
784                 lc->lconn_lastused = time( NULL );
785                 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
786                                 lc->lconn_refcnt, 0, 0 );
787         }
788 }
789
790
791 #ifdef LDAP_DEBUG
792 void
793 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
794 {
795         LDAPConn        *lc;
796         char            timebuf[32];
797
798         Debug( LDAP_DEBUG_TRACE, "** ld %p Connection%s:\n", (void *)ld, all ? "s" : "", 0 );
799         for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
800                 if ( lc->lconn_server != NULL ) {
801                         Debug( LDAP_DEBUG_TRACE, "* host: %s  port: %d%s\n",
802                                 ( lc->lconn_server->lud_host == NULL ) ? "(null)"
803                                 : lc->lconn_server->lud_host,
804                                 lc->lconn_server->lud_port, ( lc->lconn_sb ==
805                                 ld->ld_sb ) ? "  (default)" : "" );
806                 }
807                 Debug( LDAP_DEBUG_TRACE, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
808                         ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET )
809                                 ? "NeedSocket" :
810                                 ( lc->lconn_status == LDAP_CONNST_CONNECTING )
811                                         ? "Connecting" : "Connected", 0 );
812                 Debug( LDAP_DEBUG_TRACE, "  last used: %s%s\n",
813                         ldap_pvt_ctime( &lc->lconn_lastused, timebuf ),
814                         lc->lconn_rebind_inprogress ? "  rebind in progress" : "", 0 );
815                 if ( lc->lconn_rebind_inprogress ) {
816                         if ( lc->lconn_rebind_queue != NULL) {
817                                 int     i;
818
819                                 for ( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
820                                         int     j;
821                                         for( j = 0; lc->lconn_rebind_queue[i][j] != 0; j++ ) {
822                                                 Debug( LDAP_DEBUG_TRACE, "    queue %d entry %d - %s\n",
823                                                         i, j, lc->lconn_rebind_queue[i][j] );
824                                         }
825                                 }
826                         } else {
827                                 Debug( LDAP_DEBUG_TRACE, "    queue is empty\n", 0, 0, 0 );
828                         }
829                 }
830                 Debug( LDAP_DEBUG_TRACE, "\n", 0, 0, 0 );
831                 if ( !all ) {
832                         break;
833                 }
834         }
835 }
836
837
838 void
839 ldap_dump_requests_and_responses( LDAP *ld )
840 {
841         LDAPRequest     *lr;
842         LDAPMessage     *lm, *l;
843         int             i;
844
845         Debug( LDAP_DEBUG_TRACE, "** ld %p Outstanding Requests:\n",
846                 (void *)ld, 0, 0 );
847         lr = ld->ld_requests;
848         if ( lr == NULL ) {
849                 Debug( LDAP_DEBUG_TRACE, "   Empty\n", 0, 0, 0 );
850         }
851         for ( i = 0; lr != NULL; lr = lr->lr_next, i++ ) {
852                 Debug( LDAP_DEBUG_TRACE, " * msgid %d,  origid %d, status %s\n",
853                         lr->lr_msgid, lr->lr_origid,
854                         ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
855                         ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
856                         ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
857                         ( lr->lr_status == LDAP_REQST_WRITING ) ? "Writing" :
858                         ( lr->lr_status == LDAP_REQST_COMPLETED ) ? "RequestCompleted"
859                                 : "InvalidStatus" );
860                 Debug( LDAP_DEBUG_TRACE, "   outstanding referrals %d, parent count %d\n",
861                         lr->lr_outrefcnt, lr->lr_parentcnt, 0 );
862         }
863         Debug( LDAP_DEBUG_TRACE, "  ld %p request count %d (abandoned %lu)\n",
864                 (void *)ld, i, ld->ld_nabandoned );
865         Debug( LDAP_DEBUG_TRACE, "** ld %p Response Queue:\n", (void *)ld, 0, 0 );
866         if ( ( lm = ld->ld_responses ) == NULL ) {
867                 Debug( LDAP_DEBUG_TRACE, "   Empty\n", 0, 0, 0 );
868         }
869         for ( i = 0; lm != NULL; lm = lm->lm_next, i++ ) {
870                 Debug( LDAP_DEBUG_TRACE, " * msgid %d,  type %lu\n",
871                     lm->lm_msgid, (unsigned long)lm->lm_msgtype, 0 );
872                 if ( lm->lm_chain != NULL ) {
873                         Debug( LDAP_DEBUG_TRACE, "   chained responses:\n", 0, 0, 0 );
874                         for ( l = lm->lm_chain; l != NULL; l = l->lm_chain ) {
875                                 Debug( LDAP_DEBUG_TRACE,
876                                         "  * msgid %d,  type %lu\n",
877                                         l->lm_msgid,
878                                         (unsigned long)l->lm_msgtype, 0 );
879                         }
880                 }
881         }
882         Debug( LDAP_DEBUG_TRACE, "  ld %p response count %d\n", (void *)ld, i, 0 );
883 }
884 #endif /* LDAP_DEBUG */
885
886 static void
887 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
888 {
889         /* if lr_refcnt > 0, the request has been looked up 
890          * by ldap_find_request_by_msgid(); if in the meanwhile
891          * the request is free()'d by someone else, just decrease
892          * the reference count and extract it from the request
893          * list; later on, it will be freed. */
894         if ( lr->lr_prev == NULL ) {
895                 if ( lr->lr_refcnt == 0 ) {
896                         /* free'ing the first request? */
897                         assert( ld->ld_requests == lr );
898                 }
899
900                 if ( ld->ld_requests == lr ) {
901                         ld->ld_requests = lr->lr_next;
902                 }
903
904         } else {
905                 lr->lr_prev->lr_next = lr->lr_next;
906         }
907
908         if ( lr->lr_next != NULL ) {
909                 lr->lr_next->lr_prev = lr->lr_prev;
910         }
911
912         if ( lr->lr_refcnt > 0 ) {
913                 lr->lr_refcnt = -lr->lr_refcnt;
914
915                 lr->lr_prev = NULL;
916                 lr->lr_next = NULL;
917
918                 return;
919         }
920
921         if ( lr->lr_ber != NULL ) {
922                 ber_free( lr->lr_ber, 1 );
923                 lr->lr_ber = NULL;
924         }
925
926         if ( lr->lr_res_error != NULL ) {
927                 LDAP_FREE( lr->lr_res_error );
928                 lr->lr_res_error = NULL;
929         }
930
931         if ( lr->lr_res_matched != NULL ) {
932                 LDAP_FREE( lr->lr_res_matched );
933                 lr->lr_res_matched = NULL;
934         }
935
936         LDAP_FREE( lr );
937 }
938
939 void
940 ldap_free_request( LDAP *ld, LDAPRequest *lr )
941 {
942 #ifdef LDAP_R_COMPILE
943         LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_req_mutex );
944 #endif
945
946         Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
947                 lr->lr_origid, lr->lr_msgid, 0 );
948
949         /* free all referrals (child requests) */
950         while ( lr->lr_child ) {
951                 ldap_free_request( ld, lr->lr_child );
952         }
953
954         if ( lr->lr_parent != NULL ) {
955                 LDAPRequest     **lrp;
956
957                 --lr->lr_parent->lr_outrefcnt;
958                 for ( lrp = &lr->lr_parent->lr_child;
959                         *lrp && *lrp != lr;
960                         lrp = &(*lrp)->lr_refnext );
961
962                 if ( *lrp == lr ) {
963                         *lrp = lr->lr_refnext;
964                 }
965         }
966         ldap_free_request_int( ld, lr );
967 }
968
969 /*
970  * call first time with *cntp = -1
971  * when returns *cntp == -1, no referrals are left
972  *
973  * NOTE: may replace *refsp, or shuffle the contents
974  * of the original array.
975  */
976 static int ldap_int_nextref(
977         LDAP                    *ld,
978         char                    ***refsp,
979         int                     *cntp,
980         void                    *params )
981 {
982         assert( refsp != NULL );
983         assert( *refsp != NULL );
984         assert( cntp != NULL );
985
986         if ( *cntp < -1 ) {
987                 *cntp = -1;
988                 return -1;
989         }
990
991         (*cntp)++;
992
993         if ( (*refsp)[ *cntp ] == NULL ) {
994                 *cntp = -1;
995         }
996
997         return 0;
998 }
999
1000 /*
1001  * Chase v3 referrals
1002  *
1003  * Parameters:
1004  *  (IN) ld = LDAP connection handle
1005  *  (IN) lr = LDAP Request structure
1006  *  (IN) refs = array of pointers to referral strings that we will chase
1007  *              The array will be free'd by this function when no longer needed
1008  *  (IN) sref != 0 if following search reference
1009  *  (OUT) errstrp = Place to return a string of referrals which could not be followed
1010  *  (OUT) hadrefp = 1 if sucessfully followed referral
1011  *
1012  * Return value - number of referrals followed
1013  */
1014 int
1015 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
1016 {
1017         char            *unfollowed;
1018         int              unfollowedcnt = 0;
1019         LDAPRequest     *origreq;
1020         LDAPURLDesc     *srv = NULL;
1021         BerElement      *ber;
1022         char            **refarray = NULL;
1023         LDAPConn        *lc;
1024         int                      rc, count, i, j, id;
1025         LDAPreqinfo  rinfo;
1026
1027         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
1028         *hadrefp = 0;
1029
1030         Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
1031
1032         unfollowed = NULL;
1033         rc = count = 0;
1034
1035         /* If no referrals in array, return */
1036         if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
1037                 rc = 0;
1038                 goto done;
1039         }
1040
1041         /* Check for hop limit exceeded */
1042         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1043                 Debug( LDAP_DEBUG_ANY,
1044                     "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
1045                 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
1046                 rc = -1;
1047                 goto done;
1048         }
1049
1050         /* find original request */
1051         for ( origreq = lr;
1052                 origreq->lr_parent != NULL;
1053                 origreq = origreq->lr_parent )
1054         {
1055                 /* empty */ ;
1056         }
1057
1058         refarray = refs;
1059         refs = NULL;
1060
1061         if ( ld->ld_nextref_proc == NULL ) {
1062                 ld->ld_nextref_proc = ldap_int_nextref;
1063         }
1064
1065         /* parse out & follow referrals */
1066         i = -1;
1067         for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
1068                         i != -1;
1069                         ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
1070         {
1071
1072                 /* Parse the referral URL */
1073                 rc = ldap_url_parse_ext( refarray[i], &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1074                 if ( rc != LDAP_URL_SUCCESS ) {
1075                         /* ldap_url_parse_ext() returns LDAP_URL_* errors
1076                          * which do not map on API errors */
1077                         ld->ld_errno = LDAP_PARAM_ERROR;
1078                         rc = -1;
1079                         goto done;
1080                 }
1081
1082                 if( srv->lud_crit_exts ) {
1083                         int ok = 0;
1084 #ifdef HAVE_TLS
1085                         /* If StartTLS is the only critical ext, OK. */
1086                         if ( find_tls_ext( srv ) == 2 && srv->lud_crit_exts == 1 )
1087                                 ok = 1;
1088 #endif
1089                         if ( !ok ) {
1090                                 /* we do not support any other extensions */
1091                                 ld->ld_errno = LDAP_NOT_SUPPORTED;
1092                                 rc = -1;
1093                                 goto done;
1094                         }
1095                 }
1096
1097                 /* check connection for re-bind in progress */
1098                 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1099                         /* See if we've already requested this DN with this conn */
1100                         LDAPRequest *lp;
1101                         int looped = 0;
1102                         ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1103                         for ( lp = origreq; lp; ) {
1104                                 if ( lp->lr_conn == lc
1105                                         && len == lp->lr_dn.bv_len
1106                                         && len
1107                                         && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) == 0 )
1108                                 {
1109                                         looped = 1;
1110                                         break;
1111                                 }
1112                                 if ( lp == origreq ) {
1113                                         lp = lp->lr_child;
1114                                 } else {
1115                                         lp = lp->lr_refnext;
1116                                 }
1117                         }
1118                         if ( looped ) {
1119                                 ldap_free_urllist( srv );
1120                                 srv = NULL;
1121                                 ld->ld_errno = LDAP_CLIENT_LOOP;
1122                                 rc = -1;
1123                                 continue;
1124                         }
1125
1126                         if ( lc->lconn_rebind_inprogress ) {
1127                                 /* We are already chasing a referral or search reference and a
1128                                  * bind on that connection is in progress.  We must queue
1129                                  * referrals on that connection, so we don't get a request
1130                                  * going out before the bind operation completes. This happens
1131                                  * if two search references come in one behind the other
1132                                  * for the same server with different contexts.
1133                                  */
1134                                 Debug( LDAP_DEBUG_TRACE,
1135                                         "ldap_chase_v3referrals: queue referral \"%s\"\n",
1136                                         refarray[i], 0, 0);
1137                                 if( lc->lconn_rebind_queue == NULL ) {
1138                                         /* Create a referral list */
1139                                         lc->lconn_rebind_queue =
1140                                                 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
1141
1142                                         if( lc->lconn_rebind_queue == NULL) {
1143                                                 ld->ld_errno = LDAP_NO_MEMORY;
1144                                                 rc = -1;
1145                                                 goto done;
1146                                         }
1147
1148                                         lc->lconn_rebind_queue[0] = refarray;
1149                                         lc->lconn_rebind_queue[1] = NULL;
1150                                         refarray = NULL;
1151
1152                                 } else {
1153                                         /* Count how many referral arrays we already have */
1154                                         for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
1155                                                 /* empty */;
1156                                         }
1157
1158                                         /* Add the new referral to the list */
1159                                         lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
1160                                                 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
1161
1162                                         if( lc->lconn_rebind_queue == NULL ) {
1163                                                 ld->ld_errno = LDAP_NO_MEMORY;
1164                                                 rc = -1;
1165                                                 goto done;
1166                                         }
1167                                         lc->lconn_rebind_queue[j] = refarray;
1168                                         lc->lconn_rebind_queue[j+1] = NULL;
1169                                         refarray = NULL;
1170                                 }
1171
1172                                 /* We have queued the referral/reference, now just return */
1173                                 rc = 0;
1174                                 *hadrefp = 1;
1175                                 count = 1; /* Pretend we already followed referral */
1176                                 goto done;
1177                         }
1178                 } 
1179                 /* Re-encode the request with the new starting point of the search.
1180                  * Note: In the future we also need to replace the filter if one
1181                  * was provided with the search reference
1182                  */
1183
1184                 /* For references we don't want old dn if new dn empty */
1185                 if ( sref && srv->lud_dn == NULL ) {
1186                         srv->lud_dn = LDAP_STRDUP( "" );
1187                 }
1188
1189                 LDAP_NEXT_MSGID( ld, id );
1190                 ber = re_encode_request( ld, origreq->lr_ber, id,
1191                         sref, srv, &rinfo.ri_request );
1192
1193                 if( ber == NULL ) {
1194                         ld->ld_errno = LDAP_ENCODING_ERROR;
1195                         rc = -1;
1196                         goto done;
1197                 }
1198
1199                 Debug( LDAP_DEBUG_TRACE,
1200                         "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
1201                         lr->lr_msgid, refarray[i], 0);
1202
1203                 /* Send the new request to the server - may require a bind */
1204                 rinfo.ri_msgid = origreq->lr_origid;
1205                 rinfo.ri_url = refarray[i];
1206 #ifdef LDAP_R_COMPILE
1207                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1208 #endif
1209                 rc = ldap_send_server_request( ld, ber, id,
1210                         origreq, &srv, NULL, &rinfo );
1211 #ifdef LDAP_R_COMPILE
1212                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1213 #endif
1214                 if ( rc < 0 ) {
1215                         /* Failure, try next referral in the list */
1216                         Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n", 
1217                                 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1218                         unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
1219                         ldap_free_urllist( srv );
1220                         srv = NULL;
1221                         ld->ld_errno = LDAP_REFERRAL;
1222                 } else {
1223                         /* Success, no need to try this referral list further */
1224                         rc = 0;
1225                         ++count;
1226                         *hadrefp = 1;
1227
1228                         /* check if there is a queue of referrals that came in during bind */
1229                         if ( lc == NULL) {
1230                                 lc = find_connection( ld, srv, 1 );
1231                                 if ( lc == NULL ) {
1232                                         ld->ld_errno = LDAP_OPERATIONS_ERROR;
1233                                         rc = -1;
1234                                         goto done;
1235                                 }
1236                         }
1237
1238                         if ( lc->lconn_rebind_queue != NULL ) {
1239                                 /* Release resources of previous list */
1240                                 LDAP_VFREE( refarray );
1241                                 refarray = NULL;
1242                                 ldap_free_urllist( srv );
1243                                 srv = NULL;
1244
1245                                 /* Pull entries off end of queue so list always null terminated */
1246                                 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++ )
1247                                         ;
1248                                 refarray = lc->lconn_rebind_queue[j - 1];
1249                                 lc->lconn_rebind_queue[j-1] = NULL;
1250                                 /* we pulled off last entry from queue, free queue */
1251                                 if ( j == 1 ) {
1252                                         LDAP_FREE( lc->lconn_rebind_queue );
1253                                         lc->lconn_rebind_queue = NULL;
1254                                 }
1255                                 /* restart the loop the with new referral list */
1256                                 i = -1;
1257                                 continue;
1258                         }
1259                         break; /* referral followed, break out of for loop */
1260                 }
1261         } /* end for loop */
1262 done:
1263         LDAP_VFREE( refarray );
1264         ldap_free_urllist( srv );
1265         LDAP_FREE( *errstrp );
1266         
1267         if( rc == 0 ) {
1268                 *errstrp = NULL;
1269                 LDAP_FREE( unfollowed );
1270                 return count;
1271         } else {
1272                 *errstrp = unfollowed;
1273                 return rc;
1274         }
1275 }
1276
1277 /*
1278  * XXX merging of errors in this routine needs to be improved
1279  */
1280 int
1281 ldap_chase_referrals( LDAP *ld,
1282         LDAPRequest *lr,
1283         char **errstrp,
1284         int sref,
1285         int *hadrefp )
1286 {
1287         int             rc, count, id;
1288         unsigned        len;
1289         char            *p, *ref, *unfollowed;
1290         LDAPRequest     *origreq;
1291         LDAPURLDesc     *srv;
1292         BerElement      *ber;
1293         LDAPreqinfo  rinfo;
1294         LDAPConn        *lc;
1295
1296         Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
1297
1298         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
1299         *hadrefp = 0;
1300
1301         if ( *errstrp == NULL ) {
1302                 return( 0 );
1303         }
1304
1305         len = strlen( *errstrp );
1306         for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1307                 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1308                         *p = '\0';
1309                         p += LDAP_REF_STR_LEN;
1310                         break;
1311                 }
1312         }
1313
1314         if ( len < LDAP_REF_STR_LEN ) {
1315                 return( 0 );
1316         }
1317
1318         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1319                 Debug( LDAP_DEBUG_ANY,
1320                     "more than %d referral hops (dropping)\n",
1321                     ld->ld_refhoplimit, 0, 0 );
1322                     /* XXX report as error in ld->ld_errno? */
1323                     return( 0 );
1324         }
1325
1326         /* find original request */
1327         for ( origreq = lr; origreq->lr_parent != NULL;
1328              origreq = origreq->lr_parent ) {
1329                 /* empty */;
1330         }
1331
1332         unfollowed = NULL;
1333         rc = count = 0;
1334
1335         /* parse out & follow referrals */
1336         for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1337                 p = strchr( ref, '\n' );
1338                 if ( p != NULL ) {
1339                         *p++ = '\0';
1340                 }
1341
1342                 rc = ldap_url_parse_ext( ref, &srv, LDAP_PVT_URL_PARSE_NOEMPTY_DN );
1343                 if ( rc != LDAP_URL_SUCCESS ) {
1344                         Debug( LDAP_DEBUG_TRACE,
1345                                 "ignoring %s referral <%s>\n",
1346                                 ref, rc == LDAP_URL_ERR_BADSCHEME ? "unknown" : "incorrect", 0 );
1347                         rc = ldap_append_referral( ld, &unfollowed, ref );
1348                         *hadrefp = 1;
1349                         continue;
1350                 }
1351
1352                 Debug( LDAP_DEBUG_TRACE,
1353                     "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1354
1355                 *hadrefp = 1;
1356
1357                 /* See if we've already been here */
1358                 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
1359                         LDAPRequest *lp;
1360                         int looped = 0;
1361                         ber_len_t len = srv->lud_dn ? strlen( srv->lud_dn ) : 0;
1362                         for ( lp = lr; lp; lp = lp->lr_parent ) {
1363                                 if ( lp->lr_conn == lc
1364                                         && len == lp->lr_dn.bv_len )
1365                                 {
1366                                         if ( len && strncmp( srv->lud_dn, lp->lr_dn.bv_val, len ) )
1367                                                         continue;
1368                                         looped = 1;
1369                                         break;
1370                                 }
1371                         }
1372                         if ( looped ) {
1373                                 ldap_free_urllist( srv );
1374                                 ld->ld_errno = LDAP_CLIENT_LOOP;
1375                                 rc = -1;
1376                                 continue;
1377                         }
1378                 }
1379
1380                 LDAP_NEXT_MSGID( ld, id );
1381                 ber = re_encode_request( ld, origreq->lr_ber,
1382                     id, sref, srv, &rinfo.ri_request );
1383
1384                 if ( ber == NULL ) {
1385                         return -1 ;
1386                 }
1387
1388                 /* copy the complete referral for rebind process */
1389                 rinfo.ri_url = LDAP_STRDUP( ref );
1390
1391                 rinfo.ri_msgid = origreq->lr_origid;
1392
1393 #ifdef LDAP_R_COMPILE
1394                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1395 #endif
1396                 rc = ldap_send_server_request( ld, ber, id,
1397                         lr, &srv, NULL, &rinfo );
1398 #ifdef LDAP_R_COMPILE
1399                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1400 #endif
1401
1402                 LDAP_FREE( rinfo.ri_url );
1403
1404                 if( rc >= 0 ) {
1405                         ++count;
1406                 } else {
1407                         Debug( LDAP_DEBUG_ANY,
1408                                 "Unable to chase referral \"%s\" (%d: %s)\n", 
1409                                 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1410                         rc = ldap_append_referral( ld, &unfollowed, ref );
1411                 }
1412
1413                 ldap_free_urllist(srv);
1414         }
1415
1416         LDAP_FREE( *errstrp );
1417         *errstrp = unfollowed;
1418
1419         return(( rc == 0 ) ? count : rc );
1420 }
1421
1422
1423 int
1424 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1425 {
1426         int     first;
1427
1428         if ( *referralsp == NULL ) {
1429                 first = 1;
1430                 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1431                     + 1 );
1432         } else {
1433                 first = 0;
1434                 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1435                     strlen( *referralsp ) + strlen( s ) + 2 );
1436         }
1437
1438         if ( *referralsp == NULL ) {
1439                 ld->ld_errno = LDAP_NO_MEMORY;
1440                 return( -1 );
1441         }
1442
1443         if ( first ) {
1444                 strcpy( *referralsp, LDAP_REF_STR );
1445         } else {
1446                 strcat( *referralsp, "\n" );
1447         }
1448         strcat( *referralsp, s );
1449
1450         return( 0 );
1451 }
1452
1453
1454
1455 static BerElement *
1456 re_encode_request( LDAP *ld,
1457         BerElement *origber,
1458         ber_int_t msgid,
1459         int sref,
1460         LDAPURLDesc *srv,
1461         int *type )
1462 {
1463         /*
1464          * XXX this routine knows way too much about how the lber library works!
1465          */
1466         ber_int_t       along;
1467         ber_tag_t       tag;
1468         ber_tag_t       rtag;
1469         ber_int_t       ver;
1470         ber_int_t       scope;
1471         int             rc;
1472         BerElement      tmpber, *ber;
1473         struct berval           dn;
1474
1475         Debug( LDAP_DEBUG_TRACE,
1476             "re_encode_request: new msgid %ld, new dn <%s>\n",
1477             (long) msgid,
1478                 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1479
1480         tmpber = *origber;
1481
1482         /*
1483          * all LDAP requests are sequences that start with a message id.
1484          * For all except delete, this is followed by a sequence that is
1485          * tagged with the operation code.  For delete, the provided DN
1486          * is not wrapped by a sequence.
1487          */
1488         rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1489
1490         if ( rtag == LBER_ERROR ) {
1491                 ld->ld_errno = LDAP_DECODING_ERROR;
1492                 return( NULL );
1493         }
1494
1495         assert( tag != 0);
1496         if ( tag == LDAP_REQ_BIND ) {
1497                 /* bind requests have a version number before the DN & other stuff */
1498                 rtag = ber_scanf( &tmpber, "{im" /*}*/, &ver, &dn );
1499
1500         } else if ( tag == LDAP_REQ_DELETE ) {
1501                 /* delete requests don't have a DN wrapping sequence */
1502                 rtag = ber_scanf( &tmpber, "m", &dn );
1503
1504         } else if ( tag == LDAP_REQ_SEARCH ) {
1505                 /* search requests need to be re-scope-ed */
1506                 rtag = ber_scanf( &tmpber, "{me" /*"}"*/, &dn, &scope );
1507
1508                 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1509                         /* use the scope provided in reference */
1510                         scope = srv->lud_scope;
1511
1512                 } else if ( sref ) {
1513                         /* use scope implied by previous operation
1514                          *   base -> base
1515                          *   one -> base
1516                          *   subtree -> subtree
1517                          *   subordinate -> subtree
1518                          */
1519                         switch( scope ) {
1520                         default:
1521                         case LDAP_SCOPE_BASE:
1522                         case LDAP_SCOPE_ONELEVEL:
1523                                 scope = LDAP_SCOPE_BASE;
1524                                 break;
1525                         case LDAP_SCOPE_SUBTREE:
1526                         case LDAP_SCOPE_SUBORDINATE:
1527                                 scope = LDAP_SCOPE_SUBTREE;
1528                                 break;
1529                         }
1530                 }
1531
1532         } else {
1533                 rtag = ber_scanf( &tmpber, "{m" /*}*/, &dn );
1534         }
1535
1536         if( rtag == LBER_ERROR ) {
1537                 ld->ld_errno = LDAP_DECODING_ERROR;
1538                 return NULL;
1539         }
1540
1541         /* restore character zero'd out by ber_scanf*/
1542         dn.bv_val[dn.bv_len] = tmpber.ber_tag;
1543
1544         if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1545                 return NULL;
1546         }
1547
1548         if ( srv->lud_dn ) {
1549                 ber_str2bv( srv->lud_dn, 0, 0, &dn );
1550         }
1551
1552         if ( tag == LDAP_REQ_BIND ) {
1553                 rc = ber_printf( ber, "{it{iO" /*}}*/, msgid, tag, ver, &dn );
1554         } else if ( tag == LDAP_REQ_DELETE ) {
1555                 rc = ber_printf( ber, "{itON}", msgid, tag, &dn );
1556         } else if ( tag == LDAP_REQ_SEARCH ) {
1557                 rc = ber_printf( ber, "{it{Oe" /*}}*/, msgid, tag, &dn, scope );
1558         } else {
1559                 rc = ber_printf( ber, "{it{O" /*}}*/, msgid, tag, &dn );
1560         }
1561
1562         if ( rc == -1 ) {
1563                 ld->ld_errno = LDAP_ENCODING_ERROR;
1564                 ber_free( ber, 1 );
1565                 return NULL;
1566         }
1567
1568         if ( tag != LDAP_REQ_DELETE && (
1569                 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1570                 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1571             ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1572         {
1573                 ld->ld_errno = LDAP_ENCODING_ERROR;
1574                 ber_free( ber, 1 );
1575                 return NULL;
1576         }
1577
1578 #ifdef LDAP_DEBUG
1579         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1580                 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1581                     0, 0, 0 );
1582                 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1583         }
1584 #endif /* LDAP_DEBUG */
1585
1586         *type = tag;    /* return request type */
1587         return ber;
1588 }
1589
1590
1591 LDAPRequest *
1592 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1593 {
1594         LDAPRequest     *lr;
1595
1596 #ifdef LDAP_R_COMPILE
1597         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1598 #endif
1599         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1600                 if ( lr->lr_status == LDAP_REQST_COMPLETED ) {
1601                         continue;       /* Skip completed requests */
1602                 }
1603                 if ( msgid == lr->lr_msgid ) {
1604                         lr->lr_refcnt++;
1605                         break;
1606                 }
1607         }
1608 #ifdef LDAP_R_COMPILE
1609         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1610 #endif
1611
1612         return( lr );
1613 }
1614
1615 void
1616 ldap_return_request( LDAP *ld, LDAPRequest *lrx, int freeit )
1617 {
1618         LDAPRequest     *lr;
1619
1620 #ifdef LDAP_R_COMPILE
1621         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1622 #endif
1623         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1624                 if ( lr == lrx ) {
1625                         if ( lr->lr_refcnt > 0 ) {
1626                                 lr->lr_refcnt--;
1627
1628                         } else if ( lr->lr_refcnt < 0 ) {
1629                                 lr->lr_refcnt++;
1630                                 if ( lr->lr_refcnt == 0 ) {
1631                                         lr = NULL;
1632                                 }
1633                         }
1634                         break;
1635                 }
1636         }
1637         if ( lr == NULL ) {
1638                 ldap_free_request_int( ld, lrx );
1639
1640         } else if ( freeit ) {
1641                 ldap_free_request( ld, lrx );
1642         }
1643 #ifdef LDAP_R_COMPILE
1644         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1645 #endif
1646 }