aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tunnel/ipcpermissions.go
blob: 48f21f1f7cc4f33a8549b6d3043767524cc818a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package tunnel

import (
	"fmt"
	"unsafe"

	"golang.org/x/sys/windows"
	"golang.zx2c4.com/wireguard/ipc"

	"golang.zx2c4.com/wireguard/windows/conf"
)

func CopyConfigOwnerToIPCSecurityDescriptor(filename string) error {
	if conf.PathIsEncrypted(filename) {
		return nil
	}
	handle, err := windows.CreateFile(windows.StringToUTF16Ptr(filename), windows.STANDARD_RIGHTS_READ, windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
	if err != nil {
		return err
	}
	defer windows.CloseHandle(handle)
	var sid *windows.SID
	var sd windows.Handle
	//TODO: Move into x/sys/windows
	const SE_FILE_OBJECT = 1
	const OWNER_SECURITY_INFORMATION = 1
	r, _, _ := windows.NewLazySystemDLL("advapi32.dll").NewProc("GetSecurityInfo").Call(
		uintptr(handle),
		SE_FILE_OBJECT,
		OWNER_SECURITY_INFORMATION,
		uintptr(unsafe.Pointer(&sid)),
		0,
		0,
		0,
		uintptr(unsafe.Pointer(&sd)),
	)
	if r != uintptr(windows.ERROR_SUCCESS) {
		return windows.Errno(r)
	}
	defer windows.LocalFree(sd)
	if sid.IsWellKnown(windows.WinLocalSystemSid) {
		return nil
	}
	sidString, err := sid.String()
	if err != nil {
		return err
	}
	ipc.UAPISecurityDescriptor += fmt.Sprintf("(A;;GA;;;%s)", sidString)
	return nil
}