Alte DNS-Utils entfern. Fixed #104.

This commit is contained in:
Michael Hierweck 2012-07-17 13:20:00 +00:00
parent 4cb00ec2a3
commit dc7c71fc36
5 changed files with 0 additions and 722 deletions

View File

@ -1,50 +0,0 @@
package de.hsadmin.core.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.util.dns.DNSQuery;
import de.hsadmin.core.util.dns.DNSService;
public class DNSCheck {
private String dnsServer;
public DNSCheck(String dnsServer) throws HSAdminException {
this.dnsServer = dnsServer;
InetAddress dnsInetAddress;
try {
dnsInetAddress = InetAddress.getByName(dnsServer);
DNSService.SetDNSAddress(dnsInetAddress);
} catch (UnknownHostException e) {
throw new HSAdminException(e);
}
}
public boolean checkDomain(String domain) throws HSAdminException {
boolean hasNSRecord = false;
boolean hasARecord = false;
DNSQuery dnsQuery = new DNSQuery();
dnsQuery.SetQuery(DNSQuery.TYPE_NS, DNSQuery.CLASS_INTERNET, domain);
if (DNSService.PerformDNSQuery(dnsQuery)) {
hasNSRecord = dnsQuery.getDNSAuthorityRecords() != null && dnsQuery.getDNSAuthorityRecords().length > 0;
if (dnsQuery.getDNSAuthorityRecords() != null) {
System.out.println("NS:" + dnsQuery.getDNSAuthorityRecords().length);
}
} else {
throw new HSAdminException("domain " + domain + " is not delegated to " + dnsServer);
}
dnsQuery.SetQuery(DNSQuery.TYPE_A, DNSQuery.CLASS_INTERNET, domain);
if (DNSService.PerformDNSQuery(dnsQuery)) {
hasARecord = dnsQuery.getDNSAnswerRecords() != null && dnsQuery.getDNSAnswerRecords().length > 0;
if (dnsQuery.getDNSAnswerRecords() != null) {
System.out.println("A: " + dnsQuery.getDNSAnswerRecords().length);
}
} else {
throw new HSAdminException("domain " + domain + " is not delegated to " + dnsServer);
}
return hasNSRecord & !hasARecord;
}
}

View File

@ -1,395 +0,0 @@
package de.hsadmin.core.util.dns;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class DNSQuery {
// DNS constants
private final static int MAX_LABEL = 128;
// DNS flag values
private final static int QR_MASK = 0x8000;
private final static int QR_SHIFT = 0;
private final static int OPCODE_MASK = 0x7800;
private final static int AA_MASK = 0x0400;
private final static int TC_MASK = 0x0200;
private final static int RD_MASK = 0x0100;
private final static int RA_MASK = 0x0080;
private final static int ZERO_MASK = 0x0070;
private final static int RCODE_MASK = 0x000f;
// DNS query/type constants
public final static int TYPE_A = 1;
public final static int TYPE_NS = 2;
public final static int TYPE_MD = 3;
public final static int TYPE_MF = 4;
public final static int TYPE_CNAME = 5;
public final static int TYPE_SOA = 6;
public final static int TYPE_MB = 7;
public final static int TYPE_MG = 8;
public final static int TYPE_MR = 9;
public final static int TYPE_NULL = 10;
public final static int TYPE_WKS = 11;
public final static int TYPE_PTR = 12;
public final static int TYPE_HINFO = 13;
public final static int TYPE_MINFO = 14;
public final static int TYPE_MX = 15;
public final static int TYPE_AXFR = 252;
public final static int TYPE_ANY = 255;
// DNS query classes
public final static byte CLASS_INTERNET = 1;
// query data members
public int m_idQuery = 0x0101;
// opcodes
public final static int OP_NORM_QRY = 0x0000;
public final static int OP_INVERSE_QRY = 0x0800;
public final static int OP_SERVER_STAT = 0x1000;
public int m_iOpCode = OP_NORM_QRY;
// flags
public boolean m_fQueryResult = false;
public boolean m_fAuthAns = false;
public boolean m_fTruncated = false;
public boolean m_fRecurse = true;
public boolean m_fRecursionAvail = false;
private int m_iQryType = TYPE_A;
private int m_iQryClass = CLASS_INTERNET;
private String m_strQryName = null;
// return codes
public int m_iRCode = 0;
public final static int RCODE_SUCCESS = 0;
public final static int RCODE_NAME_ERROR = 0x0003;
public int m_cQuestions = 1;
public int m_cAnswerRRs = 0;
public int m_cAuthRRs = 0;
public int m_cInfoRRs = 0;
DNSResourceRecord [] m_arrAns;
DNSResourceRecord [] m_arrAuth;
DNSResourceRecord [] m_arrInfo;
public static String GetTypeDesc(int iType) {
String str = null;
switch (iType) {
case TYPE_A: { str = new String("TYPE_A"); } break;
case TYPE_NS: { str = new String("TYPE_NS"); } break;
case TYPE_CNAME: { str = new String("TYPE_CNAME"); } break;
case TYPE_SOA: { str = new String("TYPE_SOA"); } break;
case TYPE_PTR: { str = new String("TYPE_PTR"); } break;
case TYPE_HINFO: { str = new String("TYPE_HINFO"); } break;
case TYPE_MX: { str = new String("TYPE_MX"); } break;
case TYPE_AXFR: { str = new String("TYPE_AXFR"); } break;
case TYPE_ANY: { str = new String("TYPE_ANY"); } break;
default: { str = new String(Integer.toString(iType)); };
}
return str;
}
public DNSResourceRecord[] getDNSAnswerRecords() {
return m_arrAns;
}
public DNSResourceRecord[] getDNSAuthorityRecords() {
return m_arrAuth;
}
public static String GetClassDesc(int iClass) {
String str = null;
switch (iClass) {
case CLASS_INTERNET: { str = new String("CLASS_INTERNET"); } break;
default: { str = new String(); };
}
return str;
}
private int BuildFlags() {
int iFlags = 0;
iFlags |= m_iOpCode;
if (m_fRecurse) {
iFlags |= RD_MASK;
}
return iFlags;
}
private boolean SetFlags(int iFlags) {
m_fQueryResult = (iFlags & QR_MASK) == QR_MASK;
m_fAuthAns = (iFlags & AA_MASK) == AA_MASK;
m_fTruncated = (iFlags & TC_MASK) == TC_MASK;
m_fRecurse = (iFlags & RD_MASK) == RD_MASK;
m_fRecursionAvail = (iFlags & RA_MASK) == RA_MASK;
m_iRCode = iFlags & RCODE_MASK;
return m_iRCode == RCODE_SUCCESS;
}
public boolean SetQuery(int iType, int iClass, String strName) {
m_iQryType = iType;
m_iQryClass = iClass;
m_cQuestions = 1;
m_strQryName = new String(strName);
return true;
}
private boolean WriteFQDN(DataOutputStream dos, String strName) {
try {
int iPos = 0;
int iSep = 0;
while ((iSep = strName.indexOf('.', iPos)) >= 0) {
dos.writeByte((byte)(iSep - iPos));
dos.writeBytes(strName.substring(iPos, iSep));
iPos = iSep + 1;
}
if (iPos < strName.length()) {
dos.writeByte((byte)(strName.length() - iPos));
dos.writeBytes(strName.substring(iPos));
}
// terminator
dos.writeByte(0);
return true;
} catch (IOException ioe) {
return false;
}
}
public static String ReadLabelList(DataInputStream dis, byte [] abData) {
int cb;
byte [] ab = new byte[MAX_LABEL];
String str = new String();
String strNext = new String();
try {
while ((cb = dis.readUnsignedByte()) > 0) {
if ((cb & 0xc0) == 0xc0) {
int iReadOff = (int)(cb & ~0xc0) << 8;
cb = dis.readUnsignedByte();
iReadOff += (int)cb;
while (iReadOff < abData.length && ((cb = abData[iReadOff++]) > 0)) {
if ((cb & 0xc0) == 0xc0) {
return str;
}
strNext = new String(abData, 0, iReadOff, cb);
if (str.length() > 0) {
str += "." + strNext;
} else {
str = strNext;
}
iReadOff += cb;
}
return str;
} else {
byte [] abNext = new byte[cb];
if (dis.read(abNext) < cb) {
return new String();
}
strNext = new String(abNext, 0, 0, cb);
}
if (str.length() > 0) {
str += "." + strNext;
} else {
str = strNext;
}
}
} catch (IOException ioe) {
System.err.println("exception: " + ioe.getMessage());
}
return str;
}
public boolean WriteQuery(OutputStream os) {
try {
if (m_strQryName == null) {
return false;
}
DataOutputStream dos = new DataOutputStream(os);
// query sequence ID
dos.writeShort(m_idQuery);
// query flags
dos.writeShort(BuildFlags());
// question count
dos.writeShort(1);
// answer count
dos.writeShort(0);
// authority count
dos.writeShort(0);
// additional info count
dos.writeShort(0);
// write query name
WriteFQDN(dos, m_strQryName);
// query type
dos.writeShort(m_iQryType);
// query class
dos.writeShort(m_iQryClass);
return true;
} catch (IOException ioe) {
return false;
}
}
private void SortRRs(DNSResourceRecord [] arr, boolean fDescending) {
if (arr == null || arr.length < 2) {
return;
}
boolean fSwapped;
DNSResourceRecord rrSwap;
do {
fSwapped = false;
for (int i = 0; i < arr.length - 1; i++) {
boolean fSwap = false;
if (arr[i+1].m_iType < arr[i].m_iType) {
fSwap = true;
} else if (arr[i+1].m_iType == arr[i].m_iType) {
switch (arr[i].m_iType) {
case TYPE_MX: {
fSwap = arr[i+1].m_lData < arr[i].m_lData;
} break;
}
}
if (fSwap = fSwap ^ fDescending) {
rrSwap = arr[i];
arr[i] = arr[i+1];
arr[i+1] = rrSwap;
fSwapped = true;
}
}
} while (fSwapped);
}
public boolean ReadQuery(byte [] abData, int cbData) {
try {
ByteArrayInputStream is = new ByteArrayInputStream(abData, 0, cbData);
DataInputStream dis = new DataInputStream(is);
// query sequence ID
m_idQuery = dis.readShort();
// query flags
if (!SetFlags(dis.readShort())) {
return false;
}
// question count
m_cQuestions = dis.readShort();
// answer count
m_cAnswerRRs = dis.readShort();
// authority count
m_cAuthRRs = dis.readShort();
// additional info count
m_cInfoRRs = dis.readShort();
// read query name
m_strQryName = ReadLabelList(dis, abData);
// query type
m_iQryType = dis.readShort();
// query class
m_iQryClass = dis.readShort();
m_arrAns = new DNSResourceRecord[m_cAnswerRRs];
for (int i = 0; i < m_cAnswerRRs; i++) {
m_arrAns[i] = new DNSResourceRecord();
if (!m_arrAns[i].readRecord(dis, abData)) {
return false;
}
}
SortRRs(m_arrAns, false);
if (m_cAuthRRs > 0) {
m_arrAuth = new DNSResourceRecord[m_cAuthRRs];
for (int i = 0; i < m_cAuthRRs; i++) {
m_arrAuth[i] = new DNSResourceRecord();
if (!m_arrAuth[i].readRecord(dis, abData)) {
return false;
}
}
SortRRs(m_arrAuth, false);
} else {
m_arrAuth = null;
}
if (m_cInfoRRs > 0) {
m_arrInfo = new DNSResourceRecord[m_cInfoRRs];
for (int i = 0; i < m_cInfoRRs; i++) {
m_arrInfo[i] = new DNSResourceRecord();
if (!m_arrInfo[i].readRecord(dis, abData)) {
return false;
}
}
SortRRs(m_arrInfo, false);
} else {
m_arrInfo = null;
}
} catch (IOException ioe) {
System.err.println("exception: " + ioe.getMessage());
return false;
}
return true;
}
}

View File

@ -1,190 +0,0 @@
package de.hsadmin.core.util.dns;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
public class DNSResourceRecord {
public String m_strDomainName;
public int m_iType;
public int m_iClass;
public Date m_dtExpire = new Date();
public int m_cbData;
public byte [] m_abData = null;
// these fields are interpreted differently depending on m_iType
// (can you say union?)
public long m_lData;
public long m_lData1;
public long m_lData2;
public long m_lData3;
public long m_lData4;
public long m_lData5;
public String m_strData;
public String m_strData1;
public boolean readRecord(DataInputStream dis, byte [] abData) {
try {
m_strDomainName = DNSQuery.ReadLabelList(dis, abData);
m_iType = dis.readShort();
m_iClass = dis.readShort();
// time from DNS is in seconds, need milliseconds
long lSecondsToLive = dis.readInt();
Date dtNow = new Date();
m_dtExpire.setTime(dtNow.getTime() + (lSecondsToLive * 1000L));
m_cbData = dis.readShort();
m_abData = null;
switch (m_iType) {
case DNSQuery.TYPE_A: {
m_lData = dis.readInt();
} break;
case DNSQuery.TYPE_MX: {
m_lData = dis.readShort();
m_strData = DNSQuery.ReadLabelList(dis, abData);
} break;
case DNSQuery.TYPE_NS:
case DNSQuery.TYPE_MD:
case DNSQuery.TYPE_MF:
case DNSQuery.TYPE_CNAME:
case DNSQuery.TYPE_MB:
case DNSQuery.TYPE_MG:
case DNSQuery.TYPE_MR:
case DNSQuery.TYPE_PTR: {
m_strData = DNSQuery.ReadLabelList(dis, abData);
} break;
case DNSQuery.TYPE_SOA: {
// !!!LATER!!! I had a real problem getting this code to work.
// I think the spec I had was old RFC 883
m_strData = DNSQuery.ReadLabelList(dis, abData);
m_strData1 = DNSQuery.ReadLabelList(dis, abData);
// SERIAL
m_lData = dis.readUnsignedShort();
// REFRESH
m_lData1 = dis.readInt() & 0xffffffff;
// RETRY
m_lData2 = dis.readInt() & 0xffffffff;
// EXPIRE
m_lData3 = dis.readInt() & 0xffffffff;
// MINIMUM
m_lData4 = dis.readUnsignedShort();
// UNKNOWN
m_lData5 = dis.readInt() & 0xffffffff;
} break;
case DNSQuery.TYPE_MINFO:
case DNSQuery.TYPE_HINFO: {
m_strData = DNSQuery.ReadLabelList(dis, abData);
m_strData1 = DNSQuery.ReadLabelList(dis, abData);
} break;
default: {
m_abData = new byte[m_cbData];
dis.read(m_abData);
} break;
}
} catch (IOException ioe) {
System.err.println("exception: " + ioe.getMessage());
}
return true;
}
public String getMXServer() {
if (m_iType != DNSQuery.TYPE_MX) {
return new String();
}
return new String(m_strData);
}
public int getMXPref() {
if (m_iType != DNSQuery.TYPE_MX) {
return -1;
}
return (int)m_lData;
}
public void dumpRecord(PrintStream ps) {
ps.println("Domain: " + m_strDomainName);
ps.println("Type: " + DNSQuery.GetTypeDesc(m_iType));
ps.println("Class: " + DNSQuery.GetClassDesc(m_iClass));
ps.println("Expires: " + m_dtExpire.toString());
switch (m_iType) {
case DNSQuery.TYPE_A: {
ps.println("IP Address: " + Long.toHexString(m_lData));
} break;
case DNSQuery.TYPE_MX: {
ps.println("MX Server: " + m_strData);
ps.println("MX Pref: " + Long.toString(m_lData));
} break;
case DNSQuery.TYPE_NS:
case DNSQuery.TYPE_MD:
case DNSQuery.TYPE_MF:
case DNSQuery.TYPE_CNAME:
case DNSQuery.TYPE_MB:
case DNSQuery.TYPE_MG:
case DNSQuery.TYPE_MR:
case DNSQuery.TYPE_PTR: {
ps.println("Domain: " + m_strData);
} break;
case DNSQuery.TYPE_HINFO: {
ps.println("CPU: " + m_strData);
ps.println("OS: " + m_strData1);
} break;
case DNSQuery.TYPE_SOA: {
ps.println("MNAME: " + m_strData);
ps.println("RNAME: " + m_strData1);
ps.println("SERIAL: " + Long.toString(m_lData));
ps.println("REFRESH: " + Long.toString(m_lData1));
ps.println("RETRY: " + Long.toString(m_lData2));
ps.println("EXPIRE: " + Long.toString(m_lData3));
ps.println("MINIMUM: " + Long.toString(m_lData4));
ps.println("UNKNOWN: " + Long.toString(m_lData4));
} break;
default: {
ps.println("Data: " + new String(m_abData));
dumpBytes(ps, m_abData);
}
}
}
private void dumpBytes(PrintStream ps, byte [] ab) {
int i;
String strTemp;
for (i = 0; i < ab.length; i++) {
strTemp = Integer.toHexString(ab[i]);
if (strTemp.length() < 2) {
strTemp = "0" + strTemp;
}
ps.print(strTemp + " ");
if (i > 0 && ((i % 8) == 0 || i == ab.length-1)) {
ps.println();
}
}
}
}

View File

@ -1,80 +0,0 @@
package de.hsadmin.core.util.dns;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class DNSService {
public static int DNS_SOCKET = 53;
private static InetAddress m_iaDNS = null;
private static Socket m_sockService = null;
private static byte[] m_abReceive = new byte[512];
private static int m_iQuerySerial = 0x1234;
public static void SetDNSAddress(InetAddress iaDNS) {
m_iaDNS = iaDNS;
if (m_sockService == null) {
try {
m_sockService = new Socket(m_iaDNS, DNS_SOCKET, false);
} catch (IOException ioe) {
System.err.println("exception: " + ioe.getMessage());
}
}
}
public static boolean PerformDNSQuery(DNSQuery dns) {
if (m_iaDNS == null || dns == null) {
return false;
}
ByteArrayOutputStream bas = new ByteArrayOutputStream();
dns.WriteQuery(bas);
try {
m_sockService.getOutputStream().write(bas.toByteArray());
} catch (IOException ioe) {
return false;
}
try {
BufferedInputStream bis = new BufferedInputStream(m_sockService.getInputStream(), 512);
int cRetry = 5;
int cbAvail = 0;
while (cRetry-- > 0 && ((cbAvail = bis.available()) <= 0)) {
try {
Thread.currentThread().sleep(200);
} catch (InterruptedException ie) {
System.err.println("exception: " + ie.getMessage());
}
}
if (cbAvail > 0) {
int cbRead = bis.read(m_abReceive, 0, cbAvail);
m_iQuerySerial++;
if (cbRead > 0) {
dns.ReadQuery(m_abReceive, cbRead);
} else {
return false;
}
} else {
return false;
}
} catch (IOException ioe) {
return false;
}
return true;
}
}

View File

@ -11,7 +11,6 @@ import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl; import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.AuthorisationException; import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.util.DNSCheck;
import de.hsadmin.mods.dom.Domain.Status; import de.hsadmin.mods.dom.Domain.Status;
import de.hsadmin.mods.email.EMailAddress; import de.hsadmin.mods.email.EMailAddress;
import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.Pac;
@ -117,12 +116,6 @@ public class DomainModuleImpl extends AbstractModuleImpl {
if (loginUser.hasCustomerRoleFor(superDom.getUser().getPac().getCustomer())) { if (loginUser.hasCustomerRoleFor(superDom.getUser().getPac().getCustomer())) {
break; // same customer break; // same customer
} }
DNSCheck dnsCheck = new DNSCheck(dom.getDnsMaster());
if (dnsCheck.checkDomain(dom.getName())) {
break;
} else {
throw new AuthorisationException(loginUser, "add", dom);
}
} }
} }
} }