Skip to content

Commit 20f9b61

Browse files
committed
With gssencmode='require', check credential cache before connecting
Previously, libpq would establish the TCP connection, and then immediately disconnect if the credentials were not available. The same thing happened if you tried to use a Unix domain socket with gssencmode=require. Check those conditions before establishing the TCP connection. This is a very minor issue, but my motivation to do this now is that I'm about to add more detail to the tests for encryption negotiation. This makes the case of gssencmode=require but no credentials configured fail at the same stage as with gssencmode=require and GSSAPI support not compiled at all. That avoids having to deal with variations in expected output depending on build options. Discussion: https://www.postgresql.org/message-id/CAEze2Wja8VUoZygCepwUeiCrWa4jP316k0mvJrOW4PFmWP0Tcw@mail.gmail.com
1 parent 1169920 commit 20f9b61

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/interfaces/libpq/fe-connect.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,33 @@ PQconnectPoll(PGconn *conn)
28552855
/* Remember current address for possible use later */
28562856
memcpy(&conn->raddr, &addr_cur->addr, sizeof(SockAddr));
28572857

2858+
#ifdef ENABLE_GSS
2859+
2860+
/*
2861+
* Before establishing the connection, check if it's
2862+
* doomed to fail because gssencmode='require' but GSSAPI
2863+
* is not available.
2864+
*/
2865+
if (conn->gssencmode[0] == 'r')
2866+
{
2867+
if (conn->raddr.addr.ss_family == AF_UNIX)
2868+
{
2869+
libpq_append_conn_error(conn,
2870+
"GSSAPI encryption required but it is not supported over a local socket)");
2871+
goto error_return;
2872+
}
2873+
if (conn->gcred == GSS_C_NO_CREDENTIAL)
2874+
{
2875+
if (!pg_GSS_have_cred_cache(&conn->gcred))
2876+
{
2877+
libpq_append_conn_error(conn,
2878+
"GSSAPI encryption required but no credential cache");
2879+
goto error_return;
2880+
}
2881+
}
2882+
}
2883+
#endif
2884+
28582885
/*
28592886
* Set connip, too. Note we purposely ignore strdup
28602887
* failure; not a big problem if it fails.
@@ -3218,7 +3245,7 @@ PQconnectPoll(PGconn *conn)
32183245
* for GSSAPI Encryption (and skip past SSL negotiation and
32193246
* regular startup below).
32203247
*/
3221-
if (conn->try_gss && !conn->gctx)
3248+
if (conn->try_gss && !conn->gctx && conn->gcred == GSS_C_NO_CREDENTIAL)
32223249
conn->try_gss = pg_GSS_have_cred_cache(&conn->gcred);
32233250
if (conn->try_gss && !conn->gctx)
32243251
{
@@ -3237,8 +3264,9 @@ PQconnectPoll(PGconn *conn)
32373264
}
32383265
else if (!conn->gctx && conn->gssencmode[0] == 'r')
32393266
{
3267+
/* XXX: shouldn't happen */
32403268
libpq_append_conn_error(conn,
3241-
"GSSAPI encryption required but was impossible (possibly no credential cache, no server support, or using a local socket)");
3269+
"GSSAPI encryption required but was impossible");
32423270
goto error_return;
32433271
}
32443272
#endif

0 commit comments

Comments
 (0)