Jump to content

StdoutRead issue (openssl)


Recommended Posts

Hi,

I am testing this script that I'd like to plan to use in a project. But it needs to improve the way it deals with errors (wrong username or password). In fact, for a beyond understanding reason, StdoutRead seems to fail to retrieve the error line printed by openssl.

Script Object: connect to yahoo via ssl connexion, retrieve number of messages (and later the headers)

This script works but there are mysteries that need to be solved so that I could sleep :oops:

Here is the difference between the command line results retrieved in windows cmd and scite error console.

in windows CMD:

WARNING: can't open config file: /usr/local/ssl/openssl.cnf

Loading 'screen' into random state - done

depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert High Assurance CA-3

verify error:num=20:unable to get local issuer certificate

verify return:0

+OK hello from popgate-0.8.0.344798 pop012.mail.ukl.yahoo.com

USER anyone

+OK password required.

PASS anything

-ERR [AUTH] Incorrect username or password.

in scite console:

WARNING: can't open config file: /usr/local/ssl/openssl.cnf

Loading 'screen' into random state -+OK hello from popgate-0.8.0.344798 pop002.mail.ukl.yahoo.com

USER anyone

+OK password required.

PASS anything

As you can see, the red strings doesn't display in scite console and can't be properly handled by the script.

I spend the day at searching why and test everything I found on the web with no success: StringStripCR, StringStripWS, binary option/conversion, page code conversion, etc.

The weird thing is that the udf can read the green line but can't read the red ones after a - with the exactly same method. I suspect the char - to be the cause but can't confirm.

Here is the udf:

#include <Constants.au3>
#include <Array.au3>
#include <Process.au3>
Global Const $NO_ACTIVATION_LINK = 301
Global $_POP_RunPID = 0
Global $_POP_Log = @CRLF
Global $_POP_Debug = False
Global $_POP_Counter = 0
Global $SleepTime = 50
Global $FixGUIBug = True
Global $SSL_Exe_loc
Func _POP_Connect($POP_Server, $POP_Port, $SSL_Exe_loc, $Debug)
$_POP_Debug = $Debug
;Connect To Server Using OpenSSL
$_POP_RunPID = Run($SSL_Exe_loc & " s_client -connect " & $POP_Server & ":" & $POP_Port & " -quiet", "", @SW_HIDE,  $STDOUT_CHILD + $STDIN_CHILD + $STDERR_CHILD + $STDERR_MERGED)
;Hide Command Prompt
WinWait($SSL_Exe_loc)
WinActivate($SSL_Exe_loc)
WinWaitActive($SSL_Exe_loc)
;Read Buffer & Check buffer for reply code
;Sleep(2000)
$Buffer = _OpenSSL_ReadBuffer_Until("+OK ", 50)
$_POP_Log &= $Buffer
If @error Then
  SetError(1) ;Connection Failed
  Return False
EndIf
Return _OpenSSL_IsRunning()
EndFunc   ;==>_POP_Connect
Func _POP_Login($Username, $Password)
;flush buffer
Sleep(250)
_OpenSSL_ReadBuffer()
;Send Username
$temp = _POP_Send_User($Username)
If $temp == False Then
  $temp = _POP_Send_User($Username)
  If $temp == False Then
   Return $temp
  EndIf
EndIf
;Send Password
$temp = _POP_Send_Pass($Password)
If $temp == False Then
  Return $temp
EndIf
Return _OpenSSL_IsRunning()
EndFunc   ;==>_POP_Login
Func _POP_Send_User($Username)
;Send Username
$User = "USER " & $Username & @CRLF
_OpenSSL_WriteBuffer($User)
$_POP_Log &= $User
;Get Responce
$Buffer = _OpenSSL_ReadBuffer_Lock(20)
$_POP_Log &= $Buffer
If _OpenSSL_ChkForCode($Buffer, "+OK ") == 0 Then
  SetError(3) ;Wrong Username
  _OpenSSL_DebugToConsole("Wrong Username" & @CRLF)
  Return False
EndIf
EndFunc   ;==>_POP_Send_User
Func _POP_Send_Pass($Password)
;Send Password
$Pass = "PASS " & $Password & @CRLF
_OpenSSL_WriteBuffer($Pass)
$_POP_Log &= $Pass
;Get Responce
$Buffer = _OpenSSL_ReadBuffer_Lock(20)
$_POP_Log &= $Buffer
If _OpenSSL_ChkForCode($Buffer, "+OK ") == 0 Then
  SetError(4) ;Sent in a bad username/password
  _OpenSSL_DebugToConsole("Sent in a bad username/password" & @CRLF)
  Return False
EndIf
EndFunc   ;==>_POP_Send_Pass
;Support Functions (private)
Func _OpenSSL_ReadBuffer()
$Buffer = ""
$MoreToRead = 1
While $MoreToRead > 0
  $_POP_Counter += 1
  Sleep($SleepTime)
  $temp = StdoutRead($_POP_RunPID)
  ;$temp &= StderrRead($_POP_RunPID)
  ;$temp = BinaryToString (stringreplace($temp,chr(0),chr(0x20)))
  ;$temp = _ChangePageCode($temp,1)
  ;$temp = _OEMToAnsi($temp)
  $Buffer &= $temp
  $MoreToRead = @extended
  ;_OpenSSL_DebugToConsole(@CRLF & "+++" & @CRLF & $temp & @CRLF & "+++" & @CRLF)
WEnd
Return $Buffer
EndFunc   ;==>_OpenSSL_ReadBuffer
Func _OpenSSL_ReadBuffer_Lock($num = 250)
  $Buffer = ""
  $BlockAttempts = 0
  For $x = 0 To $num Step +1
   $BlockAttempts += 1
   $Buffer = _OpenSSL_ReadBuffer()
   ;$Buffer = stringreplace($Buffer,chr(0),chr(0x20))
   _OpenSSL_DebugToConsole("Read debug : " & $BlockAttempts & " = " & $Buffer & @CRLF )
   Sleep(150)
   If StringLen($Buffer) > 0 Then
    ExitLoop
   EndIf
  Next
  _OpenSSL_DebugToConsole(StringStripCR ("Read : " & $Buffer) & @CRLF)
  _OpenSSL_DebugToConsole("================================ " & "Block Attempts : " & $x & "  =================================" & @CRLF & @CRLF)
  Return $Buffer
EndFunc   ;==>_OpenSSL_ReadBuffer_Lock
Func _OpenSSL_ReadBuffer_Until($code, $num = 500)
$Buffer = ""
;Wait up to 25 seconds
For $x = 0 To $num Step +1
  $Buffer &=  _OpenSSL_ReadBuffer()
  $chk = _OpenSSL_ChkForCode($Buffer, $code)
  If $chk > 0 Then
   _OpenSSL_DebugToConsole(@CRLF & "====================== Found <" & $code & "> at char " & $chk & ": CONNEXION OK =======================" & @CRLF & _
          $Buffer)
   ExitLoop
  EndIf
  Sleep($SleepTime)
Next
_OpenSSL_DebugToConsole("=============================== " & "Finding Attempts : " & $x & "  ================================" & @CRLF & @CRLF)
If $chk <= 0 Then SetError(1)
Return $Buffer
EndFunc   ;==>_OpenSSL_ReadBuffer_Until
Func _OpenSSL_ChkForCode($Buffer, $code)
Return StringInStr($Buffer, $code)
EndFunc   ;==>_OpenSSL_ChkForCode
Func _OpenSSL_WriteBuffer_GUI()
;~  WinSetState($SSL_Exe_loc, "", @SW_SHOW)
BlockInput(1)
WinActivate($SSL_Exe_loc)
WinWaitActive($SSL_Exe_loc)
Send("FixGUIBug{ENTER}")
BlockInput(0)
;WinSetState($SSL_Exe_loc, "", @SW_HIDE)
_OpenSSL_DebugToConsole("Write-GUI: FixGUIBug" & @CRLF)
$Buffer = "FixGUIBug"
Sleep($SleepTime)
EndFunc   ;==>_OpenSSL_WriteBuffer_GUI
Func _OpenSSL_WriteBuffer($Msg)
If $FixGUIBug = False Then
  StdinWrite($_POP_RunPID, $Msg)
  _OpenSSL_DebugToConsole("Write: " & $Msg)
  Sleep($SleepTime)
Else
  _OpenSSL_WriteBuffer_GUI()
  $FixGUIBug = False
  _OpenSSL_WriteBuffer($Msg)
EndIf
EndFunc   ;==>_OpenSSL_WriteBuffer
Func _OpenSSL_Kill()
StdioClose($_POP_RunPID)
Sleep(1000)
If ProcessExists("openssl.exe") Then
  ProcessClose("cmd.exe")
  ProcessExists("openssl.exe")
  While ProcessExists("openssl.exe")
   ProcessClose("cmd.exe")
   ProcessClose("openssl.exe")
  WEnd
EndIf
EndFunc   ;==>_OpenSSL_Kill
Func _OpenSSL_DebugToConsole($string)
If $_POP_Debug Then
;~   ConsoleWrite(StringStripWS($string, 4))
  ConsoleWrite($string)
EndIf
EndFunc   ;==>_OpenSSL_DebugToConsole
Func _OpenSSL_IsRunning()
_ProcessGetName($_POP_RunPID)
If @error Then
  If ProcessExists("openssl.exe") Then
   $_POP_RunPID = ProcessExists("openssl.exe")
   Return True
  Else
   _OpenSSL_DebugToConsole("OpenSSL Closed Unexpectedly" & @CRLF)
   $_POP_Log &= "OpenSSL Closed Unexpectedly" & @CRLF
   _OpenSSL_Kill()
   Return False
  EndIf
Else
  Return True
EndIf
EndFunc   ;==>_OpenSSL_IsRunning

Here is an example script for testing:

#include <Array.au3>
#include "POP_ssl_V3.2.1.au3"
;~ Opt("OnExitFunc", "endscript")
Opt("SendKeyDelay", 0)
Opt("TrayIconDebug", 1)
;kill previous run
While ProcessExists("openssl.exe")
    ProcessClose("cmd.exe")
    ProcessClose("openssl.exe")
WEnd
;###FILL IN THIS INFO###
;~ $User = InputBox("Gmail", "Enter Username", "")
;~ $Pass = InputBox("Gmail", "Enter Password", "", "*")
$User = "anyone"
$Pass = "anything"
;set server info
$SSL_Exe_loc = @ScriptDir&"OpenSSLopenssl.exe"
$POP_Server = "pop.mail.yahoo.fr"
$POP_Port = "995"
;~  $error = ""
$msg = _POP_Connect($POP_Server, $POP_Port, $SSL_Exe_loc, True)
;~ MsgBox(0, $msg & " : " & @error, $_POP_Log)
;~  $error &= $msg & " : " & @error & @CRLF
;~  If $msg == False Then EndReport($error)
$msg = _POP_Login($User, $Pass)
;~ MsgBox(0, $msg & " : " & @error, $_POP_Log)
;~  $error &= $msg & " : " & @error & @CRLF
exit

You can download a tiny package with everything needed for testing here:

  • UDF
  • SCRIPT EXAMPLE
  • Openssl needed files
  • BATCH with the same command run by autoit
Help to solve this output mystery is welcome :bye:

Cheers.

Edited by freMea

[list][*]AutoIt 3.3.8.1[*]Win XP PRO SP3[/list]

Link to comment
Share on other sites

You are calling StdoutRead and StderrRead in the same loop.

$temp = StdoutRead($_POP_RunPID)
$temp &= StderrRead($_POP_RunPID)

Help file looks different, try two loops http://www.autoitscript.com/autoit3/docs/functions/StderrRead.htm

Yes, I did that just for test purpose (trying to solve the mystery). Originally, only
$temp = StdoutRead($_POP_RunPID)
was here but the problem is still there when it's alone. Edited by freMea

[list][*]AutoIt 3.3.8.1[*]Win XP PRO SP3[/list]

Link to comment
Share on other sites

You could just tweak the UDF to have it write to a file.

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco Finesse, Github, IRC UDF, WindowEx UDF

 

Link to comment
Share on other sites

You could just tweak the UDF to have it write to a file.

It's an idea. But I'll have to use the console to send commands anyway. It's not incompatible but I'm afraid that method might take a little much time in the process than just watching server response in openssl console output. And I have currently no idea how to route server responses to a file without using StdoutRead I'm still newby in autoIT and in programming mostly. Doesn't someone suspect StdoutRead to be buggy in that specific case? It seems not to be able to read some strings if there is - as suffix.

Edited by freMea

[list][*]AutoIt 3.3.8.1[*]Win XP PRO SP3[/list]

Link to comment
Share on other sites

  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...