Roger Upole wrote:
> raf wrote:
> > Roger Upole wrote:
> >>
> >> win32security.LookupAccountName will give you the complete sid for a 
> >> username.
> >
> > if you look at the code i supplied, you'll see that that is precisely
> > where i get the sid from:
> >
> >  sid = 
> > win32security.ConvertSidToStringSid(win32security.LookupAccountName(None, 
> > username)[0])
> >
> > so win32security.LookupAccountName is not giving me the complete sid for 
> > the username.
> > any idea why it isn't doing so? any idea what i'm doing wrong?
> 
> Sorry, from a first glance I thought you were deliberately retrieving the 
> machine's SID and then
> looking for the accounts under it.  It appears you have a machine named the 
> same as the user,
> and the call to LookupAccountName is finding the computer name first.

that's correct. so it was probably working in the past when the hostnames
didn't match the usernames but now that they do match, it no longer works.

silly me. given its name i thought it was looking up the name
of an account on the computer not the computer itself. :-)

> Try passing \\machine\username instead of just the bare user name.
> 
>        Roger

hi roger,

i tried that and got the following error:

  error: (1332, 'LookupAccountName',
    'No mapping between account names and security IDs was done.')

according to microsoft's documentation
(http://msdn.microsoft.com/en-us/library/windows/desktop/aa379159%28v=vs.85%29.aspx)
the username should be domainname\username not \\hostname\username but it
also says that a fqdn can be used instead of a windows domain name.

if i use the unqualified hostname (as returned by win32api.GetComputerName()),
it works. so the leading \\ needs to be absent.

and it even works when the windows computer name isn't the same as the dns 
hostname.

many thanks for your help. it's all good now. working code is below.

cheers,
raf

#!/usr/bin/env python
'''This module contains the user() and home() functions that
return the current user's name on Windows, and the given or
current user's home directory on Windows (even if it's been
changed in the registry).
20120911 raf <r...@raf.org>'''
import win32api, win32net, win32netcon, win32security, _winreg, pywintypes
def user(has_domain_controller=True):
    '''Return the current user's name on Windows. If has_domain_controller
    is True when you have no domain controller there may (or may not) be an
    annoying delay so pass False unless you really have a domain controller.'''
    dcname = None
    if has_domain_controller:
        try:
            dctype = win32netcon.SV_TYPE_DOMAIN_CTRL
            dcinfo = win32net.NetServerEnum(None, 100, dctype)
            dcname = r'\\' + dcinfo[0][0]['name']
        except (pywintypes.error, IndexError, KeyError):
            pass
    return win32net.NetUserGetInfo(dcname, win32api.GetUserName(), 1)['name']
def home(username=None, has_domain_controller=True):
    '''Return the given or current user's home directory on Windows
    even if it's been changed it in the registry. If has_domain_controller
    is True when you have no domain controller there may (or may not) be an
    annoying delay so pass False unless you really have a domain controller.'''
    if username is None:
        username = user(has_domain_controller)
    lookup_username = username
    hostname = win32api.GetComputerName()
    if '\\' not in lookup_username:
        lookup_username = hostname + '\\' + lookup_username
    sid = win32security.LookupAccountName(None, lookup_username)[0]
    sid = win32security.ConvertSidToStringSid(sid)
    key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\' + sid
    key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
    val, typ = _winreg.QueryValueEx(key, 'ProfileImagePath')
    if typ == _winreg.REG_EXPAND_SZ: # Which it is
        val = win32api.ExpandEnvironmentStrings(val) # Doesn't return unicode
    return val
if __name__ == '__main__':
    print('user = %r' % user())
    print('home = %r' % home())
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to