From e854439f509177bd08e53ec7f3229fbb8b483c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Mon, 30 Nov 2015 02:18:18 +0000 Subject: [PATCH] [GB.NET.SMTP] * NEW: Support for PLAIN and CRAM-MD5 authentications. git-svn-id: svn://localhost/gambas/trunk@7495 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- comp/src/gb.net.smtp/.src/Main.module | 20 +++++++- comp/src/gb.net.smtp/.src/SmtpClient.class | 58 +++++++++++++++++----- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/comp/src/gb.net.smtp/.src/Main.module b/comp/src/gb.net.smtp/.src/Main.module index be8a7b665..44bad0024 100644 --- a/comp/src/gb.net.smtp/.src/Main.module +++ b/comp/src/gb.net.smtp/.src/Main.module @@ -18,9 +18,27 @@ Public Sub Main() ' hClient.Add(File.Load("~/TexteDehorsilpleut.odt"), "application/vnd.oasis.opendocument.text", "Dehors il pleut.odt") ' hClient.Add(File.Load("~/balloon.png"), "image/png", "balloon.png") ' hClient.Send - + Dim hMsg As New SmtpClient + ' Dim sChallenge As String + ' Dim sKey As String + ' Dim sCommand As String + ' Dim sResult As String + ' + ' sChallenge = "'abc'" + ' sKey = "benoit" + ' + ' sCommand = "echo -n " & Shell$(sChallenge) & " | openssl md5 -hmac " & Shell$(sKey) + ' Shell sCommand To sResult + ' Print sResult + ' + ' sCommand = "openssl md5 -hmac " & Shell$(sKey) & " << EOF\n'abc'" + ' Shell sCommand To sResult + ' Print sResult + ' + ' Return + hMsg.Debug = True hMsg.To.Add("benoit@minisini.fr") hMsg.Subject = "Test mail headers 5" diff --git a/comp/src/gb.net.smtp/.src/SmtpClient.class b/comp/src/gb.net.smtp/.src/SmtpClient.class index 1332bdcf8..2357e8aea 100644 --- a/comp/src/gb.net.smtp/.src/SmtpClient.class +++ b/comp/src/gb.net.smtp/.src/SmtpClient.class @@ -309,21 +309,55 @@ Catch End Private Sub Authenticate() - - Dim sData As String - + + Dim sChallenge64, sChallenge, sKey, sCommand, sResponse, sDigestHex As String + If Not $sUser Then Return - - sData = $hSession.Send("AUTH PLAIN") - If $hSession.LastCode <> "334" Then Error.Raise("Unsupported authentication method") - - sData = $hSession.Send(Base64$($sUser & Chr$(0) & $sUser & Chr$(0) & $sPassword), True) - If $hSession.LastCode <> "235" Then Error.Raise("Authentication failed") - + + ' AUTH LOGIN + $hSession.Send("AUTH LOGIN") + If $hSession.LastCode = "334" Then + $hSession.Send(Base64$($sUser)) + If $hSession.LastCode = "334" Then + $hSession.Send(Base64$($sPassword)) + If $hSession.LastCode = "235" Then Return + Endif + Endif + + ' AUTH PLAIN + $hSession.Send("AUTH PLAIN") + If $hSession.LastCode <> "334" Then + $hSession.Send("AUTH PLAIN " & Base64$($sUser & Chr$(0) & $sUser & Chr$(0) & $sPassword), True) + Else + $hSession.Send(Base64$($sUser & Chr$(0) & $sUser & Chr$(0) & $sPassword), True) + Endif + If $hSession.LastCode = "235" Then Return + + ' CRAM-MD5 + $hSession.Send("AUTH CRAM-MD5") + 'If $hSession.LastCode = "334" Then Print "LastAnswer = "; $hSession.LastAnswer + + sChallenge64 = Split($hSession.LastAnswer, " ")[1] + sChallenge = UnBase64(sChallenge64) + sKey = $sPassword + + 'sCommand = "echo -n " & Shell$(sChallenge) & " | openssl md5 -hmac " & Shell$(sKey) + sCommand = "openssl md5 -hmac " & Shell$(sKey) & " << EOF\n" & sChallenge + Shell sCommand To sDigestHex + + sDigestHex = Trim(Split(sDigestHex, "=")[1]) + sResponse = Base64($sUser & sDigestHex) + + $hSession.Send(sResponse) + If $hSession.LastCode = "235" Then Return + + ' Nothing worked? + Error.Raise("Authentication failed") + Catch - + Error.Raise("Unable to authenticate: " & Error.Text) - + End Private Sub SendRecipients()