]> Pileus Git - ~andy/fetchmail/blobdiff - driver.c
Deal with Post Office/NT.
[~andy/fetchmail] / driver.c
index 0bf6ec835a560880576ee55b3938fb25b8a3dbcd..5d8e2a95fed41b530003dc4ebe312e7c9f88fa75 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -11,6 +11,9 @@
 #include  <errno.h>
 #include  <ctype.h>
 #include  <string.h>
+#ifdef HAVE_MEMORY_H
+#include  <memory.h>
+#endif /* HAVE_MEMORY_H */
 #if defined(STDC_HEADERS)
 #include  <stdlib.h>
 #endif
 #include "mx.h"
 #endif /* HAVE_GETHOSTBYNAME */
 
-#ifdef SUNOS
-#include <memory.h>
-#endif
-
 #ifdef KERBEROS_V4
 #if defined (__bsdi__)
 #include <des.h> /* order of includes matters */
@@ -84,6 +83,9 @@ static int tagnum;
 
 static char *shroud;   /* string to shroud in debug output, if  non-NULL */
 static int mytimeout;  /* value of nonreponse timeout */
+#ifdef MSGLEN
+static int msglen;     /* actual message length */
+#endif /* MSGLEN */
 
 static void set_timeout(int timeleft)
 /* reset the nonresponse-timeout */
@@ -301,7 +303,7 @@ struct idlist **xmit_names; /* list of recipient names parsed out */
 static char *parse_received(struct query *ctl, char *bufp)
 /* try to extract real addressee from the Received line */
 {
-    char *ok;
+    char *ok = (char *)NULL;
     static char rbuf[HOSTLEN + USERNAMELEN + 4]; 
 
     /*
@@ -312,9 +314,7 @@ static char *parse_received(struct query *ctl, char *bufp)
      * address in the Received line.  Sendmail itself only
      * does this when the mail has a single recipient.
      */
-    if ((ok = strstr(bufp, "by ")) == (char *)NULL)
-       ok = (char *)NULL;
-    else
+    if ((ok = strstr(bufp, "by ")) && isspace(ok[-1]))
     {
        char    *sp, *tp;
 
@@ -331,30 +331,25 @@ static char *parse_received(struct query *ctl, char *bufp)
         * recipient name after a following "for".  Otherwise
         * punt.
         */
-       if (is_host_alias(rbuf, ctl))
-           ok = strstr(sp, "for ");
-       else
+       if (!is_host_alias(rbuf, ctl))
            ok = (char *)NULL;
-    }
-
-    if (ok != 0)
-    {
-       char    *sp, *tp;
-
-       tp = rbuf;
-       sp = ok + 4;
-       if (*sp == '<')
-           sp++;
-       while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
-           if (!isspace(*sp))
-               *tp++ = *sp++;
-           else
-           {
-               /* uh oh -- whitespace here can't be right! */
-               ok = (char *)NULL;
-               break;
-           }
-       *tp = '\0';
+       else if ((ok = strstr(sp, "for ")) && isspace(ok[-1]))
+       {
+           tp = rbuf;
+           sp = ok + 4;
+           if (*sp == '<')
+               sp++;
+           while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
+               if (!isspace(*sp))
+                   *tp++ = *sp++;
+               else
+               {
+                   /* uh oh -- whitespace here can't be right! */
+                   ok = (char *)NULL;
+                   break;
+               }
+           *tp = '\0';
+       }
     }
 
     if (!ok)
@@ -435,6 +430,9 @@ static int smtp_open(struct query *ctl)
        }
     }
 
+    if (outlevel >= O_VERBOSE && ctl->smtp_socket != -1)
+       error(0, 0, "forwarding to SMTP port on %s", ctl->smtphost);
+
     return(ctl->smtp_socket);
 }
 
@@ -443,8 +441,8 @@ static FILE *sinkfp;
 static RETSIGTYPE (*sigchld)();
 static int sizeticker;
 
-static int stuffline(struct query *ctl, char *buf, flag delimited)
-/* ship a line to the given control block's SMTP server */
+static int stuffline(struct query *ctl, char *buf)
+/* ship a line to the given control block's output sink (SMTP server or MDA) */
 {
     int        n;
 
@@ -466,7 +464,7 @@ static int stuffline(struct query *ctl, char *buf, flag delimited)
      * use .<CR><LF> as EOM.  If it does, the server will already have
      * decorated any . lines it sends back up.
      */
-    if (!delimited && *buf == '.')
+    if (!protocol->delimited && *buf == '.')
        if (sinkfp && ctl->mda)
            fputs(".", sinkfp);
        else if (ctl->smtp_socket != -1)
@@ -504,10 +502,10 @@ int num;          /* index of message */
     {
        int             offset;
        struct addrblk  *next;
-    } *addrchain, **chainptr = &addrchain;
+    } *addrchain = NULL, **chainptr = &addrchain;
     char buf[MSGBUFSIZE+1], return_path[MSGBUFSIZE+1]; 
     int        from_offs, ctt_offs, env_offs, next_address;
-    char *headers, *received_for;
+    char *headers, *received_for, *desthost;
     int n, linelen, oldlen, ch, remaining;
     char               *cp;
     struct idlist      *idp, *xmit_names;
@@ -526,7 +524,10 @@ int num;           /* index of message */
     headers = received_for = NULL;
     from_offs = ctt_offs = env_offs = -1;
     oldlen = 0;
-    for (remaining = len; remaining > 0; remaining -= linelen)
+#ifdef MSGLEN
+    msglen = 0;
+#endif /* MSGLEN */
+    for (remaining = len; remaining > 0 || protocol->delimited; remaining -= linelen)
     {
        char *line;
 
@@ -537,6 +538,9 @@ int num;            /* index of message */
            if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1)
                return(PS_SOCKET);
            linelen += n;
+#ifdef MSGLEN
+           msglen += n;
+#endif /* MSGLEN */
 
            /* lines may not be properly CRLF terminated; fix this for qmail */
            if (ctl->forcecr)
@@ -551,8 +555,9 @@ int num;            /* index of message */
            }
 
            set_timeout(ctl->server.timeout);
-           /* leave extra room for reply_hack to play with */
-           line = (char *) realloc(line, strlen(line) + strlen(buf) + HOSTLEN + 1);
+
+           line = (char *) realloc(line, strlen(line) + strlen(buf) +1);
+
            strcat(line, buf);
            if (line[0] == '\r' && line[1] == '\n')
                break;
@@ -581,9 +586,24 @@ int num;           /* index of message */
            break;
        }
      
-       /* maybe this is a special message that holds mailbox annotations? */ 
-       if (protocol->retain_hdr && protocol->retain_hdr(num, line))
-           return(PS_RETAINED);
+       /*
+        * The University of Washington IMAP server (the reference
+        * implementation of IMAP4 written by Mark Crispin) relies
+        * on being able to keep base-UID information in a special
+        * message at the head of the mailbox.  This message should
+        * neither be deleted nor forwarded.
+        */
+#ifdef POP2_ENABLE
+       /*
+        * We disable this check under POP2 because there's no way to
+        * prevent deletion of the message.  So at least we ought to 
+        * forward it to the user so he or she will have some clue
+        * that things have gone awry.
+        */
+       if (protocol != pop2)
+#endif /* POP2_ENABLE */
+           if (num == 1 && !strncasecmp(line, "X-IMAP:", 7))
+               return(PS_RETAINED);
 
        /*
         * This code prevents fetchmail from becoming an accessory after
@@ -660,7 +680,7 @@ int num;            /* index of message */
        }
 
        if (ctl->rewrite)
-           reply_hack(line, realname);
+           line = reply_hack(line, realname);
 
        if (!headers)
        {
@@ -819,13 +839,18 @@ int num;          /* index of message */
        int     length = 0;
        char    *names, *cmd;
 
+       desthost = "localhost";
+
        /*
         * We go through this in order to be able to handle very
         * long lists of users and (re)implement %s.
         */
        for (idp = xmit_names; idp; idp = idp->next)
            if (idp->val.num == XMIT_ACCEPT)
+           {
                length += (strlen(idp->id) + 1);
+               good_addresses++;
+           }
        names = (char *)alloca(length);
        names[0] = '\0';
        for (idp = xmit_names; idp; idp = idp->next)
@@ -872,7 +897,7 @@ int num;            /* index of message */
        if ((smtp_open(ctl) == -1))
        {
            free_str_list(&xmit_names);
-           error(0, -1, "SMTP connect to %s failed",
+           error(0, errno, "SMTP connect to %s failed",
                  ctl->smtphost ? ctl->smtphost : "localhost");
            return(PS_SMTP);
        }
@@ -991,13 +1016,17 @@ int num;         /* index of message */
         * or MX but not a CNAME.  Some listeners (like exim)
         * enforce this.
         */
+       desthost = ctl->smtphost ? ctl->smtphost : "localhost";
        for (idp = xmit_names; idp; idp = idp->next)
            if (idp->val.num == XMIT_ACCEPT)
            {
+               if (strchr(idp->id, '@'))
+                   strcpy(addr, idp->id);
+               else
 #ifdef HAVE_SNPRINTF
-               snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id,ctl->smtphost);
+                   snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, desthost);
 #else
-               sprintf(addr, "%s@%s", idp->id, ctl->smtphost);
+                   sprintf(addr, "%s@%s", idp->id, desthost);
 #endif /* HAVE_SNPRINTF */
 
                if (SMTP_rcpt(ctl->smtp_socket, addr) == SM_OK)
@@ -1013,12 +1042,12 @@ int num;                /* index of message */
        if (!good_addresses)
        {
 #ifdef HAVE_SNPRINTF
-           snprintf(addr, sizeof(addr)-1, "%s@%s", idp->id, ctl->smtphost);
+           snprintf(addr, sizeof(addr)-1, "%s@%s", user,  desthost);
 #else
-           sprintf(addr, "%s@%s", idp->id, ctl->smtphost);
+           sprintf(addr, "%s@%s", user, desthost);
 #endif /* HAVE_SNPRINTF */
 
-           if (SMTP_rcpt(ctl->smtp_socket, user) != SM_OK)
+           if (SMTP_rcpt(ctl->smtp_socket, addr) != SM_OK)
            {
                error(0, 0, "can't even send to calling user!");
                free(headers);
@@ -1030,27 +1059,41 @@ int num;                /* index of message */
        SMTP_data(ctl->smtp_socket);
     }
 
-    /* we may need to strip carriage returns */
-    if (ctl->stripcr)
+    /* utter any per-message Received information we need here */
     {
-       char    *sp, *tp;
+       time_t  now;
 
-       for (sp = tp = headers; *sp; sp++)
-           if (*sp != '\r')
-               *tp++ =  *sp;
-       *tp = '\0';
+       /* write a line describing fetchmail's processing of the message */
+       sprintf(buf,
+       "Received: from %s\r\n\tby %s (fetchmail-%s %s run by %s)\r\n",
+               ctl->server.truename, 
+               fetchmailhost, 
+               RELEASE_ID,
+               protocol->name,
+               ctl->remotename);
 
+       if (!good_addresses)
+           sprintf(buf + strlen(buf), 
+                   "\tfor <%s@%s> (by default); ",
+                   user, desthost);
+       if (good_addresses == 1)
+       {
+           for (idp = xmit_names; idp; idp = idp->next)
+               if (idp->val.num == XMIT_ACCEPT)
+               {
+                   sprintf(buf + strlen(buf), "\tfor <%s@%s> (%s); ",
+                           idp->id, desthost,
+                           MULTIDROP(ctl) ? "multi-drop" : "single-drop");
+                   break;      /* only report first address */
+               }
+       }
+       time(&now);
+       strcat(buf, ctime(&now));
+       strcpy(buf + strlen(buf) - 1, "\r\n");
     }
 
-    /* write all the headers */
-    n = 0;
-    if (ctl->mda && sinkfp)
-       n = fwrite(headers, 1, strlen(headers), sinkfp);
-    else if (ctl->smtp_socket != -1)
-       n = SockWrite(ctl->smtp_socket, headers, strlen(headers));
-    free(headers);
-
-    if (n < 0)
+    /* ship out the synthetic Received line and the headers */
+    if (stuffline(ctl, buf) < 0 || stuffline(ctl, headers) < 0)
     {
        error(0, errno, "writing RFC822 headers");
        if (ctl->mda)
@@ -1119,9 +1162,11 @@ int num;         /* index of message */
 
        }
 
+       strcat(errmsg, "\n");
+
        /* ship out the error line */
        if (sinkfp)
-           stuffline(ctl, errmsg, protocol->delimited);
+           stuffline(ctl, errmsg);
     }
 
     free_str_list(&xmit_names);
@@ -1131,24 +1176,23 @@ int num;                /* index of message */
     *cp++ = '\r';
     *cp++ = '\n';
     *cp++ = '\0';
-    stuffline(ctl, buf, protocol->delimited);
+    stuffline(ctl, buf);
 
     return(PS_SUCCESS);
 }
 
-static int readbody(sock, ctl, forward, len, delimited)
+static int readbody(sock, ctl, forward, len)
 /* read and dispose of a message body presented on sock */
 struct query *ctl;     /* query control record */
 int sock;              /* to which the server is connected */
 int len;               /* length of message */
 flag forward;          /* TRUE to forward */
-flag delimited;                /* does the protocol use a message delimiter? */
 {
     int        linelen;
     char buf[MSGBUFSIZE+1], *cp;
 
     /* pass through the text lines */
-    while (delimited || len > 0)
+    while (protocol->delimited || len > 0)
     {
        if ((linelen = SockRead(sock, buf, sizeof(buf)-1)) == -1)
        {
@@ -1173,9 +1217,12 @@ flag delimited;          /* does the protocol use a message delimiter? */
            }
        }
        len -= linelen;
+#ifdef MSGLEN
+       msglen += linelen;
+#endif /* MSGLEN */
 
        /* check for end of message */
-       if (delimited && *buf == '.')
+       if (protocol->delimited && *buf == '.')
            if (buf[1] == '\r' && buf[2] == '\n' && buf[3] == '\0')
                break;
            else if (buf[1] == '\n' && buf[2] == '\0')
@@ -1184,7 +1231,7 @@ flag delimited;           /* does the protocol use a message delimiter? */
        /* ship out the text line */
        if (forward)
        {
-           int n = stuffline(ctl, buf, delimited);
+           int n = stuffline(ctl, buf);
 
            if (n < 0)
            {
@@ -1493,7 +1540,7 @@ const struct method *proto;       /* protocol method table */
                (void) sprintf(buf, "%s@%s", ctl->remotename, realname);
            if (outlevel > O_SILENT)
                if (count == -1)                /* only used for ETRN */
-                   error(0, 0, "Polling %s", buf);
+                   error(0, 0, "Polling %s", realname);
                else if (count == 0)
                {
                    /* these are pointless in normal daemon mode */
@@ -1546,12 +1593,12 @@ const struct method *proto;     /* protocol method table */
                force_retrieval = !peek_capable && (ctl->errcount > 0);
 
                /*
-                * We may need to get sizes in order to check message
-                * limits.  Or it may be forced because the fetch methods
-                * don't return reliable sizes.
+                * We need the size of each method before it's loaded in
+                * order to pass via the ESMTP SIZE option.  If the protocol
+                * has a getsizes method, we presume this means it doesn't
+                * get reliable sizes from message fetch responses. 
                 */
-               msgsizes = (int *)NULL;
-               if (proto->getsizes && (proto->force_getsizes || ctl->limit > 0))
+               if (proto->getsizes)
                {
                    msgsizes = (int *)alloca(sizeof(int) * count);
 
@@ -1572,6 +1619,23 @@ const struct method *proto;      /* protocol method table */
                    flag suppress_forward = FALSE;
                    flag retained = FALSE;
 
+                   /*
+                    * This check copes with Post Office/NT's annoying habit
+                    * of randomly prepending bogus LIST items of length -1.
+                    * Patrick Audley <paudley@pobox.com> tells us:
+                    * LIST shows a size of -1, RETR and TOP return
+                    * "-ERR System error - couldn't open message", and DELE
+                    * succeeds but doesn't actually delete the message.
+                    */
+                   if (msgsizes && msgsizes[num-1] == -1)
+                   {
+                       if (outlevel >= O_VERBOSE)
+                           error(0, 0, 
+                                 "Skipping message %d, length -1",
+                                 num - 1);
+                       continue;
+                   }
+
                    /* we may want to reject this message if it's old */
                    if (!fetch_it)
                    {
@@ -1645,7 +1709,7 @@ const struct method *proto;       /* protocol method table */
                            {
                                if ((ok=(protocol->fetch_body)(sock,ctl,num,&len)))
                                    goto cleanUp;
-                               if (outlevel > O_SILENT && !msgsizes)
+                               if (outlevel > O_SILENT && !wholesize)
                                    error_build(" (%d body bytes) ", len);
                                set_timeout(ctl->server.timeout);
                            }
@@ -1657,8 +1721,7 @@ const struct method *proto;       /* protocol method table */
                            ok = readbody(sock,
                                          ctl,
                                          !suppress_forward,
-                                         len,
-                                         protocol->delimited);
+                                         len);
                            if (ok == PS_TRANSIENT)
                                suppress_delete = suppress_forward = TRUE;
                            else if (ok)
@@ -1675,6 +1738,12 @@ const struct method *proto;      /* protocol method table */
                            }
                        }
 
+#ifdef MSGLEN
+                       /* check to see if the numbers matched? */
+                       if (msgsizes && msglen != msgsizes[num-1])
+                           error(0, 0, "size of message %d (%d) was not what was expected (%d)", num, msglen, msgsizes[num-1]);
+#endif /* MSGLEN */
+
                        /* end-of-message processing starts here */
                        if (outlevel == O_VERBOSE)
                            fputc('\n', stderr);
@@ -1788,6 +1857,9 @@ const struct method *proto;       /* protocol method table */
     case PS_SMTP:
        msg = "SMTP transaction";
        break;
+    case PS_DNS:
+       msg = "DNS lookup";
+       break;
     case PS_UNDEFINED:
        error(0, 0, "undefined");
        break;