Skip to content

Commit 42de04f

Browse files
committed
Don't assume that "E" response to NEGOTIATE_SSL_CODE means pre-7.0 server.
These days, such a response is far more likely to signify a server-side problem, such as fork failure. Reporting "server does not support SSL" (in sslmode=require) could be quite misleading. But the results could be even worse in sslmode=prefer: if the problem was transient and the next connection attempt succeeds, we'll have silently fallen back to protocol version 2.0, possibly disabling features the user needs. Hence, it seems best to just eliminate the assumption that backing off to non-SSL/2.0 protocol is the way to recover from an "E" response, and instead treat the server error the same as we would in non-SSL cases. I tested this change against a pre-7.0 server, and found that there was a second logic bug in the "prefer" path: the test to decide whether to make a fallback connection attempt assumed that we must have opened conn->ssl, which in fact does not happen given an "E" response. After fixing that, the code does indeed connect successfully to pre-7.0, as long as you didn't set sslmode=require. (If you did, you get "Unsupported frontend protocol", which isn't completely off base given the server certainly doesn't support SSL.) Since there seems no reason to believe that pre-7.0 servers exist anymore in the wild, back-patch to all supported branches.
1 parent 431b638 commit 42de04f

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,16 +1865,19 @@ PQconnectPoll(PGconn *conn)
18651865
/* should not happen really */
18661866
return PGRES_POLLING_READING;
18671867
}
1868-
/* mark byte consumed */
1869-
conn->inStart = conn->inCursor;
18701868
if (SSLok == 'S')
18711869
{
1870+
/* mark byte consumed */
1871+
conn->inStart = conn->inCursor;
18721872
/* Set up global SSL state if required */
18731873
if (pqsecure_initialize(conn) != 0)
18741874
goto error_return;
18751875
}
18761876
else if (SSLok == 'N')
18771877
{
1878+
/* mark byte consumed */
1879+
conn->inStart = conn->inCursor;
1880+
/* OK to do without SSL? */
18781881
if (conn->sslmode[0] == 'r' || /* "require" */
18791882
conn->sslmode[0] == 'v') /* "verify-ca" or
18801883
* "verify-full" */
@@ -1891,29 +1894,17 @@ PQconnectPoll(PGconn *conn)
18911894
}
18921895
else if (SSLok == 'E')
18931896
{
1894-
/* Received error - probably protocol mismatch */
1895-
if (conn->Pfdebug)
1896-
fprintf(conn->Pfdebug, "received error from server, attempting fallback to pre-7.0\n");
1897-
if (conn->sslmode[0] == 'r' || /* "require" */
1898-
conn->sslmode[0] == 'v') /* "verify-ca" or
1899-
* "verify-full" */
1900-
{
1901-
/* Require SSL, but server is too old */
1902-
appendPQExpBuffer(&conn->errorMessage,
1903-
libpq_gettext("server does not support SSL, but SSL was required\n"));
1904-
goto error_return;
1905-
}
1906-
/* Otherwise, try again without SSL */
1907-
conn->allow_ssl_try = false;
1908-
/* Assume it ain't gonna handle protocol 3, either */
1909-
conn->pversion = PG_PROTOCOL(2, 0);
1910-
/* Must drop the old connection */
1911-
closesocket(conn->sock);
1912-
conn->sock = -1;
1913-
conn->status = CONNECTION_NEEDED;
1914-
/* Discard any unread/unsent data */
1915-
conn->inStart = conn->inCursor = conn->inEnd = 0;
1916-
conn->outCount = 0;
1897+
/*
1898+
* Server failure of some sort, such as failure to
1899+
* fork a backend process. We need to process and
1900+
* report the error message, which might be formatted
1901+
* according to either protocol 2 or protocol 3.
1902+
* Rather than duplicate the code for that, we flip
1903+
* into AWAITING_RESPONSE state and let the code there
1904+
* deal with it. Note we have *not* consumed the "E"
1905+
* byte here.
1906+
*/
1907+
conn->status = CONNECTION_AWAITING_RESPONSE;
19171908
goto keep_going;
19181909
}
19191910
else
@@ -2148,8 +2139,7 @@ PQconnectPoll(PGconn *conn)
21482139
* then do a non-SSL retry
21492140
*/
21502141
if (conn->sslmode[0] == 'p' /* "prefer" */
2151-
&& conn->ssl
2152-
&& conn->allow_ssl_try /* redundant? */
2142+
&& conn->allow_ssl_try
21532143
&& !conn->wait_ssl_try) /* redundant? */
21542144
{
21552145
/* only retry once */

0 commit comments

Comments
 (0)