From 09d42461a071d0cae9eeb417264b1260883f96de Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 12 Jun 2019 16:30:34 +0200 Subject: conf: move to C:\ProgramData It's not deleted after OS upgrades. --- conf/path_windows.go | 45 ++++++++++++++++++++++++++++++++++++++++----- installer/customactions.js | 4 +--- main.go | 2 +- ringlogger/dump_windows.go | 30 ++++++------------------------ 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/conf/path_windows.go b/conf/path_windows.go index 34e189b2..f0941644 100644 --- a/conf/path_windows.go +++ b/conf/path_windows.go @@ -12,11 +12,13 @@ import ( "unsafe" "golang.org/x/sys/windows" + "golang.zx2c4.com/wireguard/ipc/winpipe" ) //sys coTaskMemFree(pointer uintptr) = ole32.CoTaskMemFree //sys shGetKnownFolderPath(id *windows.GUID, flags uint32, token windows.Handle, path **uint16) (err error) [failretval!=0] = shell32.SHGetKnownFolderPath var folderIDLocalAppData = windows.GUID{0xf1b32785, 0x6fba, 0x4fcf, [8]byte{0x9d, 0x55, 0x7b, 0x8e, 0x7f, 0x15, 0x70, 0x91}} +var folderIDProgramData = windows.GUID{0x62ab5d82, 0xfdc1, 0x4dc3, [8]byte{0xa9, 0xdd, 0x07, 0x0d, 0x1d, 0x49, 0x5d, 0x97}} const kfFlagCreate = 0x00008000 @@ -40,17 +42,46 @@ func tunnelConfigurationsDirectory() (string, error) { return cachedConfigFileDir, nil } +func maybeMigrate(dst string) { + var path *uint16 + err := shGetKnownFolderPath(&folderIDLocalAppData, kfFlagCreate, 0, &path) + if err != nil { + return + } + root := windows.UTF16ToString((*[(1<<31)-1]uint16)(unsafe.Pointer(path))[:]) + coTaskMemFree(uintptr(unsafe.Pointer(path))) + if len(root) == 0 { + return + } + c := windows.StringToUTF16Ptr(filepath.Join(root, "WireGuard", "Configurations")) + attr, err := windows.GetFileAttributes(c) + if err != nil || attr & windows.FILE_ATTRIBUTE_DIRECTORY == 0 { + return + } + dst16 := windows.StringToUTF16Ptr(filepath.Join(dst, "Configurations")) + + err = windows.MoveFileEx(c, dst16, windows.MOVEFILE_COPY_ALLOWED | windows.MOVEFILE_REPLACE_EXISTING) + if err == nil { + os.RemoveAll(filepath.Join(root, "WireGuard")) + } +} + func RootDirectory() (string, error) { if cachedRootDir != "" { return cachedRootDir, nil } - processToken, err := windows.OpenCurrentProcessToken() + + sd, err := winpipe.SddlToSecurityDescriptor("O:SYD:(A;;GA;;;SY)") if err != nil { return "", err } - defer processToken.Close() + sa := &windows.SecurityAttributes{ + Length: uint32(len(sd)), + SecurityDescriptor: uintptr(unsafe.Pointer(&sd[0])), + } + var path *uint16 - err = shGetKnownFolderPath(&folderIDLocalAppData, kfFlagCreate, windows.Handle(processToken), &path) + err = shGetKnownFolderPath(&folderIDProgramData, kfFlagCreate, 0, &path) if err != nil { return "", err } @@ -60,10 +91,14 @@ func RootDirectory() (string, error) { return "", errors.New("Unable to determine configuration directory") } c := filepath.Join(root, "WireGuard") - err = os.MkdirAll(c, os.ModeDir|0700) - if err != nil { + + err = windows.CreateDirectory(windows.StringToUTF16Ptr(c), sa) + if err == nil { + maybeMigrate(c) + } else if err != windows.ERROR_ALREADY_EXISTS { return "", err } + cachedRootDir = c return cachedRootDir, nil } diff --git a/installer/customactions.js b/installer/customactions.js index a7ebc95d..30854ee7 100644 --- a/installer/customactions.js +++ b/installer/customactions.js @@ -93,9 +93,7 @@ function EvaluateWireGuardServices() { function RemoveConfigFolder() { try { - var path = wsh.RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\S-1-5-18\\ProfileImagePath"); - path = wsh.ExpandEnvironmentStrings(path.toString()); - fso.DeleteFolder(fso.BuildPath(path, "AppData\\Local\\WireGuard"), true); + fso.DeleteFolder(fso.BuildPath(Session.Property("CommonAppDataFolder"), "WireGuard"), true); } catch(e) { logMessage("Failed to remove configuration on uninstall: " + e.message); } diff --git a/main.go b/main.go index e4c569cb..b134f444 100644 --- a/main.go +++ b/main.go @@ -205,7 +205,7 @@ func main() { fatal(err) } defer file.Close() - err = ringlogger.DumpTo(file, true) + err = ringlogger.DumpTo(file) if err != nil { fatal(err) } diff --git a/ringlogger/dump_windows.go b/ringlogger/dump_windows.go index 552410e5..00810ab5 100644 --- a/ringlogger/dump_windows.go +++ b/ringlogger/dump_windows.go @@ -11,35 +11,17 @@ import ( "path/filepath" "golang.org/x/sys/windows" - "golang.org/x/sys/windows/registry" + "golang.zx2c4.com/wireguard/windows/conf" ) -func DumpTo(out io.Writer, localSystem bool) error { +func DumpTo(out io.Writer) error { var path string - if !localSystem { - root, err := conf.RootDirectory() - if err != nil { - return err - } - path = filepath.Join(root, "log.bin") - } else { - k, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\S-1-5-18", registry.QUERY_VALUE) - if err != nil { - return err - } - defer k.Close() - - systemprofile, _, err := k.GetStringValue("ProfileImagePath") - if err != nil { - return err - } - systemprofile, err = registry.ExpandString(systemprofile) - if err != nil { - return err - } - path = filepath.Join(systemprofile, "AppData", "Local", "WireGuard", "log.bin") + root, err := conf.RootDirectory() + if err != nil { + return err } + path = filepath.Join(root, "log.bin") file, err := os.Open(path) if err != nil { return err -- cgit v1.2.3-59-g8ed1b