]> Pileus Git - ~andy/fetchmail/blobdiff - fetchmail.c
Before showdots,
[~andy/fetchmail] / fetchmail.c
index 9954774569db5a6c53c4b749a810ef2ba0be2918..28ad2aa2457c095ce0df91431fdbff8de964323c 100644 (file)
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>  /* needed for Sun 4.1.2 */
 #ifdef HAVE_SETRLIMIT
 #include <sys/resource.h>
 #endif /* HAVE_SETRLIMIT */
 #include <sys/utsname.h>
 
+#ifdef HAVE_NET_SOCKET_H
+#include <net/socket.h>
+#endif
 #ifdef HAVE_GETHOSTBYNAME
 #include <netdb.h>
 #endif /* HAVE_GETHOSTBYNAME */
@@ -40,6 +44,7 @@
 
 #include "getopt.h"
 #include "fetchmail.h"
+#include "socket.h"
 #include "tunable.h"
 #include "smtp.h"
 #include "netrc.h"
@@ -65,6 +70,7 @@ flag check_only;          /* if --probe was set */
 flag versioninfo;          /* emit only version info */
 char *user;                /* the name of the invoking user */
 char *home;                /* invoking user's home directory */
+char *fmhome;              /* fetchmail's home directory */
 char *program_name;        /* the name to prefix error messages with */
 flag configdump;           /* dump control blocks for configurator */
 const char *fetchmailhost;  /* either `localhost' or the host's FQDN */
@@ -78,6 +84,7 @@ static char *lockfile;                /* name of lockfile */
 static int lock_acquired;      /* have we acquired a lock */
 static int querystatus;                /* status of query */
 static int successes;          /* count number of successful polls */
+static int activecount;                /* count number of active entries */
 static struct runctl cmd_run;  /* global options set from command line */
 static time_t parsetime;       /* time of last parse */
 
@@ -95,7 +102,7 @@ static void unlockit(void)
        unlink(lockfile);
 }
 
-#ifdef __FreeBSD__
+#if defined(__FreeBSD__) && defined(__FreeBSD_USE_KVM)
 /* drop SGID kmem privileage until we need it */
 static void dropprivs(void)
 {
@@ -116,6 +123,24 @@ static void dropprivs(void)
 }
 #endif
 
+#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
+#include <time.h>
+#include <locale.h>
+static char *timestamp (void)
+{
+    time_t      now;
+    static char buf[60];
+
+    time (&now);
+    setlocale (LC_TIME, "");
+    strftime (buf, sizeof (buf), "%c", localtime(&now));
+    setlocale (LC_TIME, "C");
+    return (buf);
+}
+#else
+#define timestamp rfc822timestamp
+#endif
+
 int main(int argc, char **argv)
 {
     int st, bkgd = FALSE;
@@ -127,7 +152,7 @@ int main(int argc, char **argv)
     pid_t pid;
     int lastsig = 0;
 
-#ifdef __FreeBSD__
+#if defined(__FreeBSD__) && defined(__FreeBSD_USE_KVM)
     dropprivs();
 #endif
 
@@ -146,15 +171,15 @@ int main(int argc, char **argv)
     {
        int i;
 
-       report(stdout, "fetchmail: invoked with");
+       report(stdout, _("fetchmail: invoked with"));
        for (i = 0; i < argc; i++)
            report(stdout, " %s", argv[i]);
        report(stdout, "\n");
     }
 
 #define IDFILE_NAME    ".fetchids"
-    run.idfile = (char *) xmalloc(strlen(home)+sizeof(IDFILE_NAME)+1);
-    strcpy(run.idfile, home);
+    run.idfile = (char *) xmalloc(strlen(fmhome)+sizeof(IDFILE_NAME)+2);
+    strcpy(run.idfile, fmhome);
     strcat(run.idfile, "/");
     strcat(run.idfile, IDFILE_NAME);
   
@@ -257,9 +282,11 @@ int main(int argc, char **argv)
                sizeof(PID_DIR) + sizeof(FETCHMAIL_PIDFILE));
        sprintf(tmpbuf, "%s/%s", PID_DIR, FETCHMAIL_PIDFILE);
     } else {
-       xalloca(tmpbuf, char *, strlen(home) + sizeof(FETCHMAIL_PIDFILE) + 2);
-       strcpy(tmpbuf, home);
-       strcat(tmpbuf, "/.");
+       xalloca(tmpbuf, char *, strlen(fmhome) + sizeof(FETCHMAIL_PIDFILE) + 2);
+       strcpy(tmpbuf, fmhome);
+       strcat(tmpbuf, "/");
+        if (fmhome == home)
+          strcat(tmpbuf, ".");
        strcat(tmpbuf, FETCHMAIL_PIDFILE);
     }
 #undef FETCHMAIL_PIDFILE
@@ -281,7 +308,7 @@ int main(int argc, char **argv)
 
 #define        NETRC_FILE      ".netrc"
     /* parse the ~/.netrc file (if present) for future password lookups. */
-    xalloca(netrc_file, char *, strlen (home) + sizeof(NETRC_FILE) + 1);
+    xalloca(netrc_file, char *, strlen(home) + sizeof(NETRC_FILE) + 2);
     strcpy (netrc_file, home);
     strcat (netrc_file, "/");
     strcat (netrc_file, NETRC_FILE);
@@ -475,10 +502,11 @@ int main(int argc, char **argv)
                && ctl->server.protocol != P_IMAP_GSS
 #endif /* GSSAPI */
                && !ctl->password)
+       {
            if (!isatty(0))
            {
                fprintf(stderr,
-                       _("fetchmail: can't find a password for %s@s.\n"),
+                       _("fetchmail: can't find a password for %s@%s.\n"),
                        ctl->remotename, ctl->server.pollname);
                return(PS_AUTHFAIL);
            }
@@ -491,8 +519,9 @@ int main(int argc, char **argv)
                        strlen(ctl->server.pollname) + 1);
                (void) sprintf(tmpbuf, password_prompt,
                               ctl->remotename, ctl->server.pollname);
-               ctl->password = xstrdup((char *)getpassword(tmpbuf));
+               ctl->password = xstrdup((char *)fm_getpassword(tmpbuf));
            }
+       }
     }
 
     /*
@@ -600,100 +629,106 @@ int main(int argc, char **argv)
        sethostent(TRUE);       /* use TCP/IP for mailserver queries */
 #endif /* HAVE_RES_SEARCH */
 
+       activecount = 0;
        batchcount = 0;
        for (ctl = querylist; ctl; ctl = ctl->next)
-       {
-           if (ctl->active && !(implicitmode && ctl->server.skip))
+           if (ctl->active)
            {
-               if (ctl->wedged)
+               activecount++;
+               if (!(implicitmode && ctl->server.skip))
                {
-                   report(stderr, 
-                         _("poll of %s skipped (failed authentication or too many timeouts)\n"),
-                         ctl->server.pollname);
-                   continue;
-               }
-
-               /* check skip interval first so that it counts all polls */
-               if (run.poll_interval && ctl->server.interval) 
-               {
-                   if (ctl->server.poll_count++ % ctl->server.interval) 
+                   if (ctl->wedged)
                    {
-                       if (outlevel >= O_VERBOSE)
-                           report(stdout,
-                                   _("interval not reached, not querying %s\n"),
-                                   ctl->server.pollname);
+                       report(stderr, 
+                              _("poll of %s skipped (failed authentication or too many timeouts)\n"),
+                              ctl->server.pollname);
                        continue;
                    }
-               }
+
+                   /* check skip interval first so that it counts all polls */
+                   if (run.poll_interval && ctl->server.interval) 
+                   {
+                       if (ctl->server.poll_count++ % ctl->server.interval) 
+                       {
+                           if (outlevel >= O_VERBOSE)
+                               report(stdout,
+                                      _("interval not reached, not querying %s\n"),
+                                      ctl->server.pollname);
+                           continue;
+                       }
+                   }
 
 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
-               /*
-                * Don't do monitoring if we were woken by a signal.
-                * Note that interface_approve() does its own error logging.
-                */
-               if (!interface_approve(&ctl->server, !lastsig))
-                   continue;
+                   /*
+                    * Don't do monitoring if we were woken by a signal.
+                    * Note that interface_approve() does its own error logging.
+                    */
+                   if (!interface_approve(&ctl->server, !lastsig))
+                       continue;
 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
 
-               querystatus = query_host(ctl);
+                   querystatus = query_host(ctl);
 
 #ifdef POP3_ENABLE
-               if (!check_only)
-                   uid_end_query(ctl);
+                   /* leave the UIDL state alone if there have been any errors */
+                   if (!check_only &&
+                               ((querystatus==PS_SUCCESS) || (querystatus==PS_NOMAIL)))
+                       uid_swap_lists(ctl);
 #endif  /* POP3_ENABLE */
 
-               if (querystatus == PS_SUCCESS)
-                   successes++;
-               else if (!check_only && 
-                        ((querystatus!=PS_NOMAIL) || (outlevel==O_DEBUG)))
-               switch(querystatus)
-               {
-               case PS_SUCCESS:
-                   report(stdout, "Query status=0 (SUCCESS)\n"); break ;
-               case PS_NOMAIL: 
-                   report(stdout, "Query status=1 (NOMAIL)\n"); break ;
-               case PS_SOCKET:
-                   report(stdout, "Query status=2 (SOCKET)\n"); break ;
-               case PS_AUTHFAIL:
-                   report(stdout, "Query status=3 (AUTHFAIL)\n"); break ;
-               case PS_PROTOCOL:
-                   report(stdout, "Query status=4 (PROTOCOL)\n"); break ;
-               case PS_SYNTAX:
-                   report(stdout, "Query status=5 (SYNTAX)\n"); break ;
-               case PS_IOERR:
-                   report(stdout, "Query status=6 (IOERR)\n"); break ;
-               case PS_ERROR:
-                   report(stdout, "Query status=7 (ERROR)\n"); break ;
-               case PS_EXCLUDE:
-                   report(stdout, "Query status=8 (EXCLUDE)\n"); break ;
-               case PS_LOCKBUSY:
-                   report(stdout, "Query status=9 (LOCKBUSY)\n"); break ;
-               case PS_SMTP:
-                   report(stdout, "Query status=10 (SMTP)\n"); break ;
-               case PS_DNS:
-                   report(stdout, "Query status=11 (DNS)\n"); break ;
-               case PS_BSMTP:
-                   report(stdout, "Query status=12 (BSMTP)\n"); break ;
-               case PS_MAXFETCH:
-                   report(stdout, "Query status=13 (MAXFETCH)\n"); break ;
-               default:
-                   report(stdout, _("Query status=%d\n"), querystatus); break;
-               }
+                   if (querystatus == PS_SUCCESS)
+                       successes++;
+                   else if (!check_only && 
+                            ((querystatus!=PS_NOMAIL) || (outlevel==O_DEBUG)))
+                       switch(querystatus)
+                       {
+                       case PS_SUCCESS:
+                           report(stdout,_("Query status=0 (SUCCESS)\n"));break;
+                       case PS_NOMAIL: 
+                           report(stdout,_("Query status=1 (NOMAIL)\n")); break;
+                       case PS_SOCKET:
+                           report(stdout,_("Query status=2 (SOCKET)\n")); break;
+                       case PS_AUTHFAIL:
+                           report(stdout,_("Query status=3 (AUTHFAIL)\n"));break;
+                       case PS_PROTOCOL:
+                           report(stdout,_("Query status=4 (PROTOCOL)\n"));break;
+                       case PS_SYNTAX:
+                           report(stdout,_("Query status=5 (SYNTAX)\n")); break;
+                       case PS_IOERR:
+                           report(stdout,_("Query status=6 (IOERR)\n"));  break;
+                       case PS_ERROR:
+                           report(stdout,_("Query status=7 (ERROR)\n"));  break;
+                       case PS_EXCLUDE:
+                           report(stdout,_("Query status=8 (EXCLUDE)\n")); break;
+                       case PS_LOCKBUSY:
+                           report(stdout,_("Query status=9 (LOCKBUSY)\n"));break;
+                       case PS_SMTP:
+                           report(stdout,_("Query status=10 (SMTP)\n")); break;
+                       case PS_DNS:
+                           report(stdout,_("Query status=11 (DNS)\n")); break;
+                       case PS_BSMTP:
+                           report(stdout,_("Query status=12 (BSMTP)\n")); break;
+                       case PS_MAXFETCH:
+                           report(stdout,_("Query status=13 (MAXFETCH)\n"));break;
+                       default:
+                           report(stdout,_("Query status=%d\n"),querystatus);
+                           break;
+                       }
 
 #if (defined(linux) && !INET6_ENABLE) || defined (__FreeBSD__)
-               if (ctl->server.monitor)
-               {
-                   /*
-                    * Allow some time for the link to quiesce.  One
-                    * second is usually sufficient, three is safe.
-                    * Note:  this delay is important - don't remove!
-                    */
-                   sleep(3);
-                   interface_note_activity(&ctl->server);
-               }
+                   if (ctl->server.monitor)
+                   {
+                       /*
+                        * Allow some time for the link to quiesce.  One
+                        * second is usually sufficient, three is safe.
+                        * Note:  this delay is important - don't remove!
+                        */
+                       sleep(3);
+                       interface_note_activity(&ctl->server);
+                   }
 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
+               }
            }
-       }
 
 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
        endhostent();           /* release TCP/IP connection to nameserver */
@@ -729,7 +764,7 @@ int main(int argc, char **argv)
 
            if (outlevel >= O_VERBOSE)
                report(stdout, 
-                      _("fetchmail: sleeping at %s\n"), rfc822timestamp());
+                      _("fetchmail: sleeping at %s\n"), timestamp());
 
            /*
             * OK, now pause util it's time for the next poll cycle.
@@ -751,7 +786,7 @@ int main(int argc, char **argv)
            }
 
            if (outlevel >= O_VERBOSE)
-               report(stdout, _("awakened at %s\n"), rfc822timestamp());
+               report(stdout, _("awakened at %s\n"), timestamp());
        }
     } while
        (run.poll_interval);
@@ -824,6 +859,7 @@ static void optmerge(struct query *h2, struct query *h1, int force)
     FLAG_MERGE(bsmtp);
     FLAG_MERGE(listener);
     FLAG_MERGE(smtpaddress);
+    FLAG_MERGE(smtpname);
     FLAG_MERGE(preconnect);
     FLAG_MERGE(postconnect);
 
@@ -835,6 +871,7 @@ static void optmerge(struct query *h2, struct query *h1, int force)
     FLAG_MERGE(stripcr);
     FLAG_MERGE(pass8bits);
     FLAG_MERGE(dropstatus);
+    FLAG_MERGE(dropdelivered);
     FLAG_MERGE(mimedecode);
     FLAG_MERGE(idle);
     FLAG_MERGE(limit);
@@ -914,7 +951,7 @@ static int load_params(int argc, char **argv, int optind)
                        || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
                {
                    /* Is this correct? */
-                   if(predeclared)
+                   if (predeclared && outlevel == O_VERBOSE)
                        fprintf(stderr,_("Warning: multiple mentions of host %s in config file\n"),argv[optind]);
                    ctl->active = TRUE;
                    predeclared = TRUE;
@@ -1002,6 +1039,7 @@ static int load_params(int argc, char **argv, int optind)
            DEFAULT(ctl->forcecr, FALSE);
            DEFAULT(ctl->pass8bits, FALSE);
            DEFAULT(ctl->dropstatus, FALSE);
+           DEFAULT(ctl->dropdelivered, FALSE);
            DEFAULT(ctl->mimedecode, FALSE);
            DEFAULT(ctl->idle, FALSE);
            DEFAULT(ctl->server.dns, TRUE);
@@ -1108,17 +1146,16 @@ static int load_params(int argc, char **argv, int optind)
                /* prevent core dump from ill-formed or duplicate entry */
                if (!leadname)
                {
-                   report(stderr, 
-                          _("Lead server has no name.\n"));
+                   report(stderr, _("Lead server has no name.\n"));
                    exit(PS_SYNTAX);
                }
 
                ctl->server.truename = xstrdup(leadname);
            }
 #ifdef HAVE_GETHOSTBYNAME
-           else if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
+           else if (!configdump && (ctl->server.preauthenticate==A_KERBEROS_V4 ||
                ctl->server.preauthenticate==A_KERBEROS_V5 ||
-               (ctl->server.dns && MULTIDROP(ctl)))
+                     (ctl->server.dns && MULTIDROP(ctl))))
            {
                struct hostent  *namerec;
 
@@ -1130,14 +1167,48 @@ static int load_params(int argc, char **argv, int optind)
                    report(stderr,
                          _("couldn't find canonical DNS name of %s\n"),
                          ctl->server.pollname);
-                   exit(PS_DNS);
+                   ctl->server.truename = xstrdup(ctl->server.queryname);
+                   ctl->server.trueaddr = NULL;
                }
                else
                    ctl->server.truename=xstrdup((char *)namerec->h_name);
            }
 #endif /* HAVE_GETHOSTBYNAME */
-           else
-               ctl->server.truename = xstrdup(ctl->server.queryname);
+           else {
+#ifdef HAVE_GETHOSTBYNAME
+             struct hostent    *namerec;
+
+             /* <fetchmail@mail.julianhaight.com>
+                Get the host's IP, so we can report it like this:
+
+                Received: from hostname [10.0.0.1]
+
+                do we actually need to gethostbyname to find the IP?
+                it seems like it would be faster to do this later, when
+                we are actually resolving the hostname for a connection,
+                but I ain't that smart, so I don't know where to make
+                the change later..
+             */
+             errno = 0;
+             namerec = gethostbyname(ctl->server.queryname);
+             if (namerec == (struct hostent *)NULL)
+               {
+                 report(stderr,
+                        _("couldn't find canonical DNS name of %s\n"),
+                        ctl->server.pollname);
+                 exit(PS_DNS);
+               }
+             else {
+               ctl->server.truename=xstrdup((char *)namerec->h_name);
+               ctl->server.trueaddr=xmalloc(namerec->h_length);
+               memcpy(ctl->server.trueaddr, 
+                      namerec->h_addr_list[0],
+                      namerec->h_length);
+             }
+#else
+             ctl->server.truename = xstrdup(ctl->server.queryname);
+#endif /* HAVE_GETHOSTBYNAME */
+           }
 
            /* if no folders were specified, set up the null one as default */
            if (!ctl->mailboxes)
@@ -1292,7 +1363,10 @@ static void terminate_run(int sig)
     unlockit();
 #endif
 
-    exit(successes ? PS_SUCCESS : querystatus);
+    if (activecount == 0)
+       exit(PS_NOMAIL);
+    else
+       exit(successes ? PS_SUCCESS : querystatus);
 }
 
 /*
@@ -1326,7 +1400,7 @@ static int query_host(struct query *ctl)
               VERSION,
               ctl->server.pollname,
               showproto(ctl->server.protocol),
-              rfc822timestamp());
+              timestamp());
     }
     switch (ctl->server.protocol) {
     case P_AUTO:
@@ -1545,6 +1619,9 @@ static void dump_params (struct runctl *runp,
                printf(_("  Nonempty Status lines will be %s (dropstatus %s)\n"),
                       ctl->dropstatus ? _("discarded") : _("kept"),
                       ctl->dropstatus ? "on" : "off");
+               printf(_("  Delivered-To lines will be %s (dropdelivered %s)\n"),
+                      ctl->dropdelivered ? _("discarded") : _("kept"),
+                      ctl->dropdelivered ? "on" : "off");
                if (NUM_NONZERO(ctl->limit))
                {
                    if (NUM_NONZERO(ctl->limit))
@@ -1567,7 +1644,7 @@ static void dump_params (struct runctl *runp,
                    printf(_("  SMTP message batch limit is %d.\n"), ctl->batchlimit);
                else if (outlevel >= O_VERBOSE)
                    printf(_("  No SMTP message batch limit (--batchlimit 0).\n"));
-               if (ctl->server.protocol == P_IMAP)
+               if (ctl->server.protocol != P_ETRN)
                {
                    if (NUM_NONZERO(ctl->expunge))
                        printf(_("  Deletion interval between expunges forced to %d (--expunge %d).\n"), ctl->expunge, ctl->expunge);
@@ -1708,11 +1785,11 @@ static void dump_params (struct runctl *runp,
 #endif
 
        if (ctl->server.plugin)
-           printf(_("  Server connections will be mode via plugin %s (--plugin %s).\n"), ctl->server.plugin, ctl->server.plugin);
+           printf(_("  Server connections will be made via plugin %s (--plugin %s).\n"), ctl->server.plugin, ctl->server.plugin);
        else if (outlevel >= O_VERBOSE)
            printf(_("  No plugin command specified.\n"));
        if (ctl->server.plugout)
-           printf(_("  Listener connections will be mode via plugout %s (--plugout %s).\n"), ctl->server.plugout, ctl->server.plugout);
+           printf(_("  Listener connections will be made via plugout %s (--plugout %s).\n"), ctl->server.plugout, ctl->server.plugout);
        else if (outlevel >= O_VERBOSE)
            printf(_("  No plugout command specified.\n"));