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)
21 #include <sys/types.h>
25 #ifdef HAVE_GETHOSTBYNAME
27 #endif /* HAVE_GETHOSTBYNAME */
29 #include "fetchmail.h"
33 #define DROPDEAD 6 /* maximum bad socket opens */
35 /* prototypes for internal functions */
36 static int load_params(int, char **, int);
37 static void dump_params (struct query *);
38 static int query_host(struct query *);
39 static char *visbuf(const char *);
41 /* controls the detail level of status/progress messages written to stderr */
42 int outlevel; /* see the O_.* constants above */
43 int yydebug; /* enable parse debugging */
45 /* daemon mode control */
46 int poll_interval; /* poll interval in seconds */
47 int nodetach; /* if TRUE, don't detach daemon process */
48 char *logfile; /* log file for daemon mode */
49 int quitmode; /* if --quit was set */
50 int check_only; /* if --probe was set */
51 int cmd_batchlimit; /* if --batchlimit was set */
52 char *cmd_logfile; /* if --logfile was set */
54 /* miscellaneous global controls */
55 char *rcfile; /* path name of rc file */
56 char *idfile; /* UID list file */
57 int versioninfo; /* emit only version info */
58 char *user; /* the name of the invoking user */
60 static char *lockfile; /* name of lockfile */
61 static int querystatus; /* status of query */
62 static int lastsig; /* last signal received */
64 static void termhook(); /* forward declaration of exit hook */
66 RETSIGTYPE donothing(sig) int sig; {signal(sig, donothing); lastsig = sig;}
68 static void unlockit(void)
69 /* must-do actions for exit (but we can't count on being able to do malloc) */
74 int main (int argc, char **argv)
77 int parsestatus, implicitmode;
78 char *home, *tmpdir, tmpbuf[BUFSIZ];
84 if ((user = getenv("USER")) == (char *)NULL)
85 user = getenv("LOGNAME");
87 if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
89 if ((pw = getpwuid(getuid())) != NULL)
96 fprintf(stderr,"fetchmail: can't find your name and home directory!\n");
102 * Backward-compatibility hack. If we're called by the name of the
103 * ancestral popclient, look for .poprc. This will actually work
104 * for popclient files that don't use the removed keywords.
106 if (strcmp("popclient", argv[0]) == 0)
109 tmpdir = ".fetchmailrc";
111 rcfile = (char *) xmalloc(strlen(home)+strlen(tmpdir)+2);
112 strcpy(rcfile, home);
114 strcat(rcfile, tmpdir);
116 #define IDFILE_NAME ".fetchids"
117 idfile = (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
118 strcpy(idfile, home);
120 strcat(idfile, IDFILE_NAME);
124 if ((parsestatus = parsecmdline(argc,argv,&cmd_opts)) < 0)
128 printf("This is fetchmail release %s pl %s\n", RELEASE_ID, PATCHLEVEL);
130 /* avoid parsing the config file if all we're doing is killing a daemon */
132 implicitmode = load_params(argc, argv, optind);
134 /* set up to do lock protocol */
135 if ((tmpdir = getenv("TMPDIR")) == (char *)NULL)
137 strcpy(tmpbuf, tmpdir);
138 strcat(tmpbuf, "/fetchmail-");
139 strcat(tmpbuf, user);
141 /* perhaps we just want to check options? */
143 printf("Taking options from command line");
144 if (access(rcfile, 0))
147 printf(" and %s\n", rcfile);
148 if (outlevel == O_VERBOSE)
149 printf("Lockfile at %s\n", tmpbuf);
151 printf("SMTP message batch limit is %d.\n", batchlimit);
153 printf("No SMTP message batch limit.\n");
154 for (ctl = querylist; ctl; ctl = ctl->next) {
155 if (ctl->active && !(implicitmode && ctl->skip))
158 if (querylist == NULL)
159 (void) fprintf(stderr,
160 "No mailservers set up -- perhaps %s is missing?\n",
164 else if (!quitmode && querylist == NULL) {
165 (void) fputs("fetchmail: no mailservers have been specified.\n", stderr);
169 /* check for another fetchmail running concurrently */
171 if ((lockfile = (char *) malloc(strlen(tmpbuf) + 1)) == NULL)
173 fprintf(stderr,"fetchmail: cannot allocate memory for lock name.\n");
177 (void) strcpy(lockfile, tmpbuf);
178 if ((lockfp = fopen(lockfile, "r")) != NULL )
180 bkgd = (fscanf(lockfp,"%d %d", &pid, &st) == 2);
182 if (kill(pid, 0) == -1) {
183 fprintf(stderr,"fetchmail: removing stale lockfile\n");
191 /* perhaps user asked us to kill the other fetchmail */
196 fprintf(stderr,"fetchmail: no other fetchmail is running\n");
199 else if (kill(pid, SIGTERM) < 0)
201 fprintf(stderr,"fetchmail: error killing %s fetchmail at %d.\n",
202 bkgd ? "background" : "foreground", pid);
207 fprintf(stderr,"fetchmail: %s fetchmail at %d killed.\n",
208 bkgd ? "background" : "foreground", pid);
214 /* another fetchmail is running -- wake it up or die */
219 else if (!implicitmode)
222 "fetchmail: can't poll specified hosts with another fetchmail running at %d.\n",
229 "fetchmail: another foreground fetchmail is running at %d.\n",
233 else if (kill(pid, SIGHUP) == 0)
236 "fetchmail: background fetchmail at %d awakened.\n",
243 * Should never happen -- possible only if a background fetchmail
244 * croaks after the first kill probe above but before the SIGHUP
248 "fetchmail: elder sibling at %d died mysteriously.\n",
250 return(PS_UNDEFINED);
254 /* pick up interactively any passwords we need but don't have */
255 for (ctl = querylist; ctl; ctl = ctl->next)
256 if (ctl->active && !(implicitmode && ctl->skip) && !ctl->password[0])
258 if (ctl->authenticate == A_KERBEROS)
259 /* Server won't care what the password is, but there
260 must be some non-null string here. */
261 (void) strncpy(ctl->password,
262 ctl->remotename, PASSWORDLEN-1);
265 (void) sprintf(tmpbuf, "Enter password for %s@%s: ",
266 ctl->remotename, ctl->servername);
267 (void) strncpy(ctl->password,
268 (char *)getpassword(tmpbuf),PASSWORDLEN-1);
273 * Maybe time to go to demon mode...
275 if (poll_interval && !nodetach)
276 daemonize(logfile, termhook);
278 /* beyond here we don't want more than one fetchmail running per user */
280 signal(SIGABRT, termhook);
281 signal(SIGINT, termhook);
282 signal(SIGTERM, termhook);
283 signal(SIGALRM, termhook);
284 signal(SIGPIPE, termhook);
285 signal(SIGQUIT, termhook);
288 * With this simple hack, we make it possible for a foreground
289 * fetchmail to wake up one in daemon mode. What we want is the
290 * side effect of interrupting any sleep that may be going on,
291 * forcing fetchmail to re-poll its hosts.
293 signal(SIGHUP, donothing);
295 /* here's the exclusion lock */
296 if ( (lockfp = fopen(lockfile,"w")) != NULL ) {
297 fprintf(lockfp,"%d",getpid());
299 fprintf(lockfp," %d", poll_interval);
305 * Query all hosts. If there's only one, the error return will
306 * reflect the status of that transaction.
309 #ifdef HAVE_RES_SEARCH
310 sethostent(TRUE); /* use TCP/IP for mailserver queries */
311 #endif /* HAVE_RES_SEARCH */
314 for (ctl = querylist; ctl; ctl = ctl->next)
316 if (ctl->active && !(implicitmode && ctl->skip))
318 #ifdef HAVE_GETHOSTBYNAME
320 * This functions partly as an optimization and partly
321 * as a probe to make sure our nameserver is still up.
322 * The multidrop case (especially) needs it.
324 if (ctl->authenticate == A_KERBEROS || MULTIDROP(ctl))
326 struct hostent *namerec;
328 /* compute the canonical name of the host */
330 namerec = gethostbyname(ctl->servername);
331 if (namerec == (struct hostent *)NULL)
334 "fetchmail: skipping %s poll, ",
338 perror("general error");
339 if (errno == ENETUNREACH)
340 break; /* go to sleep */
348 free(ctl->canonical_name);
349 ctl->canonical_name = xstrdup((char *)namerec->h_name);
352 #endif /* HAVE_GETHOSTBYNAME */
354 querystatus = query_host(ctl);
356 update_uid_lists(ctl);
360 #ifdef HAVE_RES_SEARCH
361 endhostent(); /* release TCP/IP connection to nameserver */
362 #endif /* HAVE_RES_SEARCH */
365 * Close all SMTP delivery sockets. For optimum performance
366 * we'd like to hold them open til end of run, but (1) this
367 * loses if our poll interval is longer than the MTA's inactivity
368 * timeout, and (2) some MTAs (like smail) don't deliver after
369 * each message, but rather queue up mail and wait to actually
370 * deliver it until the input socket is closed.
372 for (ctl = querylist; ctl; ctl = ctl->next)
373 if (ctl->smtp_sockfp)
375 SMTP_quit(ctl->smtp_sockfp);
376 fclose(ctl->smtp_sockfp);
377 ctl->smtp_sockfp = (FILE *)NULL;
382 if (outlevel == O_VERBOSE)
387 fprintf(stderr, "fetchmail: sleeping at %s", ctime(&now));
391 * We can't use sleep(3) here because we need an alarm(3)
392 * equivalent in order to implement server nonresponse timeout.
393 * We'll just assume setitimer(2) is available since fetchmail
394 * has to have a BSDoid socket layer to work at all.
397 struct itimerval ntimeout;
399 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
400 ntimeout.it_value.tv_sec = poll_interval;
401 ntimeout.it_value.tv_usec = 0;
403 setitimer(ITIMER_REAL,&ntimeout,NULL);
404 signal(SIGALRM, donothing);
406 if (lastsig == SIGHUP)
407 (void) fputs("fetchmail: awakened by SIGHUP\n", stderr);
410 if (outlevel == O_VERBOSE)
415 fprintf(stderr, "fetchmail: awakened at %s", ctime(&now));
421 if (outlevel == O_VERBOSE)
422 fprintf(stderr,"fetchmail: normal termination, status %d\n",querystatus);
428 static int load_params(int argc, char **argv, int optind)
430 int implicitmode, st;
432 struct query def_opts, *ctl, *mp;
434 memset(&def_opts, '\0', sizeof(struct query));
436 def_opts.protocol = P_AUTO;
437 def_opts.timeout = CLIENT_TIMEOUT;
438 strcpy(def_opts.remotename, user);
439 strcpy(def_opts.smtphost, "localhost");
441 /* this builds the host list */
442 if (prc_parse_file(rcfile) != 0)
445 if ((implicitmode = (optind >= argc)))
447 for (ctl = querylist; ctl; ctl = ctl->next)
451 for (; optind < argc; optind++)
454 * If hostname corresponds to a host known from the rc file,
455 * simply declare it active. Otherwise synthesize a host
456 * record from command line and defaults
458 for (ctl = querylist; ctl; ctl = ctl->next)
459 if (strcmp(ctl->servername, argv[optind]) == 0)
462 ctl = hostalloc(&cmd_opts);
463 strcpy(ctl->servername, argv[optind]);
469 /* if there's a defaults record, merge it and lose it */
470 if (querylist && strcmp(querylist->servername, "defaults") == 0)
472 for (ctl = querylist; ctl; ctl = ctl->next)
473 optmerge(ctl, querylist);
474 querylist = querylist->next;
477 /* don't allow a defaults record after the first */
478 for (ctl = querylist; ctl; ctl = ctl->next)
479 if (strcmp(ctl->servername, "defaults") == 0)
482 /* merge in wired defaults, do sanity checks and prepare internal fields */
483 for (ctl = querylist; ctl; ctl = ctl->next)
485 if (ctl->active && !(implicitmode && ctl->skip))
487 /* merge in defaults */
488 optmerge(ctl, &def_opts);
490 /* keep lusers from shooting themselves in the foot :-) */
491 if (poll_interval && ctl->limit)
493 fprintf(stderr,"fetchmail: you'd never see large messages!\n");
497 /* make sure delivery will default to a real local user */
498 if ((pw = getpwnam(user)) == (struct passwd *)NULL)
501 "fetchmail: can't set up default delivery to %s\n", user);
502 exit(PS_SYNTAX); /* has to be from bad rc file */
506 ctl->uid = pw->pw_uid; /* for local delivery via MDA */
507 if (!ctl->localnames) /* for local delivery via SMTP */
508 save_uid(&ctl->localnames, -1, user);
511 #if !defined(HAVE_GETHOSTBYNAME) || !defined(HAVE_RES_SEARCH)
512 /* can't handle multidrop mailboxes unless we can do DNS lookups */
513 if (ctl->localnames && ctl->localnames->next)
515 fputs("fetchmail: can't handle multidrop mailboxes without DNS\n",
519 #endif /* !HAVE_GETHOSTBYNAME || !HAVE_RES_SEARCH */
522 * Assign SMTP leaders. We want to allow all query blocks
523 * sharing the same SMTP host to use the same SMTP connection.
524 * To accomplish this, we initialize each query block's leader
525 * field to point to the first block in the list with a matching
528 * In the typical case, there will be only one SMTP host (the
529 * client machine) and thus just one SMTP leader (and one listener
530 * process) through the entire poll cycle.
534 ctl->smtp_sockfp = (FILE *)NULL;
535 for (mp = querylist; mp && mp != ctl; mp = mp->next)
536 if (strcmp(mp->smtphost, ctl->smtphost) == 0)
538 ctl->lead_smtp = mp->lead_smtp;
541 ctl->lead_smtp = ctl;
545 /* similarly, compute server leaders for queries */
546 for (mp = querylist; mp && mp != ctl; mp = mp->next)
547 if (strcmp(mp->servername, ctl->servername) == 0)
549 ctl->lead_server = mp->lead_server;
552 ctl->lead_server = ctl;
558 (void) fprintf(stderr,
559 "%s configuration invalid, port number cannot be negative",
564 /* expand MDA commands */
565 if (!check_only && ctl->mda[0])
569 /* punch nulls into the delimiting whitespace in the args */
570 for (argp = ctl->mda, ctl->mda_argcount = 1; *argp != '\0'; ctl->mda_argcount++)
572 ctl->mda_argv[ctl->mda_argcount] = argp;
573 while (!(*argp == '\0' || isspace(*argp)))
579 ctl->mda_argv[ctl->mda_argcount] = (char *)NULL;
581 ctl->mda_argv[0] = ctl->mda_argv[1];
582 if ((argp = strrchr(ctl->mda_argv[1], '/')) != (char *)NULL)
583 ctl->mda_argv[1] = argp + 1 ;
588 /* initialize UID handling */
589 if ((st = prc_filecheck(idfile)) != 0)
592 initialize_saved_lists(querylist, idfile);
594 /* if cmd_batchlimit was explicitly set, use it to override batchlimit */
595 if (cmd_batchlimit > -1)
596 batchlimit = cmd_batchlimit;
598 /* if cmd_logfile was explicitly set, use it to override logfile */
600 logfile = cmd_logfile;
602 return(implicitmode);
605 void termhook(int sig)
606 /* to be executed on normal or signal-induced termination */
611 fprintf(stderr, "terminated with signal %d\n", sig);
613 /* terminate all SMTP connections cleanly */
614 for (ctl = querylist; ctl; ctl = ctl->next)
615 if (ctl->lead_smtp == ctl && ctl->smtp_sockfp != (FILE *)NULL)
616 SMTP_quit(ctl->smtp_sockfp);
619 write_saved_lists(querylist, idfile);
624 static char *showproto(int proto)
625 /* protocol index to protocol name mapping */
629 case P_AUTO: return("auto"); break;
630 case P_POP2: return("POP2"); break;
631 case P_POP3: return("POP3"); break;
632 case P_IMAP: return("IMAP"); break;
633 case P_APOP: return("APOP"); break;
634 default: return("unknown?!?"); break;
639 * Sequence of protocols to try when autoprobing, most capable to least.
641 static const int autoprobe[] = {P_IMAP, P_POP3, P_POP2};
643 static int query_host(struct query *ctl)
644 /* perform fetch transaction with single host */
648 if (outlevel == O_VERBOSE)
653 fprintf(stderr, "Querying %s (protocol %s) at %s",
654 ctl->servername, showproto(ctl->protocol), ctime(&now));
656 switch (ctl->protocol) {
658 for (i = 0; i < sizeof(autoprobe)/sizeof(autoprobe[0]); i++)
660 ctl->protocol = autoprobe[i];
661 if ((st = query_host(ctl)) == PS_SUCCESS || st == PS_NOMAIL || st == PS_AUTHFAIL)
664 ctl->protocol = P_AUTO;
678 fprintf(stderr,"fetchmail: unsupported protocol selected.\n");
683 void dump_params (struct query *ctl)
684 /* display query parameters in English */
686 printf("Options for retrieving from %s@%s:\n",
687 ctl->remotename, visbuf(ctl->servername));
688 #ifdef HAVE_GETHOSTBYNAME
689 if (ctl->canonical_name)
690 printf(" Canonical DNS name of server is %s.\n", ctl->canonical_name);
691 #endif /* HAVE_GETHOSTBYNAME */
696 printf(" Predeclared mailserver aliases:");
697 for (idp = ctl->aka; idp; idp = idp->next)
698 printf(" %s", idp->id);
701 if (ctl->skip || outlevel == O_VERBOSE)
702 printf(" This host will%s be queried when no host is specified.\n",
703 ctl->skip ? " not" : "");
704 if (ctl->password[0] == '\0')
705 printf(" Password will be prompted for.\n");
706 else if (outlevel == O_VERBOSE)
707 if (ctl->protocol == P_APOP)
708 printf(" APOP secret = '%s'.\n", visbuf(ctl->password));
710 printf(" Password = '%s'.\n", visbuf(ctl->password));
711 if (ctl->protocol == P_POP3
712 && ctl->port == KPOP_PORT
713 && ctl->authenticate == A_KERBEROS)
714 printf(" Protocol is KPOP");
716 printf(" Protocol is %s", showproto(ctl->protocol));
718 printf(" (using port %d)", ctl->port);
719 else if (outlevel == O_VERBOSE)
720 printf(" (using default port)");
723 if (ctl->authenticate == A_KERBEROS)
724 printf(" Kerberos authentication enabled.\n");
725 printf(" Server nonresponse timeout is %d seconds", ctl->timeout);
726 if (ctl->timeout == CLIENT_TIMEOUT)
727 printf(" (default).\n");
731 printf(" %s messages will be retrieved (--all %s).\n",
732 ctl->fetchall ? "All" : "Only new",
733 ctl->fetchall ? "on" : "off");
734 printf(" Fetched messages will%s be kept on the server (--keep %s).\n",
735 ctl->keep ? "" : " not",
736 ctl->keep ? "on" : "off");
737 printf(" Old messages will%s be flushed before message retrieval (--flush %s).\n",
738 ctl->flush ? "" : " not",
739 ctl->flush ? "on" : "off");
740 printf(" Rewrite of server-local addresses is %sabled (--norewrite %s).\n",
741 ctl->norewrite ? "dis" : "en",
742 ctl->norewrite ? "on" : "off");
744 printf(" Message size limit is %d bytes\n", ctl->limit);
745 else if (outlevel == O_VERBOSE)
746 printf(" No message size limit\n");
751 printf(" Messages will be delivered with %s, args:",
752 visbuf(ctl->mda_argv[0]));
753 for (cp = ctl->mda_argv+1; *cp; cp++)
754 printf(" %s", visbuf(*cp));
758 printf(" Messages will be SMTP-forwarded to '%s'.\n",
759 visbuf(ctl->smtphost));
760 if (!ctl->localnames)
761 printf(" No localnames declared for this host.\n");
767 for (idp = ctl->localnames; idp; idp = idp->next)
770 printf(" %d local name(s) recognized%s.\n",
772 (count == 1 && !strcmp(ctl->localnames->id, user)) ? " (by default)" : "");
773 if (outlevel == O_VERBOSE)
774 for (idp = ctl->localnames; idp; idp = idp->next)
776 fprintf(stderr, "\t%s -> %s\n", idp->id, idp->val.id2);
778 fprintf(stderr, "\t%s\n", idp->id);
781 if (ctl->protocol > P_POP2)
783 printf(" No UIDs saved from this host.\n");
789 for (idp = ctl->oldsaved; idp; idp = idp->next)
792 printf(" %d UIDs saved.\n", count);
793 if (outlevel == O_VERBOSE)
794 for (idp = ctl->oldsaved; idp; idp = idp->next)
795 fprintf(stderr, "\t%s %s\n", ctl->servername, idp->id);
799 int openmailpipe (char **argv)
800 /* open a one-way pipe to a mail delivery agent */
805 if (outlevel == O_VERBOSE)
809 printf("fetchmail: about to deliver via MDA %s, args:",
811 for (cp = argv+1; *cp; cp++)
812 printf(" %s", visbuf(*cp));
816 if (pipe(pipefd) < 0) {
817 perror("fetchmail: openmailpipe: pipe");
820 if ((childpid = fork()) < 0) {
821 perror("fetchmail: openmailpipe: fork");
824 else if (childpid == 0) {
826 /* in child process space */
827 close(pipefd[1]); /* close the 'write' end of the pipe */
828 close(0); /* get rid of inherited stdin */
829 if (dup(pipefd[0]) != 0) {
830 fputs("fetchmail: openmailpipe: dup() failed\n",stderr);
834 execv(argv[0], argv + 1);
836 /* if we got here, an error occurred */
837 perror("fetchmail: openmailpipe: exec");
842 /* in the parent process space */
843 close(pipefd[0]); /* close the 'read' end of the pipe */
847 int closemailpipe (fd)
848 /* close the pipe to the mail delivery agent */
854 if ((err = close(fd)) != 0)
855 perror("fetchmail: closemailpipe: close failed");
857 childpid = wait(&status);
859 #if defined(WIFEXITED) && defined(WEXITSTATUS)
861 * Try to pass up an error if the MDA returned nonzero status,
862 * on the assumption that this means it was reporting failure.
864 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
866 perror("fetchmail: MDA exited abnormally or returned nonzero status");
874 /* helper functions for string interpretation and display */
876 #define CTRL(x) ((x) & 0x1f)
879 /* process standard C-style escape sequences in a string */
880 const char *cp; /* source string with escapes */
881 char *tp; /* target buffer for digested string */
887 if (*cp == '\\' && strchr("0123456789xX", cp[1]))
889 char *dp, *hex = "00112233445566778899aAbBcCdDeEfF";
892 if (*++cp == 'x' || *cp == 'X')
893 for (++cp; (dp = strchr(hex, *cp)) && (dcount++ < 2); cp++)
894 cval = (cval * 16) + (dp - hex) / 2;
896 while (strchr("01234567",*cp) != (char*)NULL && (dcount++ < 3))
897 cval = (cval * 8) + (*cp++ - '0');
899 while ((strchr("0123456789",*cp)!=(char*)NULL)&&(dcount++ < 3))
900 cval = (cval * 10) + (*cp++ - '0');
902 else if (*cp == '\\') /* C-style character escapes */
906 case '\\': cval = '\\'; break;
907 case 'n': cval = '\n'; break;
908 case 't': cval = '\t'; break;
909 case 'b': cval = '\b'; break;
910 case 'r': cval = '\r'; break;
915 else if (*cp == '^') /* expand control-character syntax */
927 static char *visbuf(const char *buf)
928 /* visibilize a given string */
930 static char vbuf[BUFSIZ];
935 if (isprint(*buf) || *buf == ' ')
937 else if (*buf == '\n')
939 *tp++ = '\\'; *tp++ = 'n';
942 else if (*buf == '\r')
944 *tp++ = '\\'; *tp++ = 'r';
947 else if (*buf == '\b')
949 *tp++ = '\\'; *tp++ = 'b';
954 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
959 (void) sprintf(tp, "\\0x%02x", *buf++);
967 /* fetchmail.c ends here */