From 4b251f3ce1f0a0472ed10a00aeef70c69ba03a5d Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Mon, 3 Jan 2022 15:18:59 +0100 Subject: readpassword: bubble up errors instead of exiting the process This allows cleanups to happen in the caller, like removing the control socket. Fixes https://github.com/rfjakob/gocryptfs/issues/634 --- internal/readpassword/passfile.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'internal/readpassword/passfile.go') diff --git a/internal/readpassword/passfile.go b/internal/readpassword/passfile.go index 29fde6c..60902b7 100644 --- a/internal/readpassword/passfile.go +++ b/internal/readpassword/passfile.go @@ -2,28 +2,31 @@ package readpassword import ( "bytes" + "fmt" "os" - "github.com/rfjakob/gocryptfs/v2/internal/exitcodes" "github.com/rfjakob/gocryptfs/v2/internal/tlog" ) // readPassFileConcatenate reads the first line from each file name and // concatenates the results. The result does not contain any newlines. -func readPassFileConcatenate(passfileSlice []string) (result []byte) { +func readPassFileConcatenate(passfileSlice []string) (result []byte, err error) { for _, e := range passfileSlice { - result = append(result, readPassFile(e)...) + add, err := readPassFile(e) + if err != nil { + return nil, err + } + result = append(result, add...) } - return result + return result, nil } // readPassFile reads the first line from the passed file name. -func readPassFile(passfile string) []byte { +func readPassFile(passfile string) ([]byte, error) { tlog.Info.Printf("passfile: reading from file %q", passfile) f, err := os.Open(passfile) if err != nil { - tlog.Fatal.Printf("fatal: passfile: could not open %q: %v", passfile, err) - os.Exit(exitcodes.ReadPassword) + return nil, fmt.Errorf("fatal: passfile: could not open %q: %v", passfile, err) } defer f.Close() // +1 for an optional trailing newline, @@ -31,23 +34,20 @@ func readPassFile(passfile string) []byte { buf := make([]byte, maxPasswordLen+2) n, err := f.Read(buf) if err != nil { - tlog.Fatal.Printf("fatal: passfile: could not read from %q: %v", passfile, err) - os.Exit(exitcodes.ReadPassword) + return nil, fmt.Errorf("fatal: passfile: could not read from %q: %v", passfile, err) } buf = buf[:n] // Split into first line and "trailing garbage" lines := bytes.SplitN(buf, []byte("\n"), 2) if len(lines[0]) == 0 { - tlog.Fatal.Printf("fatal: passfile: empty first line in %q", passfile) - os.Exit(exitcodes.ReadPassword) + return nil, fmt.Errorf("fatal: passfile: empty first line in %q", passfile) } if len(lines[0]) > maxPasswordLen { - tlog.Fatal.Printf("fatal: passfile: max password length (%d bytes) exceeded", maxPasswordLen) - os.Exit(exitcodes.ReadPassword) + return nil, fmt.Errorf("fatal: passfile: max password length (%d bytes) exceeded", maxPasswordLen) } if len(lines) > 1 && len(lines[1]) > 0 { tlog.Warn.Printf("warning: passfile: ignoring trailing garbage (%d bytes) after first line", len(lines[1])) } - return lines[0] + return lines[0], nil } -- cgit v1.2.3