2 * fetchmail.c -- main driver module for fetchmail
4 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
14 #if defined(HAVE_UNISTD_H)
17 #if defined(HAVE_ALLOCA_H)
26 #if defined(HAVE_SYSLOG)
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_WAIT_H
38 #ifdef HAVE_GETHOSTBYNAME
40 #endif /* HAVE_GETHOSTBYNAME */
42 #include "fetchmail.h"
49 #define ENETUNREACH 128 /* Interactive doesn't know this */
50 #endif /* ENETUNREACH */
52 /* prototypes for internal functions */
53 static int load_params(int, char **, int);
54 static void dump_params (struct runctl *runp, struct query *, flag implicit);
55 static int query_host(struct query *);
57 /* controls the detail level of status/progress messages written to stderr */
58 int outlevel; /* see the O_.* constants above */
60 /* miscellaneous global controls */
61 struct runctl run; /* global controls for this run */
62 flag nodetach; /* if TRUE, don't detach daemon process */
63 flag quitmode; /* if --quit was set */
64 flag check_only; /* if --probe was set */
65 flag versioninfo; /* emit only version info */
66 char *user; /* the name of the invoking user */
67 char *home; /* invoking user's home directory */
68 char *fetchmailhost; /* the name of the host running fetchmail */
69 char *program_name; /* the name to prefix error messages with */
70 flag configdump; /* dump control blocks for configurator */
75 #endif /* NET_SECURITY */
77 static char *lockfile; /* name of lockfile */
78 static int querystatus; /* status of query */
79 static int successes; /* count number of successful polls */
80 static int lastsig; /* last signal received */
81 static struct runctl cmd_run; /* global options set from command line */
83 static void termhook(); /* forward declaration of exit hook */
86 * The function of this variable is to reduce the size of the window during
87 * which two SIGALRMS in rapid succession can hose the code. This is a
88 * bit of a kluge; the real right thing would use sigprocmask(), sigsuspend()
89 * and close the window entirely. But since the interval isn't normally
90 * going to be less than one second this is not a big issue.
93 static sig_atomic_t alarm_latch = FALSE;
95 /* assume int can be written in one atomic operation on non ANSI-C systems */
96 static int alarm_latch = FALSE;
99 RETSIGTYPE donothing(sig) int sig; {signal(sig, donothing); lastsig = sig;}
100 RETSIGTYPE gotsigalrm(sig) int sig; {signal(sig, donothing); lastsig = sig; alarm_latch = TRUE;}
103 static void unlockit(int n, void *p)
105 static void unlockit(void)
107 /* must-do actions for exit (but we can't count on being able to do malloc) */
113 /* Various EMX-specific definitions */
115 void itimerthread(void* dummy) {
116 if (outlevel == O_VERBOSE)
117 fprintf(stderr, "fetchmail: thread sleeping for %d sec.\n", poll_interval);
119 _sleep2(poll_interval*1000);
120 kill((getpid()), SIGALRM);
125 int main (int argc, char **argv)
127 int st, bkgd = FALSE;
128 int parsestatus, implicitmode = FALSE;
131 netrc_entry *netrc_list;
132 char *netrc_file, *tmpbuf;
135 envquery(argc, argv);
137 #define IDFILE_NAME ".fetchids"
138 run.idfile = (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
139 strcpy(run.idfile, home);
140 strcat(run.idfile, "/");
141 strcat(run.idfile, IDFILE_NAME);
145 if ((parsestatus = parsecmdline(argc,argv, &cmd_run, &cmd_opts)) < 0)
148 /* this hint to stdio should help messages come out in the right order */
149 setvbuf(stdout, NULL, _IOLBF, MSGBUFSIZE);
153 printf("This is fetchmail release %s", RELEASE_ID);
156 #endif /* POP2_ENABLE */
159 #endif /* POP3_ENABLE */
162 #endif /* IMAP_ENABLE */
165 #endif /* RPA_ENABLE */
168 #endif /* ETRN_ENABLE */
177 #endif /* NET_SECURITY */
180 /* this is an attempt to help remote debugging */
184 /* avoid parsing the config file if all we're doing is killing a daemon */
185 if (!(quitmode && argc == 2))
186 implicitmode = load_params(argc, argv, optind);
188 /* set up to do lock protocol */
189 #define FETCHMAIL_PIDFILE "fetchmail.pid"
190 tmpbuf = xmalloc(strlen(home) + strlen(FETCHMAIL_PIDFILE) + 3);
192 sprintf(tmpbuf, "%s/%s", PID_DIR, FETCHMAIL_PIDFILE);
194 strcpy(tmpbuf, home);
195 strcat(tmpbuf, "/.");
196 strcat(tmpbuf, FETCHMAIL_PIDFILE);
198 #undef FETCHMAIL_PIDFILE
200 /* perhaps we just want to check options? */
203 printf("Taking options from command line");
204 if (access(rcfile, 0))
207 printf(" and %s\n", rcfile);
208 if (outlevel == O_VERBOSE)
209 printf("Lockfile at %s\n", tmpbuf);
211 if (querylist == NULL)
212 (void) fprintf(stderr,
213 "No mailservers set up -- perhaps %s is missing?\n", rcfile);
215 dump_params(&run, querylist, implicitmode);
219 /* dump options as a Python dictionary, for configurator use */
222 dump_config(&run, querylist);
226 /* check for another fetchmail running concurrently */
228 if ((lockfile = (char *) malloc(strlen(tmpbuf) + 1)) == NULL)
230 fprintf(stderr,"fetchmail: cannot allocate memory for lock name.\n");
234 (void) strcpy(lockfile, tmpbuf);
235 if ((lockfp = fopen(lockfile, "r")) != NULL )
237 bkgd = (fscanf(lockfp,"%d %d", &pid, &st) == 2);
239 if (kill(pid, 0) == -1) {
240 fprintf(stderr,"fetchmail: removing stale lockfile\n");
248 /* if no mail servers listed and nothing in background, we're done */
249 if (!(quitmode && argc == 2) && pid == -1 && querylist == NULL) {
250 (void)fputs("fetchmail: no mailservers have been specified.\n",stderr);
254 /* perhaps user asked us to kill the other fetchmail */
259 fprintf(stderr,"fetchmail: no other fetchmail is running\n");
263 else if (kill(pid, SIGTERM) < 0)
265 fprintf(stderr,"fetchmail: error killing %s fetchmail at %d; bailing out.\n",
266 bkgd ? "background" : "foreground", pid);
271 fprintf(stderr,"fetchmail: %s fetchmail at %d killed.\n",
272 bkgd ? "background" : "foreground", pid);
281 /* another fetchmail is running -- wake it up or die */
287 "fetchmail: can't check mail while another fetchmail to same host is running.\n");
290 else if (!implicitmode)
293 "fetchmail: can't poll specified hosts with another fetchmail running at %d.\n",
300 "fetchmail: another foreground fetchmail is running at %d.\n",
307 "fetchmail: can't accept options while a background fetchmail is running.\n");
310 else if (kill(pid, SIGUSR1) == 0)
313 "fetchmail: background fetchmail at %d awakened.\n",
320 * Should never happen -- possible only if a background fetchmail
321 * croaks after the first kill probe above but before the
322 * SIGUSR1/SIGHUP transmission.
325 "fetchmail: elder sibling at %d died mysteriously.\n",
327 return(PS_UNDEFINED);
331 /* parse the ~/.netrc file (if present) for future password lookups. */
332 netrc_file = (char *) xmalloc (strlen (home) + 8);
333 strcpy (netrc_file, home);
334 strcat (netrc_file, "/.netrc");
335 netrc_list = parse_netrc(netrc_file);
337 /* pick up interactively any passwords we need but don't have */
338 for (ctl = querylist; ctl; ctl = ctl->next)
339 if (ctl->active && !(implicitmode && ctl->server.skip)&&!ctl->password)
341 if (ctl->server.preauthenticate == A_KERBEROS_V4 ||
342 ctl->server.preauthenticate == A_KERBEROS_V5 ||
343 ctl->server.protocol == P_IMAP_K4 ||
344 ctl->server.protocol == P_IMAP_GSS)
345 /* Server won't care what the password is, but there
346 must be some non-null string here. */
347 ctl->password = ctl->remotename;
350 /* look up the host and account in the .netrc file. */
351 netrc_entry *p = search_netrc(netrc_list,ctl->server.pollname);
352 while (p && strcmp(p->account, ctl->remotename))
353 p = search_netrc(p->next, ctl->remotename);
355 /* if we find a matching entry with a password, use it */
356 if (p && p->password)
357 ctl->password = xstrdup(p->password);
360 if (ctl->server.protocol != P_ETRN && ctl->server.protocol != P_IMAP_K4 && ctl->server.protocol != P_IMAP_GSS && !ctl->password)
363 #define PASSWORD_PROMPT "Enter password for %s@%s: "
364 tmpbuf = xmalloc(strlen(PASSWORD_PROMPT) +
365 strlen(ctl->remotename) +
366 strlen(ctl->server.pollname) + 1);
367 (void) sprintf(tmpbuf, PASSWORD_PROMPT,
368 ctl->remotename, ctl->server.pollname);
369 ctl->password = xstrdup((char *)getpassword(tmpbuf));
370 #undef PASSWORD_PROMPT
374 /* we don't need tmpbuf anymore */
378 * Maybe time to go to demon mode...
380 #if defined(HAVE_SYSLOG)
383 openlog(program_name, LOG_PID, LOG_MAIL);
388 error_init(run.poll_interval == 0 && !run.logfile);
390 if (run.poll_interval)
393 daemonize(run.logfile, termhook);
394 error( 0, 0, "starting fetchmail %s daemon ", RELEASE_ID);
397 * We'll set up a handler for these when we're sleeping,
398 * but ignore them otherwise so as not to interrupt a poll.
400 signal(SIGUSR1, SIG_IGN);
401 if (run.poll_interval && !getuid())
402 signal(SIGHUP, SIG_IGN);
405 /* beyond here we don't want more than one fetchmail running per user */
407 signal(SIGABRT, termhook);
408 signal(SIGINT, termhook);
409 signal(SIGTERM, termhook);
410 signal(SIGALRM, termhook);
411 signal(SIGPIPE, termhook);
412 signal(SIGQUIT, termhook);
414 /* here's the exclusion lock */
415 if ((lockfp = fopen(lockfile,"w")) != NULL) {
416 fprintf(lockfp,"%d",getpid());
417 if (run.poll_interval)
418 fprintf(lockfp," %d", run.poll_interval);
425 on_exit(unlockit, (char *)NULL);
430 * Query all hosts. If there's only one, the error return will
431 * reflect the status of that transaction.
434 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
436 * This was an efficiency hack that backfired. The theory
437 * was that using TCP/IP for DNS queries would get us better
438 * reliability and shave off some per-UDP-packet costs.
439 * Unfortunately it interacted badly with diald, which effectively
440 * filters out DNS queries over TCP/IP for reasons having to do
441 * with some obscure kernel problem involving bootstrapping of
442 * dynamically-addressed links. I don't understand this mess
443 * and don't want to, so it's "See ya!" to this hack.
445 sethostent(TRUE); /* use TCP/IP for mailserver queries */
446 #endif /* HAVE_RES_SEARCH */
449 for (ctl = querylist; ctl; ctl = ctl->next)
451 if (ctl->active && !(implicitmode && ctl->server.skip))
453 /* check skip interval first so that it counts all polls */
454 if (run.poll_interval && ctl->server.interval)
456 if (ctl->server.poll_count++ % ctl->server.interval)
458 if (outlevel == O_VERBOSE)
460 "fetchmail: interval not reached, not querying %s\n",
461 ctl->server.pollname);
466 #if defined(linux) && !INET6
467 /* interface_approve() does its own error logging */
468 if (!interface_approve(&ctl->server))
470 #endif /* defined(linux) && !INET6 */
472 #ifdef HAVE_GETHOSTBYNAME
474 * This functions partly as a probe to make sure our
475 * nameserver is still up. The multidrop case
476 * (especially) needs it.
478 if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
479 ctl->server.preauthenticate==A_KERBEROS_V5 ||
482 struct hostent *namerec;
484 /* compute the canonical name of the host */
486 namerec = gethostbyname(ctl->server.queryname);
487 if (namerec == (struct hostent *)NULL)
490 "skipping %s poll, ",
491 ctl->server.pollname);
494 if (errno == ENETUNREACH)
495 break; /* go to sleep */
497 #ifdef HAVE_HERROR /* NEXTSTEP doesn't */
500 #endif /* HAVE_HERROR */
505 free(ctl->server.truename);
506 ctl->server.truename=xstrdup((char *)namerec->h_name);
509 #endif /* HAVE_GETHOSTBYNAME */
511 querystatus = query_host(ctl);
513 if (querystatus == PS_SUCCESS) {
517 update_str_lists(ctl);
518 #endif /* POP3_ENABLE */
520 #if defined(linux) && !INET6
521 if (ctl->server.monitor)
523 /* Allow some time for the link to quiesce. One
524 * second is usually sufficient, three is safe.
525 * Note: this delay is important - don't remove!
528 interface_note_activity(&ctl->server);
530 #endif /* defined(linux) && !INET6 */
534 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
535 endhostent(); /* release TCP/IP connection to nameserver */
536 #endif /* HAVE_RES_SEARCH */
539 * Close all SMTP delivery sockets. For optimum performance
540 * we'd like to hold them open til end of run, but (1) this
541 * loses if our poll interval is longer than the MTA's inactivity
542 * timeout, and (2) some MTAs (like smail) don't deliver after
543 * each message, but rather queue up mail and wait to actually
544 * deliver it until the input socket is closed.
546 for (ctl = querylist; ctl; ctl = ctl->next)
547 if (ctl->smtp_socket != -1)
549 SMTP_quit(ctl->smtp_socket);
550 close(ctl->smtp_socket);
551 ctl->smtp_socket = -1;
555 * OK, we've polled. Now sleep.
557 if (run.poll_interval)
559 if (outlevel == O_VERBOSE)
564 fprintf(stderr, "fetchmail: sleeping at %s", ctime(&now));
568 * With this simple hack, we make it possible for a foreground
569 * fetchmail to wake up one in daemon mode. What we want is the
570 * side effect of interrupting any sleep that may be going on,
571 * forcing fetchmail to re-poll its hosts. The second line is
572 * for people who think all system daemons wake up on SIGHUP.
574 signal(SIGUSR1, donothing);
576 signal(SIGHUP, donothing);
579 * We can't use sleep(3) here because we need an alarm(3)
580 * equivalent in order to implement server nonresponse timeout.
581 * We'll just assume setitimer(2) is available since fetchmail
582 * has to have a BSDoid socket layer to work at all.
586 #ifdef SLEEP_WITH_ALARM /* not normally on */
588 * This code stopped working under glibc-2, apparently due
589 * to the change in signal(2) semantics. (The siginterrupt
590 * line, added later, should fix this problem.) John Stracke
591 * <francis@netscape.com> wrote:
593 * The problem seems to be that, after hitting the interval
594 * timer while talking to the server, the process no longer
595 * responds to SIGALRM. I put in printf()s to see when it
596 * reached the pause() for the poll interval, and I checked
597 * the return from setitimer(), and everything seemed to be
598 * working fine, except that the pause() just ignored SIGALRM.
599 * I thought maybe the itimer wasn't being fired, so I hit
600 * it with a SIGALRM from the command line, and it ignored
601 * that, too. SIGUSR1 woke it up just fine, and it proceeded
602 * to repoll--but, when the dummy server didn't respond, it
603 * never timed out, and SIGALRM wouldn't make it.
605 * (continued below...)
607 struct itimerval ntimeout;
609 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
610 ntimeout.it_value.tv_sec = poll_interval;
611 ntimeout.it_value.tv_usec = 0;
613 siginterrupt(SIGALRM, 1);
615 signal(SIGALRM, gotsigalrm); /* first trap signals */
616 setitimer(ITIMER_REAL,&ntimeout,NULL); /* then start timer */
617 /* there is a very small window between the next two lines */
620 signal(SIGALRM, SIG_IGN);
623 * So the workaround I used is to make it sleep by using
624 * select() instead of setitimer()/pause(). select() is
625 * perfectly happy being called with a timeout and
626 * no file descriptors; it just sleeps until it hits the
627 * timeout. The only concern I had was that it might
628 * implement its timeout with SIGALRM--there are some
629 * Unices where this is done, because select() is a library
630 * function--but apparently not.
632 struct timeval timeout;
634 timeout.tv_sec = run.poll_interval;
637 select(0,0,0,0, &timeout);
641 signal(SIGALRM, gotsigalrm);
642 _beginthread(itimerthread, NULL, 32768, NULL);
643 /* see similar code above */
646 signal(SIGALRM, SIG_IGN);
648 if (lastsig == SIGUSR1
649 || ((run.poll_interval && !getuid()) && lastsig == SIGHUP))
651 #ifdef SYS_SIGLIST_DECLARED
652 error(0, 0, "awakened by %s", sys_siglist[lastsig]);
654 error(0, 0, "awakened by signal %d", lastsig);
659 /* now lock out interrupts again */
660 signal(SIGUSR1, SIG_IGN);
662 signal(SIGHUP, SIG_IGN);
664 if (outlevel == O_VERBOSE)
669 fprintf(stderr, "fetchmail: awakened at %s", ctime(&now));
675 if (outlevel == O_VERBOSE)
676 fprintf(stderr,"fetchmail: normal termination, status %d\n",
677 successes ? PS_SUCCESS : querystatus);
680 exit(successes ? PS_SUCCESS : querystatus);
683 static int load_params(int argc, char **argv, int optind)
685 int implicitmode, st;
687 struct query def_opts, *ctl;
689 memset(&def_opts, '\0', sizeof(struct query));
690 def_opts.smtp_socket = -1;
691 def_opts.smtpaddress = (char *)0;
692 def_opts.antispam = 571;
694 def_opts.server.protocol = P_AUTO;
695 def_opts.server.timeout = CLIENT_TIMEOUT;
696 def_opts.remotename = user;
697 def_opts.expunge = 1;
699 /* this builds the host list */
700 if (prc_parse_file(rcfile, !versioninfo) != 0)
703 if ((implicitmode = (optind >= argc)))
705 for (ctl = querylist; ctl; ctl = ctl->next)
709 for (; optind < argc; optind++)
712 * If hostname corresponds to a host known from the rc file,
713 * simply declare it active. Otherwise synthesize a host
714 * record from command line and defaults
716 for (ctl = querylist; ctl; ctl = ctl->next)
717 if (!strcmp(ctl->server.pollname, argv[optind])
718 || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
722 * Allocate and link record without copying in command-line args;
723 * we'll do that with the optmerge call later on.
725 ctl = hostalloc((struct query *)NULL);
727 ctl->server.pollname = xstrdup(argv[optind]);
734 * If there's a defaults record, merge it and lose it.
736 if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
738 for (ctl = querylist->next; ctl; ctl = ctl->next)
739 optmerge(ctl, querylist);
740 querylist = querylist->next;
743 /* don't allow a defaults record after the first */
744 for (ctl = querylist; ctl; ctl = ctl->next)
745 if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
748 /* merge in wired defaults, do sanity checks and prepare internal fields */
749 for (ctl = querylist; ctl; ctl = ctl->next)
751 if (ctl->active && !(implicitmode && ctl->server.skip))
753 /* merge in defaults */
754 optmerge(ctl, &def_opts);
756 /* make sure we have a nonempty host list to forward to */
758 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
760 /* keep lusers from shooting themselves in the foot :-) */
761 if (run.poll_interval && ctl->limit)
763 fprintf(stderr,"fetchmail: you'd never see large messages!\n");
767 /* if `user' doesn't name a real local user, try to run as root */
768 if ((pw = getpwnam(user)) == (struct passwd *)NULL)
771 ctl->uid = pw->pw_uid; /* for local delivery via MDA */
772 if (!ctl->localnames) /* for local delivery via SMTP */
773 save_str_pair(&ctl->localnames, user, NULL);
775 /* this code enables flags to be turned off */
776 #define DEFAULT(flag, dflt) if (flag == FLAG_TRUE)\
778 else if (flag == FLAG_FALSE)\
782 DEFAULT(ctl->keep, FALSE);
783 DEFAULT(ctl->fetchall, FALSE);
784 DEFAULT(ctl->flush, FALSE);
785 DEFAULT(ctl->rewrite, TRUE);
786 DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL));
787 DEFAULT(ctl->forcecr, FALSE);
788 DEFAULT(ctl->pass8bits, FALSE);
789 DEFAULT(ctl->dropstatus, FALSE);
790 DEFAULT(ctl->mimedecode, FALSE);
791 DEFAULT(ctl->server.dns, TRUE);
792 DEFAULT(ctl->server.uidl, FALSE);
795 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
796 /* can't handle multidrop mailboxes unless we can do DNS lookups */
797 if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
799 ctl->server.dns = FALSE;
800 fprintf(stderr, "fetchmail: warning: no DNS available to check multidrop fetches from %s\n", ctl->server.pollname);
802 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
806 * Compute the true name of the mailserver host.
807 * There are two clashing cases here:
809 * (1) The poll name is a label, possibly on one of several
810 * poll configurations for the same host. In this case
811 * the `via' option will be present and give the true name.
813 * (2) The poll name is the true one, the via name is
814 * localhost. This is going to be typical for ssh-using
817 * We're going to assume the via name is true unless it's
820 * Each poll cycle, if we've got DNS, we'll try to canonicalize
821 * the name. This will function as a probe to ensure the
822 * host's nameserver is up.
824 if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
825 ctl->server.queryname = xstrdup(ctl->server.via);
827 ctl->server.queryname = xstrdup(ctl->server.pollname);
828 ctl->server.truename = xstrdup(ctl->server.queryname);
830 /* if no folders were specified, set up the null one as default */
832 save_str(&ctl->mailboxes, (char *)NULL, 0);
834 /* maybe user overrode timeout on command line? */
835 if (ctl->server.timeout == -1)
836 ctl->server.timeout = CLIENT_TIMEOUT;
840 if (ctl->server.port < 0)
842 (void) fprintf(stderr,
843 "%s configuration invalid, port number cannot be negative",
844 ctl->server.pollname);
847 if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
849 (void) fprintf(stderr,
850 "%s configuration invalid, RPOP requires a privileged port",
851 ctl->server.pollname);
858 /* initialize UID handling */
859 if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
863 initialize_saved_lists(querylist, run.idfile);
864 #endif /* POP3_ENABLE */
866 /* here's where we override globals */
868 run.logfile = cmd_run.logfile;
870 run.logfile = cmd_run.idfile;
871 if (cmd_run.poll_interval >= 0)
872 run.poll_interval = cmd_run.poll_interval;
873 if (cmd_run.invisible)
874 run.invisible = cmd_run.invisible;
875 if (cmd_run.use_syslog)
876 run.use_syslog = cmd_run.use_syslog;
878 /* check and daemon options are not compatible */
879 if (check_only && run.poll_interval)
880 run.poll_interval = 0;
881 return(implicitmode);
884 void termhook(int sig)
885 /* to be executed on normal or signal-induced termination */
890 * Sending SMTP QUIT on signal is theoretically nice, but led to a
891 * subtle bug. If fetchmail was terminated by signal while it was
892 * shipping message text, it would hang forever waiting for a
893 * command acknowledge. In theory we could enable the QUIT
894 * only outside of the message send. In practice, we don't
895 * care. All mailservers hang up on a dropped TCP/IP connection
900 error(0, 0, "terminated with signal %d", sig);
902 /* terminate all SMTP connections cleanly */
903 for (ctl = querylist; ctl; ctl = ctl->next)
904 if (ctl->smtp_socket != -1)
905 SMTP_quit(ctl->smtp_socket);
909 write_saved_lists(querylist, run.idfile);
910 #endif /* POP3_ENABLE */
913 * Craig Metz, the RFC1938 one-time-password guy, points out:
914 * "Remember that most kernels don't zero pages before handing them to the
915 * next process and many kernels share pages between user and kernel space.
916 * You'd be very surprised what you can find from a short program to do a
917 * malloc() and then dump the contents of the pages you got. By zeroing
918 * the secrets at end of run (earlier if you can), you make sure the next
919 * guy can't get the password/pass phrase."
921 * Right you are, Craig!
923 for (ctl = querylist; ctl; ctl = ctl->next)
925 memset(ctl->password, '\0', strlen(ctl->password));
927 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
931 exit(successes ? PS_SUCCESS : querystatus);
935 * Sequence of protocols to try when autoprobing, most capable to least.
937 static const int autoprobe[] =
941 #endif /* IMAP_ENABLE */
944 #endif /* POP3_ENABLE */
947 #endif /* POP2_ENABLE */
950 static int query_host(struct query *ctl)
951 /* perform fetch transaction with single host */
955 if (outlevel == O_VERBOSE)
960 fprintf(stderr, "fetchmail: %s querying %s (protocol %s) at %s",
962 ctl->server.pollname, showproto(ctl->server.protocol), ctime(&now));
964 switch (ctl->server.protocol) {
966 for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
968 ctl->server.protocol = autoprobe[i];
969 if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
972 ctl->server.protocol = P_AUTO;
979 fprintf(stderr, "POP2 support is not configured.\n");
981 #endif /* POP2_ENABLE */
989 fprintf(stderr, "POP3 support is not configured.\n");
991 #endif /* POP3_ENABLE */
999 fprintf(stderr, "IMAP support is not configured.\n");
1000 return(PS_PROTOCOL);
1001 #endif /* IMAP_ENABLE */
1005 fprintf(stderr, "ETRN support is not configured.\n");
1006 return(PS_PROTOCOL);
1008 #ifdef HAVE_GETHOSTBYNAME
1009 return(doETRN(ctl));
1011 fprintf(stderr, "Cannot support ETRN without gethostbyname(2).\n");
1012 return(PS_PROTOCOL);
1013 #endif /* HAVE_GETHOSTBYNAME */
1014 #endif /* ETRN_ENABLE */
1016 error(0, 0, "unsupported protocol selected.");
1017 return(PS_PROTOCOL);
1021 void dump_params (struct runctl *runp, struct query *querylist, flag implicit)
1022 /* display query parameters in English */
1026 if (runp->poll_interval)
1027 printf("Poll interval is %d seconds\n", runp->poll_interval);
1029 printf("Logfile is %s\n", runp->logfile);
1030 if (strcmp(runp->idfile, IDFILE_NAME))
1031 printf("Idfile is %s\n", runp->idfile);
1032 #if defined(HAVE_SYSLOG)
1033 if (runp->use_syslog)
1034 printf("Progress messages will be logged via syslog\n");
1036 if (runp->invisible)
1037 printf("Fetchmail will masquerade and will not generate Received\n");
1039 for (ctl = querylist; ctl; ctl = ctl->next)
1041 if (!ctl->active || (implicit && ctl->server.skip))
1044 printf("Options for retrieving from %s@%s:\n",
1045 ctl->remotename, visbuf(ctl->server.pollname));
1047 if (ctl->server.via)
1048 printf(" Mail will be retrieved via %s\n", ctl->server.via);
1050 if (ctl->server.interval)
1051 printf(" Poll of this server will occur every %d intervals.\n",
1052 ctl->server.interval);
1053 if (ctl->server.truename)
1054 printf(" True name of server is %s.\n", ctl->server.truename);
1055 if (ctl->server.skip || outlevel == O_VERBOSE)
1056 printf(" This host will%s be queried when no host is specified.\n",
1057 ctl->server.skip ? " not" : "");
1059 printf(" Password will be prompted for.\n");
1060 else if (outlevel == O_VERBOSE)
1061 if (ctl->server.protocol == P_APOP)
1062 printf(" APOP secret = '%s'.\n", visbuf(ctl->password));
1063 else if (ctl->server.protocol == P_RPOP)
1064 printf(" RPOP id = '%s'.\n", visbuf(ctl->password));
1066 printf(" Password = '%s'.\n", visbuf(ctl->password));
1067 if (ctl->server.protocol == P_POP3
1069 && !strcmp(ctl->server.service, KPOP_PORT)
1071 && ctl->server.port == KPOP_PORT
1073 && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1074 ctl->server.preauthenticate == A_KERBEROS_V5))
1075 printf(" Protocol is KPOP");
1077 printf(" Protocol is %s", showproto(ctl->server.protocol));
1079 if (ctl->server.service)
1080 printf(" (using service %s)", ctl->server.service);
1081 if (ctl->server.netsec)
1082 printf(" (using network security options %s)", ctl->server.netsec);
1084 if (ctl->server.port)
1085 printf(" (using port %d)", ctl->server.port);
1087 else if (outlevel == O_VERBOSE)
1088 printf(" (using default port)");
1089 if (ctl->server.uidl)
1090 printf(" (forcing UIDL use)");
1093 if (ctl->server.preauthenticate == A_KERBEROS_V4)
1094 printf(" Kerberos V4 preauthentication enabled.\n");
1095 if (ctl->server.preauthenticate == A_KERBEROS_V5)
1096 printf(" Kerberos V5 preauthentication enabled.\n");
1097 if (ctl->server.timeout > 0)
1098 printf(" Server nonresponse timeout is %d seconds", ctl->server.timeout);
1099 if (ctl->server.timeout == CLIENT_TIMEOUT)
1100 printf(" (default).\n");
1104 if (!ctl->mailboxes->id)
1105 printf(" Default mailbox selected.\n");
1110 printf(" Selected mailboxes are:");
1111 for (idp = ctl->mailboxes; idp; idp = idp->next)
1112 printf(" %s", idp->id);
1115 printf(" %s messages will be retrieved (--all %s).\n",
1116 ctl->fetchall ? "All" : "Only new",
1117 ctl->fetchall ? "on" : "off");
1118 printf(" Fetched messages will%s be kept on the server (--keep %s).\n",
1119 ctl->keep ? "" : " not",
1120 ctl->keep ? "on" : "off");
1121 printf(" Old messages will%s be flushed before message retrieval (--flush %s).\n",
1122 ctl->flush ? "" : " not",
1123 ctl->flush ? "on" : "off");
1124 printf(" Rewrite of server-local addresses is %sabled (--norewrite %s).\n",
1125 ctl->rewrite ? "en" : "dis",
1126 ctl->rewrite ? "off" : "on");
1127 printf(" Carriage-return stripping is %sabled (stripcr %s).\n",
1128 ctl->stripcr ? "en" : "dis",
1129 ctl->stripcr ? "on" : "off");
1130 printf(" Carriage-return forcing is %sabled (forcecr %s).\n",
1131 ctl->forcecr ? "en" : "dis",
1132 ctl->forcecr ? "on" : "off");
1133 printf(" Interpretation of Content-Transfer-Encoding is %sabled (pass8bits %s).\n",
1134 ctl->pass8bits ? "dis" : "en",
1135 ctl->pass8bits ? "on" : "off");
1136 printf(" MIME decoding is %sabled (mimedecode %s).\n",
1137 ctl->mimedecode ? "dis" : "en",
1138 ctl->mimedecode ? "on" : "off");
1139 printf(" Nonempty Status lines will be %s (dropstatus %s)\n",
1140 ctl->dropstatus ? "discarded" : "kept",
1141 ctl->dropstatus ? "on" : "off");
1142 if (NUM_NONZERO(ctl->limit))
1143 printf(" Message size limit is %d bytes (--limit %d).\n",
1144 ctl->limit, ctl->limit);
1145 else if (outlevel == O_VERBOSE)
1146 printf(" No message size limit (--limit 0).\n");
1147 if (NUM_NONZERO(ctl->fetchlimit))
1148 printf(" Received-message limit is %d (--fetchlimit %d).\n",
1149 ctl->fetchlimit, ctl->fetchlimit);
1150 else if (outlevel == O_VERBOSE)
1151 printf(" No received-message limit (--fetchlimit 0).\n");
1152 if (NUM_NONZERO(ctl->batchlimit))
1153 printf(" SMTP message batch limit is %d.\n", ctl->batchlimit);
1154 else if (outlevel == O_VERBOSE)
1155 printf(" No SMTP message batch limit (--batchlimit 0).\n");
1156 if (ctl->server.protocol == P_IMAP)
1157 if (NUM_NONZERO(ctl->expunge))
1158 printf(" Deletion interval between expunges is %d (--expunge %d).\n", ctl->expunge, ctl->expunge);
1159 else if (outlevel == O_VERBOSE)
1160 printf(" No expunges (--expunge 0).\n");
1162 printf(" Messages will be delivered with '%s.'\n", visbuf(ctl->mda));
1167 printf(" Messages will be SMTP-forwarded to:");
1168 for (idp = ctl->smtphunt; idp; idp = idp->next)
1169 if (ctl->server.protocol != P_ETRN || idp->val.status.mark)
1171 printf(" %s", idp->id);
1172 if (!idp->val.status.mark)
1173 printf(" (default)");
1176 if (ctl->smtpaddress)
1177 printf(" Host part of MAIL FROM line will be %s\n",
1180 if (ctl->antispam != -1)
1181 printf(" Listener SMTP reponse %d will be treated as a spam block\n",
1183 else if (outlevel == O_VERBOSE)
1184 printf(" Spam-blocking disabled\n");
1185 if (ctl->preconnect)
1186 printf(" Server connection will be brought up with '%s.'\n",
1187 visbuf(ctl->preconnect));
1188 else if (outlevel == O_VERBOSE)
1189 printf(" No pre-connection command.\n");
1190 if (ctl->postconnect)
1191 printf(" Server connection will be taken down with '%s.'\n",
1192 visbuf(ctl->postconnect));
1193 else if (outlevel == O_VERBOSE)
1194 printf(" No post-connection command.\n");
1195 if (!ctl->localnames)
1196 printf(" No localnames declared for this host.\n");
1202 for (idp = ctl->localnames; idp; idp = idp->next)
1205 if (count > 1 || ctl->wildcard)
1206 printf(" Multi-drop mode: ");
1208 printf(" Single-drop mode: ");
1210 printf("%d local name(s) recognized.\n", count);
1211 if (outlevel == O_VERBOSE)
1213 for (idp = ctl->localnames; idp; idp = idp->next)
1215 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1217 printf("\t%s\n", idp->id);
1219 fputs("*\n", stdout);
1222 if (count > 1 || ctl->wildcard)
1224 printf(" DNS lookup for multidrop addresses is %sabled.\n",
1225 ctl->server.dns ? "en" : "dis");
1227 if (ctl->server.envelope == STRING_DISABLED)
1228 printf(" Envelope-address routing is disabled\n");
1231 printf(" Envelope header is assumed to be: %s\n",
1232 ctl->server.envelope ? ctl->server.envelope:"Received");
1233 if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1234 printf(" Number of envelope header to be parsed: %d\n",
1235 ctl->server.envskip);
1236 if (ctl->server.qvirtual)
1237 printf(" Prefix %s will be removed from user id\n",
1238 ctl->server.qvirtual);
1239 else if (outlevel >= O_VERBOSE)
1240 printf(" No prefix stripping\n");
1243 if (ctl->server.akalist)
1247 printf(" Predeclared mailserver aliases:");
1248 for (idp = ctl->server.akalist; idp; idp = idp->next)
1249 printf(" %s", idp->id);
1252 if (ctl->server.localdomains)
1256 printf(" Local domains:");
1257 for (idp = ctl->server.localdomains; idp; idp = idp->next)
1258 printf(" %s", idp->id);
1264 if (ctl->server.interface)
1265 printf(" Connection must be through interface %s.\n", ctl->server.interface);
1266 else if (outlevel == O_VERBOSE)
1267 printf(" No interface requirement specified.\n");
1268 if (ctl->server.monitor)
1269 printf(" Polling loop will monitor %s.\n", ctl->server.monitor);
1270 else if (outlevel == O_VERBOSE)
1271 printf(" No monitor interface specified.\n");
1274 if (ctl->server.protocol > P_POP2)
1276 printf(" No UIDs saved from this host.\n");
1282 for (idp = ctl->oldsaved; idp; idp = idp->next)
1285 printf(" %d UIDs saved.\n", count);
1286 if (outlevel == O_VERBOSE)
1287 for (idp = ctl->oldsaved; idp; idp = idp->next)
1288 fprintf(stderr, "\t%s\n", idp->id);
1293 /* fetchmail.c ends here */