|
|
@ -50,433 +50,453 @@ |
|
|
|
#include <QCryptographicHash> |
|
|
|
#include <QCryptographicHash> |
|
|
|
#include <QStringList> |
|
|
|
#include <QStringList> |
|
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
namespace |
|
|
|
const short DEFAULT_PORT = 25; |
|
|
|
|
|
|
|
const short DEFAULT_PORT_SSL = 465; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray hmacMD5(QByteArray key, const QByteArray &msg) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
const int blockSize = 64; // HMAC-MD5 block size
|
|
|
|
const short DEFAULT_PORT = 25; |
|
|
|
if (key.length() > blockSize) { // if key is longer than block size (64), reduce key length with MD5 compression
|
|
|
|
const short DEFAULT_PORT_SSL = 465; |
|
|
|
key = QCryptographicHash::hash(key, QCryptographicHash::Md5); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray innerPadding(blockSize, char(0x36)); // initialize inner padding with char "6"
|
|
|
|
|
|
|
|
QByteArray outerPadding(blockSize, char(0x5c)); // initialize outer padding with char "\"
|
|
|
|
|
|
|
|
// ascii characters 0x36 ("6") and 0x5c ("\") are selected because they have large
|
|
|
|
|
|
|
|
// Hamming distance (http://en.wikipedia.org/wiki/Hamming_distance)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < key.length(); i++) { |
|
|
|
|
|
|
|
innerPadding[i] = innerPadding[i] ^ key.at(i); // XOR operation between every byte in key and innerpadding, of key length
|
|
|
|
|
|
|
|
outerPadding[i] = outerPadding[i] ^ key.at(i); // XOR operation between every byte in key and outerpadding, of key length
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// result = hash ( outerPadding CONCAT hash ( innerPadding CONCAT baseString ) ).toBase64
|
|
|
|
|
|
|
|
QByteArray total = outerPadding; |
|
|
|
|
|
|
|
QByteArray part = innerPadding; |
|
|
|
|
|
|
|
part.append(msg); |
|
|
|
|
|
|
|
total.append(QCryptographicHash::hash(part, QCryptographicHash::Md5)); |
|
|
|
|
|
|
|
return QCryptographicHash::hash(total, QCryptographicHash::Md5); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray determineFQDN() |
|
|
|
QByteArray hmacMD5(QByteArray key, const QByteArray &msg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QString hostname = QHostInfo::localHostName(); |
|
|
|
const int blockSize = 64; // HMAC-MD5 block size
|
|
|
|
if (hostname.isEmpty()) |
|
|
|
if (key.length() > blockSize) { // if key is longer than block size (64), reduce key length with MD5 compression
|
|
|
|
hostname = "localhost"; |
|
|
|
key = QCryptographicHash::hash(key, QCryptographicHash::Md5); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return hostname.toLocal8Bit(); |
|
|
|
QByteArray innerPadding(blockSize, char(0x36)); // initialize inner padding with char "6"
|
|
|
|
} |
|
|
|
QByteArray outerPadding(blockSize, char(0x5c)); // initialize outer padding with char "\"
|
|
|
|
|
|
|
|
// ascii characters 0x36 ("6") and 0x5c ("\") are selected because they have large
|
|
|
|
|
|
|
|
// Hamming distance (http://en.wikipedia.org/wiki/Hamming_distance)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < key.length(); i++) { |
|
|
|
|
|
|
|
innerPadding[i] = innerPadding[i] ^ key.at(i); // XOR operation between every byte in key and innerpadding, of key length
|
|
|
|
|
|
|
|
outerPadding[i] = outerPadding[i] ^ key.at(i); // XOR operation between every byte in key and outerpadding, of key length
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// result = hash ( outerPadding CONCAT hash ( innerPadding CONCAT baseString ) ).toBase64
|
|
|
|
|
|
|
|
QByteArray total = outerPadding; |
|
|
|
|
|
|
|
QByteArray part = innerPadding; |
|
|
|
|
|
|
|
part.append(msg); |
|
|
|
|
|
|
|
total.append(QCryptographicHash::hash(part, QCryptographicHash::Md5)); |
|
|
|
|
|
|
|
return QCryptographicHash::hash(total, QCryptographicHash::Md5); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray determineFQDN() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
QString hostname = QHostInfo::localHostName(); |
|
|
|
|
|
|
|
if (hostname.isEmpty()) |
|
|
|
|
|
|
|
hostname = "localhost"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return hostname.toLocal8Bit(); |
|
|
|
|
|
|
|
} |
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Net; |
|
|
|
using namespace Net; |
|
|
|
|
|
|
|
|
|
|
|
Smtp::Smtp(QObject *parent): QObject(parent), |
|
|
|
Smtp::Smtp(QObject *parent) |
|
|
|
state(Init), use_ssl(false) { |
|
|
|
: QObject(parent) |
|
|
|
|
|
|
|
, m_state(Init) |
|
|
|
|
|
|
|
, m_useSsl(false) |
|
|
|
|
|
|
|
{ |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
socket = new QSslSocket(this); |
|
|
|
m_socket = new QSslSocket(this); |
|
|
|
#else |
|
|
|
#else |
|
|
|
socket = new QTcpSocket(this); |
|
|
|
m_socket = new QTcpSocket(this); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
connect(socket, SIGNAL(readyRead()), SLOT(readyRead())); |
|
|
|
connect(m_socket, SIGNAL(readyRead()), SLOT(readyRead())); |
|
|
|
connect(socket, SIGNAL(disconnected()), SLOT(deleteLater())); |
|
|
|
connect(m_socket, SIGNAL(disconnected()), SLOT(deleteLater())); |
|
|
|
|
|
|
|
|
|
|
|
// Test hmacMD5 function (http://www.faqs.org/rfcs/rfc2202.html)
|
|
|
|
// Test hmacMD5 function (http://www.faqs.org/rfcs/rfc2202.html)
|
|
|
|
Q_ASSERT(hmacMD5("Jefe", "what do ya want for nothing?").toHex() |
|
|
|
Q_ASSERT(hmacMD5("Jefe", "what do ya want for nothing?").toHex() |
|
|
|
== "750c783e6ab0b503eaa86e310a5db738"); |
|
|
|
== "750c783e6ab0b503eaa86e310a5db738"); |
|
|
|
Q_ASSERT(hmacMD5(QByteArray::fromHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), |
|
|
|
Q_ASSERT(hmacMD5(QByteArray::fromHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), "Hi There").toHex() |
|
|
|
"Hi There").toHex() |
|
|
|
== "9294727a3638bb1c13f48ef8158bfc9d"); |
|
|
|
== "9294727a3638bb1c13f48ef8158bfc9d"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Smtp::~Smtp() { |
|
|
|
Smtp::~Smtp() |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
{ |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body) { |
|
|
|
void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body) |
|
|
|
const Preferences* const pref = Preferences::instance(); |
|
|
|
{ |
|
|
|
QTextCodec* latin1 = QTextCodec::codecForName("latin1"); |
|
|
|
const Preferences* const pref = Preferences::instance(); |
|
|
|
message = ""; |
|
|
|
QTextCodec* latin1 = QTextCodec::codecForName("latin1"); |
|
|
|
message += encode_mime_header("Date", QDateTime::currentDateTime().toUTC().toString("ddd, d MMM yyyy hh:mm:ss UT"), latin1); |
|
|
|
m_message = ""; |
|
|
|
message += encode_mime_header("From", from, latin1); |
|
|
|
m_message += encodeMimeHeader("Date", QDateTime::currentDateTime().toUTC().toString("ddd, d MMM yyyy hh:mm:ss UT"), latin1); |
|
|
|
message += encode_mime_header("Subject", subject, latin1); |
|
|
|
m_message += encodeMimeHeader("From", from, latin1); |
|
|
|
message += encode_mime_header("To", to, latin1); |
|
|
|
m_message += encodeMimeHeader("Subject", subject, latin1); |
|
|
|
message += "MIME-Version: 1.0\r\n"; |
|
|
|
m_message += encodeMimeHeader("To", to, latin1); |
|
|
|
message += "Content-Type: text/plain; charset=UTF-8\r\n"; |
|
|
|
m_message += "MIME-Version: 1.0\r\n"; |
|
|
|
message += "Content-Transfer-Encoding: base64\r\n"; |
|
|
|
m_message += "Content-Type: text/plain; charset=UTF-8\r\n"; |
|
|
|
message += "\r\n"; |
|
|
|
m_message += "Content-Transfer-Encoding: base64\r\n"; |
|
|
|
// Encode the body in base64
|
|
|
|
m_message += "\r\n"; |
|
|
|
QString crlf_body = body; |
|
|
|
// Encode the body in base64
|
|
|
|
QByteArray b = crlf_body.replace("\n","\r\n").toUtf8().toBase64(); |
|
|
|
QString crlf_body = body; |
|
|
|
int ct = b.length(); |
|
|
|
QByteArray b = crlf_body.replace("\n","\r\n").toUtf8().toBase64(); |
|
|
|
for (int i = 0; i < ct; i += 78) |
|
|
|
int ct = b.length(); |
|
|
|
{ |
|
|
|
for (int i = 0; i < ct; i += 78) |
|
|
|
message += b.mid(i, 78); |
|
|
|
m_message += b.mid(i, 78); |
|
|
|
} |
|
|
|
m_from = from; |
|
|
|
this->from = from; |
|
|
|
m_rcpt = to; |
|
|
|
rcpt = to; |
|
|
|
// Authentication
|
|
|
|
// Authentication
|
|
|
|
if (pref->getMailNotificationSMTPAuth()) { |
|
|
|
if (pref->getMailNotificationSMTPAuth()) { |
|
|
|
m_username = pref->getMailNotificationSMTPUsername(); |
|
|
|
username = pref->getMailNotificationSMTPUsername(); |
|
|
|
m_password = pref->getMailNotificationSMTPPassword(); |
|
|
|
password = pref->getMailNotificationSMTPPassword(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Connect to SMTP server
|
|
|
|
// Connect to SMTP server
|
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
if (pref->getMailNotificationSMTPSSL()) { |
|
|
|
if (pref->getMailNotificationSMTPSSL()) { |
|
|
|
socket->connectToHostEncrypted(pref->getMailNotificationSMTP(), DEFAULT_PORT_SSL); |
|
|
|
m_socket->connectToHostEncrypted(pref->getMailNotificationSMTP(), DEFAULT_PORT_SSL); |
|
|
|
use_ssl = true; |
|
|
|
m_useSsl = true; |
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
socket->connectToHost(pref->getMailNotificationSMTP(), DEFAULT_PORT); |
|
|
|
m_socket->connectToHost(pref->getMailNotificationSMTP(), DEFAULT_PORT); |
|
|
|
use_ssl = false; |
|
|
|
m_useSsl = false; |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::readyRead() |
|
|
|
void Smtp::readyRead() |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
// SMTP is line-oriented
|
|
|
|
// SMTP is line-oriented
|
|
|
|
buffer += socket->readAll(); |
|
|
|
m_buffer += m_socket->readAll(); |
|
|
|
while (true) |
|
|
|
while (true) { |
|
|
|
{ |
|
|
|
int pos = m_buffer.indexOf("\r\n"); |
|
|
|
int pos = buffer.indexOf("\r\n"); |
|
|
|
if (pos < 0) return; // Loop exit condition
|
|
|
|
if (pos < 0) return; // Loop exit condition
|
|
|
|
QByteArray line = m_buffer.left(pos); |
|
|
|
QByteArray line = buffer.left(pos); |
|
|
|
m_buffer = m_buffer.mid(pos + 2); |
|
|
|
buffer = buffer.mid(pos + 2); |
|
|
|
qDebug() << "Response line:" << line; |
|
|
|
qDebug() << "Response line:" << line; |
|
|
|
// Extract reponse code
|
|
|
|
// Extract reponse code
|
|
|
|
QByteArray code = line.left(3); |
|
|
|
QByteArray code = line.left(3); |
|
|
|
|
|
|
|
|
|
|
|
switch (m_state) { |
|
|
|
switch(state) { |
|
|
|
case Init: { |
|
|
|
case Init: { |
|
|
|
if (code[0] == '2') { |
|
|
|
if (code[0] == '2') { |
|
|
|
// The server may send a multiline greeting/INIT/220 response.
|
|
|
|
// The server may send a multiline greeting/INIT/220 response.
|
|
|
|
// We wait until it finishes.
|
|
|
|
// We wait until it finishes.
|
|
|
|
if (line[3] != ' ') |
|
|
|
if (line[3] != ' ') |
|
|
|
break; |
|
|
|
break; |
|
|
|
// Connection was successful
|
|
|
|
// Connection was successful
|
|
|
|
ehlo(); |
|
|
|
ehlo(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else { |
|
|
|
logError("Connection failed, unrecognized reply: "+line); |
|
|
|
logError("Connection failed, unrecognized reply: "+line); |
|
|
|
state = Close; |
|
|
|
m_state = Close; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
case EhloSent: |
|
|
|
case EhloSent: |
|
|
|
case HeloSent: |
|
|
|
case HeloSent: |
|
|
|
case EhloGreetReceived: |
|
|
|
case EhloGreetReceived: |
|
|
|
parseEhloResponse(code, line[3] != ' ', line.mid(4)); |
|
|
|
parseEhloResponse(code, line[3] != ' ', line.mid(4)); |
|
|
|
break; |
|
|
|
break; |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
case StartTLSSent: |
|
|
|
case StartTLSSent: |
|
|
|
if (code == "220") { |
|
|
|
if (code == "220") { |
|
|
|
socket->startClientEncryption(); |
|
|
|
m_socket->startClientEncryption(); |
|
|
|
ehlo(); |
|
|
|
ehlo(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
authenticate(); |
|
|
|
else { |
|
|
|
} |
|
|
|
authenticate(); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
case AuthRequestSent: |
|
|
|
case AuthRequestSent: |
|
|
|
case AuthUsernameSent: |
|
|
|
case AuthUsernameSent: |
|
|
|
if (authType == AuthPlain) authPlain(); |
|
|
|
if (m_authType == AuthPlain) authPlain(); |
|
|
|
else if (authType == AuthLogin) authLogin(); |
|
|
|
else if (m_authType == AuthLogin) authLogin(); |
|
|
|
else authCramMD5(line.mid(4)); |
|
|
|
else authCramMD5(line.mid(4)); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case AuthSent: |
|
|
|
case AuthSent: |
|
|
|
case Authenticated: |
|
|
|
case Authenticated: |
|
|
|
if (code[0] == '2') { |
|
|
|
if (code[0] == '2') { |
|
|
|
qDebug() << "Sending <mail from>..."; |
|
|
|
qDebug() << "Sending <mail from>..."; |
|
|
|
socket->write("mail from:<" + from.toLatin1() + ">\r\n"); |
|
|
|
m_socket->write("mail from:<" + m_from.toLatin1() + ">\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = Rcpt; |
|
|
|
m_state = Rcpt; |
|
|
|
} else { |
|
|
|
} |
|
|
|
// Authentication failed!
|
|
|
|
else { |
|
|
|
logError("Authentication failed, msg: "+line); |
|
|
|
// Authentication failed!
|
|
|
|
state = Close; |
|
|
|
logError("Authentication failed, msg: "+line); |
|
|
|
} |
|
|
|
m_state = Close; |
|
|
|
break; |
|
|
|
} |
|
|
|
case Rcpt: |
|
|
|
break; |
|
|
|
if (code[0] == '2') { |
|
|
|
case Rcpt: |
|
|
|
socket->write("rcpt to:<" + rcpt.toLatin1() + ">\r\n"); |
|
|
|
if (code[0] == '2') { |
|
|
|
socket->flush(); |
|
|
|
m_socket->write("rcpt to:<" + m_rcpt.toLatin1() + ">\r\n"); |
|
|
|
state = Data; |
|
|
|
m_socket->flush(); |
|
|
|
} else { |
|
|
|
m_state = Data; |
|
|
|
logError("<mail from> was rejected by server, msg: "+line); |
|
|
|
} |
|
|
|
state = Close; |
|
|
|
else { |
|
|
|
} |
|
|
|
logError("<mail from> was rejected by server, msg: "+line); |
|
|
|
break; |
|
|
|
m_state = Close; |
|
|
|
case Data: |
|
|
|
} |
|
|
|
if (code[0] == '2') { |
|
|
|
break; |
|
|
|
socket->write("data\r\n"); |
|
|
|
case Data: |
|
|
|
socket->flush(); |
|
|
|
if (code[0] == '2') { |
|
|
|
state = Body; |
|
|
|
m_socket->write("data\r\n"); |
|
|
|
} else { |
|
|
|
m_socket->flush(); |
|
|
|
logError("<Rcpt to> was rejected by server, msg: "+line); |
|
|
|
m_state = Body; |
|
|
|
state = Close; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
break; |
|
|
|
logError("<Rcpt to> was rejected by server, msg: "+line); |
|
|
|
case Body: |
|
|
|
m_state = Close; |
|
|
|
if (code[0] == '3') { |
|
|
|
} |
|
|
|
socket->write(message + "\r\n.\r\n"); |
|
|
|
break; |
|
|
|
socket->flush(); |
|
|
|
case Body: |
|
|
|
state = Quit; |
|
|
|
if (code[0] == '3') { |
|
|
|
} else { |
|
|
|
m_socket->write(m_message + "\r\n.\r\n"); |
|
|
|
logError("<data> was rejected by server, msg: "+line); |
|
|
|
m_socket->flush(); |
|
|
|
state = Close; |
|
|
|
m_state = Quit; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
else { |
|
|
|
case Quit: |
|
|
|
logError("<data> was rejected by server, msg: "+line); |
|
|
|
if (code[0] == '2') { |
|
|
|
m_state = Close; |
|
|
|
socket->write("QUIT\r\n"); |
|
|
|
} |
|
|
|
socket->flush(); |
|
|
|
break; |
|
|
|
// here, we just close.
|
|
|
|
case Quit: |
|
|
|
state = Close; |
|
|
|
if (code[0] == '2') { |
|
|
|
} else { |
|
|
|
m_socket->write("QUIT\r\n"); |
|
|
|
logError("Message was rejected by the server, error: "+line); |
|
|
|
m_socket->flush(); |
|
|
|
state = Close; |
|
|
|
// here, we just close.
|
|
|
|
} |
|
|
|
m_state = Close; |
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
|
else { |
|
|
|
qDebug() << "Disconnecting from host"; |
|
|
|
logError("Message was rejected by the server, error: "+line); |
|
|
|
socket->disconnectFromHost(); |
|
|
|
m_state = Close; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
qDebug() << "Disconnecting from host"; |
|
|
|
|
|
|
|
m_socket->disconnectFromHost(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QByteArray Smtp::encode_mime_header(const QString& key, const QString& value, QTextCodec* latin1, const QByteArray& prefix) |
|
|
|
QByteArray Smtp::encodeMimeHeader(const QString &key, const QString &value, QTextCodec *latin1, const QByteArray &prefix) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QByteArray rv = ""; |
|
|
|
QByteArray rv = ""; |
|
|
|
QByteArray line = key.toLatin1() + ": "; |
|
|
|
QByteArray line = key.toLatin1() + ": "; |
|
|
|
if (!prefix.isEmpty()) line += prefix; |
|
|
|
if (!prefix.isEmpty()) line += prefix; |
|
|
|
if (!value.contains("=?") && latin1->canEncode(value)) { |
|
|
|
if (!value.contains("=?") && latin1->canEncode(value)) { |
|
|
|
bool firstWord = true; |
|
|
|
bool firstWord = true; |
|
|
|
foreach (const QByteArray& word, value.toLatin1().split(' ')) { |
|
|
|
foreach (const QByteArray& word, value.toLatin1().split(' ')) { |
|
|
|
if (line.size() > 78) { |
|
|
|
if (line.size() > 78) { |
|
|
|
rv = rv + line + "\r\n"; |
|
|
|
rv = rv + line + "\r\n"; |
|
|
|
line.clear(); |
|
|
|
line.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (firstWord) |
|
|
|
if (firstWord) |
|
|
|
line += word; |
|
|
|
line += word; |
|
|
|
else |
|
|
|
else |
|
|
|
line += " " + word; |
|
|
|
line += " " + word; |
|
|
|
firstWord = false; |
|
|
|
firstWord = false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
else { |
|
|
|
// The text cannot be losslessly encoded as Latin-1. Therefore, we
|
|
|
|
// The text cannot be losslessly encoded as Latin-1. Therefore, we
|
|
|
|
// must use base64 encoding.
|
|
|
|
// must use base64 encoding.
|
|
|
|
QByteArray utf8 = value.toUtf8(); |
|
|
|
QByteArray utf8 = value.toUtf8(); |
|
|
|
// Use base64 encoding
|
|
|
|
// Use base64 encoding
|
|
|
|
QByteArray base64 = utf8.toBase64(); |
|
|
|
QByteArray base64 = utf8.toBase64(); |
|
|
|
int ct = base64.length(); |
|
|
|
int ct = base64.length(); |
|
|
|
line += "=?utf-8?b?"; |
|
|
|
line += "=?utf-8?b?"; |
|
|
|
for (int i = 0; i < ct; i += 4) { |
|
|
|
for (int i = 0; i < ct; i += 4) { |
|
|
|
/*if (line.length() > 72) {
|
|
|
|
/*if (line.length() > 72) {
|
|
|
|
rv += line + "?\n\r"; |
|
|
|
rv += line + "?\n\r"; |
|
|
|
line = " =?utf-8?b?"; |
|
|
|
line = " =?utf-8?b?"; |
|
|
|
}*/ |
|
|
|
}*/ |
|
|
|
line = line + base64.mid(i, 4); |
|
|
|
line = line + base64.mid(i, 4); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
line += "?="; // end encoded-word atom
|
|
|
|
} |
|
|
|
} |
|
|
|
line += "?="; // end encoded-word atom
|
|
|
|
return rv + line + "\r\n"; |
|
|
|
} |
|
|
|
|
|
|
|
return rv + line + "\r\n"; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::ehlo() |
|
|
|
void Smtp::ehlo() |
|
|
|
{ |
|
|
|
{ |
|
|
|
QByteArray address = determineFQDN(); |
|
|
|
QByteArray address = determineFQDN(); |
|
|
|
socket->write("ehlo " + address + "\r\n"); |
|
|
|
m_socket->write("ehlo " + address + "\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = EhloSent; |
|
|
|
m_state = EhloSent; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::helo() |
|
|
|
void Smtp::helo() |
|
|
|
{ |
|
|
|
{ |
|
|
|
QByteArray address = determineFQDN(); |
|
|
|
QByteArray address = determineFQDN(); |
|
|
|
socket->write("helo " + address + "\r\n"); |
|
|
|
m_socket->write("helo " + address + "\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = HeloSent; |
|
|
|
m_state = HeloSent; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::parseEhloResponse(const QByteArray& code, bool continued, const QString& line) |
|
|
|
void Smtp::parseEhloResponse(const QByteArray &code, bool continued, const QString &line) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (code != "250") { |
|
|
|
if (code != "250") { |
|
|
|
// Error
|
|
|
|
// Error
|
|
|
|
if (state == EhloSent) { |
|
|
|
if (m_state == EhloSent) { |
|
|
|
// try to send HELO instead of EHLO
|
|
|
|
// try to send HELO instead of EHLO
|
|
|
|
qDebug() << "EHLO failed, trying HELO instead..."; |
|
|
|
qDebug() << "EHLO failed, trying HELO instead..."; |
|
|
|
helo(); |
|
|
|
helo(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
// Both EHLO and HELO failed, chances are this is NOT
|
|
|
|
else { |
|
|
|
// a SMTP server
|
|
|
|
// Both EHLO and HELO failed, chances are this is NOT
|
|
|
|
logError("Both EHLO and HELO failed, msg: "+line); |
|
|
|
// a SMTP server
|
|
|
|
state = Close; |
|
|
|
logError("Both EHLO and HELO failed, msg: "+line); |
|
|
|
|
|
|
|
m_state = Close; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
if (m_state != EhloGreetReceived) { |
|
|
|
if (state != EhloGreetReceived) { |
|
|
|
if (!continued) { |
|
|
|
if (!continued) { |
|
|
|
// greeting only, no extensions
|
|
|
|
// greeting only, no extensions
|
|
|
|
qDebug() << "No extension"; |
|
|
|
qDebug() << "No extension"; |
|
|
|
m_state = EhloDone; |
|
|
|
state = EhloDone; |
|
|
|
} |
|
|
|
} else { |
|
|
|
else { |
|
|
|
// greeting followed by extensions
|
|
|
|
// greeting followed by extensions
|
|
|
|
state = EhloGreetReceived; |
|
|
|
m_state = EhloGreetReceived; |
|
|
|
qDebug () << "EHLO greet received"; |
|
|
|
qDebug () << "EHLO greet received"; |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "Supported extension: " << line.section(' ', 0, 0).toUpper() |
|
|
|
|
|
|
|
<< line.section(' ', 1); |
|
|
|
|
|
|
|
m_extensions[line.section(' ', 0, 0).toUpper()] = line.section(' ', 1); |
|
|
|
|
|
|
|
if (!continued) |
|
|
|
|
|
|
|
m_state = EhloDone; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_state != EhloDone) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_extensions.contains("STARTTLS") && m_useSsl) { |
|
|
|
|
|
|
|
qDebug() << "STARTTLS"; |
|
|
|
|
|
|
|
startTLS(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
authenticate(); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "Supported extension: " << line.section(' ', 0, 0).toUpper() |
|
|
|
|
|
|
|
<< line.section(' ', 1); |
|
|
|
|
|
|
|
extensions[line.section(' ', 0, 0).toUpper()] = line.section(' ', 1); |
|
|
|
|
|
|
|
if (!continued) |
|
|
|
|
|
|
|
state = EhloDone; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (state != EhloDone) return; |
|
|
|
|
|
|
|
if (extensions.contains("STARTTLS") && use_ssl) { |
|
|
|
|
|
|
|
qDebug() << "STARTTLS"; |
|
|
|
|
|
|
|
startTLS(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
authenticate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::authenticate() |
|
|
|
void Smtp::authenticate() |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
if (!extensions.contains("AUTH") || |
|
|
|
if (!m_extensions.contains("AUTH") || |
|
|
|
username.isEmpty() || password.isEmpty()) { |
|
|
|
m_username.isEmpty() || m_password.isEmpty()) { |
|
|
|
// Skip authentication
|
|
|
|
// Skip authentication
|
|
|
|
qDebug() << "Skipping authentication..."; |
|
|
|
qDebug() << "Skipping authentication..."; |
|
|
|
state = Authenticated; |
|
|
|
m_state = Authenticated; |
|
|
|
// At this point the server will not send any response
|
|
|
|
// At this point the server will not send any response
|
|
|
|
// So fill the buffer with a fake one to pass the tests
|
|
|
|
// So fill the buffer with a fake one to pass the tests
|
|
|
|
// in readyRead()
|
|
|
|
// in readyRead()
|
|
|
|
buffer.push_front("250 QBT FAKE RESPONSE\r\n"); |
|
|
|
m_buffer.push_front("250 QBT FAKE RESPONSE\r\n"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
// AUTH extension is supported, check which
|
|
|
|
// AUTH extension is supported, check which
|
|
|
|
// authentication modes are supported by
|
|
|
|
// authentication modes are supported by
|
|
|
|
// the server
|
|
|
|
// the server
|
|
|
|
QStringList auth = extensions["AUTH"].toUpper().split(' ', QString::SkipEmptyParts); |
|
|
|
QStringList auth = m_extensions["AUTH"].toUpper().split(' ', QString::SkipEmptyParts); |
|
|
|
if (auth.contains("CRAM-MD5")) { |
|
|
|
if (auth.contains("CRAM-MD5")) { |
|
|
|
qDebug() << "Using CRAM-MD5 authentication..."; |
|
|
|
qDebug() << "Using CRAM-MD5 authentication..."; |
|
|
|
authCramMD5(); |
|
|
|
authCramMD5(); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (auth.contains("PLAIN")) { |
|
|
|
else if (auth.contains("PLAIN")) { |
|
|
|
qDebug() << "Using PLAIN authentication..."; |
|
|
|
qDebug() << "Using PLAIN authentication..."; |
|
|
|
authPlain(); |
|
|
|
authPlain(); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (auth.contains("LOGIN")) { |
|
|
|
else if (auth.contains("LOGIN")) { |
|
|
|
qDebug() << "Using LOGIN authentication..."; |
|
|
|
qDebug() << "Using LOGIN authentication..."; |
|
|
|
authLogin(); |
|
|
|
authLogin(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
// Skip authentication
|
|
|
|
else { |
|
|
|
logError("The SMTP server does not seem to support any of the authentications modes " |
|
|
|
// Skip authentication
|
|
|
|
"we support [CRAM-MD5|PLAIN|LOGIN], skipping authentication, " |
|
|
|
logError("The SMTP server does not seem to support any of the authentications modes " |
|
|
|
"knowing it is likely to fail... Server Auth Modes: "+auth.join("|")); |
|
|
|
"we support [CRAM-MD5|PLAIN|LOGIN], skipping authentication, " |
|
|
|
state = Authenticated; |
|
|
|
"knowing it is likely to fail... Server Auth Modes: "+auth.join("|")); |
|
|
|
// At this point the server will not send any response
|
|
|
|
m_state = Authenticated; |
|
|
|
// So fill the buffer with a fake one to pass the tests
|
|
|
|
// At this point the server will not send any response
|
|
|
|
// in readyRead()
|
|
|
|
// So fill the buffer with a fake one to pass the tests
|
|
|
|
buffer.push_front("250 QBT FAKE RESPONSE\r\n"); |
|
|
|
// in readyRead()
|
|
|
|
} |
|
|
|
m_buffer.push_front("250 QBT FAKE RESPONSE\r\n"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::startTLS() |
|
|
|
void Smtp::startTLS() |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
socket->write("starttls\r\n"); |
|
|
|
m_socket->write("starttls\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = StartTLSSent; |
|
|
|
m_state = StartTLSSent; |
|
|
|
#else |
|
|
|
#else |
|
|
|
authenticate(); |
|
|
|
authenticate(); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::authCramMD5(const QByteArray& challenge) |
|
|
|
void Smtp::authCramMD5(const QByteArray& challenge) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state != AuthRequestSent) { |
|
|
|
if (m_state != AuthRequestSent) { |
|
|
|
socket->write("auth cram-md5\r\n"); |
|
|
|
m_socket->write("auth cram-md5\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
authType = AuthCramMD5; |
|
|
|
m_authType = AuthCramMD5; |
|
|
|
state = AuthRequestSent; |
|
|
|
m_state = AuthRequestSent; |
|
|
|
} else { |
|
|
|
} |
|
|
|
QByteArray response = username.toLatin1() + ' ' |
|
|
|
else { |
|
|
|
+ hmacMD5(password.toLatin1(), QByteArray::fromBase64(challenge)).toHex(); |
|
|
|
QByteArray response = m_username.toLatin1() + ' ' |
|
|
|
socket->write(response.toBase64() + "\r\n"); |
|
|
|
+ hmacMD5(m_password.toLatin1(), QByteArray::fromBase64(challenge)).toHex(); |
|
|
|
socket->flush(); |
|
|
|
m_socket->write(response.toBase64() + "\r\n"); |
|
|
|
state = AuthSent; |
|
|
|
m_socket->flush(); |
|
|
|
} |
|
|
|
m_state = AuthSent; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::authPlain() |
|
|
|
void Smtp::authPlain() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state != AuthRequestSent) { |
|
|
|
if (m_state != AuthRequestSent) { |
|
|
|
authType = AuthPlain; |
|
|
|
m_authType = AuthPlain; |
|
|
|
// Prepare Auth string
|
|
|
|
// Prepare Auth string
|
|
|
|
QByteArray auth; |
|
|
|
QByteArray auth; |
|
|
|
auth += '\0'; |
|
|
|
auth += '\0'; |
|
|
|
auth += username.toLatin1(); |
|
|
|
auth += m_username.toLatin1(); |
|
|
|
qDebug() << "username: " << username.toLatin1(); |
|
|
|
qDebug() << "username: " << m_username.toLatin1(); |
|
|
|
auth += '\0'; |
|
|
|
auth += '\0'; |
|
|
|
auth += password.toLatin1(); |
|
|
|
auth += m_password.toLatin1(); |
|
|
|
qDebug() << "password: " << password.toLatin1(); |
|
|
|
qDebug() << "password: " << m_password.toLatin1(); |
|
|
|
// Send it
|
|
|
|
// Send it
|
|
|
|
socket->write("auth plain "+ auth.toBase64() + "\r\n"); |
|
|
|
m_socket->write("auth plain "+ auth.toBase64() + "\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = AuthSent; |
|
|
|
m_state = AuthSent; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::authLogin() |
|
|
|
void Smtp::authLogin() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state != AuthRequestSent && state != AuthUsernameSent) { |
|
|
|
if ((m_state != AuthRequestSent) && (m_state != AuthUsernameSent)) { |
|
|
|
socket->write("auth login\r\n"); |
|
|
|
m_socket->write("auth login\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
authType = AuthLogin; |
|
|
|
m_authType = AuthLogin; |
|
|
|
state = AuthRequestSent; |
|
|
|
m_state = AuthRequestSent; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (state == AuthRequestSent) { |
|
|
|
else if (m_state == AuthRequestSent) { |
|
|
|
socket->write(username.toLatin1().toBase64() + "\r\n"); |
|
|
|
m_socket->write(m_username.toLatin1().toBase64() + "\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = AuthUsernameSent; |
|
|
|
m_state = AuthUsernameSent; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
socket->write(password.toLatin1().toBase64() + "\r\n"); |
|
|
|
m_socket->write(m_password.toLatin1().toBase64() + "\r\n"); |
|
|
|
socket->flush(); |
|
|
|
m_socket->flush(); |
|
|
|
state = AuthSent; |
|
|
|
m_state = AuthSent; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Smtp::logError(const QString &msg) |
|
|
|
void Smtp::logError(const QString &msg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << "Email Notification Error:" << msg; |
|
|
|
qDebug() << "Email Notification Error:" << msg; |
|
|
|
Logger::instance()->addMessage(tr("Email Notification Error:") + " " + msg, Log::CRITICAL); |
|
|
|
Logger::instance()->addMessage(tr("Email Notification Error:") + " " + msg, Log::CRITICAL); |
|
|
|
} |
|
|
|
} |
|
|
|