├── .gitignore ├── BUILDING.txt ├── Jenkinsfile ├── LICENSE ├── NOTICE ├── README.txt ├── assemble ├── pom.xml └── src │ ├── assemble │ ├── bin.xml │ └── src.xml │ ├── licensing │ ├── licenseForApp.txt │ └── noticeForApp.txt │ └── reporting-site │ └── site.xml ├── docs ├── antora.yml └── modules │ └── ROOT │ └── pages │ └── index.adoc ├── pom.xml ├── resolver ├── pom.xml └── src │ ├── main │ ├── appended-resources │ │ └── supplemental-models.xml │ ├── java │ │ └── org │ │ │ └── apache │ │ │ └── james │ │ │ └── jspf │ │ │ ├── core │ │ │ ├── DNSLookupContinuation.java │ │ │ ├── DNSRequest.java │ │ │ ├── DNSResponse.java │ │ │ ├── DNSService.java │ │ │ ├── DNSServiceEnabled.java │ │ │ ├── IPAddr.java │ │ │ ├── Inet6Util.java │ │ │ ├── MacroData.java │ │ │ ├── MacroExpand.java │ │ │ ├── MacroExpandEnabled.java │ │ │ ├── SPF1Constants.java │ │ │ ├── SPF1Record.java │ │ │ ├── SPF1Utils.java │ │ │ ├── SPFCheckEnabled.java │ │ │ ├── SPFChecker.java │ │ │ ├── SPFCheckerDNSResponseListener.java │ │ │ ├── SPFCheckerExceptionCatcher.java │ │ │ ├── SPFRecordParser.java │ │ │ ├── SPFSession.java │ │ │ ├── SPFTermsRegexps.java │ │ │ └── exceptions │ │ │ │ ├── NeutralException.java │ │ │ │ ├── NoneException.java │ │ │ │ ├── PermErrorException.java │ │ │ │ ├── SPFErrorConstants.java │ │ │ │ ├── SPFResultException.java │ │ │ │ ├── TempErrorException.java │ │ │ │ └── TimeoutException.java │ │ │ ├── executor │ │ │ ├── AsynchronousSPFExecutor.java │ │ │ ├── DNSAsynchLookupService.java │ │ │ ├── FutureSPFResult.java │ │ │ ├── IResponse.java │ │ │ ├── IResponseImpl.java │ │ │ ├── IResponseQueue.java │ │ │ ├── SPFExecutor.java │ │ │ ├── SPFResult.java │ │ │ ├── StagedMultipleSPFExecutor.java │ │ │ └── SynchronousSPFExecutor.java │ │ │ ├── impl │ │ │ ├── DNSServiceAsynchSimulator.java │ │ │ ├── DNSServiceXBillImpl.java │ │ │ ├── DefaultSPF.java │ │ │ ├── DefaultTermDefinition.java │ │ │ ├── DefaultTermsFactory.java │ │ │ ├── SPF.java │ │ │ └── SPFQuery.java │ │ │ ├── parser │ │ │ ├── MatcherBasedConfiguration.java │ │ │ ├── RFC4408SPF1Parser.java │ │ │ ├── TermDefinition.java │ │ │ └── TermsFactory.java │ │ │ ├── policies │ │ │ ├── InitialChecksPolicy.java │ │ │ ├── NeutralIfNotMatchPolicy.java │ │ │ ├── NoSPFRecordFoundPolicy.java │ │ │ ├── ParseRecordPolicy.java │ │ │ ├── Policy.java │ │ │ ├── PolicyPostFilter.java │ │ │ ├── SPFRetriever.java │ │ │ ├── SPFStrictCheckerRetriever.java │ │ │ └── local │ │ │ │ ├── BestGuessPolicy.java │ │ │ │ ├── DefaultExplanationPolicy.java │ │ │ │ ├── FallbackPolicy.java │ │ │ │ ├── OverridePolicy.java │ │ │ │ └── TrustedForwarderPolicy.java │ │ │ ├── terms │ │ │ ├── AMechanism.java │ │ │ ├── AllMechanism.java │ │ │ ├── Configuration.java │ │ │ ├── ConfigurationEnabled.java │ │ │ ├── Directive.java │ │ │ ├── ExistsMechanism.java │ │ │ ├── ExpModifier.java │ │ │ ├── GenericMechanism.java │ │ │ ├── GenericModifier.java │ │ │ ├── IP4Mechanism.java │ │ │ ├── IP6Mechanism.java │ │ │ ├── IncludeMechanism.java │ │ │ ├── MXMechanism.java │ │ │ ├── Mechanism.java │ │ │ ├── Modifier.java │ │ │ ├── PTRMechanism.java │ │ │ ├── RedirectModifier.java │ │ │ └── UnknownModifier.java │ │ │ └── wiring │ │ │ ├── WiringService.java │ │ │ ├── WiringServiceException.java │ │ │ └── WiringServiceTable.java │ └── resources │ │ └── org │ │ └── apache │ │ └── james │ │ └── jspf │ │ └── parser │ │ └── jspf.default.terms │ ├── reporting-site │ └── site.xml │ └── test │ ├── java │ └── org │ │ └── apache │ │ └── james │ │ └── jspf │ │ ├── AbstractYamlTest.java │ │ ├── AsynchronousSPFExecutorIntegrationTest.java │ │ ├── DNSServiceXBillImplTest.java │ │ ├── DefaultSPFResolverTest.java │ │ ├── LoggingDNSService.java │ │ ├── MailZoneAsynchronousYamlTest.java │ │ ├── MailZoneYamlTest.java │ │ ├── RFC4408AsynchronousYamlTest.java │ │ ├── RFC4408YamlTest.java │ │ ├── SPF1ParserTest.java │ │ ├── SPFExecutorBaseTest.java │ │ ├── SPFYamlTest.java │ │ ├── SynchronousSPFExecutorIntegrationTest.java │ │ ├── YamlTest.java │ │ ├── core │ │ ├── IPAddrTest.java │ │ ├── MacroExpandTest.java │ │ └── SPFSessionTest.java │ │ └── helpers │ │ ├── FakeResolver.java │ │ └── FakeResolverTest.java │ └── resources │ ├── dnszones │ ├── FakeResolverTest.zone │ └── SPFExecutorIntegrationTest-1.zone │ ├── logback-test.xml │ └── org │ └── apache │ └── james │ └── jspf │ ├── libspf2.LICENSE │ ├── test_parser.txt │ └── test_rfc_examples.txt ├── src ├── reporting-site │ └── site.xml └── site │ ├── site.xml │ └── xdoc │ ├── FAQ.xml │ ├── code-standards.xml │ ├── contribute.xml │ ├── design.xml │ ├── documentation.xml │ ├── index.xml │ └── rfclist.xml └── tester ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── apache │ │ └── james │ │ └── jspf │ │ └── tester │ │ ├── DNSTestingServer.java │ │ ├── DNSTestingServerLauncher.java │ │ ├── ResponseGenerator.java │ │ ├── SPFYamlTestDescriptor.java │ │ ├── TCPListener.java │ │ └── UDPListener.java └── resources │ ├── META-INF │ └── NOTICE │ └── org │ └── apache │ └── james │ └── jspf │ └── tester │ ├── mailzone-tests.yml │ ├── pyspf-tests.yml │ ├── pysqf.LICENSE │ ├── rfc4408-tests-2009.10.LICENSE │ ├── rfc4408-tests-2009.10.yml │ ├── rfc4408-tests.LICENSE │ ├── rfc4408-tests.yml │ └── tests.yml └── reporting-site └── site.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | *~ 4 | target 5 | *.iml 6 | .gitignore 7 | .idea 8 | *.patch 9 | *.log 10 | .m2 11 | *.jar 12 | *.gz 13 | *.zip 14 | *.war 15 | -------------------------------------------------------------------------------- /BUILDING.txt: -------------------------------------------------------------------------------- 1 | ############################################################################# 2 | # BUILDING JSPF 3 | ############################################################################# 4 | 5 | You will need to install maven2 as well as acquire jSPF source from subversion 6 | or a source tarball. 7 | 8 | Steps: 9 | 10 | 1) Install maven2 (v2.2.1 as of the time of this writing) 11 | 12 | 2) Add maven2 to your path. For me, I do the following: 13 | $ tar zxvf maven-2.2.1.tar.gz 14 | $ mv maven-2.2.1 /usr/local 15 | $ ln -sf /usr/local/maven-2.2.1/bin/mvn /usr/local/bin/mvn 16 | 17 | 3) Change directory to jspf source dir 18 | $ cd jspf-x/ 19 | 20 | 4) Run the build 21 | $ mvn package 22 | 23 | That's it. Please contact server-dev@james.apache.org if you have any 24 | problems. -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Apache JSPF 2 | Copyright 2009-2011 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | 7 | 8 | -------------------------------------------------------------------------------- /assemble/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 4.0.0 22 | 23 | 24 | org.apache.james 25 | apache-jspf-project 26 | 1.0.6-SNAPSHOT 27 | ../pom.xml 28 | 29 | 30 | org.apache.james.jspf 31 | apache-jspf 32 | pom 33 | 34 | Apache JAMES jSPF Assembly 35 | 36 | Apache James jSPF is an SPF spec implementation written in Java. 37 | This module assembles the release. 38 | 39 | 40 | 41 | 42 | dnsjava 43 | dnsjava 44 | 45 | 46 | net.java.dev 47 | jvyaml 48 | 49 | 50 | commons-cli 51 | commons-cli 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.apache.maven.plugins 59 | maven-assembly-plugin 60 | 61 | src/assemble/ 62 | gnu 63 | 64 | 65 | 66 | make-assembly 67 | package 68 | 69 | single 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | site-reports 80 | 81 | 82 | 83 | 91 | 92 | org.apache.maven.plugins 93 | maven-site-plugin 94 | 95 | false 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /assemble/src/assemble/bin.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | bin 21 | 22 | 23 | zip 24 | tar.gz 25 | 26 | 27 | 28 | 29 | .. 30 | / 31 | 32 | RELEASE_NOTES* 33 | README* 34 | 35 | 36 | 37 | ../resolver/target 38 | / 39 | 40 | apache-jspf*.jar 41 | 42 | 43 | 44 | ../tester/target 45 | / 46 | 47 | apache-jspf*.jar 48 | 49 | 50 | 51 | ../target/site/apidocs 52 | /apidocs 53 | 54 | 55 | 56 | 57 | 58 | src/licensing/noticeForApp.txt 59 | / 60 | NOTICE 61 | 62 | 63 | src/licensing/licenseForApp.txt 64 | / 65 | LICENSE 66 | 67 | 68 | 69 | 70 | 71 | /lib/ 72 | false 73 | runtime 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /assemble/src/assemble/src.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | src 21 | 25 | 26 | zip 27 | tar.gz 28 | 29 | 30 | 31 | .. 32 | 33 | 34 | **/target/** 35 | .* 36 | .*/** 37 | **.bak 38 | 39 | true 40 | 41 | 42 | -------------------------------------------------------------------------------- /assemble/src/licensing/noticeForApp.txt: -------------------------------------------------------------------------------- 1 | Apache JSPF 2 | Copyright 2009 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | 7 | This product includes/uses software, JUnit (http://www.junit.org/), 8 | developed by Kent Beck, Erich Gamma, and David Saff 9 | License: Common Public License Version 1.0 (http://www.opensource.org/licenses/cpl.php) 10 | 11 | This product has been inspired from the spfjava library 12 | written by Roger Fullerton and Neil Murray. 13 | 14 | This product contains DNS-aware code inspired by DNSJava 15 | test code written and copyrighted by Brian Wellington, too. 16 | (see tester\src\main\java\org\apache\james\jspf\tester\ files) 17 | 18 | This product contains resources (SPF parser tests: test_parser.txt 19 | and test_rfc_examples.txt) derived from the libspf2 library 20 | (http://www.libspf2.org/) and distributed under the BSD license 21 | and copyright by Wayne Schlitt, Shevek. 22 | (see resolver\src\test\resources\org\apache\james\jspf\libspf2.LICENSE) 23 | 24 | This product contains resources (rfc4408-tests.yml) from the 25 | openspf group, distributed under the BSD license and copyright 26 | by Stuart D. Gathman and Julian Mehnle. 27 | (see tester\src\main\resources\org\apache\james\jspf\tester\rfc4408-tests.LICENSE) 28 | 29 | This product contains resources (tests.yml, pyspf-tests.yml) 30 | from the pyspf library (http://cheeseshop.python.org/pypi/pyspf) 31 | distributed under the Python Software Foundation License and 32 | copyright by Terence Way, Stuart Gathman (stuart@bmsi.com) and 33 | Scott Kitterman. 34 | (see tester\src\main\resources\org\apache\james\jspf\tester\pyspf.LICENSE) 35 | -------------------------------------------------------------------------------- /assemble/src/reporting-site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /docs/antora.yml: -------------------------------------------------------------------------------- 1 | name: jspf 2 | title: Apache James jSPF 3 | version: '1.0.2-SNAPSHOT' 4 | prerelease: true 5 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/index.adoc: -------------------------------------------------------------------------------- 1 | = Apache James jSPF documentation 2 | 3 | 4 | TODO: This is a placeholder. Migrate older documentation. 5 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/DNSLookupContinuation.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | 23 | 24 | /** 25 | * This object is used as the return value for spf resolving tasks. 26 | * Every time a DNS resolution is needed the task should simply return 27 | * this one including the DNSRequest and a listener to be invoked 28 | * when the answer will be available. 29 | */ 30 | public class DNSLookupContinuation { 31 | 32 | private DNSRequest request; 33 | private SPFCheckerDNSResponseListener listener; 34 | 35 | public DNSLookupContinuation(DNSRequest request, SPFCheckerDNSResponseListener listener) { 36 | this.request = request; 37 | this.listener = listener; 38 | } 39 | 40 | /** 41 | * Return the DNSRequest which was used 42 | * 43 | * @return request 44 | */ 45 | public DNSRequest getRequest() { 46 | return request; 47 | } 48 | 49 | /** 50 | * Return the SPFCheckerDNSResponseListener which should called for the DNSRequest 51 | * 52 | * @return listener 53 | */ 54 | public SPFCheckerDNSResponseListener getListener() { 55 | return listener; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/DNSRequest.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.NoneException; 23 | import org.xbill.DNS.Name; 24 | import org.xbill.DNS.TextParseException; 25 | 26 | /** 27 | * Represent a DNSRequest 28 | */ 29 | public final class DNSRequest { 30 | 31 | /** The record types for the lookups */ 32 | public static final int A = 1; 33 | public static final int AAAA = 2; 34 | public static final int MX = 3; 35 | public static final int PTR = 4; 36 | public static final int TXT = 5; 37 | public static final int SPF = 6; 38 | 39 | /** 40 | * The hostname to be resolved 41 | */ 42 | private final String hostname; 43 | 44 | /** 45 | * The record type to look for 46 | */ 47 | private final int recordType; 48 | 49 | public DNSRequest(final String hostname, final int recordType) throws NoneException { 50 | if (recordType == MX || recordType == A || recordType == AAAA) { 51 | try { 52 | Name.fromString(hostname); 53 | } catch (TextParseException e) { 54 | throw new NoneException(e.getMessage()); 55 | } 56 | } 57 | this.hostname = hostname; 58 | this.recordType = recordType; 59 | } 60 | 61 | /** 62 | * Return the hostname to process the request for 63 | * 64 | * @return the hostname 65 | */ 66 | public final String getHostname() { 67 | return hostname; 68 | } 69 | 70 | /** 71 | * Return the RecordType which is use for this request 72 | * 73 | * @return the RecordType 74 | */ 75 | public final int getRecordType() { 76 | return recordType; 77 | } 78 | 79 | /** 80 | * @see java.lang.Object#toString() 81 | */ 82 | public String toString() { 83 | return getHostname()+"#"+getRecordType(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/DNSResponse.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | 23 | import org.apache.james.jspf.core.exceptions.TimeoutException; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * Represent a DNSResponse 29 | * 30 | */ 31 | public class DNSResponse { 32 | 33 | private List response; 34 | 35 | private TimeoutException exception; 36 | 37 | public DNSResponse(TimeoutException exception) { 38 | this.exception = exception; 39 | this.response = null; 40 | } 41 | 42 | public DNSResponse(List response) { 43 | this.exception = null; 44 | this.response = response; 45 | } 46 | 47 | /** 48 | * Returns the DNS response 49 | * 50 | * @return the dns repsonse 51 | * @throws TimeoutException get thrown if an timeout was returned while tried to 52 | * process a dns request 53 | */ 54 | public List getResponse() throws TimeoutException { 55 | if (exception != null) { 56 | throw exception; 57 | } else { 58 | return response; 59 | } 60 | } 61 | 62 | /** 63 | * @see java.lang.Object#toString() 64 | */ 65 | public String toString() { 66 | if (exception != null) { 67 | return "EXCEPTION!"; 68 | } else if (response != null) { 69 | return response.toString(); 70 | } else { 71 | return "NULL?"; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/DNSService.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.TimeoutException; 23 | 24 | import java.util.List; 25 | import java.util.concurrent.CompletableFuture; 26 | import java.util.concurrent.CompletionStage; 27 | 28 | /** 29 | * Interface which should be used to access all necassary DNS-Records 30 | * 31 | */ 32 | public interface DNSService { 33 | 34 | /** 35 | * Retrieve dns records for the given host 36 | * 37 | * @param request the dns request 38 | * @return an array of Strings representing the records 39 | * @throws TimeoutException 40 | */ 41 | public List getRecords(DNSRequest request) throws TimeoutException; 42 | 43 | CompletionStage> getRecordsAsync(DNSRequest request); 44 | 45 | /** 46 | * Try to get all domain names for the running host 47 | * 48 | * @return names A List contains all domain names which could resolved 49 | */ 50 | public List getLocalDomainNames(); 51 | 52 | /** 53 | * Set the timeout for DNS-Requests 54 | * 55 | * @param timeOut The timeout in seconds 56 | */ 57 | public void setTimeOut(int timeOut); 58 | 59 | /** 60 | * @return the current record limit 61 | */ 62 | public int getRecordLimit(); 63 | 64 | /** 65 | * Sets a new limit for the number of records for MX and PTR lookups. 66 | * 67 | * @param recordLimit the new limit (0 => unlimited) 68 | */ 69 | public void setRecordLimit(int recordLimit); 70 | 71 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/DNSServiceEnabled.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | 23 | /** 24 | * Components that need to log can implement this interface so that 25 | * the container will provide the DNSService 26 | */ 27 | public interface DNSServiceEnabled { 28 | /** 29 | * Provide component with a DNSService. 30 | * 31 | * @param service 32 | * the dns service. Must not be null. 33 | */ 34 | void enableDNSService(DNSService service); 35 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/MacroData.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core; 22 | 23 | /** 24 | * This interface represent all the macros which can be used in SPF-Records. 25 | * Read more here : http://www.ietf.org/rfc/rfc4408.txt Section 8 26 | * 27 | */ 28 | public interface MacroData { 29 | 30 | /** 31 | * Get current-senderpart (l) 32 | * 33 | * @return current-senderpart 34 | */ 35 | public String getCurrentSenderPart(); 36 | 37 | /** 38 | * Get responsible-sender (s) 39 | * 40 | * @return responsible-sender 41 | */ 42 | public String getMailFrom(); 43 | 44 | /** 45 | * Get sender-domain (h) 46 | * 47 | * @return sender-domain 48 | */ 49 | public String getHostName(); 50 | 51 | /** 52 | * Get current-domain (d) 53 | * 54 | * @return current-domain 55 | */ 56 | public String getCurrentDomain(); 57 | 58 | /** 59 | * Get inAddress (v) 60 | * 61 | * @return inAddress 62 | */ 63 | public String getInAddress(); 64 | 65 | /** 66 | * Get clientDomain (p) 67 | * 68 | * @return clientDomain 69 | */ 70 | public String getClientDomain(); 71 | 72 | /** 73 | * Get senderDomain (o) 74 | * 75 | * @return senderDomain 76 | */ 77 | public String getSenderDomain(); 78 | 79 | /** 80 | * Get sending-host (i) 81 | * 82 | * @return sending-host 83 | */ 84 | public String getMacroIpAddress(); 85 | 86 | /** 87 | * Get timeStamp (t) 88 | * 89 | * @return timeStamp 90 | */ 91 | public long getTimeStamp(); 92 | 93 | /** 94 | * Get readableIP (c) 95 | * 96 | * @return readableIP 97 | */ 98 | public String getReadableIP(); 99 | 100 | /** 101 | * Get receivingDomain (r) 102 | * 103 | * @return receivingDomain 104 | */ 105 | public String getReceivingDomain(); 106 | 107 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/MacroExpandEnabled.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | 23 | /** 24 | * Components that need to log can implement this interface so that 25 | * the container will provide the DNSService 26 | */ 27 | public interface MacroExpandEnabled { 28 | /** 29 | * Provide component with a DNSService. 30 | * 31 | * @param macroExpand 32 | * the macroExpand service. Must not be null. 33 | */ 34 | void enableMacroExpand(MacroExpand macroExpand); 35 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPF1Constants.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core; 22 | 23 | /** 24 | * Interface which holds Constants for SPF 25 | */ 26 | public interface SPF1Constants { 27 | 28 | /** 29 | * Qualifier for PASS 30 | */ 31 | public static final String PASS = "+"; 32 | 33 | /** 34 | * Qualifier for NEUTRAL 35 | */ 36 | public static final String NEUTRAL = "?"; 37 | 38 | /** 39 | * Qualifier for FAIL 40 | */ 41 | public static final String FAIL = "-"; 42 | 43 | /** 44 | * Qualifier for SOFTFAIL 45 | */ 46 | public static final String SOFTFAIL = "~"; 47 | 48 | /** 49 | * The valid SPF_VERSION identifier 50 | */ 51 | public static final String SPF_VERSION1 = "v=spf1"; 52 | 53 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPF1Record.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core; 22 | 23 | import java.util.ArrayList; 24 | import java.util.Iterator; 25 | import java.util.List; 26 | 27 | import org.apache.james.jspf.terms.Directive; 28 | import org.apache.james.jspf.terms.Modifier; 29 | 30 | /** 31 | * The Class represent the SPF1 Record and provide methods to get all directives 32 | * and modifiers. 33 | * 34 | */ 35 | public class SPF1Record { 36 | 37 | private String record; 38 | private List directives = new ArrayList(); 39 | private List modifiers = new ArrayList(); 40 | 41 | public SPF1Record() { 42 | this.record = null; 43 | } 44 | 45 | public SPF1Record(String record) { 46 | this.record = record; 47 | } 48 | 49 | /** 50 | * Return the directives as Collection 51 | * 52 | * @return directives Collection of all qualifier+mechanism which should be 53 | * used 54 | */ 55 | public List getDirectives() { 56 | return directives; 57 | } 58 | 59 | /** 60 | * Return the modifiers as Collection 61 | * 62 | * @return modifiers Collection of all modifiers which should be used 63 | */ 64 | public List getModifiers() { 65 | return modifiers; 66 | } 67 | 68 | /** 69 | * @return the record in its string source format 70 | */ 71 | public String getRecord() { 72 | return record; 73 | } 74 | 75 | /** 76 | * Return a single iterator over Directives and Modifiers 77 | * 78 | * @return a chained iterator of the terms 79 | */ 80 | @SuppressWarnings("unchecked") 81 | public Iterator iterator() { 82 | return new Iterator() { 83 | boolean first = true; 84 | Iterator current = getDirectives().iterator(); 85 | 86 | /** 87 | * @see java.util.Iterator#hasNext() 88 | */ 89 | public boolean hasNext() { 90 | if (current.hasNext()) { 91 | return true; 92 | } else if (first) { 93 | current = getModifiers().iterator(); 94 | first = false; 95 | return current.hasNext(); 96 | } else return false; 97 | } 98 | 99 | /** 100 | * @see java.util.Iterator#next() 101 | */ 102 | public Object next() { 103 | return current.next(); 104 | } 105 | 106 | /** 107 | * @see java.util.Iterator#remove() 108 | */ 109 | public void remove() { 110 | throw new UnsupportedOperationException("Readonly iterator"); 111 | } 112 | 113 | }; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPF1Utils.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core; 22 | 23 | import org.apache.james.jspf.core.exceptions.SPFErrorConstants; 24 | 25 | 26 | /** 27 | * 28 | * Class that offer static methods to convert SPF Results and contains all 29 | * possible results as static Strings. 30 | * 31 | */ 32 | 33 | public class SPF1Utils { 34 | 35 | public static final String DEFAULT_EXPLANATION = "http://www.openspf.org/why.html?sender=%{S}&ip=%{I}"; 36 | public static final String BEST_GUESS_RECORD = "v=spf1 a/24 mx/24 ptr ?all"; 37 | public static final String ATTRIBUTE_SPF1_RECORD = "SPF.SPF1Record"; 38 | 39 | /** 40 | * Convert raw SPF results to SPF names 41 | * 42 | * @param result The result which should converted 43 | * @return coverted result 44 | */ 45 | public static String resultToName(String result) { 46 | 47 | if (result.equals(SPF1Constants.PASS)) { 48 | return SPFErrorConstants.PASS_CONV; 49 | } else if (result.equals(SPF1Constants.FAIL)) { 50 | return SPFErrorConstants.FAIL_CONV; 51 | } else if (result.equals(SPF1Constants.NEUTRAL)) { 52 | return SPFErrorConstants.NEUTRAL_CONV; 53 | } else if (result.equals(SPF1Constants.SOFTFAIL)) { 54 | return SPFErrorConstants.SOFTFAIL_CONV; 55 | } else { 56 | return SPFErrorConstants.NEUTRAL_CONV; 57 | } 58 | 59 | } 60 | 61 | /** 62 | * Check for valid FQDN 63 | * 64 | * @param host The hostname to check 65 | * @return false or true 66 | */ 67 | public static boolean checkFQDN(String host) { 68 | String regex = "(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z]+)$"; 69 | if (host.matches(regex)) { 70 | return true; 71 | } else { 72 | return false; 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFCheckEnabled.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | 23 | /** 24 | * Components that need to log can implement this interface so that 25 | * the container will provide the SPFChecker 26 | */ 27 | public interface SPFCheckEnabled { 28 | /** 29 | * Provide component with an SPF Checker. 30 | * 31 | * @param checker 32 | * the checker. Must not be null. 33 | */ 34 | void enableSPFChecking(SPFChecker checker); 35 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFChecker.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.NeutralException; 23 | import org.apache.james.jspf.core.exceptions.NoneException; 24 | import org.apache.james.jspf.core.exceptions.PermErrorException; 25 | import org.apache.james.jspf.core.exceptions.TempErrorException; 26 | 27 | /** 28 | * Interface for the SPFChecker service. 29 | */ 30 | public interface SPFChecker { 31 | 32 | /** 33 | * Run check for SPF with the given values. 34 | * 35 | * @param spfData 36 | * The SPF1Data which should be used to run the check 37 | * @throws PermErrorException 38 | * Get thrown if an error was detected 39 | * @throws NoneException 40 | * Get thrown if no Record was found 41 | * @throws TempErrorException 42 | * Get thrown if a DNS problem was detected 43 | * @throws NeutralException 44 | * Get thrown if the result should be neutral 45 | * @throws NoneException 46 | */ 47 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, 48 | TempErrorException, NeutralException, NoneException; 49 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFCheckerDNSResponseListener.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.NeutralException; 23 | import org.apache.james.jspf.core.exceptions.NoneException; 24 | import org.apache.james.jspf.core.exceptions.PermErrorException; 25 | import org.apache.james.jspf.core.exceptions.TempErrorException; 26 | 27 | /** 28 | * 29 | * Listeners which should get called for a SPFCheck when the DNSResponse is aviable should implement 30 | * this interface. 31 | */ 32 | public interface SPFCheckerDNSResponseListener { 33 | 34 | /** 35 | * Continue the check for SPF with the given values and the given DNSResponse 36 | * 37 | * @param response 38 | * The DNSResponse which should be used to run the check 39 | * @param session 40 | * The SPFSession which should be used to run the check 41 | * @throws PermErrorException 42 | * Get thrown if an error was detected 43 | * @throws NoneException 44 | * Get thrown if no Record was found 45 | * @throws TempErrorException 46 | * Get thrown if a DNS problem was detected 47 | * @throws NeutralException 48 | * Get thrown if the result should be neutral 49 | */ 50 | public DNSLookupContinuation onDNSResponse(DNSResponse response, SPFSession session) throws PermErrorException, NoneException, TempErrorException, NeutralException; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFCheckerExceptionCatcher.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.NeutralException; 23 | import org.apache.james.jspf.core.exceptions.NoneException; 24 | import org.apache.james.jspf.core.exceptions.PermErrorException; 25 | import org.apache.james.jspf.core.exceptions.TempErrorException; 26 | 27 | /** 28 | * 29 | * Implementations get called when a SPFChecker throws an Exception 30 | */ 31 | public interface SPFCheckerExceptionCatcher { 32 | 33 | /** 34 | * Take some action on the given Exception 35 | * 36 | * @param exception the exception 37 | * @param session the SPFSession 38 | * @throws PermErrorException 39 | * @throws NoneException 40 | * @throws TempErrorException 41 | * @throws NeutralException 42 | */ 43 | public void onException(Throwable exception, SPFSession session) throws PermErrorException, NoneException, TempErrorException, NeutralException; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFRecordParser.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.apache.james.jspf.core.exceptions.NeutralException; 23 | import org.apache.james.jspf.core.exceptions.NoneException; 24 | import org.apache.james.jspf.core.exceptions.PermErrorException; 25 | 26 | /** 27 | * 28 | * Implementations of this interface should handle parsing of SPFRecords 29 | */ 30 | public interface SPFRecordParser { 31 | 32 | /** 33 | * This Method parse the given spf record and checks for syntax 34 | * 35 | * parse can be called by multiple concurrent threads. 36 | * 37 | * @param spfRecord 38 | * The String which represent the spf record in dns 39 | * @return result The SPF1Record 40 | * @throws PermErrorException 41 | * Get thrown if an syntax error was detected 42 | * @throws NoneException 43 | * Get thrown if no spf record could be found 44 | * @throws NeutralException Get thrown if an empty spf record was found 45 | */ 46 | public SPF1Record parse(String spfRecord) throws PermErrorException, 47 | NoneException, NeutralException; 48 | 49 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/SPFTermsRegexps.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | /** 23 | * This constants are used by Terms to define their matching rules. 24 | */ 25 | public interface SPFTermsRegexps { 26 | 27 | 28 | final String ALPHA_PATTERN = "[a-zA-Z]"; 29 | 30 | final String MACRO_LETTER_PATTERN_EXP = "[rctlsodipvhRCTLSODIPVH]"; 31 | 32 | final String MACRO_LETTER_PATTERN = "[lsodipvhLSODIPVH]"; 33 | 34 | final String TRANSFORMERS_REGEX = "\\d*[r]?"; 35 | 36 | final String DELEMITER_REGEX = "[\\.\\-\\+,/_\\=]"; 37 | 38 | final String MACRO_LETTERS_REGEX = MACRO_LETTER_PATTERN_EXP + TRANSFORMERS_REGEX + DELEMITER_REGEX + "*"; 39 | 40 | final String MACRO_EXPAND_REGEX = "\\%(?:\\{" 41 | + MACRO_LETTERS_REGEX + "\\}|\\%|\\_|\\-)"; 42 | 43 | final String MACRO_LITERAL_REGEX = "[\\x21-\\x24\\x26-\\x7e]"; 44 | 45 | /** 46 | * This is used by the MacroExpander 47 | */ 48 | final String MACRO_STRING_REGEX_TOKEN = MACRO_EXPAND_REGEX 49 | + "|" + MACRO_LITERAL_REGEX + "{1}"; 50 | 51 | 52 | /** 53 | * ABNF: macro-string = *( macro-expand / macro-literal ) 54 | */ 55 | final String MACRO_STRING_REGEX = "(?:" + MACRO_STRING_REGEX_TOKEN +")*"; 56 | 57 | final String ALPHA_DIGIT_PATTERN = "[a-zA-Z0-9]"; 58 | 59 | /** 60 | * ABNF: toplabel = ( *alphanum ALPHA *alphanum ) / ( 1*alphanum "-" *( 61 | * alphanum / "-" ) alphanum ) ; LDH rule plus additional TLD restrictions ; 62 | * (see [RFC3696], Section 2) 63 | */ 64 | final String TOP_LABEL_REGEX = "(?:" 65 | + ALPHA_DIGIT_PATTERN + "*" + SPFTermsRegexps.ALPHA_PATTERN 66 | + "{1}" + ALPHA_DIGIT_PATTERN + "*|(?:" 67 | + ALPHA_DIGIT_PATTERN + "+" + "\\-" + "(?:" 68 | + ALPHA_DIGIT_PATTERN + "|\\-)*" 69 | + ALPHA_DIGIT_PATTERN + "))"; 70 | 71 | /** 72 | * ABNF: domain-end = ( "." toplabel [ "." ] ) / macro-expand 73 | */ 74 | final String DOMAIN_END_REGEX = "(?:\\." + TOP_LABEL_REGEX 75 | + "\\.?" + "|" + SPFTermsRegexps.MACRO_EXPAND_REGEX + ")"; 76 | 77 | /** 78 | * ABNF: domain-spec = macro-string domain-end 79 | */ 80 | final String DOMAIN_SPEC_REGEX = "(" 81 | + SPFTermsRegexps.MACRO_STRING_REGEX + DOMAIN_END_REGEX + ")"; 82 | 83 | /** 84 | * Spring MACRO_STRING from DOMAIN_END (domain end starts with .) 85 | */ 86 | final String DOMAIN_SPEC_REGEX_R = "(" 87 | + SPFTermsRegexps.MACRO_STRING_REGEX + ")(" + DOMAIN_END_REGEX + ")"; 88 | 89 | 90 | } 91 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/NeutralException.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core.exceptions; 22 | 23 | 24 | /** 25 | * This exception get thrown if the result should be neutral 26 | * 27 | */ 28 | public class NeutralException extends SPFResultException { 29 | 30 | private static final long serialVersionUID = -3042159503967082131L; 31 | 32 | public NeutralException(String strErrorMessage) { 33 | super(strErrorMessage); 34 | } 35 | 36 | /** 37 | * @see org.apache.james.jspf.core.exceptions.SPFResultException#getResult() 38 | */ 39 | public String getResult() { 40 | return SPFErrorConstants.NEUTRAL_CONV; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/NoneException.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core.exceptions; 22 | 23 | 24 | /** 25 | * This exception get thrown if the result should be none 26 | * 27 | */ 28 | public class NoneException extends SPFResultException { 29 | 30 | private static final long serialVersionUID = 3009434152870626458L; 31 | 32 | public NoneException(String strErrorMessage) { 33 | super(strErrorMessage); 34 | } 35 | 36 | /** 37 | * @see org.apache.james.jspf.core.exceptions.SPFResultException#getResult() 38 | */ 39 | public String getResult() { 40 | return SPFErrorConstants.NONE_CONV; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/PermErrorException.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core.exceptions; 22 | 23 | 24 | /** 25 | * This exception get thrown if the result should be PermError 26 | * 27 | */ 28 | public class PermErrorException extends SPFResultException { 29 | 30 | private static final long serialVersionUID = 2244218535480311682L; 31 | 32 | public PermErrorException(String strErrorMessage) { 33 | super(strErrorMessage); 34 | } 35 | 36 | /** 37 | * @see org.apache.james.jspf.core.exceptions.SPFResultException#getResult() 38 | */ 39 | public String getResult() { 40 | return SPFErrorConstants.PERM_ERROR_CONV; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/SPFErrorConstants.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core.exceptions; 22 | 23 | public interface SPFErrorConstants { 24 | 25 | public static final String PERM_ERROR_CONV = "permerror"; 26 | public static final String TEMP_ERROR_CONV = "temperror"; 27 | public static final String NONE_CONV = "none"; 28 | public static final String PASS_CONV = "pass"; 29 | public static final String NEUTRAL_CONV = "neutral"; 30 | public static final String FAIL_CONV = "fail"; 31 | public static final String SOFTFAIL_CONV = "softfail"; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/SPFResultException.java: -------------------------------------------------------------------------------- 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.core.exceptions; 21 | 22 | /** 23 | * Root exception for SPF methods 24 | */ 25 | public abstract class SPFResultException extends Exception { 26 | 27 | /** 28 | * 29 | */ 30 | private static final long serialVersionUID = -7541368045839292195L; 31 | 32 | /** 33 | * Exception 34 | * 35 | * @param strErrorMessage string 36 | */ 37 | public SPFResultException(String strErrorMessage) { 38 | super(strErrorMessage); 39 | } 40 | 41 | /** 42 | * The result for this exception 43 | * 44 | * @return the spf result for this exception 45 | */ 46 | public abstract String getResult(); 47 | } 48 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/TempErrorException.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.core.exceptions; 22 | 23 | 24 | /** 25 | * This exception get thrown if the result should be TempError 26 | * 27 | */ 28 | public class TempErrorException extends SPFResultException { 29 | 30 | private static final long serialVersionUID = -325898188953342837L; 31 | 32 | public TempErrorException(String strErrorMessage) { 33 | super(strErrorMessage); 34 | } 35 | 36 | /** 37 | * @see org.apache.james.jspf.core.exceptions.SPFResultException#getResult() 38 | */ 39 | public String getResult() { 40 | return SPFErrorConstants.TEMP_ERROR_CONV; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/core/exceptions/TimeoutException.java: -------------------------------------------------------------------------------- 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.core.exceptions; 21 | 22 | /** 23 | * The exception thrown on lookup timeout 24 | */ 25 | public class TimeoutException extends Exception { 26 | 27 | private static final long serialVersionUID = -780967296047865457L; 28 | 29 | /** 30 | * @param message cause 31 | */ 32 | public TimeoutException(String message) { 33 | super(message); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/DNSAsynchLookupService.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | import org.apache.james.jspf.core.DNSRequest; 23 | 24 | 25 | 26 | /** 27 | * Interface which should be used to access all necassary DNS-Records 28 | * 29 | */ 30 | public interface DNSAsynchLookupService { 31 | 32 | /** 33 | * Retrieve dns records for the given host asynchronously 34 | * 35 | * @param request the dns request 36 | * @param id the identification key for the response. 37 | * @param responsePool the queue where the response will be appended. 38 | */ 39 | public void getRecordsAsynch(DNSRequest request, int id, 40 | final IResponseQueue responsePool); 41 | 42 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/IResponse.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | import java.util.List; 23 | 24 | public interface IResponse { 25 | 26 | /** 27 | * Get id of the IResponse object 28 | * 29 | * @return id 30 | */ 31 | public Object getId(); 32 | 33 | /** 34 | * Get the value which is stored in the IResponse object 35 | * 36 | * @return object 37 | */ 38 | public List getValue(); 39 | 40 | /** 41 | * Return the exception which was stored for the IResponse object 42 | * 43 | * @return exception 44 | */ 45 | public Exception getException(); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/IResponseImpl.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Implementation of an IRespone Object 27 | * 28 | */ 29 | public class IResponseImpl implements IResponse { 30 | private Exception exception = null; 31 | private List value = null; 32 | private Object id = null; 33 | 34 | public IResponseImpl(Object id, Exception e) { 35 | this.exception = e; 36 | this.id = id; 37 | } 38 | 39 | public IResponseImpl(Object id, List result) { 40 | this.value = result; 41 | this.id = id; 42 | } 43 | 44 | /** 45 | * @see org.apache.james.jspf.executor.IResponse#getException() 46 | */ 47 | public Exception getException() { 48 | return exception; 49 | } 50 | 51 | /** 52 | * @see org.apache.james.jspf.executor.IResponse#getId() 53 | */ 54 | public Object getId() { 55 | return id; 56 | } 57 | 58 | /** 59 | * @see org.apache.james.jspf.executor.IResponse#getValue() 60 | */ 61 | public List getValue() { 62 | return value; 63 | } 64 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/IResponseQueue.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | import java.util.List; 23 | 24 | 25 | /** 26 | * Queue implementation which is used to manage IResponse 27 | * 28 | */ 29 | public interface IResponseQueue extends List { 30 | 31 | /** 32 | * Return the last IResponse in the queue. If the queue is empty it will 33 | * wait until a IResponse was added 34 | * 35 | * @return response 36 | */ 37 | public IResponse removeResponse(); 38 | 39 | /** 40 | * Add the given Response to the end of the queue. 41 | * 42 | * @param r 43 | */ 44 | public void insertResponse(IResponse r); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/SPFExecutor.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | import org.apache.james.jspf.core.SPFSession; 23 | 24 | public interface SPFExecutor { 25 | 26 | /** 27 | * Execute SPF queries 28 | * 29 | * @param session 30 | * @param result 31 | */ 32 | public void execute(SPFSession session, FutureSPFResult result); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java: -------------------------------------------------------------------------------- 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.executor; 21 | 22 | import org.apache.james.jspf.core.DNSLookupContinuation; 23 | import org.apache.james.jspf.core.DNSResponse; 24 | import org.apache.james.jspf.core.DNSService; 25 | import org.apache.james.jspf.core.SPFChecker; 26 | import org.apache.james.jspf.core.SPFCheckerExceptionCatcher; 27 | import org.apache.james.jspf.core.SPFSession; 28 | import org.apache.james.jspf.core.exceptions.SPFResultException; 29 | import org.apache.james.jspf.core.exceptions.TimeoutException; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | /** 34 | * Synchronous implementation of SPFExecuter. All queries will get executed synchronously 35 | */ 36 | public class SynchronousSPFExecutor implements SPFExecutor { 37 | private static final Logger LOGGER = LoggerFactory.getLogger(SynchronousSPFExecutor.class); 38 | 39 | private DNSService dnsProbe; 40 | 41 | public SynchronousSPFExecutor(DNSService service) { 42 | this.dnsProbe = service; 43 | } 44 | 45 | /** 46 | * @see org.apache.james.jspf.executor.SPFExecutor#execute(org.apache.james.jspf.core.SPFSession, org.apache.james.jspf.executor.FutureSPFResult) 47 | */ 48 | public void execute(SPFSession session, FutureSPFResult result) { 49 | SPFChecker checker; 50 | while ((checker = session.popChecker()) != null) { 51 | // only execute checkers we added (better recursivity) 52 | LOGGER.debug("Executing checker: {}", checker); 53 | try { 54 | DNSLookupContinuation cont = checker.checkSPF(session); 55 | // if the checker returns a continuation we return it 56 | while (cont != null) { 57 | DNSResponse response; 58 | try { 59 | response = new DNSResponse(dnsProbe.getRecords(cont 60 | .getRequest())); 61 | } catch (TimeoutException e) { 62 | response = new DNSResponse(e); 63 | } 64 | cont = cont.getListener().onDNSResponse(response, session); 65 | } 66 | } catch (Exception e) { 67 | while (e != null) { 68 | checker = session.popChecker(c -> c instanceof SPFCheckerExceptionCatcher); 69 | if (checker == null) { 70 | // Error case not handled by JSPF. Throw to avoid infinite loop. See JSPF-110. 71 | throw new RuntimeException("SPFCheckerExceptionCatcher implementation not found, session: " + session, e); 72 | } 73 | try { 74 | ((SPFCheckerExceptionCatcher) checker).onException(e, session); 75 | e = null; 76 | } catch (SPFResultException ex) { 77 | e = ex; 78 | } 79 | } 80 | } 81 | } 82 | result.setSPFResult(session); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/impl/DefaultSPF.java: -------------------------------------------------------------------------------- 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.impl; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.executor.AsynchronousSPFExecutor; 24 | import org.apache.james.jspf.executor.SPFExecutor; 25 | import org.apache.james.jspf.executor.SynchronousSPFExecutor; 26 | 27 | public class DefaultSPF extends SPF { 28 | /** 29 | * Creates an instance with the default dns resolver and a SynchronousSPFExecutor 30 | * 31 | * @see #createAsync() 32 | * @see #createSync() 33 | * @see SPF#SPF(DNSService, SPFExecutor) 34 | */ 35 | public DefaultSPF() { 36 | super(new DNSServiceXBillImpl()); 37 | } 38 | 39 | /** 40 | * Creates an instance of {@link SPF} with a default dns resolver and a {@link SynchronousSPFExecutor} 41 | * 42 | * @see #createAsync() 43 | * @see SPF#SPF(DNSService, SPFExecutor) 44 | * @return SPF 45 | */ 46 | public static SPF createSync() { 47 | return new SPF(new DNSServiceXBillImpl()); 48 | } 49 | 50 | /** 51 | * Creates an instance of {@link SPF} with a default dns resolver and a {@link AsynchronousSPFExecutor} 52 | * 53 | * @see #createSync() 54 | * @see SPF#SPF(DNSService, SPFExecutor) 55 | * @return SPF 56 | */ 57 | public static SPF createAsync() { 58 | DNSServiceXBillImpl dnsService = new DNSServiceXBillImpl(); 59 | return new SPF(dnsService, new AsynchronousSPFExecutor(dnsService)); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/impl/DefaultTermDefinition.java: -------------------------------------------------------------------------------- 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.impl; 21 | 22 | 23 | import org.apache.james.jspf.parser.TermDefinition; 24 | 25 | import java.util.regex.Pattern; 26 | 27 | /** 28 | * Default implementation for the TermDefinition. 29 | * This implementation try to retrieve the definition looking up a 30 | * static REGEX field in the term class. 31 | */ 32 | public class DefaultTermDefinition implements TermDefinition { 33 | 34 | private Pattern pattern; 35 | 36 | private Class termDef; 37 | 38 | private int matchSize = 0; 39 | 40 | public DefaultTermDefinition(Class tClass) throws IllegalArgumentException, 41 | SecurityException, IllegalAccessException, NoSuchFieldException { 42 | String pString = (String) tClass.getField("REGEX").get(null); 43 | pattern = Pattern.compile(pString); 44 | termDef = tClass; 45 | calcGroups(pString); 46 | } 47 | 48 | /** 49 | * This method should be done differently. We currently don't hanlde the 50 | * escaping at all. 51 | * 52 | * @param pString 53 | */ 54 | private void calcGroups(String pString) { 55 | int i = 0; 56 | int c = 0; 57 | while (true) { 58 | int p1 = pString.indexOf("(", i); 59 | int p2 = pString.indexOf("(?:", i); 60 | if (p1 < 0) 61 | break; 62 | if (p1 != p2) 63 | c++; 64 | i = p1 + 1; 65 | } 66 | matchSize = c; 67 | } 68 | 69 | /** 70 | * @see org.apache.james.jspf.parser.TermDefinition#getPattern() 71 | */ 72 | public Pattern getPattern() { 73 | return pattern; 74 | } 75 | 76 | /** 77 | * @see org.apache.james.jspf.parser.TermDefinition#getTermDef() 78 | */ 79 | public Class getTermDef() { 80 | return termDef; 81 | } 82 | 83 | /** 84 | * @see org.apache.james.jspf.parser.TermDefinition#getMatchSize() 85 | */ 86 | public int getMatchSize() { 87 | return matchSize; 88 | } 89 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/parser/MatcherBasedConfiguration.java: -------------------------------------------------------------------------------- 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.parser; 21 | 22 | import org.apache.james.jspf.terms.Configuration; 23 | 24 | import java.util.regex.Matcher; 25 | 26 | /** 27 | * 28 | * Provides a MatchResult view of a subset of another MatchResult 29 | */ 30 | public class MatcherBasedConfiguration implements Configuration { 31 | 32 | private Matcher wrapped; 33 | 34 | private int start; 35 | 36 | private int count; 37 | 38 | /** 39 | * @param w 40 | * Original MatchResult 41 | * @param start 42 | * the position where the subresult start 43 | * @param count 44 | * number of groups part of the subresult 45 | */ 46 | public MatcherBasedConfiguration(Matcher w, int start, int count) { 47 | this.wrapped = w; 48 | this.count = count; 49 | this.start = start; 50 | } 51 | 52 | /** 53 | * @see org.apache.james.jspf.terms.Configuration#group(int) 54 | */ 55 | public String group(int arg0) { 56 | return wrapped.group(arg0 + start); 57 | } 58 | 59 | /** 60 | * @see org.apache.james.jspf.terms.Configuration#groupCount() 61 | */ 62 | public int groupCount() { 63 | return count; 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/parser/TermDefinition.java: -------------------------------------------------------------------------------- 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.parser; 21 | 22 | import java.util.regex.Pattern; 23 | 24 | /** 25 | * A term definition contains everything needed to match and create 26 | * new Terms implementations. 27 | */ 28 | public interface TermDefinition { 29 | 30 | /** 31 | * Retrieve the pattern to be used to match a string against this record type. 32 | * 33 | * @return the pattern for this term 34 | */ 35 | public Pattern getPattern(); 36 | 37 | /** 38 | * The class implementing this Term type. 39 | * 40 | * @return the class object. 41 | */ 42 | public Class getTermDef(); 43 | 44 | /** 45 | * Return the number of groups to be expected from the pattern of this 46 | * Term. 47 | * 48 | * @return the number of groups 49 | */ 50 | public int getMatchSize(); 51 | 52 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/parser/TermsFactory.java: -------------------------------------------------------------------------------- 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.parser; 21 | 22 | import org.apache.james.jspf.core.exceptions.PermErrorException; 23 | import org.apache.james.jspf.terms.Configuration; 24 | 25 | import java.util.Collection; 26 | 27 | /** 28 | * A TermsFactory define the list of known TermDefinition and create new Terms 29 | * based on its own Definition and a Configuration 30 | */ 31 | public interface TermsFactory { 32 | 33 | /** 34 | * Create a new term starting from the TermDefinition created by this factory 35 | * 36 | * @param klass the TermDefinition.getTermDef (returned by this factory) 37 | * @param subres (the configuration) 38 | * @return the generated object 39 | * @throws PermErrorException if something goes wrong 40 | * @throws InstantiationException 41 | */ 42 | public Object createTerm(Class klass, Configuration subres) 43 | throws PermErrorException, InstantiationException; 44 | 45 | /** 46 | * Return the collection of known Mechanisms 47 | * 48 | * @return a Collection of TermDefinition 49 | */ 50 | public Collection getMechanismsCollection(); 51 | 52 | /** 53 | * Return the collection of known Modifiers 54 | * 55 | * @return a Collection of TermDefinition 56 | */ 57 | public Collection getModifiersCollection(); 58 | 59 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/InitialChecksPolicy.java: -------------------------------------------------------------------------------- 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 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java: -------------------------------------------------------------------------------- 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.SPF1Constants; 24 | import org.apache.james.jspf.core.SPFSession; 25 | import org.apache.james.jspf.core.SPF1Record; 26 | import org.apache.james.jspf.core.exceptions.NeutralException; 27 | import org.apache.james.jspf.core.exceptions.NoneException; 28 | import org.apache.james.jspf.core.exceptions.PermErrorException; 29 | import org.apache.james.jspf.core.exceptions.TempErrorException; 30 | import org.apache.james.jspf.terms.Modifier; 31 | 32 | /** 33 | * Sets the result to NEUTRAL if no directive is found 34 | */ 35 | public class NeutralIfNotMatchPolicy implements PolicyPostFilter { 36 | 37 | private final class NeutralIfNotMatchModifier implements Modifier { 38 | 39 | /** 40 | * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 41 | */ 42 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, TempErrorException, NeutralException { 43 | // If no match was found set the result to neutral 44 | if (spfData.getCurrentResult() == null) { 45 | spfData.setCurrentResult(SPF1Constants.NEUTRAL); 46 | } 47 | return null; 48 | } 49 | 50 | public String toString() { 51 | return "defaultresult"; 52 | } 53 | 54 | /** 55 | * (non-Javadoc) 56 | * @see org.apache.james.jspf.terms.Modifier#enforceSingleInstance() 57 | */ 58 | public boolean enforceSingleInstance() { 59 | return false; 60 | } 61 | } 62 | 63 | /** 64 | * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record) 65 | */ 66 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, TempErrorException, NoneException, NeutralException { 67 | if (spfRecord == null) return null; 68 | // Set the result to NEUTRAL if at least a directive is present and it didn't match 69 | // Maybe we should simply append a "?all" at the end, as modifier 70 | if (spfRecord.getDirectives().size() > 0) { 71 | spfRecord.getModifiers().add(new NeutralIfNotMatchModifier()); 72 | } 73 | return spfRecord; 74 | } 75 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/NoSPFRecordFoundPolicy.java: -------------------------------------------------------------------------------- 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.SPF1Record; 23 | import org.apache.james.jspf.core.exceptions.NeutralException; 24 | import org.apache.james.jspf.core.exceptions.NoneException; 25 | import org.apache.james.jspf.core.exceptions.PermErrorException; 26 | import org.apache.james.jspf.core.exceptions.TempErrorException; 27 | 28 | /** 29 | * Throws a NoneException if no record has been found 30 | */ 31 | public class NoSPFRecordFoundPolicy implements PolicyPostFilter { 32 | /** 33 | * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record) 34 | */ 35 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record res) throws PermErrorException, TempErrorException, NoneException, NeutralException { 36 | if (res == null) { 37 | throw new NoneException("No SPF record found for host: " + currentDomain); 38 | } else { 39 | return res; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/ParseRecordPolicy.java: -------------------------------------------------------------------------------- 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.SPF1Record; 23 | import org.apache.james.jspf.core.SPFRecordParser; 24 | import org.apache.james.jspf.core.exceptions.NeutralException; 25 | import org.apache.james.jspf.core.exceptions.NoneException; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | 28 | /** 29 | * Parse the record 30 | */ 31 | public class ParseRecordPolicy implements PolicyPostFilter { 32 | 33 | private SPFRecordParser parser; 34 | 35 | public ParseRecordPolicy(SPFRecordParser parser) { 36 | this.parser = parser; 37 | } 38 | 39 | /** 40 | * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record) 41 | */ 42 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, NoneException, NeutralException { 43 | if (spfRecord == null) return null; 44 | // parse the record 45 | if (spfRecord.getRecord() != null) { 46 | return parser.parse(spfRecord.getRecord()); 47 | } else { 48 | return spfRecord; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/Policy.java: -------------------------------------------------------------------------------- 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 | 23 | import org.apache.james.jspf.core.SPF1Record; 24 | import org.apache.james.jspf.core.exceptions.NeutralException; 25 | import org.apache.james.jspf.core.exceptions.NoneException; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | import org.apache.james.jspf.core.exceptions.TempErrorException; 28 | 29 | /** 30 | * Return an spf record from a given domain. 31 | */ 32 | public interface Policy { 33 | 34 | /** 35 | * Get a record for the given domain 36 | * 37 | * @param currentDomain the domain to retrieve the SPFRecord for 38 | * @return the SPFRecord found 39 | * @throws PermErrorException exception 40 | * @throws TempErrorException exception 41 | * @throws NoneException exception 42 | * @throws NeutralException exception 43 | */ 44 | public SPF1Record getSPFRecord(String currentDomain) 45 | throws PermErrorException, TempErrorException, NoneException, 46 | NeutralException; 47 | 48 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/PolicyPostFilter.java: -------------------------------------------------------------------------------- 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 | 23 | import org.apache.james.jspf.core.SPF1Record; 24 | import org.apache.james.jspf.core.exceptions.NeutralException; 25 | import org.apache.james.jspf.core.exceptions.NoneException; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | import org.apache.james.jspf.core.exceptions.TempErrorException; 28 | 29 | /** 30 | * Return an spf record from a given domain. 31 | */ 32 | public interface PolicyPostFilter { 33 | 34 | /** 35 | * Filter or replace a record for the given domain 36 | * 37 | * @param currentDomain the domain to retrieve the SPFRecord for 38 | * @param record the previous record 39 | * @return the SPFRecord found 40 | * @throws PermErrorException exception 41 | * @throws TempErrorException exception 42 | * @throws NoneException exception 43 | * @throws NeutralException exception 44 | */ 45 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record record) 46 | throws PermErrorException, TempErrorException, NoneException, 47 | NeutralException; 48 | 49 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/local/BestGuessPolicy.java: -------------------------------------------------------------------------------- 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.local; 21 | 22 | import org.apache.james.jspf.core.SPF1Record; 23 | import org.apache.james.jspf.core.SPF1Utils; 24 | import org.apache.james.jspf.core.exceptions.NeutralException; 25 | import org.apache.james.jspf.core.exceptions.NoneException; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | import org.apache.james.jspf.core.exceptions.TempErrorException; 28 | import org.apache.james.jspf.policies.PolicyPostFilter; 29 | 30 | /** 31 | * PolicyPostFilter which handles best guess. 32 | * See http://www.openspf.org/FAQ/Best_guess_record for more informations 33 | * 34 | * 35 | */ 36 | public class BestGuessPolicy implements PolicyPostFilter { 37 | 38 | /** 39 | * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record) 40 | */ 41 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record record) 42 | throws PermErrorException, TempErrorException, NoneException, 43 | NeutralException { 44 | if (record == null) { 45 | // We should use bestguess 46 | return new SPF1Record(SPF1Utils.BEST_GUESS_RECORD); 47 | } else { 48 | return record; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/local/OverridePolicy.java: -------------------------------------------------------------------------------- 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.local; 21 | 22 | import org.apache.james.jspf.core.SPF1Record; 23 | import org.apache.james.jspf.core.SPFRecordParser; 24 | import org.apache.james.jspf.core.exceptions.NeutralException; 25 | import org.apache.james.jspf.core.exceptions.NoneException; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | import org.apache.james.jspf.core.exceptions.TempErrorException; 28 | import org.apache.james.jspf.policies.Policy; 29 | 30 | public class OverridePolicy extends FallbackPolicy implements Policy { 31 | 32 | public OverridePolicy(SPFRecordParser parser) { 33 | super(parser); 34 | } 35 | 36 | /** 37 | * @see org.apache.james.jspf.policies.Policy#getSPFRecord(java.lang.String) 38 | */ 39 | public SPF1Record getSPFRecord(String currentDomain) 40 | throws PermErrorException, TempErrorException, NoneException, 41 | NeutralException { 42 | return getMySPFRecord(currentDomain); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java: -------------------------------------------------------------------------------- 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.local; 21 | 22 | import org.apache.james.jspf.core.SPF1Record; 23 | import org.apache.james.jspf.core.exceptions.NeutralException; 24 | import org.apache.james.jspf.core.exceptions.NoneException; 25 | import org.apache.james.jspf.core.exceptions.PermErrorException; 26 | import org.apache.james.jspf.core.exceptions.TempErrorException; 27 | import org.apache.james.jspf.policies.PolicyPostFilter; 28 | import org.apache.james.jspf.terms.Directive; 29 | import org.apache.james.jspf.terms.IncludeMechanism; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | /** 34 | * PolicyPostFilter which implements trusted forwared. 35 | * See http://www.trusted-forwarder.org for more informations 36 | * 37 | */ 38 | public class TrustedForwarderPolicy implements PolicyPostFilter { 39 | private static final Logger LOGGER = LoggerFactory.getLogger(TrustedForwarderPolicy.class); 40 | 41 | /** 42 | * The hostname to include 43 | */ 44 | public static final String TRUSTED_FORWARDER_HOST = "spf.trusted-forwarder.org"; 45 | 46 | /** 47 | * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record) 48 | */ 49 | public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, TempErrorException, NoneException, NeutralException { 50 | if (spfRecord == null) return null; 51 | String mechanism = ((Directive) spfRecord.getDirectives().get(spfRecord.getDirectives().size())).toString(); 52 | if (mechanism.equals("-all") || mechanism.equals("?all")) { 53 | LOGGER.debug("Add TrustedForwarderPolicy = include:{}", TRUSTED_FORWARDER_HOST); 54 | try { 55 | IncludeMechanism trusted = new IncludeMechanism() { 56 | /** 57 | * Set the host to use 58 | * 59 | * @param host the host to include 60 | */ 61 | public synchronized IncludeMechanism setHost(String host) { 62 | this.host = host; 63 | return this; 64 | } 65 | }.setHost(TRUSTED_FORWARDER_HOST); 66 | spfRecord.getDirectives().add(spfRecord.getDirectives().size()-1, new Directive(null, trusted)); 67 | } catch (PermErrorException e) { 68 | // will never happen 69 | } 70 | } 71 | return spfRecord; 72 | } 73 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/AllMechanism.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.DNSLookupContinuation; 24 | import org.apache.james.jspf.core.SPFSession; 25 | import org.apache.james.jspf.core.exceptions.PermErrorException; 26 | 27 | /** 28 | * This class represent the all mechanism 29 | * 30 | */ 31 | public class AllMechanism implements Mechanism, ConfigurationEnabled { 32 | 33 | public static final String REGEX = "[aA][lL][lL]"; 34 | 35 | /** 36 | * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 37 | */ 38 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException { 39 | return null; 40 | } 41 | 42 | /** 43 | * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(Configuration) 44 | */ 45 | public void config(Configuration params) throws PermErrorException { 46 | // no checks needed 47 | // the regex only passes with no parameters 48 | } 49 | 50 | /** 51 | * @see java.lang.Object#toString() 52 | */ 53 | public String toString() { 54 | return "all"; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/Configuration.java: -------------------------------------------------------------------------------- 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.terms; 21 | 22 | public interface Configuration { 23 | 24 | /** 25 | * @see java.util.regex.MatchResult#group(int) 26 | */ 27 | public String group(int arg0); 28 | 29 | /** 30 | * @see java.util.regex.MatchResult#groupCount() 31 | */ 32 | public int groupCount(); 33 | 34 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/ConfigurationEnabled.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.exceptions.PermErrorException; 24 | 25 | /** 26 | * 27 | */ 28 | public interface ConfigurationEnabled { 29 | 30 | /** 31 | * Configuration 32 | * 33 | * @param params the ConfigurationMatch which should used 34 | * @throws PermErrorException get thrown if a PermError should returned 35 | */ 36 | public void config(Configuration params) throws PermErrorException; 37 | 38 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.MacroExpand; 24 | import org.apache.james.jspf.core.MacroExpandEnabled; 25 | import org.apache.james.jspf.core.SPFSession; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | 28 | /** 29 | * This abstract class represent a gerneric mechanism 30 | * 31 | */ 32 | public abstract class GenericMechanism implements Mechanism, ConfigurationEnabled, MacroExpandEnabled { 33 | 34 | /** 35 | * ABNF: ip4-cidr-length = "/" 1*DIGIT 36 | */ 37 | protected static final String IP4_CIDR_LENGTH_REGEX = "/(\\d+)"; 38 | 39 | /** 40 | * ABNF: ip6-cidr-length = "/" 1*DIGIT 41 | */ 42 | protected static final String IP6_CIDR_LENGTH_REGEX = "/(\\d+)"; 43 | 44 | /** 45 | * ABNF: dual-cidr-length = [ ip4-cidr-length ] [ "/" ip6-cidr-length ] 46 | */ 47 | protected static final String DUAL_CIDR_LENGTH_REGEX = "(?:" 48 | + IP4_CIDR_LENGTH_REGEX + ")?" + "(?:/" + IP6_CIDR_LENGTH_REGEX 49 | + ")?"; 50 | 51 | private String domain; 52 | 53 | protected MacroExpand macroExpand; 54 | 55 | /** 56 | * Expand the hostname 57 | * 58 | * @param spfData The SPF1Data to use 59 | * @throws PermErrorException get Thrown if invalid macros are used 60 | */ 61 | protected String expandHost(SPFSession spfData) throws PermErrorException { 62 | String host = getDomain(); 63 | if (host == null) { 64 | host = spfData.getCurrentDomain(); 65 | } else { 66 | // throws a PermErrorException that we cat pass through 67 | host = macroExpand.expand(host, spfData, MacroExpand.DOMAIN); 68 | } 69 | return host; 70 | } 71 | 72 | /** 73 | * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(Configuration) 74 | */ 75 | public synchronized void config(Configuration params) throws PermErrorException { 76 | if (params.groupCount() >= 1 && params.group(1) != null) { 77 | domain = params.group(1); 78 | } else { 79 | domain = null; 80 | } 81 | } 82 | 83 | /** 84 | * @return Returns the domain. 85 | */ 86 | protected synchronized String getDomain() { 87 | return domain; 88 | } 89 | 90 | /** 91 | * @see org.apache.james.jspf.core.MacroExpandEnabled#enableMacroExpand(org.apache.james.jspf.core.MacroExpand) 92 | */ 93 | public void enableMacroExpand(MacroExpand macroExpand) { 94 | this.macroExpand = macroExpand; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/GenericModifier.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.DNSLookupContinuation; 24 | import org.apache.james.jspf.core.SPFSession; 25 | import org.apache.james.jspf.core.exceptions.NeutralException; 26 | import org.apache.james.jspf.core.exceptions.NoneException; 27 | import org.apache.james.jspf.core.exceptions.PermErrorException; 28 | import org.apache.james.jspf.core.exceptions.TempErrorException; 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | 32 | /** 33 | * This abstract class represent a gerneric modifier 34 | * 35 | */ 36 | public abstract class GenericModifier implements Modifier, ConfigurationEnabled { 37 | private static final Logger LOGGER = LoggerFactory.getLogger(GenericModifier.class); 38 | 39 | private String host; 40 | 41 | /** 42 | * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 43 | */ 44 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, 45 | TempErrorException, NeutralException, NoneException { 46 | LOGGER.debug("Processing modifier: {}", this); 47 | DNSLookupContinuation res = checkSPFLogged(spfData); 48 | LOGGER.debug("Processed modifier: {} resulted in {} ", 49 | this, res == null ? spfData.getCurrentResult() : "dns continuation..."); 50 | return res; 51 | } 52 | 53 | protected abstract DNSLookupContinuation checkSPFLogged(SPFSession spfData) throws PermErrorException, 54 | TempErrorException, NeutralException, NoneException; 55 | 56 | 57 | /** 58 | * @see org.apache.james.jspf.terms.Modifier#enforceSingleInstance() 59 | */ 60 | public boolean enforceSingleInstance() { 61 | return true; 62 | } 63 | 64 | /** 65 | * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(Configuration) 66 | */ 67 | public synchronized void config(Configuration params) throws PermErrorException { 68 | if (params.groupCount() > 0) { 69 | this.host = params.group(1); 70 | } 71 | } 72 | 73 | /** 74 | * @return Returns the host. 75 | */ 76 | protected synchronized String getHost() { 77 | return host; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.DNSLookupContinuation; 24 | import org.apache.james.jspf.core.IPAddr; 25 | import org.apache.james.jspf.core.Inet6Util; 26 | import org.apache.james.jspf.core.SPFSession; 27 | import org.apache.james.jspf.core.exceptions.PermErrorException; 28 | 29 | /** 30 | * This class represent the ip4 mechanism 31 | * 32 | */ 33 | public class IP4Mechanism extends GenericMechanism { 34 | 35 | /** 36 | * ABNF: IP4 = "ip4" ":" ip4-network [ ip4-cidr-length ] 37 | */ 38 | public static final String REGEX = "[iI][pP][4]" + "\\:" + "([0-9.]+)" 39 | + "(?:" + IP4_CIDR_LENGTH_REGEX + ")?"; 40 | 41 | private IPAddr ip = null; 42 | 43 | /** 44 | * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 45 | */ 46 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException { 47 | IPAddr originalIP; 48 | 49 | originalIP = IPAddr.getAddress(spfData.getIpAddress(), getIp() 50 | .getMaskLength()); 51 | 52 | spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.valueOf(getIp().getMaskedIPAddress().equals(originalIP.getMaskedIPAddress()))); 53 | 54 | return null; 55 | } 56 | 57 | /** 58 | * @see org.apache.james.jspf.terms.GenericMechanism#config(org.apache.james.jspf.terms.Configuration) 59 | */ 60 | public synchronized void config(Configuration params) throws PermErrorException { 61 | if (params.groupCount() == 0) { 62 | throw new PermErrorException("Missing ip"); 63 | } 64 | String ipString = params.group(1); 65 | if (!isValidAddress(ipString)) { 66 | throw new PermErrorException("Invalid Address: " + ipString); 67 | } 68 | int maskLength = getMaxCidr(); 69 | if (params.groupCount() >= 2 && params.group(2) != null) { 70 | String maskLengthString = params.group(2); 71 | maskLength = Integer.parseInt(maskLengthString); 72 | 73 | if (maskLength > getMaxCidr() || (maskLengthString.length() > 1 && maskLengthString.startsWith("0"))) { 74 | throw new PermErrorException("Invalid CIDR: " + maskLengthString); 75 | } 76 | } 77 | ip = IPAddr.getAddress(ipString, maskLength); 78 | } 79 | 80 | /** 81 | * @see org.apache.james.jspf.core.Inet6Util#isValidIPV4Address(String) 82 | */ 83 | protected boolean isValidAddress(String ipString) { 84 | return Inet6Util.isValidIPV4Address(ipString); 85 | } 86 | 87 | /** 88 | * Returns the max cidr for ip4 89 | * 90 | * @return maxCidr The max cidr 91 | */ 92 | protected int getMaxCidr() { 93 | return 32; 94 | } 95 | 96 | /** 97 | * @return Returns the ip. 98 | */ 99 | protected synchronized IPAddr getIp() { 100 | return ip; 101 | } 102 | 103 | /** 104 | * @see java.lang.Object#toString() 105 | */ 106 | public String toString() { 107 | if (getIp().getMaskLength() == getMaxCidr()) { 108 | return "ip4:"+getIp().getIPAddress(); 109 | } else { 110 | return "ip4:"+getIp().getIPAddress()+"/"+getIp().getMaskLength(); 111 | } 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/IP6Mechanism.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.Inet6Util; 24 | 25 | /** 26 | * This class represent the ip6 mechanism 27 | * 28 | */ 29 | public class IP6Mechanism extends IP4Mechanism { 30 | 31 | /** 32 | * ABNF: IP6 = "ip6" ":" ip6-network [ ip6-cidr-length ] 33 | */ 34 | public static final String REGEX = "[iI][pP][6]" 35 | + "\\:([0-9A-Fa-f\\:\\.]+)" + "(?:" + IP6_CIDR_LENGTH_REGEX + ")?"; 36 | 37 | /** 38 | * @see org.apache.james.jspf.terms.IP4Mechanism#isValidAddress(String) 39 | */ 40 | protected boolean isValidAddress(String ipString) { 41 | return Inet6Util.isValidIP6Address(ipString); 42 | } 43 | 44 | /** 45 | * @see org.apache.james.jspf.terms.IP4Mechanism#getMaxCidr() 46 | */ 47 | protected int getMaxCidr() { 48 | return 128; 49 | } 50 | 51 | 52 | /** 53 | * @see java.lang.Object#toString() 54 | */ 55 | public String toString() { 56 | if (getIp().getMaskLength() == getMaxCidr()) { 57 | return "ip6:"+getIp().getIPAddress(); 58 | } else { 59 | return "ip6:"+getIp().getIPAddress()+"/"+getIp().getMaskLength(); 60 | } 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/Mechanism.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.SPFChecker; 24 | 25 | 26 | /** 27 | * This Interface represent a mechanismn 28 | * 29 | */ 30 | public interface Mechanism extends SPFChecker { 31 | 32 | } 33 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/Modifier.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.SPFChecker; 24 | 25 | /** 26 | * This Interface represent a modifier 27 | * 28 | */ 29 | public interface Modifier extends SPFChecker { 30 | 31 | /** 32 | * Return true if the Modifier is only allowed once 33 | * 34 | * @return true if only one instance of this modifier is allowed 35 | */ 36 | public boolean enforceSingleInstance(); 37 | } 38 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/terms/UnknownModifier.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.terms; 22 | 23 | import org.apache.james.jspf.core.DNSLookupContinuation; 24 | import org.apache.james.jspf.core.SPFSession; 25 | import org.apache.james.jspf.core.SPFTermsRegexps; 26 | import org.apache.james.jspf.core.exceptions.PermErrorException; 27 | 28 | /** 29 | * This Class represent an Unknown Modifier 30 | * 31 | */ 32 | public class UnknownModifier implements Modifier, ConfigurationEnabled { 33 | 34 | /** 35 | * ABNF: name = ALPHA *( ALPHA / DIGIT / "-" / "_" / "." ) ABNF: 36 | * unknown-modifier = name "=" macro-string 37 | */ 38 | public static final String REGEX = "(" + SPFTermsRegexps.ALPHA_PATTERN + "{1}" 39 | + "[A-Za-z0-9\\-\\_\\.]*" + ")" + "\\=(" 40 | + SPFTermsRegexps.MACRO_STRING_REGEX + ")"; 41 | 42 | /** 43 | * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 44 | */ 45 | public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException { 46 | return null; 47 | } 48 | 49 | /** 50 | * @see org.apache.james.jspf.terms.Modifier#enforceSingleInstance() 51 | */ 52 | public boolean enforceSingleInstance() { 53 | return false; 54 | } 55 | 56 | /** 57 | * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(org.apache.james.jspf.terms.Configuration) 58 | */ 59 | public synchronized void config(Configuration params) throws PermErrorException { 60 | if (params.groupCount() >= 2 && params.group(1) != null) { 61 | String name = params.group(1).toLowerCase(); 62 | if ("exp".equals(name) || "redirect".equals(name)) { 63 | throw new PermErrorException("exp and redirect are not valid names for unknown modifier: this probably means an invalid syntax for exp or redirect fallback to the unkown modifier."); 64 | } 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/wiring/WiringService.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf.wiring; 22 | 23 | /** 24 | * This is a service used to wire a component with its dependencies. 25 | */ 26 | public interface WiringService { 27 | 28 | /** 29 | * Called to wire a component 30 | * 31 | * @param component the component to be wired 32 | * @throws WiringServiceException if an error occours 33 | */ 34 | void wire(Object component) throws WiringServiceException; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceException.java: -------------------------------------------------------------------------------- 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.wiring; 21 | 22 | /** 23 | * Exception raised when something goes wrong with wiring 24 | */ 25 | public class WiringServiceException extends Exception { 26 | 27 | private static final long serialVersionUID = -3964282365275100901L; 28 | 29 | public WiringServiceException(String string) { 30 | super(string); 31 | } 32 | 33 | public WiringServiceException(String string, Exception nested) { 34 | super(string, nested); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /resolver/src/main/java/org/apache/james/jspf/wiring/WiringServiceTable.java: -------------------------------------------------------------------------------- 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.wiring; 21 | 22 | 23 | import java.lang.reflect.InvocationTargetException; 24 | import java.lang.reflect.Method; 25 | import java.util.Hashtable; 26 | import java.util.Iterator; 27 | 28 | /** 29 | * This class associates "Enabling interfaces" to the service that provides the 30 | * dependency. 31 | */ 32 | public class WiringServiceTable extends Hashtable,Object> implements WiringService { 33 | 34 | private static final long serialVersionUID = -9151935136150279119L; 35 | 36 | /** 37 | * @see org.apache.james.jspf.wiring.WiringService#wire(java.lang.Object) 38 | */ 39 | public void wire(Object component) throws WiringServiceException { 40 | Iterator> i = keySet().iterator(); 41 | while (i.hasNext()) { 42 | Class enablingClass = i.next(); 43 | if (enablingClass.isInstance(component)) { 44 | Method[] m = enablingClass.getDeclaredMethods(); 45 | if (m!=null && m.length == 1 && m[0] != null) { 46 | try { 47 | m[0].invoke(component, new Object[] {get(enablingClass)}); 48 | } catch (IllegalArgumentException e) { 49 | throw new WiringServiceException("Illegal argument invoking enabled service: "+enablingClass.toString(), e); 50 | } catch (InvocationTargetException e) { 51 | throw new WiringServiceException("Unable to invoke enabled service: "+enablingClass.toString(), e); 52 | } catch (IllegalAccessException e) { 53 | throw new WiringServiceException("Unable to invoke enabled service: "+enablingClass.toString(), e); 54 | } 55 | } 56 | } 57 | } 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /resolver/src/main/resources/org/apache/james/jspf/parser/jspf.default.terms: -------------------------------------------------------------------------------- 1 | 19 | 20 | mechanisms=org.apache.james.jspf.terms.AllMechanism,org.apache.james.jspf.terms.AMechanism,org.apache.james.jspf.terms.ExistsMechanism,org.apache.james.jspf.terms.IncludeMechanism,org.apache.james.jspf.terms.IP4Mechanism,org.apache.james.jspf.terms.IP6Mechanism,org.apache.james.jspf.terms.MXMechanism,org.apache.james.jspf.terms.PTRMechanism 21 | modifiers=org.apache.james.jspf.terms.ExpModifier,org.apache.james.jspf.terms.RedirectModifier,org.apache.james.jspf.terms.UnknownModifier 22 | -------------------------------------------------------------------------------- /resolver/src/reporting-site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/AsynchronousSPFExecutorIntegrationTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.executor.AsynchronousSPFExecutor; 24 | import org.apache.james.jspf.impl.DefaultSPF; 25 | import org.apache.james.jspf.impl.SPF; 26 | import org.junit.BeforeClass; 27 | import org.xbill.DNS.Lookup; 28 | import org.xbill.DNS.SimpleResolver; 29 | 30 | import java.net.UnknownHostException; 31 | 32 | /** 33 | * Class to run the tests using {@link AsynchronousSPFExecutor} 34 | */ 35 | public class AsynchronousSPFExecutorIntegrationTest extends SPFExecutorBaseTest { 36 | @BeforeClass 37 | public static void setup() { 38 | // set default resolver before the tests to avoid errors caused by previous tests 39 | try { 40 | Lookup.setDefaultResolver(new SimpleResolver()); 41 | } catch (UnknownHostException e) { 42 | throw new RuntimeException(e); 43 | } 44 | } 45 | 46 | @Override 47 | protected SPF createSPF() { 48 | return DefaultSPF.createAsync(); 49 | } 50 | 51 | @Override 52 | protected SPF createCustomSPF(DNSService dnsService) { 53 | return new SPF(dnsService, new AsynchronousSPFExecutor(dnsService)); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java: -------------------------------------------------------------------------------- 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 | 21 | package org.apache.james.jspf; 22 | 23 | import org.apache.james.jspf.impl.DNSServiceXBillImpl; 24 | import org.junit.Test; 25 | import org.xbill.DNS.DClass; 26 | import org.xbill.DNS.Name; 27 | import org.xbill.DNS.Record; 28 | import org.xbill.DNS.SPFRecord; 29 | import org.xbill.DNS.TXTRecord; 30 | import org.xbill.DNS.Type; 31 | 32 | import java.net.UnknownHostException; 33 | import java.util.List; 34 | 35 | import static org.junit.Assert.assertEquals; 36 | import static org.junit.Assert.assertFalse; 37 | import static org.junit.Assert.assertNotNull; 38 | 39 | public class DNSServiceXBillImplTest { 40 | @Test 41 | public void testGetLocalDomainNames() throws UnknownHostException { 42 | assertFalse(new DNSServiceXBillImpl().getLocalDomainNames().isEmpty()); 43 | } 44 | 45 | @Test 46 | public void testMultipleStrings() throws Exception { 47 | Record[] rr = new Record[]{ 48 | TXTRecord.fromString(Name.fromString("test.local."), 49 | Type.TXT, DClass.IN, 0, "\"string \" \"concatenated\"", Name.fromString("local."))}; 50 | List records = DNSServiceXBillImpl.convertRecordsToList(rr); 51 | assertNotNull(records); 52 | assertEquals("string concatenated", records.get(0)); 53 | 54 | rr = new Record[]{ 55 | TXTRecord.fromString(Name.fromString("test.local."), 56 | Type.TXT, DClass.IN, 0, "string", Name.fromString("local."))}; 57 | records = DNSServiceXBillImpl.convertRecordsToList(rr); 58 | assertNotNull(records); 59 | assertEquals("string", records.get(0)); 60 | 61 | rr = new Record[]{ 62 | TXTRecord.fromString(Name.fromString("test.local."), 63 | Type.TXT, DClass.IN, 0, "\"quoted string\"", Name.fromString("local."))}; 64 | records = DNSServiceXBillImpl.convertRecordsToList(rr); 65 | assertNotNull(records); 66 | assertEquals("quoted string", records.get(0)); 67 | 68 | rr = new Record[]{ 69 | SPFRecord.fromString(Name.fromString("test.local."), 70 | Type.SPF, DClass.IN, 0, "\"quot\" \"ed st\" \"ring\"", Name.fromString("local."))}; 71 | records = DNSServiceXBillImpl.convertRecordsToList(rr); 72 | assertNotNull(records); 73 | assertEquals("quoted string", records.get(0)); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/DefaultSPFResolverTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.impl.DefaultSPF; 24 | import org.apache.james.jspf.impl.SPF; 25 | import org.junit.BeforeClass; 26 | import org.xbill.DNS.Lookup; 27 | import org.xbill.DNS.SimpleResolver; 28 | 29 | import java.net.UnknownHostException; 30 | 31 | /** 32 | * Class to run the tests using the default executor returned by {@link SPF} and {@link DefaultSPF} 33 | */ 34 | public class DefaultSPFResolverTest extends SPFExecutorBaseTest { 35 | @BeforeClass 36 | public static void setup() { 37 | // set default resolver before the tests to avoid errors caused by previous tests 38 | try { 39 | Lookup.setDefaultResolver(new SimpleResolver()); 40 | } catch (UnknownHostException e) { 41 | throw new RuntimeException(e); 42 | } 43 | } 44 | 45 | @Override 46 | protected SPF createSPF() { 47 | return new DefaultSPF(); 48 | } 49 | 50 | @Override 51 | protected SPF createCustomSPF(DNSService dnsService) { 52 | return new SPF(dnsService); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/MailZoneAsynchronousYamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 24 | 25 | import java.io.IOException; 26 | import java.util.Iterator; 27 | import java.util.List; 28 | 29 | import junit.framework.Test; 30 | import junit.framework.TestSuite; 31 | 32 | public class MailZoneAsynchronousYamlTest extends MailZoneYamlTest { 33 | 34 | private static final String YAMLFILE2 = "mailzone-tests.yml"; 35 | 36 | /** 37 | * @param name 38 | * @throws IOException 39 | */ 40 | public MailZoneAsynchronousYamlTest(String name) throws IOException { 41 | super(name); 42 | } 43 | 44 | protected MailZoneAsynchronousYamlTest(SPFYamlTestDescriptor def, String test) { 45 | super(def, test); 46 | } 47 | 48 | protected MailZoneAsynchronousYamlTest(SPFYamlTestDescriptor def) { 49 | super(def); 50 | } 51 | 52 | protected String getFilename() { 53 | return YAMLFILE2; 54 | } 55 | 56 | public static Test suite() throws IOException { 57 | return new MailZoneAsynchronousSuite(); 58 | } 59 | 60 | protected DNSService getDNSService() { 61 | DNSService dns = super.getDNSService(); 62 | // Remove record limits for this test 63 | dns.setRecordLimit(0); 64 | return dns; 65 | } 66 | 67 | 68 | protected int getDnsServiceMockStyle() { 69 | return FAKE_SERVER; 70 | } 71 | 72 | protected int getSpfExecutorType() { 73 | return STAGED_EXECUTOR_MULTITHREADED; 74 | } 75 | 76 | static class MailZoneAsynchronousSuite extends TestSuite { 77 | 78 | public MailZoneAsynchronousSuite() throws IOException { 79 | super(); 80 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 81 | Iterator i = tests.iterator(); 82 | while (i.hasNext()) { 83 | SPFYamlTestDescriptor o = i.next(); 84 | addTest(new MailZoneAsynchronousYamlTest(o)); 85 | } 86 | } 87 | 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/MailZoneYamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 24 | 25 | import java.io.IOException; 26 | import java.util.Iterator; 27 | import java.util.List; 28 | 29 | import junit.framework.Test; 30 | import junit.framework.TestSuite; 31 | 32 | public class MailZoneYamlTest extends AbstractYamlTest { 33 | 34 | private static final String YAMLFILE2 = "mailzone-tests.yml"; 35 | 36 | /** 37 | * @param name 38 | * @throws IOException 39 | */ 40 | public MailZoneYamlTest(String name) throws IOException { 41 | super(name); 42 | } 43 | 44 | protected MailZoneYamlTest(SPFYamlTestDescriptor def, String test) { 45 | super(def, test); 46 | } 47 | 48 | protected MailZoneYamlTest(SPFYamlTestDescriptor def) { 49 | super(def); 50 | } 51 | 52 | protected String getFilename() { 53 | return YAMLFILE2; 54 | } 55 | 56 | public static Test suite() throws IOException { 57 | return new MailZoneSuite(); 58 | } 59 | 60 | protected DNSService getDNSService() { 61 | DNSService dns = super.getDNSService(); 62 | // Remove record limits for this test 63 | dns.setRecordLimit(0); 64 | return dns; 65 | } 66 | 67 | static class MailZoneSuite extends TestSuite { 68 | 69 | public MailZoneSuite() throws IOException { 70 | super(); 71 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 72 | Iterator i = tests.iterator(); 73 | while (i.hasNext()) { 74 | SPFYamlTestDescriptor o = i.next(); 75 | Iterator ttt = o.getTests().keySet().iterator(); 76 | while (ttt.hasNext()) { 77 | addTest(new MailZoneYamlTest(o,ttt.next())); 78 | } 79 | } 80 | } 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/RFC4408AsynchronousYamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import java.io.IOException; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | 26 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 27 | 28 | import junit.framework.Test; 29 | import junit.framework.TestSuite; 30 | import junit.textui.TestRunner; 31 | 32 | public class RFC4408AsynchronousYamlTest extends RFC4408YamlTest { 33 | 34 | private static final String YAMLFILE2 = "rfc4408-tests.yml"; 35 | /** 36 | * @param name 37 | * @throws IOException 38 | */ 39 | public RFC4408AsynchronousYamlTest(String name) throws IOException { 40 | super(name); 41 | } 42 | 43 | protected RFC4408AsynchronousYamlTest(SPFYamlTestDescriptor def) { 44 | super(def); 45 | } 46 | 47 | protected RFC4408AsynchronousYamlTest(SPFYamlTestDescriptor def, String test) { 48 | super(def, test); 49 | } 50 | 51 | protected String getFilename() { 52 | return YAMLFILE2; 53 | } 54 | 55 | public static Test suite() throws IOException { 56 | return new RFC4408AsynchronousSuite(); 57 | } 58 | 59 | protected int getDnsServiceMockStyle() { 60 | return FAKE_SERVER; 61 | } 62 | 63 | protected int getSpfExecutorType() { 64 | return STAGED_EXECUTOR_MULTITHREADED; 65 | } 66 | 67 | static class RFC4408AsynchronousSuite extends TestSuite { 68 | 69 | public RFC4408AsynchronousSuite() throws IOException { 70 | super(); 71 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 72 | Iterator i = tests.iterator(); 73 | while (i.hasNext()) { 74 | SPFYamlTestDescriptor o = i.next(); 75 | addTest(new RFC4408AsynchronousYamlTest(o)); 76 | } 77 | } 78 | 79 | } 80 | 81 | /** 82 | * This method has been created for spf spec people to let them better read the 83 | * output of our tests against their yaml file 84 | * 85 | * @param args 86 | * @throws Throwable 87 | */ 88 | public static void main(String[] args) throws Throwable { 89 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 90 | Iterator i = tests.iterator(); 91 | while (i.hasNext()) { 92 | SPFYamlTestDescriptor o = (SPFYamlTestDescriptor) i.next(); 93 | Iterator ttt = o.getTests().keySet().iterator(); 94 | while (ttt.hasNext()) { 95 | RFC4408AsynchronousYamlTest t = new RFC4408AsynchronousYamlTest(o, ttt.next()); 96 | TestRunner.run(t); 97 | } 98 | } 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import java.io.IOException; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | 26 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 27 | 28 | import junit.framework.Test; 29 | import junit.framework.TestSuite; 30 | import junit.textui.TestRunner; 31 | 32 | public class RFC4408YamlTest extends AbstractYamlTest { 33 | 34 | private static final String YAMLFILE2 = "rfc4408-tests-2009.10.yml"; 35 | 36 | /** 37 | * @param name 38 | * @throws IOException 39 | */ 40 | public RFC4408YamlTest(String name) throws IOException { 41 | super(name); 42 | } 43 | 44 | protected RFC4408YamlTest(SPFYamlTestDescriptor def) { 45 | super(def); 46 | } 47 | 48 | protected RFC4408YamlTest(SPFYamlTestDescriptor def, String test) { 49 | super(def, test); 50 | } 51 | 52 | protected String getFilename() { 53 | return YAMLFILE2; 54 | } 55 | 56 | public static Test suite() throws IOException { 57 | return new RFC4408Suite(); 58 | } 59 | 60 | static class RFC4408Suite extends TestSuite { 61 | 62 | public RFC4408Suite() throws IOException { 63 | super(); 64 | try { 65 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 66 | Iterator i = tests.iterator(); 67 | while (i.hasNext()) { 68 | SPFYamlTestDescriptor o = i.next(); 69 | Iterator ttt = o.getTests().keySet().iterator(); 70 | while (ttt.hasNext()) { 71 | addTest(new RFC4408YamlTest(o, ttt.next())); 72 | } 73 | } 74 | } catch (RuntimeException e) { 75 | if ("Unable to load the file".equals(e.getMessage())) { 76 | System.err.println("WARNING: RFC4408 tests disabled."); 77 | System.err.println("The RFC4408 test-suite is not bundled with jspf due to licensing issues."); 78 | System.err.println("You can download the yaml testsuite at the following url:"); 79 | System.err.println(" http://www.openspf.org/source/project/test-suite/"); 80 | System.err.println("and place an rfc4408-tests.yml file in the /src/test/resources/org/apache/james/jspf folder."); 81 | } 82 | } 83 | } 84 | 85 | } 86 | 87 | /** 88 | * This method has been created for spf spec people to let them better read the 89 | * output of our tests against their yaml file 90 | * 91 | * @param args 92 | * @throws Throwable 93 | */ 94 | public static void main(String[] args) throws Throwable { 95 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 96 | Iterator i = tests.iterator(); 97 | while (i.hasNext()) { 98 | SPFYamlTestDescriptor o = (SPFYamlTestDescriptor) i.next(); 99 | Iterator ttt = o.getTests().keySet().iterator(); 100 | while (ttt.hasNext()) { 101 | RFC4408YamlTest t = new RFC4408YamlTest(o,(String) ttt.next()); 102 | TestRunner.run(t); 103 | } 104 | } 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/SPFYamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import java.io.IOException; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | 26 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 27 | 28 | import junit.framework.Test; 29 | import junit.framework.TestSuite; 30 | 31 | public class SPFYamlTest extends AbstractYamlTest { 32 | 33 | private static final String YAMLFILE = "pyspf-tests.yml"; 34 | 35 | 36 | public SPFYamlTest(String name) throws IOException { 37 | super(name); 38 | } 39 | 40 | protected SPFYamlTest(SPFYamlTestDescriptor def, String test) { 41 | super(def, test); 42 | } 43 | 44 | protected String getFilename() { 45 | return YAMLFILE; 46 | } 47 | 48 | public static Test suite() throws IOException { 49 | return new SPFSuite(); 50 | } 51 | 52 | static class SPFSuite extends TestSuite { 53 | 54 | public SPFSuite() throws IOException { 55 | super(); 56 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE); 57 | Iterator i = tests.iterator(); 58 | while (i.hasNext()) { 59 | SPFYamlTestDescriptor o = i.next(); 60 | Iterator ttt = o.getTests().keySet().iterator(); 61 | while (ttt.hasNext()) { 62 | addTest(new SPFYamlTest(o, ttt.next())); 63 | } 64 | } 65 | } 66 | 67 | } 68 | } -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/SynchronousSPFExecutorIntegrationTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import org.apache.james.jspf.core.DNSService; 23 | import org.apache.james.jspf.executor.SynchronousSPFExecutor; 24 | import org.apache.james.jspf.impl.DefaultSPF; 25 | import org.apache.james.jspf.impl.SPF; 26 | import org.junit.BeforeClass; 27 | import org.xbill.DNS.Lookup; 28 | import org.xbill.DNS.SimpleResolver; 29 | 30 | import java.net.UnknownHostException; 31 | 32 | /** 33 | * Class to run the tests using {@link SynchronousSPFExecutor} 34 | */ 35 | public class SynchronousSPFExecutorIntegrationTest extends SPFExecutorBaseTest { 36 | @BeforeClass 37 | public static void setup() { 38 | // set default resolver before the tests to avoid errors caused by previous tests 39 | try { 40 | Lookup.setDefaultResolver(new SimpleResolver()); 41 | } catch (UnknownHostException e) { 42 | throw new RuntimeException(e); 43 | } 44 | } 45 | 46 | @Override 47 | protected SPF createSPF() { 48 | return DefaultSPF.createSync(); 49 | } 50 | 51 | @Override 52 | protected SPF createCustomSPF(DNSService dnsService) { 53 | return new SPF(dnsService, new SynchronousSPFExecutor(dnsService)); 54 | } 55 | } -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/YamlTest.java: -------------------------------------------------------------------------------- 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; 21 | 22 | import java.io.IOException; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | 26 | import org.apache.james.jspf.tester.SPFYamlTestDescriptor; 27 | 28 | import junit.framework.Test; 29 | import junit.framework.TestSuite; 30 | 31 | public class YamlTest extends AbstractYamlTest { 32 | 33 | private static final String YAMLFILE2 = "tests.yml"; 34 | 35 | /** 36 | * @param name 37 | * @throws IOException 38 | */ 39 | public YamlTest(String name) throws IOException { 40 | super(name); 41 | } 42 | 43 | protected YamlTest(SPFYamlTestDescriptor def, String test) { 44 | super(def, test); 45 | } 46 | 47 | protected String getFilename() { 48 | return YAMLFILE2; 49 | } 50 | 51 | public static Test suite() throws IOException { 52 | return new BasicSuite(); 53 | } 54 | 55 | static class BasicSuite extends TestSuite { 56 | 57 | public BasicSuite() throws IOException { 58 | super(); 59 | List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2); 60 | Iterator i = tests.iterator(); 61 | while (i.hasNext()) { 62 | SPFYamlTestDescriptor o = i.next(); 63 | Iterator ttt = o.getTests().keySet().iterator(); 64 | while (ttt.hasNext()) { 65 | addTest(new YamlTest(o, ttt.next())); 66 | } 67 | } 68 | } 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /resolver/src/test/java/org/apache/james/jspf/core/SPFSessionTest.java: -------------------------------------------------------------------------------- 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.core; 21 | 22 | import org.junit.Test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | 26 | public class SPFSessionTest { 27 | @Test 28 | public void testGetMacroIpAddress() { 29 | SPFSession d = new SPFSession("mailfrom@fromdomain.com","helodomain.com","2001:DB8::CB01"); 30 | assertEquals("2.0.0.1.0.D.B.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.C.B.0.1",d.getMacroIpAddress()); 31 | } 32 | 33 | @Test 34 | public void shouldReturnInvalidIP6() { 35 | String addr = "2001:db8::644:f:f:f:x"; 36 | SPFSession spfSession = new SPFSession("", "", addr); 37 | assertEquals("invalid", spfSession.getInAddress()); 38 | } 39 | 40 | @Test 41 | public void shouldReturnInvalidIP4() { 42 | String addr = "192.168.0.256"; 43 | SPFSession spfSession = new SPFSession("", "", addr); 44 | assertEquals("invalid", spfSession.getInAddress()); 45 | } 46 | 47 | @Test 48 | public void shouldReturnValidIP6() { 49 | String addr = "2001:db8::644:f:f:f:1"; 50 | SPFSession spfSession = new SPFSession("", "", addr); 51 | assertEquals("ip6", spfSession.getInAddress()); 52 | } 53 | 54 | @Test 55 | public void shouldReturnIPv4Mapped() { 56 | SPFSession spfSession = new SPFSession("", "", "::ffff:192.168.1.1"); 57 | assertEquals("ip6", spfSession.getInAddress()); 58 | } 59 | 60 | @Test 61 | public void shouldReturnIPv4() { 62 | SPFSession spfSession = new SPFSession("", "", "192.168.1.1"); 63 | assertEquals("in-addr", spfSession.getInAddress()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /resolver/src/test/resources/dnszones/FakeResolverTest.zone: -------------------------------------------------------------------------------- 1 | $TTL 30 2 | @ 30 IN SOA ns1.fakeresolver.test. hostmaster.fakeresolver.test. 1 30 30 30 30 3 | @ 30 IN NS ns1 4 | ns1 IN A 192.0.2.2 5 | 6 | shouldResolveTXT IN TXT "v=spf1 ip4:192.0.2.127 -all" 7 | shouldResolveTXT IN TXT "lkjdslkhkjlahljkjkasdhfklsdf" 8 | shouldResolveA IN A 192.0.2.128 9 | shouldResolveMX IN MX 10 shouldResolveExternallyCNAME 10 | shouldResolveExternallyCNAME IN CNAME www.example.com. 11 | 12 | shouldResolveNS IN NS shouldResolveNS.ns1 13 | shouldResolveNS IN NS shouldResolveNS.ns2 14 | shouldResolveNS IN NS shouldResolveNS.ns3 15 | shouldResolveNS.ns1 IN A 192.0.2.11 16 | shouldResolveNS.ns2 IN A 192.0.2.12 17 | shouldResolveNS.ns3 IN A 192.0.2.13 -------------------------------------------------------------------------------- /resolver/src/test/resources/dnszones/SPFExecutorIntegrationTest-1.zone: -------------------------------------------------------------------------------- 1 | $TTL 30 2 | @ 30 IN SOA ns1.fakeresolver.apache. hostmaster.fakeresolver.apache. 1 30 30 30 30 3 | @ 30 IN NS ns1 4 | 5 | shouldReturnErrorIfMoreThanOneTxtSpf1Record IN TXT "v=spf1 ip4:192.0.2.162 -all" 6 | shouldReturnErrorIfMoreThanOneTxtSpf1Record IN TXT "v=spf1 ip4:192.0.2.127 ip4:1.1.1.1 -all" 7 | 8 | shouldReturnPassIfJustOneTxtSpf1Record IN TXT "v=spf1 include:_spf.google.com include:spf.protection.outlook.com ip4:2.3.4.5 ip4:192.0.2.127 -all" 9 | 10 | shouldReturnPermErrorIfIncludeDomainNotFound IN TXT "v=spf1 ip4:4.3.2.1 include:unknownDomain.fake -all" 11 | 12 | shouldReturnPassIfMultipleStrings IN TXT "v=spf1 mx a ip4:192.0.2.127 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1 ip4:4.3.2.1" " ip4:4.3.2.1 -all" 13 | -------------------------------------------------------------------------------- /resolver/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | test-run.log 5 | false 6 | 7 | false 8 | 10 | 11 | %d{HH:mm:ss.SSS} [%-5level] %logger{35} - %msg%n%rEx 12 | 13 | 14 | 15 | 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx 19 | 20 | false 21 | 22 | ERROR 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/reporting-site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | James jSPF 24 | images/james-logo.png 25 | http://james.apache.org/jspf/index.html 26 | james-logo.png 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | org.apache.james 24 | james-skin 25 | ${james-skin.version} 26 | 27 | 28 | 29 | James jSPF 30 | images/james-logo.png 31 | https://james.apache.org/jspf/index.html 32 | james-logo.png 33 | 34 | 35 | 36 | The Apache Software Foundation 37 | images/logos/asf_logo_small.png 38 | https://www.apache.org/index.html 39 | 40 | 41 | 42 | 43 | 44 | 45 | 48 | 51 | 54 | 57 | 60 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/site/xdoc/FAQ.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 | 28 |

29 |

34 |

35 |
36 | 37 |
Can I run jSPF from the commandline?
38 |

Sure you can. Just enter "java -jar jspf-xxx.jar" for getting the usage.

39 |
40 |
41 | 42 |
43 | -------------------------------------------------------------------------------- /src/site/xdoc/code-standards.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 |

28 | Submissions to the James project must follow the coding conventions 29 | outlined in this document. James developers 30 | are asked to follow coding conventions already present in the code. 31 | (For example, if the existing code has the bracket on 32 | the same line as the if statement, then all subsequent code 33 | should also follow that convention.) Anything not 34 | explicitly mentioned in this document should adhere to the 35 | official 36 | Sun 37 | Java Coding Conventions. 38 |

39 | 40 |

41 | Developers who commit code that does not follow 42 | the coding conventions outlined in this document will be 43 | responsible for fixing their own code. 44 |

45 | 46 |

47 | 1. Spaces between parentheses are optional. The preference is to exclude 48 | extra spaces. Both of these conventions are acceptable: 49 |

50 | 51 |

52 | 53 | 54 | if (foo) 55 | 56 | or 57 | 58 | if ( foo ) 59 | 60 |

61 | 62 |

63 | 2. Four spaces. NO tabs. Period. The James 64 | mailing list receives cvs commit messages that are almost impossible 65 | to read if tabs are used. 66 |

67 | 68 |

69 | In Emacs-speak, this translates to the following command: 70 | 71 | (setq-default tab-width 4 indent-tabs-mode nil) 72 |

73 | 74 |

75 | 3. Use Unix linefeeds for all .java source code files. Only platform-specific 76 | files (e.g. .bat files for Windows) should contain non-Unix linefeeds. 77 |

78 | 79 |

80 | 4. Javadoc must exist on all methods. Contributing 81 | a missing javadoc for any method, class, variable, etc., will be GREATLY 82 | appreciated as this will help to improve the James project. 83 |

84 | 85 |

86 | 5. The Jakarta Apache/James License MUST be placed 87 | at the top of every file. 88 |

89 |
90 | 91 |
-------------------------------------------------------------------------------- /src/site/xdoc/contribute.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 |

28 | TODO! 29 |

30 |
31 | 32 |
-------------------------------------------------------------------------------- /src/site/xdoc/design.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 |

28 | TODO! 29 |

30 |
31 | 32 |
-------------------------------------------------------------------------------- /src/site/xdoc/documentation.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 |

28 | TODO! 29 |

30 |
31 | 32 |
-------------------------------------------------------------------------------- /src/site/xdoc/rfclist.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | Overview 23 | James jSPF Project Team 24 | 25 | 26 |
27 |

28 | The actual SPF-RFC:
29 | RFC4408 - Sender Policy Framework (SPF) for Authorizing Use of Domains in E-Mail, Version 1 30 |

31 |
32 | 33 |
34 | 35 | -------------------------------------------------------------------------------- /tester/src/main/java/org/apache/james/jspf/tester/ResponseGenerator.java: -------------------------------------------------------------------------------- 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.tester; 21 | 22 | 23 | public interface ResponseGenerator { 24 | 25 | public byte[] generateReply(byte[] in, int length); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /tester/src/main/resources/META-INF/NOTICE: -------------------------------------------------------------------------------- 1 | Apache JSPF 2 | Copyright 2009 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | 7 | This product has been inspired from the spfjava library 8 | written by Roger Fullerton and Neil Murray. 9 | 10 | This product contains DNS-aware code inspired by DNSJava 11 | test code written and copyrighted by Brian Wellington, too. 12 | (see org\apache\james\jspf\tester\ files) 13 | 14 | This product contains resources (rfc4408-tests.yml) from the 15 | openspf group, distributed under the BSD license and copyright 16 | by Stuart D. Gathman and Julian Mehnle. 17 | (see org\apache\james\jspf\tester\rfc4408-tests.LICENSE) 18 | 19 | This product contains resources (tests.yml, pyspf-tests.yml) 20 | from the pyspf library (http://cheeseshop.python.org/pypi/pyspf) 21 | distributed under the Python Software Foundation License and 22 | copyright by Terence Way, Stuart Gathman (stuart@bmsi.com) and 23 | Scott Kitterman. 24 | (see org\apache\james\jspf\tester\pyspf.LICENSE) 25 | -------------------------------------------------------------------------------- /tester/src/main/resources/org/apache/james/jspf/tester/rfc4408-tests-2009.10.LICENSE: -------------------------------------------------------------------------------- 1 | The RFC 4408 test-suite (rfc4408-tests.yml) is 2 | (C) 2006-2007 Stuart D Gathman 3 | 2007 Julian Mehnle 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 1. Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. The names of the authors may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /tester/src/main/resources/org/apache/james/jspf/tester/rfc4408-tests.LICENSE: -------------------------------------------------------------------------------- 1 | The RFC 4408 test-suite (rfc4408-tests.yml) is 2 | (C) 2006-2007 Stuart D Gathman 3 | 2007 Julian Mehnle 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 1. Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. The names of the authors may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /tester/src/reporting-site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------