From: Stephan Bosch Date: Fri, 26 Oct 2018 14:17:06 +0000 (+0200) Subject: lib-smtp: server: Fix forwarding a multi-line reply. X-Git-Tag: 2.3.6~98 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=587a53467c0ba95e0e4a2cc66f95536326e48a9d;p=thirdparty%2Fdovecot%2Fcore.git lib-smtp: server: Fix forwarding a multi-line reply. A multi-line reply had the '-' on the first line stripped upon sending, which makes clients see two separate responses rather than just one. This was caused by the fact that forwarded replies had the last_line field not set properly, in which case the '-' was substituted on the first line, rather than the last. The fix makes a forwarded reply indistinguishable from a normally created reply by also allowing for amending the reply with additional lines using smtp_server_reply_add_text(). --- diff --git a/src/lib-smtp/smtp-server-reply.c b/src/lib-smtp/smtp-server-reply.c index 780cb2ba09..c2b349034b 100644 --- a/src/lib-smtp/smtp-server-reply.c +++ b/src/lib-smtp/smtp-server-reply.c @@ -153,10 +153,36 @@ smtp_server_reply_create_forward(struct smtp_server_command *cmd, unsigned int index, const struct smtp_reply *from) { struct smtp_server_reply *reply; + string_t *textbuf; + char *text; + size_t last_line, i; reply = smtp_server_reply_create_index(cmd, index, from->status, smtp_reply_get_enh_code(from)); smtp_reply_write(reply->content->text, from); + + i_assert(reply->content != NULL); + textbuf = reply->content->text; + text = str_c_modifiable(textbuf); + + /* Find the last line */ + reply->content->last_line = last_line = 0; + for (i = 0; i < str_len(textbuf); i++) { + if (text[i] == '\n') { + reply->content->last_line = last_line; + last_line = i + 1; + } + } + + /* Make this reply suitable for further amendment with + smtp_server_reply_add_text() */ + if ((reply->content->last_line + 3) < str_len(textbuf)) { + i_assert(text[reply->content->last_line + 3] == ' '); + text[reply->content->last_line + 3] = '-'; + } else { + str_append_c(textbuf, '-'); + } + reply->forwarded = TRUE; return reply;