Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 57c0879

Browse files
committed
Code review for sslmode patch: eliminate memory leak, avoid giving a
completely useless error message in 'allow' case, don't retry connection at the sendauth stage (by then the server will either let us in or not, no point in wasting cycles on another try in the other SSL state).
1 parent 13ac54d commit 57c0879

File tree

2 files changed

+42
-100
lines changed

2 files changed

+42
-100
lines changed

src/interfaces/libpq/fe-connect.c

+39-97
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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? */
367367
if (conn->sslmode)
368368
free(conn->sslmode);
369-
conn->sslmode = "require";
369+
conn->sslmode = strdup("require");
370370
}
371371
#endif
372372

@@ -466,15 +466,14 @@ connectOptions2(PGconn *conn)
466466
case 'r': /* "require" */
467467
conn->status = CONNECTION_BAD;
468468
printfPQExpBuffer(&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"),
471470
conn->sslmode);
472471
return false;
473472
}
474473
#endif
475474
}
476475
else
477-
conn->sslmode = DefaultSSLMode;
476+
conn->sslmode = strdup(DefaultSSLMode);
478477

479478
return true;
480479
}
@@ -1351,7 +1350,8 @@ PQconnectPoll(PGconn *conn)
13511350
/* Don't bother requesting SSL over a Unix socket */
13521351
conn->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
{
13561356
ProtocolVersion pv;
13571357

@@ -1455,22 +1455,13 @@ PQconnectPoll(PGconn *conn)
14551455
}
14561456
else 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-
goto error_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-
goto error_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+
goto error_return;
14721464
}
1473-
14741465
/* Otherwise, proceed with normal startup */
14751466
conn->allow_ssl_try = false;
14761467
conn->status = CONNECTION_MADE;
@@ -1481,22 +1472,13 @@ PQconnectPoll(PGconn *conn)
14811472
/* Received error - probably protocol mismatch */
14821473
if (conn->Pfdebug)
14831474
fprintf(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-
goto error_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-
goto error_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+
goto error_return;
14981481
}
1499-
15001482
/* Otherwise, try again without SSL */
15011483
conn->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 */
16961680
conn->wait_ssl_try = false;
16971681
/* Must drop the old connection */
16981682
closesocket(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 */
17141698
conn->allow_ssl_try = false;
17151699
/* Must drop the old connection */
17161700
pqsecure_close(conn);
17171701
closesocket(conn->sock);
17181702
conn->sock = -1;
1719-
free(conn->ssl);
17201703
conn->status = CONNECTION_NEEDED;
17211704
goto keep_going;
17221705
}
@@ -1773,44 +1756,6 @@ PQconnectPoll(PGconn *conn)
17731756
if (fe_sendauth(areq, conn, conn->pghost, conn->pgpass,
17741757
conn->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-
goto keep_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-
goto keep_going;
1811-
}
1812-
#endif
1813-
18141759
conn->errorMessage.len = strlen(conn->errorMessage.data);
18151760
goto error_return;
18161761
}
@@ -1968,27 +1913,21 @@ PQconnectPoll(PGconn *conn)
19681913
static PGconn *
19691914
makeEmptyPGconn(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 */
19751920
WSADATA wsaData;
1976-
#endif
19771921

1978-
if (conn == NULL)
1979-
return conn;
1980-
1981-
#ifdef WIN32
19821922
if (WSAStartup(MAKEWORD(1, 1), &wsaData))
1983-
{
1984-
free(conn);
19851923
return (PGconn*) NULL;
1986-
}
1987-
19881924
WSASetLastError(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 */
19931932
MemSet((char *) conn, 0, sizeof(PGconn));
19941933

@@ -2003,7 +1942,8 @@ makeEmptyPGconn(void)
20031942
conn->notifyList = DLNewList();
20041943
conn->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)
20732013
free(conn->pguser);
20742014
if (conn->pgpass)
20752015
free(conn->pgpass);
2016+
if (conn->sslmode)
2017+
free(conn->sslmode);
20762018
/* Note that conn->Pfdebug is not ours to close or free */
20772019
if (conn->notifyList)
20782020
DLFreeList(conn->notifyList);

src/interfaces/libpq/libpq-int.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $Id: libpq-int.h,v 1.77 2003/07/26 13:50:02 momjian Exp $
15+
* $Id: libpq-int.h,v 1.78 2003/08/01 21:27:27 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -249,6 +249,7 @@ struct pg_conn
249249
char *dbName; /* database name */
250250
char *pguser; /* Postgres username and password, if any */
251251
char *pgpass;
252+
char *sslmode; /* SSL mode (require,prefer,allow,disable) */
252253

253254
/* Optional file to write trace info to */
254255
FILE *Pfdebug;
@@ -316,11 +317,10 @@ struct pg_conn
316317
PGresult *result; /* result being constructed */
317318
PGresAttValue *curTuple; /* tuple currently being read */
318319

319-
char *sslmode; /* SSL mode option string */
320320
#ifdef USE_SSL
321321
bool allow_ssl_try; /* Allowed to try SSL negotiation */
322322
bool wait_ssl_try; /* Delay SSL negotiation until after
323-
attempting normal connection */
323+
* attempting normal connection */
324324
SSL *ssl; /* SSL status, if have SSL connection */
325325
X509 *peer; /* X509 cert of server */
326326
char peer_dn[256 + 1]; /* peer distinguished name */

0 commit comments

Comments
 (0)