Files
postfixadmin/public/users/password.php
T
David Goodwin 8a1f9a11af CSRF protection - support multiple tokens (#985)
possible fix for https://github.com/postfixadmin/postfixadmin/issues/967

CSRF Token changes : rather than have one PFA_Token value in $_SESSION, store a list of them, where each value has a time stamp it was issued on. The aim is to allow the user to have multiple browser tabs open at once - and not have the last tab that loaded trample on others. Tokens expire after 1 hour.

Adds a Smarty helper function `{CSRF_Token}` which will output `<input type=hidden name=CSRF_Token value=asdf123>` or `{CSRF_Token type=url}` which outputs a literal string like 'asdf123' suitable for placing in a URL.

Adds a PHP exception handler in public/common.php, which could be used to display a more friendly error message/response (but at the moment is fairly minimal).

Removes most uses of `die()` within the codebase, and instead moves to throw an exception
2026-03-01 19:30:16 +00:00

97 lines
2.5 KiB
PHP

<?php
/**
* Postfix Admin
*
* LICENSE
* This source file is subject to the GPL license that is bundled with
* this package in the file LICENSE.TXT.
*
* Further details on the project are available at https://github.com/postfixadmin/postfixadmin
*
* @version $Id$
* @license GNU GPL v2 or later.
*
* File: password.php
* Used by users to change their mailbox (and login) password.
* Template File: password.tpl
*
* Template Variables:
*
* none
*
* Form POST \ GET Variables:
*
* fPassword_current
* fPassword
* fPassword2
*/
require_once('../common.php');
$smarty = PFASmarty::getInstance();
$smarty->configureTheme('../');
authentication_require_role('user');
$username = authentication_get_username();
$pPassword_password_text = "";
$pPassword_password_current_text = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
CsrfToken::assertValid(safepost('CSRF_Token'));
if (isset($_POST['fCancel'])) {
header("Location: main.php");
exit(0);
}
$fPassword_current = $_POST['fPassword_current'];
$fPassword = $_POST['fPassword'];
$fPassword2 = $_POST['fPassword2'];
$error = 0;
$validpass = validate_password($fPassword);
if (count($validpass) > 0) {
flash_error($validpass[0]); # TODO: honor all error messages, not only the first one
$error += 1;
}
$login = new Login('mailbox');
if (!$login->login($username, $fPassword_current)) {
$error += 1;
$pPassword_password_current_text = $PALANG['pPassword_password_current_text_error'];
}
if (empty($fPassword) or ($fPassword != $fPassword2)) {
$error += 1;
$pPassword_password_text = $PALANG['pPassword_password_text_error'];
}
if ($error == 0) {
try {
if ($login->changePassword($username, $fPassword, $fPassword_current)) {
flash_info(Config::Lang_f('pPassword_result_success', $username));
header("Location: main.php");
exit(0);
} else {
flash_error(Config::Lang_f('pPassword_result_error', $username));
}
} catch (\Exception $e) {
flash_error($e->getMessage());
}
}
}
$smarty->assign('SESSID_USERNAME', $username);
$smarty->assign('pPassword_password_current_text', $pPassword_password_current_text, false);
$smarty->assign('pPassword_password_text', $pPassword_password_text, false);
$smarty->assign('smarty_template', 'password');
$smarty->display('index.tpl');
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */