freedombone-encrypt-mail 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # GPG Encrypt a Maildir using gpgit.pl
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. USERNAME=$1
  31. PROJECT_NAME='freedombone'
  32. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  33. ADMIN_USER=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  34. if [ ! $USERNAME ]; then
  35. USERNAME=$ADMIN_USER
  36. fi
  37. MAIL_DIR=/home/$USERNAME/Maildir
  38. EMAIL_ADDRESS=$USERNAME@$HOSTNAME
  39. # Does this key exist?
  40. gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1
  41. if [ $? -gt 0 ]; then
  42. echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
  43. exit 0
  44. fi
  45. # Find all files in the Maildir specified.
  46. echo $"Calling find"
  47. find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' $4|while read line; do
  48. gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS" "/tmp/msg_$USERNAME"
  49. # Check to see if there are differences between the existing
  50. # Maildir file and what was created by gpgit.pl
  51. diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;
  52. if [ $? -gt 0 ]; then
  53. # Preserve timestamps, set ownership.
  54. chown $USERNAME:$USERNAME "/tmp/msg_$USERNAME"
  55. chmod 600 "/tmp/msg_$USERNAME"
  56. touch "/tmp/msg_$USERNAME" --reference="$line"
  57. # Unlink the original Maildir message
  58. unlink "$line"
  59. # Strip message sizes, retain experimental flags
  60. # and status flags, and copy the file over.
  61. STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")
  62. cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"
  63. #Indexes must be rebuilt, weve modified Maildir.
  64. touch "/tmp/rebuild_index_$USERNAME"
  65. else
  66. echo $"Not copying, no differences between /tmp/msg_$USERNAME and $line"
  67. fi
  68. # Remove the temporary file
  69. unlink "/tmp/msg_$USERNAME"
  70. done
  71. # Remove Dovecot index and uids for regeneration.
  72. if [ -f "/tmp/rebuild_index_$USERNAME" ]; then
  73. echo $"Removing Dovecot indexes and uids"
  74. find "$MAIL_DIR" -type f -regex '.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete
  75. # Remove the temporary file
  76. unlink "/tmp/rebuild_index_$USERNAME"
  77. else
  78. echo -n $"No messages found needing GPG encryption, not"
  79. echo $"removing Dovecot indexes and UIDs."
  80. fi
  81. exit 0