Style nits and cleanup

This commit is contained in:
Dr. Duh 2015-10-31 00:08:51 -04:00
parent 3a93a4d364
commit 9a11a83da6
1 changed files with 17 additions and 16 deletions

33
pwd.sh
View File

@ -1,13 +1,14 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# Script for managing passwords in a symmetrically encrypted file using GnuPG. # Script for managing passwords in a GunPG symmetrically encrypted file.
set -o errtrace set -o errtrace
set -o nounset set -o nounset
set -o pipefail set -o pipefail
gpg=$(command -v gpg || command -v gpg2) filter="$(command -v grep) --invert-match --regexp"
safe=${PWDSH_SAFE:=pwd.sh.safe} gpg="$(command -v gpg || command -v gpg2)"
safe="${PWDSH_SAFE:=pwd.sh.safe}"
fail () { fail () {
@ -40,7 +41,7 @@ get_pass () {
fi fi
done done
if [[ -z ${password} ]] ; then if [[ -z "${password}" ]] ; then
fail "No password provided" fail "No password provided"
fi fi
} }
@ -79,7 +80,7 @@ read_pass () {
username="${2}" username="${2}"
fi fi
if [[ -z ${username} || ${username} == "all" ]] ; then if [[ -z "${username}" || "${username}" == "all" ]] ; then
username="" username=""
fi fi
@ -108,7 +109,7 @@ gen_pass () {
fi fi
# base64: 4 characters for every 3 bytes # base64: 4 characters for every 3 bytes
${gpg} --gen-random -a 0 "$((${max} * 3/4))" | cut -c -${len} ${gpg} --gen-random --armor 0 "$((${max} * 3/4))" | cut -c -${len}
} }
@ -116,33 +117,33 @@ write_pass () {
# Write a password in safe. # Write a password in safe.
# If no password provided, clear the entry by writing an empty line. # If no password provided, clear the entry by writing an empty line.
if [[ -z ${userpass+x} ]] ; then if [[ -z "${userpass+x}" ]] ; then
new_entry=" " entry=" "
else else
new_entry="${userpass} ${username}" entry="${userpass} ${username}"
fi fi
get_pass " get_pass "
Enter password to unlock ${safe}: " ; echo Enter password to unlock ${safe}: " ; echo
# If safe exists, decrypt it and filter out username, or bail on error. # If safe exists, decrypt it and filter out username, or bail on error.
# If successful, append new entry, or blank line. # If successful, append entry, or blank line.
# Filter out any blank lines. # Filter out any blank lines.
# Finally, encrypt it all to a new safe file, or fail. # Finally, encrypt it all to a new safe file, or fail.
# If successful, update to new safe file. # If successful, update to new safe file.
( if [[ -f ${safe} ]] ; then ( if [[ -f "${safe}" ]] ; then
decrypt ${password} ${safe} | \ decrypt ${password} ${safe} | \
grep -v -e " ${username}$" || return ${filter} " ${username}$" || return
fi ; \ fi ; \
echo "${new_entry}") | \ echo "${entry}") | \
grep -v -e "^[[:space:]]*$" | \ ${filter} "^[[:space:]]*$" | \
encrypt ${password} ${safe}.new - || fail "Write to safe failed" encrypt ${password} ${safe}.new - || fail "Write to safe failed"
mv ${safe}.new ${safe} mv ${safe}.new ${safe}
} }
create_username () { create_username () {
# Create a new username and password. # Create username with password.
if [[ -z "${2+x}" ]] ; then if [[ -z "${2+x}" ]] ; then
read -p " read -p "
@ -173,7 +174,7 @@ create_username () {
sanity_check () { sanity_check () {
# Make sure required programs are installed and can be executed. # Make sure required programs are installed and are executable.
if [[ -z ${gpg} && ! -x ${gpg} ]] ; then if [[ -z ${gpg} && ! -x ${gpg} ]] ; then
fail "GnuPG is not available" fail "GnuPG is not available"