From 4f863a4cc21e862965208679025a64e709ff4ebe Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Tue, 18 Jun 2019 18:18:09 +0200
Subject: [PATCH] maintain named-pri-zones

---
 hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java    |    9 +++++++++
 hsarback/src/de/hsadmin/mods/dom/named-pri-zones.vm          |    2 +-
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java |   26 ++++++++++++++++++++++----
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index 1e33a6d..54ae349 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -2,8 +2,10 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
@@ -106,10 +108,26 @@
 			new VelocityProcessor("/de/hsadmin/mods/dom/zonefile.vm", zonefileTemplateVars, dom, zonefileTargetPath, false);
 		final Processor zonefileACLProcessor = 
 			new ShellProcessor("chown root:bind " + zonefileTargetPath + " && chmod 644 " + zonefileTargetPath);
-		final Query query = em.createQuery("SELECT d FROM Domains d WHERE d.user.pac.hive.name = :hivename");	
-		query.setParameter("hivename", dom.getUser().getHiveName());
+		final String hiveName = dom.getUser().getHiveName();
+		final Query domsQuery = em.createQuery("SELECT d FROM Domains d WHERE d.user.pac.hive.name = :hivename");	
+		domsQuery.setParameter("hivename", hiveName);
+		final List<?> domsList = domsQuery.getResultList();
+		final Set<String> domsNames = new HashSet<>();
+		for (final Object obj : domsList) {
+			if (obj instanceof Domain) {
+				domsNames.add(((Domain) obj).getName());
+			}
+		}
+		final Query pacsQuery = em.createQuery("SELECT p FROM Pacs p WHERE p.hive.name = :hivename");	
+		domsQuery.setParameter("hivename", hiveName);
+		final List<?> pacsList = pacsQuery.getResultList();
+		for (final Object obj : pacsList) {
+			if (obj instanceof Pac) {
+				domsNames.add(((Pac) obj).getName() + ".hostsharing.net");
+			}
+		}
 		final Map<String, Object> namedZonesTemplateVars = new HashMap<String, Object>();
-		namedZonesTemplateVars.put("domains", query.getResultList());
+		namedZonesTemplateVars.put("domains", domsNames);
 		final Processor prizonesFileProcessor = new CompoundProcessor(
 			new VelocityProcessor("/de/hsadmin/mods/dom/named-pri-zones.vm", 
 					namedZonesTemplateVars, dom, "/etc/bind/named.pri-zones.tmp", true),
@@ -124,7 +142,7 @@
 	private ShellProcessor createHiveDNSRemoveProcessor(String domName) {
 		return new ShellProcessor("grep -v '\"pri." + domName + "\"' /etc/bind/named.pri-zones > /etc/bind/named.pri-zones.tmp" +
 			" && mv /etc/bind/named.pri-zones.tmp /etc/bind/named.pri-zones" +
-			" && rm /etc/bind/pri." + domName +
+			" && rm -f /etc/bind/pri." + domName +
 			" && invoke-rc.d bind9 reload");
 	}
 
diff --git a/hsarback/src/de/hsadmin/mods/dom/named-pri-zones.vm b/hsarback/src/de/hsadmin/mods/dom/named-pri-zones.vm
index ba2286d..f04eb26 100644
--- a/hsarback/src/de/hsadmin/mods/dom/named-pri-zones.vm
+++ b/hsarback/src/de/hsadmin/mods/dom/named-pri-zones.vm
@@ -4,5 +4,5 @@
 //
 
 #foreach( $domain in ${domains} )
-zone "${domain.name}" { type master; file "pri.${domain.name}"; };
+zone "${domain}" { type master; file "pri.${domain}"; };
 #end
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
index 2ed39ed..f20efad 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
@@ -76,6 +76,7 @@
 			Hive hive = pac.getHive();
 			hive.getPacs().remove(pac); 
 			WaitingTasksProcessor waiting = new WaitingTasksProcessor(createDelUserProc(pac.getName()));
+			final String domainName = pac.getName() + ".hostsharing.net";
 			waiting.appendProcessor(pac.getHiveName(), new CompoundProcessor(
 				createIfDown(pac),
 				createEtcHostsProc(hive),
@@ -83,6 +84,7 @@
 				createSudouersProc(hive),
 				createProftpdConfProc(hive),
 		        createHttpdVirtualProc(hive),
+		        createHiveDNSRemoveProcessor(domainName),
 		        createAccountingRulesProc()), "remove packet");
 			return waiting;
 		} else {
@@ -90,6 +92,13 @@
 		}
 	}
 
+	private ShellProcessor createHiveDNSRemoveProcessor(String domName) {
+		return new ShellProcessor("grep -v '\"pri." + domName + "\"' /etc/bind/named.pri-zones > /etc/bind/named.pri-zones.tmp" +
+			" && mv /etc/bind/named.pri-zones.tmp /etc/bind/named.pri-zones" +
+			" && rm -f /etc/bind/pri." + domName +
+			" && invoke-rc.d bind9 reload");
+	}
+
 	private Processor createAccountingRulesProc() {
 		return new ShellProcessor("mk-iptables-rules Accounting");
 	}

--
Gitblit v1.9.0-SNAPSHOT