View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.transport.mailets;
21  
22  import javax.mail.MessagingException;
23  import javax.mail.internet.MimeMessage;
24  
25  import org.apache.james.jspf.core.Logger;
26  import org.apache.james.jspf.executor.SPFResult;
27  import org.apache.james.jspf.impl.DefaultSPF;
28  import org.apache.mailet.base.GenericMailet;
29  import org.apache.mailet.Mail;
30  import org.apache.mailet.MailAddress;
31  
32  /**
33   * Check the ip, sender, helo against SPF.
34   * Add the following attributes to the mail object:
35   * 
36   * org.apache.james.transport.mailets.spf.explanation
37   * org.apache.james.transport.mailets.spf.result
38   *
39   * Sample configuration:
40   *
41   * <mailet match="All" class="SPF">
42   *   <addHeader>true</addHeader>
43   *   <debug>false</debug>
44   * </mailet>
45   */
46  public class SPF extends GenericMailet {
47      private boolean addHeader = false;
48      private org.apache.james.jspf.impl.SPF spf;
49      public final static String EXPLANATION_ATTRIBUTE = "org.apache.james.transport.mailets.spf.explanation";
50      public final static String RESULT_ATTRIBUTE = "org.apache.james.transport.mailets.spf.result";
51  
52      /*
53       * (non-Javadoc)
54       * @see org.apache.mailet.GenericMailet#init()
55       */
56      public void init() {
57          addHeader = new Boolean(getInitParameter("addHeader", "false"))
58                  .booleanValue();
59          SPFLoggerAdapter logger = new SPFLoggerAdapter(new Boolean(
60                  getInitParameter("debug", "false")).booleanValue());
61  
62          spf = new DefaultSPF(logger);
63      }
64  
65      /*
66       * (non-Javadoc)
67       * @see org.apache.mailet.GenericMailet#service(org.apache.mailet.Mail)
68       */
69      public void service(Mail mail) throws MessagingException {
70          String sender = null;
71          MailAddress senderAddr = mail.getSender();
72          String remoteAddr = mail.getRemoteAddr();
73          String helo = mail.getRemoteHost();
74  
75          if (remoteAddr.equals("127.0.0.1") == false) {
76              if (senderAddr != null) {
77                  sender = senderAddr.toString();
78              } else {
79                  sender = "";
80              }
81              SPFResult result = spf.checkSPF(remoteAddr, sender, helo);
82              mail.setAttribute(EXPLANATION_ATTRIBUTE, result.getExplanation());
83              mail.setAttribute(RESULT_ATTRIBUTE, result.getResult());
84  
85              log("ip:" + remoteAddr + " from:" + sender + " helo:" + helo
86                      + " = " + result.getResult());
87              if (addHeader) {
88                  try {
89                      MimeMessage msg = mail.getMessage();
90                      msg.addHeader(result.getHeaderName(), result
91                              .getHeaderText());
92                      msg.saveChanges();
93                  } catch (MessagingException e) {
94                      // Ignore not be able to add headers
95                  }
96              }
97          }
98      }
99  
100     private class SPFLoggerAdapter implements Logger {
101         private boolean debug = false;
102         private String name = "SPFLogger";
103 
104         public SPFLoggerAdapter(boolean debug) {
105             this.debug = debug;
106         }
107         
108         public SPFLoggerAdapter(String name, boolean debug) {
109             this.name = name;
110             this.debug = debug;
111         }
112 
113         public void debug(String arg0) {
114             if (debug) {
115                 log(arg0);
116             }
117         }
118 
119         public void debug(String arg0, Throwable arg1) {
120             if (debug) {
121                 log(arg0, arg1);
122             }
123         }
124 
125         public void error(String arg0) {
126             log(arg0);
127         }
128 
129         public void error(String arg0, Throwable arg1) {
130             log(arg0, arg1);
131         }
132 
133         public void fatalError(String arg0) {
134             log(arg0);
135         }
136 
137         public void fatalError(String arg0, Throwable arg1) {
138             log(arg0, arg1);
139         }
140 
141         public Logger getChildLogger(String childName) {
142             return new SPFLoggerAdapter(name + "." + childName, debug);
143         }
144 
145         public void info(String arg0) {
146             log(arg0);
147         }
148 
149         public void info(String arg0, Throwable arg1) {
150             log(arg0, arg1);
151         }
152 
153         public boolean isDebugEnabled() {
154             return debug;
155         }
156 
157         public boolean isErrorEnabled() {
158             return true;
159         }
160 
161         public boolean isFatalErrorEnabled() {
162             return true;
163         }
164 
165         public boolean isInfoEnabled() {
166             return true;
167         }
168 
169         public boolean isWarnEnabled() {
170             return true;
171         }
172 
173         public void warn(String arg0) {
174             log(arg0);
175         }
176 
177         public void warn(String arg0, Throwable arg1) {
178             log(arg0, arg1);
179         }
180 
181     }
182 
183 }