2 * fetchmail.c -- main driver module for fetchmail
4 * For license terms, see the file COPYING in this directory.
10 #if defined(STDC_HEADERS)
13 #if defined(HAVE_UNISTD_H)
16 #if defined(HAVE_ALLOCA_H)
25 #if defined(HAVE_SYSLOG)
31 #include <sys/types.h>
34 #include <sys/resource.h>
35 #endif /* HAVE_SETRLIMIT */
36 #ifdef HAVE_SYS_WAIT_H
40 #ifdef HAVE_GETHOSTBYNAME
42 #endif /* HAVE_GETHOSTBYNAME */
44 #include "fetchmail.h"
51 #define ENETUNREACH 128 /* Interactive doesn't know this */
52 #endif /* ENETUNREACH */
54 /* prototypes for internal functions */
55 static int load_params(int, char **, int);
56 static void dump_params (struct runctl *runp, struct query *, flag implicit);
57 static int query_host(struct query *);
59 /* controls the detail level of status/progress messages written to stderr */
60 int outlevel; /* see the O_.* constants above */
62 /* miscellaneous global controls */
63 struct runctl run; /* global controls for this run */
64 flag nodetach; /* if TRUE, don't detach daemon process */
65 flag quitmode; /* if --quit was set */
66 flag check_only; /* if --probe was set */
67 flag versioninfo; /* emit only version info */
68 char *user; /* the name of the invoking user */
69 char *home; /* invoking user's home directory */
70 char *fetchmailhost; /* the name of the host running fetchmail */
71 char *program_name; /* the name to prefix error messages with */
72 flag configdump; /* dump control blocks for configurator */
77 #endif /* NET_SECURITY */
79 static char *lockfile; /* name of lockfile */
80 static int querystatus; /* status of query */
81 static int successes; /* count number of successful polls */
82 static int lastsig; /* last signal received */
83 static struct runctl cmd_run; /* global options set from command line */
85 static void termhook(); /* forward declaration of exit hook */
88 * The function of this variable is to reduce the size of the window during
89 * which two SIGALRMS in rapid succession can hose the code. This is a
90 * bit of a kluge; the real right thing would use sigprocmask(), sigsuspend()
91 * and close the window entirely. But since the interval isn't normally
92 * going to be less than one second this is not a big issue.
94 #if defined(STDC_HEADERS)
95 static sig_atomic_t alarm_latch = FALSE;
97 /* assume int can be written in one atomic operation on non ANSI-C systems */
98 static int alarm_latch = FALSE;
101 RETSIGTYPE donothing(sig) int sig; {signal(sig, donothing); lastsig = sig;}
102 RETSIGTYPE gotsigalrm(sig) int sig; {signal(sig, donothing); lastsig = sig; alarm_latch = TRUE;}
105 static void unlockit(int n, void *p)
107 static void unlockit(void)
109 /* must-do actions for exit (but we can't count on being able to do malloc) */
115 /* Various EMX-specific definitions */
117 void itimerthread(void* dummy) {
118 if (outlevel == O_VERBOSE)
119 fprintf(stderr, "fetchmail: thread sleeping for %d sec.\n", poll_interval);
121 _sleep2(poll_interval*1000);
122 kill((getpid()), SIGALRM);
127 int main (int argc, char **argv)
129 int st, bkgd = FALSE;
130 int parsestatus, implicitmode = FALSE;
133 netrc_entry *netrc_list;
134 char *netrc_file, *tmpbuf;
137 envquery(argc, argv);
139 #define IDFILE_NAME ".fetchids"
140 run.idfile = (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
141 strcpy(run.idfile, home);
142 strcat(run.idfile, "/");
143 strcat(run.idfile, IDFILE_NAME);
147 if ((parsestatus = parsecmdline(argc,argv, &cmd_run, &cmd_opts)) < 0)
150 /* this hint to stdio should help messages come out in the right order */
151 setvbuf(stdout, NULL, _IOLBF, MSGBUFSIZE);
155 printf("This is fetchmail release %s", RELEASE_ID);
158 #endif /* POP2_ENABLE */
161 #endif /* POP3_ENABLE */
164 #endif /* IMAP_ENABLE */
170 #endif /* RPA_ENABLE */
173 #endif /* ETRN_ENABLE */
182 #endif /* NET_SECURITY */
185 /* this is an attempt to help remote debugging */
189 /* avoid parsing the config file if all we're doing is killing a daemon */
190 if (!(quitmode && argc == 2))
191 implicitmode = load_params(argc, argv, optind);
193 /* set up to do lock protocol */
194 #define FETCHMAIL_PIDFILE "fetchmail.pid"
195 tmpbuf = xmalloc(strlen(home) + strlen(FETCHMAIL_PIDFILE) + 3);
197 sprintf(tmpbuf, "%s/%s", PID_DIR, FETCHMAIL_PIDFILE);
199 strcpy(tmpbuf, home);
200 strcat(tmpbuf, "/.");
201 strcat(tmpbuf, FETCHMAIL_PIDFILE);
203 #undef FETCHMAIL_PIDFILE
205 /* perhaps we just want to check options? */
208 printf("Taking options from command line");
209 if (access(rcfile, 0))
212 printf(" and %s\n", rcfile);
213 if (outlevel == O_VERBOSE)
214 printf("Lockfile at %s\n", tmpbuf);
216 if (querylist == NULL)
217 (void) fprintf(stderr,
218 "No mailservers set up -- perhaps %s is missing?\n", rcfile);
220 dump_params(&run, querylist, implicitmode);
224 /* dump options as a Python dictionary, for configurator use */
227 dump_config(&run, querylist);
231 /* check for another fetchmail running concurrently */
233 if ((lockfile = (char *) malloc(strlen(tmpbuf) + 1)) == NULL)
235 fprintf(stderr,"fetchmail: cannot allocate memory for lock name.\n");
239 (void) strcpy(lockfile, tmpbuf);
240 if ((lockfp = fopen(lockfile, "r")) != NULL )
242 bkgd = (fscanf(lockfp,"%d %d", &pid, &st) == 2);
244 if (kill(pid, 0) == -1) {
245 fprintf(stderr,"fetchmail: removing stale lockfile\n");
253 /* if no mail servers listed and nothing in background, we're done */
254 if (!(quitmode && argc == 2) && pid == -1 && querylist == NULL) {
255 (void)fputs("fetchmail: no mailservers have been specified.\n",stderr);
259 /* perhaps user asked us to kill the other fetchmail */
264 fprintf(stderr,"fetchmail: no other fetchmail is running\n");
268 else if (kill(pid, SIGTERM) < 0)
270 fprintf(stderr,"fetchmail: error killing %s fetchmail at %d; bailing out.\n",
271 bkgd ? "background" : "foreground", pid);
276 fprintf(stderr,"fetchmail: %s fetchmail at %d killed.\n",
277 bkgd ? "background" : "foreground", pid);
286 /* another fetchmail is running -- wake it up or die */
292 "fetchmail: can't check mail while another fetchmail to same host is running.\n");
295 else if (!implicitmode)
298 "fetchmail: can't poll specified hosts with another fetchmail running at %d.\n",
305 "fetchmail: another foreground fetchmail is running at %d.\n",
312 "fetchmail: can't accept options while a background fetchmail is running.\n");
315 else if (kill(pid, SIGUSR1) == 0)
318 "fetchmail: background fetchmail at %d awakened.\n",
325 * Should never happen -- possible only if a background fetchmail
326 * croaks after the first kill probe above but before the
327 * SIGUSR1/SIGHUP transmission.
330 "fetchmail: elder sibling at %d died mysteriously.\n",
332 return(PS_UNDEFINED);
336 /* parse the ~/.netrc file (if present) for future password lookups. */
337 netrc_file = (char *) xmalloc (strlen (home) + 8);
338 strcpy (netrc_file, home);
339 strcat (netrc_file, "/.netrc");
340 netrc_list = parse_netrc(netrc_file);
342 #ifdef HAVE_SETRLIMIT
344 * Before getting passwords, disable core dumps unless -v -d0 mode is on.
345 * Core dumps could otherwise contain passwords to be scavenged by a
348 if (outlevel < O_VERBOSE || run.poll_interval > 0)
350 struct rlimit corelimit;
351 corelimit.rlim_cur = 0;
352 corelimit.rlim_max = 0;
353 setrlimit(RLIMIT_CORE, &corelimit);
355 #endif /* HAVE_SETRLIMIT */
357 /* pick up interactively any passwords we need but don't have */
358 for (ctl = querylist; ctl; ctl = ctl->next)
359 if (ctl->active && !(implicitmode && ctl->server.skip)&&!ctl->password)
361 if (ctl->server.preauthenticate == A_KERBEROS_V4 ||
362 ctl->server.preauthenticate == A_KERBEROS_V5 ||
364 ctl->server.protocol == P_IMAP_GSS ||
366 ctl->server.protocol == P_IMAP_K4)
367 /* Server won't care what the password is, but there
368 must be some non-null string here. */
369 ctl->password = ctl->remotename;
372 /* look up the host and account in the .netrc file. */
373 netrc_entry *p = search_netrc(netrc_list,ctl->server.pollname);
374 while (p && strcmp(p->account, ctl->remotename))
375 p = search_netrc(p->next, ctl->remotename);
377 /* if we find a matching entry with a password, use it */
378 if (p && p->password)
379 ctl->password = xstrdup(p->password);
382 if (ctl->server.protocol != P_ETRN && ctl->server.protocol != P_IMAP_K4
384 && ctl->server.protocol != P_IMAP_GSS
389 #define PASSWORD_PROMPT "Enter password for %s@%s: "
390 tmpbuf = xmalloc(strlen(PASSWORD_PROMPT) +
391 strlen(ctl->remotename) +
392 strlen(ctl->server.pollname) + 1);
393 (void) sprintf(tmpbuf, PASSWORD_PROMPT,
394 ctl->remotename, ctl->server.pollname);
395 ctl->password = xstrdup((char *)getpassword(tmpbuf));
396 #undef PASSWORD_PROMPT
400 /* we don't need tmpbuf anymore */
402 tmpbuf = NULL; /* firewall code */
405 * Maybe time to go to demon mode...
407 #if defined(HAVE_SYSLOG)
410 openlog(program_name, LOG_PID, LOG_MAIL);
415 error_init(run.poll_interval == 0 && !run.logfile);
417 if (run.poll_interval)
420 daemonize(run.logfile, termhook);
421 error( 0, 0, "starting fetchmail %s daemon ", RELEASE_ID);
424 * We'll set up a handler for these when we're sleeping,
425 * but ignore them otherwise so as not to interrupt a poll.
427 signal(SIGUSR1, SIG_IGN);
428 if (run.poll_interval && !getuid())
429 signal(SIGHUP, SIG_IGN);
432 /* beyond here we don't want more than one fetchmail running per user */
434 signal(SIGABRT, termhook);
435 signal(SIGINT, termhook);
436 signal(SIGTERM, termhook);
437 signal(SIGALRM, termhook);
438 signal(SIGPIPE, termhook);
439 signal(SIGQUIT, termhook);
441 /* here's the exclusion lock */
442 if ((lockfp = fopen(lockfile,"w")) != NULL) {
443 fprintf(lockfp,"%d",getpid());
444 if (run.poll_interval)
445 fprintf(lockfp," %d", run.poll_interval);
452 on_exit(unlockit, (char *)NULL);
457 * Query all hosts. If there's only one, the error return will
458 * reflect the status of that transaction.
461 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
463 * This was an efficiency hack that backfired. The theory
464 * was that using TCP/IP for DNS queries would get us better
465 * reliability and shave off some per-UDP-packet costs.
466 * Unfortunately it interacted badly with diald, which effectively
467 * filters out DNS queries over TCP/IP for reasons having to do
468 * with some obscure kernel problem involving bootstrapping of
469 * dynamically-addressed links. I don't understand this mess
470 * and don't want to, so it's "See ya!" to this hack.
472 sethostent(TRUE); /* use TCP/IP for mailserver queries */
473 #endif /* HAVE_RES_SEARCH */
476 for (ctl = querylist; ctl; ctl = ctl->next)
478 if (ctl->active && !(implicitmode && ctl->server.skip))
480 /* check skip interval first so that it counts all polls */
481 if (run.poll_interval && ctl->server.interval)
483 if (ctl->server.poll_count++ % ctl->server.interval)
485 if (outlevel == O_VERBOSE)
487 "fetchmail: interval not reached, not querying %s\n",
488 ctl->server.pollname);
493 #if defined(linux) && !INET6
494 /* interface_approve() does its own error logging */
495 if (!interface_approve(&ctl->server))
497 #endif /* defined(linux) && !INET6 */
499 querystatus = query_host(ctl);
501 if (querystatus == PS_SUCCESS)
506 update_str_lists(ctl);
508 /* Save UID list to prevent re-fetch in case fetchmail
509 recover from crash */
510 if (!check_only && outlevel == O_VERBOSE)
512 write_saved_lists(querylist, run.idfile);
513 if (outlevel == O_VERBOSE)
514 error(0, 0, "saved UID List");
516 #endif /* POP3_ENABLE */
518 else if (!check_only &&
519 ((querystatus!=PS_NOMAIL) || (outlevel==O_VERBOSE)))
520 error(0, 0, "Query status=%d", querystatus);
522 #if defined(linux) && !INET6
523 if (ctl->server.monitor)
526 * Allow some time for the link to quiesce. One
527 * second is usually sufficient, three is safe.
528 * Note: this delay is important - don't remove!
531 interface_note_activity(&ctl->server);
533 #endif /* defined(linux) && !INET6 */
537 #if defined(HAVE_RES_SEARCH) && defined(USE_TCPIP_FOR_DNS)
538 endhostent(); /* release TCP/IP connection to nameserver */
539 #endif /* HAVE_RES_SEARCH */
542 * Close all SMTP delivery sockets. For optimum performance
543 * we'd like to hold them open til end of run, but (1) this
544 * loses if our poll interval is longer than the MTA's inactivity
545 * timeout, and (2) some MTAs (like smail) don't deliver after
546 * each message, but rather queue up mail and wait to actually
547 * deliver it until the input socket is closed.
549 for (ctl = querylist; ctl; ctl = ctl->next)
550 if (ctl->smtp_socket != -1)
552 SMTP_quit(ctl->smtp_socket);
553 close(ctl->smtp_socket);
554 ctl->smtp_socket = -1;
558 * OK, we've polled. Now sleep.
560 if (run.poll_interval)
562 if (outlevel == O_VERBOSE)
567 fprintf(stderr, "fetchmail: sleeping at %s", ctime(&now));
571 * With this simple hack, we make it possible for a foreground
572 * fetchmail to wake up one in daemon mode. What we want is the
573 * side effect of interrupting any sleep that may be going on,
574 * forcing fetchmail to re-poll its hosts. The second line is
575 * for people who think all system daemons wake up on SIGHUP.
577 signal(SIGUSR1, donothing);
579 signal(SIGHUP, donothing);
582 * We can't use sleep(3) here because we need an alarm(3)
583 * equivalent in order to implement server nonresponse timeout.
584 * We'll just assume setitimer(2) is available since fetchmail
585 * has to have a BSDoid socket layer to work at all.
589 #ifdef SLEEP_WITH_ALARM /* not normally on */
591 * This code stopped working under glibc-2, apparently due
592 * to the change in signal(2) semantics. (The siginterrupt
593 * line, added later, should fix this problem.) John Stracke
594 * <francis@netscape.com> wrote:
596 * The problem seems to be that, after hitting the interval
597 * timer while talking to the server, the process no longer
598 * responds to SIGALRM. I put in printf()s to see when it
599 * reached the pause() for the poll interval, and I checked
600 * the return from setitimer(), and everything seemed to be
601 * working fine, except that the pause() just ignored SIGALRM.
602 * I thought maybe the itimer wasn't being fired, so I hit
603 * it with a SIGALRM from the command line, and it ignored
604 * that, too. SIGUSR1 woke it up just fine, and it proceeded
605 * to repoll--but, when the dummy server didn't respond, it
606 * never timed out, and SIGALRM wouldn't make it.
608 * (continued below...)
610 struct itimerval ntimeout;
612 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
613 ntimeout.it_value.tv_sec = poll_interval;
614 ntimeout.it_value.tv_usec = 0;
616 siginterrupt(SIGALRM, 1);
618 signal(SIGALRM, gotsigalrm); /* first trap signals */
619 setitimer(ITIMER_REAL,&ntimeout,NULL); /* then start timer */
620 /* there is a very small window between the next two lines */
623 signal(SIGALRM, SIG_IGN);
626 * So the workaround I used is to make it sleep by using
627 * select() instead of setitimer()/pause(). select() is
628 * perfectly happy being called with a timeout and
629 * no file descriptors; it just sleeps until it hits the
630 * timeout. The only concern I had was that it might
631 * implement its timeout with SIGALRM--there are some
632 * Unices where this is done, because select() is a library
633 * function--but apparently not.
635 struct timeval timeout;
637 timeout.tv_sec = run.poll_interval;
640 select(0,0,0,0, &timeout);
644 signal(SIGALRM, gotsigalrm);
645 _beginthread(itimerthread, NULL, 32768, NULL);
646 /* see similar code above */
649 signal(SIGALRM, SIG_IGN);
651 if (lastsig == SIGUSR1
652 || ((run.poll_interval && !getuid()) && lastsig == SIGHUP))
654 #ifdef SYS_SIGLIST_DECLARED
655 error(0, 0, "awakened by %s", sys_siglist[lastsig]);
657 error(0, 0, "awakened by signal %d", lastsig);
662 /* now lock out interrupts again */
663 signal(SIGUSR1, SIG_IGN);
665 signal(SIGHUP, SIG_IGN);
667 if (outlevel == O_VERBOSE)
672 fprintf(stderr, "fetchmail: awakened at %s", ctime(&now));
678 if (outlevel == O_VERBOSE)
679 fprintf(stderr,"fetchmail: normal termination, status %d\n",
680 successes ? PS_SUCCESS : querystatus);
683 exit(successes ? PS_SUCCESS : querystatus);
686 static void optmerge(struct query *h2, struct query *h1, int force)
687 /* merge two options records */
690 * If force is off, modify h2 fields only when they're empty (treat h1
691 * as defaults). If force is on, modify each h2 field whenever h1
692 * is nonempty (treat h1 as an override).
694 #define LIST_MERGE(dstl, srcl) if (force ? !!srcl : !dstl) \
695 free_str_list(&dstl), \
696 append_str_list(&dstl, &srcl)
697 LIST_MERGE(h2->server.localdomains, h1->server.localdomains);
698 LIST_MERGE(h2->localnames, h1->localnames);
699 LIST_MERGE(h2->mailboxes, h1->mailboxes);
700 LIST_MERGE(h2->smtphunt, h1->smtphunt);
701 LIST_MERGE(h2->antispam, h1->antispam);
704 #define FLAG_MERGE(fld) if (force ? !!h1->fld : !h2->fld) h2->fld = h1->fld
705 FLAG_MERGE(server.via);
706 FLAG_MERGE(server.protocol);
708 FLAG_MERGE(server.service);
709 FLAG_MERGE(server.netsec);
711 FLAG_MERGE(server.port);
713 FLAG_MERGE(server.interval);
714 FLAG_MERGE(server.preauthenticate);
715 FLAG_MERGE(server.timeout);
716 FLAG_MERGE(server.envelope);
717 FLAG_MERGE(server.envskip);
718 FLAG_MERGE(server.qvirtual);
719 FLAG_MERGE(server.skip);
720 FLAG_MERGE(server.dns);
721 FLAG_MERGE(server.checkalias);
722 FLAG_MERGE(server.uidl);
725 FLAG_MERGE(server.interface);
726 FLAG_MERGE(server.monitor);
727 FLAG_MERGE(server.interface_pair);
730 FLAG_MERGE(wildcard);
731 FLAG_MERGE(remotename);
732 FLAG_MERGE(password);
734 FLAG_MERGE(smtpaddress);
735 FLAG_MERGE(preconnect);
739 FLAG_MERGE(fetchall);
743 FLAG_MERGE(pass8bits);
744 FLAG_MERGE(dropstatus);
745 FLAG_MERGE(mimedecode);
747 FLAG_MERGE(fetchlimit);
748 FLAG_MERGE(batchlimit);
753 static int load_params(int argc, char **argv, int optind)
755 int implicitmode, st;
757 struct query def_opts, *ctl;
759 memset(&def_opts, '\0', sizeof(struct query));
760 def_opts.smtp_socket = -1;
761 def_opts.smtpaddress = (char *)0;
762 save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 571;
763 save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 550;
764 save_str(&def_opts.antispam, STRING_DUMMY, 0)->val.status.num = 501;
766 def_opts.server.protocol = P_AUTO;
767 def_opts.server.timeout = CLIENT_TIMEOUT;
768 def_opts.remotename = user;
769 def_opts.expunge = 1;
771 /* this builds the host list */
772 if (prc_parse_file(rcfile, !versioninfo) != 0)
775 if ((implicitmode = (optind >= argc)))
777 for (ctl = querylist; ctl; ctl = ctl->next)
781 for (; optind < argc; optind++)
783 flag predeclared = FALSE;
786 * If hostname corresponds to a host known from the rc file,
787 * simply declare it active. Otherwise synthesize a host
788 * record from command line and defaults
790 for (ctl = querylist; ctl; ctl = ctl->next)
791 if (!strcmp(ctl->server.pollname, argv[optind])
792 || str_in_list(&ctl->server.akalist, argv[optind], TRUE))
801 * Allocate and link record without copying in
802 * command-line args; we'll do that with the optmerge
805 ctl = hostalloc((struct query *)NULL);
807 ctl->server.pollname = xstrdup(argv[optind]);
809 ctl->server.lead_server = (struct hostdata *)NULL;
814 * If there's a defaults record, merge it and lose it.
816 if (querylist && strcmp(querylist->server.pollname, "defaults") == 0)
818 for (ctl = querylist->next; ctl; ctl = ctl->next)
819 optmerge(ctl, querylist, FALSE);
820 querylist = querylist->next;
823 /* don't allow a defaults record after the first */
824 for (ctl = querylist; ctl; ctl = ctl->next)
825 if (ctl != querylist && strcmp(ctl->server.pollname, "defaults") == 0)
828 /* merge in wired defaults, do sanity checks and prepare internal fields */
829 for (ctl = querylist; ctl; ctl = ctl->next)
831 if (configdump || (ctl->active && !(implicitmode && ctl->server.skip)))
833 /* merge in defaults */
834 optmerge(ctl, &def_opts, FALSE);
836 /* force command-line options */
837 optmerge(ctl, &cmd_opts, TRUE);
839 /* make sure we have a nonempty host list to forward to */
842 save_str(&ctl->smtphunt, fetchmailhost, FALSE);
843 /* for non ETRN try to deliver mails to localhost if
844 * fetchmailhost fails
846 if (ctl->server.protocol != P_ETRN) {
847 save_str(&ctl->smtphunt, "localhost", FALSE);
851 /* keep lusers from shooting themselves in the foot :-) */
852 if (run.poll_interval && ctl->limit)
854 fprintf(stderr,"fetchmail: you'd never see large messages!\n");
858 /* if `user' doesn't name a real local user, try to run as root */
859 if ((pw = getpwnam(user)) == (struct passwd *)NULL)
862 ctl->uid = pw->pw_uid; /* for local delivery via MDA */
863 if (!ctl->localnames) /* for local delivery via SMTP */
864 save_str_pair(&ctl->localnames, user, NULL);
866 /* this code enables flags to be turned off */
867 #define DEFAULT(flag, dflt) if (flag == FLAG_TRUE)\
869 else if (flag == FLAG_FALSE)\
873 DEFAULT(ctl->keep, FALSE);
874 DEFAULT(ctl->fetchall, FALSE);
875 DEFAULT(ctl->flush, FALSE);
876 DEFAULT(ctl->rewrite, TRUE);
877 DEFAULT(ctl->stripcr, (ctl->mda != (char *)NULL));
878 DEFAULT(ctl->forcecr, FALSE);
879 DEFAULT(ctl->pass8bits, FALSE);
880 DEFAULT(ctl->dropstatus, FALSE);
881 DEFAULT(ctl->mimedecode, FALSE);
882 DEFAULT(ctl->server.dns, TRUE);
883 DEFAULT(ctl->server.uidl, FALSE);
884 DEFAULT(ctl->server.checkalias, FALSE);
887 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
888 /* can't handle multidrop mailboxes unless we can do DNS lookups */
889 if (ctl->localnames && ctl->localnames->next && ctl->server.dns)
891 ctl->server.dns = FALSE;
892 fprintf(stderr, "fetchmail: warning: no DNS available to check multidrop fetches from %s\n", ctl->server.pollname);
894 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
898 * Compute the true name of the mailserver host.
899 * There are two clashing cases here:
901 * (1) The poll name is a label, possibly on one of several
902 * poll configurations for the same host. In this case
903 * the `via' option will be present and give the true name.
905 * (2) The poll name is the true one, the via name is
906 * localhost. This is going to be typical for ssh-using
909 * We're going to assume the via name is true unless it's
912 if (ctl->server.via && strcmp(ctl->server.via, "localhost"))
913 ctl->server.queryname = xstrdup(ctl->server.via);
915 ctl->server.queryname = xstrdup(ctl->server.pollname);
918 * We may have to canonicalize the server truename for later use.
919 * Do this just once for each lead server, if necessary, in order
920 * to minimize DNS round trips.
922 if (ctl->server.lead_server)
923 ctl->server.truename = xstrdup(ctl->server.lead_server->truename);
924 #ifdef HAVE_GETHOSTBYNAME
925 else if (ctl->server.preauthenticate==A_KERBEROS_V4 ||
926 ctl->server.preauthenticate==A_KERBEROS_V5 ||
927 (ctl->server.dns && MULTIDROP(ctl)))
929 struct hostent *namerec;
931 /* compute the canonical name of the host */
933 namerec = gethostbyname(ctl->server.queryname);
934 if (namerec == (struct hostent *)NULL)
937 "couldn't find canonical DNS name of %s",
938 ctl->server.pollname);
942 ctl->server.truename=xstrdup((char *)namerec->h_name);
944 #endif /* HAVE_GETHOSTBYNAME */
946 ctl->server.truename = xstrdup(ctl->server.queryname);
948 /* if no folders were specified, set up the null one as default */
950 save_str(&ctl->mailboxes, (char *)NULL, 0);
952 /* maybe user overrode timeout on command line? */
953 if (ctl->server.timeout == -1)
954 ctl->server.timeout = CLIENT_TIMEOUT;
958 if (ctl->server.port < 0)
960 (void) fprintf(stderr,
961 "%s configuration invalid, port number cannot be negative",
962 ctl->server.pollname);
965 if (ctl->server.protocol == P_RPOP && ctl->server.port >= 1024)
967 (void) fprintf(stderr,
968 "%s configuration invalid, RPOP requires a privileged port",
969 ctl->server.pollname);
976 /* here's where we override globals */
978 run.logfile = cmd_run.logfile;
980 run.idfile = cmd_run.idfile;
981 if (cmd_run.poll_interval >= 0)
982 run.poll_interval = cmd_run.poll_interval;
983 if (cmd_run.invisible)
984 run.invisible = cmd_run.invisible;
985 if (cmd_run.use_syslog)
986 run.use_syslog = (cmd_run.use_syslog == FLAG_TRUE);
988 /* check and daemon options are not compatible */
989 if (check_only && run.poll_interval)
990 run.poll_interval = 0;
993 /* initialize UID handling */
994 if (!versioninfo && (st = prc_filecheck(run.idfile, !versioninfo)) != 0)
997 initialize_saved_lists(querylist, run.idfile);
998 #endif /* POP3_ENABLE */
1001 * If the user didn't set a last-resort user to get misaddressed
1002 * multidrop mail, set an appropriate default here.
1004 if (!run.postmaster)
1005 if (getuid()) /* ordinary user */
1006 run.postmaster = user;
1008 run.postmaster = "postmaster";
1010 return(implicitmode);
1013 void termhook(int sig)
1014 /* to be executed on normal or signal-induced termination */
1019 * Sending SMTP QUIT on signal is theoretically nice, but led to a
1020 * subtle bug. If fetchmail was terminated by signal while it was
1021 * shipping message text, it would hang forever waiting for a
1022 * command acknowledge. In theory we could enable the QUIT
1023 * only outside of the message send. In practice, we don't
1024 * care. All mailservers hang up on a dropped TCP/IP connection
1029 error(0, 0, "terminated with signal %d", sig);
1031 /* terminate all SMTP connections cleanly */
1032 for (ctl = querylist; ctl; ctl = ctl->next)
1033 if (ctl->smtp_socket != -1)
1034 SMTP_quit(ctl->smtp_socket);
1038 write_saved_lists(querylist, run.idfile);
1039 #endif /* POP3_ENABLE */
1042 * Craig Metz, the RFC1938 one-time-password guy, points out:
1043 * "Remember that most kernels don't zero pages before handing them to the
1044 * next process and many kernels share pages between user and kernel space.
1045 * You'd be very surprised what you can find from a short program to do a
1046 * malloc() and then dump the contents of the pages you got. By zeroing
1047 * the secrets at end of run (earlier if you can), you make sure the next
1048 * guy can't get the password/pass phrase."
1050 * Right you are, Craig!
1052 for (ctl = querylist; ctl; ctl = ctl->next)
1054 memset(ctl->password, '\0', strlen(ctl->password));
1056 #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT)
1060 exit(successes ? PS_SUCCESS : querystatus);
1064 * Sequence of protocols to try when autoprobing, most capable to least.
1066 static const int autoprobe[] =
1070 #endif /* IMAP_ENABLE */
1073 #endif /* POP3_ENABLE */
1076 #endif /* POP2_ENABLE */
1079 static int query_host(struct query *ctl)
1080 /* perform fetch transaction with single host */
1084 if (outlevel == O_VERBOSE)
1089 fprintf(stderr, "fetchmail: %s querying %s (protocol %s) at %s",
1091 ctl->server.pollname, showproto(ctl->server.protocol), ctime(&now));
1093 switch (ctl->server.protocol) {
1095 for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
1097 ctl->server.protocol = autoprobe[i];
1098 if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL || st == PS_LOCKBUSY || st == PS_SMTP)
1101 ctl->server.protocol = P_AUTO;
1106 return(doPOP2(ctl));
1108 fprintf(stderr, "POP2 support is not configured.\n");
1109 return(PS_PROTOCOL);
1110 #endif /* POP2_ENABLE */
1116 return(doPOP3(ctl));
1118 fprintf(stderr, "POP3 support is not configured.\n");
1119 return(PS_PROTOCOL);
1120 #endif /* POP3_ENABLE */
1128 return(doIMAP(ctl));
1130 fprintf(stderr, "IMAP support is not configured.\n");
1131 return(PS_PROTOCOL);
1132 #endif /* IMAP_ENABLE */
1136 fprintf(stderr, "ETRN support is not configured.\n");
1137 return(PS_PROTOCOL);
1139 #ifdef HAVE_GETHOSTBYNAME
1140 return(doETRN(ctl));
1142 fprintf(stderr, "Cannot support ETRN without gethostbyname(2).\n");
1143 return(PS_PROTOCOL);
1144 #endif /* HAVE_GETHOSTBYNAME */
1145 #endif /* ETRN_ENABLE */
1147 error(0, 0, "unsupported protocol selected.");
1148 return(PS_PROTOCOL);
1152 void dump_params (struct runctl *runp, struct query *querylist, flag implicit)
1153 /* display query parameters in English */
1157 if (runp->poll_interval)
1158 printf("Poll interval is %d seconds\n", runp->poll_interval);
1160 printf("Logfile is %s\n", runp->logfile);
1161 if (strcmp(runp->idfile, IDFILE_NAME))
1162 printf("Idfile is %s\n", runp->idfile);
1163 #if defined(HAVE_SYSLOG)
1164 if (runp->use_syslog)
1165 printf("Progress messages will be logged via syslog\n");
1167 if (runp->invisible)
1168 printf("Fetchmail will masquerade and will not generate Received\n");
1169 if (runp->postmaster)
1170 printf("Fetchmail will forward misaddressed multidrop messages to %s.\n",
1173 for (ctl = querylist; ctl; ctl = ctl->next)
1175 if (!ctl->active || (implicit && ctl->server.skip))
1178 printf("Options for retrieving from %s@%s:\n",
1179 ctl->remotename, visbuf(ctl->server.pollname));
1181 if (ctl->server.via && (ctl->server.protocol != P_ETRN))
1182 printf(" Mail will be retrieved via %s\n", ctl->server.via);
1184 if (ctl->server.interval)
1185 printf(" Poll of this server will occur every %d intervals.\n",
1186 ctl->server.interval);
1187 if (ctl->server.truename)
1188 printf(" True name of server is %s.\n", ctl->server.truename);
1189 if (ctl->server.skip || outlevel == O_VERBOSE)
1190 printf(" This host will%s be queried when no host is specified.\n",
1191 ctl->server.skip ? " not" : "");
1193 * Don't poll for password when there is one or when using the ETRN
1194 * or IMAP-GSS protocol
1196 if (!ctl->password && (ctl->server.protocol != P_ETRN)
1198 && (ctl->server.protocol != P_IMAP_GSS)
1201 printf(" Password will be prompted for.\n");
1202 else if (outlevel == O_VERBOSE)
1203 if (ctl->server.protocol == P_APOP)
1204 printf(" APOP secret = '%s'.\n", visbuf(ctl->password));
1205 else if (ctl->server.protocol == P_RPOP)
1206 printf(" RPOP id = '%s'.\n", visbuf(ctl->password));
1208 printf(" Password = '%s'.\n", visbuf(ctl->password));
1209 if (ctl->server.protocol == P_POP3
1211 && !strcmp(ctl->server.service, KPOP_PORT)
1213 && ctl->server.port == KPOP_PORT
1215 && (ctl->server.preauthenticate == A_KERBEROS_V4 ||
1216 ctl->server.preauthenticate == A_KERBEROS_V5))
1217 printf(" Protocol is KPOP");
1219 printf(" Protocol is %s", showproto(ctl->server.protocol));
1221 if (ctl->server.service)
1222 printf(" (using service %s)", ctl->server.service);
1223 if (ctl->server.netsec)
1224 printf(" (using network security options %s)", ctl->server.netsec);
1226 if (ctl->server.port)
1227 printf(" (using port %d)", ctl->server.port);
1229 else if (outlevel == O_VERBOSE)
1230 printf(" (using default port)");
1231 if (ctl->server.uidl && (ctl->server.protocol != P_ETRN))
1232 printf(" (forcing UIDL use)");
1235 if (ctl->server.preauthenticate == A_KERBEROS_V4)
1236 printf(" Kerberos V4 preauthentication enabled.\n");
1237 if (ctl->server.preauthenticate == A_KERBEROS_V5)
1238 printf(" Kerberos V5 preauthentication enabled.\n");
1239 if (ctl->server.timeout > 0)
1240 printf(" Server nonresponse timeout is %d seconds", ctl->server.timeout);
1241 if (ctl->server.timeout == CLIENT_TIMEOUT)
1242 printf(" (default).\n");
1246 if (ctl->server.protocol != P_ETRN) {
1247 if (!ctl->mailboxes->id)
1248 printf(" Default mailbox selected.\n");
1253 printf(" Selected mailboxes are:");
1254 for (idp = ctl->mailboxes; idp; idp = idp->next)
1255 printf(" %s", idp->id);
1258 printf(" %s messages will be retrieved (--all %s).\n",
1259 ctl->fetchall ? "All" : "Only new",
1260 ctl->fetchall ? "on" : "off");
1261 printf(" Fetched messages will%s be kept on the server (--keep %s).\n",
1262 ctl->keep ? "" : " not",
1263 ctl->keep ? "on" : "off");
1264 printf(" Old messages will%s be flushed before message retrieval (--flush %s).\n",
1265 ctl->flush ? "" : " not",
1266 ctl->flush ? "on" : "off");
1267 printf(" Rewrite of server-local addresses is %sabled (--norewrite %s).\n",
1268 ctl->rewrite ? "en" : "dis",
1269 ctl->rewrite ? "off" : "on");
1270 printf(" Carriage-return stripping is %sabled (stripcr %s).\n",
1271 ctl->stripcr ? "en" : "dis",
1272 ctl->stripcr ? "on" : "off");
1273 printf(" Carriage-return forcing is %sabled (forcecr %s).\n",
1274 ctl->forcecr ? "en" : "dis",
1275 ctl->forcecr ? "on" : "off");
1276 printf(" Interpretation of Content-Transfer-Encoding is %sabled (pass8bits %s).\n",
1277 ctl->pass8bits ? "dis" : "en",
1278 ctl->pass8bits ? "on" : "off");
1279 printf(" MIME decoding is %sabled (mimedecode %s).\n",
1280 ctl->mimedecode ? "en" : "dis",
1281 ctl->mimedecode ? "on" : "off");
1282 printf(" Nonempty Status lines will be %s (dropstatus %s)\n",
1283 ctl->dropstatus ? "discarded" : "kept",
1284 ctl->dropstatus ? "on" : "off");
1285 if (NUM_NONZERO(ctl->limit))
1286 printf(" Message size limit is %d bytes (--limit %d).\n",
1287 ctl->limit, ctl->limit);
1288 else if (outlevel == O_VERBOSE)
1289 printf(" No message size limit (--limit 0).\n");
1290 if (NUM_NONZERO(ctl->fetchlimit))
1291 printf(" Received-message limit is %d (--fetchlimit %d).\n",
1292 ctl->fetchlimit, ctl->fetchlimit);
1293 else if (outlevel == O_VERBOSE)
1294 printf(" No received-message limit (--fetchlimit 0).\n");
1295 if (NUM_NONZERO(ctl->batchlimit))
1296 printf(" SMTP message batch limit is %d.\n", ctl->batchlimit);
1297 else if (outlevel == O_VERBOSE)
1298 printf(" No SMTP message batch limit (--batchlimit 0).\n");
1299 if (ctl->server.protocol == P_IMAP)
1300 if (NUM_NONZERO(ctl->expunge))
1301 printf(" Deletion interval between expunges is %d (--expunge %d).\n", ctl->expunge, ctl->expunge);
1302 else if (outlevel == O_VERBOSE)
1303 printf(" No expunges (--expunge 0).\n");
1305 if (ctl->mda && (ctl->server.protocol != P_ETRN))
1306 printf(" Messages will be delivered with '%s.'\n", visbuf(ctl->mda));
1311 printf(" Messages will be SMTP-forwarded to:");
1312 for (idp = ctl->smtphunt; idp; idp = idp->next)
1314 printf(" %s", idp->id);
1315 if (!idp->val.status.mark)
1316 printf(" (default)");
1319 if (ctl->smtpaddress)
1320 printf(" Host part of MAIL FROM line will be %s\n",
1323 if (ctl->server.protocol != P_ETRN)
1325 if (ctl->antispam != (struct idlist *)NULL)
1329 printf(" Recognized listener spam block responses are:",
1331 for (idp = ctl->antispam; idp; idp = idp->next)
1332 printf(" %d", idp->val.status.num);
1335 else if (outlevel == O_VERBOSE)
1336 printf(" Spam-blocking disabled\n");
1338 if (ctl->preconnect)
1339 printf(" Server connection will be brought up with '%s.'\n",
1340 visbuf(ctl->preconnect));
1341 else if (outlevel == O_VERBOSE)
1342 printf(" No pre-connection command.\n");
1343 if (ctl->postconnect)
1344 printf(" Server connection will be taken down with '%s.'\n",
1345 visbuf(ctl->postconnect));
1346 else if (outlevel == O_VERBOSE)
1347 printf(" No post-connection command.\n");
1348 if (ctl->server.protocol != P_ETRN) {
1349 if (!ctl->localnames)
1350 printf(" No localnames declared for this host.\n");
1356 for (idp = ctl->localnames; idp; idp = idp->next)
1359 if (count > 1 || ctl->wildcard)
1360 printf(" Multi-drop mode: ");
1362 printf(" Single-drop mode: ");
1364 printf("%d local name(s) recognized.\n", count);
1365 if (outlevel == O_VERBOSE)
1367 for (idp = ctl->localnames; idp; idp = idp->next)
1369 printf("\t%s -> %s\n", idp->id, idp->val.id2);
1371 printf("\t%s\n", idp->id);
1373 fputs("\t*\n", stdout);
1376 if (count > 1 || ctl->wildcard)
1378 printf(" DNS lookup for multidrop addresses is %sabled.\n",
1379 ctl->server.dns ? "en" : "dis");
1380 if (ctl->server.dns)
1382 printf(" Server aliases will be compared with multidrop addresses by ");
1383 if (ctl->server.checkalias)
1384 printf("IP address.\n");
1388 if (ctl->server.envelope == STRING_DISABLED)
1389 printf(" Envelope-address routing is disabled\n");
1392 printf(" Envelope header is assumed to be: %s\n",
1393 ctl->server.envelope ? ctl->server.envelope:"Received");
1394 if (ctl->server.envskip > 1 || outlevel >= O_VERBOSE)
1395 printf(" Number of envelope header to be parsed: %d\n",
1396 ctl->server.envskip);
1397 if (ctl->server.qvirtual)
1398 printf(" Prefix %s will be removed from user id\n",
1399 ctl->server.qvirtual);
1400 else if (outlevel >= O_VERBOSE)
1401 printf(" No prefix stripping\n");
1404 if (ctl->server.akalist)
1408 printf(" Predeclared mailserver aliases:");
1409 for (idp = ctl->server.akalist; idp; idp = idp->next)
1410 printf(" %s", idp->id);
1413 if (ctl->server.localdomains)
1417 printf(" Local domains:");
1418 for (idp = ctl->server.localdomains; idp; idp = idp->next)
1419 printf(" %s", idp->id);
1426 if (ctl->server.interface)
1427 printf(" Connection must be through interface %s.\n", ctl->server.interface);
1428 else if (outlevel == O_VERBOSE)
1429 printf(" No interface requirement specified.\n");
1430 if (ctl->server.monitor)
1431 printf(" Polling loop will monitor %s.\n", ctl->server.monitor);
1432 else if (outlevel == O_VERBOSE)
1433 printf(" No monitor interface specified.\n");
1436 if (ctl->server.protocol > P_POP2 && (ctl->server.protocol != P_ETRN))
1438 printf(" No UIDs saved from this host.\n");
1444 for (idp = ctl->oldsaved; idp; idp = idp->next)
1447 printf(" %d UIDs saved.\n", count);
1448 if (outlevel == O_VERBOSE)
1449 for (idp = ctl->oldsaved; idp; idp = idp->next)
1450 fprintf(stderr, "\t%s\n", idp->id);
1455 /* fetchmail.c ends here */