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
_('Password'), _('Admin user password'),
'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' => [
......
......@@ -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
DESC 'FusionDirectory - Cyrus server description'
MUST ( cn $ fdCyrusConnect $ fdCyrusAdmin $ fdCyrusPassword )
MAY ( fdCyrusSieveServer ))
MAY ( fdCyrusUseSlashes $ fdCyrusSieveServer ))
......@@ -21,7 +21,6 @@
class mailMethodCyrus extends mailMethod
{
protected $ServerList = [];
protected $imap_handle = NULL;
protected $quota_loaded = FALSE;
......@@ -37,8 +36,16 @@ class mailMethodCyrus extends mailMethod
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();
$this->ServerList = parent::getMailServers();
}
public function connect ()
......@@ -47,37 +54,17 @@ class mailMethodCyrus extends mailMethod
parent::connect();
$this->connected = FALSE;
if (!count($this->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->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>", "");
$cfg = $this->getServerConfig();
if ($cfg === 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,
else the FusionDirectory UI may freeze for 60 seconds.
(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,
"<b>IMAP: Setting imap connect timeout to</b> (seconds)");
'<b>IMAP: Setting imap connect timeout to</b> (seconds)');
imap_timeout(1, $timeout);
$this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);
......@@ -101,18 +88,21 @@ class mailMethodCyrus extends mailMethod
public function account_exists ()
{
if (!$this->is_connected() || !$this->imap_handle) {
trigger_error("Method not connected, catch error.");
trigger_error('Method not connected, catch error.');
return [];
}
/* Get server config */
$cfg = $this->ServerList[$this->parent->gosaMailServer];
$list = @imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$list = @imap_listmailbox($this->imap_handle, $cfg['connect'], $this->account_id);
$res = (is_array($list) && count($list));
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 {
@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;
}
......@@ -260,11 +250,15 @@ class mailMethodCyrus extends mailMethod
}
$this->build_account_id();
if ($this->is_connected()) {
$cfg = $this->ServerList[$this->parent->gosaMailServer];
$list = imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$list = imap_listmailbox($this->imap_handle, $cfg['connect'], $this->account_id);
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);
if (!imap_createmailbox($this->imap_handle, $cfg["connect"].$this->account_id)) {
$this->error = imap_last_error();
......@@ -299,9 +293,13 @@ class mailMethodCyrus extends mailMethod
return FALSE;
}
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$this->build_account_id();
$cfg = $this->ServerList[$this->parent->gosaMailServer];
@imap_setacl($this->imap_handle, $this->account_id, $cfg["admin"], "lrswipcda");
if ($config->get_cfg_value("cyrusDeleteMailbox", "TRUE") == "TRUE") {
......@@ -326,8 +324,10 @@ class mailMethodCyrus extends mailMethod
$result = [];
/* Get server config */
$cfg = $this->ServerList[$this->parent->gosaMailServer];
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
/* Create search string
And prepare replacements
......@@ -515,7 +515,10 @@ class mailMethodCyrus extends mailMethod
/* 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"],
$cfg["password"], $this->getUAttribValue(), $cfg["sieve_option"]);
if (!$sieve->sieve_login()) {
......@@ -624,18 +627,19 @@ class mailMethodCyrus extends mailMethod
$ldap = $config->get_ldap_link();
$ldap->cd($config->current['BASE']);
$ldap->search('(objectClass=fdCyrusServer)',
['cn', 'fdCyrusConnect', 'fdCyrusAdmin', 'fdCyrusPassword', 'fdCyrusSieveServer']);
['cn','fdCyrusConnect','fdCyrusAdmin','fdCyrusPassword','fdCyrusSieveServer','fdCyrusUseSlashes']);
while ($attrs = $ldap->fetch()) {
sscanf($attrs['fdCyrusSieveServer'][0], '{%[^{}:]:%d/%[^{}]}', $sieve_server, $sieve_port, $sieve_option);
$serverList[$attrs['cn'][0]] = [
"server_dn" => $attrs['dn'],
"connect" => $attrs['fdCyrusConnect'][0],
"admin" => $attrs['fdCyrusAdmin'][0],
"password" => $attrs['fdCyrusPassword'][0],
"sieve_server" => $sieve_server,
"sieve_option" => $sieve_option,
"sieve_port" => $sieve_port
'server_dn' => $attrs['dn'],
'connect' => $attrs['fdCyrusConnect'][0],
'admin' => $attrs['fdCyrusAdmin'][0],
'password' => $attrs['fdCyrusPassword'][0],
'useSlashes' => ($attrs['fdCyrusUseSlashes'][0] ?? 'FALSE'),
'sieve_server' => $sieve_server,
'sieve_option' => $sieve_option,
'sieve_port' => $sieve_port
];
}
......
......@@ -44,34 +44,13 @@ class mailMethodDovecot extends mailMethod
return FALSE;
}
$servers = parent::getMailServers();
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>", "");
$cfg = $this->getServerConfig();
if ($cfg === 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();
$cfg['admin'] = $this->account_id.'*'.$cfg['admin'];
/* Setting connect timeout to 10 seconds,
......@@ -190,8 +169,10 @@ class mailMethodDovecot extends mailMethod
@DEBUG(DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>",
"<b>Attempting to create folder through Argonaut</b> on server :".$this->parent->gosaMailServer);
$servers = parent::getMailServers();
$cfg = $servers[$this->parent->gosaMailServer];
$cfg = $this->getServerConfig();
if ($cfg === FALSE) {
return FALSE;
}
$o_queue = new supportDaemon();
if (!$o_queue->is_error()) {
......
......@@ -55,11 +55,6 @@ class mailPluginConfig extends simplePlugin
_('Override the group account creation syntax. Default is %PREFIX%%UATTRIB%.'),
'fdMailFolderCreation'
),
new BooleanAttribute(
_('Use cyrus UNIX style'),
_('Determines if "foo/bar" or "foo.bar" should be uses as namespaces in IMAP.'),
'fdCyrusUseSlashes'
),
new BooleanAttribute(
_('Delete mailbox on account deletion'),
_('Determines if the mailbox should be removed from your IMAP server after the account is deleted in LDAP.'),
......
......@@ -31,16 +31,16 @@ class mailMethod
/* Naming attribute for accounts, e.g. imap. */
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 $share_prefix = 'share.';
/* 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 :
%PREFIX% => "user." or "user/" (Depending on cyrusUseSlashes=FALSE/TRUE)
%PREFIX% => "user." or "user/" (Depending on useSlashesInId=FALSE/TRUE)
%CN% => "technical" (The groups cn)
%UID% => "john" (The users uid)
%MAIL% => "john@fusiondirectory.org" (The mail address)
......@@ -52,8 +52,8 @@ class mailMethod
protected $user_id = "%PREFIX%%UATTRIB%";
protected $share_id = "%PREFIX%%UATTRIB%";
/* Create accounts in cyrus style with '/' instead of '.' */
protected $cyrusUseSlashes = FALSE;
/* Create accounts with '/' instead of '.' */
protected $useSlashesInId = FALSE;
/* gosaSharedFolderTarget settings,
* E.g.
......@@ -125,13 +125,6 @@ class mailMethod
protected function init ()
{
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
is overridden in the configuration file */
......@@ -340,11 +333,11 @@ class mailMethod
$acc_string = $this->share_id;
}
/* Create account prefix and respect "cyrusUseSlashes"
Do not replace escaped dots for cyrusUseSlashes.
/* Create account prefix and respect "useSlashesInId"
Do not replace escaped dots for useSlashesInId.
*/
$uattrib = $this->uattrib;
if ($this->cyrusUseSlashes) {
if ($this->useSlashesInId) {
$prefix = preg_replace('/([^\\\\])\./', "\\1/", $prefix);
$acc_string = preg_replace('/([^\\\\])\./', "\\1/", $acc_string);
}
......@@ -405,14 +398,14 @@ class mailMethod
This function is mainly used to read and write folder permissions.
@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)) {
$folder = trim(preg_replace("/^INBOX[\.\/]*/i", '', $folder));
}
if (!empty($folder)) {
$folder = $cyrus_delim.$folder;
$folder = $delim.$folder;
}
/* We need to move the domain to the end
......@@ -581,6 +574,27 @@ class mailMethod
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
@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