]> granicus.if.org Git - postgresql/blob - doc/src/sgml/client-auth.sgml
Add OpenBSD local indent credentials, from William Ahern.
[postgresql] / doc / src / sgml / client-auth.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/client-auth.sgml,v 1.42 2002/12/03 21:50:44 momjian Exp $
3 -->
4
5 <chapter id="client-authentication">
6  <title>Client Authentication</title>
7
8  <indexterm zone="client-authentication">
9   <primary>client authentication</primary>
10  </indexterm>
11
12  <para>
13   When a client application connects to the database server, it
14   specifies which <productname>PostgreSQL</productname> user name it
15   wants to connect as, much the same way one logs into a Unix computer
16   as a particular user. Within the SQL environment the active database
17   user name determines access privileges to database objects -- see
18   <xref linkend="user-manag"> for more information. Therefore, it is
19   essential to restrict which database users can connect.
20  </para>
21
22  <para>
23   <firstterm>Authentication</firstterm> is the process by which the
24   database server establishes the identity of the client, and by
25   extension determines whether the client application (or the user
26   who runs the client application) is permitted to connect with the
27   user name that was requested.
28  </para>
29
30  <para>
31   <productname>PostgreSQL</productname> offers a number of different
32   client authentication methods. The method used to authenticate a
33   particular client connection can be selected on the basis of
34   (client) host address, database, and user.
35  </para>
36
37  <para>
38   <productname>PostgreSQL</productname> user names are logically
39   separate from user names of the operating system in which the server
40   runs. If all the users of a particular server also have accounts on
41   the server's machine, it makes sense to assign database user names
42   that match their operating system user names. However, a server that
43   accepts remote connections may have many users who have no local
44   account, and in such cases there need be no connection between
45   database user names and OS user names.
46  </para>
47
48  <sect1 id="auth-pg-hba-conf">
49   <title>The <filename>pg_hba.conf</filename> file</title>
50
51   <indexterm zone="auth-pg-hba-conf">
52    <primary>pg_hba.conf</primary>
53   </indexterm>
54
55   <para>
56    Client authentication is controlled by the file
57    <filename>pg_hba.conf</filename> in the data directory, e.g.,
58    <filename>/usr/local/pgsql/data/pg_hba.conf</filename>.
59    (<acronym>HBA</> stands for host-based authentication.) A default
60    <filename>pg_hba.conf</filename> file is installed when the data
61    directory is initialized by <command>initdb</command>.
62   </para>
63
64   <para>
65    The general format of the <filename>pg_hba.conf</filename> file is
66    a set of records, one per line. Blank lines are ignored, as is any
67    text after the <quote>#</quote> comment character. A record is made
68    up of a number of fields which are separated by spaces and/or tabs.
69    Fields can contain white space if the field value is quoted. Records
70    cannot be continued across lines.
71   </para>
72
73   <para>
74    Each record specifies a connection type, a client IP address range
75    (if relevant for the connection type), a database name, a user name,
76    and the authentication method to be used for connections matching
77    these parameters. The first record with a matching connection type,
78    client address, requested database, and user name is used to perform
79    authentication. There is no <quote>fall-through</> or
80    <quote>backup</>: if one record is chosen and the authentication
81    fails, subsequent records are not considered. If no record matches,
82    access is denied.
83   </para>
84
85   <para>
86    A record may have one of the three formats
87    <synopsis>
88 local   <replaceable>database</replaceable>  <replaceable>user</replaceable>  <replaceable>authentication-method</replaceable>  <optional><replaceable>authentication-option</replaceable></optional>
89 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>
90 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>
91     </synopsis>
92    The meaning of the fields is as follows:
93
94    <variablelist>
95     <varlistentry>
96      <term><literal>local</literal></term>
97      <listitem>
98       <para>
99        This record matches connection attempts using Unix domain
100        sockets.  Without a record of this type, Unix-domain socket
101        connections are disallowed
102       </para>
103      </listitem>
104     </varlistentry>
105
106     <varlistentry>
107      <term><literal>host</literal></term>
108      <listitem>
109       <para>
110        This record matches connection attempts using TCP/IP networks.
111        Note that TCP/IP connections are disabled unless the server is
112        started with the <option>-i</option> option or the
113        <literal>tcpip_socket</> <filename>postgresql.conf</>
114        configuration parameter is enabled.
115       </para>
116      </listitem>
117     </varlistentry>
118
119     <varlistentry>
120      <term><literal>hostssl</literal></term>
121      <listitem>
122       <para>
123        This record matches connection attempts using SSL over TCP/IP.
124        <literal>host</literal> records will match either SSL or
125        non-SSL connection attempts, but <literal>hostssl</literal>
126        records require SSL connections.
127       </para>
128
129       <para>
130        To be able make use of this option the server must be built
131        with SSL support enabled. Furthermore, SSL must be enabled by
132        enabling the option <literal>ssl</literal> in
133        <filename>postgresql.conf</filename> (see <xref
134        linkend="runtime-config">).
135       </para>
136      </listitem>
137     </varlistentry>
138
139     <varlistentry>
140      <term><replaceable>database</replaceable></term>
141      <listitem>
142       <para>
143        Specifies which databases this record matches.  The value
144        <literal>all</literal> specifies that it matches all databases.
145        The value <literal>sameuser</> specifies that the record
146        matches if the requested database has the same name as the
147        requested user.  The value <literal>samegroup</> specifies that
148        the requested user must a member of the group with the same
149        name as the requested database.  Otherwise, this is the name of
150        a specific <productname>PostgreSQL</productname> database.
151        Multiple database names can be supplied by separating them with
152        commas.  A file containing database names can be specified by
153        preceding the file name with <literal>@</>. The file must be in
154        the same directory as <filename>pg_hba.conf</>.
155       </para>
156      </listitem>
157     </varlistentry>
158
159     <varlistentry>
160      <term><replaceable>user</replaceable></term>
161      <listitem>
162       <para>
163        Specifies which <productname>PostgreSQL</> users this record
164        matches. The value <literal>all</literal> specifies that it
165        matches all users.  Otherwise, this is the name of a specific
166        <productname>PostgreSQL</productname> user. Multiple user names
167        can be supplied by separating them with commas. Group names can
168        be specified by preceding the group name with <literal>+</>. A
169        file containing user names can be specified by preceding the
170        file name with <literal>@</>. The file must be in the same
171        directory as <filename>pg_hba.conf</>.
172       </para>
173      </listitem>
174     </varlistentry>
175
176     <varlistentry>
177      <term><replaceable>IP-address</replaceable></term>
178      <term><replaceable>IP-mask</replaceable></term>
179      <listitem>
180       <para>
181        These two fields contain IP address/mask values in standard
182        dotted decimal notation. (IP addresses can only be specified
183        numerically, not as domain or host names.)  Taken together they
184        specify the client machine IP addresses that this record
185        matches.  The precise logic is that
186        <blockquote>
187         <informalfigure>
188          <programlisting>(<replaceable>actual-IP-address</replaceable> xor <replaceable>IP-address-field</replaceable>) and <replaceable>IP-mask-field</replaceable></programlisting>
189         </informalfigure>
190        </blockquote>
191        must be zero for the record to match.  (Of course IP addresses
192        can be spoofed but this consideration is beyond the scope of
193        <productname>PostgreSQL</productname>.)
194       </para>
195
196       <para>
197        These fields only apply to <literal>host</literal> and
198        <literal>hostssl</literal> records.
199       </para>
200      </listitem>
201     </varlistentry>
202
203     <varlistentry>
204      <term><replaceable>authentication-method</replaceable></term>
205      <listitem>
206       <para>
207        Specifies the authentication method to use when connecting via
208        this record. The possible choices are summarized here; details
209        are in <xref linkend="auth-methods">.
210
211        <variablelist>
212         <varlistentry>
213          <term><literal>trust</></term>
214          <listitem>
215          <para>
216           The connection is allowed unconditionally. This method
217           allows anyone that can connect to the
218           <productname>PostgreSQL</productname> database to login as
219           any <productname>PostgreSQL</productname> user they like,
220           without the need for a password.  See <xref
221           linkend="auth-trust"> for details.
222          </para>
223         </listitem>
224        </varlistentry>
225
226        <varlistentry>
227         <term><literal>reject</></term>
228         <listitem>
229          <para>
230           The connection is rejected unconditionally. This is useful for
231           <quote>filtering out</> certain hosts from a group.
232          </para>
233         </listitem>
234        </varlistentry>
235
236        <varlistentry>
237         <term><literal>md5</></term>
238         <listitem>
239          <para>
240           Requires the client to supply an MD5 encrypted password for
241           authentication. This is the only method that allows encrypted
242           passwords to be stored in <structname>pg_shadow</structname>.
243           See <xref linkend="auth-password"> for details.
244          </para>
245         </listitem>
246        </varlistentry>
247
248        <varlistentry>
249         <term><literal>crypt</></term>
250         <listitem>
251          <para>
252           Like <literal>md5</literal> method but uses older crypt
253           encryption, which is needed for pre-7.2 clients.
254           <literal>md5</literal> is preferred for 7.2 and later clients.
255           See <xref linkend="auth-password"> for details.
256          </para>
257         </listitem>
258        </varlistentry>
259
260        <varlistentry>
261         <term><literal>password</></term>
262         <listitem>
263          <para>
264           Same as "md5", but the password is sent in clear text over the
265           network. This should not be used on untrusted networks.
266           See <xref linkend="auth-password"> for details.
267          </para>
268         </listitem>
269        </varlistentry>
270
271        <varlistentry>
272         <term><literal>krb4</></term>
273         <listitem>
274          <para>
275           Kerberos V4 is used to authenticate the user. This is only
276           available for TCP/IP connections.  See <xref
277           linkend="kerberos-auth"> for details.
278          </para>
279         </listitem>
280        </varlistentry>
281
282        <varlistentry>
283         <term><literal>krb5</></term>
284         <listitem>
285          <para>
286           Kerberos V5 is used to authenticate the user. This is only
287           available for TCP/IP connections.  See <xref
288           linkend="kerberos-auth"> for details.
289          </para>
290         </listitem>
291        </varlistentry>
292
293        <varlistentry>
294         <term><literal>ident</></term>
295         <listitem>
296          <para>
297           Obtain the operating system user name of the client (for
298           TCP/IP connections by contacting the ident server on the
299           client, for local connections by getting it from the
300           operating system) and check if the user is allowed to
301           connect as the requested database user by consulting the map
302           specified after the <literal>ident</literal> key word.
303          </para>
304
305          <para>
306           If you use the map <literal>sameuser</literal>, the user
307           names are assumed to be identical. If not, the map name is
308           looked up in the file <filename>pg_ident.conf</filename>
309           in the same directory as <filename>pg_hba.conf</filename>.
310           The connection is accepted if that file contains an
311           entry for this map name with the ident-supplied user name
312           and the requested <productname>PostgreSQL</productname> user
313           name.
314          </para>
315
316          <para>
317           For local connections, this only works on machines that
318           support Unix-domain socket credentials (currently
319           <systemitem class=osname>Linux</>, <systemitem
320           class=osname>FreeBSD</>, <systemitem class=osname>NetBSD</>,
321           <systemitem class=osname>OpenBSD</>, and 
322           <systemitem class=osname>BSD/OS</>).
323          </para>
324
325          <para>
326           See <xref linkend="auth-ident"> below for details.
327          </para>
328         </listitem>
329        </varlistentry>
330
331        <varlistentry>
332         <term><literal>pam</></term>
333         <listitem>
334          <para>
335           Authenticate using the Pluggable Authentication Modules
336           (PAM) service provided by the operating system.  See <xref
337           linkend="auth-pam"> for details.
338          </para>
339         </listitem>
340        </varlistentry>
341       </variablelist>
342
343       </para>
344      </listitem>
345     </varlistentry>
346
347     <varlistentry>
348      <term><replaceable>authentication-option</replaceable></term>
349      <listitem>
350       <para>
351        The meaning of this optional field depends on the chosen
352        authentication method and is described in the next section.
353       </para>
354      </listitem>
355     </varlistentry>
356    </variablelist>
357   </para>
358
359   <para>
360    Since the <filename>pg_hba.conf</filename> records are examined
361    sequentially for each connection attempt, the order of the records is
362    significant. Typically, earlier records will have tight connection
363    match parameters and weaker authentication methods, while later
364    records will have looser match parameters and stronger authentication
365    methods. For example, one might wish to use <literal>trust</>
366    authentication for local TCP connections but require a password for
367    remote TCP connections. In this case a record specifying
368    <literal>trust</> authentication for connections from 127.0.0.1 would
369    appear before a record specifying password authentication for a wider
370    range of allowed client IP addresses.
371   </para>
372
373   <important>
374    <para>
375     Do not prevent the superuser from accessing the template1
376     database.  Various utility commands need access to template1.
377    </para>
378   </important>
379
380   <para>
381     <indexterm>
382      <primary>SIGHUP</primary>
383     </indexterm>
384    The <filename>pg_hba.conf</filename> file is read on start-up and when
385    the <application>postmaster</> receives a
386    <systemitem>SIGHUP</systemitem> signal. If you edit the file on an
387    active system, you will need to signal the <application>postmaster</>
388    (using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
389    re-read the file.
390   </para>
391
392   <para>
393    An example of a <filename>pg_hba.conf</filename> file is shown in
394    <xref linkend="example-pg-hba.conf">. See below for details on the
395    different authentication methods.
396
397    <example id="example-pg-hba.conf">
398     <title>An example <filename>pg_hba.conf</filename> file</title>
399 <programlisting>
400 # Allow any user on the local system to connect to any database under
401 # any user name using Unix-domain sockets (the default for local
402 # connections).
403 #
404 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
405 local   all         all                                             trust
406
407 # The same using local loopback TCP/IP connections.
408 #
409 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
410 host    all         all         127.0.0.1         255.255.255.255   trust     
411
412 # Allow any user from any host with IP address 192.168.93.x to connect
413 # to database "template1" as the same user name that ident reports for
414 # the connection (typically the Unix user name).
415
416 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
417 host    template1   all         192.168.93.0      255.255.255.0     ident sameuser
418
419 # Allow a user from host 192.168.12.10 to connect to database
420 # "template1" if the user's password is correctly supplied.
421
422 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
423 host    template1   all         192.168.12.10     255.255.255.255   md5
424
425 # In the absence of preceding "host" lines, these two lines will
426 # reject all connection from 192.168.54.1 (since that entry will be
427 # matched first), but allow Kerberos V connections from anywhere else
428 # on the Internet.  The zero mask means that no bits of the host IP
429 # address are considered so it matches any host.
430
431 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
432 host    all         all         192.168.54.1      255.255.255.255   reject
433 host    all         all         0.0.0.0           0.0.0.0           krb5
434
435 # Allow users from 192.168.x.x hosts to connect to any database, if
436 # they pass the ident check.  If, for example, ident says the user is
437 # "bryanh" and he requests to connect as PostgreSQL user "guest1", the
438 # connection is allowed if there is an entry in pg_ident.conf for map
439 # "omicron" that says "bryanh" is allowed to connect as "guest1".
440 #
441 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
442 host    all         all         192.168.0.0       255.255.0.0       ident omicron
443
444 # If these are the only three lines for local connections, they will
445 # allow local users to connect only to their own databases (databases
446 # with the same name as their user name) except for administrators and
447 # members of group "support" who may connect to all databases.  The file
448 # $PGDATA/admins contains a list of user names.  Passwords are required in
449 # all cases.
450 #
451 # TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
452 local   sameuser    all                                             md5
453 local   all         @admins                                         md5
454 local   all         +support                                        md5
455
456 # The last two lines above can be combined into a single line:
457 local   all         @admins,+support                                md5
458
459 # The database column can also use lists and file names, but not groups:
460 local   db1,db2,@demodbs  all                                       md5
461 </programlisting>
462    </example>
463   </para>
464  </sect1>
465
466  <sect1 id="auth-methods">
467   <title>Authentication methods</title>
468   <para>
469    The following describes the authentication methods in more detail.
470   </para>
471
472   <sect2 id="auth-trust">
473    <title>Trust authentication</title>
474
475    <para>
476     When <literal>trust</> authentication is specified,
477     <productname>PostgreSQL</productname> assumes that anyone who can
478     connect to the server is authorized to access the database as
479     whatever database user he specifies (including the database superuser).
480     This method should only be used when there is adequate system-level
481     protection on connections to the postmaster port.
482    </para>
483
484    <para>
485     <literal>trust</> authentication is appropriate and very convenient
486     for local connections on a single-user workstation.  It is usually
487     <emphasis>not</> appropriate by itself on a multiuser machine.
488     However, you may be able to use <literal>trust</> even on a multiuser
489     machine, if you restrict access to the postmaster's socket file using
490     file-system permissions.  To do this, set the parameter
491     <varname>unix_socket_permissions</varname> (and possibly
492     <varname>unix_socket_group</varname>) in <filename>postgresql.conf</>,
493     as described in <xref linkend="runtime-config-general">.  Or you could
494     set <varname>unix_socket_directory</varname> to place the socket file
495     in a suitably restricted directory.
496    </para>
497
498    <para>
499     Setting file-system permissions only helps for Unix-socket connections.
500     Local TCP connections are not restricted by it; therefore, if you want
501     to use permissions for local security, remove the <literal>host ...
502     127.0.0.1 ...</> line from <filename>pg_hba.conf</>, or change it to a
503     non-<literal>trust</> authentication method.
504    </para>
505
506    <para>
507     <literal>trust</> authentication is only suitable for TCP connections
508     if you trust every user on every machine that is allowed to connect
509     to the server by the <filename>pg_hba.conf</> lines that specify
510     <literal>trust</>.  It is seldom reasonable to use <literal>trust</>
511     for any TCP connections other than those from <systemitem>localhost</> (127.0.0.1).
512    </para>
513
514   </sect2>
515
516   <sect2 id="auth-password">
517    <title>Password authentication</title>
518
519    <indexterm>
520     <primary>MD5</>
521    </indexterm>
522    <indexterm>
523     <primary>crypt</>
524    </indexterm>
525    <indexterm>
526     <primary>password</primary>
527    </indexterm>
528
529    <para>
530     Password-based authentication methods include <literal>md5</>,
531     <literal>crypt</>, and <literal>password</>. These methods operate
532     similarly except for the way that the password is sent across the
533     connection. If you are at all concerned about password
534     <quote>sniffing</> attacks then <literal>md5</> is preferred, with
535     <literal>crypt</> a second choice if you must support pre-7.2
536     clients. Plain <literal>password</> should especially be avoided for
537     connections over the open Internet (unless you use SSL, SSH, or
538     other communications security wrappers around the connection).
539    </para>
540
541    <para>
542     <productname>PostgreSQL</productname> database passwords are
543     separate from operating system user passwords. The password for
544     each database user is stored in the <literal>pg_shadow</> system
545     catalog table. Passwords can be managed with the query language
546     commands <command>CREATE USER</command> and <command>ALTER
547     USER</command>, e.g., <userinput>CREATE USER foo WITH PASSWORD
548     'secret';</userinput>. By default, that is, if no password has
549     been set up, the stored password is null and
550     password authentication will always fail for that user.
551    </para>
552
553    <para>
554     To restrict the set of users that are allowed to connect to certain
555     databases, list the users separated by commas, or in a separate
556     file. The file should contain user names separated by commas or one
557     user name per line, and be in the same directory as
558     <filename>pg_hba.conf</>. Mention the (base) name of the file
559     preceded with <literal>@</> in the user column. The
560     database column can similarly accept a list of values or
561     a file name. You can also specify group names by preceding the group
562     name with <literal>+</>.
563    </para>
564
565   </sect2>
566
567   <sect2 id="kerberos-auth">
568    <title>Kerberos authentication</title>
569
570    <indexterm zone="kerberos-auth">
571     <primary>Kerberos</primary>
572    </indexterm>
573
574    <para>
575     <productname>Kerberos</productname> is an industry-standard secure
576     authentication system suitable for distributed computing over a
577     public network. A description of the
578     <productname>Kerberos</productname> system is far beyond the scope
579     of this document; in all generality it can be quite complex (yet
580     powerful). The <ulink
581     url="http://www.nrl.navy.mil/CCS/people/kenh/kerberos-faq.html">Kerberos
582     <acronym>FAQ</></ulink> or <ulink
583     url="ftp://athena-dist.mit.edu">MIT Project Athena</ulink> can be
584     a good starting point for exploration. Several sources for
585     <productname>Kerberos</> distributions exist.
586    </para>
587
588    <para>
589     In order to use <productname>Kerberos</>, support for it must be
590     enabled at build time.  See <xref linkend="installation"> for more
591     information.  Both Kerberos 4 and 5 are supported, but only one
592     version can be supported in any one build.
593    </para>
594
595    <para>
596     <productname>PostgreSQL</> operates like a normal Kerberos service.
597     The name of the service principal is
598     <replaceable>servicename/hostname@realm</>, where
599     <replaceable>servicename</> is <literal>postgres</literal> (unless a
600     different service name was selected at configure time with
601     <literal>./configure --with-krb-srvnam=whatever</>).
602     <replaceable>hostname</> is the fully qualified domain name of the
603     server machine. The service principal's realm is the preferred realm
604     of the server machine.
605    </para>
606
607    <para>
608     Client principals must have their <productname>PostgreSQL</> user
609     name as their first component, for example
610     <replaceable>pgusername/otherstuff@realm</>. At present the realm of
611     the client is not checked by <productname>PostgreSQL</>; so if you
612     have cross-realm authentication enabled, then any principal in any
613     realm that can communicate with yours will be accepted.
614    </para>
615
616    <para>
617     Make sure that your server key file is readable (and preferably only
618     readable) by the <productname>PostgreSQL</productname> server
619     account (see <xref linkend="postgres-user">). The location of the
620     key file is specified with the <varname>krb_server_keyfile</> run
621     time configuration parameter. (See also <xref
622     linkend="runtime-config">.) The default is <filename>/etc/srvtab</>
623     if you are using Kerberos 4 and
624     <filename>FILE:/usr/local/pgsql/etc/krb5.keytab</> (or whichever
625     directory was specified as <varname>sysconfdir</> at build time)
626     with Kerberos 5.
627    </para>
628
629    <para>
630     To generate the keytab file, use for example (with version 5)
631 <screen>
632 <prompt>kadmin% </><userinput>ank -randkey postgres/server.my.domain.org</>
633 <prompt>kadmin% </><userinput>ktadd -k krb5.keytab postgres/server.my.domain.org</>
634 </screen>
635     Read the <productname>Kerberos</> documentation for details.
636    </para>
637
638    <para>
639     When connecting to the database make sure you have a ticket for a
640     principal matching the requested database user name. An example: For
641     database user name <literal>fred</>, both principal
642     <literal>fred@EXAMPLE.COM</> and
643     <literal>fred/users.example.com@EXAMPLE.COM</> can be used to
644     authenticate to the database server.
645    </para>
646
647    <para>
648     If you use <application>mod_auth_krb</application> and
649     <application>mod_perl</application> on your
650     <productname>Apache</productname> web server, you can use
651     <literal>AuthType KerberosV5SaveCredentials</literal> with a
652     <application>mod_perl</application> script. This gives secure
653     database access over the web, no extra passwords required.
654    </para>
655
656   </sect2>
657
658   <sect2 id="auth-ident">
659    <title>Ident-based authentication</title>
660
661    <indexterm>
662     <primary>ident</primary>
663    </indexterm>
664
665    <para>
666     The ident authentication method works by inspecting the client's
667     operating system user name and determining the allowed database
668     user names by using a map file that lists the permitted
669     corresponding user name pairs.  The determination of the client's
670     user name is the security-critical point, and it works differently
671     depending on the connection type.
672    </para>
673
674    <sect3>
675     <title>Ident Authentication over TCP/IP</title>
676
677    <para>
678     The <quote>Identification Protocol</quote> is described in
679     <citetitle>RFC 1413</citetitle>. Virtually every Unix-like
680     operating system ships with an ident server that listens on TCP
681     port 113 by default. The basic functionality of an ident server
682     is to answer questions like <quote>What user initiated the
683     connection that goes out of your port <replaceable>X</replaceable>
684     and connects to my port <replaceable>Y</replaceable>?</quote>.
685     Since <productname>PostgreSQL</> knows both <replaceable>X</> and
686     <replaceable>Y</> when a physical connection is established, it
687     can interrogate the ident server on the host of the connecting
688     client and could theoretically determine the operating system user
689     for any given connection this way.
690    </para>
691
692    <para>
693     The drawback of this procedure is that it depends on the integrity
694     of the client: if the client machine is untrusted or compromised
695     an attacker could run just about any program on port 113 and
696     return any user name he chooses. This authentication method is
697     therefore only appropriate for closed networks where each client
698     machine is under tight control and where the database and system
699     administrators operate in close contact. In other words, you must
700     trust the machine running the ident server.
701     Heed the warning:
702     <blockquote>
703      <attribution>RFC 1413</attribution>
704      <para>
705       The Identification Protocol is not intended as an authorization
706       or access control protocol.
707      </para>
708     </blockquote>
709    </para>
710    </sect3>
711
712    <sect3>
713     <title>Ident Authentication over Local Sockets</title>
714
715    <para>
716     On systems supporting <symbol>SO_PEERCRED</symbol> requests for
717     Unix-domain sockets (currently <systemitem
718     class="osname">Linux</>, <systemitem class="osname">FreeBSD</>,
719     <systemitem class="osname">NetBSD</>, and <systemitem
720     class="osname">BSD/OS</>), ident authentication can also be applied
721     to local connections. In this case, no security risk is added by
722     using ident authentication; indeed it is a preferable choice for
723     local connections on such systems.
724    </para>
725
726     <para>
727      On systems without <symbol>SO_PEERCRED</> requests, ident
728      authentication is only available for TCP/IP connections. As a
729      work around, it is possible to specify the <systemitem
730      class="systemname">localhost</> address <systemitem
731      class="systemname">127.0.0.1</> and make connections to this
732      address.
733     </para>
734     </sect3>
735
736    <sect3>
737     <title>Ident Maps</title>
738
739    <para>
740     When using ident-based authentication, after having determined the
741     name of the operating system user that initiated the connection,
742     <productname>PostgreSQL</productname> checks whether that user is
743     allowed to connect as the database user he is requesting to connect
744     as. This is controlled by the ident map argument that follows the
745     <literal>ident</> keyword in the <filename>pg_hba.conf</filename>
746     file. There is a predefined ident map <literal>sameuser</literal>,
747     which allows any operating system user to connect as the database
748     user of the same name (if the latter exists). Other maps must be
749     created manually.
750    </para>
751
752    <para>
753     <indexterm><primary>pg_ident.conf</primary></indexterm> Ident maps
754     other than <literal>sameuser</literal> are defined in the file
755     <filename>pg_ident.conf</filename> in the data directory, which
756     contains lines of the general form:
757 <synopsis>
758 <replaceable>map-name</> <replaceable>ident-username</> <replaceable>database-username</>
759 </synopsis>
760     Comments and whitespace are handled in the usual way. The
761     <replaceable>map-name</> is an arbitrary name that will be used to
762     refer to this mapping in <filename>pg_hba.conf</filename>. The other
763     two fields specify which operating system user is allowed to connect
764     as which database user. The same <replaceable>map-name</> can be
765     used repeatedly to specify more user-mappings within a single map.
766     There is no restriction regarding how many database users a given
767     operating system user may correspond to and vice versa.
768    </para>
769
770   <para>
771     <indexterm>
772      <primary>SIGHUP</primary>
773     </indexterm>
774    The <filename>pg_ident.conf</filename> file is read on start-up and
775    when the <application>postmaster</> receives a
776    <systemitem>SIGHUP</systemitem> signal. If you edit the file on an
777    active system, you will need to signal the <application>postmaster</>
778    (using <literal>pg_ctl reload</> or <literal>kill -HUP</>) to make it
779    re-read the file.
780   </para>
781
782    <para>
783     A <filename>pg_ident.conf</filename> file that could be used in
784     conjunction with the <filename>pg_hba.conf</> file in <xref
785     linkend="example-pg-hba.conf"> is shown in <xref
786     linkend="example-pg-ident.conf">. In this example setup, anyone
787     logged in to a machine on the 192.168 network that does not have the
788     Unix user name <systemitem>bryanh</>, <systemitem>ann</>, or
789     <systemitem>robert</> would not be granted access. Unix user
790     <systemitem>robert</> would only be allowed access when he tries to
791     connect as <productname>PostgreSQL</> user <systemitem>bob</>, not
792     as <systemitem>robert</> or anyone else. <systemitem>ann</> would
793     only be allowed to connect as <systemitem>ann</>. User
794     <systemitem>bryanh</> would be allowed to connect as either
795     <systemitem>bryanh</> himself or as <systemitem>guest1</>.
796    </para>
797
798    <example id="example-pg-ident.conf">
799     <title>An example <filename>pg_ident.conf</> file</title>
800 <programlisting>
801 # MAPNAME     IDENT-USERNAME    PG-USERNAME
802
803 omicron       bryanh            bryanh
804 omicron       ann               ann
805 # bob has user name robert on these machines
806 omicron       robert            bob
807 # bryanh can also connect as guest1
808 omicron       bryanh            guest1
809 </programlisting>
810    </example>
811    </sect3>
812   </sect2>
813
814   <sect2 id="auth-pam">
815    <title>PAM Authentication</title>
816
817    <para>
818     This authentication type operates similarly to
819     <firstterm>password</firstterm> except that it uses PAM (Pluggable
820     Authentication Modules) as the authentication mechanism. The
821     default PAM service name is <literal>postgresql</literal>. You can
822     optionally supply you own service name after the <literal>pam</>
823     keyword in the file. For more information about PAM, please read
824     the <ulink
825     url="http://www.kernel.org/pub/linux/libs/pam/"><productname>Linux-PAM</>
826     Page</ulink> and the <ulink
827     url="http://www.sun.com/software/solaris/pam/"><systemitem
828     class="osname">Solaris</> PAM Page</ulink>.
829    </para>
830   </sect2>
831  </sect1>
832
833   <sect1 id="client-authentication-problems">
834    <title>Authentication problems</title>
835
836    <para>
837     Genuine authentication failures and related problems generally
838     manifest themselves through error messages like the following.
839    </para>
840
841    <para>
842 <ProgramListing>
843 No pg_hba.conf entry for host 123.123.123.123, user andym, database testdb
844 </ProgramListing>
845     This is what you are most likely to get if you succeed in contacting
846     the server, but it does not want to talk to you. As the message
847     suggests, the server refused the connection request because it found
848     no authorizing entry in its <filename>pg_hba.conf</filename>
849     configuration file.
850    </para>
851
852    <para>
853 <ProgramListing>
854 Password authentication failed for user 'andym'
855 </ProgramListing>
856     Messages like this indicate that you contacted the server, and it is
857     willing to talk to you, but not until you pass the authorization
858     method specified in the <filename>pg_hba.conf</filename> file. Check
859     the password you are providing, or check your Kerberos or ident
860     software if the complaint mentions one of those authentication
861     types.
862    </para>
863
864    <para>
865 <ProgramListing>
866 FATAL 1:  user "andym" does not exist
867 </ProgramListing>
868     The indicated user name was not found.
869    </para>
870
871    <para>
872 <ProgramListing>
873 FATAL 1:  Database "testdb" does not exist in the system catalog.
874 </ProgramListing>
875     The database you are trying to connect to does not exist. Note that
876     if you do not specify a database name, it defaults to the database
877     user name, which may or may not be the right thing.
878    </para>
879
880    <para>
881     Note that the server log may contain more information about an
882     authentication failure than is reported to the client. If you are
883     confused about the reason for a failure, check the log.
884    </para>
885   </sect1>
886
887  </chapter>