]> git.sur5r.net Git - openldap/blob - libraries/libldap/request.c
Merged LDAPworldCurrent (P1-10,13,15,16,19-22)
[openldap] / libraries / libldap / request.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  request.c - sending of ldap requests; handling of referrals
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14 #ifdef MACOS
15 #include <stdlib.h>
16 #include <time.h>
17 #include "macos.h"
18 #else /* MACOS */
19 #if defined( DOS ) || defined( _WIN32 )
20 #include "msdos.h"
21 #include <time.h>
22 #include <stdlib.h>
23 #ifdef PCNFS
24 #include <tklib.h>
25 #include <tk_errno.h>
26 #include <bios.h>
27 #endif /* PCNFS */
28 #ifdef NCSA
29 #include "externs.h"
30 #endif /* NCSA */
31 #else /* DOS */
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <errno.h>
36 #ifdef _AIX
37 #include <sys/select.h>
38 #endif /* _AIX */
39 #include "portable.h"
40 #endif /* DOS */
41 #endif /* MACOS */
42 #ifdef VMS
43 #include "ucx_select.h"
44 #endif
45 #include "lber.h"
46 #include "ldap.h"
47 #include "ldap-int.h"
48
49 #ifdef USE_SYSCONF
50 #include <unistd.h>
51 #endif /* USE_SYSCONF */
52
53
54 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
55 #ifdef NEEDPROTOS
56 static LDAPConn *find_connection( LDAP *ld, LDAPServer *srv, int any );
57 static void use_connection( LDAP *ld, LDAPConn *lc );
58 static void free_servers( LDAPServer *srvlist );
59 #else /* NEEDPROTOS */
60 static LDAPConn *find_connection();
61 static void use_connection();
62 static void free_servers();
63 #endif /* NEEDPROTOS */
64 #endif /* LDAP_REFERRALS || LDAP_DNS */
65
66
67 #ifdef LDAP_DNS
68 #ifdef NEEDPROTOS
69 static LDAPServer *dn2servers( LDAP *ld, char *dn );
70 #else /* NEEDPROTOS */
71 static LDAPServer *dn2servers();
72 #endif /* NEEDPROTOS */
73 #endif /* LDAP_DNS */
74
75 #ifdef LDAP_REFERRALS
76 #ifdef NEEDPROTOS
77 static BerElement *re_encode_request( LDAP *ld, BerElement *origber,
78     int msgid, char **dnp );
79 #else /* NEEDPROTOS */
80 static BerElement *re_encode_request();
81 #endif /* NEEDPROTOS */
82 #endif /* LDAP_REFERRALS */
83
84
85 BerElement *
86 ldap_alloc_ber_with_options( LDAP *ld )
87 {
88         BerElement      *ber;
89
90         if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
91                 ld->ld_errno = LDAP_NO_MEMORY;
92 #ifdef STR_TRANSLATION
93         } else {
94                 ldap_set_ber_options( ld, ber );
95 #endif /* STR_TRANSLATION */
96         }
97
98         return( ber );
99 }
100
101
102 void
103 ldap_set_ber_options( LDAP *ld, BerElement *ber )
104 {
105         ber->ber_options = ld->ld_lberoptions;
106 #ifdef STR_TRANSLATION
107         if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
108                 ber_set_string_translators( ber,
109                     ld->ld_lber_encode_translate_proc,
110                     ld->ld_lber_decode_translate_proc );
111         }
112 #endif /* STR_TRANSLATION */
113 }
114
115
116 int
117 ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
118         BerElement *ber )
119 {
120 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
121         LDAPServer      *servers;
122 #endif /* LDAP_REFERRALS || LDAP_DNS */
123
124         Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
125
126 #if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
127         if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
128                 ld->ld_errno = LDAP_SERVER_DOWN;
129                 return( -1 );
130         }
131
132         ld->ld_errno = LDAP_SUCCESS;
133         return( ld->ld_msgid );
134 #else /* !LDAP_REFERRALS && !LDAP_DNS */
135
136 #ifdef LDAP_DNS
137         if (( ld->ld_options & LDAP_OPT_DNS ) != 0 && ldap_is_dns_dn( dn )) {
138                 if (( servers = dn2servers( ld, dn )) == NULL ) {
139                         ber_free( ber, 1 );
140                         return( -1 );
141                 }
142
143 #ifdef LDAP_DEBUG
144                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
145                         LDAPServer      *srv;
146
147                         for ( srv = servers; srv != NULL;
148                             srv = srv->lsrv_next ) {
149                                 fprintf( stderr,
150                                     "LDAP server %s:  dn %s, port %d\n",
151                                     srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
152                                     "(default)" : srv->lsrv_dn,
153                                     srv->lsrv_port );
154                         }
155                 }
156 #endif /* LDAP_DEBUG */
157         } else {
158 #endif /* LDAP_DNS */
159                 /*
160                  * use of DNS is turned off or this is an X.500 DN...
161                  * use our default connection
162                  */
163                 servers = NULL;
164 #ifdef LDAP_DNS
165         }       
166 #endif /* LDAP_DNS */
167
168         return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
169             NULL, 0 ));
170 #endif /* !LDAP_REFERRALS && !LDAP_DNS */
171 }
172
173
174
175 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
176 int
177 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
178         *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
179 {
180         LDAPRequest     *lr;
181         int incparent;
182
183         Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
184
185         incparent = 0;
186         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
187
188         if ( lc == NULL ) {
189                 if ( srvlist == NULL ) {
190                         lc = ld->ld_defconn;
191                 } else {
192                         if (( lc = find_connection( ld, srvlist, 1 )) ==
193                             NULL ) {
194                                 if ( bind && (parentreq != NULL) ) {
195                                         /* Remember the bind in the parent */
196                                         incparent = 1;
197                                         ++parentreq->lr_outrefcnt;
198                                 }
199                                 lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
200                         }
201                         free_servers( srvlist );
202                 }
203         }
204
205         if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
206                 ber_free( ber, 1 );
207                 if ( ld->ld_errno == LDAP_SUCCESS ) {
208                         ld->ld_errno = LDAP_SERVER_DOWN;
209                 }
210                 if ( incparent ) {
211                         /* Forget about the bind */
212                         --parentreq->lr_outrefcnt; 
213                 }
214                 return( -1 );
215         }
216
217         use_connection( ld, lc );
218         if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
219             NULL ) {
220                 ld->ld_errno = LDAP_NO_MEMORY;
221                 ldap_free_connection( ld, lc, 0, 0 );
222                 ber_free( ber, 1 );
223                 if ( incparent ) {
224                         /* Forget about the bind */
225                         --parentreq->lr_outrefcnt; 
226                 }
227                 return( -1 );
228         } 
229         lr->lr_msgid = msgid;
230         lr->lr_status = LDAP_REQST_INPROGRESS;
231         lr->lr_res_errno = LDAP_SUCCESS;        /* optimistic */
232         lr->lr_ber = ber;
233         lr->lr_conn = lc;
234         if ( parentreq != NULL ) {      /* sub-request */
235                 if ( !incparent ) { 
236                         /* Increment if we didn't do it before the bind */
237                         ++parentreq->lr_outrefcnt;
238                 }
239                 lr->lr_origid = parentreq->lr_origid;
240                 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
241                 lr->lr_parent = parentreq;
242                 lr->lr_refnext = parentreq->lr_refnext;
243                 parentreq->lr_refnext = lr;
244         } else {                        /* original request */
245                 lr->lr_origid = lr->lr_msgid;
246         }
247
248         if (( lr->lr_next = ld->ld_requests ) != NULL ) {
249                 lr->lr_next->lr_prev = lr;
250         }
251         ld->ld_requests = lr;
252         lr->lr_prev = NULL;
253
254         if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
255 #ifdef notyet
256                 extern int      errno;
257
258                 if ( errno == EWOULDBLOCK ) {
259                         /* need to continue write later */
260                         lr->lr_status = LDAP_REQST_WRITING;
261                         ldap_mark_select_write( ld, lc->lconn_sb );
262                 } else {
263 #else /* notyet */
264                         ld->ld_errno = LDAP_SERVER_DOWN;
265                         ldap_free_request( ld, lr );
266                         ldap_free_connection( ld, lc, 0, 0 );
267                         return( -1 );
268 #endif /* notyet */
269 #ifdef notyet
270                 }
271 #endif /* notyet */
272         } else {
273                 if ( parentreq == NULL ) {
274                         ber->ber_end = ber->ber_ptr;
275                         ber->ber_ptr = ber->ber_buf;
276                 }
277
278                 /* sent -- waiting for a response */
279                 ldap_mark_select_read( ld, lc->lconn_sb );
280         }
281
282         ld->ld_errno = LDAP_SUCCESS;
283         return( msgid );
284 }
285
286
287 LDAPConn *
288 ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
289         int connect, int bind )
290 {
291         LDAPConn        *lc;
292         LDAPServer      *prevsrv, *srv;
293         Sockbuf         *sb;
294
295         /*
296          * make a new LDAP server connection
297          * XXX open connection synchronously for now
298          */
299         if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
300             ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
301             == NULL )) {
302                 if ( lc != NULL ) {
303                         free( (char *)lc );
304                 }
305                 ld->ld_errno = LDAP_NO_MEMORY;
306                 return( NULL );
307         }
308
309         lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
310
311         if ( connect ) {
312                 prevsrv = NULL;
313
314                 for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
315                         if ( open_ldap_connection( ld, lc->lconn_sb,
316                             srv->lsrv_host, srv->lsrv_port,
317                             &lc->lconn_krbinstance, 0 ) != -1 ) {
318                                 break;
319                         }
320                         prevsrv = srv;
321                 }
322
323                 if ( srv == NULL ) {
324                     if ( !use_ldsb ) {
325                         free( (char *)lc->lconn_sb );
326                     }
327                     free( (char *)lc );
328                     ld->ld_errno = LDAP_SERVER_DOWN;
329                     return( NULL );
330                 }
331
332                 if ( prevsrv == NULL ) {
333                     *srvlistp = srv->lsrv_next;
334                 } else {
335                     prevsrv->lsrv_next = srv->lsrv_next;
336                 }
337                 lc->lconn_server = 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 ) {
349                 int             err, freepasswd, authmethod;
350                 char            *binddn, *passwd;
351                 LDAPConn        *savedefconn;
352
353                 freepasswd = err = 0;
354
355                 if ( ld->ld_rebindproc == NULL ) {
356                         binddn = passwd = "";
357                         authmethod = LDAP_AUTH_SIMPLE;
358                 } else {
359                         if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
360                             &authmethod, 0 )) == LDAP_SUCCESS ) {
361                                 freepasswd = 1;
362                         } else {
363                                 ld->ld_errno = err;
364                                 err = -1;
365                         }
366                 }
367
368
369                 if ( err == 0 ) {
370                         savedefconn = ld->ld_defconn;
371                         ld->ld_defconn = lc;
372                         ++lc->lconn_refcnt;     /* avoid premature free */
373
374                         if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
375                             LDAP_SUCCESS ) {
376                                 err = -1;
377                         }
378                         --lc->lconn_refcnt;
379                         ld->ld_defconn = savedefconn;
380                 }
381
382                 if ( freepasswd ) {
383                         (*ld->ld_rebindproc)( ld, &binddn, &passwd,
384                                 &authmethod, 1 );
385                 }
386
387                 if ( err != 0 ) {
388                         ldap_free_connection( ld, lc, 1, 0 );
389                         lc = NULL;
390                 }
391         }
392
393         return( lc );
394 }
395
396
397 static LDAPConn *
398 find_connection( LDAP *ld, LDAPServer *srv, int any )
399 /*
400  * return an existing connection (if any) to the server srv
401  * if "any" is non-zero, check for any server in the "srv" chain
402  */
403 {
404         LDAPConn        *lc;
405         LDAPServer      *ls;
406
407         for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
408                 for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
409                         if ( lc->lconn_server->lsrv_host != NULL &&
410                             ls->lsrv_host != NULL && strcasecmp(
411                             ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
412                             && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
413                                 return( lc );
414                         }
415                         if ( !any ) {
416                                 break;
417                         }
418                 }
419         }
420
421         return( NULL );
422 }
423
424
425
426 static void
427 use_connection( LDAP *ld, LDAPConn *lc )
428 {
429         ++lc->lconn_refcnt;
430         lc->lconn_lastused = time( 0 );
431 }
432
433
434 void
435 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
436 {
437         LDAPConn        *tmplc, *prevlc;
438
439         Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
440
441         if ( force || --lc->lconn_refcnt <= 0 ) {
442                 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
443                         ldap_mark_select_clear( ld, lc->lconn_sb );
444                         if ( unbind ) {
445                                 ldap_send_unbind( ld, lc->lconn_sb );
446                         }
447                         ldap_close_connection( lc->lconn_sb );
448                         if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
449                                 free( lc->lconn_sb->sb_ber.ber_buf );
450                         }
451                 }
452                 prevlc = NULL;
453                 for ( tmplc = ld->ld_conns; tmplc != NULL;
454                     tmplc = tmplc->lconn_next ) {
455                         if ( tmplc == lc ) {
456                                 if ( prevlc == NULL ) {
457                                     ld->ld_conns = tmplc->lconn_next;
458                                 } else {
459                                     prevlc->lconn_next = tmplc->lconn_next;
460                                 }
461                                 break;
462                         }
463                         prevlc = tmplc;
464                 }
465                 free_servers( lc->lconn_server );
466                 if ( lc->lconn_krbinstance != NULL ) {
467                         free( lc->lconn_krbinstance );
468                 }
469                 if ( lc->lconn_sb != &ld->ld_sb ) {
470                         free( (char *)lc->lconn_sb );
471                 }
472                 free( lc );
473                 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
474                     0, 0, 0 );
475         } else {
476                 lc->lconn_lastused = time( 0 );
477                 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
478                     lc->lconn_refcnt, 0, 0 );
479         }
480 }
481
482
483 #ifdef LDAP_DEBUG
484 void
485 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
486 {
487         LDAPConn        *lc;
488
489         fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
490         for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
491                 if ( lc->lconn_server != NULL ) {
492                         fprintf( stderr, "* host: %s  port: %d%s\n",
493                             ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
494                             : lc->lconn_server->lsrv_host,
495                             lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
496                             &ld->ld_sb ) ? "  (default)" : "" );
497                 }
498                 fprintf( stderr, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
499                     ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
500                     "NeedSocket" : ( lc->lconn_status ==
501                     LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
502                 fprintf( stderr, "  last used: %s\n",
503                     ctime( &lc->lconn_lastused ));
504                 if ( !all ) {
505                         break;
506                 }
507         }
508 }
509
510
511 void
512 ldap_dump_requests_and_responses( LDAP *ld )
513 {
514         LDAPRequest     *lr;
515         LDAPMessage     *lm, *l;
516
517         fprintf( stderr, "** Outstanding Requests:\n" );
518         if (( lr = ld->ld_requests ) == NULL ) {
519                 fprintf( stderr, "   Empty\n" );
520         }
521         for ( ; lr != NULL; lr = lr->lr_next ) {
522             fprintf( stderr, " * msgid %d,  origid %d, status %s\n",
523                 lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
524                 LDAP_REQST_INPROGRESS ) ? "InProgress" :
525                 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
526                 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
527                 "Writing" );
528             fprintf( stderr, "   outstanding referrals %d, parent count %d\n",
529                     lr->lr_outrefcnt, lr->lr_parentcnt );
530         }
531
532         fprintf( stderr, "** Response Queue:\n" );
533         if (( lm = ld->ld_responses ) == NULLMSG ) {
534                 fprintf( stderr, "   Empty\n" );
535         }
536         for ( ; lm != NULLMSG; lm = lm->lm_next ) {
537                 fprintf( stderr, " * msgid %d,  type %d\n",
538                     lm->lm_msgid, lm->lm_msgtype );
539                 if (( l = lm->lm_chain ) != NULL ) {
540                         fprintf( stderr, "   chained responses:\n" );
541                         for ( ; l != NULLMSG; l = l->lm_chain ) {
542                                 fprintf( stderr,
543                                     "  * msgid %d,  type %d\n",
544                                     l->lm_msgid, l->lm_msgtype );
545                         }
546                 }
547         }
548 }
549 #endif /* LDAP_DEBUG */
550
551
552 void
553 ldap_free_request( LDAP *ld, LDAPRequest *lr )
554 {
555         LDAPRequest     *tmplr, *nextlr;
556
557         Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
558                 lr->lr_origid, lr->lr_msgid, 0 );
559
560         if ( lr->lr_parent != NULL ) {
561                 --lr->lr_parent->lr_outrefcnt;
562         } else {
563                 /* free all referrals (child requests) */
564                 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
565                         nextlr = tmplr->lr_refnext;
566                         ldap_free_request( ld, tmplr );
567                 }
568         }
569
570         if ( lr->lr_prev == NULL ) {
571                 ld->ld_requests = lr->lr_next;
572         } else {
573                 lr->lr_prev->lr_next = lr->lr_next;
574         }
575
576         if ( lr->lr_next != NULL ) {
577                 lr->lr_next->lr_prev = lr->lr_prev;
578         }
579
580         if ( lr->lr_ber != NULL ) {
581                 ber_free( lr->lr_ber, 1 );
582         }
583
584         if ( lr->lr_res_error != NULL ) {
585                 free( lr->lr_res_error );
586         }
587
588         if ( lr->lr_res_matched != NULL ) {
589                 free( lr->lr_res_matched );
590         }
591
592         free( lr );
593 }
594
595
596 static void
597 free_servers( LDAPServer *srvlist )
598 {
599     LDAPServer  *nextsrv;
600
601     while ( srvlist != NULL ) {
602         nextsrv = srvlist->lsrv_next;
603         if ( srvlist->lsrv_dn != NULL ) {
604                 free( srvlist->lsrv_dn );
605         }
606         if ( srvlist->lsrv_host != NULL ) {
607                 free( srvlist->lsrv_host );
608         }
609         free( srvlist );
610         srvlist = nextsrv;
611     }
612 }
613 #endif /* LDAP_REFERRALS || LDAP_DNS */
614
615
616 #ifdef LDAP_REFERRALS
617 /*
618  * XXX merging of errors in this routine needs to be improved
619  */
620 int
621 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
622 {
623         int             rc, count, len, newdn;
624 #ifdef LDAP_DNS
625         int             ldapref;
626 #endif /* LDAP_DNS */
627         char            *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
628         LDAPRequest     *origreq;
629         LDAPServer      *srv;
630         BerElement      *ber;
631
632         Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
633
634         ld->ld_errno = LDAP_SUCCESS;    /* optimistic */
635         *hadrefp = 0;
636
637         if ( *errstrp == NULL ) {
638                 return( 0 );
639         }
640
641         len = strlen( *errstrp );
642         for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
643                 if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
644                     LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
645                         *p = '\0';
646                         p += LDAP_REF_STR_LEN;
647                         break;
648                 }
649         }
650
651         if ( len < LDAP_REF_STR_LEN ) {
652                 return( 0 );
653         }
654
655         if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
656                 Debug( LDAP_DEBUG_ANY,
657                     "more than %d referral hops (dropping)\n",
658                     ld->ld_refhoplimit, 0, 0 );
659                     /* XXX report as error in ld->ld_errno? */
660                     return( 0 );
661         }
662
663         /* find original request */
664         for ( origreq = lr; origreq->lr_parent != NULL;
665              origreq = origreq->lr_parent ) {
666                 ;
667         }
668
669         unfollowed = NULL;
670         rc = count = 0;
671
672         /* parse out & follow referrals */
673         for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
674 #ifdef LDAP_DNS
675                 ldapref = 0;
676 #endif /* LDAP_DNS */
677
678                 if (( p = strchr( ref, '\n' )) != NULL ) {
679                         *p++ = '\0';
680                 } else {
681                         p = NULL;
682                 }
683
684                 len = strlen( ref );
685                 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
686                     LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
687                         Debug( LDAP_DEBUG_TRACE,
688                             "chasing LDAP referral: <%s>\n", ref, 0, 0 );
689 #ifdef LDAP_DNS
690                         ldapref = 1;
691 #endif /* LDAP_DNS */
692                         tmpref = ref + LDAP_LDAP_REF_STR_LEN;
693 #ifdef LDAP_DNS
694                 } else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
695                     LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
696                         Debug( LDAP_DEBUG_TRACE,
697                             "chasing DX referral: <%s>\n", ref, 0, 0 );
698                         tmpref = ref + LDAP_DX_REF_STR_LEN;
699 #endif /* LDAP_DNS */
700                 } else {
701                         Debug( LDAP_DEBUG_TRACE,
702                             "ignoring unknown referral <%s>\n", ref, 0, 0 );
703                         rc = ldap_append_referral( ld, &unfollowed, ref );
704                         *hadrefp = 1;
705                         continue;
706                 }
707
708                 *hadrefp = 1;
709                 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
710                         *refdn++ = '\0';
711                         newdn = 1;
712                 } else {
713                         newdn = 0;
714                 }
715
716                 if (( ber = re_encode_request( ld, origreq->lr_ber,
717                     ++ld->ld_msgid, &refdn )) == NULL ) {
718                         return( -1 );
719                 }
720
721 #ifdef LDAP_DNS
722                 if ( ldapref ) {
723 #endif /* LDAP_DNS */
724                         if (( srv = (LDAPServer *)calloc( 1,
725                             sizeof( LDAPServer ))) == NULL ) {
726                                 ber_free( ber, 1 );
727                                 ld->ld_errno = LDAP_NO_MEMORY;
728                                 return( -1 );
729                         }
730
731                         if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
732                                 free( (char *)srv );
733                                 ber_free( ber, 1 );
734                                 ld->ld_errno = LDAP_NO_MEMORY;
735                                 return( -1 );
736                         }
737
738                         if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
739                                 *ports++ = '\0';
740                                 srv->lsrv_port = atoi( ports );
741                         } else {
742                                 srv->lsrv_port = LDAP_PORT;
743                         }
744 #ifdef LDAP_DNS
745                 } else {
746                         srv = dn2servers( ld, tmpref );
747                 }
748 #endif /* LDAP_DNS */
749
750                 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
751                     lr, srv, NULL, 1 ) >= 0 ) {
752                         ++count;
753                 } else {
754                         Debug( LDAP_DEBUG_ANY,
755                             "Unable to chase referral (%s)\n", 
756                             ldap_err2string( ld->ld_errno ), 0, 0 );
757                         rc = ldap_append_referral( ld, &unfollowed, ref );
758                 }
759
760                 if ( !newdn && refdn != NULL ) {
761                         free( refdn );
762                 }
763         }
764
765         free( *errstrp );
766         *errstrp = unfollowed;
767
768         return(( rc == 0 ) ? count : rc );
769 }
770
771
772 int
773 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
774 {
775         int     first;
776
777         if ( *referralsp == NULL ) {
778                 first = 1;
779                 *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
780                     + 1 );
781         } else {
782                 first = 0;
783                 *referralsp = (char *)realloc( *referralsp,
784                     strlen( *referralsp ) + strlen( s ) + 2 );
785         }
786
787         if ( *referralsp == NULL ) {
788                 ld->ld_errno = LDAP_NO_MEMORY;
789                 return( -1 );
790         }
791
792         if ( first ) {
793                 strcpy( *referralsp, LDAP_REF_STR );
794         } else {
795                 strcat( *referralsp, "\n" );
796         }
797         strcat( *referralsp, s );
798
799         return( 0 );
800 }
801
802
803
804 static BerElement *
805 re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
806 {
807 /*
808  * XXX this routine knows way too much about how the lber library works!
809  */
810         unsigned long   along, tag;
811         long            ver;
812         int             rc;
813         BerElement      tmpber, *ber;
814         char            *orig_dn;
815
816         Debug( LDAP_DEBUG_TRACE,
817             "re_encode_request: new msgid %d, new dn <%s>\n",
818             msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
819
820         tmpber = *origber;
821
822         /*
823          * all LDAP requests are sequences that start with a message id,
824          * followed by a sequence that is tagged with the operation code
825          */
826         if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
827             ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
828                 ld->ld_errno = LDAP_DECODING_ERROR;
829                 return( NULL );
830         }
831
832         if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
833                 return( NULL );
834         }
835
836         /* bind requests have a version number before the DN & other stuff */
837         if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
838             LBER_DEFAULT ) {
839                 ld->ld_errno = LDAP_DECODING_ERROR;
840                 ber_free( ber, 1 );
841                 return( NULL );
842         }
843
844         /* the rest of the request is the DN followed by other stuff */
845         if ( ber_get_stringa( &tmpber, &orig_dn ) == LBER_DEFAULT ) {
846                 ber_free( ber, 1 );
847                 return( NULL );
848         }
849
850         if ( *dnp == NULL ) {
851                 *dnp = orig_dn;
852         } else {
853                 free( orig_dn );
854         }
855
856         if ( tag == LDAP_REQ_BIND ) {
857                 rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
858         } else {
859                 rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
860         }
861
862         if ( rc == -1 ) {
863                 ber_free( ber, 1 );
864                 return( NULL );
865         }
866
867         if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
868             tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
869             ber_printf( ber, "}}" ) == -1 ) {
870                 ld->ld_errno = LDAP_ENCODING_ERROR;
871                 ber_free( ber, 1 );
872                 return( NULL );
873         }
874
875 #ifdef LDAP_DEBUG
876         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
877                 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
878                     0, 0, 0 );
879                 ber_dump( ber, 0 );
880         }
881 #endif /* LDAP_DEBUG */
882
883         return( ber );
884 }
885
886
887 LDAPRequest *
888 ldap_find_request_by_msgid( LDAP *ld, int msgid )
889 {
890         LDAPRequest     *lr;
891
892         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
893                 if ( msgid == lr->lr_msgid ) {
894                         break;
895                 }
896         }
897
898         return( lr );
899 }
900 #endif /* LDAP_REFERRALS */
901
902
903 #ifdef LDAP_DNS
904 static LDAPServer *
905 dn2servers( LDAP *ld, char *dn )        /* dn can also be a domain.... */
906 {
907         char            *p, *domain, *host, *server_dn, **dxs;
908         int             i, port;
909         LDAPServer      *srvlist, *prevsrv, *srv;
910
911         if (( domain = strrchr( dn, '@' )) != NULL ) {
912                 ++domain;
913         } else {
914                 domain = dn;
915         }
916
917         if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
918                 ld->ld_errno = LDAP_NO_MEMORY;
919                 return( NULL );
920         }
921
922         srvlist = NULL;
923
924         for ( i = 0; dxs[ i ] != NULL; ++i ) {
925                 port = LDAP_PORT;
926                 server_dn = NULL;
927                 if ( strchr( dxs[ i ], ':' ) == NULL ) {
928                         host = dxs[ i ];
929                 } else if ( strlen( dxs[ i ] ) >= 7 &&
930                     strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
931                         host = dxs[ i ] + 7;
932                         if (( p = strchr( host, ':' )) == NULL ) {
933                                 p = host;
934                         } else {
935                                 *p++ = '\0';
936                                 port = atoi( p );
937                         }
938                         if (( p = strchr( p, '/' )) != NULL ) {
939                                 server_dn = ++p;
940                                 if ( *server_dn == '\0' ) {
941                                         server_dn = NULL;
942                                 }
943                         }
944                 } else {
945                         host = NULL;
946                 }
947
948                 if ( host != NULL ) {   /* found a server we can use */
949                         if (( srv = (LDAPServer *)calloc( 1,
950                             sizeof( LDAPServer ))) == NULL ) {
951                                 free_servers( srvlist );
952                                 srvlist = NULL;
953                                 break;          /* exit loop & return */
954                         }
955
956                         /* add to end of list of servers */
957                         if ( srvlist == NULL ) {
958                                 srvlist = srv;
959                         } else {
960                                 prevsrv->lsrv_next = srv;
961                         }
962                         prevsrv = srv;
963                         
964                         /* copy in info. */
965                         if (( srv->lsrv_host = strdup( host )) == NULL ||
966                             ( server_dn != NULL && ( srv->lsrv_dn =
967                             strdup( server_dn )) == NULL )) {
968                                 free_servers( srvlist );
969                                 srvlist = NULL;
970                                 break;          /* exit loop & return */
971                         }
972                         srv->lsrv_port = port;
973                 }
974         }
975
976         ldap_value_free( dxs );
977
978         if ( srvlist == NULL ) {
979                 ld->ld_errno = LDAP_SERVER_DOWN;
980         }
981
982         return( srvlist );
983 }
984 #endif /* LDAP_DNS */