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