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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2017-02-12 15:35:50 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-06-15 22:43:31 +02:00
|
|
|
|
|
|
|
"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
|
|
|
|
// or stdin.
|
2016-06-15 22:43:31 +02:00
|
|
|
func Once(extpass string) string {
|
|
|
|
if extpass != "" {
|
|
|
|
return readPasswordExtpass(extpass)
|
|
|
|
}
|
|
|
|
if !terminal.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
|
return readPasswordStdin()
|
|
|
|
}
|
|
|
|
return readPasswordTerminal("Password: ")
|
|
|
|
}
|
|
|
|
|
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.
|
2016-06-15 22:43:31 +02:00
|
|
|
func Twice(extpass string) string {
|
|
|
|
if extpass != "" {
|
|
|
|
return readPasswordExtpass(extpass)
|
|
|
|
}
|
|
|
|
if !terminal.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
|
return readPasswordStdin()
|
|
|
|
}
|
|
|
|
p1 := readPasswordTerminal("Password: ")
|
|
|
|
p2 := readPasswordTerminal("Repeat: ")
|
|
|
|
if 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
|
|
|
}
|
|
|
|
return p1
|
|
|
|
}
|
|
|
|
|
|
|
|
// readPasswordTerminal reads a line from the terminal.
|
|
|
|
// Exits on read error or empty result.
|
|
|
|
func readPasswordTerminal(prompt string) string {
|
|
|
|
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
|
|
|
}
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
|
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.
|
2016-06-15 22:43:31 +02:00
|
|
|
func readPasswordStdin() string {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Println("Reading password from stdin")
|
2016-06-15 22:43:31 +02:00
|
|
|
p := readLineUnbuffered(os.Stdin)
|
|
|
|
if len(p) == 0 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println("Got empty password from stdin")
|
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.
|
|
|
|
func readPasswordExtpass(extpass string) string {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Println("Reading password from extpass program")
|
2017-01-29 00:34:12 +01:00
|
|
|
var parts []string
|
|
|
|
// The option "-passfile=FILE" gets transformed to
|
|
|
|
// "-extpass="/bin/cat -- FILE". We don't want to split FILE on spaces,
|
|
|
|
// so let's handle it manually.
|
|
|
|
passfileCat := "/bin/cat -- "
|
|
|
|
if strings.HasPrefix(extpass, passfileCat) {
|
|
|
|
parts = []string{"/bin/cat", "--", extpass[len(passfileCat):]}
|
|
|
|
} else {
|
|
|
|
parts = strings.Split(extpass, " ")
|
|
|
|
}
|
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".
|
|
|
|
func readLineUnbuffered(r io.Reader) (l string) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
l = l + string(b)
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 15:35:50 +01:00
|
|
|
|
|
|
|
// CheckTrailingGarbage tries to read one byte from stdin and exits with a
|
|
|
|
// fatal error if the read returns any data.
|
|
|
|
// This is meant to be called after reading the password, when there is no more
|
|
|
|
// data expected. This helps to catch problems with third-party tools that
|
|
|
|
// interface with gocryptfs.
|
|
|
|
//
|
|
|
|
// This is tested via TestInitTrailingGarbage() in tests/cli/cli_test.go.
|
|
|
|
func CheckTrailingGarbage() {
|
|
|
|
if terminal.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
|
// Be lenient when interacting with a human.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
b := make([]byte, 1)
|
|
|
|
wg.Done()
|
|
|
|
n, _ := os.Stdin.Read(b)
|
|
|
|
if n > 0 {
|
|
|
|
tlog.Fatal.Printf("Received trailing garbage after the password")
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ReadPassword)
|
2017-02-12 15:35:50 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Wait for the goroutine to start up plus one millisecond for the read to
|
|
|
|
// return. If there is data available, this SHOULD be plenty of time to
|
|
|
|
// read one byte. However, I don't see a way to be sure.
|
|
|
|
wg.Wait()
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|