aboutsummaryrefslogtreecommitdiff
path: root/modules/private/websites/tools/tools/landing/ldap_password.php
blob: b6079e5449840098cceedc77e55f9732c606175c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

/**
 *   LDAP PHP Change Password Webpage
 *   @author:   Matt Rude <http://mattrude.com>
 *   @website:  http://technology.mattrude.com/2010/11/ldap-php-change-password-webpage/
 *
 *
 *              GNU GENERAL PUBLIC LICENSE
 *                 Version 2, June 1991
 *
 * Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Everyone is permitted to copy and distribute verbatim copies
 * of this license document, but changing it is not allowed.
 */

$message = array();
$message_css = "";

function changePassword($user,$oldPassword,$newPassword,$newPasswordCnf){
  global $message;
  global $message_css;

  $server = "ldaps://ldap.immae.eu";

  error_reporting(0);
  $con = ldap_connect($server);
  ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);

  $user_dn = "uid=$user,ou=users,dc=immae,dc=eu";

  if (ldap_bind($con, $user_dn, $oldPassword) === false) {
    $user_dn = "uid=$user,ou=group_users,dc=immae,dc=eu";
    if (ldap_bind($con, $user_dn, $oldPassword) === false) {
      $message[] = "Error E101 - Current Username or Password is wrong.";
      return false;
    }
  }
  if ($newPassword != $newPasswordCnf ) {
    $message[] = "Error E102 - Your New passwords do not match!";
    return false;
  }
  if (strlen($newPassword) < 6 ) {
    $message[] = "Error E103 - Your new password is too short.<br/>Your password must be at least 6 characters long.";
    return false;
  }

  $salt = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',4)),0,4); 
  $encoded_newPassword = "{SSHA}" . base64_encode(pack("H*", sha1($newPassword.$salt)).$salt);

  $user_search = ldap_search($con,"dc=immae,dc=eu","(uid=$user)");
  $auth_entry = ldap_first_entry($con, $user_search);

  $mail_addresses = ldap_get_values($con, $auth_entry, "mail");
  $given_names = ldap_get_values($con, $auth_entry, "givenName");
  $mail_address = $mail_addresses[0];
  $first_name = $given_names[0];

  /* And Finally, Change the password */
  $entry = array();
  $entry["userPassword"] = "$encoded_newPassword";

  if (ldap_modify($con,$user_dn,$entry) === false){
    $error = ldap_error($con);
    $errno = ldap_errno($con);
    $message[] = "E201 - Your password cannot be changed, please contact the administrator.";
    $message[] = "$errno - $error";
  } else {
    $message_css = "yes";
    mail($mail_address,"Password change notice","Dear $first_name,
Your password on https://tools.immae.eu/ldap_password.php for account $user was just changed.
If you did not make this change, please contact me.
If you were the one who changed your password, you may disregard this message.

Thanks
-- 
Immae / Ismaël", "From: " . getenv("CONTACT_EMAIL"));
    $message[] = "The password for $user has been changed.<br/>An informational email has been sent to $mail_address.<br/>Your new password is now fully active.";
  }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Password Change Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://assets.immae.eu/skeleton/2.0.4/skeleton.min.css" integrity="sha256-2YQRJMXD7pIAPHiXr0s+vlRWA7GYJEK0ARns7k2sbHY=" crossorigin="anonymous" />
<style type="text/css">
  body { font-family: Verdana,Arial,Courier New; margin: auto; }

  .msg_yes { margin: 0 auto; text-align: center; color: green; background: #D4EAD4; border: 1px solid green; border-radius: 10px; margin: 2px; }
  .msg_no { margin: 0 auto; text-align: center; color: red; background: #FFF0F0; border: 1px solid red; border-radius: 10px; margin: 2px; }
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div class="container">
<form action="<?php print $_SERVER['PHP_SELF']; ?>" name="passwordChange" method="post">
<h3>Password Change Page</h3>
<?php
if (isset($_POST["submitted"])) {
  echo '<div class="row">';
  changePassword($_POST['username'],$_POST['oldPassword'],$_POST['newPassword1'],$_POST['newPassword2']);
  global $message_css;
  if ($message_css == "yes") {
    echo '<div class="msg_yes">';
  } else {
    echo '<div class="msg_no">';
    $message[] = "Your password was not changed.";
  }
  foreach ( $message as $one ) { echo "<p>$one</p>"; }
  ?></div></div><?php
} ?>
  <div class="row">
    <div class="one-third column"><label for="username">Username</label></div>
    <div class="two-thirds column"><input id="username" name="username" type="text" autocomplete="off" /></div>
  </div>
  <div class="row">
    <div class="one-third column"><label for="oldPassword">Current password</label></div>
    <div class="two-thirds column"><input id="oldPassword" name="oldPassword" type="password" /></div>
  </div>
  <div class="row">
    <div class="one-third column"><label for="newPassword1">New password</label></div>
    <div class="two-thirds column"><input id="newPassword1" name="newPassword1" type="password" /></div>
  </div>
  <div class="row">
    <div class="one-third column"><label for="newPassword2">New password (again)</label></div>
    <div class="two-thirds column"><input id="newPassword2" name="newPassword2" type="password" /></div>
  </div>
  <div class="row">
    <div class="column">
      <input name="submitted" type="submit" value="Change Password"/>
    </div>
  </div>
</form>
</div>
</body>
</html>