Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit55aea0c

Browse files
committed
Teach libpq to handle arbitrary-length lines in .pgpass files.
Historically there's been a hard-wired assumption here that no line ofa .pgpass file could be as long as NAMEDATALEN*5 bytes. That's a bitshaky to start off with, because (a) there's no reason to suppose thathost names fit in NAMEDATALEN, and (b) this figure fails to allow forbackslash escape characters. However, it fails completely if someonewants to use a very long password, and we're now hearing reports ofpeople wanting to use "security tokens" that can run up to severalhundred bytes. Another angle is that the file is specified to allowcomment lines, but there's no reason to assume that long comment linesaren't possible.Rather than guessing at what might be a more suitable limit, let'sreplace the fixed-size buffer with an expansible PQExpBuffer. Thatadds one malloc/free cycle to the typical use-case, but that's surelypretty cheap relative to the I/O this code has to do.Also, add TAP test cases to exercise this code, because there was notest coverage before.This reverts most of commit2eb3bc5, as there's no longer a need fora warning message about overlength .pgpass lines. (I kept the explicitcheck for comment lines, though.)In HEAD and v13, this also fixes an oversight in74a308c: there's notmuch point in explicit_bzero'ing the line buffer if we only do so in twoof the three exit paths.Back-patch to all supported branches, except that the test case onlygoes back to v10 where src/test/authentication/ was added.Discussion:https://postgr.es/m/4187382.1598909041@sss.pgh.pa.us
1 parent3fb67ac commit55aea0c

File tree

2 files changed

+84
-44
lines changed

2 files changed

+84
-44
lines changed

‎src/interfaces/libpq/fe-connect.c

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6849,9 +6849,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
68496849
{
68506850
FILE*fp;
68516851
structstatstat_buf;
6852-
6853-
#defineLINELEN NAMEDATALEN*5
6854-
charbuf[LINELEN];
6852+
PQExpBufferDatabuf;
68556853

68566854
if (dbname==NULL||dbname[0]=='\0')
68576855
returnNULL;
@@ -6907,63 +6905,81 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
69076905
if (fp==NULL)
69086906
returnNULL;
69096907

6908+
/* Use an expansible buffer to accommodate any reasonable line length */
6909+
initPQExpBuffer(&buf);
6910+
69106911
while (!feof(fp)&& !ferror(fp))
69116912
{
6912-
char*t=buf,
6913-
*ret,
6914-
*p1,
6915-
*p2;
6916-
intlen;
6913+
/* Make sure there's a reasonable amount of room in the buffer */
6914+
if (!enlargePQExpBuffer(&buf,128))
6915+
break;
69176916

6918-
if (fgets(buf,sizeof(buf),fp)==NULL)
6917+
/* Read some data, appending it to what we already have */
6918+
if (fgets(buf.data+buf.len,buf.maxlen-buf.len,fp)==NULL)
69196919
break;
6920+
buf.len+=strlen(buf.data+buf.len);
69206921

6921-
len=strlen(buf);
6922+
/* If we don't yet have a whole line, loop around to read more */
6923+
if (!(buf.len>0&&buf.data[buf.len-1]=='\n')&& !feof(fp))
6924+
continue;
69226925

6923-
/*Remove trailing newline */
6924-
if (len>0&&buf[len-1]=='\n')
6926+
/*ignore comments */
6927+
if (buf.data[0]!='#')
69256928
{
6926-
buf[--len]='\0';
6927-
/* Handle DOS-style line endings, too, even when not on Windows */
6928-
if (len>0&&buf[len-1]=='\r')
6929-
buf[--len]='\0';
6930-
}
6929+
char*t=buf.data;
6930+
intlen=buf.len;
69316931

6932-
if (len==0)
6933-
continue;
6932+
/* Remove trailing newline */
6933+
if (len>0&&t[len-1]=='\n')
6934+
{
6935+
t[--len]='\0';
6936+
/* Handle DOS-style line endings, too */
6937+
if (len>0&&t[len-1]=='\r')
6938+
t[--len]='\0';
6939+
}
69346940

6935-
if ((t=pwdfMatchesString(t,hostname))==NULL||
6936-
(t=pwdfMatchesString(t,port))==NULL||
6937-
(t=pwdfMatchesString(t,dbname))==NULL||
6938-
(t=pwdfMatchesString(t,username))==NULL)
6939-
continue;
6941+
if (len>0&&
6942+
(t=pwdfMatchesString(t,hostname))!=NULL&&
6943+
(t=pwdfMatchesString(t,port))!=NULL&&
6944+
(t=pwdfMatchesString(t,dbname))!=NULL&&
6945+
(t=pwdfMatchesString(t,username))!=NULL)
6946+
{
6947+
/* Found a match. */
6948+
char*ret,
6949+
*p1,
6950+
*p2;
69406951

6941-
/* Found a match. */
6942-
ret=strdup(t);
6943-
fclose(fp);
6952+
ret=strdup(t);
69446953

6945-
if (!ret)
6946-
{
6947-
/* Out of memory. XXX: an error message would be nice. */
6948-
returnNULL;
6949-
}
6954+
fclose(fp);
6955+
termPQExpBuffer(&buf);
69506956

6951-
/* De-escape password. */
6952-
for (p1=p2=ret;*p1!=':'&&*p1!='\0';++p1,++p2)
6953-
{
6954-
if (*p1=='\\'&&p1[1]!='\0')
6955-
++p1;
6956-
*p2=*p1;
6957+
if (!ret)
6958+
{
6959+
/* Out of memory. XXX: an error message would be nice. */
6960+
returnNULL;
6961+
}
6962+
6963+
/* De-escape password. */
6964+
for (p1=p2=ret;*p1!=':'&&*p1!='\0';++p1,++p2)
6965+
{
6966+
if (*p1=='\\'&&p1[1]!='\0')
6967+
++p1;
6968+
*p2=*p1;
6969+
}
6970+
*p2='\0';
6971+
6972+
returnret;
6973+
}
69576974
}
6958-
*p2='\0';
69596975

6960-
returnret;
6976+
/* No match, reset buffer to prepare for next line. */
6977+
buf.len=0;
69616978
}
69626979

69636980
fclose(fp);
6981+
termPQExpBuffer(&buf);
69646982
returnNULL;
6965-
6966-
#undef LINELEN
69676983
}
69686984

69696985

‎src/test/authentication/t/001_password.pl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
else
1919
{
20-
plantests=>8;
20+
plantests=>11;
2121
}
2222

2323

@@ -45,7 +45,9 @@ sub test_role
4545

4646
$status_string ='success'if ($expected_reseq 0);
4747

48-
my$res =$node->psql('postgres',undef,extra_params=> ['-U',$role ]);
48+
local$Test::Builder::Level =$Test::Builder::Level + 1;
49+
50+
my$res =$node->psql('postgres',undef,extra_params=> ['-U',$role,'-w' ]);
4951
is($res,$expected_res,
5052
"authentication$status_string for method$method, role$role");
5153
return;
@@ -86,3 +88,25 @@ sub test_role
8688
reset_pg_hba($node,'md5');
8789
test_role($node,'scram_role','md5', 0);
8890
test_role($node,'md5_role','md5', 0);
91+
92+
# Test .pgpass processing; but use a temp file, don't overwrite the real one!
93+
my$pgpassfile ="${TestLib::tmp_check}/pgpass";
94+
95+
delete$ENV{"PGPASSWORD"};
96+
$ENV{"PGPASSFILE"} =$pgpassfile;
97+
98+
append_to_file($pgpassfile,qq!
99+
# This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file.
100+
*:*:postgres:scram_role:pass:this is not part of the password.
101+
!);
102+
chmod 0600,$pgpassfileordie;
103+
104+
reset_pg_hba($node,'password');
105+
test_role($node,'scram_role','password from pgpass', 0);
106+
test_role($node,'md5_role','password from pgpass', 2);
107+
108+
append_to_file($pgpassfile,qq!
109+
*:*:*:md5_role:p\\ass
110+
!);
111+
112+
test_role($node,'md5_role','password from pgpass', 0);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp