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