Get rid of pgpass_from_client tracking inside libpq --- given the conclusion

that presence of the password in the conninfo string must be checked *before*
risking a connection attempt, there is no point in checking it afterwards.
This makes the specification of PQconnectionUsedPassword() a bit simpler
and perhaps more generally useful, too.
This commit is contained in:
Tom Lane 2008-09-22 14:21:44 +00:00
parent cae7ad906a
commit c52aab5525
3 changed files with 12 additions and 35 deletions

View File

@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.264 2008/09/22 13:55:13 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.265 2008/09/22 14:21:44 tgl Exp $ -->
<chapter id="libpq"> <chapter id="libpq">
<title><application>libpq</application> - C Library</title> <title><application>libpq</application> - C Library</title>
@ -1201,7 +1201,6 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
<synopsis> <synopsis>
int PQconnectionNeedsPassword(const PGconn *conn); int PQconnectionNeedsPassword(const PGconn *conn);
</synopsis> </synopsis>
</para> </para>
<para> <para>
@ -1216,19 +1215,16 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
<listitem> <listitem>
<para> <para>
Returns true (1) if the connection authentication method Returns true (1) if the connection authentication method
used a caller-supplied password. Returns false (0) if not. used a password. Returns false (0) if not.
<synopsis> <synopsis>
int PQconnectionUsedPassword(const PGconn *conn); int PQconnectionUsedPassword(const PGconn *conn);
</synopsis> </synopsis>
</para> </para>
<para> <para>
This function detects whether a password supplied to the connection This function can be applied after either a failed or successful
function was actually used. Passwords obtained from other connection attempt to detect whether the server demanded a password.
sources (such as the <filename>.pgpass</> file) are not considered
caller-supplied.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.361 2008/09/22 13:55:14 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.362 2008/09/22 14:21:44 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -232,8 +232,7 @@ static PGconn *makeEmptyPGconn(void);
static void freePGconn(PGconn *conn); static void freePGconn(PGconn *conn);
static void closePGconn(PGconn *conn); static void closePGconn(PGconn *conn);
static PQconninfoOption *conninfo_parse(const char *conninfo, static PQconninfoOption *conninfo_parse(const char *conninfo,
PQExpBuffer errorMessage, bool use_defaults, PQExpBuffer errorMessage, bool use_defaults);
bool *password_from_string);
static char *conninfo_getval(PQconninfoOption *connOptions, static char *conninfo_getval(PQconninfoOption *connOptions,
const char *keyword); const char *keyword);
static void defaultNoticeReceiver(void *arg, const PGresult *res); static void defaultNoticeReceiver(void *arg, const PGresult *res);
@ -377,8 +376,7 @@ connectOptions1(PGconn *conn, const char *conninfo)
/* /*
* Parse the conninfo string * Parse the conninfo string
*/ */
connOptions = conninfo_parse(conninfo, &conn->errorMessage, true, connOptions = conninfo_parse(conninfo, &conn->errorMessage, true);
&conn->pgpass_from_client);
if (connOptions == NULL) if (connOptions == NULL)
{ {
conn->status = CONNECTION_BAD; conn->status = CONNECTION_BAD;
@ -474,7 +472,6 @@ connectOptions2(PGconn *conn)
conn->dbName, conn->pguser); conn->dbName, conn->pguser);
if (conn->pgpass == NULL) if (conn->pgpass == NULL)
conn->pgpass = strdup(DefaultPassword); conn->pgpass = strdup(DefaultPassword);
conn->pgpass_from_client = false;
} }
/* /*
@ -560,14 +557,12 @@ PQconninfoOption *
PQconndefaults(void) PQconndefaults(void)
{ {
PQExpBufferData errorBuf; PQExpBufferData errorBuf;
bool password_from_string;
PQconninfoOption *connOptions; PQconninfoOption *connOptions;
initPQExpBuffer(&errorBuf); initPQExpBuffer(&errorBuf);
if (errorBuf.data == NULL) if (errorBuf.data == NULL)
return NULL; /* out of memory already :-( */ return NULL; /* out of memory already :-( */
connOptions = conninfo_parse("", &errorBuf, true, connOptions = conninfo_parse("", &errorBuf, true);
&password_from_string);
termPQExpBuffer(&errorBuf); termPQExpBuffer(&errorBuf);
return connOptions; return connOptions;
} }
@ -668,7 +663,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
if (conn->pgpass) if (conn->pgpass)
free(conn->pgpass); free(conn->pgpass);
conn->pgpass = strdup(pwd); conn->pgpass = strdup(pwd);
conn->pgpass_from_client = true;
} }
/* /*
@ -3127,7 +3121,6 @@ PQconninfoOption *
PQconninfoParse(const char *conninfo, char **errmsg) PQconninfoParse(const char *conninfo, char **errmsg)
{ {
PQExpBufferData errorBuf; PQExpBufferData errorBuf;
bool password_from_string;
PQconninfoOption *connOptions; PQconninfoOption *connOptions;
if (errmsg) if (errmsg)
@ -3135,8 +3128,7 @@ PQconninfoParse(const char *conninfo, char **errmsg)
initPQExpBuffer(&errorBuf); initPQExpBuffer(&errorBuf);
if (errorBuf.data == NULL) if (errorBuf.data == NULL)
return NULL; /* out of memory already :-( */ return NULL; /* out of memory already :-( */
connOptions = conninfo_parse(conninfo, &errorBuf, false, connOptions = conninfo_parse(conninfo, &errorBuf, false);
&password_from_string);
if (connOptions == NULL && errmsg) if (connOptions == NULL && errmsg)
*errmsg = errorBuf.data; *errmsg = errorBuf.data;
else else
@ -3152,12 +3144,10 @@ PQconninfoParse(const char *conninfo, char **errmsg)
* left in errorMessage. * left in errorMessage.
* Defaults are supplied (from a service file, environment variables, etc) * Defaults are supplied (from a service file, environment variables, etc)
* for unspecified options, but only if use_defaults is TRUE. * for unspecified options, but only if use_defaults is TRUE.
* *password_from_string is set TRUE if we got a password from the
* conninfo string, otherwise FALSE.
*/ */
static PQconninfoOption * static PQconninfoOption *
conninfo_parse(const char *conninfo, PQExpBuffer errorMessage, conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
bool use_defaults, bool *password_from_string) bool use_defaults)
{ {
char *pname; char *pname;
char *pval; char *pval;
@ -3168,8 +3158,6 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
PQconninfoOption *options; PQconninfoOption *options;
PQconninfoOption *option; PQconninfoOption *option;
*password_from_string = false; /* default result */
/* Make a working copy of PQconninfoOptions */ /* Make a working copy of PQconninfoOptions */
options = malloc(sizeof(PQconninfoOptions)); options = malloc(sizeof(PQconninfoOptions));
if (options == NULL) if (options == NULL)
@ -3326,12 +3314,6 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
free(buf); free(buf);
return NULL; return NULL;
} }
/*
* Special handling for password
*/
if (strcmp(option->keyword, "password") == 0)
*password_from_string = (option->val[0] != '\0');
} }
/* Done with the modifiable input string */ /* Done with the modifiable input string */
@ -3597,7 +3579,7 @@ PQconnectionUsedPassword(const PGconn *conn)
{ {
if (!conn) if (!conn)
return false; return false;
if (conn->password_needed && conn->pgpass_from_client) if (conn->password_needed)
return true; return true;
else else
return false; return false;

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.133 2008/09/19 16:40:40 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.134 2008/09/22 14:21:44 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -290,7 +290,6 @@ struct pg_conn
char *dbName; /* database name */ char *dbName; /* database name */
char *pguser; /* Postgres username and password, if any */ char *pguser; /* Postgres username and password, if any */
char *pgpass; char *pgpass;
bool pgpass_from_client; /* did password come from connect args? */
char *sslmode; /* SSL mode (require,prefer,allow,disable) */ char *sslmode; /* SSL mode (require,prefer,allow,disable) */
#if defined(KRB5) || defined(ENABLE_GSS) || defined(ENABLE_SSPI) #if defined(KRB5) || defined(ENABLE_GSS) || defined(ENABLE_SSPI)
char *krbsrvname; /* Kerberos service name */ char *krbsrvname; /* Kerberos service name */