Skip to content

Commit 29fcd22

Browse files
committed
Improve error reporting behavior in parse_hba(): give more complete
error report for getaddrinfo failures, point at correct token for syntax errors in all cases, don't log redundant messages.
1 parent 178c08d commit 29fcd22

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

src/backend/libpq/hba.c

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.120 2004/02/02 16:58:30 neilc Exp $
13+
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.121 2004/05/19 22:06:16 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -518,58 +518,60 @@ check_db(char *dbname, char *user, char *param_str)
518518
/*
519519
* Scan the rest of a host record (after the mask field)
520520
* and return the interpretation of it as *userauth_p, *auth_arg_p, and
521-
* *error_p. line points to the next token of the line.
521+
* *error_p. *line points to the next token of the line, and is
522+
* advanced over successfully-read tokens.
522523
*/
523524
static void
524-
parse_hba_auth(List *line, UserAuth *userauth_p, char **auth_arg_p,
525+
parse_hba_auth(List **line, UserAuth *userauth_p, char **auth_arg_p,
525526
bool *error_p)
526527
{
527528
char *token;
528529

529530
*auth_arg_p = NULL;
530531

531-
if (!line)
532-
*error_p = true;
533-
else
532+
/* Get authentication type token. */
533+
if (!*line)
534534
{
535-
/* Get authentication type token. */
536-
token = lfirst(line);
537-
if (strcmp(token, "trust") == 0)
538-
*userauth_p = uaTrust;
539-
else if (strcmp(token, "ident") == 0)
540-
*userauth_p = uaIdent;
541-
else if (strcmp(token, "password") == 0)
542-
*userauth_p = uaPassword;
543-
else if (strcmp(token, "krb4") == 0)
544-
*userauth_p = uaKrb4;
545-
else if (strcmp(token, "krb5") == 0)
546-
*userauth_p = uaKrb5;
547-
else if (strcmp(token, "reject") == 0)
548-
*userauth_p = uaReject;
549-
else if (strcmp(token, "md5") == 0)
550-
*userauth_p = uaMD5;
551-
else if (strcmp(token, "crypt") == 0)
552-
*userauth_p = uaCrypt;
535+
*error_p = true;
536+
return;
537+
}
538+
token = lfirst(*line);
539+
if (strcmp(token, "trust") == 0)
540+
*userauth_p = uaTrust;
541+
else if (strcmp(token, "ident") == 0)
542+
*userauth_p = uaIdent;
543+
else if (strcmp(token, "password") == 0)
544+
*userauth_p = uaPassword;
545+
else if (strcmp(token, "krb4") == 0)
546+
*userauth_p = uaKrb4;
547+
else if (strcmp(token, "krb5") == 0)
548+
*userauth_p = uaKrb5;
549+
else if (strcmp(token, "reject") == 0)
550+
*userauth_p = uaReject;
551+
else if (strcmp(token, "md5") == 0)
552+
*userauth_p = uaMD5;
553+
else if (strcmp(token, "crypt") == 0)
554+
*userauth_p = uaCrypt;
553555
#ifdef USE_PAM
554-
else if (strcmp(token, "pam") == 0)
555-
*userauth_p = uaPAM;
556+
else if (strcmp(token, "pam") == 0)
557+
*userauth_p = uaPAM;
556558
#endif
557-
else
558-
*error_p = true;
559-
line = lnext(line);
559+
else
560+
{
561+
*error_p = true;
562+
return;
560563
}
564+
*line = lnext(*line);
561565

562-
if (!*error_p)
566+
/* Get the authentication argument token, if any */
567+
if (*line)
563568
{
564-
/* Get the authentication argument token, if any */
565-
if (line)
566-
{
567-
token = lfirst(line);
568-
*auth_arg_p = pstrdup(token);
569-
/* If there is more on the line, it is an error */
570-
if (lnext(line))
571-
*error_p = true;
572-
}
569+
token = lfirst(*line);
570+
*auth_arg_p = pstrdup(token);
571+
*line = lnext(*line);
572+
/* If there is more on the line, it is an error */
573+
if (*line)
574+
*error_p = true;
573575
}
574576
}
575577

@@ -623,7 +625,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
623625
goto hba_syntax;
624626

625627
/* Read the rest of the line. */
626-
parse_hba_auth(line, &port->auth_method, &port->auth_arg, error_p);
628+
parse_hba_auth(&line, &port->auth_method, &port->auth_arg, error_p);
627629
if (*error_p)
628630
goto hba_syntax;
629631

@@ -704,13 +706,13 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
704706
{
705707
ereport(LOG,
706708
(errcode(ERRCODE_CONFIG_FILE_ERROR),
707-
errmsg("invalid IP address \"%s\" in pg_hba.conf file: %s",
708-
token, gai_strerror(ret))));
709+
errmsg("invalid IP address \"%s\" in pg_hba.conf file line %d: %s",
710+
token, line_number, gai_strerror(ret))));
709711
if (cidr_slash)
710712
*cidr_slash = '/';
711713
if (gai_result)
712714
freeaddrinfo_all(hints.ai_family, gai_result);
713-
goto hba_syntax;
715+
goto hba_other_error;
714716
}
715717

716718
if (cidr_slash)
@@ -736,16 +738,26 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
736738
ret = getaddrinfo_all(token, NULL, &hints, &gai_result);
737739
if (ret || !gai_result)
738740
{
741+
ereport(LOG,
742+
(errcode(ERRCODE_CONFIG_FILE_ERROR),
743+
errmsg("invalid IP mask \"%s\" in pg_hba.conf file line %d: %s",
744+
token, line_number, gai_strerror(ret))));
739745
if (gai_result)
740746
freeaddrinfo_all(hints.ai_family, gai_result);
741-
goto hba_syntax;
747+
goto hba_other_error;
742748
}
743749

744750
memcpy(&mask, gai_result->ai_addr, gai_result->ai_addrlen);
745751
freeaddrinfo_all(hints.ai_family, gai_result);
746752

747753
if (addr.ss_family != mask.ss_family)
748-
goto hba_syntax;
754+
{
755+
ereport(LOG,
756+
(errcode(ERRCODE_CONFIG_FILE_ERROR),
757+
errmsg("IP address and mask do not match in pg_hba.conf file line %d",
758+
line_number)));
759+
goto hba_other_error;
760+
}
749761
}
750762

751763
if (addr.ss_family != port->raddr.addr.ss_family)
@@ -778,13 +790,14 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
778790
line = lnext(line);
779791
if (!line)
780792
goto hba_syntax;
781-
parse_hba_auth(line, &port->auth_method, &port->auth_arg, error_p);
793+
parse_hba_auth(&line, &port->auth_method, &port->auth_arg, error_p);
782794
if (*error_p)
783795
goto hba_syntax;
784796
}
785797
else
786798
goto hba_syntax;
787799

800+
/* Does the entry match database and user? */
788801
if (!check_db(port->database_name, port->user_name, db))
789802
return;
790803
if (!check_user(port->user_name, user))
@@ -806,6 +819,8 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
806819
errmsg("missing field in pg_hba.conf file at end of line %d",
807820
line_number)));
808821

822+
/* Come here if suitable message already logged */
823+
hba_other_error:
809824
*error_p = true;
810825
}
811826

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy