1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.DNSRequest;
25 import org.apache.james.jspf.core.DNSResponse;
26 import org.apache.james.jspf.core.MacroExpand;
27 import org.apache.james.jspf.core.MacroExpandEnabled;
28 import org.apache.james.jspf.core.SPF1Constants;
29 import org.apache.james.jspf.core.SPFChecker;
30 import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
31 import org.apache.james.jspf.core.SPFSession;
32 import org.apache.james.jspf.core.SPFTermsRegexps;
33 import org.apache.james.jspf.core.exceptions.NeutralException;
34 import org.apache.james.jspf.core.exceptions.NoneException;
35 import org.apache.james.jspf.core.exceptions.PermErrorException;
36 import org.apache.james.jspf.core.exceptions.TempErrorException;
37 import org.apache.james.jspf.core.exceptions.TimeoutException;
38
39 import java.util.List;
40
41
42
43
44
45 public class ExpModifier extends GenericModifier implements MacroExpandEnabled, SPFCheckerDNSResponseListener {
46
47 private final class ExpandedExplanationChecker implements SPFChecker {
48
49
50
51
52 public DNSLookupContinuation checkSPF(SPFSession spfData)
53 throws PermErrorException, NoneException,
54 TempErrorException, NeutralException {
55 try {
56 String exp = (String) spfData.getAttribute(ATTRIBUTE_EXPAND_EXPLANATION);
57 String expandedExplanation = macroExpand.expand(exp, spfData, MacroExpand.EXPLANATION);
58 spfData.setExplanation(expandedExplanation);
59 } catch (PermErrorException e) {
60
61 }
62 return null;
63 }
64 }
65
66
67 private final class ExpandedChecker implements SPFChecker {
68
69
70
71
72 public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
73 NoneException, TempErrorException, NeutralException {
74 String host = macroExpand.expand(getHost(), spfData, MacroExpand.DOMAIN);
75
76 return new DNSLookupContinuation(new DNSRequest(host, DNSRequest.TXT), ExpModifier.this);
77 }
78 }
79
80
81 private static final String ATTRIBUTE_EXPAND_EXPLANATION = "ExpModifier.ExpandExplanation";
82
83
84
85
86
87
88
89
90
91
92
93 public static final String REGEX = "[eE][xX][pP]" + "\\="
94 + SPFTermsRegexps.DOMAIN_SPEC_REGEX;
95
96 private MacroExpand macroExpand;
97
98 private ExpandedChecker expandedChecker = new ExpandedChecker();
99
100 private ExpandedExplanationChecker expandedExplanationChecker = new ExpandedExplanationChecker();
101
102
103
104
105
106
107
108
109
110
111
112
113 protected DNSLookupContinuation checkSPFLogged(SPFSession spfData) throws PermErrorException, TempErrorException, NeutralException, NoneException {
114 String host = getHost();
115
116
117 if (host == null) {
118 return null;
119 }
120
121
122 if (spfData.ignoreExplanation() == true)
123 return null;
124
125
126
127 if (spfData.getCurrentResult()== null || !spfData.getCurrentResult().equals(SPF1Constants.FAIL))
128 return null;
129
130 spfData.pushChecker(expandedChecker);
131 return macroExpand.checkExpand(host, spfData, MacroExpand.DOMAIN);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public DNSLookupContinuation onDNSResponse(DNSResponse lookup, SPFSession spfData) throws PermErrorException, TempErrorException, NeutralException, NoneException {
154 try {
155 List records = lookup.getResponse();
156
157 if (records == null) {
158 return null;
159 }
160
161
162
163
164
165
166 if (records.size() > 1) {
167
168 log.debug("More then one TXT-Record found for explanation");
169
170
171 } else {
172
173 String exp = (String) records.get(0);
174 if (exp.length()>=2 && exp.charAt(0) == '"' && exp.charAt(exp.length() -1 ) == '"') {
175 exp = exp.substring(1, exp.length() - 1);
176 }
177
178 spfData.setAttribute(ATTRIBUTE_EXPAND_EXPLANATION, exp);
179
180 if ((exp != null) && (!exp.equals(""))) {
181
182 try {
183 spfData.pushChecker(expandedExplanationChecker);
184 return macroExpand.checkExpand(exp, spfData, MacroExpand.EXPLANATION);
185 } catch (PermErrorException e) {
186
187 }
188 }
189
190 }
191
192
193 } catch (TimeoutException e) {
194
195 }
196 return null;
197 }
198
199
200
201
202 public String toString() {
203 return "exp="+getHost();
204 }
205
206
207
208
209 public void enableMacroExpand(MacroExpand macroExpand) {
210 this.macroExpand = macroExpand;
211 }
212
213 }