win32security.LogonUser not working

Mark Hammond mhammond at skippinet.com.au
Tue Sep 17 07:14:57 EDT 2002


{posted and mailed]

dsavitsk wrote:
> I have gone so far as to assign every possible right in dir(ntsecuritycon),
> but i am still getting the same error. I have also assigned nearly everyone
> and every thing on my network the right to act as the os. any ideas?
> 
> -d
> 
> 
> "dsavitsk" <dsavitsk at e-coli.net> wrote in message
> news:L9Pg9.8440$yt3.4474038 at newssrv26.news.prodigy.com...
> 
>>should have mentioned
>>python:   2.1.3
>>win32all: 145
>>win2k:    sp2
>>
>>
>>"dsavitsk" <dsavitsk at e-coli.net> wrote in message
>>news:x6Pg9.8439$yt3.4473830 at newssrv26.news.prodigy.com...
>>
>>>I am trying to run code as another user. I copied the code below from
>>
>>ASPN,
>>
>>>so it would seem that someone has gotten this to work. I keep getting
>>
> the
> 
>>>error
>>>
>>>pywintypes.api_error (1314, 'LogonUser', 'A required privilege is not
>>
> held

This is a *privilege* issue, not a *permissions* issue.

This same issue happens when trying to reboot a server - from the 
RebootServer.py script I posted recently:

import win32security
import win32api
import sys
import time
from ntsecuritycon import *

def AdjustPrivilege(priv, enable = 1):
     # Get the process token.
     flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
     htoken = 
win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
     # Get the ID for the system shutdown privilege.
     id = win32security.LookupPrivilegeValue(None, priv)
     # Now obtain the privilege for this process.
     # Create a list of the privileges to be added.
     if enable:
         newPrivileges = [(id, SE_PRIVILEGE_ENABLED)]
     else:
         newPrivileges = [(id, 0)]
     # and make the adjustment.
     win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)

Look for C/C++ samples using the LogonUser function in the way you are 
trying to, and I am confident you will find similar calls to the above 
in that sample code.

Let us know if you sort it out - or better still, send me a sample .py 
file that uses it which I can include in win32all :)

Mark.




More information about the Python-list mailing list