aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/net/smtp/SMTPConnection.cpp
blob: 07d0376552abdad207b415a3f0b2cb51afff0c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// 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.
//
// 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.
//

#include "vmime/config.hpp"


#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP


#include "vmime/net/smtp/SMTPConnection.hpp"
#include "vmime/net/smtp/SMTPTransport.hpp"
#include "vmime/net/smtp/SMTPExceptions.hpp"

#include "vmime/exception.hpp"
#include "vmime/platform.hpp"

#include "vmime/security/digest/messageDigestFactory.hpp"

#include "vmime/net/defaultConnectionInfos.hpp"

#if VMIME_HAVE_SASL_SUPPORT
	#include "vmime/security/sasl/SASLContext.hpp"
#else
	#include "vmime/utility/encoder/b64Encoder.hpp"
	#include "vmime/utility/inputStreamStringAdapter.hpp"
	#include "vmime/utility/outputStreamStringAdapter.hpp"
#endif // VMIME_HAVE_SASL_SUPPORT

#if VMIME_HAVE_TLS_SUPPORT
	#include "vmime/net/tls/TLSSession.hpp"
	#include "vmime/net/tls/TLSSecuredConnectionInfos.hpp"
#endif // VMIME_HAVE_TLS_SUPPORT


// Helpers for service properties
#define GET_PROPERTY(type, prop) \
	(m_transport.lock()->getInfos().getPropertyValue <type>(getSession(), \
		dynamic_cast <const SMTPServiceInfos&>(m_transport.lock()->getInfos()).getProperties().prop))
#define HAS_PROPERTY(prop) \
	(m_transport.lock()->getInfos().hasProperty(getSession(), \
		dynamic_cast <const SMTPServiceInfos&>(m_transport.lock()->getInfos()).getProperties().prop))


namespace vmime {
namespace net {
namespace smtp {



SMTPConnection::SMTPConnection(
	const shared_ptr <SMTPTransport>& transport,
	const shared_ptr <security::authenticator>& auth
)
	: m_transport(transport),
	  m_auth(auth),
	  m_socket(null),
	  m_timeoutHandler(null),
	  m_authenticated(false),
	  m_secured(false),
	  m_extendedSMTP(false) {

	static int connectionId = 0;

	if (transport->getTracerFactory()) {
		m_tracer = transport->getTracerFactory()->create(transport, ++connectionId);
	}
}


SMTPConnection::~SMTPConnection() {

	try {

		if (isConnected()) {
			disconnect();
		} else if (m_socket) {
			internalDisconnect();
		}

	} catch (...) {

		// Don't throw in destructor
	}
}


void SMTPConnection::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);

	shared_ptr <SMTPTransport> transport = m_transport.lock();

	// Create the time-out handler
	if (transport->getTimeoutHandlerFactory()) {
		m_timeoutHandler = transport->getTimeoutHandlerFactory()->create();
	}

	// Create and connect the socket
	m_socket = transport->getSocketFactory()->create(m_timeoutHandler);
	m_socket->setTracer(m_tracer);

#if VMIME_HAVE_TLS_SUPPORT
	if (transport->isSMTPS()) {  // dedicated port/SMTPS

		shared_ptr <tls::TLSSession> tlsSession = tls::TLSSession::create(
			transport->getCertificateVerifier(),
			transport->getSession()->getTLSProperties()
		);

		shared_ptr <tls::TLSSocket> tlsSocket = tlsSession->getSocket(m_socket);

		m_socket = tlsSocket;

		m_secured = true;
		m_cntInfos = make_shared <tls::TLSSecuredConnectionInfos>(address, port, tlsSession, tlsSocket);

	} else
#endif // VMIME_HAVE_TLS_SUPPORT
	{
		m_cntInfos = make_shared <defaultConnectionInfos>(address, port);
	}

	m_socket->connect(address, port);

	// Connection
	//
	// eg:  C: <connection to server>
	// ---  S: 220 smtp.domain.com Service ready

	shared_ptr <SMTPResponse> resp;

	if ((resp = readResponse())->getCode() != 220) {
		internalDisconnect();
		throw exceptions::connection_greeting_error(resp->getText());
	}

	// Identification
	helo();

#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);

	if (!transport->isSMTPS() && tls) {  // only if not SMTPS

		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;
		}

		// Must reissue a EHLO command [RFC-2487, 5.2]
		helo();
	}
#endif // VMIME_HAVE_TLS_SUPPORT

	// Authentication
	if (GET_PROPERTY(bool, PROPERTY_OPTIONS_NEEDAUTH)) {
		authenticate();
	} else {
		m_authenticated = true;
	}
}


void SMTPConnection::helo() {

	// First, try Extended SMTP (ESMTP)
	//
	// eg:  C: EHLO thismachine.ourdomain.com
	//      S: 250-smtp.theserver.com
	//      S: 250-AUTH CRAM-MD5 DIGEST-MD5
	//      S: 250-PIPELINING
	//      S: 250 SIZE 2555555555

	sendRequest(SMTPCommand::EHLO(platform::getHandler()->getHostName()));

	shared_ptr <SMTPResponse> resp;

	if ((resp = readResponse())->getCode() != 250) {

		// Next, try "Basic" SMTP
		//
		// eg:  C: HELO thismachine.ourdomain.com
		//      S: 250 OK

		sendRequest(SMTPCommand::HELO(platform::getHandler()->getHostName()));

		if ((resp = readResponse())->getCode() != 250) {
			internalDisconnect();
			throw exceptions::connection_greeting_error(resp->getLastLine().getText());
		}

		m_extendedSMTP = false;
		m_extensions.clear();

	} else {

		m_extendedSMTP = true;
		m_extensions.clear();

		// Get supported extensions from SMTP response
		// One extension per line, format is: EXT PARAM1 PARAM2...
		for (size_t i = 1, n = resp->getLineCount() ; i < n ; ++i) {

			const string line = resp->getLineAt(i).getText();

			std::istringstream iss(line);
			iss.imbue(std::locale::classic());

			string ext;
			iss >> ext;

			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=") {

				params.push_back(utility::stringUtils::toUpper(ext.substr(5)));
				ext = "AUTH";
			}

			while (iss >> param) {
				params.push_back(utility::stringUtils::toUpper(param));
			}

			m_extensions[ext] = params;
		}
	}
}


bool SMTPConnection::hasExtension(
	const std::string& extName,
	std::vector <string>* params
) const {

	std::map <string, std::vector <string> >::const_iterator
		it = m_extensions.find(extName);

	if (it != m_extensions.end()) {

		if (params) {
			*params = (*it).second;
		}

		return true;

	} else {

		return false;
	}
}


void SMTPConnection::authenticate() {

	if (!m_extendedSMTP) {
		internalDisconnect();
		throw exceptions::command_error("AUTH", "ESMTP not supported.");
	}

	getAuthenticator()->setService(m_transport.lock());

#if VMIME_HAVE_SASL_SUPPORT
	// Try SASL authentication
	if (GET_PROPERTY(bool, PROPERTY_OPTIONS_SASL)) {

		try {

			authenticateSASL();

			m_authenticated = true;
			return;

		} catch (exception&) {

			internalDisconnect();
			throw;
		}
	}
#else  // no SASL

	// allow AUTH PLAIN over TLS - it is a popular and simple mechanism
	if (m_secured) {

		std::vector <string> authMechs;
		hasExtension("AUTH", &authMechs);

		if (authMechs.empty()) {
			throw exceptions::authentication_error("No AUTH mechanism available.");
		}

		const string plain("PLAIN");

		if (std::find(authMechs.begin(), authMechs.end(), plain) != authMechs.end()) {

			const string username = getAuthenticator()->getUsername();
			const string password = getAuthenticator()->getPassword();
			const string authToken = username + '\0' + username + '\0' + password;

			utility::inputStreamStringAdapter in(authToken);
			string authTokenBase64;
			utility::outputStreamStringAdapter out(authTokenBase64);

			vmime::utility::encoder::b64Encoder encoder;
			encoder.encode(in, out);

			sendRequest(SMTPCommand::AUTH(plain, authTokenBase64));

			shared_ptr <SMTPResponse> response = readResponse();

			const int code = response ? response->getCode() : -1;

			if (code == 235) {
				m_authenticated = true;
				return;
			}
		}
	}

#endif // VMIME_HAVE_SASL_SUPPORT

	// No other authentication method is possible
	throw exceptions::authentication_error("All authentication methods failed");
}



#if VMIME_HAVE_SASL_SUPPORT

void SMTPConnection::authenticateSASL() {

	if (!dynamicCast <security::sasl::SASLAuthenticator>(getAuthenticator())) {
		throw exceptions::authentication_error("No SASL authenticator available.");
	}

	// Obtain SASL mechanisms supported by server from ESMTP extensions
	std::vector <string> saslMechs;
	hasExtension("AUTH", &saslMechs);

	if (saslMechs.empty()) {
		throw exceptions::authentication_error("No SASL mechanism available.");
	}

	std::vector <shared_ptr <security::sasl::SASLMechanism> > mechList;

	shared_ptr <security::sasl::SASLContext> saslContext =
		security::sasl::SASLContext::create();

	for (unsigned int i = 0 ; i < saslMechs.size() ; ++i) {

		try {

			mechList.push_back
				(saslContext->createMechanism(saslMechs[i]));

		} catch (exceptions::no_such_mechanism&) {

			// Ignore mechanism
		}
	}

	if (mechList.empty()) {
		throw exceptions::authentication_error("No SASL mechanism available.");
	}

	// Try to suggest a mechanism among all those supported
	shared_ptr <security::sasl::SASLMechanism> suggestedMech =
		saslContext->suggestMechanism(mechList);

	if (!suggestedMech) {
		throw exceptions::authentication_error("Unable to suggest SASL mechanism.");
	}

	// Allow application to choose which mechanisms to use
	mechList = dynamicCast <security::sasl::SASLAuthenticator>(getAuthenticator())->
		getAcceptableMechanisms(mechList, suggestedMech);

	if (mechList.empty()) {
		throw exceptions::authentication_error("No SASL mechanism available.");
	}

	// Try each mechanism in the list in turn
	for (unsigned int i = 0 ; i < mechList.size() ; ++i) {

		shared_ptr <security::sasl::SASLMechanism> mech = mechList[i];

		shared_ptr <security::sasl::SASLSession> saslSession =
			saslContext->createSession("smtp", getAuthenticator(), mech);

		saslSession->init();

		if (saslSession->getMechanism()->hasInitialResponse()) {

			byte_t* initialResp = 0;
			size_t initialRespLen = 0;

			saslSession->evaluateChallenge(NULL, 0, &initialResp, &initialRespLen);

			string encodedInitialResp(saslContext->encodeB64(initialResp, initialRespLen));
			delete [] initialResp;

			if (encodedInitialResp.empty()) {
				sendRequest(SMTPCommand::AUTH(mech->getName(), "="));
			} else {
				sendRequest(SMTPCommand::AUTH(mech->getName(), encodedInitialResp));
			}

		} else {

			sendRequest(SMTPCommand::AUTH(mech->getName()));
		}

		for (bool cont = true ; cont ; ) {

			shared_ptr <SMTPResponse> response = readResponse();

			switch (response->getCode()) {

				case 235: {

					m_socket = saslSession->getSecuredSocket(m_socket);
					return;
				}
				case 334: {

					byte_t* challenge = 0;
					size_t challengeLen = 0;

					byte_t* resp = 0;
					size_t respLen = 0;

					try {

						// Extract challenge
						saslContext->decodeB64(response->getText(), &challenge, &challengeLen);

						// Prepare response
						saslSession->evaluateChallenge(challenge, challengeLen, &resp, &respLen);

						// Send response
						const string respB64 = saslContext->encodeB64(resp, respLen) + "\r\n";
						m_socket->sendRaw(utility::stringUtils::bytesFromString(respB64), respB64.length());

						if (m_tracer) {
							m_tracer->traceSendBytes(respB64.length() - 2, "SASL exchange");
						}

					} catch (exceptions::sasl_exception& e) {

						if (challenge) {
							delete [] challenge;
							challenge = NULL;
						}

						if (resp) {
							delete [] resp;
							resp = NULL;
						}

						// Cancel SASL exchange
						m_socket->send("*\r\n");

						if (m_tracer) {
							m_tracer->traceSend("*");
						}

					} catch (...) {

						if (challenge) {
							delete [] challenge;
						}

						if (resp) {
							delete [] resp;
						}

						throw;
					}

					if (challenge) {
						delete [] challenge;
					}

					if (resp) {
						delete [] resp;
					}

					break;
				}
				default:

					cont = false;
					break;
			}
		}
	}

	throw exceptions::authentication_error("Could not authenticate using SASL: all mechanisms failed.");
}

#endif // VMIME_HAVE_SASL_SUPPORT


#if VMIME_HAVE_TLS_SUPPORT

void SMTPConnection::startTLS() {

	try {

		sendRequest(SMTPCommand::STARTTLS());

		shared_ptr <SMTPResponse> resp = readResponse();

		if (resp->getCode() != 220) {

			throw SMTPCommandError(
				"STARTTLS", resp->getText(), resp->getCode(), resp->getEnhancedCode()
			);
		}

		shared_ptr <tls::TLSSession> tlsSession = tls::TLSSession::create(
			getTransport()->getCertificateVerifier(),
			getTransport()->getSession()->getTLSProperties()
		);

		shared_ptr <tls::TLSSocket> tlsSocket = tlsSession->getSocket(m_socket);

		tlsSocket->handshake();

		m_socket = tlsSocket;

		m_secured = true;
		m_cntInfos = make_shared <tls::TLSSecuredConnectionInfos>(
			m_cntInfos->getHost(), m_cntInfos->getPort(), tlsSession, tlsSocket
		);

	} catch (exceptions::command_error&) {

		// Non-fatal error
		throw;

	} catch (exception&) {

		// Fatal error
		internalDisconnect();
		throw;
	}
}

#endif // VMIME_HAVE_TLS_SUPPORT


void SMTPConnection::disconnect() {

	if (!isConnected()) {
		throw exceptions::not_connected();
	}

	internalDisconnect();
}


void SMTPConnection::internalDisconnect() {

	if (isConnected()) {

		try {

			sendRequest(SMTPCommand::QUIT());

			// Do not wait for server response. This is contrary to the RFC, but
			// some servers never send a response to a QUIT command.

		} catch (exception&) {

			// Not important
		}
	}

	m_socket->disconnect();
	m_socket = null;

	m_timeoutHandler = null;

	m_authenticated = false;
	m_extendedSMTP = false;

	m_secured = false;
	m_cntInfos = null;
}


void SMTPConnection::sendRequest(const shared_ptr <SMTPCommand>& cmd) {

	cmd->writeToSocket(m_socket, m_tracer);
}


shared_ptr <SMTPResponse> SMTPConnection::readResponse() {

	shared_ptr <SMTPResponse> resp = SMTPResponse::readResponse(
		m_tracer, m_socket, m_timeoutHandler, m_responseState
	);

	m_responseState = resp->getCurrentState();

	return resp;
}


bool SMTPConnection::isConnected() const {

	return m_socket && m_socket->isConnected() && m_authenticated;
}


bool SMTPConnection::isSecuredConnection() const {

	return m_secured;
}


shared_ptr <connectionInfos> SMTPConnection::getConnectionInfos() const {

	return m_cntInfos;
}


shared_ptr <SMTPTransport> SMTPConnection::getTransport() {

	return m_transport.lock();
}


shared_ptr <session> SMTPConnection::getSession() {

	return m_transport.lock()->getSession();
}


shared_ptr <socket> SMTPConnection::getSocket() {

	return m_socket;
}


shared_ptr <tracer> SMTPConnection::getTracer() {

	return m_tracer;
}


shared_ptr <timeoutHandler> SMTPConnection::getTimeoutHandler() {

	return m_timeoutHandler;
}


shared_ptr <security::authenticator> SMTPConnection::getAuthenticator() {

	return m_auth;
}


} // smtp
} // net
} // vmime


#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_SMTP