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