diff --git a/Makefile b/Makefile index aa9725f..292abe9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -var_exclude = ZPASS_.* XDG_.* REMOTE_.* DISPLAY CONFIGFILE TMPDIR -fct_exclude = _tty_on +var_exclude = ZPASS_.* XDG_.* REMOTE_.* DISPLAY CONFIGFILE TMPDIR DEBUG +fct_exclude = _tty_on sftp_cmd ftps_cmd upload download list delete zpass: src/* lxsh -o zpass -M --exclude-var "$(var_exclude)" --exclude-fct "$(fct_exclude)" src/main.sh diff --git a/src/archive.sh b/src/archive.sh index 69c0548..844b5c1 100644 --- a/src/archive.sh +++ b/src/archive.sh @@ -29,7 +29,7 @@ pack() if [ -n "$ZPASS_REMOTE_ADDR" ] then ret=0 - sftp_upload "$1/$archive" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? + remote upload "$1/$archive" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? rm -f "$1/$archive" 2>/dev/null return $ret else @@ -88,7 +88,7 @@ create() { [ -n "$ZPASS_REMOTE_ADDR" ] && { ret=0 ssh "$ZPASS_REMOTE_ADDR" "mkdir -p '$datapath'" - sftp_upload "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? + remote upload "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$? rm -rf "$file" 2>/dev/null return $ret } diff --git a/src/crypt.sh b/src/crypt.sh index d32bf2e..0ad01cc 100644 --- a/src/crypt.sh +++ b/src/crypt.sh @@ -19,7 +19,7 @@ decrypt() # get remote file [ -n "$ZPASS_REMOTE_ADDR" ] && { file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION" - sftp_download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null || return $? + 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/file.sh b/src/file.sh index 832acfd..a9d4e5d 100644 --- a/src/file.sh +++ b/src/file.sh @@ -2,25 +2,20 @@ list_files() { if [ -n "$ZPASS_REMOTE_ADDR" ] ; then - echo "$cmd" | sftp_cmd -b- << EOF -cd "$datapath" -ls -1 -EOF + remote list else - ( - cd "$datapath" - ls -1 - ) + ( cd "$datapath" && ls -1 ) fi | grep "$(escape_chars "$ZPASS_EXTENSION")$" } remove_files() { - if [ -n "$ZPASS_REMOTE_ADDR" ] ; then - echo "$cmd" | sftp_cmd -b- << EOF -rm "$datapath/$N$ZPASS_EXTENSION" -EOF - else - rm "$datapath/$N$ZPASS_EXTENSION" - fi + for file + do + if [ -n "$ZPASS_REMOTE_ADDR" ] ; then + remote delete "$datapath/$file$ZPASS_EXTENSION" + else + rm "$datapath/$file$ZPASS_EXTENSION" + fi + done } diff --git a/src/help.sh b/src/help.sh index bfa54d1..b507701 100644 --- a/src/help.sh +++ b/src/help.sh @@ -26,20 +26,21 @@ usage() rm-cache Delete the cached key for this file. Shortcut 'rmc' [Config]: - *Variable* *Default value* *Description* + *Variable* *Default value* *Description* ------------------------------------------------------------------------------------------------------------------------ - CONFIGFILE '\$XDG_CONFIG_HOME/zpass/defaut.conf' Path to the config file to load - ZPASS_PATH '\$XDG_DATA_HOME/zpass' Folder containing password files - ZPASS_CACHE_PATH '\$XDG_CACHE_HOME/zpass' Path used for caching keys - ZPASS_FILE 'default' File to use for operations - ZPASS_KEY Key to use for encrypting/decrypting files - ZPASS_KEY_CACHE_TIME '60' Time a key stays in cache for decrypting, in seconds - ZPASS_CLIPBOARD_TIME '30' Time until clipboard gets cleared after copy, in seconds - ZPASS_UNK_OP_CALL 'copy' Operation to call on unrecognized first argument - ZPASS_RAND_LEN Length of random passwords generated by 'new' - ZPASS_REMOTE_ADDR SSH server the file is on - ZPASS_REMOTE_PORT '22' SSH server port - ZPASS_SSH_ID SSH private key to use + CONFIGFILE '\$XDG_CONFIG_HOME/zpass/defaut.conf' Path to the config file to load + ZPASS_PATH '\$XDG_DATA_HOME/zpass' Folder containing password files + ZPASS_CACHE_PATH '\$XDG_CACHE_HOME/zpass' Path used for caching keys + ZPASS_FILE 'default' File to use for operations + ZPASS_KEY Key to use for encrypting/decrypting files + ZPASS_KEY_CACHE_TIME '60' Time a key stays in cache for decrypting, in seconds + ZPASS_CLIPBOARD_TIME '30' Time until clipboard gets cleared after copy, in seconds + ZPASS_UNK_OP_CALL 'copy' Operation to call on unrecognized first argument + ZPASS_RAND_LEN Length of random passwords generated by 'new' + ZPASS_REMOTE_METHOD 'scp' Method to use for remote file. scp/sftp/ftps + ZPASS_REMOTE_ADDR Server the file is on + ZPASS_REMOTE_PORT Server port + ZPASS_SSH_ID SSH private key to use for scp/sftp All operations can be shortened to their first char unless specified Unknown first argument will perform the operation described in 'ZPASS_UNK_OP_CALL' on that argument diff --git a/src/main.sh b/src/main.sh index 5dcdae7..5d88d14 100644 --- a/src/main.sh +++ b/src/main.sh @@ -1,5 +1,7 @@ #!/bin/lxsh +[ "$DEBUG" = true ] && set -x + %include util.sh config.sh *.sh ## pre exec diff --git a/src/remote.sh b/src/remote.sh index 3e8358b..fa74322 100644 --- a/src/remote.sh +++ b/src/remote.sh @@ -1,24 +1,83 @@ +# $1 = cond value , $2 = precede , $3 = separator +cond_gen() { + [ -n "$1" ] && env printf "%q%s%q" "$2" "$3" "$1" +} + +# $@ = command +ftps_cmd() { + shift 3 + user=${ZPASS_REMOTE_ADDR%%@*} + host=${ZPASS_REMOTE_ADDR#*@} + lftp << EOF +set ftp:ssl-allow true ; set ssl:verify-certificate no ; set ftp:ssl-auth TLS +open ftp://$host$(cond_gen "$ZPASS_REMOTE_PORT" ":") +user $user $ZPASS_REMOTE_PASSWORD +$(cat) +EOF +} + # $@ = args sftp_cmd() { - [ -n "$ZPASS_REMOTE_ADDR" ] || return $? - if [ -n "$ZPASS_SSH_ID" ] ; then - sftp -i "$ZPASS_SSH_ID" "$@" "$ZPASS_REMOTE_ADDR" - else - sftp "$@" "$ZPASS_REMOTE_ADDR" - fi | grep -v "^sftp>" - return 0 + { sftp -b- $(cond_gen "$ZPASS_REMOTE_PORT" -P " ") $(cond_gen "$ZPASS_SSH_ID" -i " ") "$@" "$ZPASS_REMOTE_ADDR" || return $?; } | grep -v "^sftp>" || true } -# $1 = local file , $2 = remote file -sftp_upload() { - sftp_cmd -b- >/dev/null << E -put "$1" "$2" -E +# $@ args +scp_cmd() { + scp $(cond_gen "$ZPASS_REMOTE_PORT" -P " ") $(cond_gen "$ZPASS_SSH_ID" -i " ") "$@" } -# $1 = remote file , $2 = local file -sftp_download() { - sftp_cmd -b- >/dev/null << E -get "$1" "$2" -E +# $@ = args +ssh_cmd() { + ssh $(cond_gen "$ZPASS_REMOTE_PORT" -p " ") $(cond_gen "$ZPASS_SSH_ID" -i " ") "$@" +} + +# $1 = protocol , $2 = local file , $3 = remote file +upload() { + case $1 in + scp) scp_cmd "$2" "$ZPASS_REMOTE_ADDR:$3" ;; + sftp|ftps) "$1"_cmd >/dev/null << EOF +put "$2" "$3" +EOF + esac +} + +# $1 = protocol, $2 = remote file , $3 = local file +download() { + case $1 in + scp) scp_cmd "$ZPASS_REMOTE_ADDR:$2" "$3" ;; + sftp|ftps) ${1}_cmd >/dev/null << EOF +get "$2" "$3" +EOF + esac +} + +# $1 = protocol +list() { + case $1 in + scp) ssh_cmd "cd '$datapath' && ls -1" ;; + sftp|ftps) ${1}_cmd >/dev/null << EOF +cd "$datapath" +ls -1 +EOF + esac +} + +# $1 = protocol , $2 = file +delete() { + case $1 in + scp) ssh_cmd "rm '$2'" ;; + sftp|ftps) ${1}_cmd >/dev/null << EOF +rm "$2" +EOF + esac +} + +# $1 = action , $@ = arguments +remote() { + action=$1 + shift 1 + case "${ZPASS_REMOTE_METHOD-scp}" in + scp|sftp|ftps) $action "${ZPASS_REMOTE_METHOD-scp}" "$@" ;; + *) echo "Unknown remote method: $ZPASS_REMOTE_METHOD" ;; + esac }