Skip to content

Commit

Permalink
Enable sieve check on login with auth_type DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Oct 18, 2024
1 parent 6ca536a commit 76147c4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 13 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;
}
3 changes: 2 additions & 1 deletion modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ 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)) {
Expand All @@ -1570,7 +1571,7 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima

include_once APP_PATH.'modules/sievefilters/hm-sieve.php';
$sieveClientFactory = new Hm_Sieve_Client_Factory();
$client = $sieveClientFactory->init(null, $server, $sieve_tls);
$client = $sieveClientFactory->init(null, $server);

if (!$client && $show_errors) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
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_mode'];
}
if (!$default_server_id) {
Hm_IMAP_List::add($imap_details);
Expand Down
3 changes: 2 additions & 1 deletion 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
4 changes: 2 additions & 2 deletions modules/sievefilters/hm-sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<?php

class Hm_Sieve_Client_Factory {
public function init($user_config = null, $imap_account = null, $sieve_tls=false)
public function init($user_config = null, $imap_account = null)
{
if ($imap_account && ! empty($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
3 changes: 0 additions & 3 deletions modules/smtp/hm-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ class Hm_SMTP {
private $config;
private $server;
private $starttls;
private $supports_tls;
private $supports_auth;
private $max_message_size;
private $port;
private $tls;
private $auth;
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 76147c4

Please sign in to comment.