2010-10-05 21:42:07 +02:00
|
|
|
package de.hsadmin.mods.dom;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.Query;
|
|
|
|
|
|
|
|
import de.hsadmin.core.model.AbstractEntity;
|
|
|
|
import de.hsadmin.core.qserv.CompoundProcessor;
|
|
|
|
import de.hsadmin.core.qserv.CopyFileProcessor;
|
|
|
|
import de.hsadmin.core.qserv.CreateFileProcessor;
|
|
|
|
import de.hsadmin.core.qserv.EntityProcessorFactory;
|
|
|
|
import de.hsadmin.core.qserv.Processor;
|
|
|
|
import de.hsadmin.core.qserv.ProcessorException;
|
|
|
|
import de.hsadmin.core.qserv.ShellProcessor;
|
|
|
|
import de.hsadmin.core.qserv.TemplateProcessor;
|
|
|
|
import de.hsadmin.core.qserv.WaitingTasksProcessor;
|
|
|
|
import de.hsadmin.mods.email.EMailAddress;
|
|
|
|
import de.hsadmin.mods.email.EMailAddressProcessorFactory;
|
|
|
|
import de.hsadmin.mods.pac.Pac;
|
|
|
|
import de.hsadmin.mods.user.UnixUser;
|
|
|
|
|
|
|
|
public class DomainProcessorFactory implements EntityProcessorFactory {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a Processor which creates a domain
|
|
|
|
* @throws ProcessorException
|
|
|
|
*/
|
|
|
|
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
|
|
|
|
String hiveName = entity.getHiveName();
|
|
|
|
Domain dom = (Domain) entity;
|
|
|
|
UnixUser domUser = dom.getUser();
|
|
|
|
Pac pac = domUser.getPac();
|
2011-07-15 15:45:37 +02:00
|
|
|
String pacName = pac.getName();
|
2010-10-05 21:42:07 +02:00
|
|
|
String domName = dom.getName();
|
|
|
|
String zonefileTargetPath = "/etc/bind/pri." + domName;
|
|
|
|
Map<String, String> templateVars = new HashMap<String, String>();
|
|
|
|
templateVars.put("SIO", Long.toString(System.currentTimeMillis()/1000L));
|
2011-07-15 15:45:37 +02:00
|
|
|
templateVars.put("PAC", pacName);
|
|
|
|
templateVars.put("HIVE", pac.getHiveName());
|
2010-10-05 21:42:07 +02:00
|
|
|
templateVars.put("DOM_HOSTNAME", domName);
|
2011-07-15 15:45:37 +02:00
|
|
|
templateVars.put("DOM_USERNAME", domUser.getName());
|
|
|
|
templateVars.put("PAC_HOSTNAME", pacName + ".hostsharing.net");
|
2010-10-05 21:42:07 +02:00
|
|
|
templateVars.put("DOM_IPNUMBER", pac.getCurINetAddr().getInetAddr());
|
|
|
|
Processor zonefileTemplateProcessor =
|
|
|
|
new TemplateProcessor("/de/hsadmin/mods/dom/zonefile.jtpl", templateVars, zonefileTargetPath, false);
|
|
|
|
Processor zonefileACLProcessor =
|
|
|
|
new ShellProcessor("chown root:bind " + zonefileTargetPath + " && chmod 644 " + zonefileTargetPath);
|
|
|
|
Processor prizonesFileProcessor =
|
|
|
|
new ShellProcessor("echo 'zone \"" + domName + "\" { type master; file \"pri." + domName + "\"; };' >>/etc/bind/named.pri-zones" +
|
|
|
|
" && sort /etc/bind/named.pri-zones | uniq >/etc/bind/named.pri-zones.tmp" +
|
|
|
|
" && mv /etc/bind/named.pri-zones.tmp /etc/bind/named.pri-zones");
|
|
|
|
String hiveInetAddr = pac.getHive().getInetAddr().getInetAddr();
|
2011-05-19 18:22:56 +02:00
|
|
|
Processor dnsReloadProcessor = new ShellProcessor("invoke-rc.d bind9 reload");
|
2010-10-05 21:42:07 +02:00
|
|
|
EMailAddressProcessorFactory eMailAddressProcessorFactory = new EMailAddressProcessorFactory();
|
|
|
|
Processor dnsSetupProcessor =
|
|
|
|
new CompoundProcessor(zonefileTemplateProcessor, zonefileACLProcessor, prizonesFileProcessor, dnsReloadProcessor);
|
|
|
|
WaitingTasksProcessor mainProcessor = new WaitingTasksProcessor(dnsSetupProcessor);
|
|
|
|
CompoundProcessor emailAdrProcessor = new CompoundProcessor();
|
|
|
|
Query query = em.createQuery("SELECT adr FROM " + EMailAddress.class.getAnnotation(javax.persistence.Entity.class).name()
|
|
|
|
+ " adr WHERE adr.domain.name='" + domName + "'");
|
|
|
|
List<?> resultList = query.getResultList();
|
|
|
|
for (Object obj : resultList) {
|
|
|
|
EMailAddress eMailAddress = (EMailAddress) obj;
|
|
|
|
emailAdrProcessor.appendProcessor(eMailAddressProcessorFactory.createCreateProcessor(em, eMailAddress));
|
|
|
|
}
|
|
|
|
mainProcessor.appendProcessor(hiveName, emailAdrProcessor, "Setup EMail");
|
|
|
|
Processor seczonesFileProcessor = null;
|
|
|
|
String[] dnsServerQueues = { "dns1", "dns2", "dns3" };
|
|
|
|
for (String queueName : dnsServerQueues) {
|
|
|
|
seczonesFileProcessor =
|
|
|
|
new ShellProcessor("echo 'zone \"" + domName + "\" { type slave; file \"sec." + domName + "\"; masters { " + hiveInetAddr + "; }; };' >>/etc/bind/named-hsh.conf" +
|
|
|
|
" && sort /etc/bind/named-hsh.conf | uniq >/etc/bind/named-hsh.conf.tmp" +
|
|
|
|
" && mv /etc/bind/named-hsh.conf.tmp /etc/bind/named-hsh.conf" +
|
|
|
|
" && invoke-rc.d bind9 reload");
|
|
|
|
mainProcessor.appendProcessor(queueName, seczonesFileProcessor, queueName + ".hostsharing.net");
|
|
|
|
}
|
|
|
|
|
|
|
|
Processor relayDomainsProcessor = null;
|
|
|
|
String[] mailServerQueues = { "mail1", "mail2", "mail3" };
|
|
|
|
for (String queueName : mailServerQueues) {
|
2011-05-20 15:33:45 +02:00
|
|
|
relayDomainsProcessor = new CompoundProcessor(
|
|
|
|
new ShellProcessor("postmap -r -i /etc/postfix-mailin/relaydomains",
|
2011-05-20 16:12:39 +02:00
|
|
|
domName + " anything\n" +
|
|
|
|
"." + domName + " anything\n"),
|
2011-05-20 15:33:45 +02:00
|
|
|
new ShellProcessor("postmap -r -i /etc/postfix-mailin/transport",
|
|
|
|
domName + " smtp:[" + pac.getName() + ".hostsharing.net]\n" +
|
2011-05-20 16:12:39 +02:00
|
|
|
"." + domName + " smtp:[" + pac.getName() + ".hostsharing.net]\n")
|
2011-05-20 15:33:45 +02:00
|
|
|
);
|
2010-10-05 21:42:07 +02:00
|
|
|
mainProcessor.appendProcessor(queueName, relayDomainsProcessor, queueName + ".hostsharing.net");
|
|
|
|
}
|
|
|
|
|
2011-05-19 18:22:56 +02:00
|
|
|
Processor backupMXSMTPTransportsProcessor =
|
|
|
|
new ShellProcessor("echo " + domName + " >> /etc/postfix/relaydomains" +
|
|
|
|
" && invoke-rc.d postfix reload");
|
|
|
|
mainProcessor.appendProcessor("backupmx", backupMXSMTPTransportsProcessor, "backupmx.hostsharing.net");
|
2010-10-05 21:42:07 +02:00
|
|
|
String domsDir = domUser.getHomedir() + "/doms";
|
|
|
|
String domainDir = domsDir + "/" + dom.getName();
|
|
|
|
String[] subDirs = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "cgi", "fastcgi", "cgi-ssl", "fastcgi-ssl", "etc", "var" };
|
|
|
|
String userName = domUser.getName();
|
|
|
|
Processor mkDomainDirProzessor =
|
|
|
|
new ShellProcessor(
|
|
|
|
"mkdir --mode=1550 --parents " + domsDir + " && " +
|
|
|
|
"chown httpd:" + pacName + " " + domsDir + " && " +
|
|
|
|
"mkdir --mode=750 " + domainDir + " && " +
|
|
|
|
"chown " + userName + ":httpd " + domainDir
|
|
|
|
);
|
|
|
|
CompoundProcessor domDirsProcessor = new CompoundProcessor(mkDomainDirProzessor);
|
|
|
|
for (String subDir : subDirs) {
|
|
|
|
domDirsProcessor.appendProcessor(new ShellProcessor(
|
|
|
|
"mkdir --mode=755 " + domainDir + "/" + subDir + " && " +
|
|
|
|
"chown " + userName + ":" + pacName + " " + domainDir + "/" + subDir
|
|
|
|
));
|
|
|
|
}
|
|
|
|
templateVars = new HashMap<String, String>();
|
2011-07-15 15:45:37 +02:00
|
|
|
templateVars.put("PAC", pacName);
|
|
|
|
templateVars.put("HIVE", pac.getHiveName());
|
|
|
|
templateVars.put("DOM_HOSTNAME", domName);
|
|
|
|
templateVars.put("DOM_USERNAME", domUser.getName());
|
|
|
|
templateVars.put("PAC_HOSTNAME", pacName + ".hostsharing.net");
|
|
|
|
templateVars.put("DOM_IPNUMBER", pac.getCurINetAddr().getInetAddr());
|
2010-10-05 21:42:07 +02:00
|
|
|
templateVars.put("DOMAIN", domName);
|
|
|
|
templateVars.put("USER_NAME", domUser.getComment());
|
2011-07-15 15:45:37 +02:00
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/httpd.conf.jtpl", templateVars, "/etc/apache2/sites-generated/" + domName, "root", "root", "644")
|
|
|
|
);
|
2011-05-19 18:03:59 +02:00
|
|
|
templateVars.put("PROTOCOL", "http");
|
2010-10-05 21:42:07 +02:00
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/htaccess.jtpl", templateVars, domainDir + "/htdocs/.htaccess", userName, pacName, "644")
|
|
|
|
);
|
2011-05-19 18:03:59 +02:00
|
|
|
templateVars.put("PROTOCOL", "https");
|
2010-10-05 21:42:07 +02:00
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/htaccess.jtpl", templateVars, domainDir + "/htdocs-ssl/.htaccess", userName, pacName, "644")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/index.html.jtpl", templateVars, domainDir + "/subs/www/index.html", userName, pacName, "644")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/index.html.jtpl", templateVars, domainDir + "/subs-ssl/www/index.html", userName, pacName, "644")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi/test.cgi", userName, pacName, "755")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi-ssl/test.cgi", userName, pacName, "755")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi/phpstub", userName, pacName, "755")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi-ssl/phpstub", userName, pacName, "755")
|
|
|
|
);
|
|
|
|
domDirsProcessor.appendProcessor(
|
|
|
|
new ShellProcessor("ln -s " + domainDir + " /home/doms/ && " +
|
2011-07-15 17:30:26 +02:00
|
|
|
"chown --no-dereference " + userName + ":httpd /home/doms/" + domName
|
2010-10-05 21:42:07 +02:00
|
|
|
+ " && " + "chown " + userName + ":httpd /home/doms/" + domName + "/")
|
|
|
|
);
|
|
|
|
mainProcessor.appendProcessor(hiveName, domDirsProcessor, "Setup doms/" + domName + "-Directory");
|
|
|
|
|
|
|
|
Processor domSetupProcessor =
|
2011-07-15 16:49:33 +02:00
|
|
|
new ShellProcessor("ln -s /etc/apache2/sites-generated/" + domName + " /etc/apache2/sites-enabled/010-" + domName + " && invoke-rc.d apache2 reload >/dev/null 2>&1");
|
2010-10-05 21:42:07 +02:00
|
|
|
mainProcessor.appendProcessor(hiveName, domSetupProcessor, "Setup Apache VHost");
|
|
|
|
return mainProcessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a Processor which updates a domain
|
|
|
|
*/
|
|
|
|
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a Processor which deletes a domain
|
|
|
|
*/
|
|
|
|
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
|
|
|
|
Domain dom = (Domain) entity;
|
|
|
|
String domName = dom.getName();
|
2011-06-16 12:17:52 +02:00
|
|
|
ShellProcessor emailAddressRemoveProcessor = new ShellProcessor("for KEY in $(postmap -s /etc/postfix/virtual|grep '@" + domName + "\\s'|cut -f1); do postmap -d $KEY /etc/postfix/virtual; done");
|
2010-10-05 21:42:07 +02:00
|
|
|
ShellProcessor dnsRemoveRemoveProcessor =
|
2011-06-16 11:02:01 +02:00
|
|
|
new ShellProcessor("grep -v '\"pri." + domName + "\"' /etc/bind/named.pri-zones > /etc/bind/named.pri-zones.tmp" +
|
2010-10-05 21:42:07 +02:00
|
|
|
" && mv /etc/bind/named.pri-zones.tmp /etc/bind/named.pri-zones" +
|
|
|
|
" && rm /etc/bind/pri." + domName +
|
|
|
|
" && invoke-rc.d bind9 reload");
|
|
|
|
WaitingTasksProcessor mainProcessor = new WaitingTasksProcessor(new CompoundProcessor(emailAddressRemoveProcessor, dnsRemoveRemoveProcessor));
|
|
|
|
|
|
|
|
Processor seczonesFileProcessor = null;
|
|
|
|
String[] dnsQueues = { "dns1", "dns2", "dns3" };
|
|
|
|
for (String queueName : dnsQueues) {
|
|
|
|
seczonesFileProcessor =
|
2011-06-16 11:02:01 +02:00
|
|
|
new ShellProcessor("grep -v '\"sec." + domName + "\"' /etc/bind/named-hsh.conf >/etc/bind/named-hsh.conf.tmp" +
|
2010-10-05 21:42:07 +02:00
|
|
|
" && mv /etc/bind/named-hsh.conf.tmp /etc/bind/named-hsh.conf" +
|
|
|
|
" && rm /var/cache/bind/sec." + domName +
|
|
|
|
" && invoke-rc.d bind9 reload");
|
|
|
|
mainProcessor.appendProcessor(queueName, seczonesFileProcessor, queueName + ".hostsharing.net");
|
|
|
|
}
|
|
|
|
|
|
|
|
Processor mailQueueProcessor = null;
|
|
|
|
String[] mailServerQueues = { "mail1", "mail2", "mail3" };
|
|
|
|
for (String queueName : mailServerQueues) {
|
2011-05-19 18:22:56 +02:00
|
|
|
mailQueueProcessor = new ShellProcessor("postmap -d '" + domName + "' /etc/postfix-mailin/relaydomains && " +
|
|
|
|
"postmap -d '" + domName + "' /etc/postfix-mailin/transport && " +
|
|
|
|
"postmap -d '." + domName + "' /etc/postfix-mailin/relaydomains && " +
|
|
|
|
"postmap -d '." + domName + "' /etc/postfix-mailin/transport");
|
2010-10-05 21:42:07 +02:00
|
|
|
mainProcessor.appendProcessor(queueName, mailQueueProcessor, queueName + ".hostsharing.net");
|
|
|
|
}
|
|
|
|
|
|
|
|
Processor vhostDelProcessor =
|
|
|
|
new ShellProcessor("rm /home/doms/" + domName +
|
|
|
|
" && rm -rf " + dom.getUser().getHomedir() + "/doms/" + domName +
|
|
|
|
" && rm /etc/apache2/sites-generated/" + domName +
|
|
|
|
" && rm /etc/apache2/sites-*/`ls -1 /etc/apache2/sites-enabled/ | egrep \"^[01]+-" + domName + "$\"`" +
|
|
|
|
" && invoke-rc.d apache2 reload >/dev/null 2>&1");
|
|
|
|
mainProcessor.appendProcessor(dom.getHiveName(), vhostDelProcessor, "remove apache vhost");
|
2011-05-19 18:22:56 +02:00
|
|
|
|
|
|
|
Processor backupMXSMTPRelayDelProcessor =
|
2011-06-16 11:02:01 +02:00
|
|
|
new ShellProcessor("grep -v '^" + domName + "$' /etc/postfix/relaydomains > /etc/postfix/relaydomains.tmp" +
|
2010-10-05 21:42:07 +02:00
|
|
|
" && mv /etc/postfix/relaydomains.tmp /etc/postfix/relaydomains" +
|
|
|
|
" && invoke-rc.d postfix reload");
|
2011-05-19 18:22:56 +02:00
|
|
|
mainProcessor.appendProcessor("backupmx", backupMXSMTPRelayDelProcessor, "backupmx.hostsharing.net");
|
2010-10-05 21:42:07 +02:00
|
|
|
return mainProcessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|