toggledlog: convert remaing naked fmt.Print*

Several fatal errors were just printed to stdout, which
meant they were invisible when running the test suite.

Fix this by introducing toggledlog.Fatal and convert as
follows:

Fatal errors     -> toggledlog.Fatal
Warnings         -> toggledlog.Warn
Password prompts -> fmt.Fprintf
This commit is contained in:
Jakob Unterwurzacher 2016-06-05 14:26:16 +02:00
parent ca54b665e3
commit 0c80cca674
8 changed files with 65 additions and 49 deletions

View File

@ -6,6 +6,8 @@ import (
"os/exec"
"os/signal"
"syscall"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
// The child sends us USR1 if the mount was successful
@ -30,7 +32,7 @@ func forkChild() {
c.Stdin = os.Stdin
err := c.Start()
if err != nil {
fmt.Printf("forkChild: starting %s failed: %v\n", name, err)
toggledlog.Fatal.Printf("forkChild: starting %s failed: %v\n", name, err)
os.Exit(1)
}
err = c.Wait()
@ -40,7 +42,7 @@ func forkChild() {
os.Exit(waitstat.ExitStatus())
}
}
fmt.Printf("forkChild: wait returned an unknown error: %v\n", err)
toggledlog.Fatal.Printf("forkChild: wait returned an unknown error: %v\n", err)
os.Exit(1)
}
// The child exited with 0 - let's do the same.

View File

@ -1,13 +1,14 @@
package configfile
import (
"fmt"
"log"
"math"
"os"
"golang.org/x/crypto/scrypt"
"github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
const (
@ -31,7 +32,7 @@ func NewScryptKdf(logN int) scryptKdf {
s.N = 1 << ScryptDefaultLogN
} else {
if logN < 10 {
fmt.Println("Error: scryptn below 10 is too low to make sense. Aborting.")
toggledlog.Fatal.Println("Error: scryptn below 10 is too low to make sense. Aborting.")
os.Exit(1)
}
s.N = 1 << uint32(logN)
@ -45,7 +46,7 @@ func NewScryptKdf(logN int) scryptKdf {
func (s *scryptKdf) DeriveKey(pw string) []byte {
k, err := scrypt.Key([]byte(pw), s.Salt, s.N, s.R, s.P, s.KeyLen)
if err != nil {
panic(fmt.Sprintf("DeriveKey failed: %s", err.Error()))
log.Panicf("DeriveKey failed: %v", err)
}
return k
}

View File

@ -1,9 +1,10 @@
package prefer_openssl
import (
"fmt"
"io/ioutil"
"regexp"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
// filePreferOpenSSL tells us if OpenSSL is faster than Go GCM on this machine.
@ -17,12 +18,12 @@ import (
func filePreferOpenSSL(file string) bool {
ci, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
toggledlog.Warn.Println(err)
return true
}
haveAes, err := regexp.Match(`(?m)^flags.*\baes\b`, ci)
if err != nil {
fmt.Println(err)
toggledlog.Warn.Println(err)
return true
}
return !haveAes

View File

@ -49,18 +49,24 @@ func (l *toggledLogger) Println(v ...interface{}) {
}
}
// As defined by http://elinux.org/Debugging_by_printing#Log_Levels
// Debug messages
// Can be enabled by passing "-d"
var Debug *toggledLogger
// Informational message e.g. startup information
// Informational message
// Can be disabled by passing "-q"
var Info *toggledLogger
// A warning, meaning nothing serious by itself but might indicate problems
// A warning, meaning nothing serious by itself but might indicate problems.
// Passing "-wpanic" will make this function panic after printing the message.
var Warn *toggledLogger
// Fatal error, we are about to exit
var Fatal *toggledLogger
func init() {
Debug = &toggledLogger{false, false, log.New(os.Stdout, "", 0)}
Info = &toggledLogger{true, false, log.New(os.Stdout, "", 0)}
Warn = &toggledLogger{true, false, log.New(os.Stderr, "", 0)}
Fatal = &toggledLogger{true, false, log.New(os.Stderr, "", 0)}
}

54
main.go
View File

@ -61,7 +61,7 @@ var GitVersionFuse = "[version not set - please compile using ./build.bash]"
func initDir(args *argContainer) {
err := checkDirEmpty(args.cipherdir)
if err != nil {
fmt.Printf("Invalid cipherdir: %v\n", err)
toggledlog.Fatal.Printf("Invalid cipherdir: %v\n", err)
os.Exit(ERREXIT_INIT)
}
@ -75,7 +75,7 @@ func initDir(args *argContainer) {
creator := toggledlog.ProgramName + " " + GitVersion
err = configfile.CreateConfFile(args.config, password, args.plaintextnames, args.scryptn, creator)
if err != nil {
fmt.Println(err)
toggledlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
@ -83,7 +83,7 @@ func initDir(args *argContainer) {
// Create gocryptfs.diriv in the root dir
err = nametransform.WriteDirIV(args.cipherdir)
if err != nil {
fmt.Println(err)
toggledlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
}
@ -103,10 +103,13 @@ func initDir(args *argContainer) {
func usageText() {
printVersion()
fmt.Printf("\n")
fmt.Printf("Usage: %s -init|-passwd [OPTIONS] CIPHERDIR\n", toggledlog.ProgramName)
fmt.Printf(" or %s [OPTIONS] CIPHERDIR MOUNTPOINT\n", toggledlog.ProgramName)
fmt.Printf("\nOptions:\n")
fmt.Printf(`
Usage: %s -init|-passwd [OPTIONS] CIPHERDIR
or %s [OPTIONS] CIPHERDIR MOUNTPOINT
Options:
`, toggledlog.ProgramName, toggledlog.ProgramName)
flagSet.PrintDefaults()
}
@ -115,17 +118,17 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf
// Check if the file exists at all before prompting for a password
_, err := os.Stat(args.config)
if err != nil {
fmt.Printf(colorRed+"Config file not found: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Config file not found: %v\n"+colorReset, err)
os.Exit(ERREXIT_LOADCONF)
}
if args.extpass == "" {
fmt.Printf("Password: ")
fmt.Fprintf(os.Stderr, "Password: ")
}
pw := readPassword(args.extpass)
toggledlog.Info.Printf("Decrypting master key... ")
masterkey, confFile, err = configfile.LoadConfFile(args.config, pw)
if err != nil {
fmt.Println(os.Stderr, colorRed+err.Error()+colorReset)
toggledlog.Fatal.Println(colorRed + err.Error() + colorReset)
os.Exit(ERREXIT_LOADCONF)
}
toggledlog.Info.Printf("done.")
@ -136,12 +139,12 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf
// changePassword - change the password of config file "filename"
func changePassword(args *argContainer) {
masterkey, confFile := loadConfig(args)
fmt.Println("Please enter your new password.")
toggledlog.Info.Println("Please enter your new password.")
newPw := readPasswordTwice(args.extpass)
confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN())
err := confFile.WriteFile()
if err != nil {
fmt.Println(err)
toggledlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
toggledlog.Info.Printf("Password changed.")
@ -203,7 +206,7 @@ func main() {
} else {
args.openssl, err = strconv.ParseBool(opensslAuto)
if err != nil {
fmt.Printf(colorRed+"Invalid \"-openssl\" setting: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Invalid \"-openssl\" setting: %v\n"+colorReset, err)
os.Exit(ERREXIT_USAGE)
}
}
@ -230,7 +233,7 @@ func main() {
args.cipherdir, _ = filepath.Abs(flagSet.Arg(0))
err := checkDir(args.cipherdir)
if err != nil {
fmt.Printf(colorRed+"Invalid cipherdir: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Invalid cipherdir: %v\n"+colorReset, err)
os.Exit(ERREXIT_CIPHERDIR)
}
} else {
@ -245,7 +248,8 @@ func main() {
if args.config != "" {
args.config, err = filepath.Abs(args.config)
if err != nil {
fmt.Printf(colorRed+"Invalid \"-config\" setting: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Invalid \"-config\" setting: %v\n"+colorReset, err)
os.Exit(ERREXIT_INIT)
}
toggledlog.Info.Printf("Using config file at custom location %s", args.config)
} else {
@ -257,7 +261,7 @@ func main() {
var f *os.File
f, err = os.Create(args.cpuprofile)
if err != nil {
fmt.Println(err)
toggledlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
pprof.StartCPUProfile(f)
@ -269,7 +273,7 @@ func main() {
var f *os.File
f, err = os.Create(args.memprofile)
if err != nil {
fmt.Println(err)
toggledlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
defer func() {
@ -279,7 +283,7 @@ func main() {
}()
}
if args.cpuprofile != "" || args.memprofile != "" {
fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
toggledlog.Info.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
}
// "-openssl"
if args.openssl == false {
@ -291,7 +295,7 @@ func main() {
// "-init"
if args.init {
if flagSet.NArg() > 1 {
fmt.Printf("Usage: %s -init [OPTIONS] CIPHERDIR\n", toggledlog.ProgramName)
toggledlog.Fatal.Printf("Usage: %s -init [OPTIONS] CIPHERDIR\n", toggledlog.ProgramName)
os.Exit(ERREXIT_USAGE)
}
initDir(&args) // does not return
@ -299,7 +303,7 @@ func main() {
// "-passwd"
if args.passwd {
if flagSet.NArg() > 1 {
fmt.Printf("Usage: %s -passwd [OPTIONS] CIPHERDIR\n", toggledlog.ProgramName)
toggledlog.Fatal.Printf("Usage: %s -passwd [OPTIONS] CIPHERDIR\n", toggledlog.ProgramName)
os.Exit(ERREXIT_USAGE)
}
changePassword(&args) // does not return
@ -307,17 +311,17 @@ func main() {
// Mount
// Check mountpoint
if flagSet.NArg() != 2 {
usageText()
toggledlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT\n", toggledlog.ProgramName)
os.Exit(ERREXIT_USAGE)
}
args.mountpoint, err = filepath.Abs(flagSet.Arg(1))
if err != nil {
fmt.Printf(colorRed+"Invalid mountpoint: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Invalid mountpoint: %v\n"+colorReset, err)
os.Exit(ERREXIT_MOUNTPOINT)
}
err = checkDirEmpty(args.mountpoint)
if err != nil {
fmt.Printf(colorRed+"Invalid mountpoint: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Invalid mountpoint: %v\n"+colorReset, err)
os.Exit(ERREXIT_MOUNTPOINT)
}
// Get master key
@ -424,7 +428,7 @@ func initFuseFrontend(key []byte, args argContainer, confFile *configfile.ConfFi
srv, err := fuse.NewServer(conn.RawFS(), args.mountpoint, &mOpts)
if err != nil {
fmt.Printf("Mount failed: %v", err)
toggledlog.Fatal.Printf("Mount failed: %v", err)
os.Exit(ERREXIT_MOUNT)
}
srv.SetDebug(args.fusedebug)
@ -445,7 +449,7 @@ func handleSigint(srv *fuse.Server, mountpoint string) {
<-ch
err := srv.Unmount()
if err != nil {
fmt.Print(err)
toggledlog.Warn.Print(err)
toggledlog.Info.Printf("Trying lazy unmount")
cmd := exec.Command("fusermount", "-u", "-z", mountpoint)
cmd.Stdout = os.Stdout

View File

@ -2,7 +2,6 @@ package main
import (
"encoding/hex"
"fmt"
"os"
"strings"
@ -45,11 +44,11 @@ func parseMasterKey(masterkey string) []byte {
masterkey = strings.Replace(masterkey, "-", "", -1)
key, err := hex.DecodeString(masterkey)
if err != nil {
fmt.Printf("Could not parse master key: %v\n", err)
toggledlog.Fatal.Printf("Could not parse master key: %v\n", err)
os.Exit(1)
}
if len(key) != cryptocore.KeyLen {
fmt.Printf("Master key has length %d but we require length %d\n", len(key), cryptocore.KeyLen)
toggledlog.Fatal.Printf("Master key has length %d but we require length %d\n", len(key), cryptocore.KeyLen)
os.Exit(1)
}
return key

View File

@ -7,16 +7,18 @@ import (
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
func readPasswordTwice(extpass string) string {
if extpass == "" {
fmt.Printf("Password: ")
fmt.Fprintf(os.Stderr, "Password: ")
p1 := readPassword("")
fmt.Printf("Repeat: ")
fmt.Fprintf(os.Stderr, "Repeat: ")
p2 := readPassword("")
if p1 != p2 {
fmt.Println(colorRed + "Passwords do not match" + colorReset)
toggledlog.Fatal.Println(colorRed + "Passwords do not match" + colorReset)
os.Exit(ERREXIT_PASSWORD)
}
return p1
@ -37,7 +39,7 @@ func readPassword(extpass string) string {
cmd.Stderr = os.Stderr
output, err = cmd.Output()
if err != nil {
fmt.Printf(colorRed+"extpass program returned error: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"extpass program returned error: %v\n"+colorReset, err)
os.Exit(ERREXIT_PASSWORD)
}
// Trim trailing newline like terminal.ReadPassword() does
@ -48,14 +50,14 @@ func readPassword(extpass string) string {
fd := int(os.Stdin.Fd())
output, err = terminal.ReadPassword(fd)
if err != nil {
fmt.Printf(colorRed+"Could not read password from terminal: %v\n"+colorReset, err)
toggledlog.Fatal.Printf(colorRed+"Could not read password from terminal: %v\n"+colorReset, err)
os.Exit(ERREXIT_PASSWORD)
}
fmt.Printf("\n")
fmt.Fprintf(os.Stderr, "\n")
}
password = string(output)
if password == "" {
fmt.Printf(colorRed + "Password is empty\n" + colorReset)
toggledlog.Fatal.Printf(colorRed + "Password is empty\n" + colorReset)
os.Exit(ERREXIT_PASSWORD)
}
return password

View File

@ -1,9 +1,10 @@
package main
import (
"fmt"
"os"
"syscall"
"github.com/rfjakob/gocryptfs/internal/toggledlog"
)
// Send signal USR1 to "pid" (usually our parent process). This notifies it
@ -11,11 +12,11 @@ import (
func sendUsr1(pid int) {
p, err := os.FindProcess(pid)
if err != nil {
fmt.Printf("sendUsr1: FindProcess: %v\n", err)
toggledlog.Warn.Printf("sendUsr1: FindProcess: %v\n", err)
return
}
err = p.Signal(syscall.SIGUSR1)
if err != nil {
fmt.Printf("sendUsr1: Signal: %v\n", err)
toggledlog.Warn.Printf("sendUsr1: Signal: %v\n", err)
}
}