8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.190 2002/07/20 05:43:31 momjian Exp $
11
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.191 2002/08/15 02:56:19 momjian Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -182,6 +182,9 @@ static char *conninfo_getval(PQconninfoOption *connOptions,
182
182
static void defaultNoticeProcessor (void * arg , const char * message );
183
183
static int parseServiceInfo (PQconninfoOption * options ,
184
184
PQExpBuffer errorMessage );
185
+ char * pwdfMatchesString (char * buf , char * token );
186
+ char * PasswordFromFile (char * hostname , char * port , char * dbname ,
187
+ char * username , char * pwdfile );
185
188
186
189
/*
187
190
* Connecting to a Database
@@ -388,6 +391,10 @@ PQconndefaults(void)
388
391
*
389
392
* PGPASSWORD The user's password.
390
393
*
394
+ * PGPASSWORDFILE
395
+ * A file that contains host:port:database:user:password
396
+ * for authentication
397
+ *
391
398
* PGDATABASE name of database to which to connect if <pgdatabase>
392
399
* argument is NULL or a null string
393
400
*
@@ -476,13 +483,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
476
483
libpq_gettext ("could not determine the PostgreSQL user name to use\n" ));
477
484
}
478
485
479
- if (pwd )
480
- conn -> pgpass = strdup (pwd );
481
- else if ((tmp = getenv ("PGPASSWORD" )) != NULL )
482
- conn -> pgpass = strdup (tmp );
483
- else
484
- conn -> pgpass = strdup (DefaultPassword );
485
-
486
486
if (dbName == NULL )
487
487
{
488
488
if ((tmp = getenv ("PGDATABASE" )) != NULL )
@@ -493,6 +493,17 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
493
493
else
494
494
conn -> dbName = strdup (dbName );
495
495
496
+ /* getPasswordFromFile mallocs its result, so we don't need strdup here */
497
+ if (pwd )
498
+ conn -> pgpass = strdup (pwd );
499
+ else if ((tmp = getenv ("PGPASSWORD" )) != NULL )
500
+ conn -> pgpass = strdup (tmp );
501
+ else if ((tmp = PasswordFromFile (conn -> pghost , conn -> pgport ,
502
+ conn -> dbName , conn -> pguser ,
503
+ getenv ("PGPASSWORDFILE" ))) != NULL )
504
+ conn -> pgpass = tmp ;
505
+ else
506
+ conn -> pgpass = strdup (DefaultPassword );
496
507
497
508
#ifdef USE_SSL
498
509
if ((tmp = getenv ("PGREQUIRESSL" )) != NULL )
@@ -2810,3 +2821,92 @@ defaultNoticeProcessor(void *arg, const char *message)
2810
2821
/* Note: we expect the supplied string to end with a newline already. */
2811
2822
fprintf (stderr , "%s" , message );
2812
2823
}
2824
+
2825
+ /* returns a pointer to the next token or NULL if the current
2826
+ * token doesn't match */
2827
+ char *
2828
+ pwdfMatchesString (char * buf , char * token )
2829
+ {
2830
+ char * tbuf ,
2831
+ * ttok ;
2832
+ bool bslash = false;
2833
+ if (buf == NULL || token == NULL )
2834
+ return NULL ;
2835
+ tbuf = buf ;
2836
+ ttok = token ;
2837
+ if (* tbuf == '*' )
2838
+ return tbuf + 2 ;
2839
+ while (* tbuf != 0 )
2840
+ {
2841
+ if (* tbuf == '\\' && !bslash )
2842
+ {
2843
+ tbuf ++ ;
2844
+ bslash = true;
2845
+ }
2846
+ if (* tbuf == ':' && * ttok == 0 && !bslash )
2847
+ return tbuf + 1 ;
2848
+ bslash = false;
2849
+ if (* ttok == 0 )
2850
+ return NULL ;
2851
+ if (* tbuf == * ttok )
2852
+ {
2853
+ tbuf ++ ;
2854
+ ttok ++ ;
2855
+ }
2856
+ else
2857
+ return NULL ;
2858
+ }
2859
+ return NULL ;
2860
+ }
2861
+
2862
+ /* get a password from the password file. */
2863
+ char *
2864
+ PasswordFromFile (char * hostname , char * port , char * dbname ,
2865
+ char * username , char * pwdfile )
2866
+ {
2867
+ FILE * fp ;
2868
+ #define LINELEN NAMEDATALEN*5
2869
+ char buf [LINELEN ];
2870
+
2871
+ if (pwdfile == NULL || strcmp (pwdfile , "" ) == 0 )
2872
+ return NULL ;
2873
+
2874
+ if (dbname == NULL || strcmp (dbname , "" ) == 0 )
2875
+ return NULL ;
2876
+
2877
+ if (username == NULL || strcmp (username , "" ) == 0 )
2878
+ return NULL ;
2879
+
2880
+ if (hostname == NULL )
2881
+ hostname = DefaultHost ;
2882
+
2883
+ if (port == NULL )
2884
+ port = DEF_PGPORT_STR ;
2885
+
2886
+ fp = fopen (pwdfile , "r" );
2887
+ if (fp == NULL )
2888
+ return NULL ;
2889
+
2890
+ while (!feof (fp )) {
2891
+ char * t = buf ,
2892
+ * ret ;
2893
+ fgets (buf , LINELEN - 1 , fp );
2894
+ if (strlen (buf ) == 0 )
2895
+ continue ;
2896
+
2897
+ buf [strlen (buf ) - 1 ] = 0 ;
2898
+ if ((t = pwdfMatchesString (t , hostname )) == NULL ||
2899
+ (t = pwdfMatchesString (t , port )) == NULL ||
2900
+ (t = pwdfMatchesString (t , dbname )) == NULL ||
2901
+ (t = pwdfMatchesString (t , username )) == NULL )
2902
+ continue ;
2903
+ ret = (char * )malloc (sizeof (char )* strlen (t ));
2904
+ strncpy (ret , t , strlen (t ));
2905
+ fclose (fp );
2906
+ return ret ;
2907
+ }
2908
+ fclose (fp );
2909
+ return NULL ;
2910
+
2911
+ #undef LINELEN
2912
+ }
0 commit comments