Skip to content

Commit 742c498

Browse files
authored
Improve DNS lookup error handling. (#615)
1 parent e0d343a commit 742c498

6 files changed

Lines changed: 60 additions & 27 deletions

File tree

src/bin/pg_autoctl/cli_create_drop_node.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,8 +1397,8 @@ check_hostname(const char *hostname)
13971397
}
13981398
else
13991399
{
1400-
char cidr[BUFSIZE];
1401-
char ipaddr[BUFSIZE];
1400+
char cidr[BUFSIZE] = { 0 };
1401+
char ipaddr[BUFSIZE] = { 0 };
14021402

14031403
if (!fetchLocalCIDR(hostname, cidr, BUFSIZE))
14041404
{
@@ -1407,7 +1407,9 @@ check_hostname(const char *hostname)
14071407
hostname);
14081408
}
14091409

1410+
bool useHostname = false;
1411+
14101412
/* use pghba_check_hostname for log diagnostics */
1411-
(void) pghba_check_hostname(hostname, ipaddr, sizeof(ipaddr));
1413+
(void) pghba_check_hostname(hostname, ipaddr, BUFSIZE, &useHostname);
14121414
}
14131415
}

src/bin/pg_autoctl/cli_do_show.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static void
307307
cli_show_reverse(int argc, char **argv)
308308
{
309309
char ipaddr[BUFSIZE] = { 0 };
310-
310+
bool foundHostnameFromAddress = false;
311311

312312
if (argc != 1)
313313
{
@@ -324,7 +324,9 @@ cli_show_reverse(int argc, char **argv)
324324
exit(EXIT_CODE_BAD_ARGS);
325325
}
326326

327-
if (!resolveHostnameForwardAndReverse(hostname, ipaddr, sizeof(ipaddr)))
327+
if (!resolveHostnameForwardAndReverse(hostname, ipaddr, sizeof(ipaddr),
328+
&foundHostnameFromAddress) ||
329+
!foundHostnameFromAddress)
328330
{
329331
log_fatal("Failed to find an IP address for hostname \"%s\" that "
330332
"matches hostname again in a reverse-DNS lookup.",

src/bin/pg_autoctl/ipaddr.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,15 @@ findHostnameFromLocalIpAddress(char *localIpAddress, char *hostname, int size)
679679
* of the IP addresses that our hostname forward-DNS query returns.
680680
*/
681681
bool
682-
resolveHostnameForwardAndReverse(const char *hostname, char *ipaddr, int size)
682+
resolveHostnameForwardAndReverse(const char *hostname, char *ipaddr, int size,
683+
bool *foundHostnameFromAddress)
683684
{
684685
struct addrinfo *lookup, *ai;
685686

686-
bool foundHostnameFromAddress = false;
687+
*foundHostnameFromAddress = false;
687688

688689
int error = getaddrinfo(hostname, NULL, 0, &lookup);
690+
689691
if (error != 0)
690692
{
691693
log_warn("Failed to resolve DNS name \"%s\": %s",
@@ -733,13 +735,13 @@ resolveHostnameForwardAndReverse(const char *hostname, char *ipaddr, int size)
733735
/* compare reverse-DNS lookup result with our hostname */
734736
if (strcmp(hbuf, hostname) == 0)
735737
{
736-
foundHostnameFromAddress = true;
738+
*foundHostnameFromAddress = true;
737739
break;
738740
}
739741
}
740742
freeaddrinfo(lookup);
741743

742-
return foundHostnameFromAddress;
744+
return true;
743745
}
744746

745747

src/bin/pg_autoctl/ipaddr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ bool findHostnameFromLocalIpAddress(char *localIpAddress,
3131
char *hostname, int size);
3232

3333
bool resolveHostnameForwardAndReverse(const char *hostname,
34-
char *ipaddr, int size);
34+
char *ipaddr, int size,
35+
bool *foundHostnameFromAddress);
3536

3637
bool ipaddrGetLocalHostname(char *hostname, size_t size);
3738

src/bin/pg_autoctl/pghba.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ pghba_ensure_host_rule_exists(const char *hbaFilePath,
130130
*
131131
* HBA & DNS is hard.
132132
*/
133-
bool useHostname = pghba_check_hostname(host, ipaddr, sizeof(ipaddr));
133+
bool useHostname = false;
134+
135+
if (!pghba_check_hostname(host, ipaddr, sizeof(ipaddr), &useHostname))
136+
{
137+
/* errors have already been logged (DNS failure) */
138+
}
134139

135140
if (!useHostname)
136141
{
@@ -331,8 +336,11 @@ pghba_ensure_host_rules_exist(const char *hbaFilePath,
331336
*
332337
* HBA & DNS is hard.
333338
*/
334-
useHostname =
335-
pghba_check_hostname(node->host, ipaddr, sizeof(ipaddr));
339+
if (!pghba_check_hostname(node->host, ipaddr, sizeof(ipaddr),
340+
&useHostname))
341+
{
342+
/* errors have already been logged (DNS failure) */
343+
}
336344

337345
if (!useHostname)
338346
{
@@ -709,7 +717,8 @@ pghba_enable_lan_cidr(PGSQL *pgsql,
709717
* resolve an IP address.)
710718
*/
711719
bool
712-
pghba_check_hostname(const char *hostname, char *ipaddr, size_t size)
720+
pghba_check_hostname(const char *hostname,
721+
char *ipaddr, size_t size, bool *useHostname)
713722
{
714723
/*
715724
* IP addresses do not require any DNS properties/lookups. Also hostname
@@ -720,27 +729,43 @@ pghba_check_hostname(const char *hostname, char *ipaddr, size_t size)
720729
*/
721730
if (strchr(hostname, '/') || ip_address_type(hostname) != IPTYPE_NONE)
722731
{
732+
*useHostname = true;
723733
return true;
724734
}
725735

726-
if (!resolveHostnameForwardAndReverse(hostname, ipaddr, size))
736+
bool foundHostnameFromAddress = false;
737+
738+
if (!resolveHostnameForwardAndReverse(hostname, ipaddr, size,
739+
&foundHostnameFromAddress))
727740
{
728-
/* warn users about possible DNS misconfiguration */
729-
log_warn("Failed to resolve hostname \"%s\" to an IP address that "
730-
"resolves back to the hostname on a reverse DNS lookup.",
731-
hostname);
741+
/* errors have already been logged (DNS failure) */
742+
*useHostname = true;
743+
return false;
744+
}
732745

733-
log_warn("Postgres might deny connection attempts from \"%s\", "
734-
"even with the new HBA rules.",
735-
hostname);
746+
if (foundHostnameFromAddress)
747+
{
748+
*useHostname = true;
736749

737-
log_warn("Hint: correct setup of HBA with host names requires proper "
738-
"reverse DNS setup. You might want to use IP addresses.");
750+
log_debug("pghba_check_hostname: \"%s\" <-> %s", hostname, ipaddr);
739751

740-
return false;
752+
return true;
741753
}
742754

743-
log_debug("pghba_check_hostname: \"%s\" <-> %s", hostname, ipaddr);
755+
*useHostname = false;
756+
757+
/* warn users about possible DNS misconfiguration */
758+
log_warn("Failed to resolve hostname \"%s\" to an IP address that "
759+
"resolves back to the hostname on a reverse DNS lookup.",
760+
hostname);
761+
762+
log_warn("Postgres might deny connection attempts from \"%s\", "
763+
"even with the new HBA rules.",
764+
hostname);
765+
766+
log_warn("Hint: correct setup of HBA with host names requires proper "
767+
"reverse DNS setup. You might want to use IP addresses.");
744768

769+
/* we could successfully check that we should not use the hostname */
745770
return true;
746771
}

src/bin/pg_autoctl/pghba.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ bool pghba_enable_lan_cidr(PGSQL *pgsql,
4949
HBAEditLevel hbaLevel,
5050
const char *pgdata);
5151

52-
bool pghba_check_hostname(const char *hostname, char *ipaddr, size_t size);
52+
bool pghba_check_hostname(const char *hostname, char *ipaddr, size_t size,
53+
bool *useHostname);
5354

5455
#endif /* PGHBA_H */

0 commit comments

Comments
 (0)