From 5afa722a9fedb091386e2289d0196bac4708454f Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Sat, 26 Dec 2015 22:31:09 +0000
Subject: [PATCH] Separate maildir encryption command

---
 src/freedombone              | 117 ++++-------------------------------
 src/freedombone-encrypt-mail |  93 ++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 105 deletions(-)
 create mode 100755 src/freedombone-encrypt-mail

diff --git a/src/freedombone b/src/freedombone
index c631aa6de..f1194ea9b 100755
--- a/src/freedombone
+++ b/src/freedombone
@@ -3585,9 +3585,9 @@ function time_synchronisation {
   fi
 
   if [ -f /usr/local/bin/${PROJECT_NAME}-update-date ]; then
-	  cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
+      cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
   else
-	  cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
+      cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
   fi
   chmod +x /usr/bin/updatedate
 
@@ -4804,114 +4804,21 @@ function encrypt_all_email {
   if [[ $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_NONMAILBOX" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
       return
   fi
-  if grep -Fxq "encrypt_all_email" $COMPLETION_FILE; then
-      return
-  fi
   if [[ $GPG_ENCRYPT_STORED_EMAIL != "yes" ]]; then
       return
   fi
-  echo '#!/bin/bash' > /usr/bin/encmaildir
-  echo '#' >> /usr/bin/encmaildir
-  echo '# GPLv2' >> /usr/bin/encmaildir
-  echo '# GPG Encrypt a Maildir using gpgit.pl' >> /usr/bin/encmaildir
-  echo '# Oct 03, 2014' >> /usr/bin/encmaildir
-  echo '#' >> /usr/bin/encmaildir
-  echo '# Change log:' >> /usr/bin/encmaildir
-  echo '#   Sep 03, 2011' >> /usr/bin/encmaildir
-  echo '#     - Temporary file is based on file_owner to avoid' >> /usr/bin/encmaildir
-  echo '#        issues with permission differences.' >> /usr/bin/encmaildir
-  echo '#     - Temporary file is removed after run.' >> /usr/bin/encmaildir
-  echo '#     - Optional arguments passed to "find".' >> /usr/bin/encmaildir
-  echo '#     - Full paths to binaries.' >> /usr/bin/encmaildir
-  echo '#     - Removed unneccessary need of "cat", "grep", etc.' >> /usr/bin/encmaildir
-  echo '#   Sep 04, 2011' >> /usr/bin/encmaildir
-  echo '#     - Dont remove Dovecot index/uid unless messages' >> /usr/bin/encmaildir
-  echo '#       have been GPG encrypted.' >> /usr/bin/encmaildir
-  echo '#     - Adjust file tests to not just use -e' >> /usr/bin/encmaildir
-  echo '#     - Quote all file operations' >> /usr/bin/encmaildir
-  echo '#   Sep 05, 2011' >> /usr/bin/encmaildir
-  echo '#     - Dont arbitrarily copy files, only overwrite the file' >> /usr/bin/encmaildir
-  echo '#       in ~/Maildir if it differs after calling gpgencmail.pl' >> /usr/bin/encmaildir
-  echo '#     - Only rebuild the index if we have modified ~/Maildir' >> /usr/bin/encmaildir
-  echo '#   Oct 03, 2014' >> /usr/bin/encmaildir
-  echo '#     - Minor modifications for use with Freedombone' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo 'if [[ -z "$1" || -z "$2" || -z "$3" ]]; then' >> /usr/bin/encmaildir
-  echo '  echo "Usage is ./encmaildir.sh    {optional arguments passed to find for messages such as -mtime 0}"' >> /usr/bin/encmaildir
-  echo '  exit 0' >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo 'MAIL_DIR=$1' >> /usr/bin/encmaildir
-  echo 'EMAIL_ADDRESS=$2' >> /usr/bin/encmaildir
-  echo 'USERNAME=$3' >> /usr/bin/encmaildir
-  echo 'if [ ! -d "$MAIL_DIR" ]; then' >> /usr/bin/encmaildir
-  echo "  MAIL_DIR='/home/$MY_USERNAME/Maildir'" >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo 'if [ ! $EMAIL_ADDRESS ]; then' >> /usr/bin/encmaildir
-  echo "  EMAIL_ADDRESS='$MY_EMAIL_ADDRESS'" >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo 'if [ ! $USERNAME ]; then' >> /usr/bin/encmaildir
-  echo "  USERNAME='$MY_USERNAME'" >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '# Does this key exist?' >> /usr/bin/encmaildir
-  echo 'gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1' >> /usr/bin/encmaildir
-  echo 'if [ $? -gt 0 ]; then' >> /usr/bin/encmaildir
-  echo '  echo "A GPG key for $EMAIL_ADDRESS could not be found!"' >> /usr/bin/encmaildir
-  echo '  exit 0' >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '# Find all files in the Maildir specified.' >> /usr/bin/encmaildir
-  echo 'echo "Calling find"' >> /usr/bin/encmaildir
-  echo -n 'find "$MAIL_DIR" -type f -regex ' >> /usr/bin/encmaildir
-  echo -n "'.*/\(cur\|new\)/.*' " >> /usr/bin/encmaildir
-  echo '$4|while read line; do' >> /usr/bin/encmaildir
-  echo '     gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS"  "/tmp/msg_$USERNAME"' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '     # Check to see if there are differences between the existing' >> /usr/bin/encmaildir
-  echo '     # Maildir file and what was created by gpgit.pl' >> /usr/bin/encmaildir
-  echo '     diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;' >> /usr/bin/encmaildir
-  echo '     if [ $? -gt 0 ]; then' >> /usr/bin/encmaildir
-  echo '       # Preserve timestamps, set ownership.' >> /usr/bin/encmaildir
-  echo '       chown $USERNAME:$USERNAME "/tmp/msg_$USERNAME"' >> /usr/bin/encmaildir
-  echo '       chmod 600   "/tmp/msg_$USERNAME"' >> /usr/bin/encmaildir
-  echo '       touch   "/tmp/msg_$USERNAME" --reference="$line"' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '       # Unlink the original Maildir message' >> /usr/bin/encmaildir
-  echo '       unlink "$line"' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '       # Strip message sizes, retain experimental flags' >> /usr/bin/encmaildir
-  echo '       # and status flags, and copy the file over.' >> /usr/bin/encmaildir
-  echo '       STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")' >> /usr/bin/encmaildir
-  echo '       cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '       #Indexes must be rebuilt, weve modified Maildir.' >> /usr/bin/encmaildir
-  echo '       touch "/tmp/rebuild_index_$USERNAME"' >> /usr/bin/encmaildir
-  echo '     else' >> /usr/bin/encmaildir
-  echo '       echo "Not copying, no differences between /tmp/msg_$USERNAME and $line"' >> /usr/bin/encmaildir
-  echo '     fi' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '     # Remove the temporary file' >> /usr/bin/encmaildir
-  echo '     unlink "/tmp/msg_$USERNAME"' >> /usr/bin/encmaildir
-  echo 'done' >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '# Remove Dovecot index and uids for regeneration.' >> /usr/bin/encmaildir
-  echo 'if [ -f "/tmp/rebuild_index_$USERNAME" ]; then' >> /usr/bin/encmaildir
-  echo '  echo "Removing Dovecot indexes and uids"' >> /usr/bin/encmaildir
-  echo -n '  find "$MAIL_DIR" -type f -regex ' >> /usr/bin/encmaildir
-  echo "'.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete" >> /usr/bin/encmaildir
-  echo '' >> /usr/bin/encmaildir
-  echo '  # Remove the temporary file' >> /usr/bin/encmaildir
-  echo '  unlink "/tmp/rebuild_index_$USERNAME"' >> /usr/bin/encmaildir
-  echo 'else' >> /usr/bin/encmaildir
-  echo '  echo "No messages found needing GPG encryption, not' >> /usr/bin/encmaildir
-  echo '  echo "removing Dovecot indexes and UIDs."' >> /usr/bin/encmaildir
-  echo 'fi' >> /usr/bin/encmaildir
-  echo 'exit 0' >> /usr/bin/encmaildir
+
+  if [ -f /usr/local/bin/${PROJECT_NAME}-encrypt-mail ]; then
+      cp /usr/local/bin/${PROJECT_NAME}-encrypt-mail /usr/bin/encmaildir
+  else
+      cp /usr/bin/${PROJECT_NAME}-encrypt-mail /usr/bin/encmaildir
+  fi
   chmod +x /usr/bin/encmaildir
 
+  if grep -Fxq "encrypt_all_email" $COMPLETION_FILE; then
+      return
+  fi
+
   if [ ! /home/$MY_USERNAME/README ]; then
       touch /home/$MY_USERNAME/README
   fi
diff --git a/src/freedombone-encrypt-mail b/src/freedombone-encrypt-mail
new file mode 100755
index 000000000..f7eef046b
--- /dev/null
+++ b/src/freedombone-encrypt-mail
@@ -0,0 +1,93 @@
+#!/bin/bash
+#
+# .---.                  .              .
+# |                      |              |
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
+#
+#                    Freedom in the Cloud
+#
+# GPG Encrypt a Maildir using gpgit.pl
+#
+# License
+# =======
+#
+# Copyright (C) 2014-2015 Bob Mottram <bob@robotics.uk.to>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# 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
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+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
-- 
GitLab