]> gitweb.fluxo.info Git - keyringer.git/commitdiff
Support for encrypting a whole tree (closes #21)
authorSilvio Rhatto <rhatto@riseup.net>
Fri, 15 Nov 2013 00:57:59 +0000 (22:57 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Fri, 15 Nov 2013 00:57:59 +0000 (22:57 -0200)
ChangeLog
lib/keyringer/actions/encrypt
lib/keyringer/functions
share/man/keyringer.1.mdwn

index 69d58bbee8b5a4770a709256bf91297f15aee97f..b55493d47e9bb2f3c6bfcf871ebf69421de0936f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-11-14 -       Silvio Rhatto  <rhatto@riseup.net>
+
+       Support for encrypting a whole tree (closes #21)
+
 2013-11-14 - 0.2.8 Silvio Rhatto  <rhatto@riseup.net>
 
        Updated development workflow and version scheme
index aadb9fa94e6464b112fd3b194218b531570aec56..0a40bc18b92988f6b895365dc93a4845279802da 100755 (executable)
@@ -17,6 +17,24 @@ function keyringer_usage_encrypt_batch {
   keyringer_usage_encrypt $*
 }
 
+# Encrypt a file into the datastore
+function keyringer_encrypt {
+  local file="$1"
+  shift
+
+  if [ -z "$1" ]; then
+    return 1
+  fi
+
+  if [ "$*" != "-" ]; then
+    echo "Encrypting $*..."
+  fi
+
+  mkdir -p "$KEYDIR/`dirname "$file"`"
+  $GPG --use-agent --armor -e -s $(keyringer_recipients "$RECIPIENTS_FILE") --yes --output "$KEYDIR/$file" "$*"
+  printf "\n"
+}
+
 # Usage
 if [ -z "$2" ]; then
   keyringer_action_usage
@@ -26,26 +44,31 @@ fi
 # Aditional parameters
 if [ ! -z "$3" ]; then
   # Set secret name and original file
-  FILE="$2"
+  BASEPATH="$2"
   shift 2
   UNENCRYPTED_FILE="$*"
 
-  # Get original file EXTENSION
-  FILENAME="$(basename "$UNENCRYPTED_FILE")"
-  EXTENSION="${FILENAME##*.}"
-
-  # Append file extension in the secret name
-  #
-  # Useful when opening files and the application needs the
-  # extension to guess the file type.
-  if ! echo $FILE | grep -q -e "\.$EXTENSION$"; then
-    FILE="$FILE.$EXTENSION"
+  if [ ! -d "$UNENCRYPTED_FILE" ] && echo "$UNENCRYPTED_FILE" | grep -q -e '\.'; then
+    # Get original file EXTENSION
+    FILENAME="$(basename "$UNENCRYPTED_FILE")"
+    EXTENSION="${FILENAME##*.}"
+
+    # Append file extension in the secret name
+    #
+    # Useful when opening files and the application needs the
+    # extension to guess the file type.
+    if ! echo $BASEPATH | grep -q -e "\.$EXTENSION$"; then
+      echo "Appending '$EXTENSION' into secret name..."
+      FILE="$BASEPATH.$EXTENSION"
+    fi
+  else
+    FILE="$BASEPATH"
   fi
 
   keyringer_get_new_file $FILE
 
-  if [ ! -f "$UNENCRYPTED_FILE" ]; then
-    echo "Error: cannot encrypt $UNENCRYPTED_FILE: file not found."
+  if [ ! -e "$UNENCRYPTED_FILE" ]; then
+    echo "Error: cannot encrypt $UNENCRYPTED_FILE: path not found."
     exit 1
   fi
 else
@@ -57,9 +80,7 @@ fi
 # Set recipients file
 keyringer_set_recipients "$FILE"
 
-# Encrypt
-mkdir -p "$KEYDIR/`dirname $FILE`"
-
+# Verbosity
 if [ "$BASENAME" == "encrypt" ]; then
   # Only display directions if we're running encrypt, not encrypt-batch
   if [ "$UNENCRYPTED_FILE" == "-" ]; then
@@ -67,7 +88,23 @@ if [ "$BASENAME" == "encrypt" ]; then
   fi
 fi
 
-$GPG --use-agent --armor -e -s $(keyringer_recipients "$RECIPIENTS_FILE") --yes --output "$KEYDIR/$FILE" "$UNENCRYPTED_FILE"
+# Encrypt
+if [ "$UNENCRYPTED_FILE" != "-" ] && [ -d "$UNENCRYPTED_FILE" ]; then
+  # Time to go recursive
+  BASEPATH="`basename $FILE .asc`"
+  FILEPATH="`dirname "$UNENCRYPTED_FILE"`"
+  find $UNENCRYPTED_FILE | while read file; do
+    if [ ! -d "$file" ]; then
+      dir="`dirname "$file" | sed -e "s|^$FILEPATH|$BASEPATH|g"`"
+      keyringer_get_new_file `basename "$file"`
+      keyringer_encrypt "$dir/$FILE" $file
+    fi
+  done
+
+  FILE="$OLD_FILE"
+else
+  keyringer_encrypt $FILE $UNENCRYPTED_FILE
+fi
 
 err="$?"
 
index 4c061987721fd0e5705368cfecb6cdf9b9ce5368..dd316766c4f202c101e577fb0c647d0a002ff3b1 100755 (executable)
@@ -431,10 +431,10 @@ function keyringer_get_new_file {
   fi
 
   # Sanitize and complete file name
-  FILE="`echo $FILE | sed -e s/[^A-Za-z0-9.\/\-]/_/g`"
+  FILE="`echo $FILE | sed -e 's/[^A-Za-z0-9.\/\-]/_/g'`"
 
   # Warn user about file name change
-  if [ "`basename $*`" != "`basename $FILE`" ]; then
+  if [ "`basename "$*"`" != "`basename $FILE`" ]; then
     echo "Sanitizing destination filename to `basename $FILE`"
   fi
 
index ee035e3ba86bd5b40a9e821b310fefeb80a7bfa0..876f774b7d8e8afee8e87aa89a8cb9a473b06e84 100644 (file)
@@ -94,10 +94,12 @@ edit <*secret*>
 
 encrypt <*secret*> [*file*]
 :   Encrypts content from standard input or *file* into *secret* pathname. No spaces
-    are supported in the *secret* name.
+    are supported in the *secret* name. If *file* is actually a folder, keyringer
+    will recursivelly encrypt all it's contents.
 
-encrypt-batch <*secret*>
-:   Encrypt content, batch mode.
+encrypt-batch <*secret*> [*file*]
+:   Encrypt content, batch mode. Behavior is identical to *encrypt* action, but less
+    verbose. Useful inside scripts.
 
 genpair <*ssh*|*gpg*|*ssl*|*ssl-self*> [*options*]
 :   Wrapper to generate encryption key-pairs, useful for automated key deployment.