]> git.sur5r.net Git - openldap/blob - libraries/libldap/request.c
02515ff880cffc41171526bfa75646890ad7e197
[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                         lc = find_connection( ld, srvlist, 1 );
205                         if ( lc == 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;
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 != NULL );
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
361                         srvfunc = ldap_url_dup( srvlist );
362                         if ( srvfunc == NULL ) {
363                                 ld->ld_errno = LDAP_NO_MEMORY;
364                                 err = -1;
365                         } else {
366                                 savedefconn = ld->ld_defconn;
367                                 ++lc->lconn_refcnt;     /* avoid premature free */
368                                 ld->ld_defconn = lc;
369
370                                 Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0);
371 #ifdef LDAP_R_COMPILE
372                                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
373                                 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
374 #endif
375                                 err = (*ld->ld_rebind_proc)( ld,
376                                         bind->ri_url, bind->ri_request, bind->ri_msgid,
377                                         ld->ld_rebind_params );
378 #ifdef LDAP_R_COMPILE
379                                 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
380                                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
381 #endif
382
383                                 ld->ld_defconn = savedefconn;
384                                 --lc->lconn_refcnt;
385
386                                 if ( err != 0 ) {
387                                         err = -1;
388                                         ldap_free_connection( ld, lc, 1, 0 );
389                                         lc = NULL;
390                                 }
391                                 ldap_free_urldesc( srvfunc );
392                         }
393                 } else {
394                         savedefconn = ld->ld_defconn;
395                         ++lc->lconn_refcnt;     /* avoid premature free */
396                         ld->ld_defconn = lc;
397
398                         Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0);
399 #ifdef LDAP_R_COMPILE
400                         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
401                         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
402 #endif
403                         if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
404                                 err = -1;
405                         }
406 #ifdef LDAP_R_COMPILE
407                         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
408                         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
409 #endif
410                         ld->ld_defconn = savedefconn;
411                         --lc->lconn_refcnt;
412
413                         if ( err != 0 ) {
414                                 ldap_free_connection( ld, lc, 1, 0 );
415                                 lc = NULL;
416                         }
417                 }
418                 if ( lc != NULL )
419                         lc->lconn_rebind_inprogress = 0;
420         }
421
422         return( lc );
423 }
424
425
426 static LDAPConn *
427 find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
428 /*
429  * return an existing connection (if any) to the server srv
430  * if "any" is non-zero, check for any server in the "srv" chain
431  */
432 {
433         LDAPConn        *lc;
434         LDAPURLDesc     *lcu, *lsu;
435         int lcu_port, lsu_port;
436
437         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
438                 lcu = lc->lconn_server;
439                 lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
440                         lcu->lud_port );
441
442                 for ( lsu = srv; lsu != NULL; lsu = lsu->lud_next ) {
443                         lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
444                                 lsu->lud_port );
445
446                         if ( strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
447                                 && lcu->lud_host != NULL && *lcu->lud_host != '\0'
448                             && lsu->lud_host != NULL && *lsu->lud_host != '\0'
449                                 && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0
450                             && lsu_port == lcu_port )
451                         {
452                                 return lc;
453                         }
454
455                         if ( !any ) break;
456                 }
457         }
458
459         return NULL;
460 }
461
462
463
464 static void
465 use_connection( LDAP *ld, LDAPConn *lc )
466 {
467         ++lc->lconn_refcnt;
468         lc->lconn_lastused = time( NULL );
469 }
470
471
472 void
473 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
474 {
475         LDAPConn        *tmplc, *prevlc;
476
477         Debug( LDAP_DEBUG_TRACE,
478                 "ldap_free_connection %d %d\n",
479                 force, unbind, 0 );
480
481         if ( force || --lc->lconn_refcnt <= 0 ) {
482                 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
483                         ldap_mark_select_clear( ld, lc->lconn_sb );
484                         if ( unbind ) {
485                                 ldap_send_unbind( ld, lc->lconn_sb,
486                                                 NULL, NULL );
487                         }
488                 }
489
490                 if ( lc->lconn_ber != NULL ) {
491                         ber_free( lc->lconn_ber, 1 );
492                 }
493
494                 ldap_int_sasl_close( ld, lc );
495
496                 prevlc = NULL;
497                 for ( tmplc = ld->ld_conns;
498                         tmplc != NULL;
499                         tmplc = tmplc->lconn_next )
500                 {
501                         if ( tmplc == lc ) {
502                                 if ( prevlc == NULL ) {
503                                     ld->ld_conns = tmplc->lconn_next;
504                                 } else {
505                                     prevlc->lconn_next = tmplc->lconn_next;
506                                 }
507                                 break;
508                         }
509                         prevlc = tmplc;
510                 }
511                 ldap_free_urllist( lc->lconn_server );
512 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
513                 if ( lc->lconn_krbinstance != NULL ) {
514                         LDAP_FREE( lc->lconn_krbinstance );
515                 }
516 #endif
517
518                 /* FIXME: is this at all possible? */
519                 if ( force ) {
520                         LDAPRequest     *lr;
521
522                         for ( lr = ld->ld_requests; lr; ) {
523                                 LDAPRequest     *lr_next = lr->lr_next;
524
525                                 if ( lr->lr_conn == lc ) {
526                                         ldap_free_request_int( ld, lr );
527                                 }
528
529                                 lr = lr_next;
530                         }
531                 }
532                 if ( lc->lconn_sb != ld->ld_sb ) {
533                         ber_sockbuf_free( lc->lconn_sb );
534                 }
535                 if ( lc->lconn_rebind_queue != NULL) {
536                         int i;
537                         for( i = 0; lc->lconn_rebind_queue[i] != NULL; i++ ) {
538                                 LDAP_VFREE( lc->lconn_rebind_queue[i] );
539                         }
540                         LDAP_FREE( lc->lconn_rebind_queue );
541                 }
542                 LDAP_FREE( lc );
543                 Debug( LDAP_DEBUG_TRACE,
544                         "ldap_free_connection: actually freed\n",
545                         0, 0, 0 );
546         } else {
547                 lc->lconn_lastused = time( NULL );
548                 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
549                                 lc->lconn_refcnt, 0, 0 );
550         }
551 }
552
553
554 #ifdef LDAP_DEBUG
555 void
556 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
557 {
558         LDAPConn        *lc;
559         char            timebuf[32];
560
561         fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
562         for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
563                 if ( lc->lconn_server != NULL ) {
564                         fprintf( stderr, "* host: %s  port: %d%s\n",
565                             ( lc->lconn_server->lud_host == NULL ) ? "(null)"
566                             : lc->lconn_server->lud_host,
567                             lc->lconn_server->lud_port, ( lc->lconn_sb ==
568                             ld->ld_sb ) ? "  (default)" : "" );
569                 }
570                 fprintf( stderr, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
571                     ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
572                     "NeedSocket" : ( lc->lconn_status ==
573                     LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
574                 fprintf( stderr, "  last used: %s",
575                     ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
576                 if( lc->lconn_rebind_inprogress ) {
577                         fprintf( stderr, "  rebind in progress\n");
578                         if( lc->lconn_rebind_queue != NULL) {
579                                 int i = 0;
580                                 for( ;lc->lconn_rebind_queue[i] != NULL; i++) {
581                                         int j = 0;
582                                         for( ;lc->lconn_rebind_queue[i][j] != 0; j++) {
583                                                 fprintf( stderr, "    queue %d entry %d - %s\n",
584                                                         i, j, lc->lconn_rebind_queue[i][j]);
585                                         }
586                                 }
587                         } else {
588                                 fprintf( stderr, "    queue is empty\n");
589                         }
590                 }
591                 fprintf(stderr, "\n");
592                 if ( !all ) {
593                         break;
594                 }
595         }
596 }
597
598
599 void
600 ldap_dump_requests_and_responses( LDAP *ld )
601 {
602         LDAPRequest     *lr;
603         LDAPMessage     *lm, *l;
604
605 #ifdef LDAP_R_COMPILE
606         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
607 #endif
608         fprintf( stderr, "** Outstanding Requests:\n" );
609         if (( lr = ld->ld_requests ) == NULL ) {
610                 fprintf( stderr, "   Empty\n" );
611         }
612         for ( ; lr != NULL; lr = lr->lr_next ) {
613             fprintf( stderr, " * msgid %d,  origid %d, status %s\n",
614                 lr->lr_msgid, lr->lr_origid,
615                 ( lr->lr_status == LDAP_REQST_INPROGRESS ) ? "InProgress" :
616                 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
617                 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
618                 ( lr->lr_status == LDAP_REQST_WRITING) ? "Writing" :
619                 ( lr->lr_status == LDAP_REQST_COMPLETED ? "Request Completed" : "Invalid Status"));
620             fprintf( stderr, "   outstanding referrals %d, parent count %d\n",
621                     lr->lr_outrefcnt, lr->lr_parentcnt );
622         }
623 #ifdef LDAP_R_COMPILE
624         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
625 #endif
626         fprintf( stderr, "** Response Queue:\n" );
627         if (( lm = ld->ld_responses ) == NULL ) {
628                 fprintf( stderr, "   Empty\n" );
629         }
630         for ( ; lm != NULL; lm = lm->lm_next ) {
631                 fprintf( stderr, " * msgid %d,  type %lu\n",
632                     lm->lm_msgid, (unsigned long) lm->lm_msgtype );
633                 if (( l = lm->lm_chain ) != NULL ) {
634                         fprintf( stderr, "   chained responses:\n" );
635                         for ( ; l != NULL; l = l->lm_chain ) {
636                                 fprintf( stderr,
637                                     "  * msgid %d,  type %lu\n",
638                                     l->lm_msgid,
639                                     (unsigned long) l->lm_msgtype );
640                         }
641                 }
642         }
643 }
644 #endif /* LDAP_DEBUG */
645
646 static void
647 ldap_free_request_int( LDAP *ld, LDAPRequest *lr )
648 {
649         if ( lr->lr_prev == NULL ) {
650                 /* free'ing the first request? */
651                 assert( ld->ld_requests == lr );
652                 ld->ld_requests = lr->lr_next;
653
654         } else {
655                 lr->lr_prev->lr_next = lr->lr_next;
656         }
657
658         if ( lr->lr_next != NULL ) {
659                 lr->lr_next->lr_prev = lr->lr_prev;
660         }
661
662         if ( lr->lr_ber != NULL ) {
663                 ber_free( lr->lr_ber, 1 );
664         }
665
666         if ( lr->lr_res_error != NULL ) {
667                 LDAP_FREE( lr->lr_res_error );
668         }
669
670         if ( lr->lr_res_matched != NULL ) {
671                 LDAP_FREE( lr->lr_res_matched );
672         }
673
674         LDAP_FREE( lr );
675 }
676
677 void
678 ldap_free_request( LDAP *ld, LDAPRequest *lr )
679 {
680         LDAPRequest     **ttmplr;
681
682         Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
683                 lr->lr_origid, lr->lr_msgid, 0 );
684
685         /* free all referrals (child requests) */
686         while ( lr->lr_child )
687                 ldap_free_request( ld, lr->lr_child );
688
689         if ( lr->lr_parent != NULL ) {
690                 --lr->lr_parent->lr_outrefcnt;
691                 for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext ); 
692                 if ( *ttmplr == lr )  
693                         *ttmplr = lr->lr_refnext;
694         }
695         ldap_free_request_int( ld, lr );
696 }
697
698 /*
699  * call first time with *cntp = -1
700  * when returns *cntp == -1, no referrals are left
701  *
702  * NOTE: may replace *refsp, or shuffle the contents
703  * of the original array.
704  */
705 static int ldap_int_nextref(
706         LDAP                    *ld,
707         char                    ***refsp,
708         int                     *cntp,
709         void                    *params )
710 {
711         assert( refsp != NULL );
712         assert( *refsp != NULL );
713         assert( cntp != NULL );
714
715         if ( *cntp < -1 ) {
716                 *cntp = -1;
717                 return -1;
718         }
719
720         (*cntp)++;
721
722         if ( (*refsp)[ *cntp ] == NULL ) {
723                 *cntp = -1;
724         }
725
726         return 0;
727 }
728
729 /*
730  * Chase v3 referrals
731  *
732  * Parameters:
733  *  (IN) ld = LDAP connection handle
734  *  (IN) lr = LDAP Request structure
735  *  (IN) refs = array of pointers to referral strings that we will chase
736  *              The array will be free'd by this function when no longer needed
737  *  (IN) sref != 0 if following search reference
738  *  (OUT) errstrp = Place to return a string of referrals which could not be followed
739  *  (OUT) hadrefp = 1 if sucessfully followed referral
740  *
741  * Return value - number of referrals followed
742  */
743 int
744 ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp )
745 {
746         char            *unfollowed;
747         int              unfollowedcnt = 0;
748         LDAPRequest     *origreq;
749         LDAPURLDesc     *srv = NULL;
750         BerElement      *ber;
751         char            **refarray = NULL;
752         LDAPConn        *lc;
753         int                      rc, count, i, j, id;
754         LDAPreqinfo  rinfo;
755
756         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
757         *hadrefp = 0;
758
759         Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 );
760
761         unfollowed = NULL;
762         rc = count = 0;
763
764         /* If no referrals in array, return */
765         if ( (refs == NULL) || ( (refs)[0] == NULL) ) {
766                 rc = 0;
767                 goto done;
768         }
769
770         /* Check for hop limit exceeded */
771         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
772                 Debug( LDAP_DEBUG_ANY,
773                     "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 );
774                 ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED;
775             rc = -1;
776                 goto done;
777         }
778
779         /* find original request */
780         for ( origreq = lr;
781                 origreq->lr_parent != NULL;
782                 origreq = origreq->lr_parent )
783         {
784                 /* empty */ ;
785         }
786
787         refarray = refs;
788         refs = NULL;
789
790         if ( ld->ld_nextref_proc == NULL ) {
791                 ld->ld_nextref_proc = ldap_int_nextref;
792         }
793
794         /* parse out & follow referrals */
795         i = -1;
796         for ( ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params );
797                         i != -1;
798                         ld->ld_nextref_proc( ld, &refarray, &i, ld->ld_nextref_params ) )
799         {
800
801                 /* Parse the referral URL */
802                 if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) {
803                         ld->ld_errno = rc;
804                         rc = -1;
805                         goto done;
806                 }
807
808                 if( srv->lud_crit_exts ) {
809                         /* we do not support any extensions */
810                         ld->ld_errno = LDAP_NOT_SUPPORTED;
811                         rc = -1;
812                         goto done;
813                 }
814
815                 /* treat ldap://hostpart and ldap://hostpart/ the same */
816                 if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) {
817                         LDAP_FREE( srv->lud_dn );
818                         srv->lud_dn = NULL;
819                 }
820
821                 /* check connection for re-bind in progress */
822                 if (( lc = find_connection( ld, srv, 1 )) != NULL ) {
823                         if( lc->lconn_rebind_inprogress) {
824                                 /* We are already chasing a referral or search reference and a
825                                  * bind on that connection is in progress.  We must queue
826                                  * referrals on that connection, so we don't get a request
827                                  * going out before the bind operation completes. This happens
828                                  * if two search references come in one behind the other
829                                  * for the same server with different contexts.
830                                  */
831                                 Debug( LDAP_DEBUG_TRACE,
832                                         "ldap_chase_v3referrals: queue referral \"%s\"\n",
833                                         refarray[i], 0, 0);
834                                 if( lc->lconn_rebind_queue == NULL ) {
835                                         /* Create a referral list */
836                                         lc->lconn_rebind_queue =
837                                                 (char ***) LDAP_MALLOC( sizeof(void *) * 2);
838
839                                         if( lc->lconn_rebind_queue == NULL) {
840                                                 ld->ld_errno = LDAP_NO_MEMORY;
841                                                 rc = -1;
842                                                 goto done;
843                                         }
844
845                                         lc->lconn_rebind_queue[0] = refarray;
846                                         lc->lconn_rebind_queue[1] = NULL;
847                                         refarray = NULL;
848
849                                 } else {
850                                         /* Count how many referral arrays we already have */
851                                         for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) {
852                                                 /* empty */;
853                                         }
854
855                                         /* Add the new referral to the list */
856                                         lc->lconn_rebind_queue = (char ***) LDAP_REALLOC(
857                                                 lc->lconn_rebind_queue, sizeof(void *) * (j + 2));
858
859                                         if( lc->lconn_rebind_queue == NULL ) {
860                                                 ld->ld_errno = LDAP_NO_MEMORY;
861                                                 rc = -1;
862                                                 goto done;
863                                         }
864                                         lc->lconn_rebind_queue[j] = refarray;
865                                         lc->lconn_rebind_queue[j+1] = NULL;
866                                         refarray = NULL;
867                                 }
868
869                                 /* We have queued the referral/reference, now just return */
870                                 rc = 0;
871                                 *hadrefp = 1;
872                                 count = 1; /* Pretend we already followed referral */
873                                 goto done;
874                         }
875                 } 
876                 /* Re-encode the request with the new starting point of the search.
877                  * Note: In the future we also need to replace the filter if one
878                  * was provided with the search reference
879                  */
880
881                 /* For references we don't want old dn if new dn empty */
882                 if ( sref && srv->lud_dn == NULL ) {
883                         srv->lud_dn = LDAP_STRDUP( "" );
884                 }
885
886                 LDAP_NEXT_MSGID( ld, id );
887                 ber = re_encode_request( ld, origreq->lr_ber, id,
888                         sref, srv, &rinfo.ri_request );
889
890                 if( ber == NULL ) {
891                         ld->ld_errno = LDAP_ENCODING_ERROR;
892                         rc = -1;
893                         goto done;
894                 }
895
896                 Debug( LDAP_DEBUG_TRACE,
897                         "ldap_chase_v3referral: msgid %d, url \"%s\"\n",
898                         lr->lr_msgid, refarray[i], 0);
899
900                 /* Send the new request to the server - may require a bind */
901                 rinfo.ri_msgid = origreq->lr_origid;
902                 rinfo.ri_url = refarray[i];
903 #ifdef LDAP_R_COMPILE
904                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
905 #endif
906                 rc = ldap_send_server_request( ld, ber, id,
907                         origreq, srv, NULL, &rinfo );
908 #ifdef LDAP_R_COMPILE
909                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
910 #endif
911                 if ( rc < 0 ) {
912                         /* Failure, try next referral in the list */
913                         Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%d: %s)\n", 
914                                 refarray[i], ld->ld_errno, ldap_err2string( ld->ld_errno ) );
915                         unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i] );
916                         ldap_free_urllist( srv );
917                         srv = NULL;
918                 } else {
919                         /* Success, no need to try this referral list further */
920                         rc = 0;
921                         ++count;
922                         *hadrefp = 1;
923
924                         /* check if there is a queue of referrals that came in during bind */
925                         if ( lc == NULL) {
926                                 lc = find_connection( ld, srv, 1 );
927                                 if ( lc == 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                                 refarray = lc->lconn_rebind_queue[j - 1];
945                                 lc->lconn_rebind_queue[j-1] = NULL;
946                                 /* we pulled off last entry from queue, free queue */
947                                 if ( j == 1 ) {
948                                         LDAP_FREE( lc->lconn_rebind_queue );
949                                         lc->lconn_rebind_queue = NULL;
950                                 }
951                                 /* restart the loop the with new referral list */
952                                 i = -1;
953                                 continue;
954                         }
955                         break; /* referral followed, break out of for loop */
956                 }
957         } /* end for loop */
958 done:
959         LDAP_VFREE( refarray );
960         ldap_free_urllist( srv );
961         LDAP_FREE( *errstrp );
962         
963         if( rc == 0 ) {
964                 *errstrp = NULL;
965                 LDAP_FREE( unfollowed );
966                 return count;
967         } else {
968                 ld->ld_errno = LDAP_REFERRAL;
969                 *errstrp = unfollowed;
970                 return rc;
971         }
972 }
973
974 /*
975  * XXX merging of errors in this routine needs to be improved
976  */
977 int
978 ldap_chase_referrals( LDAP *ld,
979         LDAPRequest *lr,
980         char **errstrp,
981         int sref,
982         int *hadrefp )
983 {
984         int             rc, count, id;
985         unsigned        len;
986         char            *p, *ref, *unfollowed;
987         LDAPRequest     *origreq;
988         LDAPURLDesc     *srv;
989         BerElement      *ber;
990         LDAPreqinfo  rinfo;
991
992         Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
993
994         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
995         *hadrefp = 0;
996
997         if ( *errstrp == NULL ) {
998                 return( 0 );
999         }
1000
1001         len = strlen( *errstrp );
1002         for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
1003                 if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
1004                         *p = '\0';
1005                         p += LDAP_REF_STR_LEN;
1006                         break;
1007                 }
1008         }
1009
1010         if ( len < LDAP_REF_STR_LEN ) {
1011                 return( 0 );
1012         }
1013
1014         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
1015                 Debug( LDAP_DEBUG_ANY,
1016                     "more than %d referral hops (dropping)\n",
1017                     ld->ld_refhoplimit, 0, 0 );
1018                     /* XXX report as error in ld->ld_errno? */
1019                     return( 0 );
1020         }
1021
1022         /* find original request */
1023         for ( origreq = lr; origreq->lr_parent != NULL;
1024              origreq = origreq->lr_parent ) {
1025                 /* empty */;
1026         }
1027
1028         unfollowed = NULL;
1029         rc = count = 0;
1030
1031         /* parse out & follow referrals */
1032         for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
1033                 p = strchr( ref, '\n' );
1034                 if ( p != NULL ) {
1035                         *p++ = '\0';
1036                 }
1037
1038                 rc = ldap_url_parse_ext( ref, &srv );
1039
1040                 if ( rc != LDAP_URL_SUCCESS ) {
1041                         Debug( LDAP_DEBUG_TRACE,
1042                             "ignoring unknown referral <%s>\n", ref, 0, 0 );
1043                         rc = ldap_append_referral( ld, &unfollowed, ref );
1044                         *hadrefp = 1;
1045                         continue;
1046                 }
1047
1048                 if ( srv->lud_dn != NULL && srv->lud_dn == '\0' ) {
1049                         LDAP_FREE( srv->lud_dn );
1050                         srv->lud_dn = NULL;
1051                 }
1052
1053                 Debug( LDAP_DEBUG_TRACE,
1054                     "chasing LDAP referral: <%s>\n", ref, 0, 0 );
1055
1056                 *hadrefp = 1;
1057
1058                 LDAP_NEXT_MSGID( ld, id );
1059                 ber = re_encode_request( ld, origreq->lr_ber,
1060                     id, sref, srv, &rinfo.ri_request );
1061
1062                 if ( ber == NULL ) {
1063                         return -1 ;
1064                 }
1065
1066                 /* copy the complete referral for rebind process */
1067                 rinfo.ri_url = LDAP_STRDUP( ref );
1068
1069                 rinfo.ri_msgid = origreq->lr_origid;
1070
1071 #ifdef LDAP_R_COMPILE
1072                 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1073 #endif
1074                 rc = ldap_send_server_request( ld, ber, id,
1075                         lr, srv, NULL, &rinfo );
1076 #ifdef LDAP_R_COMPILE
1077                 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1078 #endif
1079
1080                 LDAP_FREE( rinfo.ri_url );
1081
1082                 if( rc >= 0 ) {
1083                         ++count;
1084                 } else {
1085                         Debug( LDAP_DEBUG_ANY,
1086                                 "Unable to chase referral \"%s\" (%d: %s)\n", 
1087                                 ref, ld->ld_errno, ldap_err2string( ld->ld_errno ) );
1088                         rc = ldap_append_referral( ld, &unfollowed, ref );
1089                 }
1090
1091                 ldap_free_urllist(srv);
1092         }
1093
1094         LDAP_FREE( *errstrp );
1095         *errstrp = unfollowed;
1096
1097         return(( rc == 0 ) ? count : rc );
1098 }
1099
1100
1101 int
1102 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
1103 {
1104         int     first;
1105
1106         if ( *referralsp == NULL ) {
1107                 first = 1;
1108                 *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN
1109                     + 1 );
1110         } else {
1111                 first = 0;
1112                 *referralsp = (char *)LDAP_REALLOC( *referralsp,
1113                     strlen( *referralsp ) + strlen( s ) + 2 );
1114         }
1115
1116         if ( *referralsp == NULL ) {
1117                 ld->ld_errno = LDAP_NO_MEMORY;
1118                 return( -1 );
1119         }
1120
1121         if ( first ) {
1122                 strcpy( *referralsp, LDAP_REF_STR );
1123         } else {
1124                 strcat( *referralsp, "\n" );
1125         }
1126         strcat( *referralsp, s );
1127
1128         return( 0 );
1129 }
1130
1131
1132
1133 static BerElement *
1134 re_encode_request( LDAP *ld,
1135         BerElement *origber,
1136         ber_int_t msgid,
1137         int sref,
1138         LDAPURLDesc *srv,
1139         int *type )
1140 {
1141         /*
1142          * XXX this routine knows way too much about how the lber library works!
1143          */
1144         ber_int_t       along;
1145         ber_tag_t       tag;
1146         ber_tag_t       rtag;
1147         ber_int_t       ver;
1148         ber_int_t       scope;
1149         int             rc;
1150         BerElement      tmpber, *ber;
1151         char            *orig_dn;
1152         char            *dn;
1153
1154         Debug( LDAP_DEBUG_TRACE,
1155             "re_encode_request: new msgid %ld, new dn <%s>\n",
1156             (long) msgid,
1157                 ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 );
1158
1159         tmpber = *origber;
1160
1161         /*
1162          * all LDAP requests are sequences that start with a message id.
1163          * For all except delete, this is followed by a sequence that is
1164          * tagged with the operation code.  For delete, the provided DN
1165          * is not wrapped by a sequence.
1166          */
1167         rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );
1168
1169         if ( rtag == LBER_ERROR ) {
1170                 ld->ld_errno = LDAP_DECODING_ERROR;
1171                 return( NULL );
1172         }
1173
1174         assert( tag != 0);
1175         if ( tag == LDAP_REQ_BIND ) {
1176                 /* bind requests have a version number before the DN & other stuff */
1177                 rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
1178
1179         } else if ( tag == LDAP_REQ_DELETE ) {
1180                 /* delete requests don't have a DN wrapping sequence */
1181                 rtag = ber_scanf( &tmpber, "a", &orig_dn );
1182
1183         } else if ( tag == LDAP_REQ_SEARCH ) {
1184                 /* search requests need to be re-scope-ed */
1185                 rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope );
1186
1187                 if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
1188                         /* use the scope provided in reference */
1189                         scope = srv->lud_scope;
1190
1191                 } else if ( sref ) {
1192                         /* use scope implied by previous operation
1193                          *   base -> base
1194                          *   one -> base
1195                          *   subtree -> subtree
1196                          *   subordinate -> subtree
1197                          */
1198                         switch( scope ) {
1199                         default:
1200                         case LDAP_SCOPE_BASE:
1201                         case LDAP_SCOPE_ONELEVEL:
1202                                 scope = LDAP_SCOPE_BASE;
1203                                 break;
1204                         case LDAP_SCOPE_SUBTREE:
1205 #ifdef LDAP_SCOPE_SUBORDINATE
1206                         case LDAP_SCOPE_SUBORDINATE:
1207 #endif
1208                                 scope = LDAP_SCOPE_SUBTREE;
1209                                 break;
1210                         }
1211                 }
1212
1213         } else {
1214                 rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
1215         }
1216
1217         if( rtag == LBER_ERROR ) {
1218                 ld->ld_errno = LDAP_DECODING_ERROR;
1219                 return NULL;
1220         }
1221
1222         if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
1223                 return NULL;
1224         }
1225
1226         if ( srv->lud_dn == NULL ) {
1227                 dn = orig_dn;
1228         } else {
1229                 dn = srv->lud_dn;
1230         }
1231
1232         if ( tag == LDAP_REQ_BIND ) {
1233                 rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, dn );
1234         } else if ( tag == LDAP_REQ_DELETE ) {
1235                 rc = ber_printf( ber, "{itsN}", msgid, tag, dn );
1236         } else if ( tag == LDAP_REQ_SEARCH ) {
1237                 rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope );
1238         } else {
1239                 rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn );
1240         }
1241
1242         LDAP_FREE( orig_dn );
1243
1244         if ( rc == -1 ) {
1245                 ld->ld_errno = LDAP_ENCODING_ERROR;
1246                 ber_free( ber, 1 );
1247                 return NULL;
1248         }
1249
1250         if ( tag != LDAP_REQ_DELETE && (
1251                 ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
1252                 != ( tmpber.ber_end - tmpber.ber_ptr ) ||
1253             ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) )
1254         {
1255                 ld->ld_errno = LDAP_ENCODING_ERROR;
1256                 ber_free( ber, 1 );
1257                 return NULL;
1258         }
1259
1260 #ifdef LDAP_DEBUG
1261         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
1262                 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
1263                     0, 0, 0 );
1264                 ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
1265         }
1266 #endif /* LDAP_DEBUG */
1267
1268         *type = tag;    /* return request type */
1269         return ber;
1270 }
1271
1272
1273 LDAPRequest *
1274 ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid )
1275 {
1276         LDAPRequest     *lr;
1277
1278 #ifdef LDAP_R_COMPILE
1279         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1280 #endif
1281         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
1282                 if( lr->lr_status == LDAP_REQST_COMPLETED ) {
1283                         continue;       /* Skip completed requests */
1284                 }
1285                 if ( msgid == lr->lr_msgid ) {
1286                         break;
1287                 }
1288         }
1289 #ifdef LDAP_R_COMPILE
1290         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1291 #endif
1292
1293         return( lr );
1294 }
1295
1296