2001-12-27 22:37:34 +01:00
<!--
2004-12-27 20:19:24 +01:00
$PostgreSQL: pgsql/doc/src/sgml/client-auth.sgml,v 1.70 2004/12/27 19:19:23 tgl Exp $
2001-12-27 22:37:34 +01:00
-->
2000-06-18 23:24:54 +02:00
<chapter id="client-authentication">
<title>Client Authentication</title>
2001-05-13 00:51:36 +02:00
<indexterm zone="client-authentication">
<primary>client authentication</primary>
</indexterm>
2000-06-18 23:24:54 +02:00
<para>
2002-04-04 06:25:54 +02:00
When a client application connects to the database server, it
specifies which <productname>PostgreSQL</productname> user name it
wants to connect as, much the same way one logs into a Unix computer
as a particular user. Within the SQL environment the active database
2004-11-15 07:32:15 +01:00
user name determines access privileges to database objects — see
2002-04-04 06:25:54 +02:00
<xref linkend="user-manag"> for more information. Therefore, it is
essential to restrict which database users can connect.
2000-06-18 23:24:54 +02:00
</para>
<para>
<firstterm>Authentication</firstterm> is the process by which the
database server establishes the identity of the client, and by
extension determines whether the client application (or the user
2000-11-21 21:44:32 +01:00
who runs the client application) is permitted to connect with the
2000-06-18 23:24:54 +02:00
user name that was requested.
</para>
<para>
2001-11-21 06:53:41 +01:00
<productname>PostgreSQL</productname> offers a number of different
2002-08-16 06:48:16 +02:00
client authentication methods. The method used to authenticate a
particular client connection can be selected on the basis of
(client) host address, database, and user.
2000-06-18 23:24:54 +02:00
</para>
2000-11-21 21:44:32 +01:00
<para>
2002-04-04 06:25:54 +02:00
<productname>PostgreSQL</productname> user names are logically
2000-11-21 21:44:32 +01:00
separate from user names of the operating system in which the server
2002-04-04 06:25:54 +02:00
runs. If all the users of a particular server also have accounts on
2000-11-21 21:44:32 +01:00
the server's machine, it makes sense to assign database user names
2002-04-04 06:25:54 +02:00
that match their operating system user names. However, a server that
2003-03-13 02:30:29 +01:00
accepts remote connections may have many database users who have no local operating system
2002-04-04 06:25:54 +02:00
account, and in such cases there need be no connection between
database user names and OS user names.
2000-11-21 21:44:32 +01:00
</para>
2002-09-14 20:35:46 +02:00
<sect1 id="auth-pg-hba-conf">
2000-06-18 23:24:54 +02:00
<title>The <filename>pg_hba.conf</filename> file</title>
2002-09-14 20:35:46 +02:00
<indexterm zone="auth-pg-hba-conf">
2001-05-13 00:51:36 +02:00
<primary>pg_hba.conf</primary>
</indexterm>
2000-06-18 23:24:54 +02:00
<para>
2004-12-27 00:06:56 +01:00
Client authentication is controlled by a configuration file,
which traditionally is named
<filename>pg_hba.conf</filename> and is stored in the database
cluster's data directory.
2002-04-04 06:25:54 +02:00
(<acronym>HBA</> stands for host-based authentication.) A default
2002-08-16 06:48:16 +02:00
<filename>pg_hba.conf</filename> file is installed when the data
2004-12-27 00:06:56 +01:00
directory is initialized by <command>initdb</command>. It is
possible to place the authentication configuration file elsewhere,
however; see the <xref linkend="guc-hba-file"> configuration parameter.
2000-06-18 23:24:54 +02:00
</para>
<para>
2002-11-11 21:14:04 +01:00
The general format of the <filename>pg_hba.conf</filename> file is
2002-04-04 06:25:54 +02:00
a set of records, one per line. Blank lines are ignored, as is any
2003-03-13 02:30:29 +01:00
text after the <literal>#</literal> comment character. A record is made
2002-04-04 06:25:54 +02:00
up of a number of fields which are separated by spaces and/or tabs.
Fields can contain white space if the field value is quoted. Records
cannot be continued across lines.
2000-06-18 23:24:54 +02:00
</para>
2001-11-02 19:39:57 +01:00
<para>
Each record specifies a connection type, a client IP address range
2002-04-04 06:25:54 +02:00
(if relevant for the connection type), a database name, a user name,
2001-11-02 19:39:57 +01:00
and the authentication method to be used for connections matching
2002-04-04 06:25:54 +02:00
these parameters. The first record with a matching connection type,
client address, requested database, and user name is used to perform
authentication. There is no <quote>fall-through</> or
2001-11-02 19:39:57 +01:00
<quote>backup</>: if one record is chosen and the authentication
2002-04-04 06:25:54 +02:00
fails, subsequent records are not considered. If no record matches,
access is denied.
2001-11-02 19:39:57 +01:00
</para>
2000-06-18 23:24:54 +02:00
<para>
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
A record may have one of the seven formats
2003-03-13 02:30:29 +01:00
<synopsis>
2003-11-04 10:55:39 +01:00
local <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
2004-08-26 18:50:05 +02:00
host <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>CIDR-address</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
hostssl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>CIDR-address</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
2004-11-10 22:54:23 +01:00
hostnossl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>CIDR-address</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
2003-11-04 10:55:39 +01:00
host <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>IP-address</replaceable> <replaceable>IP-mask</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
hostssl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>IP-address</replaceable> <replaceable>IP-mask</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
hostnossl <replaceable>database</replaceable> <replaceable>user</replaceable> <replaceable>IP-address</replaceable> <replaceable>IP-mask</replaceable> <replaceable>authentication-method</replaceable> <optional><replaceable>authentication-option</replaceable></optional>
2003-03-13 02:30:29 +01:00
</synopsis>
2000-06-18 23:24:54 +02:00
The meaning of the fields is as follows:
<variablelist>
<varlistentry>
<term><literal>local</literal></term>
<listitem>
<para>
2003-03-13 02:30:29 +01:00
This record matches connection attempts using Unix-domain
2002-09-14 20:35:46 +02:00
sockets. Without a record of this type, Unix-domain socket
2003-09-13 18:43:38 +02:00
connections are disallowed.
2000-06-18 23:24:54 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>host</literal></term>
<listitem>
<para>
2003-12-14 00:59:07 +01:00
This record matches connection attempts made using TCP/IP.
2004-03-23 02:23:48 +01:00
<literal>host</literal> records match either
2003-12-14 00:59:07 +01:00
<acronym>SSL</acronym> or non-<acronym>SSL</acronym> connection
attempts.
2000-06-18 23:24:54 +02:00
</para>
2004-03-23 02:23:48 +01:00
<note>
<para>
Remote TCP/IP connections will not be possible unless
the server is started with an appropriate value for the
<xref linkend="guc-listen-addresses"> configuration parameter,
since the default behavior is to listen for TCP/IP connections
only on the local loopback address <literal>localhost</>.
</para>
</note>
2000-06-18 23:24:54 +02:00
</listitem>
</varlistentry>
2000-08-29 06:15:43 +02:00
<varlistentry>
<term><literal>hostssl</literal></term>
<listitem>
<para>
2004-03-23 02:23:48 +01:00
This record matches connection attempts made using TCP/IP,
but only when the connection is made with <acronym>SSL</acronym>
encryption.
2002-09-14 20:35:46 +02:00
</para>
<para>
2003-12-14 00:59:07 +01:00
To make use of this option the server must be built with
2004-03-23 02:23:48 +01:00
<acronym>SSL</acronym> support. Furthermore,
<acronym>SSL</acronym> must be enabled at server start time
by setting the <xref linkend="guc-ssl"> configuration parameter (see
<xref linkend="ssl-tcp"> for more information).
2000-08-29 06:15:43 +02:00
</para>
</listitem>
</varlistentry>
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
<varlistentry>
<term><literal>hostnossl</literal></term>
<listitem>
<para>
2004-12-27 00:06:56 +01:00
This record type has the opposite logic to <literal>hostssl</>:
it only matches connection attempts made over
2004-03-23 02:23:48 +01:00
TCP/IP that do not use <acronym>SSL</acronym>.
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
</para>
</listitem>
</varlistentry>
2000-06-18 23:24:54 +02:00
<varlistentry>
<term><replaceable>database</replaceable></term>
<listitem>
<para>
2002-09-14 20:35:46 +02:00
Specifies which databases this record matches. The value
<literal>all</literal> specifies that it matches all databases.
The value <literal>sameuser</> specifies that the record
matches if the requested database has the same name as the
requested user. The value <literal>samegroup</> specifies that
2004-12-27 00:06:56 +01:00
the requested user must be a member of the group with the same
2002-09-14 20:35:46 +02:00
name as the requested database. Otherwise, this is the name of
a specific <productname>PostgreSQL</productname> database.
Multiple database names can be supplied by separating them with
commas. A file containing database names can be specified by
2004-12-27 20:19:24 +01:00
preceding the file name with <literal>@</>.
2002-04-04 06:25:54 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>user</replaceable></term>
<listitem>
<para>
2002-11-15 04:11:18 +01:00
Specifies which <productname>PostgreSQL</> users this record
matches. The value <literal>all</literal> specifies that it
matches all users. Otherwise, this is the name of a specific
2002-04-04 06:25:54 +02:00
<productname>PostgreSQL</productname> user. Multiple user names
can be supplied by separating them with commas. Group names can
be specified by preceding the group name with <literal>+</>. A
2002-09-14 20:35:46 +02:00
file containing user names can be specified by preceding the
2004-12-27 20:19:24 +01:00
file name with <literal>@</>.
2000-06-18 23:24:54 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2004-08-26 18:50:05 +02:00
<term><replaceable>CIDR-address</replaceable></term>
2000-06-18 23:24:54 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Specifies the client machine IP address range that this record
2004-08-26 18:50:05 +02:00
matches. It contains an IP address in standard dotted decimal
notation and a CIDR mask length. (IP addresses can only be
2004-12-27 00:06:56 +01:00
specified numerically, not as domain or host names.) The mask
length indicates the number of high-order bits of the client
IP address that must match. Bits to the right of this must
be zero in the given IP address.
There must not be any white space between the IP address, the
<literal>/</literal>, and the CIDR mask length.
2004-08-26 18:50:05 +02:00
</para>
<para>
2004-12-27 00:06:56 +01:00
A typical <replaceable>CIDR-address</replaceable> is
<literal>172.20.143.89/32</literal> for a single host, or
<literal>172.20.143.0/24</literal> for a network.
To specify a single host, use a CIDR mask of 32 for IPv4 or
128 for IPv6.
2003-09-05 22:31:36 +02:00
</para>
<para>
An IP address given in IPv4 format will match IPv6 connections that
have the corresponding address, for example <literal>127.0.0.1</>
will match the IPv6 address <literal>::ffff:127.0.0.1</>. An entry
given in IPv6 format will match only IPv6 connections, even if the
represented address is in the IPv4-in-IPv6 range. Note that entries
in IPv6 format will be rejected if the system's C library does not have
support for IPv6 addresses.
2002-09-14 20:35:46 +02:00
</para>
<para>
2004-12-27 00:06:56 +01:00
This field only applies to <literal>host</literal>,
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
<literal>hostssl</literal>, and <literal>hostnossl</> records.
2000-06-18 23:24:54 +02:00
</para>
</listitem>
</varlistentry>
2003-06-25 03:20:50 +02:00
<varlistentry>
2004-08-26 18:50:05 +02:00
<term><replaceable>IP-address</replaceable></term>
2004-12-27 00:06:56 +01:00
<term><replaceable>IP-mask</replaceable></term>
2003-06-25 03:20:50 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
These fields may be used as an alternative to the
2004-08-26 18:50:05 +02:00
<replaceable>CIDR-address</replaceable> notation. Instead of
specifying the mask length, the actual mask is specified in a
2004-12-27 00:06:56 +01:00
separate column. For example, <literal>255.0.0.0</> represents an IPv4
CIDR mask length of 8, and <literal>255.255.255.255</> represents a
CIDR mask length of 32.
2003-06-25 03:20:50 +02:00
</para>
<para>
2004-12-27 00:06:56 +01:00
These fields only apply to <literal>host</literal>,
At long last I put together a patch to support 4 client SSL negotiation
modes (and replace the requiressl boolean). The four options were first
spelled out by Magnus Hagander <mha@sollentuna.net> on 2000-08-23 in email
to pgsql-hackers, archived here:
http://archives.postgresql.org/pgsql-hackers/2000-08/msg00639.php
My original less-flexible patch and the ensuing thread are archived at:
http://dbforums.com/t623845.html
Attached is a new patch, including documentation.
To sum up, there's a new client parameter "sslmode" and environment
variable "PGSSLMODE", with these options:
sslmode description
------- -----------
disable Unencrypted non-SSL only
allow Negotiate, prefer non-SSL
prefer Negotiate, prefer SSL (default)
require Require SSL
The only change to the server is a new pg_hba.conf line type,
"hostnossl", for specifying connections that are not allowed to use SSL
(for example, to prevent servers on a local network from accidentally
using SSL and wasting cycles). Thus the 3 pg_hba.conf line types are:
pg_hba.conf line types
----------------------
host applies to either SSL or regular connections
hostssl applies only to SSL connections
hostnossl applies only to regular connections
These client and server options, the postgresql.conf ssl = false option,
and finally the possibility of compiling with no SSL support at all,
make quite a range of combinations to test. I threw together a test
script to try many of them out. It's in a separate tarball with its
config files, a patch to psql so it'll announce SSL connections even in
absence of a tty, and the test output. The test is especially informative
when run on the same tty the postmaster was started on, so the FATAL:
errors during negotiation are interleaved with the psql client output.
I saw Tom write that new submissions for 7.4 have to be in before midnight
local time, and since I'm on the east coast in the US, this just makes it
in before the bell. :)
Jon Jensen
2003-07-26 15:50:02 +02:00
<literal>hostssl</literal>, and <literal>hostnossl</> records.
2003-06-25 03:20:50 +02:00
</para>
</listitem>
</varlistentry>
2000-06-18 23:24:54 +02:00
<varlistentry>
2002-09-14 20:35:46 +02:00
<term><replaceable>authentication-method</replaceable></term>
2000-06-18 23:24:54 +02:00
<listitem>
<para>
2002-04-04 06:25:54 +02:00
Specifies the authentication method to use when connecting via
this record. The possible choices are summarized here; details
are in <xref linkend="auth-methods">.
2000-09-06 21:54:52 +02:00
<variablelist>
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>trust</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Allow the connection unconditionally. This method
2002-08-16 06:48:16 +02:00
allows anyone that can connect to the
2003-03-13 02:30:29 +01:00
<productname>PostgreSQL</productname> database server to login as
2002-08-16 06:48:16 +02:00
any <productname>PostgreSQL</productname> user they like,
2002-09-14 20:35:46 +02:00
without the need for a password. See <xref
linkend="auth-trust"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>reject</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Reject the connection unconditionally. This is useful for
2002-04-04 06:25:54 +02:00
<quote>filtering out</> certain hosts from a group.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2002-04-04 06:25:54 +02:00
<term><literal>md5</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Require the client to supply an MD5-encrypted password for
authentication.
2002-09-14 20:35:46 +02:00
See <xref linkend="auth-password"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2002-04-04 06:25:54 +02:00
<term><literal>crypt</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Require the client to supply a <function>crypt()</>-encrypted
password for authentication.
<literal>md5</literal> is preferred for 7.2 and later clients,
but pre-7.2 clients only support <literal>crypt</>.
2002-09-14 20:35:46 +02:00
See <xref linkend="auth-password"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
2001-08-16 18:24:16 +02:00
<varlistentry>
2002-04-04 06:25:54 +02:00
<term><literal>password</></term>
2001-08-16 18:24:16 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Require the client to supply an unencrypted password for
authentication.
Since the password is sent in clear text over the
network, this should not be used on untrusted networks.
2002-09-14 20:35:46 +02:00
See <xref linkend="auth-password"> for details.
2002-04-04 06:25:54 +02:00
</para>
2001-08-16 18:24:16 +02:00
</listitem>
</varlistentry>
2000-09-06 21:54:52 +02:00
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>krb4</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Use Kerberos V4 to authenticate the user. This is only
2002-09-14 20:35:46 +02:00
available for TCP/IP connections. See <xref
linkend="kerberos-auth"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>krb5</></term>
2000-09-06 21:54:52 +02:00
<listitem>
<para>
2004-12-27 00:06:56 +01:00
Use Kerberos V5 to authenticate the user. This is only
2002-09-14 20:35:46 +02:00
available for TCP/IP connections. See <xref
linkend="kerberos-auth"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>ident</></term>
2000-09-06 21:54:52 +02:00
<listitem>
2002-09-14 20:35:46 +02:00
<para>
Obtain the operating system user name of the client (for
TCP/IP connections by contacting the ident server on the
client, for local connections by getting it from the
operating system) and check if the user is allowed to
connect as the requested database user by consulting the map
specified after the <literal>ident</literal> key word.
2004-12-27 00:06:56 +01:00
See <xref linkend="auth-ident"> for details.
2000-09-06 21:54:52 +02:00
</para>
</listitem>
</varlistentry>
2001-09-06 05:23:38 +02:00
<varlistentry>
2001-09-10 01:52:12 +02:00
<term><literal>pam</></term>
2001-09-06 05:23:38 +02:00
<listitem>
<para>
2002-09-14 20:35:46 +02:00
Authenticate using the Pluggable Authentication Modules
(PAM) service provided by the operating system. See <xref
linkend="auth-pam"> for details.
2001-09-06 05:23:38 +02:00
</para>
</listitem>
</varlistentry>
2000-09-06 21:54:52 +02:00
</variablelist>
2000-06-18 23:24:54 +02:00
</para>
</listitem>
</varlistentry>
2002-09-14 20:35:46 +02:00
<varlistentry>
<term><replaceable>authentication-option</replaceable></term>
<listitem>
<para>
The meaning of this optional field depends on the chosen
2004-12-27 00:06:56 +01:00
authentication method. Details appear below.
2002-09-14 20:35:46 +02:00
</para>
</listitem>
</varlistentry>
2000-06-18 23:24:54 +02:00
</variablelist>
2001-11-02 19:39:57 +01:00
</para>
2000-06-18 23:24:54 +02:00
2001-11-02 19:39:57 +01:00
<para>
2004-12-27 20:19:24 +01:00
Files included by <literal>@</> constructs are read as lists of names,
which can be separated by either whitespace or commas. Comments are
introduced by <literal>#</literal>, just as in
<filename>pg_hba.conf</filename>, and nested <literal>@</> constructs are
allowed. Unless the file name following <literal>@</> is an absolute
path, it is taken to be relative to the directory containing the
referencing file.
</para>
<para>
2001-11-02 19:39:57 +01:00
Since the <filename>pg_hba.conf</filename> records are examined
2001-11-28 21:49:10 +01:00
sequentially for each connection attempt, the order of the records is
2002-04-04 06:25:54 +02:00
significant. Typically, earlier records will have tight connection
match parameters and weaker authentication methods, while later
records will have looser match parameters and stronger authentication
methods. For example, one might wish to use <literal>trust</>
2003-03-13 02:30:29 +01:00
authentication for local TCP/IP connections but require a password for
remote TCP/IP connections. In this case a record specifying
2002-04-04 06:25:54 +02:00
<literal>trust</> authentication for connections from 127.0.0.1 would
appear before a record specifying password authentication for a wider
range of allowed client IP addresses.
2000-06-18 23:24:54 +02:00
</para>
<para>
2002-04-04 06:25:54 +02:00
The <filename>pg_hba.conf</filename> file is read on start-up and when
2003-03-13 02:30:29 +01:00
the main server process (<command>postmaster</>) receives a
<systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
signal. If you edit the file on an
active system, you will need to signal the <command>postmaster</>
2002-04-04 06:25:54 +02:00
(using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
re-read the file.
2000-06-18 23:24:54 +02:00
</para>
<para>
2004-12-27 00:06:56 +01:00
Some examples of <filename>pg_hba.conf</filename> entries are shown in
2003-03-13 02:30:29 +01:00
<xref linkend="example-pg-hba.conf">. See the next section for details on the
2000-06-18 23:24:54 +02:00
different authentication methods.
2003-03-13 02:30:29 +01:00
</para>
2000-06-18 23:24:54 +02:00
<example id="example-pg-hba.conf">
2004-12-27 00:06:56 +01:00
<title>Example <filename>pg_hba.conf</filename> entries</title>
2000-06-18 23:24:54 +02:00
<programlisting>
2002-09-14 20:35:46 +02:00
# Allow any user on the local system to connect to any database under
# any user name using Unix-domain sockets (the default for local
# connections).
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all all trust
2002-09-14 20:35:46 +02:00
# The same using local loopback TCP/IP connections.
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 127.0.0.1/32 trust
2002-09-14 20:35:46 +02:00
2004-08-26 18:50:05 +02:00
# The same as the last line but using a separate netmask column
2003-08-17 06:39:11 +02:00
#
2004-12-27 00:06:56 +01:00
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
2004-08-26 18:50:05 +02:00
host all all 127.0.0.1 255.255.255.255 trust
2003-08-17 06:39:11 +02:00
2002-09-14 20:35:46 +02:00
# Allow any user from any host with IP address 192.168.93.x to connect
# to database "template1" as the same user name that ident reports for
# the connection (typically the Unix user name).
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host template1 all 192.168.93.0/24 ident sameuser
2002-09-14 20:35:46 +02:00
# Allow a user from host 192.168.12.10 to connect to database
# "template1" if the user's password is correctly supplied.
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host template1 all 192.168.12.10/32 md5
2002-09-14 20:35:46 +02:00
# In the absence of preceding "host" lines, these two lines will
# reject all connection from 192.168.54.1 (since that entry will be
2004-12-27 00:06:56 +01:00
# matched first), but allow Kerberos 5 connections from anywhere else
2002-09-14 20:35:46 +02:00
# on the Internet. The zero mask means that no bits of the host IP
# address are considered so it matches any host.
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 192.168.54.1/32 reject
host all all 0.0.0.0/0 krb5
2002-09-14 20:35:46 +02:00
# Allow users from 192.168.x.x hosts to connect to any database, if
# they pass the ident check. If, for example, ident says the user is
# "bryanh" and he requests to connect as PostgreSQL user "guest1", the
# connection is allowed if there is an entry in pg_ident.conf for map
# "omicron" that says "bryanh" is allowed to connect as "guest1".
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 192.168.0.0/16 ident omicron
2002-09-14 20:35:46 +02:00
# If these are the only three lines for local connections, they will
# allow local users to connect only to their own databases (databases
# with the same name as their user name) except for administrators and
# members of group "support" who may connect to all databases. The file
# $PGDATA/admins contains a list of user names. Passwords are required in
# all cases.
#
2004-08-26 18:50:05 +02:00
# TYPE DATABASE USER CIDR-ADDRESS METHOD
2004-11-10 22:54:23 +01:00
local sameuser all md5
local all @admins md5
local all +support md5
2002-09-14 20:35:46 +02:00
# The last two lines above can be combined into a single line:
2004-11-10 22:54:23 +01:00
local all @admins,+support md5
2002-09-14 20:35:46 +02:00
# The database column can also use lists and file names, but not groups:
2004-11-10 22:54:23 +01:00
local db1,db2,@demodbs all md5
2000-06-18 23:24:54 +02:00
</programlisting>
</example>
</sect1>
<sect1 id="auth-methods">
<title>Authentication methods</title>
<para>
2004-12-27 00:06:56 +01:00
The following subsections describe the authentication methods in more detail.
2000-06-18 23:24:54 +02:00
</para>
2001-12-27 22:37:34 +01:00
<sect2 id="auth-trust">
<title>Trust authentication</title>
<para>
When <literal>trust</> authentication is specified,
<productname>PostgreSQL</productname> assumes that anyone who can
2004-01-26 06:35:15 +01:00
connect to the server is authorized to access the database with
whatever database user they specify (including the database superuser).
2004-12-27 00:06:56 +01:00
Of course, restrictions made in the <literal>database</> and
<literal>user</> columns still apply.
This method should only be used when there is adequate
operating-system-level protection on connections to the server.
2001-12-27 22:37:34 +01:00
</para>
<para>
2003-01-19 01:13:31 +01:00
<literal>trust</> authentication is appropriate and very
convenient for local connections on a single-user workstation. It
is usually <emphasis>not</> appropriate by itself on a multiuser
machine. However, you may be able to use <literal>trust</> even
2003-03-13 02:30:29 +01:00
on a multiuser machine, if you restrict access to the server's
Unix-domain socket file using file-system permissions. To do this, set the
2001-12-27 22:37:34 +01:00
<varname>unix_socket_permissions</varname> (and possibly
2003-01-19 01:13:31 +01:00
<varname>unix_socket_group</varname>) configuration parameters as
2003-06-25 03:14:05 +02:00
described in <xref linkend="runtime-config-connection">. Or you
2003-01-19 01:13:31 +01:00
could set the <varname>unix_socket_directory</varname>
configuration parameter to place the socket file in a suitably
restricted directory.
2001-12-27 22:37:34 +01:00
</para>
<para>
2002-01-20 23:19:57 +01:00
Setting file-system permissions only helps for Unix-socket connections.
2003-03-13 02:30:29 +01:00
Local TCP/IP connections are not restricted by it; therefore, if you want
to use file-system permissions for local security, remove the <literal>host ...
2001-12-27 22:37:34 +01:00
127.0.0.1 ...</> line from <filename>pg_hba.conf</>, or change it to a
non-<literal>trust</> authentication method.
</para>
<para>
2003-03-13 02:30:29 +01:00
<literal>trust</> authentication is only suitable for TCP/IP connections
2001-12-27 22:37:34 +01:00
if you trust every user on every machine that is allowed to connect
2002-11-11 21:14:04 +01:00
to the server by the <filename>pg_hba.conf</> lines that specify
2001-12-27 22:37:34 +01:00
<literal>trust</>. It is seldom reasonable to use <literal>trust</>
2003-03-13 02:30:29 +01:00
for any TCP/IP connections other than those from <systemitem>localhost</> (127.0.0.1).
2001-12-27 22:37:34 +01:00
</para>
</sect2>
2001-11-12 20:19:39 +01:00
<sect2 id="auth-password">
2000-06-18 23:24:54 +02:00
<title>Password authentication</title>
2001-05-13 00:51:36 +02:00
<indexterm>
2002-04-04 06:25:54 +02:00
<primary>MD5</>
2001-05-13 00:51:36 +02:00
</indexterm>
2001-11-12 20:19:39 +01:00
<indexterm>
2002-04-04 06:25:54 +02:00
<primary>crypt</>
</indexterm>
<indexterm>
<primary>password</primary>
2003-08-31 19:32:24 +02:00
<secondary>authentication</secondary>
2001-11-12 20:19:39 +01:00
</indexterm>
2001-05-13 00:51:36 +02:00
2001-12-27 22:37:34 +01:00
<para>
2003-03-13 02:30:29 +01:00
The password-based authentication methods are <literal>md5</>,
2002-04-04 06:25:54 +02:00
<literal>crypt</>, and <literal>password</>. These methods operate
2001-12-27 22:37:34 +01:00
similarly except for the way that the password is sent across the
2004-12-27 00:06:56 +01:00
connection. But only <literal>md5</> supports encrypted
passwords stored in <structname>pg_shadow</structname>;
the other two require unencrypted passwords to be stored there.
</para>
<para>
If you are at all concerned about password
2002-04-04 06:25:54 +02:00
<quote>sniffing</> attacks then <literal>md5</> is preferred, with
<literal>crypt</> a second choice if you must support pre-7.2
clients. Plain <literal>password</> should especially be avoided for
2003-12-14 00:59:07 +01:00
connections over the open Internet (unless you use <acronym>SSL</acronym>, SSH, or
2002-04-04 06:25:54 +02:00
other communications security wrappers around the connection).
2001-12-27 22:37:34 +01:00
</para>
2000-06-18 23:24:54 +02:00
<para>
2002-04-04 06:25:54 +02:00
<productname>PostgreSQL</productname> database passwords are
2002-11-11 21:14:04 +01:00
separate from operating system user passwords. The password for
each database user is stored in the <literal>pg_shadow</> system
2004-12-27 00:06:56 +01:00
catalog table. Passwords can be managed with the SQL commands
<xref linkend="sql-createuser" endterm="sql-createuser-title"> and
<xref linkend="sql-alteruser" endterm="sql-alteruser-title">,
e.g., <userinput>CREATE USER foo WITH PASSWORD 'secret';</userinput>.
By default, that is, if no password has been set up, the stored password
is null and password authentication will always fail for that user.
2000-07-04 18:32:01 +02:00
</para>
2000-06-18 23:24:54 +02:00
</sect2>
2000-08-25 12:00:35 +02:00
<sect2 id="kerberos-auth">
2000-06-18 23:24:54 +02:00
<title>Kerberos authentication</title>
2001-05-13 00:51:36 +02:00
<indexterm zone="kerberos-auth">
<primary>Kerberos</primary>
</indexterm>
2000-06-18 23:24:54 +02:00
<para>
<productname>Kerberos</productname> is an industry-standard secure
2003-08-17 01:33:49 +02:00
authentication system suitable for distributed computing over a public
network. A description of the <productname>Kerberos</productname> system
2004-12-27 00:06:56 +01:00
is far beyond the scope of this document; in full generality it can be
2003-08-17 01:33:49 +02:00
quite complex (yet powerful). The <ulink
2002-09-14 20:35:46 +02:00
url="http://www.nrl.navy.mil/CCS/people/kenh/kerberos-faq.html">Kerberos
2003-08-17 01:33:49 +02:00
<acronym>FAQ</></ulink> or <ulink url="ftp://athena-dist.mit.edu">MIT
Project Athena</ulink> can be a good starting point for exploration.
Several sources for <productname>Kerberos</> distributions exist.
</para>
<para>
While <productname>PostgreSQL</> supports both Kerberos 4 and
Kerberos 5, only Kerberos 5 is recommended. Kerberos 4 is
considered insecure and no longer recommended for general
use.
2000-06-18 23:24:54 +02:00
</para>
<para>
In order to use <productname>Kerberos</>, support for it must be
2002-09-18 22:09:32 +02:00
enabled at build time. See <xref linkend="installation"> for more
information. Both Kerberos 4 and 5 are supported, but only one
version can be supported in any one build.
2000-06-18 23:24:54 +02:00
</para>
<para>
2001-11-21 06:53:41 +01:00
<productname>PostgreSQL</> operates like a normal Kerberos service.
2001-11-19 04:58:25 +01:00
The name of the service principal is
2003-03-13 02:30:29 +01:00
<literal><replaceable>servicename</>/<replaceable>hostname</>@<replaceable>realm</></literal>, where
2002-04-04 06:25:54 +02:00
<replaceable>servicename</> is <literal>postgres</literal> (unless a
different service name was selected at configure time with
<literal>./configure --with-krb-srvnam=whatever</>).
2003-03-13 02:30:29 +01:00
<replaceable>hostname</> is the fully qualified host name of the
2002-04-04 06:25:54 +02:00
server machine. The service principal's realm is the preferred realm
of the server machine.
2001-11-19 04:58:25 +01:00
</para>
<para>
2002-04-04 06:25:54 +02:00
Client principals must have their <productname>PostgreSQL</> user
name as their first component, for example
2003-03-13 02:30:29 +01:00
<literal>pgusername/otherstuff@realm</>. At present the realm of
2002-04-04 06:25:54 +02:00
the client is not checked by <productname>PostgreSQL</>; so if you
have cross-realm authentication enabled, then any principal in any
realm that can communicate with yours will be accepted.
2001-11-19 04:58:25 +01:00
</para>
<para>
2004-03-09 17:57:47 +01:00
Make sure that your server key file is readable (and preferably
only readable) by the <productname>PostgreSQL</productname> server
2004-12-27 00:06:56 +01:00
account. (See also <xref linkend="postgres-user">.) The location
2004-03-09 17:57:47 +01:00
of the key file is specified by the <xref
2004-12-27 00:06:56 +01:00
linkend="guc-krb-server-keyfile"> configuration
2004-03-09 17:57:47 +01:00
parameter. (See also <xref linkend="runtime-config">.) The default
is <filename>/etc/srvtab</> if you are using Kerberos 4 and
<filename>/usr/local/pgsql/etc/krb5.keytab</> (or whichever
2000-08-25 12:00:35 +02:00
directory was specified as <varname>sysconfdir</> at build time)
with Kerberos 5.
2000-06-18 23:24:54 +02:00
</para>
2000-07-15 23:35:47 +02:00
<para>
To generate the keytab file, use for example (with version 5)
<screen>
2000-09-06 21:54:52 +02:00
<prompt>kadmin% </><userinput>ank -randkey postgres/server.my.domain.org</>
<prompt>kadmin% </><userinput>ktadd -k krb5.keytab postgres/server.my.domain.org</>
2000-07-15 23:35:47 +02:00
</screen>
2001-09-10 01:52:12 +02:00
Read the <productname>Kerberos</> documentation for details.
2000-07-15 23:35:47 +02:00
</para>
<para>
2001-11-19 04:58:25 +01:00
When connecting to the database make sure you have a ticket for a
2002-04-04 06:25:54 +02:00
principal matching the requested database user name. An example: For
database user name <literal>fred</>, both principal
2001-11-19 04:58:25 +01:00
<literal>fred@EXAMPLE.COM</> and
2002-04-04 06:25:54 +02:00
<literal>fred/users.example.com@EXAMPLE.COM</> can be used to
authenticate to the database server.
2000-07-15 23:35:47 +02:00
</para>
<para>
2003-06-06 20:09:17 +02:00
If you use <application>mod_auth_kerb</application> from
<ulink url="http://modauthkerb.sf.net">http://modauthkerb.sf.net</ulink>
and <application>mod_perl</application> on your
2002-04-04 06:25:54 +02:00
<productname>Apache</productname> web server, you can use
<literal>AuthType KerberosV5SaveCredentials</literal> with a
<application>mod_perl</application> script. This gives secure
database access over the web, no extra passwords required.
2000-07-15 23:35:47 +02:00
</para>
2000-06-18 23:24:54 +02:00
</sect2>
2002-09-14 20:35:46 +02:00
<sect2 id="auth-ident">
2000-06-18 23:24:54 +02:00
<title>Ident-based authentication</title>
2001-05-13 00:51:36 +02:00
<indexterm>
<primary>ident</primary>
</indexterm>
2002-09-14 20:35:46 +02:00
<para>
2004-12-27 00:06:56 +01:00
The ident authentication method works by obtaining the client's
2002-09-14 20:35:46 +02:00
operating system user name and determining the allowed database
2004-12-27 00:06:56 +01:00
user names using a map file that lists the permitted
corresponding pairs of names. The determination of the client's
2002-09-14 20:35:46 +02:00
user name is the security-critical point, and it works differently
depending on the connection type.
</para>
<sect3>
<title>Ident Authentication over TCP/IP</title>
2000-06-18 23:24:54 +02:00
<para>
The <quote>Identification Protocol</quote> is described in
<citetitle>RFC 1413</citetitle>. Virtually every Unix-like
2001-03-15 21:01:32 +01:00
operating system ships with an ident server that listens on TCP
2000-07-04 18:32:01 +02:00
port 113 by default. The basic functionality of an ident server
2000-06-18 23:24:54 +02:00
is to answer questions like <quote>What user initiated the
connection that goes out of your port <replaceable>X</replaceable>
and connects to my port <replaceable>Y</replaceable>?</quote>.
2001-11-21 06:53:41 +01:00
Since <productname>PostgreSQL</> knows both <replaceable>X</> and
2000-07-04 18:32:01 +02:00
<replaceable>Y</> when a physical connection is established, it
can interrogate the ident server on the host of the connecting
client and could theoretically determine the operating system user
for any given connection this way.
2000-06-18 23:24:54 +02:00
</para>
<para>
The drawback of this procedure is that it depends on the integrity
of the client: if the client machine is untrusted or compromised
an attacker could run just about any program on port 113 and
return any user name he chooses. This authentication method is
therefore only appropriate for closed networks where each client
machine is under tight control and where the database and system
2001-10-05 00:27:18 +02:00
administrators operate in close contact. In other words, you must
trust the machine running the ident server.
Heed the warning:
2000-06-18 23:24:54 +02:00
<blockquote>
<attribution>RFC 1413</attribution>
<para>
The Identification Protocol is not intended as an authorization
2001-07-11 23:27:07 +02:00
or access control protocol.
2000-06-18 23:24:54 +02:00
</para>
</blockquote>
</para>
2002-09-14 20:35:46 +02:00
</sect3>
<sect3>
<title>Ident Authentication over Local Sockets</title>
2000-06-18 23:24:54 +02:00
2001-08-02 01:25:39 +02:00
<para>
2002-04-04 06:25:54 +02:00
On systems supporting <symbol>SO_PEERCRED</symbol> requests for
2002-09-14 20:35:46 +02:00
Unix-domain sockets (currently <systemitem
class="osname">Linux</>, <systemitem class="osname">FreeBSD</>,
2003-02-13 06:47:46 +01:00
<systemitem class="osname">NetBSD</>, <systemitem class=osname>OpenBSD</>,
and <systemitem class="osname">BSD/OS</>), ident authentication can also
be applied to local connections. In this case, no security risk is added by
2002-09-14 20:35:46 +02:00
using ident authentication; indeed it is a preferable choice for
local connections on such systems.
2001-08-02 01:25:39 +02:00
</para>
2002-09-14 20:35:46 +02:00
<para>
On systems without <symbol>SO_PEERCRED</> requests, ident
authentication is only available for TCP/IP connections. As a
2004-12-27 00:06:56 +01:00
work-around, it is possible to specify the <systemitem
2002-09-14 20:35:46 +02:00
class="systemname">localhost</> address <systemitem
class="systemname">127.0.0.1</> and make connections to this
2004-12-27 00:06:56 +01:00
address. This method is trustworthy to the extent that you trust
the local ident server.
2002-09-14 20:35:46 +02:00
</para>
</sect3>
2004-12-27 00:06:56 +01:00
<sect3 id="auth-ident-maps">
2002-09-14 20:35:46 +02:00
<title>Ident Maps</title>
2000-06-18 23:24:54 +02:00
<para>
When using ident-based authentication, after having determined the
2001-08-02 01:25:39 +02:00
name of the operating system user that initiated the connection,
2002-04-04 06:25:54 +02:00
<productname>PostgreSQL</productname> checks whether that user is
allowed to connect as the database user he is requesting to connect
as. This is controlled by the ident map argument that follows the
2003-03-13 02:30:29 +01:00
<literal>ident</> key word in the <filename>pg_hba.conf</filename>
2002-04-04 06:25:54 +02:00
file. There is a predefined ident map <literal>sameuser</literal>,
which allows any operating system user to connect as the database
user of the same name (if the latter exists). Other maps must be
created manually.
2000-06-18 23:24:54 +02:00
</para>
<para>
2004-12-27 00:06:56 +01:00
Ident maps other than <literal>sameuser</literal> are defined in the
ident map file, which by default is named
<filename>pg_ident.conf</><indexterm><primary>pg_ident.conf</primary></indexterm>
and is stored in the
cluster's data directory. (It is possible to place the map file
elsewhere, however; see the <xref linkend="guc-ident-file">
configuration parameter.)
The ident map file contains lines of the general form:
2000-06-18 23:24:54 +02:00
<synopsis>
<replaceable>map-name</> <replaceable>ident-username</> <replaceable>database-username</>
</synopsis>
2004-12-27 00:06:56 +01:00
Comments and whitespace are handled in the same way as in
<filename>pg_hba.conf</>. The
2002-04-04 06:25:54 +02:00
<replaceable>map-name</> is an arbitrary name that will be used to
refer to this mapping in <filename>pg_hba.conf</filename>. The other
two fields specify which operating system user is allowed to connect
as which database user. The same <replaceable>map-name</> can be
used repeatedly to specify more user-mappings within a single map.
There is no restriction regarding how many database users a given
2004-12-27 00:06:56 +01:00
operating system user may correspond to, nor vice versa.
2000-06-18 23:24:54 +02:00
</para>
2001-11-02 19:39:57 +01:00
<para>
2002-04-04 06:25:54 +02:00
The <filename>pg_ident.conf</filename> file is read on start-up and
2003-03-13 02:30:29 +01:00
when the main server process (<command>postmaster</>) receives a
<systemitem>SIGHUP</systemitem><indexterm><primary>SIGHUP</primary></indexterm>
signal. If you edit the file on an
active system, you will need to signal the <command>postmaster</>
2002-04-04 06:25:54 +02:00
(using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
re-read the file.
2001-11-02 19:39:57 +01:00
</para>
2000-06-18 23:24:54 +02:00
<para>
A <filename>pg_ident.conf</filename> file that could be used in
conjunction with the <filename>pg_hba.conf</> file in <xref
linkend="example-pg-hba.conf"> is shown in <xref
2000-11-21 21:44:32 +01:00
linkend="example-pg-ident.conf">. In this example setup, anyone
2002-04-04 06:25:54 +02:00
logged in to a machine on the 192.168 network that does not have the
2003-03-13 02:30:29 +01:00
Unix user name <literal>bryanh</>, <literal>ann</>, or
<literal>robert</> would not be granted access. Unix user
<literal>robert</> would only be allowed access when he tries to
connect as <productname>PostgreSQL</> user <literal>bob</>, not
as <literal>robert</> or anyone else. <literal>ann</> would
only be allowed to connect as <literal>ann</>. User
<literal>bryanh</> would be allowed to connect as either
<literal>bryanh</> himself or as <literal>guest1</>.
2000-06-18 23:24:54 +02:00
</para>
<example id="example-pg-ident.conf">
<title>An example <filename>pg_ident.conf</> file</title>
<programlisting>
2002-09-14 20:35:46 +02:00
# MAPNAME IDENT-USERNAME PG-USERNAME
2000-06-18 23:24:54 +02:00
2002-09-14 20:35:46 +02:00
omicron bryanh bryanh
omicron ann ann
2002-04-04 06:25:54 +02:00
# bob has user name robert on these machines
2002-09-14 20:35:46 +02:00
omicron robert bob
2000-09-06 21:54:52 +02:00
# bryanh can also connect as guest1
2002-09-14 20:35:46 +02:00
omicron bryanh guest1
2000-06-18 23:24:54 +02:00
</programlisting>
</example>
2002-09-14 20:35:46 +02:00
</sect3>
</sect2>
<sect2 id="auth-pam">
2004-12-27 00:06:56 +01:00
<title>PAM authentication</title>
2002-09-14 20:35:46 +02:00
2003-08-31 19:32:24 +02:00
<indexterm zone="auth-pam">
<primary>PAM</primary>
</indexterm>
2002-09-14 20:35:46 +02:00
<para>
2003-03-13 02:30:29 +01:00
This authentication method operates similarly to
<literal>password</literal> except that it uses PAM (Pluggable
2002-09-14 20:35:46 +02:00
Authentication Modules) as the authentication mechanism. The
default PAM service name is <literal>postgresql</literal>. You can
2004-12-27 00:06:56 +01:00
optionally supply your own service name after the <literal>pam</>
key word in the file <filename>pg_hba.conf</filename>.
For more information about PAM, please read the <ulink
2002-09-14 20:35:46 +02:00
url="http://www.kernel.org/pub/linux/libs/pam/"><productname>Linux-PAM</>
Page</ulink> and the <ulink
url="http://www.sun.com/software/solaris/pam/"><systemitem
class="osname">Solaris</> PAM Page</ulink>.
</para>
2000-06-18 23:24:54 +02:00
</sect2>
</sect1>
<sect1 id="client-authentication-problems">
<title>Authentication problems</title>
<para>
Genuine authentication failures and related problems generally
manifest themselves through error messages like the following.
</para>
<para>
<ProgramListing>
2003-09-13 00:17:24 +02:00
FATAL: no pg_hba.conf entry for host "123.123.123.123", user "andym", database "testdb"
2000-06-18 23:24:54 +02:00
</ProgramListing>
2002-04-04 06:25:54 +02:00
This is what you are most likely to get if you succeed in contacting
the server, but it does not want to talk to you. As the message
suggests, the server refused the connection request because it found
no authorizing entry in its <filename>pg_hba.conf</filename>
2000-06-18 23:24:54 +02:00
configuration file.
</para>
<para>
<ProgramListing>
2003-09-13 00:17:24 +02:00
FATAL: Password authentication failed for user "andym"
2000-06-18 23:24:54 +02:00
</ProgramListing>
2002-04-04 06:25:54 +02:00
Messages like this indicate that you contacted the server, and it is
willing to talk to you, but not until you pass the authorization
method specified in the <filename>pg_hba.conf</filename> file. Check
the password you are providing, or check your Kerberos or ident
software if the complaint mentions one of those authentication
types.
2000-06-18 23:24:54 +02:00
</para>
<para>
<ProgramListing>
2003-09-13 00:17:24 +02:00
FATAL: user "andym" does not exist
2000-06-18 23:24:54 +02:00
</ProgramListing>
2001-11-28 21:49:10 +01:00
The indicated user name was not found.
2000-06-18 23:24:54 +02:00
</para>
<para>
<ProgramListing>
2003-09-13 00:17:24 +02:00
FATAL: database "testdb" does not exist
2000-06-18 23:24:54 +02:00
</ProgramListing>
2001-11-28 21:49:10 +01:00
The database you are trying to connect to does not exist. Note that
if you do not specify a database name, it defaults to the database
2000-06-18 23:24:54 +02:00
user name, which may or may not be the right thing.
</para>
2001-08-02 01:25:39 +02:00
2003-09-13 00:17:24 +02:00
<tip>
2001-08-02 01:25:39 +02:00
<para>
2003-09-13 00:17:24 +02:00
The server log may contain more information about an
2002-04-04 06:25:54 +02:00
authentication failure than is reported to the client. If you are
confused about the reason for a failure, check the log.
2001-08-02 01:25:39 +02:00
</para>
2003-09-13 00:17:24 +02:00
</tip>
2000-06-18 23:24:54 +02:00
</sect1>
</chapter>
2004-08-26 18:50:05 +02:00