diff --git a/src/defs.h b/src/defs.h index 64efcfa4..5eb4c371 100644 --- a/src/defs.h +++ b/src/defs.h @@ -378,6 +378,8 @@ struct session_ctx { int new_sd; int db_conn; int inj; + int bdat_rounds; + int bdat_last_round; struct parser_state *parser_state; struct counters *counters; }; diff --git a/src/session.c b/src/session.c index 8d222d83..ef4e27f8 100644 --- a/src/session.c +++ b/src/session.c @@ -26,7 +26,7 @@ void process_data(struct session_ctx *sctx, struct session_data *sdata, struct _ int handle_smtp_session(struct session_ctx *sctx, struct __data *data, struct __config *cfg){ - int ret, pos, readpos=0, result, n, protocol_state, prevlen=0; + int i, ret, pos, readpos=0, result, n, protocol_state, prevlen=0; char *p, buf[MAXBUFSIZE], puf[MAXBUFSIZE], resp[MAXBUFSIZE], prevbuf[MAXBUFSIZE], last2buf[2*MAXBUFSIZE+1]; struct session_data sdata; int rc; @@ -260,6 +260,26 @@ AFTER_PERIOD: } + if(cfg->enable_chunking == 1 && strncasecmp(buf, SMTP_CMD_BDAT, strlen(SMTP_CMD_BDAT)) == 0){ + process_command_bdat(sctx, &sdata, data, &protocol_state, buf, &resp[0], sizeof(resp)-1); + + if(protocol_state == SMTP_STATE_BDAT){ + snprintf(resp, sizeof(resp)-2, "250 octets received\r\n"); + + for(i=0; ibdat_rounds-1; i++){ + send_buffered_response(&sdata, data, starttls, sctx->new_sd, &resp[0], cfg); + } + + process_written_file(sctx, &sdata, data, cfg); + + unlink(sdata.ttmpfile); + unlink(sdata.tmpframe); + } + + continue; + } + + if(strncasecmp(buf, SMTP_CMD_QUIT, strlen(SMTP_CMD_QUIT)) == 0){ process_command_quit(&sdata, &protocol_state, &resp[0], sizeof(resp)-1, cfg); continue; diff --git a/src/smtp.c b/src/smtp.c index 8be40007..c71955f4 100644 --- a/src/smtp.c +++ b/src/smtp.c @@ -134,6 +134,95 @@ void process_command_data(struct session_data *sdata, int *protocol_state, char } +void process_command_bdat(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, int *protocol_state, char *buf, char *resp, int resplen){ + int n, expected_bdat_len; + char puf[MAXBUFSIZE]; + + if(*protocol_state == SMTP_STATE_RCPT_TO){ + strncat(resp, SMTP_RESP_503_ERR, resplen); + return; + } + + sctx->bdat_rounds = 0; + sctx->bdat_last_round = 0; + + while(sctx->bdat_last_round != 1){ + sctx->bdat_rounds++; + expected_bdat_len = 0; + + if(sctx->bdat_rounds == 1){ + expected_bdat_len = extract_bdat_command(sctx, sdata, buf); + + sdata->fd = open(sdata->filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP); + if(sdata->fd == -1){ + syslog(LOG_PRIORITY, "%s: %s", ERR_OPEN_TMP_FILE, sdata->ttmpfile); + strncat(resp, SMTP_RESP_451_ERR, resplen); + return; + } + else { + *protocol_state = SMTP_STATE_BDAT; + } + } + else if(sctx->bdat_last_round != 1){ + if((n = recvtimeoutssl(sctx->new_sd, &puf[0], sizeof(puf), TIMEOUT, sdata->tls, data->ssl)) > 0){ + expected_bdat_len = extract_bdat_command(sctx, sdata, puf); + } + } + + if(expected_bdat_len > 0) sdata->tot_len += read_bdat_data(sctx, sdata, data, expected_bdat_len); + } + + close(sdata->fd); + fsync(sdata->fd); + +} + + +int extract_bdat_command(struct session_ctx *sctx, struct session_data *sdata, char *buf){ + int expected_bdat_len=0; + char *p; + + // determine if this is the last BDAT command + + p = strstr(buf, " LAST"); + if(p){ + sctx->bdat_last_round = 1; + syslog(LOG_INFO, "%s: BDAT LAST", sdata->ttmpfile); + *p = '\0'; + } + + // determine the size to be read + + p = strchr(buf, ' '); + if(p){ + expected_bdat_len = atoi(p); + syslog(LOG_INFO, "%s: BDAT len=%d", sdata->ttmpfile, expected_bdat_len); + } + + if(!p || expected_bdat_len <= 0){ + syslog(LOG_INFO, "%s: malformed BDAT command", sdata->ttmpfile); + return -1; + } + + return expected_bdat_len; +} + + +int read_bdat_data(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, int expected_bdat_len){ + int n, read_bdat_len=0, written_bdat_len=0; + char puf[MAXBUFSIZE]; + + while(read_bdat_len < expected_bdat_len){ + if((n = recvtimeoutssl(sctx->new_sd, &puf[0], sizeof(puf), TIMEOUT, sdata->tls, data->ssl)) > 0){ + read_bdat_len += n; + written_bdat_len += write(sdata->fd, puf, n); + } + } + + return written_bdat_len; +} + + void process_command_quit(struct session_data *sdata, int *protocol_state, char *resp, int resplen, struct __config *cfg){ char tmpbuf[MAXBUFSIZE]; diff --git a/src/smtp.h b/src/smtp.h index 06d34910..2363a789 100644 --- a/src/smtp.h +++ b/src/smtp.h @@ -10,9 +10,13 @@ void process_command_starttls(struct session_data *sdata, struct __data *data, i void process_command_mail_from(struct session_data *sdata, int *protocol_state, char *buf, char *resp, int resplen, struct __config *cfg); void process_command_rcpt_to(struct session_data *sdata, int *protocol_state, char *buf, char *resp, int resplen); void process_command_data(struct session_data *sdata, int *protocol_state, char *resp, int resplen); +void process_command_bdat(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, int *protocol_state, char *buf, char *resp, int resplen); void process_command_quit(struct session_data *sdata, int *protocol_state, char *resp, int resplen, struct __config *cfg); void process_command_reset(struct session_data *sdata, int *protocol_state, char *resp, int resplen, struct __config *cfg); +int read_bdat_data(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, int expected_bdat_len); +int extract_bdat_command(struct session_ctx *sctx, struct session_data *sdata, char *buf); + void send_buffered_response(struct session_data *sdata, struct __data *data, int starttls, int new_sd, char *resp, struct __config *cfg); #endif /* _SMTP_H */ diff --git a/src/smtpcodes.h b/src/smtpcodes.h index 038bd130..85f7e627 100644 --- a/src/smtpcodes.h +++ b/src/smtpcodes.h @@ -12,6 +12,7 @@ #define SMTP_STATE_PERIOD 5 #define SMTP_STATE_QUIT 6 #define SMTP_STATE_FINISHED 7 +#define SMTP_STATE_BDAT 8 // SMTP commands @@ -20,6 +21,7 @@ #define SMTP_CMD_MAIL_FROM "MAIL FROM:" #define SMTP_CMD_RCPT_TO "RCPT TO:" #define SMTP_CMD_DATA "DATA" +#define SMTP_CMD_BDAT "BDAT" #define SMTP_CMD_PERIOD "\x0d\x0a\x2e\x0d\x0a" #define SMTP_CMD_QUIT "QUIT" #define SMTP_CMD_RESET "RSET"