Verified Commit b040756b authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(cyrus) Move cyrus unix style option to cyrus service

Also cleaned up a bit mail method server configuration handling.

issue #6042
parent 5d9d7ed4
...@@ -78,6 +78,11 @@ class serviceCyrus extends simpleMailMethodService ...@@ -78,6 +78,11 @@ class serviceCyrus extends simpleMailMethodService
_('Password'), _('Admin user password'), _('Password'), _('Admin user password'),
'fdCyrusPassword', TRUE 'fdCyrusPassword', TRUE
), ),
new BooleanAttribute(
_('Use UNIX style'), _('Determines if "foo/bar" or "foo.bar" should be used as namespaces in IMAP'),
'fdCyrusUseSlashes', FALSE,
($config->get_cfg_value('CyrusUseSlashes') === 'TRUE')
),
] ]
], ],
'sieve' => [ 'sieve' => [
......
...@@ -39,4 +39,4 @@ attributetype ( 1.3.6.1.4.1.38414.14.1.6 NAME 'fdCyrusAcl' ...@@ -39,4 +39,4 @@ attributetype ( 1.3.6.1.4.1.38414.14.1.6 NAME 'fdCyrusAcl'
objectclass (1.3.6.1.4.1.38414.14.2.1 NAME 'fdCyrusServer' SUP top AUXILIARY objectclass (1.3.6.1.4.1.38414.14.2.1 NAME 'fdCyrusServer' SUP top AUXILIARY
DESC 'FusionDirectory - Cyrus server description' DESC 'FusionDirectory - Cyrus server description'
MUST ( cn $ fdCyrusConnect $ fdCyrusAdmin $ fdCyrusPassword ) MUST ( cn $ fdCyrusConnect $ fdCyrusAdmin $ fdCyrusPassword )
MAY ( fdCyrusSieveServer )) MAY ( fdCyrusUseSlashes $ fdCyrusSieveServer ))
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
class mailMethodCyrus extends mailMethod class mailMethodCyrus extends mailMethod
{ {
protected $ServerList = [];
protected $imap_handle = NULL; protected $imap_handle = NULL;
protected $quota_loaded = FALSE; protected $quota_loaded = FALSE;
...@@ -37,8 +36,16 @@ class mailMethodCyrus extends mailMethod ...@@ -37,8 +36,16 @@ class mailMethodCyrus extends mailMethod
protected function init () protected function init ()
{ {
$cfg = $this->getServerConfig();
if (isset($cfg['useSlashes']) && ($cfg['useSlashes'] === 'TRUE')) {
$this->useSlashesInId = TRUE;
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 'Enabled', '<b>MAIL:</b> cyrusUseSlashes');
} else {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 'Disabled', '<b>MAIL:</b> cyrusUseSlashes');
}
parent::init(); parent::init();
$this->ServerList = parent::getMailServers();
} }
public function connect () public function connect ()
...@@ -47,37 +54,17 @@ class mailMethodCyrus extends mailMethod ...@@ -47,37 +54,17 @@ class mailMethodCyrus extends mailMethod
parent::connect(); parent::connect();
$this->connected = FALSE; $this->connected = FALSE;
if (!count($this->ServerList)) { $cfg = $this->getServerConfig();
$this->error = _("There are no IMAP compatible mail servers defined!"); if ($cfg === FALSE) {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
"<b>IMAP: No mail servers configured, check systems->server->service->imap.</b>", "");
return FALSE;
} elseif (!isset($this->ServerList[$this->parent->gosaMailServer])) {
$this->error = _("Mail server for this account is invalid!");
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
"<b>IMAP: The selected mail server '".$this->parent->gosaMailServer."' is invalid.</b>", "");
return FALSE; return FALSE;
} else {
$cfg = $this->ServerList[$this->parent->gosaMailServer];
}
/* For some reason, hiding errors with @ does not wor here... */
if (!isset($cfg['connect'])) {
$cfg['connect'] = "";
}
if (!isset($cfg['admin'])) {
$cfg['admin'] = "";
}
if (!isset($cfg['password'])) {
$cfg['password'] = "";
} }
/* Setting connect timeout to 10 seconds, /* Setting connect timeout to 10 seconds,
else the FusionDirectory UI may freeze for 60 seconds. else the FusionDirectory UI may freeze for 60 seconds.
(PHP default is 'default_socket_timeout = 60') */ (PHP default is 'default_socket_timeout = 60') */
$timeout = $config->get_cfg_value("imapTimeout", 10); $timeout = $config->get_cfg_value('imapTimeout', 10);
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $timeout, @DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $timeout,
"<b>IMAP: Setting imap connect timeout to</b> (seconds)"); '<b>IMAP: Setting imap connect timeout to</b> (seconds)');
imap_timeout(1, $timeout); imap_timeout(1, $timeout);
$this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN); $this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
...@@ -101,18 +88,21 @@ class mailMethodCyrus extends mailMethod ...@@ -101,18 +88,21 @@ class mailMethodCyrus extends mailMethod
public function account_exists () public function account_exists ()
{ {
if (!$this->is_connected() || !$this->imap_handle) { if (!$this->is_connected() || !$this->imap_handle) {
trigger_error("Method not connected, catch error."); trigger_error('Method not connected, catch error.');
return []; return [];
} }
/* Get server config */ /* Get server config */
$cfg = $this->ServerList[$this->parent->gosaMailServer]; $cfg = $this->getServerConfig();
$list = @imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id); if ($cfg === FALSE) {
return FALSE;
}
$list = @imap_listmailbox($this->imap_handle, $cfg['connect'], $this->account_id);
$res = (is_array($list) && count($list)); $res = (is_array($list) && count($list));
if ($res) { if ($res) {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "", "<b>IMAP: Account exists in imap server.</b>"); @DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $this->account_id, '<b>IMAP: Account exists in imap server.</b>');
} else { } else {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "", "<b>IMAP: Account seems NOT to exists in imap server.</b>"); @DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $this->account_id, '<b>IMAP: Account seems NOT to exists in imap server.</b>');
} }
return $res; return $res;
} }
...@@ -260,11 +250,15 @@ class mailMethodCyrus extends mailMethod ...@@ -260,11 +250,15 @@ class mailMethodCyrus extends mailMethod
} }
$this->build_account_id(); $this->build_account_id();
if ($this->is_connected()) { if ($this->is_connected()) {
$cfg = $this->ServerList[$this->parent->gosaMailServer]; $cfg = $this->getServerConfig();
$list = imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id); if ($cfg === FALSE) {
return FALSE;
}
$list = imap_listmailbox($this->imap_handle, $cfg['connect'], $this->account_id);
if ($list === FALSE) { if ($list === FALSE) {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>", @DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $this->account_id,
"<b>IMAP: Add account</b> on server :".$this->parent->gosaMailServer); "<b>IMAP: Add account</b> on server :".$this->parent->gosaMailServer);
if (!imap_createmailbox($this->imap_handle, $cfg["connect"].$this->account_id)) { if (!imap_createmailbox($this->imap_handle, $cfg["connect"].$this->account_id)) {
$this->error = imap_last_error(); $this->error = imap_last_error();
...@@ -299,9 +293,13 @@ class mailMethodCyrus extends mailMethod ...@@ -299,9 +293,13 @@ class mailMethodCyrus extends mailMethod
return FALSE; return FALSE;
} }
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$this->build_account_id(); $this->build_account_id();
$cfg = $this->ServerList[$this->parent->gosaMailServer];
@imap_setacl($this->imap_handle, $this->account_id, $cfg["admin"], "lrswipcda"); @imap_setacl($this->imap_handle, $this->account_id, $cfg["admin"], "lrswipcda");
if ($config->get_cfg_value("cyrusDeleteMailbox", "TRUE") == "TRUE") { if ($config->get_cfg_value("cyrusDeleteMailbox", "TRUE") == "TRUE") {
...@@ -326,8 +324,10 @@ class mailMethodCyrus extends mailMethod ...@@ -326,8 +324,10 @@ class mailMethodCyrus extends mailMethod
$result = []; $result = [];
/* Get server config */ $cfg = $this->getServerConfig();
$cfg = $this->ServerList[$this->parent->gosaMailServer]; if ($cfg === FALSE) {
return FALSE;
}
/* Create search string /* Create search string
And prepare replacements And prepare replacements
...@@ -515,7 +515,10 @@ class mailMethodCyrus extends mailMethod ...@@ -515,7 +515,10 @@ class mailMethodCyrus extends mailMethod
/* Try to login into sieve /* Try to login into sieve
*/ */
$cfg = $this->ServerList[$this->parent->gosaMailServer]; $cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$sieve = new sieve($cfg["sieve_server"], $cfg["sieve_port"], $cfg["admin"], $sieve = new sieve($cfg["sieve_server"], $cfg["sieve_port"], $cfg["admin"],
$cfg["password"], $this->getUAttribValue(), $cfg["sieve_option"]); $cfg["password"], $this->getUAttribValue(), $cfg["sieve_option"]);
if (!$sieve->sieve_login()) { if (!$sieve->sieve_login()) {
...@@ -624,18 +627,19 @@ class mailMethodCyrus extends mailMethod ...@@ -624,18 +627,19 @@ class mailMethodCyrus extends mailMethod
$ldap = $config->get_ldap_link(); $ldap = $config->get_ldap_link();
$ldap->cd($config->current['BASE']); $ldap->cd($config->current['BASE']);
$ldap->search('(objectClass=fdCyrusServer)', $ldap->search('(objectClass=fdCyrusServer)',
['cn', 'fdCyrusConnect', 'fdCyrusAdmin', 'fdCyrusPassword', 'fdCyrusSieveServer']); ['cn','fdCyrusConnect','fdCyrusAdmin','fdCyrusPassword','fdCyrusSieveServer','fdCyrusUseSlashes']);
while ($attrs = $ldap->fetch()) { while ($attrs = $ldap->fetch()) {
sscanf($attrs['fdCyrusSieveServer'][0], '{%[^{}:]:%d/%[^{}]}', $sieve_server, $sieve_port, $sieve_option); sscanf($attrs['fdCyrusSieveServer'][0], '{%[^{}:]:%d/%[^{}]}', $sieve_server, $sieve_port, $sieve_option);
$serverList[$attrs['cn'][0]] = [ $serverList[$attrs['cn'][0]] = [
"server_dn" => $attrs['dn'], 'server_dn' => $attrs['dn'],
"connect" => $attrs['fdCyrusConnect'][0], 'connect' => $attrs['fdCyrusConnect'][0],
"admin" => $attrs['fdCyrusAdmin'][0], 'admin' => $attrs['fdCyrusAdmin'][0],
"password" => $attrs['fdCyrusPassword'][0], 'password' => $attrs['fdCyrusPassword'][0],
"sieve_server" => $sieve_server, 'useSlashes' => ($attrs['fdCyrusUseSlashes'][0] ?? 'FALSE'),
"sieve_option" => $sieve_option, 'sieve_server' => $sieve_server,
"sieve_port" => $sieve_port 'sieve_option' => $sieve_option,
'sieve_port' => $sieve_port
]; ];
} }
......
...@@ -44,34 +44,13 @@ class mailMethodDovecot extends mailMethod ...@@ -44,34 +44,13 @@ class mailMethodDovecot extends mailMethod
return FALSE; return FALSE;
} }
$servers = parent::getMailServers(); $cfg = $this->getServerConfig();
if ($cfg === FALSE) {
if (!count($servers)) {
$this->error = _("There are no IMAP compatible mail servers defined!");
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
"<b>IMAP: No mail servers configured, check systems->server->service->imap.</b>", "");
return FALSE;
} elseif (!isset($servers[$this->parent->gosaMailServer])) {
$this->error = _("Mail server for this account is invalid!");
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
"<b>IMAP: The selected mail server '".$this->parent->gosaMailServer."' is invalid.</b>", "");
return FALSE; return FALSE;
} else {
$cfg = $servers[$this->parent->gosaMailServer];
}
/* For some reason, hiding errors with @ does not wor here... */
if (!isset($cfg['connect'])) {
$cfg['connect'] = "";
}
if (!isset($cfg['admin'])) {
$cfg['admin'] = "";
}
if (!isset($cfg['password'])) {
$cfg['password'] = "";
} }
$this->build_account_id(); $this->build_account_id();
$cfg['admin'] = $this->account_id.'*'.$cfg['admin']; $cfg['admin'] = $this->account_id.'*'.$cfg['admin'];
/* Setting connect timeout to 10 seconds, /* Setting connect timeout to 10 seconds,
...@@ -190,8 +169,10 @@ class mailMethodDovecot extends mailMethod ...@@ -190,8 +169,10 @@ class mailMethodDovecot extends mailMethod
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>", @DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>",
"<b>Attempting to create folder through Argonaut</b> on server :".$this->parent->gosaMailServer); "<b>Attempting to create folder through Argonaut</b> on server :".$this->parent->gosaMailServer);
$servers = parent::getMailServers(); $cfg = $this->getServerConfig();
$cfg = $servers[$this->parent->gosaMailServer]; if ($cfg === FALSE) {
return FALSE;
}
$o_queue = new supportDaemon(); $o_queue = new supportDaemon();
if (!$o_queue->is_error()) { if (!$o_queue->is_error()) {
......
...@@ -55,11 +55,6 @@ class mailPluginConfig extends simplePlugin ...@@ -55,11 +55,6 @@ class mailPluginConfig extends simplePlugin
_('Override the group account creation syntax. Default is %PREFIX%%UATTRIB%.'), _('Override the group account creation syntax. Default is %PREFIX%%UATTRIB%.'),
'fdMailFolderCreation' 'fdMailFolderCreation'
), ),
new BooleanAttribute(
_('Use cyrus UNIX style'),
_('Determines if "foo/bar" or "foo.bar" should be uses as namespaces in IMAP.'),
'fdCyrusUseSlashes'
),
new BooleanAttribute( new BooleanAttribute(
_('Delete mailbox on account deletion'), _('Delete mailbox on account deletion'),
_('Determines if the mailbox should be removed from your IMAP server after the account is deleted in LDAP.'), _('Determines if the mailbox should be removed from your IMAP server after the account is deleted in LDAP.'),
......
...@@ -31,16 +31,16 @@ class mailMethod ...@@ -31,16 +31,16 @@ class mailMethod
/* Naming attribute for accounts, e.g. imap. */ /* Naming attribute for accounts, e.g. imap. */
protected $uattrib = 'mail'; protected $uattrib = 'mail';
/* The account prefixes, keep the '.' here! See FAQ cyrusUseSlashes */ /* The account prefixes, keep the '.' here! See FAQ useSlashesInId */
protected $user_prefix = 'user.'; protected $user_prefix = 'user.';
protected $share_prefix = 'share.'; protected $share_prefix = 'share.';
/* Account ID creation /* Account ID creation
!!Depends on the attributes 'user_prefix'/'share_prefix' and the option 'cyrusUseSlashes' !!Depends on the attributes 'user_prefix'/'share_prefix' and the option 'useSlashesInId'
Examples - based on defaults : Examples - based on defaults :
%PREFIX% => "user." or "user/" (Depending on cyrusUseSlashes=FALSE/TRUE) %PREFIX% => "user." or "user/" (Depending on useSlashesInId=FALSE/TRUE)
%CN% => "technical" (The groups cn) %CN% => "technical" (The groups cn)
%UID% => "john" (The users uid) %UID% => "john" (The users uid)
%MAIL% => "john@fusiondirectory.org" (The mail address) %MAIL% => "john@fusiondirectory.org" (The mail address)
...@@ -52,8 +52,8 @@ class mailMethod ...@@ -52,8 +52,8 @@ class mailMethod
protected $user_id = "%PREFIX%%UATTRIB%"; protected $user_id = "%PREFIX%%UATTRIB%";
protected $share_id = "%PREFIX%%UATTRIB%"; protected $share_id = "%PREFIX%%UATTRIB%";
/* Create accounts in cyrus style with '/' instead of '.' */ /* Create accounts with '/' instead of '.' */
protected $cyrusUseSlashes = FALSE; protected $useSlashesInId = FALSE;
/* gosaSharedFolderTarget settings, /* gosaSharedFolderTarget settings,
* E.g. * E.g.
...@@ -125,13 +125,6 @@ class mailMethod ...@@ -125,13 +125,6 @@ class mailMethod
protected function init () protected function init ()
{ {
global $config; global $config;
/* Get config value for cyrusUseSlashes */
if ($config->get_cfg_value('cyrusUseSlashes') == 'TRUE') {
$this->cyrusUseSlashes = TRUE;
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 'Enabled', '<b>MAIL:</b> cyrusUseSlashes');
} else {
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 'Disabled', '<b>MAIL:</b> cyrusUseSlashes');
}
/* Check if the mail account identification attribute /* Check if the mail account identification attribute
is overridden in the configuration file */ is overridden in the configuration file */
...@@ -340,11 +333,11 @@ class mailMethod ...@@ -340,11 +333,11 @@ class mailMethod
$acc_string = $this->share_id; $acc_string = $this->share_id;
} }
/* Create account prefix and respect "cyrusUseSlashes" /* Create account prefix and respect "useSlashesInId"
Do not replace escaped dots for cyrusUseSlashes. Do not replace escaped dots for useSlashesInId.
*/ */
$uattrib = $this->uattrib; $uattrib = $this->uattrib;
if ($this->cyrusUseSlashes) { if ($this->useSlashesInId) {
$prefix = preg_replace('/([^\\\\])\./', "\\1/", $prefix); $prefix = preg_replace('/([^\\\\])\./', "\\1/", $prefix);
$acc_string = preg_replace('/([^\\\\])\./', "\\1/", $acc_string); $acc_string = preg_replace('/([^\\\\])\./', "\\1/", $acc_string);
} }
...@@ -405,14 +398,14 @@ class mailMethod ...@@ -405,14 +398,14 @@ class mailMethod
This function is mainly used to read and write folder permissions. This function is mainly used to read and write folder permissions.
@return String A valid folder id @return String A valid folder id
*/ */
public function create_folder_id ($folder) protected function create_folder_id ($folder)
{ {
$cyrus_delim = ($this->cyrusUseSlashes ? '/' : '.'); $delim = ($this->useSlashesInId ? '/' : '.');
if (!empty($folder)) { if (!empty($folder)) {
$folder = trim(preg_replace("/^INBOX[\.\/]*/i", '', $folder)); $folder = trim(preg_replace("/^INBOX[\.\/]*/i", '', $folder));
} }
if (!empty($folder)) { if (!empty($folder)) {
$folder = $cyrus_delim.$folder; $folder = $delim.$folder;
} }
/* We need to move the domain to the end /* We need to move the domain to the end
...@@ -581,6 +574,27 @@ class mailMethod ...@@ -581,6 +574,27 @@ class mailMethod
return TRUE; return TRUE;
} }
/*! \brief Returns configuration of selected mail server
@return Array configuration
*/
protected function getServerConfig ()
{
$serverList = parent::getMailServers();
if (!count($serverList)) {
$this->error = _('There are no IMAP compatible mail servers defined!');
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
'<b>IMAP: No mail servers configured, check systems->server->services</b>', '');
return FALSE;
} elseif (!isset($serverList[$this->parent->gosaMailServer])) {
$this->error = _('Mail server for this account is invalid!');
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $this->parent->gosaMailServer,
'<b>IMAP: The selected mail server is invalid</b>');
return FALSE;
} else {
return $serverList[$this->parent->gosaMailServer];
}
}
/*! \brief Returns all mail servers configured in FusionDirectory /*! \brief Returns all mail servers configured in FusionDirectory
@return Array All useable mail servers @return Array All useable mail servers
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment