Posted on April 8, 2021
| 12 min
| 2353 words
| Bas-Man
Hi. If you have visited my blog before you might have seen my series on using the trust-dns-resolver crate. As part of that series I looked at querying DNS TXT records. During the process I was reminded of SPF records. Something I used to deal with in a previous position. And this got me to thinking about using SPF records as a way to learn more about working with rust.
Keep in mind I am new to rust and these will just be some of my thoughts as I work through the task I have given myself. This will not be a production project, merely a learning experience. I will not be documenting all the code here. I will give a reference to the git repository at the end of this article.
TL;DR;
Some caveats
I will not be implementing a full solution to deconstruct an SPF record at this time.
Not all edge cases may be covered, though I will do my best to do so.
I will initially only be looking at: redirect, include, ip4 and ip6 records contained within the SPF record.
If there is no qualifier present, it is assumed to be + which equates to a Pass
During this phase I will not be looking at using generics as I want to understand the basics of using structs
I will not be writing any code that validates that the SPF record is syntactically correct.
The code is going to be fairly ugly and will get polished as I go through.
I am not looking at SPF2 (pra/mfrom)
Some SPF rules I am fairly familiar with, though I could be mistaken. (smile)
There can only be one redirect in a single SPF record.
The qualifier is optional
There can be multiple instances of ip4, ip6 and include
There can only be a single a and mx when they are not followed by a : character.
With the exception of a redirect there must an all at the end of an SPF record.
Let’s get started
I am not going to start off with how to create a rust project. There is plenty on that out there. And if you read my previous series on Working with the trust-dns-resolver Crate, you already know how to get started. In fact this is an extension to that series.
Structs
Following the basic rules I listed above each mechanism can have an optional qualifier. This will need to be dealt with. And each mechanism can occur multiple times in most cases.
The Include Struct
An include can be represented in the following ways: (Not an exhaustive list)
I will store the qualifier as a char, this will allow me to hold +,-,~, or ?. Alternatively I could create an enum type. But this is over kill since I am just trying to learning at this point.
I will store the _spf.example.com portion in a String which lives on the heap in memory.
To save on some code, I am asking the compiler to generate Default, Debug and Clone for me using the #[derive]
Implementing the Include Struct
Rust is interesting in that it allows us to create functions which are tied to a struct. In my mind this is some what like the struct is acting as an object, and the functions are then used as methods of the struct.
Single quotes can be used here to denote a type of char
Text enclosed in double-quotes are technically of type str and since my function definition expects a String, I am transforming the string. This is purely for the example purposes. I could have also easily passed String::from("_spf.example.com")
The is_* functions all simple check the value stored in qualifier and return a boolean.
The as_mechanism() function is able to reconstruct the spf mechanism and return it as a String. If the qualifier is NOT a +, it is appended to the string being created. Otherwise it is omitted.
The Ip4 Struct
An ip4 can be presented in the following ways: (not an exhaustive list)
ip4:157.55.9.128/25
+ip4:ip4:72.14.192.0/18
Or as a single host ip4:X.X.X.X which would be used as X.X.X.X/32
Here I am only using Debug and Clone as I am implementing my own Default merely for learning purposes. The astute reader might already conclude that this struct is is very similar to Include with the only real difference being String and IpNetwork. IpNetwork is provided by the ipnetwork crate. I am using this as it provides lots of the basic functions I will need and also many more that would make it useful for other applications.
This is essentially the same as Ip4 because IpNetork is an enum of V4 and V6
Defining the SPF1 Struct
Many of the mechanisms in SPF are optional and nicely, rust actually provides the type Option. Also a mechanism can appear several times, as a list and rust provides Vec which is a way to store a list of some type.
source: The original string received from the TXT DNS lookup. This will be deconstructed to populate the SPF struct.
include: This is an optional list of includes so I am creating a Vec of Include and then wrapping that inside and Option since it might actually be None
Ip4 and Ip6 are handle in the same way as Include
redirect: Only ever occurs once in a record. So I am placing it directly within SPF1, but again it is optional, so I wrap it inside an option
is_redirect: Is closely tied to direct and simply stores a bool value
all_qualifier: This is a special case. It is only ever not present when the spf record is a redirect so I am only ensure that I have a qualifer value.
Implementing SPF1 Struct
implSpf1{fnnew(str: &String)-> Self{Self{source: str.clone(),include: None,redirect: None,is_redirected: false,a: None,mx: None,ip4: None,ip6: None,all_qualifier: '+',}}fnparse(&mutself){letrecords=self.source.split_whitespace();letmutvec_of_includes: Vec<Include>=Vec::new();letmutvec_of_ip4: Vec<Ip4>=Vec::new();letmutvec_of_ip6: Vec<Ip6>=Vec::new();forrecordinrecords{// Main loop
ifrecord.contains("redirect="){letitems=record.rsplit("=");foriteminitems{self.redirect=Some(item.to_string());break;}self.is_redirected=true;}elseifrecord.contains("include:"){letqualifier_and_modified_str=return_and_remove_qualifier(record,'i');foriteminrecord.rsplit(":"){vec_of_includes.push(Include::new(qualifier_and_modified_str.0,item.to_string(),));break;// skip the 'include:'
}}elseifrecord.contains("ip4:"){letqualifier_and_modified_str=return_and_remove_qualifier(record,'i');ifletSome(raw_ip4)=qualifier_and_modified_str.1.strip_prefix("ip4:"){letnetwork: Ip4=Ip4::new(qualifier_and_modified_str.0,raw_ip4.parse().unwrap());vec_of_ip4.push(network);}}elseifrecord.contains("ip6:"){letqualifier_and_modified_str=return_and_remove_qualifier(record,'i');ifletSome(raw_ip6)=qualifier_and_modified_str.1.strip_prefix("ip6:"){letnetwork: Ip6=Ip6::new(qualifier_and_modified_str.0,raw_ip6.parse().unwrap());vec_of_ip6.push(network);}}elseifrecord.ends_with("all"){self.all_qualifier=return_and_remove_qualifier(record,'a').0;}}// End of Main Loop
ifvec_of_includes.len()>0{self.include=Some(vec_of_includes);};ifvec_of_ip4.len()>0{self.ip4=Some(vec_of_ip4);};ifvec_of_ip6.len()>0{self.ip6=Some(vec_of_ip6);};}// Code omitted for brevity
}
This function takes in a reference, borrows, a string and then internally uses clone() to make a copy of the string which then remains valid for the life time of the struct. Is also initialises the variables within the struct.
Deconstructing time (Finally)
Enter the real work horse in this process. It’s currently a little monolithic at the moment. But, it’s getting the job done.
spf.parse();
The first four lines set things up.
Split the string in to seperate parts based on white space. Placing them into an iter()
Create our mutable Vec varibles for Include, Ip4 and Ip6
Next we start the main loop which will parse each mechanism
redirect:
If the current record contains rediect= we use rsplit() which returns a new iter() and access the right side of the split first. We wrap that result into a Some() because Spf1.redirect expects an Option. Next we break from the loop since we don’t need any other parts of this iter(). We are also not concerned about the qualifier in this case as it is usually omitted.
include:
Similar to the redirect we do an rsplit() but on “:”
Create a new Include struct and push that value on to the vec_of_includes that was defined at the start of parse()
Call break to exist inner loop
ip6 has an issue. I am using strip_prefix() because I am unable to use rsplit() due to ip6 being comprised of multiple : characters. This is an issue because I am using strip_prefix() on "ip6:" This will fail if there is a qualifer present in the string.
ie: “+ip6:….”. To handle this I have a helper function return_and_remove_qualifier() which checks for the presence of a qualifier, and returns a tuple. The tuple contains the qualifier and an updated version of original string with the qualifier removed if it was present. This ensure that the strip_prefix() function will match correctly.
strip_prefix() returns Some(), so we take a peek inside and conditionally create an Ip6 struct.
Push it on vec_of_ip6, I actually do the same process for ip4
all;
If the the mechanism ends with all we just store the qualifier directly into self.all_qualifier
When we reach the end of the main loop we then conditionally wrap our various vec_of.* structs into their own Some() and assign them to their matching property in Spf1
Test Domain and Test Output.
Test Domains
I am using the following records at the time of writing.
List of TXT records found for gmail.com.
TXT Record 1:
globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8=TXT Record 2:
v=spf1 redirect=_spf.google.com
Decontructing SPF Record
SPF1: v=spf1 redirect=_spf.google.com
There are no include elements
There are no ip4 networks
There are no ip4 spf records.
Is a redirect: trueredirect: _spf.google.com
mechanism: redirect=_spf.google.com
Test Output (_netblocks.google.com)
List of TXT records found for _netblocks.google.com.
TXT Record 1:
v=spf1 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/
18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:209.85.128.0/17 ip4:216.58.192.0/19 i
p4:216.239.32.0/19 ~all
Decontructing SPF Record
SPF1: v=spf1 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.
192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:209.85.128.0/17 ip4:216.58.192.
0/19 ip4:216.239.32.0/19 ~all
There are no include elements
List of ip4 networks/hosts:
35.190.247.0/24
64.233.160.0/19
66.102.0.0/20
66.249.80.0/20
72.14.192.0/18
74.125.0.0/16
108.177.8.0/21
173.194.0.0/16
209.85.128.0/17
216.58.192.0/19
216.239.32.0/19
List of ip4 mechanisms:
ip4:35.190.247.0/24
ip4:64.233.160.0/19
ip4:66.102.0.0/20
ip4:66.249.80.0/20
ip4:72.14.192.0/18
ip4:74.125.0.0/16
ip4:108.177.8.0/21
ip4:173.194.0.0/16
ip4:209.85.128.0/17
ip4:216.58.192.0/19
ip4:216.239.32.0/19
Is a redirect: false
Test Output (_netblocks2.google.com) / Not being displayed at this time.
List of TXT records found for _netblocks2.google.com.
TXT Record 1:
v=spf1 ip6:2001:4860:4000::/36 ip6:2404:6800:4000::/36 ip6:2607:f8b0:4000::/36 ip6:2800:3f0:4000::/3
6 ip6:2a00:1450:4000::/36 ip6:2c0f:fb50:4000::/36 ~all
Decontructing SPF Record
SPF1: v=spf1 ip6:2001:4860:4000::/36 ip6:2404:6800:4000::/36 ip6:2607:f8b0:4000::/36 ip6:2800:3f0:40
00::/36 ip6:2a00:1450:4000::/36 ip6:2c0f:fb50:4000::/36 ~all
There are no include elements
There are no ip4 networks
There are no ip4 spf records.
Is a redirect: false
Test Output (hotmail.com)
List of TXT records found for hotmail.com.
TXT Record 1:
google-site-verification=gqFmgDKSUd3XGU_AzWWdojRHtW3_66W_PC3oFvQVZEw
TXT Record 2:
v=spf1 ip4:157.55.9.128/25 include:spf.protection.outlook.com include:spf-a.outlook.com include:spf-
b.outlook.com include:spf-a.hotmail.com include:_spf-ssg-b.microsoft.com include:_spf-ssg-c.microsof
t.com ~all
Decontructing SPF Record
SPF1: v=spf1 ip4:157.55.9.128/25 include:spf.protection.outlook.com include:spf-a.outlook.com includ
e:spf-b.outlook.com include:spf-a.hotmail.com include:_spf-ssg-b.microsoft.com include:_spf-ssg-c.mi
crosoft.com ~all
Include Mechanisms:
include:spf.protection.outlook.com
include:spf-a.outlook.com
include:spf-b.outlook.com
include:spf-a.hotmail.com
include:_spf-ssg-b.microsoft.com
include:_spf-ssg-c.microsoft.com
List of ip4 networks/hosts:
157.55.9.128/25
List of ip4 mechanisms:
ip4:157.55.9.128/25
Is a redirect: false
You can get a copy of the complete code at the time of this writing here