99 * Dec 17, 1997 - Todd A. Brandys
1010 *Orignal Version Completed.
1111 *
12- * $Id: crypt.c,v 1.34 2001/08/15 21:08:21 momjian Exp $
12+ * $Id: crypt.c,v 1.35 2001/08/17 02:59:19 momjian Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
1919
2020#include "postgres.h"
2121#include "libpq/crypt.h"
22+ #include "libpq/libpq.h"
2223#include "miscadmin.h"
2324#include "storage/fd.h"
2425#include "utils/nabstime.h"
@@ -276,22 +277,33 @@ md5_crypt_verify(const Port *port, const char *user, const char *pgpass)
276277return STATUS_ERROR ;
277278}
278279
280+ /* If they encrypt their password, force MD5 */
281+ if (isMD5 (passwd )&& port -> auth_method != uaMD5 )
282+ {
283+ snprintf (PQerrormsg ,PQERRORMSG_LENGTH ,
284+ "Password is stored MD5 encrypted. "
285+ "Only pg_hba.conf's MD5 protocol can be used for this user.\n" );
286+ fputs (PQerrormsg ,stderr );
287+ pqdebug ("%s" ,PQerrormsg );
288+ return STATUS_ERROR ;
289+ }
290+
279291/*
280292 * Compare with the encrypted or plain password depending on the
281293 * authentication method being used for this connection.
282294 */
283- switch (port -> auth_method )
284- {
295+ switch (port -> auth_method )
296+ {
285297case uaCrypt :
286- crypt_pwd = crypt (passwd ,port -> salt );
298+ crypt_pwd = crypt (passwd ,port -> cryptSalt );
287299break ;
288300case uaMD5 :
289301crypt_pwd = palloc (MD5_PASSWD_LEN + 1 );
290-
291302if (isMD5 (passwd ))
292303{
293304if (!EncryptMD5 (passwd + strlen ("md5" ),
294- (char * )port -> salt ,crypt_pwd ))
305+ (char * )port -> md5Salt ,
306+ sizeof (port -> md5Salt ),crypt_pwd ))
295307{
296308pfree (crypt_pwd );
297309return STATUS_ERROR ;
@@ -301,14 +313,15 @@ md5_crypt_verify(const Port *port, const char *user, const char *pgpass)
301313{
302314char * crypt_pwd2 = palloc (MD5_PASSWD_LEN + 1 );
303315
304- if (!EncryptMD5 (passwd ,port -> user ,crypt_pwd2 ))
316+ if (!EncryptMD5 (passwd ,port -> user ,strlen (port -> user ),
317+ crypt_pwd2 ))
305318{
306319pfree (crypt_pwd );
307320pfree (crypt_pwd2 );
308321return STATUS_ERROR ;
309322}
310- if (!EncryptMD5 (crypt_pwd2 + strlen ("md5" ),port -> salt ,
311- crypt_pwd ))
323+ if (!EncryptMD5 (crypt_pwd2 + strlen ("md5" ),port -> md5Salt ,
324+ sizeof ( port -> md5Salt ), crypt_pwd ))
312325{
313326pfree (crypt_pwd );
314327pfree (crypt_pwd2 );
@@ -324,7 +337,6 @@ md5_crypt_verify(const Port *port, const char *user, const char *pgpass)
324337
325338if (!strcmp (pgpass ,crypt_pwd ))
326339{
327-
328340/*
329341 * check here to be sure we are not past valuntil
330342 */