pash-posix: longer length for comments.

This commit is contained in:
Dylan Araps 2019-11-25 22:38:26 +00:00
parent f0b54c98d4
commit ef2c24e742
No known key found for this signature in database
GPG Key ID: 46D62DD9F1DE636E

View File

@ -6,33 +6,26 @@ pw_add() {
pass_name=$1 pass_name=$1
if yn "Generate a password?"; then if yn "Generate a password?"; then
# Use 'gpg' to generate the password. This # Use 'gpg' to generate the password. This could have
# could have been 'openssl', '/dev/[u]random' # been 'openssl', '/dev/[u]random' or another utility,
# or another utility, however sticking to 'gpg' # however sticking to 'gpg' removes the need for another
# removes the need for another dependency. # dependency.
# #
# The '-a' flag outputs the random bytes as # The '-a' flag outputs the random bytes as a 'base64'
# a 'base64' encoded string to allow for the # encoded string to allow for the password to be used as
# password to be used as well, a password. # well, a password.
# #
# The 'cut' is required to actually truncate # The 'cut' is required to actually truncate the password
# the password to the set length as the 'base64' # to the set length as the 'base64' encoding makes the
# encoding makes the resulting string longer # resulting string longer than the given length.
# than the given length.
pass=$("$gpg" --gen-random -a "${PASH_LENGTH:-50}" |\ pass=$("$gpg" --gen-random -a "${PASH_LENGTH:-50}" |\
cut -c -"${PASH_LENGTH:-50}") cut -c -"${PASH_LENGTH:-50}")
else else
printf 'Enter password: ' printf 'Enter password: '
# Disable echoing of output to the
# terminal while reading user input.
stty -echo stty -echo
read -r pass read -r pass
# Enable echoing and leave the terminal
# how we *should* have found it.
stty echo stty echo
printf '\n' printf '\n'
@ -41,18 +34,17 @@ pw_add() {
[ "$pass" ] || [ "$pass" ] ||
die "Failed to generate a password." die "Failed to generate a password."
# Mimic the use of an array for storing # Mimic the use of an array for storing arguments by... using
# arguments by... using the function's # the function's argument list. This is very apt isn't it?
# argument list. This is very apt... isn't it?
if [ "$PASH_KEYID" ]; then if [ "$PASH_KEYID" ]; then
set -- --trust-model always -aer "$PASH_KEYID" set -- --trust-model always -aer "$PASH_KEYID"
else else
set -- -c set -- -c
fi fi
# Use 'gpg' to store the password in an encrypted file. # Use 'gpg' to store the password in an encrypted file. The
# The 'GPG_TTY' environment variable is set to workaround # 'GPG_TTY' environment variable is set to workaround cases
# cases where 'gpg' cannot find an attached terminal. # where 'gpg' cannot find an attached terminal.
echo "$pass" | GPG_TTY=$(tty) "$gpg" "$@" -o "$pass_name.gpg" echo "$pass" | GPG_TTY=$(tty) "$gpg" "$@" -o "$pass_name.gpg"
} }
@ -66,10 +58,9 @@ pw_del() {
pw_show() { pw_show() {
pass=$("$gpg" -dq "$1.gpg") pass=$("$gpg" -dq "$1.gpg")
# If '$2' is defined, don't print the password # If '$2' is defined, don't print the password to the
# to the terminal. This is useful when the user # terminal. For example, this is used when the password is
# would just like the password copied to the # copied to the clipboard.
# clipboard.
[ "$2" ] || printf '%s\n' "$pass" [ "$2" ] || printf '%s\n' "$pass"
} }
@ -94,32 +85,29 @@ pw_list() {
yn() { yn() {
printf '%s [y/n]: ' "$1" printf '%s [y/n]: ' "$1"
# Enable raw input to allow for a single # Enable raw input to allow for a single byte to be read from
# byte to be read from stdin without needing # stdin without needing to wait for the user to press Return.
# to wait for the user to press Return.
stty -icanon stty -icanon
# Read a single byte from stdin using 'dd'. # Read a single byte from stdin using 'dd'. POSIX 'read' has
# POSIX 'read' has no support for single or # no support for single/'N' byte based input from the user.
# 'N' character based input from the user.
REPLY=$(dd ibs=1 count=1 2>/dev/null) REPLY=$(dd ibs=1 count=1 2>/dev/null)
# Disable raw input, leaving the terminal # Disable raw input, leaving the terminal how we *should*
# how we *should* have found it. # have found it.
stty icanon stty icanon
printf '\n' printf '\n'
# Handle the answer here directly enabling # Handle the answer here directly, enabling this function's
# this function's return status to be used # return status to be used in place of checking for '[yY]'
# in place of repeating this code throughout. # throughout this program.
glob "$REPLY" '[yY]' || return 1 && return 0 glob "$REPLY" '[yY]' || return 1 && return 0
} }
glob() { glob() {
# This is a simple wrapper around a case # This is a simple wrapper around a case statement to allow
# statement to allow for simple string # for simple string comparisons against globs.
# comparisons against globs.
# #
# Example: if glob "Hello World" '* World'; then # Example: if glob "Hello World" '* World'; then
case $1 in $2) return 0; esac; return 1 case $1 in $2) return 0; esac; return 1
@ -184,7 +172,7 @@ main() {
umask 077 umask 077
case $1 in case $1 in
a*) pw_add "$2" && printf '%s\n' "Saved '$2' to store." ;; a*) pw_add "$2" && printf '%s\n' "Saved '$2' to the store." ;;
c*) pw_copy "$2" ;; c*) pw_copy "$2" ;;
d*) pw_del "$2" ;; d*) pw_del "$2" ;;
s*) pw_show "$2" ;; s*) pw_show "$2" ;;