#!/bin/bash # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2021 Ayush Agarwal # # tessen - a data selection interface for pass using bemenu or rofi on Wayland # ------------------------------------------------------------------------------ # shell "strict" mode set -uo pipefail readonly PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH umask 077 # initialize the global variables readonly VERSION="1.1.0" readonly PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}" readonly CLIP_TIME="${PASSWORD_STORE_CLIP_TIME:-15}" BACKEND="${TESSEN_BACKEND:-bemenu}" # uses the value of TESSEN_BACKEND if set by user BACKEND_OPTS="" AT_TYPE="${TESSEN_AUTOTYPE-}" # uses the value of TESSEN_AUTOTYPE if set by user PASSFILE="" declare -A PASSDATA_ARR USERNAME="" PASSWORD="" # display and get the shortened path of the password file get_pass_file() { local tmp_pass_1 tmp_pass_2 tmp_pass_3 # temporarily enable globbing to get the list of gpg files shopt -s nullglob globstar tmp_pass_1=("$PREFIX"/**/*.gpg) tmp_pass_2=("${tmp_pass_1[@]#"$PREFIX"/}") tmp_pass_3=("${tmp_pass_2[@]%.gpg}") shopt -u nullglob globstar PASSFILE="$(printf '%s\n' "${tmp_pass_3[@]}" | "$BACKEND" "$BACKEND_OPTS")" if ! [[ -e "$PREFIX/$PASSFILE".gpg ]]; then exit 1 fi } # get the password data including every key-value pair inside the encrypted file get_pass_data() { local passdata passdata_regex idx key val mapfile -t passdata < <(pass "$PASSFILE") # ASSUMPTION: the key can contain alphanumerics, spaces, hyphen, underscore # the value can contain anything but it has to follow after a space passdata_regex="^[[:alnum:][:blank:]_-]+:[[:blank:]].+$" # ASSUMPTION: the basename of the gpg file is the username although one can still # select a username field inside the file, if it exists USERNAME="${PASSFILE##*/}" # ASSUMPTION: the first line of $PASSFILE will contain the password PASSWORD="${passdata[0]}" # skip the password, validate each entry against $passdata_regex, store valid results # ASSUMPTION: each key is unique otherwise, the value of the last non-unique key will be used for idx in "${passdata[@]:1}"; do if [[ "$idx" =~ $passdata_regex ]]; then key="${idx%%:*}" val="${idx#*: }" PASSDATA_ARR["$key"]="$val" else continue fi done } # get the key that the user will choose to autotype or copy get_key() { local ch="" flag=false key_arr=() if [[ "${1-}" == "pass_key_list" ]]; then key_arr=("autotype" "username" "password" "${!PASSDATA_ARR[@]}") shift elif [[ "${1-}" == "opt_key_list" ]]; then key_arr=("autotype" "copy") shift else exit 1 fi _KEY="$(printf '%s\n' "${key_arr[@]}" | "$BACKEND" "$BACKEND_OPTS")" # validate the chosen key name for ch in "${key_arr[@]}"; do if [[ "$_KEY" == "$ch" ]]; then flag=true break fi done if [[ "$flag" == "false" ]]; then exit 1 fi } # the 2nd, and possibly 3rd, stage of the menu key_menu() { local tmp_key get_key pass_key_list if [[ "$_KEY" == "autotype" ]]; then auto_type username_password exit 0 fi if [[ "${AT_TYPE-}" == "true" ]]; then auto_type "$_KEY" exit 0 elif [[ "${AT_TYPE-}" == "false" ]]; then wld_copy "$_KEY" elif [[ -z "${AT_TYPE-}" ]]; then tmp_key="$_KEY" get_key opt_key_list if [[ "$_KEY" == "autotype" ]]; then auto_type "$tmp_key" exit 0 elif [[ "$_KEY" == "copy" ]]; then wld_copy "$tmp_key" else exit 1 fi fi } auto_type() { if [[ "${1-}" == "username_password" ]]; then printf '%s' "$USERNAME" | wtype -s 100 - wtype -s 100 -k Tab -- printf '%s' "$PASSWORD" | wtype -s 100 - shift elif [[ "${1-}" == "username" ]]; then printf '%s' "$USERNAME" | wtype -s 100 - shift elif [[ "${1-}" == "password" ]]; then printf '%s' "$PASSWORD" | wtype -s 100 - shift elif [[ -n "${PASSDATA_ARR[${1-}]}" ]]; then printf '%s' "${PASSDATA_ARR[${1-}]}" | wtype -s 100 - shift else exit 1 fi } wld_copy() { if [[ "${1-}" == "username" ]]; then printf '%s' "$USERNAME" | wl-copy notify-send -t $((CLIP_TIME * 1000)) "Copied username to clipboard. Will clear in $CLIP_TIME seconds." shift clean elif [[ "${1-}" == "password" ]]; then printf '%s' "$PASSWORD" | wl-copy notify-send -t $((CLIP_TIME * 1000)) "Copied password to clipboard. Will clear in $CLIP_TIME seconds." shift clean elif [[ -n "${PASSDATA_ARR[${1-}]}" ]]; then printf '%s' "${PASSDATA_ARR[${1-}]}" | wl-copy notify-send -t $((CLIP_TIME * 1000)) "Copied ${1-} to clipboard. Will clear in $CLIP_TIME seconds." shift clean else exit 1 fi } print_help() { printf '%s\n' "${0##*/} - data selection interface for password-store on wayland" "" printf '%s\n' "Usage: ${0##*/} [options]" "" printf '%s\n' " tessen use bemenu and either autotype or copy data" printf '%s\n' " tessen -b rofi -t use rofi and always autotype data" printf '%s\n' " tessen -c use bemenu and always copy data" "" printf '%s\n' " -b, --backend, --backend= choose 'bemenu' or 'rofi' as backend (default: bemenu)" printf '%s\n' " -t, --autotype always autotype data" printf '%s\n' " -c, --clipboard always copy data" printf '%s\n' " -h, --help print this help menu" printf '%s\n' " -v, --version print the version of tessen" "" printf '%s\n' "For more details, visit https://github.com/ayushnix/pass-tessen" } validate_backend() { if [[ "$BACKEND" == "bemenu" ]]; then bmn_opt=("-i -l 10 -w --scrollbar=autohide -n") export BEMENU_OPTS="${BEMENU_OPTS:-${bmn_opt[*]}}" readonly BACKEND="bemenu" readonly BACKEND_OPTS="" unset -v bmn_opt elif [[ "$BACKEND" == "rofi" ]]; then readonly BACKEND="rofi" readonly BACKEND_OPTS="-dmenu" else printf '%s\n' "Please specify a backend: bemenu|rofi" >&2 exit 1 fi } validate_clip_time() { local clip_regex clip_regex="^[[:digit:]]+$" if [[ "$CLIP_TIME" =~ $clip_regex ]]; then return 0 else notify-send "invalid clipboard time provided" exit 1 fi } clean() { { sleep "$CLIP_TIME" wl-copy --clear } > /dev/null 2>&1 & disown unset -v PASSFILE USERNAME PASSWORD PASSDATA_ARR CHOICE } die() { wl-copy --clear unset -v PASSFILE USERNAME PASSWORD PASSDATA_ARR CHOICE } main() { local _opt # exit if the password store directory doesn't exist if ! [[ -d "$PASS_STORE" ]]; then notify-send "password store not found" exit 1 fi # parse any options given by the user while [[ "$#" -gt 0 ]]; do _opt="${1-}" case "$_opt" in -b | --backend) [[ "$#" -lt 2 ]] && { printf '%s\n' "Please specify a backend: bemenu|rofi" >&2 exit 1 } BACKEND="${2-}" validate_backend shift ;; --backend=*) BACKEND="${_opt##--backend=}" validate_backend ;; -t | --autotype) unset -v AT_TYPE 2> /dev/null || { printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2 exit 1 } readonly AT_TYPE=true ;; -c | --clipboard) unset -v AT_TYPE 2> /dev/null || { printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2 exit 1 } readonly AT_TYPE=false ;; -h | --help) print_help exit 0 ;; -v | --version) printf '%s\n' "tessen version $VERSION" exit 0 ;; --) shift break ;; *) printf '%s\n' "invalid argument detected" >&2 exit 1 ;; esac shift done unset -v _opt if unset -v BACKEND_OPTS 2> /dev/null; then validate_backend fi validate_clip_time readonly WTYPE readonly BACKEND readonly CLIP_TIME trap 'die' EXIT TERM get_pass_file get_pass_data choice_data if [[ "$WTYPE" -eq 1 ]]; then key_menu_autotype else key_menu_copy fi trap - EXIT TERM } main "$@"