session.c refactoring #3

Change-Id: I280221dc2ed0a997175526c165716195f8dbc357
Signed-off-by: SJ <sj@acts.hu>
This commit is contained in:
SJ 2016-08-19 20:04:53 +02:00
parent 27f7a99dd9
commit 25c2581e80
5 changed files with 33 additions and 36 deletions

View File

@ -13,7 +13,7 @@
#define VERSION "1.2.0-master" #define VERSION "1.2.0-master"
#define BUILD 945 #define BUILD 947
#define HOSTID "mailarchiver" #define HOSTID "mailarchiver"

View File

@ -375,10 +375,10 @@ struct counters {
struct session_ctx { struct session_ctx {
char *status; char *status;
char *message_id;
int new_sd; int new_sd;
int db_conn; int db_conn;
int inj; int inj;
struct parser_state *parser_state;
struct counters *counters; struct counters *counters;
}; };

View File

@ -102,10 +102,10 @@ static void child_sighup_handler(int sig){
static void child_main(struct child *ptr){ static void child_main(struct child *ptr){
int new_sd;
char s[INET6_ADDRSTRLEN]; char s[INET6_ADDRSTRLEN];
struct sockaddr_storage client_addr; struct sockaddr_storage client_addr;
socklen_t addr_size; socklen_t addr_size;
struct session_ctx sctx;
ptr->messages = 0; ptr->messages = 0;
@ -120,9 +120,9 @@ static void child_main(struct child *ptr){
ptr->status = READY; ptr->status = READY;
addr_size = sizeof(client_addr); addr_size = sizeof(client_addr);
new_sd = accept(sd, (struct sockaddr *)&client_addr, &addr_size); sctx.new_sd = accept(sd, (struct sockaddr *)&client_addr, &addr_size);
if(new_sd == -1) continue; if(sctx.new_sd == -1) continue;
ptr->status = BUSY; ptr->status = BUSY;
@ -133,10 +133,10 @@ static void child_main(struct child *ptr){
data.child_serial = ptr->serial; data.child_serial = ptr->serial;
sig_block(SIGHUP); sig_block(SIGHUP);
ptr->messages += handle_smtp_session(new_sd, &data, &cfg); ptr->messages += handle_smtp_session(&sctx, &data, &cfg);
sig_unblock(SIGHUP); sig_unblock(SIGHUP);
close(new_sd); close(sctx.new_sd);
if(cfg.max_requests_per_child > 0 && ptr->messages >= cfg.max_requests_per_child){ if(cfg.max_requests_per_child > 0 && ptr->messages >= cfg.max_requests_per_child){
if(cfg.verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "child (pid: %d, serial: %d) served enough: %d", getpid(), ptr->messages, ptr->serial); if(cfg.verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "child (pid: %d, serial: %d) served enough: %d", getpid(), ptr->messages, ptr->serial);

View File

@ -35,8 +35,7 @@ int make_digests(struct session_data *sdata, struct __config *cfg);
void digest_file(char *filename, char *digest); void digest_file(char *filename, char *digest);
void digest_string(char *s, char *digest); void digest_string(char *s, char *digest);
int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg); int handle_smtp_session(struct session_ctx *sctx, struct __data *data, struct __config *cfg);
int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cfg);
void remove_stripped_attachments(struct parser_state *state); void remove_stripped_attachments(struct parser_state *state);
int process_message(struct session_data *sdata, struct parser_state *state, struct __data *data, struct __config *cfg); int process_message(struct session_data *sdata, struct parser_state *state, struct __data *data, struct __config *cfg);

View File

@ -25,13 +25,12 @@ void process_written_file(struct session_ctx *sctx, struct session_data *sdata,
void process_data(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, struct parser_state *parser_state, struct __config *cfg); void process_data(struct session_ctx *sctx, struct session_data *sdata, struct __data *data, struct parser_state *parser_state, struct __config *cfg);
int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ 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 ret, pos, readpos=0, result, n, protocol_state, prevlen=0;
char *p, buf[MAXBUFSIZE], puf[MAXBUFSIZE], resp[MAXBUFSIZE], prevbuf[MAXBUFSIZE], last2buf[2*MAXBUFSIZE+1]; char *p, buf[MAXBUFSIZE], puf[MAXBUFSIZE], resp[MAXBUFSIZE], prevbuf[MAXBUFSIZE], last2buf[2*MAXBUFSIZE+1];
struct session_data sdata; struct session_data sdata;
int rc; int rc;
struct counters counters; struct counters counters;
struct session_ctx sctx;
struct timezone tz; struct timezone tz;
struct timeval tv1, tv2; struct timeval tv1, tv2;
@ -40,15 +39,14 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
bzero(&counters, sizeof(counters)); bzero(&counters, sizeof(counters));
sctx.new_sd = new_sd; sctx->inj = ERR;
sctx.inj = ERR; sctx->db_conn = 0;
sctx.db_conn = 0; sctx->status = NULL;
sctx.status = NULL; sctx->counters = &counters;
sctx.message_id = NULL; sctx->parser_state = NULL;
sctx.counters = &counters;
#ifdef HAVE_LIBWRAP #ifdef HAVE_LIBWRAP
if(is_blocked_by_tcp_wrappers(sctx.new_sd) == 1) return 0; if(is_blocked_by_tcp_wrappers(sctx->new_sd) == 1) return 0;
#endif #endif
srand(getpid()); srand(getpid());
@ -63,15 +61,15 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
#ifdef NEED_MYSQL #ifdef NEED_MYSQL
if(open_database(&sdata, cfg) == OK){ if(open_database(&sdata, cfg) == OK){
sctx.db_conn = 1; sctx->db_conn = 1;
} }
else else
syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT); syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT);
#endif #endif
if(sctx.db_conn == 0){ if(sctx->db_conn == 0){
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR_TMP, cfg->hostid); snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR_TMP, cfg->hostid);
send(sctx.new_sd, buf, strlen(buf), 0); send(sctx->new_sd, buf, strlen(buf), 0);
return 0; return 0;
} }
@ -84,10 +82,10 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_220_BANNER, cfg->hostid); snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_220_BANNER, cfg->hostid);
#endif #endif
send(sctx.new_sd, buf, strlen(buf), 0); send(sctx->new_sd, buf, strlen(buf), 0);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf);
while((n = recvtimeoutssl(sctx.new_sd, &puf[readpos], sizeof(puf)-readpos, TIMEOUT, sdata.tls, data->ssl)) > 0){ while((n = recvtimeoutssl(sctx->new_sd, &puf[readpos], sizeof(puf)-readpos, TIMEOUT, sdata.tls, data->ssl)) > 0){
pos = 0; pos = 0;
/* accept mail data */ /* accept mail data */
@ -140,7 +138,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
for(i=0; i<sdata.num_of_rcpt_to; i++){ for(i=0; i<sdata.num_of_rcpt_to; i++){
#endif #endif
write1(sctx.new_sd, SMTP_RESP_421_ERR_WRITE_FAILED, strlen(SMTP_RESP_421_ERR_WRITE_FAILED), sdata.tls, data->ssl); write1(sctx->new_sd, SMTP_RESP_421_ERR_WRITE_FAILED, strlen(SMTP_RESP_421_ERR_WRITE_FAILED), sdata.tls, data->ssl);
#ifdef HAVE_LMTP #ifdef HAVE_LMTP
} }
@ -150,7 +148,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
goto AFTER_PERIOD; goto AFTER_PERIOD;
} }
process_written_file(&sctx, &sdata, data, cfg); process_written_file(sctx, &sdata, data, cfg);
@ -172,7 +170,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
if(puf[n-2] != '\r' && puf[n-1] != '\n'){ if(puf[n-2] != '\r' && puf[n-1] != '\n'){
memmove(puf, puf+pos, n-pos); memmove(puf, puf+pos, n-pos);
memset(puf+n-pos, 0, MAXBUFSIZE-n+pos); memset(puf+n-pos, 0, MAXBUFSIZE-n+pos);
recvtimeout(sctx.new_sd, buf, MAXBUFSIZE, TIMEOUT); recvtimeout(sctx->new_sd, buf, MAXBUFSIZE, TIMEOUT);
strncat(puf, buf, MAXBUFSIZE-1-n+pos); strncat(puf, buf, MAXBUFSIZE-1-n+pos);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: partial read: %s", sdata.ttmpfile, puf); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: partial read: %s", sdata.ttmpfile, puf);
pos = 0; pos = 0;
@ -234,7 +232,7 @@ AFTER_PERIOD:
if(cfg->tls_enable > 0 && strncasecmp(buf, SMTP_CMD_STARTTLS, strlen(SMTP_CMD_STARTTLS)) == 0 && strlen(data->starttls) > 4 && sdata.tls == 0){ 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, new_sd, &resp[0], sizeof(resp)-1, cfg); process_command_starttls(&sdata, data, &protocol_state, &starttls, sctx->new_sd, &resp[0], sizeof(resp)-1, cfg);
continue; continue;
} }
@ -254,7 +252,7 @@ AFTER_PERIOD:
if(strncasecmp(buf, SMTP_CMD_DATA, strlen(SMTP_CMD_DATA)) == 0){ if(strncasecmp(buf, SMTP_CMD_DATA, strlen(SMTP_CMD_DATA)) == 0){
memset(last2buf, 0, 2*MAXBUFSIZE+1); memset(last2buf, 0, 2*MAXBUFSIZE+1);
memset(prevbuf, 0, MAXBUFSIZE); memset(prevbuf, 0, MAXBUFSIZE);
sctx.inj = ERR; sctx->inj = ERR;
prevlen = 0; prevlen = 0;
process_command_data(&sdata, &protocol_state, &resp[0], sizeof(resp)-1); process_command_data(&sdata, &protocol_state, &resp[0], sizeof(resp)-1);
@ -288,7 +286,7 @@ AFTER_PERIOD:
if(strlen(resp) > 0){ if(strlen(resp) > 0){
send_buffered_response(&sdata, data, starttls, new_sd, &resp[0], cfg); send_buffered_response(&sdata, data, starttls, sctx->new_sd, &resp[0], cfg);
memset(resp, 0, sizeof(resp)); memset(resp, 0, sizeof(resp));
} }
@ -304,9 +302,9 @@ AFTER_PERIOD:
* ie. we have timed out than send back 421 error message * ie. we have timed out than send back 421 error message
*/ */
if(protocol_state < SMTP_STATE_QUIT && sctx.inj == ERR){ if(protocol_state < SMTP_STATE_QUIT && sctx->inj == ERR){
snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR, cfg->hostid); snprintf(buf, MAXBUFSIZE-1, SMTP_RESP_421_ERR, cfg->hostid);
write1(new_sd, buf, strlen(buf), sdata.tls, data->ssl); write1(sctx->new_sd, buf, strlen(buf), sdata.tls, data->ssl);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sent: %s", sdata.ttmpfile, buf);
@ -325,7 +323,7 @@ AFTER_PERIOD:
QUITTING: QUITTING:
update_counters(&sdata, data, sctx.counters, cfg); update_counters(&sdata, data, sctx->counters, cfg);
#ifdef NEED_MYSQL #ifdef NEED_MYSQL
close_database(&sdata); close_database(&sdata);
@ -336,9 +334,9 @@ QUITTING:
SSL_free(data->ssl); SSL_free(data->ssl);
} }
if(cfg->verbosity >= _LOG_INFO) syslog(LOG_PRIORITY, "processed %llu messages", sctx.counters->c_rcvd); if(cfg->verbosity >= _LOG_INFO) syslog(LOG_PRIORITY, "processed %llu messages", sctx->counters->c_rcvd);
return (int)sctx.counters->c_rcvd; return (int)sctx->counters->c_rcvd;
} }
@ -377,7 +375,7 @@ void process_written_file(struct session_ctx *sctx, struct session_data *sdata,
parser_state = parse_message(sdata, 1, data, cfg); parser_state = parse_message(sdata, 1, data, cfg);
post_parse(sdata, &parser_state, cfg); post_parse(sdata, &parser_state, cfg);
sctx->message_id = parser_state.message_id; sctx->parser_state = &parser_state;
gettimeofday(&tv2, &tz); gettimeofday(&tv2, &tz);
sdata->__parsed = tvdiff(tv2, tv1); sdata->__parsed = tvdiff(tv2, tv1);
@ -521,7 +519,7 @@ void send_response_to_data(struct session_ctx *sctx, struct session_data *sdata,
sctx->counters->c_rcvd++; sctx->counters->c_rcvd++;
if(sctx->inj == ERR_EXISTS){ if(sctx->inj == ERR_EXISTS){
syslog(LOG_PRIORITY, "%s: discarding: duplicate message, id: %llu, message-id: %s", sdata->ttmpfile, sdata->duplicate_id, sctx->message_id); syslog(LOG_PRIORITY, "%s: discarding: duplicate message, id: %llu, message-id: %s", sdata->ttmpfile, sdata->duplicate_id, sctx->parser_state->message_id);
sctx->counters->c_duplicate++; sctx->counters->c_duplicate++;
sctx->status = S_STATUS_DUPLICATE; sctx->status = S_STATUS_DUPLICATE;
} }