简体   繁体   English

使用PHP在LDAP中的更多OU中搜索用户

[英]Search users in more OUs in LDAP with PHP

My task is ultimately to implement a single sign-on solution to login a user to my system, using the AD credentials stored in his work machine. 我的任务是使用存储在其工作机器中的AD凭证,最终实现单点登录解决方案以将用户登录到我的系统。 I'm using the ldap_ functions in PHP . 在PHP中使用ldap_函数

The problem I have is that I have to check the credentials against more than one OU (around 30, for now), and I haven't found a way to obtain those OUs from the AD system, so I have a big array of OUs as a stop-gap measure. 我遇到的问题是我必须检查多个OU的凭据(目前大约30个),而且我还没有找到从AD系统获取这些OU的方法,所以我有大量的OU作为一种权宜之计。 I obtained this list with ADexplorer. 我用ADexplorer获得了这个列表。 Is there a way to do this with PHP? 有没有办法用PHP做到这一点?

My first language is not English, so I'm having Adriano translate this for me, and I'm also having problems understanding the documentation. 我的第一语言不是英语,所以我让阿德里亚诺为我翻译这个,我在理解文档方面也遇到了问题。

At this stage, I'm basically copying this code from an example in the ldap_search manpage : 在这个阶段,我基本上是从ldap_search联机帮助页中的示例复制此代码:

<?php
$ds=ldap_connect($ldapserver);

// 42 OUs in our case
$dn[]='OU=ABC,DC=xyz,DC=ac,DC=uk';
$dn[]='OU=DEF,DC=xyz,DC=ac,DC=uk';
// ...

$totalDns = count($dn);
for ($i = 0; $i < $totalDns; $i++) {
    $id[] = $ds;
}

$filter = 'samaccountname='.$_POST['username'];

$result = ldap_search($id,$dn,$filter);

$search = false;

foreach ($result as $value) {
    if(ldap_count_entries($ds,$value)>0){
        $search = $value;
        break;
    }
}

if($search){
    $info = ldap_get_entries($ds, $search);
}else{
    $info = 'No results found';
}

Update 更新

I tried the solution suggested by Vladislav Ross, and, after a few seconds, the server spits this: 我尝试了Vladislav Ross建议的解决方案,几秒钟后,服务器吐了这个:

mod_fcgid: stderr: PHP Warning:  ldap_search() [<a href='function.ldap-search'>function.ldap-search</a>]: Search: Can't contact LDAP server in ... on line 28

This does not happen if I prepend a specific OU to the same search. 如果我将特定OU添加到同一搜索中,则不会发生这种情况。 IE if I do IE,如果我这样做

$sr = ldap_search(
     $ds,
     "OU=Usuarios,dc=test,dc=com",
     "ObjectClass=organizationalUnit",
     array("")
);

instead of 代替

$sr = ldap_search(
      $ds,
      "dc=test,dc=com",
      "ObjectClass=organizationalUnit",
      array("")
);

I do get a correct result. 我得到了正确的结果。 I tried setting the timelimit and sizelimit parameters to 0, but with the same results. 我尝试将timelimit和sizelimit参数设置为0,但结果相同。 I'm now consulting with the server guys to see what their limits are. 我现在正在咨询服务器人员,了解他们的限制是什么。

Try to use ldap_search with filter ObjectClass=organizationalUnit: 尝试将ldap_search与过滤器ObjectClass = organizationalUnit一起使用:

    $ds = ldap_connect($AD_server);
    if(!$ds) die("cannot connect to LDAP server at $AD_server.");

    $r = ldap_bind($ds, $AD_Auth_User, $AD_Auth_PWD);
    if(!$r)
    {
        ldap_get_option($ds,LDAP_OPT_ERROR_STRING,$error);
        die("cannot bind to LDAP server at $AD_server ($error).");
    };

    $sr=ldap_search($ds,"dc=test,dc=com","ObjectClass=organizationalUnit",array(""));
    $info = ldap_get_entries($ds, $sr);

    print_r($info); //<--array with OU's you need

If you don't need recurse search, use ldap_list instead of ldap_search. 如果您不需要递归搜索,请使用ldap_list而不是ldap_search。

It sounds like you want to authenticate any user in the domain. 听起来您想要对域中的任何用户进行身份验证。 Don't search multiple DNs, just search once from the root of the domain. 不要搜索多个DN,只需从域的根目录搜索一次。 So instead of using 'OU=ABC,DC=xyz,DC=ac,DC=uk' , use 'DC=xyz,DC=ac,DC=uk' . 因此,不使用'OU=ABC,DC=xyz,DC=ac,DC=uk' ,而是使用'DC=xyz,DC=ac,DC=uk'

I haven't found a way to obtain those OUs from the AD system 我还没有找到从AD系统获取这些OU的方法

Your example above suggests these OUs are direct children of DC=xyz,DC=ac,DC=uk? 上面的例子表明这些OU是DC = xyz,DC = ac,DC = uk的直接子节点? If so, the most efficient LDAP search to find these is: 如果是这样,找到这些的最有效的LDAP搜索是:

  • Search base: DC=xyz,DC=ac,DC=uk 搜索基数:DC = xyz,DC = ac,DC = uk
  • Search scope: onelevel <-- direct children of search base 搜索范围: onelevel < - 搜索基础的 直接 子项
  • Search filter: (objectclass=organizationalUnit) 搜索过滤器:(objectclass = organizationalUnit)

Test using the corresponding ldapsearch command line like this ... 使用相应的ldapsearch命令行进行测试......

ldapsearch -h <hostname> -s onelevel -b "DC=xyz,DC=ac,DC=uk" "(objectclass=organizationalUnit)"

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

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