rofi-pass/rofi-pass

306 lines
11 KiB
Bash
Executable File

#!/bin/bash -x
# 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
if [[ ! -d $HOME/.config/rofi-pass/parsers ]]; then
mkdir $HOME/.config/rofi-pass/parsers
cp /usr/share/doc/rofi-pass/parsers/* $HOME/.config/rofi-pass/parsers
fi
# 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 [[ -n "$2" && "$1" == "--root" ]]; then
root="${2}"
else
:
fi
list_passwords() {
if [[ -n "$root" ]]; then
passwords=( "${basedir}/${root}"/**/*.gpg )
else
passwords=( "${basedir}"/**/*.gpg )
fi
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
}
mainMenu () {
if [[ $1 == "manage" ]]; then
HELP="<span color='$help_color'>${edit}: Edit Entry | ${move}: Move Entry | ${delete}: Delete Entry | ${show}: Show Entry</span>"
selected_password="$(echo -e "0 Return to Main Menu\n---\n$(list_passwords 2>/dev/null)" | rofi -custom-kb-1 "${edit}" -custom-kb-2 "${move}" -custom-kb-3 "${delete}" -custom-kb-4 "${show}" -mesg "${HELP}" -dmenu -select "$entry" -p "rofi-pass > ")"
rofi_exit=$?
if [[ "${rofi_exit}" -eq 0 ]]; then true;
elif [[ "${rofi_exit}" -eq 10 ]]; then manageEntry edit;
elif [[ "${rofi_exit}" -eq 11 ]]; then manageEntry move;
elif [[ "${rofi_exit}" -eq 1 ]]; then exit;
elif [[ "${rofi_exit}" -eq 12 ]]; then manageEntry delete;
elif [[ "${rofi_exit}" -eq 13 ]]; then showEntry;
fi
if [[ "$selected_password" == "0 Return to Main Menu" ]]; then
mainMenu
else
mainMenu manage
fi
else
HELP="<span color='$help_color'>${autotype}: Autotype | ${type_user}: Type User | ${type_pass}: Type Password
${open_url}: Open URL | ${copy_name}: Copy Username | ${copy_pass}: Copy Password</span>"
selected_password="$(echo -e "[ Add Entry ]>\n[ Manage Database ]>\n---\n$(list_passwords 2>/dev/null)" | rofi -mesg "${HELP}" -dmenu -kb-custom-1 "${autotype}" -kb-custom-2 "${type_user}" -kb-custom-3 "${type_pass}" -kb-custom-4 "${open_url}" -kb-custom-5 "${copy_name}" -kb-custom-6 "${copy_pass}" -dmenu -select "$entry" -kb-custom-8 "Alt+c" -kb-custom-9 "Alt+Shift+c" -p "rofi-pass > ")"
rofi_exit=$?
if [[ "${rofi_exit}" -eq 0 ]]; then true;
elif [[ "${rofi_exit}" -eq 10 ]]; then true;
elif [[ "${rofi_exit}" -eq 13 ]]; then $BROWSER $(pass "$selected_password" | grep "URL: " | awk -F 'URL: ' '{ print $2 }'); exit;
elif [[ "${rofi_exit}" -eq 1 ]]; then exit ${rofi_exit};
fi
if [[ "$selected_password" == "[ Add Entry ]>" ]]; then
insertPass
elif [[ "$selected_password" == "[ Manage Database ]>" ]]; then
mainMenu manage
elif [[ "$selected_password" == "" ]]; then
exit
fi
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 ': ')
stuff["password"]=${password}
if [[ $rofi_exit -eq 11 ]]; then
xdotool_type "${stuff[${USERNAME_field}]}"
exit
elif [[ $rofi_exit -eq 12 ]]; then
xdotool_type "$password"
exit
elif [[ $rofi_exit -eq 14 ]]; then
echo -n "${stuff[${USERNAME_field}]}" | xclip
exit
elif [[ $rofi_exit -eq 15 ]]; then
echo -n "$password" | xclip
xclip -o | xclip -selection clipboard
notify-send "rofi-pass" "Copied Password\nClearing in 45 seconds"
$(sleep 45; echo -n "" | xclip; echo "" | xclip -selection clipboard | notify-send "rofi-pass" "Clipboard cleared") &
exit
fi
if [[ -z "${stuff["$AUTOTYPE_field"]}" ]]; then
xdotool_type "${stuff[${USERNAME_field}]}"
xdotool_type "$password"
sleep 1
if [[ ${auto_enter} == "true" ]]; then
xdotool key Return
fi
else
for word in ${stuff["$AUTOTYPE_field"]}; do
if [[ $word == ":tab" ]]; then
xdotool key Tab
else
xdotool_type "${stuff[${word}]}"
fi
#xdotool key Tab
done
#xdotool_type "$password"
if [[ ${auto_enter} == "true" ]]; then
xdotool key Return
fi
fi
# cleanup (for the paranoid)
password=''
selected_password=''
unset stuff
unset password
unset selected_password
unset password_temp
unset stuff
}
showEntry () {
menu=$(echo -e "0 Return to Manage Menu\n---\n$(pass "$selected_password")" | rofi -dmenu -p "> ")
if [[ $menu == "0 Return to Manage Menu" ]]; then mainMenu manage
elif [[ $menu == "" ]]; then exit
else
showEntry
fi
}
manageEntry () {
if [[ "$1" == "edit" ]]; then
EDITOR=$EDITOR pass edit "${selected_password}"
mainMenu manage
elif [[ $1 == "move" ]]; then
cd "$HOME"/.password-store/"${root}"
selected_password2=$(basename "$selected_password" .gpg)
group=$(find -type d -not -iwholename '*.git*' -printf '%d\t%P\n' | sort -r -nk1 | cut -f2- | rofi -dmenu -p "Choose Group > ")
pass mv "$selected_password" "${root}"/"${group}"/"${selected_password2}"
mainMenu manage
elif [[ "$1" == "delete" ]]; then
ask=$(echo -e "Yes\nNo" | rofi -dmenu -p "Are You Sure? >")
if [[ "$ask" == "Yes" ]]; then
pass rm --force "${selected_password}"
elif [[ "$ask" == "no" ]]; then
mainMenu manage
fi
else
mainMenu manage
fi
}
insertPass () {
if [[ -z "$pass" ]]; then
if [[ -n "$1" ]]; then pass="$1" pass2="***"; else pass2="Empty"; fi
fi
if [[ -z "$user" ]]; then user="Empty"; fi
if [[ -z "$domain" ]]; then domain="Empty"; fi
if [[ -z "$name" ]]; then name="Empty"; fi
menu=$(echo -e "0 Return to Main Menu\n* Accept Values and Add Password Entry\n---\n1 Name ($name)\n2 URL ($domain)\n3 User ($user)\n4 Password ($pass2)" | rofi -dmenu -p "Add Entry > ")
if [[ $menu == "0 Return to Main Menu" ]]; then mainMenu
elif [[ $menu == "" ]]; then exit
elif [[ $menu == "* Accept Values and Add Password Entry" ]]; then
notab=$(echo -e "Yes\nNo" | rofi -dmenu -p "Page uses Auto Tab? > ")
if [[ -n "$root" ]]; then
cd "${basedir}/${root}"
else
cd "${basedir}"
fi
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
if [[ -n "$root" ]]; then
pass insert -m -f "${root}/${group}/${name}" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}")
else
pass insert -m -f "${group}/${name}" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}")
fi
elif [[ "$notab" == "Yes" ]]; then
if [[ -n "$root" ]]; then
pass insert -m -f "${root}"/"$group"/"$name" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}\nNOTAB")
else
pass insert -m -f "$group"/"$name" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}\nNOTAB")
fi
fi
elif [[ $menu == "1 Name"* ]]; then
name=$(echo "" | rofi -dmenu -p "Enter Name > ")
insertPass
elif [[ $menu == "2 URL"* ]]; then
HELP="<span color='$help_color'>Enter Domain Name or chose one of the Options below</span>"
domain=$(echo -e "< Return\n---\nGet URL from active Browser Tab" | rofi -dmenu -mesg "${HELP}" -p "URL > ")
if [[ $domain == "Get URL from active Browser Tab" ]]; then
domain=$($HOME/.config/rofi-pass/parsers/$BROWSER)
insertPass
elif [[ $domain == "< Return" ]]; then
insertPass
elif [[ $domain == "" ]]; then
exit
else
insertPass
fi
elif [[ $menu == "3 User"* ]]; then
HELP="<span color='$help_color'>Enter Username</span>"
user=$(echo -e "" | rofi -dmenu -mesg "${HELP}" -p "Username > ")
insertPass
elif [[ $menu == "4 Password"* ]]; then
password_length=12
symbols="False"
numerals="True"
capitals="True"
password_gen
else
insertPass
fi
}
password_gen () {
if [[ $capitals == "True" ]]; then cap="-c"; else cap="-A"; fi
if [[ $symbols == "True" ]]; then sym="-y"; else sym=""; fi
if [[ $numerals == "True" ]]; then num="-n"; else num="-0"; fi
HELP="<span color='$help_color'>Choose one password or type your own</span>"
menu=$(echo -e "0 Return to Insert Menu\n* Generate new Password\n---\n1 Password Length\n2 Include Capitals ($capitals)\n3 Include Numerals ($numerals)\n4 Include Symbols ($symbols)\n---\n$(pwgen $num $cap $sym -B -1 $password_length 5)" | rofi -dmenu -mesg "${HELP}" -p "Password > ")
if [[ $menu == "1 Password Length" ]]; then
password_length=$(echo -e "5\n7\n10\n12\n15\n20" | rofi -dmenu -p "Choose Length > ")
password_gen
elif [[ $menu == "0 Return to Insert Menu" ]]; then
insertPass
elif [[ $menu == "* Generate new Password" ]]; then
password_gen
elif [[ $menu == "2 Include Capitals"* ]]; then
capitals=$(echo -e "True\nFalse" | rofi -dmenu -p "Capitals? > ")
password_gen
elif [[ $menu == "3 Include Numerals"* ]]; then
numerals=$(echo -e "True\nFalse" | rofi -dmenu -p "Numerals? > ")
password_gen
elif [[ $menu == "4 Include Symbols"* ]]; then
symbols=$(echo -e "True\nFalse" | rofi -dmenu -p "Symbols? > ")
password_gen
else
insertPass "$menu"
fi
}
help_msg () {
echo "rofi-pass - a rofi driven frontend to pass"
echo "--------------------------------------"
echo "(C) 2015 Rasmus Steinke <rasi@xssn.at>"
echo "(C) 2015 Thore Bödecker <me@foxxx0.de>"
echo "--------------------------------------"
echo -e "Usage\n"
echo "--insert insert new entry to password store"
echo -e "--root set custom root directory\n"
echo "Running rofi-pass without arguments will ask for root to be used,"
echo "if multiple directories exist"
}
##########################
## ##
## script entry point ##
## ##
##########################
case $1 in
--insert)
insertPass
;;
--root)
mainMenu $2
;;
--help)
help_msg
;;
*)
mainMenu
;;
esac