#AutoIt3Wrapper_UseX64=n Opt("MustDeclareVars", 1) AutoItSetOption("WinTitleMatchMode", 3) ; EXACT_MATCH! ;============================================================ ; PSM Universal Connector - Web FireFox Skeleton Dispatcher ; ------------------------------------------------- ; Use this skeleton to create your own ; connection components integrated with the PSM. ; Areas you may want to modify are marked ; with the string "CHANGE_ME". ; ; Created : June 2015 ; Cyber-Ark Software Ltd. ;============================================================ #include "PSMGenericClientWrapper.au3" #include ; Required to interact with FireFox ; Can include in order to block user to exit Internet Explorer Full Screen #include ;================================ ; Consts & Globals ;================================ Global Const $WEBSITE_NAME = "My Web" ; CHANGE_ME Global Const $MESSAGE_TITLE = "PSM - My Web Logon" ; CHANGE_ME ;Parameters ; The %s Will be replaced with the Target Account Address Global $g_pTemplateAddress = "https://%s/" ; CHANGE_ME ; The Connection Parametrs will be saved here ; Parameters that are not included in the Password Object will use these Defaults Global $g_pTargetUsername = "UserName" ; Will be fetched from the PSM Session Global $g_pTargetPassword = "Password" ; Will be fetched from the PSM Session Global $g_pTargetAddress = "Address" ; Will be fetched from the PSM Session Global $g_pFFLoadWait_Delay = 500 ; Change if needed Global $g_pFFLoadWait_Timeout = 10000 ; Change if needed ;================================ ; Consts & Globals - DO NOT CHANGE ;================================ Global Const $ERROR_MESSAGE_TITLE = "PSM " & $WEBSITE_NAME & " Dispatcher error message" Global Const $LOG_MESSAGE_PREFIX = $WEBSITE_NAME & " Dispatcher - " Global $g_ConnectionClientPID = 0 Global $g_IEInstance ;Internal Variables Global $g_ErrorMessageTime = 15000 ; Start the Code Exit Main() Func LoginProcess() LogWrite("Entered LoginProcess()", False) ; References to HTML elements in the login process local $_UserNameBoxName = "userName" ; CHANGE_ME local $_PasswordBoxName = "userPass" ; CHANGE_ME local $_WebFormName = "webui_login_mainpanel" ; CHANGE_ME local $_LoginButton = "x-btn-text" ; CHANGE_ME BlockAllInput() local $oUser = _FFObjGet($_UserNameBoxName, "name") local $oPass = _FFObjGet($_PasswordBoxName, "name") local $oButton = _FFObjGet($_LoginButton, "name") _FFSetValue($g_pTargetUsername, $oUser) _FFSetValue($g_pTargetPassword, $oPass) ; Login _FFClick($oButton) LogWrite("finished LoginProcess() successfully", False) UnblockAllBlockProhibited() EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: FetchSessionProperties ; Description ...: Fetches properties required for the session from the PSM ; Parameters ....: None ; Return values .: None ; =============================================================================================================================== Func FetchSessionProperties() ; Get the Session User Name If (PSMGenericClient_GetSessionProperty("Username", $g_pTargetUsername) <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf ; Get the Session Password If (PSMGenericClient_GetSessionProperty("Password", $g_pTargetPassword) <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf ; Get the Session Address If (PSMGenericClient_GetSessionProperty("Address", $g_pTargetAddress) <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf EndFunc ;*=*=*=*=*=*=*=*=*=*=*=*=*= ; DO NOT CHANGE FROM HERE ;*=*=*=*=*=*=*=*=*=*=*=*=*= ;======================================= ; Main ;======================================= Func Main() ; Init PSM Dispatcher utils wrapper MessageUserOn($MESSAGE_TITLE, "The PSM is about to log you on automatically which may take several seconds...") If (PSMGenericClient_Init() <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf LogWrite("successfully initialized Dispatcher Utils Wrapper", False) LogWrite("mapping local drives", False) If (PSMGenericClient_MapTSDrives() <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf ; Get the dispatcher parameters FetchSessionProperties() MessageUserOn($MESSAGE_TITLE, "Starting " & $WEBSITE_NAME & "...") LogWrite("starting FireFox...", False) ;Open FF. OpenFF will terminate the process If FireFox wasn't loaded properly. No need to check PID here. $g_ConnectionClientPID = OpenFF() if ($g_ConnectionClientPID == 0) Then Error(StringFormat("Failed to execute process [%s]", "FireFox", @error)) EndIf LogWrite(StringFormat("%s successfully invoked (PID=%d)", "FireFox", $g_ConnectionClientPID),False) ;Send PID to PSM as early as possible so recording/monitoring can begin LogWrite("sending PID to PSM") If (PSMGenericClient_SendPID($g_ConnectionClientPID) <> $PSM_ERROR_SUCCESS) Then Error(PSMGenericClient_PSMGetLastErrorString()) EndIf ;Handle Login MessageUserOff() LoginProcess() ; Terminate PSM Dispatcher utils wrapper LogWrite("Terminating Dispatcher Utils Wrapper") PSMGenericClient_Term() Return $PSM_ERROR_SUCCESS EndFunc ;================================== ; Functions ;================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: Error ; Description ...: An exception handler - displays an error message and terminates the dispatcher ; Parameters ....: $ErrorMessage - Error message to display ; $Code - [Optional] Exit error code ; =============================================================================================================================== Func Error($ErrorMessage, $Code = -1) ; If the dispatcher utils DLL was already initialized, write an error log message and terminate the wrapper If (PSMGenericClient_IsInitialized()) Then LogWrite($ErrorMessage, $LOG_LEVEL_ERROR) PSMGenericClient_Term() EndIf MessageUserOn("ERROR - PROCESS IS SHUTTING DOWN", $ErrorMessage) sleep($g_ErrorMessageTime) ; If the connection component was already invoked, terminate it If ($g_ConnectionClientPID <> 0) Then ProcessClose($g_ConnectionClientPID) $g_ConnectionClientPID = 0 EndIf Exit $Code EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: LogWrite ; Description ...: Write a PSMWinSCPDispatcher log message to standard PSM log file ; Parameters ....: $sMessage - [IN] The message to write ; $LogLevel - [Optional] [IN] Defined If the message should be handled as an error message or as a trace messge ; Return values .: $PSM_ERROR_SUCCESS - Success, otherwise error - Use PSMGenericClient_PSMGetLastErrorString for details. ; =============================================================================================================================== Func LogWrite($sMessage, $LogLevel = $LOG_LEVEL_TRACE) Return PSMGenericClient_LogWrite($LOG_MESSAGE_PREFIX & $sMessage, $LogLevel) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: AssertErrorLevel ; Description ...: Checks If error level is <> 0. If so, write to log and call error. ; Parameters ....: $error_code - the error code from last function call (@error) ; $message - Message to show to user as well as write to log ; $code - exit code (default -1) ; Return values .: None ; =============================================================================================================================== Func AssertErrorLevel($error_code, $message, $code = -1) ;Unblock input so user can exit If ($error_code <> 0) Then LogWrite(StringFormat("AssertErrorLevel - %s :: @error = %d", $message, $error_code), $LOG_LEVEL_ERROR) Error($message, $code) EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: MessageUserOn ; Description ...: Writes a message to the user, and keeps it indefinitely (until function call to MessageUserOff) ; Parameters ....: $msgTitle - Title of the message ; $msgBody - Body of the message ; Return values .: none ; =============================================================================================================================== Func MessageUserOn(Const ByRef $msgTitle, Const ByRef $msgBody) SplashOff() SplashTextOn ($msgTitle, $msgBody, -1, 54, -1, -1, 0, "Tahoma", 9, -1) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: MessageUserOff ; Description ...: See SplashOff() ; Parameters ....: ; ; Return values .: none ; =============================================================================================================================== Func MessageUserOff() SplashOff() EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: BlockAllInput ; Description ...: Blocks all input (mouse & keyboard). Use when login process runs and visible, so user can't ; manipulate the process ; Parameters ....: ; Return values .: none ; =============================================================================================================================== Func BlockAllInput() LogWrite("Blocking Input") ;Block all input - mouse and keyboard If IsDeclared("s_KeyboardKeys_Buffer") <> 0 Then _BlockInputEx(1) AssertErrorLevel(@error, StringFormat("Could not block all input. Aborting... @error: %d", @error)) Else BlockInput(1) AssertErrorLevel(@error, StringFormat("Could not block all input. Aborting... @error: %d", @error)) EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: UnblockAllBlockProhibited ; Description ...: Allows all input from the user, except for prohibited keys (such as F11). ; Parameters ....: ; ; Return values .: none ; =============================================================================================================================== Func UnblockAllBlockProhibited() If IsDeclared("s_KeyboardKeys_Buffer") <> 0 Then _BlockInputEx(0) _BlockInputEx(3, "", "{F11}|{Ctrl}") ;Ctrl - also +C? +V?... Else BlockInput(0) EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: OpenFF ; Description ...: Creates FireFox and navigates to the address ; Parameters ....: None ; Return values .: The Program Process ID (For PSM Logging) ; =============================================================================================================================== Func OpenFF() ; Translate the Web Site URL Address with the requiered device dim $_TargetURL = StringFormat($g_pTemplateAddress, $g_pTargetAddress) ; Set Load Timeout as set in the Script LogWrite(StringFormat("Setting FireFox 'LoadWaitTimeOut' to %s",3000),False) _FFAu3Option("LoadWaitTimeOut", 3000) ; Open FireFox instance and navigate to $_TargeetURL LogWrite(StringFormat("Starting FireFox, Navigating to %s",$_TargetURL),False) _FFStart() AssertErrorLevel(@error, "Call to _FFStart failed", -1002) LogWrite("Finished loading FireFox") local $hndl = 0 If _FFConnect(Default, Default, 300) Then _FFOpenURL($_TargetURL) AssertErrorLevel(@error, "Failed to navigate to site", -1002) LogWrite("FireFox is connected...", False) HardenFF() LogWrite("FireFox is Hardened...", False) $hndl = _FFWindowGetHandle() Else Error("Can't connect to FireFox") EndIf return HWindowToPID($hndl) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: HWindowToPID ; Description....: Returns the PID that owns the given window HWND. ; Parameters.....: $hWindow - HWND of the window ; Return values..: PID of the process that owns the window. If PID = 0 this function calls Error thus terminating the ; autoit process ; =============================================================================================================================== Func HWindowToPID($hWindow) Local $pPID Local $dwPID Local $result $pPID = DllStructCreate("DWORD") ; DWORD GetWindowThreadProcessId(HWND hWnd, LPDWORD lpdwProcessId) ; Parameters: ; hWnd [IN] HWND of the window ; lpdwProcessId [OUT] Process ID ; Return value: Thread ID (DWORD) $result = DllCall("user32.dll", "DWORD", "GetWindowThreadProcessId", "hwnd", $hWindow, "ptr", DllStructGetPtr($pPID)) If (@error <> 0) Then $pPID = 0 Error(StringFormat("Failed to get IE PID (Extra details: %d)", @error),-20) EndIf $dwPID = DllStructGetData($pPID, 1) $pPID = 0 return $dwPID EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: HardenFF ; Description ...: This function makes the FireFox fullscreen and disables toolbar, menubar and address bar. Note that If you do not use _ExBlockInput, ; user can press F11 key and get to the address bar. To solve this, use BlockAllInput(), AllowAllInputButF11() ; Parameters ....: None ; Return values .: None ; =============================================================================================================================== ;Func HardenFF() ; Harden FF ;~ _FFAction("presentationmode", True) ; LogWrite("Hardening FireFox (No menubar, Full screen mode", False) ;_FFCmd("toggleAffectedChrome(true)" & @CRLF & "fullScreen=true") ;EndFunc