]> Pileus Git - ~andy/fetchmail/blobdiff - driver.c
This went to Craig Metz.
[~andy/fetchmail] / driver.c
index c6a4ab99bd01421c4f18d588f5041fc7ef289af0..1be1dc0e36d76a78a80d51085411fc0e286bd742 100644 (file)
--- a/driver.c
+++ b/driver.c
 #include  <sys/time.h>
 #include  <signal.h>
 
+#ifndef HAVE_STRFTIME /* For ctime prototype */
+#include  <sys/types.h>
+#include  <time.h>
+#endif
+
 #ifdef HAVE_GETHOSTBYNAME
 #include <netdb.h>
 #include "mx.h"
 #define SIGCHLD        SIGCLD
 #endif
 
+#if INET6
+#define        SMTP_PORT       "smtp"  /* standard SMTP service port */
+#else /* INET6 */
 #define        SMTP_PORT       25      /* standard SMTP service port */
+#endif /* INET6 */
 
 #ifndef strstr         /* glibc-2.1 declares this as a macro */
 extern char *strstr(); /* needed on sysV68 R3V7.1. */
@@ -92,6 +101,12 @@ static char *shroud;        /* string to shroud in debug output, if  non-NULL */
 static int mytimeout;  /* value of nonreponse timeout */
 static int msglen;     /* actual message length */
 
+/* use these to track what was happening when the nonresponse timer fired */
+#define GENERAL_WAIT   0
+#define SERVER_WAIT    1
+#define FORWARDING_WAIT        2
+static phase;
+
 static void set_timeout(int timeleft)
 /* reset the nonresponse-timeout */
 {
@@ -142,7 +157,7 @@ static int is_host_alias(const char *name, struct query *ctl)
      * name doesn't match any is it time to call the bind library.
      * If this happens odds are good we're looking at an MX name.
      */
-    if (strcmp(lead_server->truename, name) == 0)
+    if (strcasecmp(lead_server->truename, name) == 0)
        return(TRUE);
     else if (str_in_list(&lead_server->akalist, name))
        return(TRUE);
@@ -163,7 +178,7 @@ static int is_host_alias(const char *name, struct query *ctl)
      */
     else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
     {
-       if (strcmp(ctl->server.truename, he->h_name) == 0)
+       if (strcasecmp(ctl->server.truename, he->h_name) == 0)
            goto match;
        else
            return(FALSE);
@@ -215,7 +230,7 @@ static int is_host_alias(const char *name, struct query *ctl)
     else
     {
        for (mxp = mxrecords; mxp->name; mxp++)
-           if (strcmp(ctl->server.truename, mxp->name) == 0)
+           if (strcasecmp(ctl->server.truename, mxp->name) == 0)
                goto match;
        return(FALSE);
     match:;
@@ -272,59 +287,68 @@ struct idlist **xmit_names;       /* list of recipient names parsed out */
     {
        char    *cp;
 
-       if ((cp = nxtaddr(hdr)) != (char *)NULL)
-           do {
-               char    *atsign;
-
-               if ((atsign = strchr(cp, '@')))
-               {
-                   struct idlist       *idp;
+       for (cp = nxtaddr(hdr);
+            cp != NULL;
+            cp = nxtaddr(NULL))
+       {
+           char        *atsign;
 
-                   /*
-                    * Does a trailing segment of the hostname match something
-                    * on the localdomains list?  If so, save the whole name
-                    * and keep going.
-                    */
-                   for (idp = ctl->server.localdomains; idp; idp = idp->next)
-                   {
-                       char    *rhs;
+           if ((atsign = strchr(cp, '@'))) {
+               struct idlist   *idp;
 
-                       rhs = atsign + (strlen(atsign) - strlen(idp->id));
-                       if ((rhs[-1] == '.' || rhs[-1] == '@')
-                                       && strcasecmp(rhs, idp->id) == 0)
-                       {
-                           if (outlevel == O_VERBOSE)
-                               error(0, 0, "passed through %s matching %s", 
-                                     cp, idp->id);
-                           save_str(xmit_names, XMIT_ACCEPT, cp);
-                           accept_count++;
-                           continue;
-                       }
-                   }
+               /*
+                * Does a trailing segment of the hostname match something
+                * on the localdomains list?  If so, save the whole name
+                * and keep going.
+                */
+               for (idp = ctl->server.localdomains; idp; idp = idp->next) {
+                   char        *rhs;
 
-                   /*
-                    * Check to see if the right-hand part is an alias
-                    * or MX equivalent of the mailserver.  If it's
-                    * not, skip this name.  If it is, we'll keep
-                    * going and try to find a mapping to a client name.
-                    */
-                   if (!is_host_alias(atsign+1, ctl))
+                   rhs = atsign + (strlen(atsign) - strlen(idp->id));
+                   if (rhs > atsign &&
+                       (rhs[-1] == '.' || rhs[-1] == '@') &&
+                       strcasecmp(rhs, idp->id) == 0)
                    {
-                       save_str(xmit_names, XMIT_REJECT, cp);
-                       reject_count++;
-                       continue;
+                       if (outlevel == O_VERBOSE)
+                           error(0, 0, "passed through %s matching %s", 
+                                 cp, idp->id);
+                       save_str(xmit_names, XMIT_ACCEPT, cp);
+                       accept_count++;
+                       break;
                    }
-                   atsign[0] = '\0';
                }
+               /* if we matched a local domain, idp != NULL */
+               if (idp) continue;
 
-               map_name(cp, ctl, xmit_names);
-           } while
-               ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
+               /*
+                * Check to see if the right-hand part is an alias
+                * or MX equivalent of the mailserver.  If it's
+                * not, skip this name.  If it is, we'll keep
+                * going and try to find a mapping to a client name.
+                */
+               if (!is_host_alias(atsign+1, ctl))
+               {
+                   save_str(xmit_names, XMIT_REJECT, cp);
+                   reject_count++;
+                   continue;
+               }
+               atsign[0] = '\0';
+           }
+
+           map_name(cp, ctl, xmit_names);
+       }
     }
 }
 
 static char *parse_received(struct query *ctl, char *bufp)
-/* try to extract real addressee from the Received line */
+/* try to extract real address from the Received line */
+/* If a valid Received: line is found, we return the full address in
+ * a buffer wich can be parsed from nxtaddr().  This is to ansure that
+ * the local domain part of the address can be passed along in 
+ * find_server_names() if it contains one.
+ * Note: We should return a dummy header containing the address 
+ * which makes nxtaddr() behave correctly. 
+ */
 {
     char *ok = (char *)NULL;
     static char rbuf[HOSTLEN + USERNAMELEN + 4]; 
@@ -354,18 +378,20 @@ static char *parse_received(struct query *ctl, char *bufp)
         * recipient name after a following "for".  Otherwise
         * punt.
         */
-       if (!is_host_alias(rbuf, ctl))
-           ok = (char *)NULL;
-       else if ((ok = strstr(sp, "for ")) && isspace(ok[-1]))
+       if (is_host_alias(rbuf, ctl) &&
+           (ok = strstr(sp, "for ")) && 
+           isspace(ok[-1]))
        {
            tp = rbuf;
            sp = ok + 4;
+           *tp++ = ':';        /* Here is the hack.  This is to be friend */
+           *tp++ = ' ';        /* with nxtaddr()... */
            if (*sp == '<')
                sp++;
            while (*sp == '@')          /* skip routes */
-               while (*sp++ != ':')
+               while (*sp && *sp++ != ':')
                    continue;
-           while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
+           while (*sp && *sp != '>' && *sp != ';')
                if (!isspace(*sp))
                    *tp++ = *sp++;
                else
@@ -374,16 +400,24 @@ static char *parse_received(struct query *ctl, char *bufp)
                    ok = (char *)NULL;
                    break;
                }
+           *tp++ = '\n';
            *tp = '\0';
-       }
+           if (strlen(rbuf) <= 3)      /* apparently nothing has been found */
+               ok = NULL;
+       } else
+           ok = (char *)NULL;
     }
 
     if (!ok)
        return(NULL);
     else
     {
-       if (outlevel == O_VERBOSE)
-           error(0, 0, "found Received address `%s'", rbuf);
+       if (outlevel == O_VERBOSE) {
+           char *lf = rbuf + strlen(rbuf)-1;
+           *lf = '\0';
+           error(0, 0, "found Received address `%s'", rbuf+2);
+           *lf = '\n';
+       }
        return(rbuf);
     }
 }
@@ -476,7 +510,7 @@ static int sizeticker;
 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;
+    int        n, oldphase;
     char *last;
 
     /* The line may contain NUL characters. Find the last char to use
@@ -497,6 +531,9 @@ static int stuffline(struct query *ctl, char *buf)
        }
     }
 
+    oldphase = phase;
+    phase = FORWARDING_WAIT;
+
     /*
      * SMTP byte-stuffing.  We only do this if the protocol does *not*
      * use .<CR><LF> as EOM.  If it does, the server will already have
@@ -536,6 +573,8 @@ static int stuffline(struct query *ctl, char *buf)
     else if (ctl->smtp_socket != -1)
        n = SockWrite(ctl->smtp_socket, buf, last - buf);
 
+    phase = oldphase;
+
     return(n);
 }
 
@@ -551,25 +590,33 @@ int num;          /* index of message */
     {
        int             offset;
        struct addrblk  *next;
-    } *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, *destaddr, *rcv;
-    int n, linelen, oldlen, ch, remaining, skipcount;
-    char               *cp;
+    };
+    struct addrblk     *to_addrchain = NULL;
+    struct addrblk     **to_chainptr = &to_addrchain;
+    struct addrblk     *resent_to_addrchain = NULL;
+    struct addrblk     **resent_to_chainptr = &resent_to_addrchain;
+
+    char               buf[MSGBUFSIZE+1];
+    char               return_path[HOSTLEN + USERNAMELEN + 4]; 
+    int                        from_offs, reply_to_offs, resent_from_offs;
+    int                        app_from_offs, sender_offs, resent_sender_offs;
+    int                        ctt_offs, env_offs;
+    char               *headers, *received_for, *destaddr, *rcv, *cp;
+    int                n, linelen, oldlen, ch, remaining, skipcount;
     struct idlist      *idp, *xmit_names;
     flag               good_addresses, bad_addresses, has_nuls;
     flag               no_local_matches = FALSE;
     int                        olderrs;
 
-    next_address = sizeticker = 0;
+    sizeticker = 0;
     has_nuls = FALSE;
     return_path[0] = '\0';
     olderrs = ctl->errcount;
 
     /* read message headers */
     headers = received_for = NULL;
-    from_offs = ctt_offs = env_offs = -1;
+    from_offs = reply_to_offs = resent_from_offs = app_from_offs = 
+       sender_offs = resent_sender_offs = ctt_offs = env_offs = -1;
     oldlen = 0;
     msglen = 0;
     skipcount = 0;
@@ -582,8 +629,11 @@ int num;           /* index of message */
        linelen = 0;
        line[0] = '\0';
        do {
-           if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1)
+           if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1) {
+               free(line);
+               free(headers);
                return(PS_SOCKET);
+           }
            linelen += n;
            msglen += n;
 
@@ -647,8 +697,11 @@ int num;           /* index of message */
         */
        if (protocol->port != 109)
 #endif /* POP2_ENABLE */
-           if (num == 1 && !strncasecmp(line, "X-IMAP:", 7))
+           if (num == 1 && !strncasecmp(line, "X-IMAP:", 7)) {
+               free(line);
+               free(headers);
                return(PS_RETAINED);
+           }
 
        /*
         * This code prevents fetchmail from becoming an accessory after
@@ -687,7 +740,8 @@ int num;            /* index of message */
         * unconditionally.  Nonempty ones get chucked if the user
         * turns on the dropstatus flag.
         */
-       if (!strncasecmp(line, "Status:", 7))
+       if (!strncasecmp(line, "Status:", 7) 
+                               || !strncasecmp(line, "X-Mozilla-Status:", 7))
        {
            char        *cp;
 
@@ -701,6 +755,9 @@ int num;            /* index of message */
            }
        }
 
+       if (ctl->rewrite)
+           line = reply_hack(line, ctl->server.truename);
+
        /*
         * OK, this is messy.  If we're forwarding by SMTP, it's the
         * SMTP-receiver's job (according to RFC821, page 22, section
@@ -717,16 +774,15 @@ int num;          /* index of message */
         * envelope sender from the Return-Path, the new Return-Path should be
         * exactly the same as the original one.
         */
-       if (!ctl->mda && !strncasecmp("Return-Path:", line, 12))
+       if (!strncasecmp("Return-Path:", line, 12))
        {
            strcpy(return_path, nxtaddr(line));
-           free(line);
-           continue;
+           if (!ctl->mda) {
+               free(line);
+               continue;
+           }
        }
 
-       if (ctl->rewrite)
-           line = reply_hack(line, ctl->server.truename);
-
        if (!headers)
        {
            oldlen = strlen(line);
@@ -741,20 +797,29 @@ int num;          /* index of message */
 
            newlen = oldlen + strlen(line);
            headers = (char *) realloc(headers, newlen + 1);
-           if (headers == NULL)
+           if (headers == NULL) {
+               free(line);
                return(PS_IOERR);
+           }
            strcpy(headers + oldlen, line);
            free(line);
            line = headers + oldlen;
            oldlen = newlen;
        }
 
-       if (from_offs == -1 && !strncasecmp("From:", line, 5))
-           from_offs = (line - headers);
-       else if (from_offs == -1 && !strncasecmp("Resent-From:", line, 12))
-           from_offs = (line - headers);
-       else if (from_offs == -1 && !strncasecmp("Apparently-From:", line, 16))
+       if (!strncasecmp("From:", line, 5))
            from_offs = (line - headers);
+       else if (!strncasecmp("Reply-To:", line, 9))
+           reply_to_offs = (line - headers);
+       else if (!strncasecmp("Resent-From:", line, 12))
+           resent_from_offs = (line - headers);
+       else if (!strncasecmp("Apparently-From:", line, 16))
+           app_from_offs = (line - headers);
+       else if (!strncasecmp("Sender:", line, 7))
+           sender_offs = (line - headers);
+       else if (!strncasecmp("Resent_Sender:", line, 14))
+           resent_sender_offs = (line - headers);
+
        else if (!strncasecmp("Content-Transfer-Encoding:", line, 26))
            ctt_offs = (line - headers);
        else if (!strncasecmp("Message-Id:", buf, 11 ))
@@ -775,18 +840,29 @@ int num;          /* index of message */
 
        else if (!strncasecmp("To:", line, 3)
                        || !strncasecmp("Cc:", line, 3)
-                       || !strncasecmp("Bcc:", line, 4))
+                       || !strncasecmp("Bcc:", line, 4)
+                       || !strncasecmp("Apparently-To:", line, 14))
+       {
+           *to_chainptr = xmalloc(sizeof(struct addrblk));
+           (*to_chainptr)->offset = (line - headers);
+           to_chainptr = &(*to_chainptr)->next; 
+           *to_chainptr = NULL;
+       }
+
+       else if (!strncasecmp("Resent-To:", line, 10)
+                       || !strncasecmp("Resent-Cc:", line, 10)
+                       || !strncasecmp("Resent-Bcc:", line, 11))
        {
-           *chainptr = xmalloc(sizeof(struct addrblk));
-           (*chainptr)->offset = (line - headers);
-           chainptr = &(*chainptr)->next; 
-           *chainptr = NULL;
+           *resent_to_chainptr = xmalloc(sizeof(struct addrblk));
+           (*resent_to_chainptr)->offset = (line - headers);
+           resent_to_chainptr = &(*resent_to_chainptr)->next; 
+           *resent_to_chainptr = NULL;
        }
 
        else if (ctl->server.envelope != STRING_DISABLED)
        {
            if (ctl->server.envelope 
-                       && strcasecmp(ctl->server.envelope, "received"))
+                       && strcasecmp(ctl->server.envelope, "Received"))
            {
                if (env_offs == -1 && !strncasecmp(ctl->server.envelope,
                                                line,
@@ -832,6 +908,32 @@ int num;           /* index of message */
      * In fact we have to, as this will tell us where to forward to.
      */
 
+    /*
+     * If there is a Return-Path address on the message, this was
+     * almost certainly the MAIL FROM address given the originating
+     * sendmail.  This is the best thing to use for logging the
+     * message origin (it sets up the right behavior for bounces and
+     * mailing lists).  Otherwise, fall down to the next available 
+     * envelope address wich is the most probable real sender 
+     * respectively.  *** The order is important! ***
+     * This is especially useful when receiving mailing list
+     * messages in multidrop mode.  if a local address doesn't
+     * exist, the bounce message won't be returned blindly to the 
+     * author or to the list itself but rather to the list manager
+     * (ex: specified by "Sender:") wich is less anoying.  This is
+     * true for most mailing list packages.
+     */
+    if( !return_path[0] ){
+       char *ap = NULL;
+       if (resent_sender_offs >= 0 && (ap = nxtaddr(headers + resent_sender_offs)));
+       else if (sender_offs >= 0 && (ap = nxtaddr(headers + sender_offs)));
+       else if (resent_from_offs >= 0 && (ap = nxtaddr(headers + resent_from_offs)));
+       else if (from_offs >= 0 && (ap = nxtaddr(headers + from_offs)));
+       else if (reply_to_offs >= 0 && (ap = nxtaddr(headers + reply_to_offs)));
+       else if (app_from_offs >= 0 && (ap = nxtaddr(headers + app_from_offs)));
+       if (ap) strcpy( return_path, ap );
+    }
+
     /* cons up a list of local recipients */
     xmit_names = (struct idlist *)NULL;
     bad_addresses = good_addresses = accept_count = reject_count = 0;
@@ -845,22 +947,36 @@ int num;          /* index of message */
             * We have the Received for addressee.  
             * It has to be a mailserver address, or we
             * wouldn't have got here.
+            * We use find_server_names() to let local 
+            * hostnames go through.
             */
-           map_name(received_for, ctl, &xmit_names);
-       else
-       {
+           find_server_names(received_for, ctl, &xmit_names);
+       else {
            /*
             * We haven't extracted the envelope address.
-            * So check all the header addresses.
+            * So check all the "Resent-To" header addresses if 
+            * they exist.  If and only if they don't, consider
+            * the "To" adresses.
             */
-           while (addrchain)
-           {
-               register struct addrblk *nextptr;
-
-               find_server_names(headers+addrchain->offset, ctl, &xmit_names);
-               nextptr = addrchain->next;
-               free(addrchain);
-               addrchain = nextptr;
+           register struct addrblk *nextptr;
+           if (resent_to_addrchain) {
+               /* delete the "To" chain and substitute it 
+                * with the "Resent-To" list 
+                */
+               while (to_addrchain) {
+                   nextptr = to_addrchain->next;
+                   free(to_addrchain);
+                   to_addrchain = nextptr;
+               }
+               to_addrchain = resent_to_addrchain;
+               resent_to_addrchain = NULL;
+           }
+           /* now look for remaining adresses */
+           while (to_addrchain) {
+               find_server_names(headers+to_addrchain->offset, ctl, &xmit_names);
+               nextptr = to_addrchain->next;
+               free(to_addrchain);
+               to_addrchain = nextptr;
            }
        }
        if (!accept_count)
@@ -885,6 +1001,7 @@ int num;           /* index of message */
        if (outlevel == O_VERBOSE)
            error(0,0, "forwarding and deletion suppressed due to DNS errors");
        free(headers);
+       free_str_list(&xmit_names);
        return(PS_TRANSIENT);
     }
     else if (ctl->mda)         /* we have a declared MDA */
@@ -949,7 +1066,7 @@ int num;           /* index of message */
        /* substitute From address for %F */
        if ((cp = strstr(before, "%F")))
        {
-           char *from = nxtaddr(headers + from_offs);
+           char *from = return_path;
            char        *sp;
 
            /* \177 had better be out-of-band for MDA commands */
@@ -997,6 +1114,8 @@ int num;           /* index of message */
        if (!sinkfp)
        {
            error(0, 0, "MDA open failed");
+           free(headers);
+           free_str_list(&xmit_names);
            return(PS_IOERR);
        }
 
@@ -1011,6 +1130,7 @@ int num;          /* index of message */
        {
            error(0, errno, "SMTP connect to %s failed",
                  ctl->smtphost ? ctl->smtphost : "localhost");
+           free(headers);
            free_str_list(&xmit_names);
            return(PS_SMTP);
        }
@@ -1036,14 +1156,8 @@ int num;         /* index of message */
            sprintf(options + strlen(options), " SIZE=%ld", reallen);
 
        /*
-        * If there is a Return-Path address on the message, this was
-        * almost certainly the MAIL FROM address given the originating
-        * sendmail.  This is the best thing to use for logging the
-        * message origin (it sets up the right behavior for bounces and
-        * mailing lists).  Otherwise, take the From address.
-        *
-        * Try to get the SMTP listener to take the Return-Path or
-        * From address as MAIL FROM .  If it won't, fall back on the
+        * Try to get the SMTP listener to take the Return-Path
+        * address as MAIL FROM .  If it won't, fall back on the
         * calling-user ID.  This won't affect replies, which use the
         * header From address anyway.
         *
@@ -1058,11 +1172,7 @@ int num;         /* index of message */
         * didn't pass canonicalized From/Return-Path lines, *and* the
         * local SMTP listener insists on them.
         */
-       ap = (char *)NULL;
-       if (return_path[0])
-           ap = return_path;
-       else if (from_offs == -1 || !(ap = nxtaddr(headers + from_offs)))
-           ap = user;
+       ap = (return_path[0]) ? return_path : user;
        if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
        {
            int smtperr = atoi(smtp_response);
@@ -1088,6 +1198,7 @@ int num;          /* index of message */
                 * don't prevent it from being deleted.
                 */
                free(headers);
+               free_str_list(&xmit_names);
                return(PS_REFUSED);
 
            case 452: /* insufficient system storage */
@@ -1099,6 +1210,7 @@ int num;          /* index of message */
                 */
                SMTP_rset(ctl->smtp_socket);    /* required by RFC1870 */
                free(headers);
+               free_str_list(&xmit_names);
                return(PS_TRANSIENT);
 
            case 552: /* message exceeds fixed maximum message size */
@@ -1109,6 +1221,7 @@ int num;          /* index of message */
                 */
                SMTP_rset(ctl->smtp_socket);    /* required by RFC1870 */
                free(headers);
+               free_str_list(&xmit_names);
                return(PS_REFUSED);
 
            default:    /* retry with invoking user's address */
@@ -1116,6 +1229,7 @@ int num;          /* index of message */
                {
                    error(0, -1, "SMTP error: %s", smtp_response);
                    free(headers);
+                   free_str_list(&xmit_names);
                    return(PS_SMTP);    /* should never happen */
                }
            }
@@ -1165,6 +1279,7 @@ int num;          /* index of message */
            {
                error(0, 0, "can't even send to calling user!");
                free(headers);
+               free_str_list(&xmit_names);
                return(PS_SMTP);
            }
        }
@@ -1262,6 +1377,8 @@ int num;          /* index of message */
                pclose(sinkfp);
            signal(SIGCHLD, sigchld);
        }
+       free(headers);
+       free_str_list(&xmit_names);
        return(PS_IOERR);
     }
     else if (outlevel == O_VERBOSE)
@@ -1324,8 +1441,6 @@ int num;          /* index of message */
            stuffline(ctl, errmsg);
     }
 
-    free_str_list(&xmit_names);
-
     /* issue the delimiter line */
     cp = buf;
     *cp++ = '\r';
@@ -1333,6 +1448,8 @@ int num;          /* index of message */
     *cp++ = '\0';
     stuffline(ctl, buf);
 
+    free(headers);
+    free_str_list(&xmit_names);
     return(PS_SUCCESS);
 }
 
@@ -1502,20 +1619,35 @@ const struct method *proto;     /* protocol method table */
 
     if ((js = setjmp(restart)) == 1)
     {
-       error(0, 0,
-               "timeout after %d seconds waiting for %s.",
-               ctl->server.timeout, ctl->server.pollname);
+       if (phase == SERVER_WAIT)
+           error(0, 0,
+                 "timeout after %d seconds waiting for server %s.",
+                 ctl->server.timeout, ctl->server.pollname);
+       else if (phase == FORWARDING_WAIT)
+           error(0, 0,
+                 "timeout after %d seconds waiting for %s.",
+                 ctl->server.timeout,
+                 ctl->mda ? "MDA" : "SMTP");
+       else
+           error(0, 0, "timeout after %d seconds.", ctl->server.timeout);
+
        if (ctl->smtp_socket != -1)
            close(ctl->smtp_socket);
        if (sock != -1)
            close(sock);
+       if (sinkfp)
+           pclose(sinkfp);
        ok = PS_ERROR;
     }
     else
     {
        char buf [POPBUFSIZE+1], *realhost;
        int *msgsizes, len, num, count, new, deletions = 0;
+#if INET6
+       int fetches, dispatches;
+#else /* INET6 */
        int port, fetches, dispatches;
+#endif /* INET6 */
        struct idlist *idp;
 
        /* execute pre-initialization command, if any */
@@ -1528,10 +1660,19 @@ const struct method *proto;     /* protocol method table */
        }
 
        /* open a socket to the mail server */
+#if !INET6
        port = ctl->server.port ? ctl->server.port : protocol->port;
+#endif /* !INET6 */
        realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
+#if INET6
+       if ((sock = SockOpen(realhost, 
+                            ctl->server.service ? ctl->server.service : protocol->service,
+                            ctl->server.netsec)) == -1)
+#else /* INET6 */
        if ((sock = SockOpen(realhost, port)) == -1)
+#endif /* INET6 */
        {
+#if !INET6
 #ifndef EHOSTUNREACH
 #define EHOSTUNREACH (-1)
 #endif
@@ -1539,6 +1680,7 @@ const struct method *proto;       /* protocol method table */
            {
                error_build("fetchmail: %s connection to %s failed: ", 
                             protocol->name, ctl->server.pollname);
+#ifdef HAVE_RES_SEARCH
                if (h_errno == HOST_NOT_FOUND)
                    error_complete(0, 0, "host is unknown");
                else if (h_errno == NO_ADDRESS)
@@ -1550,8 +1692,10 @@ const struct method *proto;      /* protocol method table */
                else if (h_errno)
                    error_complete(0, 0, "unknown DNS error %d", h_errno);
                else
+#endif /* HAVE_RES_SEARCH */
                    error_complete(0, errno, "local error");
            }
+#endif /* INET6 */
            ok = PS_SOCKET;
            goto closeUp;
        }
@@ -1927,6 +2071,12 @@ const struct method *proto;      /* protocol method table */
                         * now.
                         */
 
+                       /* tell the UID code we've seen this */
+                       if (ctl->newsaved)
+                           for (idp = ctl->newsaved; idp; idp = idp->next)
+                               if (idp->val.num == num)
+                                   MARK_SEEN(idp->val.num);
+
                        /* maybe we delete this message now? */
                        if (retained)
                        {
@@ -2100,8 +2250,14 @@ int sock;        /* socket to which server is connected */
 char *buf;     /* buffer to receive input */
 int size;      /* length of buffer */
 {
+    int oldphase = phase;      /* we don't have to be re-entrant */
+
+    phase = SERVER_WAIT;
     if (SockRead(sock, buf, size) == -1)
+    {
+       phase = oldphase;
        return(PS_SOCKET);
+    }
     else
     {
        if (buf[strlen(buf)-1] == '\n')
@@ -2110,6 +2266,7 @@ int size; /* length of buffer */
            buf[strlen(buf)-1] = '\r';
        if (outlevel == O_VERBOSE)
            error(0, 0, "%s< %s", protocol->name, buf);
+       phase = oldphase;
        return(PS_SUCCESS);
     }
 }
@@ -2128,6 +2285,9 @@ va_dcl
     int ok;
     char buf [POPBUFSIZE+1];
     va_list ap;
+    int oldphase = phase;      /* we don't have to be re-entrant */
+
+    phase = SERVER_WAIT;
 
     if (protocol->tagged)
        (void) sprintf(buf, "%s ", GENSYM);
@@ -2171,6 +2331,7 @@ va_dcl
     ok = (protocol->parse_response)(sock, buf);
     set_timeout(mytimeout);
 
+    phase = oldphase;
     return(ok);
 }