Compare commits

...

4 Commits

Author SHA1 Message Date
dbabd89491 Backport Login changes
Password change backport
2018-05-09 15:11:06 +09:00
9842c979b6 Backport of missing password changes for Login class
move password check into method.
do proper check for password change.
remove all password log/error outputs. ever.
2018-05-09 12:26:01 +09:00
85a327f45f Backport new password interface to legacy classes 2018-05-09 11:47:16 +09:00
7b085f86f0 Change to <?php for core core classes
Basic/DB IO/db_pgsql switch only
2018-04-17 10:05:41 +09:00
5 changed files with 222 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
<?
<?php
/*********************************************************************
* AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org)
* CREATED: 2003/03/24
@@ -145,12 +145,13 @@ class basic
// error char for the char conver
public $mbErrorChar;
// crypt saslt prefix
// [!!! DEPRECATED !!!] crypt saslt prefix
public $cryptSaltPrefix = '';
public $cryptSaltSuffix = '';
public $cryptIterationCost = 7; // this is for staying backwards compatible with the old ones
public $cryptSaltSize = 22; // default 22 chars for blowfish, 2 for STD DES, 8 for MD5,
// new better password management
protected $password_options = array ();
// session name
private $session_name = '';
private $session_id = '';
@@ -337,8 +338,10 @@ class basic
$this->session_id = session_id();
}
// init crypt settings
// [!!! DEPRECATED !!!] init crypt settings
$this->cryptInit();
// new better password init
$this->passwordInit();
// start logging running time
$this->running_time();
@@ -1572,6 +1575,11 @@ class basic
return false;
}
// [!!! DEPRECATED !!!]
// ALL crypt* methids are DEPRECATED and SHALL NOT BE USED
// use the new password* instead
// [!!! DEPRECATED !!!] -> passwordInit
// METHOD: cryptInit
// PARAMS: none
// RETURN: none
@@ -1618,6 +1626,7 @@ class basic
}
}
// [!!! DEPRECATED !!!] -> not needed
// METHOD: cryptSaltString
// PARAMS: random string length, default is 22 (for blowfish crypt)
// RETURN: random string
@@ -1645,6 +1654,7 @@ class basic
return $salt_string;
}
// [!!! DEPRECATED !!!] -> passwordSet
// METHOD: cryptString
// PARAMS: string to be crypted (one way)
// RETURN: encrypted string
@@ -1656,6 +1666,7 @@ class basic
return crypt($string, $this->cryptSaltPrefix.$this->cryptSaltString($this->cryptSaltSize).$this->cryptSaltSuffix);
}
// [!!! DEPRECATED !!!] -> passwordVerify
// METHOD: verifyCryptString
// PARAMS: plain string (eg password)
// full crypted string (from cryptString
@@ -1671,6 +1682,61 @@ class basic
}
}
// *** BETTER PASSWORD OPTIONS, must be used ***
// METHOD: passwordInit
// PARAMS: none
// RETURN: none
// DESC : inits the password options set
// currently this is et empty, and the default options are used
private function passwordInit()
{
// set default password cost: use default set automatically
$this->password_options = array (
// 'cost' => PASSWORD_BCRYPT_DEFAULT_COST
);
}
// METHOD: passwordSet
// PARAMS: password
// RETURN: hashed password
// DESC : creates the password hash
public function passwordSet($password)
{
// always use the PHP default for the password
// password options ca be set in the password init, but should be kept as default
return password_hash($password, PASSWORD_DEFAULT, $this->password_options);
}
// METHOD: passwordVerify
// PARAMS: password and hash
// RETURN: true or false
// DESC : checks if the entered password matches the hash
public function passwordVerify($password, $hash)
{
if (password_verify($password, $hash)) {
return true;
} else {
return false;
}
// in case something strange, return false on default
return false;
}
// METHOD: passwordRehashCheck
// PARAMS: hash
// RETURN: true or false
// DESC : checks if the password needs to be rehashed
public function passwordRehashCheck($hash)
{
if (password_needs_rehash($hash, PASSWORD_DEFAULT, $this->password_options)) {
return true;
} else {
return false;
}
// in case of strange, force re-hash
return true;
}
// *** COLORS ***
// METHOD: hex2rgb
@@ -2070,3 +2136,5 @@ class basic
return $_SESSION[$name] === $token;
}
}
# __END__

View File

@@ -1,4 +1,5 @@
<?
<?php
/********************************************************************
* AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org)
* CREATED: 2000/11/23
@@ -1744,3 +1745,5 @@ class db_io extends basic
return $value;
}
} // end if db class
# __END__

View File

@@ -917,16 +917,16 @@ class form extends db_array_io
} // switch
} // for each error to check
} elseif ($value["mandatory"] &&
(
// for all "normal" fields
($this->table_array[$key]["type"] != "password" && $this->table_array[$key]["type"] != "drop_down_db_input" && !$this->table_array[$key]["value"]) ||
// for drop_down_db_input check if one of both fields filled
($this->table_array[$key]["type"] == "drop_down_db_input" && !$this->table_array[$key]["input_value"] && !$this->table_array[$key]["value"]) ||
// for password
($this->table_array[$key]["type"] == "password" && !$this->table_array[$key]["value"] && !$this->table_array[$key]["HIDDEN_value"])
)
// main if end
) {
(
// for all "normal" fields
($this->table_array[$key]["type"] != "password" && $this->table_array[$key]["type"] != "drop_down_db_input" && !$this->table_array[$key]["value"]) ||
// for drop_down_db_input check if one of both fields filled
($this->table_array[$key]["type"] == "drop_down_db_input" && !$this->table_array[$key]["input_value"] && !$this->table_array[$key]["value"]) ||
// for password
($this->table_array[$key]["type"] == "password" && !$this->table_array[$key]["value"] && !$this->table_array[$key]["HIDDEN_value"])
)
// main if end
) {
// if mandatory && no input
//$this->debug('form', "A: ".$this->table_array[$key]["type"]." -- ".$this->table_array[$key]["input_value"]." -- ".$this->table_array[$key]["value"]);
if (!$this->table_array[$key]["value"] && $this->table_array[$key]["type"] != "binary") {
@@ -1171,7 +1171,6 @@ class form extends db_array_io
// DESC save a table, reference and all input fields
public function form_save_table_array($addslashes = 0)
{
// global $_FILES;
// for drop_down_db_input check if text field is filled and if, if not yet in db ...
// and upload files
if (!is_array($this->table_array)) {
@@ -1234,8 +1233,8 @@ class form extends db_array_io
// if smth in $$key_file -> save or overwrite
// if smth in $key && $$key_delete && !$$key_file-> delte
// if smth in $key, keep as is
// $_file=$key."_file";
// $_delete=$key."_delete";
// $_file=$key."_file";
// $_delete=$key."_delete";
//$this->debug('form', "UF: ".$GLOBALS["_FILES"][$key."_file"]['name']);
//$this->debug('form', "delete: ".$key."_delete => ".$GLOBALS[$key.'_delete']);
if ($GLOBALS["_FILES"][$key."_file"]['name']) {
@@ -1266,11 +1265,11 @@ class form extends db_array_io
// for password crypt it as blowfish, or if not available MD5
if ($this->table_array[$key]['type'] == 'password') {
if ($this->table_array[$key]["value"]) {
// password is stored in blowfish format, or in the format supported by this PHP version
$this->table_array[$key]["value"] = $this->cryptString($this->table_array[$key]["value"]);
// use the better new passwordSet instead of crypt based
$this->table_array[$key]['value'] = $this->passwordSet($this->table_array[$key]['value']);
$this->table_array[$key]["HIDDEN_value"] = $this->table_array[$key]["value"];
} else {
// $this->table_array[$key]["HIDDEN_value"] =
// $this->table_array[$key]["HIDDEN_value"] =
}
}
} // go through each field

View File

@@ -70,8 +70,17 @@ class login extends db_io
private $logout; // logout button
private $login_error; // login error code, can be matched to the array login_error_msg, which holds the string
private $password_change = false; // if this is set to true, the user can change passwords
private $password_change_ok = false; // password change was successful
private $pw_change_deny_users = array (); // array of users for which the password change is forbidden
// if we have password change we need to define some rules
private $password_min_length = 8;
// can have several regexes, if nothing set, all is ok
private $password_valid_chars = array (
// '^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,}$',
// '^(?.*(\pL)u)(?=.*(\pN)u)(?=.*([^\pL\pN])u).{8,}',
);
// all possible login error conditions
private $login_error_msg = array ();
// this is an array holding all strings & templates passed from the outside (translation)
@@ -225,6 +234,55 @@ class login extends db_io
parent::__destruct();
}
// METHOD: loginPasswordCheck
// PARAMS: hash, optional password, to override
// RETURN: true or false
// DESC : checks if password is valid, sets internal error login variable
private function loginPasswordCheck($hash, $password = '')
{
$password_ok = false;
if (!$password) {
$password = $this->password;
}
if ((preg_match("/^\\$2(a|y)\\$/", $hash) && CRYPT_BLOWFISH != 1) ||
(preg_match("/^\\$1\\$/", $hash) && CRYPT_MD5 != 1) ||
(preg_match("/^\\$[0-9A-Za-z.]{12}$/", $hash) && CRYPT_STD_DES != 1)
) {
// this means password cannot be decrypted because of missing crypt methods
$this->login_error = 9999;
$password_ok = false;
} elseif ((preg_match("/^\\$2(a)\\$/", $hash) ||
// old password have $07$ so we check this
(preg_match("/^\\$2(y)\\$/", $hash) && preg_match("/\\$07\\$/", $hash)) ||
preg_match("/^\\$1\\$/", $hash) ||
preg_match("/^\\$[0-9A-Za-z.]{12}$/", $hash)) &&
!$this->verifyCryptString($password, $hash)
) {
// check passwword as crypted, $2a$ or $2y$ is blowfish start, $1$ is MD5 start, $\w{12} is standard DES
// this is only for OLD $07$ password
$this->login_error = 1011;
$password_ok = false;
} elseif (preg_match("/^\\$2y\\$/", $hash) &&
!$this->passwordVerify($password, $hash)
) {
// this is the new password hash methid, is only $2y$
$this->login_error = 1013;
$password_ok = false;
} elseif (!preg_match("/^\\$2(a|y)\\$/", $hash) &&
!preg_match("/^\\$1\\$/", $hash) &&
!preg_match("/^\\$[0-9A-Za-z.]{12}$/", $hash) &&
$hash != $password
) {
// check old plain password, non case sensitive
$this->login_error = 1012;
$password_ok = false;
} else {
// all ok
$password_ok = true;
}
return $password_ok;
}
// METHOD: login_login_user
// PARAMS: none
// RETURN: none
@@ -280,16 +338,17 @@ class login extends db_io
} elseif ($res['locked']) {
// user is locked, either set or auto set
$this->login_error = 105;
} elseif ((preg_match("/^\\$2(a|y)\\$/", $res['password']) && CRYPT_BLOWFISH != 1) || (preg_match("/^\\$1\\$/", $res['password']) && CRYPT_MD5 != 1) || (preg_match("/^\\$[0-9A-Za-z.]{12}$/", $res['password']) && CRYPT_STD_DES != 1)) {
// this means password cannot be decrypted because of missing crypt methods
$this->login_error = 9999;
} elseif ((preg_match("/^\\$2(a|y)\\$/", $res['password']) || preg_match("/^\\$1\\$/", $res['password']) || preg_match("/^\\$[0-9A-Za-z.]{12}$/", $res['password'])) && !$this->verifyCryptString($this->password, $res['password'])) {
// check passwword as crypted, $2a$ or $2y$ is blowfish start, $1$ is MD5 start, $\w{12} is standard DES
$this->login_error = 1011;
} elseif (!preg_match("/^\\$2(a|y)\\$/", $res['password']) && !preg_match("/^\\$1\\$/", $res['password']) && !preg_match("/^\\$[0-9A-Za-z.]{12}$/", $res['password']) && $res['password'] != $this->password) {
// check old plain password, non case sensitive
$this->login_error = 1012;
} elseif (!$this->loginPasswordCheck($res['password'])) {
// none to be set, set in login password check
} else {
// check if the current password is an invalid hash and do a rehash and set password
// $this->debug('LOGIN', 'Hash: '.$res['password'].' -> VERIFY: '.($this->passwordVerify($this->password, $res['password']) ? 'OK' : 'FAIL').' => HASH: '.($this->passwordRehashCheck($res['password']) ? 'NEW NEEDED' : 'OK'));
if ($this->passwordRehashCheck($res['password'])) {
$new_hash = $this->passwordSet($this->password);
// update password hash to new one now
$q = "UPDATE edit_user SET password = '".$this->dbEscapeString($new_hash)."' WHERE edit_user_id = ".$res['edit_user_id'];
$this->dbExec($q);
}
// normal user processing
// set class var and session var
$_SESSION["EUID"] = $this->euid = $res["edit_user_id"];
@@ -608,6 +667,28 @@ class login extends db_io
}
}
// METHOD: loginPasswordChangeValidPassword
// PARAMS: the new password
// RETURN: true or false
// DESC : checks if the password is in a valid format
private function loginPasswordChangeValidPassword($password)
{
$is_valid_password = true;
// check for valid in regex arrays in list
if (is_array($this->password_valid_chars)) {
foreach ($this->password_valid_chars as $password_valid_chars) {
if (!preg_match("/$password_valid_chars/", $password)) {
$is_valid_password = false;
}
}
}
// check for min length
if (strlen($password) < $this->password_min_length) {
$is_valid_password = false;
}
return $is_valid_password;
}
// METHOD: login_password_change
// PARAMS: none
// RETURN: none
@@ -634,9 +715,9 @@ class login extends db_io
}
// check old passwords match -> error
if (!$this->login_error) {
$q = "SELECT edit_user_id FROM edit_user WHERE enabled = 1 AND username = '".$this->db_escape_string($this->pw_username)."' AND password = '".$this->db_escape_string($this->pw_old_password)."'";
list ($edit_user_id) = $this->db_return_row($q);
if (!$edit_user_id) {
$q = "SELECT edit_user_id, password FROM edit_user WHERE enabled = 1 AND username = '".$this->dbEscapeString($this->pw_username)."'";
list ($edit_user_id, $old_password_hash) = $this->dbReturnRow($q);
if (!$edit_user_id || !$this->loginPasswordCheck($old_password_hash, $this->pw_old_password)) {
// old password wrong
$this->login_error = 202;
$data = 'The old password does not match';
@@ -653,15 +734,23 @@ class login extends db_io
if (!$this->login_error) {
if ($this->pw_new_password != $this->pw_new_password_confirm) {
$this->login_error = 204;
$data = 'The new passwords do not match: '.$this->pw_new_password.' == '.$this->pw_new_password_confirm;
$data = 'The new passwords do not match';
}
}
// password shall match to something in minimum length or form
if (!$this->login_error) {
if (!$this->loingPasswordChangeValidPassword($this->pw_new_password)) {
$this->login_error = 205;
$data = 'The new password string is not valid';
}
}
// no error change this users password
if (!$this->login_error) {
// update the user (edit_user_id) with the new password
$q = "UPDATE edit_user SET password = '".$this->db_escape_string($this->cryptString($this->pw_new_password))."' WHERE edit_user_id = ".$edit_user_id;
$q = "UPDATE edit_user SET password = '".$this->db_escape_string($this->passwordSet($this->pw_new_password))."' WHERE edit_user_id = ".$edit_user_id;
$this->db_exec($q);
$data = 'Password change for user "'.$this->pw_username.'" from "'.$this->pw_old_password.'" to "'.$this->pw_new_password.'"';
$data = 'Password change for user "'.$this->pw_username.'"';
$this->password_change_ok = true;
}
} else {
// illegal user error
@@ -669,7 +758,7 @@ class login extends db_io
$data = 'Illegal user for password change: '.$this->pw_username;
}
// log this password change attempt
$this->write_log($event, $data, $this->login_error, $pw_username, $pw_old_password);
$this->write_log($event, $data, $this->login_error, $pw_username);
} // button pressed
}
@@ -701,6 +790,18 @@ class login extends db_io
$html_string_password_change = str_replace("{".$string."}", $data, $html_string_password_change);
}
}
// print error messagae
if ($this->login_error) {
$html_string_password_change = str_replace("{ERROR_MSG}", $this->login_error_msg[$this->login_error]."<br>", $html_string_password_change);
} else {
$html_string_password_change = str_replace("{ERROR_MSG}", "<br>", $html_string_password_change);
}
// if pw change action, show the float again
if ($this->change_password && !$this->password_change_ok) {
$html_string_password_change = str_replace('{PASSWORD_CHANGE_SHOW}', '<script language="JavaScript">ShowHideDiv(\'pw_change_div\');</script>', $html_string_password_change);
} else {
$html_string_password_change = str_replace('{PASSWORD_CHANGE_SHOW}', '', $html_string_password_change);
}
$this->login_template['strings']['PASSWORD_CHANGE_DIV'] = $html_string_password_change;
}
@@ -714,6 +815,8 @@ class login extends db_io
// print error messagae
if ($this->login_error) {
$html_string = str_replace("{ERROR_MSG}", $this->login_error_msg[$this->login_error]."<br>", $html_string);
} elseif ($this->password_change_ok && $this->password_change) {
$html_string = str_replace('{ERROR_MSG}', $this->login_error_msg[300].'<br>', $html_string);
} else {
$html_string = str_replace("{ERROR_MSG}", "<br>", $html_string);
}
@@ -752,7 +855,7 @@ class login extends db_io
$q = "SELECT username, password FROM edit_user WHERE edit_user_id = ".$this->euid;
list($username, $password) = $this->db_return_row($q);
} // if euid is set, get username (or try)
$this->write_log($event, '', $this->login_error, $username, $password);
$this->write_log($event, '', $this->login_error, $username);
} // write log under certain settings
// now close DB connection
// $this->error_msg = $this->_login();
@@ -795,6 +898,8 @@ class login extends db_io
"202" => $this->l->__("Fatal Error: <b>Password change - The old password is not correct</b>"),
"203" => $this->l->__("Fatal Error: <b>Password change - Please fill out both new password fields</b>"),
"204" => $this->l->__("Fatal Error: <b>Password change - The new passwords do not match</b>"),
"205" => $this->l->__("Fatal Error: <b>Password change - The new password is not in a valid format</b>"), // we should also not here WHAT is valid
"300" => $this->l->__("Success: <b>Password change successful</b>"), // for OK password change
"9999" => $this->l->__("Fatal Error: <b>necessary crypt engine could not be found</b>. Login is impossible") // this is bad bad error
);
@@ -821,6 +926,7 @@ class login extends db_io
<tr><td></td><td><input type="submit" name="change_password" value="{PASSWORD_CHANGE_BUTTON_VALUE}"><input type="button" name="pw_change" value="{CLOSE}" OnClick="ShowHideDiv('pw_change_div');"></td></tr>
</table>
</div>
{PASSWORD_CHANGE_SHOW}
EOM;
} else {
$strings = array_merge($strings, array (
@@ -917,12 +1023,14 @@ EOM;
// error -> if error, write error string (not enougth data, etc)
// RETURN: none
// DESC : writes detailed data into the edit user log table (keep log what user does)
private function write_log($event, $data, $error = "", $username = "", $password = "")
private function write_log($event, $data, $error = '', $username = '')
{
if ($this->login) {
$this->action = 'Login';
} elseif ($this->logout) {
$this->action = 'Logout';
} else {
$this->action = '';
}
$_data_binary = array (
'_SESSION' => $_SESSION,
@@ -937,7 +1045,7 @@ EOM;
$q .= "(username, password, euid, event_date, event, error, data, data_binary, page, ";
$q .= "ip, user_agent, referer, script_name, query_string, server_name, http_host, http_accept, http_accept_charset, http_accept_encoding, session_id, ";
$q .= "action, action_id, action_yes, action_flag, action_menu, action_loaded, action_value, action_error) ";
$q .= "VALUES ('".$this->db_escape_string($username)."', '".$this->db_escape_string($password)."', ".(($this->euid) ? $this->euid : 'NULL').", ";
$q .= "VALUES ('".$this->db_escape_string($username)."', 'PASSWORD', ".(($this->euid) ? $this->euid : 'NULL').", ";
$q .= "NOW(), '".$this->db_escape_string($event)."', '".$this->db_escape_string($error)."', '".$this->db_escape_string($data)."', '".$data_binary."', '".$this->page_name."', ";
foreach (array('REMOTE_ADDR', 'HTTP_USER_AGENT', 'HTTP_REFERER', 'SCRIPT_FILENAME', 'QUERY_STRING', 'SERVER_NAME', 'HTTP_HOST', 'HTTP_ACCEPT', 'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_ENCODING') as $server_code) {
if (array_key_exists($server_code, $_SERVER)) {

View File

@@ -1,4 +1,5 @@
<?
<?php
/*********************************************************************
* AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org)
* CREATED: 2003/04/09
@@ -393,3 +394,5 @@ class db_pgsql
return $output;
}
}
# __END__