From d5adde1eeb13ba377f7c05b9f21893c01f61ec16 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 7 May 2017 22:15:01 +0200 Subject: exitcodes: pull all exit code definitions into the package This commit defines all exit codes in one place in the exitcodes package. Also, it adds a test to verify the exit code on incorrect password, which is what SiriKali cares about the most. Fixes https://github.com/rfjakob/gocryptfs/issues/77 . --- internal/configfile/scrypt.go | 11 ++++++----- internal/exitcodes/exitcodes.go | 31 +++++++++++++++++++++++++++++-- internal/readpassword/read.go | 24 ++++++++++++------------ internal/stupidgcm/without_openssl.go | 4 +++- 4 files changed, 50 insertions(+), 20 deletions(-) (limited to 'internal') diff --git a/internal/configfile/scrypt.go b/internal/configfile/scrypt.go index 0646754..b5a3edb 100644 --- a/internal/configfile/scrypt.go +++ b/internal/configfile/scrypt.go @@ -8,6 +8,7 @@ import ( "golang.org/x/crypto/scrypt" "github.com/rfjakob/gocryptfs/internal/cryptocore" + "github.com/rfjakob/gocryptfs/internal/exitcodes" "github.com/rfjakob/gocryptfs/internal/tlog" ) @@ -84,22 +85,22 @@ func (s *ScryptKDF) validateParams() { minN := 1 << scryptMinLogN if s.N < minN { tlog.Fatal.Println("Fatal: scryptn below 10 is too low to make sense") - os.Exit(1) + os.Exit(exitcodes.ScryptParams) } if s.R < scryptMinR { tlog.Fatal.Printf("Fatal: scrypt parameter R below minimum: value=%d, min=%d", s.R, scryptMinR) - os.Exit(1) + os.Exit(exitcodes.ScryptParams) } if s.P < scryptMinP { tlog.Fatal.Printf("Fatal: scrypt parameter P below minimum: value=%d, min=%d", s.P, scryptMinP) - os.Exit(1) + os.Exit(exitcodes.ScryptParams) } if len(s.Salt) < scryptMinSaltLen { tlog.Fatal.Printf("Fatal: scrypt salt length below minimum: value=%d, min=%d", len(s.Salt), scryptMinSaltLen) - os.Exit(1) + os.Exit(exitcodes.ScryptParams) } if s.KeyLen < cryptocore.KeyLen { tlog.Fatal.Printf("Fatal: scrypt parameter KeyLen below minimum: value=%d, min=%d", len(s.Salt), cryptocore.KeyLen) - os.Exit(1) + os.Exit(exitcodes.ScryptParams) } } diff --git a/internal/exitcodes/exitcodes.go b/internal/exitcodes/exitcodes.go index 2ec3ae6..7a2efdb 100644 --- a/internal/exitcodes/exitcodes.go +++ b/internal/exitcodes/exitcodes.go @@ -8,12 +8,39 @@ import ( ) const ( + // Usage error: cli syntax etc + Usage = 1 + // 2 is reserved because it is used by Go panic + + // Mount is an error on mount + Mount = 3 + // CipherDir means that the CIPHERDIR does not exist + CipherDir = 6 + // Init is an error on filesystem init + Init = 7 + // LoadConf is an error while loading gocryptfs.conf + LoadConf = 8 + // ReadPassword means something went wrong reading the password + ReadPassword = 9 + // MountPoint error means that the mountpoint is invalid (not empty etc). + MountPoint = 10 // Other error - please inspect the message Other = 11 // PasswordIncorrect - the password was incorrect PasswordIncorrect = 12 - // TODO several other exit codes are defined in main.go. These will be - // ported over here. + // ScryptParams means that scrypt was called with invalid parameters + ScryptParams = 13 + // MasterKey means that something went wrong when parsing the "-masterkey" + // command line option + MasterKey = 14 + // SigInt means we got SIGINT + SigInt = 15 + // PanicLog means the panic log was not empty when we were unmounted + PanicLog = 16 + // ForkChild means forking the worker child failed + ForkChild = 17 + // OpenSSL means you tried to enable OpenSSL, but we were compiled without it. + OpenSSL = 18 ) // Err wraps an error with an associated numeric exit code diff --git a/internal/readpassword/read.go b/internal/readpassword/read.go index 563c62d..058735d 100644 --- a/internal/readpassword/read.go +++ b/internal/readpassword/read.go @@ -12,11 +12,11 @@ import ( "golang.org/x/crypto/ssh/terminal" + "github.com/rfjakob/gocryptfs/internal/exitcodes" "github.com/rfjakob/gocryptfs/internal/tlog" ) const ( - exitCode = 9 // 2kB limit like EncFS maxPasswordLen = 2048 ) @@ -46,7 +46,7 @@ func Twice(extpass string) string { p2 := readPasswordTerminal("Repeat: ") if p1 != p2 { tlog.Fatal.Println("Passwords do not match") - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } return p1 } @@ -60,12 +60,12 @@ func readPasswordTerminal(prompt string) string { p, err := terminal.ReadPassword(fd) if err != nil { tlog.Fatal.Printf("Could not read password from terminal: %v\n", err) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } fmt.Fprintf(os.Stderr, "\n") if len(p) == 0 { tlog.Fatal.Println("Password is empty") - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } return string(p) } @@ -77,7 +77,7 @@ func readPasswordStdin() string { p := readLineUnbuffered(os.Stdin) if len(p) == 0 { tlog.Fatal.Println("Got empty password from stdin") - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } return p } @@ -102,23 +102,23 @@ func readPasswordExtpass(extpass string) string { pipe, err := cmd.StdoutPipe() if err != nil { tlog.Fatal.Printf("extpass pipe setup failed: %v", err) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } err = cmd.Start() if err != nil { tlog.Fatal.Printf("extpass cmd start failed: %v", err) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } p := readLineUnbuffered(pipe) pipe.Close() err = cmd.Wait() if err != nil { tlog.Fatal.Printf("extpass program returned an error: %v", err) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } if len(p) == 0 { tlog.Fatal.Println("extpass: password is empty") - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } return p } @@ -130,7 +130,7 @@ func readLineUnbuffered(r io.Reader) (l string) { for { if len(l) > maxPasswordLen { tlog.Fatal.Printf("fatal: maximum password length of %d bytes exceeded", maxPasswordLen) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } n, err := r.Read(b) if err == io.EOF { @@ -138,7 +138,7 @@ func readLineUnbuffered(r io.Reader) (l string) { } if err != nil { tlog.Fatal.Printf("readLineUnbuffered: %v", err) - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } if n == 0 { continue @@ -170,7 +170,7 @@ func CheckTrailingGarbage() { n, _ := os.Stdin.Read(b) if n > 0 { tlog.Fatal.Printf("Received trailing garbage after the password") - os.Exit(exitCode) + os.Exit(exitcodes.ReadPassword) } }() // Wait for the goroutine to start up plus one millisecond for the read to diff --git a/internal/stupidgcm/without_openssl.go b/internal/stupidgcm/without_openssl.go index 1c6ebcf..efd37f3 100644 --- a/internal/stupidgcm/without_openssl.go +++ b/internal/stupidgcm/without_openssl.go @@ -5,6 +5,8 @@ package stupidgcm import ( "fmt" "os" + + "github.com/rfjakob/gocryptfs/internal/exitcodes" ) type stupidGCM struct{} @@ -16,7 +18,7 @@ const ( func errExit() { fmt.Fprintln(os.Stderr, "gocryptfs has been compiled without openssl support but you are still trying to use openssl") - os.Exit(2) + os.Exit(exitcodes.OpenSSL) } func New(_ []byte, _ bool) stupidGCM { -- cgit v1.2.3