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

Commit 5f7b1f8

Browse files
committed
Closer code review for PQconnectionUsedPassword() patch: in particular,
not OK to include postgres_fe.h into libpq-fe.h, hence declare it as returning int not bool.
1 parent 3f33d7b commit 5f7b1f8

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed

doc/src/sgml/libpq.sgml

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.236 2007/07/08 17:11:50 joe Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.237 2007/07/08 18:28:55 tgl Exp $ -->
22

33
<chapter id="libpq">
44
<title><application>libpq</application> - C Library</title>
@@ -1034,6 +1034,25 @@ int PQbackendPID(const PGconn *conn);
10341034
</listitem>
10351035
</varlistentry>
10361036

1037+
<varlistentry>
1038+
<term><function>PQconnectionUsedPassword</function><indexterm><primary>PQconnectionUsedPassword</></></term>
1039+
<listitem>
1040+
<para>
1041+
Returns true (1) if the connection authentication method
1042+
required a password to be supplied. Returns false (0) if not.
1043+
<synopsis>
1044+
int PQconnectionUsedPassword(const PGconn *conn);
1045+
</synopsis>
1046+
</para>
1047+
1048+
<para>
1049+
This function can be applied after either successful or failed
1050+
connection attempts. In the case of failure, it can for example
1051+
be used to decide whether to prompt the user for a password.
1052+
</para>
1053+
</listitem>
1054+
</varlistentry>
1055+
10371056
<varlistentry>
10381057
<term><function>PQgetssl</function><indexterm><primary>PQgetssl</></></term>
10391058
<listitem>
@@ -1059,20 +1078,6 @@ SSL *PQgetssl(const PGconn *conn);
10591078
</listitem>
10601079
</varlistentry>
10611080

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-
10761081
</variablelist>
10771082
</para>
10781083

src/include/libpq/pqcomm.h

+1-2
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.103 2007/07/08 17:11:51 joe Exp $
12+
* $PostgreSQL: pgsql/src/include/libpq/pqcomm.h,v 1.104 2007/07/08 18:28:55 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -156,7 +156,6 @@ 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 */
160159

161160
typedef uint32 AuthRequest;
162161

src/interfaces/libpq/fe-connect.c

+19-17
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.346 2007/07/08 17:11:51 joe Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.347 2007/07/08 18:28:55 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1641,10 +1641,6 @@ 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-
16481644
/* Get the password salt if there is one. */
16491645
if (areq == AUTH_REQ_MD5)
16501646
{
@@ -1670,6 +1666,10 @@ PQconnectPoll(PGconn *conn)
16701666
*/
16711667
conn->inStart = conn->inCursor;
16721668

1669+
/* Save the authentication request type, if first one. */
1670+
if (conn->areq == AUTH_REQ_OK)
1671+
conn->areq = areq;
1672+
16731673
/* Respond to the request if necessary. */
16741674

16751675
/*
@@ -1877,7 +1877,7 @@ makeEmptyPGconn(void)
18771877
conn->std_strings = false; /* unless server says differently */
18781878
conn->verbosity = PQERRORS_DEFAULT;
18791879
conn->sock = -1;
1880-
conn->areq = AUTH_REQ_UNK;
1880+
conn->areq = AUTH_REQ_OK; /* until we receive something else */
18811881
#ifdef USE_SSL
18821882
conn->allow_ssl_try = true;
18831883
conn->wait_ssl_try = false;
@@ -3396,6 +3396,19 @@ PQbackendPID(const PGconn *conn)
33963396
return conn->be_pid;
33973397
}
33983398

3399+
int
3400+
PQconnectionUsedPassword(const PGconn *conn)
3401+
{
3402+
if (!conn)
3403+
return false;
3404+
if (conn->areq == AUTH_REQ_MD5 ||
3405+
conn->areq == AUTH_REQ_CRYPT ||
3406+
conn->areq == AUTH_REQ_PASSWORD)
3407+
return true;
3408+
else
3409+
return false;
3410+
}
3411+
33993412
int
34003413
PQclientEncoding(const PGconn *conn)
34013414
{
@@ -3446,17 +3459,6 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
34463459
return status;
34473460
}
34483461

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-
34603462
PGVerbosity
34613463
PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
34623464
{

src/interfaces/libpq/libpq-fe.h

+5-5
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.137 2007/07/08 17:11:51 joe Exp $
10+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.138 2007/07/08 18:28:55 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,11 +23,10 @@ extern "C"
2323
#include <stdio.h>
2424

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

3231
/* Application-visible enum types */
3332

@@ -264,9 +263,9 @@ extern int PQserverVersion(const PGconn *conn);
264263
extern char *PQerrorMessage(const PGconn *conn);
265264
extern int PQsocket(const PGconn *conn);
266265
extern int PQbackendPID(const PGconn *conn);
266+
extern int PQconnectionUsedPassword(const PGconn *conn);
267267
extern int PQclientEncoding(const PGconn *conn);
268268
extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
269-
extern bool PQconnectionUsedPassword(const PGconn *conn);
270269

271270
/* Get the OpenSSL structure associated with a connection. Returns NULL for
272271
* unencrypted connections or if any other TLS library is in use. */
@@ -426,7 +425,8 @@ extern void PQfreemem(void *ptr);
426425
/* Exists for backward compatibility. bjm 2003-03-24 */
427426
#define PQfreeNotify(ptr) PQfreemem(ptr)
428427

429-
/* Define the string so all uses are consistent. */
428+
/* Error when no password was given. */
429+
/* Note: depending on this is deprecated; use PQconnectionUsedPassword(). */
430430
#define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
431431

432432
/*

src/interfaces/libpq/libpq-int.h

+2-2
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.120 2007/07/08 17:11:51 joe Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.121 2007/07/08 18:28:56 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -299,7 +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 */
302+
AuthRequest areq; /* auth type demanded by server */
303303

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

0 commit comments

Comments
 (0)