Skip to content

Commit 2d4bd48

Browse files
committed
Postfix filter plugin and postfix server configuration plugin.
1 parent 54fb593 commit 2d4bd48

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class postfix_filter_plugin {
32+
33+
var $plugin_name = 'postfix_filter_plugin';
34+
var $class_name = 'postfix_filter_plugin';
35+
36+
37+
var $postfix_config_dir = '/etc/postfix';
38+
39+
/*
40+
This function is called when the plugin is loaded
41+
*/
42+
43+
function onLoad() {
44+
global $app;
45+
46+
/*
47+
Register for the events
48+
*/
49+
50+
$app->plugins->registerEvent('mail_content_filter_insert','postfix_filter_plugin','insert');
51+
$app->plugins->registerEvent('mail_content_filter_update','postfix_filter_plugin','update');
52+
$app->plugins->registerEvent('mail_content_filter_delete','postfix_filter_plugin','delete');
53+
54+
55+
56+
}
57+
58+
function insert($event_name,$data) {
59+
global $app, $conf;
60+
61+
$this->update($event_name,$data);
62+
63+
}
64+
65+
function update($event_name,$data) {
66+
global $app, $conf;
67+
68+
$type = $data["new"]["type"];
69+
if($type != '') {
70+
$sql = "SELECT * FROM mail_content_filter WHERE server_id = ".intval($conf["server_id"])." AND type = '".$app->db->quote($type)."' AND active = 'y'";
71+
$rules = $app->db->queryAllRecords($sql);
72+
$content = '';
73+
foreach($rules as $rule) {
74+
$content .= $rule["pattern"]."\n";
75+
$content .= " ".$rule["action"]." ".$rule["data"]."\n";
76+
}
77+
78+
if($type == 'header') {
79+
file_put_contents('/etc/postfix/header_checks',$content);
80+
$app->log("Writing /etc/postfix/header_checks",LOGLEVEL_DEBUG);
81+
}
82+
83+
if($type == 'mime_header') {
84+
file_put_contents('/etc/postfix/mime_header_checks',$content);
85+
$app->log("Writing /etc/postfix/mime_header_checks",LOGLEVEL_DEBUG);
86+
}
87+
88+
if($type == 'nested_header') {
89+
file_put_contents('/etc/postfix/nested_header_checks',$content);
90+
$app->log("Writing /etc/postfix/nested_header_checks",LOGLEVEL_DEBUG);
91+
}
92+
93+
if($type == 'body') {
94+
file_put_contents('/etc/postfix/body_checks',$content);
95+
$app->log("Writing /etc/postfix/body_checks",LOGLEVEL_DEBUG);
96+
}
97+
}
98+
99+
$type = $data["old"]["type"];
100+
if($type != '') {
101+
$sql = "SELECT * FROM mail_content_filter WHERE server_id = ".intval($conf["server_id"])." AND type = '".$app->db->quote($type)."' AND active = 'y'";
102+
$rules = $app->db->queryAllRecords($sql);
103+
$content = '';
104+
foreach($rules as $rule) {
105+
$content .= $rule["pattern"]."\n";
106+
$content .= " ".$rule["action"]." ".$rule["data"]."\n";
107+
}
108+
109+
if($type == 'header') {
110+
file_put_contents('/etc/postfix/header_checks',$content);
111+
$app->log("Writing /etc/postfix/header_checks",LOGLEVEL_DEBUG);
112+
}
113+
114+
if($type == 'mime_header') {
115+
file_put_contents('/etc/postfix/mime_header_checks',$content);
116+
$app->log("Writing /etc/postfix/mime_header_checks",LOGLEVEL_DEBUG);
117+
}
118+
119+
if($type == 'nested_header') {
120+
file_put_contents('/etc/postfix/nested_header_checks',$content);
121+
$app->log("Writing /etc/postfix/nested_header_checks",LOGLEVEL_DEBUG);
122+
}
123+
124+
if($type == 'body') {
125+
file_put_contents('/etc/postfix/body_checks',$content);
126+
$app->log("Writing /etc/postfix/body_checks",LOGLEVEL_DEBUG);
127+
}
128+
}
129+
}
130+
131+
function delete($event_name,$data) {
132+
global $app, $conf;
133+
134+
$this->update($event_name,$data);
135+
}
136+
137+
138+
} // end class
139+
140+
?>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class postfix_server_plugin {
32+
33+
var $plugin_name = 'postfix_server_plugin';
34+
var $class_name = 'postfix_server_plugin';
35+
36+
37+
var $postfix_config_dir = '/etc/postfix';
38+
39+
/*
40+
This function is called when the plugin is loaded
41+
*/
42+
43+
function onLoad() {
44+
global $app;
45+
46+
/*
47+
Register for the events
48+
*/
49+
50+
$app->plugins->registerEvent('server_insert','postfix_server_plugin','insert');
51+
$app->plugins->registerEvent('server_update','postfix_server_plugin','update');
52+
53+
54+
55+
}
56+
57+
function insert($event_name,$data) {
58+
global $app, $conf;
59+
60+
$this->update($event_name,$data);
61+
62+
}
63+
64+
// The purpose of this plugin is to rewrite the main.cf file
65+
function update($event_name,$data) {
66+
global $app, $conf;
67+
68+
// get the config
69+
$app->uses("getconf");
70+
$mail_config = $app->getconf->get_server_config($conf["server_id"], 'mail');
71+
72+
copy('/etc/postfix/main.cf','/etc/postfix/main.cf~');
73+
74+
if($mail_config["relayhost"] != '') {
75+
exec("postconf -e 'relayhost = ".$mail_config["relayhost"]."'");
76+
exec("postconf -e 'smtp_sasl_auth_enable = yes'");
77+
exec("postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'");
78+
exec("postconf -e 'smtp_sasl_security_options ='");
79+
80+
// Store the sasl passwd
81+
$content = $mail_config["relayhost"]." ".$mail_config["relayhost_user"].":".$mail_config["relayhost_password"];
82+
file_put_contents('/etc/postfix/sasl_passwd',$content);
83+
exec("chown root:root /etc/postfix/sasl_passwd");
84+
exec("chmod 600 /etc/postfix/sasl_passwd");
85+
exec("postmap /etc/postfix/sasl_passwd");
86+
exec("/etc/init.d/postfix restart");
87+
88+
} else {
89+
exec("postconf -e 'relayhost ='");
90+
}
91+
92+
exec("postconf -e 'mailbox_size_limit = ".intval($mail_config["mailbox_size_limit"])."'");
93+
exec("postconf -e 'message_size_limit = ".intval($mail_config["message_size_limit"])."'");
94+
95+
}
96+
97+
} // end class
98+
99+
?>

0 commit comments

Comments
 (0)