简体   繁体   English

使用LDAP函数获取PHP中的Active Directory tokenGroups属性

[英]Using LDAP functions to get Active Directory tokenGroups attribute in PHP

Greetings, 问候,

I already have a working connection to the AD and can search and retrieve information from it. 我已经与AD建立了有效的连接,并且可以从中搜索和检索信息。 I've even developed a recursive method by which one can retrieve all groups for a given user. 我什至开发了一种递归方法,通过该方法可以检索给定用户的所有组。 However, I'd like to avoid the recursion if possible. 但是,如果可能,我想避免递归。 One way to do this is to get the tokenGroups attribute from the AD for the user, which should be a list of the SIDs for the groups that the specified user has membership, whether that membership be direct or indirect. 一种方法是从用户的AD获取tokenGroups属性,该属性应该是指定用户具有成员身份的组的SID列表,无论该成员身份是直接成员身份还是间接成员身份。

When I run a search for a user's AD information, though, the tokenGroups attribute isn't even in it. 但是,当我搜索用户的AD信息时,tokenGroups属性甚至不在其中。 I tried specifically requesting that information (ie, specifying it using the fourth parameter to ldap_search ) but that didn't work, either. 我尝试专门要求提供该信息(即,使用ldap_search的第四个参数指定该信息),但这也不起作用。

Thanks, David Kees 谢谢大卫·基斯


Solved my own problem and thought I'd put the answer here so that others might find it. 解决了我自己的问题,并认为我会将答案放在这里,以便其他人可以找到它。 The issue was using the ldap_search() function. 问题是使用l​​dap_search()函数。 The answer was to use the ldap_read() function instead of ldap_search(). 答案是使用ldap_read()函数而不是ldap_search()。 The difference is the scope of the request. 不同之处在于请求的范围。 The search function uses a scope of "sub" (ie, subtree) while the read function uses "base." 搜索功能使用“子”范围(即子树),而读取功能使用“基”范围。 The tokenGroups information can only be found when using a scope of "base" so using the correct PHP function was the key. 仅在使用“基本”范围时才能找到tokenGroups信息,因此使用正确的PHP函数是关键。

As I mentioned above, I was working from someone else code in perl to create my solution and the perl script used a function named "search" to do it's LDAP requests which lead me down wrong path. 如上所述,我正在使用perl中的其他代码来创建我的解决方案,并且perl脚本使用了一个名为“ search”的函数来执行LDAP请求,这导致我走错了路。

Thanks to those who took a peek at the question! 感谢那些偷看了这个问题的人!

-- -

As per the requests in the comments, here's the basics of the solution in code. 根据注释中的要求,这是代码中解决方案的基础。 I'm extracting from an object that I use so this might not be 100% but it'll be close. 我正在从使用的对象中提取数据,所以它可能不是100%,但是会很接近。 Also, variables not declared in this snipped (eg $server, $user, $password) are for you to figure out; 另外,您要弄清楚在此摘要中未声明的变量(例如$ server,$ user,$ password); I won't know your AD credentials anyway! 无论如何,我不会知道您的广告凭据!

$ldap = ldap_connect($server);
ldap_bind($ldap, $user, $password);
$tokengroups = ldap_read($ldap, $dn, "CN=*", array("tokengroups")));
$tokengroups = ldap_get_entries($ldap, $tokengroups);

At this point, $tokengroups is our results as an array. 此时, $tokengroups是我们作为数组的结果。 it should have count index as well as some other information. 它应该具有计数索引以及其他一些信息。 To extract the actual groups, you'll need to do something like this: 要提取实际的组,您需要执行以下操作:

$groups = array();
if($tokengroups["count"] > 0) {
    $groups = $tokengroups[0]["tokengroups"];
    unset($groups["count"]);

    // if you want the SID's for your groups, you can stop here.
    // if you want to decode the SID's then you can do something like this.
    // the sid_decode() here: http://www.php.net/manual/en/function.unpack.php#72591

    foreach($groups as $i => &$sid) {
        $sid = sid_decode($sid);

        $sid_dn = ldap_read($ldap, "<SID=$sid>", "CN=*", array("dn"));
        if($sid_dn !== false) {
            $group = ldap_get_entries($ldap, $sid_dn);
            $group = $group["count"] == 1 ? $group[0]["dn"] : NULL;
            $groups[$i] = $group;
        }
    }
}

That's the basics. 这就是基础。 There's one caveat: you'll probably need to work with the individual or individuals who manage AD accounts at your organization. 有一个警告:您可能需要与组织中管理AD帐户的一个或多个个人合作。 The first time I tried to get this running (a few years ago, so my memory is somewhat fuzzy) the account that I was given did not have the appropriate authorization to access the token groups information. 我第一次尝试运行该命令(几年前,所以我的记忆有些模糊),我获得的帐户没有访问令牌组信息的适当授权。 I'm sure there are other ways to do this, but because I was porting someone else's code for this specific solution, this was how I did it. 我敢肯定还有其他方法可以做到这一点,但是由于我正在为该特定解决方案移植其他人的代码,因此我就是这样做的。

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

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