123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #!/usr/bin/env sh
- multi=false
- clipboard=false
- type=false
- add=false
- otp=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 yofi wofi)"
- case "$dmenu" in
- yofi)
- dmenu="yofi dialog"
- ;;
- wofi)
- dmenu="wofi -dmenu"
- ;;
- esac
- else
- dmenu="$DMENU_COMMAND"
- fi
- else
- x11=true
- if [ -z "$DMENU_COMMAND" ]; then
- dmenu="$(checkmenu dmenu rofi bemenu)"
- case "$dmenu" in
- rofi)
- dmenu="rofi -dmenu"
- ;;
- esac
- else
- dmenu="$DMENU_COMMAND"
- fi
- fi
- prompt_prefix="$(case "$(echo "$dmenu" | cut -d " " -f 1)" in
- tofi)
- printf "%s" "--prompt="
- ;;
- *)
- printf "%s" "-p "
- ;;
- esac
- )"
- 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-o\t\tGet an otp instead of the password file (precedence over -m)\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
- ;;
- -o)
- otp=true
- shift
- ;;
- *)
- 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 $prompt_prefix"Choose Password")"
- 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 || notify-send "Error $?"
- if [ -n "$(cat /tmp/pass.txt)" ]; then
- save="$(printf "yes\nno" | $dmenu $prompt_prefix "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 ! $otp; then
- password_lines="$(pass "$file")"
- else
- password_lines="$(pass otp "$file")"
- fi
- if $multi && ! $otp; then
- sel_password="$(echo "$password_lines" | sed 'h; s/^.\{1,2\}//; s/./*/g; x; s/^\(.\{1,2\}\).*/\1/; G; s/\n//' | $dmenu)"
- password_lines="$(echo "$password_lines" | grep -x "$(echo "$sel_password" | sed 'y/*/./' )")"
- # Sed script:
- # Copy pattern space (line) to hold space. Pattern space: Foo Hold space: Foo
- # Delete first or first two characters in pattern space. Pattern space: o Hold space: Foo
- # Replace every character in patter space with "*", Pattern space: * Hold Space: Foo
- # Swap pattern space with hold space. Pattern Space: Foo Hold space: *
- # Delete all the characters in pattern space after the first two. 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*
- fi
- if [ -z "$password_lines" ]; then
- "No password found or selected"
- fi
- if $clipboard; then
- if $x11; then
- printf "%s" "$password_lines" | xclip -in -selection clipboard
- else
- printf "%s" "$password_lines" | wl-copy -n
- fi
- exit 0
- elif $type; then
- if $x11; then
- printf "%s" "$password_lines" | xdotool type --clearmodifiers --file -
- else
- printf "%s" "$password_lines" | ydotool type --file -
- fi
- exit 0
- fi
- echo "$password_lines"
|