From f8fda03d12971361781546e3f74d1a703d1841c3 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Mon, 25 Aug 1997 19:41:52 +0000 Subject: [PATCH] pg_password utility. Cleanup for psql passwords. New datetime contrib stuff for new version. Fix for strutils needing config.h. --- README | 69 ++++++++++++ contrib/datetime/datetime_functions.c | 143 ++++++++---------------- contrib/datetime/datetime_functions.doc | 37 +++--- contrib/datetime/datetime_functions.sql | 54 +++++---- src/backend/libpq/password.c | 3 +- src/bin/Makefile | 3 +- src/bin/psql/psql.c | 20 +++- src/bin/psql/stringutils.c | 3 +- 8 files changed, 183 insertions(+), 149 deletions(-) diff --git a/README b/README index 189cc994ae..b411a74b00 100644 --- a/README +++ b/README @@ -1,3 +1,72 @@ +The pathces and a prototype tool to manipulate the ``flat password file +'' functionality of PostgreSQL6.1 +1. File +Makefile +pg_passwd.c the source file of the tool. +2. How to specify pasword files and their format. +Specify the password file in the same style of Ident authentication in +$PGDATA/pg_hba.conf +host unv 133.65.96.250 255.255.255.255 password passwd +The above line allows access from 133.65.96.250 using the passwords listed +in $PGDATA/passwd. +The format of the password files follows those of /etc/passwd and +/etc/shadow: the first field is the user name, and the second field +is the encrypted password. The rest is completely ignored. Thus +the following three sample lines specify the same user and password pair:. +pg_guest:/nB7.w5Auq.BY:10031:::::: +pg_guest:/nB7.w5Auq.BY:93001:930::/home/guest:/bin/tcsh +pg_guest:/nB7.w5Auq.BY:93001 +Note that the original src/backend/libpq/password.c has a bug, which +disallows the first and the second format. If you want to use these +formats, please make sure you've applied the patch accompanied with +this tool. +3. Usage of pg_passwd +Supply the password file to the pg_passwd command. In the case described +above, after ``cd'ing to $PGDATA, the following command execution specify +the new password for pg_guest: +% pg_passwd passwd +Username: pg_guest +Password: +Re-enter password: +where the Password: and Re-enter password: prompts require the same +password input which are not displayed on the terminal. +The original password file is renamed to ``passwd.bk''. +4. How to specify pasasword authentication +You can use the password authentication fro psq, perl, or pg{tcl,tk}sh. +4.1 psql +Use the -u option. Note that the original distribution includes a bug. +Please make sure you've applied the patch distributed with this tool. +The following lines show the sample usage of the option: +% psql -h hyalos -u unv +Username: pg_guest +Password: +Welcome to the POSTGRESQL interactive sql monitor: + Please read the file COPYRIGHT for copyright terms of POSTGRESQL + type \? for help on slash commands + type \q to quit + type \g or terminate with semicolon to execute query + You are currently connected to the database: unv +unv=> +4.2 perl5 +Use the new style of the Pg.pm like this +$conn = Pg::connectdb("host=hyalos authtype=password dbname=unv + user=pg_guest password=xxxxxxx"); +For more details, the users refer to to ``src/pgsql_perl5/Pg.pm''. +4.3 pg{tcl,tk}sh +Use the pg_connect comamnd with -conninfo option thus +% set conn [pg_connect -conninfo \ + "host=hyalos authtype=password dbname=unv \ + user=pg_guest password=xxxxxxx "] +Use can list all of the keys for the option by executing the following +command: +% puts [ pg_conndefaults] +5. Acknowledgment +Mr. Ishii, SRA, pointed out the original bugs in the tool. He also +supplied the Makefile for this distribution. +------------------------------------------------------------------------- +July 2, 1997 +Yoshihiko Ichikawa, Dept of Info Sci, Fac of Sci, Ochanomizu University +E-mail: ichikawa@is.ocha.ac.jp PostgreSQL Data Base Management System (formerly known as Postgres, then as Postgres95). diff --git a/contrib/datetime/datetime_functions.c b/contrib/datetime/datetime_functions.c index dc1fec8bd2..e684ce5826 100644 --- a/contrib/datetime/datetime_functions.c +++ b/contrib/datetime/datetime_functions.c @@ -9,139 +9,90 @@ #include #include "postgres.h" -#include "pg_type.h" #include "utils/palloc.h" +#include "utils/datetime.h" -typedef struct DateADT { - char day; - char month; - short year; -} DateADT; -typedef struct TimeADT { - short hr; - short min; - float sec; -} TimeADT; - -TimeADT * -time_difference(TimeADT *time1, TimeADT *time2) +TimeADT *time_difference(TimeADT * time1, TimeADT * time2) { - TimeADT *time = (TimeADT*)palloc(sizeof(TimeADT)); - - time->sec = time1->sec - time2->sec; - time->min = time1->min - time2->min; - time->hr = time1->hr - time2->hr; - - if (time->sec < 0) { - time->sec += 60.0; - time->min--; - } else if (time->sec >= 60.0) { - time->sec -= 60.0; - time->min++; - } - - if (time->min < 0) { - time->min += 60; - time->hr--; - } else if (time->min >= 60) { - time->min -= 60; - time->hr++; - } - - if (time->hr < 0) { - time->hr += 24; - } else if (time->hr >= 24) { - time->hr -= 24; - } - - return (time); + TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT)); + *result = *time1 - *time2; + return (result); } -TimeADT * -currentTime() +TimeADT *currenttime() { time_t current_time; struct tm *tm; - TimeADT *result = (TimeADT*)palloc(sizeof(TimeADT)); + TimeADT *result = (TimeADT *) palloc(sizeof(TimeADT)); current_time = time(NULL); tm = localtime(¤t_time); - result->sec = tm->tm_sec; - result->min = tm->tm_min; - result->hr = tm->tm_hour; - + *result = ((((tm->tm_hour*60)+tm->tm_min)*60)+tm->tm_sec); return (result); } - -int4 -currentDate() +DateADT currentdate() { time_t current_time; struct tm *tm; - int4 result; - DateADT *date = (DateADT*)&result; - + DateADT result; current_time = time(NULL); tm = localtime(¤t_time); - date->day = tm->tm_mday; - date->month = tm->tm_mon+1; - date->year = tm->tm_year+1900; + result = date2j(tm->tm_year,tm->tm_mon + 1,tm->tm_mday) - + date2j(100,1,1); return (result); } - -int4 -hours(TimeADT *time) +int4 hours(TimeADT * time) { - return (time->hr); + return(*time / (60*60)); } -int4 -minutes(TimeADT *time) +int4 minutes(TimeADT * time) { - return (time->min); + return(((int) (*time / 60)) % 60); } -int4 -seconds(TimeADT *time) +int4 seconds(TimeADT * time) { - int seconds = (int)time->sec; - return (seconds); + return(((int) *time) % 60); } - -int4 -day(int4 val) +int4 day(DateADT *date) { - DateADT *date = (DateADT*)&val; - return (date->day); -} + struct tm tm; -int4 -month(int4 val) -{ - DateADT *date = (DateADT*)&val; - return (date->month); -} + j2date( (*date + date2j(2000,1,1)), + &tm.tm_year, &tm.tm_mon, &tm.tm_mday); -int4 -year(int4 val) -{ - DateADT *date = (DateADT*)&val; - return (date->year); + return (tm.tm_mday); } - -int4 -asMinutes(TimeADT *time) +int4 month(DateADT *date) { - int seconds = (int)time->sec; - return (time->min + 60*time->hr); + struct tm tm; + + j2date( (*date + date2j(2000,1,1)), + &tm.tm_year, &tm.tm_mon, &tm.tm_mday); + + return (tm.tm_mon); } +int4 year(DateADT *date) +{ + struct tm tm; + + j2date( (*date + date2j(2000,1,1)), + &tm.tm_year, &tm.tm_mon, &tm.tm_mday); -int4 -asSeconds(TimeADT *time) + return (tm.tm_year); +} +int4 asminutes(TimeADT * time) { - int seconds = (int)time->sec; - return (seconds + 60*time->min + 3600*time->hr); + int seconds = (int) *time; + + return (seconds / 60); } +int4 asseconds(TimeADT * time) +{ + int seconds = (int) *time; + return (seconds); +} diff --git a/contrib/datetime/datetime_functions.doc b/contrib/datetime/datetime_functions.doc index 0c7a01819a..1b147e7ac1 100644 --- a/contrib/datetime/datetime_functions.doc +++ b/contrib/datetime/datetime_functions.doc @@ -1,25 +1,16 @@ -From: Massimo Dal Zotto -Date: Tue, 14 May 1996 14:31:18 +0200 (MET DST) -Subject: [PG95]: new postgres functions - -- -----BEGIN PGP SIGNED MESSAGE----- - -Some time ago I read in the mailing list requests of people looking -for more time and date functions. I have now written some of them: - - time_difference(time1, time2) ,also defined as operator '-' - hour(time) - minutes(time) - seconds(time) - asMinutes(time) - asSeconds(time) - currentTime() - currentDate() - -The file can be compiled as shared library and loaded as dynamic module -without need to recompile the backend. This can also be taken as an example -of the extensibility of postgres (user-defined functions, operators, etc). -I would be nice to see more of these user contributed modules posted to this -list and hopefully accessible from the Postgres home page. +Date & time functions for use in Postgres version 6.1 & up + +This functions replaces the original ones from the postgresql.org +because the date & time structures has changed. + +There is a Makefile that should be "ajusted" +for directories where postgres home after install. + +In this case: /usr/postgres. + +Hope this can help, + + +Sergio Lenzi (lenzi@bsi.com.br) diff --git a/contrib/datetime/datetime_functions.sql b/contrib/datetime/datetime_functions.sql index 2e50a4a19f..955377909a 100644 --- a/contrib/datetime/datetime_functions.sql +++ b/contrib/datetime/datetime_functions.sql @@ -1,69 +1,75 @@ - --- SQL code to load and define 'datetime' functions - --- load the new functions - -load '/home/dz/lib/postgres/datetime_functions.so'; - --- define the new functions in postgres +func=$1 +cat <<% > datetime_functions.sql +drop function time_difference(time,time); +drop function currentdate(); +drop function currenttime(); +drop function hours(time); +drop function minutes(time); +drop function seconds(time); +drop function day(date); +drop function month(date); +drop function year(date); +drop function asminutes(time); +drop function asseconds(time); +drop operator - (time,time); create function time_difference(time,time) returns time - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; -create function currentDate() +create function currentdate() returns date - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; -create function currentTime() +create function currenttime() returns time - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function hours(time) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function minutes(time) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function seconds(time) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function day(date) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function month(date) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create function year(date) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; -create function asMinutes(time) +create function asminutes(time) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; -create function asSeconds(time) +create function asseconds(time) returns int4 - as '/home/dz/lib/postgres/datetime_functions.so' + as '$func' language 'c'; create operator - ( leftarg=time, rightarg=time, procedure=time_difference); - +% diff --git a/src/backend/libpq/password.c b/src/backend/libpq/password.c index afbd95ad1a..346d59e8bf 100644 --- a/src/backend/libpq/password.c +++ b/src/backend/libpq/password.c @@ -79,7 +79,8 @@ verify_password(char *user, char *password, Port *port, } /* kill the newline */ - test_pw[strlen(test_pw)-1] = '\0'; + if (test_pw[strlen(test_pw)-1] == '\n') + test_pw[strlen(test_pw)-1] = '\0'; strNcpy(salt, test_pw, 2); diff --git a/src/bin/Makefile b/src/bin/Makefile index a37cadee56..ecf926c6fc 100644 --- a/src/bin/Makefile +++ b/src/bin/Makefile @@ -7,7 +7,7 @@ # # # IDENTIFICATION -# $Header: /cvsroot/pgsql/src/bin/Makefile,v 1.7 1997/04/26 05:04:21 scrappy Exp $ +# $Header: /cvsroot/pgsql/src/bin/Makefile,v 1.8 1997/08/25 19:41:39 momjian Exp $ # #------------------------------------------------------------------------- @@ -21,6 +21,7 @@ $(MAKE) -C pg_version $@ $(MAKE) -C psql $@ $(MAKE) -C pg_dump $@ + $(MAKE) -C pg_passwd $@ # # Shell scripts # diff --git a/src/bin/psql/psql.c b/src/bin/psql/psql.c index 5f8dddb8be..62568a3381 100644 --- a/src/bin/psql/psql.c +++ b/src/bin/psql/psql.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.86 1997/08/22 04:13:18 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.87 1997/08/25 19:41:48 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -1239,8 +1239,8 @@ HandleSlashCmds(PsqlSettings * settings, } else if (!optarg) { /* show tables, sequences and indices */ tableList(settings, 0, 'b'); } else if (strcmp(optarg, "*") == 0) { /* show everything */ - tableList(settings, 0, 'b'); - tableList(settings, 1, 'b'); + if (tableList(settings, 0, 'b') == 0) + tableList(settings, 1, 'b'); } else { /* describe the specified table */ tableDesc(settings, optarg); } @@ -1945,6 +1945,13 @@ static void prompt_for_password(char *username, char *password) 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: "); @@ -1960,6 +1967,13 @@ static void prompt_for_password(char *username, char *password) #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"); diff --git a/src/bin/psql/stringutils.c b/src/bin/psql/stringutils.c index 6237aa5e30..cae0e7405b 100644 --- a/src/bin/psql/stringutils.c +++ b/src/bin/psql/stringutils.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.9 1997/08/19 21:36:56 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.10 1997/08/25 19:41:52 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -16,6 +16,7 @@ #include #include +#include "postgres.h" #ifndef HAVE_STRDUP #include "strdup.h" #endif