dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

pass.sh (2936B)


      1 #!/bin/sh
      2 #
      3 # pass
      4 
      5 # named pass in order to just werk when switching from popular pass program
      6 CREDENTIALS_DIR="${XDG_DATA_HOME:-${HOME}/.local/share/}/pass"
      7 
      8 NEWLINE='
      9 '
     10 
     11 get_credential() {
     12     # awk statement takes everything after the first colon
     13     credential="$(printf '%s\n' "${credentials}" | grep "\<${1}\>" | awk -F ':' '{ st = index($0, ":"); print substr($0, st + 1) }')"
     14     credential="${credential%\'}"
     15     credential="${credential#\'}"
     16 }
     17 
     18 main() {
     19     while :; do
     20         i=0
     21         for site in "${CREDENTIALS_DIR}"/*.gpg; do
     22             site="${site%.*}"
     23             if [ ${i} -eq 0 ]; then sites="${sites}${site##*/}"
     24             else sites="${sites}${NEWLINE}${site##*/}"
     25             fi
     26             i=$((i+1))
     27         done
     28 
     29         site="$(printf '%s\n' "${sites}" | dmenu -c -l 10)"
     30         [ -z "${site}" ] && break
     31         if ! printf '%s\n' "${sites}" | grep -iqx "${site}"; then
     32             unset sites && continue
     33         fi
     34 
     35         credentials="$(gpg -dq "${CREDENTIALS_DIR}"/"${site}".gpg)"
     36         [ -z "${credentials}" ] && unset sites && continue
     37 
     38         credential_types="$(printf '%s\n' "${credentials}" | awk -F ':' '{ print $1 }')"
     39 
     40         squestions="$(printf '%s\n' "${credential_types}" | grep -c 'squestion*')"
     41 
     42         credential_options="$(printf '%s\n' "${credential_types}" | ( while read -r type; do
     43             case "${type}" in
     44                 'uname')      printf 'Username\n'          ;;
     45                 'pw')         printf 'Password\n'          ;;
     46                 'email')      printf 'Email\n'             ;;
     47                 'squestion'*) i=1
     48                               while [ ${i} -le ${squestions} ]; do
     49                                   printf '%s\n' "Security Question ${i}"
     50                                   i=$((i+1))
     51                               done
     52                               break
     53                               ;;
     54             esac
     55         done
     56         printf '%s\n' "${credential_options}" ))"
     57 
     58         if { ! credential_option="$(printf '%s\n' "${credential_options}" | dmenu -c -l 10 -p 'Credentials')" ; } ||
     59            { ! printf '%s\n' "${credential_options}" | grep -iqx "${credential_option}" ; }; then
     60             unset sites credential_options && continue
     61         fi
     62 
     63         if printf '%s\n' "${credential_option}" | grep -q '\<[0-9]\>'; then num="$(printf '%s\n' "${credential_option}" | awk '{ print $3 }')" ; fi
     64 
     65         case "${credential_option}" in
     66             'Username')                 get_credential 'uname'         ;;
     67             'Password')                 get_credential 'pw'            ;;
     68             'Email')                    get_credential 'email'         ;;
     69             "Security Question ${num}") get_credential "sanswer${num}" ;;
     70         esac
     71 
     72         if printf '%s\n' "${credential}" | xclip -selection clipboard; then { herbe "${credential_option} for "${site}" copied to clipboard" & } ; fi
     73         break
     74     done
     75 }
     76 
     77 main "${@}"