From bb1101c36df617921451029bb2ecfbc08a0215b8 Mon Sep 17 00:00:00 2001 From: Jon-William Lewis Date: Tue, 16 Feb 2016 21:35:42 -0600 Subject: [PATCH] Added store decryption/encryption capability --- build/sns.sh | 55 ++++++++++++++++++++++++++++++++++++----- header.sh | 3 ++- install.sh.gpg | Bin 0 -> 1207 bytes src/main/stage1.sns.sh | 52 ++++++++++++++++++++++++++++++++++---- 4 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 install.sh.gpg diff --git a/build/sns.sh b/build/sns.sh index 3073c0c..edb43a1 100755 --- a/build/sns.sh +++ b/build/sns.sh @@ -27,8 +27,9 @@ readonly NOTES_DIR="$ROOT_DIR"/notes readonly TMP_DIR="$ROOT_DIR"/tmp readonly CONFIG_FILE="$ROOT_DIR/sns.conf" -#Color codes for error reporting +#Color codes for messages readonly RED_COLOR='\033[1;31m' +readonly YELLOW_COLOR='\033[1;33m' readonly RESET_COLOR='\033[0m' #Print the program header to stdout @@ -251,32 +252,74 @@ function list(){ if [ -r "$CONFIG_FILE" ]; then source "$CONFIG_FILE" verify_store -elif [ $1 != "-i" ]; then +elif [ "$1" != "-i" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "Configuration not found. Please run sns -i." exit 5 #ERR_NO_STORE fi +cd "$NOTES_DIR" if [ "$ENCRYPTION" == "TRUE" ]; then + # If the user chose not to decrypt notes before, clear that preference. + if [ -r "$NOTES_DIR"/.do_not_decrypt ]; then + rm "$NOTES_DIR"/.do_not_decrypt; + fi + # Check if GPG is installed. if [ ! -r "$(which gpg)" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "Encryption was specified, but GPG is not installed." exit 100 + # Check if we have a GPG recipient elif [ -z "$PUBKEY" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "No GPG recipient was provided in $CONFIG_FILE. " exit 110 + # All is good. If any previously unencrypted notes exist, encrypt them. + # No harm in extra security. else - cd "$NOTES_DIR" find . -type f -name "*.$EXT" | grep -v "gpg" | while read TMP_NOTE; do NOTE="${TMP_NOTE%.$EXT}.gpg.$EXT" - echo $NOTE encrypt - if [ -r $NOTE ]; then - rm $TMP_NOTE + if [ -r "$NOTE" ]; then + printf " $YELLOW_COLOR!$RESET_COLOR %s\n" "Encrypted ${NOTE%.$EXT}" + rm "$TMP_NOTE" fi done + fi +# If encryption isn't enabled, make sure either all notes are decrypted or the user +# does not wish to decrypt all notes. +else + if [ ! -r "$NOTES_DIR"/.do_not_decrypt ]; then + if [ -n $(find "$NOTES_DIR" -type f -name "*.gpg.$EXT" > /dev/null) ]; then + while true; do + read -p "Would you like to de-encrypt previously encrypted notes? " YN + case $YN in + [Yy]* ) + read -s -p "Please enter your passphrase: " PASSPHRASE + cd "$NOTES_DIR" + find . -type f -name "*.gpg.$EXT" | while read -r NOTE; do + gpg\ + --passphrase "$PASSPHRASE"\ + -o "${NOTE%.gpg.note}.note"\ + --decrypt "$NOTE" >> /dev/null 2>&1 + if [ -r "${NOTE%.gpg.note}.note" ]; then + printf " $YELLOW_COLOR!$RESET_COLOR %s\n"\ + "De-encrypted ${NOTE%.gpg.$EXT}" + rm "$NOTE"; + fi + done + break;; + [Nn]* ) + # Remember the user's preference. + touch "$NOTES_DIR/.do_not_decrypt" + break;; + *) + printf " $RED_COLOR!$RESET_COLOR %s\n" "Please enter Y or N" + ;; + esac + done + fi fi fi #============================================================================== diff --git a/header.sh b/header.sh index 5a27ea1..21c026d 100644 --- a/header.sh +++ b/header.sh @@ -32,8 +32,9 @@ readonly NOTES_DIR="\$ROOT_DIR"/notes readonly TMP_DIR="\$ROOT_DIR"/tmp readonly CONFIG_FILE="\$ROOT_DIR/sns.conf" -#Color codes for error reporting +#Color codes for messages readonly RED_COLOR='\033[1;31m' +readonly YELLOW_COLOR='\033[1;33m' readonly RESET_COLOR='\033[0m' #Print the program header to stdout diff --git a/install.sh.gpg b/install.sh.gpg new file mode 100644 index 0000000000000000000000000000000000000000..81deee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x^WgfhEpLu>04k5WJSV>UOttnl8XcX>e7^BhBkmy4kqppQc52 zLO+mwfR+(ikfgQvmnluToudK>HtNhuELP{tKrWIXhPe8~BVeBA&si?-5xqR=EBZ0- VvoD$giw)uCHzL+7*=g&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "Configuration not found. Please run sns -i." exit 5 #ERR_NO_STORE fi +cd "$NOTES_DIR" if [ "$ENCRYPTION" == "TRUE" ]; then + # If the user chose not to decrypt notes before, clear that preference. + if [ -r "$NOTES_DIR"/.do_not_decrypt ]; then + rm "$NOTES_DIR"/.do_not_decrypt; + fi + # Check if GPG is installed. if [ ! -r "$(which gpg)" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "Encryption was specified, but GPG is not installed." exit 100 + # Check if we have a GPG recipient elif [ -z "$PUBKEY" ]; then >&2 printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ "No GPG recipient was provided in $CONFIG_FILE. " exit 110 + # All is good. If any previously unencrypted notes exist, encrypt them. + # No harm in extra security. else - cd "$NOTES_DIR" find . -type f -name "*.$EXT" | grep -v "gpg" | while read TMP_NOTE; do NOTE="${TMP_NOTE%.$EXT}.gpg.$EXT" - echo $NOTE encrypt - if [ -r $NOTE ]; then - rm $TMP_NOTE + if [ -r "$NOTE" ]; then + printf " $YELLOW_COLOR!$RESET_COLOR %s\n" "Encrypted ${NOTE%.$EXT}" + rm "$TMP_NOTE" fi done + fi +# If encryption isn't enabled, make sure either all notes are decrypted or the user +# does not wish to decrypt all notes. +else + if [ ! -r "$NOTES_DIR"/.do_not_decrypt ]; then + if [ -n $(find "$NOTES_DIR" -type f -name "*.gpg.$EXT" > /dev/null) ]; then + while true; do + read -p "Would you like to de-encrypt previously encrypted notes? " YN + case $YN in + [Yy]* ) + read -s -p "Please enter your passphrase: " PASSPHRASE + cd "$NOTES_DIR" + find . -type f -name "*.gpg.$EXT" | while read -r NOTE; do + gpg\ + --passphrase "$PASSPHRASE"\ + -o "${NOTE%.gpg.note}.note"\ + --decrypt "$NOTE" + if [ -r "${NOTE%.gpg.note}.note" ]; then + printf " $YELLOW_COLOR!$RESET_COLOR %s\n"\ + "De-encrypted ${NOTE%.gpg.$EXT}" + rm "$NOTE"; + fi + done + break;; + [Nn]* ) + # Remember the user's preference. + touch "$NOTES_DIR/.do_not_decrypt" + break;; + *) + printf " $RED_COLOR!$RESET_COLOR %s\n" "Please enter Y or N" + ;; + esac + done + fi fi fi