1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
mod helpers;
pub mod kinds;
pub mod mechanism;
pub mod qualifier;
#[doc(hidden)]
mod tests;
use crate::spf::mechanism::Mechanism;
use crate::spf::qualifier::Qualifier;
use ipnetwork::IpNetwork;
pub enum SpfErrorType {
InvalidSource,
SourceLength,
ExceedLookup,
}
#[derive(Debug)]
pub struct Spf {
source: String,
version: String,
from_src: bool,
include: Option<Vec<Mechanism<String>>>,
redirect: Option<Mechanism<String>>,
is_redirected: bool,
a: Option<Vec<Mechanism<String>>>,
mx: Option<Vec<Mechanism<String>>>,
ip4: Option<Vec<Mechanism<IpNetwork>>>,
ip6: Option<Vec<Mechanism<IpNetwork>>>,
ptr: Option<Mechanism<String>>,
exists: Option<Vec<Mechanism<String>>>,
all: Option<Mechanism<String>>,
}
impl Default for Spf {
fn default() -> Self {
Self {
source: String::new(),
version: String::new(),
from_src: false,
include: None,
redirect: None,
is_redirected: false,
a: None,
mx: None,
ip4: None,
ip6: None,
ptr: None,
exists: None,
all: None,
}
}
}
impl Spf {
pub fn new() -> Self {
Spf::default()
}
pub fn from_str(str: &str) -> Self {
Self {
source: str.clone().to_string(),
version: String::new(),
from_src: true,
include: None,
redirect: None,
is_redirected: false,
a: None,
mx: None,
ip4: None,
ip6: None,
ptr: None,
exists: None,
all: None,
}
}
pub fn parse(&mut self) -> Result<(), SpfErrorType> {
if !self.from_src {
return Err(SpfErrorType::InvalidSource);
}
let records = self.source.split_whitespace();
let mut vec_of_includes: Vec<Mechanism<String>> = Vec::new();
let mut vec_of_ip4: Vec<Mechanism<IpNetwork>> = Vec::new();
let mut vec_of_ip6: Vec<Mechanism<IpNetwork>> = Vec::new();
let mut vec_of_a: Vec<Mechanism<String>> = Vec::new();
let mut vec_of_mx: Vec<Mechanism<String>> = Vec::new();
let mut vec_of_exists: Vec<Mechanism<String>> = Vec::new();
for record in records {
if record.contains("v=spf1") || record.starts_with("spf2.0") {
self.version = record.to_string();
} else if record.contains("redirect=") {
let items = record.rsplit("=");
for item in items {
self.redirect =
Some(Mechanism::new_redirect(Qualifier::Pass, item.to_string()));
break;
}
self.is_redirected = true;
} else if record.contains("include:") {
let qualifier_and_modified_str = return_and_remove_qualifier(record, 'i');
for item in record.rsplit(":") {
vec_of_includes.push(Mechanism::new_include(
qualifier_and_modified_str.0,
item.to_string(),
));
break;
}
} else if record.contains("exists:") {
let qualifier_and_modified_str = return_and_remove_qualifier(record, 'e');
for item in record.rsplit(":") {
vec_of_exists.push(Mechanism::new_exists(
qualifier_and_modified_str.0,
item.to_string(),
));
break;
}
} else if record.contains("ip4:") {
let qualifier_and_modified_str = return_and_remove_qualifier(record, 'i');
if let Some(raw_ip4) = qualifier_and_modified_str.1.strip_prefix("ip4:") {
let network =
Mechanism::new_ip4(qualifier_and_modified_str.0, raw_ip4.parse().unwrap());
vec_of_ip4.push(network);
}
} else if record.contains("ip6:") {
let qualifier_and_modified_str = return_and_remove_qualifier(record, 'i');
if let Some(raw_ip6) = qualifier_and_modified_str.1.strip_prefix("ip6:") {
let network =
Mechanism::new_ip6(qualifier_and_modified_str.0, raw_ip6.parse().unwrap());
vec_of_ip6.push(network);
}
} else if record.ends_with("all") {
self.all = Some(Mechanism::new_all(
return_and_remove_qualifier(record, 'a').0,
))
} else if let Some(a_mechanism) =
helpers::capture_matches(record, kinds::MechanismKind::A)
{
vec_of_a.push(a_mechanism);
} else if let Some(mx_mechanism) =
helpers::capture_matches(record, kinds::MechanismKind::MX)
{
vec_of_mx.push(mx_mechanism);
} else if let Some(ptr_mechanism) =
helpers::capture_matches(record, kinds::MechanismKind::Ptr)
{
self.ptr = Some(ptr_mechanism);
}
}
if !vec_of_includes.is_empty() {
self.include = Some(vec_of_includes);
};
if !vec_of_ip4.is_empty() {
self.ip4 = Some(vec_of_ip4);
};
if !vec_of_ip6.is_empty() {
self.ip6 = Some(vec_of_ip6);
};
if !vec_of_a.is_empty() {
self.a = Some(vec_of_a);
}
if !vec_of_mx.is_empty() {
self.mx = Some(vec_of_mx);
}
if !vec_of_exists.is_empty() {
self.exists = Some(vec_of_exists);
}
Ok(())
}
#[doc(hidden)]
pub fn set_v1(&mut self) {
self.version = String::from("v=spf1");
}
#[doc(hidden)]
pub fn set_v2_pra(&mut self) {
self.version = String::from("spf2.0/pra");
}
#[doc(hidden)]
pub fn set_v2_mfrom(&mut self) {
self.version = String::from("spf2.0/mfrom");
}
#[doc(hidden)]
pub fn set_v2_pra_mfrom(&mut self) {
self.version = String::from("spf2.0/pra,mfrom");
}
#[doc(hidden)]
pub fn set_v2_mfrom_pra(&mut self) {
self.version = String::from("spf2.0/mfrom,pra");
}
pub fn is_v1(&self) -> bool {
self.version.contains("v=spf1")
}
pub fn is_v2(&self) -> bool {
self.version.starts_with("spf2.0")
}
pub fn version(&self) -> &String {
&self.version
}
#[doc(hidden)]
pub fn append_mechanism_of_a(&mut self, mechanism: Mechanism<String>) {
let mut vec: Vec<Mechanism<String>> = Vec::new();
vec.push(mechanism);
if self.a.is_none() {
self.a = Some(vec);
} else {
self.a.as_mut().unwrap().append(&mut vec);
}
}
pub fn try_validate(&self) -> Result<(), SpfErrorType> {
if self.from_src {
if self.includes().is_some() && self.includes().unwrap().len() > 10 {
return Err(SpfErrorType::ExceedLookup);
};
if self.source.len() > 255 {
return Err(SpfErrorType::SourceLength);
};
};
Ok(())
}
pub fn as_spf(&self) -> Option<String> {
if self.try_validate().is_err() {
None
} else {
let mut spf = String::new();
spf.push_str(self.version());
if self.is_redirected {
spf.push_str(" ");
spf.push_str(self.redirect().unwrap().string().as_str());
} else {
if self.a().is_some() {
for i in self.a().unwrap().iter() {
spf.push_str(" ");
spf.push_str(i.string().as_str());
}
};
if self.mx().is_some() {
for i in self.mx().unwrap().iter() {
spf.push_str(" ");
spf.push_str(i.string().as_str());
}
};
if self.includes().is_some() {
for i in self.includes().unwrap().iter() {
spf.push_str(" ");
spf.push_str(i.string().as_str());
}
}
if !self.is_redirected {
if self.all().is_some() {
spf.push_str(" ");
spf.push_str(self.all().unwrap().string().as_str());
}
}
}
return Some(spf);
}
}
pub fn source(&self) -> &String {
&self.source
}
pub fn is_redirect(&self) -> bool {
self.is_redirected
}
pub fn redirect(&self) -> Option<&Mechanism<String>> {
self.redirect.as_ref()
}
pub fn includes(&self) -> Option<&Vec<Mechanism<String>>> {
self.include.as_ref()
}
pub fn a(&self) -> Option<&Vec<Mechanism<String>>> {
self.a.as_ref()
}
pub fn mx(&self) -> Option<&Vec<Mechanism<String>>> {
self.mx.as_ref()
}
pub fn ip4(&self) -> Option<&Vec<Mechanism<IpNetwork>>> {
self.ip4.as_ref()
}
pub fn ip6(&self) -> Option<&Vec<Mechanism<IpNetwork>>> {
self.ip6.as_ref()
}
pub fn exists(&self) -> Option<&Vec<Mechanism<String>>> {
self.exists.as_ref()
}
pub fn ptr(&self) -> Option<&Mechanism<String>> {
self.ptr.as_ref()
}
pub fn all(&self) -> Option<&Mechanism<String>> {
self.all.as_ref()
}
}
#[doc(hidden)]
fn return_and_remove_qualifier(record: &str, c: char) -> (Qualifier, &str) {
if c != record.chars().nth(0).unwrap() {
(
helpers::char_to_qualifier(record.chars().nth(0).unwrap()),
remove_qualifier(record),
)
} else {
(Qualifier::Pass, record)
}
}
#[test]
fn return_and_remove_qualifier_no_qualifier() {
let source = "no prefix";
let (c, new_str) = return_and_remove_qualifier(source, 'n');
assert_eq!(Qualifier::Pass, c);
assert_eq!(source, new_str);
}
#[test]
fn return_and_remove_qualifier_pass() {
let source = "+prefix";
let (c, new_str) = return_and_remove_qualifier(source, 'n');
assert_eq!(Qualifier::Pass, c);
assert_eq!("prefix", new_str);
}
#[test]
fn return_and_remove_qualifier_fail() {
let source = "-prefix";
let (c, new_str) = return_and_remove_qualifier(source, 'n');
assert_eq!(Qualifier::Fail, c);
assert_eq!("prefix", new_str);
}
#[test]
fn return_and_remove_qualifier_softfail() {
let source = "~prefix";
let (c, new_str) = return_and_remove_qualifier(source, 'n');
assert_eq!(Qualifier::SoftFail, c);
assert_eq!("prefix", new_str);
}
#[test]
fn return_and_remove_qualifier_neutral() {
let source = "?prefix";
let (c, new_str) = return_and_remove_qualifier(source, 'n');
assert_eq!(Qualifier::Neutral, c);
assert_eq!("prefix", new_str);
}
#[doc(hidden)]
fn remove_qualifier(record: &str) -> &str {
let mut chars = record.chars();
chars.next();
chars.as_str()
}
#[test]
fn test_remove_qualifier() {
let test_str = "abc";
let result = remove_qualifier(test_str);
assert_eq!(result, "bc");
}