From 8dc62c7dbdae10d1ea4b210e9f75f36eb4c16aa1 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Mon, 9 Nov 2020 10:24:51 +0200 Subject: 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) --- build/build.c | 23 +++++++++++++++-------- configure.ac | 2 +- lib/rpmug.c | 1 - 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 #include +#include +#include #include #include @@ -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) -- cgit v1.2.1