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