Something went wrong. Try again.
Sender Policy Framework (RFC 7208) for Zig: the record grammar, the macro language, and an evaluator that takes its resolver from you.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A `Resolver` over a table of records.//!//! This is the reason the evaluator takes its resolver rather than//! owning one. RFC 7208's own conformance suite is written as zone data//! and expected results, so a resolver that reads a table runs the whole//! of it with no network, no nameserver and no virtual machine — and a//! test can then say "this domain publishes this, that host connects,//! the answer must be `fail`" in the same shape the specification uses.//!//! It is public because the same thing is useful outside this library://! a mail server can check its own policy, and everything an//! `include:` chain reaches, against a table it writes rather than//! against the live DNS.
const std = @import("std");const Io = std.Io;const z46 = @import("z46");const Resolver = @import("Resolver.zig");
const Zone = @This();
entries: []const Entry = &.{},/// Names that answer with a temporary failure whatever is asked of them,/// for the `temperror` paths — the ones that are otherwise unreachable/// without unplugging something.failing: []const []const u8 = &.{},
pub const Entry = struct { name: []const u8, data: Data,};
pub const Data = union(enum) { /// One TXT record, already joined from the character-strings it /// would have been published in. txt: []const u8, /// An A record, written as text. a: []const u8, /// An AAAA record, written as text. aaaa: []const u8, /// One exchanger. Preference is not recorded because SPF never asks /// for it: `mx` is a membership test, not a routing decision. mx: []const u8, /// One name a reverse lookup answers with. ptr: []const u8, /// An alias. A real resolver follows one without being asked and /// returns the records of the name at the end, so this one does too /// — an implementation that did not would see an aliased domain as /// having no policy at all. cname: []const u8,};
/// How many aliases deep to follow. A chain longer than this, or one/// that loops, answers as though the name did not exist — which is what/// RFC 7208 §5.5 asks for anyway: a DNS error during a lookup means the/// name is skipped, not that the check fails.const max_aliases = 8;
/// Names are compared without case and without a trailing dot, because/// `example.com` and `example.com.` are the same name and a record may/// be written either way. The suite this reads has both.fn sameName(a: []const u8, b: []const u8) bool { return std.ascii.eqlIgnoreCase( std.mem.trimEnd(u8, a, "."), std.mem.trimEnd(u8, b, "."), );}
/// The name an alias chain ends at, or null if it loops or runs too deep.fn canonical(zone: *const Zone, name: []const u8) ?[]const u8 { var current = name; var hops: usize = 0; while (hops <= max_aliases) : (hops += 1) { var alias: ?[]const u8 = null; var other = false; for (zone.entries) |entry| { if (!sameName(entry.name, current)) continue; if (entry.data == .cname) { alias = entry.data.cname; } else { other = true; } } // Records of its own win: a name should not have both, and if it // does, what is there is more useful than where it points. if (other) return current; current = alias orelse return current; } return null;}
pub fn resolver(zone: *const Zone) Resolver { return .{ .context = @constCast(zone), .vtable = &.{ .txt = txt, .addresses = addresses, .mx = mx, .ptr = ptr, } };}
fn fails(zone: *const Zone, name: []const u8) bool { for (zone.failing) |failing| { if (sameName(failing, name)) return true; } return false;}
/// Collects every record of one shape for one name, and reports the/// absence of any as `NotFound` — which is how a nameserver answers both/// "no such name" and "no such record", and how SPF reads both.fn collect( zone: *const Zone, name: []const u8, out: *Resolver.Strings, comptime tag: std.meta.Tag(Data),) Resolver.Error!void { if (zone.fails(name)) return error.Temporary; const target = zone.canonical(name) orelse return error.NotFound; if (zone.fails(target)) return error.Temporary; var found = false; var full = false; for (zone.entries) |entry| { if (!sameName(entry.name, target)) continue; if (entry.data != tag) continue; found = true; out.append(@field(entry.data, @tagName(tag))) catch { // Whatever fit stays: §4.6.4 asks for the eleventh name to // be noticed, and for a `ptr` the first ten to be used. full = true; }; } if (full) return error.NoSpace; if (!found) return error.NotFound;}
fn txt(context: ?*anyopaque, io: Io, name: []const u8, out: *Resolver.Strings) Resolver.Error!void { _ = io; const zone: *const Zone = @ptrCast(@alignCast(context.?)); return zone.collect(name, out, .txt);}
fn mx(context: ?*anyopaque, io: Io, name: []const u8, out: *Resolver.Strings) Resolver.Error!void { _ = io; const zone: *const Zone = @ptrCast(@alignCast(context.?)); return zone.collect(name, out, .mx);}
fn ptr(context: ?*anyopaque, io: Io, name: []const u8, out: *Resolver.Strings) Resolver.Error!void { _ = io; const zone: *const Zone = @ptrCast(@alignCast(context.?)); return zone.collect(name, out, .ptr);}
fn addresses( context: ?*anyopaque, io: Io, name: []const u8, family: z46.Family, out: *Resolver.Addresses,) Resolver.Error!void { _ = io; const zone: *const Zone = @ptrCast(@alignCast(context.?)); if (zone.fails(name)) return error.Temporary; const target = zone.canonical(name) orelse return error.NotFound; if (zone.fails(target)) return error.Temporary;
var found = false; var full = false; for (zone.entries) |entry| { if (!sameName(entry.name, target)) continue; const text = switch (entry.data) { .a => |text| if (family == .ipv4) text else continue, .aaaa => |text| if (family == .ipv6) text else continue, else => continue, }; found = true; const address = z46.Address.parse(text) catch continue; out.append(address) catch { full = true; }; } if (full) return error.NoSpace; if (!found) return error.NotFound;}