Skip to content

Commit cae4688

Browse files
committed
Fix bogus behavior of PQsslAttribute(conn, "library").
Commit ebc8b7d intended to change the behavior of PQsslAttribute(NULL, "library"), but accidentally also changed what happens with a non-NULL conn pointer. Undo that so that only the intended behavior change happens. Clarify some associated documentation. Per bug #17625 from Heath Lord. Back-patch to v15. Discussion: https://postgr.es/m/17625-fc47c78b7d71b534@postgresql.org
1 parent d460faf commit cae4688

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,8 +2515,9 @@ const char *PQsslAttribute(const PGconn *conn, const char *attribute_name);
25152515

25162516
<para>
25172517
The list of available attributes varies depending on the SSL library
2518-
being used, and the type of connection. If an attribute is not
2519-
available, returns NULL.
2518+
being used and the type of connection. Returns NULL if the connection
2519+
does not use SSL or the specified attribute name is not defined for the
2520+
library in use.
25202521
</para>
25212522

25222523
<para>
@@ -2575,12 +2576,15 @@ const char *PQsslAttribute(const PGconn *conn, const char *attribute_name);
25752576

25762577
<para>
25772578
As a special case, the <literal>library</literal> attribute may be
2578-
queried without an existing connection by passing NULL as the
2579-
<literal>conn</literal> argument. The historical behavior was to return
2580-
NULL for any attribute when a NULL <literal>conn</literal> was provided;
2581-
client programs needing to differentiate between the newer and older
2582-
implementations may check the
2583-
<literal>LIBPQ_HAS_SSL_LIBRARY_DETECTION</literal> feature macro.
2579+
queried without a connection by passing NULL as
2580+
the <literal>conn</literal> argument. The result will be the default
2581+
SSL library name, or NULL if <application>libpq</application> was
2582+
compiled without any SSL support. (Prior
2583+
to <productname>PostgreSQL</productname> version 15, passing NULL as
2584+
the <literal>conn</literal> argument always resulted in NULL.
2585+
Client programs needing to differentiate between the newer and older
2586+
implementations of this case may check the
2587+
<literal>LIBPQ_HAS_SSL_LIBRARY_DETECTION</literal> feature macro.)
25842588
</para>
25852589
</listitem>
25862590
</varlistentry>
@@ -2589,7 +2593,8 @@ const char *PQsslAttribute(const PGconn *conn, const char *attribute_name);
25892593
<term><function>PQsslAttributeNames</function><indexterm><primary>PQsslAttributeNames</primary></indexterm></term>
25902594
<listitem>
25912595
<para>
2592-
Return an array of SSL attribute names available. The array is terminated by a NULL pointer.
2596+
Returns an array of SSL attribute names available.
2597+
The array is terminated by a NULL pointer.
25932598
<synopsis>
25942599
const char * const * PQsslAttributeNames(const PGconn *conn);
25952600
</synopsis>
@@ -2601,17 +2606,20 @@ const char * const * PQsslAttributeNames(const PGconn *conn);
26012606
<term><function>PQsslStruct</function><indexterm><primary>PQsslStruct</primary></indexterm></term>
26022607
<listitem>
26032608
<para>
2604-
Return a pointer to an SSL-implementation-specific object describing
2605-
the connection.
2609+
Returns a pointer to an SSL-implementation-specific object describing
2610+
the connection. Returns NULL if the connection is not encrypted
2611+
or the requested type of object is not available from the connection's
2612+
SSL implementation.
26062613
<synopsis>
26072614
void *PQsslStruct(const PGconn *conn, const char *struct_name);
26082615
</synopsis>
26092616
</para>
26102617
<para>
26112618
The struct(s) available depend on the SSL implementation in use.
26122619
For <productname>OpenSSL</productname>, there is one struct,
2613-
available under the name "OpenSSL", and it returns a pointer to the
2614-
<productname>OpenSSL</productname> <literal>SSL</literal> struct.
2620+
available under the name <literal>OpenSSL</literal>,
2621+
and it returns a pointer to
2622+
<productname>OpenSSL</productname>'s <literal>SSL</literal> struct.
26152623
To use this function, code along the following lines could be used:
26162624
<programlisting><![CDATA[
26172625
#include <libpq-fe.h>
@@ -2644,7 +2652,7 @@ void *PQsslStruct(const PGconn *conn, const char *struct_name);
26442652
<listitem>
26452653
<para>
26462654
<indexterm><primary>SSL</primary><secondary sortas="libpq">in libpq</secondary></indexterm>
2647-
Returns the SSL structure used in the connection, or null
2655+
Returns the SSL structure used in the connection, or NULL
26482656
if SSL is not in use.
26492657

26502658
<synopsis>

src/interfaces/libpq/fe-secure-openssl.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,14 +1747,21 @@ PQsslAttributeNames(PGconn *conn)
17471747
const char *
17481748
PQsslAttribute(PGconn *conn, const char *attribute_name)
17491749
{
1750-
if (strcmp(attribute_name, "library") == 0)
1751-
return "OpenSSL";
1752-
17531750
if (!conn)
1751+
{
1752+
/* PQsslAttribute(NULL, "library") reports the default SSL library */
1753+
if (strcmp(attribute_name, "library") == 0)
1754+
return "OpenSSL";
17541755
return NULL;
1756+
}
1757+
1758+
/* All attributes read as NULL for a non-encrypted connection */
17551759
if (conn->ssl == NULL)
17561760
return NULL;
17571761

1762+
if (strcmp(attribute_name, "library") == 0)
1763+
return "OpenSSL";
1764+
17581765
if (strcmp(attribute_name, "key_bits") == 0)
17591766
{
17601767
static char sslbits_str[12];

0 commit comments

Comments
 (0)