2015-12-02 21:49:51 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
2016-10-23 22:04:55 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-12-02 21:49:51 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
2016-10-23 22:04:55 +02:00
|
|
|
#include <unistd.h>
|
2015-12-02 21:49:51 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <piler.h>
|
2016-10-23 22:04:55 +02:00
|
|
|
#include "smtp.h"
|
2015-12-02 21:49:51 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_smtp_command(struct smtp_session *session, char *buf){
|
|
|
|
char response[SMALLBUFSIZE];
|
2016-08-17 21:47:19 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "processing command: *%s*", buf);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_HELO, strlen(SMTP_CMD_HELO)) == 0){
|
|
|
|
process_command_helo(session, response, sizeof(response));
|
|
|
|
return;
|
|
|
|
}
|
2016-08-17 21:47:19 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_EHLO, strlen(SMTP_CMD_EHLO)) == 0 ||
|
|
|
|
strncasecmp(buf, LMTP_CMD_LHLO, strlen(LMTP_CMD_LHLO)) == 0){
|
|
|
|
process_command_ehlo_lhlo(session, response, sizeof(response));
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_MAIL_FROM, strlen(SMTP_CMD_MAIL_FROM)) == 0){
|
|
|
|
process_command_mail_from(session, buf);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_RCPT_TO, strlen(SMTP_CMD_RCPT_TO)) == 0){
|
|
|
|
process_command_rcpt_to(session, buf);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_DATA, strlen(SMTP_CMD_DATA)) == 0){
|
|
|
|
process_command_data(session);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->enable_chunking == 1 && strncasecmp(buf, SMTP_CMD_BDAT, strlen(SMTP_CMD_BDAT)) == 0){
|
|
|
|
get_bdat_size_to_read(session, buf);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_QUIT, strlen(SMTP_CMD_QUIT)) == 0){
|
|
|
|
process_command_quit(session, response, sizeof(response));
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(strncasecmp(buf, SMTP_CMD_RESET, strlen(SMTP_CMD_RESET)) == 0){
|
|
|
|
process_command_reset(session);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->tls_enable == 1 && strncasecmp(buf, SMTP_CMD_STARTTLS, strlen(SMTP_CMD_STARTTLS)) == 0 && session->use_ssl == 0){
|
|
|
|
process_command_starttls(session);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
send_smtp_response(session, SMTP_RESP_502_ERR);
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_data(struct smtp_session *session, char *readbuf, int readlen){
|
|
|
|
char puf[SMALLBUFSIZE+BIGBUFSIZE];
|
|
|
|
int n, pos, len;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
memset(puf, 0, sizeof(puf));
|
|
|
|
|
|
|
|
if(session->buflen > 0){
|
|
|
|
memcpy(&puf[0], session->buf, session->buflen);
|
|
|
|
memcpy(&puf[session->buflen], readbuf, readlen);
|
|
|
|
len = session->buflen + readlen;
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-10-23 22:04:55 +02:00
|
|
|
memcpy(&puf[0], readbuf, readlen);
|
|
|
|
len = readlen;
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
pos = searchStringInBuffer(&puf[0], len, SMTP_CMD_PERIOD, 5);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(pos > 0){
|
|
|
|
//write(session->fd, puf, pos+2);
|
|
|
|
//session->tot_len += pos+2;
|
|
|
|
write(session->fd, puf, pos);
|
|
|
|
session->tot_len += pos;
|
|
|
|
process_command_period(session);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = search_char_backward(&puf[0], len, '\r');
|
|
|
|
|
|
|
|
if(n == -1 || len - n > 4){
|
|
|
|
write(session->fd, puf, len);
|
|
|
|
session->tot_len += len;
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
else {
|
|
|
|
write(session->fd, puf, n);
|
|
|
|
session->tot_len += n;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
snprintf(session->buf, SMALLBUFSIZE-1, "%s", &puf[n]);
|
|
|
|
session->buflen = len - n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void send_smtp_response(struct smtp_session *session, char *buf){
|
|
|
|
int rc;
|
|
|
|
char ssl_error[SMALLBUFSIZE];
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
write1(session->socket, buf, strlen(buf), session->use_ssl, session->ssl);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "sent: %s", buf);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->starttls == 1 && session->use_ssl == 0){
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "waiting for ssl handshake");
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
rc = SSL_accept(session->ssl);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "SSL_accept() finished");
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(rc == 1){
|
|
|
|
session->use_ssl = 1;
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
else {
|
|
|
|
ERR_error_string_n(ERR_get_error(), ssl_error, SMALLBUFSIZE);
|
|
|
|
syslog(LOG_PRIORITY, "%s: SSL_accept() failed, rc=%d, errorcode: %d, error text: %s\n", session->ttmpfile, rc, SSL_get_error(session->ssl, rc), ssl_error);
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_helo(struct smtp_session *session, char *buf, int buflen){
|
|
|
|
if(session->protocol_state == SMTP_STATE_INIT) session->protocol_state = SMTP_STATE_HELO;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
snprintf(buf, buflen-1, "220 %s ESMTP\r\n", session->cfg->hostid);
|
|
|
|
send_smtp_response(session, buf);
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_ehlo_lhlo(struct smtp_session *session, char *buf, int buflen){
|
|
|
|
char extensions[SMALLBUFSIZE];
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
memset(extensions, 0, sizeof(extensions));
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->protocol_state == SMTP_STATE_INIT) session->protocol_state = SMTP_STATE_HELO;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
// if tls is not started, but it's enabled in the config
|
|
|
|
if(session->use_ssl == 0 && session->cfg->tls_enable == 1) snprintf(extensions, sizeof(extensions)-1, "%s", SMTP_EXTENSION_STARTTLS);
|
|
|
|
if(session->cfg->enable_chunking == 1) strncat(extensions, SMTP_EXTENSION_CHUNKING, sizeof(extensions)-strlen(extensions)-2);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
snprintf(buf, buflen-1, SMTP_RESP_250_EXTENSIONS, session->cfg->hostid, extensions);
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
send_smtp_response(session, buf);
|
|
|
|
}
|
2016-08-19 22:33:47 +02:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
int init_ssl(struct smtp_session *session){
|
|
|
|
session->ctx = SSL_CTX_new(TLSv1_server_method());
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->ctx == NULL){
|
|
|
|
syslog(LOG_PRIORITY, "%s: SSL ctx is null!", session->ttmpfile);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(SSL_CTX_set_cipher_list(session->ctx, session->cfg->cipher_list) == 0){
|
|
|
|
syslog(LOG_PRIORITY, "failed to set cipher list: '%s'", session->cfg->cipher_list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SSL_CTX_use_PrivateKey_file(session->ctx, session->cfg->pemfile, SSL_FILETYPE_PEM) != 1){
|
|
|
|
syslog(LOG_PRIORITY, "cannot load private key from %s", session->cfg->pemfile);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(SSL_CTX_use_certificate_file(session->ctx, session->cfg->pemfile, SSL_FILETYPE_PEM) != 1){
|
|
|
|
syslog(LOG_PRIORITY, "cannot load certificate from %s", session->cfg->pemfile);
|
|
|
|
return 0;
|
2016-08-19 22:33:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
return 1;
|
2016-08-19 22:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_starttls(struct smtp_session *session){
|
|
|
|
if(session->cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "starttls request from client");
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(init_ssl(session) == 1){
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
session->ssl = SSL_new(session->ctx);
|
|
|
|
if(session->ssl){
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
SSL_set_options(session->ssl, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(SSL_set_fd(session->ssl, session->socket) == 1){
|
|
|
|
session->starttls = 1;
|
|
|
|
send_smtp_response(session, SMTP_RESP_220_READY_TO_START_TLS);
|
|
|
|
session->protocol_state = SMTP_STATE_INIT;
|
|
|
|
session->use_ssl = 1;
|
|
|
|
return;
|
|
|
|
} syslog(LOG_PRIORITY, "%s: SSL_set_fd() failed", session->ttmpfile);
|
|
|
|
} syslog(LOG_PRIORITY, "%s: SSL_new() failed", session->ttmpfile);
|
|
|
|
} syslog(LOG_PRIORITY, "%s: SSL ctx is null!", session->ttmpfile);
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
send_smtp_response(session, SMTP_RESP_454_ERR_TLS_TEMP_ERROR);
|
2016-08-19 22:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_mail_from(struct smtp_session *session, char *buf){
|
|
|
|
memset(session->mailfrom, 0, SMALLBUFSIZE);
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->protocol_state != SMTP_STATE_HELO && session->protocol_state != SMTP_STATE_PERIOD && session->protocol_state != SMTP_STATE_BDAT){
|
|
|
|
send(session->socket, SMTP_RESP_503_ERR, strlen(SMTP_RESP_503_ERR), 0);
|
2016-08-19 22:33:47 +02:00
|
|
|
}
|
2016-10-23 22:04:55 +02:00
|
|
|
else {
|
|
|
|
create_id(&(session->ttmpfile[0]), 15);
|
|
|
|
session->protocol_state = SMTP_STATE_MAIL_FROM;
|
2016-08-19 22:33:47 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
extractEmail(buf, session->mailfrom);
|
2016-08-19 23:27:24 +02:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
reset_bdat_counters(session);
|
|
|
|
session->tot_len = 0;
|
|
|
|
|
|
|
|
send_smtp_response(session, SMTP_RESP_250_OK);
|
|
|
|
}
|
2016-08-19 22:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_rcpt_to(struct smtp_session *session, char *buf){
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
if(session->protocol_state == SMTP_STATE_MAIL_FROM || session->protocol_state == SMTP_STATE_RCPT_TO){
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
// For now, we are not interested in the envelope recipients
|
|
|
|
|
|
|
|
session->protocol_state = SMTP_STATE_RCPT_TO;
|
|
|
|
send_smtp_response(session, SMTP_RESP_250_OK);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
send_smtp_response(session, SMTP_RESP_503_ERR);
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_data(struct smtp_session *session){
|
|
|
|
session->tot_len = 0;
|
|
|
|
|
|
|
|
if(session->protocol_state != SMTP_STATE_RCPT_TO){
|
|
|
|
send_smtp_response(session, SMTP_RESP_503_ERR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
session->fd = open(session->ttmpfile, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
|
|
|
|
if(session->fd == -1){
|
|
|
|
syslog(LOG_PRIORITY, "%s: %s", ERR_OPEN_TMP_FILE, session->ttmpfile);
|
|
|
|
send_smtp_response(session, SMTP_RESP_451_ERR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
session->protocol_state = SMTP_STATE_DATA;
|
|
|
|
send_smtp_response(session, SMTP_RESP_354_DATA_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_period(struct smtp_session *session){
|
|
|
|
char buf[SMALLBUFSIZE];
|
|
|
|
|
|
|
|
session->protocol_state = SMTP_STATE_PERIOD;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
// TODO: add some error handling
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
fsync(session->fd);
|
|
|
|
close(session->fd);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
session->fd = -1;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
syslog(LOG_PRIORITY, "received: %s, from=%s, size=%d", session->ttmpfile, session->mailfrom, session->tot_len);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
move_email(session);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
snprintf(buf, sizeof(buf)-1, "250 OK <%s>\r\n", session->ttmpfile);
|
|
|
|
|
|
|
|
session->buflen = 0;
|
|
|
|
memset(session->buf, 0, SMALLBUFSIZE);
|
|
|
|
|
|
|
|
send_smtp_response(session, buf);
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_quit(struct smtp_session *session, char *buf, int buflen){
|
|
|
|
session->protocol_state = SMTP_STATE_FINISHED;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
snprintf(buf, buflen-1, SMTP_RESP_221_GOODBYE, session->cfg->hostid);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
send_smtp_response(session, buf);
|
|
|
|
}
|
2015-12-02 21:49:51 +01:00
|
|
|
|
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
void process_command_reset(struct smtp_session *session){
|
|
|
|
send_smtp_response(session, SMTP_RESP_250_OK);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
session->tot_len = 0;
|
|
|
|
session->fd = -1;
|
|
|
|
session->protocol_state = SMTP_STATE_HELO;
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
reset_bdat_counters(session);
|
2015-12-02 21:49:51 +01:00
|
|
|
|
2016-10-23 22:04:55 +02:00
|
|
|
create_id(&(session->ttmpfile[0]), 15);
|
2015-12-02 21:49:51 +01:00
|
|
|
}
|
|
|
|
|