1
0
mirror of https://github.com/octoleo/Purse.git synced 2024-12-29 12:32:39 +00:00

Merge pull request #8 from drduh/wip-30jun24

Generate username and clipboard features from pwd.sh
This commit is contained in:
drduh 2024-07-04 22:06:32 +00:00 committed by GitHub
commit 849920c1b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 50 deletions

View File

@ -68,6 +68,8 @@ Several customizable options and features are also available, and can be configu
Variable | Description | Default | Values Variable | Description | Default | Values
-|-|-|- -|-|-|-
`PURSE_CLIP` | clipboard to use | `xclip` | `pbcopy` on macOS
`PURSE_CLIP_ARGS` | arguments to pass to clipboard command | unset (disabled) | `-i -selection clipboard` to use primary (control-v) clipboard with xclip
`PURSE_TIME` | seconds to clear password from clipboard/screen | `10` | any valid integer `PURSE_TIME` | seconds to clear password from clipboard/screen | `10` | any valid integer
`PURSE_LEN` | default generated password length | `14` | any valid integer `PURSE_LEN` | default generated password length | `14` | any valid integer
`PURSE_COPY` | copy password to clipboard before write | unset (disabled) | `1` or `true` to enable `PURSE_COPY` | copy password to clipboard before write | unset (disabled) | `1` or `true` to enable

102
purse.sh
View File

@ -9,10 +9,11 @@ export LC_ALL="C"
now="$(date +%s)" now="$(date +%s)"
today="$(date +%F)" today="$(date +%F)"
copy="$(command -v xclip || command -v pbcopy)"
gpg="$(command -v gpg || command -v gpg2)" gpg="$(command -v gpg || command -v gpg2)"
gpg_conf="${GNUPGHOME}/gpg.conf" gpg_conf="${HOME}/.gnupg/gpg.conf"
clip="${PWDSH_CLIP:=xclip}" # clipboard, 'pbcopy' on macOS
clip_args=${PWDSH_CLIP_ARGS:=} # args to pass to copy command
clip_dest="${PURSE_DEST:=clipboard}" # set to 'screen' to print to stdout clip_dest="${PURSE_DEST:=clipboard}" # set to 'screen' to print to stdout
clip_timeout="${PURSE_TIME:=10}" # seconds to clear clipboard/screen clip_timeout="${PURSE_TIME:=10}" # seconds to clear clipboard/screen
comment="${PURSE_COMMENT:=}" # *unencrypted* comment in files comment="${PURSE_COMMENT:=}" # *unencrypted* comment in files
@ -31,8 +32,7 @@ cleanup () {
# "Lock" files on trapped exits. # "Lock" files on trapped exits.
ret=$? ret=$?
chmod -R 0000 \ chmod -R 0000 "${safe_dir}" "${safe_ix}" 2>/dev/null
"${safe_dir}" "${safe_ix}" 2>/dev/null
exit ${ret} exit ${ret}
} }
@ -49,9 +49,33 @@ warn () {
tput setaf 3 ; printf "\nWARNING: %s\n" "${1}" ; tput sgr0 tput setaf 3 ; printf "\nWARNING: %s\n" "${1}" ; tput sgr0
} }
setup_keygroup() {
# Configure one or more recipients.
purse_keygroup="group purse_keygroup ="
keyid=""
recommend="$(${gpg} -K | grep "sec#" | \
awk -F "/" '{print $2}' | cut -c-18 | tr "\n" " ")"
printf "\n Setting up keygroup ...\n
Found recommended key IDs: %s\n
Enter one or more key IDs, preferred one last\n" "${recommend}"
while [[ -z "${keyid}" ]] ; do read -r -p "
Key ID or Enter to continue: " keyid
if [[ -z "${keyid}" ]] ; then
printf "%s\n" "${purse_keygroup}" >> "${gpg_conf}"
break
fi
purse_keygroup="${purse_keygroup} ${keyid}"
keyid=""
done
}
get_pass () { get_pass () {
# Prompt for a password. # Prompt for a password.
password=""
prompt=" ${1}" prompt=" ${1}"
printf "\n" printf "\n"
@ -105,23 +129,22 @@ read_pass () {
tail -1 | cut -d ":" -f2) tail -1 | cut -d ":" -f2)
fi fi
if [[ ! -s "${spath}" ]] ; then
fail "Secret not available" ; fi
prompt_key "password" prompt_key "password"
if [[ -s "${spath}" ]] ; then
clip <(decrypt "${spath}" | head -1) || \ clip <(decrypt "${spath}" | head -1) || \
fail "Failed to decrypt ${spath}" fail "Failed to decrypt ${spath}"
else fail "Secret not available"
fi
} }
prompt_key () { prompt_key () {
# Print a message if safe file exists. # Print a message if safe file exists.
if [[ -f "${safe_ix}" ]] ; then if [[ -f "${safe_ix}" ]] ; then
printf "\n Touch key to access %s ...\n" "${1}" printf "\n Touch key to access %s ...\n" "${1}" ; fi
fi
} }
gen_pass () { generate_pass () {
# Generate a password from urandom. # Generate a password from urandom.
if [[ -z "${3+x}" ]] ; then read -r -p " if [[ -z "${3+x}" ]] ; then read -r -p "
@ -129,13 +152,22 @@ gen_pass () {
else length="${3}" ; fi else length="${3}" ; fi
if [[ "${length}" =~ ^[0-9]+$ ]] ; then if [[ "${length}" =~ ^[0-9]+$ ]] ; then
pass_len="${length}" pass_len="${length}" ; fi
fi
tr -dc "${pass_chars}" < /dev/urandom | \ tr -dc "${pass_chars}" < /dev/urandom | \
fold -w "${pass_len}" | head -1 fold -w "${pass_len}" | head -1
} }
generate_user () {
# Generate a username.
printf "%s%s\n" \
"$(awk 'length > 2 && length < 12 {print(tolower($0))}' \
/usr/share/dict/words | grep -v "'" | sort -R | head -n2 | \
tr "\n" "_" | iconv -f utf-8 -t ascii//TRANSLIT)" \
"$(tr -dc "[:digit:]" < /dev/urandom | fold -w 4 | head -1)"
}
write_pass () { write_pass () {
# Write a password and update the index. # Write a password and update the index.
@ -143,11 +175,9 @@ write_pass () {
fold -w10 | head -1)" fold -w10 | head -1)"
if [[ -n "${pass_copy}" ]] ; then if [[ -n "${pass_copy}" ]] ; then
clip <(printf '%s' "${userpass}") clip <(printf '%s' "${userpass}") ; fi
fi
printf '%s\n' "${userpass}" | \ printf '%s\n' "${userpass}" | encrypt "${spath}" - || \
encrypt "${spath}" - || \
fail "Failed saving ${spath}" fail "Failed saving ${spath}"
if [[ -n "${encrypt_index}" ]] ; then if [[ -n "${encrypt_index}" ]] ; then
@ -195,7 +225,7 @@ clip () {
if [[ "${clip_dest}" = "screen" ]] ; then if [[ "${clip_dest}" = "screen" ]] ; then
printf '\n%s\n' "$(cat ${1})" printf '\n%s\n' "$(cat ${1})"
else "${copy}" < "${1}" ; fi else ${clip} < "${1}" ; fi
printf "\n" printf "\n"
while [[ "${clip_timeout}" -gt 0 ]] ; do while [[ "${clip_timeout}" -gt 0 ]] ; do
@ -205,40 +235,18 @@ clip () {
printf "\r\033[K Clearing password from %s ..." "${clip_dest}" printf "\r\033[K Clearing password from %s ..." "${clip_dest}"
if [[ "${clip_dest}" = "screen" ]] ; then clear if [[ "${clip_dest}" = "screen" ]] ; then clear
else printf "\n" ; printf "" | "${copy}" ; fi else printf "\n" ; printf "" | ${clip} ; fi
}
setup_keygroup() {
# Configure one or more recipients.
purse_keygroup="group purse_keygroup ="
keyid=""
recommend="$(${gpg} -K | grep "sec#" | \
awk -F "/" '{print $2}' | cut -c-18 | tr "\n" " ")"
printf "\n Setting up keygroup ...\n
Found recommended key IDs: %s\n
Enter one or more key IDs, preferred one last\n" "${recommend}"
while [[ -z "${keyid}" ]] ; do read -r -p "
Key ID or Enter to continue: " keyid
if [[ -z "${keyid}" ]] ; then
printf "%s\n" "${purse_keygroup}" >> "${gpg_conf}"
break
fi
purse_keygroup="${purse_keygroup} ${keyid}"
keyid=""
done
} }
new_entry () { new_entry () {
# Prompt for username and password. # Prompt for username and password.
while [[ -z "${username}" ]] ; do
if [[ -z "${2+x}" ]] ; then read -r -p " if [[ -z "${2+x}" ]] ; then read -r -p "
Username: " username Username (Enter to generate): " username
else username="${2}" ; fi else username="${2}" ; fi
done if [[ -z "${username}" ]] ; then
username=$(generate_user "$@")
fi
if [[ -z "${3+x}" ]] ; then if [[ -z "${3+x}" ]] ; then
get_pass "Password for \"${username}\" (Enter to generate): " get_pass "Password for \"${username}\" (Enter to generate): "
@ -247,7 +255,7 @@ new_entry () {
printf "\n" printf "\n"
if [[ -z "${password}" ]] ; then if [[ -z "${password}" ]] ; then
userpass=$(gen_pass "$@") userpass=$(generate_pass "$@")
fi fi
} }
@ -283,9 +291,11 @@ if [[ ! -d "${safe_dir}" ]] ; then mkdir -p "${safe_dir}" ; fi
chmod -R 0700 "${safe_dir}" "${safe_ix}" 2>/dev/null chmod -R 0700 "${safe_dir}" "${safe_ix}" 2>/dev/null
if [[ -z "${copy}" || ! -x "${copy}" ]] ; then if [[ -z "$(command -v ${clip})" ]] ; then
warn "Clipboard not available, passwords will print to screen/stdout!" warn "Clipboard not available, passwords will print to screen/stdout!"
clip_dest="screen" clip_dest="screen"
elif [[ -n "${clip_args}" ]] ; then
clip+=" ${clip_args}"
fi fi
username="" username=""