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

Commit 51bc3df

Browse files
committed
Arrange for the authentication request type to be preserved in
PGconn. Invent a new libpq connection-status function, PQconnectionUsedPassword() that returns true if the server demanded a password during authentication, false otherwise. This may be useful to clients in general, but is immediately useful to help plug a privilege escalation path in dblink. Per list discussion and design proposed by Tom Lane.
1 parent 8c69d88 commit 51bc3df

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

doc/src/sgml/libpq.sgml

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.235 2007/03/30 03:19:02 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.236 2007/07/08 17:11:50 joe Exp $ -->
22

33
<chapter id="libpq">
44
<title><application>libpq</application> - C Library</title>
@@ -1059,6 +1059,20 @@ SSL *PQgetssl(const PGconn *conn);
10591059
</listitem>
10601060
</varlistentry>
10611061

1062+
<varlistentry>
1063+
<term><function>PQconnectionUsedPassword</function><indexterm><primary>PQconnectionUsedPassword</></></term>
1064+
<listitem>
1065+
<para>
1066+
Returns true (1) if the connection authentication method
1067+
required a password to be supplied. Returns false (0)
1068+
otherwise.
1069+
<synopsis>
1070+
bool PQconnectionUsedPassword(const PGconn *conn);
1071+
</synopsis>
1072+
</para>
1073+
</listitem>
1074+
</varlistentry>
1075+
10621076
</variablelist>
10631077
</para>
10641078

src/include/libpq/pqcomm.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $PostgreSQL: pgsql/src/include/libpq/pqcomm.h,v 1.102 2007/01/05 22:19:55 momjian Exp $
12+
* $PostgreSQL: pgsql/src/include/libpq/pqcomm.h,v 1.103 2007/07/08 17:11:51 joe Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -156,6 +156,7 @@ extern bool Db_user_namespace;
156156
#define AUTH_REQ_CRYPT 4 /* crypt password */
157157
#define AUTH_REQ_MD5 5 /* md5 password */
158158
#define AUTH_REQ_SCM_CREDS 6 /* transfer SCM credentials */
159+
#define AUTH_REQ_UNK 7 /* User has not yet attempted to authenticate */
159160

160161
typedef uint32 AuthRequest;
161162

src/interfaces/libpq/exports.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.15 2007/03/03 19:52:46 momjian Exp $
1+
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.16 2007/07/08 17:11:51 joe Exp $
22
# Functions to be exported by libpq DLLs
33
PQconnectdb 1
44
PQsetdbLogin 2
@@ -137,3 +137,4 @@ PQdescribePortal 134
137137
PQsendDescribePrepared 135
138138
PQsendDescribePortal 136
139139
lo_truncate 137
140+
PQconnectionUsedPassword 138

src/interfaces/libpq/fe-connect.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.345 2007/03/08 19:27:28 mha Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.346 2007/07/08 17:11:51 joe Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1641,6 +1641,10 @@ PQconnectPoll(PGconn *conn)
16411641
return PGRES_POLLING_READING;
16421642
}
16431643

1644+
/* save the authentication request type */
1645+
if (conn->areq == AUTH_REQ_UNK)
1646+
conn->areq = areq;
1647+
16441648
/* Get the password salt if there is one. */
16451649
if (areq == AUTH_REQ_MD5)
16461650
{
@@ -1873,6 +1877,7 @@ makeEmptyPGconn(void)
18731877
conn->std_strings = false; /* unless server says differently */
18741878
conn->verbosity = PQERRORS_DEFAULT;
18751879
conn->sock = -1;
1880+
conn->areq = AUTH_REQ_UNK;
18761881
#ifdef USE_SSL
18771882
conn->allow_ssl_try = true;
18781883
conn->wait_ssl_try = false;
@@ -3441,6 +3446,17 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
34413446
return status;
34423447
}
34433448

3449+
bool
3450+
PQconnectionUsedPassword(const PGconn *conn)
3451+
{
3452+
if (conn->areq == AUTH_REQ_MD5 ||
3453+
conn->areq == AUTH_REQ_CRYPT ||
3454+
conn->areq == AUTH_REQ_PASSWORD)
3455+
return true;
3456+
else
3457+
return false;
3458+
}
3459+
34443460
PGVerbosity
34453461
PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
34463462
{

src/interfaces/libpq/libpq-fe.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.136 2007/03/03 19:52:46 momjian Exp $
10+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.137 2007/07/08 17:11:51 joe Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,10 +23,11 @@ extern "C"
2323
#include <stdio.h>
2424

2525
/*
26-
* postgres_ext.h defines the backend's externally visible types,
26+
* defines the backend's externally visible types,
2727
* such as Oid.
2828
*/
2929
#include "postgres_ext.h"
30+
#include "postgres_fe.h"
3031

3132
/* Application-visible enum types */
3233

@@ -265,6 +266,7 @@ extern int PQsocket(const PGconn *conn);
265266
extern int PQbackendPID(const PGconn *conn);
266267
extern int PQclientEncoding(const PGconn *conn);
267268
extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
269+
extern bool PQconnectionUsedPassword(const PGconn *conn);
268270

269271
/* Get the OpenSSL structure associated with a connection. Returns NULL for
270272
* unencrypted connections or if any other TLS library is in use. */

src/interfaces/libpq/libpq-int.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.119 2007/03/03 19:52:47 momjian Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.120 2007/07/08 17:11:51 joe Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -299,6 +299,7 @@ struct pg_conn
299299
SockAddr raddr; /* Remote address */
300300
ProtocolVersion pversion; /* FE/BE protocol version in use */
301301
int sversion; /* server version, e.g. 70401 for 7.4.1 */
302+
AuthRequest areq; /* server demanded password during auth */
302303

303304
/* Transient state needed while establishing connection */
304305
struct addrinfo *addrlist; /* list of possible backend addresses */

0 commit comments

Comments
 (0)