# -*- Perl -*-
#***********************************************************************
#
# mimedefang-filter
#
# Example filter using Mail::MIMEDefang::Async to run DNS blacklist,
# sender-domain, virus, and spam checks in parallel rather than
# sequentially.  It also performs the same authentication checks
# rspamd(8) does by default -- SPF, DKIM, and DMARC (with alignment
# and policy enforcement), plus BIMI -- and publishes the result in
# an Authentication-Results header.
#
# Requires: AnyEvent, AnyEvent::DNS, AnyEvent::Socket, AnyEvent::Handle
#
# Copyright (C) 2026 The McGrail Foundation
#
# This program may be distributed under the terms of the GNU General
# Public License, Version 2.
#
#***********************************************************************

use Mail::MIMEDefang::Async;
use Mail::MIMEDefang::Authres qw(md_authres md_org_domain);
use Mail::MIMEDefang::DKIM;
use Mail::MIMEDefang::Net qw(get_host_name);
use Mail::MIMEDefang::SPF;

#***********************************************************************
# Set administrator's e-mail address here.
#***********************************************************************
$AdminAddress = 'postmaster@localhost';
$AdminName = "MIMEDefang Administrator's Full Name";

#***********************************************************************
# Set the envelope address that quarantine warnings appear to come from.
#***********************************************************************
$DaemonAddress = 'mimedefang@localhost';

$AddWarningsInline = 0;

#***********************************************************************
# $SALocalTestsOnly defaults to 1, which disables SpamAssassin's
# network-dependent tests (Received/RBL/Razor/DNS-based checks such as
# SPF, DKIM, DMARC and URIBL) to avoid adding DNS lookup latency to the
# SMTP transaction.  This filter already runs its own SPF/DKIM/DMARC/BIMI
# checks asynchronously, so set it to 0 to also let SpamAssassin itself
# run its network tests.
#***********************************************************************
$SALocalTestsOnly = 0;

#***********************************************************************
# Points added to the SpamAssassin score in filter_end() when DMARC
# alignment fails, instead of rejecting the message outright here --
# same idea as rspamd's DMARC_POLICY_REJECT/QUARANTINE symbols, which
# feed the final score rather than causing an immediate reject.
#
# If SpamAssassin's own DMARC plugin (Mail::SpamAssassin::Plugin::DMARC)
# is enabled, it already scores these same failures.
#***********************************************************************
$DMARCRejectScore     = 0.5;
$DMARCQuarantineScore = 0.25;

md_graphdefang_log_enable('mail', 1);

# Uncomment to show the same events live in mimedefang-dashboard(1)
# md_graphdefang_feed_enable();

#***********************************************************************
# Initialise the async engine once per worker process.  All filter
# callbacks share this singleton.
#***********************************************************************
md_async_init(
    max_concurrency => 8,
    global_timeout  => 10,
    dns_timeout     => 5,
    socket_timeout  => 5,
);

# Set various stupid things your mail client does below.
$Stupidity{"NoMultipleInlines"} = 0;

detect_and_load_perl_modules();

#***********************************************************************
# filter_relay -- called at SMTP connect time.
#
# Fires four DNS lookups in parallel:
#   - Spamhaus ZEN DNSBL  (hard reject when also listed on SURBL)
#   - SpamCop DNSBL       (scored)
#   - SURBL abuse DNSBL   (hard reject when also listed on Spamhaus ZEN)
#   - Reverse DNS (PTR)   (dynamic/missing PTR logged)
#***********************************************************************
sub filter_relay {
    my($hostip, $hostname, $port, $myport, $ident) = @_;

    my $out = md_async_run_checks([
        md_async_check_dnsbl(ip => $hostip, zone => 'zen.spamhaus.org',  name => 'zen'),
        md_async_check_dnsbl(ip => $hostip, zone => 'bl.spamcop.net',    name => 'spamcop'),
        md_async_check_dnsbl(ip => $hostip, zone => 'abuse.surbl.org',   name => 'surbl'),
        md_async_check_rdns( ip => $hostip,                              name => 'rdns'),
    ]);

    my $r      = $out->{results};
    my $errors = $out->{errors};

    my $zen = md_async_interpret_dnsbl(
        records => $r->{zen},
        zone    => 'zen.spamhaus.org',
        error   => $errors->{zen},
    );

    my $spamcop = md_async_interpret_dnsbl(
        records => $r->{spamcop},
        zone    => 'bl.spamcop.net',
        error   => $errors->{spamcop},
    );
    if ($spamcop->{listed}) {
        md_graphdefang_log('dnsbl', 'bl.spamcop.net', $hostip);
        md_syslog('info', "Relay $hostip listed on SpamCop");
    }

    my $surbl = md_async_interpret_dnsbl(
        records => $r->{surbl},
        zone    => 'abuse.surbl.org',
        error   => $errors->{surbl},
    );

    if ($zen->{listed} && $surbl->{listed}) {
        md_graphdefang_log('dnsbl', 'zen+surbl', $hostip);
        return ('REJECT', "550 5.7.1 $hostip is listed on both Spamhaus ZEN and SURBL");
    }
    if ($zen->{listed}) {
        md_syslog('info', "Relay $hostip listed on Spamhaus ZEN (not in SURBL, continuing)");
    }
    if ($surbl->{listed}) {
        md_syslog('info', "Relay $hostip listed on SURBL (not in Spamhaus ZEN, continuing)");
    }

    my $rdns = md_async_interpret_rdns(
        records => $r->{rdns},
        ip      => $hostip,
        error   => $errors->{rdns},
    );
    if (!$rdns->{has_rdns}) {
        md_syslog('info', "Relay $hostip has no PTR record");
    } elsif ($rdns->{dynamic}) {
        md_syslog('info', "Relay $hostip has dynamic PTR: $rdns->{ptr}");
    }

    return ('CONTINUE', 'ok');
}

#***********************************************************************
# filter_sender -- called when the MAIL FROM: envelope address arrives.
#
# Fires three DNS lookups in parallel for the sender domain:
#   - SPF TXT record existence
#   - MX record existence
#   - DMARC TXT record
#
# Domains with neither SPF nor MX are rejected as likely forgeries.
#***********************************************************************
sub filter_sender {
    my($sender, $ip, $hostname, $helo) = @_;

    (my $addr = $sender) =~ s/^<|>$//g;
    my ($domain) = ($addr =~ /\@([\w.\-]+)/);
    return ('CONTINUE', 'ok') unless defined $domain && length $domain;

    my $out = md_async_run_checks([
        md_async_check_spf_record(  domain => $domain, name => 'spf_txt'),
        md_async_check_mx_exists(   domain => $domain, name => 'mx'),
        md_async_check_dmarc_record(domain => $domain, name => 'dmarc'),
    ]);

    my $r      = $out->{results};
    my $errors = $out->{errors};

    my $spf = md_async_interpret_spf_txt(
        records => $r->{spf_txt},
        error   => $errors->{spf_txt},
        domain  => $domain,
    );

    my $has_mx = defined($r->{mx}) && @{ $r->{mx} // [] };

    if (!$spf->{has_spf} && !$has_mx) {
        md_graphdefang_log('no_spf_no_mx', $domain);
        return ('REJECT', "550 5.7.1 Sender domain $domain has no SPF record and no MX");
    }

    my $dmarc_raw = defined($r->{dmarc}) ? $r->{dmarc}[0] : undef;
    my $dmarc = md_async_interpret_dmarc($dmarc_raw);
    if ($dmarc->{has_dmarc}) {
        md_syslog('info', "DMARC policy for $domain: $dmarc->{policy}");
    }

    #*******************************************************************
    # Real (pass/fail) SPF verification, bare SPF failure is not
    # rejected here, SPF only feeds the DMARC alignment decision made
    # later in filter_begin() once the From: header is available.
    #*******************************************************************
    $Helo        = $helo;
    $SPFMailFrom = $addr;
    $SPFDomain   = $domain;
    $SPFResult   = undef;

    my ($spfcode, $spfexpl) = md_spf_verify($addr, $ip, $helo);
    if (defined $spfcode) {
        $SPFResult = $spfcode;
        md_syslog('info', "SPF result for $addr from $ip: $spfcode") unless $spfcode eq 'none';
    }

    return ('CONTINUE', 'ok');
}

#***********************************************************************
# filter_bad_filename -- helper: true for attachments with dangerous
# extensions (identical to suggested-minimum-filter-for-windows-clients).
#***********************************************************************
sub filter_bad_filename {
    my($entity) = @_;

    my $bad_exts = '(ade|adp|app|asd|asf|asx|bas|bat|chm|cmd|com|cpl|crt|dll|exe|fxp|hlp|hta|hto|inf|ini|ins|isp|jse?|lib|lnk|mdb|mde|msc|msi|msp|mst|ocx|pcd|pif|prg|reg|scr|sct|sh|shb|shs|sys|url|vb|vbe|vbs|vcs|vxd|wmd|wms|wmz|wsc|wsf|wsh|\{[^\}]+\})';
    my $re = '\.' . $bad_exts . '\.*$';

    return 1 if (re_match($entity, $re));

    if (re_match($entity, '\.zip$') && $Features{"Archive::Zip"}) {
        my $bh = $entity->bodyhandle();
        if (defined($bh)) {
            my $path = $bh->path();
            if (defined($path)) {
                return re_match_in_zip_directory($path, $re);
            }
        }
    }
    return 0;
}

#***********************************************************************
# filter_begin -- called just before MIME parts are processed.
#
# Guards suspicious headers, copies the message for scanning, then
# calls clamd asynchronously (non-blocking socket I/O).
#***********************************************************************
sub filter_begin {
    my($entity) = @_;

    if ($SuspiciousCharsInHeaders) {
        md_graphdefang_log('suspicious_chars');
        action_quarantine_entire_message("Message quarantined because of suspicious characters in headers");
        return action_discard();
    }

    #*******************************************************************
    # DKIM verification, DMARC alignment/enforcement, BIMI, and the
    # Authentication-Results header.
    #*******************************************************************
    my $from_header = $entity->head->get('From');
    my ($from_domain) = (defined $from_header ? ($from_header =~ /\@([\w.\-]+)/) : ());

    my ($dkimres, $dkimdom) = md_dkim_verify();

    my $authres = md_authres($SPFMailFrom, $RelayAddr, get_host_name(), $Helo, $from_domain);
    if (defined $authres) {
        action_change_header("Authentication-Results", $authres);
    }

    $DMARCScore = 0;

    if (defined $from_domain && length $from_domain) {
        my $dmarc_record = md_get_dmarc_record($from_domain);
        if (defined $dmarc_record) {
            my $policy = 'none';
            $policy = lc($1) if ($dmarc_record =~ /\bp=(\w+)/);

            # DMARCbis testing mode: the record carries its own "t=y" tag
            # (distinct from the policy itself) to say "don't enforce yet".
            my $testing = ($dmarc_record =~ /\bt=y\b/i) ? 1 : 0;

            my $from_org = md_org_domain($from_domain);
            my $aligned  = 0;
            $aligned = 1 if (defined $dkimres && $dkimres eq 'pass'
                              && defined $dkimdom && md_org_domain($dkimdom) eq $from_org);
            $aligned = 1 if (!$aligned && defined $SPFResult && $SPFResult eq 'pass'
                              && defined $SPFDomain && md_org_domain($SPFDomain) eq $from_org);

            if (!$aligned) {
                if ($testing) {
                    md_graphdefang_log('dmarc_testing', $from_domain);
                    md_syslog('info', "Message from $from_domain failed DMARC alignment, but domain has DMARCbis testing (t=y) enabled -- not enforced");
                } elsif ($policy eq 'reject') {
                    $DMARCScore += $DMARCRejectScore;
                    md_graphdefang_log('dmarc_reject', $from_domain);
                    md_syslog('info', "Message from $from_domain failed DMARC alignment, policy is p=reject -- adding $DMARCRejectScore to spam score");
                } elsif ($policy eq 'quarantine') {
                    $DMARCScore += $DMARCQuarantineScore;
                    md_graphdefang_log('dmarc_quarantine', $from_domain);
                    md_syslog('info', "Message from $from_domain failed DMARC alignment, policy is p=quarantine -- adding $DMARCQuarantineScore to spam score");
                } else {
                    md_graphdefang_log('dmarc_none', $from_domain);
                    md_syslog('info', "Message from $from_domain failed DMARC alignment, policy is p=none (monitor only)");
                }
            }
        }
    }

    md_copy_orig_msg_to_work_dir_as_mbox_file();

    my($code, $category, $action) = md_async_message_contains_virus_clamd();

    $FoundVirus = ($category eq "virus");

    if ($FoundVirus) {
        md_graphdefang_log('virus', $VirusName, $RelayAddr);
        md_syslog('warning', "Discarding because of virus $VirusName");
        return action_discard();
    }

    if ($action eq "tempfail") {
        action_tempfail("Problem running virus-scanner");
        md_syslog('warning', "Problem running virus scanner: code=$code, category=$category, action=$action");
    }
}

#***********************************************************************
# filter -- called once per MIME part.
#***********************************************************************
sub filter {
    my($entity, $fname, $ext, $type) = @_;

    return if message_rejected();

    if (lc($type) eq "message/partial") {
        md_graphdefang_log('message/partial');
        return action_bounce("MIME type message/partial not accepted here");
    }

    if (filter_bad_filename($entity)) {
        md_graphdefang_log('bad_filename', $fname, $type);
        return action_drop_with_warning("An attachment named $fname was removed from this document as it\nconstituted a security hazard.  If you require this document, please contact\nthe sender and arrange an alternate means of receiving it.\n");
    }

    return action_accept();
}

#***********************************************************************
# filter_multipart -- called for multipart container parts.
#***********************************************************************
sub filter_multipart {
    my($entity, $fname, $ext, $type) = @_;

    return if message_rejected();

    if (filter_bad_filename($entity)) {
        md_graphdefang_log('bad_filename', $fname, $type);
        action_notify_administrator("A MULTIPART attachment of type $type, named $fname was dropped.\n");
        return action_drop_with_warning("An attachment of type $type, named $fname was removed from this document as it\nconstituted a security hazard.  If you require this document, please contact\nthe sender and arrange an alternate means of receiving it.\n");
    }

    if (lc($type) eq "message/partial") {
        md_graphdefang_log('message/partial');
        return action_bounce("MIME type message/partial not accepted here");
    }

    return action_accept();
}

#***********************************************************************
# defang_warning -- customise the defang notification message.
#***********************************************************************
sub defang_warning {
    my($oldfname, $fname) = @_;
    return "An attachment named '$oldfname' was converted to '$fname'.\n" .
        "To recover the file, right-click on the attachment and Save As\n" .
        "'$oldfname'\n";
}

#***********************************************************************
# filter_end -- called after all MIME parts have been processed.
#
# Runs the SpamAssassin spamc check via a non-blocking async socket.
# The md_async_spamc_check() call uses the same AnyEvent engine
# initialised at the top of this filter.
#***********************************************************************
sub filter_end {
    my($entity) = @_;

    return if message_rejected();

    if ($Features{"SpamAssassin"}) {
        if (-s "./INPUTMSG" < 512*1024) {
            my($score, $threshold, $report, $isspam) = md_async_spamc_check();
            if (defined $score) {
                if ($DMARCScore) {
                    $score += $DMARCScore;
                    $isspam = 'true' if ($score >= $threshold);
                    $report .= "\nDMARC alignment failure added $DMARCScore points to the score above.\n" if defined $report;
                }

                my $stars = "*" x ($score < 40 ? int($score) : 40);
                my($hits, $req, $names) = ($score, $threshold, '');

                # Reconstruct $names from the report for header compatibility
                if (defined $report && $report =~ /Content analysis details.*\n((?:[ \t]+[\d.]+[ \t]+\S+.*\n)+)/s) {
                    my @rules = ($1 =~ /[ \t][\d.]+[ \t]+(\S+)/g);
                    $names = join(',', @rules);
                }

                if (defined $isspam && $isspam eq 'true') {
                    action_change_header("X-Spam-Score", "$hits ($stars) $names");
                    md_graphdefang_log('spam', $hits, $RelayAddr);
                    action_add_part($entity, "text/plain", "-suggest",
                        "$report\n", "SpamAssassinReport.txt", "inline");
                } else {
                    action_delete_header("X-Spam-Score");
                }
            }
        }
    }

    # # Rspamd async check (uncomment to use instead of / alongside SpamAssassin)
    #
    # my ($hits, $req, $names, $report, $action, $is_spam);
    # if (-s "./INPUTMSG" < 512*1024) {
    #     ($hits, $req, $names, $report, $action, $is_spam) = md_async_rspamd_check();
    #     if ($is_spam eq "true") {
    #         action_change_header("X-Spam-Score", "$hits/$req $names");
    #         md_graphdefang_log('spam', $hits, $RelayAddr);
    #     } else {
    #         action_delete_header("X-Spam-Score");
    #     }
    # }

    md_graphdefang_log('mail_in');
}

# DO NOT delete the next line, or Perl will complain.
1;
