From 39eca53677abd0c397a884dd29ebc98ddfcbb270 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 26 Jan 2017 21:32:08 +0100 Subject: [PATCH] main: make sure the ctlsock file is deleted on incorrect password Otherwise the next try to mount ends in "ctlsock: listen unix ctl.sock: bind: address already in use" --- internal/configfile/config_file.go | 6 ++++-- main.go | 19 ++++++++++++------- mount.go | 9 ++++++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/internal/configfile/config_file.go b/internal/configfile/config_file.go index 0bee16f..c4865d7 100644 --- a/internal/configfile/config_file.go +++ b/internal/configfile/config_file.go @@ -83,9 +83,11 @@ func CreateConfFile(filename string, password string, plaintextNames bool, logN } // LoadConfFile - read config file from disk and decrypt the -// contained key using password. -// +// contained key using "password". // Returns the decrypted key and the ConfFile object +// +// If "password" is empty, the config file is read +// but the key is not decrypted (returns nil in its place). func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) { var cf ConfFile cf.filename = filename diff --git a/main.go b/main.go index 8ab180e..4230d94 100644 --- a/main.go +++ b/main.go @@ -51,14 +51,16 @@ Options: } // loadConfig loads the config file "args.config", prompting the user for the password -func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile) { +func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile, err error) { // Check if the file can be opened at all before prompting for a password fd, err := os.Open(args.config) if err != nil { tlog.Fatal.Printf("Cannot open config file: %v", err) - os.Exit(ErrExitLoadConf) + return nil, nil, err } fd.Close() + // The user has passed the master key (probably because he forgot the + // password). if args.masterkey != "" { masterkey = parseMasterKey(args.masterkey) _, confFile, err = configfile.LoadConfFile(args.config, "") @@ -69,20 +71,23 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf } if err != nil { tlog.Fatal.Println(err) - os.Exit(ErrExitLoadConf) + return nil, nil, err } - return masterkey, confFile + return masterkey, confFile, nil } // changePassword - change the password of config file "filename" func changePassword(args *argContainer) { - masterkey, confFile := loadConfig(args) + masterkey, confFile, err := loadConfig(args) + if err != nil { + os.Exit(ErrExitLoadConf) + } tlog.Info.Println("Please enter your new password.") newPw := readpassword.Twice(args.extpass) confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN()) if args.masterkey != "" { bak := args.config + ".bak" - err := os.Link(args.config, bak) + err = os.Link(args.config, bak) if err != nil { tlog.Fatal.Printf("Could not create backup file: %v", err) os.Exit(ErrExitInit) @@ -92,7 +97,7 @@ func changePassword(args *argContainer) { "Delete it after you have verified that you can access your files with the new password."+ tlog.ColorReset, bak) } - err := confFile.WriteFile() + err = confFile.WriteFile() if err != nil { tlog.Fatal.Println(err) os.Exit(ErrExitInit) diff --git a/mount.go b/mount.go index 3e93169..0d70201 100644 --- a/mount.go +++ b/mount.go @@ -88,7 +88,14 @@ func doMount(args *argContainer) int { } else { // Load master key from config file // Prompts the user for the password - masterkey, confFile = loadConfig(args) + masterkey, confFile, err = loadConfig(args) + if err != nil { + if args._ctlsockFd != nil { + // Close the socket file (which also deletes it) + args._ctlsockFd.Close() + } + os.Exit(ErrExitLoadConf) + } printMasterKey(masterkey) } // We cannot use JSON for pretty-printing as the fields are unexported