howtos:mail_crypt_-_decrypt_encrypt_mails
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
howtos:mail_crypt_-_decrypt_encrypt_mails [12/08/2023 22:50] – domingo | howtos:mail_crypt_-_decrypt_encrypt_mails [13/08/2023 15:18] (current) – domingo | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Intro ====== | ||
+ | |||
mailcow compress and encrypt mail stored inside the " | mailcow compress and encrypt mail stored inside the " | ||
Line 4: | Line 6: | ||
This requires that you enter the dovecot container and paste the logic. That is rather cumbersome, so here is a bash script which you can run from anywhere which takes care of that. | This requires that you enter the dovecot container and paste the logic. That is rather cumbersome, so here is a bash script which you can run from anywhere which takes care of that. | ||
+ | |||
+ | ===== Script Which Takes It All ===== | ||
It take two flags " | It take two flags " | ||
Line 76: | Line 80: | ||
fi | fi | ||
</ | </ | ||
+ | |||
+ | ===== Script Which Takes One Mailbox At a Time ===== | ||
+ | |||
+ | < | ||
+ | |||
+ | #!/bin/bash | ||
+ | |||
+ | # Define the path inside the container | ||
+ | CONTAINER_PATH="/ | ||
+ | DOCKER_COMPOSE_FILE="/ | ||
+ | |||
+ | decrypt_files() { | ||
+ | local mailbox_path=" | ||
+ | find " | ||
+ | if [[ $(head -c7 " | ||
+ | doveadm fs get compress lz4: | ||
+ | " | ||
+ | if [[ -s "/ | ||
+ | chmod 600 "/ | ||
+ | chown 5000:5000 "/ | ||
+ | mv "/ | ||
+ | else | ||
+ | rm "/ | ||
+ | fi | ||
+ | fi | ||
+ | done | ||
+ | } | ||
+ | |||
+ | encrypt_files() { | ||
+ | local mailbox_path=" | ||
+ | find " | ||
+ | if [[ $(head -c7 " | ||
+ | doveadm fs put crypt private_key_path=/ | ||
+ | " | ||
+ | chmod 600 " | ||
+ | chown 5000:5000 " | ||
+ | fi | ||
+ | done | ||
+ | } | ||
+ | |||
+ | print_help() { | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | } | ||
+ | |||
+ | # Function to list and select user mailboxes | ||
+ | select_mailbox() { | ||
+ | local paths=($(find /var/vmail/ -mindepth 2 -maxdepth 2 -type d ! -name " | ||
+ | local mailboxes=() | ||
+ | |||
+ | # Convert paths to email format | ||
+ | for path in " | ||
+ | local user=$(basename " | ||
+ | local domain=$(basename $(dirname " | ||
+ | mailboxes+=(" | ||
+ | done | ||
+ | |||
+ | select mailbox in " | ||
+ | if [[ -n $mailbox ]]; then | ||
+ | # Convert back to path format and return | ||
+ | local user=$(echo " | ||
+ | local domain=$(echo " | ||
+ | echo "/ | ||
+ | return | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | done | ||
+ | } | ||
+ | |||
+ | # Check if we're inside a Docker container | ||
+ | if [ -f /.dockerenv ]; then | ||
+ | # We are inside a container, proceed with the main logic | ||
+ | main() { | ||
+ | local mailbox_path="" | ||
+ | if [[ -z " | ||
+ | echo " | ||
+ | mailbox_path=$(select_mailbox) | ||
+ | else | ||
+ | mailbox_path=" | ||
+ | fi | ||
+ | |||
+ | case " | ||
+ | -d) | ||
+ | decrypt_files " | ||
+ | ;; | ||
+ | -e) | ||
+ | encrypt_files " | ||
+ | ;; | ||
+ | *) | ||
+ | print_help | ||
+ | ;; | ||
+ | esac | ||
+ | } | ||
+ | main " | ||
+ | else | ||
+ | # We are outside a container, so let's copy and execute the script inside the container | ||
+ | docker compose -f $DOCKER_COMPOSE_FILE cp $0 dovecot-mailcow: | ||
+ | docker compose -f $DOCKER_COMPOSE_FILE exec -T dovecot-mailcow chmod +x $CONTAINER_PATH | ||
+ | docker compose -f $DOCKER_COMPOSE_FILE exec -T dovecot-mailcow $CONTAINER_PATH " | ||
+ | fi | ||
+ | </ | ||
+ |
howtos/mail_crypt_-_decrypt_encrypt_mails.txt · Last modified: 13/08/2023 15:18 by domingo