<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import logging <EOL> try : <EOL> import win32con <EOL> import win32api <EOL> import win32process <EOL> import win32security <EOL> import win32pipe <EOL> import win32event <EOL> import win32profile <EOL> import msvcrt <EOL> import ctypes <EOL> from ctypes import wintypes <EOL> HAS_WIN32 = True <EOL> except ImportError : <EOL> HAS_WIN32 = False <EOL> import salt . utils <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_windows ( ) and HAS_WIN32 : <EOL> return '<STR_LIT>' <EOL> return False <EOL> kernel32 = ctypes . WinDLL ( '<STR_LIT>' ) <EOL> advapi32 = ctypes . WinDLL ( '<STR_LIT>' ) <EOL> INVALID_HANDLE_VALUE = wintypes . HANDLE ( - <NUM_LIT:1> ) . value <EOL> INVALID_DWORD_VALUE = wintypes . DWORD ( - <NUM_LIT:1> ) . value <EOL> INFINITE = INVALID_DWORD_VALUE <EOL> LOGON_WITH_PROFILE = <NUM_LIT> <EOL> STD_INPUT_HANDLE = wintypes . DWORD ( - <NUM_LIT:10> ) . value <EOL> STD_OUTPUT_HANDLE = wintypes . DWORD ( - <NUM_LIT:11> ) . value <EOL> STD_ERROR_HANDLE = wintypes . DWORD ( - <NUM_LIT:12> ) . value <EOL> class SECURITY_ATTRIBUTES ( ctypes . Structure ) : <EOL> _fields_ = ( ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . LPVOID ) , <EOL> ( '<STR_LIT>' , wintypes . BOOL ) ) <EOL> def __init__ ( self , ** kwds ) : <EOL> self . nLength = ctypes . sizeof ( self ) <EOL> super ( SECURITY_ATTRIBUTES , self ) . __init__ ( ** kwds ) <EOL> LPSECURITY_ATTRIBUTES = ctypes . POINTER ( SECURITY_ATTRIBUTES ) <EOL> LPBYTE = ctypes . POINTER ( wintypes . BYTE ) <EOL> LPHANDLE = PHANDLE = ctypes . POINTER ( ctypes . c_void_p ) <EOL> LPDWORD = ctypes . POINTER ( ctypes . c_ulong ) <EOL> class STARTUPINFO ( ctypes . Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = ( ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . LPWSTR ) , <EOL> ( '<STR_LIT>' , wintypes . LPWSTR ) , <EOL> ( '<STR_LIT>' , wintypes . LPWSTR ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . WORD ) , <EOL> ( '<STR_LIT>' , wintypes . WORD ) , <EOL> ( '<STR_LIT>' , LPBYTE ) , <EOL> ( '<STR_LIT>' , wintypes . HANDLE ) , <EOL> ( '<STR_LIT>' , wintypes . HANDLE ) , <EOL> ( '<STR_LIT>' , wintypes . HANDLE ) ) <EOL> def __init__ ( self , ** kwds ) : <EOL> self . cb = ctypes . sizeof ( self ) <EOL> super ( STARTUPINFO , self ) . __init__ ( ** kwds ) <EOL> LPSTARTUPINFO = ctypes . POINTER ( STARTUPINFO ) <EOL> class PROC_THREAD_ATTRIBUTE_LIST ( ctypes . Structure ) : <EOL> pass <EOL> PPROC_THREAD_ATTRIBUTE_LIST = ctypes . POINTER ( PROC_THREAD_ATTRIBUTE_LIST ) <EOL> class STARTUPINFOEX ( STARTUPINFO ) : <EOL> _fields_ = ( ( '<STR_LIT>' , PPROC_THREAD_ATTRIBUTE_LIST ) , ) <EOL> LPSTARTUPINFOEX = ctypes . POINTER ( STARTUPINFOEX ) <EOL> class PROCESS_INFORMATION ( ctypes . Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = ( ( '<STR_LIT>' , wintypes . HANDLE ) , <EOL> ( '<STR_LIT>' , wintypes . HANDLE ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) , <EOL> ( '<STR_LIT>' , wintypes . DWORD ) ) <EOL> LPPROCESS_INFORMATION = ctypes . POINTER ( PROCESS_INFORMATION ) <EOL> class HANDLE_IHV ( wintypes . HANDLE ) : <EOL> pass <EOL> def errcheck_ihv ( result , func , args ) : <EOL> if result . value == INVALID_HANDLE_VALUE : <EOL> raise ctypes . WinError ( ) <EOL> return result . value <EOL> class DWORD_IDV ( wintypes . DWORD ) : <EOL> pass <EOL> def errcheck_idv ( result , func , args ) : <EOL> if result . value == INVALID_DWORD_VALUE : <EOL> raise ctypes . WinError ( ) <EOL> return result . value <EOL> def errcheck_bool ( result , func , args ) : <EOL> if not result : <EOL> raise ctypes . WinError ( ) <EOL> return args <EOL> def _win ( func , restype , * argtypes ) : <EOL> func . restype = restype <EOL> func . argtypes = argtypes <EOL> if issubclass ( restype , HANDLE_IHV ) : <EOL> func . errcheck = errcheck_ihv <EOL> elif issubclass ( restype , DWORD_IDV ) : <EOL> func . errcheck = errcheck_idv <EOL> else : <EOL> func . errcheck = errcheck_bool <EOL> _win ( kernel32 . WaitForSingleObject , DWORD_IDV , <EOL> wintypes . HANDLE , <EOL> wintypes . DWORD ) <EOL> _win ( kernel32 . GetStdHandle , HANDLE_IHV , <EOL> wintypes . DWORD ) <EOL> _win ( kernel32 . CloseHandle , wintypes . BOOL , <EOL> wintypes . HANDLE ) <EOL> _win ( kernel32 . SetHandleInformation , wintypes . BOOL , <EOL> wintypes . HANDLE , <EOL> wintypes . DWORD , <EOL> wintypes . DWORD ) <EOL> _win ( kernel32 . DuplicateHandle , wintypes . BOOL , <EOL> wintypes . HANDLE , <EOL> wintypes . HANDLE , <EOL> wintypes . HANDLE , <EOL> LPHANDLE , <EOL> wintypes . DWORD , <EOL> wintypes . BOOL , <EOL> wintypes . DWORD ) <EOL> _win ( kernel32 . GetCurrentProcess , wintypes . HANDLE ) <EOL> _win ( kernel32 . GetExitCodeProcess , wintypes . BOOL , <EOL> wintypes . HANDLE , <EOL> LPDWORD ) <EOL> _win ( kernel32 . CreatePipe , wintypes . BOOL , <EOL> PHANDLE , <EOL> PHANDLE , <EOL> LPSECURITY_ATTRIBUTES , <EOL> wintypes . DWORD ) <EOL> _win ( advapi32 . CreateProcessWithLogonW , wintypes . BOOL , <EOL> wintypes . LPCWSTR , <EOL> wintypes . LPCWSTR , <EOL> wintypes . LPCWSTR , <EOL> wintypes . DWORD , <EOL> wintypes . LPCWSTR , <EOL> wintypes . LPWSTR , <EOL> wintypes . DWORD , <EOL> wintypes . LPCWSTR , <EOL> wintypes . LPCWSTR , <EOL> LPSTARTUPINFO , <EOL> LPPROCESS_INFORMATION ) <EOL> def DuplicateHandle ( hsrc = kernel32 . GetCurrentProcess ( ) , <EOL> srchandle = kernel32 . GetCurrentProcess ( ) , <EOL> htgt = kernel32 . GetCurrentProcess ( ) , <EOL> access = <NUM_LIT:0> , inherit = False , <EOL> options = win32con . DUPLICATE_SAME_ACCESS ) : <EOL> tgthandle = wintypes . HANDLE ( ) <EOL> kernel32 . DuplicateHandle ( hsrc , srchandle , <EOL> htgt , ctypes . byref ( tgthandle ) , <EOL> access , inherit , options ) <EOL> return tgthandle . value <EOL> def CreatePipe ( inherit_read = False , inherit_write = False ) : <EOL> read , write = wintypes . HANDLE ( ) , wintypes . HANDLE ( ) <EOL> kernel32 . CreatePipe ( ctypes . byref ( read ) , ctypes . byref ( write ) , None , <NUM_LIT:0> ) <EOL> if inherit_read : <EOL> kernel32 . SetHandleInformation ( read , win32con . HANDLE_FLAG_INHERIT , <EOL> win32con . HANDLE_FLAG_INHERIT ) <EOL> if inherit_write : <EOL> kernel32 . SetHandleInformation ( write , win32con . HANDLE_FLAG_INHERIT , <EOL> win32con . HANDLE_FLAG_INHERIT ) <EOL> return read . value , write . value <EOL> def CreateProcessWithLogonW ( username = None , <EOL> domain = None , <EOL> password = None , <EOL> logonflags = <NUM_LIT:0> , <EOL> applicationname = None , <EOL> commandline = None , <EOL> creationflags = <NUM_LIT:0> , <EOL> environment = None , <EOL> currentdirectory = None , <EOL> startupinfo = None ) : <EOL> creationflags |= win32con . CREATE_UNICODE_ENVIRONMENT <EOL> if commandline is not None : <EOL> commandline = ctypes . create_unicode_buffer ( commandline ) <EOL> if startupinfo is None : <EOL> startupinfo = STARTUPINFO ( ) <EOL> process_info = PROCESS_INFORMATION ( ) <EOL> advapi32 . CreateProcessWithLogonW ( username , <EOL> domain , <EOL> password , <EOL> logonflags , <EOL> applicationname , <EOL> commandline , <EOL> creationflags , <EOL> environment , <EOL> currentdirectory , <EOL> ctypes . byref ( startupinfo ) , <EOL> ctypes . byref ( process_info ) ) <EOL> return process_info <EOL> def make_inheritable ( token ) : <EOL> return win32api . DuplicateHandle ( win32api . GetCurrentProcess ( ) , <EOL> token , <EOL> win32api . GetCurrentProcess ( ) , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:1> , <EOL> win32con . DUPLICATE_SAME_ACCESS ) <EOL> def runas_system ( cmd , username , password ) : <EOL> domain = '<STR_LIT:.>' <EOL> if '<STR_LIT:@>' in username : <EOL> username , domain = username . split ( '<STR_LIT:@>' ) <EOL> if '<STR_LIT:\\>' in username : <EOL> domain , username = username . split ( '<STR_LIT:\\>' ) <EOL> token = win32security . LogonUser ( username , <EOL> domain , <EOL> password , <EOL> win32con . LOGON32_LOGON_INTERACTIVE , <EOL> win32con . LOGON32_PROVIDER_DEFAULT ) <EOL> security_attributes = win32security . SECURITY_ATTRIBUTES ( ) <EOL> security_attributes . bInheritHandle = <NUM_LIT:1> <EOL> stdin_read , stdin_write = win32pipe . CreatePipe ( security_attributes , <NUM_LIT:0> ) <EOL> stdin_read = make_inheritable ( stdin_read ) <EOL> stdout_read , stdout_write = win32pipe . CreatePipe ( security_attributes , <NUM_LIT:0> ) <EOL> stdout_write = make_inheritable ( stdout_write ) <EOL> stderr_read , stderr_write = win32pipe . CreatePipe ( security_attributes , <NUM_LIT:0> ) <EOL> stderr_write = make_inheritable ( stderr_write ) <EOL> startup_info = win32process . STARTUPINFO ( ) <EOL> startup_info . dwFlags = win32con . STARTF_USESTDHANDLES <EOL> startup_info . hStdInput = stdin_read <EOL> startup_info . hStdOutput = stdout_write <EOL> startup_info . hStdError = stderr_write <EOL> user_environment = win32profile . CreateEnvironmentBlock ( token , False ) <EOL> cmd = '<STR_LIT>' . format ( cmd ) <EOL> procArgs = ( None , <EOL> cmd , <EOL> security_attributes , <EOL> security_attributes , <EOL> <NUM_LIT:1> , <EOL> <NUM_LIT:0> , <EOL> user_environment , <EOL> None , <EOL> startup_info ) <EOL> hProcess , hThread , PId , TId = win32process . CreateProcessAsUser ( token , <EOL> * procArgs ) <EOL> if stdin_read is not None : <EOL> stdin_read . Close ( ) <EOL> if stdout_write is not None : <EOL> stdout_write . Close ( ) <EOL> if stderr_write is not None : <EOL> stderr_write . Close ( ) <EOL> hThread . Close ( ) <EOL> ret = { '<STR_LIT>' : PId } <EOL> fd_out = msvcrt . open_osfhandle ( stdout_read , os . O_RDONLY | os . O_TEXT ) <EOL> with os . fdopen ( fd_out , '<STR_LIT:r>' ) as f_out : <EOL> ret [ '<STR_LIT>' ] = f_out . read ( ) <EOL> fd_err = msvcrt . open_osfhandle ( stderr_read , os . O_RDONLY | os . O_TEXT ) <EOL> with os . fdopen ( fd_err , '<STR_LIT:r>' ) as f_err : <EOL> ret [ '<STR_LIT>' ] = f_err . read ( ) <EOL> if win32event . WaitForSingleObject ( hProcess , win32event . INFINITE ) == win32con . WAIT_OBJECT_0 : <EOL> exitcode = win32process . GetExitCodeProcess ( hProcess ) <EOL> ret [ '<STR_LIT>' ] = exitcode <EOL> win32api . CloseHandle ( hProcess ) <EOL> return ret <EOL> def runas ( cmd , username , password , cwd = None ) : <EOL> if win32api . GetUserName ( ) == '<STR_LIT>' : <EOL> return runas_system ( cmd , username , password ) <EOL> c2pread , c2pwrite = CreatePipe ( inherit_read = False , inherit_write = True ) <EOL> errread , errwrite = CreatePipe ( inherit_read = False , inherit_write = True ) <EOL> stdin = kernel32 . GetStdHandle ( STD_INPUT_HANDLE ) <EOL> dupin = DuplicateHandle ( srchandle = stdin , inherit = True ) <EOL> startup_info = STARTUPINFO ( dwFlags = win32con . STARTF_USESTDHANDLES , <EOL> hStdInput = dupin , <EOL> hStdOutput = c2pwrite , <EOL> hStdError = errwrite ) <EOL> cmd = '<STR_LIT>' . format ( cmd ) <EOL> domain = None <EOL> if '<STR_LIT:@>' in username : <EOL> username , domain = username . split ( '<STR_LIT:@>' ) <EOL> if '<STR_LIT:\\>' in username : <EOL> domain , username = username . split ( '<STR_LIT:\\>' ) <EOL> process_info = CreateProcessWithLogonW ( username = username , <EOL> domain = domain , <EOL> password = password , <EOL> logonflags = LOGON_WITH_PROFILE , <EOL> commandline = cmd , <EOL> startupinfo = startup_info , <EOL> currentdirectory = cwd ) <EOL> kernel32 . CloseHandle ( dupin ) <EOL> kernel32 . CloseHandle ( c2pwrite ) <EOL> kernel32 . CloseHandle ( errwrite ) <EOL> kernel32 . CloseHandle ( process_info . hThread ) <EOL> ret = { '<STR_LIT>' : process_info . dwProcessId } <EOL> fd_out = msvcrt . open_osfhandle ( c2pread , os . O_RDONLY | os . O_TEXT ) <EOL> with os . fdopen ( fd_out , '<STR_LIT:r>' ) as f_out : <EOL> ret [ '<STR_LIT>' ] = f_out . read ( ) <EOL> fd_err = msvcrt . open_osfhandle ( errread , os . O_RDONLY | os . O_TEXT ) <EOL> with os . fdopen ( fd_err , '<STR_LIT:r>' ) as f_err : <EOL> ret [ '<STR_LIT>' ] = f_err . read ( ) <EOL> if kernel32 . WaitForSingleObject ( process_info . hProcess , INFINITE ) == win32con . WAIT_OBJECT_0 : <EOL> exitcode = wintypes . DWORD ( ) <EOL> kernel32 . GetExitCodeProcess ( process_info . hProcess , <EOL> ctypes . byref ( exitcode ) ) <EOL> ret [ '<STR_LIT>' ] = exitcode . value <EOL> kernel32 . CloseHandle ( process_info . hProcess ) <EOL> return ret </s>
