Skip to content

Instantly share code, notes, and snippets.

@AnnoyingTechnology
Last active April 3, 2023 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnnoyingTechnology/3609cc127cd7600d1d1abdba8eca6b3c to your computer and use it in GitHub Desktop.
Save AnnoyingTechnology/3609cc127cd7600d1d1abdba8eca6b3c to your computer and use it in GitHub Desktop.
Decrypt and decompress Maildir emails from Mailcow
#!/bin/bash
# Check if all arguments are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <dir_to_scan> <output_folder> <private_key_path>"
exit 1
fi
# Set the input and output folders and private key path from the command line arguments
dir_to_scan="$SCRIPT_DIR/$1"
output_folder="$SCRIPT_DIR/$2"
private_key_path="$3"
# remove spaces and special characters
find $dir_to_scan -depth -name '*[!a-zA-Z0-9_-]*' -execdir bash -c 'mv "$0" "${0//[^[:alnum:]_-]/_}"' {} \;
# Loop through each file found
find "$dir_to_scan" -type f | while read file; do
# get the basename
file_base_name=$( dirname "$file" )
file_name=$( basename "$file" )
# Replace "theEmailFile" with the current file name and the private key path with the provided value
cmd="doveadm fs get compress lz4:1:crypt:private_key_path=$private_key_path:posix:prefix=$file_base_name/ $file_name"
# Generate the output filename without extension and add .eml suffix
# echo $cmd
output_file="$output_folder/$(basename "$file").eml"
# Execute the command and store the output to the output file if there are no errors and the file size is non-zero
$cmd > "$output_file"
if [ ! -s "$output_file" ]; then
rm "$output_file"
echo "Skipping file '$file' because the converted file is empty."
fi
done
@AnnoyingTechnology
Copy link
Author

AnnoyingTechnology commented Apr 3, 2023

DO NOT USE ON MAILCOW SERVER! THIS WOULD DESTROY YOUR MAILDIR

Copy the Maildir of interest on another machine.

You need to have dovecot installed on the machine where you want to run this.
You also need to retrieve the private key file from Mailcow /var/lib/docker/volumes/mailcowdockerized_crypt-vol-1/_data/ecprivkey.pem as it is expected in 3rd parameter of this script.
Then specify the Maildir as first argument, and an output directory as second argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment