Skip to content

Commit dc9fb44

Browse files
committed
added SMTP auth support contributed by TAC
1 parent 898045b commit dc9fb44

2 files changed

Lines changed: 153 additions & 6 deletions

File tree

‎Net/include/Poco/Net/SMTPClientSession.h‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//
22
// SMTPClientSession.h
33
//
4-
// $Id: //poco/svn/Net/include/Poco/Net/SMTPClientSession.h#2 $
4+
// $Id: //poco/Main/Net/include/Poco/Net/SMTPClientSession.h#4 $
55
//
66
// Library: Net
77
// Package: Mail
88
// Module: SMTPClientSession
99
//
1010
// Definition of the SMTPClientSession class.
1111
//
12-
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
12+
// Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
1313
// and Contributors.
1414
//
1515
// Permission is hereby granted, free of charge, to any person or organization
@@ -63,6 +63,13 @@ class Net_API SMTPClientSession
6363
SMTP_PORT = 25
6464
};
6565

66+
enum LoginMethod
67+
{
68+
AUTH_NONE,
69+
AUTH_CRAM_MD5,
70+
AUTH_LOGIN,
71+
};
72+
6673
SMTPClientSession(const StreamSocket& socket);
6774
/// Creates the SMTPClientSession using
6875
/// the given socket, which must be connected
@@ -94,6 +101,10 @@ class Net_API SMTPClientSession
94101
void login();
95102
/// Calls login(hostname) with the current host name.
96103

104+
void login(LoginMethod loginMethod, const std::string& username, const std::string& password);
105+
/// Logs in to the SMTP server using the given authentication method and the given
106+
/// credentials.
107+
97108
void open();
98109
/// Reads the initial response from the SMTP server.
99110
///
@@ -149,6 +160,11 @@ class Net_API SMTPClientSession
149160
static bool isTransientNegative(int status);
150161
static bool isPermanentNegative(int status);
151162

163+
void login(const std::string& hostname, std::string& response);
164+
void loginUsingCRAM_MD5(const std::string& username, const std::string& password);
165+
void loginUsingLogin(const std::string& username, const std::string& password);
166+
void loginUsingPlain(const std::string& username, const std::string& password);
167+
152168
private:
153169
DialogSocket _socket;
154170
bool _isOpen;

‎Net/src/SMTPClientSession.cpp‎

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// SMTPClientSession.cpp
33
//
4-
// $Id: //poco/svn/Net/src/SMTPClientSession.cpp#3 $
4+
// $Id: //poco/Main/Net/src/SMTPClientSession.cpp#10 $
55
//
66
// Library: Net
77
// Package: Mail
88
// Module: SMTPClientSession
99
//
10-
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
10+
// Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
1111
// and Contributors.
1212
//
1313
// Permission is hereby granted, free of charge, to any person or organization
@@ -42,8 +42,25 @@
4242
#include "Poco/Net/SocketStream.h"
4343
#include "Poco/Net/NetException.h"
4444
#include "Poco/Environment.h"
45+
#include "Poco/Net/NetworkInterface.h"
46+
#include "Poco/HMACEngine.h"
47+
#include "Poco/MD5Engine.h"
48+
#include "Poco/DigestStream.h"
49+
#include "Poco/StreamCopier.h"
50+
#include "Poco/Base64Encoder.h"
51+
#include "Poco/Base64Decoder.h"
52+
#include <sstream>
53+
#include <fstream>
54+
#include <iostream>
4555

4656

57+
using Poco::DigestEngine;
58+
using Poco::HMACEngine;
59+
using Poco::MD5Engine;
60+
using Poco::DigestOutputStream;
61+
using Poco::StreamCopier;
62+
using Poco::Base64Encoder;
63+
using Poco::Base64Decoder;
4764
using Poco::Environment;
4865

4966

@@ -89,23 +106,137 @@ Poco::Timespan SMTPClientSession::getTimeout() const
89106
}
90107

91108

92-
void SMTPClientSession::login(const std::string& hostname)
109+
void SMTPClientSession::login(const std::string& hostname, std::string& response)
93110
{
94111
open();
95-
std::string response;
96112
int status = sendCommand("EHLO", hostname, response);
97113
if (isPermanentNegative(status))
98114
status = sendCommand("HELO", hostname, response);
99115
if (!isPositiveCompletion(status)) throw SMTPException("Login failed", response);
100116
}
101117

102118

119+
void SMTPClientSession::login(const std::string& hostname)
120+
{
121+
std::string response;
122+
login(hostname, response);
123+
}
124+
125+
103126
void SMTPClientSession::login()
104127
{
105128
login(Environment::nodeName());
106129
}
107130

108131

132+
void SMTPClientSession::loginUsingCRAM_MD5(const std::string& username, const std::string& password)
133+
{
134+
int status = 0;
135+
std::string response;
136+
137+
status = sendCommand("AUTH CRAM-MD5", response);
138+
if (!isPositiveIntermediate(status)) throw SMTPException("Cannot authenticate CRAM-MD5", response);
139+
std::string challengeBase64 = response.substr(4);
140+
141+
std::istringstream istr(challengeBase64);
142+
Base64Decoder decoder(istr);
143+
std::string challenge;
144+
decoder >> challenge;
145+
146+
HMACEngine<MD5Engine> hmac(password);
147+
hmac.update(challenge);
148+
149+
const DigestEngine::Digest& digest = hmac.digest();
150+
std::string digestString(DigestEngine::digestToHex(digest));
151+
152+
std::string challengeResponse = username + " " + digestString;
153+
154+
std::ostringstream challengeResponseBase64;
155+
Base64Encoder encoder(challengeResponseBase64);
156+
encoder << challengeResponse;
157+
encoder.close();
158+
159+
status = sendCommand(challengeResponseBase64.str(), response);
160+
if (!isPositiveCompletion(status)) throw SMTPException("Login using CRAM-MD5 failed", response);
161+
}
162+
163+
164+
void SMTPClientSession::loginUsingLogin(const std::string& username, const std::string& password)
165+
{
166+
int status = 0;
167+
std::string response;
168+
169+
status = sendCommand("AUTH LOGIN", response);
170+
if (!isPositiveIntermediate(status)) throw SMTPException("Cannot authenticate LOGIN", response);
171+
172+
std::ostringstream usernameBase64;
173+
Base64Encoder usernameEncoder(usernameBase64);
174+
usernameEncoder << username;
175+
usernameEncoder.close();
176+
177+
std::ostringstream passwordBase64;
178+
Base64Encoder passwordEncoder(passwordBase64);
179+
passwordEncoder << password;
180+
passwordEncoder.close();
181+
182+
//Server request for username/password not defined could be either
183+
//S: login:
184+
//C: user_login
185+
//S: password:
186+
//C: user_password
187+
//or
188+
//S: password:
189+
//C: user_password
190+
//S: login:
191+
//C: user_login
192+
if (response == "334 VXNlcm5hbWU6") // username first (md5("Username:"))
193+
{
194+
status = sendCommand(usernameBase64.str(), response);
195+
if (!isPositiveIntermediate(status)) throw SMTPException("Login using LOGIN user name failed", response);
196+
197+
status = sendCommand(passwordBase64.str(), response);
198+
if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN password failed", response);
199+
}
200+
else if (response == "334 UGFzc3dvcmQ6") // password first (md5("Password:"))
201+
{
202+
status = sendCommand(passwordBase64.str(), response);
203+
if (!isPositiveIntermediate(status)) throw SMTPException("Login using LOGIN password failed", response);
204+
205+
status = sendCommand(usernameBase64.str(), response);
206+
if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN user name failed", response);
207+
}
208+
209+
}
210+
211+
212+
void SMTPClientSession::login(LoginMethod loginMethod, const std::string& username, const std::string& password)
213+
{
214+
std::string response;
215+
login(Environment::nodeName(), response);
216+
217+
if (loginMethod == AUTH_CRAM_MD5)
218+
{
219+
if (response.find("CRAM-MD5", 0) != std::string::npos)
220+
{
221+
loginUsingCRAM_MD5(username, password);
222+
}
223+
else throw SMTPException("The mail service does not support CRAM-MD5 authentication", response);
224+
}
225+
else if (loginMethod == AUTH_LOGIN)
226+
{
227+
if (response.find("LOGIN", 0) != std::string::npos)
228+
{
229+
loginUsingLogin(username, password);
230+
}
231+
else throw SMTPException("The mail service does not support LOGIN authentication", response);
232+
}
233+
else if (loginMethod != AUTH_NONE)
234+
{
235+
throw SMTPException("The autentication method is not supported");
236+
}
237+
}
238+
239+
109240
void SMTPClientSession::open()
110241
{
111242
if (!_isOpen)

0 commit comments

Comments
 (0)