// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A small tool that runs a check against the live DNS. //! //! ``` //! spf check [] //! spf parse //! ``` //! //! It exists because a resolver is worth exactly what a live nameserver //! says it is worth: the unit tests and the conformance suite both read //! a table, which is what makes them fast and complete and is also the //! one thing they cannot check. const std = @import("std"); const Io = std.Io; const z46 = @import("z46"); const spf = @import("spf"); pub fn main(init: std.process.Init) !void { const gpa = init.arena.allocator(); const io = init.io; const args = try init.minimal.args.toSlice(gpa); var out_buffer: [4096]u8 = undefined; var out = Io.File.stdout().writer(io, &out_buffer); const w = &out.interface; defer w.flush() catch {}; if (args.len < 2) return usage(w); if (std.mem.eql(u8, args[1], "check")) { if (args.len < 4) return usage(w); try check(io, w, args[2], args[3], if (args.len > 4) args[4] else ""); } else if (std.mem.eql(u8, args[1], "txt")) { if (args.len < 3) return usage(w); try txt(io, w, args[2]); } else if (std.mem.eql(u8, args[1], "parse")) { if (args.len < 3) return usage(w); try parse(w, args[2]); } else { try usage(w); } } fn usage(w: *Io.Writer) !void { try w.writeAll( \\usage: \\ spf check [] ask the DNS whether a host may send \\ spf txt what the resolver reads out of the DNS \\ spf parse take a record apart without asking anything \\ ); } fn check(io: Io, w: *Io.Writer, host: []const u8, helo: []const u8, from: []const u8) !void { const client = z46.Address.parse(host) catch { try w.print("not an address: {s}\n", .{host}); return; }; const stub: spf.StubResolver = try .init(io); var explanation: [512]u8 = undefined; const outcome = spf.checkMailFrom(io, stub.resolver(), client, helo, from, .{ .explanation = &explanation, .receiver = "spf-cli", }); try w.print("{t}\n", .{outcome.result}); if (outcome.explanation) |text| try w.print(" explanation {s}\n", .{text}); // The header field a receiver would write, which is the whole answer // in the form anything downstream reads it in. const received: spf.Received = .{ .result = outcome.result, .receiver = "spf-cli", .identity = if (from.len == 0) .helo else .mailfrom, .client_ip = client, .envelope_from = if (from.len == 0) null else from, .helo = helo, }; try w.print("\n{f}", .{received}); } /// What the resolver actually reads, which is the first thing to look /// at when a check answers something surprising. fn txt(io: Io, w: *Io.Writer, name: []const u8) !void { const stub: spf.StubResolver = try .init(io); var storage: [8192]u8 = undefined; var index: [64][]const u8 = undefined; var strings: spf.Resolver.Strings = .init(&storage, &index); stub.resolver().txt(io, name, &strings) catch |err| { try w.print("{t}\n", .{err}); return; }; for (strings.slice()) |record| { try w.print(" {s}{s}\n", .{ if (spf.Record.isSpf(record)) "* " else " ", record, }); } try w.print(" ({d} records)\n", .{strings.len}); } fn parse(w: *Io.Writer, text: []const u8) !void { const record = spf.Record.parse(text) catch |err| { try w.print("not a usable record: {t}\n", .{err}); return; }; var terms = record.iterate(); while (try terms.next()) |term| switch (term) { .directive => |directive| { try w.print(" {t} {t}", .{ directive.qualifier, directive.mechanism }); switch (directive.mechanism) { .all => {}, .include, .exists => |domain| try w.print(" {s}", .{domain}), .ptr => |domain| if (domain) |name| try w.print(" {s}", .{name}), .a, .mx => |dual| { if (dual.domain) |name| try w.print(" {s}", .{name}); try w.print(" /{d}//{d}", .{ dual.ip4_prefix, dual.ip6_prefix }); }, .ip4 => |network| try w.print(" {f}", .{network}), .ip6 => |network| try w.print(" {f}", .{network}), } try w.writeAll("\n"); }, .modifier => |modifier| try w.print( " modifier {s}={s}\n", .{ modifier.name, modifier.value }, ), }; if (record.redirect) |target| { try w.print(" redirect to {s}{s}\n", .{ target, // ยง6.1, and the mistake worth reporting: a record with an // `all` can never reach its redirect, because `all` always // matches. if (record.has_all) " (dead: the record has an `all`)" else "", }); } if (record.exp) |target| try w.print(" explanation from {s}\n", .{target}); }