freedombone/src/freedombone-encrypt-mail

94 lines
3.1 KiB
Plaintext
Raw Normal View History

2015-12-26 23:31:09 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# GPG Encrypt a Maildir using gpgit.pl
#
# License
# =======
#
2016-01-02 22:58:27 +01:00
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
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
ADMIN_USER=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
if [ ! $USERNAME ]; then
USERNAME=$ADMIN_USER
fi
MAIL_DIR=/home/$USERNAME/Maildir
EMAIL_ADDRESS=$USERNAME@$HOSTNAME
# Does this key exist?
gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
exit 0
fi
# Find all files in the Maildir specified.
echo $"Calling find"
find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' $4|while read line; do
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;
if [ $? -gt 0 ]; then
# Preserve timestamps, set ownership.
chown $USERNAME:$USERNAME "/tmp/msg_$USERNAME"
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