Use getnameinfo(3) instead of our own equivalent

I didn't know getnameinfo(3) existed, so I implemented it, or something
similar to it called inet_sockaddr2str().  Let's use the standard API.

Link: <https://inbox.sourceware.org/libc-alpha/0f25d60f-f183-b518-b6c1-6d46aa63ee57@gmail.com/T/>
Link: <https://stackoverflow.com/a/42190913/6872717>
Link: <https://github.com/shadow-maint/shadow/pull/617>
Link: <https://software.codidact.com/posts/287748>
Cc: Zack Weinberg <zack@owlfolio.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2023-01-19 19:33:53 +01:00 committed by Serge Hallyn
parent ac8b81c2b7
commit b2bed465e8
4 changed files with 13 additions and 93 deletions

View File

@ -194,9 +194,6 @@ extern bool gr_append_member (struct group *grp, char *member);
/* hushed.c */
extern bool hushed (const char *username);
/* inet_sockaddr2str.c */
extern const char *inet_sockaddr2str(const struct sockaddr *sa);
/* audit_help.c */
#ifdef WITH_AUDIT
extern int audit_fd;

View File

@ -38,7 +38,6 @@ libmisc_la_SOURCES = \
hushed.c \
idmapping.h \
idmapping.c \
inet_sockaddr2str.c \
isexpired.c \
limits.c \
list.c log.c \

View File

@ -1,78 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/socket.h>
#ident "$Id$"
#include "defines.h"
#include "prototypes.h"
#if !defined(INET_ADDRSTRLENMAX)
#define INET_ADDRSTRLENMAX MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)
#endif
/*
* SYNOPSIS
* const char *inet_sockaddr2str(const struct sockaddr *sa);
*
* DESCRIPTION
* This function is similar to inet_ntop(3). It transforms an address
* in 'struct in_addr' or 'struct in6_addr' form into a human-readable
* string.
*
* It receives a sockaddr structure, which is simpler to pass after
* receiving it from getaddrinfo(3). However, this function is not
* reentrant, and like inet_ntoa(3), it uses an internal buffer, for
* simplicity; anyway we're not in a multithreaded program, and it
* doesn't contain any sensitive data, so it's fine to use static
* storage here.
*
* RETURN VALUE
* This function returns a pointer to a statically allocated buffer,
* which subsequent calls will overwrite.
*
* On error, it returns NULL.
*
* ERRORS
* EAFNOSUPPORT
* The address family in sa->sa_family is not AF_INET or AF_INET6.
*
* CAVEATS
* This function is not reentrant.
*/
const char *
inet_sockaddr2str(const struct sockaddr *sa)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
static char buf[INET_ADDRSTRLENMAX];
switch (sa->sa_family) {
case AF_INET:
sin = (struct sockaddr_in *) sa;
inet_ntop(AF_INET, &sin->sin_addr, buf, NITEMS(buf));
return buf;
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
inet_ntop(AF_INET6, &sin6->sin6_addr, buf, NITEMS(buf));
return buf;
default:
errno = EAFNOSUPPORT;
return NULL;
}
}

View File

@ -267,22 +267,24 @@ static const char *resolve_hostname (const char *string)
char *addr_str;
struct addrinfo *addrs;
gai_err = getaddrinfo(string, NULL, NULL, &addrs);
if (gai_err != 0)
goto notfound;
static char host[MAXHOSTNAMELEN];
addr_str = inet_sockaddr2str(addrs[0].ai_addr);
if (addr_str == NULL) {
SYSLOG ((LOG_ERR, "inet_sockaddr2str(): %s", strerror(errno)));
abort();
gai_err = getaddrinfo(string, NULL, NULL, &addrs);
if (gai_err != 0) {
SYSLOG ((LOG_ERR, "getaddrinfo(%s): %s", string, gai_strerror(gai_err)));
return string;
}
addr_str = host;
gai_err = getnameinfo(addrs[0].ai_addr, addrs[0].ai_addrlen,
host, NITEMS(host), NULL, 0, NI_NUMERICHOST);
if (gai_err != 0) {
SYSLOG ((LOG_ERR, "getnameinfo(%s): %s", string, gai_strerror(gai_err)));
addr_str = string;
}
freeaddrinfo(addrs);
return addr_str;
notfound:
SYSLOG ((LOG_ERR, "getaddrinfo(%s): %s", string, gai_strerror(gai_err)));
return string;
}
/* from_match - match a host or tty against a list of tokens */