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"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2017-05-07 22:15:01 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/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
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Once tries to get a password from the user, either from the terminal, extpass
|
2018-03-22 00:02:10 +01:00
|
|
|
// or stdin. Leave "prompt" empty to use the default "Password: " prompt.
|
2019-03-03 13:25:30 +01:00
|
|
|
func Once(extpass []string, passfile string, prompt string) []byte {
|
2018-12-15 17:09:38 +01:00
|
|
|
if passfile != "" {
|
|
|
|
return readPassFile(passfile)
|
|
|
|
}
|
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"
|
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
if !terminal.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.
|
2019-03-03 13:25:30 +01:00
|
|
|
func Twice(extpass []string, passfile string) []byte {
|
2018-12-15 17:09:38 +01:00
|
|
|
if passfile != "" {
|
|
|
|
return readPassFile(passfile)
|
|
|
|
}
|
2019-03-03 13:25:30 +01:00
|
|
|
if len(extpass) != 0 {
|
2016-06-15 22:43:31 +02:00
|
|
|
return readPasswordExtpass(extpass)
|
|
|
|
}
|
|
|
|
if !terminal.IsTerminal(int(os.Stdin.Fd())) {
|
2018-03-22 00:02:10 +01:00
|
|
|
return readPasswordStdin("Password")
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
p1 := readPasswordTerminal("Password: ")
|
|
|
|
p2 := readPasswordTerminal("Repeat: ")
|
2018-02-18 14:26:54 +01:00
|
|
|
if !bytes.Equal(p1, p2) {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println("Passwords do not match")
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
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
|
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
return p1
|
|
|
|
}
|
|
|
|
|
|
|
|
// readPasswordTerminal reads a line from the terminal.
|
|
|
|
// Exits on read error or empty result.
|
2018-02-18 14:26:54 +01:00
|
|
|
func readPasswordTerminal(prompt string) []byte {
|
2016-06-15 22:43:31 +02:00
|
|
|
fd := int(os.Stdin.Fd())
|
|
|
|
fmt.Fprintf(os.Stderr, prompt)
|
|
|
|
// terminal.ReadPassword removes the trailing newline
|
|
|
|
p, err := terminal.ReadPassword(fd)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("Could not read password from terminal: %v\n", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
if len(p) == 0 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println("Password is empty")
|
2017-05-14 14:02:08 +02:00
|
|
|
os.Exit(exitcodes.PasswordEmpty)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
2018-02-18 14:26:54 +01:00
|
|
|
return p
|
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.
|
2018-03-22 00:02:10 +01:00
|
|
|
func readPasswordStdin(prompt string) []byte {
|
|
|
|
tlog.Info.Printf("Reading %s from stdin", prompt)
|
2016-06-15 22:43:31 +02:00
|
|
|
p := readLineUnbuffered(os.Stdin)
|
|
|
|
if len(p) == 0 {
|
2018-03-22 00:02:10 +01:00
|
|
|
tlog.Fatal.Printf("Got empty %s from stdin", prompt)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// readPasswordExtpass executes the "extpass" program and returns the first line
|
|
|
|
// of the output.
|
|
|
|
// Exits on read error or empty result.
|
2019-03-03 13:25:30 +01:00
|
|
|
func readPasswordExtpass(extpass []string) []byte {
|
|
|
|
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 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("extpass pipe setup failed: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("extpass cmd start failed: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
p := readLineUnbuffered(pipe)
|
|
|
|
pipe.Close()
|
2016-10-16 16:44:27 +02:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("extpass program returned an error: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-10-16 16:44:27 +02:00
|
|
|
}
|
2016-06-15 22:43:31 +02:00
|
|
|
if len(p) == 0 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println("extpass: password is empty")
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// readLineUnbuffered reads single bytes from "r" util it gets "\n" or EOF.
|
|
|
|
// The returned string does NOT contain the trailing "\n".
|
2018-02-18 14:26:54 +01:00
|
|
|
func readLineUnbuffered(r io.Reader) (l []byte) {
|
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 {
|
|
|
|
tlog.Fatal.Printf("fatal: maximum password length of %d bytes exceeded", maxPasswordLen)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
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 {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("readLineUnbuffered: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if b[0] == '\n' {
|
|
|
|
return l
|
|
|
|
}
|
2018-02-18 14:26:54 +01:00
|
|
|
l = append(l, b...)
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
}
|