spf-milter 0.6.0

Milter for SPF verification
Documentation
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
// SPF Milter – milter for SPF verification
// Copyright © 2020–2023 David Bürgin <dbuergin@gluet.ch>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
    auth::SpfResultKind,
    header::{format, HeaderField, HEADER_LINE_WIDTH},
    verify::{Identity, VerificationResult},
};
use std::{
    borrow::Cow,
    fmt::{self, Display, Formatter},
    net::IpAddr,
};
use viaspf::{SpfResult, SpfResultCause};

/// A `Received-SPF` header, containing the header field body in an encoded,
/// pre-formatted form.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ReceivedSpfHeader {
    body_pieces: Vec<String>,
}

impl ReceivedSpfHeader {
    pub const NAME: &'static str = "Received-SPF";

    pub fn new(
        result: &VerificationResult,
        hostname: &str,
        ip: IpAddr,
        helo_host: Option<&str>,
    ) -> Self {
        Self {
            body_pieces: prepare_field_body(result, hostname, ip, helo_host),
        }
    }
}

fn prepare_field_body(
    result: &VerificationResult,
    hostname: &str,
    ip: IpAddr,
    helo_host: Option<&str>,
) -> Vec<String> {
    let mut parts = Vec::new();

    let VerificationResult { identity, spf_result, cause } = result;

    parts.push(spf_result.kind().into());

    add_comment(&mut parts, spf_result, hostname, ip, identity);

    add_key_value_pairs(
        &mut parts,
        spf_result,
        cause.as_ref(),
        hostname,
        ip,
        helo_host,
        identity,
    );

    parts
}

fn add_comment(
    parts: &mut Vec<String>,
    spf_result: &SpfResult,
    hostname: &str,
    ip: IpAddr,
    identity: &Identity,
) {
    use SpfResult::*;
    match spf_result {
        Pass => format_pass_parts(parts, hostname, ip, identity),
        Fail(_) => format_fail_parts(parts, hostname, ip, identity),
        Softfail => format_softfail_parts(parts, hostname, ip, identity),
        Neutral => format_neutral_parts(parts, hostname, ip, identity),
        None => format_none_parts(parts, hostname, identity),
        Temperror => format_temperror_parts(parts, hostname, identity),
        Permerror => format_permerror_parts(parts, hostname, identity),
    }
}

// (mail.example.org: domain [of me@]example.org has authorized host 1.2.3.4)
fn format_pass_parts(parts: &mut Vec<String>, hostname: &str, ip: IpAddr, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    format_sender(parts, identity);
    parts.extend("has authorized host".split_whitespace().map(From::from));
    parts.push(format!("{ip})"));
}

// (mail.example.org: domain [of me@]example.org has not authorized host 1.2.3.4)
fn format_fail_parts(parts: &mut Vec<String>, hostname: &str, ip: IpAddr, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    format_sender(parts, identity);
    parts.extend("has not authorized host".split_whitespace().map(From::from));
    parts.push(format!("{ip})"));
}

// (mail.example.org: domain [of me@]example.org discourages use of host 1.2.3.4)
fn format_softfail_parts(parts: &mut Vec<String>, hostname: &str, ip: IpAddr, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    format_sender(parts, identity);
    parts.extend("discourages use of host".split_whitespace().map(From::from));
    parts.push(format!("{ip})"));
}

// (mail.example.org: domain [of me@]example.org makes no definitive authorization statement for host 1.2.3.4)
fn format_neutral_parts(parts: &mut Vec<String>, hostname: &str, ip: IpAddr, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    format_sender(parts, identity);
    parts.extend("makes no definitive authorization statement for host".split_whitespace().map(From::from));
    parts.push(format!("{ip})"));
}

// (mail.example.org: no authorization information available for sender [me@]example.org)
fn format_none_parts(parts: &mut Vec<String>, hostname: &str, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    parts.extend("no authorization information available for sender".split_whitespace().map(From::from));
    parts.push(format!("{})", format::escape_comment_word(identity.as_ref())));
}

// (mail.example.org: sender [me@]example.org could not be authorized due to a transient DNS error)
fn format_temperror_parts(parts: &mut Vec<String>, hostname: &str, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    parts.push("sender".into());
    parts.push(format::escape_comment_word(identity.as_ref()).into());
    parts.extend("could not be authorized due to a transient DNS error)".split_whitespace().map(From::from));
}

// (mail.example.org: sender [me@]example.org could not be authorized due to a permanent error in SPF records)
fn format_permerror_parts(parts: &mut Vec<String>, hostname: &str, identity: &Identity) {
    parts.push(format!("({}:", format::escape_comment_word(hostname)));
    parts.push("sender".into());
    parts.push(format::escape_comment_word(identity.as_ref()).into());
    parts.extend("could not be authorized due to a permanent error in SPF records)".split_whitespace().map(From::from));
}

// HELO identity: ‘domain <sender>’, MAIL FROM identity: ‘domain of <sender>’
fn format_sender(parts: &mut Vec<String>, sender: &Identity) {
    parts.push("domain".into());
    if let Identity::MailFrom(_) = sender {
        parts.push("of".into());
    }
    parts.push(format::escape_comment_word(sender.as_ref()).into());
}

fn add_key_value_pairs(
    parts: &mut Vec<String>,
    spf_result: &SpfResult,
    cause: Option<&SpfResultCause>,
    hostname: &str,
    ip: IpAddr,
    helo_host: Option<&str>,
    identity: &Identity,
) {
    let kvs = make_key_value_pairs(spf_result, cause, hostname, ip, helo_host, identity);

    if let [kvs @ .., last] = &kvs[..] {
        for (key, value) in kvs {
            parts.push(format!("{key}={};", format::encode_value(value)));
        }
        let (key, value) = last;
        parts.push(format!("{key}={}", format::encode_value(value)));
    }
}

fn make_key_value_pairs<'a>(
    spf_result: &'a SpfResult,
    cause: Option<&'a SpfResultCause>,
    hostname: &'a str,
    ip: IpAddr,
    helo_host: Option<&'a str>,
    identity: &'a Identity,
) -> Vec<(&'static str, Cow<'a, str>)> {
    let mut kvs = Vec::new();

    kvs.push(("receiver", hostname.into()));
    kvs.push(("client-ip", ip.to_string().into()));

    // This key records the HELO name as given by the client. This may be
    // different from what was used for evaluation, for example when `unknown`
    // was substituted for an invalid HELO identity.
    if let Some(helo_host) = helo_host {
        kvs.push(("helo", helo_host.into()));
    }

    if let Identity::MailFrom(mail_from) = &identity {
        kvs.push(("envelope-from", mail_from.into()));
    }

    kvs.push(("identity", identity.name().into()));

    match cause {
        Some(SpfResultCause::Match(mechanism)) => {
            kvs.push(("mechanism", mechanism.to_string().into()));
        }
        Some(SpfResultCause::Error(error)) => {
            kvs.push(("problem", error.to_string().into()));
        }
        None => {
            if let SpfResult::Neutral = spf_result {
                // Neutral result, no mechanism matched: use value `default`.
                // See RFC 7208, section 9.1.
                kvs.push(("mechanism", "default".into()));
            }
        }
    }

    kvs
}

impl Display for ReceivedSpfHeader {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", Self::NAME, self.body_pieces.join(" "))
    }
}

impl HeaderField for ReceivedSpfHeader {
    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn format_body(&self) -> String {
        format_header_value(
            Self::NAME.len() + 1,  // name length plus colon
            HEADER_LINE_WIDTH,
            &self.body_pieces,
        )
    }
}

fn format_header_value<I, S>(initlen: usize, limit: usize, items: I) -> String
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    // Formatting is based on Unicode character count, not octet length. See RFC
    // 6532, section 3.4.
    let mut i = initlen;
    let mut value = String::new();

    for (n, item) in items.into_iter().enumerate() {
        let item = item.as_ref();
        let len = item.chars().count() + 1;  // item length plus leading space or tab
        if i + len <= limit {
            // Skip initial space, as the space after the field name is
            // automatically added by the milter library.
            if n != 0 {
                value.push(' ');
            }
        } else {
            // Never add `\n\t` at the very beginning of the field value.
            if n != 0 {
                value.push_str("\n\t");
                i = 0;
            }
        };
        value.push_str(item);
        i += len;
    }

    value
}

#[cfg(test)]
mod tests {
    use super::*;
    use viaspf::{
        record::{Ip4CidrLength, Mechanism, A},
        ErrorCause,
    };

    #[test]
    fn format_pass_parts_ok() {
        let mut parts = Vec::new();
        format_pass_parts(
            &mut parts,
            "mail.example.org",
            IpAddr::from([1, 2, 3, 4]),
            &Identity::MailFrom("amy@example.com".into()),
        );
        assert_eq!(
            parts.join(" "),
            "(mail.example.org: domain of amy@example.com has authorized host 1.2.3.4)",
        );
    }

    #[test]
    fn format_none_parts_with_unusual_sender() {
        let mut parts = Vec::new();
        format_none_parts(
            &mut parts,
            "mail.example.org",
            &Identity::MailFrom("\"what(!)\"@example.com".into()),
        );
        assert_eq!(
            parts.join(" "),
            "(mail.example.org: no authorization information available for sender \"what\\(!\\)\"@example.com)",
        );
    }

    #[test]
    fn received_spf_header_display_pass() {
        let result = VerificationResult {
            identity: Identity::MailFrom("me@example.org".into()),
            spf_result: SpfResult::Pass,
            cause: Some(SpfResultCause::Match(Mechanism::A(A {
                domain_spec: None,
                prefix_len: Some(Ip4CidrLength::new(24).unwrap().into()),
            }))),
        };
        let header = ReceivedSpfHeader::new(
            &result,
            "mail.example.com",
            IpAddr::from([1, 2, 3, 4]),
            Some("mail.example.org"),
        );

        assert_eq!(
            header.to_string(),
            "Received-SPF: \
            pass \
            (mail.example.com: domain of me@example.org has authorized host 1.2.3.4) \
            receiver=mail.example.com; \
            client-ip=1.2.3.4; \
            helo=mail.example.org; \
            envelope-from=\"me@example.org\"; \
            identity=mailfrom; \
            mechanism=a/24"
        );
    }

    #[test]
    fn received_spf_header_display_error() {
        let result = VerificationResult {
            identity: Identity::Helo("mail.example.org".into()),
            spf_result: SpfResult::Temperror,
            cause: Some(SpfResultCause::Error(ErrorCause::Timeout)),
        };
        let header = ReceivedSpfHeader::new(
            &result,
            "mail.example.com",
            IpAddr::from([1, 2, 3, 4]),
            Some("mail.example.org"),
        );

        assert_eq!(
            header.to_string(),
            "Received-SPF: \
            temperror \
            (mail.example.com: sender mail.example.org could not be authorized due to a transient DNS error) \
            receiver=mail.example.com; \
            client-ip=1.2.3.4; \
            helo=mail.example.org; \
            identity=helo; \
            problem=\"DNS lookup timed out\""
        );
    }

    #[test]
    fn received_spf_header_display_default_neutral_result() {
        let result = VerificationResult {
            identity: Identity::MailFrom("me@example.org".into()),
            spf_result: SpfResult::Neutral,
            cause: None,
        };
        let header = ReceivedSpfHeader::new(
            &result,
            "mail.example.com",
            IpAddr::from([1, 2, 3, 4]),
            Some("mail.example.org"),
        );

        assert_eq!(
            header.to_string(),
            "Received-SPF: \
            neutral \
            (mail.example.com: domain of me@example.org makes no definitive authorization statement for host 1.2.3.4) \
            receiver=mail.example.com; \
            client-ip=1.2.3.4; \
            helo=mail.example.org; \
            envelope-from=\"me@example.org\"; \
            identity=mailfrom; \
            mechanism=default"
        );
    }

    #[test]
    fn format_header_value_ok() {
        let parts = ["one", "two", "three", "four", "five"];
        assert_eq!(
            format_header_value(0, 10, parts),
            "one two\n\
            \tthree\n\
            \tfour five",
        );
        assert_eq!(
            format_header_value(0, 11, parts),
            "one two\n\
            \tthree four\n\
            \tfive",
        );
        assert_eq!(
            format_header_value(7, 11, parts),
            "one\n\
            \ttwo three\n\
            \tfour five",
        );
        assert_eq!(
            format_header_value(8, 11, parts),
            "one\n\
            \ttwo three\n\
            \tfour five",
        );

        let parts = ["一二三", "two", "three", "four", "five"];
        assert_eq!(
            format_header_value(0, 10, parts),
            "一二三 two\n\
            \tthree\n\
            \tfour five",
        );
    }
}