From 21abfc85b0e8afcbb4e1397e1a11564336fedc71 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 20 Sep 2004 21:38:15 +0000 Subject: Better processing of result messages --- common/smtppass.c | 10 +++---- src/proxsmtpd.c | 86 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/common/smtppass.c b/common/smtppass.c index 72ca71c..468991a 100644 --- a/common/smtppass.c +++ b/common/smtppass.c @@ -1314,6 +1314,7 @@ cleanup: int sp_fail_data(spctx_t* ctx, const char* smtp_status) { + char buf[256 + KL(SMTP_REJPREFIX) + KL(CRLF) + 1]; char* t = NULL; int len, x; int pref = 0; @@ -1335,14 +1336,11 @@ int sp_fail_data(spctx_t* ctx, const char* smtp_status) if(pref || crlf) { - x = (len > 256 ? 256 : len) + KL(SMTP_REJPREFIX) + KL(CRLF) + 1; - t = (char*)alloca(x + 1); - /* Note that we truncate long lines */ - snprintf(t, x, "%s%.256s%s", pref ? SMTP_REJPREFIX : "", + snprintf(buf, sizeof(buf), "%s%.256s%s", pref ? SMTP_REJPREFIX : "", smtp_status, crlf ? CRLF : ""); - t[x] = 0; - smtp_status = t; + buf[sizeof(buf) - 1] = 0; + smtp_status = buf; } if(spio_write_data(ctx, &(ctx->client), smtp_status) == -1) diff --git a/src/proxsmtpd.c b/src/proxsmtpd.c index b57f3b8..00c11e7 100644 --- a/src/proxsmtpd.c +++ b/src/proxsmtpd.c @@ -47,6 +47,7 @@ #include #include #include +#include #include "usuals.h" @@ -73,7 +74,7 @@ pxstate_t; * STRINGS */ -#define SMTP_REJECTED "550 Content Rejected\r\n" +#define REJECTED "Content Rejected" #define DEFAULT_CONFIG CONF_PREFIX "/proxsmtpd.conf" #define CFG_FILTERCMD "FilterCommand" @@ -107,6 +108,7 @@ pxstate_t g_pxstate; static void usage(); static int process_file_command(spctx_t* sp); static int process_pipe_command(spctx_t* sp); +static void final_reject_message(char* buf, int buflen); static void buffer_reject_message(char* data, char* buf, int buflen); static int kill_process(spctx_t* sp, pid_t pid); static int wait_process(spctx_t* sp, pid_t pid, int* status); @@ -379,9 +381,11 @@ static int process_file_command(spctx_t* sp) sp_message(sp, LOG_ERR, "couldn't read data from filter command"); RETURN(-1); } + + break; } - else if(r <= 0) + if(r == 0) break; /* Null terminate */ @@ -425,10 +429,12 @@ static int process_file_command(spctx_t* sp) /* Check code and use stderr if bad code */ else { - if(sp_fail_data(sp, ebuf[0] == 0 ? SMTP_REJECTED : ebuf) == -1) + final_reject_message(ebuf, sizeof(ebuf)); + + if(sp_fail_data(sp, ebuf) == -1) RETURN(-1); /* message already printed */ - sp_add_log(sp, "status=", ebuf[0] == 0 ? "FAILED" : ebuf); + sp_add_log(sp, "status=", ebuf); } ret = 0; @@ -741,19 +747,21 @@ static int process_pipe_command(spctx_t* sp) /* A successful response */ if(WEXITSTATUS(status) == 0) { - sp_add_log(sp, "status=", "FILTERED"); - if(sp_done_data(sp, NULL) == -1) RETURN(-1); /* message already printed */ + + sp_add_log(sp, "status=", "FILTERED"); } /* Check code and use stderr if bad code */ else { - sp_add_log(sp, "status=", ebuf[0] == 0 ? "FAILED" : ebuf); + final_reject_message(ebuf, sizeof(ebuf)); - if(sp_fail_data(sp, ebuf[0] == 0 ? SMTP_REJECTED : ebuf) == -1) + if(sp_fail_data(sp, ebuf) == -1) RETURN(-1); /* message already printed */ + + sp_add_log(sp, "status=", ebuf); } ret = 0; @@ -785,29 +793,61 @@ cleanup: return ret; } -static void buffer_reject_message(char* data, char* buf, int buflen) +static void final_reject_message(char* buf, int buflen) { - char* t; + if(buf[0] == 0) + strlcpy(buf, REJECTED, buflen); + else + trim_end(buf); +} - /* Take away all junk at beginning and end */ - data = trim_space(data); +static void buffer_reject_message(char* data, char* buf, int buflen) +{ + int len = strlen(data); + char* t = data + len; + int newline = 0; - /* - * Look for the last new line in the message. We - * don't care about stuff before that. - */ - t = strchr(data, '\n'); - if(t == NULL) + while(t > data && isspace(*(t - 1))) { - t = data; + t--; + + if(*t == '\n') + newline = 1; } - else + + /* No valid line */ + if(t > data) { - t++; - buf[0] = 0; /* Start a new message */ + if(newline) + *t = 0; + + t = strrchr(data, '\n'); + if(t == NULL) + { + t = trim_start(data); + + /* + * Basically if we already have a newline at the end + * then we need to start a new line + */ + if(buf[strlen(buf)] == '\n') + buf[0] = 0; + } + else + { + t = trim_start(t); + + /* Start a new line */ + buf[0] = 0; + } + + /* t points to a valid line */ + strlcat(buf, t, buflen); } - strlcat(buf, t, buflen); + /* Always append if we found a newline */ + if(newline) + strlcat(buf, "\n", buflen); } static int wait_process(spctx_t* sp, pid_t pid, int* status) -- cgit v1.2.3