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>
25 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
26 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
27 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
28 static void free_servers LDAP_P(( LDAPServer *srvlist ));
29 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
32 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
33 static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
34 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
36 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
37 static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
38 int msgid, char **dnp ));
39 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
43 ldap_alloc_ber_with_options( LDAP *ld )
47 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
48 ld->ld_errno = LDAP_NO_MEMORY;
49 #ifdef STR_TRANSLATION
51 ldap_set_ber_options( ld, ber );
52 #endif /* STR_TRANSLATION */
60 ldap_set_ber_options( LDAP *ld, BerElement *ber )
62 ber->ber_options = ld->ld_lberoptions;
63 #ifdef STR_TRANSLATION
64 if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
65 ber_set_string_translators( ber,
66 ld->ld_lber_encode_translate_proc,
67 ld->ld_lber_decode_translate_proc );
69 #endif /* STR_TRANSLATION */
74 ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
77 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
78 LDAPServer *servers, *srv;
79 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
81 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
83 if ( ! lber_pvt_sb_in_use(&ld->ld_sb ) ) {
84 /* not connected yet */
86 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
87 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
88 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
89 strdup( ld->ld_defhost )) == NULL ))
91 if (srv != NULL) free( srv );
92 ld->ld_errno = LDAP_NO_MEMORY;
96 srv->lsrv_port = ld->ld_defport;
98 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 ))
101 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
103 ld->ld_errno = LDAP_SERVER_DOWN;
106 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
108 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
109 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
110 ld->ld_defport, &ld->ld_host, 0 ) < 0 )
112 ldap_ld_free( ld, 0 );
113 ld->ld_errno = LDAP_SERVER_DOWN;
116 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
118 Debug( LDAP_DEBUG_TRACE,
119 "ldap_delayed_open successful, ld_host is %s\n",
120 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
124 #if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
126 if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
127 ld->ld_errno = LDAP_SERVER_DOWN;
131 ld->ld_errno = LDAP_SUCCESS;
132 return( ld->ld_msgid );
133 #else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
135 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
136 if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
137 && ldap_is_dns_dn( dn ) )
139 if (( servers = dn2servers( ld, dn )) == NULL ) {
145 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
148 for ( srv = servers; srv != NULL;
149 srv = srv->lsrv_next ) {
151 "LDAP server %s: dn %s, port %d\n",
152 srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
153 "(default)" : srv->lsrv_dn,
157 #endif /* LDAP_DEBUG */
159 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
161 * use of DNS is turned off or this is an X.500 DN...
162 * use our default connection
165 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
167 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
169 return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
171 #endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
176 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
178 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
179 *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
184 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
187 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
190 if ( srvlist == NULL ) {
193 if (( lc = find_connection( ld, srvlist, 1 )) ==
195 if ( bind && (parentreq != NULL) ) {
196 /* Remember the bind in the parent */
198 ++parentreq->lr_outrefcnt;
200 lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
202 free_servers( srvlist );
206 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
208 if ( ld->ld_errno == LDAP_SUCCESS ) {
209 ld->ld_errno = LDAP_SERVER_DOWN;
212 /* Forget about the bind */
213 --parentreq->lr_outrefcnt;
218 use_connection( ld, lc );
219 if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
221 ld->ld_errno = LDAP_NO_MEMORY;
222 ldap_free_connection( ld, lc, 0, 0 );
225 /* Forget about the bind */
226 --parentreq->lr_outrefcnt;
230 lr->lr_msgid = msgid;
231 lr->lr_status = LDAP_REQST_INPROGRESS;
232 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
235 if ( parentreq != NULL ) { /* sub-request */
237 /* Increment if we didn't do it before the bind */
238 ++parentreq->lr_outrefcnt;
240 lr->lr_origid = parentreq->lr_origid;
241 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
242 lr->lr_parent = parentreq;
243 lr->lr_refnext = parentreq->lr_refnext;
244 parentreq->lr_refnext = lr;
245 } else { /* original request */
246 lr->lr_origid = lr->lr_msgid;
249 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
250 lr->lr_next->lr_prev = lr;
252 ld->ld_requests = lr;
255 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
257 if ( errno == EWOULDBLOCK ) {
258 /* need to continue write later */
259 lr->lr_status = LDAP_REQST_WRITING;
260 ldap_mark_select_write( ld, lc->lconn_sb );
263 ld->ld_errno = LDAP_SERVER_DOWN;
264 ldap_free_request( ld, lr );
265 ldap_free_connection( ld, lc, 0, 0 );
272 if ( parentreq == NULL ) {
273 ber->ber_end = ber->ber_ptr;
274 ber->ber_ptr = ber->ber_buf;
277 /* sent -- waiting for a response */
278 ldap_mark_select_read( ld, lc->lconn_sb );
281 ld->ld_errno = LDAP_SUCCESS;
287 ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
288 int connect, int bind )
291 LDAPServer *prevsrv, *srv;
295 * make a new LDAP server connection
296 * XXX open connection synchronously for now
298 if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
299 ( !use_ldsb && ( (sb = lber_pvt_sb_alloc()) == NULL ))) {
303 ld->ld_errno = LDAP_NO_MEMORY;
307 lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
312 for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
313 if ( open_ldap_connection( ld, lc->lconn_sb,
314 srv->lsrv_host, srv->lsrv_port,
315 &lc->lconn_krbinstance, 0 ) != -1 ) {
323 lber_pvt_sb_free( lc->lconn_sb );
326 ld->ld_errno = LDAP_SERVER_DOWN;
330 if ( prevsrv == NULL ) {
331 *srvlistp = srv->lsrv_next;
333 prevsrv->lsrv_next = srv->lsrv_next;
335 lc->lconn_server = srv;
338 lc->lconn_status = LDAP_CONNST_CONNECTED;
339 lc->lconn_next = ld->ld_conns;
343 * XXX for now, we always do a synchronous bind. This will have
344 * to change in the long run...
347 int err, freepasswd, authmethod;
348 char *binddn, *passwd;
349 LDAPConn *savedefconn;
351 freepasswd = err = 0;
353 if ( ld->ld_rebindproc == NULL ) {
354 binddn = passwd = "";
355 authmethod = LDAP_AUTH_SIMPLE;
357 if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
358 &authmethod, 0 )) == LDAP_SUCCESS ) {
368 savedefconn = ld->ld_defconn;
370 ++lc->lconn_refcnt; /* avoid premature free */
372 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
377 ld->ld_defconn = savedefconn;
381 (*ld->ld_rebindproc)( ld, &binddn, &passwd,
386 ldap_free_connection( ld, lc, 1, 0 );
396 find_connection( LDAP *ld, LDAPServer *srv, int any )
398 * return an existing connection (if any) to the server srv
399 * if "any" is non-zero, check for any server in the "srv" chain
405 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
406 for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
407 if ( lc->lconn_server->lsrv_host != NULL &&
408 ls->lsrv_host != NULL && strcasecmp(
409 ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
410 && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
425 use_connection( LDAP *ld, LDAPConn *lc )
428 lc->lconn_lastused = time( 0 );
433 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
435 LDAPConn *tmplc, *prevlc;
437 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
439 if ( force || --lc->lconn_refcnt <= 0 ) {
440 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
441 ldap_mark_select_clear( ld, lc->lconn_sb );
443 ldap_send_unbind( ld, lc->lconn_sb );
445 ldap_close_connection( lc->lconn_sb );
446 lber_pvt_sb_destroy( lc->lconn_sb );
449 for ( tmplc = ld->ld_conns; tmplc != NULL;
450 tmplc = tmplc->lconn_next ) {
452 if ( prevlc == NULL ) {
453 ld->ld_conns = tmplc->lconn_next;
455 prevlc->lconn_next = tmplc->lconn_next;
461 free_servers( lc->lconn_server );
462 if ( lc->lconn_krbinstance != NULL ) {
463 free( lc->lconn_krbinstance );
465 if ( lc->lconn_sb != &ld->ld_sb ) {
466 lber_pvt_sb_free( lc->lconn_sb );
469 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
472 lc->lconn_lastused = time( 0 );
473 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
474 lc->lconn_refcnt, 0, 0 );
481 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
486 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
487 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
488 if ( lc->lconn_server != NULL ) {
489 fprintf( stderr, "* host: %s port: %d%s\n",
490 ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
491 : lc->lconn_server->lsrv_host,
492 lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
493 &ld->ld_sb ) ? " (default)" : "" );
495 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
496 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
497 "NeedSocket" : ( lc->lconn_status ==
498 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
499 fprintf( stderr, " last used: %s\n",
500 ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
509 ldap_dump_requests_and_responses( LDAP *ld )
514 fprintf( stderr, "** Outstanding Requests:\n" );
515 if (( lr = ld->ld_requests ) == NULL ) {
516 fprintf( stderr, " Empty\n" );
518 for ( ; lr != NULL; lr = lr->lr_next ) {
519 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
520 lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
521 LDAP_REQST_INPROGRESS ) ? "InProgress" :
522 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
523 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
525 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
526 lr->lr_outrefcnt, lr->lr_parentcnt );
529 fprintf( stderr, "** Response Queue:\n" );
530 if (( lm = ld->ld_responses ) == NULLMSG ) {
531 fprintf( stderr, " Empty\n" );
533 for ( ; lm != NULLMSG; lm = lm->lm_next ) {
534 fprintf( stderr, " * msgid %d, type %d\n",
535 lm->lm_msgid, lm->lm_msgtype );
536 if (( l = lm->lm_chain ) != NULL ) {
537 fprintf( stderr, " chained responses:\n" );
538 for ( ; l != NULLMSG; l = l->lm_chain ) {
540 " * msgid %d, type %d\n",
541 l->lm_msgid, l->lm_msgtype );
546 #endif /* LDAP_DEBUG */
550 ldap_free_request( LDAP *ld, LDAPRequest *lr )
552 LDAPRequest *tmplr, *nextlr;
554 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
555 lr->lr_origid, lr->lr_msgid, 0 );
557 if ( lr->lr_parent != NULL ) {
558 --lr->lr_parent->lr_outrefcnt;
560 /* free all referrals (child requests) */
561 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
562 nextlr = tmplr->lr_refnext;
563 ldap_free_request( ld, tmplr );
567 if ( lr->lr_prev == NULL ) {
568 ld->ld_requests = lr->lr_next;
570 lr->lr_prev->lr_next = lr->lr_next;
573 if ( lr->lr_next != NULL ) {
574 lr->lr_next->lr_prev = lr->lr_prev;
577 if ( lr->lr_ber != NULL ) {
578 ber_free( lr->lr_ber, 1 );
581 if ( lr->lr_res_error != NULL ) {
582 free( lr->lr_res_error );
585 if ( lr->lr_res_matched != NULL ) {
586 free( lr->lr_res_matched );
594 free_servers( LDAPServer *srvlist )
598 while ( srvlist != NULL ) {
599 nextsrv = srvlist->lsrv_next;
600 if ( srvlist->lsrv_dn != NULL ) {
601 free( srvlist->lsrv_dn );
603 if ( srvlist->lsrv_host != NULL ) {
604 free( srvlist->lsrv_host );
610 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
613 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
615 * XXX merging of errors in this routine needs to be improved
618 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
620 int rc, count, len, newdn;
621 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
623 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
624 char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
625 LDAPRequest *origreq;
629 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
631 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
634 if ( *errstrp == NULL ) {
638 len = strlen( *errstrp );
639 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
640 if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
641 LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
643 p += LDAP_REF_STR_LEN;
648 if ( len < LDAP_REF_STR_LEN ) {
652 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
653 Debug( LDAP_DEBUG_ANY,
654 "more than %d referral hops (dropping)\n",
655 ld->ld_refhoplimit, 0, 0 );
656 /* XXX report as error in ld->ld_errno? */
660 /* find original request */
661 for ( origreq = lr; origreq->lr_parent != NULL;
662 origreq = origreq->lr_parent ) {
669 /* parse out & follow referrals */
670 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
671 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
673 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
675 if (( p = strchr( ref, '\n' )) != NULL ) {
682 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
683 LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
684 Debug( LDAP_DEBUG_TRACE,
685 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
686 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
688 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
689 tmpref = ref + LDAP_LDAP_REF_STR_LEN;
690 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
691 } else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
692 LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
693 Debug( LDAP_DEBUG_TRACE,
694 "chasing DX referral: <%s>\n", ref, 0, 0 );
695 tmpref = ref + LDAP_DX_REF_STR_LEN;
696 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
698 Debug( LDAP_DEBUG_TRACE,
699 "ignoring unknown referral <%s>\n", ref, 0, 0 );
700 rc = ldap_append_referral( ld, &unfollowed, ref );
706 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
713 if (( ber = re_encode_request( ld, origreq->lr_ber,
714 ++ld->ld_msgid, &refdn )) == NULL ) {
718 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
720 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
721 if (( srv = (LDAPServer *)calloc( 1,
722 sizeof( LDAPServer ))) == NULL ) {
724 ld->ld_errno = LDAP_NO_MEMORY;
728 if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
731 ld->ld_errno = LDAP_NO_MEMORY;
735 if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
737 srv->lsrv_port = atoi( ports );
739 srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
741 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
743 srv = dn2servers( ld, tmpref );
745 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
747 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
748 lr, srv, NULL, 1 ) >= 0 ) {
751 Debug( LDAP_DEBUG_ANY,
752 "Unable to chase referral (%s)\n",
753 ldap_err2string( ld->ld_errno ), 0, 0 );
754 rc = ldap_append_referral( ld, &unfollowed, ref );
757 if ( !newdn && refdn != NULL ) {
763 *errstrp = unfollowed;
765 return(( rc == 0 ) ? count : rc );
770 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
774 if ( *referralsp == NULL ) {
776 *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
780 *referralsp = (char *)realloc( *referralsp,
781 strlen( *referralsp ) + strlen( s ) + 2 );
784 if ( *referralsp == NULL ) {
785 ld->ld_errno = LDAP_NO_MEMORY;
790 strcpy( *referralsp, LDAP_REF_STR );
792 strcat( *referralsp, "\n" );
794 strcat( *referralsp, s );
802 re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
805 * XXX this routine knows way too much about how the lber library works!
807 unsigned long along, tag;
810 BerElement tmpber, *ber;
813 Debug( LDAP_DEBUG_TRACE,
814 "re_encode_request: new msgid %d, new dn <%s>\n",
815 msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
820 * all LDAP requests are sequences that start with a message id,
821 * followed by a sequence that is tagged with the operation code
823 if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
824 ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
825 ld->ld_errno = LDAP_DECODING_ERROR;
829 if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
833 /* bind requests have a version number before the DN & other stuff */
834 if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
836 ld->ld_errno = LDAP_DECODING_ERROR;
841 /* the rest of the request is the DN followed by other stuff */
842 if ( ber_get_stringa( &tmpber, &orig_dn ) == LBER_DEFAULT ) {
847 if ( *dnp == NULL ) {
853 if ( tag == LDAP_REQ_BIND ) {
854 rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
856 rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
864 if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
865 tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
866 ber_printf( ber, "}}" ) == -1 ) {
867 ld->ld_errno = LDAP_ENCODING_ERROR;
873 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
874 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
876 lber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
878 #endif /* LDAP_DEBUG */
885 ldap_find_request_by_msgid( LDAP *ld, int msgid )
889 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
890 if ( msgid == lr->lr_msgid ) {
897 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
900 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
902 dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */
904 char *p, *domain, *host, *server_dn, **dxs;
906 LDAPServer *srvlist, *prevsrv, *srv;
908 if (( domain = strrchr( dn, '@' )) != NULL ) {
914 if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
915 ld->ld_errno = LDAP_NO_MEMORY;
921 for ( i = 0; dxs[ i ] != NULL; ++i ) {
922 port = openldap_ldap_global_options.ldo_defport;
924 if ( strchr( dxs[ i ], ':' ) == NULL ) {
926 } else if ( strlen( dxs[ i ] ) >= 7 &&
927 strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
929 if (( p = strchr( host, ':' )) == NULL ) {
935 if (( p = strchr( p, '/' )) != NULL ) {
937 if ( *server_dn == '\0' ) {
945 if ( host != NULL ) { /* found a server we can use */
946 if (( srv = (LDAPServer *)calloc( 1,
947 sizeof( LDAPServer ))) == NULL ) {
948 free_servers( srvlist );
950 break; /* exit loop & return */
953 /* add to end of list of servers */
954 if ( srvlist == NULL ) {
957 prevsrv->lsrv_next = srv;
962 if (( srv->lsrv_host = strdup( host )) == NULL ||
963 ( server_dn != NULL && ( srv->lsrv_dn =
964 strdup( server_dn )) == NULL )) {
965 free_servers( srvlist );
967 break; /* exit loop & return */
969 srv->lsrv_port = port;
973 ldap_value_free( dxs );
975 if ( srvlist == NULL ) {
976 ld->ld_errno = LDAP_SERVER_DOWN;
981 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */