2016-07-06 21:51:25 +02:00
|
|
|
// Package readpassword reads a password from the terminal of from stdin.
|
2016-06-15 22:43:31 +02:00
|
|
|
package readpassword
|
|
|
|
|
|
|
|
import (
|
2018-02-18 14:26:54 +01:00
|
|
|
"bytes"
|
2016-06-15 22:43:31 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-28 12:03:34 +02:00
|
|
|
"golang.org/x/term"
|
2016-06-15 22:43:31 +02:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
|
2016-06-15 22:43:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-04-29 15:15:11 +02:00
|
|
|
// 2kB limit like EncFS
|
|
|
|
maxPasswordLen = 2048
|
2016-06-15 22:43:31 +02:00
|
|
|
)
|
|
|
|
|
2020-05-17 19:31:04 +02:00
|
|
|
// Once tries to get a password from the user, either from the terminal, extpass, passfile
|
2018-03-22 00:02:10 +01:00
|
|
|
// or stdin. Leave "prompt" empty to use the default "Password: " prompt.
|
2022-01-03 15:18:59 +01:00
|
|
|
func Once(extpass []string, passfile []string, prompt string) ([]byte, error) {
|
2020-05-17 19:31:04 +02:00
|
|
|
if len(passfile) != 0 {
|
|
|
|
return readPassFileConcatenate(passfile)
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
2019-03-03 13:25:30 +01:00
|
|
|
if len(extpass) != 0 {
|
2016-06-15 22:43:31 +02:00
|
|
|
return readPasswordExtpass(extpass)
|
|
|
|
}
|
2018-03-22 00:02:10 +01:00
|
|
|
if prompt == "" {
|
|
|
|
prompt = "Password"
|
|
|
|
}
|
2022-08-28 12:03:34 +02:00
|
|
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
2018-03-22 00:02:10 +01:00
|
|
|
return readPasswordStdin(prompt)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2018-03-22 00:02:10 +01:00
|
|
|
return readPasswordTerminal(prompt + ": ")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Twice is the same as Once but will prompt twice if we get the password from
|
|
|
|
// the terminal.
|
2022-01-03 15:18:59 +01:00
|
|
|
func Twice(extpass []string, passfile []string) ([]byte, error) {
|
2020-05-17 19:31:04 +02:00
|
|
|
if len(passfile) != 0 {
|
|
|
|
return readPassFileConcatenate(passfile)
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
2019-03-03 13:25:30 +01:00
|
|
|
if len(extpass) != 0 {
|
2016-06-15 22:43:31 +02:00
|
|
|
return readPasswordExtpass(extpass)
|
|
|
|
}
|
2022-08-28 12:03:34 +02:00
|
|
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
2018-03-22 00:02:10 +01:00
|
|
|
return readPasswordStdin("Password")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
p1, err := readPasswordTerminal("Password: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p2, err := readPasswordTerminal("Repeat: ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-18 14:26:54 +01:00
|
|
|
if !bytes.Equal(p1, p2) {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("Passwords do not match")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2018-02-18 15:33:35 +01:00
|
|
|
// Wipe the password duplicate from memory
|
|
|
|
for i := range p2 {
|
|
|
|
p2[i] = 0
|
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
return p1, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPasswordTerminal reads a line from the terminal.
|
|
|
|
// Exits on read error or empty result.
|
2022-01-03 15:18:59 +01:00
|
|
|
func readPasswordTerminal(prompt string) ([]byte, error) {
|
2016-06-15 22:43:31 +02:00
|
|
|
fd := int(os.Stdin.Fd())
|
|
|
|
fmt.Fprintf(os.Stderr, prompt)
|
2022-08-28 12:03:34 +02:00
|
|
|
// term.ReadPassword removes the trailing newline
|
|
|
|
p, err := term.ReadPassword(fd)
|
2016-06-15 22:43:31 +02:00
|
|
|
if err != nil {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("Could not read password from terminal: %v\n", err)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
if len(p) == 0 {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("Password is empty")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
return p, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
2017-02-12 13:55:15 +01:00
|
|
|
// readPasswordStdin reads a line from stdin.
|
|
|
|
// It exits with a fatal error on read error or empty result.
|
2022-01-03 15:18:59 +01:00
|
|
|
func readPasswordStdin(prompt string) ([]byte, error) {
|
2018-03-22 00:02:10 +01:00
|
|
|
tlog.Info.Printf("Reading %s from stdin", prompt)
|
2022-01-03 15:18:59 +01:00
|
|
|
p, err := readLineUnbuffered(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
if len(p) == 0 {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("Got empty %s from stdin", prompt)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
return p, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPasswordExtpass executes the "extpass" program and returns the first line
|
|
|
|
// of the output.
|
|
|
|
// Exits on read error or empty result.
|
2022-01-03 15:18:59 +01:00
|
|
|
func readPasswordExtpass(extpass []string) ([]byte, error) {
|
2019-03-03 13:25:30 +01:00
|
|
|
var parts []string
|
|
|
|
if len(extpass) == 1 {
|
|
|
|
parts = strings.Split(extpass[0], " ")
|
|
|
|
} else {
|
|
|
|
parts = extpass
|
|
|
|
}
|
|
|
|
tlog.Info.Printf("Reading password from extpass program %q, arguments: %q\n", parts[0], parts[1:])
|
2016-06-15 22:43:31 +02:00
|
|
|
cmd := exec.Command(parts[0], parts[1:]...)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
pipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("extpass pipe setup failed: %v", err)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("extpass cmd start failed: %v", err)
|
|
|
|
}
|
|
|
|
p, err := readLineUnbuffered(pipe)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
pipe.Close()
|
2016-10-16 16:44:27 +02:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("extpass program returned an error: %v", err)
|
2016-10-16 16:44:27 +02:00
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
if len(p) == 0 {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("extpass: password is empty")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
return p, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLineUnbuffered reads single bytes from "r" util it gets "\n" or EOF.
|
|
|
|
// The returned string does NOT contain the trailing "\n".
|
2022-01-03 15:18:59 +01:00
|
|
|
func readLineUnbuffered(r io.Reader) (l []byte, err error) {
|
2016-06-15 22:43:31 +02:00
|
|
|
b := make([]byte, 1)
|
|
|
|
for {
|
2017-02-13 09:13:22 +01:00
|
|
|
if len(l) > maxPasswordLen {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("fatal: maximum password length of %d bytes exceeded", maxPasswordLen)
|
2017-02-13 09:13:22 +01:00
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
n, err := r.Read(b)
|
|
|
|
if err == io.EOF {
|
2022-01-03 15:18:59 +01:00
|
|
|
return l, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-01-03 15:18:59 +01:00
|
|
|
return nil, fmt.Errorf("readLineUnbuffered: %v", err)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if b[0] == '\n' {
|
2022-01-03 15:18:59 +01:00
|
|
|
return l, nil
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2018-02-18 14:26:54 +01:00
|
|
|
l = append(l, b...)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
}
|