- Code: Select all
// ---------------- Start of LDAP authentication code ----------------
$auth_type="ldap"; // Possible values: ldap | mysql
$ldap_server="XX.XX.XX.XX";
$base_dn="DC=domain, DC=local";
$root_dn="domain\ldap";
$pass_dn="password";
if ($auth_type == "ldap"){
if($connect=@ldap_connect($ldap_server)){ // if connected to ldap server
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS,0);
// bind to ldap connection
if(($bind=@ldap_bind($connect , $root_dn, $pass_dn)) == false){
print "bind:__FAILED__<br>\n";
return false;
}
// search for user
if (($res_id = ldap_search( $connect, $base_dn, "samaccountname=$user")) == false) {
print "failure: search in LDAP-tree failed<br>";
return false;
}
// verify if there is only one entry of this user:
if (ldap_count_entries($connect, $res_id) > 1) {
print "failure: user $user found more than once<br>\n";
return false;
}
elseif (ldap_count_entries($connect, $res_id) == 1){
//print "pllllll";
//return false;
if (( $entry_id = ldap_first_entry($connect, $res_id))== false) {
print "failur: entry of searchresult couln't be fetched<br>\n";
return false;
}
if (( $user_dn = ldap_get_dn($connect, $entry_id)) == false) {
print "failure: user-dn coulnd't be fetched<br>\n";
return false;
}
/* Authentifizierung des User */
if (($link_id = ldap_bind($connect, $user_dn, $pass)) == false) {
print "failure: username, password didn't match: $user_dn<br>\n";
return false;
}
// verify if user is already registered at database:
$sel0 = mysql_query("SELECT ID,pass FROM user WHERE name = '$user'");
$chk = mysql_fetch_array($sel0);
// if user already exists, just keep the password updated:
if ($chk["ID"] != "")
{
if ($chk["pass"] != $pass)
{
$this->admin_editpass($chk["ID"], $pass, $pass);
}
}
// if user isn't registered at database yet, add the user right now:
else
{
$filter = "(|(samaccountname=$user))";
$LDAPattributes = array("sn","givenname","mail");
$sr = ldap_search($connect, $base_dn, $filter, $LDAPattributes);
$entry = ldap_get_entries($connect, $sr);
//$entry[0]["mail"][0]
$newid = $this->add($user, $entry[0]["mail"][0], 0, $pass, /*$admin*/1, /*$sysloc*/"");
}
unset($chk);
// Now the database is updated the system can try the normal database auth
}
@ldap_close($connect);
}
}
// ---------------- End of LDAP authentication code ------------------