From c52aab5525c13a3d378cd09f4187844ce697a948 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 22 Sep 2008 14:21:44 +0000 Subject: [PATCH] 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. --- doc/src/sgml/libpq.sgml | 12 ++++-------- src/interfaces/libpq/fe-connect.c | 32 +++++++------------------------ src/interfaces/libpq/libpq-int.h | 3 +-- 3 files changed, 12 insertions(+), 35 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 4086f2f7b2..2e13e1daa4 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,4 +1,4 @@ - + <application>libpq</application> - C Library @@ -1201,7 +1201,6 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg); int PQconnectionNeedsPassword(const PGconn *conn); - @@ -1216,19 +1215,16 @@ PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg); 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. int PQconnectionUsedPassword(const PGconn *conn); - - This function detects whether a password supplied to the connection - function was actually used. Passwords obtained from other - sources (such as the .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. diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index bd70679aa2..0d2ec1dda6 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -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; diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index b29057bde9..9702c61682 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -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 */ -- 2.40.0