8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.307 2005/06/04 20:42:43 momjian Exp $
11
+ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.308 2005/06/10 03:02:30 momjian Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -3217,9 +3217,9 @@ static char *
3217
3217
PasswordFromFile (char * hostname , char * port , char * dbname , char * username )
3218
3218
{
3219
3219
FILE * fp ;
3220
- char homedir [MAXPGPATH ];
3221
3220
char pgpassfile [MAXPGPATH ];
3222
3221
struct stat stat_buf ;
3222
+ char * passfile_env ;
3223
3223
3224
3224
#define LINELEN NAMEDATALEN*5
3225
3225
char buf [LINELEN ];
@@ -3236,15 +3236,38 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
3236
3236
if (port == NULL )
3237
3237
port = DEF_PGPORT_STR ;
3238
3238
3239
- if (!pqGetHomeDirectory (homedir , sizeof (homedir )))
3240
- return NULL ;
3239
+ if ((passfile_env = getenv ("PGPASSFILE" )) != NULL )
3240
+ {
3241
+ /* use the literal path from the environment, if set */
3242
+ StrNCpy (pgpassfile , passfile_env , MAXPGPATH );
3243
+ if (!pgpassfile )
3244
+ {
3245
+ fprintf (stderr , libpq_gettext ("out of memory\n" ));
3246
+ return NULL ;
3247
+ }
3248
+ }
3249
+ else
3250
+ {
3251
+ char homedir [MAXPGPATH ];
3241
3252
3242
- snprintf (pgpassfile , sizeof (pgpassfile ), "%s/%s" , homedir , PGPASSFILE );
3253
+ if (!pqGetHomeDirectory (homedir , sizeof (homedir )))
3254
+ return NULL ;
3255
+ snprintf (pgpassfile , sizeof (pgpassfile ), "%s/%s" , homedir , PGPASSFILE );
3256
+ }
3243
3257
3244
3258
/* If password file cannot be opened, ignore it. */
3245
3259
if (stat (pgpassfile , & stat_buf ) == -1 )
3246
3260
return NULL ;
3247
3261
3262
+ if (!S_ISREG (stat_buf .st_mode ))
3263
+ {
3264
+ fprintf (stderr ,
3265
+ libpq_gettext ("WARNING: Password file %s is not a plain file.\n" ),
3266
+ pgpassfile );
3267
+ free (pgpassfile );
3268
+ return NULL ;
3269
+ }
3270
+
3248
3271
#ifndef WIN32
3249
3272
/* If password file is insecure, alert the user and ignore it. */
3250
3273
if (stat_buf .st_mode & (S_IRWXG | S_IRWXO ))
0 commit comments