Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * LDAP PHP Change Password Webpage
  5. * @author: Matt Rude <http://mattrude.com>
  6. * @website: http://technology.mattrude.com/2010/11/ldap-php-change-password-webpage/
  7. *
  8. *
  9. * GNU GENERAL PUBLIC LICENSE
  10. * Version 2, June 1991
  11. *
  12. * Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
  13. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14. * Everyone is permitted to copy and distribute verbatim copies
  15. * of this license document, but changing it is not allowed.
  16. */
  17.  
  18. $message = array();
  19. $message_css = "";
  20.  
  21. function changePassword($user,$oldPassword,$newPassword,$newPasswordCnf){
  22. global $message;
  23. global $message_css;
  24.  
  25. $domain = 'dc.lab';
  26. $server = "ldaps://LAB-DC01.".$domain;
  27. $dn = "CN=Users,DC=dc,DC=lab";
  28. $useradmin = "administrateur@dc.lab";
  29. $passwordadmin = "sk.fallen";
  30.  
  31. error_reporting(0);
  32. ldap_connect($server);
  33. $con = ldap_connect($server);
  34. ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
  35.  
  36. if (ldap_bind($con, $user . '@' . $domain, $oldPassword) === false) {
  37. $message[] = "Error E101 - Current Username or Password is wrong.";
  38. return false;
  39. }
  40.  
  41. // bind anon and find user by uid
  42. $user_search = ldap_search($con,$dn,"(&(objectClass=User)(sAMAccountName=$user))",array( "sAMAccountName", "mail", "givenName", "*"));
  43. $user_get = ldap_get_entries($con, $user_search);
  44. $user_entry = ldap_first_entry($con, $user_search);
  45. $user_dn = ldap_get_dn($con, $user_entry);
  46. $user_id = $user_get[0]["samaccountname"][0];
  47. $user_givenName = $user_get[0]["givenName"][0];
  48. $user_search_arry = array( "*", "ou", "sAMAccountName", "mail", "passwordRetryCount", "passwordhistory" );
  49. $user_search_filter = "(&(objectClass=User)(sAMAccountName=$user_id))";
  50. $user_search_opt = ldap_search($con,$user_dn,$user_search_filter,$user_search_arry);
  51. $user_get_opt = ldap_get_entries($con, $user_search_opt);
  52. $passwordRetryCount = $user_get_opt[0]["passwordRetryCount"][0];
  53. $passwordhistory = $user_get_opt[0]["passwordhistory"][0];
  54.  
  55. //$message[] = "Username: " . $user_id;
  56. //$message[] = "DN: " . $user_dn;
  57. //$message[] = "Current Pass: " . $oldPassword;
  58. //$message[] = "New Pass: " . $newPassword;
  59.  
  60. $mail = $user_get[0]["mail"][0];
  61.  
  62. $message[] = "PR:" . $user_id;
  63. $message[] = "PR:" . $mail;
  64. $message[] = "PR:" . $passwordRetryCount;
  65. $message[] = "PH:" . $passwordhistory;
  66.  
  67. /* Start the testing */
  68. if ( $passwordRetryCount == 3 ) {
  69. $message[] = "Error E101 - Your Account is Locked Out!!!";
  70. return false;
  71. }
  72. if ($newPassword != $newPasswordCnf ) {
  73. $message[] = "Error E102 - Your New passwords do not match!";
  74. return false;
  75. }
  76. $encoded_newPassword = iconv("UTF-8", "UTF-16LE", '"' . $newPassword . '"');
  77. $history_arr = ldap_get_values($con,$user_dn,"passwordhistory");
  78. if ( $history_arr ) {
  79. $message[] = "Error E102 - Your new password matches one of the last 10 passwords that you used, you MUST come up with a new password.";
  80. return false;
  81. }
  82. if (strlen($newPassword) < 8 ) {
  83. $message[] = "Error E103 - Your new password is too short.<br/>Your password must be at least 8 characters long.";
  84. return false;
  85. }
  86. if (!preg_match("/[0-9]/",$newPassword)) {
  87. $message[] = "Error E104 - Your new password must contain at least one number.";
  88. return false;
  89. }
  90. if (!preg_match("/[a-zA-Z]/",$newPassword)) {
  91. $message[] = "Error E105 - Your new password must contain at least one letter.";
  92. return false;
  93. }
  94. if (!preg_match("/[A-Z]/",$newPassword)) {
  95. $message[] = "Error E106 - Your new password must contain at least one uppercase letter.";
  96. return false;
  97. }
  98. if (!preg_match("/[a-z]/",$newPassword)) {
  99. $message[] = "Error E107 - Your new password must contain at least one lowercase letter.";
  100. return false;
  101. }
  102. if (!$user_get) {
  103. $message[] = "Error E200 - Unable to connect to server, you may not change your password at this time, sorry.";
  104. return false;
  105. }
  106.  
  107. $auth_entry = ldap_first_entry($con, $user_search);
  108. $mail_addresses = ldap_get_values($con, $auth_entry, "mail");
  109. $given_names = ldap_get_values($con, $auth_entry, "givenName");
  110. $password_history = ldap_get_values($con, $auth_entry, "passwordhistory");
  111. $mail_address = $mail_addresses[0];
  112. $first_name = $given_names[0];
  113.  
  114. /* And Finally, Change the password */
  115. $entry = array();
  116. $entry["unicodePwd"] = "$encoded_newPassword";
  117. ldap_bind($con,$useradmin,$passwordadmin);
  118.  
  119. if (ldap_modify($con,$user_dn,$entry) === false){
  120. $error = ldap_error($con);
  121. $errno = ldap_errno($con);
  122. $message[] = "E201 - Your password cannot be change, please contact the administrator.";
  123. $message[] = "$errno - $error";
  124. } else {
  125. $message_css = "yes";
  126. mail($mail_address,"Password change notice","Dear $first_name,
  127. Your password on http://support.example.com for account $user_id was just changed. If you did not make this change, please contact support@example.com.
  128. If you were the one who changed your password, you may disregard this message.
  129.  
  130. Thanks
  131. -Matt");
  132. $message[] = "The password for $user_id has been changed.<br/>An informational email as been sent to $mail_address.<br/>Your new password is now fully Active.";
  133. }
  134. }
  135.  
  136. ?>
  137. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  138. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  139. <head>
  140. <title>Password Change Page</title>
  141. <style type="text/css">
  142. body { font-family: Verdana,Arial,Courier New; font-size: 0.7em; }
  143. th { text-align: right; padding: 0.8em; }
  144. #container { text-align: center; width: 500px; margin: 5% auto; }
  145. .msg_yes { margin: 0 auto; text-align: center; color: green; background: #D4EAD4; border: 1px solid green; border-radius: 10px; margin: 2px; }
  146. .msg_no { margin: 0 auto; text-align: center; color: red; background: #FFF0F0; border: 1px solid red; border-radius: 10px; margin: 2px; }
  147. </style>
  148. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  149. </head>
  150. <body>
  151. <div id="container">
  152. <h2>Password Change Page</h2>
  153. <p>Your new password must be 8 characters long or longer and have at least:<br/>
  154. one capital letter, one lowercase letter, &amp; one number.<br/>
  155. You must use a new password, your current password<br/>can not be the same as your new password.</p>
  156. <?php
  157. if (isset($_POST["submitted"])) {
  158. changePassword($_POST['username'],$_POST['oldPassword'],$_POST['newPassword1'],$_POST['newPassword2']);
  159. global $message_css;
  160. if ($message_css == "yes") {
  161. ?><div class="msg_yes"><?php
  162. } else {
  163. ?><div class="msg_no"><?php
  164. $message[] = "Your password was not changed.";
  165. }
  166. foreach ( $message as $one ) { echo "<p>$one</p>"; }
  167. ?></div><?php
  168. } ?>
  169. <form action="<?php print $_SERVER['PHP_SELF']; ?>" name="passwordChange" method="post">
  170. <table style="width: 400px; margin: 0 auto;">
  171. <tr><th>Username or Email Address:</th><td><input name="username" type="text" size="20px" autocomplete="off" /></td></tr>
  172. <tr><th>Current password:</th><td><input name="oldPassword" size="20px" type="password" /></td></tr>
  173. <tr><th>New password:</th><td><input name="newPassword1" size="20px" type="password" /></td></tr>
  174. <tr><th>New password (again):</th><td><input name="newPassword2" size="20px" type="password" /></td></tr>
  175. <tr><td colspan="2" style="text-align: center;" >
  176. <input name="submitted" type="submit" value="Change Password"/>
  177. <button onclick="$('frm').action='changepassword.php';$('frm').submit();">Cancel</button>
  178. </td></tr>
  179. </table>
  180. </form>
  181. </div>
  182. </body>
  183. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement