Postgresql 中文操作指南

21.10. LDAP Authentication #

此身份验证方法与 password 的运行方式很相似,但它使用 LDAP 作为密码验证方法。LDAP 仅用于验证用户名/密码对。因此,用户必须在数据库中已存在,然后才能使用 LDAP 进行身份验证。

This authentication method operates similarly to password except that it uses LDAP as the password verification method. LDAP is used only to validate the user name/password pairs. Therefore the user must already exist in the database before LDAP can be used for authentication.

LDAP 身份验证可以在两种模式下运行。在第一种模式中(称之为简单绑定模式),服务器会绑定到作为 prefix username suffix 构建的 DN。通常,prefix 参数用于在 Active Directory 环境中指定 cn=DOMAIN__。_suffix_ 用于在非 Active Directory 环境中指定 DN 的其余部分。

LDAP authentication can operate in two modes. In the first mode, which we will call the simple bind mode, the server will bind to the distinguished name constructed as prefix username suffix. Typically, the prefix parameter is used to specify cn=, or DOMAIN_\_ in an Active Directory environment. suffix is used to specify the remaining part of the DN in a non-Active Directory environment.

在第二种模式中(称之为搜索 + 绑定模式),服务器首先使用 ldapbinddnldapbindpasswd 指定的固定用户名和密码绑定到 LDAP 目录,并对试图登录数据库的用户执行搜索。如果未配置任何用户和密码,将尝试匿名绑定到目录。搜索将在 ldapbasedn 的子树上进行,并将尝试对 ldapsearchattribute 中指定属性进行精确匹配。此搜索找到用户后,服务器将断开连接,并使用客户端指定的密码重新绑定到目录作为此用户,以验证登录是否正确。此模式与其他软件(如 Apache mod_authnz_ldappam_ldap)中 LDAP 身份验证方案使用的方法相同。此方法大幅提高了用户对象在目录中的存储位置的灵活性,但会导致与 LDAP 服务器建立两个单独的连接。

In the second mode, which we will call the search+bind mode, the server first binds to the LDAP directory with a fixed user name and password, specified with ldapbinddn and ldapbindpasswd, and performs a search for the user trying to log in to the database. If no user and password is configured, an anonymous bind will be attempted to the directory. The search will be performed over the subtree at ldapbasedn, and will try to do an exact match of the attribute specified in ldapsearchattribute. Once the user has been found in this search, the server disconnects and re-binds to the directory as this user, using the password specified by the client, to verify that the login is correct. This mode is the same as that used by LDAP authentication schemes in other software, such as Apache mod_authnz_ldap and pam_ldap. This method allows for significantly more flexibility in where the user objects are located in the directory, but will cause two separate connections to the LDAP server to be made.

以下配置选项在两种模式中都使用:

The following configuration options are used in both modes:

  • ldapserver

    • Names or IP addresses of LDAP servers to connect to. Multiple servers may be specified, separated by spaces.

  • ldapport

    • Port number on LDAP server to connect to. If no port is specified, the LDAP library’s default port setting will be used.

  • ldapscheme

    • Set to ldaps to use LDAPS. This is a non-standard way of using LDAP over SSL, supported by some LDAP server implementations. See also the ldaptls option for an alternative.

  • ldaptls

    • Set to 1 to make the connection between PostgreSQL and the LDAP server use TLS encryption. This uses the StartTLS operation per RFC 4513. See also the ldapscheme option for an alternative.

请注意,使用 ldapschemeldaptls 仅加密 PostgreSQL 服务器和 LDAP 服务器之间的流量。除非在 PostgreSQL 服务器和 PostgreSQL 客户端之间也使用 SSL,否则 PostgreSQL 服务器和 PostgreSQL 客户端之间的连接仍将保持未加密状态。

Note that using ldapscheme or ldaptls only encrypts the traffic between the PostgreSQL server and the LDAP server. The connection between the PostgreSQL server and the PostgreSQL client will still be unencrypted unless SSL is used there as well.

以下选项仅在简单绑定模式中使用:

The following options are used in simple bind mode only:

  • ldapprefix

    • String to prepend to the user name when forming the DN to bind as, when doing simple bind authentication.

  • ldapsuffix

    • String to append to the user name when forming the DN to bind as, when doing simple bind authentication.

使用 search+bind 模式时,仅使用以下选项:

The following options are used in search+bind mode only:

  • ldapbasedn

    • Root DN to begin the search for the user in, when doing search+bind authentication.

  • ldapbinddn

    • DN of user to bind to the directory with to perform the search when doing search+bind authentication.

  • ldapbindpasswd

    • Password for user to bind to the directory with to perform the search when doing search+bind authentication.

  • ldapsearchattribute

    • Attribute to match against the user name in the search when doing search+bind authentication. If no attribute is specified, the uid attribute will be used.

  • ldapsearchfilter

    • The search filter to use when doing search+bind authentication. Occurrences of $username will be replaced with the user name. This allows for more flexible search filters than ldapsearchattribute.

  • ldapurl

    • An RFC 4516 LDAP URL. This is an alternative way to write some of the other LDAP options in a more compact and standard form. The format is

ldap[s]://host[:port]/basedn[?[attribute][?[scope][?[filter]]]]
  • scope must be one of base, one, sub, typically the last. (The default is base, which is normally not useful in this application.) attribute can nominate a single attribute, in which case it is used as a value for ldapsearchattribute. If attribute is empty then filter can be used as a value for ldapsearchfilter.

  • The URL scheme ldaps chooses the LDAPS method for making LDAP connections over SSL, equivalent to using ldapscheme=ldaps. To use encrypted LDAP connections using the StartTLS operation, use the normal URL scheme ldap and specify the ldaptls option in addition to ldapurl.

  • For non-anonymous binds, ldapbinddn and ldapbindpasswd must be specified as separate options.

  • LDAP URLs are currently only supported with OpenLDAP, not on Windows.

将简单绑定的配置选项与 search+bind 的选项混合使用是一种错误。

It is an error to mix configuration options for simple bind with options for search+bind.

使用 search+bind 模式时,可以利用 ldapsearchattribute 指定的单个属性执行搜索,也可以使用 ldapsearchfilter 指定的自定义搜索过滤器执行搜索。指定 ldapsearchattribute=foo 等效于指定 ldapsearchfilter="(foo=$username)"。如果未指定任何选项,则默认值为 ldapsearchattribute=uid

When using search+bind mode, the search can be performed using a single attribute specified with ldapsearchattribute, or using a custom search filter specified with ldapsearchfilter. Specifying ldapsearchattribute=foo is equivalent to specifying ldapsearchfilter="(foo=$username)". If neither option is specified the default is ldapsearchattribute=uid.

如果 PostgreSQL 使用 OpenLDAP 编译为 LDAP 客户机库,则可以省略 ldapserver 设置。在此情况下,将通过 RFC 2782 DNS SRV 记录查找主机名和端口的列表。将查找该名称 _ldap._tcp.DOMAIN,其中 DOMAINldapbasedn 中提取。

If PostgreSQL was compiled with OpenLDAP as the LDAP client library, the ldapserver setting may be omitted. In that case, a list of host names and ports is looked up via RFC 2782 DNS SRV records. The name _ldap._tcp.DOMAIN is looked up, where DOMAIN is extracted from ldapbasedn.

以下是简单绑定的 LDAP 配置示例:

Here is an example for a simple-bind LDAP configuration:

host ... ldap ldapserver=ldap.example.net ldapprefix="cn=" ldapsuffix=", dc=example, dc=net"

当请求连接到数据库用户 someuser 的数据库服务器时,PostgreSQL 将尝试使用 DN cn=someuser, dc=example, dc=net 和客户端提供的密码绑定到 LDAP 服务器。如果连接成功,则授予数据库访问权限。

When a connection to the database server as database user someuser is requested, PostgreSQL will attempt to bind to the LDAP server using the DN cn=someuser, dc=example, dc=net and the password provided by the client. If that connection succeeds, the database access is granted.

以下是 search+bind 配置示例:

Here is an example for a search+bind configuration:

host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchattribute=uid

当请求连接到数据库用户 someuser 的数据库服务器时,PostgreSQL 将尝试匿名绑定(因为未指定 ldapbinddn)到 LDAP 服务器,在指定的基 DN 下搜索 (uid=someuser)。如果找到条目,它将使用找到的信息和客户端提供的密码再次尝试绑定。如果第二次连接成功,则授予数据库访问权限。

When a connection to the database server as database user someuser is requested, PostgreSQL will attempt to bind anonymously (since ldapbinddn was not specified) to the LDAP server, perform a search for (uid=someuser) under the specified base DN. If an entry is found, it will then attempt to bind using that found information and the password supplied by the client. If that second connection succeeds, the database access is granted.

以下是作为 URL 编写的相同 search+bind 配置:

Here is the same search+bind configuration written as a URL:

host ... ldap ldapurl="ldap://ldap.example.net/dc=example,dc=net?uid?sub"

一些其他支持基于 LDAP 的身份验证的软件使用相同的 URL 格式,这样便于共享配置。

Some other software that supports authentication against LDAP uses the same URL format, so it will be easier to share the configuration.

以下是一个允许通过用户 ID 或电子邮件地址进行身份验证的搜索+绑定配置示例,它使用 ldapsearchfilter 而不是 ldapsearchattribute

Here is an example for a search+bind configuration that uses ldapsearchfilter instead of ldapsearchattribute to allow authentication by user ID or email address:

host ... ldap ldapserver=ldap.example.net ldapbasedn="dc=example, dc=net" ldapsearchfilter="(|(uid=$username)(mail=$username))"

以下是一个使用 DNS SRV 发现来查找域名 example.net 的 LDAP 服务的主机名和端口的搜索+绑定配置示例:

Here is an example for a search+bind configuration that uses DNS SRV discovery to find the host name(s) and port(s) for the LDAP service for the domain name example.net:

host ... ldap ldapbasedn="dc=example,dc=net"

Tip

由于 LDAP 通常使用逗号和空格来分隔 DN 的不同部分,因此在配置 LDAP 选项时,通常需要使用带双引号的参数值,如示例所示。

Since LDAP often uses commas and spaces to separate the different parts of a DN, it is often necessary to use double-quoted parameter values when configuring LDAP options, as shown in the examples.