]> 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                         if (outlevel == O_VERBOSE)
496                             error(0, 0, "saved UID List");
497                     }
498 #endif  /* POP3_ENABLE */
499                 }
500                 else if (!check_only && 
501                          ((querystatus!=PS_NOMAIL) || (outlevel==O_VERBOSE)))
502                     error(0, 0, "Query status=%d", querystatus);
503
504 #if defined(linux) && !INET6
505                 if (ctl->server.monitor)
506                 {
507                     /*
508                      * Allow some time for the link to quiesce.  One
509                      * second is usually sufficient, three is safe.
510                      * Note:  this delay is important - don't remove!
511                      */
512                     sleep(3);
513                     interface_note_activity(&ctl->server);
514                 }
515 #endif /* defined(linux) && !INET6 */
516             }
517         }
518
519 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
520         endhostent();           /* release TCP/IP connection to nameserver */
521 #endif /* HAVE_RES_SEARCH */
522
523         /*
524          * Close all SMTP delivery sockets.  For optimum performance
525          * we'd like to hold them open til end of run, but (1) this
526          * loses if our poll interval is longer than the MTA's inactivity
527          * timeout, and (2) some MTAs (like smail) don't deliver after
528          * each message, but rather queue up mail and wait to actually
529          * deliver it until the input socket is closed. 
530          */
531         for (ctl = querylist; ctl; ctl = ctl->next)
532             if (ctl->smtp_socket != -1)
533             {
534                 SMTP_quit(ctl->smtp_socket);
535                 close(ctl->smtp_socket);
536                 ctl->smtp_socket = -1;
537             }
538
539         /*
540          * OK, we've polled.  Now sleep.
541          */
542         if (run.poll_interval)
543         {
544             if (outlevel == O_VERBOSE)
545             {
546                 time_t  now;
547
548                 time(&now);
549                 fprintf(stderr, "fetchmail: sleeping at %s", ctime(&now));
550             }
551
552             /*
553              * With this simple hack, we make it possible for a foreground 
554              * fetchmail to wake up one in daemon mode.  What we want is the
555              * side effect of interrupting any sleep that may be going on,
556              * forcing fetchmail to re-poll its hosts.  The second line is
557              * for people who think all system daemons wake up on SIGHUP.
558              */
559             signal(SIGUSR1, donothing);
560             if (!getuid())
561                 signal(SIGHUP, donothing);
562
563             /*
564              * We can't use sleep(3) here because we need an alarm(3)
565              * equivalent in order to implement server nonresponse timeout.
566              * We'll just assume setitimer(2) is available since fetchmail
567              * has to have a BSDoid socket layer to work at all.
568              */
569             {
570 #ifndef __EMX__
571 #ifdef SLEEP_WITH_ALARM         /* not normally on */
572                 /* 
573                  * This code stopped working under glibc-2, apparently due
574                  * to the change in signal(2) semantics.  (The siginterrupt
575                  * line, added later, should fix this problem.) John Stracke
576                  * <francis@netscape.com> wrote:
577                  *
578                  * The problem seems to be that, after hitting the interval
579                  * timer while talking to the server, the process no longer
580                  * responds to SIGALRM.  I put in printf()s to see when it
581                  * reached the pause() for the poll interval, and I checked
582                  * the return from setitimer(), and everything seemed to be
583                  * working fine, except that the pause() just ignored SIGALRM.
584                  * I thought maybe the itimer wasn't being fired, so I hit
585                  * it with a SIGALRM from the command line, and it ignored
586                  * that, too.  SIGUSR1 woke it up just fine, and it proceeded
587                  * to repoll--but, when the dummy server didn't respond, it
588                  * never timed out, and SIGALRM wouldn't make it.
589                  *
590                  * (continued below...)
591                  */
592                 struct itimerval ntimeout;
593
594                 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
595                 ntimeout.it_value.tv_sec  = poll_interval;
596                 ntimeout.it_value.tv_usec = 0;
597
598                 siginterrupt(SIGALRM, 1);
599                 alarm_latch = FALSE;
600                 signal(SIGALRM, gotsigalrm);    /* first trap signals */
601                 setitimer(ITIMER_REAL,&ntimeout,NULL);  /* then start timer */
602                 /* there is a very small window between the next two lines */
603                 if (!alarm_latch)
604                     pause();
605                 signal(SIGALRM, SIG_IGN);
606 #else
607                 /* 
608                  * So the workaround I used is to make it sleep by using
609                  * select() instead of setitimer()/pause().  select() is
610                  * perfectly happy being called with a timeout and
611                  * no file descriptors; it just sleeps until it hits the
612                  * timeout.  The only concern I had was that it might
613                  * implement its timeout with SIGALRM--there are some
614                  * Unices where this is done, because select() is a library
615                  * function--but apparently not.
616                  */
617                 struct timeval timeout;
618
619                 timeout.tv_sec = run.poll_interval;
620                 timeout.tv_usec = 0;
621                 lastsig = 0;
622                 select(0,0,0,0, &timeout);
623 #endif
624 #else /* EMX */
625                 alarm_latch = FALSE;
626                 signal(SIGALRM, gotsigalrm);
627                 _beginthread(itimerthread, NULL, 32768, NULL);
628                 /* see similar code above */
629                 if (!alarm_latch)
630                     pause();
631                 signal(SIGALRM, SIG_IGN);
632 #endif /* ! EMX */
633                 if (lastsig == SIGUSR1
634                         || ((run.poll_interval && !getuid()) && lastsig == SIGHUP))
635                 {
636 #ifdef SYS_SIGLIST_DECLARED
637                     error(0, 0, "awakened by %s", sys_siglist[lastsig]);
638 #else
639                     error(0, 0, "awakened by signal %d", lastsig);
640 #endif
641                 }
642             }
643
644             /* now lock out interrupts again */
645             signal(SIGUSR1, SIG_IGN);
646             if (!getuid())
647                 signal(SIGHUP, SIG_IGN);
648
649             if (outlevel == O_VERBOSE)
650             {
651                 time_t  now;
652
653                 time(&now);
654                 fprintf(stderr, "fetchmail: awakened at %s", ctime(&now));
655             }
656         }
657     } while
658         (run.poll_interval);
659
660     if (outlevel == O_VERBOSE)
661         fprintf(stderr,"fetchmail: normal termination, status %d\n",
662                 successes ? PS_SUCCESS : querystatus);
663
664     termhook(0);
665     exit(successes ? PS_SUCCESS : querystatus);
666 }
667
668 static void optmerge(struct query *h2, struct query *h1, int force)
669 /* merge two options records */
670 {
671     /*
672      * If force is off, modify h2 fields only when they're empty (treat h1
673      * as defaults).  If force is on, modify each h2 field whenever h1
674      * is nonempty (treat h1 as an override).  
675      */
676 #define LIST_MERGE(dstl, srcl) if (force ? !!srcl : !dstl) \
677                                                 free_str_list(&dstl), \
678                                                 append_str_list(&dstl, &srcl)
679     LIST_MERGE(h2->server.localdomains, h1->server.localdomains);
680     LIST_MERGE(h2->localnames, h1->localnames);
681     LIST_MERGE(h2->mailboxes, h1->mailboxes);
682     LIST_MERGE(h2->smtphunt, h1->smtphunt);
683     LIST_MERGE(h2->antispam, h1->antispam);
684 #undef LIST_MERGE
685
686 #define FLAG_MERGE(fld) if (force ? !!h1->fld : !h2->fld) h2->fld = h1->fld
687     FLAG_MERGE(server.via);
688     FLAG_MERGE(server.protocol);
689 #if INET6
690     FLAG_MERGE(server.service);
691     FLAG_MERGE(server.netsec);
692 #else /* INET6 */
693     FLAG_MERGE(server.port);
694 #endif /* INET6 */
695     FLAG_MERGE(server.interval);
696     FLAG_MERGE(server.preauthenticate);
697     FLAG_MERGE(server.timeout);
698     FLAG_MERGE(server.envelope);
699     FLAG_MERGE(server.envskip);
700     FLAG_MERGE(server.qvirtual);
701     FLAG_MERGE(server.skip);
702     FLAG_MERGE(server.dns);
703     FLAG_MERGE(server.checkalias);
704     FLAG_MERGE(server.uidl);
705
706 #ifdef linux
707     FLAG_MERGE(server.interface);
708     FLAG_MERGE(server.monitor);
709     FLAG_MERGE(server.interface_pair);
710 #endif /* linux */
711
712     FLAG_MERGE(wildcard);
713     FLAG_MERGE(remotename);
714     FLAG_MERGE(password);
715     FLAG_MERGE(mda);
716     FLAG_MERGE(smtpaddress);
717     FLAG_MERGE(preconnect);
718
719     FLAG_MERGE(keep);
720     FLAG_MERGE(flush);
721     FLAG_MERGE(fetchall);
722     FLAG_MERGE(rewrite);
723     FLAG_MERGE(forcecr);
724     FLAG_MERGE(stripcr);
725     FLAG_MERGE(pass8bits);
726     FLAG_MERGE(dropstatus);
727     FLAG_MERGE(mimedecode);
728     FLAG_MERGE(limit);
729     FLAG_MERGE(fetchlimit);
730     FLAG_MERGE(batchlimit);
731     FLAG_MERGE(expunge);
732 #undef FLAG_MERGE
733 }
734
735 static int load_params(int argc, char **argv, int optind)
736 {
737     int implicitmode, st;
738     struct passwd *pw;
739     struct query def_opts, *ctl;
740
741     memset(&def_opts, '\0', sizeof(struct query));
742     def_opts.smtp_socket = -1;
743     def_opts.smtpaddress = (char *)0;
744     save_str(&def_opts.antispam, (char *)NULL, 0)->val.status.num = 571;
745
746     def_opts.server.protocol = P_AUTO;
747     def_opts.server.timeout = CLIENT_TIMEOUT;
748     def_opts.remotename = user;
749     def_opts.expunge = 1;
750
751     /* this builds the host list */
752     if (prc_parse_file(rcfile, !versioninfo) != 0)
753         exit(PS_SYNTAX);
754
755     if ((implicitmode = (optind >= argc)))
756     {
757         for (ctl = querylist; ctl; ctl = ctl->next)
758             ctl->active = TRUE;
759     }
760     else
761         for (; optind < argc; optind++) 
762         {
763             flag        predeclared =  FALSE;
764
765             /*
766              * If hostname corresponds to a host known from the rc file,
767              * simply declare it active.  Otherwise synthesize a host
768              * record from command line and defaults
769              */
770             for (ctl = querylist; ctl; ctl = ctl->next)
771                 if (!strcmp(ctl->server.pollname, argv[optind])
772                         || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
773                 {
774                     ctl->active = TRUE;
775                     predeclared = TRUE;
776                 }
777
778             if (!predeclared)
779             {
780                 /*
781                  * Allocate and link record without copying in
782                  * command-line args; we'll do that with the optmerge
783                  * call later on.
784                  */
785                 ctl = hostalloc((struct query *)NULL);
786                 ctl->server.via =
787                     ctl->server.pollname = xstrdup(argv[optind]);
788                 ctl->active = TRUE;
789             }
790         }
791
792     /*
793      * If there's a defaults record, merge it and lose it.
794      */ 
795     if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
796     {
797         for (ctl = querylist->next; ctl; ctl = ctl->next)
798             optmerge(ctl, querylist, FALSE);
799         querylist = querylist->next;
800     }
801
802     /* don't allow a defaults record after the first */
803     for (ctl = querylist; ctl; ctl = ctl->next)
804         if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
805             exit(PS_SYNTAX);
806
807     /* merge in wired defaults, do sanity checks and prepare internal fields */
808     for (ctl = querylist; ctl; ctl = ctl->next)
809     {
810         if (configdump || (ctl->active && !(implicitmode && ctl->server.skip)))
811         {
812             /* merge in defaults */
813             optmerge(ctl, &def_opts, FALSE);
814
815             /* force command-line options */
816             optmerge(ctl, &cmd_opts, TRUE);
817
818             /* make sure we have a nonempty host list to forward to */
819             if (!ctl->smtphunt)
820             {
821                 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
822                 /* for non ETRN try to deliver mails to localhost if
823                  * fetchmailhost fails
824                  */
825                 if (ctl->server.protocol != P_ETRN) {
826                     save_str(&ctl->smtphunt, "localhost", FALSE);
827                 }
828             }
829
830             /* keep lusers from shooting themselves in the foot :-) */
831             if (run.poll_interval && ctl->limit)
832             {
833                 fprintf(stderr,"fetchmail: you'd never see large messages!\n");
834                 exit(PS_SYNTAX);
835             }
836
837             /* if `user' doesn't name a real local user, try to run as root */
838             if ((pw = getpwnam(user)) == (struct passwd *)NULL)
839                 ctl->uid = 0;
840             else
841                 ctl->uid = pw->pw_uid;  /* for local delivery via MDA */
842             if (!ctl->localnames)       /* for local delivery via SMTP */
843                 save_str_pair(&ctl->localnames, user, NULL);
844
845             /* this code enables flags to be turned off */
846 #define DEFAULT(flag, dflt)     if (flag == FLAG_TRUE)\
847                                         flag = TRUE;\
848                                 else if (flag == FLAG_FALSE)\
849                                         flag = FALSE;\
850                                 else\
851                                         flag = (dflt)
852             DEFAULT(ctl->keep, FALSE);
853             DEFAULT(ctl->fetchall, FALSE);
854             DEFAULT(ctl->flush, FALSE);
855             DEFAULT(ctl->rewrite, TRUE);
856             DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL)); 
857             DEFAULT(ctl->forcecr, FALSE);
858             DEFAULT(ctl->pass8bits, FALSE);
859             DEFAULT(ctl->dropstatus, FALSE);
860             DEFAULT(ctl->mimedecode, FALSE);
861             DEFAULT(ctl->server.dns, TRUE);
862             DEFAULT(ctl->server.uidl, FALSE);
863             DEFAULT(ctl->server.checkalias, FALSE);
864 #undef DEFAULT
865
866 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
867             /* can't handle multidrop mailboxes unless we can do DNS lookups */
868             if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
869             {
870                 ctl->server.dns = FALSE;
871                 fprintf(stderr, "fetchmail: warning: no DNS available to check multidrop fetches from %s\n", ctl->server.pollname);
872             }
873 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
874
875             /*
876              *
877              * Compute the true name of the mailserver host.  
878              * There are two clashing cases here:
879              *
880              * (1) The poll name is a label, possibly on one of several
881              *     poll configurations for the same host.  In this case 
882              *     the `via' option will be present and give the true name.
883              *
884              * (2) The poll name is the true one, the via name is 
885              *     localhost.   This is going to be typical for ssh-using
886              *     configurations.
887              *
888              * We're going to assume the via name is true unless it's
889              * localhost.
890              */
891             if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
892                 ctl->server.queryname = xstrdup(ctl->server.via);
893             else
894                 ctl->server.queryname = xstrdup(ctl->server.pollname);
895
896             /*
897              * We may have to canonicalize the server truename for later use.
898              * Do this just once for each lead server, if necessary, in order
899              * to minimize DNS round trips.
900              */
901             if (ctl->server.lead_server)
902                 ctl->server.truename = xstrdup(ctl->server.lead_server->truename);
903 #ifdef HAVE_GETHOSTBYNAME
904             else if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
905                 ctl->server.preauthenticate==A_KERBEROS_V5 ||
906                 (ctl->server.dns && MULTIDROP(ctl)))
907             {
908                 struct hostent  *namerec;
909
910                 /* compute the canonical name of the host */
911                 errno = 0;
912                 namerec = gethostbyname(ctl->server.queryname);
913                 if (namerec == (struct hostent *)NULL)
914                 {
915                     error(0, errno,
916                           "couldn't find canonical DNS name of %s",
917                           ctl->server.pollname);
918                     exit(PS_DNS);
919                 }
920                 else
921                     ctl->server.truename=xstrdup((char *)namerec->h_name);
922             }
923 #endif /* HAVE_GETHOSTBYNAME */
924             else
925                 ctl->server.truename = xstrdup(ctl->server.queryname);
926
927             /* if no folders were specified, set up the null one as default */
928             if (!ctl->mailboxes)
929                 save_str(&ctl->mailboxes, (char *)NULL, 0);
930
931             /* maybe user overrode timeout on command line? */
932             if (ctl->server.timeout == -1)      
933                 ctl->server.timeout = CLIENT_TIMEOUT;
934
935 #if !INET6
936             /* sanity checks */
937             if (ctl->server.port < 0)
938             {
939                 (void) fprintf(stderr,
940                                "%s configuration invalid, port number cannot be negative",
941                                ctl->server.pollname);
942                 exit(PS_SYNTAX);
943             }
944             if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
945             {
946                 (void) fprintf(stderr,
947                                "%s configuration invalid, RPOP requires a privileged port",
948                                ctl->server.pollname);
949                 exit(PS_SYNTAX);
950             }
951 #endif /* !INET6 */
952         }
953     }
954
955     /* here's where we override globals */
956     if (cmd_run.logfile)
957         run.logfile = cmd_run.logfile;
958     if (cmd_run.idfile)
959         run.idfile = cmd_run.idfile;
960     if (cmd_run.poll_interval >= 0)
961         run.poll_interval = cmd_run.poll_interval;
962     if (cmd_run.invisible)
963         run.invisible = cmd_run.invisible;
964     if (cmd_run.use_syslog)
965         run.use_syslog = (cmd_run.use_syslog == FLAG_TRUE);
966
967     /* check and daemon options are not compatible */
968     if (check_only && run.poll_interval)
969         run.poll_interval = 0;
970
971 #ifdef POP3_ENABLE
972     /* initialize UID handling */
973     if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
974         exit(st);
975     else
976         initialize_saved_lists(querylist, run.idfile);
977 #endif /* POP3_ENABLE */
978
979     /*
980      * If the user didn't set a last-resort user to get misaddressed
981      * multidrop mail, set an appropriate default here.
982      */
983     if (!run.postmaster)
984         if (getuid())                           /* ordinary user */
985             run.postmaster = user;
986         else                                    /* root */
987             run.postmaster = "postmaster";
988
989     return(implicitmode);
990 }
991
992 void termhook(int sig)
993 /* to be executed on normal or signal-induced termination */
994 {
995     struct query        *ctl;
996
997     /*
998      * Sending SMTP QUIT on signal is theoretically nice, but led to a 
999      * subtle bug.  If fetchmail was terminated by signal while it was 
1000      * shipping message text, it would hang forever waiting for a
1001      * command acknowledge.  In theory we could enable the QUIT
1002      * only outside of the message send.  In practice, we don't
1003      * care.  All mailservers hang up on a dropped TCP/IP connection
1004      * anyway.
1005      */
1006
1007     if (sig != 0)
1008         error(0, 0, "terminated with signal %d", sig);
1009     else
1010         /* terminate all SMTP connections cleanly */
1011         for (ctl = querylist; ctl; ctl = ctl->next)
1012             if (ctl->smtp_socket != -1)
1013                 SMTP_quit(ctl->smtp_socket);
1014
1015 #ifdef POP3_ENABLE
1016     if (!check_only)
1017         write_saved_lists(querylist, run.idfile);
1018 #endif /* POP3_ENABLE */
1019
1020     /* 
1021      * Craig Metz, the RFC1938 one-time-password guy, points out:
1022      * "Remember that most kernels don't zero pages before handing them to the
1023      * next process and many kernels share pages between user and kernel space.
1024      * You'd be very surprised what you can find from a short program to do a
1025      * malloc() and then dump the contents of the pages you got. By zeroing
1026      * the secrets at end of run (earlier if you can), you make sure the next
1027      * guy can't get the password/pass phrase."
1028      *
1029      * Right you are, Craig!
1030      */
1031     for (ctl = querylist; ctl; ctl = ctl->next)
1032         if (ctl->password)
1033           memset(ctl->password, '\0', strlen(ctl->password));
1034
1035 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
1036     unlockit();
1037 #endif
1038
1039     exit(successes ? PS_SUCCESS : querystatus);
1040 }
1041
1042 /*
1043  * Sequence of protocols to try when autoprobing, most capable to least.
1044  */
1045 static const int autoprobe[] = 
1046 {
1047 #ifdef IMAP_ENABLE
1048     P_IMAP,
1049 #endif /* IMAP_ENABLE */
1050 #ifdef POP3_ENABLE
1051     P_POP3,
1052 #endif /* POP3_ENABLE */
1053 #ifdef POP2_ENABLE
1054     P_POP2
1055 #endif /* POP2_ENABLE */
1056 };
1057
1058 static int query_host(struct query *ctl)
1059 /* perform fetch transaction with single host */
1060 {
1061     int i, st;
1062
1063     if (outlevel == O_VERBOSE)
1064     {
1065         time_t now;
1066
1067         time(&now);
1068         fprintf(stderr, "fetchmail: %s querying %s (protocol %s) at %s",
1069             RELEASE_ID,
1070             ctl->server.pollname, showproto(ctl->server.protocol), ctime(&now));
1071     }
1072     switch (ctl->server.protocol) {
1073     case P_AUTO:
1074         for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
1075         {
1076             ctl->server.protocol = autoprobe[i];
1077             if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
1078                 break;
1079         }
1080         ctl->server.protocol = P_AUTO;
1081         return(st);
1082         break;
1083     case P_POP2:
1084 #ifdef POP2_ENABLE
1085         return(doPOP2(ctl));
1086 #else
1087         fprintf(stderr, "POP2 support is not configured.\n");
1088         return(PS_PROTOCOL);
1089 #endif /* POP2_ENABLE */
1090         break;
1091     case P_POP3:
1092     case P_APOP:
1093     case P_RPOP:
1094 #ifdef POP3_ENABLE
1095         return(doPOP3(ctl));
1096 #else
1097         fprintf(stderr, "POP3 support is not configured.\n");
1098         return(PS_PROTOCOL);
1099 #endif /* POP3_ENABLE */
1100         break;
1101     case P_IMAP:
1102     case P_IMAP_K4:
1103 #ifdef GSSAPI
1104     case P_IMAP_GSS:
1105 #endif /* GSSAPI */
1106 #ifdef IMAP_ENABLE
1107         return(doIMAP(ctl));
1108 #else
1109         fprintf(stderr, "IMAP support is not configured.\n");
1110         return(PS_PROTOCOL);
1111 #endif /* IMAP_ENABLE */
1112         break;
1113     case P_ETRN:
1114 #ifndef ETRN_ENABLE
1115         fprintf(stderr, "ETRN support is not configured.\n");
1116         return(PS_PROTOCOL);
1117 #else
1118 #ifdef HAVE_GETHOSTBYNAME
1119         return(doETRN(ctl));
1120 #else
1121         fprintf(stderr, "Cannot support ETRN without gethostbyname(2).\n");
1122         return(PS_PROTOCOL);
1123 #endif /* HAVE_GETHOSTBYNAME */
1124 #endif /* ETRN_ENABLE */
1125     default:
1126         error(0, 0, "unsupported protocol selected.");
1127         return(PS_PROTOCOL);
1128     }
1129 }
1130
1131 void dump_params (struct runctl *runp, struct query *querylist, flag implicit)
1132 /* display query parameters in English */
1133 {
1134     struct query *ctl;
1135
1136     if (runp->poll_interval)
1137         printf("Poll interval is %d seconds\n", runp->poll_interval);
1138     if (runp->logfile)
1139         printf("Logfile is %s\n", runp->logfile);
1140     if (strcmp(runp->idfile, IDFILE_NAME))
1141         printf("Idfile is %s\n", runp->idfile);
1142 #if defined(HAVE_SYSLOG)
1143     if (runp->use_syslog)
1144         printf("Progress messages will be logged via syslog\n");
1145 #endif
1146     if (runp->invisible)
1147         printf("Fetchmail will masquerade and will not generate Received\n");
1148     if (runp->postmaster)
1149         printf("Fetchmail will forward misaddressed multidrop messages to %s.",
1150                runp->postmaster);
1151
1152     for (ctl = querylist; ctl; ctl = ctl->next)
1153     {
1154         if (!ctl->active || (implicit && ctl->server.skip))
1155             continue;
1156
1157         printf("Options for retrieving from %s@%s:\n",
1158                ctl->remotename, visbuf(ctl->server.pollname));
1159
1160         if (ctl->server.via && (ctl->server.protocol != P_ETRN))
1161             printf("  Mail will be retrieved via %s\n", ctl->server.via);
1162
1163         if (ctl->server.interval)
1164             printf("  Poll of this server will occur every %d intervals.\n",
1165                    ctl->server.interval);
1166         if (ctl->server.truename)
1167             printf("  True name of server is %s.\n", ctl->server.truename);
1168         if (ctl->server.skip || outlevel == O_VERBOSE)
1169             printf("  This host will%s be queried when no host is specified.\n",
1170                    ctl->server.skip ? " not" : "");
1171         /*
1172          * Don't poll for password when there is one or when using the ETRN
1173          * or IMAP-GSS protocol
1174          */
1175         if (!ctl->password && (ctl->server.protocol != P_ETRN)
1176 #ifdef GSSAPI
1177             && (ctl->server.protocol != P_IMAP_GSS)
1178 #endif /* GSSAPI */
1179         )
1180             printf("  Password will be prompted for.\n");
1181         else if (outlevel == O_VERBOSE)
1182             if (ctl->server.protocol == P_APOP)
1183                 printf("  APOP secret = '%s'.\n", visbuf(ctl->password));
1184             else if (ctl->server.protocol == P_RPOP)
1185                 printf("  RPOP id = '%s'.\n", visbuf(ctl->password));
1186             else
1187                 printf("  Password = '%s'.\n", visbuf(ctl->password));
1188         if (ctl->server.protocol == P_POP3 
1189 #if INET6
1190             && !strcmp(ctl->server.service, KPOP_PORT)
1191 #else /* INET6 */
1192             && ctl->server.port == KPOP_PORT
1193 #endif /* INET6 */
1194             && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1195                 ctl->server.preauthenticate == A_KERBEROS_V5))
1196             printf("  Protocol is KPOP");
1197         else
1198             printf("  Protocol is %s", showproto(ctl->server.protocol));
1199 #if INET6
1200         if (ctl->server.service)
1201             printf(" (using service %s)", ctl->server.service);
1202         if (ctl->server.netsec)
1203             printf(" (using network security options %s)", ctl->server.netsec);
1204 #else /* INET6 */
1205         if (ctl->server.port)
1206             printf(" (using port %d)", ctl->server.port);
1207 #endif /* INET6 */
1208         else if (outlevel == O_VERBOSE)
1209             printf(" (using default port)");
1210         if (ctl->server.uidl && (ctl->server.protocol != P_ETRN))
1211             printf(" (forcing UIDL use)");
1212         putchar('.');
1213         putchar('\n');
1214         if (ctl->server.preauthenticate == A_KERBEROS_V4)
1215             printf("  Kerberos V4 preauthentication enabled.\n");
1216         if (ctl->server.preauthenticate == A_KERBEROS_V5)
1217             printf("  Kerberos V5 preauthentication enabled.\n");
1218         if (ctl->server.timeout > 0)
1219             printf("  Server nonresponse timeout is %d seconds", ctl->server.timeout);
1220         if (ctl->server.timeout ==  CLIENT_TIMEOUT)
1221             printf(" (default).\n");
1222         else
1223             printf(".\n");
1224
1225         if (ctl->server.protocol != P_ETRN) {
1226                 if (!ctl->mailboxes->id)
1227                     printf("  Default mailbox selected.\n");
1228                 else
1229                 {
1230                     struct idlist *idp;
1231
1232                     printf("  Selected mailboxes are:");
1233                     for (idp = ctl->mailboxes; idp; idp = idp->next)
1234                         printf(" %s", idp->id);
1235                     printf("\n");
1236                 }
1237                 printf("  %s messages will be retrieved (--all %s).\n",
1238                        ctl->fetchall ? "All" : "Only new",
1239                        ctl->fetchall ? "on" : "off");
1240                 printf("  Fetched messages will%s be kept on the server (--keep %s).\n",
1241                        ctl->keep ? "" : " not",
1242                        ctl->keep ? "on" : "off");
1243                 printf("  Old messages will%s be flushed before message retrieval (--flush %s).\n",
1244                        ctl->flush ? "" : " not",
1245                        ctl->flush ? "on" : "off");
1246                 printf("  Rewrite of server-local addresses is %sabled (--norewrite %s).\n",
1247                        ctl->rewrite ? "en" : "dis",
1248                        ctl->rewrite ? "off" : "on");
1249                 printf("  Carriage-return stripping is %sabled (stripcr %s).\n",
1250                        ctl->stripcr ? "en" : "dis",
1251                        ctl->stripcr ? "on" : "off");
1252                 printf("  Carriage-return forcing is %sabled (forcecr %s).\n",
1253                        ctl->forcecr ? "en" : "dis",
1254                        ctl->forcecr ? "on" : "off");
1255                 printf("  Interpretation of Content-Transfer-Encoding is %sabled (pass8bits %s).\n",
1256                        ctl->pass8bits ? "dis" : "en",
1257                        ctl->pass8bits ? "on" : "off");
1258                 printf("  MIME decoding is %sabled (mimedecode %s).\n",
1259                        ctl->mimedecode ? "en" : "dis",
1260                        ctl->mimedecode ? "on" : "off");
1261                 printf("  Nonempty Status lines will be %s (dropstatus %s)\n",
1262                        ctl->dropstatus ? "discarded" : "kept",
1263                        ctl->dropstatus ? "on" : "off");
1264                 if (NUM_NONZERO(ctl->limit))
1265                     printf("  Message size limit is %d bytes (--limit %d).\n", 
1266                            ctl->limit, ctl->limit);
1267                 else if (outlevel == O_VERBOSE)
1268                     printf("  No message size limit (--limit 0).\n");
1269                 if (NUM_NONZERO(ctl->fetchlimit))
1270                     printf("  Received-message limit is %d (--fetchlimit %d).\n",
1271                            ctl->fetchlimit, ctl->fetchlimit);
1272                 else if (outlevel == O_VERBOSE)
1273                     printf("  No received-message limit (--fetchlimit 0).\n");
1274                 if (NUM_NONZERO(ctl->batchlimit))
1275                     printf("  SMTP message batch limit is %d.\n", ctl->batchlimit);
1276                 else if (outlevel == O_VERBOSE)
1277                     printf("  No SMTP message batch limit (--batchlimit 0).\n");
1278                 if (ctl->server.protocol == P_IMAP)
1279                     if (NUM_NONZERO(ctl->expunge))
1280                         printf("  Deletion interval between expunges is %d (--expunge %d).\n", ctl->expunge, ctl->expunge);
1281                     else if (outlevel == O_VERBOSE)
1282                         printf("  No expunges (--expunge 0).\n");
1283         }
1284         if (ctl->mda && (ctl->server.protocol != P_ETRN))
1285             printf("  Messages will be delivered with '%s.'\n", visbuf(ctl->mda));
1286         else
1287         {
1288             struct idlist *idp;
1289
1290             printf("  Messages will be SMTP-forwarded to:");
1291             for (idp = ctl->smtphunt; idp; idp = idp->next)
1292             {
1293                 printf(" %s", idp->id);
1294                 if (!idp->val.status.mark)
1295                     printf(" (default)");
1296             }
1297             printf("\n");
1298             if (ctl->smtpaddress)
1299                 printf("  Host part of MAIL FROM line will be %s\n",
1300                        ctl->smtpaddress);
1301         }
1302         if (ctl->server.protocol != P_ETRN)
1303         {
1304                 if (ctl->antispam != (struct idlist *)NULL)
1305                 {
1306                     struct idlist *idp;
1307
1308                     printf("  Recognized listener spam block responses are:",
1309                            ctl->antispam);
1310                     for (idp = ctl->antispam; idp; idp = idp->next)
1311                         printf(" %d", idp->val.status.num);
1312                     printf("\n");
1313                 }
1314                 else if (outlevel == O_VERBOSE)
1315                     printf("  Spam-blocking disabled\n");
1316         }
1317         if (ctl->preconnect)
1318             printf("  Server connection will be brought up with '%s.'\n",
1319                    visbuf(ctl->preconnect));
1320         else if (outlevel == O_VERBOSE)
1321             printf("  No pre-connection command.\n");
1322         if (ctl->postconnect)
1323             printf("  Server connection will be taken down with '%s.'\n",
1324                    visbuf(ctl->postconnect));
1325         else if (outlevel == O_VERBOSE)
1326             printf("  No post-connection command.\n");
1327         if (ctl->server.protocol != P_ETRN) {
1328                 if (!ctl->localnames)
1329                     printf("  No localnames declared for this host.\n");
1330                 else
1331                 {
1332                     struct idlist *idp;
1333                     int count = 0;
1334
1335                     for (idp = ctl->localnames; idp; idp = idp->next)
1336                         ++count;
1337
1338                     if (count > 1 || ctl->wildcard)
1339                         printf("  Multi-drop mode: ");
1340                     else
1341                         printf("  Single-drop mode: ");
1342
1343                     printf("%d local name(s) recognized.\n", count);
1344                     if (outlevel == O_VERBOSE)
1345                     {
1346                         for (idp = ctl->localnames; idp; idp = idp->next)
1347                             if (idp->val.id2)
1348                                 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1349                             else
1350                                 printf("\t%s\n", idp->id);
1351                         if (ctl->wildcard)
1352                             fputs("\t*\n", stdout);
1353                     }
1354
1355                     if (count > 1 || ctl->wildcard)
1356                     {
1357                         printf("  DNS lookup for multidrop addresses is %sabled.\n",
1358                                ctl->server.dns ? "en" : "dis");
1359                         if (ctl->server.dns)
1360                         {
1361                             printf("  Server aliases will be compared with multidrop addresses by ");
1362                             if (ctl->server.checkalias)
1363                                 printf("IP address.");
1364                             else
1365                                 printf("name.");
1366                         }
1367                         if (ctl->server.envelope == STRING_DISABLED)
1368                             printf("  Envelope-address routing is disabled\n");
1369                         else
1370                         {
1371                             printf("  Envelope header is assumed to be: %s\n",
1372                                    ctl->server.envelope ? ctl->server.envelope:"Received");
1373                             if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1374                                 printf("  Number of envelope header to be parsed: %d\n",
1375                                        ctl->server.envskip);
1376                             if (ctl->server.qvirtual)
1377                                 printf("  Prefix %s will be removed from user id\n",
1378                                        ctl->server.qvirtual);
1379                             else if (outlevel >= O_VERBOSE) 
1380                                 printf("  No prefix stripping\n");
1381                         }
1382
1383                         if (ctl->server.akalist)
1384                         {
1385                             struct idlist *idp;
1386
1387                             printf("  Predeclared mailserver aliases:");
1388                             for (idp = ctl->server.akalist; idp; idp = idp->next)
1389                                 printf(" %s", idp->id);
1390                             putchar('\n');
1391                         }
1392                         if (ctl->server.localdomains)
1393                         {
1394                             struct idlist *idp;
1395
1396                             printf("  Local domains:");
1397                             for (idp = ctl->server.localdomains; idp; idp = idp->next)
1398                                 printf(" %s", idp->id);
1399                             putchar('\n');
1400                         }
1401                     }
1402                 }
1403         }
1404 #ifdef  linux
1405         if (ctl->server.interface)
1406             printf("  Connection must be through interface %s.\n", ctl->server.interface);
1407         else if (outlevel == O_VERBOSE)
1408             printf("  No interface requirement specified.\n");
1409         if (ctl->server.monitor)
1410             printf("  Polling loop will monitor %s.\n", ctl->server.monitor);
1411         else if (outlevel == O_VERBOSE)
1412             printf("  No monitor interface specified.\n");
1413 #endif
1414
1415         if (ctl->server.protocol > P_POP2 && (ctl->server.protocol != P_ETRN))
1416             if (!ctl->oldsaved)
1417                 printf("  No UIDs saved from this host.\n");
1418             else
1419             {
1420                 struct idlist *idp;
1421                 int count = 0;
1422
1423                 for (idp = ctl->oldsaved; idp; idp = idp->next)
1424                     ++count;
1425
1426                 printf("  %d UIDs saved.\n", count);
1427                 if (outlevel == O_VERBOSE)
1428                     for (idp = ctl->oldsaved; idp; idp = idp->next)
1429                         fprintf(stderr, "\t%s\n", idp->id);
1430             }
1431     }
1432 }
1433
1434 /* fetchmail.c ends here */