--- /james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java 2006-09-24 16:43:59.000000000 +0200 +++ /james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java 2006-09-24 17:41:33.000000000 +0200 @@ -49,6 +49,8 @@ private Logger log; + private String defaultPolicy = "v=spf1 a/24 mx/24 ptr ?all"; + /** * Uses default Log4JLogger and DNSJava based dns resolver */ @@ -79,6 +81,69 @@ } /** + * Set default SPF policy to use in non configured domains + * Example: v=spf1 a/24 mx/24 ptr ?all + * + * @param spfRecord + * The spf record to check if none exists + */ + public void setDefaultPolicy(String defaultPolicy) { + this.defaultPolicy = defaultPolicy; + } + + /** + * Set default SPF policy to use in non configured domains + * @return String The default policy + */ + public String getDefaultPolicy() { + return defaultPolicy; + } + + /** + * Run check for SPF with the given values usign a default + * policy if none exists. + * + * @param ipAddress + * The ipAddress the connection is comming from + * @param mailFrom + * The mailFrom which was provided + * @param hostName + * The hostname which was provided as HELO/EHLO + * @return result The SPFResult + */ + public SPFResult checkSPFguess(String ipAddress, String mailFrom, String hostName) { + return checkSPFguess(ipAddress, mailFrom, hostName, null); + } + + /** + * Run check for SPF with the given values usign a default + * policy if none exists. + * + * @param ipAddress + * The ipAddress the connection is comming from + * @param mailFrom + * The mailFrom which was provided + * @param hostName + * The hostname which was provided as HELO/EHLO + * @param spfRecord + * The spf record to check if none exists + * Example: v=spf1 a/24 mx/24 ptr ?all + * + * @return result The SPFResult + */ + public SPFResult checkSPFguess(String ipAddress, String mailFrom, String hostName, String spfRecord) { + SPFResult res = checkSPF(ipAddress, mailFrom, hostName); + // If result is none, retry with defaultPolicy + if (SPF1Utils.NONE_CONV.equals(res.getResult())) { + String spfEntry = (spfRecord == null ? defaultPolicy : spfRecord); + // logging + log.info("No SPF-Record found in DNS, retrying with defaultPolicy: " + spfEntry); + res = checkSPF(ipAddress, mailFrom, hostName, spfEntry); + } + return res; + } + + /** * Run check for SPF with the given values. * * @param ipAddress @@ -90,6 +155,23 @@ * @return result The SPFResult */ public SPFResult checkSPF(String ipAddress, String mailFrom, String hostName) { + return checkSPF(ipAddress, mailFrom, hostName, null); + } + + /** + * Run check for SPF with the given values. + * + * @param ipAddress + * The ipAddress the connection is comming from + * @param mailFrom + * The mailFrom which was provided + * @param hostName + * The hostname which was provided as HELO/EHLO + * @param spfRecord + * The spf record to check + * @return result The SPFResult + */ + public SPFResult checkSPF(String ipAddress, String mailFrom, String hostName, String spfRecord) { SPF1Data spfData = null; String result = null; String resultChar = null; @@ -98,7 +180,7 @@ try { // Setup the data spfData = new SPF1Data(mailFrom, hostName, ipAddress, dnsProbe); - SPFInternalResult res = checkSPF(spfData); + SPFInternalResult res = checkSPF(spfData, spfRecord); resultChar = res.getResultChar(); result = SPF1Utils.resultToName(resultChar); explanation = res.getExplanation(); @@ -134,6 +216,8 @@ * * @param spfData * The SPF1Data which should be used to run the check + * @param spfRecord + * The spf record to check * @return result * The SPFInternalResult * @throws PermErrorException @@ -147,15 +231,42 @@ */ public SPFInternalResult checkSPF(SPF1Data spfData) throws PermErrorException, NoneException, TempErrorException, NeutralException { + return checkSPF(spfData, null); + } + + /** + * Run check for SPF with the given values. + * + * @param spfData + * The SPF1Data which should be used to run the check + * @return result + * The SPFInternalResult + * @throws PermErrorException + * Get thrown if an error was detected + * @throws NoneException + * Get thrown if no Record was found + * @throws TempErrorException + * Get thrown if a DNS problem was detected + * @throws NeutralException + * Get thrown if the result should be neutral + */ + public SPFInternalResult checkSPF(SPF1Data spfData, String spfDnsEntry) throws PermErrorException, + NoneException, TempErrorException, NeutralException { String result = SPF1Constants.NEUTRAL; String explanation = null; - // Get the raw dns txt entry which contains a spf entry - String spfDnsEntry = dnsProbe.getSpfRecord(spfData.getCurrentDomain(), - SPF1Constants.SPF_VERSION); + if (spfDnsEntry == null) { + // Get the raw dns txt entry which contains a spf entry + spfDnsEntry = dnsProbe.getSpfRecord(spfData.getCurrentDomain(), + SPF1Constants.SPF_VERSION); - // logging - log.debug("Start parsing SPF-Record:" + spfDnsEntry); + // logging + log.debug("Start parsing DNS SPF-Record: " + spfDnsEntry); + } + else { + // logging + log.debug("Start parsing Forced SPF-Record: " + spfDnsEntry); + } SPF1Record spfRecord = parser.parse(spfDnsEntry);