use of com.zimbra.soap.admin.message.CheckDomainMXRecordResponse in project zm-mailbox by Zimbra.
the class CheckDomainMXRecord method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
CheckDomainMXRecordRequest req = JaxbUtil.elementToJaxb(request);
DomainBy domainBy = req.getDomain().getBy().toKeyDomainBy();
String value = req.getDomain().getKey();
Domain domain = prov.get(domainBy, value);
checkDomainRight(zsc, domain, Admin.R_checkDomainMXRecord);
String SMTPHost = domain.getAttr(Provisioning.A_zimbraDNSCheckHostname, true);
String domainName = domain.getName();
if (SMTPHost == null || SMTPHost.length() < 1)
SMTPHost = domain.getAttr(Provisioning.A_zimbraSmtpHostname, false);
if (SMTPHost == null || SMTPHost.length() < 1)
SMTPHost = prov.getLocalServer().getAttr(Provisioning.A_zimbraSmtpHostname);
if (SMTPHost == null || SMTPHost.length() < 1)
SMTPHost = prov.getConfig().getAttr(Provisioning.A_zimbraSmtpHostname);
if (SMTPHost == null || SMTPHost.length() < 1)
SMTPHost = domain.getName();
String SMTPHostMatch = String.format("^\\d+\\s%s\\.$", SMTPHost);
ZimbraLog.soap.info("checking domain mx record");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
String message = String.format("Domain is configured to use SMTP host: %s. None of the MX records match this name.", SMTPHost);
// Element response = zsc.createElement(AdminConstants.CHECK_DOMAIN_MX_RECORD_RESPONSE);
CheckDomainMXRecordResponse resp = new CheckDomainMXRecordResponse();
boolean found = false;
try {
DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes(domainName, new String[] { "MX" });
if (attrs.size() < 1) {
throw ServiceException.FAILURE("NoMXRecordsForDomain", null);
}
for (NamingEnumeration<? extends Attribute> ne = attrs.getAll(); ne.hasMore(); ) {
Attribute attr = (Attribute) ne.next();
if (attr.size() == 1) {
ZimbraLog.soap.info("single attribute");
Object o = attr.get();
if (o instanceof String) {
String rec = o.toString();
ZimbraLog.soap.info("found MX record " + rec);
if (rec.matches(SMTPHostMatch)) {
found = true;
break;
}
resp.addEntry(rec);
} else {
String rec = new String((byte[]) o);
ZimbraLog.soap.info("found MX attribute " + attr.getID() + " = " + rec);
if (rec.matches(SMTPHostMatch)) {
found = true;
break;
}
resp.addEntry(rec);
}
} else {
ZimbraLog.soap.info("multivalued attribute");
for (int i = 0; i < attr.size(); i++) {
Object o = attr.get(i);
if (o instanceof String) {
String rec = o.toString();
ZimbraLog.soap.info("found MX record " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
if (rec.matches(SMTPHostMatch)) {
found = true;
break;
}
resp.addEntry(rec);
} else {
String rec = new String((byte[]) o);
ZimbraLog.soap.info("found MX attribute " + attr.getID() + "-" + Integer.toString(i) + " = " + rec);
if (rec.matches(SMTPHostMatch)) {
found = true;
break;
}
resp.addEntry(rec);
//message = String.format("%s %s", message,rec);
}
}
}
}
if (found)
resp.setCode("Ok");
else {
resp.setCode("Failed");
resp.setMessage(message);
}
} catch (NameNotFoundException e) {
throw ServiceException.FAILURE("NameNotFoundException", e);
} catch (NamingException e) {
throw ServiceException.FAILURE("Failed to verify domain's MX record. " + e.getMessage(), e);
}
return zsc.jaxbToElement(resp);
}
Aggregations