summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2020-11-09 10:24:51 +0200
committerPanu Matilainen <pmatilai@redhat.com>2020-12-10 13:28:07 +0200
commit8dc62c7dbdae10d1ea4b210e9f75f36eb4c16aa1 (patch)
treec0f48cb6b24289ca7b9a48a3c76a78311034e624
parent4d3b57bed1e78936b86377efe9c4b92a731c7f8b (diff)
downloadrpm-8dc62c7dbdae10d1ea4b210e9f75f36eb4c16aa1.tar.gz
Eliminate use of obsolete gethostbyname() function
We only actually used gethostbyname() for canonicalizing buildhost, convert that to use getaddrinfo() instead, which actually has an option for retrieving exactly what we want. The other "use" was to initialize name services, but as we don't need or use hostnames for any operation, we can just as well drop it. User and group names are what we care about. (cherry picked from commit c223d84fbf1ebb7a862f297e65878a2a78653e8e)
-rw-r--r--build/build.c23
-rw-r--r--configure.ac2
-rw-r--r--lib/rpmug.c1
3 files changed, 16 insertions, 10 deletions
diff --git a/build/build.c b/build/build.c
index e50c3784e..a6888b77d 100644
--- a/build/build.c
+++ b/build/build.c
@@ -7,6 +7,8 @@
#include <errno.h>
#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
@@ -42,21 +44,26 @@ static rpm_time_t getBuildTime(void)
static char * buildHost(void)
{
char* hostname;
- struct hostent *hbn;
char *bhMacro;
bhMacro = rpmExpand("%{?_buildhost}", NULL);
if (strcmp(bhMacro, "") != 0) {
rasprintf(&hostname, "%s", bhMacro);
} else {
- hostname = rcalloc(1024, sizeof(*hostname));
- (void) gethostname(hostname, 1024);
- hbn = gethostbyname(hostname);
- if (hbn)
- strcpy(hostname, hbn->h_name);
- else
- rpmlog(RPMLOG_WARNING,
+ hostname = rcalloc(NI_MAXHOST + 1, sizeof(*hostname));
+ if (gethostname(hostname, NI_MAXHOST) == 0) {
+ struct addrinfo *ai, hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_CANONNAME;
+
+ if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) {
+ strcpy(hostname, ai->ai_canonname);
+ } else {
+ rpmlog(RPMLOG_WARNING,
_("Could not canonicalize hostname: %s\n"), hostname);
+ }
+ freeaddrinfo(ai);
+ }
}
free(bhMacro);
return(hostname);
diff --git a/configure.ac b/configure.ac
index ca30fd7c7..a78c8d9fa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -690,7 +690,7 @@ AC_CHECK_FUNCS([secure_getenv __secure_getenv])
AC_CHECK_FUNCS(
[mkstemp getcwd basename dirname realpath setenv unsetenv regcomp lchown \
- utimes getline localtime_r statvfs ],
+ utimes getline localtime_r statvfs getaddrinfo ],
[], [AC_MSG_ERROR([function required by rpm])])
AC_LIBOBJ(fnmatch)
diff --git a/lib/rpmug.c b/lib/rpmug.c
index 119d8e585..2de62111c 100644
--- a/lib/rpmug.c
+++ b/lib/rpmug.c
@@ -174,7 +174,6 @@ static void loadLibs(void)
endpwent();
(void) getgrnam(GID_0_GROUP);
endgrent();
- (void) gethostbyname("localhost");
}
int rpmugInit(void)