Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a bug and adding support for custom save device days and latest RC elastic skin. #165

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

InputOutputZ
Copy link

@InputOutputZ InputOutputZ commented Aug 23, 2022

This is a PR for following proposed changes:-

Adding support for custom save device with X days
Adding support for recent Roundcube elastic skin, i.e. tested on 1.6.
Fixed the bug when activated already TFA is not being reflected in the settings.
Adding support for customised QR image colour in config options.

This is a PR for following proposed changes:-

Adding support to custom save device with X days
Adding support for recent Roundcube elastic skin, i.e. tested on 1.6.
Fixed the bug when activated already TFA is not being reflected in the settings.
@Sp1l
Copy link
Contributor

Sp1l commented Aug 26, 2022

Doesn't work for me. I get an error on the plugin settings page

Oops... something went wrong!

An internal error has occurred. Your request cannot be processed at this time.
For administrators: Please check the application and/or server error logs for more information.

Merged config changes in config.inc.php

//$rcmail_config['allow_save_device_30days'] = true;
$rcmail_config['allow_save_device_xdays'] = true;
$rcmail_config['save_device_xdays'] = 30;
$rcmail_config['qr_image_colour'] = "#000000";

FreeBSD 13.1 / PHP 8.1.9 / Roundcube 1.6.0

Can't find errors in the error.log or php-fpm logs

@InputOutputZ
Copy link
Author

I have tested on php 8.1.3 and RC 1.6 and it works fine, anyhow I will carry more tests tomorrow and will keep you updated.

For the time being, I recommend to debug with following method, try to use log_error(print_r($anyvariable,true)) and figure out at which line the plugin breaks, also, try to check web server logs and RC logs/errors.log and I would appreciate if you can post any found errors, with thanks.

Zakaria.

@InputOutputZ
Copy link
Author

Doesn't work for me. I get an error on the plugin settings page

Oops... something went wrong!

An internal error has occurred. Your request cannot be processed at this time. For administrators: Please check the application and/or server error logs for more information.

Merged config changes in config.inc.php

//$rcmail_config['allow_save_device_30days'] = true;
$rcmail_config['allow_save_device_xdays'] = true;
$rcmail_config['save_device_xdays'] = 30;
$rcmail_config['qr_image_colour'] = "#000000";

FreeBSD 13.1 / PHP 8.1.9 / Roundcube 1.6.0

Can't find errors in the error.log or php-fpm logs

I made few changes, not sure if they will fix the bug, but I recommend to give it a go 👍 .

Also, make sure to set $rcmail_config['enable_ua2fa'] to false, if you are not using users_allowed_2fa option, or comment it out its enabling configuration. Also, make sure to adjust the language you use to reflect the following modified label:- $labels['dont_ask_me_xdays'] = 'Don't ask me codes again on this computer for % days'. Also, when cloning the repo, make sure to run git checkout patch-1.

Good luck.

Zakaria.

@Sp1l
Copy link
Contributor

Sp1l commented Aug 29, 2022

I made few changes, not sure if they will fix the bug, but I recommend to give it a go 👍 .

Thanks! Testing!

Also, make sure to set $rcmail_config['enable_ua2fa'] to false, if you are not using users_allowed_2fa option, or comment it out its enabling configuration. Also, make sure to adjust the language you use to reflect the following modified label:- $labels['dont_ask_me_xdays'] = 'Don't ask me codes again on this computer for % days'. Also, when cloning the repo, make sure to run git checkout patch-1.

Naturally added your repo to the remotes and pulled the patch-1 branch. Using that to generate diffs I can add to the FreeBSD port.

No luck with this yet, still get that same fatal error message. Any clue how I can enable more logs (without resorting to PHP xdebug)?

Whilst you're at it: $config['log_dir'] = '/var/log/roundcube/';

Should be able to reuse that as prefix in

private $_logs_file = '/logs/log_errors_2FA.txt';
...
        // log error into $_logs_file directory
        private function __logError() {
                file_put_contents(realpath(".").$this->_logs_file, date("Y-m-d H:i:s")."|".$_SERVER['HTTP_X_FORWARDED_FOR']."|".$_SERVER['REMOTE_ADDR']."\n", FILE_APPEND);
        }

@Sp1l
Copy link
Contributor

Sp1l commented Aug 29, 2022

Found and enabled the debug_logger plugin. Not much more info to be found.

[29-Aug-2022 17:26:35 +0200] start: Action: plugin.twofactor_gauthenticator. Task: settings.
[29-Aug-2022 17:26:35 +0200] end: Action: plugin.twofactor_gauthenticator. Task: settings.  - 0.0071 seconds

@InputOutputZ
Copy link
Author

InputOutputZ commented Aug 29, 2022

With that being said, I recommend this hacky way, open twofactor_gauthenticator.php and in each method at the beginning add

error_log(print_r("The plugin works fine so far ".$anyvariable,true))

Keep moving logging call down the lines until you find at which point the plugin breaks i.e. you wont find any new logs. I would appreciate if you can send over the results.

With thanks.

Zakaria.

@Sp1l
Copy link
Contributor

Sp1l commented Aug 30, 2022

Got something, added

ini_set('display_errors', 1);

early in the plugin, now I have something useful

Warning: Undefined array key "twofactor_gauthenticator" in /usr/local/www/roundcube/plugins/twofactor_gauthenticator/twofactor_gauthenticator.php on line 451

Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /usr/local/www/roundcube/plugins/twofactor_gauthenticator/twofactor_gauthenticator.php:291 Stack trace: #0 /usr/local/www/roundcube/program/include/rcmail_output_html.php(1484): twofactor_gauthenticator->twofactor_gauthenticator_form(Array) #1 [internal function]: rcmail_output_html->xml_command(Array) #2 /usr/local/www/roundcube/program/include/rcmail_output_html.php(1322): preg_replace_callback('/<roundcube:([-...', Array, '<roundcube:incl...') #3 /usr/local/www/roundcube/program/include/rcmail_output_html.php(825): rcmail_output_html->parse_xml('<roundcube:incl...') #4 /usr/local/www/roundcube/program/include/rcmail_output_html.php(654): rcmail_output_html->parse('plugin', false) #5 /usr/local/www/roundcube/plugins/twofactor_gauthenticator/twofactor_gauthenticator.php(232): rcmail_output_html->send('plugin') #6 /usr/local/www/roundcube/program/lib/Roundcube/rcube_plugin_api.php(575): twofactor_gauthenticator->twofactor_gauthenticator_init() #7 /usr/local/www/roundcube/program/include/rcmail.php(248): rcube_plugin_api->exec_action('plugin.twofacto...') #8 /usr/local/www/roundcube/index.php(278): rcmail->action_handler() #9 {main} thrown in /usr/local/www/roundcube/plugins/twofactor_gauthenticator/twofactor_gauthenticator.php on line 291

@Sp1l
Copy link
Contributor

Sp1l commented Aug 30, 2022

There you go! Fixes the error if no twofactor_gauthenticator element is found in the user's prefs.

diff --git a/twofactor_gauthenticator.php b/twofactor_gauthenticator.php
index 20fa225..978284c 100644
--- a/twofactor_gauthenticator.php
+++ b/twofactor_gauthenticator.php
@@ -445,7 +445,11 @@ class twofactor_gauthenticator extends rcube_plugin
                $user = $rcmail->user;

                $arr_prefs = $user->get_prefs();
-               return $arr_prefs['twofactor_gauthenticator'];
+               if ( array_key_exists('twofactor_gauthenticator') ) {
+                       return $arr_prefs['twofactor_gauthenticator'];
+               } else {
+                       return [];
+               }
        }

        // we can set array to NULL to remove

@Sp1l
Copy link
Contributor

Sp1l commented Aug 30, 2022

To update all localizations in one go, you can run

sed -i .orig 's/30days/xdays/;s/30/%/' localization/*.inc

@InputOutputZ
Copy link
Author

InputOutputZ commented Aug 30, 2022

There you go! Fixes the error if no twofactor_gauthenticator element is found in the user's prefs.

diff --git a/twofactor_gauthenticator.php b/twofactor_gauthenticator.php
index 20fa225..978284c 100644
--- a/twofactor_gauthenticator.php
+++ b/twofactor_gauthenticator.php
@@ -445,7 +445,11 @@ class twofactor_gauthenticator extends rcube_plugin
                $user = $rcmail->user;

                $arr_prefs = $user->get_prefs();
-               return $arr_prefs['twofactor_gauthenticator'];
+               if ( array_key_exists('twofactor_gauthenticator') ) {
+                       return $arr_prefs['twofactor_gauthenticator'];
+               } else {
+                       return [];
+               }
        }

        // we can set array to NULL to remove

I think you've missed the array in array_key_exists parameter, given this is its format.

Anyhow, I've just submitted this fix along in patch-1, and thanks for it as well as the sed tip, its very much appreciated.

@InputOutputZ
Copy link
Author

By the way, this is how it looked like before:-

Web Mode
Before-Web-Mode

Mobile Mode
Before-Mobile-Mode

After:-

Web Mode
After-Web-Mode

Mobile Mode
After-Mobile-Mode

With thanks.

Zakaria.

@Sp1l
Copy link
Contributor

Sp1l commented Aug 31, 2022

I think you've missed the array in array_key_exists parameter, given this is its format.

Anyhow, I've just submitted this fix along in patch-1, and thanks for it as well as the sed tip, its very much appreciated.

Yep, was monkey-patching that in live env and replicating back to the git repo.

Thanks for bringing this plugin up-to-date!!

@Sp1l
Copy link
Contributor

Sp1l commented Aug 31, 2022

The recovery codes don't show for me...

Improved logging a bit

diff --git a/twofactor_gauthenticator.php b/twofactor_gauthenticator.php
index 83f102d..6f3742b 100644
--- a/twofactor_gauthenticator.php
+++ b/twofactor_gauthenticator.php
@@ -19,8 +19,9 @@ class twofactor_gauthenticator extends rcube_plugin
 {
        private $_number_recovery_codes = 4;

-        // relative from RC home dir, not plugin directory
-        private $_logs_file = '/logs/log_errors_2FA.txt';
+        // relative to $config['log_dir'], not plugin directory
+        private $_log_dir = '.';
+        private $_log_file = 'twofactor_gauthenticator.txt';

     function init()
     {
@@ -32,7 +33,8 @@ class twofactor_gauthenticator extends rcube_plugin
        $this->add_hook('render_page', array($this, 'popup_msg_enrollment'));

        $this->load_config();
-
+       $this->_log_dir = realpath($rcmail->config->get('log_dir','.')).DIRECTORY_SEPARATOR;
+
                $allowedPlugin = $this->__pluginAllowedByConfig();

                // skipping all logic and plugin not appears
@@ -573,8 +575,8 @@ class twofactor_gauthenticator extends rcube_plugin


         // log error into $_logs_file directory
-        private function __logError() {
-                file_put_contents(realpath(".").$this->_logs_file, date("Y-m-d H:i:s")."|".$_SERVER['HTTP_X_FORWARDED_FOR']."|".$_SERVER['REMOTE_ADDR']."\n", FILE_APPEND);
+        private function __logError($msg = '') {
+                file_put_contents($this->_log_dir.$this->_log_file, date("Y-m-d H:i:s")."|".$_SERVER['HTTP_X_FORWARDED_FOR']."|".$_SERVER['REMOTE_ADDR']."|".$msg."\n", FILE_APPEND);
         }

 }

@InputOutputZ
Copy link
Author

I recommend to check browser console, it could be a Javascript issue or another array_key_exists missing, anyhow it works for me just fine.

@alexandregz
Copy link
Owner

I try to check with your code but code is incorrect every time I try to config with all fields.

Do you have test this?

@InputOutputZ
Copy link
Author

InputOutputZ commented Sep 4, 2022

I try to check with your code but code is incorrect every time I try to config with all fields.

Do you have test this?

Yes, I did test this on RC 1.6 and it works. Please try after recent commit. Since I made several changes from first commit.

freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this pull request Sep 18, 2022
 * Mark 8.1 compatible for Roundcube 1.6.0
 * Add patch [1]

[1]: alexandregz/twofactor_gauthenticator#165

PR:		265653
@InputOutputZ
Copy link
Author

An update, I just fixed one tiny bug in X days.

mekanix pushed a commit to mekanix/freebsd-ports that referenced this pull request Sep 21, 2022
 * Mark 8.1 compatible for Roundcube 1.6.0
 * Add patch [1]

[1]: alexandregz/twofactor_gauthenticator#165

PR:		265653
@Sp1l
Copy link
Contributor

Sp1l commented Oct 18, 2022

Hi,

@InputOutputZ Can you please fix the extraneous ( in https://github.com/InputOutputZ/twofactor_gauthenticator/blob/patch-1/twofactor_gauthenticator.php#L242?
and also complete the localization fix sed -i .orig 's/30days/xdays/;s/30/%/' localization/*.inc?

@alexandregz Saw your comment on your failing test(s), can you elaborate? Really would like to see a new release for this.

@InputOutputZ
Copy link
Author

Hi,

@InputOutputZ Can you please fix the extraneous ( in https://github.com/InputOutputZ/twofactor_gauthenticator/blob/patch-1/twofactor_gauthenticator.php#L242? and also complete the localization fix sed -i .orig 's/30days/xdays/;s/30/%/' localization/*.inc?

@alexandregz Saw your comment on your failing test(s), can you elaborate? Really would like to see a new release for this.

Just fixed it. Mind me, for localisation, I will leave it for @alexandregz to look after, since the code you provided works only for English localisation.

Zakaria.

@InputOutputZ
Copy link
Author

Tiny bug fix. Email address is set to undefined in the authenticator app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants