@@ -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 */
711719bool
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}
0 commit comments