2005-07-23 11:44:46 +02:00
|
|
|
<?php
|
2010-07-27 16:29:20 +02:00
|
|
|
/**
|
2019-08-29 12:48:44 +02:00
|
|
|
* EGroupware preferences: Security and passwords
|
2010-07-27 16:29:20 +02:00
|
|
|
*
|
|
|
|
* @package preferences
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Joseph Engo <jengo@phpgroupware.org>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
|
|
|
|
2016-04-30 19:16:36 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
use EGroupware\Api\Framework;
|
|
|
|
use EGroupware\Api\Etemplate;
|
2019-06-05 13:10:25 +02:00
|
|
|
use PragmaRX\Google2FAQRCode\Google2FA;
|
|
|
|
use EGroupware\Api\Mail\Credentials;
|
2016-04-30 19:16:36 +02:00
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
/**
|
|
|
|
* Security and passwords
|
|
|
|
*
|
|
|
|
* Other apps can add tabs to this popup by implementing the "preferences_security" hook
|
|
|
|
* like eg. the OpenID App does to allow users to revoke tokens.
|
|
|
|
*/
|
2014-04-17 09:37:21 +02:00
|
|
|
class preferences_password
|
2010-07-27 16:29:20 +02:00
|
|
|
{
|
|
|
|
var $public_functions = array(
|
|
|
|
'change' => True
|
|
|
|
);
|
2019-06-05 13:10:25 +02:00
|
|
|
const GAUTH_ANDROID = 'https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2';
|
|
|
|
const GAUTH_IOS = 'https://appstore.com/googleauthenticator';
|
2010-07-27 16:29:20 +02:00
|
|
|
|
2015-03-12 17:58:01 +01:00
|
|
|
/**
|
2019-06-05 13:10:25 +02:00
|
|
|
* Change password, two factor auth or revoke tokens
|
2015-03-12 17:58:01 +01:00
|
|
|
*
|
|
|
|
* @param type $content
|
|
|
|
*/
|
|
|
|
function change($content = null)
|
2005-07-23 11:44:46 +02:00
|
|
|
{
|
2019-09-11 12:15:23 +02:00
|
|
|
$GLOBALS['egw_info']['flags']['app_header'] = lang('Security & Password');
|
2019-06-05 13:10:25 +02:00
|
|
|
$tmpl = new Etemplate('preferences.password');
|
2015-07-01 14:44:34 +02:00
|
|
|
|
2019-06-05 13:10:25 +02:00
|
|
|
$readonlys = $sel_options = [];
|
|
|
|
try {
|
|
|
|
// PHP 7.1+: using SVG image backend (requiring XMLWriter) and not ImageMagic extension
|
|
|
|
if (class_exists('BaconQrCode\Renderer\Image\SvgImageBackEnd'))
|
|
|
|
{
|
|
|
|
$image_backend = new \BaconQrCode\Renderer\Image\SvgImageBackEnd;
|
|
|
|
}
|
|
|
|
$google2fa = new Google2FA($image_backend);
|
|
|
|
$prefs = new Api\Preferences($GLOBALS['egw_info']['user']['account_id']);
|
|
|
|
$prefs->read_repository();
|
|
|
|
|
|
|
|
if (!is_array($content))
|
|
|
|
{
|
|
|
|
$content = [];
|
|
|
|
$content['2fa'] = $this->generateQRCode($google2fa)+[
|
|
|
|
'gauth_android' => self::GAUTH_ANDROID,
|
|
|
|
'gauth_ios' => self::GAUTH_IOS,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$secret_key = $content['2fa']['secret_key'];
|
|
|
|
unset($content['2fa']['secret_key']);
|
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
// check user password for everything but password change, where it will be checked anyway
|
|
|
|
$auth = new Api\Auth();
|
|
|
|
if ($content['tabs'] !== 'change_password' &&
|
|
|
|
!$auth->authenticate($GLOBALS['egw_info']['user']['account_lid'], $content['password']))
|
2019-06-05 13:10:25 +02:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
$tmpl->set_validation_error('password', lang('Password is invalid'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch($content['tabs'])
|
|
|
|
{
|
|
|
|
case 'change_password':
|
2019-09-19 12:57:43 +02:00
|
|
|
if (!$GLOBALS['egw']->acl->check('nopasswordchange', 1) && $content['button']['save'])
|
2019-06-05 13:10:25 +02:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
if (($errors = self::do_change($content['password'], $content['n_passwd'], $content['n_passwd_2'])))
|
2019-06-05 13:10:25 +02:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
Framework::message(implode("\n", $errors), 'error');
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
Framework::refresh_opener(lang('Password changed'), 'preferences');
|
2019-06-05 13:10:25 +02:00
|
|
|
Framework::window_close();
|
|
|
|
}
|
2019-08-29 12:48:44 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-06-05 13:10:25 +02:00
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
case 'two_factor_auth':
|
2021-10-26 19:58:53 +02:00
|
|
|
switch(key($content['2fa']['action'] ?? []))
|
2019-08-29 12:48:44 +02:00
|
|
|
{
|
|
|
|
case 'show':
|
|
|
|
$content['2fa'] = $this->generateQRCode($google2fa, false);
|
|
|
|
break;
|
|
|
|
case 'reset':
|
|
|
|
$content['2fa'] = $this->generateQRCode($google2fa, true);
|
|
|
|
Framework::message(lang('New secret generated, you need to save it to disable the old one!'));
|
|
|
|
break;
|
|
|
|
case 'disable':
|
|
|
|
if (Credentials::delete(0, $GLOBALS['egw_info']['user']['account_id'], Credentials::TWOFA))
|
|
|
|
{
|
|
|
|
Framework::refresh_opener(lang('Secret deleted, two factor authentication disabled.'), 'preferences');
|
|
|
|
Framework::window_close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Framework::message(lang('Failed to delete secret!'), 'error');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // no action, save secret
|
|
|
|
if (!$google2fa->verifyKey($secret_key, $content['2fa']['code']))
|
|
|
|
{
|
|
|
|
$tmpl->set_validation_error('code', lang('Code is invalid'), '2fa');
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
if (($content['2fa']['cred_id'] = Credentials::write(0,
|
|
|
|
$GLOBALS['egw_info']['user']['account_lid'],
|
|
|
|
$secret_key, Credentials::TWOFA,
|
|
|
|
$GLOBALS['egw_info']['user']['account_id'],
|
|
|
|
$content['2fa']['cred_id'])))
|
|
|
|
{
|
|
|
|
Framework::refresh_opener(lang('Two Factor Auth enabled.'), 'preferences');
|
|
|
|
Framework::window_close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Framework::message(lang('Failed to store secret!'), 'error');
|
|
|
|
}
|
|
|
|
break;
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
2019-08-29 12:48:44 +02:00
|
|
|
unset($content['2fa']['action']);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// for other tabs call their save_callback (user password is already checked!)
|
|
|
|
if (!empty($content['save_callbacks'][$content['tabs']]) &&
|
|
|
|
($msg = call_user_func_array($content['save_callbacks'][$content['tabs']], [&$content])))
|
|
|
|
{
|
|
|
|
Framework::message($msg, 'success');
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
2019-08-29 12:48:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
Framework::message($e->getMessage(), 'error');
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:28:33 +02:00
|
|
|
// disable 2FA tab, if admin disabled it
|
|
|
|
if ($GLOBALS['egw_info']['server']['2fa_required'] === 'disabled')
|
|
|
|
{
|
|
|
|
$readonlys['tabs']['two_factor_auth'] = true;
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:15:23 +02:00
|
|
|
// disable password change, if user has not right to change it
|
|
|
|
if ($GLOBALS['egw']->acl->check('nopasswordchange', 1))
|
|
|
|
{
|
|
|
|
$readonlys['tabs']['change_password'] = true;
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
$preserve = [
|
2019-06-05 13:10:25 +02:00
|
|
|
'2fa' => $content['2fa']+[
|
|
|
|
'secret_key' => $secret_key,
|
2019-08-29 12:48:44 +02:00
|
|
|
]
|
|
|
|
];
|
2019-06-05 13:10:25 +02:00
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
$tmpl->setElementAttribute('tabs', 'add_tabs', true);
|
2023-01-11 21:21:01 +01:00
|
|
|
$tabs =& $tmpl->getElementAttribute('tabs', 'extraTabs');
|
2019-08-29 12:48:44 +02:00
|
|
|
if (($first_call = !isset($tabs)))
|
2019-06-05 13:10:25 +02:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
$tabs = array();
|
|
|
|
}
|
2023-07-11 13:39:46 +02:00
|
|
|
// register hooks, if new "application password" hook not yet registered (should be removed after 24.1)
|
|
|
|
if (!in_array('preferences', array_keys(Api\Hooks::implemented('preferences_security'))))
|
2019-08-29 12:48:44 +02:00
|
|
|
{
|
|
|
|
Api\Hooks::read(true);
|
|
|
|
}
|
2023-07-11 13:39:46 +02:00
|
|
|
$hook_data = Api\Hooks::process(array('location' => 'preferences_security')+$content, ['preferences', 'openid'], true);
|
2019-08-29 12:48:44 +02:00
|
|
|
foreach($hook_data as $extra_tabs)
|
|
|
|
{
|
|
|
|
if (!$extra_tabs) continue;
|
2019-06-05 13:10:25 +02:00
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
foreach(isset($extra_tabs[0]) ? $extra_tabs : [$extra_tabs] as $extra_tab)
|
|
|
|
{
|
|
|
|
if (!empty($extra_tab['data']) && is_array($extra_tab['data']))
|
2015-03-12 17:58:01 +01:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
$content = array_merge($content, $extra_tab['data']);
|
2013-07-14 15:05:24 +02:00
|
|
|
}
|
2019-08-29 12:48:44 +02:00
|
|
|
if (!empty($extra_tab['preserve']) && is_array($extra_tab['preserve']))
|
|
|
|
{
|
|
|
|
$preserve = array_merge($preserve, $extra_tab['preserve']);
|
|
|
|
}
|
|
|
|
if (!empty($extra_tab['sel_options']) && is_array($extra_tab['sel_options']))
|
|
|
|
{
|
|
|
|
$sel_options = array_merge($sel_options, $extra_tab['sel_options']);
|
|
|
|
}
|
|
|
|
if (!empty($extra_tab['readonlys']) && is_array($extra_tab['readonlys']))
|
|
|
|
{
|
|
|
|
$readonlys = array_merge($readonlys, $extra_tab['readonlys']);
|
|
|
|
}
|
|
|
|
if (!empty($extra_tab['save_callback']))
|
2013-06-23 12:46:26 +02:00
|
|
|
{
|
2019-08-29 12:48:44 +02:00
|
|
|
$preserve['save_callbacks'][$extra_tab['name']] = $extra_tab['save_callback'];
|
2013-06-23 12:46:26 +02:00
|
|
|
}
|
2019-08-29 12:48:44 +02:00
|
|
|
// we must NOT add tabs more then once!
|
|
|
|
if ($first_call && !empty($extra_tab['label']) && !empty($extra_tab['name']))
|
|
|
|
{
|
|
|
|
$tabs[] = array(
|
|
|
|
'label' => $extra_tab['label'],
|
|
|
|
'template' => $extra_tab['name'],
|
|
|
|
'prepend' => $extra_tab['prepend'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//error_log(__METHOD__."() changed tabs=".array2string($tabs));
|
2005-07-23 11:44:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-12 17:58:01 +01:00
|
|
|
|
2019-08-29 12:48:44 +02:00
|
|
|
$tmpl->exec('preferences.preferences_password.change', $content, $sel_options, $readonlys, $preserve, 2);
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
2010-07-27 16:29:20 +02:00
|
|
|
|
2019-06-05 13:10:25 +02:00
|
|
|
/**
|
|
|
|
* Generate QRCode and optional new secret
|
|
|
|
*
|
|
|
|
* @param Google2FA $google2fa
|
|
|
|
* @param boolean|null $generate =null null: generate new qrCode/secret, if none exists
|
|
|
|
* true: allways generate new qrCode (to reset existing one)
|
|
|
|
* false: use existing secret, but generate qrCode
|
|
|
|
* @return array with keys "qrc" and "cred_id"
|
|
|
|
*/
|
|
|
|
protected function generateQRCode(Google2FA $google2fa, $generate=null)
|
|
|
|
{
|
|
|
|
$creds = Credentials::read(0, Credentials::TWOFA, $GLOBALS['egw_info']['user']['account_id']);
|
|
|
|
|
|
|
|
if (!$generate && $creds && strlen($creds['2fa_password']) >= 16)
|
|
|
|
{
|
|
|
|
$secret_key = $creds['2fa_password'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$secret_key = $google2fa->generateSecretKey();//16, $GLOBALS['egw_info']['user']['account_lid']);
|
|
|
|
}
|
|
|
|
if (isset($generate) || empty($creds))
|
|
|
|
{
|
|
|
|
$image = $google2fa->getQRCodeInline(
|
|
|
|
!empty($GLOBALS['egw_info']['server']['site_title']) ?
|
|
|
|
$GLOBALS['egw_info']['server']['site_title'] : 'EGroupware',
|
|
|
|
$GLOBALS['egw_info']['user']['account_email'],
|
|
|
|
$secret_key
|
|
|
|
);
|
2019-06-17 12:29:37 +02:00
|
|
|
// bacon/bacon-qr-code >= 2 does not generate a data-url itself, but 1.x does :(
|
|
|
|
if (substr($image, 0, 11) !== 'data:image/')
|
|
|
|
{
|
|
|
|
$image = 'data:image/'.(substr($image, 0, 5) === '<?xml' ? 'svg+xml' : 'png').
|
|
|
|
';base64,'.base64_encode($image);
|
|
|
|
}
|
2019-06-05 13:10:25 +02:00
|
|
|
}
|
|
|
|
return [
|
2019-06-17 12:29:37 +02:00
|
|
|
'qrc' => $image,
|
|
|
|
'hide_qrc' => empty($image),
|
2019-06-05 13:10:25 +02:00
|
|
|
'cred_id' => !empty($creds) ? $creds['2fa_cred_id'] : null,
|
|
|
|
'secret_key' => $secret_key,
|
|
|
|
'status' => !empty($creds) ? lang('Two Factor Auth is already setup.') : '',
|
|
|
|
];
|
2005-07-23 11:44:46 +02:00
|
|
|
}
|
2015-07-01 14:44:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do some basic checks and then change password
|
|
|
|
*
|
|
|
|
* @param string $old_passwd
|
|
|
|
* @param string $new_passwd
|
|
|
|
* @param string $new_passwd2
|
|
|
|
* @return array with already translated errors
|
|
|
|
*/
|
|
|
|
public static function do_change($old_passwd, $new_passwd, $new_passwd2)
|
|
|
|
{
|
|
|
|
if ($GLOBALS['egw_info']['flags']['currentapp'] != 'preferences')
|
|
|
|
{
|
2016-04-30 19:16:36 +02:00
|
|
|
Api\Translation::add_app('preferences');
|
2015-07-01 14:44:34 +02:00
|
|
|
}
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if (isset($GLOBALS['egw_info']['user']['passwd']) &&
|
|
|
|
$old_passwd !== $GLOBALS['egw_info']['user']['passwd'])
|
|
|
|
{
|
|
|
|
$errors[] = lang('The old password is not correct');
|
|
|
|
}
|
|
|
|
if ($new_passwd != $new_passwd2)
|
|
|
|
{
|
|
|
|
$errors[] = lang('The two passwords are not the same');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($old_passwd !== false && $old_passwd == $new_passwd)
|
|
|
|
{
|
|
|
|
$errors[] = lang('Old password and new password are the same. This is invalid. You must enter a new password');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$new_passwd)
|
|
|
|
{
|
|
|
|
$errors[] = lang('You must enter a password');
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow auth backends or configured password strenght to throw exceptions and display there message
|
|
|
|
if (!$errors)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (!$GLOBALS['egw']->auth->change_password($old_passwd, $new_passwd,
|
|
|
|
$GLOBALS['egw']->session->account_id))
|
|
|
|
{
|
|
|
|
// if we have no specific error, add general message
|
|
|
|
$errors[] = lang('Failed to change password.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$errors[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $errors;
|
|
|
|
}
|
2023-07-11 13:39:46 +02:00
|
|
|
}
|