// Counts DMARC policies in a snapshot written by dmarc-collect.js. // It reads only the snapshot file, so anyone running it on the same file gets // the same output, line for line. // // Usage: node dmarc-policy-count.js // // Record selection follows RFC 9989 (which obsoletes RFC 7489): TXT records at // _dmarc. that do not begin with "v=DMARC1" are ignored; if more than // one remains, the domain is treated as having no usable DMARC policy. const fs = require('fs'); const crypto = require('crypto'); const file = process.argv[2]; const buf = fs.readFileSync(file); console.log('snapshot sha256', crypto.createHash('sha256').update(buf).digest('hex'), 'bytes', buf.length); const s = JSON.parse(buf.toString('utf8')); console.log('list', s.list.name, '| list sha256', s.list.sha256, '| licence', s.list.licence); console.log('public suffix list sha256', s.publicSuffixList.sha256); console.log('collected', s.collectedFrom, 'to', s.collectedTo, 'via', s.resolver); console.log('list entries read', s.listEntriesRead, '| registrable domains', s.records.length); function classify(r) { if (r.error || (r.status !== 0 && r.status !== 3)) return { cls: 'lookup failed' }; const recs = r.txt.filter((t) => /^v=DMARC1\s*(;|$)/i.test(t.trim())); if (recs.length === 0) return { cls: 'no record' }; if (recs.length > 1) return { cls: 'more than one record (no usable policy)' }; const tags = {}; for (const part of recs[0].split(';')) { const i = part.indexOf('='); if (i > 0) tags[part.slice(0, i).trim().toLowerCase()] = part.slice(i + 1).trim().toLowerCase(); } const p = tags.p; if (!['none', 'quarantine', 'reject'].includes(p)) return { cls: 'p tag missing or invalid', tags }; return { cls: `p=${p}`, tags }; } const ORDER = ['p=reject', 'p=quarantine', 'p=none', 'p tag missing or invalid', 'more than one record (no usable policy)', 'no record', 'lookup failed']; const pct = (n, t) => (t ? ((100 * n) / t).toFixed(1) : '0.0') + '%'; function report(label, recs) { const counts = Object.fromEntries(ORDER.map((k) => [k, 0])); let partial = 0, withRua = 0, spStricter = 0; for (const r of recs) { const c = classify(r); counts[c.cls]++; if (c.tags) { if (c.tags.rua) withRua++; const pctTag = c.tags.pct !== undefined ? Number(c.tags.pct) : 100; if ((c.cls === 'p=reject' || c.cls === 'p=quarantine') && pctTag < 100) partial++; const rank = { none: 0, quarantine: 1, reject: 2 }; if (c.tags.sp in rank && c.tags.p in rank && rank[c.tags.sp] > rank[c.tags.p]) spStricter++; } } const t = recs.length; const published = counts['p=reject'] + counts['p=quarantine'] + counts['p=none'] + counts['p tag missing or invalid']; console.log(`\n== ${label}: ${t} domains ==`); for (const k of ORDER) console.log(`${k.padEnd(42)} ${String(counts[k]).padStart(6)} ${pct(counts[k], t)}`); console.log(`${'one DMARC record published (any p)'.padEnd(42)} ${String(published).padStart(6)} ${pct(published, t)}`); const enforced = counts['p=reject'] + counts['p=quarantine']; console.log(`${'enforcing (quarantine or reject)'.padEnd(42)} ${String(enforced).padStart(6)} ${pct(enforced, t)}`); console.log(`${' of which pct below 100'.padEnd(42)} ${String(partial).padStart(6)}`); console.log(`${'with a rua (aggregate report) address'.padEnd(42)} ${String(withRua).padStart(6)} ${pct(withRua, t)}`); console.log(`${'sp stricter than p'.padEnd(42)} ${String(spStricter).padStart(6)}`); } report('All', s.records); report('Rank band 1 to 1,000', s.records.slice(0, 1000)); report('Rank band 1,001 to 5,000', s.records.slice(1000, 5000)); report('Rank band 5,001 to 10,000', s.records.slice(5000, 10000));