diff --git a/install/lib/installer_base.lib.php b/install/lib/installer_base.lib.php index a73b9d0922f82f3e29d4192f176aadd730f163fe..71753a6b3317276d0c936954d346cfb53783b736 100644 --- a/install/lib/installer_base.lib.php +++ b/install/lib/installer_base.lib.php @@ -3847,6 +3847,10 @@ class installer_base { $root_cron_jobs[] = "0 0 * * * ".$install_dir."/server/scripts/create_daily_nginx_access_logs.sh &> /dev/null"; } + if ($conf['services']['mail'] == 1) { + $root_cron_jobs[] = "30 23 * * * ".$install_dir."/server/scripts/handle_mailbox_soft_deleted.sh &> /dev/null"; + } + foreach($root_cron_jobs as $cron_job) { if(!in_array($cron_job."\n", $existing_root_cron_jobs)) { $existing_root_cron_jobs[] = $cron_job."\n"; diff --git a/install/tpl/server.ini.master b/install/tpl/server.ini.master index f6ab6365df43f297eb7074e63eb13fe338f1d585..a0faf66908b60766650b1e1ff0632ad669b559df 100644 --- a/install/tpl/server.ini.master +++ b/install/tpl/server.ini.master @@ -56,6 +56,7 @@ relayhost_user= relayhost_password= mailbox_size_limit=0 message_size_limit=0 +mailbox_soft_delete=n mailbox_quota_stats=y realtime_blackhole_list=zen.spamhaus.org overquota_notify_threshold=90 diff --git a/interface/web/admin/form/server_config.tform.php b/interface/web/admin/form/server_config.tform.php index e1ca3c7ca0f656b948a80033a4b040cc2f388c5d..7af535e60042681dd5e952e4953575460968d510 100644 --- a/interface/web/admin/form/server_config.tform.php +++ b/interface/web/admin/form/server_config.tform.php @@ -734,6 +734,12 @@ $form["tabs"]['mail'] = array( 'default' => 'y', 'value' => array(0 => 'n', 1 => 'y') ), + 'mailbox_soft_delete' => array ( + 'datatype' => 'VARCHAR', + 'formtype' => 'CHECKBOX', + 'default' => 'n', + 'value' => array(0 => 'n', 1 => 'y') + ), 'mailbox_quota_stats' => array ( 'datatype' => 'VARCHAR', 'formtype' => 'CHECKBOX', diff --git a/interface/web/admin/lib/lang/en_server_config.lng b/interface/web/admin/lib/lang/en_server_config.lng index d730862ba71be5fe396d2d889f74a8545d3697b1..1ee224a44a48eaf94338978cfc975f1c0f0170be 100644 --- a/interface/web/admin/lib/lang/en_server_config.lng +++ b/interface/web/admin/lib/lang/en_server_config.lng @@ -355,3 +355,5 @@ $wb['tooltip_jailkit_hardlinks_txt'] = 'Using hardlinks is insecure, but saves d $wb['jailkit_hardlinks_allow_txt'] = 'Allow hardlinks within the jail'; $wb['jailkit_hardlinks_no_txt'] = 'No, remove hardlinked files'; $wb['jailkit_hardlinks_yes_txt'] = 'Yes, use hardlinks if possible'; +$wb['mailbox_soft_delete_txt'] = 'Mailbox soft delete'; +$wb['mailbox_soft_delete_info_txt'] = 'by default cleaned up after 7 days.'; diff --git a/interface/web/admin/lib/lang/nl_server_config.lng b/interface/web/admin/lib/lang/nl_server_config.lng index 66394fe32e26a40f1d5d2348dd84905317a45a5b..2af5d6a9eac8ad0674ad4efe96aed222d6d12f44 100644 --- a/interface/web/admin/lib/lang/nl_server_config.lng +++ b/interface/web/admin/lib/lang/nl_server_config.lng @@ -349,3 +349,5 @@ $wb['bind_keyfiles_dir_error_empty'] = 'BIND keyfiles directory is empty.'; $wb['bind_zonefiles_masterprefix_error_regex'] = 'Invalid BIND zonefiles master prefix.'; $wb['bind_zonefiles_slaveprefix_error_regex'] = 'Invalid BIND zonefiles slave prefix.'; $wb['bind_keyfiles_dir_error_regex'] = 'Invalid BIND keyfiles directory.'; +$wb['mailbox_soft_delete_txt'] = 'Mailbox soft delete'; +$wb['mailbox_soft_delete_info_txt'] = 'by default cleaned up after 7 days.'; diff --git a/interface/web/admin/templates/server_config_mail_edit.htm b/interface/web/admin/templates/server_config_mail_edit.htm index 1876b78f4bbbbc3cd67e8be47c1860a7d55040d9..84683191c804141cc6024a207dd92d3e1cb47af4 100644 --- a/interface/web/admin/templates/server_config_mail_edit.htm +++ b/interface/web/admin/templates/server_config_mail_edit.htm @@ -140,6 +140,12 @@ <a data-toggle="tooltip" title="{tmpl_var name='tooltip_stress_adaptive_txt'}">{tmpl_var name="stress_adaptive"}</a> </div> </div> + <div class="form-group"> + <label class="col-sm-3 control-label">{tmpl_var name='mailbox_soft_delete_txt'}</label> + <div class="col-sm-9"> + {tmpl_var name='mailbox_soft_delete'} {tmpl_var name='mailbox_soft_delete_info_txt'} + </div> + </div> <div class="form-group"> <label class="col-sm-3 control-label">{tmpl_var name='mailbox_quota_stats_txt'}</label> <div class="col-sm-9"> diff --git a/server/plugins-available/mail_plugin.inc.php b/server/plugins-available/mail_plugin.inc.php index 2baf07ec7dd07ad69f7203b77924bb166cf1f8f9..53836a1392f50d4dda9268f83f464fa6ec78d149 100644 --- a/server/plugins-available/mail_plugin.inc.php +++ b/server/plugins-available/mail_plugin.inc.php @@ -429,8 +429,19 @@ class mail_plugin { $maildir_path_deleted = false; $old_maildir_path = $data['old']['maildir']; 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) { - $app->system->exec_safe('rm -rf ?', $old_maildir_path); - $app->log('Deleted the Maildir: '.$data['old']['maildir'], LOGLEVEL_DEBUG); + if ($mail_config['mailbox_soft_delete'] == 'y') { + // Move it, adding a date based suffix. A cronjob should purge or archive. + $thrash_maildir_path = $old_maildir_path . '-deleted-' . date("YmdHis"); + $app->system->exec_safe('mv ? ?', $old_maildir_path, $thrash_maildir_path); + + // Update the dir's timestamp to make filtering on age easier in any cleanup cronjob. + $app->system->exec_safe('touch ?', $thrash_maildir_path); + + $app->log('Renamed the Maildir: ' . $data['old']['maildir'] . ' to ' . $thrash_maildir_path, LOGLEVEL_DEBUG); + } else { + $app->system->exec_safe('rm -rf ?', $old_maildir_path); + $app->log('Deleted the Maildir: '.$data['old']['maildir'], LOGLEVEL_DEBUG); + } $maildir_path_deleted = true; } else { $app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'], LOGLEVEL_ERROR); @@ -473,8 +484,19 @@ class mail_plugin { //* Delete maildomain path $old_maildomain_path = $mail_config['homedir_path'].'/'.$data['old']['domain']; 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'])) { - $app->system->exec_safe('rm -rf ?', $old_maildomain_path); - $app->log('Deleted the mail domain directory: '.$old_maildomain_path, LOGLEVEL_DEBUG); + if ($mail_config['mailbox_soft_delete'] == 'y') { + // Move it, adding a date based suffix. A cronjob should purge or archive. + $thrash_maildomain_path = $old_maildomain_path . '-deleted-' . date("YmdHis"); + $app->system->exec_safe('mv ? ?', $old_maildomain_path, $thrash_maildomain_path); + + // Update the dir's timestamp to make filtering on age easier in any cleanup cronjob. + $app->system->exec_safe('touch ?', $thrash_maildomain_path); + + $app->log('Renamed the mail domain directory: ' . $old_maildomain_path . ' to ' . $thrash_maildomain_path, LOGLEVEL_DEBUG); + } else { + $app->system->exec_safe('rm -rf ?', $old_maildomain_path); + $app->log('Deleted the mail domain directory: '.$old_maildomain_path, LOGLEVEL_DEBUG); + } $maildomain_path_deleted = true; } else { $app->log('Possible security violation when deleting the mail domain directory: '.$old_maildomain_path, LOGLEVEL_ERROR); diff --git a/server/scripts/handle_mailbox_soft_deleted.sh b/server/scripts/handle_mailbox_soft_deleted.sh new file mode 100644 index 0000000000000000000000000000000000000000..7c7f280839d31233adabfd39832ed089e5fc66d3 --- /dev/null +++ b/server/scripts/handle_mailbox_soft_deleted.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Archive directories for deleted mailboxes. +delay_days=7 + +# Test if there is something to do... to avoid 'No such file or directory' from find later. +ls /var/vmail/*/[a-z0-9.-]*-deleted-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] >/dev/null 2>&1 +if [ $? != 0 ]; then + exit 0; +fi + +function remove_soft_deleted_mailbox { + dir=$1 + + echo "Purging $dir" + rm -r "$dir" +} + +function compress_soft_deleted_mailbox { + dir=$1 + + backupfile="${dir}.tar.bz2" + + # Test if backup file already exists + if [ -f $backupfile ]; then + # Skip + echo "ERROR: Backupfile($backupfile) exists!" >&2 + continue + fi + + echo "Compressing for $dir" + tar cvfj "$backupfile" --remove-files "$dir" 2> >( grep -v "tar: Removing leading" >&2) +} + +# List deleted mailboxs to archive +# -mtime +7 ===> Only mailboxes deleted more then 7 days ago +# Test that the last dir component matches e.g. xxx-deleted-20220101094242 (14 digits) +# command: xxx-`date "+%Y%m%d%H%M%S"` +find /var/vmail/*/[a-z0-9.-]*-deleted-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] -maxdepth 0 -type d -mtime +$delay_days | while read line; do + # example $line: "/var/vmail/example.com/info-20220101094242" + + dir=$line + + # Uncomment the desired cleanup method below, or be creative and create your own. + + remove_soft_deleted_mailbox $dir + #compress_soft_deleted_mailbox $dir + +done