8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -123,7 +123,7 @@ static const PQconninfoOption PQconninfoOptions[] = {
123
123
"Database-Password" , "*" , 20 },
124
124
125
125
{"connect_timeout" , "PGCONNECT_TIMEOUT" , NULL , NULL ,
126
- "Connect-timeout" , "" , 10 }, /* strlen( INT32_MAX) == 10 */
126
+ "Connect-timeout" , "" , 10 }, /* strlen(INT32_MAX) == 10 */
127
127
128
128
{"dbname" , "PGDATABASE" , NULL , NULL ,
129
129
"Database-Name" , "" , 20 },
@@ -315,8 +315,14 @@ PQconnectStart(const char *conninfo)
315
315
tmp = conninfo_getval (connOptions , "password" );
316
316
conn -> pgpass = tmp ? strdup (tmp ) : NULL ;
317
317
if (conn -> pgpass == NULL || conn -> pgpass [0 ] == '\0' )
318
+ {
319
+ if (conn -> pgpass )
320
+ free (conn -> pgpass );
318
321
conn -> 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
+ }
320
326
tmp = conninfo_getval (connOptions , "connect_timeout" );
321
327
conn -> connect_timeout = tmp ? strdup (tmp ) : NULL ;
322
328
#ifdef USE_SSL
@@ -506,14 +512,13 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
506
512
else
507
513
conn -> dbName = strdup (dbName );
508
514
509
- /*
510
- * getPasswordFromFile mallocs its result, so we don't need strdup
511
- * here
512
- */
513
515
if (pwd )
514
516
conn -> pgpass = strdup (pwd );
515
517
else if ((tmp = getenv ("PGPASSWORD" )) != NULL )
516
518
conn -> pgpass = strdup (tmp );
519
+ else if ((tmp = PasswordFromFile (conn -> pghost , conn -> pgport ,
520
+ conn -> dbName , conn -> pguser )) != NULL )
521
+ conn -> pgpass = tmp ;
517
522
else
518
523
conn -> pgpass = strdup (DefaultPassword );
519
524
@@ -2946,7 +2951,7 @@ pwdfMatchesString(char *buf, char *token)
2946
2951
return NULL ;
2947
2952
}
2948
2953
2949
- /* get a password from the password file. */
2954
+ /* Get a password from the password file. Return value is malloc'd . */
2950
2955
char *
2951
2956
PasswordFromFile (char * hostname , char * port , char * dbname , char * username )
2952
2957
{
@@ -2972,17 +2977,15 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
2972
2977
2973
2978
/* Look for it in the home dir */
2974
2979
home = getenv ("HOME" );
2975
- if (home )
2980
+ if (!home )
2981
+ return NULL ;
2982
+
2983
+ pgpassfile = malloc (strlen (home ) + 1 + strlen (PGPASSFILE ) + 1 );
2984
+ if (!pgpassfile )
2976
2985
{
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" ));
2985
2987
return NULL ;
2988
+ }
2986
2989
2987
2990
sprintf (pgpassfile , "%s/%s" , home , PGPASSFILE );
2988
2991
@@ -3014,12 +3017,18 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
3014
3017
{
3015
3018
char * t = buf ,
3016
3019
* ret ;
3020
+ int len ;
3017
3021
3018
3022
fgets (buf , LINELEN - 1 , fp );
3019
- if (strlen (buf ) == 0 )
3023
+
3024
+ len = strlen (buf );
3025
+ if (len == 0 )
3020
3026
continue ;
3021
3027
3022
- buf [strlen (buf ) - 1 ] = 0 ;
3028
+ /* Remove trailing newline */
3029
+ if (buf [len - 1 ] == '\n' )
3030
+ buf [len - 1 ] = 0 ;
3031
+
3023
3032
if ((t = pwdfMatchesString (t , hostname )) == NULL ||
3024
3033
(t = pwdfMatchesString (t , port )) == NULL ||
3025
3034
(t = pwdfMatchesString (t , dbname )) == NULL ||
0 commit comments