diff options
Diffstat (limited to 'src/bin/pg_dump/pg_backup_db.c')
-rw-r--r-- | src/bin/pg_dump/pg_backup_db.c | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index 0818b353e43..89fbabdfe6d 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -5,7 +5,7 @@ * Implements the basic DB functions used by the archiver. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.80 2008/08/16 02:25:06 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.81 2009/02/02 20:07:37 adunstan Exp $ * *------------------------------------------------------------------------- */ @@ -116,29 +116,36 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username) /* * Connect to the db again. + * + * Note: it's not really all that sensible to use a single-entry password + * cache if the username keeps changing. In current usage, however, the + * username never does change, so one savedPassword is sufficient. We do + * update the cache on the off chance that the password has changed since the + * start of the run. */ static PGconn * _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) { PGconn *newConn; - char *newdb; - char *newuser; - char *password = NULL; + const char *newdb; + const char *newuser; + char *password = AH->savedPassword; bool new_pass; if (!reqdb) newdb = PQdb(AH->connection); else - newdb = (char *) reqdb; + newdb = reqdb; - if (!requser || (strlen(requser) == 0)) + if (!requser || strlen(requser) == 0) newuser = PQuser(AH->connection); else - newuser = (char *) requser; + newuser = requser; - ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n", newdb, newuser); + ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n", + newdb, newuser); - if (AH->requirePassword) + if (AH->requirePassword && password == NULL) { password = simple_prompt("Password: ", 100, false); if (password == NULL) @@ -170,12 +177,13 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) if (password) free(password); password = simple_prompt("Password: ", 100, false); + if (password == NULL) + die_horribly(AH, modulename, "out of memory\n"); new_pass = true; } } while (new_pass); - if (password) - free(password); + AH->savedPassword = password; /* check for version mismatch */ _check_database_version(AH); @@ -190,6 +198,10 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) * Make a database connection with the given parameters. The * connection handle is returned, the parameters are stored in AHX. * An interactive password prompt is automatically issued if required. + * + * Note: it's not really all that sensible to use a single-entry password + * cache if the username keeps changing. In current usage, however, the + * username never does change, so one savedPassword is sufficient. */ PGconn * ConnectDatabase(Archive *AHX, @@ -200,21 +212,19 @@ ConnectDatabase(Archive *AHX, int reqPwd) { ArchiveHandle *AH = (ArchiveHandle *) AHX; - char *password = NULL; + char *password = AH->savedPassword; bool new_pass; if (AH->connection) die_horribly(AH, modulename, "already connected to a database\n"); - if (reqPwd) + if (reqPwd && password == NULL) { password = simple_prompt("Password: ", 100, false); if (password == NULL) die_horribly(AH, modulename, "out of memory\n"); - AH->requirePassword = true; } - else - AH->requirePassword = false; + AH->requirePassword = reqPwd; /* * Start the connection. Loop until we have a password if requested by @@ -236,12 +246,13 @@ ConnectDatabase(Archive *AHX, { PQfinish(AH->connection); password = simple_prompt("Password: ", 100, false); + if (password == NULL) + die_horribly(AH, modulename, "out of memory\n"); new_pass = true; } } while (new_pass); - if (password) - free(password); + AH->savedPassword = password; /* check to see that the backend connection was successfully made */ if (PQstatus(AH->connection) == CONNECTION_BAD) |