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;
020    
021    import org.apache.james.protocols.smtp.SMTPRetCode;
022    import org.apache.james.protocols.smtp.SMTPSession;
023    import org.apache.james.protocols.smtp.dsn.DSNStatus;
024    import org.apache.james.protocols.smtp.hook.HookResult;
025    import org.apache.james.protocols.smtp.hook.HookReturnCode;
026    import org.apache.james.protocols.smtp.hook.RcptHook;
027    import org.apache.mailet.MailAddress;
028    
029    /**
030     * Handler which check for authenticated users
031     */
032    public abstract class AbstractAuthRequiredToRelayRcptHook implements RcptHook {
033    
034        
035        /**
036         * @see org.apache.james.protocols.smtp.hook.RcptHook#doRcpt(org.apache.james.protocols.smtp.SMTPSession,
037         *      org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
038         */
039        public HookResult doRcpt(SMTPSession session, MailAddress sender,
040                MailAddress rcpt) {
041            if (!session.isRelayingAllowed()) {
042                String toDomain = rcpt.getDomain();
043                if (isLocalDomain(toDomain) == false) {
044                    if (session.isAuthSupported()) {
045                        return new HookResult(HookReturnCode.DENY,
046                                SMTPRetCode.AUTH_REQUIRED, DSNStatus.getStatus(
047                                        DSNStatus.PERMANENT,
048                                        DSNStatus.SECURITY_AUTH)
049                                        + " Authentication Required");
050                    } else {
051                        return new HookResult(
052                                HookReturnCode.DENY,
053                                // sendmail returns 554 (SMTPRetCode.TRANSACTION_FAILED).
054                                // it is not clear in RFC wether it is better to use 550 or 554.
055                                SMTPRetCode.MAILBOX_PERM_UNAVAILABLE,
056                                DSNStatus.getStatus(DSNStatus.PERMANENT,
057                                        DSNStatus.SECURITY_AUTH)
058                                        + " Requested action not taken: relaying denied");
059                    }
060                }
061    
062            }
063            return new HookResult(HookReturnCode.DECLINED);
064        }
065    
066        
067        /**
068         * Return true if the given domain is a local domain for this server
069         * 
070         * @param domain
071         * @return isLocal
072         */
073        protected abstract boolean isLocalDomain(String domain);
074        
075    }