From 35727b04e330b323ace37d8a6de9d9b264dd7e96 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Thu, 07 Mar 2019 19:37:57 +0100
Subject: [PATCH] IPv6 changes

---
 hsarback/src/de/hsadmin/mods/dom/apache-vhost.vm             |    4 
 hsarback/src/de/hsadmin/mods/pac/hosts.vm                    |    8 +-
 hsarback/src/de/hsadmin/mods/pac/proftpd-pacs-conf.vm        |    2 
 hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv4.vm            |    2 
 hsarback/src/de/hsadmin/remote/PacRemote.java                |    4 
 hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java    |   36 ++++++------
 hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv6.vm            |    2 
 hsarback/src/de/hsadmin/core/util/IPv6Trick.java             |   60 ++++++++++++++++++++
 hsarback/src/de/hsadmin/mods/pac/INetAddress.java            |   10 ++
 hsarback/src/de/hsadmin/mods/pac/Hive.java                   |    2 
 hsarback/src/de/hsadmin/mods/dom/zonefile.vm                 |    8 +-
 hsarback/src/de/hsadmin/mods/dom/named-hsh-conf.vm           |    2 
 hsarback/src/de/hsadmin/mods/pac/httpd-virtual.vm            |    4 
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java |    6 +-
 hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java          |    4 
 15 files changed, 110 insertions(+), 44 deletions(-)

diff --git a/hsarback/src/de/hsadmin/core/util/IPv6Trick.java b/hsarback/src/de/hsadmin/core/util/IPv6Trick.java
new file mode 100644
index 0000000..1bc578b
--- /dev/null
+++ b/hsarback/src/de/hsadmin/core/util/IPv6Trick.java
@@ -0,0 +1,60 @@
+package de.hsadmin.core.util;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import de.hsadmin.core.qserv.ProcessorException;
+
+public class IPv6Trick {
+	
+	private static final String IPv4_83_223_79 = "83.223.79"; 	// Alboin-Kontor Berlin
+	private static final String IPv4_83_223_91 = "83.223.91"; 	// Alboin-Kontor Berlin
+	private static final String IPv4_83_223_95 = "83.223.95"; 	// Alboin-Kontor Berlin
+	private static final String IPv6_PREFIX_AK = "2a01:37:1000::1";
+
+	private static final String IPv4_83_223_78 = "83.223.78"; 	// e-Shelter Berlin
+	private static final String IPv4_83_223_94 = "83.223.94"; 	// e-Shelter Berlin
+	private static final String IPv6_PREFIX_ES = "2a01:37:2000::1";
+
+	
+	public static String convertIPv4ToIPv6(final String ipv4address) throws ProcessorException {
+		if (ipv4address == null || ipv4address.length() == 0) {
+			throw new ProcessorException("no IPv4 address given");
+		}
+ 		try {
+ 			final InetAddress inetV4Address = InetAddress.getByName(ipv4address);
+			if (ipv4address.startsWith(IPv4_83_223_79) 			// Alboin-Kontor Berlin
+					|| ipv4address.startsWith(IPv4_83_223_91)
+					|| ipv4address.startsWith(IPv4_83_223_95) ) {
+				return embedIPv4Address(inetV4Address, InetAddress.getByName(IPv6_PREFIX_AK));
+			}
+			if (ipv4address.startsWith(IPv4_83_223_78) 			// e-Shelter Berlin
+					|| ipv4address.startsWith(IPv4_83_223_94) ) {
+				return embedIPv4Address(inetV4Address, InetAddress.getByName(IPv6_PREFIX_ES));
+			}
+		} catch (UnknownHostException e) {
+			throw new ProcessorException(e);
+		}
+		throw new ProcessorException("unknown IPv4 address given");
+	}
+
+
+	private static String embedIPv4Address(final InetAddress ipv4address, final InetAddress ipv6Mask) throws UnknownHostException {
+		final byte[] ipv4Bytes = ipv4address.getAddress();
+		final byte[] ipv6Bytes = ipv6Mask.getAddress();
+		ipv6Bytes[10] = ipv4Bytes[0];
+		ipv6Bytes[11] = ipv4Bytes[1];
+		ipv6Bytes[12] = ipv4Bytes[2];
+		ipv6Bytes[13] = ipv4Bytes[3];
+		ipv6Bytes[14] = 0;
+		ipv6Bytes[15] = 0;
+		final InetAddress ipv6Address = InetAddress.getByAddress(ipv6Bytes);
+		final String ipv6String = ipv6Address.getHostAddress();
+		if (ipv6String.startsWith("/")) {
+			return ipv6String.substring(1);
+		} else {
+			return ipv6String;
+		}
+	}
+	
+}
diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index e751143..1e33a6d 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -129,7 +129,7 @@
 	}
 
 	private Processor createMailinSetupProcessor(EntityManager em, Domain dom, Pac pac) throws ProcessorException {
-		final String inetAddr = pac.getCurINetAddr().getInetAddr();
+		final String inet4Addr = pac.getCurINetAddr().getInet4Addr();
 		final CompoundProcessor cp = new CompoundProcessor(
 			createPostgreyConfiguration(em),
 			new ShellProcessor("postmap -r -i /etc/postfix-mailin/relaydomains || true", 
@@ -150,8 +150,8 @@
 			}
 			cp.appendProcessor(
 					new ShellProcessor("postmap -r -i /etc/postfix-mailin/transport || true", 
-							dom.getName() + " smtp:" + inetAddr + ":225\n" +
-							"." + dom.getName() + " smtp:" + inetAddr + ":225\n")
+							dom.getName() + " smtp:" + inet4Addr + ":225\n" +
+							"." + dom.getName() + " smtp:" + inet4Addr + ":225\n")
 					);		
 		} else {
 			cp.appendProcessor(
diff --git a/hsarback/src/de/hsadmin/mods/dom/apache-vhost.vm b/hsarback/src/de/hsadmin/mods/dom/apache-vhost.vm
index 9ace68f..7834096 100644
--- a/hsarback/src/de/hsadmin/mods/dom/apache-vhost.vm
+++ b/hsarback/src/de/hsadmin/mods/dom/apache-vhost.vm
@@ -3,7 +3,7 @@
 # Do not edit manually. Changes will be overwritten.
 #
 
-<VirtualHost ${pac.curINetAddr.inetAddr}:80 ${pac.oldINetAddr.inetAddr}:80 ${pac.curINet6Addr.inetAddr}:80 ${pac.oldINet6Addr.inetAddr}:80>
+<VirtualHost ${pac.curINetAddr.inet4Addr}:80 ${pac.oldINetAddr.inet4Addr}:80 ${pac.curINetAddr.inet6Addr}:80 ${pac.oldINetAddr.inet6Addr}:80>
     ServerName ${dom.name}
     ServerAlias ${dom.serveraliases}
     ServerAdmin webmaster@${dom.name}
@@ -76,7 +76,7 @@
     Action application/x-httpd-php /fastcgi-bin/phpstub
 </VirtualHost>
 
-<VirtualHost ${pac.curINetAddr.inetAddr}:443 ${pac.oldINetAddr.inetAddr}:443 ${pac.curINet6Addr.inetAddr}:443 ${pac.oldINet6Addr.inetAddr}:443>
+<VirtualHost ${pac.curINetAddr.inet4Addr}:443 ${pac.oldINetAddr.inet4Addr}:443 ${pac.curINetAddr.inet6Addr}:443 ${pac.oldINetAddr.inet6Addr}:443>
     ServerName ${dom.name}
     ServerAlias ${dom.serveraliases}
     ServerAdmin webmaster@${dom.name}
diff --git a/hsarback/src/de/hsadmin/mods/dom/named-hsh-conf.vm b/hsarback/src/de/hsadmin/mods/dom/named-hsh-conf.vm
index be4f101..92322e8 100644
--- a/hsarback/src/de/hsadmin/mods/dom/named-hsh-conf.vm
+++ b/hsarback/src/de/hsadmin/mods/dom/named-hsh-conf.vm
@@ -4,5 +4,5 @@
 //
 
 #foreach( $domain in ${domains} )
-zone "${domain.name}" { type slave; file "sec.${domain.name}"; masters { ${domain.user.pac.hive.inetAddr.inetAddr}; }; };
+zone "${domain.name}" { type slave; file "sec.${domain.name}"; masters { ${domain.user.pac.hive.inetAddr.inet4Addr}; }; };
 #end
diff --git a/hsarback/src/de/hsadmin/mods/dom/zonefile.vm b/hsarback/src/de/hsadmin/mods/dom/zonefile.vm
index b5d7a40..e75b12c 100644
--- a/hsarback/src/de/hsadmin/mods/dom/zonefile.vm
+++ b/hsarback/src/de/hsadmin/mods/dom/zonefile.vm
@@ -15,12 +15,12 @@
 ${dom.name}.      IN    MX  30 mailin2.hostsharing.net.
 ${dom.name}.      IN    MX  30 mailin3.hostsharing.net.
 
-${dom.name}.      IN    A ${pac.curINetAddr.inetAddr}
-;${dom.name}.      IN    AAAA ${pac.curINet6Addr.inetAddr}
+${dom.name}.      IN    A ${pac.curINetAddr.inet4Addr}
+;${dom.name}.      IN    AAAA ${pac.curINetAddr.inet6Addr}
 
 *.${dom.name}.    IN    MX  30 mailin1.hostsharing.net.
 *.${dom.name}.    IN    MX  30 mailin2.hostsharing.net.
 *.${dom.name}.    IN    MX  30 mailin3.hostsharing.net.
 
-*.${dom.name}.    IN    A    ${pac.curINetAddr.inetAddr}
-;*.${dom.name}.    IN    AAAA ${pac.curINet6Addr.inetAddr}
+*.${dom.name}.    IN    A    ${pac.curINetAddr.inet4Addr}
+;*.${dom.name}.    IN    AAAA ${pac.curINetAddr.inet6Addr}
diff --git a/hsarback/src/de/hsadmin/mods/pac/Hive.java b/hsarback/src/de/hsadmin/mods/pac/Hive.java
index 537656e..deaf3a8 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Hive.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Hive.java
@@ -145,7 +145,7 @@
 	}
 	
 	public String getDefaultGateway() {
-		String hiveIP = getInetAddr().getInetAddr();
+		String hiveIP = getInetAddr().getInet4Addr();
 		return Config.getInstance().getProperty("hsadmin.default_gateway", hiveIP.substring(0, hiveIP.lastIndexOf('.')) + ".1");
 	}
 }
diff --git a/hsarback/src/de/hsadmin/mods/pac/INetAddress.java b/hsarback/src/de/hsadmin/mods/pac/INetAddress.java
index bfaeff3..682cb3c 100644
--- a/hsarback/src/de/hsadmin/mods/pac/INetAddress.java
+++ b/hsarback/src/de/hsadmin/mods/pac/INetAddress.java
@@ -13,6 +13,8 @@
 import javax.persistence.Table;
 
 import de.hsadmin.core.model.AbstractEntity;
+import de.hsadmin.core.qserv.ProcessorException;
+import de.hsadmin.core.util.IPv6Trick;
 import de.hsadmin.mods.user.UnixUser;
 
 @Entity(name = "INetAddresses")
@@ -51,7 +53,7 @@
 
 	@Override
 	public String createStringKey() {
-		return getInetAddr();
+		return getInet4Addr();
 	}
 
 	@Override
@@ -67,10 +69,14 @@
 		this.id = id;
 	}
 
-	public String getInetAddr() {
+	public String getInet4Addr() {
 		return name;
 	}
 
+	public String getInet6Addr() throws ProcessorException {
+		return IPv6Trick.convertIPv4ToIPv6(name);
+	}
+
 	public void setInetAddr(String inetAddr) {
 		this.name = inetAddr;
 	}
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
index 9b6e839..2c4d970 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
@@ -49,10 +49,10 @@
 		basepac = (BasePac) qBasepac.getSingleResult();
 		pac.setBasepac(basepac);
 		INetAddress curINetAddr = pac.getCurINetAddr();
-		if (curINetAddr == null || curINetAddr.getInetAddr() == null || curINetAddr.getInetAddr().length() == 0) {
+		if (curINetAddr == null || curINetAddr.getInet4Addr() == null || curINetAddr.getInet4Addr().length() == 0) {
 			throw new HSAdminException("curinetaddr required");
 		}
-		Query qINetAddr = em.createNativeQuery("SELECT * FROM inet_addr WHERE inet_addr = inet '" + curINetAddr.getInetAddr() + "'", INetAddress.class);
+		Query qINetAddr = em.createNativeQuery("SELECT * FROM inet_addr WHERE inet_addr = inet '" + curINetAddr.getInet4Addr() + "'", INetAddress.class);
 		curINetAddr = (INetAddress) qINetAddr.getSingleResult();
 		pac.setCurINetAddr(curINetAddr);
 		Customer customer = pac.getCustomer();
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
index aafd023..0b5d6f8 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
@@ -175,30 +175,30 @@
 				);
 	}
 
-	private Processor createIfUp(final Pac pac) {
-		return new CompoundProcessor(new ShellProcessor("ip addr add "
-				+ pac.getCurINetAddr().getInetAddr()
+	private Processor createIfUp(final Pac pac) throws ProcessorException {
+		return new CompoundProcessor(
+			new ShellProcessor("ip addr add "
+				+ pac.getCurINetAddr().getInet4Addr()
 				+ "/32 dev eth0"
-				+ pac.getName()
 			),
-        new ShellProcessor("ip addr add "
-        + pac.getCurINet6Addr().getInetAddr()
-        + "/128 dev eth0 noprefixroute preferred_lft 0"
-        + pac.getName()
-      ));
+			new ShellProcessor("ip addr add "
+		        + pac.getCurINetAddr().getInet6Addr()
+		        + "/128 dev eth0 noprefixroute preferred_lft 0"
+			)
+		);
 	}
 
-	private Processor createIfDown(final Pac pac) {
-		return new CompoundProcessor(new ShellProcessor("ip addr del "
-				+ pac.getCurINetAddr().getInetAddr()
+	private Processor createIfDown(final Pac pac) throws ProcessorException {
+		return new CompoundProcessor(
+			new ShellProcessor("ip addr del "
+				+ pac.getCurINetAddr().getInet4Addr()
 				+ "/32 dev eth0"
-				+ pac.getName()
 			),
-        new ShellProcessor("ip addr add "
-        + pac.getCurINet6Addr().getInetAddr()
-        + "/128 dev eth0 noprefixroute preferred_lft 0"
-        + pac.getName()
-      ));
+			new ShellProcessor("ip addr del "
+		        + pac.getCurINetAddr().getInet6Addr()
+		        + "/128 dev eth0 noprefixroute preferred_lft 0"
+			)
+		);
 	}
 
 	private UnixUser getPacAdminUser(Pac pac) throws ProcessorException {
diff --git a/hsarback/src/de/hsadmin/mods/pac/hosts.vm b/hsarback/src/de/hsadmin/mods/pac/hosts.vm
index 0a2fdb1..14d15d8 100644
--- a/hsarback/src/de/hsadmin/mods/pac/hosts.vm
+++ b/hsarback/src/de/hsadmin/mods/pac/hosts.vm
@@ -11,10 +11,10 @@
 ff02::2                         ip6-allrouters
 ff02::3                         ip6-allhosts
 
-${hive.inetAddr.inetAddr} ${hive.name}.hostsharing.net ${hive.name} localhive
-${hive.inet6Addr.inetAddr} ${hive.name}.hostsharing.net ${hive.name} localhive
+${hive.inetAddr.inet4Addr} ${hive.name}.hostsharing.net ${hive.name} localhive
+${hive.inetAddr.inet6Addr} ${hive.name}.hostsharing.net ${hive.name} localhive
 
 #foreach( $pac in ${hive.webpacs} )
-${pac.curINetAddr.inetAddr} ${pac.name}.hostsharing.net ${pac.name}
-${pac.curINet6Addr.inetAddr} ${pac.name}.hostsharing.net ${pac.name}
+${pac.curINetAddr.inet4Addr} ${pac.name}.hostsharing.net ${pac.name}
+${pac.curINetAddr.inet6Addr} ${pac.name}.hostsharing.net ${pac.name}
 #end
diff --git a/hsarback/src/de/hsadmin/mods/pac/httpd-virtual.vm b/hsarback/src/de/hsadmin/mods/pac/httpd-virtual.vm
index 5c5fa2b..13b9922 100644
--- a/hsarback/src/de/hsadmin/mods/pac/httpd-virtual.vm
+++ b/hsarback/src/de/hsadmin/mods/pac/httpd-virtual.vm
@@ -4,7 +4,7 @@
 #
 
 #foreach( $pac in ${hive.webpacs} )
-<VirtualHost ${pac.curINetAddr.inetAddr}:80 ${pac.curINet6Addr.inetAddr}:80>
+<VirtualHost ${pac.curINetAddr.inet4Addr}:80 ${pac.curINetAddr.inet6Addr}:80>
     ServerName _
     ServerAdmin webmaster@${pac.name}.hostsharing.net
 
@@ -15,7 +15,7 @@
 
 </VirtualHost>
 
-<VirtualHost ${pac.curINetAddr.inetAddr}:443 ${pac.curINet6Addr.inetAddr}:443>
+<VirtualHost ${pac.curINetAddr.inet4Addr}:443 ${pac.curINetAddr.inet6Addr}:443>
     ServerName _
     ServerAdmin webmaster@${pac.name}.hostsharing.net
 
diff --git a/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv4.vm b/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv4.vm
index aa542d0..3095ebc 100644
--- a/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv4.vm
+++ b/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv4.vm
@@ -4,5 +4,5 @@
 #
 #
 #foreach( $pac in ${hive.webpacs} )
-eth0:${pac.name}:${pac.curINetAddr.inetAddr}/32
+eth0:${pac.name}:${pac.curINetAddr.inet4Addr}/32
 #end
\ No newline at end of file
diff --git a/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv6.vm b/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv6.vm
index 577218b..f1e955f 100644
--- a/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv6.vm
+++ b/hsarback/src/de/hsadmin/mods/pac/pac-addr-ipv6.vm
@@ -4,5 +4,5 @@
 #
 #
 #foreach( $pac in ${hive.webpacs} )
-eth0:${pac.name}:${pac.curINet6Addr.inetAddr}/128
+eth0:${pac.name}:${pac.curINetAddr.inet6Addr}/128
 #end
diff --git a/hsarback/src/de/hsadmin/mods/pac/proftpd-pacs-conf.vm b/hsarback/src/de/hsadmin/mods/pac/proftpd-pacs-conf.vm
index e35ffb2..cf86562 100644
--- a/hsarback/src/de/hsadmin/mods/pac/proftpd-pacs-conf.vm
+++ b/hsarback/src/de/hsadmin/mods/pac/proftpd-pacs-conf.vm
@@ -4,7 +4,7 @@
 #
 
 #foreach( $pac in ${hive.webpacs} )
-<VirtualHost ${pac.curINetAddr.inetAddr} ${pac.curINet6Addr.inetAddr}>
+<VirtualHost ${pac.curINetAddr.inet4Addr} ${pac.curINetAddr.inet6Addr}>
         DefaultRoot             ~ ${pac.name}
         ServerName              "${pac.name}.hostsharing.net"
         AllowOverwrite          on
diff --git a/hsarback/src/de/hsadmin/remote/PacRemote.java b/hsarback/src/de/hsadmin/remote/PacRemote.java
index 2142e31..ff1daac 100644
--- a/hsarback/src/de/hsadmin/remote/PacRemote.java
+++ b/hsarback/src/de/hsadmin/remote/PacRemote.java
@@ -25,7 +25,7 @@
 		resultMap.put("id", Long.toString(pac.getId()));
 		resultMap.put("hive", pac.getHiveName());
 		resultMap.put("customer", pac.getCustomer().getName());
-		resultMap.put("curinetaddr", pac.getCurINetAddr().getInetAddr());
+		resultMap.put("curinetaddr", pac.getCurINetAddr().getInet4Addr());
 		resultMap.put("created", TextUtil.format(pac.getCreated()));
 		resultMap.put("free", TextUtil.format(pac.isFree()));
 		Date cancelled = pac.getCancelled();
@@ -76,7 +76,7 @@
 		}
 		INetAddress curINetAddr = pac.getCurINetAddr();
 		String inetAddrString = (String) setParams.get("curinetaddr");
-		if ((curINetAddr == null || curINetAddr.getInetAddr() == null) && assertNotNull(inetAddrString)) {
+		if ((curINetAddr == null || curINetAddr.getInet4Addr() == null) && assertNotNull(inetAddrString)) {
 			curINetAddr = new INetAddress(inetAddrString);
 			pac.setCurINetAddr(curINetAddr);
 		}

--
Gitblit v1.9.0-SNAPSHOT