/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/* Portions
* Copyright (c) 1995 Regents of the University of Michigan.
* All rights reserved.
*
#include "portable.h"
-#ifndef lint
-static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
#include <stdio.h>
#include <stdlib.h>
#include <ac/unistd.h>
#include "ldap-int.h"
+#include "lber.h"
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
static void free_servers LDAP_P(( LDAPServer *srvlist ));
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
int msgid, char **dnp ));
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
BerElement *
ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
BerElement *ber )
{
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
- LDAPServer *servers;
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
+ LDAPServer *servers, *srv;
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
-#if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
+ if ( ! lber_pvt_sb_in_use(&ld->ld_sb ) ) {
+ /* not connected yet */
+
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+ if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
+ NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
+ strdup( ld->ld_defhost )) == NULL ))
+ {
+ if (srv != NULL) free( srv );
+ ld->ld_errno = LDAP_NO_MEMORY;
+ return( -1 );
+ }
+
+ srv->lsrv_port = ld->ld_defport;
+
+ if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 ))
+ == NULL )
+ {
+ if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
+ free( (char *)srv );
+ ld->ld_errno = LDAP_SERVER_DOWN;
+ return( -1 );
+ }
+ ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
+
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+ if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
+ ld->ld_defport, &ld->ld_host, 0 ) < 0 )
+ {
+ ldap_ld_free( ld, 0 );
+ ld->ld_errno = LDAP_SERVER_DOWN;
+ return( -1 );
+ }
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+
+ Debug( LDAP_DEBUG_TRACE,
+ "ldap_delayed_open successful, ld_host is %s\n",
+ ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
+ }
+
+
+#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
+
if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
ld->ld_errno = LDAP_SERVER_DOWN;
return( -1 );
ld->ld_errno = LDAP_SUCCESS;
return( ld->ld_msgid );
-#else /* !LDAP_REFERRALS && !LDAP_DNS */
+#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_DNS
- if (( LDAP_BOOL_GET(ld, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+ if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
&& ldap_is_dns_dn( dn ) )
{
if (( servers = dn2servers( ld, dn )) == NULL ) {
}
#endif /* LDAP_DEBUG */
} else {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
/*
* use of DNS is turned off or this is an X.500 DN...
* use our default connection
*/
servers = NULL;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
NULL, 0 ));
-#endif /* !LDAP_REFERRALS && !LDAP_DNS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
}
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
int
ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
*parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
#ifdef notyet
- extern int errno;
-
if ( errno == EWOULDBLOCK ) {
/* need to continue write later */
lr->lr_status = LDAP_REQST_WRITING;
* XXX open connection synchronously for now
*/
if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
- ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
- == NULL )) {
+ ( !use_ldsb && ( (sb = lber_pvt_sb_alloc()) == NULL ))) {
if ( lc != NULL ) {
free( (char *)lc );
}
if ( srv == NULL ) {
if ( !use_ldsb ) {
- free( (char *)lc->lconn_sb );
+ lber_pvt_sb_free( lc->lconn_sb );
}
free( (char *)lc );
ld->ld_errno = LDAP_SERVER_DOWN;
freepasswd = err = 0;
- if ( ld->ld_rebindproc == NULL ) {
+ if ( ld->ld_rebindproc == 0 ) {
binddn = passwd = "";
authmethod = LDAP_AUTH_SIMPLE;
} else {
ldap_send_unbind( ld, lc->lconn_sb );
}
ldap_close_connection( lc->lconn_sb );
- if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
- free( lc->lconn_sb->sb_ber.ber_buf );
- }
+ lber_pvt_sb_destroy( lc->lconn_sb );
+ ber_clear( &lc->lconn_ber, 1 );
}
prevlc = NULL;
for ( tmplc = ld->ld_conns; tmplc != NULL;
free( lc->lconn_krbinstance );
}
if ( lc->lconn_sb != &ld->ld_sb ) {
- free( (char *)lc->lconn_sb );
+ lber_pvt_sb_free( lc->lconn_sb );
}
free( lc );
Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
{
LDAPConn *lc;
+ char timebuf[32];
fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
"NeedSocket" : ( lc->lconn_status ==
LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
fprintf( stderr, " last used: %s\n",
- ctime( &lc->lconn_lastused ));
+ ldap_pvt_ctime( &lc->lconn_lastused, timebuf ));
if ( !all ) {
break;
}
srvlist = nextsrv;
}
}
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
/*
* XXX merging of errors in this routine needs to be improved
*/
ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
{
int rc, count, len, newdn;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
int ldapref;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
LDAPRequest *origreq;
LDAPServer *srv;
/* parse out & follow referrals */
for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
ldapref = 0;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
if (( p = strchr( ref, '\n' )) != NULL ) {
*p++ = '\0';
LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
Debug( LDAP_DEBUG_TRACE,
"chasing LDAP referral: <%s>\n", ref, 0, 0 );
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
ldapref = 1;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
tmpref = ref + LDAP_LDAP_REF_STR_LEN;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
} else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
Debug( LDAP_DEBUG_TRACE,
"chasing DX referral: <%s>\n", ref, 0, 0 );
tmpref = ref + LDAP_DX_REF_STR_LEN;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
} else {
Debug( LDAP_DEBUG_TRACE,
"ignoring unknown referral <%s>\n", ref, 0, 0 );
return( -1 );
}
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
if ( ldapref ) {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
if (( srv = (LDAPServer *)calloc( 1,
sizeof( LDAPServer ))) == NULL ) {
ber_free( ber, 1 );
*ports++ = '\0';
srv->lsrv_port = atoi( ports );
} else {
- srv->lsrv_port = LDAP_PORT;
+ srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
}
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
} else {
srv = dn2servers( ld, tmpref );
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
lr, srv, NULL, 1 ) >= 0 ) {
if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
0, 0, 0 );
- ber_dump( ber, 0 );
+ lber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 );
}
#endif /* LDAP_DEBUG */
return( lr );
}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
static LDAPServer *
dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */
{
srvlist = NULL;
for ( i = 0; dxs[ i ] != NULL; ++i ) {
- port = LDAP_PORT;
+ port = openldap_ldap_global_options.ldo_defport;
server_dn = NULL;
if ( strchr( dxs[ i ], ':' ) == NULL ) {
host = dxs[ i ];
return( srvlist );
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */