2005-08-23 19:11:19 +00:00
|
|
|
//
|
|
|
|
// VMime library (http://www.vmime.org)
|
2013-01-10 16:30:31 +00:00
|
|
|
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
|
2005-08-23 19:11:19 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2009-09-06 12:02:10 +00:00
|
|
|
// published by the Free Software Foundation; either version 3 of
|
2005-08-23 19:11:19 +00:00
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
2007-03-27 12:42:53 +00:00
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// Linking this library statically or dynamically with other modules is making
|
|
|
|
// a combined work based on this library. Thus, the terms and conditions of
|
|
|
|
// the GNU General Public License cover the whole combination.
|
2005-08-23 19:11:19 +00:00
|
|
|
//
|
|
|
|
|
2012-11-01 17:20:06 +00:00
|
|
|
#include "vmime/config.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP
|
|
|
|
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
#include "vmime/net/smtp/SMTPTransport.hpp"
|
2006-01-07 08:46:20 +00:00
|
|
|
#include "vmime/net/smtp/SMTPResponse.hpp"
|
2013-02-12 15:58:27 +00:00
|
|
|
#include "vmime/net/smtp/SMTPCommand.hpp"
|
|
|
|
#include "vmime/net/smtp/SMTPCommandSet.hpp"
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
#include "vmime/exception.hpp"
|
2006-10-11 14:52:41 +00:00
|
|
|
#include "vmime/platform.hpp"
|
2005-08-23 19:11:19 +00:00
|
|
|
#include "vmime/mailboxList.hpp"
|
|
|
|
|
|
|
|
#include "vmime/utility/filteredStream.hpp"
|
2005-09-17 09:08:45 +00:00
|
|
|
#include "vmime/utility/stringUtils.hpp"
|
2012-04-14 11:46:05 +00:00
|
|
|
#include "vmime/utility/outputStreamSocketAdapter.hpp"
|
|
|
|
#include "vmime/utility/streamUtils.hpp"
|
2005-09-17 09:08:45 +00:00
|
|
|
|
2006-01-29 17:36:34 +00:00
|
|
|
#include "vmime/net/defaultConnectionInfos.hpp"
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
#if VMIME_HAVE_SASL_SUPPORT
|
|
|
|
#include "vmime/security/sasl/SASLContext.hpp"
|
|
|
|
#endif // VMIME_HAVE_SASL_SUPPORT
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
#if VMIME_HAVE_TLS_SUPPORT
|
|
|
|
#include "vmime/net/tls/TLSSession.hpp"
|
2006-01-29 17:36:34 +00:00
|
|
|
#include "vmime/net/tls/TLSSecuredConnectionInfos.hpp"
|
2005-10-03 21:29:04 +00:00
|
|
|
#endif // VMIME_HAVE_TLS_SUPPORT
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
// Helpers for service properties
|
|
|
|
#define GET_PROPERTY(type, prop) \
|
2005-10-03 21:29:04 +00:00
|
|
|
(getInfos().getPropertyValue <type>(getSession(), \
|
|
|
|
dynamic_cast <const SMTPServiceInfos&>(getInfos()).getProperties().prop))
|
2005-08-23 19:11:19 +00:00
|
|
|
#define HAS_PROPERTY(prop) \
|
2005-10-03 21:29:04 +00:00
|
|
|
(getInfos().hasProperty(getSession(), \
|
|
|
|
dynamic_cast <const SMTPServiceInfos&>(getInfos()).getProperties().prop))
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace vmime {
|
|
|
|
namespace net {
|
|
|
|
namespace smtp {
|
|
|
|
|
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
SMTPTransport::SMTPTransport(ref <session> sess, ref <security::authenticator> auth, const bool secured)
|
2005-08-23 19:11:19 +00:00
|
|
|
: transport(sess, getInfosInstance(), auth), m_socket(NULL),
|
2005-10-03 21:29:04 +00:00
|
|
|
m_authentified(false), m_extendedSMTP(false), m_timeoutHandler(NULL),
|
2013-02-12 15:58:27 +00:00
|
|
|
m_isSMTPS(secured), m_secured(false)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SMTPTransport::~SMTPTransport()
|
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (isConnected())
|
|
|
|
disconnect();
|
|
|
|
else if (m_socket)
|
|
|
|
internalDisconnect();
|
|
|
|
}
|
|
|
|
catch (vmime::exception&)
|
|
|
|
{
|
|
|
|
// Ignore
|
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string SMTPTransport::getProtocolName() const
|
|
|
|
{
|
|
|
|
return "smtp";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SMTPTransport::connect()
|
|
|
|
{
|
|
|
|
if (isConnected())
|
|
|
|
throw exceptions::already_connected();
|
|
|
|
|
|
|
|
const string address = GET_PROPERTY(string, PROPERTY_SERVER_ADDRESS);
|
|
|
|
const port_t port = GET_PROPERTY(port_t, PROPERTY_SERVER_PORT);
|
|
|
|
|
|
|
|
// Create the time-out handler
|
2005-10-04 18:49:59 +00:00
|
|
|
if (getTimeoutHandlerFactory())
|
|
|
|
m_timeoutHandler = getTimeoutHandlerFactory()->create();
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
// Create and connect the socket
|
2010-08-05 10:30:22 +00:00
|
|
|
m_socket = getSocketFactory()->create(m_timeoutHandler);
|
2005-10-03 21:29:04 +00:00
|
|
|
|
|
|
|
#if VMIME_HAVE_TLS_SUPPORT
|
2006-01-29 17:36:34 +00:00
|
|
|
if (m_isSMTPS) // dedicated port/SMTPS
|
2005-10-03 21:29:04 +00:00
|
|
|
{
|
|
|
|
ref <tls::TLSSession> tlsSession =
|
2012-11-03 08:27:12 +00:00
|
|
|
tls::TLSSession::create(getCertificateVerifier());
|
2005-10-03 21:29:04 +00:00
|
|
|
|
|
|
|
ref <tls::TLSSocket> tlsSocket =
|
|
|
|
tlsSession->getSocket(m_socket);
|
|
|
|
|
|
|
|
m_socket = tlsSocket;
|
2006-01-29 17:36:34 +00:00
|
|
|
|
|
|
|
m_secured = true;
|
|
|
|
m_cntInfos = vmime::create <tls::TLSSecuredConnectionInfos>(address, port, tlsSession, tlsSocket);
|
2005-10-03 21:29:04 +00:00
|
|
|
}
|
2006-01-29 17:36:34 +00:00
|
|
|
else
|
2006-02-09 21:03:16 +00:00
|
|
|
#endif // VMIME_HAVE_TLS_SUPPORT
|
2006-01-29 17:36:34 +00:00
|
|
|
{
|
|
|
|
m_cntInfos = vmime::create <defaultConnectionInfos>(address, port);
|
|
|
|
}
|
2005-10-03 21:29:04 +00:00
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
m_socket->connect(address, port);
|
|
|
|
|
|
|
|
// Connection
|
|
|
|
//
|
|
|
|
// eg: C: <connection to server>
|
|
|
|
// --- S: 220 smtp.domain.com Service ready
|
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
ref <SMTPResponse> resp;
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
if ((resp = readResponse())->getCode() != 220)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
internalDisconnect();
|
2006-01-07 08:46:20 +00:00
|
|
|
throw exceptions::connection_greeting_error(resp->getText());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Identification
|
2006-10-02 19:51:27 +00:00
|
|
|
helo();
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
#if VMIME_HAVE_TLS_SUPPORT
|
|
|
|
// Setup secured connection, if requested
|
|
|
|
const bool tls = HAS_PROPERTY(PROPERTY_CONNECTION_TLS)
|
|
|
|
&& GET_PROPERTY(bool, PROPERTY_CONNECTION_TLS);
|
|
|
|
const bool tlsRequired = HAS_PROPERTY(PROPERTY_CONNECTION_TLS_REQUIRED)
|
|
|
|
&& GET_PROPERTY(bool, PROPERTY_CONNECTION_TLS_REQUIRED);
|
|
|
|
|
2006-05-05 11:13:11 +00:00
|
|
|
if (!m_isSMTPS && tls) // only if not SMTPS
|
2005-10-03 21:29:04 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
startTLS();
|
|
|
|
}
|
|
|
|
// Non-fatal error
|
|
|
|
catch (exceptions::command_error&)
|
|
|
|
{
|
|
|
|
if (tlsRequired)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TLS is not required, so don't bother
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fatal error
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2006-10-02 19:51:27 +00:00
|
|
|
|
|
|
|
// Must reissue a EHLO command [RFC-2487, 5.2]
|
|
|
|
helo();
|
2005-10-03 21:29:04 +00:00
|
|
|
}
|
|
|
|
#endif // VMIME_HAVE_TLS_SUPPORT
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
// Authentication
|
|
|
|
if (GET_PROPERTY(bool, PROPERTY_OPTIONS_NEEDAUTH))
|
2005-09-17 09:08:45 +00:00
|
|
|
authenticate();
|
2005-11-07 11:55:40 +00:00
|
|
|
else
|
|
|
|
m_authentified = true;
|
2005-09-17 09:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-02 19:51:27 +00:00
|
|
|
void SMTPTransport::helo()
|
|
|
|
{
|
|
|
|
// First, try Extended SMTP (ESMTP)
|
|
|
|
//
|
|
|
|
// eg: C: EHLO thismachine.ourdomain.com
|
|
|
|
// S: 250-smtp.theserver.com
|
2007-03-28 08:28:12 +00:00
|
|
|
// S: 250-AUTH CRAM-MD5 DIGEST-MD5
|
|
|
|
// S: 250-PIPELINING
|
|
|
|
// S: 250 SIZE 2555555555
|
2006-10-02 19:51:27 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::EHLO(platform::getHandler()->getHostName()));
|
2006-10-02 19:51:27 +00:00
|
|
|
|
|
|
|
ref <SMTPResponse> resp;
|
|
|
|
|
|
|
|
if ((resp = readResponse())->getCode() != 250)
|
|
|
|
{
|
|
|
|
// Next, try "Basic" SMTP
|
|
|
|
//
|
|
|
|
// eg: C: HELO thismachine.ourdomain.com
|
|
|
|
// S: 250 OK
|
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::HELO(platform::getHandler()->getHostName()));
|
2006-10-02 19:51:27 +00:00
|
|
|
|
|
|
|
if ((resp = readResponse())->getCode() != 250)
|
|
|
|
{
|
|
|
|
internalDisconnect();
|
|
|
|
throw exceptions::connection_greeting_error(resp->getLastLine().getText());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_extendedSMTP = false;
|
2007-03-28 08:28:12 +00:00
|
|
|
m_extensions.clear();
|
2006-10-02 19:51:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_extendedSMTP = true;
|
2007-03-28 08:28:12 +00:00
|
|
|
m_extensions.clear();
|
|
|
|
|
|
|
|
// Get supported extensions from SMTP response
|
2007-04-24 09:06:41 +00:00
|
|
|
// One extension per line, format is: EXT PARAM1 PARAM2...
|
2012-12-12 15:35:55 +00:00
|
|
|
for (size_t i = 1, n = resp->getLineCount() ; i < n ; ++i)
|
2007-03-28 08:28:12 +00:00
|
|
|
{
|
|
|
|
const string line = resp->getLineAt(i).getText();
|
|
|
|
std::istringstream iss(line);
|
|
|
|
|
|
|
|
string ext;
|
|
|
|
iss >> ext;
|
|
|
|
|
2007-04-24 09:06:41 +00:00
|
|
|
std::vector <string> params;
|
|
|
|
string param;
|
|
|
|
|
|
|
|
// Special case: some servers send "AUTH=MECH [MECH MECH...]"
|
|
|
|
if (ext.length() >= 5 && utility::stringUtils::toUpper(ext.substr(0, 5)) == "AUTH=")
|
2007-03-28 08:28:12 +00:00
|
|
|
{
|
2007-04-24 09:06:41 +00:00
|
|
|
params.push_back(utility::stringUtils::toUpper(ext.substr(5)));
|
|
|
|
ext = "AUTH";
|
2007-03-28 08:28:12 +00:00
|
|
|
}
|
|
|
|
|
2007-04-24 09:06:41 +00:00
|
|
|
while (iss >> param)
|
|
|
|
params.push_back(utility::stringUtils::toUpper(param));
|
2007-03-28 08:28:12 +00:00
|
|
|
|
2007-04-24 09:06:41 +00:00
|
|
|
m_extensions[ext] = params;
|
2007-03-28 08:28:12 +00:00
|
|
|
}
|
2006-10-02 19:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
void SMTPTransport::authenticate()
|
|
|
|
{
|
|
|
|
if (!m_extendedSMTP)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
internalDisconnect();
|
|
|
|
throw exceptions::command_error("AUTH", "ESMTP not supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
getAuthenticator()->setService(thisRef().dynamicCast <service>());
|
|
|
|
|
|
|
|
#if VMIME_HAVE_SASL_SUPPORT
|
|
|
|
// First, try SASL authentication
|
|
|
|
if (GET_PROPERTY(bool, PROPERTY_OPTIONS_SASL))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
authenticateSASL();
|
|
|
|
|
|
|
|
m_authentified = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (exceptions::authentication_error& e)
|
|
|
|
{
|
|
|
|
if (!GET_PROPERTY(bool, PROPERTY_OPTIONS_SASL_FALLBACK))
|
|
|
|
{
|
|
|
|
// Can't fallback on normal authentication
|
|
|
|
internalDisconnect();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ignore, will try normal authentication
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (exception& e)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
internalDisconnect();
|
2005-09-17 09:08:45 +00:00
|
|
|
throw e;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
2005-09-17 09:08:45 +00:00
|
|
|
}
|
|
|
|
#endif // VMIME_HAVE_SASL_SUPPORT
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
// No other authentication method is possible
|
|
|
|
throw exceptions::authentication_error("All authentication methods failed");
|
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
#if VMIME_HAVE_SASL_SUPPORT
|
|
|
|
|
|
|
|
void SMTPTransport::authenticateSASL()
|
|
|
|
{
|
|
|
|
if (!getAuthenticator().dynamicCast <security::sasl::SASLAuthenticator>())
|
|
|
|
throw exceptions::authentication_error("No SASL authenticator available.");
|
|
|
|
|
2007-03-28 08:28:12 +00:00
|
|
|
// Obtain SASL mechanisms supported by server from ESMTP extensions
|
|
|
|
const std::vector <string> saslMechs =
|
|
|
|
(m_extensions.find("AUTH") != m_extensions.end())
|
|
|
|
? m_extensions["AUTH"] : std::vector <string>();
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
if (saslMechs.empty())
|
|
|
|
throw exceptions::authentication_error("No SASL mechanism available.");
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
std::vector <ref <security::sasl::SASLMechanism> > mechList;
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
ref <security::sasl::SASLContext> saslContext =
|
|
|
|
vmime::create <security::sasl::SASLContext>();
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
for (unsigned int i = 0 ; i < saslMechs.size() ; ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
mechList.push_back
|
|
|
|
(saslContext->createMechanism(saslMechs[i]));
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_mechanism&)
|
|
|
|
{
|
|
|
|
// Ignore mechanism
|
|
|
|
}
|
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
if (mechList.empty())
|
|
|
|
throw exceptions::authentication_error("No SASL mechanism available.");
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
// Try to suggest a mechanism among all those supported
|
|
|
|
ref <security::sasl::SASLMechanism> suggestedMech =
|
|
|
|
saslContext->suggestMechanism(mechList);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
if (!suggestedMech)
|
|
|
|
throw exceptions::authentication_error("Unable to suggest SASL mechanism.");
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
// Allow application to choose which mechanisms to use
|
|
|
|
mechList = getAuthenticator().dynamicCast <security::sasl::SASLAuthenticator>()->
|
|
|
|
getAcceptableMechanisms(mechList, suggestedMech);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
if (mechList.empty())
|
|
|
|
throw exceptions::authentication_error("No SASL mechanism available.");
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
// Try each mechanism in the list in turn
|
|
|
|
for (unsigned int i = 0 ; i < mechList.size() ; ++i)
|
|
|
|
{
|
|
|
|
ref <security::sasl::SASLMechanism> mech = mechList[i];
|
|
|
|
|
|
|
|
ref <security::sasl::SASLSession> saslSession =
|
|
|
|
saslContext->createSession("smtp", getAuthenticator(), mech);
|
|
|
|
|
|
|
|
saslSession->init();
|
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::AUTH(mech->getName()));
|
2005-09-17 09:08:45 +00:00
|
|
|
|
|
|
|
for (bool cont = true ; cont ; )
|
|
|
|
{
|
2006-01-07 08:46:20 +00:00
|
|
|
ref <SMTPResponse> response = readResponse();
|
2005-09-17 09:08:45 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
switch (response->getCode())
|
2005-09-17 09:08:45 +00:00
|
|
|
{
|
|
|
|
case 235:
|
|
|
|
{
|
|
|
|
m_socket = saslSession->getSecuredSocket(m_socket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case 334:
|
|
|
|
{
|
2006-04-18 19:04:30 +00:00
|
|
|
byte_t* challenge = 0;
|
2012-11-29 21:33:04 +00:00
|
|
|
long challengeLen = 0;
|
2005-09-17 09:08:45 +00:00
|
|
|
|
2006-04-18 19:04:30 +00:00
|
|
|
byte_t* resp = 0;
|
2012-11-29 21:33:04 +00:00
|
|
|
long respLen = 0;
|
2005-09-17 09:08:45 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Extract challenge
|
2006-01-07 08:46:20 +00:00
|
|
|
saslContext->decodeB64(response->getText(), &challenge, &challengeLen);
|
2005-09-17 09:08:45 +00:00
|
|
|
|
|
|
|
// Prepare response
|
|
|
|
saslSession->evaluateChallenge
|
|
|
|
(challenge, challengeLen, &resp, &respLen);
|
|
|
|
|
|
|
|
// Send response
|
2013-02-12 15:58:27 +00:00
|
|
|
m_socket->send(saslContext->encodeB64(resp, respLen) + "\r\n");
|
2005-09-17 09:08:45 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::sasl_exception& e)
|
|
|
|
{
|
|
|
|
if (challenge)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
delete [] challenge;
|
|
|
|
challenge = NULL;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
2005-09-17 09:08:45 +00:00
|
|
|
|
|
|
|
if (resp)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
delete [] resp;
|
|
|
|
resp = NULL;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
2005-09-17 09:08:45 +00:00
|
|
|
|
|
|
|
// Cancel SASL exchange
|
2013-02-12 15:58:27 +00:00
|
|
|
m_socket->sendRaw("*\r\n", 3);
|
2005-09-17 09:08:45 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (challenge)
|
|
|
|
delete [] challenge;
|
|
|
|
|
|
|
|
if (resp)
|
|
|
|
delete [] resp;
|
|
|
|
|
|
|
|
throw;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
if (challenge)
|
|
|
|
delete [] challenge;
|
|
|
|
|
|
|
|
if (resp)
|
|
|
|
delete [] resp;
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-09-17 09:08:45 +00:00
|
|
|
default:
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
cont = false;
|
|
|
|
break;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
throw exceptions::authentication_error
|
|
|
|
("Could not authenticate using SASL: all mechanisms failed.");
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
#endif // VMIME_HAVE_SASL_SUPPORT
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
#if VMIME_HAVE_TLS_SUPPORT
|
|
|
|
|
|
|
|
void SMTPTransport::startTLS()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::STARTTLS());
|
2005-10-03 21:29:04 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
ref <SMTPResponse> resp = readResponse();
|
2005-10-03 21:29:04 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
if (resp->getCode() != 220)
|
|
|
|
throw exceptions::command_error("STARTTLS", resp->getText());
|
2005-10-03 21:29:04 +00:00
|
|
|
|
|
|
|
ref <tls::TLSSession> tlsSession =
|
2012-11-03 08:27:12 +00:00
|
|
|
tls::TLSSession::create(getCertificateVerifier());
|
2005-10-03 21:29:04 +00:00
|
|
|
|
|
|
|
ref <tls::TLSSocket> tlsSocket =
|
|
|
|
tlsSession->getSocket(m_socket);
|
|
|
|
|
|
|
|
tlsSocket->handshake(m_timeoutHandler);
|
|
|
|
|
|
|
|
m_socket = tlsSocket;
|
2006-01-29 17:36:34 +00:00
|
|
|
|
|
|
|
m_secured = true;
|
|
|
|
m_cntInfos = vmime::create <tls::TLSSecuredConnectionInfos>
|
|
|
|
(m_cntInfos->getHost(), m_cntInfos->getPort(), tlsSession, tlsSocket);
|
2005-10-03 21:29:04 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::command_error&)
|
|
|
|
{
|
|
|
|
// Non-fatal error
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (exception&)
|
|
|
|
{
|
|
|
|
// Fatal error
|
|
|
|
internalDisconnect();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // VMIME_HAVE_TLS_SUPPORT
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool SMTPTransport::isConnected() const
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
return (m_socket && m_socket->isConnected() && m_authentified);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool SMTPTransport::isSecuredConnection() const
|
2006-01-29 17:36:34 +00:00
|
|
|
{
|
|
|
|
return m_secured;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <connectionInfos> SMTPTransport::getConnectionInfos() const
|
|
|
|
{
|
|
|
|
return m_cntInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
void SMTPTransport::disconnect()
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::not_connected();
|
|
|
|
|
|
|
|
internalDisconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SMTPTransport::internalDisconnect()
|
|
|
|
{
|
2005-11-30 12:12:01 +00:00
|
|
|
try
|
|
|
|
{
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::QUIT());
|
2010-12-08 08:52:54 +00:00
|
|
|
readResponse();
|
2005-11-30 12:12:01 +00:00
|
|
|
}
|
|
|
|
catch (exception&)
|
|
|
|
{
|
|
|
|
// Not important
|
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
m_socket->disconnect();
|
|
|
|
m_socket = NULL;
|
|
|
|
|
|
|
|
m_timeoutHandler = NULL;
|
|
|
|
|
|
|
|
m_authentified = false;
|
|
|
|
m_extendedSMTP = false;
|
2006-01-29 17:36:34 +00:00
|
|
|
|
|
|
|
m_secured = false;
|
|
|
|
m_cntInfos = NULL;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SMTPTransport::noop()
|
|
|
|
{
|
2006-02-24 17:57:17 +00:00
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::not_connected();
|
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
sendRequest(SMTPCommand::NOOP());
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
ref <SMTPResponse> resp = readResponse();
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
if (resp->getCode() != 250)
|
|
|
|
throw exceptions::command_error("NOOP", resp->getText());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SMTPTransport::send(const mailbox& expeditor, const mailboxList& recipients,
|
|
|
|
utility::inputStream& is, const utility::stream::size_type size,
|
2005-10-06 11:08:56 +00:00
|
|
|
utility::progressListener* progress)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2006-02-24 17:57:17 +00:00
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::not_connected();
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
// If no recipient/expeditor was found, throw an exception
|
|
|
|
if (recipients.isEmpty())
|
|
|
|
throw exceptions::no_recipient();
|
|
|
|
else if (expeditor.isEmpty())
|
|
|
|
throw exceptions::no_expeditor();
|
|
|
|
|
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
const bool hasPipelining =
|
|
|
|
m_extensions.find("PIPELINING") != m_extensions.end();
|
2012-11-11 20:55:44 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
ref <SMTPResponse> resp;
|
|
|
|
ref <SMTPCommandSet> commands = SMTPCommandSet::create(hasPipelining);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
// Emit the "MAIL" command
|
|
|
|
commands->addCommand(SMTPCommand::MAIL(expeditor));
|
2012-11-11 20:55:44 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
// Emit a "RCPT TO" command for each recipient
|
|
|
|
for (size_t i = 0 ; i < recipients.getMailboxCount() ; ++i)
|
|
|
|
{
|
|
|
|
const mailbox& mbox = *recipients.getMailboxAt(i);
|
|
|
|
commands->addCommand(SMTPCommand::RCPT(mbox));
|
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
// Prepare sending of message data
|
|
|
|
commands->addCommand(SMTPCommand::DATA());
|
2012-11-11 20:55:44 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
// Read response for "MAIL" command
|
|
|
|
commands->writeToSocket(m_socket);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
if ((resp = readResponse())->getCode() != 250)
|
|
|
|
{
|
|
|
|
internalDisconnect();
|
|
|
|
throw exceptions::command_error(commands->getLastCommandSent()->getText(), resp->getText());
|
2012-11-11 20:55:44 +00:00
|
|
|
}
|
2013-02-12 15:58:27 +00:00
|
|
|
|
|
|
|
// Read responses for "RCPT TO" commands
|
|
|
|
for (size_t i = 0 ; i < recipients.getMailboxCount() ; ++i)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2013-02-12 15:58:27 +00:00
|
|
|
commands->writeToSocket(m_socket);
|
2012-11-11 20:55:44 +00:00
|
|
|
|
|
|
|
if ((resp = readResponse())->getCode() != 250)
|
|
|
|
{
|
|
|
|
internalDisconnect();
|
2013-02-12 15:58:27 +00:00
|
|
|
throw exceptions::command_error(commands->getLastCommandSent()->getText(), resp->getText());
|
2012-11-11 20:55:44 +00:00
|
|
|
}
|
2013-02-12 15:58:27 +00:00
|
|
|
}
|
2012-11-11 20:55:44 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
// Read response for "DATA" command
|
|
|
|
commands->writeToSocket(m_socket);
|
2012-11-11 20:55:44 +00:00
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
if ((resp = readResponse())->getCode() != 354)
|
|
|
|
{
|
|
|
|
internalDisconnect();
|
|
|
|
throw exceptions::command_error(commands->getLastCommandSent()->getText(), resp->getText());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
2012-11-11 20:55:44 +00:00
|
|
|
// Send the message data
|
2005-08-23 19:11:19 +00:00
|
|
|
// Stream copy with "\n." to "\n.." transformation
|
|
|
|
utility::outputStreamSocketAdapter sos(*m_socket);
|
|
|
|
utility::dotFilteredOutputStream fos(sos);
|
|
|
|
|
|
|
|
utility::bufferedStreamCopy(is, fos, size, progress);
|
|
|
|
|
2005-10-20 16:56:04 +00:00
|
|
|
fos.flush();
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
// Send end-of-data delimiter
|
|
|
|
m_socket->sendRaw("\r\n.\r\n", 5);
|
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
if ((resp = readResponse())->getCode() != 250)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
internalDisconnect();
|
2006-01-07 08:46:20 +00:00
|
|
|
throw exceptions::command_error("DATA", resp->getText());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-12 15:58:27 +00:00
|
|
|
void SMTPTransport::sendRequest(ref <SMTPCommand> cmd)
|
2012-11-11 20:55:44 +00:00
|
|
|
{
|
2013-02-12 15:58:27 +00:00
|
|
|
cmd->writeToSocket(m_socket);
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-07 08:46:20 +00:00
|
|
|
ref <SMTPResponse> SMTPTransport::readResponse()
|
2005-09-17 09:08:45 +00:00
|
|
|
{
|
2012-11-11 20:55:44 +00:00
|
|
|
ref <SMTPResponse> resp = SMTPResponse::readResponse
|
|
|
|
(m_socket, m_timeoutHandler, m_responseState);
|
|
|
|
|
|
|
|
m_responseState = resp->getCurrentState();
|
|
|
|
|
|
|
|
return resp;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Service infos
|
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
SMTPServiceInfos SMTPTransport::sm_infos(false);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
const serviceInfos& SMTPTransport::getInfosInstance()
|
|
|
|
{
|
2005-10-03 21:29:04 +00:00
|
|
|
return sm_infos;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const serviceInfos& SMTPTransport::getInfos() const
|
|
|
|
{
|
2005-10-03 21:29:04 +00:00
|
|
|
return sm_infos;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // smtp
|
|
|
|
} // net
|
|
|
|
} // vmime
|
2012-11-01 17:20:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP
|
|
|
|
|