0

I'm trying to install MIMEDefang on my Debian Stretch but it doesn't work out the of box and I can't find any guides that work.

After apt install mimedefang I added the following to /etc/postfix/main.cf:

smtpd_milters = unix:/var/spool/MIMEDefang/mimedefang.sock
milter_default_action = accept

Reloaded postfix, and service mimedefang status says it is active and running.

Even tried copying /etc/mimedefang-filter to /etc/mail/mimedefang-filter and made it executable...

But still I just get this in /var/log/mail.log:

postfix/smtpd[29832]: warning: connect to Milter service unix:/var/spool/MIMEDefang/mimedefang.sock: No such file or directory

The file /var/spool/MIMEDefang/mimedefang.sock exists though.

sendmail is already installed from before.

How do I install and activate this thing?

  • Please expand your question to explain how mimedefang is running, what you did to make it running. Any error logs? – Rui F Ribeiro Oct 25 '18 at 7:44
  • I feel I have done that already? – TheStoryCoder Nov 6 '18 at 3:20
0

With the kind help of Benoît Panizzon on the MIMEDefang mailing list I found out that I need to use inet instead of unix as the listening socket, so that it's listening on the TCP port on the specified local or remote host. So the setup procedure for MIMEDefang on Debian/Ubuntu is:

  1. Install MIMEDefang: apt install mimedefang
  2. In /etc/default/mimedefang set (use other port if you want): SOCKET=inet:33333
  3. Add to /etc/postfic/main.cf: smtpd_milters = inet:localhost:33333 milter_default_action = accept
  4. Copy /etc/mimedefang-filter to /etc/mail/mimedefang-filter, and modify it according to your needs (don't need to make it executable).
  5. Reload Postfix and MIMEDefang services: sudo systemctl reload postfix mimedefang

MIMEDefang must be reloaded every time you change mimedefang-filter.


By the way, this presentation gives a good understanding of MIMEDefang: https://www.mimedefang.org/static/mimedefang-lisa04.pdf

The actual Perl script being run is located in /usr/bin/mimedefang.pl which then includes mimedefang-filter.


I also wanted to be able to write my filtering logic in PHP instead of Perl, so I came up with the following solution. Add this to the end of eg. the filter_begin function in mimedefang-filter:

%passToPhp = ("Sender", $Sender, "Recipients", \@Recipients, "Subject", $Subject, "RelayAddr", $RelayAddr, "RelayHostname", $RelayHostname, "Helo", $Helo, "QueueID", $QueueID, "MessageID", $MessageID);

my $cmd = "/path/to/your/email-filter.php";
$cmd .= " " . encode_base64(encode_json(\%passToPhp), '');
my $phpOutput = `$cmd`;

md_syslog('info', "PHP filter output: $phpOutput");  #causes entry in /var/log/mail.log
if ($phpOutput eq "bounce") {
    action_bounce("We dont want this particular message.");
} elsif ($phpOutput eq "discard") {
    action_discard();
}

Then use the following code in email-filter.php to get you started:

#!/usr/bin/php
<?php
// runs as user "defang". This file must have execute permissions.

// Get variables from mimedefang that we passed along, headers, raw message, and extra information from mimedefang
$arguments = ($argv[1] ? base64_decode($argv[1]) : null);
if ($arguments) $arguments = json_decode($arguments, true);
$headers = file_get_contents('HEADERS');
$raw_message = file_get_contents('INPUTMSG');
$commands = file_get_contents('COMMANDS');

// Get all the MIME parts into an array
$mimeparts = [];
chdir('./Work');
foreach (glob('*') as $mimepart_file) {
    if (is_dir($file)) continue;
    $mimeparts[$mimepart_file] = file_get_contents($mimepart_file);
}

// Do all your logic here...

if ($someLogic == 'spam') {
    echo 'discard';
    // echo 'bounce';  //use this line if you want to have a bounce message sent back to sender (but you probably don't want that for spam)
}

Monitor /var/log/mail.log for ensuring everything works as it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.