-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
glog: avoid calling user.Current() on windows (#69)
Use the current process token to look up the user's name on Windows. This is more reliable than using the USER or USERNAME environment variables, which are not always set, or might be overridden by the user accidentally or maliciously. It follows the implementation of the user.Current() implementation in the standard library. cl/650142356 (google-internal)
- Loading branch information
Showing
3 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !windows | ||
|
||
package glog | ||
|
||
import "os/user" | ||
|
||
func lookupUser() string { | ||
if current, err := user.Current(); err == nil { | ||
return current.Username | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build windows | ||
|
||
package glog | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// This follows the logic in the standard library's user.Current() function, except | ||
// that it leaves out the potentially expensive calls required to look up the user's | ||
// display name in Active Directory. | ||
func lookupUser() string { | ||
token, err := syscall.OpenCurrentProcessToken() | ||
if err != nil { | ||
return "" | ||
} | ||
defer token.Close() | ||
tokenUser, err := token.GetTokenUser() | ||
if err != nil { | ||
return "" | ||
} | ||
username, _, accountType, err := tokenUser.User.Sid.LookupAccount("") | ||
if err != nil { | ||
return "" | ||
} | ||
if accountType != syscall.SidTypeUser { | ||
return "" | ||
} | ||
return username | ||
} |