summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc G. Fournier1998-01-29 02:26:47 +0000
committerMarc G. Fournier1998-01-29 02:26:47 +0000
commit2780576e36bfe0a2263123b520f248ee4fcaa4fd (patch)
treefd91324dec8cca3738b486f5e2712767522a1b0c
parent0427469f57a256f99d2766a443b969385e737369 (diff)
From: "Pedro J. Lobo" <[email protected]>
I've patched pg_dump.c and createdb to add support for password authentication, using the '-u' switch as in psql. I have updated also the man pages.
-rw-r--r--src/bin/createdb/createdb.sh7
-rw-r--r--src/bin/pg_dump/pg_dump.c104
-rw-r--r--src/man/createdb.17
-rw-r--r--src/man/pg_dump.17
4 files changed, 118 insertions, 7 deletions
diff --git a/src/bin/createdb/createdb.sh b/src/bin/createdb/createdb.sh
index 8a1e0ab574d..6fee8f28392 100644
--- a/src/bin/createdb/createdb.sh
+++ b/src/bin/createdb/createdb.sh
@@ -11,7 +11,7 @@
#
#
# IDENTIFICATION
-# $Header: /cvsroot/pgsql/src/bin/createdb/Attic/createdb.sh,v 1.7 1997/11/07 06:25:25 thomas Exp $
+# $Header: /cvsroot/pgsql/src/bin/createdb/Attic/createdb.sh,v 1.8 1998/01/29 02:26:21 scrappy Exp $
#
#-------------------------------------------------------------------------
@@ -31,6 +31,8 @@ fi
dbname=$USER
+PASSWDOPT="";
+
while test -n "$1"
do
case $1 in
@@ -39,6 +41,7 @@ do
-a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;;
+ -u) PASSWDOPT=$1;;
-D) dbpath=$2; shift;;
-*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;;
*) dbname=$1;;
@@ -80,7 +83,7 @@ else
location="with location = '$dbpath'"
fi
-psql -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "create database $dbname $location" template1
+psql $PASSWDOPT -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "create database $dbname $location" template1
if [ $? -ne 0 ]; then
echo "$CMDNAME: database creation failed on $dbname."
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 1e465b5296c..8dd8ef28cb7 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -21,7 +21,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.60 1998/01/16 23:20:39 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.61 1998/01/29 02:26:25 scrappy Exp $
*
* Modifications - 6/10/96 - [email protected] - version 1.13.dhb
*
@@ -44,6 +44,10 @@
* - Added functions to free allocated memory used for retrieving
* indices,tables,inheritance,types,functions and aggregates.
* No more leaks reported by Purify.
+ *
+ *
+ * Modifications - 1/26/98 - [email protected]
+ * - Added support for password authentication
*-------------------------------------------------------------------------
*/
@@ -67,6 +71,10 @@
#include "strdup.h"
#endif
+#ifdef HAVE_TERMIOS_H
+#include <termios.h>
+#endif
+
#include "pg_dump.h"
static void dumpSequence(FILE *fout, TableInfo tbinfo);
@@ -135,6 +143,8 @@ usage(const char *progname)
fprintf(stderr,
"\t -z \t\t dump ACLs (grant/revoke)\n");
fprintf(stderr,
+ "\t -u \t\t use password authentication\n");
+ fprintf(stderr,
"\nIf dbname is not supplied, then the DATABASE environment "
"variable value is used.\n");
fprintf(stderr, "\n");
@@ -455,6 +465,62 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout,
}
+static void
+prompt_for_password(char *username, char *password)
+{
+ int length;
+
+#ifdef HAVE_TERMIOS_H
+ struct termios t_orig,
+ t;
+
+#endif
+
+ printf("Username: ");
+ fgets(username, 9, stdin);
+ length = strlen(username);
+ /* skip rest of the line */
+ if (length > 0 && username[length - 1] != '\n')
+ {
+ static char buf[512];
+
+ do
+ {
+ fgets(buf, 512, stdin);
+ } while (buf[strlen(buf) - 1] != '\n');
+ }
+ if (length > 0 && username[length - 1] == '\n')
+ username[length - 1] = '\0';
+
+ printf("Password: ");
+#ifdef HAVE_TERMIOS_H
+ tcgetattr(0, &t);
+ t_orig = t;
+ t.c_lflag &= ~ECHO;
+ tcsetattr(0, TCSADRAIN, &t);
+#endif
+ fgets(password, 9, stdin);
+#ifdef HAVE_TERMIOS_H
+ tcsetattr(0, TCSADRAIN, &t_orig);
+#endif
+
+ length = strlen(password);
+ /* skip rest of the line */
+ if (length > 0 && password[length - 1] != '\n')
+ {
+ static char buf[512];
+
+ do
+ {
+ fgets(buf, 512, stdin);
+ } while (buf[strlen(buf) - 1] != '\n');
+ }
+ if (length > 0 && password[length - 1] == '\n')
+ password[length - 1] = '\0';
+
+ printf("\n\n");
+}
+
int
main(int argc, char **argv)
@@ -470,6 +536,11 @@ main(int argc, char **argv)
acls = 0;
TableInfo *tblinfo;
int numTables;
+ char connect_string[512] = "";
+ char tmp_string[128];
+ char username[64];
+ char password[64];
+ int use_password = 0;
g_verbose = false;
@@ -481,7 +552,7 @@ main(int argc, char **argv)
progname = *argv;
- while ((c = getopt(argc, argv, "adDf:h:op:st:vz")) != EOF)
+ while ((c = getopt(argc, argv, "adDf:h:op:st:vzu")) != EOF)
{
switch (c)
{
@@ -520,6 +591,9 @@ main(int argc, char **argv)
case 'z': /* Dump oids */
acls = 1;
break;
+ case 'u':
+ use_password = 1;
+ break;
default:
usage(progname);
break;
@@ -551,7 +625,31 @@ main(int argc, char **argv)
exit(2);
}
- g_conn = PQsetdb(pghost, pgport, NULL, NULL, dbname);
+ /*g_conn = PQsetdb(pghost, pgport, NULL, NULL, dbname);*/
+ if (pghost != NULL) {
+ sprintf(tmp_string, "host=%s ", pghost);
+ strcat(connect_string, tmp_string);
+ }
+ if (pgport != NULL) {
+ sprintf(tmp_string, "port=%s ", pgport);
+ strcat(connect_string, tmp_string);
+ }
+ if (dbname != NULL) {
+ sprintf(tmp_string, "dbname=%s ", dbname);
+ strcat(connect_string, tmp_string);
+ }
+ if (use_password) {
+ prompt_for_password(username, password);
+ strcat(connect_string, "authtype=password ");
+ sprintf(tmp_string, "user=%s ", username);
+ strcat(connect_string, tmp_string);
+ sprintf(tmp_string, "password=%s ", password);
+ strcat(connect_string, tmp_string);
+ bzero(tmp_string, sizeof(tmp_string));
+ bzero(password, sizeof(password));
+ }
+ g_conn = PQconnectdb(connect_string);
+ bzero(connect_string, sizeof(connect_string));
/* check to see that the backend connection was successfully made */
if (PQstatus(g_conn) == CONNECTION_BAD)
{
diff --git a/src/man/createdb.1 b/src/man/createdb.1
index eece6870045..4650839a599 100644
--- a/src/man/createdb.1
+++ b/src/man/createdb.1
@@ -1,6 +1,6 @@
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
-.\" $Header: /cvsroot/pgsql/src/man/Attic/createdb.1,v 1.7 1998/01/26 01:42:42 scrappy Exp $
+.\" $Header: /cvsroot/pgsql/src/man/Attic/createdb.1,v 1.8 1998/01/29 02:26:33 scrappy Exp $
.TH CREATEDB UNIX 11/05/95 PostgreSQL PostgreSQL
.SH NAME
createdb - create a database
@@ -16,6 +16,8 @@ host]
.BR -p
port]
[\c
+.BR "-u"]
+[\c
.BR -D
location]
[dbname]
@@ -77,6 +79,9 @@ extension on which the
is listening for connections. Defaults to 5432, or the value of the
.SM PGPORT
environment variable (if set).
+.TP
+.BR "-u"
+Use password authentication. Prompts for username and password.
.SH EXAMPLES
.nf
# create the demo database using the postmaster on the local host, port 5432.
diff --git a/src/man/pg_dump.1 b/src/man/pg_dump.1
index ad1fdd23858..33f66f05480 100644
--- a/src/man/pg_dump.1
+++ b/src/man/pg_dump.1
@@ -1,6 +1,6 @@
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
-.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.9 1998/01/11 22:17:46 momjian Exp $
+.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.10 1998/01/29 02:26:47 scrappy Exp $
.TH PG_DUMP UNIX 1/20/96 PostgreSQL PostgreSQL
.SH NAME
pg_dump - dumps out a Postgres database into a script file
@@ -38,6 +38,8 @@ table]
[\c
.BR "-v"
]
+[\c
+.BR "-u"]
dbname
.in -5n
.SH DESCRIPTION
@@ -89,6 +91,9 @@ Dump out only the schema, no data
.BR "-t" " table"
Dump for this table only
.TP
+.BR "-u"
+Use password authentication. Prompts for username and password.
+.TP
.BR "-v" ""
Specifies verbose mode
.PP