2003-04-02 02:49:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* getaddrinfo.c
|
|
|
|
* Support getaddrinfo() on platforms that don't have it.
|
|
|
|
*
|
2003-07-24 01:30:41 +02:00
|
|
|
* We also supply getnameinfo() here, assuming that the platform will have
|
2014-05-06 18:12:18 +02:00
|
|
|
* it if and only if it has getaddrinfo(). If this proves false on some
|
2003-07-24 01:30:41 +02:00
|
|
|
* platform, we'll need to split this file and provide a separate configure
|
|
|
|
* test for getnameinfo().
|
2003-04-02 02:49:28 +02:00
|
|
|
*
|
2006-07-16 22:28:01 +02:00
|
|
|
* Windows may or may not have these routines, so we handle Windows specially
|
2005-08-25 00:13:23 +02:00
|
|
|
* by dynamically checking for their existence. If they already exist, we
|
|
|
|
* use the Windows native routines, but if not, we use our own.
|
|
|
|
*
|
2003-04-02 02:49:28 +02:00
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Copyright (c) 2003-2022, PostgreSQL Global Development Group
|
2003-04-02 02:49:28 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/port/getaddrinfo.c
|
2003-04-02 02:49:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is intended to be used in both frontend and backend, so use c.h */
|
2003-03-29 12:31:52 +01:00
|
|
|
#include "c.h"
|
2003-04-02 02:49:28 +02:00
|
|
|
|
2003-03-29 12:31:52 +01:00
|
|
|
#include <sys/socket.h>
|
2003-04-02 02:49:28 +02:00
|
|
|
#include <netdb.h>
|
2003-03-29 12:31:52 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2003-04-02 02:49:28 +02:00
|
|
|
#include "getaddrinfo.h"
|
2006-07-16 22:28:01 +02:00
|
|
|
#include "libpq/pqcomm.h" /* needed for struct sockaddr_storage */
|
2017-10-02 05:05:27 +02:00
|
|
|
#include "port/pg_bswap.h"
|
2003-04-02 02:49:28 +02:00
|
|
|
|
2005-08-25 00:13:23 +02:00
|
|
|
|
2022-01-11 19:46:12 +01:00
|
|
|
#ifdef FRONTEND
|
|
|
|
static int pqGethostbyname(const char *name,
|
|
|
|
struct hostent *resultbuf,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
struct hostent **result,
|
|
|
|
int *herrno);
|
|
|
|
#endif
|
|
|
|
|
2005-08-25 00:13:23 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
/*
|
|
|
|
* The native routines may or may not exist on the Windows platform we are on,
|
|
|
|
* so we dynamically look up the routines, and call them via function pointers.
|
|
|
|
* Here we need to declare what the function pointers look like
|
|
|
|
*/
|
|
|
|
typedef int (__stdcall * getaddrinfo_ptr_t) (const char *nodename,
|
2017-06-21 20:39:04 +02:00
|
|
|
const char *servname,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res);
|
2005-08-25 00:13:23 +02:00
|
|
|
|
2017-06-21 20:39:04 +02:00
|
|
|
typedef void (__stdcall * freeaddrinfo_ptr_t) (struct addrinfo *ai);
|
2005-08-25 00:13:23 +02:00
|
|
|
|
2017-06-21 20:39:04 +02:00
|
|
|
typedef int (__stdcall * getnameinfo_ptr_t) (const struct sockaddr *sa,
|
|
|
|
int salen,
|
2019-08-13 06:53:41 +02:00
|
|
|
char *node, int nodelen,
|
|
|
|
char *service, int servicelen,
|
2017-06-21 20:39:04 +02:00
|
|
|
int flags);
|
2005-08-25 00:13:23 +02:00
|
|
|
|
|
|
|
/* static pointers to the native routines, so we only do the lookup once. */
|
|
|
|
static getaddrinfo_ptr_t getaddrinfo_ptr = NULL;
|
|
|
|
static freeaddrinfo_ptr_t freeaddrinfo_ptr = NULL;
|
|
|
|
static getnameinfo_ptr_t getnameinfo_ptr = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
haveNativeWindowsIPv6routines(void)
|
|
|
|
{
|
|
|
|
void *hLibrary = NULL;
|
2005-10-14 01:22:11 +02:00
|
|
|
static bool alreadyLookedForIpv6routines = false;
|
2005-08-25 00:13:23 +02:00
|
|
|
|
|
|
|
if (alreadyLookedForIpv6routines)
|
|
|
|
return (getaddrinfo_ptr != NULL);
|
|
|
|
|
|
|
|
/*
|
2020-02-19 05:20:33 +01:00
|
|
|
* For Windows XP and later versions, the IPv6 routines are present in the
|
|
|
|
* WinSock 2 library (ws2_32.dll).
|
2005-08-25 00:13:23 +02:00
|
|
|
*/
|
|
|
|
hLibrary = LoadLibraryA("ws2_32");
|
|
|
|
|
|
|
|
/* If hLibrary is null, we couldn't find a dll with functions */
|
|
|
|
if (hLibrary != NULL)
|
|
|
|
{
|
|
|
|
/* We found a dll, so now get the addresses of the routines */
|
|
|
|
|
2020-10-21 08:17:51 +02:00
|
|
|
getaddrinfo_ptr = (getaddrinfo_ptr_t) (pg_funcptr_t) GetProcAddress(hLibrary,
|
|
|
|
"getaddrinfo");
|
|
|
|
freeaddrinfo_ptr = (freeaddrinfo_ptr_t) (pg_funcptr_t) GetProcAddress(hLibrary,
|
|
|
|
"freeaddrinfo");
|
|
|
|
getnameinfo_ptr = (getnameinfo_ptr_t) (pg_funcptr_t) GetProcAddress(hLibrary,
|
|
|
|
"getnameinfo");
|
2005-08-25 00:13:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If any one of the routines is missing, let's play it safe and
|
|
|
|
* ignore them all
|
|
|
|
*/
|
|
|
|
if (getaddrinfo_ptr == NULL ||
|
|
|
|
freeaddrinfo_ptr == NULL ||
|
|
|
|
getnameinfo_ptr == NULL)
|
|
|
|
{
|
|
|
|
FreeLibrary(hLibrary);
|
|
|
|
hLibrary = NULL;
|
|
|
|
getaddrinfo_ptr = NULL;
|
|
|
|
freeaddrinfo_ptr = NULL;
|
|
|
|
getnameinfo_ptr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-14 01:22:11 +02:00
|
|
|
alreadyLookedForIpv6routines = true;
|
2005-08-25 00:13:23 +02:00
|
|
|
return (getaddrinfo_ptr != NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-06-12 09:36:51 +02:00
|
|
|
/*
|
|
|
|
* get address info for ipv4 sockets.
|
|
|
|
*
|
|
|
|
* Bugs: - only one addrinfo is set even though hintp is NULL or
|
|
|
|
* ai_socktype is 0
|
|
|
|
* - AI_CANONNAME is not supported.
|
|
|
|
* - servname can only be a number, not text.
|
|
|
|
*/
|
2003-03-29 12:31:52 +01:00
|
|
|
int
|
|
|
|
getaddrinfo(const char *node, const char *service,
|
2017-06-21 20:39:04 +02:00
|
|
|
const struct addrinfo *hintp,
|
|
|
|
struct addrinfo **res)
|
2003-03-29 12:31:52 +01:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
struct addrinfo *ai;
|
|
|
|
struct sockaddr_in sin,
|
|
|
|
*psin;
|
|
|
|
struct addrinfo hints;
|
2003-06-12 09:36:51 +02:00
|
|
|
|
2006-06-08 00:24:46 +02:00
|
|
|
#ifdef WIN32
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2005-08-25 00:13:23 +02:00
|
|
|
/*
|
|
|
|
* If Windows has native IPv6 support, use the native Windows routine.
|
|
|
|
* Otherwise, fall through and use our own code.
|
|
|
|
*/
|
|
|
|
if (haveNativeWindowsIPv6routines())
|
|
|
|
return (*getaddrinfo_ptr) (node, service, hintp, res);
|
|
|
|
#endif
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if (hintp == NULL)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy(&hints, hintp, sizeof(hints));
|
2003-03-29 12:31:52 +01:00
|
|
|
|
2003-06-12 09:36:51 +02:00
|
|
|
if (hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC)
|
2003-03-29 12:31:52 +01:00
|
|
|
return EAI_FAMILY;
|
|
|
|
|
2003-06-12 09:36:51 +02:00
|
|
|
if (hints.ai_socktype == 0)
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2003-03-29 12:31:52 +01:00
|
|
|
|
|
|
|
if (!node && !service)
|
|
|
|
return EAI_NONAME;
|
|
|
|
|
2003-04-02 02:49:28 +02:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
|
2003-03-29 12:31:52 +01:00
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if (node[0] == '\0')
|
2017-10-02 00:36:14 +02:00
|
|
|
sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
|
2003-06-12 09:36:51 +02:00
|
|
|
else if (hints.ai_flags & AI_NUMERICHOST)
|
2003-03-29 12:31:52 +01:00
|
|
|
{
|
2003-06-12 09:36:51 +02:00
|
|
|
if (!inet_aton(node, &sin.sin_addr))
|
Fix assorted issues in client host name lookup.
The code for matching clients to pg_hba.conf lines that specify host names
(instead of IP address ranges) failed to complain if reverse DNS lookup
failed; instead it silently didn't match, so that you might end up getting
a surprising "no pg_hba.conf entry for ..." error, as seen in bug #9518
from Mike Blackwell. Since we don't want to make this a fatal error in
situations where pg_hba.conf contains a mixture of host names and IP
addresses (clients matching one of the numeric entries should not have to
have rDNS data), remember the lookup failure and mention it as DETAIL if
we get to "no pg_hba.conf entry". Apply the same approach to forward-DNS
lookup failures, too, rather than treating them as immediate hard errors.
Along the way, fix a couple of bugs that prevented us from detecting an
rDNS lookup error reliably, and make sure that we make only one rDNS lookup
attempt; formerly, if the lookup attempt failed, the code would try again
for each host name entry in pg_hba.conf. Since more or less the whole
point of this design is to ensure there's only one lookup attempt not one
per entry, the latter point represents a performance bug that seems
sufficient justification for back-patching.
Also, adjust src/port/getaddrinfo.c so that it plays as well as it can
with this code. Which is not all that well, since it does not have actual
support for rDNS lookup, but at least it should return the expected (and
required by spec) error codes so that the main code correctly perceives the
lack of functionality as a lookup failure. It's unlikely that PG is still
being used in production on any machines that require our getaddrinfo.c,
so I'm not excited about working harder than this.
To keep the code in the various branches similar, this includes
back-patching commits c424d0d1052cb4053c8712ac44123f9b9a9aa3f2 and
1997f34db4687e671690ed054c8f30bb501b1168 into 9.2 and earlier.
Back-patch to 9.1 where the facility for hostnames in pg_hba.conf was
introduced.
2014-04-02 23:11:24 +02:00
|
|
|
return EAI_NONAME;
|
2003-03-29 12:31:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct hostent *hp;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-06-14 20:20:33 +02:00
|
|
|
#ifdef FRONTEND
|
|
|
|
struct hostent hpstr;
|
2004-09-28 02:07:01 +02:00
|
|
|
char buf[BUFSIZ];
|
2003-08-04 02:43:34 +02:00
|
|
|
int herrno = 0;
|
2003-03-29 12:31:52 +01:00
|
|
|
|
2003-06-14 20:20:33 +02:00
|
|
|
pqGethostbyname(node, &hpstr, buf, sizeof(buf),
|
2003-08-04 02:43:34 +02:00
|
|
|
&hp, &herrno);
|
2003-06-14 20:20:33 +02:00
|
|
|
#else
|
2003-03-29 12:31:52 +01:00
|
|
|
hp = gethostbyname(node);
|
2003-06-14 20:20:33 +02:00
|
|
|
#endif
|
2003-03-29 12:31:52 +01:00
|
|
|
if (hp == NULL)
|
|
|
|
{
|
|
|
|
switch (h_errno)
|
|
|
|
{
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
case NO_DATA:
|
2003-06-12 09:36:51 +02:00
|
|
|
return EAI_NONAME;
|
2003-03-29 12:31:52 +01:00
|
|
|
case TRY_AGAIN:
|
|
|
|
return EAI_AGAIN;
|
|
|
|
case NO_RECOVERY:
|
|
|
|
default:
|
|
|
|
return EAI_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hp->h_addrtype != AF_INET)
|
2003-06-12 09:36:51 +02:00
|
|
|
return EAI_FAIL;
|
2003-03-29 12:31:52 +01:00
|
|
|
|
2003-04-28 01:56:53 +02:00
|
|
|
memcpy(&(sin.sin_addr), hp->h_addr, hp->h_length);
|
2003-03-29 12:31:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-12 09:36:51 +02:00
|
|
|
if (hints.ai_flags & AI_PASSIVE)
|
2017-10-02 00:36:14 +02:00
|
|
|
sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
|
2003-04-28 01:56:53 +02:00
|
|
|
else
|
2017-10-02 00:36:14 +02:00
|
|
|
sin.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
|
2003-03-29 12:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (service)
|
2017-10-02 00:36:14 +02:00
|
|
|
sin.sin_port = pg_hton16((unsigned short) atoi(service));
|
2003-07-24 01:30:41 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
|
|
|
|
sin.sin_len = sizeof(sin);
|
2003-06-12 09:36:51 +02:00
|
|
|
#endif
|
2003-03-29 12:31:52 +01:00
|
|
|
|
|
|
|
ai = malloc(sizeof(*ai));
|
|
|
|
if (!ai)
|
|
|
|
return EAI_MEMORY;
|
2003-06-12 09:36:51 +02:00
|
|
|
|
2003-03-29 12:31:52 +01:00
|
|
|
psin = malloc(sizeof(*psin));
|
|
|
|
if (!psin)
|
|
|
|
{
|
|
|
|
free(ai);
|
|
|
|
return EAI_MEMORY;
|
|
|
|
}
|
|
|
|
|
2003-04-02 02:49:28 +02:00
|
|
|
memcpy(psin, &sin, sizeof(*psin));
|
2003-03-29 12:31:52 +01:00
|
|
|
|
2003-06-12 09:36:51 +02:00
|
|
|
ai->ai_flags = 0;
|
2003-04-02 02:49:28 +02:00
|
|
|
ai->ai_family = AF_INET;
|
2003-06-12 09:36:51 +02:00
|
|
|
ai->ai_socktype = hints.ai_socktype;
|
|
|
|
ai->ai_protocol = hints.ai_protocol;
|
2003-03-29 12:31:52 +01:00
|
|
|
ai->ai_addrlen = sizeof(*psin);
|
|
|
|
ai->ai_addr = (struct sockaddr *) psin;
|
|
|
|
ai->ai_canonname = NULL;
|
|
|
|
ai->ai_next = NULL;
|
|
|
|
|
|
|
|
*res = ai;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-06-21 20:39:04 +02:00
|
|
|
freeaddrinfo(struct addrinfo *res)
|
2003-03-29 12:31:52 +01:00
|
|
|
{
|
|
|
|
if (res)
|
|
|
|
{
|
2006-06-08 00:24:46 +02:00
|
|
|
#ifdef WIN32
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2005-08-25 00:13:23 +02:00
|
|
|
/*
|
|
|
|
* If Windows has native IPv6 support, use the native Windows routine.
|
|
|
|
* Otherwise, fall through and use our own code.
|
|
|
|
*/
|
|
|
|
if (haveNativeWindowsIPv6routines())
|
|
|
|
{
|
2005-08-25 19:51:01 +02:00
|
|
|
(*freeaddrinfo_ptr) (res);
|
2005-08-25 00:13:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-29 12:31:52 +01:00
|
|
|
if (res->ai_addr)
|
|
|
|
free(res->ai_addr);
|
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 02:49:28 +02:00
|
|
|
const char *
|
2003-03-29 12:31:52 +01:00
|
|
|
gai_strerror(int errcode)
|
|
|
|
{
|
2003-04-02 02:49:28 +02:00
|
|
|
#ifdef HAVE_HSTRERROR
|
2003-08-04 02:43:34 +02:00
|
|
|
int hcode;
|
2003-03-29 12:31:52 +01:00
|
|
|
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case EAI_NONAME:
|
|
|
|
hcode = HOST_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
case EAI_AGAIN:
|
|
|
|
hcode = TRY_AGAIN;
|
|
|
|
break;
|
|
|
|
case EAI_FAIL:
|
|
|
|
default:
|
|
|
|
hcode = NO_RECOVERY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hstrerror(hcode);
|
2005-10-15 04:49:52 +02:00
|
|
|
#else /* !HAVE_HSTRERROR */
|
2003-04-02 02:49:28 +02:00
|
|
|
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case EAI_NONAME:
|
|
|
|
return "Unknown host";
|
|
|
|
case EAI_AGAIN:
|
|
|
|
return "Host name lookup failure";
|
2005-10-15 04:49:52 +02:00
|
|
|
/* Errors below are probably WIN32 only */
|
2005-08-25 19:51:01 +02:00
|
|
|
#ifdef EAI_BADFLAGS
|
|
|
|
case EAI_BADFLAGS:
|
|
|
|
return "Invalid argument";
|
|
|
|
#endif
|
|
|
|
#ifdef EAI_FAMILY
|
|
|
|
case EAI_FAMILY:
|
|
|
|
return "Address family not supported";
|
|
|
|
#endif
|
|
|
|
#ifdef EAI_MEMORY
|
|
|
|
case EAI_MEMORY:
|
|
|
|
return "Not enough memory";
|
|
|
|
#endif
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME /* MSVC/WIN64 duplicate */
|
2005-08-25 19:51:01 +02:00
|
|
|
case EAI_NODATA:
|
|
|
|
return "No host data of that type was found";
|
|
|
|
#endif
|
|
|
|
#ifdef EAI_SERVICE
|
|
|
|
case EAI_SERVICE:
|
|
|
|
return "Class type not found";
|
|
|
|
#endif
|
|
|
|
#ifdef EAI_SOCKTYPE
|
|
|
|
case EAI_SOCKTYPE:
|
|
|
|
return "Socket type not supported";
|
|
|
|
#endif
|
2003-04-02 02:49:28 +02:00
|
|
|
default:
|
|
|
|
return "Unknown server error";
|
|
|
|
}
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
#endif /* HAVE_HSTRERROR */
|
2003-03-29 12:31:52 +01:00
|
|
|
}
|
2003-06-12 09:36:51 +02:00
|
|
|
|
|
|
|
/*
|
2003-07-24 01:30:41 +02:00
|
|
|
* Convert an ipv4 address to a hostname.
|
2003-08-04 02:43:34 +02:00
|
|
|
*
|
Fix assorted issues in client host name lookup.
The code for matching clients to pg_hba.conf lines that specify host names
(instead of IP address ranges) failed to complain if reverse DNS lookup
failed; instead it silently didn't match, so that you might end up getting
a surprising "no pg_hba.conf entry for ..." error, as seen in bug #9518
from Mike Blackwell. Since we don't want to make this a fatal error in
situations where pg_hba.conf contains a mixture of host names and IP
addresses (clients matching one of the numeric entries should not have to
have rDNS data), remember the lookup failure and mention it as DETAIL if
we get to "no pg_hba.conf entry". Apply the same approach to forward-DNS
lookup failures, too, rather than treating them as immediate hard errors.
Along the way, fix a couple of bugs that prevented us from detecting an
rDNS lookup error reliably, and make sure that we make only one rDNS lookup
attempt; formerly, if the lookup attempt failed, the code would try again
for each host name entry in pg_hba.conf. Since more or less the whole
point of this design is to ensure there's only one lookup attempt not one
per entry, the latter point represents a performance bug that seems
sufficient justification for back-patching.
Also, adjust src/port/getaddrinfo.c so that it plays as well as it can
with this code. Which is not all that well, since it does not have actual
support for rDNS lookup, but at least it should return the expected (and
required by spec) error codes so that the main code correctly perceives the
lack of functionality as a lookup failure. It's unlikely that PG is still
being used in production on any machines that require our getaddrinfo.c,
so I'm not excited about working harder than this.
To keep the code in the various branches similar, this includes
back-patching commits c424d0d1052cb4053c8712ac44123f9b9a9aa3f2 and
1997f34db4687e671690ed054c8f30bb501b1168 into 9.2 and earlier.
Back-patch to 9.1 where the facility for hostnames in pg_hba.conf was
introduced.
2014-04-02 23:11:24 +02:00
|
|
|
* Bugs: - Only supports NI_NUMERICHOST and NI_NUMERICSERV behavior.
|
|
|
|
* It will never resolve a hostname.
|
2003-06-12 09:36:51 +02:00
|
|
|
* - No IPv6 support.
|
|
|
|
*/
|
|
|
|
int
|
2017-06-21 20:39:04 +02:00
|
|
|
getnameinfo(const struct sockaddr *sa, int salen,
|
2003-07-24 01:30:41 +02:00
|
|
|
char *node, int nodelen,
|
|
|
|
char *service, int servicelen, int flags)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
2006-06-08 00:24:46 +02:00
|
|
|
#ifdef WIN32
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2005-08-25 00:13:23 +02:00
|
|
|
/*
|
|
|
|
* If Windows has native IPv6 support, use the native Windows routine.
|
|
|
|
* Otherwise, fall through and use our own code.
|
|
|
|
*/
|
|
|
|
if (haveNativeWindowsIPv6routines())
|
|
|
|
return (*getnameinfo_ptr) (sa, salen, node, nodelen,
|
|
|
|
service, servicelen, flags);
|
|
|
|
#endif
|
|
|
|
|
2003-06-12 09:36:51 +02:00
|
|
|
/* Invalid arguments. */
|
|
|
|
if (sa == NULL || (node == NULL && service == NULL))
|
|
|
|
return EAI_FAIL;
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
2003-06-24 01:52:00 +02:00
|
|
|
if (sa->sa_family == AF_INET6)
|
2003-08-04 02:43:34 +02:00
|
|
|
return EAI_FAMILY;
|
2003-06-12 09:36:51 +02:00
|
|
|
#endif
|
|
|
|
|
Fix assorted issues in client host name lookup.
The code for matching clients to pg_hba.conf lines that specify host names
(instead of IP address ranges) failed to complain if reverse DNS lookup
failed; instead it silently didn't match, so that you might end up getting
a surprising "no pg_hba.conf entry for ..." error, as seen in bug #9518
from Mike Blackwell. Since we don't want to make this a fatal error in
situations where pg_hba.conf contains a mixture of host names and IP
addresses (clients matching one of the numeric entries should not have to
have rDNS data), remember the lookup failure and mention it as DETAIL if
we get to "no pg_hba.conf entry". Apply the same approach to forward-DNS
lookup failures, too, rather than treating them as immediate hard errors.
Along the way, fix a couple of bugs that prevented us from detecting an
rDNS lookup error reliably, and make sure that we make only one rDNS lookup
attempt; formerly, if the lookup attempt failed, the code would try again
for each host name entry in pg_hba.conf. Since more or less the whole
point of this design is to ensure there's only one lookup attempt not one
per entry, the latter point represents a performance bug that seems
sufficient justification for back-patching.
Also, adjust src/port/getaddrinfo.c so that it plays as well as it can
with this code. Which is not all that well, since it does not have actual
support for rDNS lookup, but at least it should return the expected (and
required by spec) error codes so that the main code correctly perceives the
lack of functionality as a lookup failure. It's unlikely that PG is still
being used in production on any machines that require our getaddrinfo.c,
so I'm not excited about working harder than this.
To keep the code in the various branches similar, this includes
back-patching commits c424d0d1052cb4053c8712ac44123f9b9a9aa3f2 and
1997f34db4687e671690ed054c8f30bb501b1168 into 9.2 and earlier.
Back-patch to 9.1 where the facility for hostnames in pg_hba.conf was
introduced.
2014-04-02 23:11:24 +02:00
|
|
|
/* Unsupported flags. */
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
|
|
return EAI_AGAIN;
|
|
|
|
|
2003-07-24 01:30:41 +02:00
|
|
|
if (node)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
2003-06-24 01:52:00 +02:00
|
|
|
if (sa->sa_family == AF_INET)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
2019-08-19 01:27:23 +02:00
|
|
|
if (pg_inet_net_ntop(AF_INET,
|
|
|
|
&((struct sockaddr_in *) sa)->sin_addr,
|
|
|
|
sa->sa_family == AF_INET ? 32 : 128,
|
|
|
|
node, nodelen) == NULL)
|
2011-04-10 17:42:00 +02:00
|
|
|
return EAI_MEMORY;
|
2003-06-12 09:36:51 +02:00
|
|
|
}
|
2010-11-24 23:04:19 +01:00
|
|
|
else
|
2003-06-12 09:36:51 +02:00
|
|
|
return EAI_MEMORY;
|
|
|
|
}
|
|
|
|
|
2003-07-24 01:30:41 +02:00
|
|
|
if (service)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int ret = -1;
|
2003-07-24 01:30:41 +02:00
|
|
|
|
2003-06-24 01:52:00 +02:00
|
|
|
if (sa->sa_family == AF_INET)
|
2003-06-12 09:36:51 +02:00
|
|
|
{
|
2003-07-24 01:30:41 +02:00
|
|
|
ret = snprintf(service, servicelen, "%d",
|
2017-10-02 00:36:14 +02:00
|
|
|
pg_ntoh16(((struct sockaddr_in *) sa)->sin_port));
|
2003-06-12 09:36:51 +02:00
|
|
|
}
|
Clean up assorted misuses of snprintf()'s result value.
Fix a small number of places that were testing the result of snprintf()
but doing so incorrectly. The right test for buffer overrun, per C99,
is "result >= bufsize" not "result > bufsize". Some places were also
checking for failure with "result == -1", but the standard only says
that a negative value is delivered on failure.
(Note that this only makes these places correct if snprintf() delivers
C99-compliant results. But at least now these places are consistent
with all the other places where we assume that.)
Also, make psql_start_test() and isolation_start_test() check for
buffer overrun while constructing their shell commands. There seems
like a higher risk of overrun, with more severe consequences, here
than there is for the individual file paths that are made elsewhere
in the same functions, so this seemed like a worthwhile change.
Also fix guc.c's do_serialize() to initialize errno = 0 before
calling vsnprintf. In principle, this should be unnecessary because
vsnprintf should have set errno if it returns a failure indication ...
but the other two places this coding pattern is cribbed from don't
assume that, so let's be consistent.
These errors are all very old, so back-patch as appropriate. I think
that only the shell command overrun cases are even theoretically
reachable in practice, but there's not much point in erroneous error
checks.
Discussion: https://postgr.es/m/17245.1534289329@sss.pgh.pa.us
2018-08-15 22:29:31 +02:00
|
|
|
if (ret < 0 || ret >= servicelen)
|
2003-06-12 09:36:51 +02:00
|
|
|
return EAI_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-11 19:46:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around gethostbyname() or gethostbyname_r() to mimic
|
|
|
|
* POSIX gethostbyname_r() behaviour, if it is not available or required.
|
|
|
|
*/
|
|
|
|
#ifdef FRONTEND
|
|
|
|
static int
|
|
|
|
pqGethostbyname(const char *name,
|
|
|
|
struct hostent *resultbuf,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
struct hostent **result,
|
|
|
|
int *herrno)
|
|
|
|
{
|
|
|
|
#if defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* broken (well early POSIX draft) gethostbyname_r() which returns 'struct
|
|
|
|
* hostent *'
|
|
|
|
*/
|
|
|
|
*result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno);
|
|
|
|
return (*result == NULL) ? -1 : 0;
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* no gethostbyname_r(), just use gethostbyname() */
|
|
|
|
*result = gethostbyname(name);
|
|
|
|
|
|
|
|
if (*result != NULL)
|
|
|
|
*herrno = h_errno;
|
|
|
|
|
|
|
|
if (*result != NULL)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* FRONTEND */
|