<div dir="ltr"><div class="gmail_extra">Do the people of the Password Store Mailing List's Republic of Passwords support this commit? Facundo -- is this what you wanted?<br><br><div class="gmail_quote">On Tue, May 6, 2014 at 5:24 PM, Jason A. Donenfeld <span dir="ltr"><<a href="mailto:Jason@zx2c4.com" target="_blank">Jason@zx2c4.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">---<br>
man/pass.1 | 6 ++++--<br>
src/password-store.sh | 28 +++++++++++++++++++++-------<br>
2 files changed, 25 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/man/pass.1 b/man/pass.1<br>
index 35c81ee..a433a72 100644<br>
--- a/man/pass.1<br>
+++ b/man/pass.1<br>
@@ -110,7 +110,7 @@ ensure that temporary files are created in \fI/dev/shm\fP in order to avoid writ<br>
difficult-to-erase disk sectors. If \fI/dev/shm\fP is not accessible, fallback to<br>
the ordinary \fITMPDIR\fP location, and print a warning.<br>
.TP<br>
-\fBgenerate\fP [ \fI--no-symbols\fP, \fI-n\fP ] [ \fI--clip\fP, \fI-c\fP ] [ \fI--force\fP, \fI-f\fP ] \fIpass-name pass-length\fP<br>
+\fBgenerate\fP [ \fI--no-symbols\fP, \fI-n\fP ] [ \fI--clip\fP, \fI-c\fP ] [ \fI--in-place\fP, \fI-i\fP ] [ \fI--force\fP, \fI-f\fP ] \fIpass-name pass-length\fP<br>
Generate a new password using<br>
.BR pwgen (1)<br>
of length \fIpass-length\fP and insert into \fIpass-name\fP. If \fI--no-symbols\fP or \fI-n\fP<br>
@@ -120,7 +120,9 @@ it to the clipboard using<br>
.BR xclip (1)<br>
and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP) seconds.<br>
Prompt before overwriting an existing password,<br>
-unless \fI--force\fP or \fI-f\fP is specified.<br>
+unless \fI--force\fP or \fI-f\fP is specified. If \fI--in-place\fP or \fI-i\fP is<br>
+specified, do not interactively prompt, and only replace the first line of the password<br>
+file with the new generated password, keeping the remainder of the file intact.<br>
.TP<br>
\fBrm\fP [ \fI--recursive\fP, \fI-r\fP ] [ \fI--force\fP, \fI-f\fP ] \fIpass-name\fP<br>
Remove the password named \fIpass-name\fP from the password store. This command is<br>
diff --git a/src/password-store.sh b/src/password-store.sh<br>
index 8e80798..8e1a124 100755<br>
--- a/src/password-store.sh<br>
+++ b/src/password-store.sh<br>
@@ -230,10 +230,11 @@ cmd_usage() {<br>
overwriting existing password unless forced.<br>
$PROGRAM edit pass-name<br>
Insert a new password or edit an existing password using ${EDITOR:-vi}.<br>
- $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length<br>
+ $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--in-place,-i] [--force,-f] pass-name pass-length<br>
Generate a new password of pass-length with optionally no symbols.<br>
Optionally put it on the clipboard and clear board after 45 seconds.<br>
Prompt before overwriting existing password unless forced.<br>
+ Optionally replace only the first line of an existing file with a new password.<br>
$PROGRAM rm [--recursive,-r] [--force,-f] pass-name<br>
Remove existing password or directory, optionally forcefully.<br>
$PROGRAM mv [--force,-f] old-path new-path<br>
@@ -430,18 +431,19 @@ cmd_edit() {<br>
}<br>
<br>
cmd_generate() {<br>
- local opts clip=0 force=0 symbols="-y"<br>
- opts="$($GETOPT -o ncf -l no-symbols,clip,force -n "$PROGRAM" -- "$@")"<br>
+ local opts clip=0 force=0 symbols="-y" inplace=0<br>
+ opts="$($GETOPT -o ncif -l no-symbols,clip,in-place,force -n "$PROGRAM" -- "$@")"<br>
local err=$?<br>
eval set -- "$opts"<br>
while true; do case $1 in<br>
-n|--no-symbols) symbols=""; shift ;;<br>
-c|--clip) clip=1; shift ;;<br>
-f|--force) force=1; shift ;;<br>
+ -i|--in-place) inplace=1; shift ;;<br>
--) shift; break ;;<br>
esac done<br>
<br>
- [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length"<br>
+ [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND [--no-symbols,-n] [--clip,-c] [--in-place,-i] [--force,-f] pass-name pass-length"<br>
local path="$1"<br>
local length="$2"<br>
check_sneaky_paths "$path"<br>
@@ -450,12 +452,24 @@ cmd_generate() {<br>
set_gpg_recipients "$(dirname "$path")"<br>
local passfile="$PREFIX/$path.gpg"<br>
<br>
- [[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"<br>
+ [[ $inplace -eq 0 && $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"<br>
<br>
local pass="$(pwgen -s $symbols $length 1)"<br>
[[ -n $pass ]] || exit 1<br>
- $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$pass"<br>
- git_add_file "$passfile" "Add generated password for $path to store."<br>
+ if [[ $inplace -eq 0 ]]; then<br>
+ $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" <<<"$pass"<br>
+ else<br>
+ local passfile_temp="${passfile}.tmp.${RANDOM}.${RANDOM}.${RANDOM}.${RANDOM}.--"<br>
+ if $GPG -d "${GPG_OPTS[@]}" "$passfile" | sed $'1c \\\n'"$(sed 's/[\/&]/\\&/g' <<<"$pass")"$'\n' | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile_temp" "${GPG_OPTS[@]}"; then<br>
+ mv "$passfile_temp" "$passfile"<br>
+ else<br>
+ rm -f "$passfile_temp"<br>
+ die "Could not reencrypt new password."<br>
+ fi<br>
+ fi<br>
+ local verb="Add"<br>
+ [[ $inplace -eq 1 ]] && verb="Replace"<br>
+ git_add_file "$passfile" "$verb generated password for ${path}."<br>
<br>
if [[ $clip -eq 0 ]]; then<br>
echo "The generated password to $path is:"<br>
<span class=""><font color="#888888">--<br>
1.9.2<br>
<br>
</font></span></blockquote></div><div><br></div></div></div>