rofi-pass/rofi-pass
2015-06-02 20:20:44 +02:00

300 lines
10 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 [[ -n "$2" ]]; then
root="$2"
fi
get_root () {
if [[ -n "$2" ]]; then
root="$2"
else
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
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 () {
if [[ -z "$root" ]]; then
get_root
fi
if [[ -n "$2" ]]; then
root="$2"
fi
if [[ $1 == "manage" ]]; then
HELP="<span color='$help_color'>Alt+1: Edit Entry | Alt+2: Move Entry | Alt+3: Delete Entry | Alt+4 Show Entry</span>"
selected_password="$(echo -e "0 Return to Main Menu\n---\n$(list_passwords 2>/dev/null)" | rofi -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'>Alt+1: Autotype | Alt+2: Type User | Alt+3: Type Password
Alt+4: Open URL | Alt+c: Copy Username | Alt+Shift+c: Copy Password</span>"
selected_password="$(echo -e "[ Add Entry ]>\n[ Manage Database ]>\n---\n$(list_passwords 2>/dev/null)" | rofi -mesg "${HELP}" -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 }');
elif [[ "${rofi_exit}" -eq 1 ]]; then exit ${rofi_exit};
fi
if [[ "$selected_password" == "[ Add Entry ]>" ]]; then
insertPass2
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 ': ')
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 17 ]]; then
echo -n "${stuff[${USERNAME_field}]}" | xclip
exit
elif [[ $rofi_exit -eq 18 ]]; then
echo -n "$password" | xclip
exit
fi
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
exit
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
exit
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
}
insertPass2 () {
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
menu=$(echo -e "0 Return to Main Menu\n* Accept Values and Add Password Entry\n---\n1 URL ($domain)\n2 User ($user)\n3 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? > ")
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}")
elif [[ "$notab" == "Yes" ]]; then
pass insert -m -f "${root}"/"$group"/"$domain" < <(echo -e "${pass}\nUserName: ${user}\n---\nURL: ${domain}\nNOTAB")
fi
elif [[ $menu == "1 URL"* ]]; then
HELP="<span color='$help_color'>Enter Domain Name or chose one of the Options below</span>"
domain=$(echo -e "Get URL from active Chromium Tab" | rofi -dmenu -mesg "${HELP}" -p "URL > ")
if [[ $domain == "Get URL from active Chromium Tab" ]]; then
target=$(wmctrl -l | grep Chromium | awk '{print $(NF-2)}' | sed 's~http[s]*://~~g'| sed 's/^\[//' | sed 's/\]$//')
domain=${target%/}
insertPass2
fi
elif [[ $menu == "2 User"* ]]; then
HELP="<span color='$help_color'>Enter Username</span>"
user=$(echo -e "" | rofi -dmenu -mesg "${HELP}" -p "Username > ")
insertPass2
elif [[ $menu == "3 Password"* ]]; then
password_length=12
symbols="False"
numerals="True"
capitals="True"
password_gen
else
insertPass2
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
insertPass2
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
insertPass2 "$menu"
fi
}
mainMenu
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