]> Pileus Git - ~andy/fetchmail/blob - fetchmail.c
Return of the dancing progress dots.
[~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 #include <ctype.h>
10 #if defined(STDC_HEADERS)
11 #include <stdlib.h>
12 #endif
13 #if defined(HAVE_UNISTD_H)
14 #include <unistd.h>
15 #endif
16 #include <string.h>
17 #include <signal.h>
18 #if defined(HAVE_SYSLOG)
19 #include <syslog.h>
20 #endif
21 #include <pwd.h>
22 #include <errno.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_SETRLIMIT
27 #include <sys/resource.h>
28 #endif /* HAVE_SETRLIMIT */
29 #ifdef HAVE_SYS_WAIT_H
30 #include <sys/wait.h>
31 #endif
32
33 #ifdef HAVE_GETHOSTBYNAME
34 #include <netdb.h>
35 #endif /* HAVE_GETHOSTBYNAME */
36
37 #ifdef HESIOD
38 #include <hesiod.h>
39 #endif
40
41 #include "fetchmail.h"
42 #include "tunable.h"
43 #include "smtp.h"
44 #include "getopt.h"
45 #include "netrc.h"
46 #include "i18n.h"
47
48 #ifndef ENETUNREACH
49 #define ENETUNREACH   128       /* Interactive doesn't know this */
50 #endif /* ENETUNREACH */
51
52 /* prototypes for internal functions */
53 static int load_params(int, char **, int);
54 static void dump_params (struct runctl *runp, struct query *, flag implicit);
55 static int query_host(struct query *);
56
57 /* controls the detail level of status/progress messages written to stderr */
58 int outlevel;               /* see the O_.* constants above */
59
60 /* miscellaneous global controls */
61 struct runctl run;          /* global controls for this run */
62 flag nodetach;              /* if TRUE, don't detach daemon process */
63 flag quitmode;              /* if --quit was set */
64 flag check_only;            /* if --probe was set */
65 flag versioninfo;           /* emit only version info */
66 char *user;                 /* the name of the invoking user */
67 char *home;                 /* invoking user's home directory */
68 char *program_name;         /* the name to prefix error messages with */
69 flag configdump;            /* dump control blocks for configurator */
70 const char *fetchmailhost;  /* either `localhost' or the host's FQDN */
71
72 #if NET_SECURITY
73 void *request = NULL;
74 int requestlen = 0;
75 #endif /* NET_SECURITY */
76
77 static char *lockfile;          /* name of lockfile */
78 static int querystatus;         /* status of query */
79 static int successes;           /* count number of successful polls */
80 static int lastsig;             /* last signal received */
81 static struct runctl cmd_run;   /* global options set from command line */
82
83 static void termhook(int);              /* forward declaration of exit hook */
84
85 #if 0
86 #define SLEEP_WITH_ALARM
87 #endif
88
89 #ifdef SLEEP_WITH_ALARM
90 /*
91  * The function of this variable is to remove the window during which a
92  * SIGALRM can hose the code (ALARM is triggered *before* pause() is called).
93  * This is a bit of a kluge; the real right thing would use sigprocmask(),
94  * sigsuspend().
95  * This work around lets the interval timer trigger the first alarm after the
96  * required interval and will then generate alarms all 5 seconds, until it
97  * is certain, that the critical section (ie., the window) is left.
98  */
99 #if defined(STDC_HEADERS)
100 static sig_atomic_t     alarm_latch = FALSE;
101 #else
102 /* assume int can be written in one atomic operation on non ANSI-C systems */
103 static int              alarm_latch = FALSE;
104 #endif
105
106 RETSIGTYPE gotsigalrm(sig)
107 int sig;
108 {
109     signal(sig, gotsigalrm);
110     lastsig = sig;
111     alarm_latch = TRUE;
112 }
113 #endif /* SLEEP_WITH_ALARM */
114
115 RETSIGTYPE donothing(int sig) {signal(sig, donothing); lastsig = sig;}
116
117 #ifdef HAVE_ON_EXIT
118 static void unlockit(int n, void *p)
119 #else
120 static void unlockit(void)
121 #endif
122 /* must-do actions for exit (but we can't count on being able to do malloc) */
123 {
124     unlink(lockfile);
125 }
126
127 #ifdef __EMX__
128 /* Various EMX-specific definitions */
129 int itimerflag;
130 void itimerthread(void* dummy) {
131   if (outlevel >= O_VERBOSE)
132     fprintf(stderr, _("fetchmail: thread sleeping for %d sec.\n"), poll_interval);
133   while(1) {
134     _sleep2(poll_interval*1000);
135     kill((getpid()), SIGALRM);
136   }
137 }
138 #endif
139
140 int main (int argc, char **argv)
141 {
142     int st, bkgd = FALSE;
143     int parsestatus, implicitmode = FALSE;
144     FILE        *lockfp;
145     struct query *ctl;
146     netrc_entry *netrc_list;
147     char *netrc_file, *tmpbuf;
148     pid_t pid;
149
150     envquery(argc, argv);
151 #ifdef ENABLE_NLS
152     bindtextdomain(PACKAGE, LOCALEDIR);
153     textdomain(PACKAGE);
154 #endif
155
156 #define IDFILE_NAME     ".fetchids"
157     run.idfile = (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
158     strcpy(run.idfile, home);
159     strcat(run.idfile, "/");
160     strcat(run.idfile, IDFILE_NAME);
161   
162     outlevel = O_NORMAL;
163
164     if ((parsestatus = parsecmdline(argc,argv, &cmd_run, &cmd_opts)) < 0)
165         exit(PS_SYNTAX);
166
167     if (versioninfo)
168     {
169         printf(_("This is fetchmail release %s"), VERSION);
170 #ifdef POP2_ENABLE
171         printf("+POP2");
172 #endif /* POP2_ENABLE */
173 #ifndef POP3_ENABLE
174         printf("-POP3");
175 #endif /* POP3_ENABLE */
176 #ifndef IMAP_ENABLE
177         printf("-IMAP");
178 #endif /* IMAP_ENABLE */
179 #ifdef GSSAPI
180         printf("+IMAP-GSS");
181 #endif /* GSSAPI */
182 #ifdef RPA_ENABLE
183         printf("+RPA");
184 #endif /* RPA_ENABLE */
185 #ifdef SDPS_ENABLE
186         printf("+SDPS");
187 #endif /* SDPS_ENABLE */
188 #ifndef ETRN_ENABLE
189         printf("-ETRN");
190 #endif /* ETRN_ENABLE */
191 #if OPIE
192         printf("+OPIE");
193 #endif /* OPIE */
194 #if INET6
195         printf("+INET6");
196 #endif /* INET6 */
197 #if NET_SECURITY
198         printf("+NETSEC");
199 #endif /* NET_SECURITY */
200 #ifdef HAVE_SOCKS
201   #if HAVE_SOCKS
202         printf("+SOCKS");
203   #endif
204 #endif /* HAVE_SOCKS */
205 #if ENABLE_NLS
206         printf("+NLS");
207 #endif /* ENABLE_NLS */
208         putchar('\n');
209
210         /* this is an attempt to help remote debugging */
211         system("uname -a");
212     }
213
214     /* avoid parsing the config file if all we're doing is killing a daemon */ 
215     if (!(quitmode && argc == 2))
216         implicitmode = load_params(argc, argv, optind);
217
218     /* set up to do lock protocol */
219 #define FETCHMAIL_PIDFILE       "fetchmail.pid"
220     if (!getuid()) {
221         xalloca(tmpbuf, char *,
222                 strlen(PID_DIR) + strlen(FETCHMAIL_PIDFILE) + 2);
223         sprintf(tmpbuf, "%s/%s", PID_DIR, FETCHMAIL_PIDFILE);
224     } else {
225         xalloca(tmpbuf, char *, strlen(home) + strlen(FETCHMAIL_PIDFILE) + 3);
226         strcpy(tmpbuf, home);
227         strcat(tmpbuf, "/.");
228         strcat(tmpbuf, FETCHMAIL_PIDFILE);
229     }
230 #undef FETCHMAIL_PIDFILE
231
232     /* perhaps we just want to check options? */
233     if (versioninfo)
234     {
235         printf(_("Taking options from command line"));
236         if (access(rcfile, 0))
237             printf("\n");
238         else
239             printf(_(" and %s\n"), rcfile);
240         if (outlevel >= O_VERBOSE)
241             printf(_("Lockfile at %s\n"), tmpbuf);
242
243         if (querylist == NULL)
244             (void) fprintf(stderr,
245                 _("No mailservers set up -- perhaps %s is missing?\n"), rcfile);
246         else
247             dump_params(&run, querylist, implicitmode);
248         exit(0);
249     }
250
251     /* dump options as a Python dictionary, for configurator use */
252     if (configdump)
253     {
254         dump_config(&run, querylist);
255         exit(0);
256     }
257
258     /* check for another fetchmail running concurrently */
259     pid = -1;
260     if ((lockfile = (char *) malloc(strlen(tmpbuf) + 1)) == NULL)
261     {
262         fprintf(stderr,_("fetchmail: cannot allocate memory for lock name.\n"));
263         exit(PS_EXCLUDE);
264     }
265     else
266         (void) strcpy(lockfile, tmpbuf);
267     if ((lockfp = fopen(lockfile, "r")) != NULL )
268     {
269         bkgd = (fscanf(lockfp,"%d %d", &pid, &st) == 2);
270
271         if (kill(pid, 0) == -1) {
272             fprintf(stderr,_("fetchmail: removing stale lockfile\n"));
273             pid = -1;
274             bkgd = FALSE;
275             unlink(lockfile);
276         }
277         fclose(lockfp);
278     }
279
280     /* if no mail servers listed and nothing in background, we're done */
281     if (!(quitmode && argc == 2) && pid == -1 && querylist == NULL) {
282         (void)fputs(_("fetchmail: no mailservers have been specified.\n"),stderr);
283         exit(PS_SYNTAX);
284     }
285
286     /* perhaps user asked us to kill the other fetchmail */
287     if (quitmode)
288     {
289         if (pid == -1) 
290         {
291             fprintf(stderr,_("fetchmail: no other fetchmail is running\n"));
292             if (argc == 2)
293                 exit(PS_EXCLUDE);
294         }
295         else if (kill(pid, SIGTERM) < 0)
296         {
297             fprintf(stderr,_("fetchmail: error killing %s fetchmail at %d; bailing out.\n"),
298                     bkgd ? _("background") : _("foreground"), pid);
299             exit(PS_EXCLUDE);
300         }
301         else
302         {
303             fprintf(stderr,_("fetchmail: %s fetchmail at %d killed.\n"),
304                     bkgd ? _("background") : _("foreground"), pid);
305             unlink(lockfile);
306             if (argc == 2)
307                 exit(0);
308             else
309                 pid = -1; 
310         }
311     }
312
313     /* another fetchmail is running -- wake it up or die */
314     if (pid != -1)
315     {
316         if (check_only)
317         {
318             fprintf(stderr,
319                  _("fetchmail: can't check mail while another fetchmail to same host is running.\n"));
320             return(PS_EXCLUDE);
321         }
322         else if (!implicitmode)
323         {
324             fprintf(stderr,
325                  _("fetchmail: can't poll specified hosts with another fetchmail running at %d.\n"),
326                  pid);
327                 return(PS_EXCLUDE);
328         }
329         else if (!bkgd)
330         {
331             fprintf(stderr,
332                  _("fetchmail: another foreground fetchmail is running at %d.\n"),
333                  pid);
334                 return(PS_EXCLUDE);
335         }
336         else if (argc > 1)
337         {
338             fprintf(stderr,
339                     _("fetchmail: can't accept options while a background fetchmail is running.\n"));
340             return(PS_EXCLUDE);
341         }
342         else if (kill(pid, SIGUSR1) == 0)
343         {
344             fprintf(stderr,
345                     _("fetchmail: background fetchmail at %d awakened.\n"),
346                     pid);
347             return(0);
348         }
349         else
350         {
351             /*
352              * Should never happen -- possible only if a background fetchmail
353              * croaks after the first kill probe above but before the
354              * SIGUSR1/SIGHUP transmission.
355              */
356             fprintf(stderr,
357                     _("fetchmail: elder sibling at %d died mysteriously.\n"),
358                     pid);
359             return(PS_UNDEFINED);
360         }
361     }
362
363     /* parse the ~/.netrc file (if present) for future password lookups. */
364     xalloca(netrc_file, char *, strlen (home) + 8);
365     strcpy (netrc_file, home);
366     strcat (netrc_file, "/.netrc");
367     netrc_list = parse_netrc(netrc_file);
368
369 #ifdef HAVE_SETRLIMIT
370     /*
371      * Before getting passwords, disable core dumps unless -v -d0 mode is on.
372      * Core dumps could otherwise contain passwords to be scavenged by a
373      * cracker.
374      */
375     if (outlevel < O_VERBOSE || run.poll_interval > 0)
376     {
377         struct rlimit corelimit;
378         corelimit.rlim_cur = 0;
379         corelimit.rlim_max = 0;
380         setrlimit(RLIMIT_CORE, &corelimit);
381     }
382 #endif /* HAVE_SETRLIMIT */
383
384     /* pick up interactively any passwords we need but don't have */ 
385     for (ctl = querylist; ctl; ctl = ctl->next)
386     {
387         if (ctl->active && !(implicitmode && ctl->server.skip)&&!ctl->password)
388         {
389             if (ctl->server.preauthenticate == A_KERBEROS_V4 ||
390                 ctl->server.preauthenticate == A_KERBEROS_V5 ||
391 #ifdef GSSAPI
392                 ctl->server.protocol == P_IMAP_GSS ||
393 #endif /* GSSAPI */
394                 ctl->server.protocol == P_IMAP_K4)
395                 /* Server won't care what the password is, but there
396                    must be some non-null string here.  */
397                 ctl->password = ctl->remotename;
398             else
399             {
400                 netrc_entry *p;
401
402                 /* look up the pollname and account in the .netrc file. */
403                 p = search_netrc(netrc_list, ctl->server.pollname);
404                 while (p && strcmp(p->account, ctl->remotename))
405                     p = search_netrc(p->next, ctl->remotename);
406                 /* if we find a matching entry with a password, use it */
407                 if (p && p->password)
408                     ctl->password = xstrdup(p->password);
409
410                 /* otherwise try with "via" name if there is one */
411                 else if (ctl->server.via)
412                 {
413                     p = search_netrc(netrc_list, ctl->server.via);
414                     while (p && strcmp(p->account, ctl->remotename))
415                         p = search_netrc(p->next, ctl->remotename);
416                     if (p && p->password)
417                         ctl->password = xstrdup(p->password);
418                 }
419             }
420
421             if (ctl->server.protocol != P_ETRN && ctl->server.protocol != P_IMAP_K4
422 #ifdef GSSAPI
423                 && ctl->server.protocol != P_IMAP_GSS
424 #endif /* GSSAPI */
425                 && !ctl->password)
426             {
427                 char* password_prompt = _("Enter password for %s@%s: ");
428
429                 xalloca(tmpbuf, char *, strlen(password_prompt) +
430                                         strlen(ctl->remotename) +
431                                         strlen(ctl->server.pollname) + 1);
432                 (void) sprintf(tmpbuf, password_prompt,
433                                ctl->remotename, ctl->server.pollname);
434                 ctl->password = xstrdup((char *)getpassword(tmpbuf));
435             }
436         }
437     }
438
439 /* Time to initiate the SOCKS library (this is not mandatory: it just
440  registers the correct application name for logging purpose. If you
441  have some problem, comment these lines). */
442 #ifdef HAVE_SOCKS
443   #if HAVE_SOCKS
444 /* Mmmh... I don't like hardcoded application names,
445  but "fetchmail" is everywhere... */
446     SOCKSinit("fetchmail");
447   #endif
448 #endif /* HAVE_SOCKS */
449
450     /*
451      * Maybe time to go to demon mode...
452      */
453 #if defined(HAVE_SYSLOG)
454     if (run.use_syslog)
455     {
456         openlog(program_name, LOG_PID, LOG_MAIL);
457         report_init(-1);
458     }
459     else
460 #endif
461         report_init((run.poll_interval == 0 || nodetach) && !run.logfile);
462
463     if (run.poll_interval)
464     {
465         if (!nodetach)
466             daemonize(run.logfile, termhook);
467         report(stdout, 0, _("starting fetchmail %s daemon \n"), VERSION);
468
469         /*
470          * We'll set up a handler for these when we're sleeping,
471          * but ignore them otherwise so as not to interrupt a poll.
472          */
473         signal(SIGUSR1, SIG_IGN);
474         if (run.poll_interval && !getuid())
475             signal(SIGHUP, SIG_IGN);
476     }
477
478 #ifdef linux
479     interface_init();
480 #endif /* linux */
481
482     /* beyond here we don't want more than one fetchmail running per user */
483     umask(0077);
484     signal(SIGABRT, termhook);
485     signal(SIGINT, termhook);
486     signal(SIGTERM, termhook);
487     signal(SIGALRM, termhook);
488     signal(SIGPIPE, termhook);
489     signal(SIGQUIT, termhook);
490
491     /* here's the exclusion lock */
492     if ((lockfp = fopen(lockfile,"w")) != NULL) {
493         fprintf(lockfp,"%d",getpid());
494         if (run.poll_interval)
495             fprintf(lockfp," %d", run.poll_interval);
496         fclose(lockfp);
497
498 #ifdef HAVE_ATEXIT
499         atexit(unlockit);
500 #endif
501 #ifdef HAVE_ON_EXIT
502         on_exit(unlockit, (char *)NULL);
503 #endif
504     }
505
506     /*
507      * Query all hosts. If there's only one, the error return will
508      * reflect the status of that transaction.
509      */
510     do {
511 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
512         /*
513          * This was an efficiency hack that backfired.  The theory
514          * was that using TCP/IP for DNS queries would get us better
515          * reliability and shave off some per-UDP-packet costs.
516          * Unfortunately it interacted badly with diald, which effectively 
517          * filters out DNS queries over TCP/IP for reasons having to do
518          * with some obscure kernel problem involving bootstrapping of
519          * dynamically-addressed links.  I don't understand this mess
520          * and don't want to, so it's "See ya!" to this hack.
521          */
522         sethostent(TRUE);       /* use TCP/IP for mailserver queries */
523 #endif /* HAVE_RES_SEARCH */
524
525         batchcount = 0;
526         for (ctl = querylist; ctl; ctl = ctl->next)
527         {
528             if (ctl->active && !(implicitmode && ctl->server.skip))
529             {
530                 if (ctl->wedged)
531                 {
532                     report(stderr, 0, 
533                           _("poll of %s skipped (failed authentication or too many timeouts)\n"),
534                           ctl->server.pollname);
535                     continue;
536                 }
537
538                 /* check skip interval first so that it counts all polls */
539                 if (run.poll_interval && ctl->server.interval) 
540                 {
541                     if (ctl->server.poll_count++ % ctl->server.interval) 
542                     {
543                         if (outlevel >= O_VERBOSE)
544                             report(stdout, 0,
545                                     _("interval not reached, not querying %s\n"),
546                                     ctl->server.pollname);
547                         continue;
548                     }
549                 }
550
551 #if defined(linux) && !INET6
552                 /* interface_approve() does its own error logging */
553                 if (!interface_approve(&ctl->server))
554                     continue;
555 #endif /* defined(linux) && !INET6 */
556
557                 querystatus = query_host(ctl);
558
559                 if (querystatus == PS_SUCCESS)
560                 {
561                     successes++;
562 #ifdef POP3_ENABLE
563                     if (!check_only)
564                         update_str_lists(ctl);
565
566                    /* Save UID list to prevent re-fetch in case fetchmail 
567                       recover from crash */
568                     if (!check_only)
569                     {
570                         write_saved_lists(querylist, run.idfile);
571                         if (outlevel >= O_DEBUG)
572                             report(stdout, 0, _("saved UID List\n"));
573                     }
574 #endif  /* POP3_ENABLE */
575                 }
576                 else if (!check_only && 
577                          ((querystatus!=PS_NOMAIL) || (outlevel==O_DEBUG)))
578                     report(stdout, 0, _("Query status=%d\n"), querystatus);
579
580 #if defined(linux) && !INET6
581                 if (ctl->server.monitor)
582                 {
583                     /*
584                      * Allow some time for the link to quiesce.  One
585                      * second is usually sufficient, three is safe.
586                      * Note:  this delay is important - don't remove!
587                      */
588                     sleep(3);
589                     interface_note_activity(&ctl->server);
590                 }
591 #endif /* defined(linux) && !INET6 */
592             }
593         }
594
595 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
596         endhostent();           /* release TCP/IP connection to nameserver */
597 #endif /* HAVE_RES_SEARCH */
598
599         /*
600          * Close all SMTP delivery sockets.  For optimum performance
601          * we'd like to hold them open til end of run, but (1) this
602          * loses if our poll interval is longer than the MTA's inactivity
603          * timeout, and (2) some MTAs (like smail) don't deliver after
604          * each message, but rather queue up mail and wait to actually
605          * deliver it until the input socket is closed. 
606          */
607         for (ctl = querylist; ctl; ctl = ctl->next)
608             if (ctl->smtp_socket != -1)
609             {
610                 SMTP_quit(ctl->smtp_socket);
611                 close(ctl->smtp_socket);
612                 ctl->smtp_socket = -1;
613             }
614
615         /*
616          * OK, we've polled.  Now sleep.
617          */
618         if (run.poll_interval)
619         {
620             /* 
621              * Because passwords can expire, it may happen that *all*
622              * hosts are now out of the loop due to authfail
623              * conditions.  If this happens daemon-mode fetchmail
624              * should softly and silently vanish away, rather than
625              * spinning uselessly.
626              */
627             int unwedged = 0;
628
629             for (ctl = querylist; ctl; ctl = ctl->next)
630                 if (ctl->active && !(implicitmode && ctl->server.skip))
631                     if (!ctl->wedged)
632                         unwedged++;
633             if (!unwedged)
634             {
635                 report(stderr, 0, _("All connections are wedged.  Exiting.\n"));
636                 exit(PS_AUTHFAIL);
637             }
638
639             if (outlevel >= O_VERBOSE)
640                 report(stdout, 0, 
641                        _("fetchmail: sleeping at %s\n"), rfc822timestamp());
642
643             /*
644              * With this simple hack, we make it possible for a foreground 
645              * fetchmail to wake up one in daemon mode.  What we want is the
646              * side effect of interrupting any sleep that may be going on,
647              * forcing fetchmail to re-poll its hosts.  The second line is
648              * for people who think all system daemons wake up on SIGHUP.
649              */
650             signal(SIGUSR1, donothing);
651             if (!getuid())
652                 signal(SIGHUP, donothing);
653
654             /* time for a pause in the action... */
655             {
656 #ifndef __EMX__
657 #ifdef SLEEP_WITH_ALARM         /* not normally on */
658                 /*
659                  * We can't use sleep(3) here because we need an alarm(3)
660                  * equivalent in order to implement server nonresponse timeout.
661                  * We'll just assume setitimer(2) is available since fetchmail
662                  * has to have a BSDoid socket layer to work at all.
663                  */
664                 /* 
665                  * This code stopped working under glibc-2, apparently due
666                  * to the change in signal(2) semantics.  (The siginterrupt
667                  * line, added later, should fix this problem.) John Stracke
668                  * <francis@netscape.com> wrote:
669                  *
670                  * The problem seems to be that, after hitting the interval
671                  * timer while talking to the server, the process no longer
672                  * responds to SIGALRM.  I put in printf()s to see when it
673                  * reached the pause() for the poll interval, and I checked
674                  * the return from setitimer(), and everything seemed to be
675                  * working fine, except that the pause() just ignored SIGALRM.
676                  * I thought maybe the itimer wasn't being fired, so I hit
677                  * it with a SIGALRM from the command line, and it ignored
678                  * that, too.  SIGUSR1 woke it up just fine, and it proceeded
679                  * to repoll--but, when the dummy server didn't respond, it
680                  * never timed out, and SIGALRM wouldn't make it.
681                  *
682                  * (continued below...)
683                  */
684                 struct itimerval ntimeout;
685
686                 ntimeout.it_interval.tv_sec = 5; /* repeat alarm every 5 secs */
687                 ntimeout.it_interval.tv_usec = 0;
688                 ntimeout.it_value.tv_sec  = run.poll_interval;
689                 ntimeout.it_value.tv_usec = 0;
690
691                 siginterrupt(SIGALRM, 1);
692                 alarm_latch = FALSE;
693                 signal(SIGALRM, gotsigalrm);    /* first trap signals */
694                 setitimer(ITIMER_REAL,&ntimeout,NULL);  /* then start timer */
695                 /* there is a very small window between the next two lines */
696                 /* which could result in a deadlock.  But this will now be  */
697                 /* caught by periodical alarms (see it_interval) */
698                 if (!alarm_latch)
699                     pause();
700                 /* stop timer */
701                 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
702                 ntimeout.it_value.tv_sec  = ntimeout.it_value.tv_usec = 0;
703                 setitimer(ITIMER_REAL,&ntimeout,NULL);  /* now stop timer */
704                 signal(SIGALRM, SIG_IGN);
705 #else
706                 /* 
707                  * So the workaround I used is to make it sleep by using
708                  * select() instead of setitimer()/pause().  select() is
709                  * perfectly happy being called with a timeout and
710                  * no file descriptors; it just sleeps until it hits the
711                  * timeout.  The only concern I had was that it might
712                  * implement its timeout with SIGALRM--there are some
713                  * Unices where this is done, because select() is a library
714                  * function--but apparently not.
715                  */
716                 struct timeval timeout;
717
718                 timeout.tv_sec = run.poll_interval;
719                 timeout.tv_usec = 0;
720                 lastsig = 0;
721                 select(0,0,0,0, &timeout);
722 #endif
723 #else /* EMX */
724                 alarm_latch = FALSE;
725                 signal(SIGALRM, gotsigalrm);
726                 _beginthread(itimerthread, NULL, 32768, NULL);
727                 /* see similar code above */
728                 if (!alarm_latch)
729                     pause();
730                 signal(SIGALRM, SIG_IGN);
731 #endif /* ! EMX */
732                 if (lastsig == SIGUSR1
733                         || ((run.poll_interval && !getuid()) && lastsig == SIGHUP))
734                 {
735 #ifdef SYS_SIGLIST_DECLARED
736                     report(stdout, 0, 
737                            _("awakened by %s\n"), sys_siglist[lastsig]);
738 #else
739                     report(stdout, 0, 
740                            _("awakened by signal %d\n"), lastsig);
741 #endif
742                     /* received a wakeup - unwedge all servers in case */
743                     /* the problem has been manually repaired          */
744                     for (ctl = querylist; ctl; ctl = ctl->next)
745                         ctl->wedged = FALSE;
746                 }
747             }
748
749             /* now lock out interrupts again */
750             signal(SIGUSR1, SIG_IGN);
751             if (!getuid())
752                 signal(SIGHUP, SIG_IGN);
753
754             if (outlevel >= O_VERBOSE)
755                 report(stdout, 0, _("awakened at %s\n"), rfc822timestamp());
756         }
757     } while
758         (run.poll_interval);
759
760     if (outlevel >= O_VERBOSE)
761         report(stdout, 0, _("normal termination, status %d\n"),
762                 successes ? PS_SUCCESS : querystatus);
763
764     termhook(0);
765     exit(successes ? PS_SUCCESS : querystatus);
766 }
767
768 static void optmerge(struct query *h2, struct query *h1, int force)
769 /* merge two options records */
770 {
771     /*
772      * If force is off, modify h2 fields only when they're empty (treat h1
773      * as defaults).  If force is on, modify each h2 field whenever h1
774      * is nonempty (treat h1 as an override).  
775      */
776 #define LIST_MERGE(dstl, srcl) if (force ? !!srcl : !dstl) \
777                                                 free_str_list(&dstl), \
778                                                 append_str_list(&dstl, &srcl)
779     LIST_MERGE(h2->server.localdomains, h1->server.localdomains);
780     LIST_MERGE(h2->localnames, h1->localnames);
781     LIST_MERGE(h2->mailboxes, h1->mailboxes);
782     LIST_MERGE(h2->smtphunt, h1->smtphunt);
783     LIST_MERGE(h2->antispam, h1->antispam);
784 #undef LIST_MERGE
785
786 #define FLAG_MERGE(fld) if (force ? !!h1->fld : !h2->fld) h2->fld = h1->fld
787     FLAG_MERGE(server.via);
788     FLAG_MERGE(server.protocol);
789 #if INET6
790     FLAG_MERGE(server.service);
791     FLAG_MERGE(server.netsec);
792 #else /* INET6 */
793     FLAG_MERGE(server.port);
794 #endif /* INET6 */
795     FLAG_MERGE(server.interval);
796     FLAG_MERGE(server.preauthenticate);
797     FLAG_MERGE(server.timeout);
798     FLAG_MERGE(server.envelope);
799     FLAG_MERGE(server.envskip);
800     FLAG_MERGE(server.qvirtual);
801     FLAG_MERGE(server.skip);
802     FLAG_MERGE(server.dns);
803     FLAG_MERGE(server.checkalias);
804     FLAG_MERGE(server.uidl);
805
806 #ifdef linux
807     FLAG_MERGE(server.interface);
808     FLAG_MERGE(server.monitor);
809     FLAG_MERGE(server.interface_pair);
810 #endif /* linux */
811
812     FLAG_MERGE(server.plugin);
813     FLAG_MERGE(server.plugout);
814     FLAG_MERGE(wildcard);
815     FLAG_MERGE(remotename);
816     FLAG_MERGE(password);
817     FLAG_MERGE(mda);
818     FLAG_MERGE(bsmtp);
819     FLAG_MERGE(listener);
820     FLAG_MERGE(smtpaddress);
821     FLAG_MERGE(preconnect);
822     FLAG_MERGE(postconnect);
823
824     FLAG_MERGE(keep);
825     FLAG_MERGE(flush);
826     FLAG_MERGE(fetchall);
827     FLAG_MERGE(rewrite);
828     FLAG_MERGE(forcecr);
829     FLAG_MERGE(stripcr);
830     FLAG_MERGE(pass8bits);
831     FLAG_MERGE(dropstatus);
832     FLAG_MERGE(mimedecode);
833     FLAG_MERGE(limit);
834     FLAG_MERGE(warnings);
835     FLAG_MERGE(fetchlimit);
836     FLAG_MERGE(batchlimit);
837     FLAG_MERGE(expunge);
838
839     FLAG_MERGE(properties);
840 #undef FLAG_MERGE
841 }
842
843 static int load_params(int argc, char **argv, int optind)
844 {
845     int implicitmode, st;
846     struct passwd *pw;
847     struct query def_opts, *ctl;
848
849     memset(&def_opts, '\0', sizeof(struct query));
850     def_opts.smtp_socket = -1;
851     def_opts.smtpaddress = (char *)0;
852     save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 571;
853     save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 550;
854     save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 501;
855
856     def_opts.server.protocol = P_AUTO;
857     def_opts.server.timeout = CLIENT_TIMEOUT;
858     def_opts.warnings = WARNING_INTERVAL;
859     def_opts.remotename = user;
860     def_opts.expunge = 1;
861     def_opts.listener = SMTP_MODE;
862
863     /* this builds the host list */
864     if (prc_parse_file(rcfile, !versioninfo) != 0)
865         exit(PS_SYNTAX);
866
867     if ((implicitmode = (optind >= argc)))
868     {
869         for (ctl = querylist; ctl; ctl = ctl->next)
870             ctl->active = TRUE;
871     }
872     else
873         for (; optind < argc; optind++) 
874         {
875             flag        predeclared =  FALSE;
876
877             /*
878              * If hostname corresponds to a host known from the rc file,
879              * simply declare it active.  Otherwise synthesize a host
880              * record from command line and defaults
881              */
882             for (ctl = querylist; ctl; ctl = ctl->next)
883                 if (!strcmp(ctl->server.pollname, argv[optind])
884                         || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
885                 {
886                     /* Is this correct? */
887                     if(predeclared)
888                         fprintf(stderr,_("Warning: multiple mentions of host %s in config file\n"),argv[optind]);
889                     ctl->active = TRUE;
890                     predeclared = TRUE;
891                 }
892
893             if (!predeclared)
894             {
895                 /*
896                  * Allocate and link record without copying in
897                  * command-line args; we'll do that with the optmerge
898                  * call later on.
899                  */
900                 ctl = hostalloc((struct query *)NULL);
901                 ctl->server.via =
902                     ctl->server.pollname = xstrdup(argv[optind]);
903                 ctl->active = TRUE;
904                 ctl->server.lead_server = (struct hostdata *)NULL;
905             }
906         }
907
908     /*
909      * If there's a defaults record, merge it and lose it.
910      */ 
911     if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
912     {
913         for (ctl = querylist->next; ctl; ctl = ctl->next)
914             optmerge(ctl, querylist, FALSE);
915         querylist = querylist->next;
916     }
917
918     /* don't allow a defaults record after the first */
919     for (ctl = querylist; ctl; ctl = ctl->next)
920         if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
921             exit(PS_SYNTAX);
922
923     /* use localhost if we never fetch the FQDN of this host */
924     fetchmailhost = "localhost";
925
926     /* merge in wired defaults, do sanity checks and prepare internal fields */
927     for (ctl = querylist; ctl; ctl = ctl->next)
928     {
929         ctl->wedged = FALSE;
930
931         if (configdump || (ctl->active && !(implicitmode && ctl->server.skip)))
932         {
933             /* merge in defaults */
934             optmerge(ctl, &def_opts, FALSE);
935
936             /* force command-line options */
937             optmerge(ctl, &cmd_opts, TRUE);
938
939             /* this code enables flags to be turned off */
940 #define DEFAULT(flag, dflt)     if (flag == FLAG_TRUE)\
941                                         flag = TRUE;\
942                                 else if (flag == FLAG_FALSE)\
943                                         flag = FALSE;\
944                                 else\
945                                         flag = (dflt)
946             DEFAULT(ctl->keep, FALSE);
947             DEFAULT(ctl->fetchall, FALSE);
948             DEFAULT(ctl->flush, FALSE);
949             DEFAULT(ctl->rewrite, TRUE);
950             DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL)); 
951             DEFAULT(ctl->forcecr, FALSE);
952             DEFAULT(ctl->pass8bits, FALSE);
953             DEFAULT(ctl->dropstatus, FALSE);
954             DEFAULT(ctl->mimedecode, TRUE);
955             DEFAULT(ctl->server.dns, TRUE);
956             DEFAULT(ctl->server.uidl, FALSE);
957             DEFAULT(ctl->server.checkalias, FALSE);
958 #undef DEFAULT
959
960             /*
961              * DNS support is required for some protocols.
962              *
963              * If we're using ETRN, the smtp hunt list is the list of
964              * systems we're polling on behalf of; these have to be 
965              * fully-qualified domain names.  The default for this list
966              * should be the FQDN of localhost.
967              *
968              * If we're using Kerberos for authentication, we need 
969              * the FQDN in order to generate capability keys.
970              */
971             if (ctl->server.protocol == P_ETRN
972                          || ctl->server.preauthenticate == A_KERBEROS_V4
973                          || ctl->server.preauthenticate == A_KERBEROS_V5)
974                 if (strcmp(fetchmailhost, "localhost") == 0)
975                         fetchmailhost = host_fqdn();
976
977             /*
978              * Make sure we have a nonempty host list to forward to.
979              */
980             if (!ctl->smtphunt)
981                 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
982
983             /* if `user' doesn't name a real local user, try to run as root */
984             if ((pw = getpwnam(user)) == (struct passwd *)NULL)
985                 ctl->uid = 0;
986             else
987                 ctl->uid = pw->pw_uid;  /* for local delivery via MDA */
988             if (!ctl->localnames)       /* for local delivery via SMTP */
989                 save_str_pair(&ctl->localnames, user, NULL);
990
991 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
992             /* can't handle multidrop mailboxes unless we can do DNS lookups */
993             if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
994             {
995                 ctl->server.dns = FALSE;
996                 fprintf(stderr, _("fetchmail: warning: no DNS available to check multidrop fetches from %s\n"), ctl->server.pollname);
997             }
998 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
999
1000             /*
1001              *
1002              * Compute the true name of the mailserver host.  
1003              * There are two clashing cases here:
1004              *
1005              * (1) The poll name is a label, possibly on one of several
1006              *     poll configurations for the same host.  In this case 
1007              *     the `via' option will be present and give the true name.
1008              *
1009              * (2) The poll name is the true one, the via name is 
1010              *     localhost.   This is going to be typical for ssh-using
1011              *     configurations.
1012              *
1013              * We're going to assume the via name is true unless it's
1014              * localhost.
1015              */
1016             if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
1017                 ctl->server.queryname = xstrdup(ctl->server.via);
1018             else
1019                 ctl->server.queryname = xstrdup(ctl->server.pollname);
1020
1021 #ifdef HESIOD
1022         /* If either the pollname or vianame are "hesiod" we want to
1023            lookup the user's hesiod pobox host */
1024
1025         if (!strcasecmp(ctl->server.queryname, "hesiod")) {
1026             struct hes_postoffice *hes_p;
1027             hes_p = hes_getmailhost(ctl->remotename);
1028             if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
1029                  free(ctl->server.queryname);
1030                  ctl->server.queryname = xstrdup(hes_p->po_host);
1031                  if (ctl->server.via)
1032                      free(ctl->server.via);
1033                  ctl->server.via = xstrdup(hes_p->po_host);
1034             } else {
1035                  report(stderr, errno, 
1036                         _("couldn't find HESIOD pobox for %s\n"),
1037                         ctl->remotename);
1038             }
1039         }
1040 #endif /* HESIOD */
1041
1042             /*
1043              * We may have to canonicalize the server truename for later use.
1044              * Do this just once for each lead server, if necessary, in order
1045              * to minimize DNS round trips.
1046              */
1047             if (ctl->server.lead_server)
1048                 ctl->server.truename = xstrdup(ctl->server.lead_server->truename);
1049 #ifdef HAVE_GETHOSTBYNAME
1050             else if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
1051                 ctl->server.preauthenticate==A_KERBEROS_V5 ||
1052                 (ctl->server.dns && MULTIDROP(ctl)))
1053             {
1054                 struct hostent  *namerec;
1055
1056                 /* compute the canonical name of the host */
1057                 errno = 0;
1058                 namerec = gethostbyname(ctl->server.queryname);
1059                 if (namerec == (struct hostent *)NULL)
1060                 {
1061                     report(stderr, errno,
1062                           _("couldn't find canonical DNS name of %s\n"),
1063                           ctl->server.pollname);
1064                     exit(PS_DNS);
1065                 }
1066                 else
1067                     ctl->server.truename=xstrdup((char *)namerec->h_name);
1068             }
1069 #endif /* HAVE_GETHOSTBYNAME */
1070             else
1071                 ctl->server.truename = xstrdup(ctl->server.queryname);
1072
1073             /* if no folders were specified, set up the null one as default */
1074             if (!ctl->mailboxes)
1075                 save_str(&ctl->mailboxes, (char *)NULL, 0);
1076
1077             /* maybe user overrode timeout on command line? */
1078             if (ctl->server.timeout == -1)      
1079                 ctl->server.timeout = CLIENT_TIMEOUT;
1080
1081 #if !INET6
1082             /* sanity checks */
1083             if (ctl->server.port < 0)
1084             {
1085                 (void) fprintf(stderr,
1086                                _("%s configuration invalid, port number cannot be negative\n"),
1087                                ctl->server.pollname);
1088                 exit(PS_SYNTAX);
1089             }
1090             if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
1091             {
1092                 (void) fprintf(stderr,
1093                                _("%s configuration invalid, RPOP requires a privileged port\n"),
1094                                ctl->server.pollname);
1095                 exit(PS_SYNTAX);
1096             }
1097             if (ctl->listener == LMTP_MODE)
1098             {
1099                 struct idlist   *idp;
1100
1101                 for (idp = ctl->smtphunt; idp; idp = idp->next)
1102                 {
1103                     char        *cp;
1104
1105                     if (!(cp = strrchr(idp->id, '/')) ||
1106 #ifdef INET6 
1107                                 (strcmp(++cp, SMTP_PORT) == 0))
1108 #else
1109                                 (atoi(++cp) == SMTP_PORT))
1110 #endif /* INET6 */
1111                     {
1112                         (void) fprintf(stderr,
1113                                        _("%s configuration invalid, LMTP can't use default SMTP port\n"),
1114                                        ctl->server.pollname);
1115                         exit(PS_SYNTAX);
1116                     }
1117                 }
1118             }
1119 #endif /* !INET6 */
1120         }
1121     }
1122
1123     /* here's where we override globals */
1124     if (cmd_run.logfile)
1125         run.logfile = cmd_run.logfile;
1126     if (cmd_run.idfile)
1127         run.idfile = cmd_run.idfile;
1128     if (cmd_run.poll_interval >= 0)
1129         run.poll_interval = cmd_run.poll_interval;
1130     if (cmd_run.invisible)
1131         run.invisible = cmd_run.invisible;
1132     if (cmd_run.use_syslog)
1133         run.use_syslog = (cmd_run.use_syslog == FLAG_TRUE);
1134     if (cmd_run.postmaster)
1135         run.postmaster = cmd_run.postmaster;
1136
1137     /* check and daemon options are not compatible */
1138     if (check_only && run.poll_interval)
1139         run.poll_interval = 0;
1140
1141 #ifdef POP3_ENABLE
1142     /* initialize UID handling */
1143     if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
1144         exit(st);
1145     else
1146         initialize_saved_lists(querylist, run.idfile);
1147 #endif /* POP3_ENABLE */
1148
1149     /*
1150      * If the user didn't set a last-resort user to get misaddressed
1151      * multidrop mail, set an appropriate default here.
1152      */
1153     if (!run.postmaster)
1154         if (getuid())                           /* ordinary user */
1155             run.postmaster = user;
1156         else                                    /* root */
1157             run.postmaster = "postmaster";
1158
1159     return(implicitmode);
1160 }
1161
1162 static void termhook(int sig)
1163 /* to be executed on normal or signal-induced termination */
1164 {
1165     struct query        *ctl;
1166
1167     /*
1168      * Sending SMTP QUIT on signal is theoretically nice, but led to a 
1169      * subtle bug.  If fetchmail was terminated by signal while it was 
1170      * shipping message text, it would hang forever waiting for a
1171      * command acknowledge.  In theory we could enable the QUIT
1172      * only outside of the message send.  In practice, we don't
1173      * care.  All mailservers hang up on a dropped TCP/IP connection
1174      * anyway.
1175      */
1176
1177     if (sig != 0)
1178         report(stdout, 0, _("terminated with signal %d\n"), sig);
1179     else
1180         /* terminate all SMTP connections cleanly */
1181         for (ctl = querylist; ctl; ctl = ctl->next)
1182             if (ctl->smtp_socket != -1)
1183                 SMTP_quit(ctl->smtp_socket);
1184
1185 #ifdef POP3_ENABLE
1186     if (!check_only)
1187         write_saved_lists(querylist, run.idfile);
1188 #endif /* POP3_ENABLE */
1189
1190     /* 
1191      * Craig Metz, the RFC1938 one-time-password guy, points out:
1192      * "Remember that most kernels don't zero pages before handing them to the
1193      * next process and many kernels share pages between user and kernel space.
1194      * You'd be very surprised what you can find from a short program to do a
1195      * malloc() and then dump the contents of the pages you got. By zeroing
1196      * the secrets at end of run (earlier if you can), you make sure the next
1197      * guy can't get the password/pass phrase."
1198      *
1199      * Right you are, Craig!
1200      */
1201     for (ctl = querylist; ctl; ctl = ctl->next)
1202         if (ctl->password)
1203           memset(ctl->password, '\0', strlen(ctl->password));
1204
1205 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
1206     unlockit();
1207 #endif
1208
1209     exit(successes ? PS_SUCCESS : querystatus);
1210 }
1211
1212 /*
1213  * Sequence of protocols to try when autoprobing, most capable to least.
1214  */
1215 static const int autoprobe[] = 
1216 {
1217 #ifdef IMAP_ENABLE
1218     P_IMAP,
1219 #endif /* IMAP_ENABLE */
1220 #ifdef POP3_ENABLE
1221     P_POP3,
1222 #endif /* POP3_ENABLE */
1223 #ifdef POP2_ENABLE
1224     P_POP2
1225 #endif /* POP2_ENABLE */
1226 };
1227
1228 static int query_host(struct query *ctl)
1229 /* perform fetch transaction with single host */
1230 {
1231     int i, st;
1232
1233     if (outlevel >= O_VERBOSE)
1234     {
1235         time_t now;
1236
1237         time(&now);
1238         report(stdout, 0, _("%s querying %s (protocol %s) at %s\n"),
1239             VERSION,
1240             ctl->server.pollname, showproto(ctl->server.protocol), ctime(&now));
1241     }
1242     switch (ctl->server.protocol) {
1243     case P_AUTO:
1244         for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
1245         {
1246             ctl->server.protocol = autoprobe[i];
1247             if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
1248                 break;
1249         }
1250         ctl->server.protocol = P_AUTO;
1251         return(st);
1252         break;
1253     case P_POP2:
1254 #ifdef POP2_ENABLE
1255         return(doPOP2(ctl));
1256 #else
1257         report(stderr, 0, _("POP2 support is not configured.\n"));
1258         return(PS_PROTOCOL);
1259 #endif /* POP2_ENABLE */
1260         break;
1261     case P_POP3:
1262     case P_APOP:
1263     case P_RPOP:
1264 #ifdef POP3_ENABLE
1265         return(doPOP3(ctl));
1266 #else
1267         report(stderr, 0, _("POP3 support is not configured.\n"));
1268         return(PS_PROTOCOL);
1269 #endif /* POP3_ENABLE */
1270         break;
1271     case P_IMAP:
1272     case P_IMAP_K4:
1273 #ifdef GSSAPI
1274     case P_IMAP_GSS:
1275 #endif /* GSSAPI */
1276 #ifdef IMAP_ENABLE
1277         return(doIMAP(ctl));
1278 #else
1279         report(stderr, 0, _("IMAP support is not configured.\n"));
1280         return(PS_PROTOCOL);
1281 #endif /* IMAP_ENABLE */
1282         break;
1283     case P_ETRN:
1284 #ifndef ETRN_ENABLE
1285         report(stderr, 0, _("ETRN support is not configured.\n"));
1286         return(PS_PROTOCOL);
1287 #else
1288 #ifdef HAVE_GETHOSTBYNAME
1289         return(doETRN(ctl));
1290 #else
1291         report(stderr, 0, _("Cannot support ETRN without gethostbyname(2).\n"));
1292         return(PS_PROTOCOL);
1293 #endif /* HAVE_GETHOSTBYNAME */
1294 #endif /* ETRN_ENABLE */
1295     default:
1296         report(stderr, 0, _("unsupported protocol selected.\n"));
1297         return(PS_PROTOCOL);
1298     }
1299 }
1300
1301 static void dump_params (struct runctl *runp,
1302                          struct query *querylist, flag implicit)
1303 /* display query parameters in English */
1304 {
1305     struct query *ctl;
1306
1307     if (runp->poll_interval)
1308         printf(_("Poll interval is %d seconds\n"), runp->poll_interval);
1309     if (runp->logfile)
1310         printf(_("Logfile is %s\n"), runp->logfile);
1311     if (strcmp(runp->idfile, IDFILE_NAME))
1312         printf(_("Idfile is %s\n"), runp->idfile);
1313 #if defined(HAVE_SYSLOG)
1314     if (runp->use_syslog)
1315         printf(_("Progress messages will be logged via syslog\n"));
1316 #endif
1317     if (runp->invisible)
1318         printf(_("Fetchmail will masquerade and will not generate Received\n"));
1319     if (runp->postmaster)
1320         printf(_("Fetchmail will forward misaddressed multidrop messages to %s.\n"),
1321                runp->postmaster);
1322
1323     for (ctl = querylist; ctl; ctl = ctl->next)
1324     {
1325         if (!ctl->active || (implicit && ctl->server.skip))
1326             continue;
1327
1328         printf(_("Options for retrieving from %s@%s:\n"),
1329                ctl->remotename, visbuf(ctl->server.pollname));
1330
1331         if (ctl->server.via && (ctl->server.protocol != P_ETRN))
1332             printf(_("  Mail will be retrieved via %s\n"), ctl->server.via);
1333
1334         if (ctl->server.interval)
1335             printf(_("  Poll of this server will occur every %d intervals.\n"),
1336                    ctl->server.interval);
1337         if (ctl->server.truename)
1338             printf(_("  True name of server is %s.\n"), ctl->server.truename);
1339         if (ctl->server.skip || outlevel >= O_VERBOSE)
1340             printf(_("  This host %s be queried when no host is specified.\n"),
1341                    ctl->server.skip ? _("will not") : _("will"));
1342         /*
1343          * Don't poll for password when there is one or when using the ETRN
1344          * or IMAP-GSS protocol
1345          */
1346         if (!ctl->password && (ctl->server.protocol != P_ETRN)
1347 #ifdef GSSAPI
1348             && (ctl->server.protocol != P_IMAP_GSS)
1349 #endif /* GSSAPI */
1350         )
1351             printf(_("  Password will be prompted for.\n"));
1352         else if (outlevel >= O_VERBOSE)
1353             if (ctl->server.protocol == P_APOP)
1354                 printf(_("  APOP secret = \"%s\".\n"), visbuf(ctl->password));
1355             else if (ctl->server.protocol == P_RPOP)
1356                 printf(_("  RPOP id = \"%s\".\n"), visbuf(ctl->password));
1357             else
1358                 printf(_("  Password = \"%s\".\n"), visbuf(ctl->password));
1359         if (ctl->server.protocol == P_POP3 
1360 #if INET6
1361             && !strcmp(ctl->server.service, KPOP_PORT)
1362 #else /* INET6 */
1363             && ctl->server.port == KPOP_PORT
1364 #endif /* INET6 */
1365             && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1366                 ctl->server.preauthenticate == A_KERBEROS_V5))
1367             printf(_("  Protocol is KPOP with Kerberos %s authentication"),
1368                    ctl->server.preauthenticate == A_KERBEROS_V5 ? "V" : "IV");
1369         else
1370             printf(_("  Protocol is %s"), showproto(ctl->server.protocol));
1371 #if INET6
1372         if (ctl->server.service)
1373             printf(_(" (using service %s)"), ctl->server.service);
1374         if (ctl->server.netsec)
1375             printf(_(" (using network security options %s)"), ctl->server.netsec);
1376 #else /* INET6 */
1377         if (ctl->server.port)
1378             printf(_(" (using port %d)"), ctl->server.port);
1379 #endif /* INET6 */
1380         else if (outlevel >= O_VERBOSE)
1381             printf(_(" (using default port)"));
1382         if (ctl->server.uidl && (ctl->server.protocol != P_ETRN))
1383             printf(_(" (forcing UIDL use)"));
1384         putchar('.');
1385         putchar('\n');
1386         if (ctl->server.preauthenticate == A_KERBEROS_V4)
1387             printf(_("  Kerberos V4 preauthentication enabled.\n"));
1388         if (ctl->server.preauthenticate == A_KERBEROS_V5)
1389             printf(_("  Kerberos V5 preauthentication enabled.\n"));
1390         if (ctl->server.timeout > 0)
1391             printf(_("  Server nonresponse timeout is %d seconds"), ctl->server.timeout);
1392         if (ctl->server.timeout ==  CLIENT_TIMEOUT)
1393             printf(_(" (default).\n"));
1394         else
1395             printf(".\n");
1396
1397         if (ctl->server.protocol != P_ETRN) {
1398                 if (!ctl->mailboxes->id)
1399                     printf(_("  Default mailbox selected.\n"));
1400                 else
1401                 {
1402                     struct idlist *idp;
1403
1404                     printf(_("  Selected mailboxes are:"));
1405                     for (idp = ctl->mailboxes; idp; idp = idp->next)
1406                         printf(" %s", idp->id);
1407                     printf("\n");
1408                 }
1409                 printf(_("  %s messages will be retrieved (--all %s).\n"),
1410                        ctl->fetchall ? _("All") : _("Only new"),
1411                        ctl->fetchall ? "on" : "off");
1412                 printf(_("  Fetched messages %s be kept on the server (--keep %s).\n"),
1413                        ctl->keep ? _("will") : _("will not"),
1414                        ctl->keep ? "on" : "off");
1415                 printf(_("  Old messages %s be flushed before message retrieval (--flush %s).\n"),
1416                        ctl->flush ? _("will") : _("will not"),
1417                        ctl->flush ? "on" : "off");
1418                 printf(_("  Rewrite of server-local addresses is %s (--norewrite %s).\n"),
1419                        ctl->rewrite ? _("enabled") : _("disabled"),
1420                        ctl->rewrite ? "off" : "on");
1421                 printf(_("  Carriage-return stripping is %s (stripcr %s).\n"),
1422                        ctl->stripcr ? _("enabled") : _("disabled"),
1423                        ctl->stripcr ? "on" : "off");
1424                 printf(_("  Carriage-return forcing is %s (forcecr %s).\n"),
1425                        ctl->forcecr ? _("enabled") : _("disabled"),
1426                        ctl->forcecr ? "on" : "off");
1427                 printf(_("  Interpretation of Content-Transfer-Encoding is %s (pass8bits %s).\n"),
1428                        ctl->pass8bits ? _("disabled") : _("enabled"),
1429                        ctl->pass8bits ? "on" : "off");
1430                 printf(_("  MIME decoding is %s (mimedecode %s).\n"),
1431                        ctl->mimedecode ? _("enabled") : _("disabled"),
1432                        ctl->mimedecode ? "on" : "off");
1433                 printf(_("  Nonempty Status lines will be %s (dropstatus %s)\n"),
1434                        ctl->dropstatus ? _("discarded") : _("kept"),
1435                        ctl->dropstatus ? "on" : "off");
1436                 if (NUM_NONZERO(ctl->limit))
1437                 {
1438                     if (NUM_NONZERO(ctl->limit))
1439                         printf(_("  Message size limit is %d octets (--limit %d).\n"), 
1440                                ctl->limit, ctl->limit);
1441                     else if (outlevel >= O_VERBOSE)
1442                         printf(_("  No message size limit (--limit 0).\n"));
1443                     if (run.poll_interval > 0)
1444                         printf(_("  Message size warning interval is %d seconds (--warnings %d).\n"), 
1445                                ctl->warnings, ctl->warnings);
1446                     else if (outlevel >= O_VERBOSE)
1447                         printf(_("  Size warnings on every poll (--warnings 0).\n"));
1448                 }
1449                 if (NUM_NONZERO(ctl->fetchlimit))
1450                     printf(_("  Received-message limit is %d (--fetchlimit %d).\n"),
1451                            ctl->fetchlimit, ctl->fetchlimit);
1452                 else if (outlevel >= O_VERBOSE)
1453                     printf(_("  No received-message limit (--fetchlimit 0).\n"));
1454                 if (NUM_NONZERO(ctl->batchlimit))
1455                     printf(_("  SMTP message batch limit is %d.\n"), ctl->batchlimit);
1456                 else if (outlevel >= O_VERBOSE)
1457                     printf(_("  No SMTP message batch limit (--batchlimit 0).\n"));
1458                 if (ctl->server.protocol == P_IMAP)
1459                     if (NUM_NONZERO(ctl->expunge))
1460                         printf(_("  Deletion interval between expunges is %d (--expunge %d).\n"), ctl->expunge, ctl->expunge);
1461                     else if (outlevel >= O_VERBOSE)
1462                         printf(_("  No expunges (--expunge 0).\n"));
1463         }
1464         if (ctl->bsmtp)
1465             printf(_("  Messages will be appended to %s as BSMTP\n"), visbuf(ctl->bsmtp));
1466         else if (ctl->mda && (ctl->server.protocol != P_ETRN))
1467             printf(_("  Messages will be delivered with \"%s\".\n"), visbuf(ctl->mda));
1468         else
1469         {
1470             struct idlist *idp;
1471
1472             printf(_("  Messages will be %cMTP-forwarded to:"), ctl->listener);
1473             for (idp = ctl->smtphunt; idp; idp = idp->next)
1474             {
1475                 printf(" %s", idp->id);
1476                 if (!idp->val.status.mark)
1477                     printf(_(" (default)"));
1478             }
1479             printf("\n");
1480             if (ctl->smtpaddress)
1481                 printf(_("  Host part of MAIL FROM line will be %s\n"),
1482                        ctl->smtpaddress);
1483         }
1484         if (ctl->server.protocol != P_ETRN)
1485         {
1486                 if (ctl->antispam != (struct idlist *)NULL)
1487                 {
1488                     struct idlist *idp;
1489
1490                     printf(_("  Recognized listener spam block responses are:"));
1491                     for (idp = ctl->antispam; idp; idp = idp->next)
1492                         printf(" %d", idp->val.status.num);
1493                     printf("\n");
1494                 }
1495                 else if (outlevel >= O_VERBOSE)
1496                     printf(_("  Spam-blocking disabled\n"));
1497         }
1498         if (ctl->preconnect)
1499             printf(_("  Server connection will be brought up with \"%s\".\n"),
1500                    visbuf(ctl->preconnect));
1501         else if (outlevel >= O_VERBOSE)
1502             printf(_("  No pre-connection command.\n"));
1503         if (ctl->postconnect)
1504             printf(_("  Server connection will be taken down with \"%s\".\n"),
1505                    visbuf(ctl->postconnect));
1506         else if (outlevel >= O_VERBOSE)
1507             printf(_("  No post-connection command.\n"));
1508         if (ctl->server.protocol != P_ETRN) {
1509                 if (!ctl->localnames)
1510                     printf(_("  No localnames declared for this host.\n"));
1511                 else
1512                 {
1513                     struct idlist *idp;
1514                     int count = 0;
1515
1516                     for (idp = ctl->localnames; idp; idp = idp->next)
1517                         ++count;
1518
1519                     if (count > 1 || ctl->wildcard)
1520                         printf(_("  Multi-drop mode: "));
1521                     else
1522                         printf(_("  Single-drop mode: "));
1523
1524                     printf(_("%d local name(s) recognized.\n"), count);
1525                     if (outlevel >= O_VERBOSE)
1526                     {
1527                         for (idp = ctl->localnames; idp; idp = idp->next)
1528                             if (idp->val.id2)
1529                                 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1530                             else
1531                                 printf("\t%s\n", idp->id);
1532                         if (ctl->wildcard)
1533                             fputs("\t*\n", stdout);
1534                     }
1535
1536                     if (count > 1 || ctl->wildcard)
1537                     {
1538                         printf(_("  DNS lookup for multidrop addresses is %s.\n"),
1539                                ctl->server.dns ? _("enabled") : _("disabled"));
1540                         if (ctl->server.dns)
1541                         {
1542                             printf(_("  Server aliases will be compared with multidrop addresses by "));
1543                             if (ctl->server.checkalias)
1544                                 printf(_("IP address.\n"));
1545                             else
1546                                 printf(_("name.\n"));
1547                         }
1548                         if (ctl->server.envelope == STRING_DISABLED)
1549                             printf(_("  Envelope-address routing is disabled\n"));
1550                         else
1551                         {
1552                             printf(_("  Envelope header is assumed to be: %s\n"),
1553                                    ctl->server.envelope ? ctl->server.envelope:_("Received"));
1554                             if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1555                                 printf(_("  Number of envelope header to be parsed: %d\n"),
1556                                        ctl->server.envskip);
1557                             if (ctl->server.qvirtual)
1558                                 printf(_("  Prefix %s will be removed from user id\n"),
1559                                        ctl->server.qvirtual);
1560                             else if (outlevel >= O_VERBOSE) 
1561                                 printf(_("  No prefix stripping\n"));
1562                         }
1563
1564                         if (ctl->server.akalist)
1565                         {
1566                             struct idlist *idp;
1567
1568                             printf(_("  Predeclared mailserver aliases:"));
1569                             for (idp = ctl->server.akalist; idp; idp = idp->next)
1570                                 printf(" %s", idp->id);
1571                             putchar('\n');
1572                         }
1573                         if (ctl->server.localdomains)
1574                         {
1575                             struct idlist *idp;
1576
1577                             printf(_("  Local domains:"));
1578                             for (idp = ctl->server.localdomains; idp; idp = idp->next)
1579                                 printf(" %s", idp->id);
1580                             putchar('\n');
1581                         }
1582                     }
1583                 }
1584         }
1585 #ifdef  linux
1586         if (ctl->server.interface)
1587             printf(_("  Connection must be through interface %s.\n"), ctl->server.interface);
1588         else if (outlevel >= O_VERBOSE)
1589             printf(_("  No interface requirement specified.\n"));
1590         if (ctl->server.monitor)
1591             printf(_("  Polling loop will monitor %s.\n"), ctl->server.monitor);
1592         else if (outlevel >= O_VERBOSE)
1593             printf(_("  No monitor interface specified.\n"));
1594 #endif
1595
1596         if (ctl->server.plugin)
1597             printf(_("  Server connections will be mode via plugin %s (--plugin %s).\n"), ctl->server.plugin, ctl->server.plugin);
1598         else if (outlevel >= O_VERBOSE)
1599             printf(_("  No plugin command specified.\n"));
1600         if (ctl->server.plugout)
1601             printf(_("  Listener connections will be mode via plugout %s (--plugout %s).\n"), ctl->server.plugout, ctl->server.plugout);
1602         else if (outlevel >= O_VERBOSE)
1603             printf(_("  No plugout command specified.\n"));
1604
1605         if (ctl->server.protocol > P_POP2 && (ctl->server.protocol != P_ETRN))
1606             if (!ctl->oldsaved)
1607                 printf(_("  No UIDs saved from this host.\n"));
1608             else
1609             {
1610                 struct idlist *idp;
1611                 int count = 0;
1612
1613                 for (idp = ctl->oldsaved; idp; idp = idp->next)
1614                     ++count;
1615
1616                 printf(_("  %d UIDs saved.\n"), count);
1617                 if (outlevel >= O_VERBOSE)
1618                     for (idp = ctl->oldsaved; idp; idp = idp->next)
1619                         printf("\t%s\n", idp->id);
1620             }
1621
1622         if (ctl->properties)
1623             printf(_("  Pass-through properties \"%s\".\n"),
1624                    visbuf(ctl->properties));
1625     }
1626 }
1627
1628 /* fetchmail.c ends here */