001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.jspf.policies.local;
021
022 import org.apache.james.jspf.core.Logger;
023 import org.apache.james.jspf.core.SPF1Record;
024 import org.apache.james.jspf.core.exceptions.NeutralException;
025 import org.apache.james.jspf.core.exceptions.NoneException;
026 import org.apache.james.jspf.core.exceptions.PermErrorException;
027 import org.apache.james.jspf.core.exceptions.TempErrorException;
028 import org.apache.james.jspf.policies.PolicyPostFilter;
029 import org.apache.james.jspf.terms.Directive;
030 import org.apache.james.jspf.terms.IncludeMechanism;
031
032 /**
033 * PolicyPostFilter which implements trusted forwared.
034 * See http://www.trusted-forwarder.org for more informations
035 *
036 */
037 public class TrustedForwarderPolicy implements PolicyPostFilter {
038
039 /**
040 * The hostname to include
041 */
042 public static final String TRUSTED_FORWARDER_HOST = "spf.trusted-forwarder.org";
043
044
045 private Logger log;
046
047 /**
048 * @param log the logger
049 */
050 public TrustedForwarderPolicy(Logger log) {
051 this.log = log;
052 }
053
054 /**
055 * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record)
056 */
057 public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, TempErrorException, NoneException, NeutralException {
058 if (spfRecord == null) return null;
059 String mechanism = ((Directive) spfRecord.getDirectives().get(spfRecord.getDirectives().size())).toString();
060 if (mechanism.equals("-all") || mechanism.equals("?all")) {
061 log.debug("Add TrustedForwarderPolicy = include:"+TRUSTED_FORWARDER_HOST);
062 try {
063 IncludeMechanism trusted = new IncludeMechanism() {
064 /**
065 * Set the host to use
066 *
067 * @param host the host to include
068 */
069 public synchronized IncludeMechanism setHost(String host) {
070 this.host = host;
071 return this;
072 }
073 }.setHost(TRUSTED_FORWARDER_HOST);
074 spfRecord.getDirectives().add(spfRecord.getDirectives().size()-1, new Directive(null, trusted, log.getChildLogger("trustedforwarder")));
075 } catch (PermErrorException e) {
076 // will never happen
077 }
078 }
079 return spfRecord;
080 }
081 }