001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    package org.apache.james.protocols.smtp.core.fastfail;
020    
021    import java.util.Collection;
022    
023    import org.apache.james.protocols.smtp.DNSService;
024    import org.apache.james.protocols.smtp.SMTPRetCode;
025    import org.apache.james.protocols.smtp.SMTPSession;
026    import org.apache.james.protocols.smtp.TemporaryResolutionException;
027    import org.apache.james.protocols.smtp.dsn.DSNStatus;
028    import org.apache.james.protocols.smtp.hook.HookResult;
029    import org.apache.james.protocols.smtp.hook.HookReturnCode;
030    import org.apache.james.protocols.smtp.hook.MailHook;
031    import org.apache.mailet.MailAddress;
032    
033    /**
034     * Add MFDNSCheck feature to SMTPServer. This handler reject mail from domains which have not an an valid MX record.  
035     * 
036     */
037    public class ValidSenderDomainHandler implements MailHook {
038        
039        private DNSService dnsService = null;
040    
041        /**
042         * Sets the DNS service.
043         * @param dnsService the dnsService to set
044         */
045        public final void setDNSService(DNSService dnsService) {
046            this.dnsService = dnsService;
047        }
048        
049            
050    
051    
052        
053        protected boolean check(SMTPSession session, MailAddress senderAddress) {
054            // null sender so return
055            if (senderAddress == null) return false;
056    
057            Collection<String> records = null;
058                
059            // try to resolv the provided domain in the senderaddress. If it can not resolved do not accept it.
060            try {
061                records = dnsService.findMXRecords(senderAddress.getDomain());
062            } catch (TemporaryResolutionException e) {
063                // TODO: Should we reject temporary ?
064            }
065        
066            if (records == null || records.size() == 0) {
067                return true;
068            }
069    
070            return false;
071        }
072        
073        /**
074         * @see org.apache.james.protocols.smtp.hook.MailHook#doMail(org.apache.james.protocols.smtp.SMTPSession, org.apache.mailet.MailAddress)
075         */
076        public HookResult doMail(SMTPSession session, MailAddress sender) {
077            if (check(session,sender)) {
078                return new HookResult(HookReturnCode.DENY,SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " sender " + sender + " contains a domain with no valid MX records");
079            } else {
080                return new HookResult(HookReturnCode.DECLINED);
081            }
082        }
083    }