#!/usr/bin/env bash # dmenu-pass This script is based on the fzf-pass script by ReekyMarko # https://git.reekynet.com/ReekyMarko/fzf-pass/ but using dmenu instead of fzf, # which works on wayland without additional configuration. It uses ydotool to # type the information. if command -v ydotool > /dev/null; then CMD=$(which ydotool) RTN="Enter" elif command -v xdotool > /dev/null; then CMD=$(which xdotool) RTN="Return" else echo "Please install either ydotool or xdotool." exit 1 fi NOTIFY=$(command -v notify-send) STORE=${PASSWORD_STORE_DIR:-$HOME/.password-store} PASSFILE=$(find "$STORE" -type f -name '*.gpg' | sed -e "s|$STORE/||g" -e 's|.gpg$||g' | dmenu -i) if [[ -z $PASSFILE ]]; then exit 0 fi PASSDATA="$(PASSWORD_STORE_DIR=$STORE pass "$PASSFILE")" declare -a KEYS declare -A OPTS KEYS+=("Password") OPTS["Password"]="$(echo "$PASSDATA" | head -n 1)" PASSDATA="$(echo "$PASSDATA" | tail -n +2)" # Type the password if it's the only line in the file if [[ -z $PASSDATA ]]; then $CMD type "${OPTS["Password"]}" $NOTIFY $0 "Inserted password for $PASSFILE" exit 0 fi # Iterate through the file and add all the options to the hash table OPTS. If # user|username exists, add also the Autotype option at the begining of the KEYS # array, to use it as default option. pattern_option="^([^:]+):\s*(.+)" pattern_user="^[Uu]ser(name)?" while IFS= read -r line; do if [[ "$line" =~ $pattern_option ]]; then KEY="${BASH_REMATCH[1]^}" VALUE="${BASH_REMATCH[2]}" if [[ "$KEY" =~ $pattern_user ]]; then KEY="Username" KEYS=("$KEY" "${KEYS[@]}") KEYS=("Autotype" "${KEYS[@]}") else KEYS+=("$KEY") fi OPTS["$KEY"]="$VALUE" fi done <<<"$PASSDATA" RESP=$( IFS=$'\n' echo "${KEYS[*]}" | dmenu -i ) if [[ -z $RESP ]]; then exit 0 fi case "$RESP" in Autotype) $CMD type "${OPTS["Username"]}" && $CMD key Tab && $CMD type "${OPTS["Password"]}" && $CMD key $RTN $NOTIFY $0 "Inserted username and password for $PASSFILE" ;; Otpauth) $CMD type "$(pass otp "$PASSFILE")" $NOTIFY $0 "Inserted otp for $PASSFILE" ;; *) $CMD type "${OPTS["$RESP"]}" $NOTIFY $0 "Inserted $RESP for $PASSFILE" ;; esac