# /// script
# requires-python = ">=3.12"
# dependencies = ["dnspython"]
# ///
"""Can email in a site's name be faked? Read each domain's public DMARC, SPF and MX
records through two independent public resolvers and sort the DMARC policy.

A lookup that fails (timeout, SERVFAIL) is an error, never "no record": only an
answer of NXDOMAIN or no-data counts as missing.

Usage: uv run 07_email.py <csv with a "domain" column> <new out.jsonl> [--sample N]
"""

import asyncio
import csv
import json
import random
import re
import sys
from datetime import datetime, timezone
from pathlib import Path

import dns.asyncresolver
import dns.exception
import dns.resolver

RESOLVERS = {"cloudflare": "1.1.1.1", "google": "8.8.8.8"}
TRIES = 3
POLICIES = ("none", "quarantine", "reject")


async def lookup(resolver, name: str, rtype: str) -> dict:
    """{'status': 'ok'|'missing'|'error', 'records': [...]}"""
    last = ""
    for attempt in range(TRIES):
        try:
            answers = await resolver.resolve(name, rtype)
            if rtype == "TXT":
                records = ["".join(s.decode("utf-8", "replace") for s in a.strings) for a in answers]
            else:
                records = [f"{a.preference} {a.exchange}" for a in answers]
            return {"status": "ok", "records": sorted(records)}
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            return {"status": "missing", "records": []}
        except (dns.exception.DNSException, OSError) as e:
            last = type(e).__name__
            await asyncio.sleep(1 + attempt)
    return {"status": "error", "records": [], "error": last}


def tags(record: str) -> dict:
    out = {}
    for part in record.split(";"):
        if "=" in part:
            k, v = part.split("=", 1)
            out.setdefault(k.strip().lower(), v.strip())
    return out


def sort_dmarc(txt: dict) -> dict:
    """The policy a receiving mail server would apply (RFC 7489, sections 6.3 and 6.6.3)."""
    if txt["status"] == "error":
        return {"policy": "error"}
    # The version is matched to the letter; tag names and values in any case
    records = [t for t in txt["records"] if re.match(r"\s*[vV]\s*=\s*DMARC1\s*(;|$)", t)]
    if not records:
        return {"policy": "missing"}
    if len(records) > 1:
        return {"policy": "invalid", "why": "more than one DMARC record"}
    t = tags(records[0])
    p, sp = t.get("p", "").lower(), t["sp"].lower() if "sp" in t else None
    if p not in POLICIES:
        return {"policy": "invalid", "why": f"p={p or 'absent'}"}
    if sp is not None and sp not in POLICIES:
        return {"policy": "invalid", "why": f"sp={sp}"}
    # A pct that is not 0 to 100 is ignored, which leaves the default: all
    pct = int(t["pct"]) if re.fullmatch(r"[0-9]{1,3}", t.get("pct", "")) and int(t["pct"]) <= 100 else 100
    # Reports go to a mailto: address; anything else reaches nobody
    reports = bool(re.search(r"(^|,)\s*mailto:[^\s,@!]+@[^\s,@!]+(![0-9]+[kmgt]?)?\s*(,|$)", t.get("rua", ""), re.I))
    return {"policy": p, "pct": pct, "sp": sp, "reports": reports}


def sort_spf(txt: dict) -> str:
    if txt["status"] == "error":
        return "error"
    records = [t for t in txt["records"] if re.match(r"v=spf1(\s|$)", t, re.I)]
    if not records:
        return "missing"
    if len(records) > 1:
        return "invalid"
    m = re.search(r"(?:^|\s)([-~?+])all\s*$", records[0].strip(), re.I)
    if "redirect=" in records[0].lower() and not m:
        return "redirect"
    return {"-": "hard fail", "~": "soft fail", "?": "neutral", "+": "pass all"}[m.group(1)] if m else "no all"


async def check(resolvers: dict, domain: str, sem: asyncio.Semaphore) -> dict:
    async with sem:
        out: dict = {"domain": domain, "checked": datetime.now(timezone.utc).isoformat(timespec="seconds")}
        for label, r in resolvers.items():
            dmarc_txt, spf_txt, mx = await asyncio.gather(
                lookup(r, f"_dmarc.{domain}", "TXT"), lookup(r, domain, "TXT"), lookup(r, domain, "MX")
            )
            out[label] = {
                "dmarc": sort_dmarc(dmarc_txt),
                "dmarc_raw": [t for t in dmarc_txt["records"] if "dmarc" in t.lower()],
                "spf": sort_spf(spf_txt),
                "spf_raw": [t for t in spf_txt["records"] if t.lower().startswith("v=spf1")],
                "mx": "error" if mx["status"] == "error" else "null" if mx["records"] == ["0 ."] else "yes" if mx["records"] else "none",
            }
        a, b = (out[k] for k in resolvers)
        out["agree"] = all(a[k] == b[k] for k in ("dmarc", "spf", "mx"))
        return out


async def main() -> None:
    src, dst = Path(sys.argv[1]), Path(sys.argv[2])
    domains = [r["domain"] for r in csv.DictReader(src.open())]
    if "--sample" in sys.argv:
        random.seed(7)
        domains = random.sample(domains, int(sys.argv[sys.argv.index("--sample") + 1]))
    resolvers = {}
    for label, ip in RESOLVERS.items():
        r = dns.asyncresolver.Resolver(configure=False)
        r.nameservers, r.timeout, r.lifetime = [ip], 4, 8
        resolvers[label] = r
    sem = asyncio.Semaphore(40)
    # A pass is one file, written whole: an existing one is never added to
    with dst.open("x") as f:
        for i, task in enumerate(asyncio.as_completed([check(resolvers, d, sem) for d in domains]), 1):
            f.write(json.dumps(await task, ensure_ascii=False) + "\n")
            if i % 500 == 0:
                print(i, "of", len(domains), flush=True)
    print("done:", len(domains), "checked")


asyncio.run(main())
