From d324ab5c74d1dc207fe4dfea2dc8ea9916a14f6b Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Fri, 9 Sep 2011 01:07:17 +0200 Subject: [PATCH] import ITS#6925 patch; fix peeraddr variable name clash in LDAP_CONNECTIONLESS code; fix scope of addr[] when inet_ntop(3) is used --- servers/slapd/connection.c | 10 +++++- servers/slapd/daemon.c | 65 ++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c index 130838ced8..7d14e0c818 100644 --- a/servers/slapd/connection.c +++ b/servers/slapd/connection.c @@ -1500,12 +1500,20 @@ connection_input( Connection *conn , conn_readinfo *cri ) #ifdef LDAP_CONNECTIONLESS if ( conn->c_is_udp ) { char peername[sizeof("IP=255.255.255.255:65336")]; + const char *peeraddr_string = NULL; len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr)); if (len != sizeof(struct sockaddr)) return 1; +#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) + char addr[INET_ADDRSTRLEN]; + peeraddr_string = inet_ntop( AF_INET, &peeraddr.sa_in_addr.sin_addr, + addr, sizeof(addr) ); +#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ + peeraddr_string = inet_ntoa( peeraddr.sa_in_addr.sin_addr ); +#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ sprintf( peername, "IP=%s:%d", - inet_ntoa( peeraddr.sa_in_addr.sin_addr ), + peeraddr_string, (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) ); Statslog( LDAP_DEBUG_STATS, "conn=%lu UDP request from %s (%s) accepted.\n", diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c index 8e8a69d560..46a07a6f51 100644 --- a/servers/slapd/daemon.c +++ b/servers/slapd/daemon.c @@ -1524,43 +1524,44 @@ slap_open_listener( switch ( (*sal)->sa_family ) { #ifdef LDAP_PF_LOCAL case AF_LOCAL: { - char *addr = ((struct sockaddr_un *)*sal)->sun_path; - l.sl_name.bv_len = strlen(addr) + sizeof("PATH=") - 1; + char *path = ((struct sockaddr_un *)*sal)->sun_path; + l.sl_name.bv_len = strlen(path) + STRLENOF("PATH="); l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len + 1 ); snprintf( l.sl_name.bv_val, l.sl_name.bv_len + 1, - "PATH=%s", addr ); + "PATH=%s", path ); } break; #endif /* LDAP_PF_LOCAL */ case AF_INET: { - char *s; -#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) char addr[INET_ADDRSTRLEN]; - inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr, + const char *s; +#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) + s = inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr, addr, sizeof(addr) ); - s = addr; #else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr ); #endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ + if (!s) s = SLAP_STRING_UNKNOWN; port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port ); l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") ); snprintf( l.sl_name.bv_val, sizeof("IP=255.255.255.255:65535"), - "IP=%s:%d", - s != NULL ? s : SLAP_STRING_UNKNOWN, port ); + "IP=%s:%d", s, port ); l.sl_name.bv_len = strlen( l.sl_name.bv_val ); } break; #ifdef LDAP_PF_INET6 case AF_INET6: { char addr[INET6_ADDRSTRLEN]; - inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr, + const char *s; + s = inet_ntop( AF_INET6, &((struct sockaddr_in6 *)*sal)->sin6_addr, addr, sizeof addr); + if (!s) s = SLAP_STRING_UNKNOWN; port = ntohs( ((struct sockaddr_in6 *)*sal)->sin6_port ); - l.sl_name.bv_len = strlen(addr) + sizeof("IP=[]:65535"); + l.sl_name.bv_len = strlen(s) + sizeof("IP=[]:65535"); l.sl_name.bv_val = ber_memalloc( l.sl_name.bv_len ); snprintf( l.sl_name.bv_val, l.sl_name.bv_len, "IP=[%s]:%d", - addr, port ); + s, port ); l.sl_name.bv_len = strlen( l.sl_name.bv_val ); } break; #endif /* LDAP_PF_INET6 */ @@ -1803,7 +1804,9 @@ slap_listener( #endif /* SLAPD_RLOOKUPS */ char *dnsname = NULL; - char *peeraddr = NULL; + const char *peeraddr = NULL; + /* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */ + char addr[INET6_ADDRSTRLEN]; #ifdef LDAP_PF_LOCAL char peername[MAXPATHLEN + sizeof("PATH=")]; #ifdef LDAP_PF_LOCAL_SENDMSG @@ -1971,30 +1974,39 @@ slap_listener( # ifdef LDAP_PF_INET6 case AF_INET6: if ( IN6_IS_ADDR_V4MAPPED(&from.sa_in6_addr.sin6_addr) ) { +#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) + peeraddr = inet_ntop( AF_INET, + ((struct in_addr *)&from.sa_in6_addr.sin6_addr.s6_addr[12]), + addr, sizeof(addr) ); +#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ peeraddr = inet_ntoa( *((struct in_addr *) &from.sa_in6_addr.sin6_addr.s6_addr[12]) ); - sprintf( peername, "IP=%s:%d", - peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, +#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ + if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN; + sprintf( peername, "IP=%s:%d", peeraddr, (unsigned) ntohs( from.sa_in6_addr.sin6_port ) ); } else { - char addr[INET6_ADDRSTRLEN]; - - peeraddr = (char *) inet_ntop( AF_INET6, + peeraddr = inet_ntop( AF_INET6, &from.sa_in6_addr.sin6_addr, addr, sizeof addr ); - sprintf( peername, "IP=[%s]:%d", - peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, + if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN; + sprintf( peername, "IP=[%s]:%d", peeraddr, (unsigned) ntohs( from.sa_in6_addr.sin6_port ) ); } break; # endif /* LDAP_PF_INET6 */ - case AF_INET: + case AF_INET: { +#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP ) + peeraddr = inet_ntop( AF_INET, &from.sa_in_addr.sin_addr, + addr, sizeof(addr) ); +#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ peeraddr = inet_ntoa( from.sa_in_addr.sin_addr ); - sprintf( peername, "IP=%s:%d", - peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, +#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ + if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN; + sprintf( peername, "IP=%s:%d", peeraddr, (unsigned) ntohs( from.sa_in_addr.sin_port ) ); - break; + } break; default: slapd_close(sfd); @@ -2025,7 +2037,7 @@ slap_listener( ldap_pvt_thread_mutex_lock( &sd_tcpd_mutex ); rc = hosts_ctl("slapd", dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN, - peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, + peeraddr, SLAP_STRING_UNKNOWN ); ldap_pvt_thread_mutex_unlock( &sd_tcpd_mutex ); if ( !rc ) { @@ -2034,8 +2046,7 @@ slap_listener( "fd=%ld DENIED from %s (%s)\n", (long) sfd, dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN, - peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, - 0, 0 ); + peeraddr, 0, 0 ); slapd_close(sfd); return 0; } -- 2.39.5