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">
<title><application>libpq</application> - C Library</title>
@ -1201,7 +1201,6 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
<synopsis>
int PQconnectionNeedsPassword(const PGconn *conn);
</synopsis>
</para>
<para>
@ -1216,19 +1215,16 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
<listitem>
<para>
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>
int PQconnectionUsedPassword(const PGconn *conn);
</synopsis>
</para>
<para>
This function detects whether a password supplied to the connection
function was actually used. Passwords obtained from other
sources (such as the <filename>.pgpass</> file) are not considered
caller-supplied.
This function can be applied after either a failed or successful
connection attempt to detect whether the server demanded a password.
</para>
</listitem>
</varlistentry>

View File

@ -8,7 +8,7 @@
*
*
* 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 closePGconn(PGconn *conn);
static PQconninfoOption *conninfo_parse(const char *conninfo,
PQExpBuffer errorMessage, bool use_defaults,
bool *password_from_string);
PQExpBuffer errorMessage, bool use_defaults);
static char *conninfo_getval(PQconninfoOption *connOptions,
const char *keyword);
static void defaultNoticeReceiver(void *arg, const PGresult *res);
@ -377,8 +376,7 @@ connectOptions1(PGconn *conn, const char *conninfo)
/*
* Parse the conninfo string
*/
connOptions = conninfo_parse(conninfo, &conn->errorMessage, true,
&conn->pgpass_from_client);
connOptions = conninfo_parse(conninfo, &conn->errorMessage, true);
if (connOptions == NULL)
{
conn->status = CONNECTION_BAD;
@ -474,7 +472,6 @@ connectOptions2(PGconn *conn)
conn->dbName, conn->pguser);
if (conn->pgpass == NULL)
conn->pgpass = strdup(DefaultPassword);
conn->pgpass_from_client = false;
}
/*
@ -560,14 +557,12 @@ PQconninfoOption *
PQconndefaults(void)
{
PQExpBufferData errorBuf;
bool password_from_string;
PQconninfoOption *connOptions;
initPQExpBuffer(&errorBuf);
if (errorBuf.data == NULL)
return NULL; /* out of memory already :-( */
connOptions = conninfo_parse("", &errorBuf, true,
&password_from_string);
connOptions = conninfo_parse("", &errorBuf, true);
termPQExpBuffer(&errorBuf);
return connOptions;
}
@ -668,7 +663,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
if (conn->pgpass)
free(conn->pgpass);
conn->pgpass = strdup(pwd);
conn->pgpass_from_client = true;
}
/*
@ -3127,7 +3121,6 @@ PQconninfoOption *
PQconninfoParse(const char *conninfo, char **errmsg)
{
PQExpBufferData errorBuf;
bool password_from_string;
PQconninfoOption *connOptions;
if (errmsg)
@ -3135,8 +3128,7 @@ PQconninfoParse(const char *conninfo, char **errmsg)
initPQExpBuffer(&errorBuf);
if (errorBuf.data == NULL)
return NULL; /* out of memory already :-( */
connOptions = conninfo_parse(conninfo, &errorBuf, false,
&password_from_string);
connOptions = conninfo_parse(conninfo, &errorBuf, false);
if (connOptions == NULL && errmsg)
*errmsg = errorBuf.data;
else
@ -3152,12 +3144,10 @@ PQconninfoParse(const char *conninfo, char **errmsg)
* left in errorMessage.
* Defaults are supplied (from a service file, environment variables, etc)
* 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 *
conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
bool use_defaults, bool *password_from_string)
bool use_defaults)
{
char *pname;
char *pval;
@ -3168,8 +3158,6 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
PQconninfoOption *options;
PQconninfoOption *option;
*password_from_string = false; /* default result */
/* Make a working copy of PQconninfoOptions */
options = malloc(sizeof(PQconninfoOptions));
if (options == NULL)
@ -3326,12 +3314,6 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
free(buf);
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 */
@ -3597,7 +3579,7 @@ PQconnectionUsedPassword(const PGconn *conn)
{
if (!conn)
return false;
if (conn->password_needed && conn->pgpass_from_client)
if (conn->password_needed)
return true;
else
return false;

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* 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 *pguser; /* Postgres username and password, if any */
char *pgpass;
bool pgpass_from_client; /* did password come from connect args? */
char *sslmode; /* SSL mode (require,prefer,allow,disable) */
#if defined(KRB5) || defined(ENABLE_GSS) || defined(ENABLE_SSPI)
char *krbsrvname; /* Kerberos service name */