Skip to content

Commit

Permalink
Correct sieve connection when using scheme (cypht-org#1199)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Shadow243 and josaphatim authored Oct 18, 2024
1 parent 296b44d commit 28b39ac
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 31 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 10 additions & 2 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),

/*
| -------------------
Expand Down
7 changes: 5 additions & 2 deletions lib/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions modules/core/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand All @@ -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,
Expand Down Expand Up @@ -1064,7 +1065,8 @@ public function process() {
'jmap',
$this,
$hideFromCombinedView,
$imapServerId
$imapServerId,
$imapSieveTls
);

if(!isset($this->jmap_server_id)) {
Expand Down Expand Up @@ -1113,6 +1115,7 @@ public function process() {
$this,
$hideFromCombinedView,
$imapServerId,
$imapSieveTls
);

if(!isset($this->imap_server_id)) {
Expand Down
1 change: 1 addition & 0 deletions modules/core/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()},
)
}

Expand Down Expand Up @@ -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('');
}
}
Expand Down
13 changes: 6 additions & 7 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1412,10 +1412,20 @@ protected function output() {
<input class="form-check-input" type="checkbox" id="srv_setup_stepper_enable_sieve" onchange="handleSieveStatusChange(this)">
<label class="form-check-label" for="srv_setup_stepper_enable_sieve">'.$this->trans('Enable Sieve').'</label>
</div>
<div class="form-floating hide" id="srv_setup_stepper_imap_sieve_host_bloc">
<input required type="text" id="srv_setup_stepper_imap_sieve_host" name="srv_setup_stepper_imap_sieve_host" class="txt_fld form-control" value="" placeholder="'.$this->trans('Sieve Host').'">
<label class="" for="srv_setup_stepper_imap_sieve_host">'.$this->trans('Sieve Host').'</label>
<span id="srv_setup_stepper_imap_sieve_host-error" class="invalid-feedback"></span>
<div id="srv_setup_stepper_imap_sieve_host_bloc" class="hide">
<div class="form-floating">
<input required type="text" id="srv_setup_stepper_imap_sieve_host" name="srv_setup_stepper_imap_sieve_host" class="txt_fld form-control" value="" placeholder="'.$this->trans('Sieve Host').'">
<label class="" for="srv_setup_stepper_imap_sieve_host">'.$this->trans('Sieve Host').'</label>
<span id="srv_setup_stepper_imap_sieve_host-error" class="invalid-feedback"></span>
</div>
<div class="form-floating">
<div class="form-check" id="srv_setup_stepper_imap_sieve_mode_tls_bloc">
<input class="form-check-input" type="checkbox" role="switch" value="1" id="srv_setup_stepper_imap_sieve_mode_tls" name="srv_setup_stepper_imap_sieve_mode_tls">
<label class="form-check-label" for="srv_setup_stepper_imap_sieve_mode_tls">
'.$this->trans('Sieve TLS Mode').'
</label>
</div>
</div>
</div>';
}
$res .= '</div>';
Expand Down
6 changes: 3 additions & 3 deletions modules/nux/assets/data/server_accounts_sample.csv
Original file line number Diff line number Diff line change
@@ -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 protected];secret;;FALSE;imap.example.org;993;TRUE;FALSE;smtp.example.org;465;TRUE;tls://imap.exemple.org;4190;[email protected];;FALSE
Mailbox 2;[email protected];secret2;jmap.example2.org;FALSE;;;;;smtp.example2.org;465;TRUE;tls://jmap.example2.org;4190;[email protected];my-signature;TRUE
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 protected];secret;;FALSE;imap.example.org;993;TRUE;FALSE;smtp.example.org;465;TRUE;tls://imap.exemple.org;4190;true;[email protected];;FALSE
Mailbox 2;[email protected];secret2;jmap.example2.org;FALSE;;;;;smtp.example2.org;465;TRUE;tls://jmap.example2.org;4190;false;[email protected];my-signature;TRUE
6 changes: 4 additions & 2 deletions modules/nux/assets/data/server_accounts_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]
signature:
Expand All @@ -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: [email protected]
signature: my-signature
Expand Down
2 changes: 2 additions & 0 deletions modules/nux/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public function process()
$this,
$server['jmap']['hide_from_combined_view'],
false,
$server['sieve']['tls'],
false
);
if (! $jmap_server_id) {
Expand All @@ -357,6 +358,7 @@ public function process()
$this,
$server['imap']['hide_from_combined_view'],
false,
$server['sieve']['tls'],
false
);
if (! $imap_server_id) {
Expand Down
9 changes: 6 additions & 3 deletions modules/nux/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@
),
'sieve' => array(
'port' => 4190,
'host' => 'mail.gandi.net'
'host' => 'mail.gandi.net',
'tls' => true
)
));

Expand Down Expand Up @@ -240,7 +241,8 @@
),
'sieve' => array(
'port' => 4190,
'host' => 'tls://imap.migadu.com'
'host' => 'imap.migadu.com',
'tls' => true
)
));

Expand All @@ -258,7 +260,8 @@
),
'sieve' => array(
'port' => 4190,
'host' => 'tls://mail.postale.io'
'host' => 'mail.postale.io',
'tls' => true
)
));

Expand Down
4 changes: 2 additions & 2 deletions modules/sievefilters/hm-sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 0 additions & 1 deletion modules/smtp/hm-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions scripts/create_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ function imap_auth_port_setting($current) {
}

function imap_auth_sieve_conf_host_setting($current) {
return '<tr><td>Sieve configuration</td><td><input type="number" value="'.$current.'" name="imap_auth_sieve_conf_host" /></td></tr>';
return '<tr><td>Sieve configuration</td><td><input type="text" value="'.$current.'" name="imap_auth_sieve_conf_host" /></td></tr>';
}
function imap_auth_sieve_tls_mode_setting($current) {
return '<tr><td>Sieve configuration mode</td><td><input type="text" value="'.$current.'" name="imap_auth_sieve_tls_mode" /></td></tr>';
}

function imap_auth_server_setting($current) {
Expand Down Expand Up @@ -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' => '',
Expand Down

0 comments on commit 28b39ac

Please sign in to comment.