Allow full SSL certificate verification (wherein libpq checks its host name
authorTom Lane <[email protected]>
Wed, 14 Jul 2010 17:10:03 +0000 (17:10 +0000)
committerTom Lane <[email protected]>
Wed, 14 Jul 2010 17:10:03 +0000 (17:10 +0000)
parameter against server cert's CN field) to succeed in the case where
both host and hostaddr are specified.  As with the existing precedents
for Kerberos, GSSAPI, SSPI, it is the calling application's responsibility
that host and hostaddr match up --- we just use the host name as given.
Per bug #5559 from Christopher Head.

In passing, make the error handling and messages for the no-host-name-given
failure more consistent among these four cases, and correct a lie in the
documentation: we don't attempt to reverse-lookup host from hostaddr
if host is missing.

Back-patch to 8.4 where SSL cert verification was introduced.

doc/src/sgml/libpq.sgml
src/interfaces/libpq/fe-auth.c
src/interfaces/libpq/fe-secure.c
src/interfaces/libpq/libpq-int.h

index b0e60937c2c567add82e1e0250969dc6dad11507..08f7525fb33b24face37d6ad98c85379e387c985 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.289.2.2 2010/06/17 16:03:36 tgl Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.289.2.3 2010/07/14 17:10:02 tgl Exp $ -->
 
 <chapter id="libpq">
  <title><application>libpq</application> - C Library</title>
 
           <para>
            Using <literal>hostaddr</> instead of <literal>host</> allows the
-           application to avoid a host name look-up, which might be important in
-           applications with time constraints. However, Kerberos and GSSAPI authentication
-           requires the host name. The following therefore applies: If
-           <literal>host</> is specified without <literal>hostaddr</>, a host name
-           lookup occurs. If <literal>hostaddr</> is specified without
-           <literal>host</>, the value for <literal>hostaddr</> gives the remote
-           address. When Kerberos is used, a reverse name query occurs to obtain
-           the host name for Kerberos. If both
-           <literal>host</> and <literal>hostaddr</> are specified, the value for
-           <literal>hostaddr</> gives the remote address; the value for
-           <literal>host</> is ignored, unless Kerberos is used, in which case that
-           value is used for Kerberos authentication. (Note that authentication is
-           likely to fail if <application>libpq</application> is passed a host name
-           that is not the name of the machine at <literal>hostaddr</>.)  Also,
-           <literal>host</> rather than <literal>hostaddr</> is used to identify
-           the connection in <filename>~/.pgpass</> (see
+           application to avoid a host name look-up, which might be important
+           in applications with time constraints. However, a host name is
+           required for Kerberos, GSSAPI, or SSPI authentication, as well as
+           for full SSL certificate verification. The following rules are
+           used:
+           If <literal>host</> is specified without <literal>hostaddr</>,
+           a host name lookup occurs.
+           If <literal>hostaddr</> is specified without <literal>host</>,
+           the value for <literal>hostaddr</> gives the server address.
+           The connection attempt will fail in any of the cases where a
+           host name is required.
+           If both <literal>host</> and <literal>hostaddr</> are specified,
+           the value for <literal>hostaddr</> gives the server address.
+           The value for <literal>host</> is ignored unless needed for
+           authentication or verification purposes, in which case it will be
+           used as the host name.  Note that authentication is likely to fail
+           if <literal>host</> is not the name of the machine at
+           <literal>hostaddr</>.
+           Also, note that <literal>host</> rather than <literal>hostaddr</>
+           is used to identify the connection in <filename>~/.pgpass</> (see
            <xref linkend="libpq-pgpass">).
           </para>
 
index 71959fac8b09638accfca623c341c3bb1fe6ee29..34e884ec4da3485c1d57c6cf24fc79038b45e7ec 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.142.2.1 2010/03/08 10:01:24 mha Exp $
+ *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.142.2.2 2010/07/14 17:10:03 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -206,10 +206,10 @@ pg_krb5_sendauth(PGconn *conn)
 
        info.pg_krb5_initialised = 0;
 
-       if (!conn->pghost)
+       if (!(conn->pghost && conn->pghost[0] != '\0'))
        {
                printfPQExpBuffer(&conn->errorMessage,
-                                                 "pg_krb5_sendauth: hostname must be specified for Kerberos authentication\n");
+                                                 libpq_gettext("host name must be specified\n"));
                return STATUS_ERROR;
        }
 
@@ -426,9 +426,10 @@ pg_GSS_startup(PGconn *conn)
        int                     maxlen;
        gss_buffer_desc temp_gbuf;
 
-       if (!conn->pghost)
+       if (!(conn->pghost && conn->pghost[0] != '\0'))
        {
-               printfPQExpBuffer(&conn->errorMessage, libpq_gettext("host name must be specified\n"));
+               printfPQExpBuffer(&conn->errorMessage,
+                                                 libpq_gettext("host name must be specified\n"));
                return STATUS_ERROR;
        }
 
@@ -652,9 +653,10 @@ pg_SSPI_startup(PGconn *conn, int use_negotiate)
         * but not more complex. We can skip the @REALM part, because Windows will
         * fill that in for us automatically.
         */
-       if (conn->pghost == NULL)
+       if (!(conn->pghost && conn->pghost[0] != '\0'))
        {
-               printfPQExpBuffer(&conn->errorMessage, libpq_gettext("host name must be specified\n"));
+               printfPQExpBuffer(&conn->errorMessage,
+                                                 libpq_gettext("host name must be specified\n"));
                return STATUS_ERROR;
        }
        conn->sspitarget = malloc(strlen(conn->krbsrvname) + strlen(conn->pghost) + 2);
index 03318424abdb395f9302f2ec761fdde217b84a97..e7cd2a4561e2825bcbe41927732279a36df91dc1 100644 (file)
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.127.2.2 2009/12/30 03:45:53 tgl Exp $
+ *       $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.127.2.3 2010/07/14 17:10:03 tgl Exp $
  *
  * NOTES
  *
@@ -527,16 +527,16 @@ static bool
 verify_peer_name_matches_certificate(PGconn *conn)
 {
        /*
-        * If told not to verify the peer name, don't do it. Return 0 indicating
+        * If told not to verify the peer name, don't do it. Return true indicating
         * that the verification was successful.
         */
        if (strcmp(conn->sslmode, "verify-full") != 0)
                return true;
 
-       if (conn->pghostaddr)
+       if (!(conn->pghost && conn->pghost[0] != '\0'))
        {
                printfPQExpBuffer(&conn->errorMessage,
-                                                 libpq_gettext("verified SSL connections are only supported when connecting to a host name"));
+                                                 libpq_gettext("host name must be specified for a verified SSL connection\n"));
                return false;
        }
        else
@@ -555,7 +555,7 @@ verify_peer_name_matches_certificate(PGconn *conn)
                else
                {
                        printfPQExpBuffer(&conn->errorMessage,
-                                                         libpq_gettext("server common name \"%s\" does not match host name \"%s\""),
+                                                         libpq_gettext("server common name \"%s\" does not match host name \"%s\"\n"),
                                                          conn->peer_cn, conn->pghost);
                        return false;
                }
index 51b7128b862c292bddf09c69a559ef28b13efeb8..bf918e323023b3bcb5944a78c6eb0a5b3dd7dbc3 100644 (file)
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.143 2009/06/23 18:13:23 mha Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.143.2.1 2010/07/14 17:10:03 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -283,10 +283,9 @@ struct pg_conn
 {
        /* Saved values of connection options */
        char       *pghost;                     /* the machine on which the server is running */
-       char       *pghostaddr;         /* the IPv4 address of the machine on which
-                                                                * the server is running, in IPv4
-                                                                * numbers-and-dots notation. Takes precedence
-                                                                * over above. */
+       char       *pghostaddr;         /* the numeric IP address of the machine on
+                                                                * which the server is running.  Takes
+                                                                * precedence over above. */
        char       *pgport;                     /* the server's communication port */
        char       *pgunixsocket;       /* the Unix-domain socket that the server is
                                                                 * listening on; if NULL, uses a default