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