Skip to content

Commit 2604cb9

Browse files
author
Florian Schaal
committed
remove backups when deleting a mail-domain or mail-user
1 parent 69b1bc6 commit 2604cb9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

server/plugins-available/mail_plugin.inc.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,45 @@ function user_delete($event_name, $data) {
317317
$app->uses("getconf");
318318
$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
319319

320+
$maildir_path_deleted = false;
320321
$old_maildir_path = escapeshellcmd($data['old']['maildir']);
321322
if($old_maildir_path != $mail_config['homedir_path'] && strlen($old_maildir_path) > strlen($mail_config['homedir_path']) && !stristr($old_maildir_path, '//') && !stristr($old_maildir_path, '..') && !stristr($old_maildir_path, '*') && strlen($old_maildir_path) >= 10) {
322323
exec('rm -rf '.escapeshellcmd($old_maildir_path));
323324
$app->log('Deleted the Maildir: '.$data['old']['maildir'], LOGLEVEL_DEBUG);
325+
$maildir_path_deleted = true;
324326
} else {
325327
$app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'], LOGLEVEL_ERROR);
326328
}
329+
//* Delete the mail-backups
330+
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
331+
$backup_dir = $server_config['backup_dir'];
332+
//* mount backup directory, if necessary
333+
$mount_backup = true;
334+
$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
335+
if($server_config['backup_dir'] != '' && $maildir_path_deleted) {
336+
if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
337+
if(!$app->system->is_mounted($backup_dir)){
338+
exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
339+
sleep(1);
340+
if(!$app->system->is_mounted($backup_dir)) $mount_backup = false;
341+
}
342+
}
343+
if($mount_backup){
344+
$sql = "SELECT * FROM mail_domain WHERE domain = '".explode("@",$data['old']['email'])[1]."'";
345+
$domain_rec = $app->db->queryOneRecord($sql);
346+
$mail_backup_dir = $backup_dir.'/mail'.$domain_rec['domain_id'];
347+
$mail_backup_files = 'mail'.$data['old']['mailuser_id'];
348+
exec(escapeshellcmd('rm -f '.$mail_backup_dir.'/'.$mail_backup_files).'*');
349+
//* cleanup database
350+
$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$domain_rec['domain_id']." AND mailuser_id = ".$data['old']['mailuser_id'];
351+
$app->db->query($sql);
352+
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
353+
354+
$app->log('Deleted the mail backups for: '.$data['old']['email'], LOGLEVEL_DEBUG);
355+
356+
357+
}
358+
}
327359
}
328360

329361
function domain_delete($event_name, $data) {
@@ -333,11 +365,13 @@ function domain_delete($event_name, $data) {
333365
$app->uses("getconf");
334366
$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
335367

368+
$maildomain_path_deleted = false;
336369
//* Delete maildomain path
337370
$old_maildomain_path = escapeshellcmd($mail_config['homedir_path'].'/'.$data['old']['domain']);
338371
if($old_maildomain_path != $mail_config['homedir_path'] && !stristr($old_maildomain_path, '//') && !stristr($old_maildomain_path, '..') && !stristr($old_maildomain_path, '*') && !stristr($old_maildomain_path, '&') && strlen($old_maildomain_path) >= 10 && !empty($data['old']['domain'])) {
339372
exec('rm -rf '.escapeshellcmd($old_maildomain_path));
340373
$app->log('Deleted the mail domain directory: '.$old_maildomain_path, LOGLEVEL_DEBUG);
374+
$maildomain_path_deleted = true;
341375
} else {
342376
$app->log('Possible security violation when deleting the mail domain directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
343377
}
@@ -350,6 +384,33 @@ function domain_delete($event_name, $data) {
350384
} else {
351385
$app->log('Possible security violation when deleting the mail domain mailfilter directory: '.$old_maildomain_path, LOGLEVEL_ERROR);
352386
}
387+
388+
//* Delete the mail-backups
389+
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
390+
$backup_dir = $server_config['backup_dir'];
391+
//* mount backup directory, if necessary
392+
$mount_backup = true;
393+
$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
394+
if($server_config['backup_dir'] != '' && $maildomain_path_deleted) {
395+
if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
396+
if(!$app->system->is_mounted($backup_dir)){
397+
exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
398+
sleep(1);
399+
if(!$app->system->is_mounted($backup_dir)) $mount_backup = false;
400+
}
401+
}
402+
if($mount_backup){
403+
$mail_backup_dir = $backup_dir.'/mail'.$data['old']['domain_id'];
404+
exec(escapeshellcmd('rm -rf '.$mail_backup_dir));
405+
//* cleanup database
406+
$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$data_old['domain_id'];
407+
$app->db->query($sql);
408+
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
409+
410+
$app->log('Deleted the mail backup directory: '.$mail_backup_dir, LOGLEVEL_DEBUG);
411+
}
412+
}
413+
353414
}
354415

355416
function transport_update($event_name, $data) {

0 commit comments

Comments
 (0)