private void appendReportingMTA(StringBuffer buffer) { try { buffer.append("Reporting-MTA: dns; " + dns.getHostName(dns.getLocalHost())) .append(LINE_BREAK); } catch (Exception e) { LOGGER.error("Sending DSN without required Reporting-MTA Address", e); } }
@Override protected boolean hasMXRecord(SMTPSession session, String domain) { // null sender so return if (domain == null) { return false; } Collection<String> records = null; // try to resolv the provided domain in the senderaddress. If it can not resolved do not accept it. try { records = dnsService.findMXRecords(domain); } catch (org.apache.james.dnsservice.api.TemporaryResolutionException e) { // TODO: Should we reject temporary ? } return !(records == null || records.size() == 0); }
/** * @see org.apache.james.protocols.smtp.DNSService#getAllByName(java.lang.String) */ public InetAddress[] getAllByName(String host) throws UnknownHostException { return dns.getAllByName(host); }
/** * Simple localhost resolution. * * @throws UnknownHostException */ @Test public void testLocalhost() throws UnknownHostException { assertThat(DNS_SERVER.getByName("localhost").toString()).isEqualTo("localhost/127.0.0.1"); String localHost = DNS_SERVER.getHostName(InetAddress.getByName("127.0.0.1")); // We only can check if the returned localhost is not empty. Its value // depends on the hosts file. assertThat(localHost.length() > 0).isTrue(); }
@Test public void containsDomainShouldReturnDetectedIp() throws Exception { String detected = "detected.tld"; String detectedIp = "148.25.32.1"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress = mock(InetAddress.class); when(detectedAddress.getHostAddress()).thenReturn(detectedIp); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); assertThat(domainList.containsDomain(Domain.of(detectedIp))).isTrue(); }
/** * @see org.apache.james.protocols.smtp.DNSService#getLocalHost() */ public InetAddress getLocalHost() throws UnknownHostException { return dns.getLocalHost(); }
@Override protected boolean resolve(String ip) { try { dns.getByName(ip); return true; } catch (UnknownHostException e) { return false; } }
mx = dnsService.findMXRecords(domain).iterator(); } catch (TemporaryResolutionException e1) { return new HookResult(HookReturnCode.DENYSOFT); String ip = dnsService.getByName(mxRec).getHostAddress();
/** * @see org.apache.james.protocols.smtp.DNSService#getHostName(java.net.InetAddress) */ public String getHostName(InetAddress addr) { return dns.getHostName(addr); }
public MXHostAddressIterator(Iterator<String> hosts, int defaultPort, DNSService dns, boolean useSingleIP) { checkNotNull(hosts, "Hosts is null"); checkNotNull(dns, "Dns is null"); final List<HostAddress> hAddresses = Lists.newArrayList(); while (hosts.hasNext()) { String nextHostname = hosts.next(); Map.Entry<String, String> hostAndPort = extractHostAndPort(nextHostname, defaultPort); try { final Collection<InetAddress> addrs; if (useSingleIP) { addrs = ImmutableList.of(dns.getByName(hostAndPort.getKey())); } else { addrs = dns.getAllByName(hostAndPort.getKey()); } for (InetAddress addr : addrs) { hAddresses.add(new HostAddress(hostAndPort.getKey(), "smtp://" + addr.getHostAddress() + ":" + hostAndPort.getValue())); } } catch (UnknownHostException uhe) { // this should never happen, since we just got // this host from mxHosts, which should have // already done this check. String logBuffer = "Couldn't resolve IP address for discovered host " + hostAndPort.getKey() + "."; LOGGER.error(logBuffer); } } addresses = hAddresses.iterator(); }
/** * @see org.apache.james.protocols.smtp.DNSService#findTXTRecords(java.lang.String) */ public Collection<String> findTXTRecords(String hostname) { return dns.findTXTRecords(hostname); }
/** * Performs DNS lookups as needed to find servers which should or might * support SMTP. Returns an Iterator over HostAddress, a specialized * subclass of javax.mail.URLName, which provides location information for * servers that are specified as mail handlers for the given hostname. This * is done using MX records, and the HostAddress instances are returned * sorted by MX priority. If no host is found for domainName, the Iterator * returned will be empty and the first call to hasNext() will return false. * * @see org.apache.james.dnsservice.api.DNSService#getSMTPHostAddresses(String) * @since Mailet API v2.2.0a16-unstable * @param domainName * - the domain for which to find mail servers * @return an Iterator over HostAddress instances, sorted by priority */ public Iterator<HostAddress> getSMTPHostAddresses(String domainName) { try { return dns.getSMTPHostAddresses(domainName); } catch (TemporaryResolutionException e) { // TODO: We only do this to not break backward compatiblity. Should // fixed later return Collections.unmodifiableCollection(new ArrayList<HostAddress>(0)).iterator(); } }
@Test public void getDomainsShouldContainDetectedDomainsAndIps() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String detected = "detected.tld"; String detectedIp = "148.25.32.1"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress = mock(InetAddress.class); when(detectedAddress.getHostAddress()).thenReturn(detectedIp); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); assertThat(domainList.getDomains()).contains(Domain.of(detected), Domain.of(detectedIp)); }
private String getRemoteHost() { try { return dns.getLocalHost().getHostName(); } catch (UnknownHostException e) { return "localhost"; } }
/** * @see org.apache.james.protocols.smtp.DNSService#getByName(java.lang.String) */ public InetAddress getByName(String host) throws UnknownHostException { return dns.getByName(host); }
mx = dnsService.findMXRecords(domain.name()).iterator(); } catch (TemporaryResolutionException e1) { return HookResult.DENYSOFT; String ip = dnsService.getByName(mxRec).getHostAddress();
@Override protected Collection<String> resolveTXTRecords(String ip) { return dns.findTXTRecords(ip); } }
/** * @see org.apache.james.protocols.smtp.DNSService#getSMTPHostAddresses(java.lang.String) */ public Iterator<HostAddress> getSMTPHostAddresses(String domainName) throws TemporaryResolutionException { try { return dns.getSMTPHostAddresses(domainName); } catch (org.apache.james.dnsservice.api.TemporaryResolutionException e) { throw new TemporaryResolutionException(e.getMessage()); } }
private ImmutableList<Domain> detectDomains() { if (autoDetect) { String hostName; try { hostName = dns.getHostName(dns.getLocalHost()); } catch (UnknownHostException ue) { hostName = "localhost"; } LOGGER.info("Local host is: {}", hostName); if (hostName != null && !hostName.equals("localhost")) { return ImmutableList.of(Domain.of(hostName)); } } return ImmutableList.of(); }
@Test public void getDomainsShouldContainDetectedDomainsAndIpsOfAddedDomains() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String added = "added.tld"; String detected = "detected.tld"; String detectedIp1 = "148.25.32.1"; String detectedIp2 = "148.25.32.2"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress1 = mock(InetAddress.class); InetAddress detectedAddress2 = mock(InetAddress.class); when(detectedAddress1.getHostAddress()).thenReturn(detectedIp1); when(detectedAddress2.getHostAddress()).thenReturn(detectedIp2); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress1)); when(dnsService.getAllByName(added)).thenReturn(ImmutableList.of(detectedAddress2)); domainList.addDomain(Domain.of(added)); assertThat(domainList.getDomains()) .extracting(Domain::name) .contains(detected, detectedIp1, added, detectedIp2); }