pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Allow continuations in "pg_hba.conf" files
11+ messages / 4 participants
[nested] [flat]

Commits from this thread:
  2020-09-03 8f8154a503  Allow records to span multiple lines in pg_hba.conf and pg_ident.conf.
* Allow continuations in "pg_hba.conf" files
@ 2020-03-25 18:09 Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Fabien COELHO @ 2020-03-25 18:09 UTC (permalink / raw)
  To: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>


Hello,

After writing an unreadable and stupidly long line for ldap 
authentification in a "pg_hba.conf" file, I figured out that allowing 
continuations looked simple enough and should just be done.

Patch attached.

-- 
Fabien.

Attachments:

pg-hba-cont-1.patch (text/x-diff, 3.1K) download diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml index 5f1eec78fb..cf3b432c34 100644 --- a/doc/src/sgml/client-auth.sgml +++ b/doc/src/sgml/client-auth.sgml @@ -77,7 +77,7 @@ The general format of the <filename>pg_hba.conf</filename> file is a set of records, one per line. Blank lines are ignored, as is any text after the <literal>#</literal> comment character. - Records cannot be continued across lines. + Records can be backslash-continued across lines. A record is made up of a number of fields which are separated by spaces and/or tabs. Fields can contain white space if the field value is double-quoted. diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index da5189a4fa..95c2cfc8e4 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -486,8 +486,43 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) char *lineptr; List *current_line = NIL; char *err_msg = NULL; + char *cur = rawline; + int len = sizeof(rawline); + int continuations = 0; - if (!fgets(rawline, sizeof(rawline), file)) + /* read input and handle simplistic backslash continuations */ + while ((cur = fgets(cur, len, file)) != NULL) + { + int curlen = strlen(cur); + char *curend = cur + curlen - 1; + + if (curlen == len - 1) + { + /* Line too long! */ + ereport(elevel, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("authentication file line too long"), + errcontext("line %d of configuration file \"%s\"", + line_number + continuations, filename))); + err_msg = "authentication file line too long"; + } + + /* Strip trailing linebreak from rawline */ + while (curend >= cur && (*curend == '\n' || *curend == '\r')) + *curend-- = '\0'; + + /* empty or not a continuation, we are done */ + if (curend < cur || *curend != '\\') + break; + + /* else we have a continuation, just blank it and loop */ + continuations++; + *curend++ = ' '; + len -= (curend - cur); + cur = curend; + } + + if (cur == NULL) { int save_errno = errno; @@ -501,21 +536,6 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) filename, strerror(save_errno)); rawline[0] = '\0'; } - if (strlen(rawline) == MAX_LINE - 1) - { - /* Line too long! */ - ereport(elevel, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("authentication file line too long"), - errcontext("line %d of configuration file \"%s\"", - line_number, filename))); - err_msg = "authentication file line too long"; - } - - /* Strip trailing linebreak from rawline */ - lineptr = rawline + strlen(rawline) - 1; - while (lineptr >= rawline && (*lineptr == '\n' || *lineptr == '\r')) - *lineptr-- = '\0'; /* Parse fields */ lineptr = rawline; @@ -543,7 +563,7 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) *tok_lines = lappend(*tok_lines, tok_line); } - line_number++; + line_number += continuations + 1; } MemoryContextSwitchTo(oldcxt);
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
@ 2020-03-25 19:39 ` Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 1 reply; 11+ messages in thread

From: Justin Pryzby @ 2020-03-25 19:39 UTC (permalink / raw)
  To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: pgsql-hackers@lists.postgresql.org

Hi,

On Wed, Mar 25, 2020 at 07:09:38PM +0100, Fabien COELHO wrote:
> 
> Hello,
> 
> After writing an unreadable and stupidly long line for ldap authentification
> in a "pg_hba.conf" file, I figured out that allowing continuations looked
> simple enough and should just be done.

I tried this briefly.

> -   Records cannot be continued across lines.
> +   Records can be backslash-continued across lines.

Maybe say: "lines ending with a backslash are logically continued on the next
line", or similar.

> +			/* else we have a continuation, just blank it and loop */
> +			continuations++;
> +			*curend++ = ' ';

Since it puts a blank there, it creates a "word" boundary, which I gather
worked for your use case.  But I wonder whether it's needed to add a space (or
otherwise, document that lines cannot be split beween words?).

You might compare this behavior with that of makefiles (or find a better
example) which I happen to recall *don't* add a space; if you want that, you
have to end the line with: " \" not just "\".

Anyway, I checked that the current patch handles users split across lines, like:
alice,\
bob,\
carol

As written, that depends on the parser's behavior of ignoring commas and
blanks, since it sees:
"alice,[SPACE]bob,[SPACE]carol"

Maybe it'd be nice to avoid depending on that.

I tried with a username called "alice,bob", split across lines:

"alice,\
bob",\

But then your patch makes it look for a user called "alice, bob" (with a
space).  I realize that's not a compelling argument :)

Note, that also appears to affect the "username maps" file.  So mention in that
chapter, too.
https://www.postgresql.org/docs/current/auth-username-maps.html

Cheers,
-- 
Justin





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
@ 2020-03-25 20:45   ` Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  0 siblings, 1 reply; 11+ messages in thread

From: Fabien COELHO @ 2020-03-25 20:45 UTC (permalink / raw)
  To: Justin Pryzby <pryzby@telsasoft.com>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>


Hello Justin,

thanks for the feedback.

>> -   Records cannot be continued across lines.
>> +   Records can be backslash-continued across lines.
>
> Maybe say: "lines ending with a backslash are logically continued on the next
> line", or similar.

I tried to change it along that.

> Since it puts a blank there, it creates a "word" boundary, which I gather
> worked for your use case.  But I wonder whether it's needed to add a space (or
> otherwise, document that lines cannot be split beween words?).

Hmmm. Ok, you are right. I hesitated while doing it. I removed the char 
instead, so that it does not add a word break.

> Note, that also appears to affect the "username maps" file.  So mention 
> in that chapter, too. 
> https://www.postgresql.org/docs/current/auth-username-maps.html

Indeed, the same tokenizer is used. I updated a sentence to point on 
continuations.

-- 
Fabien.

Attachments:

pg-hba-cont-2.patch (text/x-diff, 3.8K) download diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml index 5f1eec78fb..4f947b0235 100644 --- a/doc/src/sgml/client-auth.sgml +++ b/doc/src/sgml/client-auth.sgml @@ -77,7 +77,8 @@ The general format of the <filename>pg_hba.conf</filename> file is a set of records, one per line. Blank lines are ignored, as is any text after the <literal>#</literal> comment character. - Records cannot be continued across lines. + Lines ending with a backslash are logically continued on the next + line, so a record can span several lines. A record is made up of a number of fields which are separated by spaces and/or tabs. Fields can contain white space if the field value is double-quoted. @@ -821,7 +822,7 @@ local db1,db2,@demodbs all md5 <synopsis> <replaceable>map-name</replaceable> <replaceable>system-username</replaceable> <replaceable>database-username</replaceable> </synopsis> - Comments and whitespace are handled in the same way as in + Comments, whitespace and continuations are handled in the same way as in <filename>pg_hba.conf</filename>. The <replaceable>map-name</replaceable> is an arbitrary name that will be used to refer to this mapping in <filename>pg_hba.conf</filename>. The other diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index da5189a4fa..bae20dbc06 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -486,8 +486,43 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) char *lineptr; List *current_line = NIL; char *err_msg = NULL; + char *cur = rawline; + int len = sizeof(rawline); + int continuations = 0; - if (!fgets(rawline, sizeof(rawline), file)) + /* read input and handle simplistic backslash continuations */ + while ((cur = fgets(cur, len, file)) != NULL) + { + int curlen = strlen(cur); + char *curend = cur + curlen - 1; + + if (curlen == len - 1) + { + /* Line too long! */ + ereport(elevel, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("authentication file line too long"), + errcontext("line %d of configuration file \"%s\"", + line_number + continuations, filename))); + err_msg = "authentication file line too long"; + } + + /* Strip trailing linebreak from rawline */ + while (curend >= cur && (*curend == '\n' || *curend == '\r')) + *curend-- = '\0'; + + /* empty or not a continuation, we are done */ + if (curend < cur || *curend != '\\') + break; + + /* else we have a continuation, just remove it and loop */ + continuations++; + *curend = '\0'; + len -= (curend - cur); + cur = curend; + } + + if (cur == NULL) { int save_errno = errno; @@ -501,21 +536,6 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) filename, strerror(save_errno)); rawline[0] = '\0'; } - if (strlen(rawline) == MAX_LINE - 1) - { - /* Line too long! */ - ereport(elevel, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("authentication file line too long"), - errcontext("line %d of configuration file \"%s\"", - line_number, filename))); - err_msg = "authentication file line too long"; - } - - /* Strip trailing linebreak from rawline */ - lineptr = rawline + strlen(rawline) - 1; - while (lineptr >= rawline && (*lineptr == '\n' || *lineptr == '\r')) - *lineptr-- = '\0'; /* Parse fields */ lineptr = rawline; @@ -543,7 +563,7 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) *tok_lines = lappend(*tok_lines, tok_line); } - line_number++; + line_number += continuations + 1; } MemoryContextSwitchTo(oldcxt);
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
@ 2020-04-02 00:20     ` David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: David Zhang @ 2020-04-02 00:20 UTC (permalink / raw)
  To: pgsql-hackers@lists.postgresql.org; +Cc: Fabien Coelho <postgresql.org@coelho.net>

Hi Fabien,
Should we consider the case "\ ", i.e. one or more spaces after the backslash?
For example, if I replace a user map 
"mymap   /^(.*)@mydomain\.com$      \1" with 
"mymap   /^(.*)@mydomain\.com$      \ "
"\1"
by adding one extra space after the backslash, then I got the pg_role="\\"
but I think what we expect is pg_role="\\1"

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
@ 2020-04-02 04:25       ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 1 reply; 11+ messages in thread

From: Kyotaro Horiguchi @ 2020-04-02 04:25 UTC (permalink / raw)
  To: david.zhang@highgo.ca; +Cc: pgsql-hackers@lists.postgresql.org; postgresql.org@coelho.net

At Thu, 02 Apr 2020 00:20:12 +0000, David Zhang <david.zhang@highgo.ca> wrote in 
> Hi Fabien,
> Should we consider the case "\ ", i.e. one or more spaces after the backslash?
> For example, if I replace a user map 
> "mymap   /^(.*)@mydomain\.com$      \1" with 
> "mymap   /^(.*)@mydomain\.com$      \ "
> "\1"
> by adding one extra space after the backslash, then I got the pg_role="\\"
> but I think what we expect is pg_role="\\1"

FWIW, I don't think so. Generally a trailing backspace is an escape
character for the following newline.  And '\ ' is a escaped space,
which is usualy menas a space itself.

In this case escape character doesn't work generally and I think it is
natural that a backslash in the middle of a line is a backslash
character itself.

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
@ 2020-04-02 05:25         ` Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 05:58           ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-04-02 18:05           ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  0 siblings, 2 replies; 11+ messages in thread

From: Fabien COELHO @ 2020-04-02 05:25 UTC (permalink / raw)
  To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; +Cc: david.zhang@highgo.ca, PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; postgresql.org@coelho.net


Hello,

> FWIW, I don't think so. Generally a trailing backspace is an escape
> character for the following newline.  And '\ ' is a escaped space,
> which is usualy menas a space itself.
>
> In this case escape character doesn't work generally and I think it is 
> natural that a backslash in the middle of a line is a backslash 
> character itself.

I concur: The backslash char is only a continuation as the very last 
character of the line, before cr/nl line ending markers.

There are no assumption about backslash escaping, quotes and such, which 
seems reasonable given the lexing structure of the files, i.e. records of 
space-separated words, and # line comments.

-- 
Fabien.





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
@ 2020-04-02 05:58           ` Justin Pryzby <pryzby@telsasoft.com>
  2020-04-02 06:46             ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  1 sibling, 1 reply; 11+ messages in thread

From: Justin Pryzby @ 2020-04-02 05:58 UTC (permalink / raw)
  To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; david.zhang@highgo.ca; pgsql-hackers@lists.postgresql.org; postgresql.org@coelho.net

On Thu, Apr 02, 2020 at 07:25:36AM +0200, Fabien COELHO wrote:
> 
> Hello,
> 
> > FWIW, I don't think so. Generally a trailing backspace is an escape
> > character for the following newline.  And '\ ' is a escaped space,
> > which is usualy menas a space itself.
> > 
> > In this case escape character doesn't work generally and I think it is
> > natural that a backslash in the middle of a line is a backslash
> > character itself.
> 
> I concur: The backslash char is only a continuation as the very last
> character of the line, before cr/nl line ending markers.
> 
> There are no assumption about backslash escaping, quotes and such, which
> seems reasonable given the lexing structure of the files, i.e. records of
> space-separated words, and # line comments.

Quoting does allow words containing spaces:

https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
|A record is made up of a number of fields which are separated by spaces and/or
|tabs. Fields can contain white space if the field value is double-quoted.
|Quoting one of the keywords in a database, user, or address field (e.g., all or
|replication) makes the word lose its special meaning, and just match a
|database, user, or host with that name.

-- 
Justin





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 05:58           ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
@ 2020-04-02 06:46             ` Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 0 replies; 11+ messages in thread

From: Fabien COELHO @ 2020-04-02 06:46 UTC (permalink / raw)
  To: Justin Pryzby <pryzby@telsasoft.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; david.zhang@highgo.ca, PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; postgresql.org@coelho.net


Hi Justin,

>> There are no assumption about backslash escaping, quotes and such, which
>> seems reasonable given the lexing structure of the files, i.e. records of
>> space-separated words, and # line comments.
>
> Quoting does allow words containing spaces:

Ok.

I meant that the continuation handling does not care of that, i.e. if the 
continuation is within quotes, then the quoted stuff is implicitely 
continuated, there is no different rule because it is within quotes.

-- 
Fabien.





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
@ 2020-04-02 18:05           ` David Zhang <david.zhang@highgo.ca>
  2020-04-03 05:46             ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  1 sibling, 1 reply; 11+ messages in thread

From: David Zhang @ 2020-04-02 18:05 UTC (permalink / raw)
  To: Fabien COELHO <coelho@cri.ensmp.fr>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; postgresql.org@coelho.net

On 2020-04-01 10:25 p.m., Fabien COELHO wrote:

>
> Hello,
>
>> FWIW, I don't think so. Generally a trailing backspace is an escape
>> character for the following newline.  And '\ ' is a escaped space,
>> which is usualy menas a space itself.
>>
>> In this case escape character doesn't work generally and I think it 
>> is natural that a backslash in the middle of a line is a backslash 
>> character itself.
>
> I concur: The backslash char is only a continuation as the very last 
> character of the line, before cr/nl line ending markers.

+Agree. However, it would nice to update the sentence below if I 
understand it correctly.

"+   Comments, whitespace and continuations are handled in the same way 
as in" pg_hba.conf

For example, if a user provide a configuration like below (even such a 
comments is not recommended)

"host    all     all     127.0.0.1/32    trust  #COMMENTS, it works"

i.e. the original pg_hba.conf allows to have comments in each line, but 
with new continuation introduced, the comments has to be put to the last 
line.

>
> There are no assumption about backslash escaping, quotes and such, 
> which seems reasonable given the lexing structure of the files, i.e. 
> records of space-separated words, and # line comments.
>
-- 
David

Software Engineer
Highgo Software Inc. (Canada)
www.highgo.ca






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 18:05           ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
@ 2020-04-03 05:46             ` Fabien COELHO <coelho@cri.ensmp.fr>
  2020-07-06 15:38               ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 1 reply; 11+ messages in thread

From: Fabien COELHO @ 2020-04-03 05:46 UTC (permalink / raw)
  To: David Zhang <david.zhang@highgo.ca>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>


Hello David,

> +Agree. However, it would nice to update the sentence below if I understand 
> it correctly.
>
> "+   Comments, whitespace and continuations are handled in the same way as 
> in" pg_hba.conf

In the attached v3, I've tried to clarify comments and doc about 
tokenization rules relating to comments, strings and continuations.

-- 
Fabien.

Attachments:

pg-hba-cont-3.patch (text/x-diff, 5.4K) download diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml index 5f1eec78fb..5d6412de9b 100644 --- a/doc/src/sgml/client-auth.sgml +++ b/doc/src/sgml/client-auth.sgml @@ -77,13 +77,16 @@ The general format of the <filename>pg_hba.conf</filename> file is a set of records, one per line. Blank lines are ignored, as is any text after the <literal>#</literal> comment character. - Records cannot be continued across lines. + Lines ending with a backslash are logically continued on the next + line, so a record can span several lines. A record is made up of a number of fields which are separated by spaces and/or tabs. Fields can contain white space if the field value is double-quoted. Quoting one of the keywords in a database, user, or address field (e.g., <literal>all</literal> or <literal>replication</literal>) makes the word lose its special meaning, and just match a database, user, or host with that name. + If a continuation occurs within a quoted text or a comment, + the quoted text or comment are continued. </para> <para> @@ -821,7 +824,7 @@ local db1,db2,@demodbs all md5 <synopsis> <replaceable>map-name</replaceable> <replaceable>system-username</replaceable> <replaceable>database-username</replaceable> </synopsis> - Comments and whitespace are handled in the same way as in + Comments, whitespace and continuations are handled in the same way as in <filename>pg_hba.conf</filename>. The <replaceable>map-name</replaceable> is an arbitrary name that will be used to refer to this mapping in <filename>pg_hba.conf</filename>. The other diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index da5189a4fa..e88758c5aa 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -166,11 +166,17 @@ pg_isblank(const char c) /* * Grab one token out of the string pointed to by *lineptr. * - * Tokens are strings of non-blank - * characters bounded by blank characters, commas, beginning of line, and - * end of line. Blank means space or tab. Tokens can be delimited by - * double quotes (this allows the inclusion of blanks, but not newlines). - * Comments (started by an unquoted '#') are skipped. + * Tokens are strings of non-blank characters bounded by blank characters, + * commas, beginning of line, and end of line. Blank means space or tab. + * + * Tokens can be delimited by double quotes (this allows the inclusion of + * blanks or '#', but not newlines). If a continuations occured within the + * quoted text, the quoted text is continued on the next line. There is no + * escape mechanism, thus character '"' cannot be part of a token. + * + * Comments (started by an unquoted '#') are skipped, i.e. the remainder + * of the line is ignored. If a line with a comment was backslash-continued, + * the continuated text is part of the comment, thus ignored as well. * * The token, if any, is returned at *buf (a buffer of size bufsz), and * *lineptr is advanced past the token. @@ -486,8 +492,43 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) char *lineptr; List *current_line = NIL; char *err_msg = NULL; + char *cur = rawline; + int len = sizeof(rawline); + int continuations = 0; - if (!fgets(rawline, sizeof(rawline), file)) + /* read input and handle simplistic backslash continuations */ + while ((cur = fgets(cur, len, file)) != NULL) + { + int curlen = strlen(cur); + char *curend = cur + curlen - 1; + + if (curlen == len - 1) + { + /* Line too long! */ + ereport(elevel, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("authentication file line too long"), + errcontext("line %d of configuration file \"%s\"", + line_number + continuations, filename))); + err_msg = "authentication file line too long"; + } + + /* Strip trailing linebreak from rawline */ + while (curend >= cur && (*curend == '\n' || *curend == '\r')) + *curend-- = '\0'; + + /* empty or not a continuation, we are done */ + if (curend < cur || *curend != '\\') + break; + + /* else we have a continuation, just remove it and loop */ + continuations++; + *curend = '\0'; + len -= (curend - cur); + cur = curend; + } + + if (cur == NULL) { int save_errno = errno; @@ -501,21 +542,6 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) filename, strerror(save_errno)); rawline[0] = '\0'; } - if (strlen(rawline) == MAX_LINE - 1) - { - /* Line too long! */ - ereport(elevel, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("authentication file line too long"), - errcontext("line %d of configuration file \"%s\"", - line_number, filename))); - err_msg = "authentication file line too long"; - } - - /* Strip trailing linebreak from rawline */ - lineptr = rawline + strlen(rawline) - 1; - while (lineptr >= rawline && (*lineptr == '\n' || *lineptr == '\r')) - *lineptr-- = '\0'; /* Parse fields */ lineptr = rawline; @@ -543,7 +569,7 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) *tok_lines = lappend(*tok_lines, tok_line); } - line_number++; + line_number += continuations + 1; } MemoryContextSwitchTo(oldcxt);
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Allow continuations in "pg_hba.conf" files
  2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-03-25 19:39 ` Re: Allow continuations in "pg_hba.conf" files Justin Pryzby <pryzby@telsasoft.com>
  2020-03-25 20:45   ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 00:20     ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-02 04:25       ` Re: Allow continuations in "pg_hba.conf" files Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2020-04-02 05:25         ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
  2020-04-02 18:05           ` Re: Allow continuations in "pg_hba.conf" files David Zhang <david.zhang@highgo.ca>
  2020-04-03 05:46             ` Re: Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
@ 2020-07-06 15:38               ` Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 0 replies; 11+ messages in thread

From: Fabien COELHO @ 2020-07-06 15:38 UTC (permalink / raw)
  To: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; David Zhang <david.zhang@highgo.ca>; Justin Pryzby <pryzby@telsasoft.com>


> In the attached v3, I've tried to clarify comments and doc about tokenization 
> rules relating to comments, strings and continuations.

Attached v4 improves comments & doc as suggested by Justin.

-- 
Fabien.

Attachments:

pg-hba-cont-4.patch (text/x-diff, 5.3K) download diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml index 5cd88b462d..f1fc17935d 100644 --- a/doc/src/sgml/client-auth.sgml +++ b/doc/src/sgml/client-auth.sgml @@ -77,13 +77,16 @@ The general format of the <filename>pg_hba.conf</filename> file is a set of records, one per line. Blank lines are ignored, as is any text after the <literal>#</literal> comment character. - Records cannot be continued across lines. + Lines ending with a backslash are logically continued on the next + line, so a record can span several lines. A record is made up of a number of fields which are separated by spaces and/or tabs. Fields can contain white space if the field value is double-quoted. Quoting one of the keywords in a database, user, or address field (e.g., <literal>all</literal> or <literal>replication</literal>) makes the word lose its special meaning, and just match a database, user, or host with that name. + If a continuation occurs within quoted text or a comment, + it is continued. </para> <para> @@ -821,7 +824,7 @@ local db1,db2,@demodbs all md5 <synopsis> <replaceable>map-name</replaceable> <replaceable>system-username</replaceable> <replaceable>database-username</replaceable> </synopsis> - Comments and whitespace are handled in the same way as in + Comments, whitespace and continuations are handled in the same way as in <filename>pg_hba.conf</filename>. The <replaceable>map-name</replaceable> is an arbitrary name that will be used to refer to this mapping in <filename>pg_hba.conf</filename>. The other diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index da5189a4fa..6c3b780ace 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -166,11 +166,17 @@ pg_isblank(const char c) /* * Grab one token out of the string pointed to by *lineptr. * - * Tokens are strings of non-blank - * characters bounded by blank characters, commas, beginning of line, and - * end of line. Blank means space or tab. Tokens can be delimited by - * double quotes (this allows the inclusion of blanks, but not newlines). - * Comments (started by an unquoted '#') are skipped. + * Tokens are strings of non-blank characters bounded by blank characters, + * commas, beginning of line, and end of line. Blank means space or tab. + * + * Tokens can be delimited by double quotes (this allows the inclusion of + * blanks or '#', but not newlines). If a continuation occurs within the + * quoted text, the quoted text is continued on the next line. There is no + * escape mechanism, thus character '"' cannot be part of a token. + * + * Comments (started by an unquoted '#') are skipped, i.e. the remainder + * of the line is ignored. If a line with a comment is backslash-continued, + * the continued text is part of the comment, thus ignored as well. * * The token, if any, is returned at *buf (a buffer of size bufsz), and * *lineptr is advanced past the token. @@ -486,8 +492,43 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) char *lineptr; List *current_line = NIL; char *err_msg = NULL; + char *cur = rawline; + int len = sizeof(rawline); + int continuations = 0; - if (!fgets(rawline, sizeof(rawline), file)) + /* read input and handle simple backslash continuations */ + while ((cur = fgets(cur, len, file)) != NULL) + { + int curlen = strlen(cur); + char *curend = cur + curlen - 1; + + if (curlen == len - 1) + { + /* Line too long! */ + ereport(elevel, + errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("authentication file line too long"), + errcontext("line %d of configuration file \"%s\"", + line_number + continuations, filename)); + err_msg = "authentication file line too long"; + } + + /* Strip trailing linebreak from rawline */ + while (curend >= cur && (*curend == '\n' || *curend == '\r')) + *curend-- = '\0'; + + /* empty or not a continuation, we are done */ + if (curend < cur || *curend != '\\') + break; + + /* else we have a continuation, just remove it and loop */ + continuations++; + *curend = '\0'; + len -= (curend - cur); + cur = curend; + } + + if (cur == NULL) { int save_errno = errno; @@ -501,21 +542,6 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) filename, strerror(save_errno)); rawline[0] = '\0'; } - if (strlen(rawline) == MAX_LINE - 1) - { - /* Line too long! */ - ereport(elevel, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("authentication file line too long"), - errcontext("line %d of configuration file \"%s\"", - line_number, filename))); - err_msg = "authentication file line too long"; - } - - /* Strip trailing linebreak from rawline */ - lineptr = rawline + strlen(rawline) - 1; - while (lineptr >= rawline && (*lineptr == '\n' || *lineptr == '\r')) - *lineptr-- = '\0'; /* Parse fields */ lineptr = rawline; @@ -543,7 +569,7 @@ tokenize_file(const char *filename, FILE *file, List **tok_lines, int elevel) *tok_lines = lappend(*tok_lines, tok_line); } - line_number++; + line_number += continuations + 1; } MemoryContextSwitchTo(oldcxt);
^ permalink raw reply [nested|flat] 11+ messages in thread


end of thread, other threads:[~2020-07-06 15:38 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-25 18:09 Allow continuations in "pg_hba.conf" files Fabien COELHO <coelho@cri.ensmp.fr>
2020-03-25 19:39 ` Justin Pryzby <pryzby@telsasoft.com>
2020-03-25 20:45   ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-04-02 00:20     ` David Zhang <david.zhang@highgo.ca>
2020-04-02 04:25       ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
2020-04-02 05:25         ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-04-02 05:58           ` Justin Pryzby <pryzby@telsasoft.com>
2020-04-02 06:46             ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-04-02 18:05           ` David Zhang <david.zhang@highgo.ca>
2020-04-03 05:46             ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-07-06 15:38               ` Fabien COELHO <coelho@cri.ensmp.fr>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox