From ba0c0e022c151dc0db260e2a2b79e452000e87a2 Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Sun, 31 Jan 1999 01:09:00 +0000 Subject: [PATCH] Fix ldap_send_initial_request() to open connection if not already openned (by ldap_open()). This allows ldap_init() to function properly! --- clients/tools/ldapdelete.c | 4 ++-- clients/tools/ldapmodify.c | 4 ++-- clients/tools/ldapmodrdn.c | 4 ++-- clients/tools/ldappasswd.c | 4 ++-- clients/tools/ldapsearch.c | 12 +++++----- libraries/libldap/open.c | 8 ++++--- libraries/libldap/request.c | 44 ++++++++++++++++++++++++++++++++++++- 7 files changed, 62 insertions(+), 18 deletions(-) diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c index eac2d12064..01c47fe645 100644 --- a/clients/tools/ldapdelete.c +++ b/clients/tools/ldapdelete.c @@ -122,8 +122,8 @@ main( int argc, char **argv ) (void) SIGNAL( SIGPIPE, SIG_IGN ); #endif - if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) { - perror( "ldap_open" ); + if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) { + perror( "ldap_init" ); exit( 1 ); } diff --git a/clients/tools/ldapmodify.c b/clients/tools/ldapmodify.c index 5b18950dc7..4b5ac118ab 100644 --- a/clients/tools/ldapmodify.c +++ b/clients/tools/ldapmodify.c @@ -184,8 +184,8 @@ main( int argc, char **argv ) #endif if ( !not ) { - if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) { - perror( "ldap_open" ); + if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) { + perror( "ldap_init" ); exit( 1 ); } diff --git a/clients/tools/ldapmodrdn.c b/clients/tools/ldapmodrdn.c index 267cc780cf..6319f68b48 100644 --- a/clients/tools/ldapmodrdn.c +++ b/clients/tools/ldapmodrdn.c @@ -146,8 +146,8 @@ main(int argc, char **argv) (void) SIGNAL( SIGPIPE, SIG_IGN ); #endif - if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) { - perror( "ldap_open" ); + if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) { + perror( "ldap_init" ); exit( 1 ); } diff --git a/clients/tools/ldappasswd.c b/clients/tools/ldappasswd.c index d6e079a25c..b8ef876c6c 100644 --- a/clients/tools/ldappasswd.c +++ b/clients/tools/ldappasswd.c @@ -568,9 +568,9 @@ main (int argc, char *argv[]) #endif /* connect to server */ - if ((ld = ldap_open (ldaphost, ldapport)) == NULL) + if ((ld = ldap_init (ldaphost, ldapport)) == NULL) { - perror (ldaphost); + perror ("ldap_init"); exit (1); } diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index 87a5543013..dc1a319dd6 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -254,10 +254,6 @@ main( int argc, char **argv ) } } - if ( verbose ) { - printf( "ldap_open( %s, %d )\n", ldaphost, ldapport ); - } - if ( debug ) { lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ); ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ); @@ -268,8 +264,12 @@ main( int argc, char **argv ) (void) SIGNAL( SIGPIPE, SIG_IGN ); #endif - if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) { - perror( ldaphost ); + if ( verbose ) { + printf( "ldap_init( %s, %d )\n", ldaphost, ldapport ); + } + + if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) { + perror( "ldap_init" ); exit( 1 ); } diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c index 913c07fcfe..5792b92e0a 100644 --- a/libraries/libldap/open.c +++ b/libraries/libldap/open.c @@ -48,13 +48,11 @@ ldap_open( char *host, int port ) return( NULL ); } - /* we'll assume we're talking version 2 for now */ - ld->ld_version = LDAP_VERSION2; - #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( (char*) srv ); ldap_ld_free( ld, 0 ); return( NULL ); } @@ -195,6 +193,10 @@ ldap_init( char *defhost, int defport ) #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */ #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */ + /* we'll assume we're talking version 2 for now */ + ld->ld_version = LDAP_VERSION2; + + ld->ld_sb.sb_sd = -1; return( ld ); } diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index 7ea8e32393..6dfa9d1718 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -75,12 +75,54 @@ ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn, BerElement *ber ) { #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS ) - LDAPServer *servers; + 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 ( ld->ld_sb.sb_sd == -1 ) { + /* 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 ); -- 2.39.5