kumo_spf/
spec.rs

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
use crate::SpfContext;
use dns_resolver::IpDisplay;
use std::fmt::{self, Write};
use std::time::SystemTime;

fn starts_with_number(input: &str) -> Result<(Option<u32>, &str), String> {
    let i = input
        .find(|c: char| !c.is_numeric() && c != '.')
        .unwrap_or_else(|| input.len());
    if i == 0 {
        return Ok((None, input));
    }
    let number = input[..i]
        .parse::<u32>()
        .map_err(|err| format!("error parsing number from {input}: {err}"))?;
    Ok((Some(number), &input[i..]))
}

#[derive(Debug)]
pub(crate) struct MacroSpec {
    elements: Vec<MacroElement>,
}

impl MacroSpec {
    pub(crate) fn parse(s: &str) -> Result<Self, String> {
        let mut elements = vec![];

        fn add_literal(elements: &mut Vec<MacroElement>, literal: &str) {
            match elements.last_mut() {
                Some(MacroElement::Literal(prior)) => {
                    prior.push_str(literal);
                }
                _ => {
                    elements.push(MacroElement::Literal(literal.to_string()));
                }
            }
        }

        fn is_macro_literal(c: char) -> bool {
            let c = c as u32;
            (c >= 0x21 && c <= 0x24) || (c >= 0x26 && c <= 0x7e)
        }

        let mut s = s;
        while !s.is_empty() {
            if s.starts_with("%%") {
                add_literal(&mut elements, "%");
                s = &s[2..];
                continue;
            }
            if s.starts_with("%_") {
                add_literal(&mut elements, " ");
                s = &s[2..];
                continue;
            }
            if s.starts_with("%-") {
                add_literal(&mut elements, "%20");
                s = &s[2..];
                continue;
            }
            if s.starts_with("%{") {
                if s.len() < 4 {
                    return Err(format!("unexpected end of input in {s}"));
                }

                let (name, url_escape) = MacroName::parse(
                    s.chars()
                        .nth(2)
                        .ok_or_else(|| format!("unexpected end of input in {s}"))?,
                )?;
                let mut transformer_digits = None;
                let mut reverse = false;

                let remain = if let Ok((n, r)) = starts_with_number(&s[3..]) {
                    transformer_digits = n;
                    r
                } else {
                    &s[3..]
                };

                let delimiters = if remain.starts_with('r') {
                    reverse = true;
                    &remain[1..]
                } else {
                    remain
                };

                let (delimiters, remain) = delimiters
                    .split_once('}')
                    .ok_or_else(|| format!("expected '}}' to close macro in {s}"))?;

                elements.push(MacroElement::Macro(MacroTerm {
                    name,
                    transformer_digits,
                    reverse,
                    url_escape,
                    delimiters: delimiters.to_string(),
                }));

                s = remain;
                continue;
            }

            if !is_macro_literal(s.chars().next().unwrap()) {
                return Err(format!("invalid macro char in {s}"));
            }

            add_literal(&mut elements, &s[0..1]);
            s = &s[1..];
        }

        Ok(Self { elements })
    }

    pub(crate) fn expand(&self, cx: &SpfContext<'_>) -> Result<String, String> {
        let (mut result, mut buf) = (String::new(), String::new());
        for element in &self.elements {
            let m = match element {
                MacroElement::Literal(t) => {
                    result.push_str(&t);
                    continue;
                }
                MacroElement::Macro(m) => m,
            };

            buf.clear();
            match m.name {
                MacroName::Sender => buf.push_str(cx.sender),
                MacroName::LocalPart => buf.push_str(&cx.local_part),
                MacroName::SenderDomain => buf.push_str(&cx.sender_domain),
                MacroName::Domain => buf.push_str(&cx.domain),
                MacroName::ReverseDns => buf.push_str(match cx.client_ip.is_ipv4() {
                    true => "in-addr",
                    false => "ip6",
                }),
                MacroName::ClientIp => {
                    buf.write_fmt(format_args!("{}", cx.client_ip)).unwrap();
                }
                MacroName::Ip => buf
                    .write_fmt(format_args!(
                        "{}",
                        IpDisplay {
                            ip: cx.client_ip,
                            reverse: false
                        }
                    ))
                    .unwrap(),
                MacroName::CurrentUnixTimeStamp => buf
                    .write_fmt(format_args!(
                        "{}",
                        cx.now
                            .duration_since(SystemTime::UNIX_EPOCH)
                            .map(|d| d.as_secs())
                            .unwrap_or(0)
                    ))
                    .unwrap(),
                MacroName::RelayingHostName
                | MacroName::HeloDomain
                | MacroName::ValidatedDomainName => {
                    return Err(format!("{:?} has not been implemented", m.name))
                }
            };

            let delimiters = if m.delimiters.is_empty() {
                "."
            } else {
                &m.delimiters
            };

            let mut tokens: Vec<&str> = buf.split(|c| delimiters.contains(c)).collect();

            if m.reverse {
                tokens.reverse();
            }

            if let Some(n) = m.transformer_digits {
                let n = n as usize;
                while tokens.len() > n {
                    tokens.remove(0);
                }
            }

            let output = tokens.join(".");

            if m.url_escape {
                // https://datatracker.ietf.org/doc/html/rfc7208#section-7.3:
                //   Uppercase macros expand exactly as their lowercase
                //   equivalents, and are then URL escaped.  URL escaping
                //   MUST be performed for characters not in the
                //   "unreserved" set.
                // https://datatracker.ietf.org/doc/html/rfc3986#section-2.3:
                //    unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
                for c in output.chars() {
                    if c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' || c == '~' {
                        result.push(c);
                    } else {
                        let mut bytes = [0u8; 4];
                        for b in c.encode_utf8(&mut bytes).bytes() {
                            result.push_str(&format!("%{b:02x}"));
                        }
                    }
                }
            } else {
                result.push_str(&output);
            }
        }

        Ok(result)
    }
}

impl fmt::Display for MacroSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut first = true;
        for element in &self.elements {
            if first {
                first = false;
            } else {
                f.write_str(" ")?;
            }

            match element {
                MacroElement::Literal(lit) => write!(f, "{lit}")?,
                MacroElement::Macro(term) => write!(f, "{term}")?,
            }
        }
        Ok(())
    }
}

#[derive(Debug)]
enum MacroElement {
    Literal(String),
    Macro(MacroTerm),
}

#[derive(Debug)]
struct MacroTerm {
    pub name: MacroName,
    /// digits were present in the transformer section
    pub transformer_digits: Option<u32>,
    /// The output needs to be URL-escaped
    pub url_escape: bool,
    /// the `r` transformer was present
    pub reverse: bool,
    /// The list of delimiters, if any, otherwise an empty string
    pub delimiters: String,
}

impl fmt::Display for MacroTerm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "%{}{}", self.name.as_char(), self.delimiters)?;
        if let Some(digits) = self.transformer_digits {
            write!(f, "{}", digits)?;
        }
        if self.reverse {
            f.write_str("r")?;
        }
        if self.url_escape {
            f.write_str("/")?;
        }
        Ok(())
    }
}

#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone)]
enum MacroName {
    /// `s` - <sender>
    Sender,
    /// `l` - local-part of <sender>
    LocalPart,
    /// `o` - domain of <sender>
    SenderDomain,
    /// `d` - <domain>
    Domain,
    /// `i` - <ip>
    Ip,
    /// `p` - the validated domain name of <ip> (do not use)
    ValidatedDomainName,
    /// `v` the string `in-addr` if <ip> is ipv4, or `ip6` is <ip> is ipv6
    ReverseDns,
    /// `h` the HELO/EHLO domain
    HeloDomain,
    /// `c` - only in "exp" text: the SMTP client IP (easily readable format)
    ClientIp,
    /// `r` - only in "exp" text: domain name of host performing the check
    RelayingHostName,
    /// `t` - only in "exp" text: the current timestamp
    CurrentUnixTimeStamp,
}

impl MacroName {
    fn parse(c: char) -> Result<(Self, bool), String> {
        let escape = c.is_ascii_uppercase();
        Ok((
            match c.to_ascii_lowercase() {
                's' => Self::Sender,
                'l' => Self::LocalPart,
                'o' => Self::SenderDomain,
                'd' => Self::Domain,
                'i' => Self::Ip,
                'p' => Self::ValidatedDomainName,
                'v' => Self::ReverseDns,
                'h' => Self::HeloDomain,
                'c' => Self::ClientIp,
                'r' => Self::RelayingHostName,
                't' => Self::CurrentUnixTimeStamp,
                _ => return Err(format!("invalid macro name {c}")),
            },
            escape,
        ))
    }

    fn as_char(&self) -> char {
        match self {
            Self::Sender => 's',
            Self::LocalPart => 'l',
            Self::SenderDomain => 'o',
            Self::Domain => 'd',
            Self::Ip => 'i',
            Self::ValidatedDomainName => 'p',
            Self::ReverseDns => 'v',
            Self::HeloDomain => 'h',
            Self::ClientIp => 'c',
            Self::RelayingHostName => 'r',
            Self::CurrentUnixTimeStamp => 't',
        }
    }
}

#[cfg(test)]
mod test {
    use std::net::IpAddr;

    use super::*;
    use crate::spec::MacroSpec;

    #[test]
    fn test_eval() {
        // <https://datatracker.ietf.org/doc/html/rfc7208#section-7.4>

        let mut ctx = SpfContext::new(
            "strong-bad@email.example.com",
            "email.example.com",
            IpAddr::from([192, 0, 2, 3]),
        )
        .unwrap();

        for (input, expect) in &[
            ("%{s}", "strong-bad@email.example.com"),
            ("%{o}", "email.example.com"),
            ("%{d}", "email.example.com"),
            ("%{d4}", "email.example.com"),
            ("%{d3}", "email.example.com"),
            ("%{d2}", "example.com"),
            ("%{d1}", "com"),
            ("%{dr}", "com.example.email"),
            ("%{d2r}", "example.email"),
            ("%{l}", "strong-bad"),
            ("%{l-}", "strong.bad"),
            ("%{lr}", "strong-bad"),
            ("%{lr-}", "bad.strong"),
            ("%{l1r-}", "strong"),
        ] {
            let spec = MacroSpec::parse(input).unwrap();
            let output = spec.expand(&ctx).unwrap();
            k9::assert_equal!(&output, expect, "{input}");
        }

        for (input, expect) in &[
            (
                "%{ir}.%{v}._spf.%{d2}",
                "3.2.0.192.in-addr._spf.example.com",
            ),
            ("%{lr-}.lp._spf.%{d2}", "bad.strong.lp._spf.example.com"),
            (
                "%{lr-}.lp.%{ir}.%{v}._spf.%{d2}",
                "bad.strong.lp.3.2.0.192.in-addr._spf.example.com",
            ),
            (
                "%{ir}.%{v}.%{l1r-}.lp._spf.%{d2}",
                "3.2.0.192.in-addr.strong.lp._spf.example.com",
            ),
            (
                "%{d2}.trusted-domains.example.net",
                "example.com.trusted-domains.example.net",
            ),
            ("%{c}", "192.0.2.3"),
        ] {
            let spec = MacroSpec::parse(input).unwrap();
            let output = spec.expand(&ctx).unwrap();
            k9::assert_equal!(&output, expect, "{input}");
        }

        ctx.client_ip = IpAddr::from([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0xcb01]);
        for (input, expect) in &[
            (
                "%{ir}.%{v}._spf.%{d2}",
                "1.0.b.c.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0\
                 .0.0.0.0.8.b.d.0.1.0.0.2.ip6._spf.example.com",
            ),
            ("%{c}", "2001:db8::cb01"),
            ("%{C}", "2001%3adb8%3a%3acb01"),
        ] {
            let spec = MacroSpec::parse(input).unwrap();
            let output = spec.expand(&ctx).unwrap();
            k9::assert_equal!(&output, expect, "{input}");
        }
    }
}