Parse received header / TLS

pull/194/merge
M66B 4 years ago
parent 3b3f06d713
commit 303ebe3bf2

@ -41,6 +41,7 @@ import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.CertificateParsingException; import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
@ -501,6 +502,20 @@ public class ConnectionHelper {
return addr; return addr;
} }
static boolean isLocalIPAddress(String host) {
boolean numeric = ConnectionHelper.jni_is_numeric_address(host);
if (!numeric)
return false;
try {
InetAddress addr = ConnectionHelper.from6to4(InetAddress.getByName(host));
return (addr.isSiteLocalAddress() || addr.isLinkLocalAddress());
} catch (UnknownHostException ex) {
Log.e(ex);
return false;
}
}
static List<String> getCommonNames(Context context, String domain, int port, int timeout) throws IOException { static List<String> getCommonNames(Context context, String domain, int port, int timeout) throws IOException {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
InetSocketAddress address = new InetSocketAddress(domain, port); InetSocketAddress address = new InetSocketAddress(domain, port);

@ -2393,8 +2393,6 @@ public class HtmlHelper {
try { try {
// https://datatracker.ietf.org/doc/html/rfc2821#section-4.4 // https://datatracker.ietf.org/doc/html/rfc2821#section-4.4
final List<String> words = Collections.unmodifiableList(Arrays.asList(
"from", "by", "via", "with", "id", "for"));
final DateFormat DTF = Helper.getDateTimeInstance(context, DateFormat.SHORT, DateFormat.MEDIUM); final DateFormat DTF = Helper.getDateTimeInstance(context, DateFormat.SHORT, DateFormat.MEDIUM);
ByteArrayInputStream bis = new ByteArrayInputStream(headers.getBytes()); ByteArrayInputStream bis = new ByteArrayInputStream(headers.getBytes());
@ -2456,7 +2454,7 @@ public class HtmlHelper {
s = ssb.length(); s = ssb.length();
ssb.append(w[j]); ssb.append(w[j]);
if (!p && words.contains(w[j].toLowerCase(Locale.ROOT))) if (!p && MessageHelper.RECEIVED_WORDS.contains(w[j].toLowerCase(Locale.ROOT)))
ssb.setSpan(new ForegroundColorSpan(textColorLink), s, ssb.length(), 0); ssb.setSpan(new ForegroundColorSpan(textColorLink), s, ssb.length(), 0);
if (w[j].endsWith(")")) if (w[j].endsWith(")"))

@ -66,9 +66,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.IDN; import java.net.IDN;
import java.net.InetAddress;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
@ -135,6 +133,10 @@ public class MessageHelper {
static final String HEADER_CORRELATION_ID = "X-Correlation-ID"; static final String HEADER_CORRELATION_ID = "X-Correlation-ID";
static final int MAX_SUBJECT_AGE = 48; // hours static final int MAX_SUBJECT_AGE = 48; // hours
static final List<String> RECEIVED_WORDS = Collections.unmodifiableList(Arrays.asList(
"from", "by", "via", "with", "id", "for"
));
private static final int MAX_HEADER_LENGTH = 998; private static final int MAX_HEADER_LENGTH = 998;
private static final int MAX_MESSAGE_SIZE = 10 * 1024 * 1024; // bytes private static final int MAX_MESSAGE_SIZE = 10 * 1024 * 1024; // bytes
private static final long ATTACHMENT_PROGRESS_UPDATE = 1500L; // milliseconds private static final long ATTACHMENT_PROGRESS_UPDATE = 1500L; // milliseconds
@ -1917,6 +1919,12 @@ public class MessageHelper {
} }
Boolean getTLS() throws MessagingException { Boolean getTLS() throws MessagingException {
Boolean tls = _getTLS();
Log.i("--- TLS=" + tls);
return tls;
}
Boolean _getTLS() throws MessagingException {
// https://datatracker.ietf.org/doc/html/rfc2821#section-4.4 // https://datatracker.ietf.org/doc/html/rfc2821#section-4.4
// Time-stamp-line = "Received:" FWS Stamp <CRLF> // Time-stamp-line = "Received:" FWS Stamp <CRLF>
@ -1943,66 +1951,94 @@ public class MessageHelper {
if (received == null || received.length == 0) if (received == null || received.length == 0)
return null; return null;
final List<String> words = Collections.unmodifiableList(Arrays.asList(
"from", "by", "via", "with", "id", "for"));
// First header is last added header // First header is last added header
Log.i("=======");
for (String r : received) { for (String r : received) {
String header = MimeUtility.unfold(r); String header = MimeUtility.unfold(r);
Log.i("--- header=" + header);
int s, e; // Strip date
do {
s = header.indexOf('(');
e = header.indexOf(')', s);
if (s > 0 && e > 0)
header = header.substring(0, s) + header.substring((e + 1));
} while (s > 0 && e > 0);
int semi = header.lastIndexOf(';'); int semi = header.lastIndexOf(';');
if (semi > 0) if (semi > 0)
header = header.substring(0, semi); header = header.substring(0, semi);
header = header.replaceAll("\\s+", " "); if (header.contains("TLS"))
continue;
Log.i("--- header=" + header); // (qmail nnn invoked by uid nnn); 1 Jan 2022 00:00:00 -0000
if (header.contains("qmail") && header.contains("invoked by uid"))
continue;
// Normalize white space
header = header.replaceAll("\\s+", " ");
// Get key/values
String[] parts = header.split(" "); String[] parts = header.split(" ");
boolean hasFrom = false; Map<String, StringBuilder> kv = new HashMap<>();
boolean hasWith = false; String key = null;
for (int j = 0; j < parts.length - 1; j++) { for (int p = 0; p < parts.length; p++) {
Log.i("--- part " + j + " " + parts[j] + "=" + parts[j + 1]); String k = parts[p].toLowerCase(Locale.ROOT);
if ("from".equalsIgnoreCase(parts[j])) { if (RECEIVED_WORDS.contains(k)) {
hasFrom = true; key = k;
String from = parts[j + 1].toLowerCase(Locale.ROOT); if (!kv.containsKey(key))
boolean numeric = ConnectionHelper.jni_is_numeric_address(from); kv.put(key, new StringBuilder());
Log.i("--- numeric=" + numeric); } else if (key != null) {
if (numeric) StringBuilder sb = kv.get(key);
try { if (sb.length() > 0)
InetAddress addr = InetAddress.getByName(from); sb.append(' ');
if (addr.isSiteLocalAddress()) sb.append(parts[p]);
break;
Log.i("--- remote");
} catch (UnknownHostException ex) {
Log.e(ex);
} }
} else if ("with".equalsIgnoreCase(parts[j])) {
hasWith = true;
if (hasFrom) {
String with = parts[j + 1].toUpperCase(Locale.ROOT);
Log.i("--- MTP=" + with.contains("MTP") + " MTPS=" + with.contains("MTPS"));
// https://www.iana.org/assignments/mail-parameters/mail-parameters.txt
if (!with.contains("MTP"))
return null;
if (!with.contains("MTPS"))
return false;
} }
break;
// Dump
for (String k : kv.keySet())
Log.i("--- " + k + "=" + kv.get(k));
// Check if 'by' local address
if (kv.containsKey("by")) {
String by = kv.get("by").toString();
if (by.contains("localhost") || by.contains("[127.0.0.1]") || by.contains("[::1]"))
continue;
int b = by.indexOf(' ');
String host = (b < 0 ? by : by.substring(0, b));
if (ConnectionHelper.isLocalIPAddress(host))
continue;
} }
if (words.contains(parts[j]))
j++; // Check if 'from' local address
if (kv.containsKey("from")) {
String from = kv.get("from").toString();
if (from.contains("localhost") || from.contains("[127.0.0.1]") || from.contains("[::1]"))
continue;
int f = from.indexOf(' ');
String host = (f < 0 ? from : from.substring(0, f));
if (ConnectionHelper.isLocalIPAddress(host))
continue;
} }
if (hasFrom && !hasWith)
// Check Microsoft front end transport (proxy)
// https://social.technet.microsoft.com/wiki/contents/articles/50370.exchange-2016-what-is-the-front-end-transport-service-on-the-mailbox-role.aspx
if (kv.containsKey("via")) {
String via = kv.get("via").toString();
if ("Frontend Transport".equals(via))
continue;
}
// Check protocol
if (!kv.containsKey("with"))
return null; return null;
// https://www.iana.org/assignments/mail-parameters/mail-parameters.txt
String with = kv.get("with").toString();
int w = with.indexOf(' ');
String protocol = (w < 0 ? with : with.substring(0, w)).toLowerCase(Locale.ROOT);
if ("mapi".equals(protocol)) // https://en.wikipedia.org/wiki/MAPI
continue;
if (!protocol.contains("mtp"))
return null;
if (!protocol.contains("mtps"))
return false;
} }
return true; return true;

Loading…
Cancel
Save