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