8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -63,7 +63,6 @@ inet_aton(const char *cp, struct in_addr * inp)
63
63
64
64
#ifdef USE_SSL
65
65
static SSL_CTX * SSL_context = NULL ;
66
-
67
66
#endif
68
67
69
68
#define NOTIFYLIST_INITIAL_SIZE 10
@@ -131,6 +130,11 @@ static const PQconninfoOption PQconninfoOptions[] = {
131
130
{"options" , "PGOPTIONS" , DefaultOption , NULL ,
132
131
"Backend-Debug-Options" , "D" , 40 },
133
132
133
+ #ifdef USE_SSL
134
+ {"requiressl" , "PGREQUIRESSL" , "0" , NULL ,
135
+ "Require-SSL" , "" , 1 },
136
+ #endif
137
+
134
138
/* Terminating entry --- MUST BE LAST */
135
139
{NULL , NULL , NULL , NULL ,
136
140
NULL , NULL , 0 }
@@ -303,6 +307,10 @@ PQconnectStart(const char *conninfo)
303
307
conn -> pguser = tmp ? strdup (tmp ) : NULL ;
304
308
tmp = conninfo_getval (connOptions , "password" );
305
309
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
306
314
307
315
/* ----------
308
316
* Free the option info - all is in conn now
@@ -475,6 +483,14 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
475
483
else
476
484
conn -> dbName = strdup (dbName );
477
485
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
+
478
494
if (error )
479
495
conn -> status = CONNECTION_BAD ;
480
496
else
@@ -781,13 +797,55 @@ connectDBStart(PGconn *conn)
781
797
goto connect_errReturn ;
782
798
#endif
783
799
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
+ * ----------
789
810
*/
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
+ {
790
819
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
791
849
/* Attempt to negotiate SSL usage */
792
850
if (conn -> allow_ssl_try )
793
851
{
@@ -837,7 +895,7 @@ connectDBStart(PGconn *conn)
837
895
{
838
896
/* Received error - probably protocol mismatch */
839
897
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" );
841
899
close (conn -> sock );
842
900
conn -> allow_ssl_try = FALSE;
843
901
return connectDBStart (conn );
@@ -849,55 +907,15 @@ connectDBStart(PGconn *conn)
849
907
goto connect_errReturn ;
850
908
}
851
909
}
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 )
866
911
{
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
+ }
871
917
#endif
872
- {
873
918
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
- }
901
919
902
920
/*
903
921
* This makes the connection non-blocking, for all those cases which
@@ -2485,6 +2503,15 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
2485
2503
2486
2504
#endif
2487
2505
2506
+ #ifdef USE_SSL
2507
+ SSL * PQgetssl (PGconn * conn )
2508
+ {
2509
+ if (!conn )
2510
+ return NULL ;
2511
+ return conn -> ssl ;
2512
+ }
2513
+ #endif
2514
+
2488
2515
void
2489
2516
PQtrace (PGconn * conn , FILE * debug_port )
2490
2517
{
0 commit comments