Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 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. $mail_address = $user_get[0]["mail"][0];
  49.  
  50. /*$user_search_arry = array( "*", "ou", "sAMAccountName", "mail", "passwordRetryCount", "passwordhistory" );
  51. $user_search_filter = "(&(objectClass=User)(sAMAccountName=$user_id))";
  52. $user_search_opt = ldap_search($con,$user_dn,$user_search_filter,$user_search_arry);
  53. $user_get_opt = ldap_get_entries($con, $user_search_opt);
  54. $passwordRetryCount = $user_get_opt[0]["passwordRetryCount"][0];
  55. $passwordhistory = $user_get_opt[0]["passwordhistory"][0];*/
  56.  
  57. //$message[] = "Username: " . $user_id;
  58. //$message[] = "DN: " . $user_dn;
  59. //$message[] = "Current Pass: " . $oldPassword;
  60. //$message[] = "New Pass: " . $newPassword;
  61.  
  62. //$mail = $user_get[0]["mail"][0];
  63.  
  64. $message[] = "PR:" . $user_id;
  65. $message[] = "PR:" . $mail;
  66. $message[] = "GN:" . $user_givenName;
  67. $message[] = "ML:" . $mail_address;
  68.  
  69. /* Start the testing */
  70. /*if ( $passwordRetryCount == 3 ) {
  71. $message[] = "Error E101 - Your Account is Locked Out!!!";
  72. return false;
  73. }*/
  74. if ($newPassword != $newPasswordCnf ) {
  75. $message[] = "Error E102 - Your New passwords do not match!";
  76. return false;
  77. }
  78. $encoded_newPassword = iconv("UTF-8", "UTF-16LE", '"' . $newPassword . '"');
  79. /*$history_arr = ldap_get_values($con,$user_dn,"passwordhistory");
  80. if ( $history_arr ) {
  81. $message[] = "Error E102 - Your new password matches one of the last 10 passwords that you used, you MUST come up with a new password.";
  82. return false;
  83. }*/
  84. if (strlen($newPassword) < 8 ) {
  85. $message[] = "Error E103 - Your new password is too short.<br/>Your password must be at least 8 characters long.";
  86. return false;
  87. }
  88. if (!preg_match("/[0-9]/",$newPassword)) {
  89. $message[] = "Error E104 - Your new password must contain at least one number.";
  90. return false;
  91. }
  92. if (!preg_match("/[a-zA-Z]/",$newPassword)) {
  93. $message[] = "Error E105 - Your new password must contain at least one letter.";
  94. return false;
  95. }
  96. if (!preg_match("/[A-Z]/",$newPassword)) {
  97. $message[] = "Error E106 - Your new password must contain at least one uppercase letter.";
  98. return false;
  99. }
  100. if (!preg_match("/[a-z]/",$newPassword)) {
  101. $message[] = "Error E107 - Your new password must contain at least one lowercase letter.";
  102. return false;
  103. }
  104. /*if (!$user_get) {
  105. $message[] = "Error E200 - Unable to connect to server, you may not change your password at this time, sorry.";
  106. return false;
  107. }*/
  108.  
  109. /*$auth_entry = ldap_first_entry($con, $user_search);
  110. $mail_addresses = ldap_get_values($con, $auth_entry, "mail");
  111. $given_names = ldap_get_values($con, $auth_entry, "givenName");
  112. $password_history = ldap_get_values($con, $auth_entry, "passwordhistory");
  113. $mail_address = $mail_addresses[0];
  114. $first_name = $given_names[0];*/
  115.  
  116. /* And Finally, Change the password */
  117. $entry = array();
  118. $entry["unicodePwd"] = "$encoded_newPassword";
  119. ldap_bind($con,$useradmin,$passwordadmin);
  120.  
  121. if (ldap_modify($con,$user_dn,$entry) === false){
  122. $error = ldap_error($con);
  123. $errno = ldap_errno($con);
  124. $message[] = "E201 - Your password cannot be change, please contact the administrator.";
  125. $message[] = "$errno - $error";
  126. } else {
  127. $message_css = "yes";
  128. mail($mail_address,"Password change notice","Dear $user_givenName,
  129. 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.
  130. If you were the one who changed your password, you may disregard this message.
  131.  
  132. Thanks
  133. -Matt");
  134. $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.";
  135. }
  136. }
  137.  
  138. ?>
  139. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  140. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  141. <head>
  142. <title>Password Change Page</title>
  143. <style type="text/css">
  144. body { font-family: Verdana,Arial,Courier New; font-size: 0.7em; }
  145. th { text-align: right; padding: 0.8em; }
  146. #container { text-align: center; width: 500px; margin: 5% auto; }
  147. .msg_yes { margin: 0 auto; text-align: center; color: green; background: #D4EAD4; border: 1px solid green; border-radius: 10px; margin: 2px; }
  148. .msg_no { margin: 0 auto; text-align: center; color: red; background: #FFF0F0; border: 1px solid red; border-radius: 10px; margin: 2px; }
  149. </style>
  150. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  151. </head>
  152. <body>
  153. <div id="container">
  154. <h2>Password Change Page</h2>
  155. <p>Your new password must be 8 characters long or longer and have at least:<br/>
  156. one capital letter, one lowercase letter, &amp; one number.<br/>
  157. You must use a new password, your current password<br/>can not be the same as your new password.</p>
  158. <?php
  159. if (isset($_POST["submitted"])) {
  160. changePassword($_POST['username'],$_POST['oldPassword'],$_POST['newPassword1'],$_POST['newPassword2']);
  161. global $message_css;
  162. if ($message_css == "yes") {
  163. ?><div class="msg_yes"><?php
  164. } else {
  165. ?><div class="msg_no"><?php
  166. $message[] = "Your password was not changed.";
  167. }
  168. foreach ( $message as $one ) { echo "<p>$one</p>"; }
  169. ?></div><?php
  170. } ?>
  171. <form action="<?php print $_SERVER['PHP_SELF']; ?>" name="passwordChange" method="post">
  172. <table style="width: 400px; margin: 0 auto;">
  173. <tr><th>Username or Email Address:</th><td><input name="username" type="text" size="20px" autocomplete="off" /></td></tr>
  174. <tr><th>Current password:</th><td><input name="oldPassword" size="20px" type="password" /></td></tr>
  175. <tr><th>New password:</th><td><input name="newPassword1" size="20px" type="password" /></td></tr>
  176. <tr><th>New password (again):</th><td><input name="newPassword2" size="20px" type="password" /></td></tr>
  177. <tr><td colspan="2" style="text-align: center;" >
  178. <input name="submitted" type="submit" value="Change Password"/>
  179. <button onclick="$('frm').action='changepassword.php';$('frm').submit();">Cancel</button>
  180. </td></tr>
  181. </table>
  182. </form>
  183. </div>
  184. </body>
  185. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement