]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-smtp: server: Fix forwarding a multi-line reply.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Fri, 26 Oct 2018 14:17:06 +0000 (16:17 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Thu, 21 Mar 2019 08:02:35 +0000 (10:02 +0200)
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().

src/lib-smtp/smtp-server-reply.c

index 780cb2ba0986dcf971ccf5f7b7bc3743f342bdc2..c2b349034b867722b57b5cea5d6bb5972a3eb605 100644 (file)
@@ -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;