From cadd25346c0808e7cfa81c6667bd058fdc358532 Mon Sep 17 00:00:00 2001
From: Purodha Blissenbach <purodha.blissenbach@hostsharing.net>
Date: Fri, 21 Sep 2012 13:51:14 +0200
Subject: [PATCH] Merge branch 'master' of ssh://hsh04-source@hsh04.hostsharing.net/home/doms/source.hostsharing.net/source/hsadmin.git

---
 hsarback/src/de/hsadmin/mods/dom/httpd-vhost-static.jtpl     |   42 ++++++-------
 hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java       |    5 +
 hsarback/src/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl    |   54 +++++++-----------
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java |   69 +++++++++++++++-------
 4 files changed, 91 insertions(+), 79 deletions(-)

diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java b/hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java
index ec201e0..2e7038d 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java
@@ -11,6 +11,7 @@
 import de.hsadmin.core.model.AbstractEntity;
 import de.hsadmin.core.model.AbstractModuleImpl;
 import de.hsadmin.core.model.AuthorisationException;
+import de.hsadmin.core.model.GenericModuleImpl;
 import de.hsadmin.core.model.HSAdminException;
 import de.hsadmin.mods.email.EMailAddress;
 import de.hsadmin.mods.pac.Pac;
@@ -177,9 +178,9 @@
 		Query query = em.createQuery("SELECT adr FROM " + EMailAddress.class.getAnnotation(javax.persistence.Entity.class).name() 
 				+ " adr WHERE adr.domain.name='" + dom.getName() + "'");
 		List<?> resultList = query.getResultList();
+		GenericModuleImpl helperModule = new GenericModuleImpl(getTransaction());
 		for (Object obj : resultList) {
-			EMailAddress eMailAddress = (EMailAddress) obj;
-			em.remove(eMailAddress);
+			helperModule.delete((EMailAddress) obj);
 		}
 		super.delete(existingEntity);
 	}
diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index 7572ddd..dc7652f 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -100,11 +100,8 @@
 	public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
 		Domain dom = (Domain) entity;
 		String domName = dom.getName();
-		WaitingTasksProcessor mainProcessor = new WaitingTasksProcessor(
-				new CompoundProcessor(
-					createHiveEMailRemoveProcessor(domName), 
-					createHiveDNSRemoveProcessor(domName)
-				)
+		WaitingTasksProcessor mainProcessor = new WaitingTasksProcessor(						 
+				createHiveDNSRemoveProcessor(domName)
 			);
 		Config config = Config.getInstance();
 		for (String queueName : config.getProperty("queues.dns").split(",")) {
@@ -125,6 +122,7 @@
 			new TemplateProcessor("/de/hsadmin/mods/dom/zonefile.jtpl", templateVars, zonefileTargetPath, false);
 		Processor zonefileACLProcessor = 
 			new ShellProcessor("chown root:bind " + zonefileTargetPath + " && chmod 644 " + zonefileTargetPath);
+		// TODO Use templates and regenerate the file.
 		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" +
@@ -157,14 +155,6 @@
 		return emailAdrProcessor;
 	}
 
-	private ShellProcessor createHiveEMailRemoveProcessor(String domName) {
-		return new ShellProcessor(
-			"postmap -d '" + domName + "' /etc/postfix-mailin/virtual && " + 
-			"for KEY in $(postmap -s /etc/postfix-mailin/virtual|grep '@" + domName + "\\s'|cut -f1); " +
-				"do postmap -d $KEY /etc/postfix-mailin/virtual; done"
-			);
-	}
-
 	private Processor createDNSServerSetupProcessor(String domName, String pacInetAddr) {
 		Processor seczonesFileProcessor;
 		seczonesFileProcessor = 
@@ -176,8 +166,8 @@
 	}
 
 	private Processor createDNSServerRemoveProcessor(String domName) {
+		// TODO Use templates and regenerate the file. 
 		return new ShellProcessor("grep -v '\"sec." + domName + "\"' /etc/bind/named-hsh.conf >/etc/bind/named-hsh.conf.tmp" +
-	// TODO sed -e'/sec.abc.example.com/d' -i /etc/bind/named-hsh.conf 
 				" && mv /etc/bind/named-hsh.conf.tmp /etc/bind/named-hsh.conf" +
 				" && rm /var/cache/bind/sec." + domName + 
 				" && invoke-rc.d bind9 reload");
@@ -302,18 +292,53 @@
 		String linkPrefix = Integer.toString(100 - level);
 		String pac = dom.getUser().getPac().getName();
 		Query query = em.createQuery("SELECT d FROM Domains d WHERE d.domainoptions.name = :option AND d.name = :domname");
-		query.setParameter("option", "nohtdocsfallback");
 		query.setParameter("domname", dom.getName());
-		List<?> result = query.getResultList();
-		List<Map<String, String>> iterateMaps = new ArrayList<Map<String, String>>();
-		if (!result.isEmpty()) {
-			iterateMaps.add(new HashMap<String, String>());
+		// TODO: This code should be cleaned up after switching to the velocity template engine.
+		query.setParameter("option", "indexes");
+		if (query.getResultList().isEmpty()) {
+			templateVars.put("INDEXES", "+Indexes");			
+		} else {
+			templateVars.put("INDEXES", "-Indexes");
 		}
+		// TODO: This code should be cleaned up after switching to the velocity template engine.
+		query.setParameter("option", "includes");
+		if (query.getResultList().isEmpty()) {
+			templateVars.put("INCLUDES", "+IncludesNoExec");			
+		} else {
+			templateVars.put("INCLUDES", "-Includes");
+		}		
+		// TODO: This code should be cleaned up after switching to the velocity template engine.
+		query.setParameter("option", "multiviews");
+		if (query.getResultList().isEmpty()) {
+			templateVars.put("MULTIVIEWS", "+MultiViews");			
+		} else {
+			templateVars.put("MULTIVIEWS", "-MultiViews");
+		}				
+		// TODO: This code should be cleaned up after switching to the velocity template engine.
+		query.setParameter("option", "htdocsfallback");		
+		if (query.getResultList().isEmpty()) {
+			templateVars.put("HTDOCSFALLBACKHTTP",
+		    "    RewriteCond %{REQUEST_URI} !^/cgi-bin/\n" +
+		    "    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/\n" +
+		    "    RewriteCond %{HTTP_HOST} ^(.+)\\.{DOM_HOSTNAME}\\.?(:80)?$ [novary]\n" +
+		    "    RewriteCond /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1} !-d\n" +
+		    "    RewriteRule ^(.*) - [redirect=404,last]");
+			templateVars.put("HTDOCSFALLBACKHTTPS",
+		    "    RewriteCond %{REQUEST_URI} !^/cgi-bin/\n" +
+		    "    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/\n" +
+		    "    RewriteCond %{HTTP_HOST} ^(.+)\\.{DOM_HOSTNAME}\\.?(:443)?$ [novary]\n" +
+		    "    RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} !-d\n" +
+		    "    RewriteRule ^(.*) - [redirect=404,last]");						
+		} else {
+			templateVars.put("HTDOCSFALLBACKHTTP", "\n");
+			templateVars.put("HTDOCSFALLBACKHTTPS", "\n");
+		}
+	    		
 		Processor domSetupProcessor = new CompoundProcessor(
 			createDomainDirectoriesProcessor(dom, templateVars),
-			new CreateFileProcessor(selectVHostTemplate(dom), templateVars, iterateMaps.iterator(), "/etc/apache2/sites-available/" + domName + ".tmp", "root", "root", "644", true),
-			new ShellProcessor("ls /etc/apache2/pems/" + pac + ".pem >/dev/null 2>&1 " +
-					"&& sed -i '/SSLCertificate.*default/d' " + "/etc/apache2/sites-available/" + domName + ".tmp" + 
+			new CreateFileProcessor(selectVHostTemplate(dom), templateVars, "/etc/apache2/sites-available/" + domName + ".tmp", "root", "root", "644", true),
+			new ShellProcessor("ls /etc/apache2/pems/" + pac + ".pem >/dev/null 2>&1" +
+					" && sed -i '/SSLCertificate.*default/d' " + "/etc/apache2/sites-available/" + domName + ".tmp" + 
 					" && (ls /etc/apache2/pems/" + pac + ".chain.pem >/dev/null 2>&1 || sed -i '/SSLCertificateChain.*" + pac + "/d' " + "/etc/apache2/sites-available/" + domName + ".tmp )" + 
 					" || sed -i '/SSLCertificate.*" + pac + "/d' " + "/etc/apache2/sites-available/" + domName + ".tmp"),
 			new ShellProcessor(
diff --git a/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl b/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl
index e8d7af6..c572834 100644
--- a/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl
+++ b/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl
@@ -17,40 +17,34 @@
     Alias /fastcgi-bin/ /home/doms/{DOM_HOSTNAME}/fastcgi/
 
     <Directory />
-        Options -ExecCGI +IncludesNOEXEC +Indexes +MultiViews +SymLinksIfOwnerMatch
+        Options -ExecCGI {INCLUDES} {INDEXES} {MULTIVIEWS} +SymLinksIfOwnerMatch
     </Directory>
 
     <Directory /home/doms/{DOM_HOSTNAME}/>
-       AllowOverride AuthConfig FileInfo Indexes Limit
+        AllowOverride AuthConfig FileInfo Indexes Limit
     </Directory>
 
-    <Directory /home/doms/{DOM_HOSTNAME}/cgi-bin/>
+    <Location /cgi-bin/>
         SetHandler cgi-script
-        Options +ExecCGI -Indexes -MultiViews
-    </Directory>
+        Options +ExecCGI {INCLUDES} -Indexes -MultiViews +SymLinksIfOwnerMatch
+    </Location>
 
-    <Directory /home/doms/{DOM_HOSTNAME}/fastcgi-bin/>
+    <Location /fastcgi-bin/>
         SetHandler fcgid-script
-        Options +ExecCGI -Indexes -MultiViews
-    </Directory>
+        Options +ExecCGI {INCLUDES} -Indexes -MultiViews +SymLinksIfOwnerMatch
+    </Location>
 
     RewriteEngine On
     RewriteOptions Inherit
     
     RewriteCond %{REQUEST_URI} !^/cgi-bin/
-    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:80)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1} -d
-    RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1}$1 [last]
+    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/    
+    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:[0-9]+)?$ [novary]
+    RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} -d
+    RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1}$1 [last]
     
-<!-- BEGIN: iterate -->
-    RewriteCond %{REQUEST_URI} !^/cgi-bin/
-    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:80)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1} !-d
-    RewriteRule ^(.*) - [redirect=404,last]
+    {HTDOCSFALLBACKHTTP}
 
-<!-- END: iterate -->
     AddType application/x-httpd-php .php .php5 .php4 .php3
     Action application/x-httpd-php /fastcgi-bin/phpstub
 
@@ -77,7 +71,7 @@
 
     <Directory />
         SSLRequireSSL On
-        Options -ExecCGI +IncludesNOEXEC +Indexes +MultiViews +SymLinksIfOwnerMatch
+        Options -ExecCGI {INCLUDES} {INDEXES} {MULTIVIEWS} +SymLinksIfOwnerMatch
     </Directory>
 
     <Directory /home/doms/{DOM_HOSTNAME}/>
@@ -86,31 +80,25 @@
 
     <Location /cgi-bin/>
         SetHandler cgi-script
-        Options +ExecCGI -Indexes -MultiViews
+        Options +ExecCGI {INCLUDES} -Indexes -MultiViews +SymLinksIfOwnerMatch
     </Location>
 
     <Location /fastcgi-bin/>
         SetHandler fcgid-script
-        Options +ExecCGI -Indexes -MultiViews
+        Options +ExecCGI {INCLUDES} -Indexes -MultiViews +SymLinksIfOwnerMatch
     </Location>
     
     RewriteEngine On
     RewriteOptions Inherit
-
+    
     RewriteCond %{REQUEST_URI} !^/cgi-bin/
-    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:443)?$ [novary]
+    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/    
+    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:[0-9]+)?$ [novary]
     RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} -d
     RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1}$1 [last]
     
-<!-- BEGIN: nosubdomainfallbackforhttps -->
-    RewriteCond %{REQUEST_URI} !^/cgi-bin/
-    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:443)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} !-d
-    RewriteRule ^(.*) - [redirect=404,last]
-    
-<!-- END: nosubdomainfallbackforhttps -->
+    {HTDOCSFALLBACKHTTPS}
+
     AddType application/x-httpd-php .php .php5 .php4 .php3
     Action application/x-httpd-php /fastcgi-bin/phpstub
 
diff --git a/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-static.jtpl b/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-static.jtpl
index 4a2cb1c..058b4aa 100644
--- a/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-static.jtpl
+++ b/hsarback/src/de/hsadmin/mods/dom/httpd-vhost-static.jtpl
@@ -14,33 +14,32 @@
     DocumentRoot /home/doms/{DOM_HOSTNAME}/htdocs
 
     <Directory />
-        Options -ExecCGI +IncludesNOEXEC +Indexes +MultiViews +SymLinksIfOwnerMatch
+        Options -ExecCGI {INCLUDES} {INDEXES} {MULTIVIEWS} +SymLinksIfOwnerMatch
     </Directory>
 
     <Directory /home/doms/{DOM_HOSTNAME}/>
-       AllowOverride AuthConfig FileInfo Indexes Limit
+        AllowOverride AuthConfig FileInfo Indexes Limit
     </Directory>
 
-    <Directory /home/doms/{DOM_HOSTNAME}/cgi-bin/>
+    <Location /cgi-bin/>
         Redirect 501 /
-    </Directory>
+    </Location>
 
-    <Directory /home/doms/{DOM_HOSTNAME}/fastcgi-bin/>
+    <Location /fastcgi-bin/>
         Redirect 501 /
-    </Directory>
+    </Location>
 
     RewriteEngine On
     RewriteOptions Inherit
     
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:80)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1} -d
-    RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1}$1 [last]
+    RewriteCond %{REQUEST_URI} !^/cgi-bin/
+    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/    
+    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:[0-9]+)?$ [novary]
+    RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} -d
+    RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1}$1 [last]
     
-<!-- BEGIN: iterate -->    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:80)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs/${tolower:%1} !-d
-    RewriteRule ^(.*) - [redirect=404,last]
-    
-<!-- END: iterate -->
+    {HTDOCSFALLBACKHTTP}
+
 </VirtualHost>
 
 <VirtualHost {DOM_IPNUMBER}:443 {DOM_IPNUMBEREX}:443>
@@ -61,7 +60,7 @@
 
     <Directory />
         SSLRequireSSL On
-        Options -ExecCGI +IncludesNOEXEC +Indexes +MultiViews +SymLinksIfOwnerMatch
+        Options -ExecCGI {INCLUDES} {INDEXES} {MULTIVIEWS} +SymLinksIfOwnerMatch
     </Directory>
 
     <Directory /home/doms/{DOM_HOSTNAME}/>
@@ -74,19 +73,18 @@
 
     <Location /fastcgi-bin/>
         Redirect 501 /
-    </Location>
+    </Location>    
     
     RewriteEngine On
     RewriteOptions Inherit
     
-    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:443)?$ [novary]
+    RewriteCond %{REQUEST_URI} !^/cgi-bin/
+    RewriteCond %{REQUEST_URI} !^/fastcgi-bin/    
+    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:[0-9]+)?$ [novary]
     RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} -d
     RewriteRule ^(.*) /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1}$1 [last]
-
-<!-- BEGIN: nosubdomainfallbackforhttps -->    RewriteCond %{HTTP_HOST} ^(.+)\.{DOM_HOSTNAME}\.?(:443)?$ [novary]
-    RewriteCond /home/doms/{DOM_HOSTNAME}/subs-ssl/${tolower:%1} !-d
-    RewriteRule ^(.*) - [redirect=404,last]
     
-<!-- END: nosubdomainfallbackforhttps -->
+    {HTDOCSFALLBACKHTTPS}
+
 </VirtualHost>
 <!-- END: main -->
\ No newline at end of file

--
Gitblit v1.9.0-SNAPSHOT