mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

xpass (3052B)


      1 #!/bin/bash
      2 
      3 # type passwords with xdotool, messages with zenity
      4 # usage: choose a FILE in the dmenu to type the password for it
      5 # type "FILE USER" to generate a new password for FILE and type it
      6 # (additionally storing "USER" as second line in the file)
      7 
      8 # for this script to work, you need a pinentry wrapper that prompts using GTK,
      9 # see e.g., my-pinentry which prompts in curses or GTK depending on environment
     10 
     11 # inspired by similar scripts, e.g.:
     12 # https://gist.github.com/ivyl/7429582
     13 # http://www.christoph-egger.org/weblog/entry/48
     14 # http://git.zx2c4.com/password-store/tree/contrib/dmenu/passmenu (ships with pass)
     15 # https://github.com/carnager/rofi-pass (much more featureful)
     16 
     17 set -x
     18 
     19 # ensure that the layout is correct
     20 ~/bin/layout
     21 
     22 export PINENTRY_USER_DATA="gtk" 
     23 
     24 cd ~/.password-store
     25 
     26 # my-rofi is just a dmenu wrapper
     27 # TODO adapt depending on whether it exists or not
     28 RET=$(ls *.gpg | sed 's/\.gpg$//' | ~/bin/my-rofi -dmenu)
     29 if [ -z "$RET" ]
     30 then
     31   # user aborted
     32   exit 0
     33 fi
     34 FILE=$(echo "$RET" | awk '{print $1}')
     35 USER=$(echo "$RET" | awk '{print $2}')
     36 if [ ! -f "${FILE}.gpg" ]
     37 then
     38   # file does not exist, create password
     39   # TODO ask for confirmation
     40   # TODO don't save login if not specified
     41   # also save anything specified on the commandline to the file
     42   echo "generate"
     43   echo -e "will be replaced by password\nlogin: $USER" | pass add -m "$FILE" || {
     44     zenity --error --text 'problem with pass add; aborting';
     45     exit 2; }
     46   PINENTRY_USER_DATA="gtk" pass generate -i -n "$FILE" 12 || {
     47     zenity --error --text 'problem with pass generate; aborting';
     48     exit 2; }
     49   zenity --info --text "generated password for $FILE $USER, will now type it"
     50 fi
     51 
     52 # file now exists, retrieve password
     53 PASSWORD=$(pass "$FILE" | head -1)
     54 if [[ $USER = "login" || $USER = "user" || $USER = "fill" || $USER = "filln" ]]
     55 then
     56   # user wanted login instead of password
     57   USERDAT=$(pass "$FILE" | grep 'login:' | head -1 | grep '^login:' | cut -d ':' -f2 | cut -c 2-)
     58   if [ -z $USERDAT ]
     59   then
     60     # try another way
     61     # keep line containing '@'
     62     USERDAT=$(pass "$FILE" | grep '@' | head -1 | sed 's/\(.* \)\?\([^ ]*@[^ ]*\)\( .*\)\?/\2/g')
     63     if [ -z $USERDAT ]
     64     then
     65       # fallback to default
     66       USERDAT=$(whoami)
     67     fi
     68   fi
     69 fi
     70 WINDOW=$(xdotool getactivewindow)
     71 
     72 function dologin() {
     73   echo "type --window $WINDOW '$USERDAT'" | xdotool -
     74 }
     75 
     76 function dofill() {
     77   echo "type --window $WINDOW '$USERDAT'" | xdotool -
     78   sleep 0.1
     79   echo "key --window $WINDOW Tab" | xdotool -
     80   sleep 0.1
     81   # delete any password which may already be autocompleted
     82   seq 20 | sed 's/.*/key --window '"$WINDOW"' BackSpace/g' | xdotool -
     83   sleep 0.1
     84   echo "type --window $WINDOW '$PASSWORD'" | xdotool -
     85 }
     86 
     87 case $USER in
     88   login)
     89     dologin
     90     ;;
     91   user)
     92     dologin
     93     ;;
     94   fill)
     95     dofill
     96     echo "key --window $WINDOW KP_Enter" | xdotool -
     97     ;;
     98   filln)
     99     dofill
    100     ;;
    101   show)
    102     # user wants to display password instead
    103     zenity --info --text "$PASSWORD"
    104     ;;
    105   *)
    106   echo "type --window $WINDOW $PASSWORD" | xdotool -
    107 esac
    108