[PATCH] Add option '-m' to generate to add additional multiline data.

Calin Iorgulescu calin.iorgulescu at gmail.com
Fri Jan 18 11:43:49 CET 2019


Hello,

@Tobias:

> That's something that has been bugging me for years! Is there any reason
> why `generate -m` doesn't expect lines on stdin, like `insert -m` does?
>

No particular reason. I don't really use the '--multiline' mode as part of my workflow, so implementing it as a list of arguments seemed to make more sense at the time.

@Tristan:

> Thanks for this -- this is a wishlist item that I raised here on
> December 28.  Your patch is one solution to the problem, but I have to
> question why you haven't made your "-m" option for "pass generate" work
> the same way it does for "pass insert" (i.e., by reading the extra lines
> from stdin rather than from command-line arguments). I think having two
> different behaviours for the same command will lead to a lot of
> confusion.  (Also, why not provide an additional long option version
> named "--multiline", as "pass insert" does?)

As I mentioned above to Tobias, this just felt more natural at the time. But I agree that having different behaviors for the same flag is confusing. Hence this new patch.

@Pass Word:
> One nice thing about the multiple -m options on the command line is you could do something like this:
> 
> -m "user: "$(pass generate -n deleteme 10 2>/dev/null| tail -1)

That's a nice use case I hadn't considered! Presumably, one could even use a tool like 'pwgen' instead of poluting pass's git history with a "deleteme" password.
This could still be done using the way that multiline behaves for inserts. For example, the same line could be written as:

$ echo "user:"$(pwgen 15 1) | pass generate -m widgets.com 30


@all:

I am glad to see this feature raises some interest! As mentioned above, I do think it's worth being consistent with the behavior of '-m' for insert. This patch does just that: it aims to implement '-m' in the same way that it is done for insert. Additionally, it prints out an error message if '-m' is used together with '-i' (as there might be several ways to merge existing multiline entries when changing the password in-place).

Best,
Calin

---
 src/password-store.sh | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index d89d455..406820a 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -276,7 +276,7 @@ cmd_usage() {
 	        overwriting existing password unless forced.
 	    $PROGRAM edit pass-name
 	        Insert a new password or edit an existing password using ${EDITOR:-vi}.
-	    $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--in-place,-i | --force,-f] pass-name [pass-length]
+	    $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--in-place,-i | --force,-f] [--multiline,-m] pass-name [pass-length]
 	        Generate a new password of pass-length (or $GENERATED_LENGTH if unspecified) with optionally no symbols.
 	        Optionally put it on the clipboard and clear board after $CLIP_TIME seconds.
 	        Prompt before overwriting existing password unless forced.
@@ -491,8 +491,8 @@ cmd_edit() {
 }
 
 cmd_generate() {
-	local opts qrcode=0 clip=0 force=0 characters="$CHARACTER_SET" inplace=0 pass
-	opts="$($GETOPT -o nqcif -l no-symbols,qrcode,clip,in-place,force -n "$PROGRAM" -- "$@")"
+	local opts qrcode=0 clip=0 force=0 characters="$CHARACTER_SET" inplace=0 multiline=0 pass
+	opts="$($GETOPT -o nqcifm -l no-symbols,qrcode,clip,in-place,force,multiline -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
@@ -501,10 +501,12 @@ cmd_generate() {
 		-c|--clip) clip=1; shift ;;
 		-f|--force) force=1; shift ;;
 		-i|--in-place) inplace=1; shift ;;
+		-m|--multiline) multiline=1; shift ;;
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 || ( $# -ne 2 && $# -ne 1 ) || ( $force -eq 1 && $inplace -eq 1 ) || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--qrcode,-q] [--in-place,-i | --force,-f] pass-name [pass-length]"
+	[[ $err -ne 0 || ( $# -ne 2 && $# -ne 1 ) || ( $force -eq 1 && $inplace -eq 1 ) || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--qrcode,-q] [--in-place,-i | --force,-f] [--multiline,m] pass-name [pass-length]"
+	[[ $inplace -eq 1 && $multiline -eq 1 ]] && die "Error: multiline entries are not allowed when replacing a password in-place."
 	local path="$1"
 	local length="${2:-$GENERATED_LENGTH}"
 	check_sneaky_paths "$path"
@@ -520,7 +522,14 @@ cmd_generate() {
 	read -r -n $length pass < <(LC_ALL=C tr -dc "$characters" < /dev/urandom)
 	[[ ${#pass} -eq $length ]] || die "Could not generate password from /dev/urandom."
 	if [[ $inplace -eq 0 ]]; then
-		echo "$pass" | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "Password encryption aborted."
+		if [[ $multiline -eq 1 ]]; then
+			echo "Enter contents of $path and press Ctrl+D when finished:"
+			echo
+		fi
+		{
+			echo "$pass"
+			[[ $multiline -eq 1 ]] && cat || true
+		} | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" || die "Password encryption aborted."
 	else
 		local passfile_temp="${passfile}.tmp.${RANDOM}.${RANDOM}.${RANDOM}.${RANDOM}.--"
 		if { echo "$pass"; $GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +2; } | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile_temp" "${GPG_OPTS[@]}"; then
-- 
2.20.1



More information about the Password-Store mailing list