88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.132 2000/08/20 10:55:35 petere Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.133 2000/08/30 14:54:23 momjian Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -63,7 +63,6 @@ inet_aton(const char *cp, struct in_addr * inp)
6363
6464#ifdef USE_SSL
6565static SSL_CTX * SSL_context = NULL ;
66-
6766#endif
6867
6968#define NOTIFYLIST_INITIAL_SIZE 10
@@ -131,6 +130,11 @@ static const PQconninfoOption PQconninfoOptions[] = {
131130 {"options" , "PGOPTIONS" , DefaultOption , NULL ,
132131 "Backend-Debug-Options" , "D" , 40 },
133132
133+ #ifdef USE_SSL
134+ {"requiressl" , "PGREQUIRESSL" , "0" , NULL ,
135+ "Require-SSL" , "" , 1 },
136+ #endif
137+
134138 /* Terminating entry --- MUST BE LAST */
135139 {NULL , NULL , NULL , NULL ,
136140 NULL , NULL , 0 }
@@ -303,6 +307,10 @@ PQconnectStart(const char *conninfo)
303307 conn -> pguser = tmp ? strdup (tmp ) : NULL ;
304308 tmp = conninfo_getval (connOptions , "password" );
305309 conn -> pgpass = tmp ? strdup (tmp ) : NULL ;
310+ #ifdef USE_SSL
311+ tmp = conninfo_getval (connOptions , "requiressl" );
312+ conn -> require_ssl = tmp ? (tmp [0 ]== '1' ?true:false) : false;
313+ #endif
306314
307315 /* ----------
308316 * Free the option info - all is in conn now
@@ -475,6 +483,14 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
475483 else
476484 conn -> dbName = strdup (dbName );
477485
486+
487+ #ifdef USE_SSL
488+ if ((tmp = getenv ("PGREQUIRESSL" )) != NULL )
489+ conn -> require_ssl = (tmp [0 ]== '1' )?true:false;
490+ else
491+ conn -> require_ssl = 0 ;
492+ #endif
493+
478494 if (error )
479495 conn -> status = CONNECTION_BAD ;
480496 else
@@ -781,13 +797,55 @@ connectDBStart(PGconn *conn)
781797 goto connect_errReturn ;
782798#endif
783799
784- #ifdef USE_SSL
785-
786- /*
787- * This needs to be done before we set into nonblocking, since SSL
788- * negotiation does not like that mode
800+ /* ----------
801+ * Start / make connection. We are hopefully in non-blocking mode
802+ * now, but it is possible that:
803+ * 1. Older systems will still block on connect, despite the
804+ * non-blocking flag. (Anyone know if this is true?)
805+ * 2. We are running under Windows, and aren't even trying
806+ * to be non-blocking (see above).
807+ * 3. We are using SSL.
808+ * Thus, we have make arrangements for all eventualities.
809+ * ----------
789810 */
811+ if (connect (conn -> sock , & conn -> raddr .sa , conn -> raddr_len ) < 0 )
812+ {
813+ #ifndef WIN32
814+ if (errno == EINPROGRESS || errno == 0 )
815+ #else
816+ if (WSAGetLastError () == WSAEINPROGRESS )
817+ #endif
818+ {
790819
820+ /*
821+ * This is fine - we're in non-blocking mode, and the
822+ * connection is in progress.
823+ */
824+ conn -> status = CONNECTION_STARTED ;
825+ }
826+ else
827+ {
828+ /* Something's gone wrong */
829+ printfPQExpBuffer (& conn -> errorMessage ,
830+ "connectDBStart() -- connect() failed: %s\n"
831+ "\tIs the postmaster running%s at '%s'\n"
832+ "\tand accepting connections on %s '%s'?\n" ,
833+ strerror (errno ),
834+ (family == AF_INET ) ? " (with -i)" : "" ,
835+ conn -> pghost ? conn -> pghost : "localhost" ,
836+ (family == AF_INET ) ?
837+ "TCP/IP port" : "Unix socket" ,
838+ conn -> pgport );
839+ goto connect_errReturn ;
840+ }
841+ }
842+ else
843+ {
844+ /* We're connected already */
845+ conn -> status = CONNECTION_MADE ;
846+ }
847+
848+ #ifdef USE_SSL
791849 /* Attempt to negotiate SSL usage */
792850 if (conn -> allow_ssl_try )
793851 {
@@ -837,7 +895,7 @@ connectDBStart(PGconn *conn)
837895 {
838896 /* Received error - probably protocol mismatch */
839897 if (conn -> Pfdebug )
840- fprintf (conn -> Pfdebug , "Postmaster reports error, attempting fallback to pre-6.6 .\n" );
898+ fprintf (conn -> Pfdebug , "Postmaster reports error, attempting fallback to pre-7.0 .\n" );
841899 close (conn -> sock );
842900 conn -> allow_ssl_try = FALSE;
843901 return connectDBStart (conn );
@@ -849,55 +907,15 @@ connectDBStart(PGconn *conn)
849907 goto connect_errReturn ;
850908 }
851909 }
852- #endif
853-
854- /* ----------
855- * Start / make connection. We are hopefully in non-blocking mode
856- * now, but it is possible that:
857- * 1. Older systems will still block on connect, despite the
858- * non-blocking flag. (Anyone know if this is true?)
859- * 2. We are running under Windows, and aren't even trying
860- * to be non-blocking (see above).
861- * 3. We are using SSL.
862- * Thus, we have make arrangements for all eventualities.
863- * ----------
864- */
865- if (connect (conn -> sock , & conn -> raddr .sa , conn -> raddr_len ) < 0 )
910+ if (conn -> require_ssl && !conn -> ssl )
866911 {
867- #ifndef WIN32
868- if (errno == EINPROGRESS || errno == 0 )
869- #else
870- if (WSAGetLastError () == WSAEINPROGRESS )
912+ /* Require SSL, but server does not support/want it */
913+ printfPQExpBuffer (& conn -> errorMessage ,
914+ "Server does not support SSL when SSL was required.\n" );
915+ goto connect_errReturn ;
916+ }
871917#endif
872- {
873918
874- /*
875- * This is fine - we're in non-blocking mode, and the
876- * connection is in progress.
877- */
878- conn -> status = CONNECTION_STARTED ;
879- }
880- else
881- {
882- /* Something's gone wrong */
883- printfPQExpBuffer (& conn -> errorMessage ,
884- "connectDBStart() -- connect() failed: %s\n"
885- "\tIs the postmaster running%s at '%s'\n"
886- "\tand accepting connections on %s '%s'?\n" ,
887- strerror (errno ),
888- (family == AF_INET ) ? " (with -i)" : "" ,
889- conn -> pghost ? conn -> pghost : "localhost" ,
890- (family == AF_INET ) ?
891- "TCP/IP port" : "Unix socket" ,
892- conn -> pgport );
893- goto connect_errReturn ;
894- }
895- }
896- else
897- {
898- /* We're connected already */
899- conn -> status = CONNECTION_MADE ;
900- }
901919
902920 /*
903921 * This makes the connection non-blocking, for all those cases which
@@ -2485,6 +2503,15 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
24852503
24862504#endif
24872505
2506+ #ifdef USE_SSL
2507+ SSL * PQgetssl (PGconn * conn )
2508+ {
2509+ if (!conn )
2510+ return NULL ;
2511+ return conn -> ssl ;
2512+ }
2513+ #endif
2514+
24882515void
24892516PQtrace (PGconn * conn , FILE * debug_port )
24902517{
0 commit comments