MySQL Multi Policy Server for Postfix

MySQL Multi Policy Server befindet sich noch im BETA Status bzw. Entwicklungsphase.

Download komplett unter → Downloads

Status

  • Postgrey funktioniert soweit schon. (Valid ist noch nicht fertig.)
  • Autoresponder funktioniert.

Folgende Funktionen soll MMPS dann können wenn es fertig ist

  • Postgrey per User / Domain aktivieren / deaktivieren.
  • Delay per User / Domain
  • Valid per User / Domain

Datenbank Layout

CREATE TABLE `autoresponder_mail` (
  `id` int(11) NOT NULL auto_increment,
  `sender` varchar(255) NOT NULL,
  `sender_name` varchar(255) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `von_timestamp` int(11) NOT NULL,
  `bis_timestamp` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `sender` (`sender`)
) TYPE=MyISAM  COMMENT='Autoresponder Tabelle' AUTO_INCREMENT=2 ;

--
-- Daten für Tabelle `autoresponder_mail`
-- 

INSERT INTO `autoresponder_mail` (`id`, `sender`, `sender_name`, `subject`, `message`, `von_timestamp`, `bis_timestamp`) VALUES
(1, ' test@example.com', 'TEST HELIX', 'Bin nicht im Büro', 'Hallo,\r\n\r\nich bin zurzeit nicht im Büro.\r\n\r\nAb 30. Mai 2008 bin ich wieder da.\r\n\r\nGruß\r\n\r\nMaximilian Thoma\r\n\r\näüö?ß', 0, 999999999);

CREATE TABLE `black_mail` (
  `id` int(11) NOT NULL auto_increment,
  `recipient` varchar(255) NOT NULL,
  `sender` varchar(255) NOT NULL,
  `aktiv` int(2) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM COMMENT='Blacklist' AUTO_INCREMENT=1 ;

CREATE TABLE `grey_active` (
  `id` int(11) NOT NULL auto_increment,
  `client_address` varchar(255) NOT NULL,
  `recipient` varchar(255) NOT NULL,
  `sender` varchar(255) NOT NULL,
  `timestamp` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `recipient` (`recipient`,`sender`)
) TYPE=MyISAM  COMMENT='Aktiven Greylistenings' AUTO_INCREMENT=38 ;

CREATE TABLE `grey_mail` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL COMMENT 'Domain oder Mailadresse',
  `delay` int(11) NOT NULL COMMENT 'Verzögerung bis es durch Greylistening freigegeben wird',
  `valid` int(11) NOT NULL COMMENT 'Wie lange gilt die Freigabe in Sekunden',
  PRIMARY KEY  (`id`),
  KEY `name` (`name`)
) TYPE=MyISAM  COMMENT='User/Domains für die Greylistening aktiviert ist' AUTO_INCREMENT=3 ;

--
-- Daten für Tabelle `grey_mail`
-- 

INSERT INTO `grey_mail` (`id`, `name`, `delay`, `valid`) VALUES
(1, ' postmaster@example.com', 60, 3600),
(2, ' test@example.com', 15, 3600);

CREATE TABLE `pps_log` (
  `id` int(11) NOT NULL auto_increment,
  `sender` varchar(255) NOT NULL,
  `recipient` varchar(255) NOT NULL,
  `client_address` varchar(255) NOT NULL,
  `timestamp` int(11) NOT NULL,
  `comment` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM COMMENT='MTHPPS LOG' AUTO_INCREMENT=1 ;

CREATE TABLE `white_mail` (
  `id` int(11) NOT NULL auto_increment,
  `recipient` varchar(255) NOT NULL,
  `sender` varchar(255) NOT NULL,
  `aktiv` int(2) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM COMMENT='Whitelist' AUTO_INCREMENT=1 ;

PHP Script

<?php
 
// MySQL Multi Policy Server V.1.0
// Maximilian Thoma
// http://www.thoma.cc
// info@thoma.cc
//////////////////////////////////////////////////////////////////////////////////////
// OPTIONS
//////////////////////////////////////////////////////////////////////////////////////
 
$mysqlhost="localhost";
$mysqluser="mps";
$mysqlpass="PASSWORD";
$mysqldb="multipolicyserver";
 
 
//////////////////////////////////////////////////////////////////////////////////////
// Ab hier nichts mehr ändern !
//////////////////////////////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////////////////////////////////
// DB Class
//////////////////////////////////////////////////////////////////////////////////////
class db {
 
 var $link_id  = 0;
 var $query_id = 0;
 var $record   = array();
 
 var $errdesc    = "";
 var $errno   = 0;
 var $show_error = 0;
 
 var $server   = "";
 var $user     = "";
 var $password = "";
 var $database = "";
 
 var $appname  = "MySQL Postgrey Policy Server";
 
 function db($server,$user,$password,$database) {
 $this->server=$server;
 $this->user=$user;
 $this->password=$password;
 $this->database=$database;
 $this->connect();
 }
 
 function connect() {
 $this->link_id=mysql_connect($this->server,$this->user,$this->password);
 if (!$this->link_id) $this->print_error("Link-ID == false, connect failed");
 if ($this->database!="") $this->select_db($this->database);
 }
 
 function geterrdesc() {
 $this->error=mysql_error();
 return $this->error;
 }
 
 function geterrno() {
 $this->errno=mysql_errno();
 return $this->errno;
 }
 
function select_db($database="") {
 if ($database!="") $this->database=$database;
 if(!@mysql_select_db($this->database, $this->link_id)) $this->print_error("cannot use database ".$this->database);
 }
 
 function query($query_string) {
 global $query_count;
 //echo $query_string."<br>";
 $query_count++;
 
 $this->query_id = mysql_query($query_string,$this->link_id);
 if (!$this->query_id) $this->print_error("Invalid SQL: ".$query_string);
 return $this->query_id;
 }
 
 function fetch_array($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 $this->record = mysql_fetch_array($this->query_id);
 return $this->record;
 }
 
 function fetch_row($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 $this->record = mysql_fetch_row($this->query_id);
 return $this->record;
 }
 
 function fetch_object($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 $this->record = mysql_fetch_object($this->query_id);
 return $this->record;
 }
 
 
 function free_result($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 return @mysql_free_result($this->query_id);
 }
 
 function query_first($query_string) {
 $this->query($query_string);
 $returnarray=$this->fetch_array($this->query_id);
 $this->free_result($this->$query_id);
 return $returnarray;
 }
 
 function num_rows($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 return mysql_num_rows($this->query_id);
 }
 
 function num_fields($query_id=-1) {
 if ($query_id!=-1) $this->query_id=$query_id;
 return mysql_num_fields($this->query_id);
 }
 
 function field_name($query_id=-1,$num) {
 if ($query_id!=-1) $this->query_id=$query_id;
 return mysql_field_name($this->query_id,$num);
 }
 
 function insert_id() {
 return mysql_insert_id($this->link_id);
 }
 
 function print_error($errormsg) {
 $this->errdesc=mysql_error();
 $this->errno=mysql_errno();
 $errormsg="Database error in $this->appname: $errormsg\n<br>";
 $errormsg.="mysql error: $this->errdesc\n<br>";
 $errormsg.="mysql error number: $this->errno\n<br>";
 $errormsg.="Date: ".date("d.m.Y @ H:i")."\n<br>";
 $errormsg.="Script: ".getenv("REQUEST_URI")."\n<br>";
 $errormsg.="Referer: ".getenv("HTTP_REFERER")."\n<br><br>";
 
 if($this->show_error) $errormsg = "$errormsg";
 else $errormsg = "\n $errormsg \n";
 die("</table>Database Error!\n".$errormsg);
 }
}
//////////////////////////////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////////////////////////////////
// DB INIT
//////////////////////////////////////////////////////////////////////////////////////
$db = new db($mysqlhost,$mysqluser,$mysqlpass,$mysqldb);
 
//////////////////////////////////////////////////////////////////////////////////////
// STDIN
//////////////////////////////////////////////////////////////////////////////////////
if ($fp=fopen("php://stdin","r")) {
 
 while($stop!=1){
 
 $line = fgets($fp,512);
 $teile = explode("=", $line);
 $stdin[$teile[0]]=$teile[1];
 // Entfernen von Leerzeilen und Zeilenumbrüchen
 $stdin[$teile[0]] = preg_replace("/\r|\n/s", "", $stdin[$teile[0]]);
 if($line=="\n"){$stop=1;}
 
 }
fclose($fp);
}
 
//////////////////////////////////////////////////////////////////////////////////////
 
 // LOG
 //$zeit=time();
 //$db->query("INSERT INTO pps_log (zeit, recipient, sender, client_address) VALUES ('$zeit', '$stdin[recipient]', '$stdin[sender]', '$stdin[client_address]')");
 
 // Mailadressen zerlegen
 
 $sender_expl=explode('@',$stdin[sender]);
 $sender_domain=$sender_expl[1];
 
 $recipient_expl=explode('@',$stdin[recipient]);
 
 ////////////////////////////////////////////////////////////////////////////////
/// WHITELIST
////////////////////////////////////////////////////////////////////////////////
 
 
////////////////////////////////////////////////////////////////////////////////
/// BLACKLIST
////////////////////////////////////////////////////////////////////////////////
 
 
 
////////////////////////////////////////////////////////////////////////////////
/// POSTGREYLISTENING
////////////////////////////////////////////////////////////////////////////////
 // Überprüfen ob Domain oder User überhaupt mthpostgrey verwendet ?
 
$grey_req1=$db->query("SELECT * FROM grey_mail WHERE name='$stdin[recipient]'");
$grey_fet=$db->fetch_array($grey_req1);
$grey_sec=$grey_fet[delay];
 
 $user_on=$db->num_rows($grey_req1);
 
 if($user_on==0){
 $action="DUNNO";
 $stdout = fopen('php://stdout', 'w');
 fwrite($stdout,"action=$action\n\n");
 fclose($stdout);
 exit;
 } else {
 $grey_test1_q=$db->query("SELECT * FROM grey_active WHERE sender='$stdin[sender]' AND recipient='$stdin[recipient]' AND client_address='$stdin[client_address]'");
 $grey_test1_r=$db->num_rows($grey_test1_q);
 
 if($grey_test1_r==0){
 $action="defer_if_permit 1 MySQL Multi Policy Server is active. Refer to http://www.thoma.cc/  - Greylisted for: ".$wartezeit1." Seconds.";
 $stdout = fopen('php://stdout', 'w');
 fwrite($stdout,"action=$action\n\n");
 fclose($stdout);
 $a_time=time();
 $db->query("INSERT INTO grey_active (sender, recipient, client_address,timestamp) VALUES ('$stdin[sender]','$stdin[recipient]','$stdin[clie$
 exit;
 } else {
 $grey_test2_q=$db->query("SELECT * FROM grey_active WHERE sender='$stdin[sender]' AND recipient='$stdin[recipient]' AND client_address='$stdin[client_address]'");
 $grey_test3_r=$db->fetch_array($db->query("SELECT * FROM grey_mail WHERE name='$stdin[recipient]'"));
 $delay_time=$grey_test3_r[delay];
 //$valid_time=$grey_test3_r[valid];
 $grey_test2_r=$db->fetch_array($grey_test2_q);
 $first_time=$grey_test2_r[timestamp];
 $b_time=time();
 
 //$time_valid_max=$b_time+$valid_time;
 $time_delay=$b_time+$delay_time;
 // Überprüfen ob Delay Time schon vorbei ist
 if($delay_time<(time()-$first_time)){
 $action="DUNNO";
 $action2="PREPEND X-MTHPPS: Greylistening Filter active.";
 $stdout = fopen('php://stdout', 'w');
 fwrite($stdout,"action=$action2\n\n");
 fwrite($stdout,"action=$action\n\n");
 fclose($stdout);
 } else {
 $wartezeit1=$delay_time-(time()-$first_time);
 $action="defer_if_permit 2 MySQL Multi Policy Server is active. Refer to http://www.thoma.cc/  - Greylisted for: ".$wartezeit1." Seconds.";
 $stdout = fopen('php://stdout', 'w');
 fwrite($stdout,"action=$action\n\n");
 fclose($stdout);
 exit;
 }
 
 }
 
 
 }
 
 
////////////////////////////////////////////////////////////////////////////////
/// AUTORESPONDER
////////////////////////////////////////////////////////////////////////////////
 
 $auto_test1_q=$db->query("SELECT * FROM autoresponder_mail where sender='$stdin[recipient]'");
 $auto_test1_r=$db->num_rows($auto_test1_q);
 
 if($auto_test1_r!=0){
 
 $auto_test1_r2=$db->fetch_array($auto_test1_q);
 
 $empfaenger = $stdin[sender];
 $betreff = $auto_test1_r2[subject];
 $text = $auto_test1_r2[message];
 mail($empfaenger, $betreff, $text, "From: $auto_test1_r2[sender_name] <$auto_test1_r2[sender]>");
 
 }
 
 
 
//////////////////////////////////////////////////////////////////////////////////////
 
//$action="DUNNO";
//$stdout = fopen('php://stdout', 'w');
//fwrite($stdout,"action=$action\n\n");
//fclose($stdout);
 
//////////////////////////////////////////////////////////////////////////////////////
 
 
 
 
?>

Anpassungen main.cf

smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_non_fqdn_recipient,   reject_unauth_destination, reject_unauth_pipelining, reject_invalid_hostname, check_policy_service inet:127.0.0.1:9990

Anpassungen master.cf

127.0.0.1:9990 inet n n n – 0 spawn user=phppostgrey argv=/opt/dev_postgrey/bin/php -f /postgrey/postgrey_v4.php

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

Wir benutzen Cookies um die Nutzerfreundlichkeit der Webseite zu verbessen. Durch Deinen Besuch stimmst Du dem zu.