From 5148e66a945e7ee02df29cc75b9b5fa07223b571 Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 16 Apr 2021 14:11:19 +0200 Subject: [PATCH] fix cache and clipboard --- src/archive.sh | 8 ++++---- src/cache.sh | 6 +----- src/clipboard.sh | 51 ++++++++++++++++++++++++++++++++++++------------ src/config.sh | 3 ++- src/main.sh | 4 ++-- src/operation.sh | 27 ++++++++++++++----------- src/util.sh | 11 ++++++----- 7 files changed, 70 insertions(+), 40 deletions(-) diff --git a/src/archive.sh b/src/archive.sh index 45f423d..1c35c9e 100644 --- a/src/archive.sh +++ b/src/archive.sh @@ -15,7 +15,7 @@ unpack() { pack() { # clean empty dirs - archive="archive_$(randalnum 5)" + archive="archive_$(randalnum 20)" ( cd "$1" || exit $? if [ -n "$2" ] @@ -46,8 +46,8 @@ archive_exec() { err=0 # tmp files - tmpdir="$TMPDIR/zpass_$(randalnum 5)" - keyfile="$tmpdir/$(randalnum 5).key" + tmpdir="$TMPDIR/zpass_$(randalnum 20)" + keyfile="$tmpdir/$(randalnum 20).key" # operation ( # unpack @@ -67,7 +67,7 @@ archive_exec() create() { if [ -f "$file" ] then - tmpdir="$TMPDIR/zpass_$(randalnum 5)" + tmpdir="$TMPDIR/zpass_$(randalnum 20)" # pack n repack with no tmp key: create new unpack "$tmpdir" || return $? pack "$tmpdir" || { echo "Encryption error" >&2 && return 1 ; } diff --git a/src/cache.sh b/src/cache.sh index 827ee71..9178c0f 100644 --- a/src/cache.sh +++ b/src/cache.sh @@ -20,11 +20,7 @@ get_key_cached() { delete_cache() { if [ "$1" -gt 0 ] 2>/dev/null then - for I in $(screen -ls | grep "zpass_$(keyfile)" | awk '{print $1}') - do - screen -S "$I" -X stuff "^C" - done - screen -dmS "zpass_$(keyfile)" sh -c "sleep $1 ; $0 rmc" # call zpass with cache delete + nohup sh -c "sleep $1;rm -f '$cachepath/$(keyfile)'" >/dev/null 2>&1 & else rm -f "$cachepath/$(keyfile)" 2>/dev/null fi diff --git a/src/clipboard.sh b/src/clipboard.sh index 8db7bf8..421581b 100644 --- a/src/clipboard.sh +++ b/src/clipboard.sh @@ -9,22 +9,48 @@ clipboard() in="$in $ln" done - printf "%s" "$in" | xclip -selection clipboard - which wl-copy >/dev/null 2>&1 && printf "%s" "$in" | wl-copy + if [ "$XDG_SESSION_TYPE" = x11 ] ; then + printf "%s" "$in" | xclip -selection clipboard + elif [ "$XDG_SESSION_TYPE" = wayland ] ; then + printf "%s" "$in" | wl-copy + fi +} + +get_clipboard() { + if [ "$XDG_SESSION_TYPE" = x11 ] ; then + xclip -selection clipboard -o + elif [ "$XDG_SESSION_TYPE" = wayland ] ; then + wl-paste + fi } # $1 = delay in sec clipboard_clear() { if [ -n "$1" ] then - for I in $(screen -ls | grep "$fname"_clipboard | awk '{print $1}') - do - screen -S "$I" -X stuff "^C" - done - screen -dmS "$fname"_clipboard sh -c "sleep $1 - xclip -selection clipboard < /dev/null - which wl-copy 2>&1 && wl-copy < /dev/null - sleep 1" + clipval=$(get_clipboard | sed 's|"|\"|g;s|\\|\\|g;' ) + tmpfifo="$TMPDIR/zpass_tmpfifo_$(randalnum 20)" + mkfifo "$tmpfifo" + nohup sh -c '#LXSH_PARSE_MINIFY + pass=$(cat "$2") + rm -f "$2" + sleep $1 + clip=$( + if [ "$XDG_SESSION_TYPE" = x11 ] ; then + xclip -selection clipboard -o 2>/dev/null + elif [ "$XDG_SESSION_TYPE" = wayland ] ; then + wl-paste + fi + ) + [ "$clip" != "$pass" ] && exit 1 + + if [ "$XDG_SESSION_TYPE" = x11 ] ; then + xclip -selection clipboard < /dev/null + elif [ "$XDG_SESSION_TYPE" = wayland ] ; then + wl-copy < /dev/null + fi + ' zpass_clipclear "$1" "$tmpfifo" >/dev/null 2>&1 & + get_clipboard > "$tmpfifo" else echo | clipboard fi @@ -32,14 +58,15 @@ clipboard_clear() { copy_check() { - if ps -e | grep -qi wayland + if [ "$XDG_SESSION_TYPE" = wayland ] then which wl-copy >/dev/null 2>&1 || error 1 "ERROR: running wayland but wl-clipboard is not installed" - elif [ -n "$DISPLAY" ] + elif [ "$XDG_SESSION_TYPE" = x11 ] then which xclip >/dev/null 2>&1 || error 1 "ERROR: running X but xclip is not installed" else error 1 "ERROR: no graphical server detected" fi + return 0 } diff --git a/src/config.sh b/src/config.sh index 8998f10..2828118 100644 --- a/src/config.sh +++ b/src/config.sh @@ -12,7 +12,7 @@ configpath="$HOME/.config/zpass" [ -z "$TMPDIR" ] && TMPDIR=/tmp # stash env config -tmpenv="$TMPDIR/zpassenv_$(randalnum 5)" +tmpenv="$TMPDIR/zpassenv_$(randalnum 20)" env | grep '^ZPASS_.*=' | sed "s/'/'\\\''/g;s/=/='/;s/$/'/g" > "$tmpenv" # load config file @@ -41,6 +41,7 @@ datapath="${datapath#\~/}" [ -z "$ZPASS_REMOTE_ADDR" ] && [ "$(echo "$datapath" | cut -c1)" != '/' ] && datapath="$HOME/$datapath" file="$datapath/$ZPASS_FILE$ZPASS_EXTENSION" +FILE=$file [ -z "$ZPASS_REMOTE_ADDR" ] && { mkdir -p "$datapath" 2>/dev/null || error 1 "Could not create '$datapath'"; } mkdir -p "$cachepath" 2>/dev/null && chmod -R go-rwx "$cachepath" 2>/dev/null diff --git a/src/main.sh b/src/main.sh index 14df22f..9ccd1f3 100644 --- a/src/main.sh +++ b/src/main.sh @@ -17,8 +17,8 @@ case $arg in lsf|list-files) list_files ;; rmf|rm-file) remove_files "$@" ;; cc|cache-clear) clear_cache 2>/dev/null ;; - ch|cached) get_key_cached >/dev/null 2>&1 ;; - rmc|rm-cache) delete_cache 0 >/dev/null 2>&1 ;; + ch|cached) get_key_cached >/dev/null ;; + rmc|rm-cache) delete_cache 0 >/dev/null ;; c|create) create ;; t|tree) sanitize_paths "$@" && _tree "$@" ;; s|set) sanitize_paths "$1" && _set "$@" ;; diff --git a/src/operation.sh b/src/operation.sh index 821e632..31730b2 100644 --- a/src/operation.sh +++ b/src/operation.sh @@ -10,7 +10,7 @@ _tree() for N do [ $# -gt 1 ] && echo "> $N:" - echo "$fulltree" | grep "^$(escape_chars "$N")" | sed "s|^$N||g ; "' s|^/||g ; /\/$/d ; /^$/d' + echo "$fulltree" | grep "^$(escape_chars "$N")" | sed "s|^$N||g;s|^/||g;/\/$/d;/^$/d" done else @@ -22,7 +22,7 @@ _tree() get() { [ $# -lt 1 ] && return 1 - __NOPACK=y archive_exec sh -c ' + __NOPACK=y archive_exec sh -c '#LXSH_PARSE_MINIFY for N do ( @@ -31,7 +31,7 @@ get() exit 1 ) || { echo "$N: not found" >&2 && exit 1; } done - ' sh "$@" + ' zpass "$@" } # $1 = path @@ -45,13 +45,15 @@ copy() new() { [ $# -lt 1 ] && return 1 - archive_exec sh -c " + archive_exec sh -c '#LXSH_PARSE_MINIFY + len=$1 + shift 1 for N do - mkdir -p \"\$(dirname \"\$N\")\" || exit \$? - { tr -cd 'a-zA-Z0-9!-.' < /dev/urandom | head -c $ZPASS_RAND_LEN && echo; } > \"\$N\" || exit \$? + mkdir -p "$(dirname "$N")" || exit $? + { tr -cd "a-zA-Z0-9\!-." < /dev/urandom | head -c$len && echo; } > "$N" || exit $? done - " sh "$@" + ' zpass "$ZPASS_RAND_LEN" "$@" } # $1 = path , $@ = value @@ -60,7 +62,9 @@ _set() [ $# -lt 1 ] && return 1 ref=$1 shift 1 - archive_exec sh -c "mkdir -p '$(dirname "$ref")' && printf '%s\n' '$*' > '$ref'" + archive_exec sh -c '#LXSH_PARSE_MINIFY + mkdir -p "$(dirname "$1")" && printf "%s\n" "$2" > "$1" + ' zpass "$ref" "$*" } add() @@ -77,13 +81,14 @@ add() fileset() { contents=$(cat "$2") || return $? - _set "$1" "$2" + _set "$1" "$contents" } move() { [ $# -lt 1 ] && return 1 - archive_exec sh -c 'set -e + archive_exec sh -c '#LXSH_PARSE_MINIFY + set -e for last ; do true ; done if [ "$#" -gt 2 ] ; then mkdir -p "$last" @@ -91,5 +96,5 @@ move() mkdir -p "$(dirname "$last")" fi mv -f -- "$@" - ' sh "$@" + ' zpass "$@" } diff --git a/src/util.sh b/src/util.sh index e8eb09d..aeccbb5 100644 --- a/src/util.sh +++ b/src/util.sh @@ -30,17 +30,18 @@ sanitize_paths() # $1 = file getpath() { - if [ -n "$ZPASS_REMOTE_ADDR" ] - then - echo "$ZPASS_REMOTE_PORT:$ZPASS_REMOTE_ADDR:$file" + if [ -n "$ZPASS_REMOTE_ADDR" ] ; then + echo "$ZPASS_REMOTE_PORT:$ZPASS_REMOTE_ADDR:$FILE" + elif [ "$(echo "$FILE" | cut -c1)" = / ] ; then + echo "$FILE" else - echo "$(pwd)/$file" + echo "$(pwd)/$FILE" fi } # $1 = file filehash(){ - getpath "$file" | md5sum | cut -d' ' -f1 + getpath "$FILE" | md5sum | cut -d' ' -f1 } keyfile(){