8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.201 2002/09/04 20:31:46 momjian Exp $
11
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.202 2002/09/05 22:05:50 momjian Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -66,6 +66,7 @@ inet_aton(const char *cp, struct in_addr * inp)
66
66
#define NOTIFYLIST_INITIAL_SIZE 10
67
67
#define NOTIFYLIST_GROWBY 10
68
68
69
+ #define PGPASSFILE "/.pgpass"
69
70
70
71
/* ----------
71
72
* Definition of the conninfo parameters and their fallback resources.
@@ -186,7 +187,7 @@ static int parseServiceInfo(PQconninfoOption *options,
186
187
PQExpBuffer errorMessage );
187
188
char * pwdfMatchesString (char * buf ,char * token );
188
189
char * PasswordFromFile (char * hostname ,char * port ,char * dbname ,
189
- char * username , char * pwdfile );
190
+ char * username );
190
191
191
192
/*
192
193
*Connecting to a Database
@@ -395,10 +396,6 @@ PQconndefaults(void)
395
396
*
396
397
* PGPASSWORD The user's password.
397
398
*
398
- * PGPASSWORDFILE
399
- * A file that contains host:port:database:user:password
400
- * for authentication
401
- *
402
399
* PGDATABASE name of database to which to connect if <pgdatabase>
403
400
* argument is NULL or a null string
404
401
*
@@ -506,8 +503,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
506
503
else if ((tmp = getenv ("PGPASSWORD" ))!= NULL )
507
504
conn -> pgpass = strdup (tmp );
508
505
else if ((tmp = PasswordFromFile (conn -> pghost ,conn -> pgport ,
509
- conn -> dbName ,conn -> pguser ,
510
- getenv ("PGPASSWORDFILE" )))!= NULL )
506
+ conn -> dbName ,conn -> pguser )))
511
507
conn -> pgpass = tmp ;
512
508
else
513
509
conn -> pgpass = strdup (DefaultPassword );
@@ -2905,22 +2901,20 @@ pwdfMatchesString(char *buf, char *token)
2905
2901
2906
2902
/* get a password from the password file. */
2907
2903
char *
2908
- PasswordFromFile (char * hostname ,char * port ,char * dbname ,
2909
- char * username ,char * pwdfile )
2904
+ PasswordFromFile (char * hostname ,char * port ,char * dbname ,char * username )
2910
2905
{
2911
2906
FILE * fp ;
2907
+ char * pgpassfile ;
2908
+ char * home ;
2909
+ struct stat stat_buf ;
2912
2910
2913
2911
#define LINELEN NAMEDATALEN*5
2914
2912
char buf [LINELEN ];
2915
- struct stat stat_buf ;
2916
-
2917
- if (pwdfile == NULL || strcmp (pwdfile ,"" )== 0 )
2918
- return NULL ;
2919
2913
2920
- if (dbname == NULL || strcmp (dbname , "" )== 0 )
2914
+ if (dbname == NULL || strlen (dbname )== 0 )
2921
2915
return NULL ;
2922
2916
2923
- if (username == NULL || strcmp (username , "" )== 0 )
2917
+ if (username == NULL || strlen (username )== 0 )
2924
2918
return NULL ;
2925
2919
2926
2920
if (hostname == NULL )
@@ -2929,20 +2923,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
2929
2923
if (port == NULL )
2930
2924
port = DEF_PGPORT_STR ;
2931
2925
2926
+ /* Look for it in the home dir */
2927
+ home = getenv ("HOME" );
2928
+ if (home )
2929
+ {
2930
+ pgpassfile = malloc (strlen (home )+ strlen (PGPASSFILE )+ 1 );
2931
+ if (!pgpassfile )
2932
+ {
2933
+ fprintf (stderr ,gettext ("%s: out of memory\n" ),pset .progname );
2934
+ exit (EXIT_FAILURE );
2935
+ }
2936
+ }
2937
+ else
2938
+ return NULL ;
2939
+
2940
+ sprintf (pgpassfile ,"%s" PGPASSFILE ,home );
2941
+
2932
2942
/* If password file cannot be opened, ignore it. */
2933
- if (stat (pwdfile ,& stat_buf )== -1 )
2943
+ if (stat (pgpassfile ,& stat_buf )== -1 )
2944
+ {
2945
+ free (pgpassfile );
2934
2946
return NULL ;
2947
+ }
2935
2948
2936
2949
/* If password file is insecure, alert the user and ignore it. */
2937
2950
if (stat_buf .st_mode & (S_IRWXG |S_IRWXO ))
2938
2951
{
2939
2952
fprintf (stderr ,
2940
2953
libpq_gettext ("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)" ),
2941
- pwdfile );
2954
+ pgpassfile );
2955
+ free (pgpassfile );
2942
2956
return NULL ;
2943
2957
}
2944
2958
2945
- fp = fopen (pwdfile ,"r" );
2959
+ fp = fopen (pgpassfile ,"r" );
2960
+ free (pgpassfile );
2946
2961
if (fp == NULL )
2947
2962
return NULL ;
2948
2963
@@ -2965,6 +2980,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname,
2965
2980
fclose (fp );
2966
2981
return ret ;
2967
2982
}
2983
+
2968
2984
fclose (fp );
2969
2985
return NULL ;
2970
2986