2016-04-28 15 views
0

的LDAP查找成員我有一個關於在Active Directory組的用戶成員,並與PHP抓住這種會員資格的問題。我最大的問題/情況是,我有一個網站,我做,基本上我試圖分配一個以關在Active Directory組的管理員,我知道如何檢查的地位元件上的帳戶,但我的問題是,有一些羣體那裏沒有顯示,並且沒有顯示的一個組是我需要的組。有沒有辦法檢查誰是該組的成員,而不是檢查該用戶是否爲該組的成員。另外,如果有人知道爲什麼某些羣體並不在我的搜索出現我寧願搜索會員,那是因爲那將是檢查一個簡單的邏輯聲明如果用戶是該組中,我的代碼如下,它的工作,但作爲我說有一些團體沒有出現,我想我讀了關於如何存儲會員資格的地方,以及是直接會員還是您是另一組團體的成員。我們用作默認組的一個組是域用戶,但是即使該組的成員身份是直接的,因爲在該成員中沒有人擁有該成員,因爲所有用戶都不是包含用戶的其他安全組。一組PHP

在這個程序中的某些時候,我需要將某些用戶標記爲「管理員」,並且最簡單的方法是存儲標誌VIA會話變量,如果他們是某個管理員組的成員,如上所述,我遇到的問題是用戶沒有出現在他們的一些組中,因此這不起作用,那些給管理員提供訪問權的組不會出現在我的MemberOf區域中。在最後階段,我將不得不通過5-6組並將所有成員添加到數據庫中,這是一個類似的問題,應該有類似的解決方案,所以如果我找出那一個,我可以用一塊石頭殺死兩隻鳥太。我的意思這個問題是我需要抓住一組,如像「用戶HR」並上傳所有的數據庫由給定名稱和姓以及其他領域的一些默認值,但我不知道該怎麼從一個組抓取用戶,我知道如何抓住用戶組,但即使這樣也不能抓住組中的所有組,並且如果我能夠撤消該組並檢查我自己的成員,這會讓事情變得很容易,我們目前正在使用的爲我們完成所有這些工作的應用程序都是在ASP.net中,但大約有10年的歷史,並且有一個管理員帳號用於訪問羣組等等,即使這樣做,我仍然不確定我會得到一個組的成員。

代碼:

<?php 
$ldap = ldap_connect("192.168.1.**"); 
$ldap_dn = "DC=************,DC=local"; 
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); 
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 
$access = NULL; 
if ($bind = ldap_bind($ldap, "***********\\" . $_POST['username'], $_POST['password'])) { 
    $filter = "(sAMAccountName=" . $_POST['username'] . ")"; 
    $attr = array("memberof","givenname","sn","mail"); 
    $result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server"); 
    $entries = ldap_get_entries($ldap, $result); 
    $givenname = $entries[0]['givenname'][0] . " " . $entries[0]['sn'][0]; 
    ldap_unbind($ldap); 
    //var_dump($entries[0]["sn"][0]); 
    //var_dump($givenname); 
    //var_dump($entries[0]); 
    // check groups 
    foreach($entries[0]['memberof'] as $grps) { 
     // is manager, break loop 
     //if (strpos($grps, $ldap_manager_group)) { $access = 2; break; } 
     // is user 
     //var_dump($grps); 
     if (strpos($grps, "****** * *** *****")) $access = "****** *"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "***")) $access = "***"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "***")) $access = "***"; 
    } 
    if ($access != NULL) { 
     // establish session variables 
     $_SESSION['user'] = $_POST['username']; 
     $_SESSION['access'] = $access; 
     $_SESSION['givenname'] = $givenname; 
     $_SESSION['email'] = $entries[0]['mail'][0]; 
     return true; 
     } else { 
     //echo "No rights?"; 
     // user has no rights 
     return false; 
    } 
} else { 
    //header("Location: login.php?Error=Invalid Identity"); 
    echo "Elese Here"; 
} 
?> 

編輯:

我一直在嘗試使用本教程:samjlevy.com我理解它的大部分,但我得到了一些錯誤:

Warning: ldap_search(): Search: Operations error in C:\inetpub\wwwroot\InOutBoard\test.php on line 62 

Warning: ldap_get_entries() expects parameter 2 to be resource, boolean given in C:\inetpub\wwwroot\InOutBoard\test.php on line 63 

Warning: array_shift() expects parameter 1 to be array, null given in C:\inetpub\wwwroot\InOutBoard\test.php on line 66 

Warning: Invalid argument supplied for foreach() in C:\inetpub\wwwroot\InOutBoard\test.php on line 72 
Array () 

它們都似乎與搜索,因爲它不工作它返回一個NULL設置結果,這是不會允許其他部分運行。我不知道我的$ ldap_dn是否正確,因爲我使用上面的php代碼中的同一個。我的佈局如下(我是新來這個,我相信這是正確的):DC =公司,DC =本地,所以它應該是我想要的組:CN =尋找對象,OU = lowestLevel歐,OU =組,OU =公司,DC =公司,DC =本地

我將不得不使用它作爲我的$ ldap_dn?

編輯2:(更新後的代碼)

此代碼顯示所有的用戶是在和工作得非常好,該組有編寫使用了類似的網頁採取了第二頁的方式其中一個組織,並抓住所有成員呢?

<?php 

$ldap = ldap_connect("192.168.1.**"); 
$ldap_dn = "DC=Company,DC=local"; 
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); 
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 
$access = NULL; 
if ($bind = ldap_bind($ldap, "**********\\" . $_POST['username'], $_POST['password'])) { 
    $filter = "(sAMAccountName=" . $_POST['username'] . ")"; 
    $attr = array("memberof","givenname","sn","mail","distinguishedname"); 
    $result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server"); 
    $entries = ldap_get_entries($ldap, $result); 
    $givenname = $entries[0]['givenname'][0] . " " . $entries[0]['sn'][0]; 
    //ldap_unbind($ldap); 
    //var_dump($entries[0]["sn"][0]); 
    //var_dump($givenname); 
    //var_dump($entries[0]); 
    var_dump($entries[0]['distinguishedname'][0]); 

    $gFilter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=".$entries[0]['distinguishedname'][0]."))"; 
    $gAttr = array("cn"); 
    $result1 = ldap_search($ldap, $ldap_dn, $gFilter, $gAttr) or exit("Unable to search LDAP server"); 
    $groups = ldap_get_entries($ldap, $result1); 
    var_dump($groups); 
    // check groups 
    foreach($entries[0]['memberof'] as $grps) { 
     // is manager, break loop 
     //if (strpos($grps, $ldap_manager_group)) { $access = 2; break; } 

     // is user 
     //var_dump($grps); 
     if (strpos($grps, "****** * *** *****")) $access = "****** *"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "***")) $access = "***"; 
     if (strpos($grps, "*** Group")) $access = "***"; 
     if (strpos($grps, "***")) $access = "***"; 

    } 

    if ($access != NULL) { 
     // establish session variables 
     $_SESSION['user'] = $_POST['username']; 
     $_SESSION['access'] = $access; 
     $_SESSION['givenname'] = $givenname; 
     $_SESSION['email'] = $entries[0]['mail'][0]; 
     return true; 
     } else { 
     //echo "No rights?"; 
     // user has no rights 
     return false; 
    } 
} else { 
    //header("Location: login.php?Error=Invalid Identity"); 
    echo "Elese Here"; 
} 
?> 

第二頁:這一頁應該找一羣看起來像它可能試圖這樣做,但不會挑選出組的成員,但我們的OU的,而不是一個成員。請看下面的代碼佈局以及它抓取的內容。

<?php 
function get_members($group=FALSE,$inclusive=FALSE) { 
    $ldap_host = "192.168.1.***"; 
    $ldap_dn = "OU=******,OU=*****,OU=**********,DC=Company,DC=local"; 
    $ldap_usr_dom = "@".$ldap_host; 
    $user = "*******"; 
    $password = "******"; 
    $keep = array(
     "samaccountname", 
     "distinguishedname" 
    ); 
    $ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP"); 
    ldap_bind($ldap, "REGION5SYSTEMS\\" . $user, $password) or die("Could not bind to LDAP");ry 
    if($group) $query = "(&"; else $query = ""; 
    $query .= "(&(objectClass=user)(objectCategory=person))"; 
    if(is_array($group)) { 
     // Looking for a members amongst multiple groups 
      if($inclusive) { 
       $query .= "(|"; 
      } else { 
       $query .= "(&"; 
      } 
      foreach($group as $g) $query .= "(memberOf=CN=$g,$ldap_dn)"; 
      $query .= ")"; 
    } elseif($group) { 
     $query .= "(memberOf=CN=$group,$ldap_dn)"; 
    } 
    if($group) $query .= ")"; else $query .= ""; 
    $results = ldap_search($ldap,$ldap_dn,$query); 
    $entries = ldap_get_entries($ldap, $results); 
    array_shift($entries); 
    $output = array(); // Declare the output array 
    $i = 0; // Counter 
    // Build output array 
    foreach($entries as $u) { 
     foreach($keep as $x) { 
      // Check for attribute 
      if(isset($u[$x][0])) $attrval = $u[$x][0]; else $attrval = NULL; 
      $output[$i][$x] = $attrval; 
     } 
     $i++; 
    } 

    return $output; 
} 

// Example Output 
print_r(get_members()); // Gets all users in 'Users' 
print_r(get_members("Group I'm search for")); // Gets all members of 'Test Group' 
?> 

所以我們的DC是DC =公司名稱,DC =本地,然後我們有文件夾和OU的和其中一個OU的被命名爲「公司名稱」,並嵌套在它是OU的如管理員計算機,聯繫人,組,等我看到的OU是用戶和嵌套在我們的大廈(他們使用我們的domian)和一個額外的OU這是爲這個項目的不同組織。該項目的背景是一名員工inoutBoard,所以我們在該OU中有3個安全組,一個是其他組織的成員,一個是我們組織的成員,另一個是來自我們組織和他們組織的經理,基本上是可以改變的人如果他們忘記這麼做的話,他人的狀態就是這樣。理想情況下,我們想要做的是擁有某種同步按鈕,它可以檢查這些組的成員,然後將它們上傳到數據庫以及一些默認值,例如不在辦公室的默認狀態,沒有描述或類似的。這些團體也不包含人員,他們還包含其他安全團體。如果我將$ ldap_dn設置爲「CN = Company,DC = Company,DC = Local」,那麼我們希望能夠找到這些組的成員,第二位我附加的代碼將有效。它將打印Users OU中的所有用戶,然後進入所有OU,然後從這些OU中打印用戶,除了我們實際需要的OU(即具有用於輸出板的組的OU)。如果我將該路徑指定爲$ ldap_dn,那麼它只會打印array()而不會顯示其他任何內容。任何想法?

回答

0

memberOf屬性將只包含直接組成員資格,所以遞歸成員資格將不會被列出。但是,你可以專門查詢用戶的遞歸組成員像這樣(以適應你的代碼有點綁定後直接):

$filter = "(sAMAccountName=" . $_POST['username'] . ")"; 
$attr = array("givenname","sn","mail", "distinguishedname"); 
$result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server"); 
$entries = ldap_get_entries($ldap, $result); 

$gFilter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=".$entries[0]['distinguishedname'][0]."))"; 
$gAttr = array("cn"); 
$result = ldap_search($ldap, $ldap_dn, $gFilter, $gAttr) or exit("Unable to search LDAP server"); 
$groups = ldap_get_entries($ldap, $result); 

目前沒有測試,但是這是一般的過濾器和過程爲了做到這一點。獲取用戶的DN,然後使用匹配規則OID 1.2.840.113556.1.4.1941以遞歸方式獲取組。

您沒有得到「域用戶」組顯示的原因是因爲這是AD中「特殊」主要組,存儲在用戶的primaryGroupId屬性中。在這裏附加信息:

https://support.microsoft.com/en-us/kb/297951

+0

我試圖找出OID的規則,我必須改變這個數字的東西我拉在我結束? – AndyPet74

+0

此代碼給出了此錯誤:警告:ldap_search():4不是在C:\ inetpub \ wwwroot \ InOutBoard \ authorize.php上的有效ldap鏈接資源在22行這是否與gAttr區域有關? – AndyPet74

+0

不,OID將始終是該號碼。該過濾器中唯一真正改變的是最後(當前放置'distinguishedName'屬性值的地方,因爲這將根據用戶登錄的情況而改變)。至於你的錯誤......這似乎很奇怪。我查了很多次邏輯,我不確定你是怎麼做到的。我會從你的代碼中取出'ldap_unbind'語句開始,因爲這不應該被需要。您的腳本是否仍然與原始文章中的一樣? – ChadSikorra