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