piler/src/session.c

497 lines
16 KiB
C
Raw Normal View History

2011-11-14 15:57:52 +01:00
/*
* session.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
2011-11-14 15:57:52 +01:00
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
2012-10-28 20:36:46 +01:00
#include <openssl/ssl.h>
#include <openssl/err.h>
2011-11-14 15:57:52 +01:00
#include <piler.h>
#include "smtp.h"
2011-11-14 15:57:52 +01:00
2015-11-27 11:56:19 +01:00
int is_blocked_by_tcp_wrappers(int sd);
2012-01-07 00:00:36 +01:00
int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
int i, ret, pos, readpos=0, result, n, inj=ERR, protocol_state, prevlen=0;
2014-08-18 12:58:45 +02:00
char *p, *rcpt, buf[MAXBUFSIZE], puf[MAXBUFSIZE], resp[MAXBUFSIZE], prevbuf[MAXBUFSIZE], last2buf[2*MAXBUFSIZE+1];
char virusinfo[SMALLBUFSIZE], delay[SMALLBUFSIZE], tmpbuf[SMALLBUFSIZE];
2011-11-19 21:25:44 +01:00
char *arule = NULL;
char *status = NULL;
2011-11-14 15:57:52 +01:00
struct session_data sdata;
2015-11-21 23:06:47 +01:00
struct parser_state parser_state;
2011-11-14 15:57:52 +01:00
int db_conn=0;
int rc;
struct __counters counters;
struct timezone tz;
struct timeval tv1, tv2;
2012-10-28 20:36:46 +01:00
#ifdef HAVE_STARTTLS
int starttls = 0;
#endif
2011-11-14 15:57:52 +01:00
#ifdef HAVE_LIBWRAP
2015-11-27 11:56:19 +01:00
if(is_blocked_by_tcp_wrappers(new_sd) == 1) return 0;
#endif
2013-09-11 09:19:29 +02:00
srand(getpid());
2015-11-21 23:06:47 +01:00
protocol_state = SMTP_STATE_INIT;
2011-11-14 15:57:52 +01:00
init_session_data(&sdata, cfg);
2012-10-28 20:36:46 +01:00
sdata.tls = 0;
2011-11-14 15:57:52 +01:00
bzero(&counters, sizeof(counters));
/* open database connection */
db_conn = 0;
#ifdef NEED_MYSQL
2013-04-28 14:18:09 +02:00
if(open_database(&sdata, cfg) == OK){
2011-11-14 15:57:52 +01:00
db_conn = 1;
2011-12-07 15:24:52 +01:00
}
2011-11-14 15:57:52 +01:00
else
syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT);
#endif
if(db_conn == 0){
2014-05-23 11:35:41 +02:00
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR_TMP, cfg->hostid);
send(new_sd, buf, strlen(buf), 0);
return 0;
}
2011-11-14 15:57:52 +01:00
gettimeofday(&tv1, &tz);
#ifdef HAVE_LMTP
snprintf(buf, MAXBUFSIZE-1, LMTP_RESP_220_BANNER, cfg->hostid);
#else
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_220_BANNER, cfg->hostid);
#endif
send(new_sd, buf, strlen(buf), 0);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf);
while((n = recvtimeoutssl(new_sd, &puf[readpos], sizeof(puf)-readpos, TIMEOUT, sdata.tls, data->ssl)) > 0){
2011-11-14 15:57:52 +01:00
pos = 0;
/* accept mail data */
2015-11-21 23:06:47 +01:00
if(protocol_state == SMTP_STATE_DATA){
2011-11-14 15:57:52 +01:00
/* join the last 2 buffer */
memset(last2buf, 0, 2*MAXBUFSIZE+1);
memcpy(last2buf, prevbuf, MAXBUFSIZE);
memcpy(last2buf+prevlen, puf, MAXBUFSIZE);
pos = searchStringInBuffer(last2buf, 2*MAXBUFSIZE+1, SMTP_CMD_PERIOD, 5);
if(pos > 0){
/* fix position */
pos = pos - prevlen;
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: period found", sdata.ttmpfile);
/* write data only to (and including) the trailing period (.) */
ret = write(sdata.fd, puf, pos);
sdata.tot_len += ret;
/* fix posistion! */
pos += strlen(SMTP_CMD_PERIOD);
2011-11-22 12:31:54 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: got: (.)", sdata.ttmpfile);
2011-11-14 15:57:52 +01:00
2015-11-21 23:06:47 +01:00
protocol_state = SMTP_STATE_PERIOD;
2011-11-14 15:57:52 +01:00
/* make sure we had a successful read */
rc = fsync(sdata.fd);
close(sdata.fd);
gettimeofday(&tv2, &tz);
sdata.__acquire = tvdiff(tv2, tv1);
if(rc){
syslog(LOG_PRIORITY, "failed writing data: %s", sdata.ttmpfile);
#ifdef HAVE_LMTP
for(i=0; i<sdata.num_of_rcpt_to; i++){
#endif
2013-04-09 14:50:27 +02:00
write1(new_sd, SMTP_RESP_421_ERR_WRITE_FAILED, strlen(SMTP_RESP_421_ERR_WRITE_FAILED), sdata.tls, data->ssl);
2011-11-14 15:57:52 +01:00
#ifdef HAVE_LMTP
}
#endif
memset(puf, 0, MAXBUFSIZE);
goto AFTER_PERIOD;
}
gettimeofday(&tv1, &tz);
2015-09-03 14:41:52 +02:00
data->folder = 0;
2015-11-21 23:06:47 +01:00
parser_state = parse_message(&sdata, 1, data, cfg);
post_parse(&sdata, &parser_state, cfg);
2011-11-14 15:57:52 +01:00
gettimeofday(&tv2, &tz);
sdata.__parsed = tvdiff(tv2, tv1);
2014-08-18 12:58:45 +02:00
if(cfg->syslog_recipients == 1){
2015-11-21 23:06:47 +01:00
rcpt = parser_state.b_to;
2014-08-18 12:58:45 +02:00
do {
rcpt = split_str(rcpt, " ", tmpbuf, sizeof(tmpbuf)-1);
if(does_it_seem_like_an_email_address(tmpbuf) == 1){
syslog(LOG_PRIORITY, "%s: rcpt=%s", sdata.ttmpfile, tmpbuf);
}
} while(rcpt);
}
2011-11-14 15:57:52 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: parsed message", sdata.ttmpfile);
2013-07-12 22:54:45 +02:00
if(cfg->archive_only_mydomains == 1 && sdata.internal_sender == 0 && sdata.internal_recipient == 0){
2015-11-21 23:06:47 +01:00
remove_stripped_attachments(&parser_state);
2013-07-12 22:54:45 +02:00
inj = ERR_MYDOMAINS;
2014-07-01 11:43:36 +02:00
snprintf(sdata.acceptbuf, SMALLBUFSIZE-1, "250 Ok %s\r\n", sdata.ttmpfile);
2013-07-12 22:54:45 +02:00
write1(new_sd, sdata.acceptbuf, strlen(sdata.acceptbuf), sdata.tls, data->ssl);
2015-11-21 23:06:47 +01:00
syslog(LOG_PRIORITY, "%s: discarding: not on mydomains, from=%s, message-id=%s", sdata.ttmpfile, sdata.fromemail, parser_state.message_id);
2013-07-12 22:54:45 +02:00
goto END_OF_PROCESSING;
}
2011-12-29 12:11:28 +01:00
make_digests(&sdata, cfg);
2011-11-14 15:57:52 +01:00
#ifdef HAVE_ANTIVIRUS
if(cfg->use_antivirus == 1){
2014-07-01 11:43:36 +02:00
sdata.rav = do_av_check(&sdata, &virusinfo[0], data, cfg);
2011-11-14 15:57:52 +01:00
}
#endif
#ifdef HAVE_LMTP
for(i=0; i<sdata.num_of_rcpt_to; i++){
#else
i = 0;
#endif
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: round %d in injection", sdata.ttmpfile, i);
inj = ERR;
2014-05-12 01:05:44 +02:00
status = S_STATUS_UNDEF;
2011-11-14 15:57:52 +01:00
if(db_conn == 1){
2011-12-13 17:05:22 +01:00
if(sdata.restored_copy == 1){
2013-07-12 22:54:45 +02:00
syslog(LOG_PRIORITY, "%s: discarding: restored copy", sdata.ttmpfile);
2011-12-13 17:05:22 +01:00
inj = OK;
}
else if(AVIR_VIRUS == sdata.rav){
2011-11-14 15:57:52 +01:00
syslog(LOG_PRIORITY, "%s: found virus: %s", sdata.ttmpfile, virusinfo);
counters.c_virus++;
inj = OK;
} else if(strlen(sdata.bodydigest) < 10) {
syslog(LOG_PRIORITY, "%s: invalid digest", sdata.ttmpfile);
inj = ERR;
} else {
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: processing message", sdata.ttmpfile);
2011-11-19 21:25:44 +01:00
/* check message against archiving rules */
2015-11-21 23:06:47 +01:00
arule = check_againt_ruleset(data->archiving_rules, &parser_state, sdata.tot_len, sdata.spam_message);
2011-11-19 21:25:44 +01:00
if(arule){
2013-07-12 22:54:45 +02:00
syslog(LOG_PRIORITY, "%s: discarding: archiving policy: *%s*", sdata.ttmpfile, arule);
2011-11-19 21:25:44 +01:00
inj = OK;
2011-12-27 20:51:56 +01:00
counters.c_ignore++;
2015-11-21 23:06:47 +01:00
remove_stripped_attachments(&parser_state);
2014-05-12 01:05:44 +02:00
status = S_STATUS_DISCARDED;
2011-11-19 21:25:44 +01:00
}
else {
2015-11-21 23:06:47 +01:00
inj = process_message(&sdata, &parser_state, data, cfg);
unlink(parser_state.message_id_hash);
2012-02-10 13:57:04 +01:00
counters.c_size += sdata.tot_len;
2014-05-05 16:00:33 +02:00
counters.c_stored_size = sdata.stored_len;
2014-05-12 01:05:44 +02:00
status = S_STATUS_STORED;
2011-11-19 21:25:44 +01:00
}
2011-11-14 15:57:52 +01:00
}
}
/* set the accept buffer */
2014-07-01 11:43:36 +02:00
snprintf(sdata.acceptbuf, SMALLBUFSIZE-1, "250 Ok %s <%s>\r\n", sdata.ttmpfile, sdata.rcptto[i]);
2011-11-14 15:57:52 +01:00
if(inj == ERR){
2014-07-01 11:43:36 +02:00
snprintf(sdata.acceptbuf, SMALLBUFSIZE-1, "451 %s <%s>\r\n", sdata.ttmpfile, sdata.rcptto[i]);
status = S_STATUS_ERROR;
}
2011-11-14 15:57:52 +01:00
2013-04-09 14:50:27 +02:00
write1(new_sd, sdata.acceptbuf, strlen(sdata.acceptbuf), sdata.tls, data->ssl);
2011-11-14 15:57:52 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, sdata.acceptbuf);
counters.c_rcvd++;
if(inj == ERR_EXISTS){
2015-11-21 23:06:47 +01:00
syslog(LOG_PRIORITY, "%s: discarding: duplicate message, id: %llu, message-id: %s", sdata.ttmpfile, sdata.duplicate_id, parser_state.message_id);
2011-11-14 15:57:52 +01:00
counters.c_duplicate++;
status = S_STATUS_DUPLICATE;
2011-11-14 15:57:52 +01:00
}
2012-06-06 20:38:16 +02:00
snprintf(delay, SMALLBUFSIZE-1, "delay=%.2f, delays=%.2f/%.2f/%.2f/%.2f/%.2f/%.2f",
2011-11-14 15:57:52 +01:00
(sdata.__acquire+sdata.__parsed+sdata.__av+sdata.__compress+sdata.__encrypt+sdata.__store)/1000000.0,
sdata.__acquire/1000000.0, sdata.__parsed/1000000.0, sdata.__av/1000000.0, sdata.__compress/1000000.0, sdata.__encrypt/1000000.0, sdata.__store/1000000.0);
2015-11-21 23:06:47 +01:00
syslog(LOG_PRIORITY, "%s: from=%s, size=%d/%d, attachments=%d, reference=%s, message-id=%s, retention=%d, folder=%d, %s, status=%s", sdata.ttmpfile, sdata.fromemail, sdata.tot_len, sdata.stored_len, parser_state.n_attachments, parser_state.reference, parser_state.message_id, parser_state.retention, data->folder, delay, status);
2011-11-14 15:57:52 +01:00
#ifdef HAVE_LMTP
} /* for */
#endif
2013-07-12 22:54:45 +02:00
END_OF_PROCESSING:
2011-11-14 15:57:52 +01:00
unlink(sdata.ttmpfile);
2011-11-19 21:25:44 +01:00
unlink(sdata.tmpframe);
2011-11-14 15:57:52 +01:00
/* if we have nothing after the trailing (.), we can read
the next command from the network */
if(puf[n-3] == '.' && puf[n-2] == '\r' && puf[n-1] == '\n') continue;
/* if we left something in the puffer, we are ready to proceed
to handle the additional commands, such as QUIT */
/* if we miss the trailing \r\n, ie. we need another read */
if(puf[n-2] != '\r' && puf[n-1] != '\n'){
memmove(puf, puf+pos, n-pos);
memset(puf+n-pos, 0, MAXBUFSIZE-n+pos);
2015-12-28 14:50:37 +01:00
recvtimeout(new_sd, buf, MAXBUFSIZE, TIMEOUT);
2011-11-14 15:57:52 +01:00
strncat(puf, buf, MAXBUFSIZE-1-n+pos);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: partial read: %s", sdata.ttmpfile, puf);
pos = 0;
}
} /* pos > 0, PERIOD found */
2011-11-14 15:57:52 +01:00
else {
ret = write(sdata.fd, puf, n);
sdata.tot_len += ret;
memcpy(prevbuf, puf, n);
prevlen = n;
continue;
}
} /* if protocol_state == SMTP_STATE_DATA */
2011-11-14 15:57:52 +01:00
AFTER_PERIOD:
/* handle smtp commands */
memset(resp, 0, sizeof(resp));
2011-11-14 15:57:52 +01:00
p = &puf[pos];
readpos = 0;
2011-11-14 15:57:52 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: command=*%s*", sdata.ttmpfile, p);
2011-11-14 15:57:52 +01:00
do {
p = split(p, '\n', buf, sizeof(buf)-1, &result);
2011-11-14 15:57:52 +01:00
if(result == 0){
if(strlen(buf) > 0){
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: partial read: *%s*", sdata.ttmpfile, buf);
snprintf(puf, sizeof(puf)-5, "%s", buf);
readpos = strlen(puf);
}
2011-11-14 15:57:52 +01:00
break;
}
2012-10-28 20:36:46 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: got: %s", sdata.ttmpfile, buf);
2011-11-14 15:57:52 +01: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(&sdata, data, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2011-11-14 15:57:52 +01:00
continue;
/* FIXME: implement the ENHANCEDSTATUSCODE extensions */
}
if(strncasecmp(buf, SMTP_CMD_HELO, strlen(SMTP_CMD_HELO)) == 0){
2015-11-21 23:06:47 +01:00
if(protocol_state == SMTP_STATE_INIT) protocol_state = SMTP_STATE_HELO;
2015-12-28 14:50:37 +01:00
strncat(resp, SMTP_RESP_250_OK, sizeof(resp)-strlen(resp)-1);
2011-11-14 15:57:52 +01:00
continue;
}
2012-10-28 20:36:46 +01:00
#ifdef HAVE_STARTTLS
if(cfg->tls_enable > 0 && strncasecmp(buf, SMTP_CMD_STARTTLS, strlen(SMTP_CMD_STARTTLS)) == 0 && strlen(data->starttls) > 4 && sdata.tls == 0){
process_command_starttls(&sdata, data, &protocol_state, &starttls, buf, new_sd, &resp[0], sizeof(resp)-1, cfg);
2012-10-28 20:36:46 +01:00
continue;
}
#endif
2011-11-14 15:57:52 +01:00
if(strncasecmp(buf, SMTP_CMD_MAIL_FROM, strlen(SMTP_CMD_MAIL_FROM)) == 0){
process_command_mail_from(&sdata, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2011-11-14 15:57:52 +01:00
continue;
}
if(strncasecmp(buf, SMTP_CMD_RCPT_TO, strlen(SMTP_CMD_RCPT_TO)) == 0){
process_command_rcpt_to(&sdata, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2011-11-14 15:57:52 +01:00
continue;
}
if(strncasecmp(buf, SMTP_CMD_DATA, strlen(SMTP_CMD_DATA)) == 0){
memset(last2buf, 0, 2*MAXBUFSIZE+1);
memset(prevbuf, 0, MAXBUFSIZE);
inj = ERR;
prevlen = 0;
process_command_data(&sdata, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2011-11-14 15:57:52 +01:00
continue;
}
if(strncasecmp(buf, SMTP_CMD_QUIT, strlen(SMTP_CMD_QUIT)) == 0){
process_command_quit(&sdata, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2012-06-20 10:28:20 +02:00
continue;
2011-11-14 15:57:52 +01:00
}
if(strncasecmp(buf, SMTP_CMD_NOOP, strlen(SMTP_CMD_NOOP)) == 0){
2015-12-28 14:50:37 +01:00
strncat(resp, SMTP_RESP_250_OK, sizeof(resp)-strlen(resp)-1);
2011-11-14 15:57:52 +01:00
continue;
}
if(strncasecmp(buf, SMTP_CMD_RESET, strlen(SMTP_CMD_RESET)) == 0){
process_command_reset(&sdata, &protocol_state, buf, &resp[0], sizeof(resp)-1, cfg);
2011-11-14 15:57:52 +01:00
continue;
}
2011-11-14 15:57:52 +01:00
/* by default send 502 command not implemented message */
syslog(LOG_PRIORITY, "%s: invalid command: *%s*", sdata.ttmpfile, buf);
2015-12-28 14:50:37 +01:00
strncat(resp, SMTP_RESP_502_ERR, sizeof(resp)-strlen(resp)-1);
} while(p);
2011-11-14 15:57:52 +01:00
if(strlen(resp) > 0){
send_buffered_response(&sdata, data, &protocol_state, starttls, buf, new_sd, &resp[0], sizeof(resp)-1, cfg);
memset(resp, 0, sizeof(resp));
2011-11-14 15:57:52 +01:00
}
2015-11-21 23:06:47 +01:00
if(protocol_state == SMTP_STATE_FINISHED){
2012-06-20 10:28:20 +02:00
goto QUITTING;
}
2011-11-14 15:57:52 +01:00
} /* while */
/*
* if we are not in SMTP_STATE_QUIT and the message was not injected,
* ie. we have timed out than send back 421 error message
*/
2015-11-21 23:06:47 +01:00
if(protocol_state < SMTP_STATE_QUIT && inj == ERR){
2011-11-14 15:57:52 +01:00
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR, cfg->hostid);
2013-04-09 14:50:27 +02:00
write1(new_sd, buf, strlen(buf), sdata.tls, data->ssl);
2012-10-28 20:36:46 +01:00
2011-11-14 15:57:52 +01:00
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf);
if(sdata.fd != -1){
syslog(LOG_PRIORITY, "%s: removing stale files: %s, %s", sdata.ttmpfile, sdata.ttmpfile, sdata.tmpframe);
close(sdata.fd);
unlink(sdata.ttmpfile);
unlink(sdata.tmpframe);
}
2011-11-14 15:57:52 +01:00
goto QUITTING;
}
QUITTING:
2012-01-07 00:00:36 +01:00
update_counters(&sdata, data, &counters, cfg);
2011-11-14 15:57:52 +01:00
#ifdef NEED_MYSQL
2013-04-28 14:18:09 +02:00
close_database(&sdata);
2011-11-14 15:57:52 +01:00
#endif
2012-10-28 20:36:46 +01:00
#ifdef HAVE_STARTTLS
if(sdata.tls == 1){
SSL_shutdown(data->ssl);
SSL_free(data->ssl);
}
#endif
2011-11-14 15:57:52 +01:00
if(cfg->verbosity >= _LOG_INFO) syslog(LOG_PRIORITY, "processed %llu messages", counters.c_rcvd);
2012-01-07 00:00:36 +01:00
return (int)counters.c_rcvd;
2011-11-14 15:57:52 +01:00
}
2015-11-27 11:56:19 +01:00
#ifdef HAVE_LIBWRAP
int is_blocked_by_tcp_wrappers(int sd){
struct request_info req;
request_init(&req, RQ_DAEMON, PROGNAME, RQ_FILE, sd, 0);
fromhost(&req);
if(!hosts_access(&req)){
send(sd, SMTP_RESP_550_ERR_YOU_ARE_BANNED_BY_LOCAL_POLICY, strlen(SMTP_RESP_550_ERR_YOU_ARE_BANNED_BY_LOCAL_POLICY), 0);
syslog(LOG_PRIORITY, "denied connection from %s by tcp_wrappers", eval_client(&req));
return 1;
}
return 0;
}
#endif