88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.256 2003/07/28 00:09:16 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.257 2003/08/01 21:27:26 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -366,7 +366,7 @@ connectOptions1(PGconn *conn, const char *conninfo)
366366/* here warn that the requiressl option is deprecated? */
367367if (conn -> sslmode )
368368free (conn -> sslmode );
369- conn -> sslmode = "require" ;
369+ conn -> sslmode = strdup ( "require" ) ;
370370}
371371#endif
372372
@@ -466,15 +466,14 @@ connectOptions2(PGconn *conn)
466466case 'r' :/* "require" */
467467conn -> status = CONNECTION_BAD ;
468468printfPQExpBuffer (& conn -> errorMessage ,
469- libpq_gettext ("sslmode \"%s\" invalid when SSL "
470- "support is not compiled in\n" ),
469+ libpq_gettext ("sslmode \"%s\" invalid when SSL support is not compiled in\n" ),
471470conn -> sslmode );
472471return false;
473472}
474473#endif
475474}
476475else
477- conn -> sslmode = DefaultSSLMode ;
476+ conn -> sslmode = strdup ( DefaultSSLMode ) ;
478477
479478return true;
480479}
@@ -1351,7 +1350,8 @@ PQconnectPoll(PGconn *conn)
13511350/* Don't bother requesting SSL over a Unix socket */
13521351conn -> allow_ssl_try = false;
13531352}
1354- if (conn -> allow_ssl_try && !conn -> wait_ssl_try && conn -> ssl == NULL )
1353+ if (conn -> allow_ssl_try && !conn -> wait_ssl_try &&
1354+ conn -> ssl == NULL )
13551355{
13561356ProtocolVersion pv ;
13571357
@@ -1455,22 +1455,13 @@ PQconnectPoll(PGconn *conn)
14551455}
14561456else if (SSLok == 'N' )
14571457{
1458- switch (conn -> sslmode [0 ]) {
1459- case 'r' :/* "require" */
1460- /* Require SSL, but server does not want it */
1461- printfPQExpBuffer (& conn -> errorMessage ,
1462- libpq_gettext ("server does not support SSL, but SSL was required\n" ));
1463- gotoerror_return ;
1464- case 'a' :/* "allow" */
1465- /*
1466- * normal startup already failed,
1467- * so SSL failure means the end
1468- */
1469- printfPQExpBuffer (& conn -> errorMessage ,
1470- libpq_gettext ("server does not support SSL, and previous non-SSL attempt failed\n" ));
1471- gotoerror_return ;
1458+ if (conn -> sslmode [0 ]== 'r' )/* "require" */
1459+ {
1460+ /* Require SSL, but server does not want it */
1461+ printfPQExpBuffer (& conn -> errorMessage ,
1462+ libpq_gettext ("server does not support SSL, but SSL was required\n" ));
1463+ gotoerror_return ;
14721464}
1473-
14741465/* Otherwise, proceed with normal startup */
14751466conn -> allow_ssl_try = false;
14761467conn -> status = CONNECTION_MADE ;
@@ -1481,22 +1472,13 @@ PQconnectPoll(PGconn *conn)
14811472/* Received error - probably protocol mismatch */
14821473if (conn -> Pfdebug )
14831474fprintf (conn -> Pfdebug ,"Postmaster reports error, attempting fallback to pre-7.0.\n" );
1484- switch (conn -> sslmode [0 ]) {
1485- case 'r' :/* "require" */
1486- /* Require SSL, but server is too old */
1487- printfPQExpBuffer (& conn -> errorMessage ,
1488- libpq_gettext ("server does not support SSL, but SSL was required\n" ));
1489- gotoerror_return ;
1490- case 'a' :/* "allow" */
1491- /*
1492- * normal startup already failed,
1493- * so SSL failure means the end
1494- */
1495- printfPQExpBuffer (& conn -> errorMessage ,
1496- libpq_gettext ("server does not support SSL, and previous non-SSL attempt failed\n" ));
1497- gotoerror_return ;
1475+ if (conn -> sslmode [0 ]== 'r' )/* "require" */
1476+ {
1477+ /* Require SSL, but server is too old */
1478+ printfPQExpBuffer (& conn -> errorMessage ,
1479+ libpq_gettext ("server does not support SSL, but SSL was required\n" ));
1480+ gotoerror_return ;
14981481}
1499-
15001482/* Otherwise, try again without SSL */
15011483conn -> allow_ssl_try = false;
15021484/* Assume it ain't gonna handle protocol 3, either */
@@ -1686,13 +1668,15 @@ PQconnectPoll(PGconn *conn)
16861668
16871669#ifdef USE_SSL
16881670/*
1689- * if sslmode is "allow" and we haven't tried an
1690- *SSL connection already, then retry with an SSL connection
1671+ * if sslmode is "allow" and we haven't tried an SSL
1672+ * connection already, then retry with an SSL connection
16911673 */
1692- if (conn -> wait_ssl_try
1674+ if (conn -> sslmode [ 0 ] == 'a' /* "allow" */
16931675&& conn -> ssl == NULL
1694- && conn -> allow_ssl_try )
1676+ && conn -> allow_ssl_try
1677+ && conn -> wait_ssl_try )
16951678{
1679+ /* only retry once */
16961680conn -> wait_ssl_try = false;
16971681/* Must drop the old connection */
16981682closesocket (conn -> sock );
@@ -1703,20 +1687,19 @@ PQconnectPoll(PGconn *conn)
17031687
17041688/*
17051689 * if sslmode is "prefer" and we're in an SSL
1706- * connection and we haven't already tried a non-SSL
1707- * for "allow", then do a non-SSL retry
1690+ * connection, then do a non-SSL retry
17081691 */
1709- if (! conn -> wait_ssl_try
1692+ if (conn -> sslmode [ 0 ] == 'p' /* "prefer" */
17101693&& conn -> ssl
1711- && conn -> allow_ssl_try
1712- && conn -> sslmode [ 0 ] == 'p' ) /*"prefer" */
1694+ && conn -> allow_ssl_try /* redundant? */
1695+ && ! conn -> wait_ssl_try ) /*redundant? */
17131696{
1697+ /* only retry once */
17141698conn -> allow_ssl_try = false;
17151699/* Must drop the old connection */
17161700pqsecure_close (conn );
17171701closesocket (conn -> sock );
17181702conn -> sock = -1 ;
1719- free (conn -> ssl );
17201703conn -> status = CONNECTION_NEEDED ;
17211704gotokeep_going ;
17221705}
@@ -1773,44 +1756,6 @@ PQconnectPoll(PGconn *conn)
17731756if (fe_sendauth (areq ,conn ,conn -> pghost ,conn -> pgpass ,
17741757conn -> errorMessage .data )!= STATUS_OK )
17751758{
1776- #ifdef USE_SSL
1777- /*
1778- * if sslmode is "allow" and we haven't tried an
1779- * SSL connection already, then retry with an SSL connection
1780- */
1781- if (conn -> wait_ssl_try
1782- && conn -> ssl == NULL
1783- && conn -> allow_ssl_try )
1784- {
1785- conn -> wait_ssl_try = false;
1786- /* Must drop the old connection */
1787- closesocket (conn -> sock );
1788- conn -> sock = -1 ;
1789- conn -> status = CONNECTION_NEEDED ;
1790- gotokeep_going ;
1791- }
1792-
1793- /*
1794- * if sslmode is "prefer" and we're in an SSL
1795- * connection and we haven't already tried a non-SSL
1796- * for "allow", then do a non-SSL retry
1797- */
1798- if (!conn -> wait_ssl_try
1799- && conn -> ssl
1800- && conn -> allow_ssl_try
1801- && conn -> sslmode [0 ]== 'p' )/* "prefer" */
1802- {
1803- conn -> allow_ssl_try = false;
1804- /* Must drop the old connection */
1805- pqsecure_close (conn );
1806- closesocket (conn -> sock );
1807- conn -> sock = -1 ;
1808- free (conn -> ssl );
1809- conn -> status = CONNECTION_NEEDED ;
1810- gotokeep_going ;
1811- }
1812- #endif
1813-
18141759conn -> errorMessage .len = strlen (conn -> errorMessage .data );
18151760gotoerror_return ;
18161761}
@@ -1968,27 +1913,21 @@ PQconnectPoll(PGconn *conn)
19681913static PGconn *
19691914makeEmptyPGconn (void )
19701915{
1971- PGconn * conn = ( PGconn * ) malloc ( sizeof ( PGconn )) ;
1916+ PGconn * conn ;
19721917
1973- /* needed to use the static libpq under windows as well */
19741918#ifdef WIN32
1919+ /* needed to use the static libpq under windows as well */
19751920WSADATA wsaData ;
1976- #endif
19771921
1978- if (conn == NULL )
1979- return conn ;
1980-
1981- #ifdef WIN32
19821922if (WSAStartup (MAKEWORD (1 ,1 ),& wsaData ))
1983- {
1984- free (conn );
19851923return (PGconn * )NULL ;
1986- }
1987-
19881924WSASetLastError (0 );
1989-
19901925#endif
19911926
1927+ conn = (PGconn * )malloc (sizeof (PGconn ));
1928+ if (conn == NULL )
1929+ return conn ;
1930+
19921931/* Zero all pointers and booleans */
19931932MemSet ((char * )conn ,0 ,sizeof (PGconn ));
19941933
@@ -2003,7 +1942,8 @@ makeEmptyPGconn(void)
20031942conn -> notifyList = DLNewList ();
20041943conn -> sock = -1 ;
20051944#ifdef USE_SSL
2006- conn -> allow_ssl_try = TRUE;
1945+ conn -> allow_ssl_try = true;
1946+ conn -> wait_ssl_try = false;
20071947#endif
20081948
20091949/*
@@ -2073,6 +2013,8 @@ freePGconn(PGconn *conn)
20732013free (conn -> pguser );
20742014if (conn -> pgpass )
20752015free (conn -> pgpass );
2016+ if (conn -> sslmode )
2017+ free (conn -> sslmode );
20762018/* Note that conn->Pfdebug is not ours to close or free */
20772019if (conn -> notifyList )
20782020DLFreeList (conn -> notifyList );