Skip to content

Commit 9a33517

Browse files
committed
always keep spamfilter_users entries: #6201
1 parent 20c5d50 commit 9a33517

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+561
-337
lines changed

install/dist/tpl/gentoo/amavisd-ispconfig.conf.master

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ use strict;
5151
$sql_select_policy =
5252
'SELECT *,spamfilter_users.id'.
5353
' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'.
54-
' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC';
54+
' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'.
55+
' ORDER BY spamfilter_users.priority DESC';
5556

5657

5758
$sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- default spamfilter_users.policy_id to 0
2+
ALTER TABLE `spamfilter_users` ALTER `policy_id` SET DEFAULT 0;

install/sql/ispconfig3.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ CREATE TABLE `spamfilter_users` (
15451545
`sys_perm_other` varchar(5) NOT NULL DEFAULT '',
15461546
`server_id` int(11) unsigned NOT NULL DEFAULT '0',
15471547
`priority` tinyint(3) unsigned NOT NULL default '7',
1548-
`policy_id` int(11) unsigned NOT NULL default '1',
1548+
`policy_id` int(11) unsigned NOT NULL default '0',
15491549
`email` varchar(255) NOT NULL DEFAULT '',
15501550
`fullname` varchar(64) default NULL,
15511551
`local` varchar(1) default NULL,

install/tpl/amavisd_user_config.master

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ use strict;
3333
$sql_select_policy =
3434
'SELECT *,spamfilter_users.id'.
3535
' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'.
36-
' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC';
36+
' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'.
37+
' ORDER BY spamfilter_users.priority DESC';
3738

3839

3940
$sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'.

install/tpl/fedora_amavisd_conf.master

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,8 @@ $banned_filename_re = new_RE(
751751
$sql_select_policy =
752752
'SELECT *,spamfilter_users.id'.
753753
' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'.
754-
' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC';
754+
' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'.
755+
' ORDER BY spamfilter_users.priority DESC';
755756

756757

757758
$sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'.

install/tpl/opensuse_amavisd_conf.master

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ $banned_filename_re = new_RE(
746746
$sql_select_policy =
747747
'SELECT *,spamfilter_users.id'.
748748
' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'.
749-
' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC';
749+
' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'.
750+
' ORDER BY spamfilter_users.priority DESC';
750751

751752

752753
$sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'.

interface/lib/classes/remote.d/client.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function client_delete_everything($session_id, $client_id)
413413
$app->db->query("DELETE FROM sys_user WHERE client_id = ?", $client_id);
414414

415415
//* Delete all records (sub-clients, mail, web, etc....) of this client.
416-
$tables = 'cron,dns_rr,dns_soa,dns_slave,ftp_user,mail_access,mail_content_filter,mail_domain,mail_forwarding,mail_get,mail_user,mail_user_filter,shell_user,spamfilter_users,support_message,web_database,web_database_user,web_domain,web_traffic,domain,mail_mailinglist,client';
416+
$tables = 'cron,dns_rr,dns_soa,dns_slave,ftp_user,mail_access,mail_content_filter,mail_domain,mail_forwarding,mail_get,mail_user,mail_user_filter,shell_user,spamfilter_users,support_message,web_database,web_database_user,web_domain,web_traffic,domain,mail_mailinglist,client,spamfilter_wblist';
417417
$tables_array = explode(',', $tables);
418418
$client_group_id = $app->functions->intval($client_group['groupid']);
419419
if($client_group_id > 1) {

interface/lib/classes/remote.d/mail.inc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ public function mail_spamfilter_whitelist_get($session_id, $primary_id)
663663
return $app->remoting_lib->getDataRecord($primary_id);
664664
}
665665

666-
//* biała lista e-mail
666+
//* add spamfilter whitelist entry
667667
public function mail_spamfilter_whitelist_add($session_id, $client_id, $params)
668668
{
669669
if (!$this->checkPerm($session_id, 'mail_spamfilter_whitelist_add'))
@@ -763,7 +763,7 @@ public function mail_spamfilter_user_get($session_id, $primary_id)
763763
return $app->remoting_lib->getDataRecord($primary_id);
764764
}
765765

766-
//* filtr spamu użytkowników e-mail
766+
//* Add new spamfilter_users
767767
public function mail_spamfilter_user_add($session_id, $client_id, $params)
768768
{
769769
if (!$this->checkPerm($session_id, 'mail_spamfilter_user_add'))

interface/lib/plugins/mail_mail_domain_plugin.inc.php

Lines changed: 224 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ class mail_mail_domain_plugin {
1313

1414
/*
1515
This function is called when the plugin is loaded
16-
*/
16+
*/
1717
function onLoad() {
1818
global $app;
19+
1920
//Register for the events
2021
$app->plugin->registerEvent('mail:mail_domain:on_after_insert', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
2122
$app->plugin->registerEvent('mail:mail_domain:on_after_update', 'mail_mail_domain_plugin', 'mail_mail_domain_edit');
2223
}
2324

2425
/*
2526
Function to create the sites_web_domain rule and insert it into the custom rules
26-
*/
27+
*/
2728
function mail_mail_domain_edit($event_name, $page_form) {
2829
global $app, $conf;
2930

3031
$domain = $app->functions->idn_encode($page_form->dataRecord['domain']);
3132

32-
// make sure that the record belongs to the client group and not the admin group when a dmin inserts it
33+
// make sure that the record belongs to the client group and not the admin group when admin inserts it
3334
// also make sure that the user can not delete entry created by an admin
3435
if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) {
3536
$client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]);
@@ -64,6 +65,8 @@ function mail_mail_domain_edit($event_name, $page_form) {
6465
$app->uses('getconf');
6566
$mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail');
6667

68+
$old_domain = $app->functions->idn_encode($page_form->oldDataRecord['domain']);
69+
6770
//* Update the mailboxes
6871
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", "%@" . $page_form->oldDataRecord['domain']);
6972
$sys_groupid = $app->functions->intval((isset($page_form->dataRecord['client_group_id']))?$page_form->dataRecord['client_group_id']:$page_form->oldDataRecord['sys_groupid']);
@@ -76,25 +79,164 @@ function mail_mail_domain_edit($event_name, $page_form) {
7679
$maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]);
7780
$maildir = str_replace("[localpart]", $mail_parts[0], $maildir);
7881
$email = $mail_parts[0].'@'.$domain;
82+
83+
// update spamfilter_users and spamfilter_wblist if email change
84+
$skip_spamfilter_users_update = false;
85+
if($email != $rec['email']) {
86+
$tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['email']);
87+
if($tmp_olduser['id'] > 0) {
88+
$tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email);
89+
if($tmp_newuser['id'] > 0) {
90+
// There is a spamfilter_users for both old and new email, we'll update old wblist entries
91+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
92+
foreach ($tmp_wblist as $tmp) {
93+
$update_data = array(
94+
'rid' => $tmp_newuser['id'],
95+
'sys_userid' => $client_user_id,
96+
'sys_groupid' => $sys_groupid,
97+
);
98+
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
99+
}
100+
101+
// now delete old spamfilter_users entry
102+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']);
103+
} else {
104+
$update_data = array(
105+
'email' => $email,
106+
'sys_userid' => $client_user_id,
107+
'sys_groupid' => $sys_groupid,
108+
);
109+
if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['email'])) {
110+
$update_data['fullname'] = $app->functions->idn_decode($email);
111+
}
112+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']);
113+
$skip_spamfilter_users_update = true;
114+
115+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
116+
$update_data = array(
117+
'sys_userid' => $client_user_id,
118+
'sys_groupid' => $sys_groupid,
119+
);
120+
foreach ($tmp_wblist as $tmp) {
121+
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
122+
}
123+
}
124+
}
125+
126+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email);
127+
if($tmp_user["id"] > 0) {
128+
// There is already a record that we will update
129+
if(!$skip_spamfilter_users_update) {
130+
$update_data = array(
131+
'sys_userid' => $client_user_id,
132+
'sys_groupid' => $sys_groupid,
133+
);
134+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']);
135+
}
136+
} else {
137+
// We create a new record
138+
$insert_data = array(
139+
"sys_userid" => $client_user_id,
140+
"sys_groupid" => $sys_groupid,
141+
"sys_perm_user" => 'riud',
142+
"sys_perm_group" => 'riud',
143+
"sys_perm_other" => '',
144+
"server_id" => $page_form->dataRecord["server_id"],
145+
"priority" => 5,
146+
"policy_id" => 0,
147+
"email" => $email,
148+
"fullname" => $app->functions->idn_decode($email),
149+
"local" => 'Y'
150+
);
151+
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
152+
}
153+
}
154+
79155
$app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']);
156+
80157
}
81158
}
82159

83160
//* Update the aliases
84-
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $page_form->oldDataRecord['domain'], "%@" . $page_form->oldDataRecord['domain']);
161+
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $old_domain, '%@' . $old_domain);
85162
if(is_array($forwardings)) {
86163
foreach($forwardings as $rec) {
87-
$destination = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['destination']);
88-
$source = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['source']);
164+
$destination = str_replace($old_domain, $domain, $rec['destination']);
165+
$source = str_replace($old_domain, $domain, $rec['source']);
166+
167+
// update spamfilter_users and spamfilter_wblist if source email changes
168+
$skip_spamfilter_users_update = false;
169+
if(strpos($rec['source'],'@'.$old_domain) && $source != $rec['source']) {
170+
$tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['source']);
171+
if($tmp_olduser['id'] > 0) {
172+
$tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source);
173+
if($tmp_newuser['id'] > 0) {
174+
// There is a spamfilter_users for both old and new email, we'll update old wblist entries
175+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
176+
foreach ($tmp_wblist as $tmp) {
177+
$update_data = array(
178+
'rid' => $tmp_newuser['id'],
179+
'sys_userid' => $client_user_id,
180+
'sys_groupid' => $sys_groupid,
181+
);
182+
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
183+
}
184+
185+
// now delete old spamfilter_users entry
186+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']);
187+
} else {
188+
$update_data = array(
189+
'email' => $source,
190+
'sys_userid' => $client_user_id,
191+
'sys_groupid' => $sys_groupid,
192+
);
193+
if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['source'])) {
194+
$update_data['fullname'] = $app->functions->idn_decode($source);
195+
}
196+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']);
197+
$skip_spamfilter_users_update = true;
198+
199+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']);
200+
$update_data = array(
201+
'sys_userid' => $client_user_id,
202+
'sys_groupid' => $sys_groupid,
203+
);
204+
foreach ($tmp_wblist as $tmp) {
205+
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
206+
}
207+
}
208+
}
209+
210+
211+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source);
212+
if($tmp_user["id"] > 0) {
213+
// There is already a record that we will update
214+
if(!$skip_spamfilter_users_update) {
215+
$update_data = array(
216+
'sys_userid' => $client_user_id,
217+
'sys_groupid' => $sys_groupid,
218+
);
219+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']);
220+
}
221+
}
222+
223+
}
224+
89225
$app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']);
90226
}
91227
}
92228

93229
//* Update the mailinglist
94-
$mailing_lists = $app->db->queryAllRecords("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = ?", $page_form->oldDataRecord['domain']);
95-
if(is_array($mailing_lists)) {
96-
foreach($mailing_lists as $rec) {
97-
$app->db->datalogUpdate('mail_mailinglist', array("sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailinglist_id', $rec['mailinglist_id']);
230+
$mailinglists = $app->db->queryAllRecords("SELECT * FROM mail_mailinglist WHERE domain = ?", $old_domain);
231+
if(is_array($mailinglists)) {
232+
foreach($mailinglists as $rec) {
233+
$update_data = array(
234+
'sys_userid' => $client_user_id,
235+
'sys_groupid' => $sys_groupid,
236+
'domain' => $domain,
237+
'email' => str_replace($old_domain, $domain, $rec['email']),
238+
);
239+
$app->db->datalogUpdate('mail_mailinglist', $update_data, 'mailinglist_id', $rec['mailinglist_id']);
98240
}
99241
}
100242

@@ -107,25 +249,81 @@ function mail_mail_domain_edit($event_name, $page_form) {
107249
}
108250
}
109251

110-
if ($page_form->oldDataRecord["domain"] != $domain) {
111-
//* Delete the old spamfilter record
112-
$tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", "@" . $page_form->oldDataRecord["domain"]);
113-
$app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]);
114-
unset($tmp);
252+
// Spamfilter policy
253+
$policy_id = $app->functions->intval($page_form->dataRecord["policy"]);
254+
255+
// If domain changes, update spamfilter_users
256+
// and fire spamfilter_wblist_update events so rspamd files are rewritten
257+
if ($old_domain != $domain) {
258+
$tmp_users = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email LIKE ?", '%@' . $old_domain);
259+
if(is_array($tmp_users)) {
260+
foreach ($tmp_users as $tmp_old) {
261+
$tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain);
262+
if($tmp_new['id'] > 0) {
263+
// There is a spamfilter_users for both old and new domain, we'll update old wblist entries
264+
$update_data = array(
265+
'sys_userid' => $client_user_id,
266+
'sys_groupid' => $sys_groupid,
267+
'rid' => $tmp_new['id'],
268+
);
269+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_old['id']);
270+
foreach ($tmp_wblist as $tmp) {
271+
$app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']);
272+
}
273+
274+
// now delete old spamfilter_users entry
275+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_old['id']);
276+
277+
/// and update the new
278+
$update_data = array(
279+
'sys_userid' => $client_user_id,
280+
'sys_groupid' => $sys_groupid,
281+
);
282+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']);
283+
} else {
284+
$update_data = array(
285+
'sys_userid' => $client_user_id,
286+
'sys_groupid' => $sys_groupid,
287+
'email' => '@' . $domain,
288+
);
289+
if($tmp_old['fullname'] == '@' . $old_domain) {
290+
$update_data['fullname'] = '@' . $domain;
291+
}
292+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']);
293+
}
294+
}
295+
}
115296
}
116-
$app->db->query("UPDATE spamfilter_users SET email=REPLACE(email, ?, ?), sys_userid = ?, sys_groupid = ? WHERE email LIKE ?", $page_form->oldDataRecord['domain'], $domain, $client_user_id, $sys_groupid, "%@" . $page_form->oldDataRecord['domain']);
117297

118-
} // end if domain name changed
119-
120-
//* Force-update the aliases (required for spamfilter changes)
121-
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $domain, "%@" . $domain);
122-
123-
if(is_array($forwardings)) {
124-
foreach($forwardings as $rec) {
125-
$app->db->datalogUpdate('mail_forwarding', array("source" => $rec['source']), 'forwarding_id', $rec['forwarding_id'],true);
298+
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain);
299+
if(isset($tmp_user['id']) && $tmp_user['id'] > 0) {
300+
// There is already a record that we will update
301+
if($policy_id != $tmp_user['policy_id']) {
302+
$update_data = array(
303+
'policy_id' => $policy_id,
304+
);
305+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user["id"]);
306+
}
307+
} else {
308+
// We create a new record
309+
$insert_data = array(
310+
"sys_userid" => $client_user_id,
311+
"sys_groupid" => $sys_groupid,
312+
"sys_perm_user" => 'riud',
313+
"sys_perm_group" => 'riud',
314+
"sys_perm_other" => '',
315+
"server_id" => $page_form->dataRecord["server_id"],
316+
"priority" => 5,
317+
"policy_id" => $policy_id,
318+
"email" => '@' . $domain,
319+
"fullname" => '@' . $domain,
320+
"local" => 'Y'
321+
);
322+
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
126323
}
127-
}
128-
324+
325+
} // end if domain name changed
326+
129327
}
130328

131329
}

0 commit comments

Comments
 (0)