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