mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-16 15:01:15 +02:00
added SMTP auth support contributed by TAC
This commit is contained in:
parent
898045b4eb
commit
dc9fb44829
@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// SMTPClientSession.h
|
// SMTPClientSession.h
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Net/include/Poco/Net/SMTPClientSession.h#2 $
|
// $Id: //poco/Main/Net/include/Poco/Net/SMTPClientSession.h#4 $
|
||||||
//
|
//
|
||||||
// Library: Net
|
// Library: Net
|
||||||
// Package: Mail
|
// Package: Mail
|
||||||
@ -9,7 +9,7 @@
|
|||||||
//
|
//
|
||||||
// Definition of the SMTPClientSession class.
|
// Definition of the SMTPClientSession class.
|
||||||
//
|
//
|
||||||
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
|
// Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
|
||||||
// and Contributors.
|
// and Contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person or organization
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
@ -63,6 +63,13 @@ public:
|
|||||||
SMTP_PORT = 25
|
SMTP_PORT = 25
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum LoginMethod
|
||||||
|
{
|
||||||
|
AUTH_NONE,
|
||||||
|
AUTH_CRAM_MD5,
|
||||||
|
AUTH_LOGIN,
|
||||||
|
};
|
||||||
|
|
||||||
SMTPClientSession(const StreamSocket& socket);
|
SMTPClientSession(const StreamSocket& socket);
|
||||||
/// Creates the SMTPClientSession using
|
/// Creates the SMTPClientSession using
|
||||||
/// the given socket, which must be connected
|
/// the given socket, which must be connected
|
||||||
@ -94,6 +101,10 @@ public:
|
|||||||
void login();
|
void login();
|
||||||
/// Calls login(hostname) with the current host name.
|
/// Calls login(hostname) with the current host name.
|
||||||
|
|
||||||
|
void login(LoginMethod loginMethod, const std::string& username, const std::string& password);
|
||||||
|
/// Logs in to the SMTP server using the given authentication method and the given
|
||||||
|
/// credentials.
|
||||||
|
|
||||||
void open();
|
void open();
|
||||||
/// Reads the initial response from the SMTP server.
|
/// Reads the initial response from the SMTP server.
|
||||||
///
|
///
|
||||||
@ -149,6 +160,11 @@ protected:
|
|||||||
static bool isTransientNegative(int status);
|
static bool isTransientNegative(int status);
|
||||||
static bool isPermanentNegative(int status);
|
static bool isPermanentNegative(int status);
|
||||||
|
|
||||||
|
void login(const std::string& hostname, std::string& response);
|
||||||
|
void loginUsingCRAM_MD5(const std::string& username, const std::string& password);
|
||||||
|
void loginUsingLogin(const std::string& username, const std::string& password);
|
||||||
|
void loginUsingPlain(const std::string& username, const std::string& password);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DialogSocket _socket;
|
DialogSocket _socket;
|
||||||
bool _isOpen;
|
bool _isOpen;
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
//
|
//
|
||||||
// SMTPClientSession.cpp
|
// SMTPClientSession.cpp
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Net/src/SMTPClientSession.cpp#3 $
|
// $Id: //poco/Main/Net/src/SMTPClientSession.cpp#10 $
|
||||||
//
|
//
|
||||||
// Library: Net
|
// Library: Net
|
||||||
// Package: Mail
|
// Package: Mail
|
||||||
// Module: SMTPClientSession
|
// Module: SMTPClientSession
|
||||||
//
|
//
|
||||||
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
|
// Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
|
||||||
// and Contributors.
|
// and Contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person or organization
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
@ -42,8 +42,25 @@
|
|||||||
#include "Poco/Net/SocketStream.h"
|
#include "Poco/Net/SocketStream.h"
|
||||||
#include "Poco/Net/NetException.h"
|
#include "Poco/Net/NetException.h"
|
||||||
#include "Poco/Environment.h"
|
#include "Poco/Environment.h"
|
||||||
|
#include "Poco/Net/NetworkInterface.h"
|
||||||
|
#include "Poco/HMACEngine.h"
|
||||||
|
#include "Poco/MD5Engine.h"
|
||||||
|
#include "Poco/DigestStream.h"
|
||||||
|
#include "Poco/StreamCopier.h"
|
||||||
|
#include "Poco/Base64Encoder.h"
|
||||||
|
#include "Poco/Base64Decoder.h"
|
||||||
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
using Poco::DigestEngine;
|
||||||
|
using Poco::HMACEngine;
|
||||||
|
using Poco::MD5Engine;
|
||||||
|
using Poco::DigestOutputStream;
|
||||||
|
using Poco::StreamCopier;
|
||||||
|
using Poco::Base64Encoder;
|
||||||
|
using Poco::Base64Decoder;
|
||||||
using Poco::Environment;
|
using Poco::Environment;
|
||||||
|
|
||||||
|
|
||||||
@ -89,10 +106,9 @@ Poco::Timespan SMTPClientSession::getTimeout() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SMTPClientSession::login(const std::string& hostname)
|
void SMTPClientSession::login(const std::string& hostname, std::string& response)
|
||||||
{
|
{
|
||||||
open();
|
open();
|
||||||
std::string response;
|
|
||||||
int status = sendCommand("EHLO", hostname, response);
|
int status = sendCommand("EHLO", hostname, response);
|
||||||
if (isPermanentNegative(status))
|
if (isPermanentNegative(status))
|
||||||
status = sendCommand("HELO", hostname, response);
|
status = sendCommand("HELO", hostname, response);
|
||||||
@ -100,12 +116,127 @@ void SMTPClientSession::login(const std::string& hostname)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SMTPClientSession::login(const std::string& hostname)
|
||||||
|
{
|
||||||
|
std::string response;
|
||||||
|
login(hostname, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SMTPClientSession::login()
|
void SMTPClientSession::login()
|
||||||
{
|
{
|
||||||
login(Environment::nodeName());
|
login(Environment::nodeName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SMTPClientSession::loginUsingCRAM_MD5(const std::string& username, const std::string& password)
|
||||||
|
{
|
||||||
|
int status = 0;
|
||||||
|
std::string response;
|
||||||
|
|
||||||
|
status = sendCommand("AUTH CRAM-MD5", response);
|
||||||
|
if (!isPositiveIntermediate(status)) throw SMTPException("Cannot authenticate CRAM-MD5", response);
|
||||||
|
std::string challengeBase64 = response.substr(4);
|
||||||
|
|
||||||
|
std::istringstream istr(challengeBase64);
|
||||||
|
Base64Decoder decoder(istr);
|
||||||
|
std::string challenge;
|
||||||
|
decoder >> challenge;
|
||||||
|
|
||||||
|
HMACEngine<MD5Engine> hmac(password);
|
||||||
|
hmac.update(challenge);
|
||||||
|
|
||||||
|
const DigestEngine::Digest& digest = hmac.digest();
|
||||||
|
std::string digestString(DigestEngine::digestToHex(digest));
|
||||||
|
|
||||||
|
std::string challengeResponse = username + " " + digestString;
|
||||||
|
|
||||||
|
std::ostringstream challengeResponseBase64;
|
||||||
|
Base64Encoder encoder(challengeResponseBase64);
|
||||||
|
encoder << challengeResponse;
|
||||||
|
encoder.close();
|
||||||
|
|
||||||
|
status = sendCommand(challengeResponseBase64.str(), response);
|
||||||
|
if (!isPositiveCompletion(status)) throw SMTPException("Login using CRAM-MD5 failed", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SMTPClientSession::loginUsingLogin(const std::string& username, const std::string& password)
|
||||||
|
{
|
||||||
|
int status = 0;
|
||||||
|
std::string response;
|
||||||
|
|
||||||
|
status = sendCommand("AUTH LOGIN", response);
|
||||||
|
if (!isPositiveIntermediate(status)) throw SMTPException("Cannot authenticate LOGIN", response);
|
||||||
|
|
||||||
|
std::ostringstream usernameBase64;
|
||||||
|
Base64Encoder usernameEncoder(usernameBase64);
|
||||||
|
usernameEncoder << username;
|
||||||
|
usernameEncoder.close();
|
||||||
|
|
||||||
|
std::ostringstream passwordBase64;
|
||||||
|
Base64Encoder passwordEncoder(passwordBase64);
|
||||||
|
passwordEncoder << password;
|
||||||
|
passwordEncoder.close();
|
||||||
|
|
||||||
|
//Server request for username/password not defined could be either
|
||||||
|
//S: login:
|
||||||
|
//C: user_login
|
||||||
|
//S: password:
|
||||||
|
//C: user_password
|
||||||
|
//or
|
||||||
|
//S: password:
|
||||||
|
//C: user_password
|
||||||
|
//S: login:
|
||||||
|
//C: user_login
|
||||||
|
if (response == "334 VXNlcm5hbWU6") // username first (md5("Username:"))
|
||||||
|
{
|
||||||
|
status = sendCommand(usernameBase64.str(), response);
|
||||||
|
if (!isPositiveIntermediate(status)) throw SMTPException("Login using LOGIN user name failed", response);
|
||||||
|
|
||||||
|
status = sendCommand(passwordBase64.str(), response);
|
||||||
|
if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN password failed", response);
|
||||||
|
}
|
||||||
|
else if (response == "334 UGFzc3dvcmQ6") // password first (md5("Password:"))
|
||||||
|
{
|
||||||
|
status = sendCommand(passwordBase64.str(), response);
|
||||||
|
if (!isPositiveIntermediate(status)) throw SMTPException("Login using LOGIN password failed", response);
|
||||||
|
|
||||||
|
status = sendCommand(usernameBase64.str(), response);
|
||||||
|
if (!isPositiveCompletion(status)) throw SMTPException("Login using LOGIN user name failed", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SMTPClientSession::login(LoginMethod loginMethod, const std::string& username, const std::string& password)
|
||||||
|
{
|
||||||
|
std::string response;
|
||||||
|
login(Environment::nodeName(), response);
|
||||||
|
|
||||||
|
if (loginMethod == AUTH_CRAM_MD5)
|
||||||
|
{
|
||||||
|
if (response.find("CRAM-MD5", 0) != std::string::npos)
|
||||||
|
{
|
||||||
|
loginUsingCRAM_MD5(username, password);
|
||||||
|
}
|
||||||
|
else throw SMTPException("The mail service does not support CRAM-MD5 authentication", response);
|
||||||
|
}
|
||||||
|
else if (loginMethod == AUTH_LOGIN)
|
||||||
|
{
|
||||||
|
if (response.find("LOGIN", 0) != std::string::npos)
|
||||||
|
{
|
||||||
|
loginUsingLogin(username, password);
|
||||||
|
}
|
||||||
|
else throw SMTPException("The mail service does not support LOGIN authentication", response);
|
||||||
|
}
|
||||||
|
else if (loginMethod != AUTH_NONE)
|
||||||
|
{
|
||||||
|
throw SMTPException("The autentication method is not supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SMTPClientSession::open()
|
void SMTPClientSession::open()
|
||||||
{
|
{
|
||||||
if (!_isOpen)
|
if (!_isOpen)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user