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