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.jspf.policies;
21  
22  import org.apache.james.jspf.core.DNSLookupContinuation;
23  import org.apache.james.jspf.core.SPF1Record;
24  import org.apache.james.jspf.core.SPF1Utils;
25  import org.apache.james.jspf.core.SPFChecker;
26  import org.apache.james.jspf.core.SPFSession;
27  import org.apache.james.jspf.core.exceptions.NeutralException;
28  import org.apache.james.jspf.core.exceptions.NoneException;
29  import org.apache.james.jspf.core.exceptions.PermErrorException;
30  import org.apache.james.jspf.core.exceptions.TempErrorException;
31  import org.xbill.DNS.Name;
32  import org.xbill.DNS.TextParseException;
33  
34  /**
35   * Run the checks on the validity of the domain
36   * This is an override filter to be executed as the first 
37   * so it should be added as the last filter.
38   */
39  public final class InitialChecksPolicy implements SPFChecker {
40      
41      /**
42       * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
43       */
44      public DNSLookupContinuation checkSPF(SPFSession spfData)
45              throws PermErrorException, TempErrorException, NeutralException,
46              NoneException {
47          SPF1Record res = (SPF1Record) spfData.getAttribute(SPF1Utils.ATTRIBUTE_SPF1_RECORD);
48          if (res == null) {
49  
50              // Initial checks (spec 4.3)
51              String currentDomain = spfData.getCurrentDomain();
52              if (currentDomain != null) {
53                  String[] labels = currentDomain.split("\\.");
54                  for (int i = 0; i < labels.length; i++) {
55                      if (labels[i] != null && labels[i].length() > 63) {
56                          throw new NoneException("Domain "+currentDomain+" is malformed (label longer than 63 characters)");
57                      }
58                  }
59              }
60              
61              if (spfData.getSenderDomain().indexOf('.') < 0) {
62                  throw new NoneException("Sender domain "+spfData.getSenderDomain()+" is not an FQDN.");
63              }
64              
65              try {
66                  Name.fromString(spfData.getSenderDomain());
67              } catch (TextParseException e) {
68                  throw new NoneException("Invalid sender domain: "+e.getMessage());
69              }
70          }
71          return null;
72      }
73  }