From 93211b9170d463a9224fb45916847f0baa53f99d Mon Sep 17 00:00:00 2001 From: "Marc G. Fournier" Date: Sat, 31 Jan 1998 20:14:15 +0000 Subject: [PATCH] From: Phil Thompson I haven't had final confirmation from Peter yet, but the attached patch needs to be applied for the Beta otherwise password and crypt authentication just won't work. It puts back the loop in libpq and also fixes a couple of problems with maintaining compatability with pre-6.3 drivers. --- src/backend/libpq/auth.c | 75 ++++++++++++++++++----------- src/backend/libpq/crypt.c | 2 +- src/backend/libpq/pqpacket.c | 6 ++- src/backend/postmaster/postmaster.c | 16 +++--- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index b0891e4c5f..be29eff5a8 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.24 1998/01/29 03:23:05 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.25 1998/01/31 20:12:06 scrappy Exp $ * *------------------------------------------------------------------------- */ @@ -47,6 +47,7 @@ static void handle_krb5_auth(Port *port); static void handle_password_auth(Port *port); static void readPasswordPacket(char *arg, PacketLen len, char *pkt); static void pg_passwordv0_recvauth(char *arg, PacketLen len, char *pkt); +static int checkPassword(Port *port, char *user, char *password); static int old_be_recvauth(Port *port); static int map_old_to_new(Port *port, UserAuth old, int status); @@ -346,19 +347,19 @@ static void pg_passwordv0_recvauth(char *arg, PacketLen len, char *pkt) cp = start = pp->data; - while (len > 0) - if (*cp++ == '\0') - { - if (user == NULL) - user = start; - else + while (len-- > 0) + if (*cp++ == '\0') { - password = start; - break; - } + if (user == NULL) + user = start; + else + { + password = start; + break; + } - start = cp; - } + start = cp; + } if (user == NULL || password == NULL) { @@ -369,9 +370,25 @@ static void pg_passwordv0_recvauth(char *arg, PacketLen len, char *pkt) auth_failed(port); } - else if (map_old_to_new(port, uaPassword, - verify_password(port->auth_arg, user, password)) != STATUS_OK) - auth_failed(port); + else + { + int status; + UserAuth saved; + + /* Check the password. */ + + saved = port->auth_method; + port->auth_method = uaPassword; + + status = checkPassword(port, user, password); + + port->auth_method = saved; + + /* Adjust the result if necessary. */ + + if (map_old_to_new(port, uaPassword, status) != STATUS_OK) + auth_failed(port); + } } @@ -579,24 +596,28 @@ static void readPasswordPacket(char *arg, PacketLen len, char *pkt) StrNCpy(password, ((PasswordPacket *)pkt)->passwd, len); - /* - * Use the local flat password file if clear passwords are used and the - * file is specified. Otherwise use the password in the pg_user table, - * encrypted or not. - */ - - if (port->auth_method == uaPassword && port->auth_arg[0] != '\0') - { - if (verify_password(port->auth_arg, port->user, password) != STATUS_OK) - auth_failed(port); - } - else if (crypt_verify(port, port->user, password) != STATUS_OK) + if (checkPassword(port, port->user, password) != STATUS_OK) auth_failed(port); else sendAuthRequest(port, AUTH_REQ_OK, handle_done_auth); } +/* + * Use the local flat password file if clear passwords are used and the file is + * specified. Otherwise use the password in the pg_user table, encrypted or + * not. + */ + +static int checkPassword(Port *port, char *user, char *password) +{ + if (port->auth_method == uaPassword && port->auth_arg[0] != '\0') + return verify_password(port->auth_arg, user, password); + + return crypt_verify(port, user, password); +} + + /* * Server demux routine for incoming authentication information for protocol * version 0. diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c index 7a72275bc4..b87b05cd42 100644 --- a/src/backend/libpq/crypt.c +++ b/src/backend/libpq/crypt.c @@ -271,7 +271,7 @@ int crypt_verify(Port* port, const char* user, const char* pgpass) { if (!strcmp(pgpass, crypt_pwd)) { /* check here to be sure we are not past valuntil */ - if (!valuntil) + if (!valuntil || strcmp(valuntil, "\\N") == 0) vuntil = INVALID_ABSTIME; else vuntil = nabstimein(valuntil); diff --git a/src/backend/libpq/pqpacket.c b/src/backend/libpq/pqpacket.c index 0a00a97ec6..66b7ea4393 100644 --- a/src/backend/libpq/pqpacket.c +++ b/src/backend/libpq/pqpacket.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.13 1998/01/26 01:41:12 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.14 1998/01/31 20:12:09 scrappy Exp $ * *------------------------------------------------------------------------- */ @@ -40,6 +40,10 @@ void PacketReceiveSetup(Packet *pkt, void (*iodone)(), char *arg) pkt->iodone = iodone; pkt->arg = arg; pkt->state = ReadingPacketLength; + + /* Clear the destination. */ + + MemSet(&pkt->pkt, 0, sizeof (pkt->pkt)); } diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 8b25be20df..5f966532c4 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.72 1998/01/27 15:34:43 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.73 1998/01/31 20:14:15 scrappy Exp $ * * NOTES * @@ -719,15 +719,11 @@ static void readStartupPacket(char *arg, PacketLen len, char *pkt) port = (Port *)arg; si = (StartupPacket *)pkt; - /* At the moment the startup packet must be a fixed length. */ - - if (len != sizeof (StartupPacket)) - { - PacketSendError(&port->pktInfo, "Invalid startup packet."); - return; - } - - /* Get the parameters from the startup packet as C strings. */ + /* + * Get the parameters from the startup packet as C strings. The packet + * destination was cleared first so a short packet has zeros silently + * added and a long packet is silently truncated. + */ StrNCpy(port->database, si->database, sizeof (port->database) - 1); StrNCpy(port->user, si->user, sizeof (port->user) - 1); -- 2.40.0