fetchmail-friends
[Top] [All Lists]

Re: [fetchmail] [PATCH] bug in 6.1.2 regarding broken headers

2002-11-25 04:06:56
Quoting from Gerd v. Egidy's mail on Sun, Nov 24, 2002 at 02:41:23AM +0100:
fetchmail: POP3> RETR 1
fetchmail: POP3< +OK 62984 octets
fetchmail: reading message XXX:1 of 3 (62984 octets)
fetchmail: message delimiter found while scanning headers
fetchmail: message XXX:1 was not the expected length (1122 actual != 62984 
expected)
fetchmail: SMTP>. (EOM)
fetchmail: smtp listener protocol errorfetchmail:  not flushed
fetchmail: POP3> RETR 2
fetchmail: POP3< Content-Type: text/plain; charset=iso-8859-1

This means that the previous claim of message delimiter found is
actually incorrect.

On going through the code, the headers_ok flag is set by readheaders()
to false not only on finding a delimiter line but also on finding a
header line without a colon.

There are two bugs in the code:

1) The search for end of headers is "\r\n". Thus, if there is a line
like "\n" or "\r\r\n", fetchmail does not treat it as end of headers.

I think the actual check should be a line of the form "\r*\n".

2) If there is a header line without a colon, fetchmail sets the
headers_ok flag to FALSE, does not continue reading, and returns
PS_TRUNCATED. Thus, the remaining header and body is not read at all.

Note that this stage is also reached if the end of headers like
"\r\r\n" has been missed because of 1) and fetchmail tries to
interpret the body as headers.

Here is a patch which fixes both the bugs.

===============================================================
diff -Naur fetchmail-6.1.2.orig/transact.c fetchmail-6.1.2/transact.c
--- fetchmail-6.1.2.orig/transact.c     Thu Oct 31 19:05:46 2002
+++ fetchmail-6.1.2/transact.c  Mon Nov 25 16:21:54 2002
@@ -339,6 +339,14 @@
 
 #define EMPTYLINE(s)   ((s)[0] == '\r' && (s)[1] == '\n' && (s)[2] == '\0')
 
+/* end of header is "\r*\n" */
+int eohline (const char *s)
+{
+    while (s[0] == '\r')
+       s++;
+    return (s[0] == '\n' && s[1] == '\0');
+}
+
 int readheaders(int sock,
                       long fetchlen,
                       long reallen,
@@ -476,7 +484,7 @@
                }
 
            /* check for end of headers */
-           if (EMPTYLINE(line))
+           if (eohline(line))
            {
                headers_ok = TRUE;
                has_nuls = (linelen != strlen(line));
@@ -506,10 +514,12 @@
             */
            if (!isspace(line[0]) && !strchr(line, ':'))
            {
-               headers_ok = FALSE;
-               has_nuls = (linelen != strlen(line));
-               free(line);
-               goto process_headers;
+               /* the headers_ok flag cannot be set to FALSE here as the
+                * delimiter has not occurred yet. */
+               if (outlevel > O_SILENT)
+                   report(stdout,
+                          GT_("incorrect header line found while scanning 
headers\n"));
+               retain_mail = 1;
            }
 
            /* check for RFC822 continuations */
===============================================================

Sunil Shetye.