cmd/age: allow reading both passphrase and input from a terminal

Fixes #196
Closes #258
This commit is contained in:
Filippo Valsorda
2021-04-23 01:59:11 -04:00
parent 581cff8473
commit eaf2cef49d
2 changed files with 8 additions and 5 deletions

View File

@@ -45,15 +45,15 @@ func (i *LazyScryptIdentity) Unwrap(stanzas []*age.Stanza) (fileKey []byte, err
return fileKey, err
}
// stdinInUse is set in main. It's a singleton like os.Stdin.
var stdinInUse bool
// readPassphrase reads a passphrase from the terminal. If stdin is not
// connected to a terminal, it tries /dev/tty and fails if that's not available.
// It does not read from a non-terminal stdin, so it does not check stdinInUse.
func readPassphrase() ([]byte, error) {
fd := int(os.Stdin.Fd())
if !terminal.IsTerminal(fd) || stdinInUse {
if !terminal.IsTerminal(fd) {
tty, err := os.Open("/dev/tty")
if err != nil {
return nil, fmt.Errorf("standard input is not available or not a terminal, and opening /dev/tty failed: %v", err)
return nil, fmt.Errorf("standard input is not a terminal, and opening /dev/tty failed: %v", err)
}
defer tty.Close()
fd = int(tty.Fd())

View File

@@ -22,6 +22,9 @@ import (
"golang.org/x/crypto/ssh"
)
// stdinInUse is set in main. It's a singleton like os.Stdin.
var stdinInUse bool
func parseRecipient(arg string) (age.Recipient, error) {
switch {
case strings.HasPrefix(arg, "age1"):