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