简体   繁体   English

如何在php中使用LDAP从Active Directory搜索和更新用户数据?

[英]How search and update user data from Active Directory using LDAP in php?

I using php 5.6 and i try search user in Active Directory by LDAP in php. 我使用php 5.6,并尝试通过php中的LDAP在Active Directory中搜索用户。 Example search: 搜索示例:

ldap_connect($host, '389');
ldap_search($client, 'DC=shamsa,DC=real,DC=kamchatka,DC=ru', '(&(objectClass=user)(CN=user_name))', '*');

I successfully get user data, but userPassword does not exist in returned fields. 我成功获取了用户数据,但是userPassword在返回的字段中不存在。 I need user password for compare password from Active Data and password entered by user. 我需要用户密码来比较活动数据中的密码和用户输入的密码。

How i can get user password from Active Data or how compare password entered by user and password in Active Directory without getting? 我如何从Active Data获取用户密码,或者如何比较用户输入的密码和Active Directory中的密码而不获取?

By the way, also, i need update user data in Active Directory. 顺便说一句,我还需要更新Active Directory中的用户数据。 For example update name and email fields. 例如,更新名称和电子邮件字段。 How i can do that? 我该怎么做?

Thank you for answers. 谢谢你的回答。

If you only want to compare the password the user provided in a login interface with the password stored in LDAP/Active Directory you should use the ldap_bind command for that with the users dn and the password. 如果只想将登录界面中提供的用户密码与LDAP / Active Directory中存储的密码进行比较,则应将ldap_bind命令与用户dn和密码一起使用。 It returns true when password and username/DN match and false when not. 如果密码和用户名/ DN匹配,则返回true,否则返回false。

To get the DN you'd typically bind with a known user that has read access to the ldap, search for the user by its username and then bind again with the returned users DN and the password. 要获取DN,通常需要与对ldap具有读取访问权限的已知用户绑定,请按用户名搜索该用户,然后再次与返回的用户DN和密码绑定。 There is a gist that illustrates that at https://gist.github.com/heiglandreas/5689592 https://gist.github.com/heiglandreas/5689592上有一个说明要点的要点

If you also want to set the users password you'd want to bind with the user as well as usually only the user (and admin accounts) are allowed to read (and write) the users password. 如果您还想设置用户密码,则希望与该用户绑定,通常情况下,只允许该用户(和管理员帐户)读取(和写入)该用户密码。 So you'd also want to proceed as before but then add a call to update the users password-attribute. 因此,您还希望像以前一样继续操作,但随后添加一个呼叫以更新用户的密码属性。 For a solution and more in depth knowledge have a look at fe http://www.letu.edu/people/markroedel/netcccu/activedirectorypasswordchanges.htm 有关解决方案和更深入的知识, 请访问http://www.letu.edu/people/markroedel/netcccu/activedirectorypasswordchanges.htm

Updating other userdata than password will also be done using ldap_modify or ldap_mod_replace 还可以使用ldap_modify或ldap_mod_replace来更新除密码以外的其他用户数据。

Use ldap_modify to update an ldap entry. 使用ldap_modify来更新ldap条目。 It's pretty simple : 这很简单:

$entry['mail'] = 'example@domain.com';
$entry['cn'] = 'test';
ldap_modify($connection, $dn, $entry);

userPassword is an operational attribute. userPassword操作属性。

An operational attribute is used internally by the server. 服务器内部使用操作属性。 Generally, it is readable but can't be modified by a user. 通常,它是可读的,但不能被用户修改。

In order to retrieve an operational attribute, you have to require it explicitely or request all of them using the special attribute + (like you did with * to request all user attributes). 为了检索操作属性,您必须明确地要求它,或使用特殊属性+请求所有这些属性(就像使用*来请求所有用户属性一样)。

You can also request all (both user and operational) attributes with : 您还可以使用以下命令请求所有 (用户和操作)属性:

ldap_search($connection, $base_dn, $filter, ['*', '+']);

Now, you want to compare a submitted user password with a password hash string stored in AD. 现在,您想将提交的用户密码与存储在AD中的密码哈希字符串进行比较。 That means you need to manually encrypt/hash/salt the submitted string according to the hash scheme used by AD (eg : {SSHA}, {SHA}, {SMD5}, {MD5}, {CRYPT}, ... ) so that the comparison is feasible. 这意味着您需要根据AD所使用的哈希方案(例如: {SSHA}, {SHA}, {SMD5}, {MD5}, {CRYPT}, ... )手动对提交的字符串进行加密/哈希/加盐{SSHA}, {SHA}, {SMD5}, {MD5}, {CRYPT}, ...比较是可行的。

Here an example of how to manually generate a {MD5} hash based on a clear text password : 这是一个如何基于明文密码手动生成{MD5}哈希的示例:

$hash = '{md5}' . base64_encode(pack('H*', md5($clear_txt_passwd)));

=> This is not straightforward and a match is not guaranteed even if the input string is correct since you may encounter interoperability issues between ldap implementations and/or systems/OSs (hash/salt functions can differ from one to another, especially using crypt library that is system dependant). =>这并不简单,即使输入字符串正确也不保证匹配,因为您可能会在ldap实现和/或系统/ OS之间遇到互操作性问题(哈希/盐函数可能彼此不同,尤其是使用crypt库时)这取决于系统)。

If you want to test user authentication directly against AD, just use ldap_bind : 如果要直接针对AD测试用户身份验证,只需使用ldap_bind

$bind = ldap_bind($connection, $dn, $passwd); // returns TRUE on success, FALSE on failure.
echo "$dn\n" . ($bind ? 'LDAP bind OK' : 'LDAP bind failed');

But if, for example, users credentials are to be checked both against an existing openLdap server and AD, it would require to constantly sync/replicate user entries, instead it is better to keep one single authentication backend. 但是,例如,如果要同时针对现有的openLdap服务器和AD检查用户凭据,则需要不断同步/复制用户条目,而最好保留一个身份验证后端。 In such case you would have to use Pass-Through Authentication using the dedicated {SASL} scheme in the 1st backend (openLDAP in this example) : With this configuration, Cyrus SASL is used as a password handler and delegates password verification to any back-end server with SASL supports for checking passwords (AD in your case but it could be Kerberos or an IMAP server). 在这种情况下,您将必须在第一个后端(此示例中为openLDAP)使用专用的{SASL}方案使用直通身份验证 :通过此配置,Cyrus SASL用作密码处理程序, 并将密码验证委派给任何后端具有SASL的最终服务器支持检查密码(在您的情况下为AD,但可以是Kerberos或IMAP服务器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM