diff options
Diffstat (limited to 'lib/keyringer/actions')
-rwxr-xr-x | lib/keyringer/actions/append | 28 | ||||
-rwxr-xr-x | lib/keyringer/actions/del | 8 | ||||
-rwxr-xr-x | lib/keyringer/actions/encrypt | 71 | ||||
-rwxr-xr-x | lib/keyringer/actions/ls | 7 | ||||
l--------- | lib/keyringer/actions/rm | 1 | ||||
-rwxr-xr-x | lib/keyringer/actions/tree | 31 |
6 files changed, 103 insertions, 43 deletions
diff --git a/lib/keyringer/actions/append b/lib/keyringer/actions/append index e945bff..e307056 100755 --- a/lib/keyringer/actions/append +++ b/lib/keyringer/actions/append @@ -10,29 +10,11 @@ source "$LIB" || exit 1 # Get file keyringer_get_file "$2" -OLDIFS="$IFS" -IFS=$'\n' - -CONTENT=($(keyringer_exec decrypt "$BASEDIR" "$FILE")) - +# Only display directions if we're running append, not append-batch if [ "$BASENAME" == "append" ]; then - # only display directions if we're running append, not append-batch - printf "\n%s currently has %d lines\n\n" "$FILE" "${#CONTENT[@]}" - printf "Now please write the content to be appended on %s, finnishing with Ctrl-D:\n" "$FILE" -fi - -APPEND=($(cat -)) - -NEW=( ${CONTENT[@]} ${APPEND[@]} ) - -for element in $(seq 0 $((${#NEW[@]} - 1))); do - echo ${NEW[$element]} -done | keyringer_exec encrypt-batch $BASEDIR $FILE - -err="$?" - -if [ "$err" != "0" ]; then - exit "$err" + printf "Please write the content to be appended on %s, finnishing with Ctrl-D:\n" "$FILE" fi -IFS="$OLDIFS" +# Append content to an existing secret +( keyringer_exec decrypt "$BASEDIR" "$FILE" && cat ) | \ + keyringer_exec encrypt-batch $BASEDIR $FILE diff --git a/lib/keyringer/actions/del b/lib/keyringer/actions/del index babd212..d160ac4 100755 --- a/lib/keyringer/actions/del +++ b/lib/keyringer/actions/del @@ -10,7 +10,13 @@ source "$LIB" || exit 1 # Get file keyringer_get_file "$2" +# Set options +if [ ! -z "$3" ]; then + shift 2 + OPTS="$*" +fi + # Remove if [ -d "$BASEDIR/.git" ]; then - keyringer_exec git "$BASEDIR" rm "keys/$FILE" + keyringer_exec git "$BASEDIR" rm $OPTS "keys/$FILE" fi diff --git a/lib/keyringer/actions/encrypt b/lib/keyringer/actions/encrypt index aadb9fa..0a40bc1 100755 --- a/lib/keyringer/actions/encrypt +++ b/lib/keyringer/actions/encrypt @@ -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="$?" diff --git a/lib/keyringer/actions/ls b/lib/keyringer/actions/ls index ec8080b..bb66263 100755 --- a/lib/keyringer/actions/ls +++ b/lib/keyringer/actions/ls @@ -10,7 +10,10 @@ source "$LIB" || exit 1 # Aditional parameters CWD="`pwd`" -# Run list command +# Avoid leading slash shift -cd "$KEYDIR" && ls $* +ARGS="`echo "$*" | sed -e "s|^/*||"`" + +# Run list command +cd "$KEYDIR" && ls $ARGS cd "$CWD" diff --git a/lib/keyringer/actions/rm b/lib/keyringer/actions/rm new file mode 120000 index 0000000..1a7ac23 --- /dev/null +++ b/lib/keyringer/actions/rm @@ -0,0 +1 @@ +del
\ No newline at end of file diff --git a/lib/keyringer/actions/tree b/lib/keyringer/actions/tree new file mode 100755 index 0000000..8e94cb0 --- /dev/null +++ b/lib/keyringer/actions/tree @@ -0,0 +1,31 @@ +#!/bin/bash +# +# List keys. +# + +# Thanks http://www.centerkey.com/tree/ +function keyringer_tree { + ls -R $* | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' +} + +# Load functions +LIB="`dirname $0`/../functions" +source "$LIB" || exit 1 + +# Aditional parameters +CWD="`pwd`" + +# Avoid leading slash +shift +ARGS="`echo "$*" | sed -e "s|^/*||"`" + +# Check implementation +if which tree &> /dev/null; then + TREE="tree" +else + TREE="keyringer_tree" +fi + +# Run list command +cd "$KEYDIR" && $TREE $ARGS +cd "$CWD" |