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