From 28b39ac709c49e6e794621eea938411cb0066b57 Mon Sep 17 00:00:00 2001 From: Ngesera Steven Date: Fri, 18 Oct 2024 19:57:11 +0300 Subject: [PATCH] Correct sieve connection when using scheme (#1199) * Correct sieve connection when using scheme * Make sieve tls mode dynamic to support all servers * Enable sieve check on login with auth_type DB * Sieve tls option in bulk import --------- Co-authored-by: Josaphat Imani --- .env.example | 1 + config/app.php | 12 ++++++++++-- lib/auth.php | 7 +++++-- modules/core/handler_modules.php | 7 +++++-- modules/core/setup.php | 1 + modules/core/site.js | 11 ++++++++++- modules/imap/functions.php | 13 ++++++------- modules/imap/handler_modules.php | 1 + modules/imap/output_modules.php | 18 ++++++++++++++---- .../nux/assets/data/server_accounts_sample.csv | 6 +++--- .../assets/data/server_accounts_sample.yaml | 6 ++++-- modules/nux/modules.php | 2 ++ modules/nux/services.php | 9 ++++++--- modules/sievefilters/hm-sieve.php | 4 ++-- modules/smtp/hm-smtp.php | 1 - scripts/create_config.php | 8 ++++++-- 16 files changed, 76 insertions(+), 31 deletions(-) diff --git a/.env.example b/.env.example index 20b898b1b1..1b227412c2 100644 --- a/.env.example +++ b/.env.example @@ -17,6 +17,7 @@ IMAP_AUTH_SERVER=localhost IMAP_AUTH_PORT=143 IMAP_AUTH_TLS= IMAP_AUTH_SIEVE_CONF_HOST= +IMAP_AUTH_SIEVE_TLS_MODE=true DEFAULT_SMTP_NAME= DEFAULT_SMTP_SERVER= diff --git a/config/app.php b/config/app.php index 63931704a8..1f0cf46162 100644 --- a/config/app.php +++ b/config/app.php @@ -135,10 +135,18 @@ /* | | The hostname/IP address and port sieve is listening on. Example: example.org:4190 - | Note: Add tls:// prefix to enable explicit STARTTLS + | Keep this blank to disable sieve filter support on login | */ - 'imap_auth_sieve_conf_host' => env('IMAP_AUTH_SIEVE_CONF_HOST', 'tls://mail.gandi.net:4190'), + 'imap_auth_sieve_conf_host' => env('IMAP_AUTH_SIEVE_CONF_HOST', ''), + + /* + | + | this will add the tls:// prefix to the sieve host if set to true + | to make this work make sure you set DEFAULT_SETTING_ENABLE_SIEVE_FILTER to true + | + */ + 'imap_auth_sieve_tls_mode' => env('IMAP_AUTH_SIEVE_TLS_MODE', true), /* | ------------------- diff --git a/lib/auth.php b/lib/auth.php index 5aec439ac4..71343b190f 100644 --- a/lib/auth.php +++ b/lib/auth.php @@ -160,6 +160,7 @@ class Hm_Auth_IMAP extends Hm_Auth { public function __construct($config) { $this->site_config = $config; require_once APP_PATH.'modules/imap/hm-imap.php'; + include_once APP_PATH.'modules/sievefilters/hm-sieve.php'; } /* IMAP authentication server settings */ @@ -194,7 +195,7 @@ private function check_connection($imap) { */ public function check_credentials($user, $pass) { $imap = new Hm_IMAP(); - list($server, $port, $tls, $sieve_config) = get_auth_config($this->site_config, 'imap'); + list($server, $port, $tls, $sieve_config, $sieve_tls_mode) = get_auth_config($this->site_config, 'imap'); if (!$user || !$pass || !$server || !$port) { Hm_Debug::add($imap->show_debug(true)); Hm_Debug::add('Invalid IMAP auth configuration settings'); @@ -203,7 +204,8 @@ public function check_credentials($user, $pass) { $this->imap_settings = ['server' => $server, 'port' => $port, 'tls' => $tls, 'username' => $user, 'password' => $pass, 'no_caps' => false, 'blacklisted_extensions' => ['enable'], - 'sieve_config_host' => $sieve_config + 'sieve_config_host' => $sieve_config, + 'sieve_tls' => $sieve_tls_mode ]; return $this->check_connection($imap); } @@ -319,6 +321,7 @@ function get_auth_config($config, $prefix) { $ret = [$server, $port, $tls]; if ($prefix == 'imap') { $ret[] = $config->get($prefix.'_auth_sieve_conf_host', false); + $ret[] = $config->get($prefix.'_auth_sieve_tls_mode', false); } return $ret; } diff --git a/modules/core/handler_modules.php b/modules/core/handler_modules.php index 4a5544ea0c..800cc65767 100644 --- a/modules/core/handler_modules.php +++ b/modules/core/handler_modules.php @@ -1008,13 +1008,13 @@ public function process() { 'srv_setup_stepper_profile_signature', 'srv_setup_stepper_profile_reply_to', 'srv_setup_stepper_imap_sieve_host', + 'srv_setup_stepper_imap_sieve_mode_tls', 'srv_setup_stepper_only_jmap', 'srv_setup_stepper_imap_hide_from_c_page', 'srv_setup_stepper_jmap_address', 'srv_setup_stepper_imap_server_id', 'srv_setup_stepper_smtp_server_id', )); - if ($success) { // Destructure form array into variables [ @@ -1036,6 +1036,7 @@ public function process() { 'srv_setup_stepper_profile_signature' => $profileSignature, 'srv_setup_stepper_profile_reply_to' => $profileReplyTo, 'srv_setup_stepper_imap_sieve_host' => $imapSieveHost, + 'srv_setup_stepper_imap_sieve_mode_tls' => $imapSieveTls, 'srv_setup_stepper_only_jmap' => $onlyJmap, 'srv_setup_stepper_imap_hide_from_c_page' => $hideFromCombinedView, 'srv_setup_stepper_jmap_address' => $jmapAddress, @@ -1064,7 +1065,8 @@ public function process() { 'jmap', $this, $hideFromCombinedView, - $imapServerId + $imapServerId, + $imapSieveTls ); if(!isset($this->jmap_server_id)) { @@ -1113,6 +1115,7 @@ public function process() { $this, $hideFromCombinedView, $imapServerId, + $imapSieveTls ); if(!isset($this->imap_server_id)) { diff --git a/modules/core/setup.php b/modules/core/setup.php index 6e5003b6cc..b87796895f 100644 --- a/modules/core/setup.php +++ b/modules/core/setup.php @@ -333,6 +333,7 @@ 'srv_setup_stepper_profile_signature' => FILTER_DEFAULT, 'srv_setup_stepper_profile_reply_to' => FILTER_DEFAULT, 'srv_setup_stepper_imap_sieve_host' => FILTER_DEFAULT, + 'srv_setup_stepper_imap_sieve_mode_tls' => FILTER_VALIDATE_BOOLEAN, 'srv_setup_stepper_only_jmap' => FILTER_VALIDATE_BOOLEAN, 'srv_setup_stepper_jmap_hide_from_c_page' => FILTER_VALIDATE_BOOLEAN, 'srv_setup_stepper_jmap_address' => FILTER_DEFAULT, diff --git a/modules/core/site.js b/modules/core/site.js index 3d113eb656..6f8b12ece1 100644 --- a/modules/core/site.js +++ b/modules/core/site.js @@ -2260,6 +2260,7 @@ function submitSmtpImapServer() { { name: 'srv_setup_stepper_imap_port', value: $('#srv_setup_stepper_imap_port').val() }, { name: 'srv_setup_stepper_imap_tls', value: $('input[name="srv_setup_stepper_imap_tls"]:checked').val() }, { name: 'srv_setup_stepper_enable_sieve', value: $('#srv_setup_stepper_enable_sieve').prop('checked') }, + { name: 'srv_setup_stepper_imap_sieve_mode_tls', value: $('#srv_setup_stepper_imap_sieve_mode_tls').prop('checked') }, { name: 'srv_setup_stepper_create_profile', value: $('#srv_setup_stepper_create_profile').prop('checked') }, { name: 'srv_setup_stepper_profile_is_default', value: $('#srv_setup_stepper_profile_is_default').prop('checked') }, { name: 'srv_setup_stepper_profile_signature', value: $('#srv_setup_stepper_profile_signature').val() }, @@ -2313,6 +2314,7 @@ function resetQuickSetupForm() { $("#srv_setup_stepper_is_sender").prop('checked', true); $("#srv_setup_stepper_is_receiver").prop('checked', true); $("#srv_setup_stepper_enable_sieve").prop('checked', false); + $("#srv_setup_stepper_imap_sieve_mode_tls").prop('checked', false); $("#srv_setup_stepper_only_jmap").prop('checked', false); $('#step_config-imap_bloc').show(); $('#step_config-smtp_bloc').show(); @@ -2462,6 +2464,7 @@ function display_config_step(stepNumber) { if($('#srv_setup_stepper_enable_sieve').is(':checked')) { requiredFields.push( {key: 'srv_setup_stepper_imap_sieve_host', value: $('#srv_setup_stepper_imap_sieve_host').val()}, + {key: 'srv_setup_stepper_imap_sieve_mode_tls', value: $('#srv_setup_stepper_imap_sieve_mode_tls').val()}, ) } @@ -2522,11 +2525,17 @@ function getServiceDetails(providerKey){ $('#srv_setup_stepper_enable_sieve') .prop('checked', true) .trigger('change'); + $('#srv_setup_stepper_imap_sieve_mode_tls') + .prop('checked', serverConfig.sieve.tls) + .trigger('change'); $('#srv_setup_stepper_imap_sieve_host').val(serverConfig.sieve.host + ':' + serverConfig.sieve.port); } else { $('#srv_setup_stepper_enable_sieve') .prop('checked', false) - .trigger('change');; + .trigger('change'); + $('#srv_setup_stepper_imap_sieve_mode_tls') + .prop('checked', false) + .trigger('change'); $('#srv_setup_stepper_imap_sieve_host').val(''); } } diff --git a/modules/imap/functions.php b/modules/imap/functions.php index 3dd58c16b6..e68c0156a0 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -1516,16 +1516,15 @@ function forward_dropdown($output,$reply_args) { if (!hm_exists('parse_sieve_config_host')) { function parse_sieve_config_host($host) { $url = parse_url($host); - $host = $url['host'] ?? $url['path']; + if(!isset($url['host'])) { + $host = $url['path']; + } $port = $url['port'] ?? '4190'; - $scheme = $url['scheme'] ?? 'tcp://'; - $tls = $scheme === 'tls'; - // $host = '$scheme://'.$host; - return [$host, $port, $tls]; + return [$host, $port]; }} if (!hm_exists('connect_to_imap_server')) { - function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $imap_sieve_host, $enableSieve, $type, $context, $hidden = false, $server_id = false, $show_errors = true) { + function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $imap_sieve_host, $enableSieve, $type, $context, $hidden = false, $server_id = false, $sieve_tls = false, $show_errors = true) { $imap_list = array( 'name' => $name, 'server' => $address, @@ -1547,8 +1546,8 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima if ($enableSieve && $imap_sieve_host) { $imap_list['sieve_config_host'] = $imap_sieve_host; + $imap_list['sieve_tls'] = $sieve_tls; } - if ($server_id) { if (Hm_IMAP_List::edit($server_id, $imap_list)) { $imap_server_id = $server_id; diff --git a/modules/imap/handler_modules.php b/modules/imap/handler_modules.php index e90665a392..4525307f67 100644 --- a/modules/imap/handler_modules.php +++ b/modules/imap/handler_modules.php @@ -1744,6 +1744,7 @@ public function process() { ); if (! empty($auth_server['sieve_config_host'])) { $imap_details['sieve_config_host'] = $auth_server['sieve_config_host']; + $imap_details['sieve_tls'] = $auth_server['sieve_tls']; } if (!$default_server_id) { Hm_IMAP_List::add($imap_details); diff --git a/modules/imap/output_modules.php b/modules/imap/output_modules.php index e204ba9941..a3d4f04e8e 100644 --- a/modules/imap/output_modules.php +++ b/modules/imap/output_modules.php @@ -1412,10 +1412,20 @@ protected function output() { -
- - - +
+
+ + + +
+
+
+ + +
+
'; } $res .= '
'; diff --git a/modules/nux/assets/data/server_accounts_sample.csv b/modules/nux/assets/data/server_accounts_sample.csv index 06b08ad8dd..02d3d719af 100644 --- a/modules/nux/assets/data/server_accounts_sample.csv +++ b/modules/nux/assets/data/server_accounts_sample.csv @@ -1,3 +1,3 @@ -server_name;username;password;jmap_server;jmap_hide_from_combined_view;imap_server;imap_port;imap_tls;imap_hide_from_combined_view;smtp_server;smtp_port;smtp_tls;sieve_host;sieve_port;profile_reply_to;profile_signature;profile_is_default -Mailbox 1;email@example.org;secret;;FALSE;imap.example.org;993;TRUE;FALSE;smtp.example.org;465;TRUE;tls://imap.exemple.org;4190;email@example.org;;FALSE -Mailbox 2;test@example2.org;secret2;jmap.example2.org;FALSE;;;;;smtp.example2.org;465;TRUE;tls://jmap.example2.org;4190;test@example2.org;my-signature;TRUE \ No newline at end of file +server_name;username;password;jmap_server;jmap_hide_from_combined_view;imap_server;imap_port;imap_tls;imap_hide_from_combined_view;smtp_server;smtp_port;smtp_tls;sieve_host;sieve_port;sieve_tls;profile_reply_to;profile_signature;profile_is_default +Mailbox 1;email@example.org;secret;;FALSE;imap.example.org;993;TRUE;FALSE;smtp.example.org;465;TRUE;tls://imap.exemple.org;4190;true;email@example.org;;FALSE +Mailbox 2;test@example2.org;secret2;jmap.example2.org;FALSE;;;;;smtp.example2.org;465;TRUE;tls://jmap.example2.org;4190;false;test@example2.org;my-signature;TRUE \ No newline at end of file diff --git a/modules/nux/assets/data/server_accounts_sample.yaml b/modules/nux/assets/data/server_accounts_sample.yaml index 951c3a9504..8c35ae897a 100644 --- a/modules/nux/assets/data/server_accounts_sample.yaml +++ b/modules/nux/assets/data/server_accounts_sample.yaml @@ -14,8 +14,9 @@ Mailbox 1: port: 465 tls: true sieve: - host: tls://imap.example.org + host: imap.example.org port: 4190 + tls: true profile: reply_to: email@example.org signature: @@ -36,8 +37,9 @@ Mailbox 2: port: 465 tls: true sieve: - host: tls://jmap.example2.org + host: jmap.example2.org port: 4190 + tls: false profile: reply_to: test@example2.org signature: my-signature diff --git a/modules/nux/modules.php b/modules/nux/modules.php index 1024905138..c47d702e4f 100644 --- a/modules/nux/modules.php +++ b/modules/nux/modules.php @@ -333,6 +333,7 @@ public function process() $this, $server['jmap']['hide_from_combined_view'], false, + $server['sieve']['tls'], false ); if (! $jmap_server_id) { @@ -357,6 +358,7 @@ public function process() $this, $server['imap']['hide_from_combined_view'], false, + $server['sieve']['tls'], false ); if (! $imap_server_id) { diff --git a/modules/nux/services.php b/modules/nux/services.php index 1bdc3dc8da..39c97d0d91 100644 --- a/modules/nux/services.php +++ b/modules/nux/services.php @@ -194,7 +194,8 @@ ), 'sieve' => array( 'port' => 4190, - 'host' => 'mail.gandi.net' + 'host' => 'mail.gandi.net', + 'tls' => true ) )); @@ -240,7 +241,8 @@ ), 'sieve' => array( 'port' => 4190, - 'host' => 'tls://imap.migadu.com' + 'host' => 'imap.migadu.com', + 'tls' => true ) )); @@ -258,7 +260,8 @@ ), 'sieve' => array( 'port' => 4190, - 'host' => 'tls://mail.postale.io' + 'host' => 'mail.postale.io', + 'tls' => true ) )); diff --git a/modules/sievefilters/hm-sieve.php b/modules/sievefilters/hm-sieve.php index e969605410..21bbd578ad 100644 --- a/modules/sievefilters/hm-sieve.php +++ b/modules/sievefilters/hm-sieve.php @@ -5,9 +5,9 @@ class Hm_Sieve_Client_Factory { public function init($user_config = null, $imap_account = null) { if ($imap_account && ! empty($imap_account['sieve_config_host'])) { - list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($imap_account['sieve_config_host']); + list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_account['sieve_config_host']); $client = new PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port); - $client->connect($imap_account['user'], $imap_account['pass'], $sieve_tls, "", "PLAIN"); + $client->connect($imap_account['user'], $imap_account['pass'], $imap_account['sieve_tls'], "", "PLAIN"); return $client; } else { $errorMsg = 'Invalid config host'; diff --git a/modules/smtp/hm-smtp.php b/modules/smtp/hm-smtp.php index b692449eba..ace96b3069 100644 --- a/modules/smtp/hm-smtp.php +++ b/modules/smtp/hm-smtp.php @@ -411,7 +411,6 @@ function authenticate($username, $password, $mech) { break; } } - if (!isset($result)) { $result = 'An error occurred authenticating to the SMTP server'; $res = $this->get_response(); diff --git a/scripts/create_config.php b/scripts/create_config.php index f54faec75d..3c7577ec26 100644 --- a/scripts/create_config.php +++ b/scripts/create_config.php @@ -83,7 +83,10 @@ function imap_auth_port_setting($current) { } function imap_auth_sieve_conf_host_setting($current) { - return 'Sieve configuration'; + return 'Sieve configuration'; +} +function imap_auth_sieve_tls_mode_setting($current) { + return 'Sieve configuration mode'; } function imap_auth_server_setting($current) { @@ -231,7 +234,8 @@ function setting_defaults() { 'imap_auth_server' => 'localhost', 'imap_auth_port' => '143', 'imap_auth_tls' => '', - 'imap_auth_sieve_conf_host' => '' + 'imap_auth_sieve_conf_host' => '', + 'imap_auth_sieve_tls_mode' => false ), 'SMTP' => array( 'default_smtp_name' => '',