2011-05-05 42 views
0

我正在使用以下脚本获取apache中登录的用户详细信息。它工作的一种享受。获取登录用户Email | NTLM/Apache/PHP

我很好奇,是否有可能从此登录用户的电子邮件地址?

我可以拉什么其他信息?名字和姓氏?

如果这不起作用,我该如何获取登录用户的电子邮件地址?可能吗?

在此先感谢

// This a copy taken 2008-08-21 from http://siphon9.net/loune/f/ntlm.php.txt to make sure the code is not lost. 
// For more information see: 
// http://blogs.msdn.com/cellfish/archive/2008/08/26/getting-the-logged-on-windows-user-in-your-apache-server.aspx 

// NTLM specs http://davenport.sourceforge.net/ntlm.html 

$headers = apache_request_headers(); 

if (!isset($headers['Authorization'])){ 
     header('HTTP/1.1 401 Unauthorized'); 
     header('WWW-Authenticate: NTLM'); 
     exit; 
} 

$auth = $headers['Authorization']; 

if (substr($auth,0,5) == 'NTLM ') { 
     $msg = base64_decode(substr($auth, 5)); 
     if (substr($msg, 0, 8) != "NTLMSSP\x00") 
       die('error header not recognised'); 

     if ($msg[8] == "\x01") { 
       $msg2 = "NTLMSSP\x00\x02"."\x00\x00\x00\x00". // target name len/alloc 
         "\x00\x00\x00\x00". // target name offset 
         "\x01\x02\x81\x01". // flags 
         "\x00\x00\x00\x00\x00\x00\x00\x00". // challenge 
         "\x00\x00\x00\x00\x00\x00\x00\x00". // context 
         "\x00\x00\x00\x00\x30\x00\x00\x00"; // target info len/alloc/offset 

       header('HTTP/1.1 401 Unauthorized'); 
       header('WWW-Authenticate: NTLM '.trim(base64_encode($msg2))); 
       exit; 
     } 
     else if ($msg[8] == "\x03") { 
       function get_msg_str($msg, $start, $unicode = true) { 
         $len = (ord($msg[$start+1]) * 256) + ord($msg[$start]); 
         $off = (ord($msg[$start+5]) * 256) + ord($msg[$start+4]); 
         if ($unicode) 
           return str_replace("\0", '', substr($msg, $off, $len)); 
         else 
           return substr($msg, $off, $len); 
       } 
       $user = get_msg_str($msg, 36); 
       $domain = get_msg_str($msg, 28); 
       $workstation = get_msg_str($msg, 44); 
       print $msg; 

       print "You are $user from $workstation.$domain"; 
     } 
} 

回答

0

对不起,先生的NTLM规范只是关于身份验证,并只提供用户名,域名和你的挑战散列的认证响应。您不会以这种方式获得姓名或电子邮件地址。

1

一旦你知道他们是谁,你总是可以使用php-ldap模块来查找更多关于登录用户的信息。此示例将输出您选择的专有名称的所有条目。你需要在这里做一些工作,为自己的情况提供信息。 Windows中的LDP工具非常方便查找要放在这里的信息。

<?php 
//specify the Distinguished Name 
$dn = "CN=Joe Bloggs,OU=SomeOU,DC=SomeDomain,DC=com"; 

$filter = "(sAMAccountName=bloggsj)"; 

$ad = ldap_connect("ldap://yourADserver"); 
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3); 
$bd = ldap_bind($ad,"[email protected]","secret") or die("couldn't bind to AD!"); 

$result = ldap_search($ad, $dn, $filter); 
$entries = ldap_get_entries($ad, $result); 

print_r($entries); 
?>