rofi-pass/rofi-pass
2015-05-24 16:22:49 +02:00

216 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
# rofi-pass
# (c) 2015 Rasmus Steinke <rasi@xssn.at>
# (c) 2015 Thore Bödecker <me@foxxx0.de>
shopt -s nullglob globstar
# read global config file
source /etc/rofi-pass.conf
# check if local config exists and load it
if [[ -f $HOME/.config/rofi-pass/config ]]; then
source $HOME/.config/rofi-pass/config
fi
# check for BROWSER variable, use xdg-open as fallback
if [[ -z $BROWSER ]]; then
export BROWSER=xdg-open
fi
if [[ $(find "$basedir" -maxdepth 1 \( ! -name '.*' \) -type d | wc -l) == "1" ]]; then
root="$(find "${basedir}" -maxdepth 1 \( ! -name '.*' \) -type d -exec basename {} \;)"
else
root=$(find "${basedir}" -maxdepth 1 \( ! -name '.*' \) -type d -exec basename {} \; | rofi -dmenu -p "Choose Database")
fi
list_passwords() {
passwords=( ~/.password-store/**/*.gpg )
passwords=( ~/.password-store/"${root}"/**/*.gpg )
for password in "${passwords[@]}"; do
filename="${password#$basedir}"
filename="${filename%.gpg}"
echo "$filename"
done
}
xdotool_type() {
for (( i=0; i<${#1}; i++ )); do
xdotool type "${1:$i:1}"
done
}
notify () {
7aa3C2
}
mainMenu () {
#notify
HELP="<span color='$help_color'>Alt+1: Autotype | Alt+2: Type User | Alt+3: Type Password
Alt+4: Open URL | Alt+5: Show/Edit</span>"
selected_password="$(echo -e "[ Add Entry ]\n---\n$(list_passwords 2>/dev/null)" | rofi -mesg "${HELP}" -dmenu -select "$entry" -p "rofi-pass > ")"
rofi_exit=$?
case ${rofi_exit} in
0)
true
;;
10)
true
;;
13) chromium $(pass "$selected_password" | grep "URL: " | awk -F 'URL: ' '{ print $2 }')
exit
;;
14) show
;;
1)
exit ${rofi_exit}
;;
esac
if [[ "$selected_password" == "Return to Main Menu" ]]; then
globalMenu
elif [[ "$selected_password" == "[ Add Entry ]" ]]; then
insertPass
fi
password_temp=$(pass "$selected_password")
password=$(echo "${password_temp}" | head -1)
declare -A stuff
while read LINE; do
_id=$(echo "${LINE}" | awk -F': ' '{print $1}')
_val=$(echo "${LINE}" | awk -F': ' '{print $2}')
stuff["${_id}"]=${_val}
done < <(pass "${selected_password}" | tail -n+2 | grep ': ')
if [[ $rofi_exit -eq 11 ]]; then
xdotool_type "${stuff[${USERNAME_field}]}"
exit
elif [[ $rofi_exit -eq 12 ]]; then
xdotool_type "$password"
exit
fi
case "$1" in
password)
xdotool_type "$password"
;;
user)
xdotool_type "${stuff[${USERNAME_field}]}"
;;
url)
xdotool_type "${stuff[${URL_field}]}"
;;
*)
if [[ $(echo "${password_temp}" | tail -1) == "NOTAB" ]]; then
if [[ -z "${stuff['CustomOrder']}" ]]; then
xdotool_type "${stuff[${USERNAME_field}]}"
xdotool_type "$password"
else
for word in ${stuff['CustomOrder']}; do
xdotool_type "${stuff[${word}]}"
done
xdotool_type "$password"
fi
else
if [[ -z "${stuff['CustomOrder']}" ]]; then
xdotool_type "${stuff[${USERNAME_field}]}"
xdotool key Tab
xdotool_type "$password"
else
for word in ${stuff['CustomOrder']}; do
xdotool_type "${stuff[${word}]}"
xdotool key Tab
done
xdotool_type "$password"
fi
fi
;;
esac
# cleanup (for the paranoid)
password=''
selected_password=''
unset stuff
unset password
unset selected_password
unset password_temp
unset stuff
}
show () {
while true; do
menu=$(echo -e "Return to Main Menu\n---\nEdit Entry\n---\n$(pass "$selected_password")" | rofi -dmenu -mesg "<span color='$help_color'>Alt+1: Copy Selection to Clipboard (Default) | Alt+2: Open Selection in Browser</span>" -p "Choose Entry > ")
val=$?
if [[ "$menu" == "Edit Entry" ]]; then
EDITOR=$EDITOR pass edit "${selected_password}"
elif [[ "$menu" == "" ]]; then
exit
elif [[ "$menu" == "Return to Main Menu" ]]; then
globalMenu
else
if [[ $(echo "$menu" | grep ": ") == "" ]]; then
if [[ $val -eq 11 ]]; then
chromium $(echo -e "${menu}" | awk -F 'URL: ' '{ print $2 }')
else
echo -n "$menu" | xclip
xclip -o | xclip -selection clipboard
fi
elif [[ $val -eq 10 || $val -eq 0 ]]; then
menu=$(echo -n "${menu}" | awk -F ': ' '{ print $2 }')
echo -n "${menu}" | xclip
xclip -o | xclip -selection clipboard
fi
fi
done
}
insertPass () {
quiT () {
if [[ $? -eq 1 ]]; then
exit
fi
}
target=$(wmctrl -l | grep Chromium | awk '{print $(NF-2)}' | sed 's~http[s]*://~~g'| sed 's/^\[//' | sed 's/\]$//')
domain=${target%/}
pass=$(echo -e "Password for site $domain" | rofi -dmenu -p "Enter Password > ")
quiT
user=$(echo -e "Username for site $domain" | rofi -dmenu -p "Enter User > ")
quiT
notab=$(echo -e "Yes\nNo" | rofi -dmenu -p "Page uses Auto Tab? > ")
quiT
cd "$HOME"/.password-store/"${root}"
group=$(find -type d -not -iwholename '*.git*' -printf '%d\t%P\n' | sort -r -nk1 | cut -f2- | rofi -dmenu -p "Choose Group > ")
if [[ "$notab" == "No" ]]; then
pass insert -m -f "${root}"/"$group"/"$domain" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}")
w
elif [[ "$notab" == "Yes" ]]; then
pass insert -m -f "${root}"/"$group"/"$domain" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}\nNOTAB")
fi
}
##########################
## ##
## script entry point ##
## ##
##########################
case $1 in
insert)
insertPass
;;
*)
mainMenu
;;
esac