// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Identifier alignment, and the verdict that follows from it — [RFC //! 7489 §3.1](https://datatracker.ietf.org/doc/html/rfc7489#section-3.1) //! and §6.6.2. //! //! DMARC's whole contribution is one sentence: a message passes if some //! authenticated identifier belongs to the same domain as the `From:` //! field a person reads. Everything else — the record, the walk, the //! policy — is in service of that sentence. //! //! The reason it is needed at all is that both underlying mechanisms //! authenticate something else. A message can carry a perfectly valid //! DKIM signature from a domain its sender controls, and pass SPF for a //! bounce address its sender controls, while saying `From:` whatever it //! likes. Alignment is what makes those facts mean something about the //! name the reader sees. const std = @import("std"); const Io = std.Io; const Record = @import("Record.zig"); const Resolver = @import("Resolver.zig"); const discover = @import("discover.zig"); /// What SPF or DKIM said. /// /// The full set from [RFC 8601 /// §2.7](https://datatracker.ietf.org/doc/html/rfc8601#section-2.7), /// not just the ones alignment turns on: only `pass` can align, but a /// receiver that recorded every failure as "fail" would throw away the /// difference between a forgery, a misconfiguration and a nameserver /// that was down — which is most of what an operator reads these /// results for. pub const AuthResult = enum { /// Nothing to check: no policy, or no signature. none, pass, fail, /// SPF only: the domain says it is not sure yet. softfail, /// A policy that declines to say. neutral, /// The result was overridden by the receiver's own policy. §2.4. policy, temperror, permerror, }; /// One thing SPF or DKIM authenticated: a result and the domain it is /// about. pub const Identifier = struct { result: AuthResult, /// For SPF, the domain of the `MAIL FROM` address that was checked /// — or of the HELO name, when the reverse-path was empty. For /// DKIM, the signature's `d=`. domain: []const u8, }; /// What a caller knows about a message before DMARC looks at it. pub const Message = struct { /// The domain of the `From:` field. §3.1 calls this the Author /// Domain, and it is the one identifier DMARC exists to protect. /// /// A message with more than one `From:` address, or more than one /// `From:` field, cannot be evaluated — §6.6.1 says so, and a caller /// that flattens the two into one is deciding something DMARC /// declined to. from_domain: []const u8, /// The SPF result, if one was obtained. spf: ?Identifier = null, /// Every DKIM signature that verified, with the domain that signed /// it. §3.1.1: a message can carry several, and one aligned /// signature is enough. dkim: []const Identifier = &.{}, }; /// Why a message passed or failed, which is what a report carries and /// what an operator reads when working out where their mail comes from. pub const Disposition = enum { /// An aligned identifier passed. pass, /// Nothing aligned, so the domain owner's policy applies. fail, /// No policy was found: DMARC does not apply to this message. none, /// A DNS failure got in the way. §4.10.1 leaves the choice between /// delivering and deferring to the receiver, so it is reported /// rather than decided. temperror, }; pub const Outcome = struct { disposition: Disposition, /// What the domain owner asked for, when nothing aligned. Null when /// the message passed or no policy applies. policy: ?Record.Policy = null, /// The record that was applied, and where it was found. record: ?Record = null, /// True when SPF passed *and* its domain aligned. spf_aligned: bool = false, /// True when some DKIM signature verified *and* its domain aligned. dkim_aligned: bool = false, /// The share of failing mail the owner asked the policy be applied /// to. A receiver applying a policy to less than all of it is how a /// domain owner rolls one out, and the selection is the receiver's /// to make — this library reports the number rather than rolling a /// die on somebody else's behalf. percent: u8 = 100, }; pub const Error = discover.Error; /// Evaluates a message against the policy of its `From:` domain. /// /// The order is §6.6.2's: find the policy first, because without one /// there is nothing to decide and no reason to spend queries on /// alignment; then look for one aligned identifier; then, if there is /// none, work out which of `p`, `sp` and `np` the domain owner meant. pub fn evaluate( io: Io, resolver: Resolver, message: Message, buffer: []u8, record_storage: []u8, ) Error!Outcome { const found = policyOrTemp(io, resolver, message.from_domain, buffer, record_storage) catch |err| switch (err) { error.Temporary => return .{ .disposition = .temperror }, else => |other| return other, }; const policy_record = found orelse return .{ .disposition = .none }; var outcome: Outcome = .{ .disposition = .fail, .record = policy_record.record, .percent = policy_record.record.percent, }; // §3.1.1: one aligned signature is enough, and a message may carry // several. The DKIM check comes first because it costs nothing when // alignment is strict and because it is the identifier that survives // forwarding. for (message.dkim) |signature| { if (signature.result != .pass) continue; if (try aligned( io, resolver, signature.domain, message.from_domain, policy_record.record.dkim_alignment, )) { outcome.dkim_aligned = true; break; } } if (message.spf) |spf| { if (spf.result == .pass and try aligned( io, resolver, spf.domain, message.from_domain, policy_record.record.spf_alignment, )) { outcome.spf_aligned = true; } } if (outcome.dkim_aligned or outcome.spf_aligned) { outcome.disposition = .pass; return outcome; } // Nothing aligned, so the owner's policy applies — and which of the // three it is depends on where the record was found and whether the // `From:` domain exists. const exists = resolver.exists(io, message.from_domain) catch true; outcome.policy = policy_record.record.policyFor( policy_record.owner, message.from_domain, exists, ); return outcome; } fn policyOrTemp( io: Io, resolver: Resolver, domain: []const u8, buffer: []u8, record_storage: []u8, ) Error!?discover.Found { return discover.policy(io, resolver, domain, buffer, record_storage); } /// Whether `identifier` and `from_domain` are in alignment. /// /// Strict is a string comparison. Relaxed asks whether the two share an /// organizational domain, which is what lets an organization sign with /// `example.com` and write `From: news.example.com` — and what stops a /// signature by `com` from aligning with anything, since `com` is not an /// organizational domain and never resolves to one. pub fn aligned( io: Io, resolver: Resolver, identifier: []const u8, from_domain: []const u8, mode: Record.Alignment, ) Error!bool { const a = std.mem.trimEnd(u8, identifier, "."); const b = std.mem.trimEnd(u8, from_domain, "."); if (a.len == 0 or b.len == 0) return false; // An exact match is alignment in either mode, and answering it here // saves two walks in the case that is by far the commonest. if (std.ascii.eqlIgnoreCase(a, b)) return true; if (mode == .strict) return false; var identifier_buffer: [discover.max_name_len]u8 = undefined; var from_buffer: [discover.max_name_len]u8 = undefined; const identifier_org = try discover.organizationalDomain(io, resolver, a, &identifier_buffer); const from_org = try discover.organizationalDomain(io, resolver, b, &from_buffer); return std.ascii.eqlIgnoreCase(identifier_org, from_org); } const testing = std.testing; const Zone = @import("Zone.zig"); /// One organization that signs and sends from several names, which is /// the shape relaxed alignment exists for. const zone: Zone = .{ .entries = &.{ .{ .name = "_dmarc.example.com", .txt = "v=DMARC1; p=reject; sp=quarantine; np=reject" }, .{ .name = "_dmarc.strict.example.com", .txt = "v=DMARC1; p=reject; adkim=s; aspf=s" }, .{ .name = "_dmarc.watching.example.com", .txt = "v=DMARC1; p=none" }, }, .existing = &.{ "example.com", "news.example.com", "strict.example.com", "watching.example.com", "bounces.example.com", "example.net", }, }; fn check(message: Message) !Outcome { var buffer: [discover.max_name_len]u8 = undefined; var storage: [1024]u8 = undefined; return evaluate(testing.io, zone.resolver(), message, &buffer, &storage); } test "an aligned SPF pass is a DMARC pass" { const outcome = try check(.{ .from_domain = "example.com", .spf = .{ .result = .pass, .domain = "example.com" }, }); try testing.expectEqual(Disposition.pass, outcome.disposition); try testing.expect(outcome.spf_aligned); try testing.expect(!outcome.dkim_aligned); } test "an aligned DKIM signature is a DMARC pass on its own" { const outcome = try check(.{ .from_domain = "example.com", .dkim = &.{.{ .result = .pass, .domain = "example.com" }}, }); try testing.expectEqual(Disposition.pass, outcome.disposition); try testing.expect(outcome.dkim_aligned); } test "relaxed alignment is what lets one organization use several names" { // The case §3.1.2 works through: SPF passes for a bounce domain, and // the message says `From:` the organization's main name. Same // organization, so aligned. const relaxed = try check(.{ .from_domain = "example.com", .spf = .{ .result = .pass, .domain = "bounces.example.com" }, }); try testing.expectEqual(Disposition.pass, relaxed.disposition); try testing.expect(relaxed.spf_aligned); // And §3.1.1's: a signature by the organization, on mail from a // subdomain of it. const signed = try check(.{ .from_domain = "news.example.com", .dkim = &.{.{ .result = .pass, .domain = "example.com" }}, }); try testing.expectEqual(Disposition.pass, signed.disposition); } test "strict alignment is a string comparison and nothing more" { // The same facts as above, against a domain that asked for strict. const outcome = try check(.{ .from_domain = "strict.example.com", .spf = .{ .result = .pass, .domain = "bounces.example.com" }, .dkim = &.{.{ .result = .pass, .domain = "example.com" }}, }); try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expect(!outcome.spf_aligned); try testing.expect(!outcome.dkim_aligned); try testing.expectEqual(Record.Policy.reject, outcome.policy.?); // An exact match still aligns, in either mode. const exact = try check(.{ .from_domain = "strict.example.com", .dkim = &.{.{ .result = .pass, .domain = "strict.example.com" }}, }); try testing.expectEqual(Disposition.pass, exact.disposition); } test "somebody else's valid signature does not authenticate your name" { // The attack DMARC exists to stop, and the reason a valid signature // is not enough on its own: anyone can sign a message with a domain // they control and then write whatever they like in `From:`. const outcome = try check(.{ .from_domain = "example.com", .spf = .{ .result = .pass, .domain = "example.net" }, .dkim = &.{.{ .result = .pass, .domain = "example.net" }}, }); try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expect(!outcome.spf_aligned); try testing.expect(!outcome.dkim_aligned); try testing.expectEqual(Record.Policy.reject, outcome.policy.?); } test "one aligned signature out of several is enough" { // §3.1.1. A message passing through a mailing list collects // signatures, and only one of them has to be the author's. const outcome = try check(.{ .from_domain = "example.com", .dkim = &.{ .{ .result = .pass, .domain = "list.example.net" }, .{ .result = .fail, .domain = "example.com" }, .{ .result = .pass, .domain = "example.com" }, }, }); try testing.expectEqual(Disposition.pass, outcome.disposition); try testing.expect(outcome.dkim_aligned); } test "a signature that did not verify cannot align" { const outcome = try check(.{ .from_domain = "example.com", .dkim = &.{.{ .result = .fail, .domain = "example.com" }}, .spf = .{ .result = .fail, .domain = "example.com" }, }); try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expect(!outcome.dkim_aligned); try testing.expect(!outcome.spf_aligned); } test "which policy applies depends on where the record was found" { // The domain's own record, so `p`. const own = try check(.{ .from_domain = "example.com" }); try testing.expectEqual(Record.Policy.reject, own.policy.?); // A subdomain with no record of its own, so `sp` from the record // the walk reached. const sub = try check(.{ .from_domain = "news.example.com" }); try testing.expectEqual(Record.Policy.quarantine, sub.policy.?); // And a subdomain nobody ever registered, so `np`. const absent = try check(.{ .from_domain = "typo.example.com" }); try testing.expectEqual(Record.Policy.reject, absent.policy.?); } test "no policy is not a policy of none" { const outcome = try check(.{ .from_domain = "nothing.test", .spf = .{ .result = .fail, .domain = "nothing.test" }, }); // A receiver has been told nothing rather than told to do nothing, // and §4.10.1 is explicit that DMARC must not be applied. try testing.expectEqual(Disposition.none, outcome.disposition); try testing.expectEqual(@as(?Record.Policy, null), outcome.policy); } test "a domain watching rather than asking" { const outcome = try check(.{ .from_domain = "watching.example.com", .spf = .{ .result = .fail, .domain = "elsewhere.test" }, }); // It failed, and the owner asked for nothing to be done about it. // The distinction from `none` above is the whole of what `p=none` // is for: reports without consequences. try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expectEqual(Record.Policy.none, outcome.policy.?); } test "a DNS failure is reported rather than decided" { const broken: Zone = .{ .entries = &.{.{ .name = "_dmarc.example.com", .txt = "v=DMARC1; p=reject" }}, .failing = &.{"_dmarc.example.com"}, }; var buffer: [discover.max_name_len]u8 = undefined; var storage: [1024]u8 = undefined; const outcome = try evaluate( testing.io, broken.resolver(), .{ .from_domain = "example.com" }, &buffer, &storage, ); // Delivering or deferring is the receiver's call, so the library // hands back what it knows instead of making it. try testing.expectEqual(Disposition.temperror, outcome.disposition); } test "pct is reported rather than rolled" { const rolling: Zone = .{ .entries = &.{.{ .name = "_dmarc.example.com", .txt = "v=DMARC1; p=reject; pct=10" }}, .existing = &.{"example.com"}, }; var buffer: [discover.max_name_len]u8 = undefined; var storage: [1024]u8 = undefined; const outcome = try evaluate( testing.io, rolling.resolver(), .{ .from_domain = "example.com" }, &buffer, &storage, ); try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expectEqual(Record.Policy.reject, outcome.policy.?); // The selection is the receiver's to make; a library that rolled a // die here would be making a delivery decision on somebody else's // behalf, and an unrepeatable one at that. try testing.expectEqual(@as(u8, 10), outcome.percent); } test "a signature by a public suffix aligns with nothing" { // §3.1.1 calls this out: `d=com` must never align, because `com` is // not an organizational domain. Here the tree walk is what says so, // where RFC 7489 needed a list to. const suffixed: Zone = .{ .entries = &.{ .{ .name = "_dmarc.com", .txt = "v=DMARC1; p=none; psd=y; rua=mailto:a@b.com" }, .{ .name = "_dmarc.example.com", .txt = "v=DMARC1; p=reject" }, }, .existing = &.{"example.com"}, }; var buffer: [discover.max_name_len]u8 = undefined; var storage: [1024]u8 = undefined; const outcome = try evaluate( testing.io, suffixed.resolver(), .{ .from_domain = "example.com", .dkim = &.{.{ .result = .pass, .domain = "com" }}, }, &buffer, &storage, ); try testing.expectEqual(Disposition.fail, outcome.disposition); try testing.expect(!outcome.dkim_aligned); }