#include-once ;============================================================ ; PSM Generic Client DLL Wrapper ; ------------------------------ ; ; Library that wraps the PSMDispatcherUtils.DLL functions ; ; Created : April 2013 ; Cyber-Ark Software Ltd. ;============================================================ ;======================================= ; Consts ;======================================= Global Const $DISPATCHER_UTILS_DLL = "PSMDispatcherUtils.dll" Global Const $DISPATCHER_UTILS_DRIVER_DLL = "PSMGenericClientDriver.dll" Global Const $INVALID_HANDLE_VALUE_INTERNAL = -1 ; renamed from $INVALID_HANDLE_VALUE to prevent conflicts with WinAPI.au3 Global Const $LOG_LEVEL_ERROR = 0 Global Const $LOG_LEVEL_TRACE = 1 Global Const $MIN_ARGUMENTS_NUMBER = 1 Global Const $TEST_MODE = "/test" ;======================================= ; Enums ;======================================= Global Enum Step -1 _ $PSM_ERROR_SUCCESS = 0, _ $PSM_ERROR_GENERAL_ERROR, _ $PSM_ERROR_ALREADY_LOADED, _ $PSM_ERROR_LOAD_FAILED, _ $PSM_ERROR_FAILED_TO_CALL, _ $PSM_ERROR_CALL_FAILED ;======================================= ; Globals ;======================================= Global $g_DllHandle = $INVALID_HANDLE_VALUE_INTERNAL Global $g_PSMLastError = "" ;======================================= ; Library utilities functions ;======================================= ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_Init ; Description ...: Initialize the dispatcher utils wrapper library. ; This function should only be called once per execution, and must be the first library function to be called! ; Parameters ....: None ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_Init () Local $PSMTestMode = false; Local $PSMComponentsFolder Local $DispatcherUtilsDLL; if ($CmdLine[0] < $MIN_ARGUMENTS_NUMBER) Then Return SetPSMError(StringFormat("Invalid number of command line arguments (received %d, expecting %d)", $CmdLine[0], $MIN_ARGUMENTS_NUMBER)) EndIf ; Handle $PSMComponentsFolder if (NOT FileExists($CmdLine[1])) Then Return SetPSMError(StringFormat("Invalid components folder argument received (%s)", $CmdLine[1])) EndIf $PSMComponentsFolder = $CmdLine[1] ; Handle $PSMTestMode if ($CmdLine[0] == $MIN_ARGUMENTS_NUMBER + 1) Then if ($CmdLine[2] == $TEST_MODE) Then $PSMTestMode = True EndIf EndIf If ($g_DllHandle <> $INVALID_HANDLE_VALUE_INTERNAL) Then Return SetPSMError("DLL already loaded", $PSM_ERROR_ALREADY_LOADED) EndIf if ($PSMTestMode == False) Then $DispatcherUtilsDLL = $DISPATCHER_UTILS_DLL Else $DispatcherUtilsDLL = $DISPATCHER_UTILS_DRIVER_DLL EndIf $g_DllHandle = DllOpen($PSMComponentsFolder & $DispatcherUtilsDLL) if ($g_DllHandle == $INVALID_HANDLE_VALUE_INTERNAL) Then Return SetPSMError("Failed to load DLL " & $PSMComponentsFolder & $DispatcherUtilsDLL, $PSM_ERROR_LOAD_FAILED) EndIf Return $PSM_ERROR_SUCCESS EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_Term ; Description ...: Terminate the dispatcher wrapper library ; Parameters ....: None ; Return values .: None ; =============================================================================================================================== Func PSMGenericClient_Term () ; Performing logics that should be executed before the client dispatcher ends ; This can take some time, and because we are during termination process we don't want any tooltips to appear from this point ToolTip("") PSMGenericClient_FinalizeDispatcher(); If ($g_DllHandle <> $INVALID_HANDLE_VALUE_INTERNAL) Then DllClose($g_DllHandle) $g_DllHandle = $INVALID_HANDLE_VALUE_INTERNAL EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_IsInitialized ; Description ...: Determines if the DLL was already initialized ; Parameters ....: None ; Return values .: True if the DLL was initialized, False otherwise ; =============================================================================================================================== Func PSMGenericClient_IsInitialized () If ($g_DllHandle <> $INVALID_HANDLE_VALUE_INTERNAL) Then Return True Else Return False EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_PSMGetLastErrorString ; Description ...: Gets the description of the last PSM error that occured ; Parameters ....: None. ; Return values .: Last PSM error (string) ; =============================================================================================================================== Func PSMGenericClient_PSMGetLastErrorString() Return "PSMGenericClientWrapper error: " & $g_PSMLastError EndFunc ;======================================= ; DLL wrapper functions ;======================================= ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_SendPID ; Description ...: Sends the connection client's PID to the PSM server session ; Parameters ....: $PID - [IN] The process ID to send to the PSM server ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_SendPID ($PID) Local $Result ; DWORD SendPID(DWORD dwPID) $Result = DllCall($g_DllHandle, "DWORD:cdecl", "SendPID", "DWORD", $PID) if (@error <> 0) Then Return SetPSMError("Failed to call DLL function SendPID (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then Return SetPSMError("DLL function SendPID failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf Return $PSM_ERROR_SUCCESS EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_LogWrite ; Description ...: Write a log message to standard PSM log file ; Parameters ....: $sMessage - [IN] The message to write ; $LogLevel - [IN] Defined if the message should be handled as an error message. ; Valid values are either $LOG_LEVEL_TRACE or $LOG_LEVEL_ERROR. ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_LogWrite($sMessage, $LogLevel) Local $Result ; Prepare the message stream Local $pMessage = DllStructCreate("char[" & StringLen($sMessage) & "]") DllStructSetData($pMessage, 1, $sMessage) ; DWORD LogWrite(char* szString, DWORD dwStringLength, DWORD dwLogLevel) $Result = DllCall($g_DllHandle, "DWORD:cdecl", "LogWrite", "ptr", DllStructGetPtr($pMessage), "DWORD", StringLen($sMessage), "DWORD", $LogLevel) if (@error <> 0) Then Return SetPSMError("Failed to call DLL function LogWrite (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then Return SetPSMError("DLL function LogWrite failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf Return $PSM_ERROR_SUCCESS EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_MapTSDrives ; Description ...: Maps TS mapped local drives as actual drives for the session ; Parameters ....: None ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_MapTSDrives () Local $Result ; DWORD SendPID(DWORD dwPID) $Result = DllCall($g_DllHandle, "DWORD:cdecl", "MapTSDrives") if (@error <> 0) Then Return SetPSMError("Failed to call DLL function MapTSDrives (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then Return SetPSMError("DLL function MapTSDrives failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf Return $PSM_ERROR_SUCCESS EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_GetSessionPropertyBufferLength ; Description ...: Get the length required for buffer to contain the requested session property (including the trailing string ; terminating zero) ; Parameters ....: $sSessionPropertyName - [IN] The name of the session property to retrieve ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_GetSessionPropertyBufferLength($sSessionPropertyName) Local $pLength Local $dwLength Local $Result ; Prepare the property name Local $pSessionPropertyName = DllStructCreate("char[" & StringLen($sSessionPropertyName) & "]") DllStructSetData($pSessionPropertyName, 1, $sSessionPropertyName) ; DWORD GetSessionPropertyBufferLength(char* szPropertyName, DWORD dwPropertyNameLength, DWORD* dwLength) $pLength = DllStructCreate("DWORD") $Result = DllCall($g_DllHandle, "DWORD:cdecl", "GetSessionPropertyBufferLength", "ptr", DllStructGetPtr($pSessionPropertyName), "DWORD", StringLen($sSessionPropertyName), "ptr", DllStructGetPtr($pLength)) if (@error <> 0) Then $pLength = 0 Return SetPSMError("Failed to call DLL function GetSessionPropertyBufferLength (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then $pLength = 0 Return SetPSMError("DLL function GetSessionPropertyBufferLength failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf $dwLength = DllStructGetData($pLength, 1) $pLength = 0 Return $dwLength EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_GetSessionProperty ; Description ...: Gets the session property from PSM ; Parameters ....: $sSessionPropertyName - [IN] Session property name ; $SessionProperty - [OUT] Session property value ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_GetSessionProperty($sSessionPropertyName, ByRef $SessionPropertyValue) Local $dwLength Local $Result ; Prepare the property name Local $pSessionPropertyName = DllStructCreate("char[" & StringLen($sSessionPropertyName) & "]") DllStructSetData($pSessionPropertyName, 1, $sSessionPropertyName) $dwLength = PSMGenericClient_GetSessionPropertyBufferLength($sSessionPropertyName) if ($dwLength < 0) Then Return SetPSMError("Failed to get dispatcher parameters (error: " & $g_PSMLastError & ")", $dwLength) EndIf ; DWORD GetSessionProperty(char* szPropertyName, DWORD dwPropertyNameLength, char* szBuffer, DWORD dwBufferSize) Local $pBuffer = DllStructCreate("char[" & $dwLength & "]") $Result = DllCall($g_DllHandle, "DWORD:cdecl", "GetSessionProperty", "ptr", DllStructGetPtr($pSessionPropertyName), "DWORD", StringLen($sSessionPropertyName), "ptr", DllStructGetPtr($pBuffer), "DWORD", $dwLength) if (@error <> 0) Then $pBuffer = 0 Return SetPSMError("Failed to call DLL function GetSessionProperty (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then $pBuffer = 0 Return SetPSMError("DLL function GetSessionProperty failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf $SessionPropertyValue = DllStructGetData($pBuffer, 1) $pBuffer = 0 Return $PSM_ERROR_SUCCESS EndFunc ;======================================= ; Internal functions ;======================================= ; #FUNCTION# ==================================================================================================================== ; Name...........: SetPSMError ; Description ...: Sets last PSM error string ; =============================================================================================================================== Func SetPSMError($szError, $nRC = $PSM_ERROR_GENERAL_ERROR) $g_PSMLastError = $szError Return $nRC EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: PSMGenericClient_FinalizeDispatcher ; Description ...: Performing logics that should be executed before the client dispatcher ends ; Parameters ....: None ; Return values .: $PSM_ERROR_SUCCESS - Success, Otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func PSMGenericClient_FinalizeDispatcher () Local $Result ; DWORD FinalizeDispatcher() $Result = DllCall($g_DllHandle, "DWORD:cdecl", "FinalizeDispatcher") if (@error <> 0) Then Return SetPSMError("Failed to call DLL function FinalizeDispatcher (" & @error & ")", $PSM_ERROR_FAILED_TO_CALL) EndIf if ($Result[0] <> True) Then Return SetPSMError("DLL function FinalizeDispatcher failed (" & @error & ")", $PSM_ERROR_CALL_FAILED) EndIf Return $PSM_ERROR_SUCCESS EndFunc