123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #!/usr/bin/env sh
- multi=false
- clipboard=false
- type=false
- add=false
- file=""
- dir=""
- checkmenu(){
- for i in "$@"; do
- if command -v "$i" >/dev/null 2>&1; then
- echo "$i";
- break;
- fi
- done
- }
- if [ -n "$WAYLAND_DISPLAY" ]; then
- x11=false
- if [ -z "$DMENU_COMMAND" ]; then
- dmenu="$(checkmenu dmenu-wl bemenu tofi)"
- case "$dmenu" in
- rofi)
- dmenu="rofi -dmenu"
- ;;
- esac
- else
- dmenu="$DMENU_COMMAND"
- fi
- else
- x11=true
- if [ -z "$DMENU_COMMAND" ]; then
- dmenu="$(checkmenu dmenu rofi bemenu)"
- else
- dmenu="$DMENU_COMMAND"
- fi
- fi
- if [ -z "$dmenu" ]; then
- echo "No menu found, specify menu with the DMENU_COMMAND environment variable or install a known menu"
- exit 1;
- fi
- usage="$(printf "USAGE:\nneopassmenu [OPTS] [Pass Entry]\n\nOptions:\n-a\t\tGenerate or modify a password\n-c\t\tCopy to clipboard instead of printing to stdout\n-t\t\tSimulate typing instead of printing to stdout\n-m\t\tSelect a specific line from a multiline file\n-h\\t\tPrint usage information\n\nCONFIGURATION:\nEnvironement variables:\nDMENU_COMMAND\tSpecify dmenu command (default: dmenu)")"
- cd "${PASSWORD_STORE_DIR:-"$HOME/.password-store"}" || exit 2
- while [ "$#" -gt 0 ]; do
- case "$1" in
- -a)
- add=true
- shift
- ;;
- -c)
- clipboard=true
- type=false
- shift
- ;;
- -t)
- clipboard=false
- type=true
- shift
- ;;
- -m)
- multi=true
- shift
- ;;
- -h)
- echo "$usage"
- exit 0
- ;;
- *)
- if [ -f "$1" ] || [ -f "${1}.gpg" ]; then
- file="$1"
- shift
- elif [ -d "$1" ]; then
- dir="$1"
- shift
- else
- echo "$usage" >&2
- exit 1
- fi
- ;;
- esac
- done
- if [ -z "$file" ]; then
- while true; do
- file="$(printf "..\n%s" "$(find "./$dir" -maxdepth 1)" | sed -e 's#^'"./$dir"'##;s#^/##;s/.gpg$//' -e '/^$/d; /^.gpg-id$/d; /^.git\(attributes\)\{0,1\}$/d' | $dmenu)"
- if [ -z "$dir" ]; then
- newdir="$file"
- else
- newdir="${dir%/}/$file"
- fi
- if [ -z "$file" ]; then
- exit 127
- elif [ "$file" = ".." ]; then
- dir="${dir%/}"
- if echo "$dir" | grep -q "/"; then
- dir="${dir%/*}"
- else
- dir=""
- fi
- elif [ -d "$newdir" ]; then
- dir="$newdir"
- elif [ -f "$newdir" ] || [ -f "${newdir}.gpg" ] || "$add"; then
- file="$newdir"
- break
- else
- break
- fi
- done
- fi
- if "$add"; then
- if [ -f "${file}.gpg" ]; then
- pass "$file" > /tmp/pass.txt
- fi
- printf "\n\n%s" "$(dd count=2 if=/dev/random of=/dev/stdout 2>/dev/null ibs=512 obs=512 | uuencode -m /dev/stdout | tail -n +2 | tr -d '\n')" >> /tmp/pass.txt
- fileopener /tmp/pass.txt
- if [ -n "$(cat /tmp/pass.txt)" ]; then
- save="$(printf "yes\nno" | $dmenu -p "Save changes? ")"
- if [ "$save" = "yes" ]; then
- cat /tmp/pass.txt | pass add -m -f "$file"
- fi
- rm /tmp/pass.txt
- exit 0
- fi
- rm /tmp/pass.txt
- exit 1
- fi
- if ! [ -f "$file" ] && ! [ -f "${file}.gpg" ]; then
- exit 127
- fi
- if $clipboard && ! $multi; then
- printf "%s" "$(pass "$file")" | cb copy
- exit 0
- elif $type && ! $multi; then
- if $x11; then
- printf "%s" "$(pass "$file")" | xdotool type --clearmodifiers --file -
- else
- printf "%s" "$(pass "$file")" | ydotool type --file -
- fi
- exit 0
- fi
- password_lines="$(pass "$file")"
- if ! $multi; then
- echo "$password_lines"
- exit 0
- fi
- password="$(echo "$password_lines" | sed '{ h; s/^.$//; s/^..//; s/./*/g; x; s/^\(..\).*/\1/; G; s/\n//; }' | $dmenu)"
- # Sed script:
- # Copy pattern space (line) to hold space. Pattern space: Foo Hold space: Foo
- # If the patter space is a single character, delete the character (prevents "a" to be outputted as "aa")
- # Delete the first two characters of the patter space. Pattern Space: o; Hold space: Foo
- # Replace all the characters in the pattern space with the literal "*". Pattern Space: *; Hold space: Foo
- # Swap pattern space with hold space. Pattern Space: Foo Hold space: *
- # Match the first two characters of the pattern space, then all the rest of the characters
- # substitute with the whole match (entire line) with the first submatch (first 2 characters). Pattern Space: Fo Hold Space: *
- # Append newline followed by the Hold Space to the Pattern Space. Pattern Space: Fo\n* Hold Space: *
- # Remove the (supposedly only) newline from the pattern space. Pattern Space: Fo* Hold Space: *
- # Sed automatically prints the pattern space at the end of a script (unless the -n option is given). Output: Fo*
- password="$(echo "$password_lines" | grep -xE "$(echo "$password" | sed 'y/*/./' )")"
- if "$clipboard"; then
- printf "%s" "$password" | cb copy
- exit 0
- elif "$type"; then
- if $x11; then
- printf "%s" "$password" | xdotool type --clearmodifiers --file -
- else
- printf "%s" "$password" | ydotool type --file -
- fi
- exit 0
- fi
- echo "$password"
|