From 5f7b1f8d9d835c6869637f0b0df184e6fc5e0d03 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 8 Jul 2007 18:28:56 +0000
Subject: [PATCH] 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.

---
 doc/src/sgml/libpq.sgml           | 35 +++++++++++++++++-------------
 src/include/libpq/pqcomm.h        |  3 +--
 src/interfaces/libpq/fe-connect.c | 36 ++++++++++++++++---------------
 src/interfaces/libpq/libpq-fe.h   | 10 ++++-----
 src/interfaces/libpq/libpq-int.h  |  4 ++--
 5 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index bbaade6944f..e788fa109b7 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.236 2007/07/08 17:11:50 joe Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.237 2007/07/08 18:28:55 tgl Exp $ -->
 
  <chapter id="libpq">
   <title><application>libpq</application> - C Library</title>
@@ -1034,6 +1034,25 @@ int PQbackendPID(const PGconn *conn);
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><function>PQconnectionUsedPassword</function><indexterm><primary>PQconnectionUsedPassword</></></term>
+     <listitem>
+      <para>
+       Returns true (1) if the connection authentication method
+       required a password to be supplied. Returns false (0) if not.
+<synopsis>
+int PQconnectionUsedPassword(const PGconn *conn);
+</synopsis>
+      </para>
+
+      <para>
+       This function can be applied after either successful or failed
+       connection attempts.  In the case of failure, it can for example
+       be used to decide whether to prompt the user for a password.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><function>PQgetssl</function><indexterm><primary>PQgetssl</></></term>
      <listitem>
@@ -1059,20 +1078,6 @@ SSL *PQgetssl(const PGconn *conn);
      </listitem>
     </varlistentry>
 
-    <varlistentry>
-     <term><function>PQconnectionUsedPassword</function><indexterm><primary>PQconnectionUsedPassword</></></term>
-     <listitem>
-      <para>
-       Returns true (1) if the connection authentication method
-       required a password to be supplied. Returns false (0)
-       otherwise.
-       <synopsis>
-       bool PQconnectionUsedPassword(const PGconn *conn);
-       </synopsis>
-      </para>
-     </listitem>
-    </varlistentry>
-
 </variablelist>
 </para>
 
diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h
index 047940b7adf..50118e4d94d 100644
--- a/src/include/libpq/pqcomm.h
+++ b/src/include/libpq/pqcomm.h
@@ -9,7 +9,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/libpq/pqcomm.h,v 1.103 2007/07/08 17:11:51 joe Exp $
+ * $PostgreSQL: pgsql/src/include/libpq/pqcomm.h,v 1.104 2007/07/08 18:28:55 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -156,7 +156,6 @@ extern bool Db_user_namespace;
 #define AUTH_REQ_CRYPT		4	/* crypt password */
 #define AUTH_REQ_MD5		5	/* md5 password */
 #define AUTH_REQ_SCM_CREDS	6	/* transfer SCM credentials */
-#define AUTH_REQ_UNK		7	/* User has not yet attempted to authenticate */
 
 typedef uint32 AuthRequest;
 
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index a19b444c593..5263099ba49 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.346 2007/07/08 17:11:51 joe Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.347 2007/07/08 18:28:55 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1641,10 +1641,6 @@ keep_going:						/* We will come back to here until there is
 					return PGRES_POLLING_READING;
 				}
 
-				/* save the authentication request type */
-				if (conn->areq == AUTH_REQ_UNK)
-					conn->areq = areq;
-
 				/* Get the password salt if there is one. */
 				if (areq == AUTH_REQ_MD5)
 				{
@@ -1670,6 +1666,10 @@ keep_going:						/* We will come back to here until there is
 				 */
 				conn->inStart = conn->inCursor;
 
+				/* Save the authentication request type, if first one. */
+				if (conn->areq == AUTH_REQ_OK)
+					conn->areq = areq;
+
 				/* Respond to the request if necessary. */
 
 				/*
@@ -1877,7 +1877,7 @@ makeEmptyPGconn(void)
 	conn->std_strings = false;	/* unless server says differently */
 	conn->verbosity = PQERRORS_DEFAULT;
 	conn->sock = -1;
-	conn->areq = AUTH_REQ_UNK;
+	conn->areq = AUTH_REQ_OK;	/* until we receive something else */
 #ifdef USE_SSL
 	conn->allow_ssl_try = true;
 	conn->wait_ssl_try = false;
@@ -3396,6 +3396,19 @@ PQbackendPID(const PGconn *conn)
 	return conn->be_pid;
 }
 
+int
+PQconnectionUsedPassword(const PGconn *conn)
+{
+	if (!conn)
+		return false;
+	if (conn->areq == AUTH_REQ_MD5 ||
+		conn->areq == AUTH_REQ_CRYPT ||
+		conn->areq == AUTH_REQ_PASSWORD)
+		return true;
+	else
+		return false;
+}
+
 int
 PQclientEncoding(const PGconn *conn)
 {
@@ -3446,17 +3459,6 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
 	return status;
 }
 
-bool
-PQconnectionUsedPassword(const PGconn *conn)
-{
-	if (conn->areq == AUTH_REQ_MD5 ||
-		conn->areq == AUTH_REQ_CRYPT ||
-		conn->areq == AUTH_REQ_PASSWORD)
-		return true;
-	else
-		return false;
-}
-
 PGVerbosity
 PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
 {
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index efda5a0049e..93dbeb37d8b 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.137 2007/07/08 17:11:51 joe Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.138 2007/07/08 18:28:55 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -23,11 +23,10 @@ extern		"C"
 #include <stdio.h>
 
 /*
- * defines the backend's externally visible types,
+ * postgres_ext.h defines the backend's externally visible types,
  * such as Oid.
  */
 #include "postgres_ext.h"
-#include "postgres_fe.h"
 
 /* Application-visible enum types */
 
@@ -264,9 +263,9 @@ extern int	PQserverVersion(const PGconn *conn);
 extern char *PQerrorMessage(const PGconn *conn);
 extern int	PQsocket(const PGconn *conn);
 extern int	PQbackendPID(const PGconn *conn);
+extern int	PQconnectionUsedPassword(const PGconn *conn);
 extern int	PQclientEncoding(const PGconn *conn);
 extern int	PQsetClientEncoding(PGconn *conn, const char *encoding);
-extern bool	PQconnectionUsedPassword(const PGconn *conn);
 
 /* Get the OpenSSL structure associated with a connection. Returns NULL for
  * unencrypted connections or if any other TLS library is in use. */
@@ -426,7 +425,8 @@ extern void PQfreemem(void *ptr);
 /* Exists for backward compatibility.  bjm 2003-03-24 */
 #define PQfreeNotify(ptr) PQfreemem(ptr)
 
-/* Define the string so all uses are consistent. */
+/* Error when no password was given. */
+/* Note: depending on this is deprecated; use PQconnectionUsedPassword(). */
 #define PQnoPasswordSupplied	"fe_sendauth: no password supplied\n"
 
 /*
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index 12a9bd83a69..98080f2350a 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.120 2007/07/08 17:11:51 joe Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.121 2007/07/08 18:28:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -299,7 +299,7 @@ struct pg_conn
 	SockAddr	raddr;			/* Remote address */
 	ProtocolVersion pversion;	/* FE/BE protocol version in use */
 	int			sversion;		/* server version, e.g. 70401 for 7.4.1 */
-	AuthRequest	areq;			/* server demanded password during auth */
+	AuthRequest	areq;			/* auth type demanded by server */
 
 	/* Transient state needed while establishing connection */
 	struct addrinfo *addrlist;	/* list of possible backend addresses */
-- 
GitLab