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"
This commit is contained in:
Jakob Unterwurzacher 2017-01-26 21:32:08 +01:00
parent a7c7588deb
commit 39eca53677
3 changed files with 24 additions and 10 deletions

View File

@ -83,9 +83,11 @@ func CreateConfFile(filename string, password string, plaintextNames bool, logN
} }
// LoadConfFile - read config file from disk and decrypt the // 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 // 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) { func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) {
var cf ConfFile var cf ConfFile
cf.filename = filename cf.filename = filename

19
main.go
View File

@ -51,14 +51,16 @@ Options:
} }
// loadConfig loads the config file "args.config", prompting the user for the password // 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 // Check if the file can be opened at all before prompting for a password
fd, err := os.Open(args.config) fd, err := os.Open(args.config)
if err != nil { if err != nil {
tlog.Fatal.Printf("Cannot open config file: %v", err) tlog.Fatal.Printf("Cannot open config file: %v", err)
os.Exit(ErrExitLoadConf) return nil, nil, err
} }
fd.Close() fd.Close()
// The user has passed the master key (probably because he forgot the
// password).
if args.masterkey != "" { if args.masterkey != "" {
masterkey = parseMasterKey(args.masterkey) masterkey = parseMasterKey(args.masterkey)
_, confFile, err = configfile.LoadConfFile(args.config, "") _, confFile, err = configfile.LoadConfFile(args.config, "")
@ -69,20 +71,23 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf
} }
if err != nil { if err != nil {
tlog.Fatal.Println(err) 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" // changePassword - change the password of config file "filename"
func changePassword(args *argContainer) { 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.") tlog.Info.Println("Please enter your new password.")
newPw := readpassword.Twice(args.extpass) newPw := readpassword.Twice(args.extpass)
confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN()) confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN())
if args.masterkey != "" { if args.masterkey != "" {
bak := args.config + ".bak" bak := args.config + ".bak"
err := os.Link(args.config, bak) err = os.Link(args.config, bak)
if err != nil { if err != nil {
tlog.Fatal.Printf("Could not create backup file: %v", err) tlog.Fatal.Printf("Could not create backup file: %v", err)
os.Exit(ErrExitInit) 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."+ "Delete it after you have verified that you can access your files with the new password."+
tlog.ColorReset, bak) tlog.ColorReset, bak)
} }
err := confFile.WriteFile() err = confFile.WriteFile()
if err != nil { if err != nil {
tlog.Fatal.Println(err) tlog.Fatal.Println(err)
os.Exit(ErrExitInit) os.Exit(ErrExitInit)

View File

@ -88,7 +88,14 @@ func doMount(args *argContainer) int {
} else { } else {
// Load master key from config file // Load master key from config file
// Prompts the user for the password // 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) printMasterKey(masterkey)
} }
// We cannot use JSON for pretty-printing as the fields are unexported // We cannot use JSON for pretty-printing as the fields are unexported