freedombone/src/freedombone-encrypt-mail

101 lines
3.2 KiB
Plaintext
Raw Permalink Normal View History

2015-12-26 23:31:09 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2015-12-26 23:31:09 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2015-12-26 23:31:09 +01:00
#
# GPG Encrypt a Maildir using gpgit.pl
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2015-12-26 23:31:09 +01:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2015-12-26 23:31:09 +01:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2015-12-26 23:31:09 +01:00
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-12-26 23:31:09 +01:00
USERNAME=$1
2016-10-13 12:31:28 +02:00
PROJECT_NAME='freedombone'
2018-03-02 20:17:02 +01:00
COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt"
2016-10-13 12:31:28 +02:00
2018-03-02 20:17:02 +01:00
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
2016-10-16 20:50:56 +02:00
for f in $UTILS_FILES
do
2018-03-02 20:17:02 +01:00
source "$f"
2016-10-16 20:50:56 +02:00
done
ADMIN_USER=$(get_completion_param "Admin user")
2015-12-26 23:31:09 +01:00
2018-03-02 20:17:02 +01:00
if [ ! "$USERNAME" ]; then
2015-12-26 23:31:09 +01:00
USERNAME=$ADMIN_USER
fi
MAIL_DIR=/home/$USERNAME/Maildir
EMAIL_ADDRESS=$USERNAME@$HOSTNAME
# Does this key exist?
2018-03-02 20:17:02 +01:00
if ! gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1; then
2015-12-26 23:31:09 +01:00
echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
exit 0
fi
# Find all files in the Maildir specified.
echo $"Calling find"
2018-03-02 20:17:02 +01:00
find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' "$4"|while read -r line; do
2015-12-26 23:31:09 +01:00
gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS" "/tmp/msg_$USERNAME"
# Check to see if there are differences between the existing
# Maildir file and what was created by gpgit.pl
diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;
2018-03-02 20:17:02 +01:00
# shellcheck disable=SC2181
2015-12-26 23:31:09 +01:00
if [ $? -gt 0 ]; then
# Preserve timestamps, set ownership.
2018-03-02 20:17:02 +01:00
chown "$USERNAME":"$USERNAME" "/tmp/msg_$USERNAME"
2015-12-26 23:31:09 +01:00
chmod 600 "/tmp/msg_$USERNAME"
touch "/tmp/msg_$USERNAME" --reference="$line"
# Unlink the original Maildir message
unlink "$line"
# Strip message sizes, retain experimental flags
# and status flags, and copy the file over.
STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")
cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"
#Indexes must be rebuilt, weve modified Maildir.
touch "/tmp/rebuild_index_$USERNAME"
else
echo $"Not copying, no differences between /tmp/msg_$USERNAME and $line"
fi
# Remove the temporary file
unlink "/tmp/msg_$USERNAME"
done
# Remove Dovecot index and uids for regeneration.
if [ -f "/tmp/rebuild_index_$USERNAME" ]; then
echo $"Removing Dovecot indexes and uids"
find "$MAIL_DIR" -type f -regex '.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete
# Remove the temporary file
unlink "/tmp/rebuild_index_$USERNAME"
else
echo -n $"No messages found needing GPG encryption, not"
echo $"removing Dovecot indexes and UIDs."
fi
exit 0