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 */
376 tmpbuf = NULL; /* firewall code */
379 * Maybe time to go to demon mode...
381 #if defined(HAVE_SYSLOG)
384 openlog(program_name, LOG_PID, LOG_MAIL);
389 error_init(run.poll_interval == 0 && !run.logfile);
391 if (run.poll_interval)
394 daemonize(run.logfile, termhook);
395 error( 0, 0, "starting fetchmail %s daemon ", RELEASE_ID);
398 * We'll set up a handler for these when we're sleeping,
399 * but ignore them otherwise so as not to interrupt a poll.
401 signal(SIGUSR1, SIG_IGN);
402 if (run.poll_interval && !getuid())
403 signal(SIGHUP, SIG_IGN);
406 /* beyond here we don't want more than one fetchmail running per user */
408 signal(SIGABRT, termhook);
409 signal(SIGINT, termhook);
410 signal(SIGTERM, termhook);
411 signal(SIGALRM, termhook);
412 signal(SIGPIPE, termhook);
413 signal(SIGQUIT, termhook);
415 /* here's the exclusion lock */
416 if ((lockfp = fopen(lockfile,"w")) != NULL) {
417 fprintf(lockfp,"%d",getpid());
418 if (run.poll_interval)
419 fprintf(lockfp," %d", run.poll_interval);
426 on_exit(unlockit, (char *)NULL);
431 * Query all hosts. If there's only one, the error return will
432 * reflect the status of that transaction.
435 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
437 * This was an efficiency hack that backfired. The theory
438 * was that using TCP/IP for DNS queries would get us better
439 * reliability and shave off some per-UDP-packet costs.
440 * Unfortunately it interacted badly with diald, which effectively
441 * filters out DNS queries over TCP/IP for reasons having to do
442 * with some obscure kernel problem involving bootstrapping of
443 * dynamically-addressed links. I don't understand this mess
444 * and don't want to, so it's "See ya!" to this hack.
446 sethostent(TRUE); /* use TCP/IP for mailserver queries */
447 #endif /* HAVE_RES_SEARCH */
450 for (ctl = querylist; ctl; ctl = ctl->next)
452 if (ctl->active && !(implicitmode && ctl->server.skip))
454 /* check skip interval first so that it counts all polls */
455 if (run.poll_interval && ctl->server.interval)
457 if (ctl->server.poll_count++ % ctl->server.interval)
459 if (outlevel == O_VERBOSE)
461 "fetchmail: interval not reached, not querying %s\n",
462 ctl->server.pollname);
467 #if defined(linux) && !INET6
468 /* interface_approve() does its own error logging */
469 if (!interface_approve(&ctl->server))
471 #endif /* defined(linux) && !INET6 */
473 #ifdef HAVE_GETHOSTBYNAME
475 * This functions partly as a probe to make sure our
476 * nameserver is still up. The multidrop case
477 * (especially) needs it.
479 if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
480 ctl->server.preauthenticate==A_KERBEROS_V5 ||
483 struct hostent *namerec;
485 /* compute the canonical name of the host */
487 namerec = gethostbyname(ctl->server.queryname);
488 if (namerec == (struct hostent *)NULL)
491 "skipping %s poll, ",
492 ctl->server.pollname);
495 if (errno == ENETUNREACH)
496 break; /* go to sleep */
498 #ifdef HAVE_HERROR /* NEXTSTEP doesn't */
501 #endif /* HAVE_HERROR */
506 free(ctl->server.truename);
507 ctl->server.truename=xstrdup((char *)namerec->h_name);
510 #endif /* HAVE_GETHOSTBYNAME */
512 querystatus = query_host(ctl);
514 if (querystatus == PS_SUCCESS) {
518 update_str_lists(ctl);
519 #endif /* POP3_ENABLE */
521 #if defined(linux) && !INET6
522 if (ctl->server.monitor)
524 /* Allow some time for the link to quiesce. One
525 * second is usually sufficient, three is safe.
526 * Note: this delay is important - don't remove!
529 interface_note_activity(&ctl->server);
531 #endif /* defined(linux) && !INET6 */
535 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
536 endhostent(); /* release TCP/IP connection to nameserver */
537 #endif /* HAVE_RES_SEARCH */
540 * Close all SMTP delivery sockets. For optimum performance
541 * we'd like to hold them open til end of run, but (1) this
542 * loses if our poll interval is longer than the MTA's inactivity
543 * timeout, and (2) some MTAs (like smail) don't deliver after
544 * each message, but rather queue up mail and wait to actually
545 * deliver it until the input socket is closed.
547 for (ctl = querylist; ctl; ctl = ctl->next)
548 if (ctl->smtp_socket != -1)
550 SMTP_quit(ctl->smtp_socket);
551 close(ctl->smtp_socket);
552 ctl->smtp_socket = -1;
556 * OK, we've polled. Now sleep.
558 if (run.poll_interval)
560 if (outlevel == O_VERBOSE)
565 fprintf(stderr, "fetchmail: sleeping at %s", ctime(&now));
569 * With this simple hack, we make it possible for a foreground
570 * fetchmail to wake up one in daemon mode. What we want is the
571 * side effect of interrupting any sleep that may be going on,
572 * forcing fetchmail to re-poll its hosts. The second line is
573 * for people who think all system daemons wake up on SIGHUP.
575 signal(SIGUSR1, donothing);
577 signal(SIGHUP, donothing);
580 * We can't use sleep(3) here because we need an alarm(3)
581 * equivalent in order to implement server nonresponse timeout.
582 * We'll just assume setitimer(2) is available since fetchmail
583 * has to have a BSDoid socket layer to work at all.
587 #ifdef SLEEP_WITH_ALARM /* not normally on */
589 * This code stopped working under glibc-2, apparently due
590 * to the change in signal(2) semantics. (The siginterrupt
591 * line, added later, should fix this problem.) John Stracke
592 * <francis@netscape.com> wrote:
594 * The problem seems to be that, after hitting the interval
595 * timer while talking to the server, the process no longer
596 * responds to SIGALRM. I put in printf()s to see when it
597 * reached the pause() for the poll interval, and I checked
598 * the return from setitimer(), and everything seemed to be
599 * working fine, except that the pause() just ignored SIGALRM.
600 * I thought maybe the itimer wasn't being fired, so I hit
601 * it with a SIGALRM from the command line, and it ignored
602 * that, too. SIGUSR1 woke it up just fine, and it proceeded
603 * to repoll--but, when the dummy server didn't respond, it
604 * never timed out, and SIGALRM wouldn't make it.
606 * (continued below...)
608 struct itimerval ntimeout;
610 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
611 ntimeout.it_value.tv_sec = poll_interval;
612 ntimeout.it_value.tv_usec = 0;
614 siginterrupt(SIGALRM, 1);
616 signal(SIGALRM, gotsigalrm); /* first trap signals */
617 setitimer(ITIMER_REAL,&ntimeout,NULL); /* then start timer */
618 /* there is a very small window between the next two lines */
621 signal(SIGALRM, SIG_IGN);
624 * So the workaround I used is to make it sleep by using
625 * select() instead of setitimer()/pause(). select() is
626 * perfectly happy being called with a timeout and
627 * no file descriptors; it just sleeps until it hits the
628 * timeout. The only concern I had was that it might
629 * implement its timeout with SIGALRM--there are some
630 * Unices where this is done, because select() is a library
631 * function--but apparently not.
633 struct timeval timeout;
635 timeout.tv_sec = run.poll_interval;
638 select(0,0,0,0, &timeout);
642 signal(SIGALRM, gotsigalrm);
643 _beginthread(itimerthread, NULL, 32768, NULL);
644 /* see similar code above */
647 signal(SIGALRM, SIG_IGN);
649 if (lastsig == SIGUSR1
650 || ((run.poll_interval && !getuid()) && lastsig == SIGHUP))
652 #ifdef SYS_SIGLIST_DECLARED
653 error(0, 0, "awakened by %s", sys_siglist[lastsig]);
655 error(0, 0, "awakened by signal %d", lastsig);
660 /* now lock out interrupts again */
661 signal(SIGUSR1, SIG_IGN);
663 signal(SIGHUP, SIG_IGN);
665 if (outlevel == O_VERBOSE)
670 fprintf(stderr, "fetchmail: awakened at %s", ctime(&now));
676 if (outlevel == O_VERBOSE)
677 fprintf(stderr,"fetchmail: normal termination, status %d\n",
678 successes ? PS_SUCCESS : querystatus);
681 exit(successes ? PS_SUCCESS : querystatus);
684 static int load_params(int argc, char **argv, int optind)
686 int implicitmode, st;
688 struct query def_opts, *ctl;
690 memset(&def_opts, '\0', sizeof(struct query));
691 def_opts.smtp_socket = -1;
692 def_opts.smtpaddress = (char *)0;
693 def_opts.antispam = 571;
695 def_opts.server.protocol = P_AUTO;
696 def_opts.server.timeout = CLIENT_TIMEOUT;
697 def_opts.remotename = user;
698 def_opts.expunge = 1;
700 /* this builds the host list */
701 if (prc_parse_file(rcfile, !versioninfo) != 0)
704 if ((implicitmode = (optind >= argc)))
706 for (ctl = querylist; ctl; ctl = ctl->next)
710 for (; optind < argc; optind++)
713 * If hostname corresponds to a host known from the rc file,
714 * simply declare it active. Otherwise synthesize a host
715 * record from command line and defaults
717 for (ctl = querylist; ctl; ctl = ctl->next)
718 if (!strcmp(ctl->server.pollname, argv[optind])
719 || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
723 * Allocate and link record without copying in command-line args;
724 * we'll do that with the optmerge call later on.
726 ctl = hostalloc((struct query *)NULL);
728 ctl->server.pollname = xstrdup(argv[optind]);
735 * If there's a defaults record, merge it and lose it.
737 if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
739 for (ctl = querylist->next; ctl; ctl = ctl->next)
740 optmerge(ctl, querylist);
741 querylist = querylist->next;
744 /* don't allow a defaults record after the first */
745 for (ctl = querylist; ctl; ctl = ctl->next)
746 if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
749 /* merge in wired defaults, do sanity checks and prepare internal fields */
750 for (ctl = querylist; ctl; ctl = ctl->next)
752 if (configdump || (ctl->active && !(implicitmode && ctl->server.skip)))
754 /* merge in defaults */
755 optmerge(ctl, &def_opts);
757 /* make sure we have a nonempty host list to forward to */
760 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
761 /* for non ETRN try to deliver mails to localhost if
762 * fetchmailhost fails
764 if (ctl->server.protocol != P_ETRN) {
765 save_str(&ctl->smtphunt, "localhost", FALSE);
769 /* keep lusers from shooting themselves in the foot :-) */
770 if (run.poll_interval && ctl->limit)
772 fprintf(stderr,"fetchmail: you'd never see large messages!\n");
776 /* if `user' doesn't name a real local user, try to run as root */
777 if ((pw = getpwnam(user)) == (struct passwd *)NULL)
780 ctl->uid = pw->pw_uid; /* for local delivery via MDA */
781 if (!ctl->localnames) /* for local delivery via SMTP */
782 save_str_pair(&ctl->localnames, user, NULL);
784 /* this code enables flags to be turned off */
785 #define DEFAULT(flag, dflt) if (flag == FLAG_TRUE)\
787 else if (flag == FLAG_FALSE)\
791 DEFAULT(ctl->keep, FALSE);
792 DEFAULT(ctl->fetchall, FALSE);
793 DEFAULT(ctl->flush, FALSE);
794 DEFAULT(ctl->rewrite, TRUE);
795 DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL));
796 DEFAULT(ctl->forcecr, FALSE);
797 DEFAULT(ctl->pass8bits, FALSE);
798 DEFAULT(ctl->dropstatus, FALSE);
799 DEFAULT(ctl->mimedecode, FALSE);
800 DEFAULT(ctl->server.dns, TRUE);
801 DEFAULT(ctl->server.uidl, FALSE);
804 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
805 /* can't handle multidrop mailboxes unless we can do DNS lookups */
806 if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
808 ctl->server.dns = FALSE;
809 fprintf(stderr, "fetchmail: warning: no DNS available to check multidrop fetches from %s\n", ctl->server.pollname);
811 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
815 * Compute the true name of the mailserver host.
816 * There are two clashing cases here:
818 * (1) The poll name is a label, possibly on one of several
819 * poll configurations for the same host. In this case
820 * the `via' option will be present and give the true name.
822 * (2) The poll name is the true one, the via name is
823 * localhost. This is going to be typical for ssh-using
826 * We're going to assume the via name is true unless it's
829 * Each poll cycle, if we've got DNS, we'll try to canonicalize
830 * the name. This will function as a probe to ensure the
831 * host's nameserver is up.
833 if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
834 ctl->server.queryname = xstrdup(ctl->server.via);
836 ctl->server.queryname = xstrdup(ctl->server.pollname);
837 ctl->server.truename = xstrdup(ctl->server.queryname);
839 /* if no folders were specified, set up the null one as default */
841 save_str(&ctl->mailboxes, (char *)NULL, 0);
843 /* maybe user overrode timeout on command line? */
844 if (ctl->server.timeout == -1)
845 ctl->server.timeout = CLIENT_TIMEOUT;
849 if (ctl->server.port < 0)
851 (void) fprintf(stderr,
852 "%s configuration invalid, port number cannot be negative",
853 ctl->server.pollname);
856 if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
858 (void) fprintf(stderr,
859 "%s configuration invalid, RPOP requires a privileged port",
860 ctl->server.pollname);
867 /* initialize UID handling */
868 if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
872 initialize_saved_lists(querylist, run.idfile);
873 #endif /* POP3_ENABLE */
875 /* here's where we override globals */
877 run.logfile = cmd_run.logfile;
879 run.logfile = cmd_run.idfile;
880 if (cmd_run.poll_interval >= 0)
881 run.poll_interval = cmd_run.poll_interval;
882 if (cmd_run.invisible)
883 run.invisible = cmd_run.invisible;
884 if (cmd_run.use_syslog)
885 run.use_syslog = cmd_run.use_syslog;
887 /* check and daemon options are not compatible */
888 if (check_only && run.poll_interval)
889 run.poll_interval = 0;
890 return(implicitmode);
893 void termhook(int sig)
894 /* to be executed on normal or signal-induced termination */
899 * Sending SMTP QUIT on signal is theoretically nice, but led to a
900 * subtle bug. If fetchmail was terminated by signal while it was
901 * shipping message text, it would hang forever waiting for a
902 * command acknowledge. In theory we could enable the QUIT
903 * only outside of the message send. In practice, we don't
904 * care. All mailservers hang up on a dropped TCP/IP connection
909 error(0, 0, "terminated with signal %d", sig);
911 /* terminate all SMTP connections cleanly */
912 for (ctl = querylist; ctl; ctl = ctl->next)
913 if (ctl->smtp_socket != -1)
914 SMTP_quit(ctl->smtp_socket);
918 write_saved_lists(querylist, run.idfile);
919 #endif /* POP3_ENABLE */
922 * Craig Metz, the RFC1938 one-time-password guy, points out:
923 * "Remember that most kernels don't zero pages before handing them to the
924 * next process and many kernels share pages between user and kernel space.
925 * You'd be very surprised what you can find from a short program to do a
926 * malloc() and then dump the contents of the pages you got. By zeroing
927 * the secrets at end of run (earlier if you can), you make sure the next
928 * guy can't get the password/pass phrase."
930 * Right you are, Craig!
932 for (ctl = querylist; ctl; ctl = ctl->next)
934 memset(ctl->password, '\0', strlen(ctl->password));
936 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
940 exit(successes ? PS_SUCCESS : querystatus);
944 * Sequence of protocols to try when autoprobing, most capable to least.
946 static const int autoprobe[] =
950 #endif /* IMAP_ENABLE */
953 #endif /* POP3_ENABLE */
956 #endif /* POP2_ENABLE */
959 static int query_host(struct query *ctl)
960 /* perform fetch transaction with single host */
964 if (outlevel == O_VERBOSE)
969 fprintf(stderr, "fetchmail: %s querying %s (protocol %s) at %s",
971 ctl->server.pollname, showproto(ctl->server.protocol), ctime(&now));
973 switch (ctl->server.protocol) {
975 for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
977 ctl->server.protocol = autoprobe[i];
978 if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
981 ctl->server.protocol = P_AUTO;
988 fprintf(stderr, "POP2 support is not configured.\n");
990 #endif /* POP2_ENABLE */
998 fprintf(stderr, "POP3 support is not configured.\n");
1000 #endif /* POP3_ENABLE */
1006 return(doIMAP(ctl));
1008 fprintf(stderr, "IMAP support is not configured.\n");
1009 return(PS_PROTOCOL);
1010 #endif /* IMAP_ENABLE */
1014 fprintf(stderr, "ETRN support is not configured.\n");
1015 return(PS_PROTOCOL);
1017 #ifdef HAVE_GETHOSTBYNAME
1018 return(doETRN(ctl));
1020 fprintf(stderr, "Cannot support ETRN without gethostbyname(2).\n");
1021 return(PS_PROTOCOL);
1022 #endif /* HAVE_GETHOSTBYNAME */
1023 #endif /* ETRN_ENABLE */
1025 error(0, 0, "unsupported protocol selected.");
1026 return(PS_PROTOCOL);
1030 void dump_params (struct runctl *runp, struct query *querylist, flag implicit)
1031 /* display query parameters in English */
1035 if (runp->poll_interval)
1036 printf("Poll interval is %d seconds\n", runp->poll_interval);
1038 printf("Logfile is %s\n", runp->logfile);
1039 if (strcmp(runp->idfile, IDFILE_NAME))
1040 printf("Idfile is %s\n", runp->idfile);
1041 #if defined(HAVE_SYSLOG)
1042 if (runp->use_syslog)
1043 printf("Progress messages will be logged via syslog\n");
1045 if (runp->invisible)
1046 printf("Fetchmail will masquerade and will not generate Received\n");
1048 for (ctl = querylist; ctl; ctl = ctl->next)
1050 if (!ctl->active || (implicit && ctl->server.skip))
1053 printf("Options for retrieving from %s@%s:\n",
1054 ctl->remotename, visbuf(ctl->server.pollname));
1056 if (ctl->server.via)
1057 printf(" Mail will be retrieved via %s\n", ctl->server.via);
1059 if (ctl->server.interval)
1060 printf(" Poll of this server will occur every %d intervals.\n",
1061 ctl->server.interval);
1062 if (ctl->server.truename)
1063 printf(" True name of server is %s.\n", ctl->server.truename);
1064 if (ctl->server.skip || outlevel == O_VERBOSE)
1065 printf(" This host will%s be queried when no host is specified.\n",
1066 ctl->server.skip ? " not" : "");
1068 printf(" Password will be prompted for.\n");
1069 else if (outlevel == O_VERBOSE)
1070 if (ctl->server.protocol == P_APOP)
1071 printf(" APOP secret = '%s'.\n", visbuf(ctl->password));
1072 else if (ctl->server.protocol == P_RPOP)
1073 printf(" RPOP id = '%s'.\n", visbuf(ctl->password));
1075 printf(" Password = '%s'.\n", visbuf(ctl->password));
1076 if (ctl->server.protocol == P_POP3
1078 && !strcmp(ctl->server.service, KPOP_PORT)
1080 && ctl->server.port == KPOP_PORT
1082 && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1083 ctl->server.preauthenticate == A_KERBEROS_V5))
1084 printf(" Protocol is KPOP");
1086 printf(" Protocol is %s", showproto(ctl->server.protocol));
1088 if (ctl->server.service)
1089 printf(" (using service %s)", ctl->server.service);
1090 if (ctl->server.netsec)
1091 printf(" (using network security options %s)", ctl->server.netsec);
1093 if (ctl->server.port)
1094 printf(" (using port %d)", ctl->server.port);
1096 else if (outlevel == O_VERBOSE)
1097 printf(" (using default port)");
1098 if (ctl->server.uidl)
1099 printf(" (forcing UIDL use)");
1102 if (ctl->server.preauthenticate == A_KERBEROS_V4)
1103 printf(" Kerberos V4 preauthentication enabled.\n");
1104 if (ctl->server.preauthenticate == A_KERBEROS_V5)
1105 printf(" Kerberos V5 preauthentication enabled.\n");
1106 if (ctl->server.timeout > 0)
1107 printf(" Server nonresponse timeout is %d seconds", ctl->server.timeout);
1108 if (ctl->server.timeout == CLIENT_TIMEOUT)
1109 printf(" (default).\n");
1113 if (!ctl->mailboxes->id)
1114 printf(" Default mailbox selected.\n");
1119 printf(" Selected mailboxes are:");
1120 for (idp = ctl->mailboxes; idp; idp = idp->next)
1121 printf(" %s", idp->id);
1124 printf(" %s messages will be retrieved (--all %s).\n",
1125 ctl->fetchall ? "All" : "Only new",
1126 ctl->fetchall ? "on" : "off");
1127 printf(" Fetched messages will%s be kept on the server (--keep %s).\n",
1128 ctl->keep ? "" : " not",
1129 ctl->keep ? "on" : "off");
1130 printf(" Old messages will%s be flushed before message retrieval (--flush %s).\n",
1131 ctl->flush ? "" : " not",
1132 ctl->flush ? "on" : "off");
1133 printf(" Rewrite of server-local addresses is %sabled (--norewrite %s).\n",
1134 ctl->rewrite ? "en" : "dis",
1135 ctl->rewrite ? "off" : "on");
1136 printf(" Carriage-return stripping is %sabled (stripcr %s).\n",
1137 ctl->stripcr ? "en" : "dis",
1138 ctl->stripcr ? "on" : "off");
1139 printf(" Carriage-return forcing is %sabled (forcecr %s).\n",
1140 ctl->forcecr ? "en" : "dis",
1141 ctl->forcecr ? "on" : "off");
1142 printf(" Interpretation of Content-Transfer-Encoding is %sabled (pass8bits %s).\n",
1143 ctl->pass8bits ? "en" : "dis",
1144 ctl->pass8bits ? "on" : "off");
1145 printf(" MIME decoding is %sabled (mimedecode %s).\n",
1146 ctl->mimedecode ? "en" : "dis",
1147 ctl->mimedecode ? "on" : "off");
1148 printf(" Nonempty Status lines will be %s (dropstatus %s)\n",
1149 ctl->dropstatus ? "discarded" : "kept",
1150 ctl->dropstatus ? "on" : "off");
1151 if (NUM_NONZERO(ctl->limit))
1152 printf(" Message size limit is %d bytes (--limit %d).\n",
1153 ctl->limit, ctl->limit);
1154 else if (outlevel == O_VERBOSE)
1155 printf(" No message size limit (--limit 0).\n");
1156 if (NUM_NONZERO(ctl->fetchlimit))
1157 printf(" Received-message limit is %d (--fetchlimit %d).\n",
1158 ctl->fetchlimit, ctl->fetchlimit);
1159 else if (outlevel == O_VERBOSE)
1160 printf(" No received-message limit (--fetchlimit 0).\n");
1161 if (NUM_NONZERO(ctl->batchlimit))
1162 printf(" SMTP message batch limit is %d.\n", ctl->batchlimit);
1163 else if (outlevel == O_VERBOSE)
1164 printf(" No SMTP message batch limit (--batchlimit 0).\n");
1165 if (ctl->server.protocol == P_IMAP)
1166 if (NUM_NONZERO(ctl->expunge))
1167 printf(" Deletion interval between expunges is %d (--expunge %d).\n", ctl->expunge, ctl->expunge);
1168 else if (outlevel == O_VERBOSE)
1169 printf(" No expunges (--expunge 0).\n");
1171 printf(" Messages will be delivered with '%s.'\n", visbuf(ctl->mda));
1176 printf(" Messages will be SMTP-forwarded to:");
1177 for (idp = ctl->smtphunt; idp; idp = idp->next)
1179 printf(" %s", idp->id);
1180 if (!idp->val.status.mark)
1181 printf(" (default)");
1184 if (ctl->smtpaddress)
1185 printf(" Host part of MAIL FROM line will be %s\n",
1188 if (ctl->antispam != -1)
1189 printf(" Listener SMTP reponse %d will be treated as a spam block\n",
1191 else if (outlevel == O_VERBOSE)
1192 printf(" Spam-blocking disabled\n");
1193 if (ctl->preconnect)
1194 printf(" Server connection will be brought up with '%s.'\n",
1195 visbuf(ctl->preconnect));
1196 else if (outlevel == O_VERBOSE)
1197 printf(" No pre-connection command.\n");
1198 if (ctl->postconnect)
1199 printf(" Server connection will be taken down with '%s.'\n",
1200 visbuf(ctl->postconnect));
1201 else if (outlevel == O_VERBOSE)
1202 printf(" No post-connection command.\n");
1203 if (!ctl->localnames)
1204 printf(" No localnames declared for this host.\n");
1210 for (idp = ctl->localnames; idp; idp = idp->next)
1213 if (count > 1 || ctl->wildcard)
1214 printf(" Multi-drop mode: ");
1216 printf(" Single-drop mode: ");
1218 printf("%d local name(s) recognized.\n", count);
1219 if (outlevel == O_VERBOSE)
1221 for (idp = ctl->localnames; idp; idp = idp->next)
1223 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1225 printf("\t%s\n", idp->id);
1227 fputs("*\n", stdout);
1230 if (count > 1 || ctl->wildcard)
1232 printf(" DNS lookup for multidrop addresses is %sabled.\n",
1233 ctl->server.dns ? "en" : "dis");
1235 if (ctl->server.envelope == STRING_DISABLED)
1236 printf(" Envelope-address routing is disabled\n");
1239 printf(" Envelope header is assumed to be: %s\n",
1240 ctl->server.envelope ? ctl->server.envelope:"Received");
1241 if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1242 printf(" Number of envelope header to be parsed: %d\n",
1243 ctl->server.envskip);
1244 if (ctl->server.qvirtual)
1245 printf(" Prefix %s will be removed from user id\n",
1246 ctl->server.qvirtual);
1247 else if (outlevel >= O_VERBOSE)
1248 printf(" No prefix stripping\n");
1251 if (ctl->server.akalist)
1255 printf(" Predeclared mailserver aliases:");
1256 for (idp = ctl->server.akalist; idp; idp = idp->next)
1257 printf(" %s", idp->id);
1260 if (ctl->server.localdomains)
1264 printf(" Local domains:");
1265 for (idp = ctl->server.localdomains; idp; idp = idp->next)
1266 printf(" %s", idp->id);
1272 if (ctl->server.interface)
1273 printf(" Connection must be through interface %s.\n", ctl->server.interface);
1274 else if (outlevel == O_VERBOSE)
1275 printf(" No interface requirement specified.\n");
1276 if (ctl->server.monitor)
1277 printf(" Polling loop will monitor %s.\n", ctl->server.monitor);
1278 else if (outlevel == O_VERBOSE)
1279 printf(" No monitor interface specified.\n");
1282 if (ctl->server.protocol > P_POP2)
1284 printf(" No UIDs saved from this host.\n");
1290 for (idp = ctl->oldsaved; idp; idp = idp->next)
1293 printf(" %d UIDs saved.\n", count);
1294 if (outlevel == O_VERBOSE)
1295 for (idp = ctl->oldsaved; idp; idp = idp->next)
1296 fprintf(stderr, "\t%s\n", idp->id);
1301 /* fetchmail.c ends here */