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

Commit8331c11

Browse files
committed
Get rid of client-code dependencies on the exact text of the no-password
error message, by using PQconnectionUsedPassword() instead. Somedaywe might be able to localize that error message, but not until thiscoding technique has disappeared everywhere.
1 parent5f7b1f8 commit8331c11

File tree

6 files changed

+58
-69
lines changed

6 files changed

+58
-69
lines changed

‎src/bin/pg_ctl/pg_ctl.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.81 2007/07/02 21:58:31 mha Exp $
7+
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.82 2007/07/08 19:07:38 tgl Exp $
88
*
99
*-------------------------------------------------------------------------
1010
*/
@@ -480,15 +480,18 @@ test_postmaster_connection(bool do_checkpoint)
480480
if (!*portstr)
481481
snprintf(portstr,sizeof(portstr),"%d",DEF_PGPORT);
482482

483-
/* We need to set a connect timeout otherwise on Windows the SCM will probably timeout first */
484-
snprintf(connstr,sizeof(connstr),"dbname=postgres port=%s connect_timeout=5",portstr);
483+
/*
484+
* We need to set a connect timeout otherwise on Windows the SCM will
485+
* probably timeout first
486+
*/
487+
snprintf(connstr,sizeof(connstr),
488+
"dbname=postgres port=%s connect_timeout=5",portstr);
485489

486490
for (i=0;i<wait_seconds;i++)
487491
{
488492
if ((conn=PQconnectdb(connstr))!=NULL&&
489493
(PQstatus(conn)==CONNECTION_OK||
490-
(strcmp(PQerrorMessage(conn),
491-
PQnoPasswordSupplied)==0)))
494+
PQconnectionUsedPassword(conn)))
492495
{
493496
PQfinish(conn);
494497
success= true;

‎src/bin/pg_dump/pg_backup_db.c

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.75 2006/10/04 00:30:05 momjian Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.76 2007/07/08 19:07:38 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -123,13 +123,11 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
123123
staticPGconn*
124124
_connectDB(ArchiveHandle*AH,constchar*reqdb,constchar*requser)
125125
{
126-
intneed_pass;
127126
PGconn*newConn;
128-
char*password=NULL;
129-
intbadPwd=0;
130-
intnoPwd=0;
131127
char*newdb;
132128
char*newuser;
129+
char*password=NULL;
130+
boolnew_pass;
133131

134132
if (!reqdb)
135133
newdb=PQdb(AH->connection);
@@ -152,7 +150,7 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
152150

153151
do
154152
{
155-
need_pass= false;
153+
new_pass= false;
156154
newConn=PQsetdbLogin(PQhost(AH->connection),PQport(AH->connection),
157155
NULL,NULL,newdb,
158156
newuser,password);
@@ -161,30 +159,23 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
161159

162160
if (PQstatus(newConn)==CONNECTION_BAD)
163161
{
164-
noPwd= (strcmp(PQerrorMessage(newConn),
165-
PQnoPasswordSupplied)==0);
166-
badPwd= (strncmp(PQerrorMessage(newConn),
167-
"Password authentication failed for user",39)==0);
168-
169-
if (noPwd||badPwd)
170-
{
171-
if (badPwd)
172-
fprintf(stderr,"Password incorrect\n");
173-
174-
fprintf(stderr,"Connecting to %s as %s\n",
175-
newdb,newuser);
176-
177-
need_pass= true;
178-
if (password)
179-
free(password);
180-
password=simple_prompt("Password: ",100, false);
181-
}
182-
else
162+
if (!PQconnectionUsedPassword(newConn))
183163
die_horribly(AH,modulename,"could not reconnect to database: %s",
184164
PQerrorMessage(newConn));
185165
PQfinish(newConn);
166+
167+
if (password)
168+
fprintf(stderr,"Password incorrect\n");
169+
170+
fprintf(stderr,"Connecting to %s as %s\n",
171+
newdb,newuser);
172+
173+
if (password)
174+
free(password);
175+
password=simple_prompt("Password: ",100, false);
176+
new_pass= true;
186177
}
187-
}while (need_pass);
178+
}while (new_pass);
188179

189180
if (password)
190181
free(password);
@@ -214,7 +205,7 @@ ConnectDatabase(Archive *AHX,
214205
{
215206
ArchiveHandle*AH= (ArchiveHandle*)AHX;
216207
char*password=NULL;
217-
boolneed_pass= false;
208+
boolnew_pass;
218209

219210
if (AH->connection)
220211
die_horribly(AH,modulename,"already connected to a database\n");
@@ -235,24 +226,23 @@ ConnectDatabase(Archive *AHX,
235226
*/
236227
do
237228
{
238-
need_pass= false;
229+
new_pass= false;
239230
AH->connection=PQsetdbLogin(pghost,pgport,NULL,NULL,
240231
dbname,username,password);
241232

242233
if (!AH->connection)
243234
die_horribly(AH,modulename,"failed to connect to database\n");
244235

245236
if (PQstatus(AH->connection)==CONNECTION_BAD&&
246-
strcmp(PQerrorMessage(AH->connection),PQnoPasswordSupplied)==0&&
237+
PQconnectionUsedPassword(AH->connection)&&
238+
password==NULL&&
247239
!feof(stdin))
248240
{
249241
PQfinish(AH->connection);
250-
need_pass= true;
251-
free(password);
252-
password=NULL;
253242
password=simple_prompt("Password: ",100, false);
243+
new_pass= true;
254244
}
255-
}while (need_pass);
245+
}while (new_pass);
256246

257247
if (password)
258248
free(password);

‎src/bin/pg_dump/pg_dumpall.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.91 2007/05/15 20:20:21 alvherre Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.92 2007/07/08 19:07:38 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -1310,7 +1310,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
13101310
constchar*pguser,boolrequire_password,boolfail_on_error)
13111311
{
13121312
PGconn*conn;
1313-
boolneed_pass= false;
1313+
boolnew_pass;
13141314
constchar*remoteversion_str;
13151315
intmy_version;
13161316
staticchar*password=NULL;
@@ -1324,7 +1324,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
13241324
*/
13251325
do
13261326
{
1327-
need_pass= false;
1327+
new_pass= false;
13281328
conn=PQsetdbLogin(pghost,pgport,NULL,NULL,dbname,pguser,password);
13291329

13301330
if (!conn)
@@ -1335,17 +1335,15 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
13351335
}
13361336

13371337
if (PQstatus(conn)==CONNECTION_BAD&&
1338-
strcmp(PQerrorMessage(conn),PQnoPasswordSupplied)==0&&
1338+
PQconnectionUsedPassword(conn)&&
1339+
password==NULL&&
13391340
!feof(stdin))
13401341
{
13411342
PQfinish(conn);
1342-
need_pass= true;
1343-
if (password)
1344-
free(password);
1345-
password=NULL;
13461343
password=simple_prompt("Password: ",100, false);
1344+
new_pass= true;
13471345
}
1348-
}while (need_pass);
1346+
}while (new_pass);
13491347

13501348
/* check to see that the backend connection was successfully made */
13511349
if (PQstatus(conn)==CONNECTION_BAD)

‎src/bin/psql/command.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.179 2007/03/03 17:19:11 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.180 2007/07/0819:07:38 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"command.h"
@@ -1110,11 +1110,11 @@ do_connect(char *dbname, char *user, char *host, char *port)
11101110
* If the user asked to be prompted for a password, ask for one now. If
11111111
* not, use the password from the old connection, provided the username
11121112
* has not changed. Otherwise, try to connect without a password first,
1113-
* and then ask for a password ifwe got the appropriate error message.
1113+
* and then ask for a password ifneeded.
11141114
*
1115-
* XXX: this behavioris broken. Itleads to spurious connection attempts
1116-
* in the postmaster's log, and doing a string comparison against the
1117-
*returned error message is pretty fragile.
1115+
* XXX: this behavior leads to spurious connection attempts recorded
1116+
* in the postmaster's log. But libpq offers no API that would let us
1117+
*obtain a password and then continue with the first connection attempt.
11181118
*/
11191119
if (pset.getPassword)
11201120
{
@@ -1141,7 +1141,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
11411141
* Connection attempt failed; either retry the connection attempt with
11421142
* a new password, or give up.
11431143
*/
1144-
if (strcmp(PQerrorMessage(n_conn),PQnoPasswordSupplied)==0)
1144+
if (!password&&PQconnectionUsedPassword(n_conn))
11451145
{
11461146
PQfinish(n_conn);
11471147
password=prompt_for_password(user);

‎src/bin/psql/startup.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.140 2007/02/01 19:10:29 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.141 2007/07/08 19:07:38 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99

@@ -108,7 +108,7 @@ main(int argc, char *argv[])
108108
char*username=NULL;
109109
char*password=NULL;
110110
char*password_prompt=NULL;
111-
boolneed_pass;
111+
boolnew_pass;
112112

113113
set_pglocale_pgservice(argv[0],"psql");
114114

@@ -204,23 +204,22 @@ main(int argc, char *argv[])
204204
/* loop until we have a password if requested by backend */
205205
do
206206
{
207-
need_pass= false;
207+
new_pass= false;
208208
pset.db=PQsetdbLogin(options.host,options.port,NULL,NULL,
209209
options.action==ACT_LIST_DB&&options.dbname==NULL ?
210210
"postgres" :options.dbname,
211211
username,password);
212212

213213
if (PQstatus(pset.db)==CONNECTION_BAD&&
214-
strcmp(PQerrorMessage(pset.db),PQnoPasswordSupplied)==0&&
214+
PQconnectionUsedPassword(pset.db)&&
215+
password==NULL&&
215216
!feof(stdin))
216217
{
217218
PQfinish(pset.db);
218-
need_pass= true;
219-
free(password);
220-
password=NULL;
221219
password=simple_prompt(password_prompt,100, false);
220+
new_pass= true;
222221
}
223-
}while (need_pass);
222+
}while (new_pass);
224223

225224
free(username);
226225
free(password);

‎src/bin/scripts/common.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.26 2007/04/09 18:21:22 mha Exp $
10+
* $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.27 2007/07/08 19:07:38 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -100,7 +100,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
100100
{
101101
PGconn*conn;
102102
char*password=NULL;
103-
boolneed_pass= false;
103+
boolnew_pass;
104104

105105
if (require_password)
106106
password=simple_prompt("Password: ",100, false);
@@ -111,7 +111,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
111111
*/
112112
do
113113
{
114-
need_pass= false;
114+
new_pass= false;
115115
conn=PQsetdbLogin(pghost,pgport,NULL,NULL,dbname,pguser,password);
116116

117117
if (!conn)
@@ -122,16 +122,15 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
122122
}
123123

124124
if (PQstatus(conn)==CONNECTION_BAD&&
125-
strcmp(PQerrorMessage(conn),PQnoPasswordSupplied)==0&&
125+
PQconnectionUsedPassword(conn)&&
126+
password==NULL&&
126127
!feof(stdin))
127128
{
128129
PQfinish(conn);
129-
need_pass= true;
130-
free(password);
131-
password=NULL;
132130
password=simple_prompt("Password: ",100, false);
131+
new_pass= true;
133132
}
134-
}while (need_pass);
133+
}while (new_pass);
135134

136135
if (password)
137136
free(password);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp