Use SOA record for quick setup

pull/194/merge
M66B 3 years ago
parent 0e96e2beec
commit 395f903edc

@ -33,6 +33,7 @@ import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord; import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Message; import org.xbill.DNS.Message;
import org.xbill.DNS.Record; import org.xbill.DNS.Record;
import org.xbill.DNS.SOARecord;
import org.xbill.DNS.SRVRecord; import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.SimpleResolver; import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TXTRecord; import org.xbill.DNS.TXTRecord;
@ -109,6 +110,9 @@ public class DnsHelper {
case "mx": case "mx":
rtype = Type.MX; rtype = Type.MX;
break; break;
case "soa":
rtype = Type.SOA;
break;
case "srv": case "srv":
rtype = Type.SRV; rtype = Type.SRV;
break; break;
@ -213,6 +217,9 @@ public class DnsHelper {
if (record instanceof MXRecord) { if (record instanceof MXRecord) {
MXRecord mx = (MXRecord) record; MXRecord mx = (MXRecord) record;
result.add(new DnsRecord(mx.getTarget().toString(true))); result.add(new DnsRecord(mx.getTarget().toString(true)));
} else if (record instanceof SOARecord) {
SOARecord soa = (SOARecord) record;
result.add(new DnsRecord(soa.getHost().toString(true)));
} else if (record instanceof SRVRecord) { } else if (record instanceof SRVRecord) {
SRVRecord srv = (SRVRecord) record; SRVRecord srv = (SRVRecord) record;
result.add(new DnsRecord(srv.getTarget().toString(true), srv.getPort())); result.add(new DnsRecord(srv.getTarget().toString(true), srv.getPort()));

@ -128,7 +128,7 @@ public class EmailProvider implements Parcelable {
List<EmailProvider> providers = loadProfiles(context); List<EmailProvider> providers = loadProfiles(context);
for (EmailProvider provider : providers) for (EmailProvider provider : providers)
if (provider.domain != null) if (provider.enabled && provider.domain != null)
for (String domain : provider.domain) for (String domain : provider.domain)
if (!result.contains(domain)) if (!result.contains(domain))
result.add(domain); result.add(domain);
@ -270,7 +270,7 @@ public class EmailProvider implements Parcelable {
List<EmailProvider> providers = loadProfiles(context); List<EmailProvider> providers = loadProfiles(context);
for (EmailProvider provider : providers) for (EmailProvider provider : providers)
if (provider.domain != null) if (provider.enabled && provider.domain != null)
for (String d : provider.domain) for (String d : provider.domain)
if (domain.toLowerCase(Locale.ROOT).matches(d)) { if (domain.toLowerCase(Locale.ROOT).matches(d)) {
EntityLog.log(context, "Provider from domain=" + domain + " (" + d + ")"); EntityLog.log(context, "Provider from domain=" + domain + " (" + d + ")");
@ -281,47 +281,64 @@ public class EmailProvider implements Parcelable {
List<EmailProvider> candidates = List<EmailProvider> candidates =
new ArrayList<>(_fromDomain(context, domain.toLowerCase(Locale.ROOT), email, discover)); new ArrayList<>(_fromDomain(context, domain.toLowerCase(Locale.ROOT), email, discover));
try { if (false) // Unsafe: the password could be sent to an unrelated email server
InetAddress iaddr = InetAddress.getByName(domain.toLowerCase(Locale.ROOT)); try {
List<String> commonNames = InetAddress iaddr = InetAddress.getByName(domain.toLowerCase(Locale.ROOT));
ConnectionHelper.getCommonNames(context, domain.toLowerCase(Locale.ROOT), 443, SCAN_TIMEOUT); List<String> commonNames =
EntityLog.log(context, "Website common names=" + TextUtils.join(",", commonNames)); ConnectionHelper.getCommonNames(context, domain.toLowerCase(Locale.ROOT), 443, SCAN_TIMEOUT);
for (String commonName : commonNames) { EntityLog.log(context, "Website common names=" + TextUtils.join(",", commonNames));
String altName = commonName; for (String commonName : commonNames) {
if (altName.startsWith("*.")) { String altName = commonName;
altName = altName.substring(2); if (altName.startsWith("*.")) {
if (commonNames.contains(altName)) altName = altName.substring(2);
continue; if (commonNames.contains(altName))
} continue;
}
if (!altName.equalsIgnoreCase(domain)) if (!altName.equalsIgnoreCase(domain))
try { try {
InetAddress ialt = InetAddress.getByName(altName); InetAddress ialt = InetAddress.getByName(altName);
if (!ialt.equals(iaddr)) { if (!ialt.equals(iaddr)) {
EntityLog.log(context, "Using website common name=" + altName); EntityLog.log(context, "Using website common name=" + altName);
candidates.addAll(_fromDomain(context, altName.toLowerCase(Locale.ROOT), email, discover)); candidates.addAll(_fromDomain(context, altName.toLowerCase(Locale.ROOT), email, discover));
}
} catch (Throwable ex) {
Log.w(ex);
} }
} catch (Throwable ex) { }
Log.w(ex); } catch (Throwable ex) {
} Log.w(ex);
} }
} catch (Throwable ex) {
Log.w(ex);
}
try { try {
DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, domain, "mx"); List<DnsHelper.DnsRecord> records = new ArrayList<>();
try {
DnsHelper.DnsRecord[] mx = DnsHelper.lookup(context, domain, "mx");
if (mx.length > 0)
records.add(mx[0]);
} catch (Throwable ex) {
Log.w(ex);
}
try {
// Primary DNS server
DnsHelper.DnsRecord[] soa = DnsHelper.lookup(context, domain, "soa");
if (soa.length > 0)
records.add(soa[0]);
} catch (Throwable ex) {
Log.w(ex);
}
for (DnsHelper.DnsRecord record : records) for (DnsHelper.DnsRecord record : records)
if (!TextUtils.isEmpty(record.name)) { if (!TextUtils.isEmpty(record.name)) {
String target = record.name.toLowerCase(Locale.ROOT); String target = record.name.toLowerCase(Locale.ROOT);
EntityLog.log(context, "MX target=" + target); EntityLog.log(context, "MX/SOA target=" + target);
for (EmailProvider provider : providers) { for (EmailProvider provider : providers) {
if (provider.mx != null) if (provider.enabled && provider.mx != null)
for (String mx : provider.mx) for (String mx : provider.mx)
if (target.matches(mx)) { if (target.matches(mx)) {
EntityLog.log(context, "From MX domain=" + domain); EntityLog.log(context, "From MX/SOA domain=" + domain);
candidates.add(provider); candidates.add(provider);
break; break;
} }
@ -329,7 +346,7 @@ public class EmailProvider implements Parcelable {
String mxparent = UriHelper.getParentDomain(context, target); String mxparent = UriHelper.getParentDomain(context, target);
String pdomain = UriHelper.getParentDomain(context, provider.imap.host); String pdomain = UriHelper.getParentDomain(context, provider.imap.host);
if (mxparent.equalsIgnoreCase(pdomain)) { if (mxparent.equalsIgnoreCase(pdomain)) {
EntityLog.log(context, "From MX host=" + provider.imap.host); EntityLog.log(context, "From MX/SOA host=" + provider.imap.host);
candidates.add(provider); candidates.add(provider);
break; break;
} }
@ -385,7 +402,8 @@ public class EmailProvider implements Parcelable {
// - ISPDB is not always correct // - ISPDB is not always correct
// - documentation links // - documentation links
for (EmailProvider provider : providers) for (EmailProvider provider : providers)
if (provider.imap.host.equals(candidate.imap.host) || if (provider.enabled &&
provider.imap.host.equals(candidate.imap.host) ||
provider.smtp.host.equals(candidate.smtp.host)) { provider.smtp.host.equals(candidate.smtp.host)) {
EntityLog.log(context, "Replacing auto config by profile=" + provider.name); EntityLog.log(context, "Replacing auto config by profile=" + provider.name);
if (!BuildConfig.DEBUG) if (!BuildConfig.DEBUG)
@ -941,7 +959,7 @@ public class EmailProvider implements Parcelable {
public boolean starttls; public boolean starttls;
// Scores: // Scores:
// 0 from MX record // 0 from MX/SOA record
// 10 from port scan // 10 from port scan
// +2 trusted host // +2 trusted host
// +1 trusted DNS name // +1 trusted DNS name

Loading…
Cancel
Save