Skip to content

Commit 9c428f3

Browse files
author
David Coutadeur
committed
add passwordCheck hook (#219)
1 parent 4e272a4 commit 9c428f3

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

conf/config.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
# "ignoreError" => false,
337337
# "encodebase64" => false
338338
# ),
339+
# "passwordCheck" => array(),
339340
# "passwordLock" => array(),
340341
# "passwordUnlock" => array(),
341342
# "accountEnable" => array(),
@@ -346,6 +347,7 @@
346347
# "deleteAccount" => array()
347348
#);
348349
# passwordReset, passwordLock,...: entrypoints triggering hooks
350+
# * passwordCheck: input: login, new password
349351
# * passwordReset: input: login, new password
350352
# * passwordLock: input: login
351353
# * passwordUnlock: input: login

htdocs/checkpassword.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
require_once("../conf/config.inc.php");
2525
require __DIR__ . '/../vendor/autoload.php';
26+
require_once("../lib/hook.inc.php");
2627

2728
# Connect to LDAP
2829
$ldap_connection = $ldapInstance->connect();
@@ -36,20 +37,38 @@
3637
error_log("LDAP - $dn not found using the configured search settings, reject request");
3738
} else {
3839

39-
if ($use_checkpasswordhistory) {
40-
$password_history = $ldapInstance->get_attribute_values($dn, "pwdHistory");
41-
foreach ($password_history as $previous_password) {
42-
preg_match("/(?<={).*(?=})/", $previous_password, $algorithm);
43-
preg_match("/{(?<={).*/", $previous_password, $hash);
44-
if (\Ltb\Password::check_password($password, $hash[0], $algorithm[0])) {
45-
$result = "passwordinhistory";
40+
41+
if ( isset($hook_login_attribute) ) {
42+
$hook_login = get_hook_login($dn, $ldapInstance, $hook_login_attribute);
43+
}
44+
45+
list($prehook_return, $prehook_message) =
46+
hook($prehook, 'passwordCheck', $hook_login, array( 'password' => $password ));
47+
48+
49+
if ( $prehook_return > 0 and !$prehook['passwordCheck']['ignoreError']) {
50+
$result = "passwordinvalid";
51+
} else {
52+
if ($use_checkpasswordhistory) {
53+
$password_history = $ldapInstance->get_attribute_values($dn, "pwdHistory");
54+
foreach ($password_history as $previous_password) {
55+
preg_match("/(?<={).*(?=})/", $previous_password, $algorithm);
56+
preg_match("/{(?<={).*/", $previous_password, $hash);
57+
if (\Ltb\Password::check_password($password, $hash[0], $algorithm[0])) {
58+
$result = "passwordinhistory";
59+
}
4660
}
4761
}
62+
63+
if (!$result) {
64+
$bind = ldap_bind($ldap, $dn, $password);
65+
$result = $bind ? "passwordok" : "passwordinvalid";
66+
}
4867
}
4968

50-
if (!$result) {
51-
$bind = ldap_bind($ldap, $dn, $password);
52-
$result = $bind ? "passwordok" : "passwordinvalid";
69+
if ( $result === "passwordok" ) {
70+
list($posthook_return, $posthook_message) =
71+
hook($posthook, 'passwordCheck', $hook_login, array( 'password' => $password ));
5372
}
5473

5574
}
@@ -59,4 +78,11 @@
5978
auditlog($audit_log_file, $dn, $audit_admin, "checkpassword", $result, NULL);
6079
}
6180

62-
header('Location: index.php?page=display&dn='.$dn.'&checkpasswordresult='.$result);
81+
$location = 'index.php?page=display&dn='.$dn.'&checkpasswordresult='.$result;
82+
if ( isset($prehook_return) and $prehook['passwordCheck']['displayError'] and $prehook_return > 0 ) {
83+
$location .= '&prehookresult='.$prehook_message;
84+
}
85+
if ( isset($posthook_return) and $posthook['passwordCheck']['displayError'] and $posthook_return > 0 ) {
86+
$location .= '&posthookresult='.$posthook_message;
87+
}
88+
header('Location: '.$location);

htdocs/resetpassword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
hook($prehook, 'passwordReset', $hook_login, array( 'password' => $password ));
9393

9494

95-
if ( $prehook_return > 0 and !$ignore_prehook_return) {
95+
if ( $prehook_return > 0 and !$prehook['passwordReset']['ignoreError']) {
9696
$result = "passwordrefused";
9797
} else {
9898
$reset = ($pwdreset === "true") ? true : false;

lib/hook.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function call_external_command($hookConfig, $entrypoint, $login_value, $params)
7676

7777
switch ($entrypoint) {
7878

79+
case "passwordCheck":
7980
case "passwordReset":
8081
$password = $params['password'];
8182
$command = password_hook_command($hookConfig[$entrypoint]['externalScript'],
@@ -131,6 +132,7 @@ function call_external_function($hookConfig, $entrypoint, $login_value, $params)
131132

132133
switch ($entrypoint) {
133134

135+
case "passwordCheck":
134136
case "passwordReset":
135137
$password = $params['password'];
136138
if( isset($hookConfig[$entrypoint]['encodebase64']) &&

0 commit comments

Comments
 (0)