syscall: Use the service account to get user information #61972
+3
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When calling user.Current() to get Username normally, everything is fine, but if the caller is a service user, err will be returned and Username cannot be obtained
After checking the code, I found an error when calling GetUserProfileDirectory() in current() to obtain information. The service account does not have a profile directory, so there will be an error of The system cannot find the file specified., but it does not affect others The acquisition of information should not directly return err to cause all information to be invalid
If you'd like to reproduce what I said, please run user.Current() in is
func main() {
username, err := user.Current()
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(username.Username)
}
}
go build -o current.exe
Correct
C:>current.exe
TEST\Administrator
Incorrect
C:/inetpub/wwwroot/test/ >C:\current.exe
The system cannot find the file specified.
But in fact, we have already obtained other information at this time, such as the username
func lookupUsernameAndDomain(usid *syscall.SID) (username, domain string, e error) {
username, domain, t, e := usid.LookupAccount("")
if e != nil {
return "", "", e
}
if t != syscall.SidTypeUser {
return "", "", fmt.Errorf("user: should be user account type, not %d", t)
}
return username, domain, nil
}
func main() {
t, e := syscall.OpenCurrentProcessToken()
if e != nil {
return
}
defer t.Close()
u, e := t.GetTokenUser()
if e != nil {
return
}
}
go build -o current.exe
C:>current.exe
TEST\Administrator
C:/inetpub/wwwroot/test/ >C:\current.exe
IIS APPPOOL\test