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