* @throws UnknownHostException
* @throws DNSException
*/
public static boolean verifySenderAddress(DNSResolver dresolver, InetAddressResolver iresolver, ReceivedHeaderParser parser, String receivedHeaderLine, String senderAddress) throws JasenParseException, UnknownHostException, DNSException {
ReceivedHeaderParserData data = parser.parse(receivedHeaderLine, iresolver);
if(data == null) {
throw new JasenParseException("Unable to parse header.", ParseErrorType.PARSE_ERROR);
}
// Use the InetAddress cache
InetAddress receiverAddress = iresolver.getByName(data.getSenderIPAddress());
String senderDomain = getDomainFromAddress(senderAddress);
String receiverDomain = receiverAddress.getHostName();
// Get the MX records for the sender domain
MXRecord[] senderMXRecords = DNSUtils.getMXRecords(dresolver, senderDomain);
if (senderMXRecords != null && senderMXRecords.length > 0) {
// Get the root domain for both the receiver and sender
if (!DNSUtils.isIPv4Address(receiverDomain)) {
String senderRootDomain = DNSUtils.getRootDomain(data.getSenderHostName());
String receiverRootDomain = DNSUtils.getRootDomain(receiverDomain);
if (receiverRootDomain.equalsIgnoreCase(senderRootDomain)) {
// Get the MX records for the receiver domain
MXRecord[] receiverMXRecords = DNSUtils.getMXRecords(dresolver, receiverRootDomain);
// The sender address is valid if one IP matches
MXRecord senderMX = null;
MXRecord receiverMX = null;
if (senderMXRecords != null && senderMXRecords.length > 0) {
if (receiverMXRecords != null && receiverMXRecords.length > 0) {
for (int i = 0; i < senderMXRecords.length; i++) {
senderMX = senderMXRecords[i];
for (int j = 0; j < receiverMXRecords.length; j++) {
receiverMX = receiverMXRecords[j];
if (senderMX.getAddress().getHostAddress().equalsIgnoreCase(receiverMX.getAddress().getHostAddress())) {
return true;
}
}
}
}
else {
return false;
}
}
else {
return false;
}
}
else {
// The IP does not match the host. This may be a forgery but we will still test the MX
// records against the IP address
String mtaIPAddress = data.getSenderIPAddress();
for (int i = 0; i < senderMXRecords.length; i++) {
if (senderMXRecords[i].getAddress().getHostAddress().equals(mtaIPAddress)) {
return true;
}
}