From b97fdec199045aa7de9902ba587696e9557d41dd Mon Sep 17 00:00:00 2001 From: zawz Date: Thu, 12 Aug 2021 18:31:15 +0200 Subject: [PATCH] fix remote file create --- src/archive.sh | 52 ++++++++++++++++++++++++++++++++++++-------------- src/crypt.sh | 1 + src/main.sh | 3 ++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/archive.sh b/src/archive.sh index 265974c..7c09c03 100644 --- a/src/archive.sh +++ b/src/archive.sh @@ -62,18 +62,50 @@ archive_exec() # no argument create_file() { - if [ -f "$file" ] - then + if [ -n "$remote_host" ] ; then + file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION" + tmpfile=$file + if remote download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null 2>&1 ; then + local archive_tmpdir="$TMPDIR/zpass_$(randalnum 20)" + + # unpack locally + remote_host= unpack "$archive_tmpdir" || { + rm -rf "$archive_tmpdir" "$file" + return 1 + } + # pack and send + pack "$archive_tmpdir" || { + echo "Encryption error" >&2 + rm -rf "$archive_tmpdir" "$file" + return 1 + } + # cleanup + rm -rf "$archive_tmpdir" "$file" + else + # get key + [ -z "$ZPASS_KEY" ] && { + ZPASS_KEY=$(new_key_with_confirm) || { echo "Cancelled" >&2 && return 100 ; } + } + # create archive + tar -cf - -T /dev/null | encrypt "$ZPASS_KEY" > "$file" || { + echo "Encryption error" >&2 + rm -f "$file" + return 1 + } + + ret=0 + remote create "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? + rm -f "$file" + return $ret + fi + + elif [ -f "$file" ] ; then archive_tmpdir="$TMPDIR/zpass_$(randalnum 20)" # pack n repack with no tmp key: create new unpack "$archive_tmpdir" || return $? pack "$archive_tmpdir" || { echo "Encryption error" >&2 && return 1 ; } rm -rf "$archive_tmpdir" else - # if remote: file tmp and try to get file - [ -n "$remote_host" ] && { - file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION" - } # get key [ -z "$ZPASS_KEY" ] && { ZPASS_KEY=$(new_key_with_confirm) || { echo "Cancelled" >&2 && return 100 ; } @@ -81,17 +113,9 @@ create_file() { # create archive tar -cf - -T /dev/null | encrypt "$ZPASS_KEY" > "$file" || { echo "Encryption error" >&2 - # echo "$tmperr" >&2 rm "$file" return 1 } - # if is remote: create remote file - [ -n "$remote_host" ] && { - ret=0 - remote create "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? - rm -rf "$file" 2>/dev/null - return $ret - } fi return 0 } diff --git a/src/crypt.sh b/src/crypt.sh index befb707..1e973ce 100644 --- a/src/crypt.sh +++ b/src/crypt.sh @@ -19,6 +19,7 @@ decrypt() # get remote file [ -n "$remote_host" ] && { file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION" + tmpfile=$file remote download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null || return $? } cat "$file" >/dev/null 2>&1 || { echo "File doesn't exist. Use 'zpass create' to create the file" >&2 && return 1; } # no file diff --git a/src/main.sh b/src/main.sh index ebfd4ae..118dee6 100644 --- a/src/main.sh +++ b/src/main.sh @@ -1,8 +1,9 @@ #!/bin/lxsh +unset archive_tmpdir tmpfile _stop() { stty echo - rm -rf "$archive_tmpdir" + rm -rf "$archive_tmpdir" "$tmpfile" } [ "$DEBUG" = true ] && set -x