tAdd possibility to read password from stdin - safe - password protected secret keeper
HTML git clone git://git.z3bra.org/safe.git
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 879752024f41f05503b49fd8c24da0ca3756619b
DIR parent 0f6635934701f13688f1cad6556e3953bfa0f1ee
HTML Author: Willy Goiffon <contact@z3bra.org>
Date: Tue, 24 May 2022 15:50:10 +0200
Add possibility to read password from stdin
Diffstat:
M safe.1 | 5 +++--
M safe.c | 18 ++++++++++++------
2 files changed, 15 insertions(+), 8 deletions(-)
---
DIR diff --git a/safe.1 b/safe.1
t@@ -6,10 +6,9 @@
.Nd digital safe for your secrets
.Sh SYNOPSIS
.Nm
-.Op Fl hr
+.Op Fl abfhr
.Op Fl p Ar prompt
.Op Fl s Ar safe
-.Op Fl af
.Ar secret
.Sh DESCRIPTION
.Nm
t@@ -42,6 +41,8 @@ Encrypt stdin to your safe as
Use
.Fl f
to overwrite an existing secret.
+.It Fl b
+Batch mode. Reads master password from stdin.
.It Fl f
Force writing to
.Ar secret
DIR diff --git a/safe.c b/safe.c
t@@ -42,7 +42,7 @@ char *argv0;
void
usage(void)
{
- fprintf(stderr, "usage: %s [-hr] [-s safe] [-p prompt] [[-af] entry]\n", argv0);
+ fprintf(stderr, "usage: %s [-bhr] [-s safe] [-p prompt] [[-af] entry]\n", argv0);
exit(1);
}
t@@ -168,7 +168,7 @@ spawn_askpass(const char *askpass, const char *msg, char *buf, size_t bufsiz)
}
int
-readpass(const char *prompt, uint8_t **target, size_t *len, int askflag)
+readpass(const char *prompt, uint8_t **target, size_t *len, int askflag, int stdinflag)
{
char pass[BUFSIZ], *askpass, *p;
if (askflag) {
t@@ -179,7 +179,10 @@ readpass(const char *prompt, uint8_t **target, size_t *len, int askflag)
if (!p)
err(1, "askpass:");
} else {
- p = readpassphrase(prompt, pass, sizeof(pass), RPP_ECHO_OFF|RPP_REQUIRE_TTY);
+ int flags = 0;
+ flags |= RPP_ECHO_OFF;
+ flags |= stdinflag ? RPP_STDIN : RPP_REQUIRE_TTY;
+ p = readpassphrase(prompt, pass, sizeof(pass), flags);
if (!p)
err(1, "readpassphrase:");
}
t@@ -374,7 +377,7 @@ readsecret(struct safe *s, int in, int out)
int
main(int argc, char *argv[])
{
- int aflag = 0, rflag = 0, kflag = 0, fflag = 0;
+ int aflag = 0, bflag = 0, rflag = 0, kflag = 0, fflag = 0;
int fd, haskey = 0, hasmaster = 1, ttyfd;
char *prompt, *secret, *sockp, *safe = SAFE;
struct safe s;
t@@ -391,6 +394,9 @@ main(int argc, char *argv[])
case 'a':
aflag = 1;
break;
+ case 'b':
+ bflag = 1;
+ break;
case 'p':
prompt = EARGF(usage());
break;
t@@ -448,7 +454,7 @@ main(int argc, char *argv[])
close(ttyfd);
if (!haskey) {
- if (readpass(prompt, &passphrase, &pplen, kflag) < 0)
+ if (readpass(prompt, &passphrase, &pplen, kflag, bflag) < 0)
return -1;
sodium_mlock(passphrase, pplen);
t@@ -459,7 +465,7 @@ main(int argc, char *argv[])
size_t pplen2 = 0;
/* input for master password again to check */
- if (readpass("verify:", &passphrase2, &pplen2, kflag) < 0)
+ if (readpass("verify:", &passphrase2, &pplen2, kflag, bflag) < 0)
return -1;
sodium_mlock(passphrase2, pplen2);