Skip to content

Mailbox auto aliases #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 16, 2020
Merged
15 changes: 15 additions & 0 deletions application/configs/application.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,25 @@ vimbadmin_plugins.AccessPermissions.type.SIEVE = "SIEVE"



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Allow admins to force that for a mailbox/domain basic aliases are existing
; If a new mailbox is created the system will check if the aliases are existing, if not they are created.

vimbadmin_plugins.MailboxAutomaticAliases.disabled = false

; These aliases should always exist, it is not recommened to delete it
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "postmaster"
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "abuse"

; These aliases are optional, but it recommended to not remove them
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "hostmaster"
vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "webmaster"

; Define this if emails should be forwarded to a fixed address instead of the first mailbox address of the domain
;vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping.postmaster = "postmaster@example.net"
;vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping.abuse = "abuse@example.net"
;vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping.hostmaster = "hostmaster@example.net"
;vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping.webmaster = "webmaster@example.net"



Expand Down
193 changes: 193 additions & 0 deletions application/plugins/MailboxAutomaticAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

/**
* @copyright Copyright (c) 2014 Matthias Fechner
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
* @author Matthias Fechner <matthias _at_ fechner.net>
*/

/**
* The Mailbox Automatic Aliases Plugin
*
* The plugin ensures that a required set of aliases for a domain are existent.
* Required aliases are:
* postmaster@domain.tld
* abuse@domain.tld
* Optional aliases are:
* webmaster@domain.tld
* hostmaster@domains.tld
*
* See https://github.com/idefix6/vimbadmin-mailbox-automatic-aliases
*
* Add the following lines to configs/application.ini:
* vimbadmin_plugins.MailboxAutomaticAliases.disabled = false
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "postmaster"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "abuse"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "hostmaster"
* vimbadmin_plugins.MailboxAutomaticAliases.defaultAliases[] = "webmaster"
*
* @package ViMbAdmin
* @subpackage Plugins
*/
class ViMbAdminPlugin_MailboxAutomaticAliases extends ViMbAdmin_Plugin implements OSS_Plugin_Observer {
private $defaultAliases;
private $defaultMapping;

public function __construct(OSS_Controller_Action $controller) {
parent::__construct($controller, get_class() );

// read config parameters
$this->defaultAliases = $controller->getOptions()['vimbadmin_plugins']['MailboxAutomaticAliases']['defaultAliases'];
$this->defaultMapping = $controller->getOptions()['vimbadmin_plugins']['MailboxAutomaticAliases']['defaultMapping'];
}

/**
* Is called after a mailbox is created. It ensures that required aliases are created.
*
* @param $controller
* @param $options
*/
public function mailbox_add_addPostflush($controller, $options) {
// get domain
$domainId = $controller->getDomain()->getId();
$domain = $controller->getDomain()->getDomain();

// get mailbox
$mailbox = $controller->getMailbox()->getUsername();

// check if domain has enforced aliases or do we have to create them?
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
$aliasList = $controller->getD2EM()->getRepository( "\\Entities\\Alias" )->filterForAliasList( $item . '@' . $domain, $controller->getAdmin(), $domainId, true );
if(count($aliasList) == 0) {
$alias = new \Entities\Alias();
$alias->setAddress($item.'@'.$domain);
if($this->defaultMapping[$item]) {
$alias->setGoto($this->defaultMapping[$item]);
} else {
$alias->setGoto($mailbox);
}
$alias->setDomain($controller->getDomain());
$alias->setActive(1);
$alias->setCreated(new \DateTime());
$controller->getD2EM()->persist($alias);
// Increase alias count for domain
$controller->getDomain()->increaseAliasCount();
$controller->getD2EM()->flush();
$controller->addMessage( sprintf(_("Auto-Created alias %s -> %s."), $alias->getAddress(), $alias->getGoto()));
}
}
}
}

/**
* Check if the aliases is allowed to be removed. If not return false, else return true.
* Check if the alias is used as administrative destination and if yes deny removal.
*
* @param $controller
* @param $options
* @return bool
*/
public function alias_delete_preRemove($controller, $options) {
// get alias that should be deleted
$alias = $controller->getAlias()->getAddress();
$domain = $controller->getDomain()->getDomain();

// check if the alias to delete is not enforced by the plugin
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
if($alias == $item.'@'.$domain) {
// not allowed to delete, show error message and stop delete
$controller->addMessage( sprintf( _("Alias %s is required and cannot be deleted. See <a href=\"https://www.ietf.org/rfc/rfc2142.txt\" target=\"page\">RFC2142</a>"), $alias), OSS_Message::ERROR);
return false;
}
}
}

# @TODO Add check for administrative alias removal
return true;
}

public function alias_toggleActive_preToggle($controller, $options) {
// get alias that should be deleted
$alias = $controller->getAlias()->getAddress();
$domain = $controller->getDomain()->getDomain();

if($options['active'] == 'true') {
// we have to check if it is allowed to disable this alias
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
if($alias == $item.'@'.$domain) {
// not allowed to delete, show error message and stop delete
print( sprintf( _("Alias %s is required and cannot be disabled. See <a href=\"https://www.ietf.org/rfc/rfc2142.txt\" target=\"page\">RFC2142</a>"), $alias));
exit(0);
}
}
}

}
# @TODO Check here also for administrative destination alias
return true;
}

/**
* Is called after an alias is created. It ensures that required aliases are created.
*
* @param $controller
* @param $options
*/
public function alias_add_addPreflush($controller, $options) {
// get domain
$domainId = $controller->getDomain()->getId();
$domain = $controller->getDomain()->getDomain();

// get alias
$aliasGoto = $controller->getalias()->getGoto();

// check if domain has enforced aliases or do we have to create them?
if($this->defaultAliases) {
foreach($this->defaultAliases as $key => $item) {
$aliasList = $controller->getD2EM()->getRepository( "\\Entities\\Alias" )->filterForAliasList( $item . '@' . $domain, $controller->getAdmin(), $domainId, true );
if(count($aliasList) == 0) {
$alias = new \Entities\Alias();
$alias->setAddress($item.'@'.$domain);
if($this->defaultMapping[$item]) {
$alias->setGoto($this->defaultMapping[$item]);
}else{
$alias->setGoto($aliasGoto);
}
$alias->setDomain($controller->getDomain());
$alias->setActive(1);
$alias->setCreated(new \DateTime());
$controller->getD2EM()->persist($alias);
// Increase alias count for domain
$controller->getDomain()->increaseAliasCount();
$controller->getD2EM()->flush();
$controller->addMessage( sprintf(_("Auto-Created alias %s -> %s."), $alias->getAddress(), $alias->getGoto()));
}
}
}
}

/**
* Check that a mailbox cannot be removed if used as administrative destination mailbox
*
*/
public function mailbox_purge_preRemove($controller, $options) {
/* check if mailbox is not an administrative mailbox */
$mailbox = $controller->getMailbox()->getUserName();

if($this->defaultMapping) {
foreach($this->defaultMapping as $key => $item) {
if($mailbox == $item) {
// not allowed to delete, show error message and stop delete
$controller->addMessage( sprintf( _("Mailbox %s is defined as automatic alias to fullfill <a href=\"https://www.ietf.org/rfc/rfc2142.txt\" target=\"page\">RFC2142</a>. If you want to remove it, update you application.ini file first. Check key vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping."), $mailbox), OSS_Message::ERROR);
//print( sprintf( _("Mailbox %s is defined as automatic alias to fullfill <a href=\"https://www.ietf.org/rfc/rfc2142.txt\" target=\"page\">RFC2142</a>. If you want to remove it, update you application.ini file first. Check key vimbadmin_plugins.MailboxAutomaticAliases.defaultMapping."), $mailbox));
return false;
}
}
}
return true;
}

}