]> git.sur5r.net Git - openldap/blob - libraries/libldap/request.c
plug leaks; cleanup
[openldap] / libraries / libldap / request.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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     if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) {
74                 ld->ld_errno = LDAP_NO_MEMORY;
75         }
76
77         return( ber );
78 }
79
80
81 void
82 ldap_set_ber_options( LDAP *ld, BerElement *ber )
83 {
84         ber->ber_options = ld->ld_lberoptions;
85 }
86
87
88 ber_int_t
89 ldap_send_initial_request(
90         LDAP *ld,
91         ber_tag_t msgtype,
92         const char *dn,
93         BerElement *ber,
94         ber_int_t msgid)
95 {
96         LDAPURLDesc     *servers;
97         int rc;
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
100
101         if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
102                 /* not connected yet */
103                 int rc = ldap_open_defconn( ld );
104
105                 if( rc < 0 ) {
106                         ber_free( ber, 1 );
107                         return( -1 );
108                 }
109
110                 Debug( LDAP_DEBUG_TRACE,
111                         "ldap_open_defconn: successful\n",
112                         0, 0, 0 );
113         }
114
115         {
116                 /*
117                  * use of DNS is turned off or this is an X.500 DN...
118                  * use our default connection
119                  */
120                 servers = NULL;
121         }       
122
123 #ifdef LDAP_CONNECTIONLESS
124         if (LDAP_IS_UDP(ld)) {
125                 if (msgtype == LDAP_REQ_BIND) {
126                         if (ld->ld_options.ldo_cldapdn)
127                                 ldap_memfree(ld->ld_options.ldo_cldapdn);
128                         ld->ld_options.ldo_cldapdn = ldap_strdup(dn);
129                         return 0;
130                 }
131                 if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH)
132                         return LDAP_PARAM_ERROR;
133         }
134 #endif
135 #ifdef LDAP_R_COMPILE
136         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
137 #endif
138         rc = ldap_send_server_request( ld, ber, msgid, NULL,
139                 servers, NULL, NULL );
140 #ifdef LDAP_R_COMPILE
141         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
142 #endif
143         if (servers)
144                 ldap_free_urllist(servers);
145         return(rc);
146 }
147
148
149 int
150 ldap_int_flush_request(
151         LDAP *ld,
152         LDAPRequest *lr )
153 {
154         LDAPConn *lc = lr->lr_conn;
155
156         if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
157                 if ( errno == EAGAIN ) {
158                         /* need to continue write later */
159                         lr->lr_status = LDAP_REQST_WRITING;
160                         ldap_mark_select_write( ld, lc->lconn_sb );
161                         ld->ld_errno = LDAP_BUSY;
162                         return -2;
163                 } else {
164                         ld->ld_errno = LDAP_SERVER_DOWN;
165                         ldap_free_request( ld, lr );
166                         ldap_free_connection( ld, lc, 0, 0 );
167                         return( -1 );
168                 }
169         } else {
170                 if ( lr->lr_parent == NULL ) {
171                         lr->lr_ber->ber_end = lr->lr_ber->ber_ptr;
172                         lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf;
173                 }
174                 lr->lr_status = LDAP_REQST_INPROGRESS;
175
176                 /* sent -- waiting for a response */
177                 ldap_mark_select_read( ld, lc->lconn_sb );
178         }
179         return 0;
180 }
181
182 int
183 ldap_send_server_request(
184         LDAP *ld,
185         BerElement *ber,
186         ber_int_t msgid,
187         LDAPRequest *parentreq,
188         LDAPURLDesc *srvlist,
189         LDAPConn *lc,
190         LDAPreqinfo *bind )
191 {
192         LDAPRequest     *lr;
193         int incparent, rc;
194
195         Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
196
197         incparent = 0;
198         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
199
200         if ( lc == NULL ) {
201                 if ( srvlist == NULL ) {
202                         lc = ld->ld_defconn;
203                 } else {
204                         if (( lc = find_connection( ld, srvlist, 1 )) ==
205                             NULL ) {
206                                 if ( (bind != NULL) && (parentreq != NULL) ) {
207                                         /* Remember the bind in the parent */
208                                         incparent = 1;
209                                         ++parentreq->lr_outrefcnt;
210                                 }
211                                 lc = ldap_new_connection( ld, srvlist, 0, 1, bind );
212                         }
213                 }
214         }
215
216         if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
217                 ber_free( ber, 1 );
218                 if ( ld->ld_errno == LDAP_SUCCESS ) {
219                         ld->ld_errno = LDAP_SERVER_DOWN;
220                 }
221                 if ( incparent ) {
222                         /* Forget about the bind */
223                         --parentreq->lr_outrefcnt; 
224                 }
225                 return( -1 );
226         }
227
228         use_connection( ld, lc );
229
230         /* If we still have an incomplete write, try to finish it before
231          * dealing with the new request. If we don't finish here, return
232          * LDAP_BUSY and let the caller retry later. We only allow a single
233          * request to be in WRITING state.
234          */
235         rc = 0;
236         if ( ld->ld_requests &&
237                 ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
238                 ldap_int_flush_request( ld, ld->ld_requests ) < 0 )
239         {
240                 rc = -1;
241         }
242         if ( rc ) return rc;
243
244         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
245         if ( lr == NULL ) {
246                 ld->ld_errno = LDAP_NO_MEMORY;
247                 ldap_free_connection( ld, lc, 0, 0 );
248                 ber_free( ber, 1 );
249                 if ( incparent ) {
250                         /* Forget about the bind */
251                         --parentreq->lr_outrefcnt; 
252                 }
253                 return( -1 );
254         } 
255         lr->lr_msgid = msgid;
256         lr->lr_status = LDAP_REQST_INPROGRESS;
257         lr->lr_res_errno = LDAP_SUCCESS;        /* optimistic */
258         lr->lr_ber = ber;
259         lr->lr_conn = lc;
260         if ( parentreq != NULL ) {      /* sub-request */
261                 if ( !incparent ) { 
262                         /* Increment if we didn't do it before the bind */
263                         ++parentreq->lr_outrefcnt;
264                 }
265                 lr->lr_origid = parentreq->lr_origid;
266                 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
267                 lr->lr_parent = parentreq;
268                 lr->lr_refnext = parentreq->lr_child;
269                 parentreq->lr_child = lr;
270         } else {                        /* original request */
271                 lr->lr_origid = lr->lr_msgid;
272         }
273
274         lr->lr_prev = NULL;
275         if (( lr->lr_next = ld->ld_requests ) != NULL ) {
276                 lr->lr_next->lr_prev = lr;
277         }
278         ld->ld_requests = lr;
279
280         ld->ld_errno = LDAP_SUCCESS;
281         if ( ldap_int_flush_request( ld, lr ) == -1 ) {
282                 msgid = -1;
283         }
284
285         return( msgid );
286 }
287
288 LDAPConn *
289 ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
290         int connect, LDAPreqinfo *bind )
291 {
292         LDAPConn        *lc;
293         LDAPURLDesc     *srv;
294
295         Debug( LDAP_DEBUG_TRACE, "ldap_new_connection %d %d %d\n",
296                 use_ldsb, connect, (bind != NULL) );
297         /*
298          * make a new LDAP server connection
299          * XXX open connection synchronously for now
300          */
301         lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ) );
302         if ( lc == NULL ) {
303                 ld->ld_errno = LDAP_NO_MEMORY;
304                 return( NULL );
305         }
306         
307         if ( use_ldsb ) {
308                 assert( ld->ld_sb );
309                 lc->lconn_sb = ld->ld_sb;
310
311         } else {
312                 lc->lconn_sb = ber_sockbuf_alloc();
313                 if ( lc->lconn_sb == NULL ) {
314                         LDAP_FREE( (char *)lc );
315                         ld->ld_errno = LDAP_NO_MEMORY;
316                         return( NULL );
317                 }
318         }
319
320         if ( connect ) {
321                 for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) {
322                         if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 )
323                         {
324                                 break;
325                         }
326                 }
327
328                 if ( srv == NULL ) {
329                         if ( !use_ldsb ) {
330                                 ber_sockbuf_free( lc->lconn_sb );
331                         }
332                         LDAP_FREE( (char *)lc );
333                         ld->ld_errno = LDAP_SERVER_DOWN;
334                         return( NULL );
335                 }
336
337                 lc->lconn_server = ldap_url_dup( srv );
338         }
339
340         lc->lconn_status = LDAP_CONNST_CONNECTED;
341         lc->lconn_next = ld->ld_conns;
342         ld->ld_conns = lc;
343
344         /*
345          * XXX for now, we always do a synchronous bind.  This will have
346          * to change in the long run...
347          */
348         if ( bind != NULL) {
349                 int             err = 0;
350                 LDAPConn        *savedefconn;
351
352                 /* Set flag to prevent additional referrals
353                  * from being processed on this
354                  * connection until the bind has completed
355                  */
356                 lc->lconn_rebind_inprogress = 1;
357                 /* V3 rebind function */
358                 if ( ld->ld_rebind_proc != NULL) {
359                         LDAPURLDesc     *srvfunc;
360                         if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) {
361                                 ld->ld_errno = LDAP_NO_MEMORY;
362                                 err = -1;
363                         } else {
364                                 savedefconn = ld->ld_defconn;
365                                 ++lc->lconn_refcnt;     /* avoid premature free */
366                                 ld->ld_defconn = lc;
367
368                                 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
369 #ifdef LDAP_R_COMPILE
370                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
371                                 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
372 #endif
373                                 err = (*ld->ld_rebind_proc)( ld,
374                                         bind->ri_url, bind->ri_request, bind->ri_msgid,
375                                         ld->ld_rebind_params );
376 #ifdef LDAP_R_COMPILE
377                                 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
378                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
379 #endif
380
381                                 ld->ld_defconn = savedefconn;
382                                 --lc->lconn_refcnt;
383
384                                 if ( err != 0 ) {
385                                         err = -1;
386                                         ldap_free_connection( ld, lc, 1, 0 );
387                                         lc = NULL;
388                                 }
389                                 ldap_free_urldesc( srvfunc );
390                         }
391                 } else {
392                         savedefconn = ld->ld_defconn;
393                         ++lc->lconn_refcnt;     /* avoid premature free */
394                         ld->ld_defconn = lc;
395
396                         Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
397 #ifdef LDAP_R_COMPILE
398                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
399                         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
400 #endif
401                         if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
402                                 err = -1;
403                         }
404 #ifdef LDAP_R_COMPILE
405                         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
406                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
407 #endif
408                         ld->ld_defconn = savedefconn;
409                         --lc->lconn_refcnt;
410
411                         if ( err != 0 ) {
412                                 ldap_free_connection( ld, lc, 1, 0 );
413                                 lc = NULL;
414                         }
415                 }
416                 if ( lc != NULL )
417                         lc->lconn_rebind_inprogress = 0;
418         }
419
420         return( lc );
421 }
422
423
424 static LDAPConn *
425 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
426 /*
427  * return an existing connection (if any) to the server srv
428  * if "any" is non-zero, check for any server in the "srv" chain
429  */
430 {
431         LDAPConn        *lc;
432         LDAPURLDesc     *lcu, *lsu;
433         int lcu_port, lsu_port;
434
435         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
436                 lcu = lc->lconn_server;
437                 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
438                         lcu->lud_port );
439
440                 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
441                         lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
442                                 lsu->lud_port );
443
444                         if ( strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
445                                 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
446                             && lsu->lud_host != NULL && *lsu->lud_host != '\0'
447                                 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0
448                             && lsu_port == lcu_port )
449                         {
450                                 return lc;
451                         }
452
453                         if ( !any ) break;
454                 }
455         }
456
457         return NULL;
458 }
459
460
461
462 static void
463 use_connection( LDAP *ld, LDAPConn *lc )
464 {
465         ++lc->lconn_refcnt;
466         lc->lconn_lastused = time( NULL );
467 }
468
469
470 void
471 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
472 {
473         LDAPConn        *tmplc, *prevlc;
474
475         Debug( LDAP_DEBUG_TRACE,
476                 "ldap_free_connection %d %d\n",
477                 force, unbind, 0 );
478
479         if ( force || --lc->lconn_refcnt <= 0 ) {
480                 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
481                         ldap_mark_select_clear( ld, lc->lconn_sb );
482                         if ( unbind ) {
483                                 ldap_send_unbind( ld, lc->lconn_sb, NULL, NULL );
484                         }
485                 }
486
487                 if ( lc->lconn_ber != NULL ) {
488                         ber_free( lc->lconn_ber, 1 );
489                 }
490
491                 ldap_int_sasl_close( ld, lc );
492
493                 prevlc = NULL;
494                 for ( tmplc = ld->ld_conns; tmplc != NULL;
495                     tmplc = tmplc->lconn_next ) {
496                         if ( tmplc == lc ) {
497                                 if ( prevlc == NULL ) {
498                                     ld->ld_conns = tmplc->lconn_next;
499                                 } else {
500                                     prevlc->lconn_next = tmplc->lconn_next;
501                                 }
502                                 break;
503                         }
504                         prevlc = tmplc;
505                 }
506                 ldap_free_urllist( lc->lconn_server );
507 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
508                 if ( lc->lconn_krbinstance != NULL ) {
509                         LDAP_FREE( lc->lconn_krbinstance );
510                 }
511 #endif
512
513                 /* FIXME: is this at all possible? */
514                 if ( force ) {
515                         LDAPRequest     *lr;
516
517 #ifdef LDAP_R_COMPILE
518                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
519 #endif
520                         for ( lr = ld->ld_requests; lr; ) {
521                                 LDAPRequest     *lr_next = lr->lr_next;
522
523                                 if ( lr->lr_conn == lc ) {
524                                         ldap_free_request_int( ld, lr );
525                                 }
526
527                                 lr = lr_next;
528                         }
529 #ifdef LDAP_R_COMPILE
530                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
531 #endif
532                 }
533                 if ( lc->lconn_sb != ld->ld_sb ) {
534                         ber_sockbuf_free( lc->lconn_sb );
535                 }
536                 if ( lc->lconn_rebind_queue != NULL) {
537                         int i;
538                         for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
539                                 LDAP_VFREE( lc->lconn_rebind_queue[i] );
540                         }
541                         LDAP_FREE( lc->lconn_rebind_queue );
542                 }
543                 LDAP_FREE( lc );
544                 Debug( LDAP_DEBUG_TRACE,
545                         "ldap_free_connection: actually freed\n",
546                         0, 0, 0 );
547         } else {
548                 lc->lconn_lastused = time( NULL );
549                 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
550                                 lc->lconn_refcnt, 0, 0 );
551         }
552 }
553
554
555 #ifdef LDAP_DEBUG
556 void
557 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
558 {
559         LDAPConn        *lc;
560         char            timebuf[32];
561
562         fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
563         for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
564                 if ( lc->lconn_server != NULL ) {
565                         fprintf( stderr, "* host: %s  port: %d%s\n",
566                             ( lc->lconn_server->lud_host == NULL ) ? "(null)"
567                             : lc->lconn_server->lud_host,
568                             lc->lconn_server->lud_port, ( lc->lconn_sb ==
569                             ld->ld_sb ) ? "  (default)" : "" );
570                 }
571                 fprintf( stderr, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
572                     ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
573                     "NeedSocket" : ( lc->lconn_status ==
574                     LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
575                 fprintf( stderr, "  last used: %s",
576                     ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
577                 if( lc->lconn_rebind_inprogress ) {
578                         fprintf( stderr, "  rebind in progress\n");
579                         if( lc->lconn_rebind_queue != NULL) {
580                                 int i = 0;
581                                 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
582                                         int j = 0;
583                                         for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
584                                                 fprintf( stderr, "    queue %d entry %d - %s\n",
585                                                         i, j, lc->lconn_rebind_queue[i][j]);
586                                         }
587                                 }
588                         } else {
589                                 fprintf( stderr, "    queue is empty\n");
590                         }
591                 }
592                 fprintf(stderr, "\n");
593                 if ( !all ) {
594                         break;
595                 }
596         }
597 }
598
599
600 void
601 ldap_dump_requests_and_responses( LDAP *ld )
602 {
603         LDAPRequest     *lr;
604         LDAPMessage     *lm, *l;
605
606 #ifdef LDAP_R_COMPILE
607         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
608 #endif
609         fprintf( stderr, "** Outstanding Requests:\n" );
610         if (( lr = ld->ld_requests ) == NULL ) {
611                 fprintf( stderr, "   Empty\n" );
612         }
613         for ( ; lr != NULL; lr = lr->lr_next ) {
614             fprintf( stderr, " * msgid %d,  origid %d, status %s\n",
615                 lr->lr_msgid, lr->lr_origid,
616                 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
617                 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
618                 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
619                 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
620                 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
621             fprintf( stderr, "   outstanding referrals %d, parent count %d\n",
622                     lr->lr_outrefcnt, lr->lr_parentcnt );
623         }
624 #ifdef LDAP_R_COMPILE
625         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
626 #endif
627         fprintf( stderr, "** Response Queue:\n" );
628         if (( lm = ld->ld_responses ) == NULL ) {
629                 fprintf( stderr, "   Empty\n" );
630         }
631         for ( ; lm != NULL; lm = lm->lm_next ) {
632                 fprintf( stderr, " * msgid %d,  type %lu\n",
633                     lm->lm_msgid, (unsigned long) lm->lm_msgtype );
634                 if (( l = lm->lm_chain ) != NULL ) {
635                         fprintf( stderr, "   chained responses:\n" );
636                         for ( ; l != NULL; l = l->lm_chain ) {
637                                 fprintf( stderr,
638                                     "  * msgid %d,  type %lu\n",
639                                     l->lm_msgid,
640                                     (unsigned long) l->lm_msgtype );
641                         }
642                 }
643         }
644 }
645 #endif /* LDAP_DEBUG */
646
647 static void
648 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
649 {
650         if ( lr->lr_prev == NULL ) {
651                 /* free'ing the first request? */
652                 assert( ld->ld_requests == lr );
653                 ld->ld_requests = lr->lr_next;
654
655         } else {
656                 lr->lr_prev->lr_next = lr->lr_next;
657         }
658
659         if ( lr->lr_next != NULL ) {
660                 lr->lr_next->lr_prev = lr->lr_prev;
661         }
662
663         if ( lr->lr_ber != NULL ) {
664                 ber_free( lr->lr_ber, 1 );
665         }
666
667         if ( lr->lr_res_error != NULL ) {
668                 LDAP_FREE( lr->lr_res_error );
669         }
670
671         if ( lr->lr_res_matched != NULL ) {
672                 LDAP_FREE( lr->lr_res_matched );
673         }
674
675         LDAP_FREE( lr );
676 }
677
678 void
679 ldap_free_request( LDAP *ld, LDAPRequest *lr )
680 {
681         LDAPRequest     **ttmplr;
682
683         Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
684                 lr->lr_origid, lr->lr_msgid, 0 );
685
686         /* free all referrals (child requests) */
687         while ( lr->lr_child )
688                 ldap_free_request( ld, lr->lr_child );
689
690         if ( lr->lr_parent != NULL ) {
691                 --lr->lr_parent->lr_outrefcnt;
692                 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext ); 
693                 if ( *ttmplr == lr )  
694                         *ttmplr = lr->lr_refnext;
695         }
696         ldap_free_request_int( ld, lr );
697 }
698
699 /*
700  * call first time with *cntp = -1
701  * when returns *cntp == -1, no referrals are left
702  *
703  * NOTE: may replace *refsp, or shuffle the contents
704  * of the original array.
705  */
706 static int ldap_int_nextref(
707         LDAP                    *ld,
708         char                    ***refsp,
709         int                     *cntp,
710         void                    *params )
711 {
712         assert( refsp != NULL );
713         assert( *refsp != NULL );
714         assert( cntp != NULL );
715
716         if ( *cntp < -1 ) {
717                 *cntp = -1;
718                 return -1;
719         }
720
721         (*cntp)++;
722
723         if ( (*refsp)[ *cntp ] == NULL ) {
724                 *cntp = -1;
725         }
726
727         return 0;
728 }
729
730 /*
731  * Chase v3 referrals
732  *
733  * Parameters:
734  *  (IN) ld = LDAP connection handle
735  *  (IN) lr = LDAP Request structure
736  *  (IN) refs = array of pointers to referral strings that we will chase
737  *              The array will be free'd by this function when no longer needed
738  *  (IN) sref != 0 if following search reference
739  *  (OUT) errstrp = Place to return a string of referrals which could not be followed
740  *  (OUT) hadrefp = 1 if sucessfully followed referral
741  *
742  * Return value - number of referrals followed
743  */
744 int
745 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
746 {
747         char            *unfollowed;
748         int              unfollowedcnt = 0;
749         LDAPRequest     *origreq;
750         LDAPURLDesc     *srv = NULL;
751         BerElement      *ber;
752         char            **refarray = NULL;
753         LDAPConn        *lc;
754         int                      rc, count, i, j, id;
755         LDAPreqinfo  rinfo;
756
757         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
758         *hadrefp = 0;
759
760         Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
761
762         unfollowed = NULL;
763         rc = count = 0;
764
765         /* If no referrals in array, return */
766         if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
767                 rc = 0;
768                 goto done;
769         }
770
771         /* Check for hop limit exceeded */
772         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
773                 Debug( LDAP_DEBUG_ANY,
774                     "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
775                 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
776             rc = -1;
777                 goto done;
778         }
779
780         /* find original request */
781         for ( origreq = lr;
782                 origreq->lr_parent != NULL;
783                 origreq = origreq->lr_parent )
784         {
785                 /* empty */ ;
786         }
787
788         refarray = refs;
789         refs = NULL;
790
791         if ( ld->ld_nextref_proc == NULL ) {
792                 ld->ld_nextref_proc = ldap_int_nextref;
793         }
794
795         /* parse out & follow referrals */
796         i = -1;
797         for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
798                         i != -1;
799                         ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
800         {
801
802                 /* Parse the referral URL */
803                 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
804                         ld->ld_errno = rc;
805                         rc = -1;
806                         goto done;
807                 }
808
809                 if( srv->lud_crit_exts ) {
810                         /* we do not support any extensions */
811                         ld->ld_errno = LDAP_NOT_SUPPORTED;
812                         rc = -1;
813                         goto done;
814                 }
815
816                 /* treat ldap://hostpart and ldap://hostpart/ the same */
817                 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
818                         LDAP_FREE( srv->lud_dn );
819                         srv->lud_dn = NULL;
820                 }
821
822                 /* check connection for re-bind in progress */
823                 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
824                         if( lc->lconn_rebind_inprogress) {
825                                 /* We are already chasing a referral or search reference and a
826                                  * bind on that connection is in progress.  We must queue
827                                  * referrals on that connection, so we don't get a request
828                                  * going out before the bind operation completes. This happens
829                                  * if two search references come in one behind the other
830                                  * for the same server with different contexts.
831                                  */
832                                 Debug( LDAP_DEBUG_TRACE,
833                                         "ldap_chase_v3referrals: queue referral \"%s\"\n",
834                                         refarray[i], 0, 0);
835                                 if( lc->lconn_rebind_queue == NULL ) {
836                                         /* Create a referral list */
837                                         lc->lconn_rebind_queue =
838                                                 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
839
840                                         if( lc->lconn_rebind_queue == NULL) {
841                                                 ld->ld_errno = LDAP_NO_MEMORY;
842                                                 rc = -1;
843                                                 goto done;
844                                         }
845
846                                         lc->lconn_rebind_queue[0] = refarray;
847                                         lc->lconn_rebind_queue[1] = NULL;
848                                         refarray = NULL;
849
850                                 } else {
851                                         /* Count how many referral arrays we already have */
852                                         for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
853                                                 /* empty */;
854                                         }
855
856                                         /* Add the new referral to the list */
857                                         lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
858                                                 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
859
860                                         if( lc->lconn_rebind_queue == NULL ) {
861                                                 ld->ld_errno = LDAP_NO_MEMORY;
862                                                 rc = -1;
863                                                 goto done;
864                                         }
865                                         lc->lconn_rebind_queue[j] = refarray;
866                                         lc->lconn_rebind_queue[j+1] = NULL;
867                                         refarray = NULL;
868                                 }
869
870                                 /* We have queued the referral/reference, now just return */
871                                 rc = 0;
872                                 *hadrefp = 1;
873                                 count = 1; /* Pretend we already followed referral */
874                                 goto done;
875                         }
876                 } 
877                 /* Re-encode the request with the new starting point of the search.
878                  * Note: In the future we also need to replace the filter if one
879                  * was provided with the search reference
880                  */
881
882                 /* For references we don't want old dn if new dn empty */
883                 if ( sref && srv->lud_dn == NULL ) {
884                         srv->lud_dn = LDAP_STRDUP( "" );
885                 }
886
887                 LDAP_NEXT_MSGID( ld, id );
888                 ber = re_encode_request( ld, origreq->lr_ber, id,
889                         sref, srv, &rinfo.ri_request );
890
891                 if( ber == NULL ) {
892                         ld->ld_errno = LDAP_ENCODING_ERROR;
893                         rc = -1;
894                         goto done;
895                 }
896
897                 Debug( LDAP_DEBUG_TRACE,
898                         "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
899                         lr->lr_msgid, refarray[i], 0);
900
901                 /* Send the new request to the server - may require a bind */
902                 rinfo.ri_msgid = origreq->lr_origid;
903                 rinfo.ri_url = refarray[i];
904 #ifdef LDAP_R_COMPILE
905                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
906 #endif
907                 rc = ldap_send_server_request( ld, ber, id,
908                         origreq, srv, NULL, &rinfo );
909 #ifdef LDAP_R_COMPILE
910                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
911 #endif
912                 if ( rc < 0 ) {
913                         /* Failure, try next referral in the list */
914                         Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n", 
915                                 refarray[i], ldap_err2string( ld->ld_errno ), 0);
916                         unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]);
917                         ldap_free_urllist(srv);
918                         srv = NULL;
919                 } else {
920                         /* Success, no need to try this referral list further */
921                         rc = 0;
922                         ++count;
923                         *hadrefp = 1;
924
925                         /* check if there is a queue of referrals that came in during bind */
926                         if( lc == NULL) {
927                                 if (( lc = find_connection( ld, srv, 1 )) == NULL ) {
928                                         ld->ld_errno = LDAP_OPERATIONS_ERROR;
929                                         rc = -1;
930                                         goto done;
931                                 }
932                         }
933
934                         if( lc->lconn_rebind_queue != NULL) {
935                                 /* Release resources of previous list */
936                                 LDAP_VFREE(refarray);
937                                 refarray = NULL;
938                                 ldap_free_urllist(srv);
939                                 srv = NULL;
940
941                                 /* Pull entries off end of queue so list always null terminated */
942                                 for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
943                                         ;
944                                 }
945                                 refarray = lc->lconn_rebind_queue[j-1];
946                                 lc->lconn_rebind_queue[j-1] = NULL;
947                                 /* we pulled off last entry from queue, free queue */
948                                 if ( j == 1 ) {
949                                         LDAP_FREE( lc->lconn_rebind_queue);
950                                         lc->lconn_rebind_queue = NULL;
951                                 }
952                                 /* restart the loop the with new referral list */
953                                 i = -1;
954                                 continue;
955                         }
956                         break; /* referral followed, break out of for loop */
957                 }
958         } /* end for loop */
959 done:
960         LDAP_VFREE( refarray );
961         ldap_free_urllist( srv );
962         LDAP_FREE( *errstrp );
963         
964         if( rc == 0 ) {
965                 *errstrp = NULL;
966                 LDAP_FREE( unfollowed );
967                 return count;
968         } else {
969                 ld->ld_errno = LDAP_REFERRAL;
970                 *errstrp = unfollowed;
971                 return rc;
972         }
973 }
974
975 /*
976  * XXX merging of errors in this routine needs to be improved
977  */
978 int
979 ldap_chase_referrals( LDAP *ld,
980         LDAPRequest *lr,
981         char **errstrp,
982         int sref,
983         int *hadrefp )
984 {
985         int             rc, count, id;
986         unsigned        len;
987         char            *p, *ref, *unfollowed;
988         LDAPRequest     *origreq;
989         LDAPURLDesc     *srv;
990         BerElement      *ber;
991         LDAPreqinfo  rinfo;
992
993         Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
994
995         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
996         *hadrefp = 0;
997
998         if ( *errstrp == NULL ) {
999                 return( 0 );
1000         }
1001
1002         len = strlen( *errstrp );
1003         for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1004                 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1005                         *p = '\0';
1006                         p += LDAP_REF_STR_LEN;
1007                         break;
1008                 }
1009         }
1010
1011         if ( len < LDAP_REF_STR_LEN ) {
1012                 return( 0 );
1013         }
1014
1015         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1016                 Debug( LDAP_DEBUG_ANY,
1017                     "more than %d referral hops (dropping)\n",
1018                     ld->ld_refhoplimit, 0, 0 );
1019                     /* XXX report as error in ld->ld_errno? */
1020                     return( 0 );
1021         }
1022
1023         /* find original request */
1024         for ( origreq = lr; origreq->lr_parent != NULL;
1025              origreq = origreq->lr_parent ) {
1026                 /* empty */;
1027         }
1028
1029         unfollowed = NULL;
1030         rc = count = 0;
1031
1032         /* parse out & follow referrals */
1033         for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1034                 if (( p = strchr( ref, '\n' )) != NULL ) {
1035                         *p++ = '\0';
1036                 } else {
1037                         p = NULL;
1038                 }
1039
1040                 rc = ldap_url_parse_ext( ref, &srv );
1041
1042                 if ( rc != LDAP_URL_SUCCESS ) {
1043                         Debug( LDAP_DEBUG_TRACE,
1044                             "ignoring unknown referral <%s>\n", ref, 0, 0 );
1045                         rc = ldap_append_referral( ld, &unfollowed, ref );
1046                         *hadrefp = 1;
1047                         continue;
1048                 }
1049
1050                 if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1051                         LDAP_FREE( srv->lud_dn );
1052                         srv->lud_dn = NULL;
1053                 }
1054
1055                 Debug( LDAP_DEBUG_TRACE,
1056                     "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1057
1058                 *hadrefp = 1;
1059
1060                 LDAP_NEXT_MSGID( ld, id );
1061                 ber = re_encode_request( ld, origreq->lr_ber,
1062                     id, sref, srv, &rinfo.ri_request );
1063
1064                 if( ber == NULL ) {
1065                         return -1 ;
1066                 }
1067
1068                 /* copy the complete referral for rebind process */
1069                 rinfo.ri_url = LDAP_STRDUP( ref );
1070
1071                 rinfo.ri_msgid = origreq->lr_origid;
1072
1073 #ifdef LDAP_R_COMPILE
1074                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1075 #endif
1076                 rc = ldap_send_server_request( ld, ber, id,
1077                         lr, srv, NULL, &rinfo );
1078 #ifdef LDAP_R_COMPILE
1079                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1080 #endif
1081
1082                 LDAP_FREE( rinfo.ri_url );
1083
1084                 if( rc >= 0 ) {
1085                         ++count;
1086                 } else {
1087                         Debug( LDAP_DEBUG_ANY,
1088                             "Unable to chase referral (%s)\n", 
1089                             ldap_err2string( ld->ld_errno ), 0, 0 );
1090                         rc = ldap_append_referral( ld, &unfollowed, ref );
1091                 }
1092
1093                 ldap_free_urllist(srv);
1094         }
1095
1096         LDAP_FREE( *errstrp );
1097         *errstrp = unfollowed;
1098
1099         return(( rc == 0 ) ? count : rc );
1100 }
1101
1102
1103 int
1104 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1105 {
1106         int     first;
1107
1108         if ( *referralsp == NULL ) {
1109                 first = 1;
1110                 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1111                     + 1 );
1112         } else {
1113                 first = 0;
1114                 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1115                     strlen( *referralsp ) + strlen( s ) + 2 );
1116         }
1117
1118         if ( *referralsp == NULL ) {
1119                 ld->ld_errno = LDAP_NO_MEMORY;
1120                 return( -1 );
1121         }
1122
1123         if ( first ) {
1124                 strcpy( *referralsp, LDAP_REF_STR );
1125         } else {
1126                 strcat( *referralsp, "\n" );
1127         }
1128         strcat( *referralsp, s );
1129
1130         return( 0 );
1131 }
1132
1133
1134
1135 static BerElement *
1136 re_encode_request( LDAP *ld,
1137         BerElement *origber,
1138         ber_int_t msgid,
1139         int sref,
1140         LDAPURLDesc *srv,
1141         int *type )
1142 {
1143         /*
1144          * XXX this routine knows way too much about how the lber library works!
1145          */
1146         ber_int_t       along;
1147         ber_tag_t       tag;
1148         ber_tag_t       rtag;
1149         ber_int_t       ver;
1150         ber_int_t       scope;
1151         int             rc;
1152         BerElement      tmpber, *ber;
1153         char            *orig_dn;
1154         char            *dn;
1155
1156         Debug( LDAP_DEBUG_TRACE,
1157             "re_encode_request: new msgid %ld, new dn <%s>\n",
1158             (long) msgid,
1159                 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1160
1161         tmpber = *origber;
1162
1163         /*
1164          * all LDAP requests are sequences that start with a message id.
1165          * For all except delete, this is followed by a sequence that is
1166          * tagged with the operation code.  For delete, the provided DN
1167          * is not wrapped by a sequence.
1168          */
1169         rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1170
1171         if ( rtag == LBER_ERROR ) {
1172                 ld->ld_errno = LDAP_DECODING_ERROR;
1173                 return( NULL );
1174         }
1175
1176         assert( tag != 0);
1177         if ( tag == LDAP_REQ_BIND ) {
1178                 /* bind requests have a version number before the DN & other stuff */
1179                 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1180
1181         } else if ( tag == LDAP_REQ_DELETE ) {
1182                 /* delete requests don't have a DN wrapping sequence */
1183                 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1184
1185         } else if ( tag == LDAP_REQ_SEARCH ) {
1186                 /* search requests need to be re-scope-ed */
1187                 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1188
1189                 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1190                         /* use the scope provided in reference */
1191                         scope = srv->lud_scope;
1192
1193                 } else if ( sref ) {
1194                         /* use scope implied by previous operation
1195                          *   base -> base
1196                          *   one -> base
1197                          *   subtree -> subtree
1198                          *   subordinate -> subtree
1199                          */
1200                         switch( scope ) {
1201                         default:
1202                         case LDAP_SCOPE_BASE:
1203                         case LDAP_SCOPE_ONELEVEL:
1204                                 scope = LDAP_SCOPE_BASE;
1205                                 break;
1206                         case LDAP_SCOPE_SUBTREE:
1207 #ifdef LDAP_SCOPE_SUBORDINATE
1208                         case LDAP_SCOPE_SUBORDINATE:
1209 #endif
1210                                 scope = LDAP_SCOPE_SUBTREE;
1211                                 break;
1212                         }
1213                 }
1214
1215         } else {
1216                 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1217         }
1218
1219         if( rtag == LBER_ERROR ) {
1220                 ld->ld_errno = LDAP_DECODING_ERROR;
1221                 return NULL;
1222         }
1223
1224         if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1225                 return NULL;
1226         }
1227
1228         if ( srv->lud_dn == NULL ) {
1229                 dn = orig_dn;
1230         } else {
1231                 dn = srv->lud_dn;
1232         }
1233
1234         if ( tag == LDAP_REQ_BIND ) {
1235                 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1236         } else if ( tag == LDAP_REQ_DELETE ) {
1237                 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1238         } else if ( tag == LDAP_REQ_SEARCH ) {
1239                 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1240         } else {
1241                 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1242         }
1243
1244         LDAP_FREE( orig_dn );
1245
1246         if ( rc == -1 ) {
1247                 ld->ld_errno = LDAP_ENCODING_ERROR;
1248                 ber_free( ber, 1 );
1249                 return NULL;
1250         }
1251
1252         if ( tag != LDAP_REQ_DELETE && (
1253                 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1254                 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1255             ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1256         {
1257                 ld->ld_errno = LDAP_ENCODING_ERROR;
1258                 ber_free( ber, 1 );
1259                 return NULL;
1260         }
1261
1262 #ifdef LDAP_DEBUG
1263         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1264                 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1265                     0, 0, 0 );
1266                 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1267         }
1268 #endif /* LDAP_DEBUG */
1269
1270         *type = tag;    /* return request type */
1271         return ber;
1272 }
1273
1274
1275 LDAPRequest *
1276 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1277 {
1278         LDAPRequest     *lr;
1279
1280 #ifdef LDAP_R_COMPILE
1281         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1282 #endif
1283         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1284                 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1285                         continue;       /* Skip completed requests */
1286                 }
1287                 if ( msgid == lr->lr_msgid ) {
1288                         break;
1289                 }
1290         }
1291 #ifdef LDAP_R_COMPILE
1292         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1293 #endif
1294
1295         return( lr );
1296 }
1297
1298