【发布时间】:2016-03-12 04:07:17
【问题描述】:
我正在尝试获取一个非常简单的 PHP 脚本来更改我的 Active Directory 域中的用户 密码。
这是我在网上找到的一些脚本:
<?php
$uid = 'Mohammed Noureldin';
$newPassword = '5omeGoodP@ssword';
$bindDn = 'CN=Administrator,OU=UsersOU,DC=example,DC=local';
$bindPassword = 'An0therGoodP@ssword';
$baseDn = 'OU=UsersOU,DC=example,DC=local';
$protocolVersion = 3;
$ldap = ldap_connect('localhost');
if (!ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $protocolVersion))
{
exit('Failed to set protocol version to '.$protocolVersion);
}
// bind anonymously so that we can verify if the server really is running
ldap_bind($ldap);
if (ldap_errno($ldap) !== 0)
{
exit('Could not connect to LDAP server');
}
// now bind with the correct username and password
ldap_bind($ldap, $bindDn, $bindPassword);
if (ldap_errno($ldap) !== 0)
{
exit('ERROR: '.ldap_error($ldap));
}
$searchResults = ldap_search($ldap, $baseDn, 'cn='.$uid);
// no matching records
if ($searchResults === false)
{
exit('No user found');
}
if (!is_resource($searchResults))
{
exit('Error in search results.');
}
// create the unicode password
$len = strlen($newPassword);
$newPass = '"';
for ($i = 0; $i < $len; $i++)
{
$newPass .= "{$newPassword{$i}}\000";
}
$newPass .= '"';
$entry = ldap_first_entry($ldap, $searchResults);
if (!is_resource($entry))
{
exit('Couldn\'t get entry');
}
$userDn = ldap_get_dn($ldap, $entry);
if (!$userDn)
{
exit('Errrrrrrrrr1');
}
if (!ldap_modify($ldap, $userDn, array('unicodePwd' => $newPass)))
{
exit(ldap_errno($ldap)." ". ldap_error($ldap));
}
?>
这个 PHP 页面的输出是这个 error 消息:
53 服务器不愿意执行
脚本根本不起作用(用户的密码未更改)。
我知道 AD 将密码存储在 unicodePwd 字段中的主要原理(如果到目前为止仍然如此),并且我知道我必须使用 安全连接并且我正在使用它(希望它设置正确)。
我在谷歌上搜索了该错误消息,但找不到任何实用的解决方案。
我还尝试了一些其他脚本,但这个是迄今为止最好的,因为其他脚本在之前的一些步骤中给了我一些错误(例如绑定)。
非常感谢解决该问题的任何帮助,甚至另一个功能性脚本可能是个好主意! 提前致谢。
【问题讨论】:
-
此代码没有安全地连接到 LDAP 服务器。见documentation
-
如果我在第三方应用程序(Apache Directory Studio)上使用端口 636 和 ldaps,并且我能够连接,这是否意味着我正确使用了安全连接?
-
这仅表示您的第三方应用程序正在正确使用安全连接。不代表你写的代码就是这样做的!
-
好的,你能帮我让我的代码使用安全连接吗?我错过了什么?
标签: active-directory php ldap