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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
//! This module allows you to deconstruct an existing SPF DNS record into its
//! constituent parts.
//! It is not intended to validate the spf record.
mod errors;
#[cfg(test)]
mod tests;
mod validate;
use crate::helpers;
use crate::mechanism::Kind;
pub use crate::mechanism::{Mechanism, ParsedMechanism, Qualifier};
pub use crate::spf::errors::SpfError;
use ipnetwork::IpNetwork;
// Make this public in the future
use crate::spf::validate::{SpfRfcStandard, SpfValidationResult};
use std::{convert::TryFrom, str::FromStr};
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
/// The definition of the Spf struct which contains all information related a single
/// SPF record.
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Spf {
source: String,
version: String,
from_src: bool,
redirect: Option<Mechanism<String>>,
is_redirected: bool,
a: Option<Vec<Mechanism<String>>>,
mx: Option<Vec<Mechanism<String>>>,
include: 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>>,
was_parsed: bool,
was_validated: bool,
is_valid: bool,
#[allow(dead_code)]
warnings: Option<Vec<String>>,
}
impl std::fmt::Display for Spf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.build_spf_string())
}
}
/// Creates an `Spf Struct` by parsing a string representation of Spf.
///
/// # Examples:
///
///```rust
/// use decon_spf::Spf;
/// use decon_spf::SpfError;
/// // Successful
/// let input = "v=spf1 a mx -all";
/// let spf: Spf = input.parse().unwrap();
/// assert_eq!(spf.to_string(), input);
///
/// // Additional Space between `A` and `MX`
/// let bad_input = "v=spf1 a mx -all";
/// let err: SpfError = bad_input.parse::<Spf>().unwrap_err();
/// assert_eq!(err.to_string(), SpfError::WhiteSpaceSyntaxError.to_string());
/// // err.to_string() -> "Spf contains two or more consecutive whitespace characters.");
///
/// // Example with warn-dns feature enabled.
/// // Spf contains an invalid DNS host entry
/// let bad_spf2: Spf = "v=spf1 a mx:example.m/24 -all".parse().unwrap();
/// // Note: `warn-dns` will check `example.m` ignoring `/24`
/// #[cfg(feature = "warn-dns")]
/// assert_eq!(bad_spf2.has_warnings(), true);
/// #[cfg(feature = "warn-dns")]
/// assert_eq!(bad_spf2.warnings().unwrap()[0], "example.m/24");
///```
///
impl FromStr for Spf {
type Err = SpfError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let source = String::from(s);
if !source.starts_with("v=spf1") && !source.starts_with("spf2.0") {
return Err(SpfError::InvalidSource);
};
if source.len() > helpers::MAX_SPF_STRING_LENGTH {
return Err(SpfError::SourceLengthExceeded);
};
if helpers::spf_check_whitespace(source.as_str()) {
return Err(SpfError::WhiteSpaceSyntaxError);
};
// Basic Checks are ok.
let mut spf = Spf::new();
// Setup Vecs
let records = 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();
#[cfg(feature = "warn-dns")]
let mut vec_of_warnings: Vec<String> = Vec::new();
for record in records {
// Consider ensuring we do this once at least and then skip
if record.contains("v=spf1") || record.starts_with("spf2.0") {
spf.version = record.to_string();
} else if record.contains("redirect=") {
let mut items = record.rsplit('=');
if let Some(rrdata) = items.next() {
let m = Mechanism::generic_inclusive(
Kind::Redirect,
Qualifier::Pass,
Some(rrdata.to_string()),
);
#[cfg(feature = "warn-dns")]
{
helpers::check_for_dns_warning(&mut vec_of_warnings, &m.raw());
}
spf.redirect = Some(m);
spf.is_redirected = true;
}
} else if record.contains("include:") {
let qualifier_and_modified_str = helpers::return_and_remove_qualifier(record, 'i');
if let Some(rrdata) = record.rsplit(':').next() {
let m = Mechanism::generic_inclusive(
Kind::Include,
qualifier_and_modified_str.0,
Some(rrdata.to_string()),
);
#[cfg(feature = "warn-dns")]
{
helpers::check_for_dns_warning(&mut vec_of_warnings, &m.raw());
}
vec_of_includes.push(m);
}
} else if let Some(exists_mechanism) = helpers::capture_matches(record, Kind::Exists) {
if !exists_mechanism.raw().contains('/') {
#[cfg(feature = "warn-dns")]
{
helpers::check_for_dns_warning(
&mut vec_of_warnings,
&exists_mechanism.raw(),
);
}
vec_of_exists.push(exists_mechanism);
}
} else if record.contains("ip4:") {
// Match an ip4
let qualifier_and_modified_str = helpers::return_and_remove_qualifier(record, 'i');
if let Some(raw_ip4) = qualifier_and_modified_str.1.strip_prefix("ip4:") {
let valid_ip4 = raw_ip4.parse();
match valid_ip4 {
Ok(ip4) => {
let network = Mechanism::ip(qualifier_and_modified_str.0, ip4);
vec_of_ip4.push(network);
}
Err(ip4) => return Err(SpfError::InvalidIPAddr(ip4)),
}
}
} else if record.contains("ip6:") {
// Match an ip6
let qualifier_and_modified_str = helpers::return_and_remove_qualifier(record, 'i');
if let Some(raw_ip6) = qualifier_and_modified_str.1.strip_prefix("ip6:") {
let valid_ip6 = raw_ip6.parse();
match valid_ip6 {
Ok(ip6) => {
let network = Mechanism::ip(qualifier_and_modified_str.0, ip6);
vec_of_ip6.push(network);
}
Err(ip6) => return Err(SpfError::InvalidIPAddr(ip6)),
}
}
} else if record.ends_with("all") && (record.len() == 3 || record.len() == 4) {
spf.all = Some(Mechanism::all(
helpers::return_and_remove_qualifier(record, 'a').0,
));
// Handle A, MX and PTR types.
} else if let Some(a_mechanism) = helpers::capture_matches(record, Kind::A) {
#[cfg(feature = "warn-dns")]
{
if !a_mechanism.raw().starts_with('/')
&& !helpers::dns_is_valid(helpers::get_domain_before_slash(
&a_mechanism.raw(),
))
{
vec_of_warnings.push(a_mechanism.raw());
}
}
vec_of_a.push(a_mechanism);
} else if let Some(mx_mechanism) = helpers::capture_matches(record, Kind::MX) {
#[cfg(feature = "warn-dns")]
{
if !mx_mechanism.raw().starts_with('/')
&& !helpers::dns_is_valid(helpers::get_domain_before_slash(
&mx_mechanism.raw(),
))
{
vec_of_warnings.push(mx_mechanism.raw());
}
}
vec_of_mx.push(mx_mechanism);
} else if let Some(ptr_mechanism) = helpers::capture_matches(record, Kind::Ptr) {
#[cfg(feature = "warn-dns")]
{
helpers::check_for_dns_warning(&mut vec_of_warnings, &ptr_mechanism.raw());
}
spf.ptr = Some(ptr_mechanism);
}
}
// Move vec_of_* int the SPF struct
if !vec_of_includes.is_empty() {
spf.include = Some(vec_of_includes);
};
if !vec_of_ip4.is_empty() {
spf.ip4 = Some(vec_of_ip4);
};
if !vec_of_ip6.is_empty() {
spf.ip6 = Some(vec_of_ip6);
};
if !vec_of_a.is_empty() {
spf.a = Some(vec_of_a);
}
if !vec_of_mx.is_empty() {
spf.mx = Some(vec_of_mx);
}
if !vec_of_exists.is_empty() {
spf.exists = Some(vec_of_exists);
}
#[cfg(feature = "warn-dns")]
{
if !vec_of_warnings.is_empty() {
spf.warnings = Some(vec_of_warnings);
}
}
spf.was_parsed = true;
spf.is_valid = true;
spf.source = source;
Ok(spf)
}
}
impl TryFrom<&str> for Spf {
type Error = SpfError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Spf::from_str(s)
}
}
impl Spf {
/// Create a new empty Spf struct.
pub fn new() -> Self {
Spf::default()
}
/// Check that data stored in the Spf Struct is considered a valid Spf Record.
pub fn is_valid(&self) -> bool {
if self.was_parsed || self.was_validated {
return self.is_valid;
};
false
}
/// Check if there were any warnings when parsing the Spf String.
/// This can only be changed to `true` when `warn-dns` feature has been eabled. Other wise it
/// will always be `false`
#[cfg_attr(docsrs, doc(cfg(feature = "warn-dns")))]
#[cfg(feature = "warn-dns")]
pub fn has_warnings(&self) -> bool {
self.warnings.is_some()
}
/// Set version to `v=spf1`
pub fn set_v1(&mut self) {
self.version = String::from("v=spf1");
}
/// Set version to `spf2.0/pra`
pub fn set_v2_pra(&mut self) {
self.version = String::from("spf2.0/pra");
}
/// Set version to `spf2.0/mfrom`
pub fn set_v2_mfrom(&mut self) {
self.version = String::from("spf2.0/mfrom");
}
/// Set version to `spf2.0/pra,mfrom`
pub fn set_v2_pra_mfrom(&mut self) {
self.version = String::from("spf2.0/pra,mfrom");
}
/// Set version to `spf2.0/mfrom,pra`
pub fn set_v2_mfrom_pra(&mut self) {
self.version = String::from("spf2.0/mfrom,pra");
}
/// Check that version is v1
pub fn is_v1(&self) -> bool {
self.version.contains("v=spf1")
}
/// Check that version is v2
pub fn is_v2(&self) -> bool {
self.version.starts_with("spf2.0")
}
/// Return a reference to version
pub fn version(&self) -> &String {
&self.version
}
/// Append a Redirect Mechanism to the Spf Struct.
fn append_mechanism_of_redirect(&mut self, mechanism: Mechanism<String>) {
self.redirect = Some(mechanism);
self.is_redirected = true;
if self.all.is_some() {
self.all = None;
}
}
/// Clear the passed Kind which has been passed.
/// Sets the passed mechanism to `None`
///
/// # Note:
/// This method clears all assocated Mechanism for the [`Kind`](Kind) provided.
///
/// # Example:
/// ```
/// use decon_spf::mechanism::{Qualifier, Kind, Mechanism};
/// use decon_spf::Spf;
/// let mut new_spf_record = Spf::new();
/// new_spf_record.set_v1();
/// new_spf_record.append_mechanism(Mechanism::all(Qualifier::Pass));
/// new_spf_record.append_mechanism(Mechanism::a(Qualifier::Pass));
/// new_spf_record.append_ip_mechanism(Mechanism::new_ip(Qualifier::Pass,
/// "203.32.160.0/23".parse().unwrap()));
/// assert_eq!(new_spf_record.to_string(), "v=spf1 a ip4:203.32.160.0/23 all".to_string());
/// // Remove ip4 Mechanism
/// new_spf_record.clear_mechanism(Kind::IpV4);
/// assert_eq!(new_spf_record.to_string(), "v=spf1 a all".to_string());
///```
pub fn clear_mechanism(&mut self, kind: Kind) {
match kind {
Kind::Redirect => {
self.redirect = None;
self.is_redirected = false;
}
Kind::A => self.a = None,
Kind::MX => self.mx = None,
Kind::Include => self.include = None,
Kind::IpV4 => self.ip4 = None,
Kind::IpV6 => self.ip6 = None,
Kind::Exists => self.exists = None,
Kind::Ptr => self.ptr = None,
Kind::All => self.all = None,
}
}
fn append_mechanism_of_a(&mut self, mechanism: Mechanism<String>) {
if let Some(a) = &mut self.a {
a.push(mechanism);
} else {
self.a = Some(vec![mechanism]);
}
}
fn append_mechanism_of_mx(&mut self, mechanism: Mechanism<String>) {
if let Some(mx) = &mut self.mx {
mx.push(mechanism);
} else {
self.mx = Some(vec![mechanism]);
}
}
fn append_mechanism_of_include(&mut self, mechanism: Mechanism<String>) {
if let Some(include) = &mut self.include {
include.push(mechanism);
} else {
self.include = Some(vec![mechanism]);
}
}
fn append_mechanism_of_ip4(&mut self, mechanism: Mechanism<IpNetwork>) {
if let Some(ip4) = &mut self.ip4 {
ip4.push(mechanism);
} else {
self.ip4 = Some(vec![mechanism]);
}
}
fn append_mechanism_of_ip6(&mut self, mechanism: Mechanism<IpNetwork>) {
if let Some(ip6) = &mut self.ip6 {
ip6.push(mechanism);
} else {
self.ip6 = Some(vec![mechanism]);
}
}
fn append_mechanism_of_exists(&mut self, mechanism: Mechanism<String>) {
if let Some(exists) = &mut self.exists {
exists.push(mechanism);
} else {
self.exists = Some(vec![mechanism]);
}
}
fn append_mechanism_of_ptr(&mut self, mechanism: Mechanism<String>) {
self.ptr = Some(mechanism);
}
fn append_mechanism_of_all(&mut self, mechanism: Mechanism<String>) {
if self.redirect.is_none() {
self.all = Some(mechanism);
}
}
/// Appends the passed `Mechanism<String>` to the SPF struct.
/// This only works for Mechanism which are *NOT* `ip4:` or `ip6:`
///
/// # Example:
/// ```
/// use decon_spf::mechanism::{Qualifier, Mechanism};
/// use decon_spf::Spf;
/// let mut new_spf_record = Spf::new();
/// new_spf_record.set_v1();
/// new_spf_record.append_mechanism(Mechanism::new_redirect(Qualifier::Pass,
/// "_spf.example.com".to_string()));
/// new_spf_record.append_mechanism(Mechanism::all(Qualifier::Pass));
/// assert_eq!(new_spf_record.to_string(), "v=spf1 redirect=_spf.example.com".to_string());
/// ```
///
/// # Note:
/// If the Spf is already set as `Redirect` trying to append an `All`
/// Mechanism will have no affect.
// Consider make this a Result
pub fn append_mechanism(&mut self, mechanism: Mechanism<String>) {
match mechanism.kind() {
Kind::Redirect => self.append_mechanism_of_redirect(mechanism),
Kind::A => self.append_mechanism_of_a(mechanism),
Kind::MX => self.append_mechanism_of_mx(mechanism),
Kind::Include => self.append_mechanism_of_include(mechanism),
Kind::Exists => self.append_mechanism_of_exists(mechanism),
Kind::Ptr => self.append_mechanism_of_ptr(mechanism),
Kind::All => self.append_mechanism_of_all(mechanism),
_ => {}
}
}
/// Appends the passed `Mechanism<IpNetwork>` to the SPF struct.
///
/// # Example:
/// ```
/// use decon_spf::mechanism::{Qualifier, Mechanism};
/// use decon_spf::Spf;
/// let mut new_spf_record = Spf::new();
/// new_spf_record.set_v1();
/// new_spf_record.append_ip_mechanism(Mechanism::new_ip(Qualifier::Pass,
/// "203.32.160.0/23".parse().unwrap()));
/// new_spf_record.append_mechanism(Mechanism::all(Qualifier::Pass));
/// assert_eq!(new_spf_record.to_string(), "v=spf1 ip4:203.32.160.0/23 all".to_string());
/// ```
pub fn append_ip_mechanism(&mut self, mechanism: Mechanism<IpNetwork>) {
match mechanism.kind() {
Kind::IpV4 => self.append_mechanism_of_ip4(mechanism),
Kind::IpV6 => self.append_mechanism_of_ip6(mechanism),
_ => {
unreachable!()
}
}
}
/// # Note: Experimential
/// *Do not use.*
/// Very rudimentary validation check.
/// - Will fail if the length of `source` is more than MAX_SPF_STRING_LENGTH characters See:
/// [`SourceLengthExceeded`](SpfError::SourceLengthExceeded)
/// - Will fail if there are more than 10 DNS lookups. Looks are required for each `A`, `MX`
/// , `Redirect`, and `Include` Mechanism. See: [`LookupLimitExceeded`](SpfError::LookupLimitExceeded)
/// (This will change given new information)
#[deprecated(note = "This is expected to be depreciated.")]
pub fn try_validate(&mut self) -> Result<(), SpfError> {
if self.from_src {
if self.source.len() > helpers::MAX_SPF_STRING_LENGTH {
return Err(SpfError::SourceLengthExceeded);
} else if !self.was_parsed {
return Err(SpfError::HasNotBeenParsed);
};
};
// Redirect should be the only mechanism present. Any additional values are not permitted.
if self.redirect().is_some() && self.all().is_some() {
return Err(SpfError::RedirectWithAllMechanism);
}
if validate::check_lookup_count(self) > 10 {
return Err(SpfError::LookupLimitExceeded);
}
self.is_valid = true;
Ok(())
}
#[allow(dead_code)]
fn validate(&mut self, rfc: SpfRfcStandard) -> Result<&Self, SpfError> {
return match rfc {
SpfRfcStandard::Rfc4408 => validate::validate_rfc4408(self),
};
}
#[allow(dead_code)]
fn validate_to_string(&mut self, rfc: SpfRfcStandard) -> SpfValidationResult {
let res = match rfc {
SpfRfcStandard::Rfc4408 => validate::validate_rfc4408(self),
};
match res {
Ok(x) => SpfValidationResult::Valid(x),
Err(x) => SpfValidationResult::InValid(x),
}
}
fn build_spf_string(&self) -> String {
let mut spf = String::new();
spf.push_str(self.version());
if let Some(a) = self.a() {
spf.push_str(helpers::build_spf_str(a).as_str());
};
if let Some(mx) = self.mx() {
spf.push_str(helpers::build_spf_str(mx).as_str());
};
if let Some(includes) = self.includes() {
spf.push_str(helpers::build_spf_str(includes).as_str());
}
if let Some(ip4) = self.ip4() {
spf.push_str(helpers::build_spf_str_from_ip(ip4).as_str());
}
if let Some(ip6) = self.ip6() {
spf.push_str(helpers::build_spf_str_from_ip(ip6).as_str());
}
if let Some(exists) = self.exists() {
spf.push_str(helpers::build_spf_str(exists).as_str());
}
if let Some(ptr) = self.ptr() {
spf.push(' ');
spf.push_str(ptr.to_string().as_str());
}
if self.is_redirected {
spf.push(' ');
spf.push_str(self.redirect().unwrap().to_string().as_str());
}
// All can only be used if this is not a redirect.
if !self.is_redirected && self.all().is_some() {
spf.push(' ');
spf.push_str(self.all().unwrap().to_string().as_str());
}
spf
}
/// Returns a reference to the string stored in `source`
pub fn source(&self) -> &String {
// Source is set to "" by default.
&self.source
}
/// True if there is a redirect present in the spf record.
pub fn is_redirect(&self) -> bool {
self.is_redirected
}
/// Returns a reference to the `Redirect` Mechanism
pub fn redirect(&self) -> Option<&Mechanism<String>> {
self.redirect.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<String>` for `Include`
pub fn includes(&self) -> Option<&Vec<Mechanism<String>>> {
self.include.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<String>` for `A`
pub fn a(&self) -> Option<&Vec<Mechanism<String>>> {
self.a.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<String>` for `MX`
pub fn mx(&self) -> Option<&Vec<Mechanism<String>>> {
self.mx.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<IpNetwork>` for `IP4`
pub fn ip4(&self) -> Option<&Vec<Mechanism<IpNetwork>>> {
self.ip4.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<IpNetwork>` for `IP6`
pub fn ip6(&self) -> Option<&Vec<Mechanism<IpNetwork>>> {
self.ip6.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<String>` for `Exists`
pub fn exists(&self) -> Option<&Vec<Mechanism<String>>> {
self.exists.as_ref()
}
/// Returns a reference to the a `Vec` of `Mechanism<String>` for `Ptr`
pub fn ptr(&self) -> Option<&Mechanism<String>> {
self.ptr.as_ref()
}
/// Returns a reference to `Mechanism<String>` for `All`
pub fn all(&self) -> Option<&Mechanism<String>> {
self.all.as_ref()
}
/// Return a reference to the list of domains that gave warnings.
#[cfg_attr(docsrs, doc(cfg(feature = "warn-dns")))]
#[cfg(feature = "warn-dns")]
pub fn warnings(&self) -> Option<&Vec<String>> {
self.warnings.as_ref()
}
}