From 3b03b64b771679d935188b7e0b46866ad5608aef Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Tue, 15 Aug 2000 01:55:43 +0000 Subject: [PATCH] Add char* ldap_pvt_get_fqdn(char*) which returns the FQDN of the input. In input==NULL, returns FQDN of local host. Fixed copy_hostent() uninitialized pointer bug. Replaced gethostname calls with ldap_pvt_get_fqdn( NULL ) calls. --- include/ldap_pvt.h | 2 ++ libraries/libldap/init.c | 10 ++-------- libraries/libldap/util-int.c | 31 +++++++++++++++++++++++++++++++ libraries/liblutil/authpasswd.c | 8 ++++---- libraries/liblutil/passwd.c | 8 ++++---- servers/slapd/sasl.c | 7 +------ 6 files changed, 44 insertions(+), 22 deletions(-) diff --git a/include/ldap_pvt.h b/include/ldap_pvt.h index 92e287cdfd..5a2fa336cd 100644 --- a/include/ldap_pvt.h +++ b/include/ldap_pvt.h @@ -45,6 +45,8 @@ ldap_pvt_ctime LDAP_P(( const time_t *tp, char *buf )); +LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * )); + LDAP_F( int ) ldap_pvt_gethostbyname_a LDAP_P(( const char *name, diff --git a/libraries/libldap/init.c b/libraries/libldap/init.c index b8e23caaf2..1941a8b980 100644 --- a/libraries/libldap/init.c +++ b/libraries/libldap/init.c @@ -427,7 +427,7 @@ void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \ || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL) -char * ldap_int_hostname = "localhost"; +char * ldap_int_hostname = NULL; #endif void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl ) @@ -438,13 +438,7 @@ void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl ) #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \ || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL) - { - static char hostbuf[MAXHOSTNAMELEN+1]; - if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) { - hostbuf[MAXHOSTNAMELEN] = '\0'; - ldap_int_hostname = hostbuf; - } - } + ldap_int_hostname = ldap_pvt_get_fqdn( ldap_int_hostname ); #endif ldap_int_utils_init(); diff --git a/libraries/libldap/util-int.c b/libraries/libldap/util-int.c index 126b573cb2..0cc70589d6 100644 --- a/libraries/libldap/util-int.c +++ b/libraries/libldap/util-int.c @@ -142,6 +142,7 @@ int ldap_pvt_gethostbyname_a( # define NEED_COPY_HOSTENT struct hostent *he; int retval; + *buf = NULL; ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex ); @@ -162,6 +163,7 @@ int ldap_pvt_gethostbyname_a( return retval; #else + *buf = NULL; *result = gethostbyname( name ); if (*result!=NULL) { @@ -221,6 +223,7 @@ int ldap_pvt_gethostbyaddr_a( # define NEED_COPY_HOSTENT struct hostent *he; int retval; + *buf = NULL; ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex ); @@ -241,6 +244,7 @@ int ldap_pvt_gethostbyaddr_a( return retval; #else /* gethostbyaddr() */ + *buf = NULL; *result = gethostbyaddr( addr, len, type ); if (*result!=NULL) { @@ -377,4 +381,31 @@ static char *safe_realloc( char **buf, int len ) } #endif +char * ldap_pvt_get_fqdn( char *name ) +{ + char *fqdn, *ha_buf; + char hostbuf[MAXHOSTNAMELEN+1]; + struct hostent *hp, he_buf; + int rc, local_h_errno; + + if( name == NULL ) { + if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) { + hostbuf[MAXHOSTNAMELEN] = '\0'; + name = hostbuf; + } else { + name = "localhost"; + } + } + + rc = ldap_pvt_gethostbyname_a( name, + &he_buf, &ha_buf, &hp, &local_h_errno ); + + if( rc < 0 || hp == NULL || hp->h_name == NULL ) { + fqdn = LDAP_STRDUP( name ); + } else { + fqdn = LDAP_STRDUP( hp->h_name ); + } + LDAP_FREE( ha_buf ); + return fqdn; +} diff --git a/libraries/liblutil/authpasswd.c b/libraries/liblutil/authpasswd.c index e179f32dbc..b7dab5a25d 100644 --- a/libraries/liblutil/authpasswd.c +++ b/libraries/liblutil/authpasswd.c @@ -609,18 +609,18 @@ static int chk_kerberos( } { - char host[MAXHOSTNAMELEN+1]; + char *host = ldap_pvt_get_fqdn( NULL ); - if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) { + if( host == NULL ) { krb5_free_principal( context, client ); krb5_free_context( context ); return 1; } - host[MAXHOSTNAMELEN] = '\0'; - ret = krb5_sname_to_principal( context, host, "ldap", KRB5_NT_SRV_HST, &server ); + + ber_memfree( host ); } if (ret) { diff --git a/libraries/liblutil/passwd.c b/libraries/liblutil/passwd.c index 68c2709ab6..eca2ff12b1 100644 --- a/libraries/liblutil/passwd.c +++ b/libraries/liblutil/passwd.c @@ -726,18 +726,18 @@ static int chk_kerberos( } { - char host[MAXHOSTNAMELEN+1]; + char *host = ldap_pvt_get_fqdn( NULL ); - if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) { + if( host == NULL ) { krb5_free_principal( context, client ); krb5_free_context( context ); return 1; } - host[MAXHOSTNAMELEN] = '\0'; - ret = krb5_sname_to_principal( context, host, "ldap", KRB5_NT_SRV_HST, &server ); + + ber_memfree( host ); } if (ret) { diff --git a/servers/slapd/sasl.c b/servers/slapd/sasl.c index 6e46d65fe3..2f37bb0f85 100644 --- a/servers/slapd/sasl.c +++ b/servers/slapd/sasl.c @@ -196,12 +196,7 @@ int slap_sasl_init( void ) } if( sasl_host == NULL ) { - static char hostname[MAXHOSTNAMELEN+1]; - - if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) { - hostname[MAXHOSTNAMELEN] = '\0'; - sasl_host = hostname; - } + sasl_host = ldap_pvt_get_fqdn( NULL ); } Debug( LDAP_DEBUG_TRACE, -- 2.39.5