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