Commit 2780576e authored by Marc G. Fournier's avatar Marc G. Fournier

From: "Pedro J. Lobo" <pjlobo@euitt.upm.es>

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.
parent 0427469f
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# #
# IDENTIFICATION # 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 ...@@ -31,6 +31,8 @@ fi
dbname=$USER dbname=$USER
PASSWDOPT="";
while test -n "$1" while test -n "$1"
do do
case $1 in case $1 in
...@@ -39,6 +41,7 @@ do ...@@ -39,6 +41,7 @@ do
-a) AUTHSYS=$2; shift;; -a) AUTHSYS=$2; shift;;
-h) PGHOST=$2; shift;; -h) PGHOST=$2; shift;;
-p) PGPORT=$2; shift;; -p) PGPORT=$2; shift;;
-u) PASSWDOPT=$1;;
-D) dbpath=$2; shift;; -D) dbpath=$2; shift;;
-*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;; -*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;;
*) dbname=$1;; *) dbname=$1;;
...@@ -80,7 +83,7 @@ else ...@@ -80,7 +83,7 @@ else
location="with location = '$dbpath'" location="with location = '$dbpath'"
fi 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 if [ $? -ne 0 ]; then
echo "$CMDNAME: database creation failed on $dbname." echo "$CMDNAME: database creation failed on $dbname."
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* *
* *
* IDENTIFICATION * 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 - dave@bensoft.com - version 1.13.dhb * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -44,6 +44,10 @@ ...@@ -44,6 +44,10 @@
* - Added functions to free allocated memory used for retrieving * - Added functions to free allocated memory used for retrieving
* indices,tables,inheritance,types,functions and aggregates. * indices,tables,inheritance,types,functions and aggregates.
* No more leaks reported by Purify. * No more leaks reported by Purify.
*
*
* Modifications - 1/26/98 - pjlobo@euitt.upm.es
* - Added support for password authentication
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -67,6 +71,10 @@ ...@@ -67,6 +71,10 @@
#include "strdup.h" #include "strdup.h"
#endif #endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#include "pg_dump.h" #include "pg_dump.h"
static void dumpSequence(FILE *fout, TableInfo tbinfo); static void dumpSequence(FILE *fout, TableInfo tbinfo);
...@@ -134,6 +142,8 @@ usage(const char *progname) ...@@ -134,6 +142,8 @@ usage(const char *progname)
"\t -v \t\t verbose\n"); "\t -v \t\t verbose\n");
fprintf(stderr, fprintf(stderr,
"\t -z \t\t dump ACLs (grant/revoke)\n"); "\t -z \t\t dump ACLs (grant/revoke)\n");
fprintf(stderr,
"\t -u \t\t use password authentication\n");
fprintf(stderr, fprintf(stderr,
"\nIf dbname is not supplied, then the DATABASE environment " "\nIf dbname is not supplied, then the DATABASE environment "
"variable value is used.\n"); "variable value is used.\n");
...@@ -455,6 +465,62 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout, ...@@ -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 int
main(int argc, char **argv) main(int argc, char **argv)
...@@ -470,6 +536,11 @@ main(int argc, char **argv) ...@@ -470,6 +536,11 @@ main(int argc, char **argv)
acls = 0; acls = 0;
TableInfo *tblinfo; TableInfo *tblinfo;
int numTables; int numTables;
char connect_string[512] = "";
char tmp_string[128];
char username[64];
char password[64];
int use_password = 0;
g_verbose = false; g_verbose = false;
...@@ -481,7 +552,7 @@ main(int argc, char **argv) ...@@ -481,7 +552,7 @@ main(int argc, char **argv)
progname = *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) switch (c)
{ {
...@@ -520,6 +591,9 @@ main(int argc, char **argv) ...@@ -520,6 +591,9 @@ main(int argc, char **argv)
case 'z': /* Dump oids */ case 'z': /* Dump oids */
acls = 1; acls = 1;
break; break;
case 'u':
use_password = 1;
break;
default: default:
usage(progname); usage(progname);
break; break;
...@@ -551,7 +625,31 @@ main(int argc, char **argv) ...@@ -551,7 +625,31 @@ main(int argc, char **argv)
exit(2); 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 */ /* check to see that the backend connection was successfully made */
if (PQstatus(g_conn) == CONNECTION_BAD) if (PQstatus(g_conn) == CONNECTION_BAD)
{ {
......
.\" This is -*-nroff-*- .\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here.... .\" 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 .TH CREATEDB UNIX 11/05/95 PostgreSQL PostgreSQL
.SH NAME .SH NAME
createdb - create a database createdb - create a database
...@@ -16,6 +16,8 @@ host] ...@@ -16,6 +16,8 @@ host]
.BR -p .BR -p
port] port]
[\c [\c
.BR "-u"]
[\c
.BR -D .BR -D
location] location]
[dbname] [dbname]
...@@ -77,6 +79,9 @@ extension on which the ...@@ -77,6 +79,9 @@ extension on which the
is listening for connections. Defaults to 5432, or the value of the is listening for connections. Defaults to 5432, or the value of the
.SM PGPORT .SM PGPORT
environment variable (if set). environment variable (if set).
.TP
.BR "-u"
Use password authentication. Prompts for username and password.
.SH EXAMPLES .SH EXAMPLES
.nf .nf
# create the demo database using the postmaster on the local host, port 5432. # create the demo database using the postmaster on the local host, port 5432.
......
.\" This is -*-nroff-*- .\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here.... .\" 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 .TH PG_DUMP UNIX 1/20/96 PostgreSQL PostgreSQL
.SH NAME .SH NAME
pg_dump - dumps out a Postgres database into a script file pg_dump - dumps out a Postgres database into a script file
...@@ -38,6 +38,8 @@ table] ...@@ -38,6 +38,8 @@ table]
[\c [\c
.BR "-v" .BR "-v"
] ]
[\c
.BR "-u"]
dbname dbname
.in -5n .in -5n
.SH DESCRIPTION .SH DESCRIPTION
...@@ -89,6 +91,9 @@ Dump out only the schema, no data ...@@ -89,6 +91,9 @@ Dump out only the schema, no data
.BR "-t" " table" .BR "-t" " table"
Dump for this table only Dump for this table only
.TP .TP
.BR "-u"
Use password authentication. Prompts for username and password.
.TP
.BR "-v" "" .BR "-v" ""
Specifies verbose mode Specifies verbose mode
.PP .PP
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment