@@ -825,37 +825,37 @@ initialize_SSL(PGconn *conn)
825
825
char homedir [MAXPGPATH ];
826
826
char fnbuf [MAXPGPATH ];
827
827
char sebuf [256 ];
828
+ bool have_homedir ;
828
829
bool have_cert ;
829
830
EVP_PKEY * pkey = NULL ;
830
831
831
832
/*
832
833
* We'll need the home directory if any of the relevant parameters are
833
- * defaulted.
834
+ * defaulted. If pqGetHomeDirectory fails, act as though none of the
835
+ * files could be found.
834
836
*/
835
837
if (!(conn -> sslcert && strlen (conn -> sslcert )> 0 )||
836
838
!(conn -> sslkey && strlen (conn -> sslkey )> 0 )||
837
839
!(conn -> sslrootcert && strlen (conn -> sslrootcert )> 0 )||
838
840
!(conn -> sslcrl && strlen (conn -> sslcrl )> 0 ))
839
- {
840
- if (!pqGetHomeDirectory (homedir ,sizeof (homedir )))
841
- {
842
- printfPQExpBuffer (& conn -> errorMessage ,
843
- libpq_gettext ("could not get home directory to locate client certificate files\n" ));
844
- return -1 ;
845
- }
846
- }
847
- else
848
- {
849
- homedir [0 ]= '\0' ;
850
- }
841
+ have_homedir = pqGetHomeDirectory (homedir ,sizeof (homedir ));
842
+ else /* won't need it */
843
+ have_homedir = false;
851
844
852
845
/* Read the client certificate file */
853
846
if (conn -> sslcert && strlen (conn -> sslcert )> 0 )
854
847
strncpy (fnbuf ,conn -> sslcert ,sizeof (fnbuf ));
855
- else
848
+ else if ( have_homedir )
856
849
snprintf (fnbuf ,sizeof (fnbuf ),"%s/%s" ,homedir ,USER_CERT_FILE );
850
+ else
851
+ fnbuf [0 ]= '\0' ;
857
852
858
- if (stat (fnbuf ,& buf )!= 0 )
853
+ if (fnbuf [0 ]== '\0' )
854
+ {
855
+ /* no home directory, proceed without a client cert */
856
+ have_cert = false;
857
+ }
858
+ else if (stat (fnbuf ,& buf )!= 0 )
859
859
{
860
860
/*
861
861
* If file is not present, just go on without a client cert; server
@@ -1001,11 +1001,13 @@ initialize_SSL(PGconn *conn)
1001
1001
strncpy (fnbuf ,conn -> sslkey ,sizeof (fnbuf ));
1002
1002
}
1003
1003
}
1004
- else
1004
+ else if ( have_homedir )
1005
1005
{
1006
1006
/* No PGSSLKEY specified, load default file */
1007
1007
snprintf (fnbuf ,sizeof (fnbuf ),"%s/%s" ,homedir ,USER_KEY_FILE );
1008
1008
}
1009
+ else
1010
+ fnbuf [0 ]= '\0' ;
1009
1011
1010
1012
if (have_cert && fnbuf [0 ]!= '\0' )
1011
1013
{
@@ -1060,10 +1062,13 @@ initialize_SSL(PGconn *conn)
1060
1062
*/
1061
1063
if (conn -> sslrootcert && strlen (conn -> sslrootcert )> 0 )
1062
1064
strncpy (fnbuf ,conn -> sslrootcert ,sizeof (fnbuf ));
1063
- else
1065
+ else if ( have_homedir )
1064
1066
snprintf (fnbuf ,sizeof (fnbuf ),"%s/%s" ,homedir ,ROOT_CERT_FILE );
1067
+ else
1068
+ fnbuf [0 ]= '\0' ;
1065
1069
1066
- if (stat (fnbuf ,& buf )== 0 )
1070
+ if (fnbuf [0 ]!= '\0' &&
1071
+ stat (fnbuf ,& buf )== 0 )
1067
1072
{
1068
1073
X509_STORE * cvstore ;
1069
1074
@@ -1082,11 +1087,14 @@ initialize_SSL(PGconn *conn)
1082
1087
{
1083
1088
if (conn -> sslcrl && strlen (conn -> sslcrl )> 0 )
1084
1089
strncpy (fnbuf ,conn -> sslcrl ,sizeof (fnbuf ));
1085
- else
1090
+ else if ( have_homedir )
1086
1091
snprintf (fnbuf ,sizeof (fnbuf ),"%s/%s" ,homedir ,ROOT_CRL_FILE );
1092
+ else
1093
+ fnbuf [0 ]= '\0' ;
1087
1094
1088
1095
/* Set the flags to check against the complete CRL chain */
1089
- if (X509_STORE_load_locations (cvstore ,fnbuf ,NULL )== 1 )
1096
+ if (fnbuf [0 ]!= '\0' &&
1097
+ X509_STORE_load_locations (cvstore ,fnbuf ,NULL )== 1 )
1090
1098
{
1091
1099
/* OpenSSL 0.96 does not support X509_V_FLAG_CRL_CHECK */
1092
1100
#ifdef X509_V_FLAG_CRL_CHECK
@@ -1116,9 +1124,19 @@ initialize_SSL(PGconn *conn)
1116
1124
*/
1117
1125
if (conn -> sslmode [0 ]== 'v' )/* "verify-ca" or "verify-full" */
1118
1126
{
1119
- printfPQExpBuffer (& conn -> errorMessage ,
1120
- libpq_gettext ("root certificate file \"%s\" does not exist\n"
1121
- "Either provide the file or change sslmode to disable server certificate verification.\n" ),fnbuf );
1127
+ /*
1128
+ * The only way to reach here with an empty filename is if
1129
+ * pqGetHomeDirectory failed. That's a sufficiently unusual case
1130
+ * that it seems worth having a specialized error message for it.
1131
+ */
1132
+ if (fnbuf [0 ]== '\0' )
1133
+ printfPQExpBuffer (& conn -> errorMessage ,
1134
+ libpq_gettext ("could not get home directory to locate root certificate file\n"
1135
+ "Either provide the file or change sslmode to disable server certificate verification.\n" ));
1136
+ else
1137
+ printfPQExpBuffer (& conn -> errorMessage ,
1138
+ libpq_gettext ("root certificate file \"%s\" does not exist\n"
1139
+ "Either provide the file or change sslmode to disable server certificate verification.\n" ),fnbuf );
1122
1140
return -1 ;
1123
1141
}
1124
1142
}