Get file owner in Windows

Wolfgang Strobl ws at mystrobl.de
Sun Nov 11 08:14:57 EST 2001


On 10 Nov 2001 06:53:40 -0800, ny_r_marquez at yahoo.com (R.Marquez) wrote
:

>Someone named Itamar posted this same question to the Python-win32
>mailing list:
>http://mail.python.org/pipermail/python-win32/2001-May/000053.html
>Here is what it says:
>
>	lets say I want to get the security information on a
>	file. So I do:
>	
>	>>> o = win32security.GetFileSecurity(r"c:\mingc.pyd",
>	win32security.OWNER_SECURITY_INFORMATION)
>	>>> o
>	<PySECURITY_DESCRIPTOR object at 010E9350>
>	
>	
>	So what do I do with 'o' now in order to get the
>	owner? I tried:
>	
>	>>> s = win32security.SID(o)
>	>>> win32security.LookupAccountSid(None, s)
>	Traceback (innermost last):
>	  File "<interactive input>", line 1, in ?
>	api_error: (1332, 'LookupAccountSid', 'No mapping
>	between account names and security IDs was done.')
>	
>	What am I doing wrong?
>
>No one responded to his post.  I get the same results that he does.  I
>have also looked through Mark's "Pyhton Programming on Win32" (good
>book, by the way), but it does not seem to have this information
>either.  Does anyone have the knowhow to make Itamar's code work? 

Hmm. 

Well,  the following snippet printed  PySID:S-1-0x008014000000 for
every file I tried on my home machine, independent of the real ower.  
This looks like a bug in win32security.GetFileSecurity to me.

PythonWin 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on
win32.

-------

for f in glob.glob("d:/*.*"):
    try:
        o =
win32security.GetFileSecurity(f,win32security.OWNER_SECURITY_INFORMATION)
        s = win32security.SID(o)
        print str(s),
    except:
        print "n/a",
    print "   ",f

----------
Interestingly,

def prsid(name):
    import string
    print string.rjust(name,20),
    try:
        sid,box,what=win32security.LookupAccountName(None,name)
        print str(sid),box,what
    except:
        print "oops"
prsid("wolfgang")

works well, so it doesn't seem to be a problem with PySIDs.

For reference: there is an entry 
"Finding the Owner of a File Object" in the August 2001 MSDN library,
which contains sample code. The function
GetTextualSid code from Q131320 isn't necessary for displaying the sid
in textual form, anymore, at least on Win2000, there now is API
function ConvertSidToStringSid. Addings this to the aforementioned
sample codes resulted in little utility "lookupsid" which produces
the following output on my system:

H:\e\c\security\lookupsid\Debug>lookupsid myfile.txt
SID=S-1-5-21-436374069-854245398-842925246-1004
Account owner = nobody

Somebody should have a look into the source for
win32security.GetFileSecurity, I guess.

-- 
Wir danken für die Beachtung aller Sicherheitsbestimmungen



More information about the Python-list mailing list