]> Pileus Git - ~andy/fetchmail/blob - fetchmail.c
Let's get rid of the old protocols with preauthentication bundled in.
[~andy/fetchmail] / fetchmail.c
1 /*
2  * fetchmail.c -- main driver module for fetchmail
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6 #include "config.h"
7
8 #include <stdio.h>
9 #if defined(STDC_HEADERS)
10 #include <stdlib.h>
11 #endif
12 #if defined(HAVE_UNISTD_H)
13 #include <unistd.h>
14 #endif
15 #include <fcntl.h>
16 #include <string.h>
17 #include <signal.h>
18 #if defined(HAVE_SYSLOG)
19 #include <syslog.h>
20 #endif
21 #include <pwd.h>
22 #ifdef __FreeBSD__
23 #include <grp.h>
24 #endif
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>   /* needed for Sun 4.1.2 */
29 #ifdef HAVE_SETRLIMIT
30 #include <sys/resource.h>
31 #endif /* HAVE_SETRLIMIT */
32 #include <sys/utsname.h>
33
34 #ifdef HAVE_NET_SOCKET_H
35 #include <net/socket.h>
36 #endif
37 #ifdef HAVE_GETHOSTBYNAME
38 #include <netdb.h>
39 #endif /* HAVE_GETHOSTBYNAME */
40
41 #ifdef HESIOD
42 #include <hesiod.h>
43 #endif
44
45 #include "getopt.h"
46 #include "fetchmail.h"
47 #include "socket.h"
48 #include "tunable.h"
49 #include "smtp.h"
50 #include "netrc.h"
51 #include "i18n.h"
52
53 #ifndef ENETUNREACH
54 #define ENETUNREACH   128       /* Interactive doesn't know this */
55 #endif /* ENETUNREACH */
56
57 /* prototypes for internal functions */
58 static int load_params(int, char **, int);
59 static void dump_params (struct runctl *runp, struct query *, flag implicit);
60 static int query_host(struct query *);
61
62 /* controls the detail level of status/progress messages written to stderr */
63 int outlevel;               /* see the O_.* constants above */
64
65 /* miscellaneous global controls */
66 struct runctl run;          /* global controls for this run */
67 flag nodetach;              /* if TRUE, don't detach daemon process */
68 flag quitmode;              /* if --quit was set */
69 flag check_only;            /* if --probe was set */
70 flag versioninfo;           /* emit only version info */
71 char *user;                 /* the name of the invoking user */
72 char *home;                 /* invoking user's home directory */
73 char *fmhome;               /* fetchmail's home directory */
74 char *program_name;         /* the name to prefix error messages with */
75 flag configdump;            /* dump control blocks for configurator */
76 const char *fetchmailhost;  /* either `localhost' or the host's FQDN */
77
78 #if NET_SECURITY
79 void *request = NULL;
80 int requestlen = 0;
81 #endif /* NET_SECURITY */
82
83 static char *lockfile;          /* name of lockfile */
84 static int lock_acquired;       /* have we acquired a lock */
85 static int querystatus;         /* status of query */
86 static int successes;           /* count number of successful polls */
87 static int activecount;         /* count number of active entries */
88 static struct runctl cmd_run;   /* global options set from command line */
89 static time_t parsetime;        /* time of last parse */
90
91 static void terminate_run(int);
92 static void terminate_poll(int);
93
94 #ifdef HAVE_ON_EXIT
95 static void unlockit(int n, void *p)
96 #else
97 static void unlockit(void)
98 #endif
99 /* must-do actions for exit (but we can't count on being able to do malloc) */
100 {
101     if (lockfile && lock_acquired)
102         unlink(lockfile);
103 }
104
105 #if defined(__FreeBSD__) && defined(__FreeBSD_USE_KVM)
106 /* drop SGID kmem privileage until we need it */
107 static void dropprivs(void)
108 {
109     struct group *gr;
110     gid_t        egid;
111     gid_t        rgid;
112     
113     egid = getegid();
114     rgid = getgid();
115     gr = getgrgid(egid);
116     
117     if (gr && !strcmp(gr->gr_name, "kmem"))
118     {
119         extern void interface_set_gids(gid_t egid, gid_t rgid);
120         interface_set_gids(egid, rgid);
121         setegid(rgid);
122     }
123 }
124 #endif
125
126 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
127 #include <time.h>
128 #include <locale.h>
129 static char *timestamp (void)
130 {
131     time_t      now;
132     static char buf[60];
133
134     time (&now);
135     setlocale (LC_TIME, "");
136     strftime (buf, sizeof (buf), "%c", localtime(&now));
137     setlocale (LC_TIME, "C");
138     return (buf);
139 }
140 #else
141 #define timestamp rfc822timestamp
142 #endif
143
144 int main(int argc, char **argv)
145 {
146     int st, bkgd = FALSE;
147     int parsestatus, implicitmode = FALSE;
148     FILE        *lockfp;
149     struct query *ctl;
150     netrc_entry *netrc_list;
151     char *netrc_file, *tmpbuf;
152     pid_t pid;
153     int lastsig = 0;
154
155 #if defined(__FreeBSD__) && defined(__FreeBSD_USE_KVM)
156     dropprivs();
157 #endif
158
159     envquery(argc, argv);
160 #ifdef ENABLE_NLS
161     bindtextdomain(PACKAGE, LOCALEDIR);
162     textdomain(PACKAGE);
163 #endif
164
165     /*
166      * Note: because we can't initialize reporting before we  know whether
167      * syslog is supposed to be on, this message will go to stdout and
168      * be lost when running in background.
169      */
170     if (outlevel >= O_VERBOSE)
171     {
172         int i;
173
174         report(stdout, _("fetchmail: invoked with"));
175         for (i = 0; i < argc; i++)
176             report(stdout, " %s", argv[i]);
177         report(stdout, "\n");
178     }
179
180 #define IDFILE_NAME     ".fetchids"
181     run.idfile = (char *) xmalloc(strlen(fmhome)+sizeof(IDFILE_NAME)+2);
182     strcpy(run.idfile, fmhome);
183     strcat(run.idfile, "/");
184     strcat(run.idfile, IDFILE_NAME);
185   
186     outlevel = O_NORMAL;
187
188     /*
189      * We used to arrange for the lockfile to be removed on exit close
190      * to where the lock was asserted.  Now we need to do it here, because
191      * we might have re-executed in background with an existing lockfile
192      * as the result of a changed rcfile (see the code near the execvp(3)
193      * call near the beginning of the polling loop for details).  We want
194      * to be sure the lockfile gets nuked on any error exit, basically.
195      */
196 #ifdef HAVE_ATEXIT
197     atexit(unlockit);
198 #endif
199 #ifdef HAVE_ON_EXIT
200     on_exit(unlockit, (char *)NULL);
201 #endif
202
203     if ((parsestatus = parsecmdline(argc,argv, &cmd_run, &cmd_opts)) < 0)
204         exit(PS_SYNTAX);
205
206     if (versioninfo)
207     {
208         printf(_("This is fetchmail release %s"), VERSION);
209 #ifdef POP2_ENABLE
210         printf("+POP2");
211 #endif /* POP2_ENABLE */
212 #ifndef POP3_ENABLE
213         printf("-POP3");
214 #endif /* POP3_ENABLE */
215 #ifndef IMAP_ENABLE
216         printf("-IMAP");
217 #endif /* IMAP_ENABLE */
218 #ifdef GSSAPI
219         printf("+IMAP-GSS");
220 #endif /* GSSAPI */
221 #ifdef RPA_ENABLE
222         printf("+RPA");
223 #endif /* RPA_ENABLE */
224 #ifdef NTLM_ENABLE
225         printf("+NTLM");
226 #endif /* NTLM_ENABLE */
227 #ifdef SDPS_ENABLE
228         printf("+SDPS");
229 #endif /* SDPS_ENABLE */
230 #ifndef ETRN_ENABLE
231         printf("-ETRN");
232 #endif /* ETRN_ENABLE */
233 #ifndef ODMR_ENABLE
234         printf("-ODMR");
235 #endif /* ODMR_ENABLE */
236 #ifdef SSL_ENABLE
237         printf("+SSL");
238 #endif
239 #if OPIE_ENABLE
240         printf("+OPIE");
241 #endif /* OPIE_ENABLE */
242 #if INET6_ENABLE
243         printf("+INET6");
244 #endif /* INET6_ENABLE */
245 #if NET_SECURITY
246         printf("+NETSEC");
247 #endif /* NET_SECURITY */
248 #ifdef HAVE_SOCKS
249         printf("+SOCKS");
250 #endif /* HAVE_SOCKS */
251 #if ENABLE_NLS
252         printf("+NLS");
253 #endif /* ENABLE_NLS */
254         putchar('\n');
255         fflush(stdout);
256
257         /* this is an attempt to help remote debugging */
258         system("uname -a");
259     }
260
261     /* avoid parsing the config file if all we're doing is killing a daemon */ 
262     if (!(quitmode && argc == 2))
263         implicitmode = load_params(argc, argv, optind);
264
265 #if defined(HAVE_SYSLOG)
266     /* logging should be set up early in case we were restarted from exec */
267     if (run.use_syslog)
268     {
269 #if defined(LOG_MAIL)
270         openlog(program_name, LOG_PID, LOG_MAIL);
271 #else
272         /* Assume BSD4.2 openlog with two arguments */
273         openlog(program_name, LOG_PID);
274 #endif
275         report_init(-1);
276     }
277     else
278 #endif
279         report_init((run.poll_interval == 0 || nodetach) && !run.logfile);
280
281     /* set up to do lock protocol */
282 #define FETCHMAIL_PIDFILE       "fetchmail.pid"
283     if (!getuid()) {
284         xalloca(tmpbuf, char *,
285                 sizeof(PID_DIR) + sizeof(FETCHMAIL_PIDFILE));
286         sprintf(tmpbuf, "%s/%s", PID_DIR, FETCHMAIL_PIDFILE);
287     } else {
288         xalloca(tmpbuf, char *, strlen(fmhome) + sizeof(FETCHMAIL_PIDFILE) + 2);
289         strcpy(tmpbuf, fmhome);
290         strcat(tmpbuf, "/");
291         if (fmhome == home)
292            strcat(tmpbuf, ".");
293         strcat(tmpbuf, FETCHMAIL_PIDFILE);
294     }
295 #undef FETCHMAIL_PIDFILE
296
297 #ifdef HAVE_SETRLIMIT
298     /*
299      * Before getting passwords, disable core dumps unless -v -d0 mode is on.
300      * Core dumps could otherwise contain passwords to be scavenged by a
301      * cracker.
302      */
303     if (outlevel < O_VERBOSE || run.poll_interval > 0)
304     {
305         struct rlimit corelimit;
306         corelimit.rlim_cur = 0;
307         corelimit.rlim_max = 0;
308         setrlimit(RLIMIT_CORE, &corelimit);
309     }
310 #endif /* HAVE_SETRLIMIT */
311
312 #define NETRC_FILE      ".netrc"
313     /* parse the ~/.netrc file (if present) for future password lookups. */
314     xalloca(netrc_file, char *, strlen(home) + sizeof(NETRC_FILE) + 2);
315     strcpy (netrc_file, home);
316     strcat (netrc_file, "/");
317     strcat (netrc_file, NETRC_FILE);
318     netrc_list = parse_netrc(netrc_file);
319 #undef NETRC_FILE
320
321     /* pick up passwords where we can */ 
322     for (ctl = querylist; ctl; ctl = ctl->next)
323     {
324         if (ctl->active && !(implicitmode && ctl->server.skip)&&!ctl->password)
325         {
326             if (ctl->server.preauthenticate != A_PASSWORD)
327                 /* Server won't care what the password is, but there
328                    must be some non-null string here.  */
329                 ctl->password = ctl->remotename;
330             else
331             {
332                 netrc_entry *p;
333
334                 /* look up the pollname and account in the .netrc file. */
335                 p = search_netrc(netrc_list,
336                                  ctl->server.pollname, ctl->remotename);
337                 /* if we find a matching entry with a password, use it */
338                 if (p && p->password)
339                     ctl->password = xstrdup(p->password);
340
341                 /* otherwise try with "via" name if there is one */
342                 else if (ctl->server.via)
343                 {
344                     p = search_netrc(netrc_list, 
345                                      ctl->server.via, ctl->remotename);
346                     if (p && p->password)
347                         ctl->password = xstrdup(p->password);
348                 }
349             }
350         }
351     }
352
353     /* perhaps we just want to check options? */
354     if (versioninfo)
355     {
356         int havercfile = access(rcfile, 0);
357
358         printf(_("Taking options from command line%s%s\n"),
359                                 havercfile ? "" :  _(" and "),
360                                 havercfile ? "" : rcfile);
361
362         if (outlevel >= O_VERBOSE)
363             printf(_("Lockfile at %s\n"), tmpbuf);
364
365         if (querylist == NULL)
366             fprintf(stderr,
367                     _("No mailservers set up -- perhaps %s is missing?\n"),
368                     rcfile);
369         else
370             dump_params(&run, querylist, implicitmode);
371         exit(0);
372     }
373
374     /* dump options as a Python dictionary, for configurator use */
375     if (configdump)
376     {
377         dump_config(&run, querylist);
378         exit(0);
379     }
380
381     /* check for another fetchmail running concurrently */
382     pid = -1;
383     if ((lockfile = (char *) malloc(strlen(tmpbuf) + 1)) == NULL)
384     {
385         report(stderr,_("fetchmail: cannot allocate memory for lock name.\n"));
386         exit(PS_EXCLUDE);
387     }
388     else
389         (void) strcpy(lockfile, tmpbuf);
390     if ((lockfp = fopen(lockfile, "r")) != NULL )
391     {
392         bkgd = (fscanf(lockfp,"%d %d", &pid, &st) == 2);
393
394         if (kill(pid, 0) == -1) {
395             fprintf(stderr,_("fetchmail: removing stale lockfile\n"));
396             pid = -1;
397             bkgd = FALSE;
398             unlink(lockfile);
399         }
400         fclose(lockfp); /* not checking should be safe, file mode was "r" */
401     }
402
403     /* if no mail servers listed and nothing in background, we're done */
404     if (!(quitmode && argc == 2) && pid == -1 && querylist == NULL) {
405         (void)fputs(_("fetchmail: no mailservers have been specified.\n"),stderr);
406         exit(PS_SYNTAX);
407     }
408
409     /* perhaps user asked us to kill the other fetchmail */
410     if (quitmode)
411     {
412         if (pid == -1) 
413         {
414             fprintf(stderr,_("fetchmail: no other fetchmail is running\n"));
415             if (argc == 2)
416                 exit(PS_EXCLUDE);
417         }
418         else if (kill(pid, SIGTERM) < 0)
419         {
420             fprintf(stderr,_("fetchmail: error killing %s fetchmail at %d; bailing out.\n"),
421                     bkgd ? _("background") : _("foreground"), pid);
422             exit(PS_EXCLUDE);
423         }
424         else
425         {
426             fprintf(stderr,_("fetchmail: %s fetchmail at %d killed.\n"),
427                     bkgd ? _("background") : _("foreground"), pid);
428             unlink(lockfile);
429             if (argc == 2)
430                 exit(0);
431             else
432                 pid = -1; 
433         }
434     }
435
436     /* another fetchmail is running -- wake it up or die */
437     if (pid != -1)
438     {
439         if (check_only)
440         {
441             fprintf(stderr,
442                  _("fetchmail: can't check mail while another fetchmail to same host is running.\n"));
443             return(PS_EXCLUDE);
444         }
445         else if (!implicitmode)
446         {
447             fprintf(stderr,
448                  _("fetchmail: can't poll specified hosts with another fetchmail running at %d.\n"),
449                  pid);
450                 return(PS_EXCLUDE);
451         }
452         else if (!bkgd)
453         {
454             fprintf(stderr,
455                  _("fetchmail: another foreground fetchmail is running at %d.\n"),
456                  pid);
457                 return(PS_EXCLUDE);
458         }
459         else if (argc > 1)
460         {
461             /* this test enables re-execing on a changed rcfile */
462             if (getpid() == pid)
463                 lock_acquired = TRUE;
464             else
465             {
466                 fprintf(stderr,
467                         _("fetchmail: can't accept options while a background fetchmail is running.\n"));
468                 return(PS_EXCLUDE);
469             }
470         }
471         else if (kill(pid, SIGUSR1) == 0)
472         {
473             fprintf(stderr,
474                     _("fetchmail: background fetchmail at %d awakened.\n"),
475                     pid);
476             return(0);
477         }
478         else
479         {
480             /*
481              * Should never happen -- possible only if a background fetchmail
482              * croaks after the first kill probe above but before the
483              * SIGUSR1/SIGHUP transmission.
484              */
485             fprintf(stderr,
486                     _("fetchmail: elder sibling at %d died mysteriously.\n"),
487                     pid);
488             return(PS_UNDEFINED);
489         }
490     }
491
492     /* pick up interactively any passwords we need but don't have */ 
493     for (ctl = querylist; ctl; ctl = ctl->next)
494     {
495         if (ctl->active && !(implicitmode && ctl->server.skip)
496                 && ctl->server.preauthenticate == A_PASSWORD
497                 && !ctl->password)
498         {
499             if (!isatty(0))
500             {
501                 fprintf(stderr,
502                         _("fetchmail: can't find a password for %s@%s.\n"),
503                         ctl->remotename, ctl->server.pollname);
504                 return(PS_AUTHFAIL);
505             }
506             else
507             {
508                 char* password_prompt = _("Enter password for %s@%s: ");
509
510                 xalloca(tmpbuf, char *, strlen(password_prompt) +
511                         strlen(ctl->remotename) +
512                         strlen(ctl->server.pollname) + 1);
513                 (void) sprintf(tmpbuf, password_prompt,
514                                ctl->remotename, ctl->server.pollname);
515                 ctl->password = xstrdup((char *)fm_getpassword(tmpbuf));
516             }
517         }
518     }
519
520     /*
521      * Time to initiate the SOCKS library (this is not mandatory: it just
522      * registers the correct application name for logging purpose. If you
523      * have some problem, comment out these lines).
524      */
525 #ifdef HAVE_SOCKS
526     SOCKSinit("fetchmail");
527 #endif /* HAVE_SOCKS */
528
529     /*
530      * Maybe time to go to demon mode...
531      */
532     if (run.poll_interval)
533     {
534         if (nodetach)
535             deal_with_sigchld();
536         else
537             daemonize(run.logfile, terminate_run);
538         report(stdout, _("starting fetchmail %s daemon \n"), VERSION);
539
540         /*
541          * We'll set up a handler for these when we're sleeping,
542          * but ignore them otherwise so as not to interrupt a poll.
543          */
544         signal(SIGUSR1, SIG_IGN);
545         if (run.poll_interval && !getuid())
546             signal(SIGHUP, SIG_IGN);
547     }
548     else if (run.logfile && access(run.logfile, F_OK) == 0)
549     {
550         freopen(run.logfile, "a", stdout);
551         freopen(run.logfile, "a", stderr);
552     }
553
554
555 #ifdef linux
556     interface_init();
557 #endif /* linux */
558
559     /* beyond here we don't want more than one fetchmail running per user */
560     umask(0077);
561     signal(SIGABRT, terminate_run);
562     signal(SIGINT, terminate_run);
563     signal(SIGTERM, terminate_run);
564     signal(SIGALRM, terminate_run);
565     signal(SIGPIPE, terminate_run);
566     signal(SIGQUIT, terminate_run);
567
568     /* here's the exclusion lock */
569 #ifndef O_SYNC
570 #define O_SYNC  0       /* use it if we have it */
571 #endif
572     if (!lock_acquired)
573     {
574       if ((st = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_SYNC, 0666)) != -1)
575       {
576           sprintf(tmpbuf,"%d", getpid());
577           write(st, tmpbuf, strlen(tmpbuf));
578           if (run.poll_interval)
579           {
580               sprintf(tmpbuf," %d", run.poll_interval);
581               write(st, tmpbuf, strlen(tmpbuf));
582           }
583           close(st);    /* should be safe, fd was opened with O_SYNC */
584           lock_acquired = TRUE;
585       }
586       else
587       {
588           fprintf(stderr,       _("fetchmail: lock creation failed.\n"));
589           return(PS_EXCLUDE);
590       }
591     }
592
593     /*
594      * Query all hosts. If there's only one, the error return will
595      * reflect the status of that transaction.
596      */
597     do {
598         /* 
599          * Check to see if the rcfile has been touched.  If so,
600          * re-exec so the file will be reread.  Doing it this way
601          * avoids all the complications of trying to deallocate the
602          * in-core control structures -- and the potential memory
603          * leaks...
604          */
605         struct stat     rcstat;
606
607         if (stat(rcfile, &rcstat) == -1)
608         {
609             if (errno != ENOENT)
610                 report(stderr, 
611                        _("couldn't time-check %s (error %d)\n"),
612                        rcfile, errno);
613         }
614         else if (rcstat.st_mtime > parsetime)
615         {
616             report(stdout, _("restarting fetchmail (%s changed)\n"), rcfile);
617             /*
618              * Matthias Andree: Isn't this prone to introduction of
619              * "false" programs by interfering with PATH? Those
620              * path-searching execs might not be the best ideas for
621              * this reason.
622              *
623              * Rob Funk: But is there any way for someone to modify
624              * the PATH variable of a running fetchmail?  I don't know
625              * of a way.
626              *
627              * Dave's change makes fetchmail restart itself in exactly
628              * the way it was started from the shell (or shell script)
629              * in the first place.  If you're concerned about PATH
630              * contamination, call fetchmail initially with a full
631              * path, and use Dave's patch.
632              *
633              * Not using a -p variant of exec means that the restart
634              * will break if both (a) the user depended on PATH to
635              * call fetchmail in the first place, and (b) the system
636              * doesn't save the whole path in argv[0] if the whole
637              * path wasn't used in the initial call.  (If I recall
638              * correctly, Linux saves it but many other Unices don't.)
639              */
640             execvp(argv[0], argv);
641             report(stderr, _("attempt to re-exec fetchmail failed\n"));
642         }
643
644 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
645         /*
646          * This was an efficiency hack that backfired.  The theory
647          * was that using TCP/IP for DNS queries would get us better
648          * reliability and shave off some per-UDP-packet costs.
649          * Unfortunately it interacted badly with diald, which effectively 
650          * filters out DNS queries over TCP/IP for reasons having to do
651          * with some obscure Linux kernel problem involving bootstrapping of
652          * dynamically-addressed links.  I don't understand this mess
653          * and don't want to, so it's "See ya!" to this hack.
654          */
655         sethostent(TRUE);       /* use TCP/IP for mailserver queries */
656 #endif /* HAVE_RES_SEARCH */
657
658         activecount = 0;
659         batchcount = 0;
660         for (ctl = querylist; ctl; ctl = ctl->next)
661             if (ctl->active)
662             {
663                 activecount++;
664                 if (!(implicitmode && ctl->server.skip))
665                 {
666                     if (ctl->wedged)
667                     {
668                         report(stderr, 
669                                _("poll of %s skipped (failed authentication or too many timeouts)\n"),
670                                ctl->server.pollname);
671                         continue;
672                     }
673
674                     /* check skip interval first so that it counts all polls */
675                     if (run.poll_interval && ctl->server.interval) 
676                     {
677                         if (ctl->server.poll_count++ % ctl->server.interval) 
678                         {
679                             if (outlevel >= O_VERBOSE)
680                                 report(stdout,
681                                        _("interval not reached, not querying %s\n"),
682                                        ctl->server.pollname);
683                             continue;
684                         }
685                     }
686
687 #if (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__)
688                     /*
689                      * Don't do monitoring if we were woken by a signal.
690                      * Note that interface_approve() does its own error logging.
691                      */
692                     if (!interface_approve(&ctl->server, !lastsig))
693                         continue;
694 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
695
696                     querystatus = query_host(ctl);
697
698 #ifdef POP3_ENABLE
699                     /* leave the UIDL state alone if there have been any errors */
700                     if (!check_only &&
701                                 ((querystatus==PS_SUCCESS) || (querystatus==PS_NOMAIL) || (querystatus==PS_MAXFETCH)))
702                         uid_swap_lists(ctl);
703 #endif  /* POP3_ENABLE */
704
705                     if (querystatus == PS_SUCCESS)
706                         successes++;
707                     else if (!check_only && 
708                              ((querystatus!=PS_NOMAIL) || (outlevel==O_DEBUG)))
709                         switch(querystatus)
710                         {
711                         case PS_SUCCESS:
712                             report(stdout,_("Query status=0 (SUCCESS)\n"));break;
713                         case PS_NOMAIL: 
714                             report(stdout,_("Query status=1 (NOMAIL)\n")); break;
715                         case PS_SOCKET:
716                             report(stdout,_("Query status=2 (SOCKET)\n")); break;
717                         case PS_AUTHFAIL:
718                             report(stdout,_("Query status=3 (AUTHFAIL)\n"));break;
719                         case PS_PROTOCOL:
720                             report(stdout,_("Query status=4 (PROTOCOL)\n"));break;
721                         case PS_SYNTAX:
722                             report(stdout,_("Query status=5 (SYNTAX)\n")); break;
723                         case PS_IOERR:
724                             report(stdout,_("Query status=6 (IOERR)\n"));  break;
725                         case PS_ERROR:
726                             report(stdout,_("Query status=7 (ERROR)\n"));  break;
727                         case PS_EXCLUDE:
728                             report(stdout,_("Query status=8 (EXCLUDE)\n")); break;
729                         case PS_LOCKBUSY:
730                             report(stdout,_("Query status=9 (LOCKBUSY)\n"));break;
731                         case PS_SMTP:
732                             report(stdout,_("Query status=10 (SMTP)\n")); break;
733                         case PS_DNS:
734                             report(stdout,_("Query status=11 (DNS)\n")); break;
735                         case PS_BSMTP:
736                             report(stdout,_("Query status=12 (BSMTP)\n")); break;
737                         case PS_MAXFETCH:
738                             report(stdout,_("Query status=13 (MAXFETCH)\n"));break;
739                         default:
740                             report(stdout,_("Query status=%d\n"),querystatus);
741                             break;
742                         }
743
744 #if (defined(linux) && !INET6_ENABLE) || defined (__FreeBSD__)
745                     if (ctl->server.monitor)
746                     {
747                         /*
748                          * Allow some time for the link to quiesce.  One
749                          * second is usually sufficient, three is safe.
750                          * Note:  this delay is important - don't remove!
751                          */
752                         sleep(3);
753                         interface_note_activity(&ctl->server);
754                     }
755 #endif /* (defined(linux) && !INET6_ENABLE) || defined(__FreeBSD__) */
756                 }
757             }
758
759 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
760         endhostent();           /* release TCP/IP connection to nameserver */
761 #endif /* HAVE_RES_SEARCH */
762
763         /* close connections cleanly */
764         terminate_poll(0);
765
766         /*
767          * OK, we've polled.  Now sleep.
768          */
769         if (run.poll_interval)
770         {
771             /* 
772              * Because passwords can expire, it may happen that *all*
773              * hosts are now out of the loop due to authfail
774              * conditions.  If this happens daemon-mode fetchmail
775              * should softly and silently vanish away, rather than
776              * spinning uselessly.
777              */
778             int unwedged = 0;
779
780             for (ctl = querylist; ctl; ctl = ctl->next)
781                 if (ctl->active && !(implicitmode && ctl->server.skip))
782                     if (!ctl->wedged)
783                         unwedged++;
784             if (!unwedged)
785             {
786                 report(stderr, _("All connections are wedged.  Exiting.\n"));
787                 /* FIXME: someday, send notification mail */
788                 exit(PS_AUTHFAIL);
789             }
790
791             if (outlevel >= O_VERBOSE)
792                 report(stdout, 
793                        _("fetchmail: sleeping at %s\n"), timestamp());
794
795             /*
796              * OK, now pause util it's time for the next poll cycle.
797              * A nonzero return indicates we received a wakeup signal;
798              * unwedge all servers in case the problem has been
799              * manually repaired.
800              */
801             if ((lastsig = interruptible_idle(run.poll_interval)))
802             {
803 #ifdef SYS_SIGLIST_DECLARED
804                 report(stdout, 
805                        _("awakened by %s\n"), sys_siglist[lastsig]);
806 #else
807                 report(stdout, 
808                        _("awakened by signal %d\n"), lastsig);
809 #endif
810                 for (ctl = querylist; ctl; ctl = ctl->next)
811                     ctl->wedged = FALSE;
812             }
813
814             if (outlevel >= O_VERBOSE)
815                 report(stdout, _("awakened at %s\n"), timestamp());
816         }
817     } while
818         (run.poll_interval);
819
820     if (outlevel >= O_VERBOSE)
821         report(stdout, _("normal termination, status %d\n"),
822                 successes ? PS_SUCCESS : querystatus);
823
824     terminate_run(0);
825
826     if (successes)
827         exit(PS_SUCCESS);
828     else if (querystatus)
829         exit(querystatus);
830     else
831         /* in case we interrupted before a successful fetch */
832         exit(PS_NOMAIL);
833
834     exit(successes ?  : querystatus);
835 }
836
837 static void list_merge(struct idlist **dstl, struct idlist **srcl, int force)
838 {
839     /*
840      * If force is off, modify dstl fields only when they're empty (treat srcl
841      * as defaults).  If force is on, modify each dstl field whenever scrcl
842      * is nonempty (treat srcl as an override).  
843      */
844     if (force ? !!*srcl : !*dstl)
845     {
846         struct idlist *cpl = copy_str_list(*srcl);
847
848         append_str_list(dstl, &cpl);
849     }
850 }
851
852 static void optmerge(struct query *h2, struct query *h1, int force)
853 /* merge two options records */
854 {
855     list_merge(&h2->server.localdomains, &h1->server.localdomains, force);
856     list_merge(&h2->localnames, &h1->localnames, force);
857     list_merge(&h2->mailboxes, &h1->mailboxes, force);
858     list_merge(&h2->smtphunt, &h1->smtphunt, force);
859     list_merge(&h2->antispam, &h1->antispam, force);
860
861 #define FLAG_MERGE(fld) if (force ? !!h1->fld : !h2->fld) h2->fld = h1->fld
862     FLAG_MERGE(server.via);
863     FLAG_MERGE(server.protocol);
864 #if INET6_ENABLE
865     FLAG_MERGE(server.service);
866     FLAG_MERGE(server.netsec);
867 #else /* INET6_ENABLE */
868     FLAG_MERGE(server.port);
869 #endif /* INET6_ENABLE */
870     FLAG_MERGE(server.interval);
871     FLAG_MERGE(server.preauthenticate);
872     FLAG_MERGE(server.timeout);
873     FLAG_MERGE(server.envelope);
874     FLAG_MERGE(server.envskip);
875     FLAG_MERGE(server.qvirtual);
876     FLAG_MERGE(server.skip);
877     FLAG_MERGE(server.dns);
878     FLAG_MERGE(server.checkalias);
879     FLAG_MERGE(server.uidl);
880     FLAG_MERGE(server.principal);
881
882 #if defined(linux) || defined(__FreeBSD__)
883     FLAG_MERGE(server.interface);
884     FLAG_MERGE(server.monitor);
885     FLAG_MERGE(server.interface_pair);
886 #endif /* linux || defined(__FreeBSD__) */
887
888     FLAG_MERGE(server.plugin);
889     FLAG_MERGE(server.plugout);
890
891     FLAG_MERGE(wildcard);
892     FLAG_MERGE(remotename);
893     FLAG_MERGE(password);
894     FLAG_MERGE(mda);
895     FLAG_MERGE(bsmtp);
896     FLAG_MERGE(listener);
897     FLAG_MERGE(smtpaddress);
898     FLAG_MERGE(smtpname);
899     FLAG_MERGE(preconnect);
900     FLAG_MERGE(postconnect);
901
902     FLAG_MERGE(keep);
903     FLAG_MERGE(flush);
904     FLAG_MERGE(fetchall);
905     FLAG_MERGE(rewrite);
906     FLAG_MERGE(forcecr);
907     FLAG_MERGE(stripcr);
908     FLAG_MERGE(pass8bits);
909     FLAG_MERGE(dropstatus);
910     FLAG_MERGE(dropdelivered);
911     FLAG_MERGE(mimedecode);
912     FLAG_MERGE(idle);
913     FLAG_MERGE(limit);
914     FLAG_MERGE(warnings);
915     FLAG_MERGE(fetchlimit);
916     FLAG_MERGE(batchlimit);
917 #ifdef  SSL_ENABLE
918     FLAG_MERGE(use_ssl);
919     FLAG_MERGE(sslkey);
920     FLAG_MERGE(sslcert);
921 #endif
922     FLAG_MERGE(expunge);
923
924     FLAG_MERGE(properties);
925 #undef FLAG_MERGE
926 }
927
928 static int load_params(int argc, char **argv, int optind)
929 {
930     int implicitmode, st;
931     struct passwd *pw;
932     struct query def_opts, *ctl;
933     struct stat rcstat;
934
935     run.bouncemail = TRUE;
936     run.spambounce = FALSE;     /* don't bounce back to innocent bystanders */
937
938     memset(&def_opts, '\0', sizeof(struct query));
939     def_opts.smtp_socket = -1;
940     def_opts.smtpaddress = (char *)0;
941     def_opts.smtpname = (char *)0;
942 #define ANTISPAM(n)     save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = (n)
943     ANTISPAM(571);      /* sendmail */
944     ANTISPAM(550);      /* old exim */
945     ANTISPAM(501);      /* new exim */
946     ANTISPAM(554);      /* Postfix */
947 #undef ANTISPAM
948
949     def_opts.server.protocol = P_AUTO;
950     def_opts.server.timeout = CLIENT_TIMEOUT;
951     def_opts.warnings = WARNING_INTERVAL;
952     def_opts.remotename = user;
953     def_opts.listener = SMTP_MODE;
954
955     /* note the parse time, so we can pick up on modifications */
956     parsetime = 0;      /* foil compiler warnings */
957     if (stat(rcfile, &rcstat) != -1)
958         parsetime = rcstat.st_mtime;
959     else if (errno != ENOENT)
960         report(stderr, _("couldn't time-check the run-control file\n"));
961
962     /* this builds the host list */
963     if ((st = prc_parse_file(rcfile, !versioninfo)) != 0)
964         /*
965          * FIXME: someday, send notification mail here if backgrounded.
966          * Right now, that can happen if the user changes the rcfile
967          * while the fetchmail is running in background.  Do similarly
968          * for the other exit() calls in this function.
969          */
970         exit(st);
971
972     if ((implicitmode = (optind >= argc)))
973     {
974         for (ctl = querylist; ctl; ctl = ctl->next)
975             ctl->active = TRUE;
976     }
977     else
978         for (; optind < argc; optind++) 
979         {
980             flag        predeclared =  FALSE;
981
982             /*
983              * If hostname corresponds to a host known from the rc file,
984              * simply declare it active.  Otherwise synthesize a host
985              * record from command line and defaults
986              */
987             for (ctl = querylist; ctl; ctl = ctl->next)
988                 if (!strcmp(ctl->server.pollname, argv[optind])
989                         || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
990                 {
991                     /* Is this correct? */
992                     if (predeclared && outlevel == O_VERBOSE)
993                         fprintf(stderr,_("Warning: multiple mentions of host %s in config file\n"),argv[optind]);
994                     ctl->active = TRUE;
995                     predeclared = TRUE;
996                 }
997
998             if (!predeclared)
999             {
1000                 /*
1001                  * Allocate and link record without copying in
1002                  * command-line args; we'll do that with the optmerge
1003                  * call later on.
1004                  */
1005                 ctl = hostalloc((struct query *)NULL);
1006                 ctl->server.via =
1007                     ctl->server.pollname = xstrdup(argv[optind]);
1008                 ctl->active = TRUE;
1009                 ctl->server.lead_server = (struct hostdata *)NULL;
1010             }
1011         }
1012
1013     /*
1014      * If there's a defaults record, merge it and lose it.
1015      */ 
1016     if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
1017     {
1018         for (ctl = querylist->next; ctl; ctl = ctl->next)
1019             optmerge(ctl, querylist, FALSE);
1020         querylist = querylist->next;
1021     }
1022
1023     /* don't allow a defaults record after the first */
1024     for (ctl = querylist; ctl; ctl = ctl->next)
1025         if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
1026             exit(PS_SYNTAX);
1027
1028     /* use localhost if we never fetch the FQDN of this host */
1029     fetchmailhost = "localhost";
1030
1031     /* here's where we override globals */
1032     if (cmd_run.logfile)
1033         run.logfile = cmd_run.logfile;
1034     if (cmd_run.idfile)
1035         run.idfile = cmd_run.idfile;
1036     /* do this before the keep/fetchall test below, otherwise -d0 may fail */
1037     if (cmd_run.poll_interval >= 0)
1038         run.poll_interval = cmd_run.poll_interval;
1039     if (cmd_run.invisible)
1040         run.invisible = cmd_run.invisible;
1041     if (cmd_run.showdots)
1042         run.showdots = cmd_run.showdots;
1043     if (cmd_run.use_syslog)
1044         run.use_syslog = (cmd_run.use_syslog == FLAG_TRUE);
1045     if (cmd_run.postmaster)
1046         run.postmaster = cmd_run.postmaster;
1047     if (cmd_run.bouncemail)
1048         run.bouncemail = cmd_run.bouncemail;
1049
1050     /* check and daemon options are not compatible */
1051     if (check_only && run.poll_interval)
1052         run.poll_interval = 0;
1053
1054     /* merge in wired defaults, do sanity checks and prepare internal fields */
1055     for (ctl = querylist; ctl; ctl = ctl->next)
1056     {
1057         ctl->wedged = FALSE;
1058
1059         if (configdump || ctl->active )
1060         {
1061             /* merge in defaults */
1062             optmerge(ctl, &def_opts, FALSE);
1063
1064             /* force command-line options */
1065             optmerge(ctl, &cmd_opts, TRUE);
1066
1067             /* this code enables flags to be turned off */
1068 #define DEFAULT(flag, dflt)     if (flag == FLAG_TRUE)\
1069                                         flag = TRUE;\
1070                                 else if (flag == FLAG_FALSE)\
1071                                         flag = FALSE;\
1072                                 else\
1073                                         flag = (dflt)
1074             DEFAULT(ctl->keep, FALSE);
1075             DEFAULT(ctl->fetchall, FALSE);
1076             DEFAULT(ctl->flush, FALSE);
1077             DEFAULT(ctl->rewrite, TRUE);
1078             DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL)); 
1079             DEFAULT(ctl->forcecr, FALSE);
1080             DEFAULT(ctl->pass8bits, FALSE);
1081             DEFAULT(ctl->dropstatus, FALSE);
1082             DEFAULT(ctl->dropdelivered, FALSE);
1083             DEFAULT(ctl->mimedecode, FALSE);
1084             DEFAULT(ctl->idle, FALSE);
1085             DEFAULT(ctl->server.dns, TRUE);
1086             DEFAULT(ctl->server.uidl, FALSE);
1087 #ifdef  SSL_ENABLE
1088             DEFAULT(ctl->use_ssl, FALSE);
1089 #endif
1090             DEFAULT(ctl->server.checkalias, FALSE);
1091 #undef DEFAULT
1092
1093             /*
1094              * DNS support is required for some protocols.  We used to
1095              * do this unconditionally, but it made fetchmail excessively
1096              * vulnerable to misconfigured DNS setups.
1097              *
1098              * If we're using ETRN or ODMR, the smtp hunt list is the
1099              * list of systems we're polling on behalf of; these have
1100              * to be fully-qualified domain names.  The default for
1101              * this list should be the FQDN of localhost.
1102              *
1103              * If we're using Kerberos for authentication, we need 
1104              * the FQDN in order to generate capability keys.  */
1105             if (ctl->server.protocol == P_ETRN
1106 #ifdef ODMR_ENABLE 
1107                          || ctl->server.protocol == P_ODMR
1108 #endif /* ODMR_ENABLE */
1109                          || ctl->server.preauthenticate == A_KERBEROS_V4
1110                          || ctl->server.preauthenticate == A_KERBEROS_V5)
1111                 if (strcmp(fetchmailhost, "localhost") == 0)
1112                         fetchmailhost = host_fqdn();
1113
1114             /*
1115              * Make sure we have a nonempty host list to forward to.
1116              */
1117             if (!ctl->smtphunt)
1118                 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
1119
1120             /* if `user' doesn't name a real local user, try to run as root */
1121             if ((pw = getpwnam(user)) == (struct passwd *)NULL)
1122                 ctl->uid = 0;
1123             else
1124                 ctl->uid = pw->pw_uid;  /* for local delivery via MDA */
1125             if (!ctl->localnames)       /* for local delivery via SMTP */
1126                 save_str_pair(&ctl->localnames, user, NULL);
1127
1128 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
1129             /* can't handle multidrop mailboxes unless we can do DNS lookups */
1130             if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
1131             {
1132                 ctl->server.dns = FALSE;
1133                 report(stderr, _("fetchmail: warning: no DNS available to check multidrop fetches from %s\n"), ctl->server.pollname);
1134             }
1135 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
1136
1137             /*
1138              *
1139              * Compute the true name of the mailserver host.  
1140              * There are two clashing cases here:
1141              *
1142              * (1) The poll name is a label, possibly on one of several
1143              *     poll configurations for the same host.  In this case 
1144              *     the `via' option will be present and give the true name.
1145              *
1146              * (2) The poll name is the true one, the via name is 
1147              *     localhost.   This is going to be typical for ssh-using
1148              *     configurations.
1149              *
1150              * We're going to assume the via name is true unless it's
1151              * localhost.
1152              */
1153             if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
1154                 ctl->server.queryname = xstrdup(ctl->server.via);
1155             else
1156                 ctl->server.queryname = xstrdup(ctl->server.pollname);
1157
1158 #ifdef HESIOD
1159         /* If either the pollname or vianame are "hesiod" we want to
1160            lookup the user's hesiod pobox host */
1161
1162         if (!strcasecmp(ctl->server.queryname, "hesiod")) {
1163             struct hes_postoffice *hes_p;
1164             hes_p = hes_getmailhost(ctl->remotename);
1165             if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
1166                  free(ctl->server.queryname);
1167                  ctl->server.queryname = xstrdup(hes_p->po_host);
1168                  if (ctl->server.via)
1169                      free(ctl->server.via);
1170                  ctl->server.via = xstrdup(hes_p->po_host);
1171             } else {
1172                  report(stderr,
1173                         _("couldn't find HESIOD pobox for %s\n"),
1174                         ctl->remotename);
1175             }
1176         }
1177 #endif /* HESIOD */
1178
1179             /*
1180              * We may have to canonicalize the server truename for later use.
1181              * Do this just once for each lead server, if necessary, in order
1182              * to minimize DNS round trips.
1183              */
1184             if (ctl->server.lead_server)
1185             {
1186                 char    *leadname = ctl->server.lead_server->truename;
1187
1188                 /* prevent core dump from ill-formed or duplicate entry */
1189                 if (!leadname)
1190                 {
1191                     report(stderr, _("Lead server has no name.\n"));
1192                     exit(PS_SYNTAX);
1193                 }
1194
1195                 ctl->server.truename = xstrdup(leadname);
1196             }
1197 #ifdef HAVE_GETHOSTBYNAME
1198             else if (!configdump)
1199             {
1200                 if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
1201                       ctl->server.preauthenticate==A_KERBEROS_V5 ||
1202                     (ctl->server.dns && MULTIDROP(ctl)))
1203                 {
1204                     struct hostent      *namerec;
1205
1206                     /* compute the canonical name of the host */
1207                     errno = 0;
1208                     namerec = gethostbyname(ctl->server.queryname);
1209                     if (namerec == (struct hostent *)NULL)
1210                     {
1211                         report(stderr,
1212                                _("couldn't find canonical DNS name of %s\n"),
1213                                ctl->server.pollname);
1214                         ctl->server.truename = xstrdup(ctl->server.queryname);
1215                         ctl->server.trueaddr = NULL;
1216                     }
1217                     else
1218                         ctl->server.truename=xstrdup((char *)namerec->h_name);
1219                 }
1220 #endif /* HAVE_GETHOSTBYNAME */
1221                 else {
1222 #ifdef HAVE_GETHOSTBYNAME
1223                     struct hostent      *namerec;
1224                     
1225                     /* <fetchmail@mail.julianhaight.com>
1226                        Get the host's IP, so we can report it like this:
1227
1228                        Received: from hostname [10.0.0.1]
1229
1230                        do we actually need to gethostbyname to find the IP?
1231                        it seems like it would be faster to do this later, when
1232                        we are actually resolving the hostname for a connection,
1233                        but I ain't that smart, so I don't know where to make
1234                        the change later..
1235                     */
1236                     errno = 0;
1237                     namerec = gethostbyname(ctl->server.queryname);
1238                     if (namerec == (struct hostent *)NULL)
1239                     {
1240                         report(stderr,
1241                                _("couldn't find canonical DNS name of %s\n"),
1242                                ctl->server.pollname);
1243                         exit(PS_DNS);
1244                     }
1245                     else {
1246                         ctl->server.truename=xstrdup((char *)namerec->h_name);
1247                         ctl->server.trueaddr=xmalloc(namerec->h_length);
1248                         memcpy(ctl->server.trueaddr, 
1249                                namerec->h_addr_list[0],
1250                                namerec->h_length);
1251                     }
1252 #else
1253                     ctl->server.truename = xstrdup(ctl->server.queryname);
1254 #endif /* HAVE_GETHOSTBYNAME */
1255                 }
1256             }
1257
1258             /* if no folders were specified, set up the null one as default */
1259             if (!ctl->mailboxes)
1260                 save_str(&ctl->mailboxes, (char *)NULL, 0);
1261
1262             /* maybe user overrode timeout on command line? */
1263             if (ctl->server.timeout == -1)      
1264                 ctl->server.timeout = CLIENT_TIMEOUT;
1265
1266 #if !INET6_ENABLE
1267             /* sanity checks */
1268             if (ctl->server.port < 0)
1269             {
1270                 (void) fprintf(stderr,
1271                                _("%s configuration invalid, port number cannot be negative\n"),
1272                                ctl->server.pollname);
1273                 exit(PS_SYNTAX);
1274             }
1275             if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
1276             {
1277                 (void) fprintf(stderr,
1278                                _("%s configuration invalid, RPOP requires a privileged port\n"),
1279                                ctl->server.pollname);
1280                 exit(PS_SYNTAX);
1281             }
1282             if (ctl->listener == LMTP_MODE)
1283             {
1284                 struct idlist   *idp;
1285
1286                 for (idp = ctl->smtphunt; idp; idp = idp->next)
1287                 {
1288                     char        *cp;
1289
1290                     if (!(cp = strrchr(idp->id, '/')) ||
1291                                 (atoi(++cp) == SMTP_PORT))
1292                     {
1293                         (void) fprintf(stderr,
1294                                        _("%s configuration invalid, LMTP can't use default SMTP port\n"),
1295                                        ctl->server.pollname);
1296                         exit(PS_SYNTAX);
1297                     }
1298                 }
1299             }
1300 #endif /* !INET6_ENABLE */
1301
1302             /*
1303              * "I beg to you, have mercy on the week minds like myself."
1304              * wrote Pehr Anderson.  Your petition is granted.
1305              */
1306             if (ctl->fetchall && ctl->keep && run.poll_interval && !nodetach)
1307             {
1308                 (void) fprintf(stderr,
1309                                _("Both fetchall and keep on in daemon mode is a mistake!\n"));
1310                 exit(PS_SYNTAX);
1311             }
1312         }
1313     }
1314
1315 #ifdef POP3_ENABLE
1316     /* initialize UID handling */
1317     if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
1318         exit(st);
1319     else
1320         initialize_saved_lists(querylist, run.idfile);
1321 #endif /* POP3_ENABLE */
1322
1323     /*
1324      * If the user didn't set a last-resort user to get misaddressed
1325      * multidrop mail, set an appropriate default here.
1326      */
1327     if (!run.postmaster)
1328     {
1329         if (getuid())                           /* ordinary user */
1330             run.postmaster = user;
1331         else                                    /* root */
1332             run.postmaster = "postmaster";
1333     }
1334
1335     return(implicitmode);
1336 }
1337
1338 static void terminate_poll(int sig)
1339 /* to be executed at the end of a poll cycle */
1340 {
1341     /*
1342      * Close all SMTP delivery sockets.  For optimum performance
1343      * we'd like to hold them open til end of run, but (1) this
1344      * loses if our poll interval is longer than the MTA's inactivity
1345      * timeout, and (2) some MTAs (like smail) don't deliver after
1346      * each message, but rather queue up mail and wait to actually
1347      * deliver it until the input socket is closed. 
1348      *
1349      * Sending SMTP QUIT on signal is theoretically nice, but led to a 
1350      * subtle bug.  If fetchmail was terminated by signal while it was 
1351      * shipping message text, it would hang forever waiting for a
1352      * command acknowledge.  In theory we could enable the QUIT
1353      * only outside of the message send.  In practice, we don't
1354      * care.  All mailservers hang up on a dropped TCP/IP connection
1355      * anyway.
1356      */
1357
1358     if (sig != 0)
1359         report(stdout, _("terminated with signal %d\n"), sig);
1360     else
1361     {
1362         struct query *ctl;
1363
1364         /* terminate all SMTP connections cleanly */
1365         for (ctl = querylist; ctl; ctl = ctl->next)
1366             if (ctl->smtp_socket != -1)
1367             {
1368                 SMTP_quit(ctl->smtp_socket);
1369                 SockClose(ctl->smtp_socket);
1370                 ctl->smtp_socket = -1;
1371             }
1372     }
1373
1374 #ifdef POP3_ENABLE
1375     /*
1376      * Update UID information at end of each poll, rather than at end
1377      * of run, because that way we don't lose all UIDL information since
1378      * the beginning of time if fetchmail crashes.
1379      */
1380     if (!check_only)
1381         write_saved_lists(querylist, run.idfile);
1382 #endif /* POP3_ENABLE */
1383 }
1384
1385 static void terminate_run(int sig)
1386 /* to be executed on normal or signal-induced termination */
1387 {
1388     struct query        *ctl;
1389
1390     terminate_poll(sig);
1391
1392     /* 
1393      * Craig Metz, the RFC1938 one-time-password guy, points out:
1394      * "Remember that most kernels don't zero pages before handing them to the
1395      * next process and many kernels share pages between user and kernel space.
1396      * You'd be very surprised what you can find from a short program to do a
1397      * malloc() and then dump the contents of the pages you got. By zeroing
1398      * the secrets at end of run (earlier if you can), you make sure the next
1399      * guy can't get the password/pass phrase."
1400      *
1401      * Right you are, Craig!
1402      */
1403     for (ctl = querylist; ctl; ctl = ctl->next)
1404         if (ctl->password)
1405           memset(ctl->password, '\0', strlen(ctl->password));
1406
1407 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
1408     unlockit();
1409 #endif
1410
1411     if (activecount == 0)
1412         exit(PS_NOMAIL);
1413     else
1414         exit(successes ? PS_SUCCESS : querystatus);
1415 }
1416
1417 /*
1418  * Sequence of protocols to try when autoprobing, most capable to least.
1419  */
1420 static const int autoprobe[] = 
1421 {
1422 #ifdef IMAP_ENABLE
1423     P_IMAP,
1424 #endif /* IMAP_ENABLE */
1425 #ifdef POP3_ENABLE
1426     P_POP3,
1427 #endif /* POP3_ENABLE */
1428 #ifdef POP2_ENABLE
1429     P_POP2
1430 #endif /* POP2_ENABLE */
1431 };
1432
1433 static int query_host(struct query *ctl)
1434 /* perform fetch transaction with single host */
1435 {
1436     int i, st;
1437
1438     /*
1439      * If we're syslogging the progress messages are automatically timestamped.
1440      * Force timestamping if we're going to a logfile.
1441      */
1442     if (outlevel >= O_VERBOSE || (run.logfile && outlevel > O_SILENT))
1443     {
1444         report(stdout, _("%s querying %s (protocol %s) at %s\n"),
1445                VERSION,
1446                ctl->server.pollname,
1447                showproto(ctl->server.protocol),
1448                timestamp());
1449     }
1450     switch (ctl->server.protocol) {
1451     case P_AUTO:
1452         for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
1453         {
1454             ctl->server.protocol = autoprobe[i];
1455             if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
1456                 break;
1457         }
1458         ctl->server.protocol = P_AUTO;
1459         return(st);
1460     case P_POP2:
1461 #ifdef POP2_ENABLE
1462         return(doPOP2(ctl));
1463 #else
1464         report(stderr, _("POP2 support is not configured.\n"));
1465         return(PS_PROTOCOL);
1466 #endif /* POP2_ENABLE */
1467         break;
1468     case P_POP3:
1469     case P_APOP:
1470     case P_RPOP:
1471 #ifdef POP3_ENABLE
1472         return(doPOP3(ctl));
1473 #else
1474         report(stderr, _("POP3 support is not configured.\n"));
1475         return(PS_PROTOCOL);
1476 #endif /* POP3_ENABLE */
1477         break;
1478     case P_IMAP:
1479 #ifdef GSSAPI
1480     case P_IMAP_GSS:
1481 #endif /* GSSAPI */
1482 #ifdef IMAP_ENABLE
1483         return(doIMAP(ctl));
1484 #else
1485         report(stderr, _("IMAP support is not configured.\n"));
1486         return(PS_PROTOCOL);
1487 #endif /* IMAP_ENABLE */
1488     case P_ETRN:
1489 #ifndef ETRN_ENABLE
1490         report(stderr, _("ETRN support is not configured.\n"));
1491         return(PS_PROTOCOL);
1492 #else
1493 #ifdef HAVE_GETHOSTBYNAME
1494         return(doETRN(ctl));
1495 #else
1496         report(stderr, _("Cannot support ETRN without gethostbyname(2).\n"));
1497         return(PS_PROTOCOL);
1498 #endif /* HAVE_GETHOSTBYNAME */
1499 #endif /* ETRN_ENABLE */
1500     case P_ODMR:
1501 #ifndef ODMR_ENABLE
1502         report(stderr, _("ODMR support is not configured.\n"));
1503         return(PS_PROTOCOL);
1504 #else
1505 #ifdef HAVE_GETHOSTBYNAME
1506         return(doODMR(ctl));
1507 #else
1508         report(stderr, _("Cannot support ODMR without gethostbyname(2).\n"));
1509         return(PS_PROTOCOL);
1510 #endif /* HAVE_GETHOSTBYNAME */
1511 #endif /* ODMR_ENABLE */
1512     default:
1513         report(stderr, _("unsupported protocol selected.\n"));
1514         return(PS_PROTOCOL);
1515     }
1516 }
1517
1518 static void dump_params (struct runctl *runp,
1519                          struct query *querylist, flag implicit)
1520 /* display query parameters in English */
1521 {
1522     struct query *ctl;
1523
1524     if (runp->poll_interval)
1525         printf(_("Poll interval is %d seconds\n"), runp->poll_interval);
1526     if (runp->logfile)
1527         printf(_("Logfile is %s\n"), runp->logfile);
1528     if (strcmp(runp->idfile, IDFILE_NAME))
1529         printf(_("Idfile is %s\n"), runp->idfile);
1530 #if defined(HAVE_SYSLOG)
1531     if (runp->use_syslog)
1532         printf(_("Progress messages will be logged via syslog\n"));
1533 #endif
1534     if (runp->invisible)
1535         printf(_("Fetchmail will masquerade and will not generate Received\n"));
1536     if (runp->showdots)
1537         printf(_("Fetchmail will show progress dots even in logfiles.\n"));
1538     if (runp->postmaster)
1539         printf(_("Fetchmail will forward misaddressed multidrop messages to %s.\n"),
1540                runp->postmaster);
1541
1542     if (!runp->bouncemail)
1543         printf(_("Fetchmail will direct error mail to the postmaster.\n"));
1544     else if (outlevel >= O_VERBOSE)
1545         printf(_("Fetchmail will direct error mail to the sender.\n"));
1546
1547     for (ctl = querylist; ctl; ctl = ctl->next)
1548     {
1549         if (!ctl->active || (implicit && ctl->server.skip))
1550             continue;
1551
1552         printf(_("Options for retrieving from %s@%s:\n"),
1553                ctl->remotename, visbuf(ctl->server.pollname));
1554
1555         if (ctl->server.via && (ctl->server.protocol < P_ETRN))
1556             printf(_("  Mail will be retrieved via %s\n"), ctl->server.via);
1557
1558         if (ctl->server.interval)
1559             printf(_("  Poll of this server will occur every %d intervals.\n"),
1560                    ctl->server.interval);
1561         if (ctl->server.truename)
1562             printf(_("  True name of server is %s.\n"), ctl->server.truename);
1563         if (ctl->server.skip || outlevel >= O_VERBOSE)
1564             printf(_("  This host %s be queried when no host is specified.\n"),
1565                    ctl->server.skip ? _("will not") : _("will"));
1566         /*
1567          * Don't poll for password when there is one or when using the ETRN
1568          * or GSSAPI or KERBEROS protocol
1569          */
1570         /* ETRN, IMAP_GSS, and IMAP_K4 do not need a password, so skip this */
1571         if ( (ctl->server.protocol != P_ETRN)
1572 #ifdef GSSAPI
1573                         && (ctl->server.preauthenticate != A_GSSAPI)
1574 #endif /* GSSAPI */
1575                         && (ctl->server.preauthenticate != A_KERBEROS_V4) 
1576                         && (ctl->server.preauthenticate != A_KERBEROS_V5)) 
1577         {
1578                 if (!ctl->password)
1579                         printf(_("  Password will be prompted for.\n"));
1580                 else if (outlevel >= O_VERBOSE)
1581                 {
1582                         if (ctl->server.protocol == P_APOP)
1583                                 printf(_("  APOP secret = \"%s\".\n"),
1584                                                         visbuf(ctl->password));
1585                         else if (ctl->server.protocol == P_RPOP)
1586                                 printf(_("  RPOP id = \"%s\".\n"),
1587                                                         visbuf(ctl->password));
1588                         else
1589                                 printf(_("  Password = \"%s\".\n"),
1590                                                         visbuf(ctl->password));
1591                 }
1592         }
1593
1594         if (ctl->server.protocol == P_POP3 
1595 #if INET6_ENABLE
1596             && ctl->server.service && !strcmp(ctl->server.service, KPOP_PORT)
1597 #else /* INET6_ENABLE */
1598             && ctl->server.port == KPOP_PORT
1599 #endif /* INET6_ENABLE */
1600             && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1601                 ctl->server.preauthenticate == A_KERBEROS_V5))
1602             printf(_("  Protocol is KPOP with Kerberos %s authentication"),
1603                    ctl->server.preauthenticate == A_KERBEROS_V5 ? "V" : "IV");
1604         else
1605             printf(_("  Protocol is %s"), showproto(ctl->server.protocol));
1606 #if INET6_ENABLE
1607         if (ctl->server.service)
1608             printf(_(" (using service %s)"), ctl->server.service);
1609         if (ctl->server.netsec)
1610             printf(_(" (using network security options %s)"), ctl->server.netsec);
1611 #else /* INET6_ENABLE */
1612         if (ctl->server.port)
1613             printf(_(" (using port %d)"), ctl->server.port);
1614 #endif /* INET6_ENABLE */
1615         else if (outlevel >= O_VERBOSE)
1616             printf(_(" (using default port)"));
1617         if (ctl->server.uidl && (ctl->server.protocol < P_ETRN))
1618             printf(_(" (forcing UIDL use)"));
1619         putchar('.');
1620         putchar('\n');
1621         if (ctl->server.preauthenticate == A_KERBEROS_V4)
1622             printf(_("  Kerberos V4 preauthentication enabled.\n"));
1623         else if (ctl->server.preauthenticate == A_KERBEROS_V5)
1624             printf(_("  Kerberos V5 preauthentication enabled.\n"));
1625         else if (ctl->server.preauthenticate == A_SSH)
1626             printf(_("  End-to-end encryption assumed.\n"));
1627         if (ctl->server.principal != (char *) NULL) {
1628             printf(_("  Mail service principal is: %s\n"), ctl->server.principal);
1629         }
1630 #ifdef  SSL_ENABLE
1631         if (ctl->use_ssl)
1632             printf("  SSL encrypted sessions enabled.\n");
1633 #endif
1634         if (ctl->server.timeout > 0)
1635             printf(_("  Server nonresponse timeout is %d seconds"), ctl->server.timeout);
1636         if (ctl->server.timeout ==  CLIENT_TIMEOUT)
1637             printf(_(" (default).\n"));
1638         else
1639             printf(".\n");
1640
1641         if (ctl->server.protocol < P_ETRN) {
1642                 if (!ctl->mailboxes->id)
1643                     printf(_("  Default mailbox selected.\n"));
1644                 else
1645                 {
1646                     struct idlist *idp;
1647
1648                     printf(_("  Selected mailboxes are:"));
1649                     for (idp = ctl->mailboxes; idp; idp = idp->next)
1650                         printf(" %s", idp->id);
1651                     printf("\n");
1652                 }
1653                 printf(_("  %s messages will be retrieved (--all %s).\n"),
1654                        ctl->fetchall ? _("All") : _("Only new"),
1655                        ctl->fetchall ? "on" : "off");
1656                 printf(_("  Fetched messages %s be kept on the server (--keep %s).\n"),
1657                        ctl->keep ? _("will") : _("will not"),
1658                        ctl->keep ? "on" : "off");
1659                 printf(_("  Old messages %s be flushed before message retrieval (--flush %s).\n"),
1660                        ctl->flush ? _("will") : _("will not"),
1661                        ctl->flush ? "on" : "off");
1662                 printf(_("  Rewrite of server-local addresses is %s (--norewrite %s).\n"),
1663                        ctl->rewrite ? _("enabled") : _("disabled"),
1664                        ctl->rewrite ? "off" : "on");
1665                 printf(_("  Carriage-return stripping is %s (stripcr %s).\n"),
1666                        ctl->stripcr ? _("enabled") : _("disabled"),
1667                        ctl->stripcr ? "on" : "off");
1668                 printf(_("  Carriage-return forcing is %s (forcecr %s).\n"),
1669                        ctl->forcecr ? _("enabled") : _("disabled"),
1670                        ctl->forcecr ? "on" : "off");
1671                 printf(_("  Interpretation of Content-Transfer-Encoding is %s (pass8bits %s).\n"),
1672                        ctl->pass8bits ? _("disabled") : _("enabled"),
1673                        ctl->pass8bits ? "on" : "off");
1674                 printf(_("  MIME decoding is %s (mimedecode %s).\n"),
1675                        ctl->mimedecode ? _("enabled") : _("disabled"),
1676                        ctl->mimedecode ? "on" : "off");
1677                 printf(_("  Idle after poll is %s (idle %s).\n"),
1678                        ctl->idle ? _("enabled") : _("disabled"),
1679                        ctl->idle ? "on" : "off");
1680                 printf(_("  Nonempty Status lines will be %s (dropstatus %s)\n"),
1681                        ctl->dropstatus ? _("discarded") : _("kept"),
1682                        ctl->dropstatus ? "on" : "off");
1683                 printf(_("  Delivered-To lines will be %s (dropdelivered %s)\n"),
1684                        ctl->dropdelivered ? _("discarded") : _("kept"),
1685                        ctl->dropdelivered ? "on" : "off");
1686                 if (NUM_NONZERO(ctl->limit))
1687                 {
1688                     if (NUM_NONZERO(ctl->limit))
1689                         printf(_("  Message size limit is %d octets (--limit %d).\n"), 
1690                                ctl->limit, ctl->limit);
1691                     else if (outlevel >= O_VERBOSE)
1692                         printf(_("  No message size limit (--limit 0).\n"));
1693                     if (run.poll_interval > 0)
1694                         printf(_("  Message size warning interval is %d seconds (--warnings %d).\n"), 
1695                                ctl->warnings, ctl->warnings);
1696                     else if (outlevel >= O_VERBOSE)
1697                         printf(_("  Size warnings on every poll (--warnings 0).\n"));
1698                 }
1699                 if (NUM_NONZERO(ctl->fetchlimit))
1700                     printf(_("  Received-message limit is %d (--fetchlimit %d).\n"),
1701                            ctl->fetchlimit, ctl->fetchlimit);
1702                 else if (outlevel >= O_VERBOSE)
1703                     printf(_("  No received-message limit (--fetchlimit 0).\n"));
1704                 if (NUM_NONZERO(ctl->batchlimit))
1705                     printf(_("  SMTP message batch limit is %d.\n"), ctl->batchlimit);
1706                 else if (outlevel >= O_VERBOSE)
1707                     printf(_("  No SMTP message batch limit (--batchlimit 0).\n"));
1708                 if (ctl->server.protocol < P_ETRN)
1709                 {
1710                     if (NUM_NONZERO(ctl->expunge))
1711                         printf(_("  Deletion interval between expunges forced to %d (--expunge %d).\n"), ctl->expunge, ctl->expunge);
1712                     else if (outlevel >= O_VERBOSE)
1713                         printf(_("  No forced expunges (--expunge 0).\n"));
1714                 }
1715         }
1716         if (ctl->bsmtp)
1717             printf(_("  Messages will be appended to %s as BSMTP\n"), visbuf(ctl->bsmtp));
1718         else if (ctl->mda && (ctl->server.protocol < P_ETRN))
1719             printf(_("  Messages will be delivered with \"%s\".\n"), visbuf(ctl->mda));
1720         else
1721         {
1722             struct idlist *idp;
1723
1724             printf(_("  Messages will be %cMTP-forwarded to:"), ctl->listener);
1725             for (idp = ctl->smtphunt; idp; idp = idp->next)
1726             {
1727                 printf(" %s", idp->id);
1728                 if (!idp->val.status.mark)
1729                     printf(_(" (default)"));
1730             }
1731             printf("\n");
1732             if (ctl->smtpaddress)
1733                 printf(_("  Host part of MAIL FROM line will be %s\n"),
1734                        ctl->smtpaddress);
1735             if (ctl->smtpname)
1736                 printf(_("  Address to be put in RCPT TO lines shipped to SMTP will be %s\n"),
1737                        ctl->smtpname);
1738         }
1739         if (ctl->server.protocol < P_ETRN)
1740         {
1741                 if (ctl->antispam != (struct idlist *)NULL)
1742                 {
1743                     struct idlist *idp;
1744
1745                     printf(_("  Recognized listener spam block responses are:"));
1746                     for (idp = ctl->antispam; idp; idp = idp->next)
1747                         printf(" %d", idp->val.status.num);
1748                     printf("\n");
1749                 }
1750                 else if (outlevel >= O_VERBOSE)
1751                     printf(_("  Spam-blocking disabled\n"));
1752         }
1753         if (ctl->preconnect)
1754             printf(_("  Server connection will be brought up with \"%s\".\n"),
1755                    visbuf(ctl->preconnect));
1756         else if (outlevel >= O_VERBOSE)
1757             printf(_("  No pre-connection command.\n"));
1758         if (ctl->postconnect)
1759             printf(_("  Server connection will be taken down with \"%s\".\n"),
1760                    visbuf(ctl->postconnect));
1761         else if (outlevel >= O_VERBOSE)
1762             printf(_("  No post-connection command.\n"));
1763         if (ctl->server.protocol < P_ETRN) {
1764                 if (!ctl->localnames)
1765                     printf(_("  No localnames declared for this host.\n"));
1766                 else
1767                 {
1768                     struct idlist *idp;
1769                     int count = 0;
1770
1771                     for (idp = ctl->localnames; idp; idp = idp->next)
1772                         ++count;
1773
1774                     if (count > 1 || ctl->wildcard)
1775                         printf(_("  Multi-drop mode: "));
1776                     else
1777                         printf(_("  Single-drop mode: "));
1778
1779                     printf(_("%d local name(s) recognized.\n"), count);
1780                     if (outlevel >= O_VERBOSE)
1781                     {
1782                         for (idp = ctl->localnames; idp; idp = idp->next)
1783                             if (idp->val.id2)
1784                                 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1785                             else
1786                                 printf("\t%s\n", idp->id);
1787                         if (ctl->wildcard)
1788                             fputs("\t*\n", stdout);
1789                     }
1790
1791                     if (count > 1 || ctl->wildcard)
1792                     {
1793                         printf(_("  DNS lookup for multidrop addresses is %s.\n"),
1794                                ctl->server.dns ? _("enabled") : _("disabled"));
1795                         if (ctl->server.dns)
1796                         {
1797                             printf(_("  Server aliases will be compared with multidrop addresses by "));
1798                             if (ctl->server.checkalias)
1799                                 printf(_("IP address.\n"));
1800                             else
1801                                 printf(_("name.\n"));
1802                         }
1803                         if (ctl->server.envelope == STRING_DISABLED)
1804                             printf(_("  Envelope-address routing is disabled\n"));
1805                         else
1806                         {
1807                             printf(_("  Envelope header is assumed to be: %s\n"),
1808                                    ctl->server.envelope ? ctl->server.envelope:_("Received"));
1809                             if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1810                                 printf(_("  Number of envelope header to be parsed: %d\n"),
1811                                        ctl->server.envskip);
1812                             if (ctl->server.qvirtual)
1813                                 printf(_("  Prefix %s will be removed from user id\n"),
1814                                        ctl->server.qvirtual);
1815                             else if (outlevel >= O_VERBOSE) 
1816                                 printf(_("  No prefix stripping\n"));
1817                         }
1818
1819                         if (ctl->server.akalist)
1820                         {
1821                             struct idlist *idp;
1822
1823                             printf(_("  Predeclared mailserver aliases:"));
1824                             for (idp = ctl->server.akalist; idp; idp = idp->next)
1825                                 printf(" %s", idp->id);
1826                             putchar('\n');
1827                         }
1828                         if (ctl->server.localdomains)
1829                         {
1830                             struct idlist *idp;
1831
1832                             printf(_("  Local domains:"));
1833                             for (idp = ctl->server.localdomains; idp; idp = idp->next)
1834                                 printf(" %s", idp->id);
1835                             putchar('\n');
1836                         }
1837                     }
1838                 }
1839         }
1840 #if defined(linux) || defined(__FreeBSD__)
1841         if (ctl->server.interface)
1842             printf(_("  Connection must be through interface %s.\n"), ctl->server.interface);
1843         else if (outlevel >= O_VERBOSE)
1844             printf(_("  No interface requirement specified.\n"));
1845         if (ctl->server.monitor)
1846             printf(_("  Polling loop will monitor %s.\n"), ctl->server.monitor);
1847         else if (outlevel >= O_VERBOSE)
1848             printf(_("  No monitor interface specified.\n"));
1849 #endif
1850
1851         if (ctl->server.plugin)
1852             printf(_("  Server connections will be made via plugin %s (--plugin %s).\n"), ctl->server.plugin, ctl->server.plugin);
1853         else if (outlevel >= O_VERBOSE)
1854             printf(_("  No plugin command specified.\n"));
1855         if (ctl->server.plugout)
1856             printf(_("  Listener connections will be made via plugout %s (--plugout %s).\n"), ctl->server.plugout, ctl->server.plugout);
1857         else if (outlevel >= O_VERBOSE)
1858             printf(_("  No plugout command specified.\n"));
1859
1860         if (ctl->server.protocol > P_POP2 && (ctl->server.protocol < P_ETRN))
1861         {
1862             if (!ctl->oldsaved)
1863                 printf(_("  No UIDs saved from this host.\n"));
1864             else
1865             {
1866                 struct idlist *idp;
1867                 int count = 0;
1868
1869                 for (idp = ctl->oldsaved; idp; idp = idp->next)
1870                     ++count;
1871
1872                 printf(_("  %d UIDs saved.\n"), count);
1873                 if (outlevel >= O_VERBOSE)
1874                     for (idp = ctl->oldsaved; idp; idp = idp->next)
1875                         printf("\t%s\n", idp->id);
1876             }
1877         }
1878
1879         if (ctl->properties)
1880             printf(_("  Pass-through properties \"%s\".\n"),
1881                    visbuf(ctl->properties));
1882     }
1883 }
1884
1885 /* fetchmail.c ends here */