Hi,

There has been a discussion a while ago about the issue of trailing CR
in smtp lines (https://marc.info/?l=openbsd-tech&m=156890805128121&w=2).

It is agreed that stripping an optional CR at io level is a problem
because the information is lost and there is no way to take a specific
action at the protocol level if needed.

So this diffs moves the CR stripping from io level to protocol level for
SMTP dialogs. Other uses of io_getline() are internal and expect simple LF
line ending. The current behavior should not change.

Comments?

Eric.


Index: bounce.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/bounce.c,v
retrieving revision 1.81
diff -u -p -r1.81 bounce.c
--- bounce.c    25 Nov 2019 12:11:26 -0000      1.81
+++ bounce.c    20 Apr 2020 08:06:43 -0000
@@ -728,6 +728,10 @@ bounce_io(struct io *io, int evt, void *
                if (line == NULL)
                        break;
 
+               /* Strip trailing '\r' */
+               if (len && line[len - 1] == '\r')
+                       line[--len] = '\0';
+
                log_trace(TRACE_BOUNCE, "bounce: %p: <<< %s", s, line);
 
                if ((error = parse_smtp_response(line, len, &msg, &cont))) {
Index: iobuf.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/iobuf.c,v
retrieving revision 1.12
diff -u -p -r1.12 iobuf.c
--- iobuf.c     3 Oct 2019 07:03:23 -0000       1.12
+++ iobuf.c     18 Mar 2020 20:25:41 -0000
@@ -174,10 +174,9 @@ iobuf_getline(struct iobuf *iobuf, size_
                         * the next call to iobuf_normalize() or iobuf_extend().
                         */
                        iobuf_drop(iobuf, i + 1);
-                       len = (i && buf[i - 1] == '\r') ? i - 1 : i;
-                       buf[len] = '\0';
+                       buf[i] = '\0';
                        if (rlen)
-                               *rlen = len;
+                               *rlen = i;
                        return (buf);
                }
 
Index: lka_filter.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/lka_filter.c,v
retrieving revision 1.61
diff -u -p -r1.61 lka_filter.c
--- lka_filter.c        17 Apr 2020 14:20:13 -0000      1.61
+++ lka_filter.c        19 Apr 2020 18:56:57 -0000
@@ -851,7 +851,7 @@ filter_data_internal(struct filter_sessi
 
        /* no filter_entry, we either had none or reached end of chain */
        if (filter_entry == NULL) {
-               io_printf(fs->io, "%s\r\n", line);
+               io_printf(fs->io, "%s\n", line);
                return;
        }
 
Index: mta_session.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/mta_session.c,v
retrieving revision 1.134
diff -u -p -r1.134 mta_session.c
--- mta_session.c       10 Apr 2020 19:28:57 -0000      1.134
+++ mta_session.c       19 Apr 2020 10:25:55 -0000
@@ -1257,6 +1257,10 @@ mta_io(struct io *io, int evt, void *arg
                        return;
                }
 
+               /* Strip trailing '\r' */
+               if (len && line[len - 1] == '\r')
+                       line[--len] = '\0';
+
                log_trace(TRACE_MTA, "mta: %p: <<< %s", s, line);
                mta_report_protocol_server(s, line);
 
Index: smtp_client.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/smtp_client.c,v
retrieving revision 1.13
diff -u -p -r1.13 smtp_client.c
--- smtp_client.c       24 Feb 2020 23:54:27 -0000      1.13
+++ smtp_client.c       19 Apr 2020 10:26:25 -0000
@@ -680,6 +680,10 @@ smtp_client_readline(struct smtp_client 
                return 0;
        }
 
+       /* Strip trailing '\r' */
+       if (len && line[len - 1] == '\r')
+               line[--len] = '\0';
+
        log_trace(TRACE_SMTPCLT, "%p: <<< %s", proto, line);
 
        /* Validate SMTP  */
Index: smtp_session.c
===================================================================
RCS file: /cvs/src/usr.sbin/smtpd/smtp_session.c,v
retrieving revision 1.425
diff -u -p -r1.425 smtp_session.c
--- smtp_session.c      15 Mar 2020 16:34:57 -0000      1.425
+++ smtp_session.c      20 Apr 2020 08:06:35 -0000
@@ -1132,6 +1132,10 @@ smtp_io(struct io *io, int evt, void *ar
                if (line == NULL)
                        return;
 
+               /* Strip trailing '\r' */
+               if (len && line[len - 1] == '\r')
+                       line[--len] = '\0';
+
                /* Message body */
                eom = 0;
                if (s->state == STATE_BODY) {

Reply via email to