Hallo,
Ik wil een AD wachtwoord reset script maken.
De eerste form is om de gebruiker uit AD te halen en de gegevens op te halen.
Als de gebruiker gelocked is dan wordt een mail gestuurd naar de gebruiker met een code.
Er verschijnt een tweede form om de code in te vullen. Na het drukken van de 2e submit moet de mail code vergeleken worden met wat in het code veld staat. Het laatste werkt niet.
Wat doe ik verkeerd? Als je het script kan verbeteren, dan hoor ik dat ook graag :)
Code:
<div class="reset">
<form name="form" method="post">
Username: <input type="text" name="username">
<input type="submit">
</form>
</div>
<?php
//------------------
// Connect to the LDAP server
//------------------
include '../Beheer/psl-config.php';
include '../Beheer/functions.php';
$ldapconn = ldap_connect($ADserver)
or die("Could not connect to LDAP server.");
if (FALSE === $ldapconn){
die("<p>Failed to connect to the LDAP server </p>");
}
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3)
or die("Unable to set LDAP protocol version");
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0); //we need this for doing an LDAP search
if (TRUE !== ldap_bind($ldapconn, $ldaprdn, $ldappass)){
die("<p>Failed to bind to LDAP server.</p>");
}
//------------------
// Get a list of all AD users
// https://www.geekshangout.com/php-example-get-data-active-directory-via-ldap/
//------------------
if(isset($_POST['username']) && !empty($_POST['username'])){
$username = htmlspecialchars ($_POST['username']);
}
$ldap_filter = "(&(objectCategory=user)(samaccountname=$username))";
$result = ldap_search($ldapconn, $ldap_base, $ldap_filter)
or die ("Error in search query:".ldap_error($ldapconn));
if (FALSE !== $result){
$GetEntries = ldap_get_entries($ldapconn, $result);
$FirstEntry = ldap_first_entry($ldapconn, $result);
// For each account returned by AD
for ($x=0; $x<$GetEntries['count']; $x++){
//
//Retrieve values from AD
//
//Windows Username
$LDAP_samaccountname = "";
if (!empty($GetEntries[$x]['samaccountname'][0])){
$LDAP_samaccountname = $GetEntries[$x]['samaccountname'][0];
if ($LDAP_samaccountname == "NULL"){
$LDAP_samaccountname = "";
}
}
else {
//#There is no samaccountname s0 assume this is an AD contact record so generate a unique username
$LDAP_uSNCreated = $GetEntries[$x]['usncreated'][0];
$LDAP_samaccountname = "CONTACT_" . $LDAP_uSNCreated;
}
//account status
$LDAP_status= "";
if (!empty($GetEntries[$x]['useraccountcontrol'][0])){
$LDAP_status = $GetEntries[$x]['useraccountcontrol'][0];
if ($LDAP_status == "NULL"){
$LDAP_status = "";
}
if ($LDAP_status == "16"){
$LDAP_status = "Lockout";
}
if ($LDAP_status == "512"){
$LDAP_status = "Enabled";
}
if ($LDAP_status == "514"){
$LDAP_status = "Disabled";
}
if ($LDAP_status == "544"){
$LDAP_status = "Enabled, Password not required";
}
if ($LDAP_status == "546"){
$LDAP_status = "Disabled, Password not required";
}
if ($LDAP_status == "66048"){
$LDAP_status = "Enabled, Password doesn't expire";
}
if ($LDAP_status == "66050"){
$LDAP_status = "Disabled, Password doesn't expire";
}
}
//Lockout
$LDAP_lockout= "";
$lockoutTime = ldap_get_values($ldapconn, $FirstEntry, "lockoutTime");
if ($lockoutTime[0] == 0){
$LDAP_lockout = "No";
$to = AD_Entries($GetEntries,'mail');
$test = Send_mail($to);
echo "line 106  " . $test . "<br/>";
}
if ($lockoutTime[0] == 1){
$LDAP_lockout = "Yes";
}
echo '<table border = "1">
<tr bgcolor="#cccccc">
<td>Username</td>
<td>Last Name</td>
<td>First Name</td>
<td>E-Mail Address</td>
<td>Account status</td>
<td>Lockout</td>
</tr>';
echo "<tr><td><strong>".$LDAP_samaccountname."</strong></td>";
echo "<td>";
echo AD_Entries($GetEntries,'sn');
echo "</td>";
echo "<td>";
echo AD_Entries($GetEntries,'givenname');
echo "</td>";
echo "<td>";
echo AD_Entries($GetEntries,'mail');
echo "</td>";
echo "<td>".$LDAP_status."</td>";
echo "<td>".$LDAP_lockout."</td></tr>";
}
if (isset($_POST['code']) && !empty($_POST['code'])){
$code = htmlspecialchars ($_POST['code']);
}
if (!empty($code)){
if ($code == $test){
echo "good";
}
else{
echo "bad";
echo "<br>";
echo "line 149  " . $code;
echo "<br>";
echo "line 151  " . $test;
}
}
if (!empty($test)){
echo "line 156  " . $test;
?>
<div class="code">
<form method="post">
Code: <input type="text" name="code">
<input type="submit">
</form>
</div>
<?php
}
//END for loop
}
ldap_unbind($ldapconn); // Clean up after ourselves.
echo("</table>"); //close the table
/*
To unlock:
$acctEntry["lockouttime"][0] = '1';
$mod = ldap_modify($ds, $dn, $acctEntry);
To lock:
$acctEntry["lockouttime"][0] = '0';
$mod = ldap_modify($ds, $dn, $acctEntry);
To enable:
$acctEntry["useraccountcontrol"][0] = '512';
$mod = ldap_modify($ds, $dn, $acctEntry);
To disable:
$acctEntry["useraccountcontrol"][0] = '514';
$mod = ldap_modify($ds, $dn, $acctEntry);
[mail]
https://blog.edmdesigner.com/sending-email-with-php/
*/
?>
functions.php
<?php
function AD_Entries($GetEntries,$Entries){
for ($x=0; $x<$GetEntries['count']; $x++){
Switch ($Entries){
default:
$LDAP_Entries = "";
if (!empty($GetEntries[$x][$Entries][0])){
$LDAP_Entries = $GetEntries[$x][$Entries][0];
if ($LDAP_Entries == "NULL"){
$LDAP_Entries = "";
}
}
return $LDAP_Entries;
}
}
}
function Send_mail($to){
$msg = uniqid();
$headers = 'From: [email protected]' . "\r\n" .
'Reply-to: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail= mail($to,'Test',$msg,$headers);
if($mail){
echo "message has been sent <br/>";
return $msg;
}
else {
echo "error";
}
}
?>
2.558 views