This is a script that I wrote based on LDAPSearch. I will also include a separate posting of functions based on dseditgroup.
<?php
//
// An assortment of functions for quering LDAP.
//
// Global variables:
// $Server - Name of the LDAP server to be queried.
// $Base - Search base for the LDAP server.
// $LDAPsearch - Fundamental command string component of the ldapsearch command to be used in the search.
//
// Member functions:
//
// GetLDAPelement($string)
// GetAppleGroupName($groupid)
// GetAppleGroupUID($groupname)
// GetAppleNestGroupUIDs($groupname)
// GetLDAPGroupMembership($groupname)
// IsLDAPGroupMember($username,$groupname)
// TestLDAPGroupMembership($username,$groupname,&$stack)
// LDAPTest()
//
global $Server;
global $Base;
global $LDAPsearch;
global $debug;
//$debug=1;
$Server="ldap.server.local";
$Base="dc=dc=me,dc=edu";
$LDAPsearch="/usr/bin/ldapsearch -LLL -x -h $Server -b \"cn=groups,${Base}\"";
function GetLDAPelement($string)
{
// LDAP returns its results with the form:
// <fieldname> : <value>
// We are interested in the value of the field, so we can strip off everything before the ":"
// and trim off leading and trailing empty space.
global $debug;
if( $debug) echo "Input string = $string\n";
$result=trim(substr($string,strpos($string,':')+1));
if( $debug) echo "Result string = $result\n";
return $result;
}
function GetAppleGroupName($groupid)
{
global $Server;
global $Base;
global $LDAPsearch;
$command="${LDAPsearch} \"(apple-generateduid=$groupid)\" cn | grep \"cn:\" ";
exec ($command,$result);
return GetLDAPelement($result[0]);
}
function GetAppleGroupUID($groupname)
{
//
// return the apple-generateduid for the specified $groupname.
//
global $Server;
global $Base;
global $LDAPsearch;
$command="$LDAPsearch \"(cn=$groupname)\" apple-generateduid | grep \"apple-generateduid:\" ";
exec ($command,$result);
return GetLDAPelement($result[0]);
}
function GetAppleNestGroupUIDs($groupname)
{
//
// Return the apple-group-nestedgroups given $groupname.
// A $groupname may belong to 0 or more nested groups.
//
global $Server;
global $Base;
global $LDAPsearch;
$command="$LDAPsearch \"(cn=$groupname)\" apple-group-nestedgroup | grep \"apple-group-nestedgroup:\" ";
exec ($command,$result);
for($i=0;$i<count($result);$i++)
{
$result[$i]=GetLDAPelement($result[$i]);
}
return $result;
}
function GetAppleSubGroupNames($groupid)
{
//
// Given a nested $groupid, return the names of the groups that have the same $groupid.
//
global $Server;
global $Base;
global $LDAPsearch;
$command="$LDAPsearch \"(apple-group-nestedgroup=$groupid)\" cn |grep \"cn:\"";
exec($command,$result);
for($i=0;$i<count($result);$i++)
{
$result[$i]=GetLDAPelement($result[$i]);
}
return($result);
}
function GetLDAPGroupMembership($groupname)
{
//
// Given $groupname, return the users that are direct members of the group.
// Nested groups are NOT checked. It only checks the "memberUid" elements of $groupname.
//
global $Server;
global $Base;
global $LDAPsearch;
$command="$LDAPsearch \"(cn=$groupname)\" memberUid | grep \"memberUid:\" ";
exec ($command,$result);
for($i=0;$i<count($result);$i++)
{
if( $debug) echo "Searching $result[$i]\n";
$result[$i]=GetLDAPelement($result[$i]);
}
return $result;
}
function IsLDAPGroupMember($username,$groupname)
{
//
// Determine if a $username is a member of a $groupname. Nested groups are checked.
//
$stack=array();
return TestLDAPGroupMembership($username,$groupname,$stack);
}
function TestLDAPGroupMembership($username,$groupname,&$stack)
{
// This is an internal function to be used as the "engine" of IsLDAPGroupMember().
//
// Test to see if $username is a direct or nested member of $groupname.
// The &$stack is passed by reference, and keeps managing the stack of ${groupname}s that
// have already been tested. This is necessary to make the function recursive without having
// infinite looping due to doubly nested groups.
//
global $debug;
// See if we have already searched this $groupname for the $username. If we have,
// we did not find it the last time, so we will not find it this time. We can stop here.
if( array_search($groupname,$stack)) return false;
if( $debug)echo "Searching for user $username in group $groupname.\n";
$result=GetLDAPGroupMembership($groupname);
if( $debug)echo "Searching direct membership.\n";
// If we find $username in the list of $result, $username IS a group member! We are Done!
if( array_search($username,$result)) return true;
// Add this $groupname to the $stack of groups that have been directly searched.
array_push($stack,$groupname);
if( $debug)echo "Searching sub groups\n";
$groupid=GetAppleNestGroupUIDs($groupname);
if( $debug)echo "Detected ".count($groupid)." groups.\n";
for($group=0;$group<count($groupid);$group++)
{
if( $debug)echo "Group id = $groupid[$group]\n";
$mastername=GetAppleGroupName($groupid[$group]);
if( $debug)echo "Master group name = $mastername\n";
if(TestLDAPGroupMembership($username,$mastername,$stack)) return true;
}
// If we have ended up here, no username was a match!
return false;
}
function LDAPTest()
{
echo "Group Membership test in nested group.\n";
if( IsLDAPGroupMember("root","user"))
{
echo "root is a member of user!\n";
}
else
{
echo "root is NOT a member of user!\n";
}
}
?>