如何通过命令行使用 LDAP 进行身份验证?

如何通过命令行使用 LDAP 进行身份验证?

LDAP 服务器托管在 Solaris 上。客户端是 CentOS。通过 LDAP 进行 OpenLDAP/NSLCD/SSH 身份验证可以正常工作,但我无法使用 ldapsearch 命令来调试 LDAP 问题。

[root@tst-01 ~]# ldapsearch
SASL/EXTERNAL authentication started
ldap_sasl_interactive_bind_s: Unknown authentication method (-6)
        additional info: SASL(-4): no mechanism available:
[root@tst-01 ~]# cat /etc/openldap/ldap.conf
TLS_CACERTDIR /etc/openldap/cacerts
URI ldap://ldap1.tst.domain.tld ldap://ldap2.tst.domain.tld
BASE dc=tst,dc=domain,dc=tld
[root@tst-01 ~]# ls -al /etc/openldap/cacerts
total 12
drwxr-xr-x. 2 root root 4096 Jun  6 10:31 .
drwxr-xr-x. 3 root root 4096 Jun 10 10:12 ..
-rw-r--r--. 1 root root  895 Jun  6 10:01 cacert.pem
lrwxrwxrwx. 1 root root   10 Jun  6 10:31 cf848aa4.0 -> cacert.pem
[root@tst-01 ~]#

我已尝试通过 ldapsearch 提供 /etc/openldap/cacerts/cacert.pem 作为参数的证书进行身份验证,但它不接受该证书进行身份验证。

答案1

您可能希望关闭 SASL 并使用“-x”选项进行简单身份验证。例如,搜索以查找特定用户

ldapsearch -x -D "uid=search-user,ou=People,dc=example,dc=com" \
           -W -H ldap://ldap.example.com -b "ou=People,dc=example,dc=com" \
           -s sub 'uid=test-user'

将找到“test-user”

  • -D- 使用绑定用户“search-user”
  • -W- 提示输入密码
  • -H- LDAP 服务器的 URL。本例中为非 SSL;对于 SSL,请使用“ldaps://”
  • -b- 搜索基础
  • -s- 搜索范围 - 即根据对于树的底部,对于上层和下层用于递归搜索树(可能需要一段时间)
  • 最后,搜索过滤器作为非选项参数。在本例中,我们将搜索“test-user”的 uid

答案2

我似乎在问同样的问题:https://stackoverflow.com/questions/27571558/how-was-authentication-built-on-ldap

http://thecarlhall.wordpress.com/2011/01/04/ldap-authentication-authorization-dissected-and-digested/

  • 获取与 LDAP 服务器的连接。
  • 绑定为应用程序用户。
  • 搜索需要验证的用户的 DN(专有名称)。
  • 绑定为要使用步骤 3 中的 DN 进行身份验证的用户。

可以总结为(命令行中的实验):

$ ldapsearch -x -h ldap.company.com -s sub -b 'dc=europe,dc=com' "uid=XYZ"
....
dn: uid=XYZ,dc=sales,dc=europe,dc=com
...
$ ldapsearch -W -h ldap.company.com -D 'uid=XYZ,dc=sales,dc=europe,dc=com' \
    -s sub -b 'dc=europe,dc=com' "uid=XYZ"

答案3

请注意,如果你不知道完整的绑定 DN,你也可以使用你的普通用户名或电子邮件-U

ldapsearch -v -h contoso.com -U [email protected] -w 'MY_PASSWORD' -b 'DC=contoso,DC=com' '(objectClass=computer)'

答案4

我们正在使用免费IPA/IDM我可以使用下面的方法对此进行身份验证:

$ ldapsearch -h idm-01a.somednsdom.com \
    -D 'uid=<my username>,cn=users,cn=accounts,dc=somedcdom,dc=com' \
    -o ldif-wrap=no \
    -b 'cn=accounts,dc=somedcdom,dc=com' \
    -W uid=<my username>
解释
  • 这将返回uid=<my username>
  • uid=<my username>是过滤器(符合 RFC 4515 的 LDAP 搜索过滤器)
  • uid=<my username>是要执行的查询/过滤
  • o ldif-wrap=no禁用结果换行
  • 强制-W查询ldapsearch绑定专有名称的密码uid=<my username>,cn=users,cn=accounts,dc=somedcdom,dc=com
  • 当提示输入该用户的密码时,提示将如下所示:

    Enter LDAP Password:
    
参考文献

请参阅ldapsearch手册页和 CLI 帮助:

   -D binddn
          Use the Distinguished Name binddn to bind to the LDAP directory.  
          For SASL binds, the server is expected to ignore this value.

   -b searchbase
          Use searchbase as the starting point for the search instead of the 
          default.

   -W     Prompt for simple authentication.  This is used instead of specifying 
          the password on the command line.

  -o <opt>[=<optparam] general options
             nettimeout=<timeout> (in seconds, or "none" or "max")
             ldif-wrap=<width> (in columns, or "no" for no wrapping)

完整示例

$ ldapsearch -h idm-01a.somednsdom.com \
    -D 'uid=joeuser,cn=users,cn=accounts,dc=somedcdom,dc=com' \
    -o ldif-wrap=no \
    -b 'cn=accounts,dc=somedcdom,dc=com' \
    -W uid=joeuser

# extended LDIF
#
# LDAPv3
# base <cn=accounts,dc=somedcdom,dc=com> with scope subtree
# filter: uid=joeuser
# requesting: ALL
#

# joeuser, users, accounts, somedcdom.com
dn: uid=joeuser,cn=users,cn=accounts,dc=somedcdom,dc=com
memberOf: cn=ipausers,cn=groups,cn=accounts,dc=somedcdom,dc=com
memberOf: cn=sysadmin,cn=groups,cn=accounts,dc=somedcdom,dc=com
memberOf: ipaUniqueID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX,cn=sudorules,cn=sudo,dc=somedcdom,dc=com
memberOf: cn=eng-systems,cn=groups,cn=accounts,dc=somedcdom,dc=com
memberOf: ipaUniqueID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX,cn=hbac,dc=somedcdom,dc=com
memberOf: cn=admins,cn=groups,cn=accounts,dc=somedcdom,dc=com
memberOf: ipaUniqueID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX,cn=sudorules,cn=sudo,dc=somedcdom,dc=com
memberOf: cn=User Administrator,cn=roles,cn=accounts,dc=somedcdom,dc=com
memberOf: cn=User Administrators,cn=privileges,cn=pbac,dc=somedcdom,dc=com
memberOf: cn=System: Add User to default group,cn=permissions,cn=pbac,dc=somedcdom,dc=com
...
...
krbLoginFailedCount: 0
krbLastFailedAuth: 20190320223946Z
loginShell: /bin/bash
krbExtraData:: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
krbPasswordExpiration: 20190829144625Z
krbLastPwdChange: 20190302144625Z
krbLastAdminUnlock: 20190111080021Z
ipaSshPubKey: ssh-rsa A....XXXXXXXXXXXX...jelByox0PM5Q== [email protected]
mepManagedEntry: cn=joeuser,cn=groups,cn=accounts,dc=somedcdom,dc=com
displayName: Joe User
uid: joeuser
krbCanonicalName: [email protected]
objectClass: top
objectClass: person
objectClass: organizationalperson
objectClass: inetorgperson
objectClass: inetuser
objectClass: posixaccount
objectClass: krbprincipalaux
objectClass: krbticketpolicyaux
objectClass: ipaobject
objectClass: ipasshuser
objectClass: ipaSshGroupOfPubKeys
objectClass: mepOriginEntry
initials: JU
gecos: Joe User
sn: Mingolelli
homeDirectory: /home/joeuser
mail: [email protected]
krbPrincipalName: [email protected]
givenName: Joe
cn: Joe User
ipaUniqueID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
uidNumber: 900000000
gidNumber: 900000000
krbPwdPolicyReference: cn=admins,cn=SOMEDCDOM.COM,cn=kerberos,dc=somedcdom,dc=com

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

相关内容