使用PHP更新AD密码问题

时间:2015-03-18 06:31:58

标签: php active-directory ldap

您好我已经编写了以下脚本来更新特定用户的密码

<?php

function create_ldap_connection() {

        $ip = "192.168.168.1";
        $ldaps_url = "192.168.168.1";

        $port = 389;
        $ldap_conn = ldap_connect($ldaps_url, $port) or die("Sorry! Could not connect to LDAP server ($ip)");

        $password = "password";
        $binddn = "CN=Administrator,CN=Users,DC=ad,DC=test,DC=com";
        $result = ldap_bind( $ldap_conn, $binddn, $password ) or die("  Error: Couldn't bind to server using provided credentials!");

        if($result) {

                return $ldap_conn;
        }
        else {
                die (" Error: Couldn't bind to server with supplied credentials!");
        }
}

function get_user_dn($ldap_conn, $user_name) {

        /* Write the below details as per your AD setting */
        $basedn = "OU=ITS Users,DC=ad,DC=test,DC=com";

        /* Search the user details in AD server */
        $searchResults = ldap_search($ldap_conn, $basedn, $user_name);

        if(!is_resource($searchResults)) die('Error in search results.');
        /* Get the first entry from the searched result */
        $entry = ldap_first_entry($ldap_conn, $searchResults);

        $info = ldap_get_entries($ldap_conn, $searchResults);
        echo $info["count"]." entries returned\n";

        return ldap_get_dn($ldap_conn, $entry);
}

function pwd_encryption($newPassword) {

        $newPassword = "\"" . $newPassword . "\"";
        $len = strlen($newPassword);
        $newPassw = "";

        for ($i = 0; $i < $len; $i++) {

                $newPassw .= "{$newPassword {$i}}\000";
        }

        $userdata["unicodePwd"] = $newPassw;
        return $userdata;
}

$user_name = "(|(sn=archieg*)(SamAccountName=archieg*))";

$user_password = "password!1234";
$ldap_conn = create_ldap_connection();
$userDn = get_user_dn($ldap_conn, $user_name);
$userdata = pwd_encryption ($user_password);

print_r($userdata);

//$result = ldap_mod_replace($ldap_conn, $userDn , $userdata);  /* Check whether the password updated successfully or not. */
$result = ldap_modify($ldap_conn, $userDn , $userdata);

if($result) {

        echo "Success attempting to modify password in AD";
}
else {

        echo "Error: Please try again later!\n";
        $e = ldap_error($ldap_conn);
        $e_no = ldap_errno($ldap_conn);
        echo $e . "\n";
        echo $e_no . "\n";
}

?>

但是,当我运行此操作时,我收到以下错误,

[root@web chpasswd]# php ad_change.php 
PHP Warning:  Module 'intl' already loaded in Unknown on line 0
1 entries returned
Array
(
    [unicodePwd] => "password!1234"
)
Error: Please try again later!
Server is unwilling to perform
53
[root@web chpasswd]# 

我在这里做错了什么?我一直在玩加密,但这也无济于事。我有Windows Server 2012 R2 Active Directory。

非常感谢

1 个答案:

答案 0 :(得分:1)

我发现这不是我的代码的问题。我必须在我的服务器上设置证书颁发机构。这就是我做的,

确保您的PHP安装启用了ldap和openssl扩展。

Windows / Linux程序

验证ldap.conf文件设置。

对于Windows,请验证C:\ openldap \ sysconf \ ldap.conf文件是否存在。

对于Linux,请验证/etc/openldap/ldap.conf文件是否存在。如果没有,请创建它。

对于Linux和Windows,ldap.conf文件应包含以下行:** -

TLS_REQCERT永远不会

如果您希望php使用颁发证书的证书颁发机构验证ldap服务器的ssl证书,则需要将根证书放在此处: 导出受信任的根证书。 (有关详细信息,请参阅如何通过SSL测试LDAP)中的步骤1。

使用此命令将DER转换为PEM:

openssl x509 -in RootCert.der -inform DER -out RootCert.pem -outform PEM

在Windows上,您可以从以下两个站点下载openssl二进制文件:

http://gnuwin32.sourceforge.net/packages.html

http://www.ShininglightPro.com/

现在将rootcert.pem复制到certs文件夹:

对于Linux,/ etc / openldap / cert / rootcert.pem

对于Windows,C:\ openldap \ sysconf \ certs \ rootcert.pem

对于Linux和Windows,ldap.conf文件应包含以下行:

(Linux)TLS_CACERT /etc/openldap/cert/rootcert.pem

(Windows)TLS_CACERT c:\ OpenLDAP \ sysconf \ certs \ rootcert.pem

您可以在https://github.com/achintha85/AD_User_Password_Change_PHP

上找到我的最新代码

希望这有助于将来。