88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.221 2003/01/08 21:33:27 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.222 2003/01/30 19:49:54 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -123,7 +123,7 @@ static const PQconninfoOption PQconninfoOptions[] = {
123123"Database-Password" ,"*" ,20 },
124124
125125{"connect_timeout" ,"PGCONNECT_TIMEOUT" ,NULL ,NULL ,
126- "Connect-timeout" ,"" ,10 },/* strlen( INT32_MAX) == 10 */
126+ "Connect-timeout" ,"" ,10 },/* strlen(INT32_MAX) == 10 */
127127
128128{"dbname" ,"PGDATABASE" ,NULL ,NULL ,
129129"Database-Name" ,"" ,20 },
@@ -315,8 +315,14 @@ PQconnectStart(const char *conninfo)
315315tmp = conninfo_getval (connOptions ,"password" );
316316conn -> pgpass = tmp ?strdup (tmp ) :NULL ;
317317if (conn -> pgpass == NULL || conn -> pgpass [0 ]== '\0' )
318+ {
319+ if (conn -> pgpass )
320+ free (conn -> pgpass );
318321conn -> pgpass = PasswordFromFile (conn -> pghost ,conn -> pgport ,
319- conn -> dbName ,conn -> pguser );
322+ conn -> dbName ,conn -> pguser );
323+ if (conn -> pgpass == NULL )
324+ conn -> pgpass = strdup (DefaultPassword );
325+ }
320326tmp = conninfo_getval (connOptions ,"connect_timeout" );
321327conn -> connect_timeout = tmp ?strdup (tmp ) :NULL ;
322328#ifdef USE_SSL
@@ -506,14 +512,13 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
506512else
507513conn -> dbName = strdup (dbName );
508514
509- /*
510- * getPasswordFromFile mallocs its result, so we don't need strdup
511- * here
512- */
513515if (pwd )
514516conn -> pgpass = strdup (pwd );
515517else if ((tmp = getenv ("PGPASSWORD" ))!= NULL )
516518conn -> pgpass = strdup (tmp );
519+ else if ((tmp = PasswordFromFile (conn -> pghost ,conn -> pgport ,
520+ conn -> dbName ,conn -> pguser ))!= NULL )
521+ conn -> pgpass = tmp ;
517522else
518523conn -> pgpass = strdup (DefaultPassword );
519524
@@ -2946,7 +2951,7 @@ pwdfMatchesString(char *buf, char *token)
29462951return NULL ;
29472952}
29482953
2949- /*get a password from the password file. */
2954+ /*Get a password from the password file. Return value is malloc'd . */
29502955char *
29512956PasswordFromFile (char * hostname ,char * port ,char * dbname ,char * username )
29522957{
@@ -2972,17 +2977,15 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
29722977
29732978/* Look for it in the home dir */
29742979home = getenv ("HOME" );
2975- if (home )
2980+ if (!home )
2981+ return NULL ;
2982+
2983+ pgpassfile = malloc (strlen (home )+ 1 + strlen (PGPASSFILE )+ 1 );
2984+ if (!pgpassfile )
29762985{
2977- pgpassfile = malloc (strlen (home )+ 1 + strlen (PGPASSFILE )+ 1 );
2978- if (!pgpassfile )
2979- {
2980- fprintf (stderr ,libpq_gettext ("out of memory\n" ));
2981- return NULL ;
2982- }
2983- }
2984- else
2986+ fprintf (stderr ,libpq_gettext ("out of memory\n" ));
29852987return NULL ;
2988+ }
29862989
29872990sprintf (pgpassfile ,"%s/%s" ,home ,PGPASSFILE );
29882991
@@ -3014,12 +3017,18 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
30143017{
30153018char * t = buf ,
30163019* ret ;
3020+ int len ;
30173021
30183022fgets (buf ,LINELEN - 1 ,fp );
3019- if (strlen (buf )== 0 )
3023+
3024+ len = strlen (buf );
3025+ if (len == 0 )
30203026continue ;
30213027
3022- buf [strlen (buf )- 1 ]= 0 ;
3028+ /* Remove trailing newline */
3029+ if (buf [len - 1 ]== '\n' )
3030+ buf [len - 1 ]= 0 ;
3031+
30233032if ((t = pwdfMatchesString (t ,hostname ))== NULL ||
30243033(t = pwdfMatchesString (t ,port ))== NULL ||
30253034(t = pwdfMatchesString (t ,dbname ))== NULL ||