Dokumentation Postfix/Amavis/Mysql/squirrelmail/policyd der Belug

Dieser Artikel beschreibt unser Mailsystem, eigentlich gedacht als interne Referenz, hoffentlich auch für andere nützlich. Ziel ist ein Mailserver für virtuelle Domains, der komplett über eine Weboberfläche verwaltet wird. Backend zur Datenspeicherung ist Mysql, die Mails liegen im Maildir-Format auf Platte und können über POP, Webmail oder Imap gelesen/abgeholt werden. Eine Nutzerabhängige Spam/Virenfilterung ist implementiert.

Postfix /Mysql/Apache installieren

todo

Postfix konfigurieren

Nachdem Postfix mittels apt-get installiert wurde (Es werden umbedingt die Pakete postfix / postfix-mysql benötigt!) geht es jetzt an die Konfiguration. Die Hauptkonfiguration wird in der Datei /etc/postfix/main.cf gemacht. Zuerst passen wir einige kleine aber dennoch wichtige Variablen an: vi /etc/postfix/main.cf myhostname = der_hostname_des_mailservers mydomain = Die_domain_des_mailservers myorigin = Name der Firma (wenn auf datei /etc/mailname verwiesen wird, dann dort eintragen) mynetworks = 127.0.0.0/8 (legt den Netzbereich fest, für den unser Mailserver zuständig ist.) inet_interfaces = all (an jeder Netzwerkschnittstelle werden Mails angenommen)

Mit diesen einstellungen funktioniert postfix prinzipiell schon einmal. Es gibt noch keine Userverwaltung über mysql, aber der Daemon hat alle wichtigen infos, die zum Starten benötigt werden. Falls es hier bereits fehler gibt, können diese in /var/log/mail.* ausgelesen werden. (mit den anderen Optionen wie z.b. anbindung an mysql mach ich mal weiter, wenn ich mehr zeit hab *gg*) Sebastian Andres

Spamassassin konfigurieren

todo

amavisnewsql squirrelmail plugin

Gibt uns die Möglichkeit, Spamassasin-Regeln nutzerabhängig aufzustellen und eine Serverbasierte Filterung vorzunehmen. Der Spam wird nicht mehr zugestellt, sondern eine Benachrichtigung. Der Nutzer kann im Fall von falsch-Positiven die Nachricht "freigeben", dann wird sie zugestellt. Alternativ wird ***SPAM*** in den Betreff geschrieben. Dafür existiert ein Squirrelmail plugin amavisnewsql . Dieses Plugin nutzt ein MYSQL-Backend, von dem Amavis liest und die Nachrichten dann filtert. Statusberichte werden einmal pro Tag erstellt und per Email zugestellt.

Folgendes muss bereits funktionieren:

Debian Etch, Amavisd-new, Squirrelmail, postfix, mysql, spamassassin

.notstored erstellen

Erstelle ein Verzeichnis .notstored im spamquarantine Verzeichnis. Wenn eine Nachricht nicht zugestellt werden kann, wird Sie in dieses Verzeichnis verschoben und eine Fehlermeldung ins Maillog geschrieben. Dieser Ordner wird dann für unsere Filterung benutzt.

mkdir /var/lib/amavis/virusmails/.notstored
chown amavis:amavis !$

php-db installieren

apt-get install php-db unp  

plugin entpacken

cd /www/webmail/versionen/
unp amavisnewsql-0.8.0-1.4.tar.gz
cp -r ./amavisnewsql ./squirrelmail-1.5.1/plugins/

config.php anpassen Kopiere die config.php.dist nach config.php und ändere einige Sachen:

cd ./squirrelmail-1.5.1/plugins/
mv config.php.dist config.dist.php
vi config.php
ändern:
$CONFIG["dsn"] = 'mysql://amavis:password@localhost/amavis';
$CONFIG["default_domain"] = "belug.de";
$CONFIG["webmail_url"] = "https://belug.de/webmail/";
$CONFIG["digest_subject"] = "Belug Spambenachrichtiguung";
$CONFIG["digest_from"] = '"Belug Spam Quarantine" <unsere@email.de>';

Datenbank erstellen Erzeuge eine Datenbank und Nutzer "amavis" und gib dem Benutzer die Rechte: SELECT INSERT UPDATE DELETE Dann spiele die Datenbank ein:

cd ./utils/
mysql -u root -p amavis < schema.mysql

cleanquarantine.php editieren Folgendes ändern:

cd ../
vim cleanquarantine.php
#!/usr/bin/env php
DEFINE ("BASEINCLUDE", "/www/webmail/versionen/squirrelmail-1.5.1/plugins/amavisnewsql/");

generatedigest.php editieren folgendes ändern:

vim generatedigest.php
#!/usr/bin/env php
DEFINE ("BASEINCLUDE", "/htdocs/squirrel/plugins/amavisnewsql/");
include(BASEINCLUDE."htmlMimeMail/htmlMimeMail.php");<---FEHLER???

process_bsmtp.php editieren:

script and change the BASEINCLUDE setting to match your directory. This script must be located on the same system with your amavis-new install since it picks up quarantined messages and places them in the database.

vi process_bsmtp.php
change -> #!/usr/local/bin/php -q to #!/usr/bin/php
change -> DEFINE ("BASEINCLUDE", "/htdocs/squirrel/plugins/amavisnewsql/");
change -> DEFINE ("QUARANTINEDIR", "/var/virusmails");
change ->  } else if (preg_match("/^X-Spam-Status:.*hits=([0-9]+\.[0-9]+)/", $line, $match)) {
to } else if (preg_match("/^X-Spam-Status:.*score=([0-9]+\.[0-9]+)/", $line, $match)) {

Step ten: Set up cron jobs for cleanquarantine.php, process_bsmtp.php and generatedigest.php (suggested entries listed below)

*/5 * * * * /webmail/plugins/amavisnewsql/utils/process_bsmtp.php
0 15 * * * /webmail/plugins/amavisnewsql/utils/cleanquarantine.php
30 16 * * * /webmail/plugins/amavisnewsql/utils/generatedigest.php

Step eleven: In amavisd.conf change the default sql statement for $sql_select_white_black_list to the following:

vi /etc/amavisd.conf
$sql_select_white_black_list = 'SELECT wb FROM wblist'.' WHERE (rid=?) AND (wblist.email IN (%k))'.' ORDER BY wblist.priority DESC';

Step twelve: Also in amavisd.conf be sure to enable sql lookups with @lookup_sql_dsn to match your database settings.

@lookup_sql_dsn = ( ['DBI:mysql:database=amavis;host=localhost', 'amavis', 'password'] );

Step thirteen: Also in amavisd.conf Use the bsmtp methods:

$virus_quarantine_method = "bsmtp:$QUARANTINEDIR/virus-%i-%n.bsmtp";
$spam_quarantine_method = "bsmtp:$QUARANTINEDIR/spam-%b-%i-%n.bsmtp";
$virus_quarantine_to = 'virus-quarantine';
$final_virus_destiny      = D_DISCARD;
# (defaults to D_DISCARD)
$final_banned_destiny     = D_DISCARD;
# (defaults to D_BOUNCE)
$final_spam_destiny       = D_DISCARD;
# (defaults to D_BOUNCE)
$final_spam_destiny       = D_DISCARD;
$final_bad_header_destiny = D_PASS;
# (defaults to D_PASS), D_BOUNCE suggested

At this point, I figured I was rolling, but I was having some problems. The problem I ran into, Amavis was not quarantining emails. After subscribing to the amavis-users mailing list (https://lists.sourceforge.net/lists/listinfo/amavis-user) and setting high debug levels, we discovered that there were some incorrect values in the 'policy' table for spam_quarantine_to. These values need to be set to 'NULL'. Once this was set, I was seeing quarantined emails. Next, I was noticing bad-header emails being quarantined. To prevent this, add the following to your amavid.conf

$final_bad_header_destiny = D_PASS;
$bad_header_quarantine_method = 'local:badh-%m';
$bad_header_quarantine_to = undef;

I also had an existing system using postfix and postfixadmin to manage my virtmail users. For quarantining to start working they need to be added to the 'users' table in amavis. This occurs automatically upon logging into webmail. But I need to get users in first to start handling their spam. I wrote this simple CLI-PHP script to suck users from my postfix table and insert them into my amavis table using addamavisuser.php script.

vi addusers.php
#!/usr/bin/php
<?
$DBserver       = "localhost";
$DBname         = "postfix";
$DBuser         = "postfixadmin";
$DBpassword     = "password";
// put it all together
if(!($connect = mysql_connect($DBserver, $DBuser, $DBpassword)))
{
echo "Error Connecting to the Database.";
//echo mysql_error(); exit();
}
$DB             = mysql_select_db($DBname);
$sql            = "SELECT * FROM mailbox WHERE username <> ''";
if(!$rs = mysql_query($sql,$connect)){
echo "Database Error";
exit();
}
while($row = mysql_fetch_object($rs)){
$email  = $row->username;
system("./addamavisuser.php $email ' ' $email");
}
mysql_free_result($rs);
?>

chmod 700
addusers.php

./addusers.php I also wanted to make a simple query to see who my SPAM hogs were on the server. I'll probably create a cron job, but this is something that I'll want to run every once and awhile.. not all the time.

mysql -u root -p mysql
> use amavis;
Database changed mysql
> mysql
> SELECT COUNT(msg.id) AS SpamCount, users.username
-> FROM msg
-> INNER JOIN msgowner ON msg.id = msgowner.msgid
-> INNER JOIN users ON msgowner.rid = users.id
-> GROUP BY username
-> ORDER BY SpamCount DESC;

This will give you a nice read out of numbers of spam with each email address that's associated in the quarantine.

'

LutzWillek/postfix-doku (zuletzt geändert am 2007-12-23 22:47:46 durch localhost)