Skip to content

Commit

Permalink
windows: add Windows directory accessors
Browse files Browse the repository at this point in the history
These are useful for the same reason that the already existing
GetSystemDirectory is.

Change-Id: I3041ce6cbeb66a4f8a5960fbaf39381c8c9c80d6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/191837
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
zx2c4 committed Aug 26, 2019
1 parent acd9dae commit c7b8b68
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
42 changes: 40 additions & 2 deletions windows/security_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ func (tml *Tokenmandatorylabel) Size() uint32 {
//sys DuplicateTokenEx(existingToken Token, desiredAccess uint32, tokenAttributes *SecurityAttributes, impersonationLevel uint32, tokenType uint32, newToken *Token) (err error) = advapi32.DuplicateTokenEx
//sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
//sys getSystemDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemDirectoryW
//sys getWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetWindowsDirectoryW
//sys getSystemWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemWindowsDirectoryW

// An access token contains the security information for a logon session.
// The system creates an access token when a user logs on, and every
Expand Down Expand Up @@ -785,8 +787,8 @@ func (token Token) GetLinkedToken() (Token, error) {
return linkedToken, nil
}

// GetSystemDirectory retrieves path to current location of the system
// directory, which is typically, though not always, C:\Windows\System32.
// GetSystemDirectory retrieves the path to current location of the system
// directory, which is typically, though not always, `C:\Windows\System32`.
func GetSystemDirectory() (string, error) {
n := uint32(MAX_PATH)
for {
Expand All @@ -802,6 +804,42 @@ func GetSystemDirectory() (string, error) {
}
}

// GetWindowsDirectory retrieves the path to current location of the Windows
// directory, which is typically, though not always, `C:\Windows`. This may
// be a private user directory in the case that the application is running
// under a terminal server.
func GetWindowsDirectory() (string, error) {
n := uint32(MAX_PATH)
for {
b := make([]uint16, n)
l, e := getWindowsDirectory(&b[0], n)
if e != nil {
return "", e
}
if l <= n {
return UTF16ToString(b[:l]), nil
}
n = l
}
}

// GetSystemWindowsDirectory retrieves the path to current location of the
// Windows directory, which is typically, though not always, `C:\Windows`.
func GetSystemWindowsDirectory() (string, error) {
n := uint32(MAX_PATH)
for {
b := make([]uint16, n)
l, e := getSystemWindowsDirectory(&b[0], n)
if e != nil {
return "", e
}
if l <= n {
return UTF16ToString(b[:l]), nil
}
n = l
}
}

// IsMember reports whether the access token t is a member of the provided SID.
func (t Token) IsMember(sid *SID) (bool, error) {
var b int32
Expand Down
17 changes: 17 additions & 0 deletions windows/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ func TestGetSystemDirectory(t *testing.T) {
t.Fatalf("System directory does not end in system32: %s", d)
}
}

func TestGetWindowsDirectory(t *testing.T) {
d1, err := windows.GetWindowsDirectory()
if err != nil {
t.Fatalf("Failed to get Windows directory: %s", err)
}
d2, err := windows.GetSystemWindowsDirectory()
if err != nil {
t.Fatalf("Failed to get system Windows directory: %s", err)
}
if !strings.HasSuffix(strings.ToLower(d1), `\windows`) {
t.Fatalf("Windows directory does not end in windows: %s", d1)
}
if !strings.HasSuffix(strings.ToLower(d2), `\windows`) {
t.Fatalf("System Windows directory does not end in windows: %s", d2)
}
}
28 changes: 28 additions & 0 deletions windows/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7b8b68

Please sign in to comment.