aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'conf/store.go')
-rw-r--r--conf/store.go112
1 files changed, 15 insertions, 97 deletions
diff --git a/conf/store.go b/conf/store.go
index 21bd3a22..02807b77 100644
--- a/conf/store.go
+++ b/conf/store.go
@@ -1,13 +1,12 @@
/* SPDX-License-Identifier: MIT
*
- * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
*/
package conf
import (
"errors"
- "io/ioutil"
"os"
"path/filepath"
"strings"
@@ -15,109 +14,41 @@ import (
"golang.zx2c4.com/wireguard/windows/conf/dpapi"
)
-const configFileSuffix = ".conf.dpapi"
-const configFileUnencryptedSuffix = ".conf"
+const (
+ configFileSuffix = ".conf.dpapi"
+ configFileUnencryptedSuffix = ".conf"
+)
func ListConfigNames() ([]string, error) {
configFileDir, err := tunnelConfigurationsDirectory()
if err != nil {
return nil, err
}
- files, err := ioutil.ReadDir(configFileDir)
+ files, err := os.ReadDir(configFileDir)
if err != nil {
return nil, err
}
configs := make([]string, len(files))
i := 0
for _, file := range files {
- name := filepath.Base(file.Name())
- if len(name) <= len(configFileSuffix) || !strings.HasSuffix(name, configFileSuffix) {
- continue
- }
- if !file.Mode().IsRegular() || file.Mode().Perm()&0444 == 0 {
- continue
- }
- name = strings.TrimSuffix(name, configFileSuffix)
- if !TunnelNameIsValid(name) {
- continue
- }
- configs[i] = name
- i++
- }
- return configs[:i], nil
-}
-
-func MigrateUnencryptedConfigs() (int, []error) {
- configFileDir, err := tunnelConfigurationsDirectory()
- if err != nil {
- return 0, []error{err}
- }
- files, err := ioutil.ReadDir(configFileDir)
- if err != nil {
- return 0, []error{err}
- }
- errs := make([]error, len(files))
- i := 0
- e := 0
- for _, file := range files {
- path := filepath.Join(configFileDir, file.Name())
- name := filepath.Base(file.Name())
- if len(name) <= len(configFileUnencryptedSuffix) || !strings.HasSuffix(name, configFileUnencryptedSuffix) {
- continue
- }
- if !file.Mode().IsRegular() || file.Mode().Perm()&0444 == 0 {
- continue
- }
-
- // We don't use ioutil's ReadFile, because we actually want RDWR, so that we can take advantage
- // of Windows file locking for ensuring the file is finished being written.
- f, err := os.OpenFile(path, os.O_RDWR, 0)
- if err != nil {
- errs[e] = err
- e++
- continue
- }
- bytes, err := ioutil.ReadAll(f)
- f.Close()
+ name, err := NameFromPath(file.Name())
if err != nil {
- errs[e] = err
- e++
continue
}
- _, err = FromWgQuickWithUnknownEncoding(string(bytes), "input")
- if err != nil {
- errs[e] = err
- e++
+ if !file.Type().IsRegular() {
continue
}
-
- bytes, err = dpapi.Encrypt(bytes, strings.TrimSuffix(name, configFileUnencryptedSuffix))
+ info, err := file.Info()
if err != nil {
- errs[e] = err
- e++
- continue
- }
- dstFile := strings.TrimSuffix(path, configFileUnencryptedSuffix) + configFileSuffix
- if _, err = os.Stat(dstFile); err != nil && !os.IsNotExist(err) {
- errs[e] = errors.New("Unable to migrate to " + dstFile + " as it already exists")
- e++
continue
}
- err = ioutil.WriteFile(dstFile, bytes, 0600)
- if err != nil {
- errs[e] = err
- e++
- continue
- }
- err = os.Remove(path)
- if err != nil && os.Remove(dstFile) == nil {
- errs[e] = err
- e++
+ if info.Mode().Perm()&0o444 == 0 {
continue
}
+ configs[i] = name
i++
}
- return i, errs[:e]
+ return configs[:i], nil
}
func LoadFromName(name string) (*Config, error) {
@@ -129,15 +60,11 @@ func LoadFromName(name string) (*Config, error) {
}
func LoadFromPath(path string) (*Config, error) {
- if !disableAutoMigration {
- tunnelConfigurationsDirectory() // Provoke migrations, if needed.
- }
-
name, err := NameFromPath(path)
if err != nil {
return nil, err
}
- bytes, err := ioutil.ReadFile(path)
+ bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
@@ -171,7 +98,7 @@ func NameFromPath(path string) (string, error) {
return name, nil
}
-func (config *Config) Save() error {
+func (config *Config) Save(overwrite bool) error {
if !TunnelNameIsValid(config.Name) {
return errors.New("Tunnel name is not valid")
}
@@ -185,16 +112,7 @@ func (config *Config) Save() error {
if err != nil {
return err
}
- err = ioutil.WriteFile(filename+".tmp", bytes, 0600)
- if err != nil {
- return err
- }
- err = os.Rename(filename+".tmp", filename)
- if err != nil {
- os.Remove(filename + ".tmp")
- return err
- }
- return nil
+ return writeLockedDownFile(filename, overwrite, bytes)
}
func (config *Config) Path() (string, error) {