Simplify mail setup: dom module controls transports and relaydomains (and relayrecipients in case of backupmxforexternalmx), email module controls virtual and relayrecipients.

This commit is contained in:
Michael Hierweck 2017-09-12 10:43:34 +02:00
parent c7fbfcc85c
commit f3a2ac9123
2 changed files with 39 additions and 52 deletions

View File

@ -160,7 +160,15 @@ public class DomainProcessorFactory implements EntityProcessorFactory {
dom.getName() + " smtp:" + inetAddr + ":225\n" + dom.getName() + " smtp:" + inetAddr + ":225\n" +
"." + dom.getName() + " smtp:" + inetAddr + ":225\n") "." + dom.getName() + " smtp:" + inetAddr + ":225\n")
); );
cp.appendProcessor(
new ShellProcessor("postmap -r -i /etc/postfix-mailin/relayrecipients",
"@" + dom.getName() + " anything\n")
);
} else { } else {
cp.appendProcessor(
new ShellProcessor(
"postmap -d '@" + dom.getName() + "' /etc/postfix-mailin/relayrecipients")
);
cp.appendProcessor( cp.appendProcessor(
new ShellProcessor( new ShellProcessor(
"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " + "postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " +
@ -197,6 +205,7 @@ public class DomainProcessorFactory implements EntityProcessorFactory {
Processor mailQueueProcessor = new CompoundProcessor( Processor mailQueueProcessor = new CompoundProcessor(
createPostgreyConfiguration(em), createPostgreyConfiguration(em),
new ShellProcessor( new ShellProcessor(
"postmap -d '@" + dom.getName() + "' /etc/postfix-mailin/relayrecpients && " +
"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/relaydomains && " + "postmap -d '" + dom.getName() + "' /etc/postfix-mailin/relaydomains && " +
"postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " + "postmap -d '" + dom.getName() + "' /etc/postfix-mailin/transport && " +
"postmap -d '." + dom.getName() + "' /etc/postfix-mailin/relaydomains && " + "postmap -d '." + dom.getName() + "' /etc/postfix-mailin/relaydomains && " +

View File

@ -15,69 +15,61 @@ import de.hsadmin.core.util.Config;
import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.Pac;
public class EMailAddressProcessorFactory implements EntityProcessorFactory { public class EMailAddressProcessorFactory implements EntityProcessorFactory {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) { public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
// TODO: combine both keys in a single call (optimization) // TODO: combine both keys in a single call (optimization)
EMailAddress email = (EMailAddress) entity; EMailAddress email = (EMailAddress) entity;
Pac pac = email.getDomain().getUser().getPac();
CompoundProcessor cp = new CompoundProcessor(); CompoundProcessor cp = new CompoundProcessor();
cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix-mailin/virtual", cp.appendProcessor(
email.getFullDomain() + " -" ) ); new ShellProcessor("postmap -r -i /etc/postfix-mailin/virtual", email.getFullDomain() + " -"));
Config config = Config.getInstance(); cp.appendProcessor(new ShellProcessor("postmap -r -i /etc/postfix-mailin/virtual",
cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix-mailin/virtual", email.getEMailAddress() + " " + email.getTarget()));
email.getEMailAddress() + " " + email.getTarget() ) );
WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp); WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
Config config = Config.getInstance();
for (String queueName : config.getProperty("queues.mail").split(",")) { for (String queueName : config.getProperty("queues.mail").split(",")) {
waitingTasksProcessor.appendProcessor(queueName, new ShellProcessor( "postmap -r -i /etc/postfix-mailin/relayrecipients", waitingTasksProcessor.appendProcessor(queueName,
email.getEMailAddress() + " anything" ), "Add to relayrecipients" ); new ShellProcessor("postmap -r -i /etc/postfix-mailin/relayrecipients",
} email.getEMailAddress() + " anything"),
if (emailAddressCount(em, email) <= 1) { "Add to relayrecipients");
String domName = email.getDomain().getName();
for (String queueName : config.getProperty("queues.mail").split(",")) {
waitingTasksProcessor.appendProcessor(queueName, createMailinSetupProcessor(domName, pac), queueName + ".hostsharing.net");
}
} }
return waitingTasksProcessor; return waitingTasksProcessor;
} }
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) { public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
// TODO: if update is specified by primary-key or DB query instead of OID, // TODO: if update is specified by primary-key or DB query instead of
// a postmap -d might be neccessary // OID, a postmap -d might be neccessary
return createCreateProcessor(em, entity); return createCreateProcessor(em, entity);
} }
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) { public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
CompoundProcessor cp = new CompoundProcessor(); CompoundProcessor cp = new CompoundProcessor();
EMailAddress email = (EMailAddress) entity; EMailAddress email = (EMailAddress) entity;
cp.appendProcessor( if (emailAddressCount(em, email) == 0) {
new ShellProcessor( "postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/virtual" ) ); // last email address removed
String fullDomain = email.getFullDomain();
cp.appendProcessor(new ShellProcessor("postmap -d '" + fullDomain + "' /etc/postfix-mailin/virtual"));
}
cp.appendProcessor(
new ShellProcessor("postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/virtual"));
Config config = Config.getInstance(); Config config = Config.getInstance();
WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp); WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
for (String queueName : config.getProperty("queues.mail").split(",")) { for (String queueName : config.getProperty("queues.mail").split(",")) {
waitingTasksProcessor.appendProcessor(queueName, waitingTasksProcessor.appendProcessor(queueName,
new ShellProcessor( "postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/relayrecipients" ), new ShellProcessor(
"Remove from relayrecipients" ); "postmap -d '" + email.getEMailAddress() + "' /etc/postfix-mailin/relayrecipients"),
} "Remove from relayrecipients");
if (emailAddressCount(em, email) == 0) {
// remove the domain from virtual.db
String fullDomain = email.getFullDomain();
cp.appendProcessor(
new ShellProcessor( "postmap -d '" + fullDomain + "' /etc/postfix-mailin/virtual" ) );
for (String queueName : config.getProperty("queues.mail").split(",")) {
waitingTasksProcessor.appendProcessor(queueName, createMailinDeleteProcessor(fullDomain), queueName + ".hostsharing.net");
}
} }
return waitingTasksProcessor; return waitingTasksProcessor;
} }
private int emailAddressCount(EntityManager em, EMailAddress email) { private int emailAddressCount(EntityManager em, EMailAddress email) {
Query query; Query query;
if ( email.getSubdomain() != null ) { if (email.getSubdomain() != null) {
query = em.createQuery("SELECT e FROM EMailAddresses e WHERE e.subdomain=:subdomain AND e.domain=:domain"); query = em.createQuery("SELECT e FROM EMailAddresses e WHERE e.subdomain=:subdomain AND e.domain=:domain");
query.setParameter("subdomain", email.getSubdomain()); query.setParameter("subdomain", email.getSubdomain());
} } else {
else { query = em.createQuery(
query = em.createQuery("SELECT e FROM EMailAddresses e WHERE ( e.subdomain IS NULL OR e.subdomain = '' ) AND e.domain=:domain"); "SELECT e FROM EMailAddresses e WHERE ( e.subdomain IS NULL OR e.subdomain = '' ) AND e.domain=:domain");
} }
query.setParameter("domain", email.getDomain()); query.setParameter("domain", email.getDomain());
List<?> result = query.getResultList(); List<?> result = query.getResultList();
@ -87,18 +79,4 @@ public class EMailAddressProcessorFactory implements EntityProcessorFactory {
return result.size(); return result.size();
} }
private Processor createMailinSetupProcessor(String domName, Pac pac) {
String inetAddr = pac.getCurINetAddr().getInetAddr();
return
new ShellProcessor("postmap -r -i /etc/postfix-mailin/transport",
domName + " smtp:" + inetAddr + ":225\n" +
"." + domName + " smtp:" + inetAddr + ":225\n");
}
private Processor createMailinDeleteProcessor(String domName) {
return new ShellProcessor(
"postmap -d '" + domName + "' /etc/postfix-mailin/transport && " +
"postmap -d '." + domName + "' /etc/postfix-mailin/transport");
}
} }