FAQ
Reviewers: golang-dev1,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go/


Description:
os/user: faster user lookup on Windows

Trying to lookup user's display name with directory services can
take a several seconds when user's computer is not in a domain.
As a workaround, check if domain name matches computer name
and don't use directory services for local user names.
Fixes issue 5298.

Please review this at https://codereview.appspot.com/8541047/

Affected files:
M src/pkg/os/user/lookup_windows.go


Index: src/pkg/os/user/lookup_windows.go
===================================================================
--- a/src/pkg/os/user/lookup_windows.go
+++ b/src/pkg/os/user/lookup_windows.go
@@ -6,38 +6,43 @@

import (
"fmt"
+ "strings"
"syscall"
"unsafe"
)

func lookupFullName(domain, username, domainAndUser string) (string,
error) {
- // try domain controller first
- name, e := syscall.TranslateAccountName(domainAndUser,
- syscall.NameSamCompatible, syscall.NameDisplay, 50)
+ computer, e := syscall.ComputerName()
+ if e != nil || !strings.EqualFold(domain, computer) {
+ // try domain controller first
+ name, e := syscall.TranslateAccountName(domainAndUser,
+ syscall.NameSamCompatible, syscall.NameDisplay, 50)
+ if e == nil {
+ return name, nil
+ }
+ }
+ // domain lookup failed or it's a local computer
+ d, e := syscall.UTF16PtrFromString(domain)
if e != nil {
- // domain lookup failed, perhaps this pc is not part of domain
- d, e := syscall.UTF16PtrFromString(domain)
- if e != nil {
- return "", e
- }
- u, e := syscall.UTF16PtrFromString(username)
- if e != nil {
- return "", e
- }
- var p *byte
- e = syscall.NetUserGetInfo(d, u, 10, &p)
- if e != nil {
- // path executed when a domain user is disconnected from the domain
- // pretend username is fullname
- return username, nil
- }
- defer syscall.NetApiBufferFree(p)
- i := (*syscall.UserInfo10)(unsafe.Pointer(p))
- if i.FullName == nil {
- return "", nil
- }
- name =
syscall.UTF16ToString((*[1024]uint16)(unsafe.Pointer(i.FullName))[:])
+ return "", e
}
+ u, e := syscall.UTF16PtrFromString(username)
+ if e != nil {
+ return "", e
+ }
+ var p *byte
+ e = syscall.NetUserGetInfo(d, u, 10, &p)
+ if e != nil {
+ // path executed when a domain user is disconnected from the domain
+ // pretend username is fullname
+ return username, nil
+ }
+ defer syscall.NetApiBufferFree(p)
+ i := (*syscall.UserInfo10)(unsafe.Pointer(p))
+ if i.FullName == nil {
+ return "", nil
+ }
+ name :=
syscall.UTF16ToString((*[1024]uint16)(unsafe.Pointer(i.FullName))[:])
return name, nil
}



--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Brad Fitzpatrick at Apr 17, 2013 at 4:30 pm
    The issue discussed a Windows API to ask whether your computer was part of
    a domain. Why aren't you using that?

    Is checking whether the computer's name == domain name a good heuristic for
    domain membership? Seems non-ideal.


    On Wed, Apr 17, 2013 at 9:29 AM, wrote:

    Reviewers: golang-dev1,

    Message:
    Hello golang-dev@googlegroups.com,

    I'd like you to review this change to
    https://code.google.com/p/go/


    Description:
    os/user: faster user lookup on Windows

    Trying to lookup user's display name with directory services can
    take a several seconds when user's computer is not in a domain.
    As a workaround, check if domain name matches computer name
    and don't use directory services for local user names.
    Fixes issue 5298.

    Please review this at https://codereview.appspot.**com/8541047/<https://codereview.appspot.com/8541047/>

    Affected files:
    M src/pkg/os/user/lookup_**windows.go


    Index: src/pkg/os/user/lookup_**windows.go
    ==============================**==============================**=======
    --- a/src/pkg/os/user/lookup_**windows.go
    +++ b/src/pkg/os/user/lookup_**windows.go
    @@ -6,38 +6,43 @@

    import (
    "fmt"
    + "strings"
    "syscall"
    "unsafe"
    )

    func lookupFullName(domain, username, domainAndUser string) (string,
    error) {
    - // try domain controller first
    - name, e := syscall.TranslateAccountName(**domainAndUser,
    - syscall.NameSamCompatible, syscall.NameDisplay, 50)
    + computer, e := syscall.ComputerName()
    + if e != nil || !strings.EqualFold(domain, computer) {
    + // try domain controller first
    + name, e := syscall.TranslateAccountName(**domainAndUser,
    + syscall.NameSamCompatible, syscall.NameDisplay, 50)
    + if e == nil {
    + return name, nil
    + }
    + }
    + // domain lookup failed or it's a local computer
    + d, e := syscall.UTF16PtrFromString(**domain)
    if e != nil {
    - // domain lookup failed, perhaps this pc is not part of
    domain
    - d, e := syscall.UTF16PtrFromString(**domain)
    - if e != nil {
    - return "", e
    - }
    - u, e := syscall.UTF16PtrFromString(**username)
    - if e != nil {
    - return "", e
    - }
    - var p *byte
    - e = syscall.NetUserGetInfo(d, u, 10, &p)
    - if e != nil {
    - // path executed when a domain user is
    disconnected from the domain
    - // pretend username is fullname
    - return username, nil
    - }
    - defer syscall.NetApiBufferFree(p)
    - i := (*syscall.UserInfo10)(unsafe.**Pointer(p))
    - if i.FullName == nil {
    - return "", nil
    - }
    - name = syscall.UTF16ToString((*[1024]**
    uint16)(unsafe.Pointer(i.**FullName))[:])
    + return "", e
    }
    + u, e := syscall.UTF16PtrFromString(**username)
    + if e != nil {
    + return "", e
    + }
    + var p *byte
    + e = syscall.NetUserGetInfo(d, u, 10, &p)
    + if e != nil {
    + // path executed when a domain user is disconnected from
    the domain
    + // pretend username is fullname
    + return username, nil
    + }
    + defer syscall.NetApiBufferFree(p)
    + i := (*syscall.UserInfo10)(unsafe.**Pointer(p))
    + if i.FullName == nil {
    + return "", nil
    + }
    + name := syscall.UTF16ToString((*[1024]**uint16)(unsafe.Pointer(i.*
    *FullName))[:])
    return name, nil
    }



    --

    ---You received this message because you are subscribed to the Google
    Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-dev+unsubscribe@**googlegroups.com<golang-dev%2Bunsubscribe@googlegroups.com>
    .
    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Snaury at Apr 17, 2013 at 4:50 pm

    On 2013/04/17 16:30:38, bradfitz wrote:
    The issue discussed a Windows API to ask whether your computer was part of
    a domain. Why aren't you using that?
    Several reasons. First, I would need to add a new API (GetNetworkParams)
    to syscall, rather complex structure and code to marshal that, as well
    as probably sync.Once to call that only once. Next, if machine is part
    of a domain doesn't really tell us anything: logged in user might be a
    local user on a home network, and the problem would be the same, trying
    to query domain controller when there's none.

    It's very difficult to have a computer name same as a domain name,
    unless you are on a domain controller (I can't test what really happens
    then), and if you are looking up an account on a local computer, then
    you have to use NetUserGetInfo anyway.

    The ideal solution would probably be to compare sid prefixes between
    user and computer, but I'm not sure which API can give us sid of a local
    computer.
    Is checking whether the computer's name == domain name a good
    heuristic for
    domain membership? Seems non-ideal.
    Since user name is only remote if it's not on a current computer it's
    probably good, though I'm not entirely sure. :(

    https://codereview.appspot.com/8541047/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brad Fitzpatrick at Apr 17, 2013 at 4:53 pm

    On Wed, Apr 17, 2013 at 9:50 AM, wrote:
    On 2013/04/17 16:30:38, bradfitz wrote:

    The issue discussed a Windows API to ask whether your computer was part of
    a domain. Why aren't you using that?
    Several reasons. First, I would need to add a new API (GetNetworkParams)
    to syscall, rather complex structure and code to marshal that, as well
    as probably sync.Once to call that only once.

    Those reasons alone aren't compelling enough. We can help you with those.

    Let's figure out what's the correct behavior first, and then worry about
    implementation.

    Next, if machine is part
    of a domain doesn't really tell us anything: logged in user might be a
    local user on a home network, and the problem would be the same, trying
    to query domain controller when there's none.

    It's very difficult to have a computer name same as a domain name,
    unless you are on a domain controller (I can't test what really happens
    then), and if you are looking up an account on a local computer, then
    you have to use NetUserGetInfo anyway.

    The ideal solution would probably be to compare sid prefixes between
    user and computer, but I'm not sure which API can give us sid of a local
    computer.


    Is checking whether the computer's name == domain name a good

    heuristic for
    domain membership? Seems non-ideal.
    Since user name is only remote if it's not on a current computer it's
    probably good, though I'm not entirely sure. :(

    https://codereview.appspot.**com/8541047/<https://codereview.appspot.com/8541047/>
    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Snaury at Apr 17, 2013 at 5:07 pm

    On 2013/04/17 16:53:46, bradfitz wrote:
    Let's figure out what's the correct behavior first, and then worry about
    implementation.
    The correct behavior is rather obvious. If username is on current or
    another computer, we should use NetUserGetInfo. If username is on a
    domain, we should use TranslateAccountName. The problem is how to
    distinguish between computer and domain names. :)

    https://codereview.appspot.com/8541047/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedApr 17, '13 at 4:29p
activeApr 17, '13 at 5:07p
posts5
users2
websitegolang.org

2 users in discussion

Snaury: 3 posts Brad Fitzpatrick: 2 posts

People

Translate

site design / logo © 2022 Grokbase