* Style cleanup.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42259 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -6,37 +6,40 @@
|
|||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//! implementation of the SMTP protocol
|
|
||||||
|
|
||||||
#include <DataIO.h>
|
//! Implementation of the SMTP protocol
|
||||||
#include <Message.h>
|
|
||||||
#include <Alert.h>
|
|
||||||
#include <TextControl.h>
|
|
||||||
#include <Entry.h>
|
|
||||||
#include <File.h>
|
|
||||||
#include <Path.h>
|
|
||||||
#include <MenuField.h>
|
|
||||||
|
|
||||||
#include <cctype>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
|
|
||||||
#include <ProtocolConfigView.h>
|
#include "smtp.h"
|
||||||
#include <mail_encoding.h>
|
|
||||||
#include <MailSettings.h>
|
|
||||||
#include <NodeMessage.h>
|
|
||||||
#include <crypt.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
#include "smtp.h"
|
#include <arpa/inet.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <Alert.h>
|
||||||
|
#include <DataIO.h>
|
||||||
|
#include <Entry.h>
|
||||||
|
#include <File.h>
|
||||||
|
#include <MenuField.h>
|
||||||
|
#include <Message.h>
|
||||||
|
#include <Path.h>
|
||||||
|
#include <TextControl.h>
|
||||||
|
|
||||||
|
#include <crypt.h>
|
||||||
|
#include <mail_encoding.h>
|
||||||
|
#include <MailSettings.h>
|
||||||
|
#include <NodeMessage.h>
|
||||||
|
#include <ProtocolConfigView.h>
|
||||||
|
|
||||||
#ifdef USE_SSL
|
#ifdef USE_SSL
|
||||||
# include <openssl/md5.h>
|
# include <openssl/md5.h>
|
||||||
#else
|
#else
|
||||||
@@ -65,18 +68,14 @@ using namespace std;
|
|||||||
** written by Martin Schaaf <[email protected]>
|
** written by Martin Schaaf <[email protected]>
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
MD5Hmac(unsigned char *digest,
|
MD5Hmac(unsigned char *digest, const unsigned char* text, int text_len,
|
||||||
const unsigned char* text, int text_len,
|
const unsigned char* key, int key_len)
|
||||||
const unsigned char* key, int key_len)
|
|
||||||
{
|
{
|
||||||
MD5_CTX context;
|
MD5_CTX context;
|
||||||
unsigned char k_ipad[64]; /* inner padding -
|
unsigned char k_ipad[64];
|
||||||
* key XORd with ipad
|
// inner padding - key XORd with ipad
|
||||||
*/
|
unsigned char k_opad[64];
|
||||||
unsigned char k_opad[64]; /* outer padding -
|
// outer padding - key XORd with opad
|
||||||
* key XORd with opad
|
|
||||||
*/
|
|
||||||
/* unsigned char tk[16]; */
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* start out by storing key in pads */
|
/* start out by storing key in pads */
|
||||||
@@ -106,7 +105,6 @@ MD5Hmac(unsigned char *digest,
|
|||||||
* and text is the data being protected
|
* and text is the data being protected
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/* XOR key with ipad and opad values */
|
/* XOR key with ipad and opad values */
|
||||||
for (i = 0; i < 64; i++) {
|
for (i = 0; i < 64; i++) {
|
||||||
k_ipad[i] ^= 0x36;
|
k_ipad[i] ^= 0x36;
|
||||||
@@ -134,9 +132,8 @@ MD5Hmac(unsigned char *digest,
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
MD5HexHmac(char *hexdigest,
|
MD5HexHmac(char *hexdigest, const unsigned char* text, int text_len,
|
||||||
const unsigned char* text, int text_len,
|
const unsigned char* key, int key_len)
|
||||||
const unsigned char* key, int key_len)
|
|
||||||
{
|
{
|
||||||
unsigned char digest[16];
|
unsigned char digest[16];
|
||||||
int i;
|
int i;
|
||||||
@@ -317,9 +314,7 @@ SMTPProtocol::Disconnect()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Process EMail to be sent
|
//! Process EMail to be sent
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
SMTPProtocol::SendMessages(const std::vector<entry_ref>& mails,
|
SMTPProtocol::SendMessages(const std::vector<entry_ref>& mails,
|
||||||
size_t totalBytes)
|
size_t totalBytes)
|
||||||
@@ -351,8 +346,7 @@ SMTPProtocol::SendMessages(const std::vector<entry_ref>& mails,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Opens connection to server
|
//! Opens connection to server
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
||||||
{
|
{
|
||||||
@@ -381,28 +375,20 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
|||||||
if (hostIP == 0)
|
if (hostIP == 0)
|
||||||
return EHOSTUNREACH;
|
return EHOSTUNREACH;
|
||||||
|
|
||||||
#ifndef HAIKU_TARGET_PLATFORM_BEOS
|
|
||||||
fSocket = socket(AF_INET, SOCK_STREAM, 0);
|
fSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
#else
|
if (fSocket < 0)
|
||||||
fSocket = socket(AF_INET, 2, 0);
|
return errno;
|
||||||
#endif
|
|
||||||
if (fSocket >= 0) {
|
struct sockaddr_in saAddr;
|
||||||
struct sockaddr_in saAddr;
|
memset(&saAddr, 0, sizeof(saAddr));
|
||||||
memset(&saAddr, 0, sizeof(saAddr));
|
saAddr.sin_family = AF_INET;
|
||||||
saAddr.sin_family = AF_INET;
|
saAddr.sin_port = htons(port);
|
||||||
saAddr.sin_port = htons(port);
|
saAddr.sin_addr.s_addr = hostIP;
|
||||||
saAddr.sin_addr.s_addr = hostIP;
|
int result = connect(fSocket, (struct sockaddr *)&saAddr,
|
||||||
int result = connect(fSocket, (struct sockaddr *) &saAddr, sizeof(saAddr));
|
sizeof(saAddr));
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
#ifndef HAIKU_TARGET_PLATFORM_BEOS
|
close(fSocket);
|
||||||
close(fSocket);
|
fSocket = -1;
|
||||||
#else
|
|
||||||
closesocket(fSocket);
|
|
||||||
#endif
|
|
||||||
fSocket = -1;
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -428,17 +414,12 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
|||||||
error << ". (SSL connection error)";
|
error << ". (SSL connection error)";
|
||||||
ShowError(error.String());
|
ShowError(error.String());
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
#ifndef HAIKU_TARGET_PLATFORM_BEOS
|
close(fSocket);
|
||||||
close(fSocket);
|
fSocket = -1;
|
||||||
#else
|
|
||||||
closesocket(fSocket);
|
|
||||||
#endif
|
|
||||||
fSocket = -1;
|
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif // USE_SSL
|
||||||
#endif
|
|
||||||
|
|
||||||
BString line;
|
BString line;
|
||||||
ReceiveResponse(line);
|
ReceiveResponse(line);
|
||||||
@@ -460,28 +441,23 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
|||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_SSL
|
||||||
|
|
||||||
#ifdef USE_SSL
|
|
||||||
// Check for STARTTLS
|
// Check for STARTTLS
|
||||||
if(use_STARTTLS)
|
if (use_STARTTLS) {
|
||||||
{
|
|
||||||
const char *res = fLog.String();
|
const char *res = fLog.String();
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
SSL_library_init();
|
SSL_library_init();
|
||||||
RAND_seed(this,sizeof(SMTPProtocol));
|
RAND_seed(this,sizeof(SMTPProtocol));
|
||||||
::sprintf(cmd, "STARTTLS"CRLF);
|
::sprintf(cmd, "STARTTLS"CRLF);
|
||||||
|
|
||||||
if ((p = ::strstr(res, "STARTTLS")) != NULL)
|
if ((p = ::strstr(res, "STARTTLS")) != NULL) {
|
||||||
{
|
|
||||||
// Server advertises STARTTLS support
|
// Server advertises STARTTLS support
|
||||||
if(SendCommand(cmd) != B_OK)
|
if (SendCommand(cmd) != B_OK) {
|
||||||
{
|
|
||||||
delete[] cmd;
|
delete[] cmd;
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should start TLS negotiation
|
// We should start TLS negotiation
|
||||||
use_ssl = true;
|
use_ssl = true;
|
||||||
ctx = SSL_CTX_new(TLSv1_method());
|
ctx = SSL_CTX_new(TLSv1_method());
|
||||||
@@ -492,25 +468,23 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp)
|
|||||||
SSL_set_connect_state(ssl);
|
SSL_set_connect_state(ssl);
|
||||||
if(SSL_do_handshake(ssl) != 1)
|
if(SSL_do_handshake(ssl) != 1)
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
|
|
||||||
// Should send EHLO command again
|
// Should send EHLO command again
|
||||||
if(!esmtp)
|
if(!esmtp)
|
||||||
::sprintf(cmd, "HELO %s"CRLF, localhost);
|
::sprintf(cmd, "HELO %s"CRLF, localhost);
|
||||||
else
|
else
|
||||||
::sprintf(cmd, "EHLO %s"CRLF, localhost);
|
::sprintf(cmd, "EHLO %s"CRLF, localhost);
|
||||||
|
|
||||||
if(SendCommand(cmd) != B_OK)
|
if (SendCommand(cmd) != B_OK) {
|
||||||
{
|
|
||||||
delete[] cmd;
|
delete[] cmd;
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif // USE_SSL
|
||||||
|
|
||||||
delete[] cmd;
|
delete[] cmd;
|
||||||
|
|
||||||
// Check auth type
|
// Check auth type
|
||||||
if (esmtp) {
|
if (esmtp) {
|
||||||
const char *res = fLog.String();
|
const char *res = fLog.String();
|
||||||
@@ -805,8 +779,6 @@ SMTPProtocol::Close()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Send mail */
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message)
|
SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message)
|
||||||
{
|
{
|
||||||
@@ -845,14 +817,14 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message)
|
|||||||
// SMTP server will remove the periods. Of course, the POP server may then
|
// SMTP server will remove the periods. Of course, the POP server may then
|
||||||
// add some of its own, but the POP client should take care of them.
|
// add some of its own, but the POP client should take care of them.
|
||||||
|
|
||||||
ssize_t amountRead;
|
ssize_t amountRead;
|
||||||
ssize_t amountToRead;
|
ssize_t amountToRead;
|
||||||
ssize_t amountUnread;
|
ssize_t amountUnread;
|
||||||
ssize_t bufferLen = 0;
|
ssize_t bufferLen = 0;
|
||||||
const int bufferMax = 2000;
|
const int bufferMax = 2000;
|
||||||
bool foundCRLFPeriod;
|
bool foundCRLFPeriod;
|
||||||
int i;
|
int i;
|
||||||
bool messageEndedWithCRLF = false;
|
bool messageEndedWithCRLF = false;
|
||||||
|
|
||||||
message->Seek(0, SEEK_END);
|
message->Seek(0, SEEK_END);
|
||||||
amountUnread = message->Position();
|
amountUnread = message->Position();
|
||||||
@@ -883,15 +855,15 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message)
|
|||||||
if (data[i] == '\r' && data[i+1] == '\n' && data[i+2] == '.') {
|
if (data[i] == '\r' && data[i+1] == '\n' && data[i+2] == '.') {
|
||||||
foundCRLFPeriod = true;
|
foundCRLFPeriod = true;
|
||||||
// Send data up to the CRLF, and include the period too.
|
// Send data up to the CRLF, and include the period too.
|
||||||
#ifdef USE_SSL
|
#ifdef USE_SSL
|
||||||
if (use_ssl) {
|
if (use_ssl) {
|
||||||
if (SSL_write(ssl,data,i + 3) < 0) {
|
if (SSL_write(ssl,data,i + 3) < 0) {
|
||||||
amountUnread = 0; // Stop when an error happens.
|
amountUnread = 0; // Stop when an error happens.
|
||||||
bufferLen = 0;
|
bufferLen = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
if (send (fSocket,data, i + 3,0) < 0) {
|
if (send (fSocket,data, i + 3,0) < 0) {
|
||||||
amountUnread = 0; // Stop when an error happens.
|
amountUnread = 0; // Stop when an error happens.
|
||||||
bufferLen = 0;
|
bufferLen = 0;
|
||||||
@@ -954,8 +926,7 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Receives response from server.
|
//! Receives response from server.
|
||||||
|
|
||||||
int32
|
int32
|
||||||
SMTPProtocol::ReceiveResponse(BString &out)
|
SMTPProtocol::ReceiveResponse(BString &out)
|
||||||
{
|
{
|
||||||
@@ -1064,7 +1035,9 @@ SMTPProtocol::SendCommand(const char *cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Instantiate hook
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
OutboundProtocol*
|
OutboundProtocol*
|
||||||
instantiate_outbound_protocol(BMailAccountSettings* settings)
|
instantiate_outbound_protocol(BMailAccountSettings* settings)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user