2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * request.c - sending of ldap requests; handling of referrals
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 #include <ac/unistd.h>
26 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
27 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
28 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
29 static void free_servers LDAP_P(( LDAPServer *srvlist ));
30 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
33 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
34 static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
35 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
37 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
38 static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
39 int msgid, char **dnp ));
40 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
44 ldap_alloc_ber_with_options( LDAP *ld )
48 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
49 ld->ld_errno = LDAP_NO_MEMORY;
50 #ifdef STR_TRANSLATION
52 ldap_set_ber_options( ld, ber );
53 #endif /* STR_TRANSLATION */
61 ldap_set_ber_options( LDAP *ld, BerElement *ber )
63 ber->ber_options = ld->ld_lberoptions;
64 #ifdef STR_TRANSLATION
65 if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
66 ber_set_string_translators( ber,
67 ld->ld_lber_encode_translate_proc,
68 ld->ld_lber_decode_translate_proc );
70 #endif /* STR_TRANSLATION */
75 ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
78 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
79 LDAPServer *servers, *srv;
80 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
82 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
84 if ( ! lber_pvt_sb_in_use(&ld->ld_sb ) ) {
85 /* not connected yet */
87 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
88 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
89 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
90 strdup( ld->ld_defhost )) == NULL ))
92 if (srv != NULL) free( srv );
94 ld->ld_errno = LDAP_NO_MEMORY;
98 srv->lsrv_port = ld->ld_defport;
100 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 ))
103 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
106 ld->ld_errno = LDAP_SERVER_DOWN;
109 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
111 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
112 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
113 ld->ld_defport, &ld->ld_host, 0 ) < 0 )
115 ldap_ld_free( ld, 0 );
117 ld->ld_errno = LDAP_SERVER_DOWN;
120 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
122 Debug( LDAP_DEBUG_TRACE,
123 "ldap_delayed_open successful, ld_host is %s\n",
124 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
128 #if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
130 if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
131 ld->ld_errno = LDAP_SERVER_DOWN;
135 ld->ld_errno = LDAP_SUCCESS;
136 return( ld->ld_msgid );
137 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
139 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
140 if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
141 && ldap_is_dns_dn( dn ) )
143 if (( servers = dn2servers( ld, dn )) == NULL ) {
149 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
152 for ( srv = servers; srv != NULL;
153 srv = srv->lsrv_next ) {
155 "LDAP server %s: dn %s, port %d\n",
156 srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
157 "(default)" : srv->lsrv_dn,
161 #endif /* LDAP_DEBUG */
163 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
165 * use of DNS is turned off or this is an X.500 DN...
166 * use our default connection
169 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
171 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
173 return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
175 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
180 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
182 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
183 *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
188 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
191 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
194 if ( srvlist == NULL ) {
197 if (( lc = find_connection( ld, srvlist, 1 )) ==
199 if ( bind && (parentreq != NULL) ) {
200 /* Remember the bind in the parent */
202 ++parentreq->lr_outrefcnt;
204 lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
206 free_servers( srvlist );
210 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
212 if ( ld->ld_errno == LDAP_SUCCESS ) {
213 ld->ld_errno = LDAP_SERVER_DOWN;
216 /* Forget about the bind */
217 --parentreq->lr_outrefcnt;
222 use_connection( ld, lc );
223 if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
225 ld->ld_errno = LDAP_NO_MEMORY;
226 ldap_free_connection( ld, lc, 0, 0 );
229 /* Forget about the bind */
230 --parentreq->lr_outrefcnt;
234 lr->lr_msgid = msgid;
235 lr->lr_status = LDAP_REQST_INPROGRESS;
236 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
239 if ( parentreq != NULL ) { /* sub-request */
241 /* Increment if we didn't do it before the bind */
242 ++parentreq->lr_outrefcnt;
244 lr->lr_origid = parentreq->lr_origid;
245 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
246 lr->lr_parent = parentreq;
247 lr->lr_refnext = parentreq->lr_refnext;
248 parentreq->lr_refnext = lr;
249 } else { /* original request */
250 lr->lr_origid = lr->lr_msgid;
253 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
254 lr->lr_next->lr_prev = lr;
256 ld->ld_requests = lr;
259 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
261 if ( errno == EWOULDBLOCK ) {
262 /* need to continue write later */
263 lr->lr_status = LDAP_REQST_WRITING;
264 ldap_mark_select_write( ld, lc->lconn_sb );
267 ld->ld_errno = LDAP_SERVER_DOWN;
268 ldap_free_request( ld, lr );
269 ldap_free_connection( ld, lc, 0, 0 );
276 if ( parentreq == NULL ) {
277 ber->ber_end = ber->ber_ptr;
278 ber->ber_ptr = ber->ber_buf;
281 /* sent -- waiting for a response */
282 ldap_mark_select_read( ld, lc->lconn_sb );
285 ld->ld_errno = LDAP_SUCCESS;
291 ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
292 int connect, int bind )
295 LDAPServer *prevsrv, *srv;
299 * make a new LDAP server connection
300 * XXX open connection synchronously for now
302 if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
303 ( !use_ldsb && ( (sb = lber_pvt_sb_alloc()) == NULL ))) {
307 ld->ld_errno = LDAP_NO_MEMORY;
311 lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
316 for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
317 if ( open_ldap_connection( ld, lc->lconn_sb,
318 srv->lsrv_host, srv->lsrv_port,
319 &lc->lconn_krbinstance, 0 ) != -1 ) {
327 lber_pvt_sb_free( lc->lconn_sb );
330 ld->ld_errno = LDAP_SERVER_DOWN;
334 if ( prevsrv == NULL ) {
335 *srvlistp = srv->lsrv_next;
337 prevsrv->lsrv_next = srv->lsrv_next;
339 lc->lconn_server = srv;
342 lc->lconn_status = LDAP_CONNST_CONNECTED;
343 lc->lconn_next = ld->ld_conns;
347 * XXX for now, we always do a synchronous bind. This will have
348 * to change in the long run...
351 int err, freepasswd, authmethod;
352 char *binddn, *passwd;
353 LDAPConn *savedefconn;
355 freepasswd = err = 0;
357 if ( ld->ld_rebindproc == 0 ) {
358 binddn = passwd = "";
359 authmethod = LDAP_AUTH_SIMPLE;
361 if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
362 &authmethod, 0 )) == LDAP_SUCCESS ) {
372 savedefconn = ld->ld_defconn;
374 ++lc->lconn_refcnt; /* avoid premature free */
376 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
381 ld->ld_defconn = savedefconn;
385 (*ld->ld_rebindproc)( ld, &binddn, &passwd,
390 ldap_free_connection( ld, lc, 1, 0 );
400 find_connection( LDAP *ld, LDAPServer *srv, int any )
402 * return an existing connection (if any) to the server srv
403 * if "any" is non-zero, check for any server in the "srv" chain
409 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
410 for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
411 if ( lc->lconn_server->lsrv_host != NULL &&
412 ls->lsrv_host != NULL && strcasecmp(
413 ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
414 && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
429 use_connection( LDAP *ld, LDAPConn *lc )
432 lc->lconn_lastused = time( 0 );
437 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
439 LDAPConn *tmplc, *prevlc;
441 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
443 if ( force || --lc->lconn_refcnt <= 0 ) {
444 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
445 ldap_mark_select_clear( ld, lc->lconn_sb );
447 ldap_send_unbind( ld, lc->lconn_sb );
449 ldap_close_connection( lc->lconn_sb );
450 lber_pvt_sb_destroy( lc->lconn_sb );
451 ber_clear( &lc->lconn_ber, 1 );
454 for ( tmplc = ld->ld_conns; tmplc != NULL;
455 tmplc = tmplc->lconn_next ) {
457 if ( prevlc == NULL ) {
458 ld->ld_conns = tmplc->lconn_next;
460 prevlc->lconn_next = tmplc->lconn_next;
466 free_servers( lc->lconn_server );
467 if ( lc->lconn_krbinstance != NULL ) {
468 free( lc->lconn_krbinstance );
470 if ( lc->lconn_sb != &ld->ld_sb ) {
471 lber_pvt_sb_free( lc->lconn_sb );
474 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
477 lc->lconn_lastused = time( 0 );
478 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
479 lc->lconn_refcnt, 0, 0 );
486 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
491 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
492 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
493 if ( lc->lconn_server != NULL ) {
494 fprintf( stderr, "* host: %s port: %d%s\n",
495 ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
496 : lc->lconn_server->lsrv_host,
497 lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
498 &ld->ld_sb ) ? " (default)" : "" );
500 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
501 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
502 "NeedSocket" : ( lc->lconn_status ==
503 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
504 fprintf( stderr, " last used: %s\n",
505 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
514 ldap_dump_requests_and_responses( LDAP *ld )
519 fprintf( stderr, "** Outstanding Requests:\n" );
520 if (( lr = ld->ld_requests ) == NULL ) {
521 fprintf( stderr, " Empty\n" );
523 for ( ; lr != NULL; lr = lr->lr_next ) {
524 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
525 lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
526 LDAP_REQST_INPROGRESS ) ? "InProgress" :
527 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
528 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
530 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
531 lr->lr_outrefcnt, lr->lr_parentcnt );
534 fprintf( stderr, "** Response Queue:\n" );
535 if (( lm = ld->ld_responses ) == NULLMSG ) {
536 fprintf( stderr, " Empty\n" );
538 for ( ; lm != NULLMSG; lm = lm->lm_next ) {
539 fprintf( stderr, " * msgid %d, type %d\n",
540 lm->lm_msgid, lm->lm_msgtype );
541 if (( l = lm->lm_chain ) != NULL ) {
542 fprintf( stderr, " chained responses:\n" );
543 for ( ; l != NULLMSG; l = l->lm_chain ) {
545 " * msgid %d, type %d\n",
546 l->lm_msgid, l->lm_msgtype );
551 #endif /* LDAP_DEBUG */
555 ldap_free_request( LDAP *ld, LDAPRequest *lr )
557 LDAPRequest *tmplr, *nextlr;
559 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
560 lr->lr_origid, lr->lr_msgid, 0 );
562 if ( lr->lr_parent != NULL ) {
563 --lr->lr_parent->lr_outrefcnt;
565 /* free all referrals (child requests) */
566 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
567 nextlr = tmplr->lr_refnext;
568 ldap_free_request( ld, tmplr );
572 if ( lr->lr_prev == NULL ) {
573 ld->ld_requests = lr->lr_next;
575 lr->lr_prev->lr_next = lr->lr_next;
578 if ( lr->lr_next != NULL ) {
579 lr->lr_next->lr_prev = lr->lr_prev;
582 if ( lr->lr_ber != NULL ) {
583 ber_free( lr->lr_ber, 1 );
586 if ( lr->lr_res_error != NULL ) {
587 free( lr->lr_res_error );
590 if ( lr->lr_res_matched != NULL ) {
591 free( lr->lr_res_matched );
599 free_servers( LDAPServer *srvlist )
603 while ( srvlist != NULL ) {
604 nextsrv = srvlist->lsrv_next;
605 if ( srvlist->lsrv_dn != NULL ) {
606 free( srvlist->lsrv_dn );
608 if ( srvlist->lsrv_host != NULL ) {
609 free( srvlist->lsrv_host );
615 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
618 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
620 * XXX merging of errors in this routine needs to be improved
623 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
625 int rc, count, len, newdn;
626 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
628 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
629 char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
630 LDAPRequest *origreq;
634 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
636 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
639 if ( *errstrp == NULL ) {
643 len = strlen( *errstrp );
644 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
645 if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
646 LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
648 p += LDAP_REF_STR_LEN;
653 if ( len < LDAP_REF_STR_LEN ) {
657 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
658 Debug( LDAP_DEBUG_ANY,
659 "more than %d referral hops (dropping)\n",
660 ld->ld_refhoplimit, 0, 0 );
661 /* XXX report as error in ld->ld_errno? */
665 /* find original request */
666 for ( origreq = lr; origreq->lr_parent != NULL;
667 origreq = origreq->lr_parent ) {
674 /* parse out & follow referrals */
675 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
676 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
678 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
680 if (( p = strchr( ref, '\n' )) != NULL ) {
687 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
688 LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
689 Debug( LDAP_DEBUG_TRACE,
690 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
691 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
693 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
694 tmpref = ref + LDAP_LDAP_REF_STR_LEN;
695 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
696 } else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
697 LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
698 Debug( LDAP_DEBUG_TRACE,
699 "chasing DX referral: <%s>\n", ref, 0, 0 );
700 tmpref = ref + LDAP_DX_REF_STR_LEN;
701 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
703 Debug( LDAP_DEBUG_TRACE,
704 "ignoring unknown referral <%s>\n", ref, 0, 0 );
705 rc = ldap_append_referral( ld, &unfollowed, ref );
711 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
718 if (( ber = re_encode_request( ld, origreq->lr_ber,
719 ++ld->ld_msgid, &refdn )) == NULL ) {
723 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
725 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
726 if (( srv = (LDAPServer *)calloc( 1,
727 sizeof( LDAPServer ))) == NULL ) {
729 ld->ld_errno = LDAP_NO_MEMORY;
733 if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
736 ld->ld_errno = LDAP_NO_MEMORY;
740 if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
742 srv->lsrv_port = atoi( ports );
744 srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
746 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
748 srv = dn2servers( ld, tmpref );
750 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
752 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
753 lr, srv, NULL, 1 ) >= 0 ) {
756 Debug( LDAP_DEBUG_ANY,
757 "Unable to chase referral (%s)\n",
758 ldap_err2string( ld->ld_errno ), 0, 0 );
759 rc = ldap_append_referral( ld, &unfollowed, ref );
762 if ( !newdn && refdn != NULL ) {
768 *errstrp = unfollowed;
770 return(( rc == 0 ) ? count : rc );
775 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
779 if ( *referralsp == NULL ) {
781 *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
785 *referralsp = (char *)realloc( *referralsp,
786 strlen( *referralsp ) + strlen( s ) + 2 );
789 if ( *referralsp == NULL ) {
790 ld->ld_errno = LDAP_NO_MEMORY;
795 strcpy( *referralsp, LDAP_REF_STR );
797 strcat( *referralsp, "\n" );
799 strcat( *referralsp, s );
807 re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
810 * XXX this routine knows way too much about how the lber library works!
812 unsigned long along, tag;
815 BerElement tmpber, *ber;
818 Debug( LDAP_DEBUG_TRACE,
819 "re_encode_request: new msgid %d, new dn <%s>\n",
820 msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
825 * all LDAP requests are sequences that start with a message id,
826 * followed by a sequence that is tagged with the operation code
828 if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
829 ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
830 ld->ld_errno = LDAP_DECODING_ERROR;
834 if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
838 /* bind requests have a version number before the DN & other stuff */
839 if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
841 ld->ld_errno = LDAP_DECODING_ERROR;
846 /* the rest of the request is the DN followed by other stuff */
847 if ( ber_get_stringa( &tmpber, &orig_dn ) == LBER_DEFAULT ) {
852 if ( *dnp == NULL ) {
858 if ( tag == LDAP_REQ_BIND ) {
859 rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
861 rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
869 if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
870 tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
871 ber_printf( ber, "}}" ) == -1 ) {
872 ld->ld_errno = LDAP_ENCODING_ERROR;
878 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
879 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
881 lber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
883 #endif /* LDAP_DEBUG */
890 ldap_find_request_by_msgid( LDAP *ld, int msgid )
894 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
895 if ( msgid == lr->lr_msgid ) {
902 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
905 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
907 dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */
909 char *p, *domain, *host, *server_dn, **dxs;
911 LDAPServer *srvlist, *prevsrv, *srv;
913 if (( domain = strrchr( dn, '@' )) != NULL ) {
919 if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
920 ld->ld_errno = LDAP_NO_MEMORY;
926 for ( i = 0; dxs[ i ] != NULL; ++i ) {
927 port = openldap_ldap_global_options.ldo_defport;
929 if ( strchr( dxs[ i ], ':' ) == NULL ) {
931 } else if ( strlen( dxs[ i ] ) >= 7 &&
932 strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
934 if (( p = strchr( host, ':' )) == NULL ) {
940 if (( p = strchr( p, '/' )) != NULL ) {
942 if ( *server_dn == '\0' ) {
950 if ( host != NULL ) { /* found a server we can use */
951 if (( srv = (LDAPServer *)calloc( 1,
952 sizeof( LDAPServer ))) == NULL ) {
953 free_servers( srvlist );
955 break; /* exit loop & return */
958 /* add to end of list of servers */
959 if ( srvlist == NULL ) {
962 prevsrv->lsrv_next = srv;
967 if (( srv->lsrv_host = strdup( host )) == NULL ||
968 ( server_dn != NULL && ( srv->lsrv_dn =
969 strdup( server_dn )) == NULL )) {
970 free_servers( srvlist );
972 break; /* exit loop & return */
974 srv->lsrv_port = port;
978 ldap_value_free( dxs );
980 if ( srvlist == NULL ) {
981 ld->ld_errno = LDAP_SERVER_DOWN;
986 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */