2 * driver.c -- generic driver for mail fetch method protocols
4 * Copyright 1997 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
20 #include <sys/socket.h>
22 #ifdef HAVE_PKG_hesiod
39 #include "fetchmail.h"
40 #include "getaddrinfo.h"
45 /* throw types for runtime errors */
46 #define THROW_TIMEOUT 1 /* server timed out */
48 /* magic values for the message length array */
49 #define MSGLEN_UNKNOWN 0 /* length unknown (0 is impossible) */
50 #define MSGLEN_INVALID -1 /* length passed back is invalid */
51 #define MSGLEN_TOOLARGE -2 /* message is too large */
52 #define MSGLEN_OLD -3 /* message is old */
54 int pass; /* how many times have we re-polled? */
55 int stage; /* where are we? */
56 int phase; /* where are we, for error-logging purposes? */
57 int batchcount; /* count of messages sent in current batch */
58 flag peek_capable; /* can we peek for better error recovery? */
59 int mailserver_socket_temp = -1; /* socket to free if connect timeout */
61 struct addrinfo *ai0, *ai1; /* clean these up after signal */
63 static volatile int timeoutcount = 0; /* count consecutive timeouts */
64 static volatile int idletimeout = 0; /* timeout occured in idle stage? */
66 static sigjmp_buf restart;
68 int is_idletimeout(void)
69 /* last timeout occured in idle stage? */
74 void resetidletimeout(void)
79 void set_timeout(int timeleft)
80 /* reset the nonresponse-timeout */
82 struct itimerval ntimeout;
87 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
88 ntimeout.it_value.tv_sec = timeleft;
89 ntimeout.it_value.tv_usec = 0;
90 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
93 static void timeout_handler (int signal)
94 /* handle SIGALRM signal indicating a server timeout */
97 if(stage != STAGE_IDLE) {
99 /* XXX FIXME: this siglongjmp must die - it's not safe to be
100 * called from a function handler and breaks, for instance,
102 siglongjmp(restart, THROW_TIMEOUT);
107 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
109 static int cleanupSockClose (int fd)
110 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
112 (void)SockTimeout(fd, CLEANUP_TIMEOUT); /* ignore errors */
113 return SockClose(fd);
117 /** authenticate to the server host using Kerberos V5 */
118 static int kerberos5_auth(int socket /** socket to server host */, const char *canonical /** server name */)
120 krb5_error_code retval;
121 krb5_context context;
123 krb5_principal client = NULL, server = NULL;
124 krb5_error *err_ret = NULL;
126 krb5_auth_context auth_context = NULL;
128 krb5_init_context(&context);
129 krb5_auth_con_init(context, &auth_context);
131 if ((retval = krb5_cc_default(context, &ccdef))) {
132 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
136 if ((retval = krb5_cc_get_principal(context, ccdef, &client))) {
137 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
141 if ((retval = krb5_sname_to_principal(context, canonical, "pop",
144 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
148 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
149 "KPOPV1.0", client, server,
150 AP_OPTS_MUTUAL_REQUIRED,
151 NULL, /* no data to checksum */
152 0, /* no creds, use ccache instead */
156 NULL); /* don't need reply */
158 krb5_free_principal(context, server);
159 krb5_free_principal(context, client);
160 krb5_auth_con_free(context, auth_context);
164 if (err_ret && err_ret->e_text) {
165 char *t = err_ret->e_text;
166 char *tt = sdump(t, strlen(t));
167 report(stderr, GT_("krb5_sendauth: %s [server says '%s']\n"),
168 error_message(retval), tt);
171 if (err_ret && err_ret->text.length) {
172 char *tt = sdump(err_ret->text.data, err_ret->text.length);
173 report(stderr, GT_("krb5_sendauth: %s [server says '%s']\n"),
174 error_message(retval), tt);
177 krb5_free_error(context, err_ret);
179 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
185 #endif /* KERBEROS_V5 */
187 static void clean_skipped_list(struct idlist **skipped_list)
188 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
190 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
191 prev = current = head = *skipped_list;
197 /* if item has no reference, remove it */
198 if (current && current->val.status.mark == 0)
200 if (current == head) /* remove first item (head) */
202 head = current->next;
203 if (current->id) free(current->id);
205 prev = current = head;
207 else /* remove middle/last item */
211 if (current->id) free(current->id);
216 else /* skip this item */
219 current = current->next;
223 *skipped_list = head;
226 static void send_size_warnings(struct query *ctl)
227 /* send warning mail with skipped msg; reset msg count when user notified */
230 int msg_to_send = FALSE;
231 struct idlist *head=NULL, *current=NULL;
232 int max_warning_poll_count;
238 /* don't start a notification message unless we need to */
239 for (current = head; current; current = current->next)
240 if (current->val.status.num == 0 && current->val.status.mark)
246 * There's no good way to recover if we can't send notification mail,
247 * but it's not a disaster, either, since the skipped mail will not
250 if (open_warning_by_mail(ctl))
252 stuff_warning(iana_charset, ctl,
253 GT_("Subject: Fetchmail oversized-messages warning"));
254 stuff_warning(NULL, ctl, "%s", "");
256 stuff_warning(NULL, ctl,
257 GT_("The following oversized messages were deleted on server %s account %s:"),
258 ctl->server.pollname, ctl->remotename);
260 stuff_warning(NULL, ctl,
261 GT_("The following oversized messages remain on server %s account %s:"),
262 ctl->server.pollname, ctl->remotename);
264 stuff_warning(NULL, ctl, "%s", "");
266 if (run.poll_interval == 0)
267 max_warning_poll_count = 0;
269 max_warning_poll_count = ctl->warnings/run.poll_interval;
271 /* parse list of skipped msg, adding items to the mail */
272 for (current = head; current; current = current->next)
274 if (current->val.status.num == 0 && current->val.status.mark)
276 nbr = current->val.status.mark;
277 size = atoi(current->id);
279 stuff_warning(NULL, ctl,
280 ngettext(" %d message %d octets long deleted by fetchmail.",
281 " %d messages %d octets long deleted by fetchmail.", nbr),
284 stuff_warning(NULL, ctl,
285 ngettext(" %d message %d octets long skipped by fetchmail.",
286 " %d messages %d octets long skipped by fetchmail.", nbr),
289 current->val.status.num++;
290 current->val.status.mark = 0;
292 if (current->val.status.num >= max_warning_poll_count)
293 current->val.status.num = 0;
296 stuff_warning(NULL, ctl, "%s", "");
298 close_warning_by_mail(ctl, (struct msgblk *)NULL);
301 static void mark_oversized(struct query *ctl, int size)
302 /* mark a message oversized */
304 struct idlist *current=NULL, *tmp=NULL;
308 /* convert size to string */
309 snprintf(sizestr, sizeof(sizestr), "%d", size);
311 /* build a list of skipped messages
312 * val.id = size of msg (string cnvt)
313 * val.status.num = warning_poll_count
314 * val.status.mask = nbr of msg this size
317 current = ctl->skipped;
319 /* initialise warning_poll_count to the
320 * current value so that all new msg will
321 * be included in the next mail
323 cnt = current ? current->val.status.num : 0;
325 /* if entry exists, increment the count */
326 if (current && (tmp = str_in_list(¤t, sizestr, FALSE)))
328 tmp->val.status.mark++;
330 /* otherwise, create a new entry */
331 /* initialise with current poll count */
334 tmp = save_str(&ctl->skipped, sizestr, 1);
335 tmp->val.status.num = cnt;
339 static int eat_trailer(int sock, struct query *ctl)
341 /* we only need this LF if we're printing ticker dots
342 * AND we are dumping protocol traces. */
343 if (outlevel >= O_VERBOSE && want_progress()) fputc('\n', stdout);
344 return (ctl->server.base_protocol->trail)(sock, ctl, tag);
347 static int fetch_messages(int mailserver_socket, struct query *ctl,
348 int count, int **msgsizes, int maxfetch,
349 int *fetches, int *dispatches, int *deletions)
350 /* fetch messages in lockstep mode */
352 flag force_retrieval;
353 int num, firstnum = 1, lastnum = 0, err, len;
354 int fetchsizelimit = ctl->fetchsizelimit;
356 int initialfetches = *fetches;
358 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit))
360 /* for POP3, we can get the size of one mail only! Unfortunately, this
361 * protocol specific test cannot be done elsewhere as the protocol
362 * could be "auto". */
363 switch (ctl->server.protocol)
365 case P_POP3: case P_APOP:
369 /* Time to allocate memory to store the sizes */
371 *msgsizes = (int *)xmalloc(sizeof(int) * fetchsizelimit);
375 * What forces this code is that in
376 * IMAP2bis you can't fetch a message without
377 * having it marked `seen'. In POP3 and IMAP4, on the
378 * other hand, you can (peek_capable is set by
379 * each driver module to convey this; it's not a
380 * method constant because of the difference between
381 * IMAP2bis and IMAP4, and because POP3 doesn't peek
382 * if fetchall is on).
384 * The result of being unable to peek is that if there's
385 * any kind of transient error (DNS lookup failure, or
386 * sendmail refusing delivery due to process-table limits)
387 * the message will be marked "seen" on the server without
388 * having been delivered. This is not a big problem if
389 * fetchmail is running in foreground, because the user
390 * will see a "skipped" message when it next runs and get
393 * But in daemon mode this leads to the message
394 * being silently ignored forever. This is not
397 * We compensate for this by checking the error
398 * count from the previous pass and forcing all
399 * messages to be considered new if it's nonzero.
401 force_retrieval = !peek_capable && (ctl->errcount > 0);
403 for (num = 1; num <= count; num++)
405 flag suppress_delete = FALSE;
406 flag suppress_forward = FALSE;
407 flag suppress_readbody = FALSE;
408 flag retained = FALSE;
409 int msgcode = MSGLEN_UNKNOWN;
411 /* check if the message is old
412 * Note: the size of the message may not be known here */
413 if (ctl->fetchall || force_retrieval) {
416 if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num)) {
417 msgcode = MSGLEN_OLD;
420 if (msgcode == MSGLEN_OLD)
423 * To avoid flooding the logs when using --keep, report
424 * skipping for old messages only when --flush is on.
426 if (outlevel > O_SILENT && ctl->flush)
429 GT_("skipping message %s@%s:%d"),
430 ctl->remotename, ctl->server.truename, num);
436 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit) &&
439 /* Instead of getting the sizes of all mails at the start, we get
440 * the sizes in blocks of fetchsizelimit. This leads to better
441 * performance when there are too many mails (say, 10000) in
442 * the mailbox and either we are not getting all the mails at
443 * one go (--fetchlimit 100) or there is a frequent socket
444 * error while just getting the sizes of all mails! */
447 int oldstage = stage;
449 lastnum = num + fetchsizelimit - 1;
452 for (i = 0; i < fetchsizelimit; i++)
455 stage = STAGE_GETSIZES;
456 err = (ctl->server.base_protocol->getpartialsizes)(mailserver_socket, num, lastnum, *msgsizes);
463 msgsize = *msgsizes ? (*msgsizes)[num-firstnum] : 0;
465 /* check if the message is oversized */
466 if (NUM_NONZERO(ctl->limit) && (msgsize > ctl->limit))
467 msgcode = MSGLEN_TOOLARGE;
468 /* else if (msgsize == 512)
469 msgcode = MSGLEN_OLD; (hmh) sample code to skip message */
473 if (msgcode == MSGLEN_TOOLARGE)
475 mark_oversized(ctl, msgsize);
476 if (!ctl->limitflush)
477 suppress_delete = TRUE;
479 if (outlevel > O_SILENT)
481 /* old messages are already handled above */
483 GT_("skipping message %s@%s:%d (%d octets)"),
484 ctl->remotename, ctl->server.truename, num,
490 * Invalid lengths are produced by Post Office/NT's
491 * annoying habit of randomly prepending bogus
492 * LIST items of length -1. Patrick Audley
493 * <paudley@pobox.com> tells us: LIST shows a
494 * size of -1, RETR and TOP return "-ERR
495 * System error - couldn't open message", and
496 * DELE succeeds but doesn't actually delete
499 report_build(stdout, GT_(" (length -1)"));
501 case MSGLEN_TOOLARGE:
502 report_build(stdout, GT_(" (oversized)"));
509 /* XXX FIXME: make this one variable, wholesize and
510 separatefetchbody query the same variable just with
512 flag wholesize = !ctl->server.base_protocol->fetch_body;
513 flag separatefetchbody = (ctl->server.base_protocol->fetch_body) ? TRUE : FALSE;
515 /* request a message */
516 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
517 if (err == PS_TRANSIENT) /* server is probably Exchange */
520 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)\n"),
521 ctl->remotename, ctl->server.truename, num,
528 /* -1 means we didn't see a size in the response */
535 if (outlevel > O_SILENT)
537 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
538 ctl->remotename, ctl->server.truename,
542 report_build(stdout, wholesize ? GT_(" (%d octets)")
543 : GT_(" (%d header octets)"), len);
544 if (want_progress()) {
545 /* flush and add a blank to append ticker dots */
546 report_flush(stdout);
552 * Read the message headers and ship them to the
555 err = readheaders(mailserver_socket, len, msgsize,
557 /* pass the suppress_readbody flag only if the underlying
558 * protocol does not fetch the body separately */
559 separatefetchbody ? 0 : &suppress_readbody);
561 if (err == PS_RETAINED)
562 suppress_forward = suppress_delete = retained = TRUE;
563 else if (err == PS_TRANSIENT)
564 suppress_delete = suppress_forward = TRUE;
565 else if (err == PS_REFUSED)
566 suppress_forward = TRUE;
570 /* tell server we got it OK and resynchronize */
571 if (separatefetchbody && ctl->server.base_protocol->trail)
573 err = eat_trailer(mailserver_socket, ctl);
574 if (err) return(err);
577 /* do not read the body which is not being forwarded only if
578 * the underlying protocol allows the body to be fetched
580 if (separatefetchbody && suppress_forward)
581 suppress_readbody = TRUE;
584 * If we're using IMAP4 or something else that
585 * can fetch headers separately from bodies,
586 * it's time to request the body now. This
587 * fetch may be skipped if we got an anti-spam
588 * or other PS_REFUSED error response during
591 if (!suppress_readbody)
593 if (separatefetchbody)
596 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len))) {
597 if (err == PS_ERROR && ctl->server.retrieveerror) {
599 * Mark a message with a protocol error as seen.
600 * This can be used to see which messages we've attempted
601 * to download, but failed.
603 if (ctl->server.retrieveerror == RE_MARKSEEN) {
604 if ((ctl->server.base_protocol->mark_seen)(mailserver_socket,ctl,num)) {
609 if (ctl->server.retrieveerror != RE_ABORT) {
611 * Do not abort download session. Continue with the next message.
613 * Prevents a malformed message from blocking all other messages
614 * behind it in the mailbox from being downloaded.
616 * Reconnect to SMTP to force this incomplete message to be dropped.
617 * Required because we've already begun the DATA portion of the
618 * interaction with the SMTP server (commands are ignored/
619 * considered part of the message data).
621 abort_message_sink(ctl);
623 // Ensure we don't delete the failed message from the server.
624 suppress_delete = TRUE;
626 // Bookkeeping required before next message can be downloaded.
635 * Work around a bug in Novell's
636 * broken GroupWise IMAP server;
637 * its body FETCH response is missing
638 * the required length for the data
639 * string. This violates RFC2060.
642 len = msgsize - msgblk.msglen;
644 if (outlevel > O_SILENT)
646 GT_(" (%d body octets)"), len);
647 if (want_progress()) {
648 report_flush(stdout);
654 /* process the body now */
655 err = readbody(mailserver_socket,
660 if (err == PS_TRANSIENT)
661 suppress_delete = suppress_forward = TRUE;
665 /* tell server we got it OK and resynchronize */
666 if (ctl->server.base_protocol->trail) {
667 err = eat_trailer(mailserver_socket, ctl);
668 if (err) return(err);
672 /* count # messages forwarded on this pass */
673 if (!suppress_forward)
677 * Check to see if the numbers matched?
679 * Yes, some servers foo this up horribly.
680 * All IMAP servers seem to get it right, and
681 * so does Eudora QPOP at least in 2.xx
684 * Microsoft Exchange gets it completely
685 * wrong, reporting compressed rather than
686 * actual sizes (so the actual length of
687 * message is longer than the reported size).
688 * Another fine example of Microsoft brain death!
690 * Some older POP servers, like the old UCB
691 * POP server and the pre-QPOP QUALCOMM
692 * versions, report a longer size in the LIST
693 * response than actually gets shipped up.
694 * It's unclear what is going on here, as the
695 * QUALCOMM server (at least) seems to be
696 * reporting the on-disk size correctly.
698 * qmail-pop3d also goofs up message sizes and does not
699 * count the line end characters properly.
701 if (msgblk.msglen != msgsize)
703 if (outlevel >= O_DEBUG)
705 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
706 ctl->remotename, ctl->server.truename, num,
707 msgblk.msglen, msgsize);
710 /* end-of-message processing starts here */
711 if (!close_sink(ctl, &msgblk, !suppress_forward))
714 suppress_delete = TRUE;
722 * At this point in flow of control,
723 * either we've bombed on a protocol error
724 * or had delivery refused by the SMTP server
725 * or we've seen `accepted for delivery' and the message is shipped.
726 * It's safe to mark the message seen and delete it on the server now.
729 /* in softbounce mode, suppress deletion and marking as seen */
730 if (suppress_forward)
731 suppress_delete = suppress_delete || run.softbounce;
733 /* maybe we delete this message now? */
736 if (outlevel > O_SILENT)
737 report_complete(stdout, GT_(" retained\n"));
739 else if (ctl->server.base_protocol->delete_msg
741 && ((msgcode >= 0 && !ctl->keep)
742 || (msgcode == MSGLEN_OLD && ctl->flush)
743 || (msgcode == MSGLEN_TOOLARGE && ctl->limitflush)))
746 if (outlevel > O_SILENT)
747 report_complete(stdout, GT_(" flushed\n"));
748 err = (ctl->server.base_protocol->delete_msg)(mailserver_socket, ctl, num);
755 * To avoid flooding the logs when using --keep, report
756 * skipping of new messages only.
758 if (outlevel > O_SILENT && msgcode != MSGLEN_OLD)
759 report_complete(stdout, GT_(" not flushed\n"));
761 /* maybe we mark this message as seen now? */
762 if (ctl->server.base_protocol->mark_seen
764 && (msgcode >= 0 && ctl->keep))
766 err = (ctl->server.base_protocol->mark_seen)(mailserver_socket, ctl, num);
772 /* perhaps this as many as we're ready to handle */
773 if (maxfetch && maxfetch <= *fetches && num < count)
775 int remcount = count - (*fetches - initialfetches);
777 ngettext("fetchlimit %d reached; %d message left on server %s account %s\n",
778 "fetchlimit %d reached; %d messages left on server %s account %s\n", remcount),
779 maxfetch, remcount, ctl->server.truename, ctl->remotename);
782 } /* for (num = 1; num <= count; num++) */
787 /* retrieve messages from server using given protocol method table */
788 static int do_session(
789 /* parsed options with merged-in defaults */
791 /* protocol method table */
792 const struct method *proto,
793 /* maximum number of messages to fetch */
796 static int *msgsizes;
797 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
799 int deletions = 0, js;
801 SIGHANDLERTYPE alrmsave;
803 ctl->server.base_protocol = proto;
808 init_transact(proto);
810 /* set up the server-nonresponse timeout */
811 alrmsave = set_signal_handler(SIGALRM, timeout_handler);
812 mytimeout = ctl->server.timeout;
814 if ((js = sigsetjmp(restart,1)))
816 /* exception caught */
819 sigfillset(&allsigs);
820 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
823 fm_freeaddrinfo(ai0); ai0 = NULL;
827 fm_freeaddrinfo(ai1); ai1 = NULL;
830 if (js == THROW_TIMEOUT)
832 if (phase == OPEN_WAIT)
834 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
835 ctl->server.timeout, ctl->server.pollname);
836 else if (phase == SERVER_WAIT)
838 GT_("timeout after %d seconds waiting for server %s.\n"),
839 ctl->server.timeout, ctl->server.pollname);
840 else if (phase == FORWARDING_WAIT)
842 GT_("timeout after %d seconds waiting for %s.\n"),
844 ctl->mda ? "MDA" : "SMTP");
845 else if (phase == LISTENER_WAIT)
847 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
850 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
853 * If we've exceeded our threshold for consecutive timeouts,
854 * try to notify the user, then mark the connection wedged.
855 * Don't do this if the connection can idle, though; idle
856 * timeouts just mean the frequency of mail is low.
858 if (timeoutcount > MAX_TIMEOUTS
859 && !open_warning_by_mail(ctl))
861 stuff_warning(iana_charset, ctl,
862 GT_("Subject: fetchmail sees repeated timeouts"));
863 stuff_warning(NULL, ctl, "%s", "");
864 stuff_warning(NULL, ctl,
865 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\n"),
867 ctl->remotename, ctl->server.truename);
868 stuff_warning(NULL, ctl,
869 GT_("This could mean that your mailserver is stuck, or that your SMTP\n" \
870 "server is wedged, or that your mailbox file on the server has been\n" \
871 "corrupted by a server error. You can run `fetchmail -v -v' to\n" \
872 "diagnose the problem.\n\n" \
873 "Fetchmail won't poll this mailbox again until you restart it.\n"));
874 close_warning_by_mail(ctl, (struct msgblk *)NULL);
884 /* sigsetjmp returned zero -> normal operation */
885 char buf[MSGBUFSIZE+1], *realhost;
886 int count, newm, bytes;
887 int fetches, dispatches, oldphase;
890 /* execute pre-initialization command, if any */
891 if (ctl->preconnect && (err = system(ctl->preconnect)))
893 if (WIFSIGNALED(err))
895 GT_("pre-connection command terminated with signal %d\n"), WTERMSIG(err));
898 GT_("pre-connection command failed with status %d\n"), WEXITSTATUS(err));
903 /* open a socket to the mail server */
906 set_timeout(mytimeout);
908 #ifdef HAVE_PKG_hesiod
909 /* If either the pollname or vianame are "hesiod" we want to
910 lookup the user's hesiod pobox host */
911 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
912 struct hes_postoffice *hes_p;
913 hes_p = hes_getmailhost(ctl->remotename);
914 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
915 free(ctl->server.queryname);
916 ctl->server.queryname = xstrdup(hes_p->po_host);
918 free(ctl->server.via);
919 ctl->server.via = xstrdup(hes_p->po_host);
922 GT_("couldn't find HESIOD pobox for %s\n"),
929 * Canonicalize the server truename for later use. This also
930 * functions as a probe for whether the mailserver is accessible.
931 * We try it on each poll cycle until we get a result. This way,
932 * fetchmail won't fail if started up when the network is inaccessible.
934 if (ctl->server.dns && !ctl->server.trueaddr)
936 if (ctl->server.lead_server)
938 char *leadname = ctl->server.lead_server->truename;
940 /* prevent core dump from ill-formed or duplicate entry */
943 report(stderr, GT_("Lead server has no name.\n"));
950 xfree(ctl->server.truename);
951 ctl->server.truename = xstrdup(leadname);
955 struct addrinfo hints, *res;
958 memset(&hints, 0, sizeof(hints));
959 hints.ai_socktype = SOCK_STREAM;
960 hints.ai_family = AF_UNSPEC;
961 hints.ai_flags = AI_CANONNAME;
963 hints.ai_flags |= AI_ADDRCONFIG;
966 error = fm_getaddrinfo(ctl->server.queryname, NULL, &hints, &res);
970 GT_("couldn't find canonical DNS name of %s (%s): %s\n"),
971 ctl->server.pollname, ctl->server.queryname,
972 gai_strerror(error));
980 xfree(ctl->server.truename);
981 /* Older FreeBSD versions return NULL in ai_canonname
982 * if they cannot canonicalize, rather than copying
983 * the queryname here, as IEEE Std 1003.1-2001
984 * requires. Work around NULL. */
985 if (res->ai_canonname != NULL) {
986 ctl->server.truename = xstrdup(res->ai_canonname);
988 ctl->server.truename = xstrdup(ctl->server.queryname);
990 ctl->server.trueaddr = (struct sockaddr *)xmalloc(res->ai_addrlen);
991 ctl->server.trueaddr_len = res->ai_addrlen;
992 memcpy(ctl->server.trueaddr, res->ai_addr, res->ai_addrlen);
993 fm_freeaddrinfo(res);
998 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
1000 /* allow time for the port to be set up if we have a plugin */
1001 if (ctl->server.plugin)
1003 if ((mailserver_socket = SockOpen(realhost,
1004 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
1005 ctl->server.plugin, &ai0)) == -1)
1007 char errbuf[BUFSIZ];
1010 * Avoid generating a bogus error every poll cycle when we're
1011 * in daemon mode but the connection to the outside world
1014 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
1015 && run.poll_interval))
1017 report_build(stderr, GT_("%s connection to %s failed"),
1018 ctl->server.base_protocol->name, ctl->server.pollname);
1019 strlcpy(errbuf, strerror(err_no), sizeof(errbuf));
1020 report_complete(stderr, ": %s\n", errbuf);
1029 /* Save the socket opened. Useful if Fetchmail hangs on SSLOpen
1030 * because the socket can be closed.
1032 mailserver_socket_temp = mailserver_socket;
1033 set_timeout(mytimeout);
1035 /* perform initial SSL handshake on open connection */
1037 SSLOpen(mailserver_socket, ctl->sslcert, ctl->sslkey,
1038 ctl->sslproto, ctl->sslcertck,
1039 ctl->sslcertfile, ctl->sslcertpath,
1040 ctl->sslfingerprint, ctl->sslcommonname ?
1041 ctl->sslcommonname : realhost, ctl->server.pollname,
1042 &ctl->remotename) == -1)
1045 report(stderr, GT_("SSL connection failed.\n"));
1050 /* Fetchmail didn't hang on SSLOpen,
1051 * then no need to set mailserver_socket_temp
1053 mailserver_socket_temp = -1;
1056 /* A timeout is still defined before SSLOpen,
1057 * then Fetchmail hanging on SSLOpen is handled.
1063 if (ctl->server.authenticate == A_KERBEROS_V5)
1065 set_timeout(mytimeout);
1066 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1071 #endif /* KERBEROS_V5 */
1073 /* accept greeting message from mail server */
1074 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1078 /* try to get authorized to fetch mail */
1079 stage = STAGE_GETAUTH;
1080 if (ctl->server.base_protocol->getauth)
1082 set_timeout(mytimeout);
1083 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1088 if (err == PS_LOCKBUSY)
1089 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1091 ctl->server.truename);
1092 else if (err == PS_SERVBUSY)
1093 report(stderr, GT_("Server busy error on %s@%s\n"),
1095 ctl->server.truename);
1096 else if (err == PS_AUTHFAIL)
1098 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1100 ctl->server.truename,
1101 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1103 if (ctl->server.authenticate == A_ANY && !ctl->wehaveauthed) {
1104 report(stderr, GT_("For help, see http://www.fetchmail.info/fetchmail-FAQ.html#R15\n"));
1108 * If we're running in background, try to mail the
1109 * calling user a heads-up about the authentication
1110 * failure once it looks like this isn't a fluke
1111 * due to the server being temporarily inaccessible.
1112 * When we get third succesive failure, we notify the user
1113 * but only if we haven't already managed to get
1114 * authorization. After that, once we get authorization
1115 * we let the user know service is restored.
1117 if (run.poll_interval
1118 && !ctl->wehavesentauthnote
1119 && ((ctl->wehaveauthed && ++ctl->authfailcount >= 10)
1120 || (!ctl->wehaveauthed && ++ctl->authfailcount >= 3))
1121 && !open_warning_by_mail(ctl))
1123 ctl->wehavesentauthnote = 1;
1124 stuff_warning(iana_charset, ctl,
1125 GT_("Subject: fetchmail authentication failed on %s@%s"),
1126 ctl->remotename, ctl->server.truename);
1127 stuff_warning(NULL, ctl, "%s", "");
1128 stuff_warning(NULL, ctl,
1129 GT_("Fetchmail could not get mail from %s@%s.\n"),
1131 ctl->server.truename);
1132 if (ctl->wehaveauthed) {
1133 stuff_warning(NULL, ctl, GT_("\
1134 The attempt to get authorization failed.\n\
1135 Since we have already succeeded in getting authorization for this\n\
1136 connection, this is probably another failure mode (such as busy server)\n\
1137 that fetchmail cannot distinguish because the server didn't send a useful\n\
1139 stuff_warning(NULL, ctl, GT_("\
1141 However, if you HAVE changed your account details since starting the\n\
1142 fetchmail daemon, you need to stop the daemon, change your configuration\n\
1143 of fetchmail, and then restart the daemon.\n\
1145 The fetchmail daemon will continue running and attempt to connect\n\
1146 at each cycle. No future notifications will be sent until service\n\
1149 stuff_warning(NULL, ctl, GT_("\
1150 The attempt to get authorization failed.\n\
1151 This probably means your password is invalid, but some servers have\n\
1152 other failure modes that fetchmail cannot distinguish from this\n\
1153 because they don't send useful error messages on login failure.\n\
1155 The fetchmail daemon will continue running and attempt to connect\n\
1156 at each cycle. No future notifications will be sent until service\n\
1159 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1162 else if (err == PS_REPOLL)
1164 if (outlevel >= O_VERBOSE)
1165 report(stderr, GT_("Repoll immediately on %s@%s\n"),
1167 ctl->server.truename);
1170 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1172 ctl->server.truename);
1179 * This connection has given us authorization at least once.
1181 * There are dodgy server (clubinternet.fr for example) that
1182 * give spurious authorization failures on patently good
1183 * account/password details, then 5 minutes later let you in!
1185 * This is meant to build in some tolerance of such nasty bits
1188 ctl->wehaveauthed = 1;
1189 /*if (ctl->authfailcount >= 3)*/
1190 if (ctl->wehavesentauthnote)
1192 ctl->wehavesentauthnote = 0;
1194 GT_("Authorization OK on %s@%s\n"),
1196 ctl->server.truename);
1197 if (!open_warning_by_mail(ctl))
1199 stuff_warning(iana_charset, ctl,
1200 GT_("Subject: fetchmail authentication OK on %s@%s"),
1201 ctl->remotename, ctl->server.truename);
1202 stuff_warning(NULL, ctl, "%s", "");
1203 stuff_warning(NULL, ctl,
1204 GT_("Fetchmail was able to log into %s@%s.\n"),
1206 ctl->server.truename);
1207 stuff_warning(NULL, ctl,
1208 GT_("Service has been restored.\n"));
1209 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1214 * Reporting only after the first three
1215 * consecutive failures, or ten consecutive
1216 * failures after we have managed to get
1219 ctl->authfailcount = 0;
1223 ctl->errcount = fetches = 0;
1225 /* now iterate over each folder selected */
1226 for (idp = ctl->mailboxes; idp; idp = idp->next)
1228 ctl->folder = idp->id;
1234 /* reset timeout, in case we did an IDLE */
1235 mytimeout = ctl->server.timeout;
1237 if (outlevel >= O_DEBUG)
1240 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1242 report(stdout, GT_("selecting or re-polling default folder\n"));
1245 /* compute # of messages and number of new messages waiting */
1246 stage = STAGE_GETRANGE;
1247 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &newm, &bytes);
1251 /* show user how many messages we downloaded */
1253 (void) snprintf(buf, sizeof(buf),
1254 GT_("%s at %s (folder %s)"),
1255 ctl->remotename, ctl->server.pollname, idp->id);
1257 (void) snprintf(buf, sizeof(buf), GT_("%s at %s"),
1258 ctl->remotename, ctl->server.pollname);
1259 if (outlevel > O_SILENT)
1261 if (count == -1) /* only used for ETRN */
1262 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1263 else if (count != 0)
1265 if (newm != -1 && (count - newm) > 0)
1266 report_build(stdout, ngettext("%d message (%d %s) for %s", "%d messages (%d %s) for %s", (unsigned long)count),
1269 ngettext("seen", "seen", (unsigned long)count-newm),
1272 report_build(stdout, ngettext("%d message for %s",
1273 "%d messages for %s",
1277 report_complete(stdout, ".\n");
1279 report_complete(stdout, GT_(" (%d octets).\n"), bytes);
1283 /* these are pointless in normal daemon mode */
1284 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1285 report(stdout, GT_("No mail for %s\n"), buf);
1289 /* very important, this is where we leave the do loop */
1295 if (newm == -1 || ctl->fetchall)
1297 fetches = newm; /* set error status correctly */
1299 * There used to be a `goto noerror' here, but this
1300 * prevented checking of multiple folders. This
1301 * comment is a reminder in case I introduced some
1302 * subtle bug by removing it...
1310 * Don't trust the message count passed by the server.
1311 * Without this check, it might be possible to do a
1312 * DNS-spoofing attack that would pass back a ridiculous
1313 * count, and allocate a malloc area that would overlap
1314 * a portion of the stack.
1316 if ((unsigned)count > INT_MAX/sizeof(int))
1318 report(stderr, GT_("bogus message count!"));
1324 * We need the size of each message before it's
1325 * loaded in order to pass it to the ESMTP SIZE
1326 * option. If the protocol has a getsizes method,
1327 * we presume this means it doesn't get reliable
1328 * sizes from message fetch responses.
1330 * If the protocol supports getting sizes of subset of
1331 * messages, we skip this step now.
1333 if (proto->getsizes &&
1334 !(proto->getpartialsizes && NUM_NONZERO(ctl->fetchsizelimit)))
1337 msgsizes = (int *)xmalloc(sizeof(int) * count);
1338 for (i = 0; i < count; i++)
1341 stage = STAGE_GETSIZES;
1342 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1349 for (i = 0; i < count; i++)
1350 bytes += msgsizes[i];
1354 /* read, forward, and delete messages */
1355 stage = STAGE_FETCH;
1357 /* fetch in lockstep mode */
1358 err = fetch_messages(mailserver_socket, ctl,
1361 &fetches, &dispatches, &deletions);
1362 if (err != PS_SUCCESS && err != PS_MAXFETCH)
1365 if (!check_only && ctl->skipped
1366 && run.poll_interval > 0 && !nodetach)
1368 clean_skipped_list(&ctl->skipped);
1369 send_size_warnings(ctl);
1373 /* end-of-mailbox processing before we repoll or switch to another one */
1374 if (ctl->server.base_protocol->end_mailbox_poll)
1376 tmperr = (ctl->server.base_protocol->end_mailbox_poll)(mailserver_socket, ctl);
1382 /* Return now if we have reached the fetchlimit */
1383 if (maxfetch && maxfetch <= fetches)
1387 * Only repoll if we either had some actual forwards
1388 * or are idling for new mails and had no errors.
1389 * Otherwise it is far too easy to get into infinite loops.
1391 (ctl->server.base_protocol->retry && (dispatches || ctl->idle) && !ctl->errcount);
1394 /* XXX: From this point onwards, preserve err unless a new error has occurred */
1397 /* PS_SUCCESS, PS_MAXFETCH: ordinary termination with no errors -- officially log out */
1398 stage = STAGE_LOGOUT;
1399 tmperr = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1400 if (tmperr != PS_SUCCESS)
1403 * Hmmmm...arguably this would be incorrect if we had fetches but
1404 * no dispatches (due to oversized messages, etc.)
1406 else if (err == PS_SUCCESS && fetches == 0)
1409 * Close all SMTP delivery sockets. For optimum performance
1410 * we'd like to hold them open til end of run, but (1) this
1411 * loses if our poll interval is longer than the MTA's
1412 * inactivity timeout, and (2) some MTAs (like smail) don't
1413 * deliver after each message, but rather queue up mail and
1414 * wait to actually deliver it until the input socket is
1417 * don't send QUIT for ODMR case because we're acting as a
1418 * proxy between the SMTP server and client.
1420 smtp_close(ctl, ctl->server.protocol != P_ODMR);
1421 cleanupSockClose(mailserver_socket);
1425 /* we only get here on error */
1426 if (err != 0 && err != PS_SOCKET && err != PS_REPOLL)
1428 stage = STAGE_LOGOUT;
1429 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1432 /* try to clean up all streams */
1435 * Sending SMTP QUIT on signal is theoretically nice, but led
1436 * to a subtle bug. If fetchmail was terminated by signal
1437 * while it was shipping message text, it would hang forever
1438 * waiting for a command acknowledge. In theory we could
1439 * enable the QUIT only outside of the message send. In
1440 * practice, we don't care. All mailservers hang up on a
1441 * dropped TCP/IP connection anyway.
1444 if (mailserver_socket != -1) {
1445 cleanupSockClose(mailserver_socket);
1447 /* If there was a connect timeout, the socket should be closed.
1448 * mailserver_socket_temp contains the socket to close.
1450 if (mailserver_socket_temp != -1) {
1451 cleanupSockClose(mailserver_socket_temp);
1452 mailserver_socket_temp = -1;
1456 /* no report on PS_AUTHFAIL */
1461 msg = GT_("socket");
1464 msg = GT_("missing or bad RFC822 header");
1470 msg = GT_("client/server synchronization");
1473 msg = GT_("client/server protocol");
1476 msg = GT_("lock busy on server");
1479 msg = GT_("SMTP transaction");
1482 msg = GT_("DNS lookup");
1485 msg = GT_("undefined");
1489 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT
1491 report(stderr, GT_("%s error while fetching from %s@%s and delivering to SMTP host %s\n"),
1492 msg, ctl->remotename, ctl->server.pollname,
1493 ctl->smtphost ? ctl->smtphost : GT_("unknown"));
1495 report(stderr, GT_("%s error while fetching from %s@%s\n"),
1496 msg, ctl->remotename, ctl->server.pollname);
1503 /* execute wrapup command, if any */
1504 if (ctl->postconnect && (tmperr = system(ctl->postconnect)))
1506 if (WIFSIGNALED(tmperr))
1507 report(stderr, GT_("post-connection command terminated with signal %d\n"), WTERMSIG(tmperr));
1509 report(stderr, GT_("post-connection command failed with status %d\n"), WEXITSTATUS(tmperr));
1510 if (err == PS_SUCCESS)
1514 set_timeout(0); /* cancel any pending alarm */
1515 set_signal_handler(SIGALRM, alrmsave);
1519 /** retrieve messages from server using given protocol method table */
1520 int do_protocol(struct query *ctl /** parsed options with merged-in defaults */,
1521 const struct method *proto /** protocol method table */)
1526 if (ctl->server.authenticate == A_KERBEROS_V5)
1528 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1531 #endif /* KERBEROS_V5 */
1533 /* lacking methods, there are some options that may fail */
1536 /* check for unsupported options */
1539 GT_("Option --flush is not supported with %s\n"),
1543 else if (ctl->fetchall) {
1545 GT_("Option --all is not supported with %s\n"),
1550 if (!(proto->getsizes || proto->getpartialsizes)
1551 && NUM_SPECIFIED(ctl->limit))
1554 GT_("Option --limit is not supported with %s\n"),
1560 * If no expunge limit or we do expunges within the driver,
1561 * then just do one session, passing in any fetchlimit.
1563 if ((ctl->keep && !ctl->flush) ||
1564 proto->retry || !NUM_SPECIFIED(ctl->expunge))
1565 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1567 * There's an expunge limit, and it isn't handled in the driver itself.
1568 * OK; do multiple sessions, each fetching a limited # of messages.
1569 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1570 * (if it was nonzero).
1576 int expunge = NUM_VALUE_OUT(ctl->expunge);
1577 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1580 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1581 expunge = fetchlimit - totalcount;
1582 err = do_session(ctl, proto, expunge);
1583 totalcount += expunge;
1584 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1586 if (err != PS_LOCKBUSY)
1588 else if (lockouts >= MAX_LOCKOUTS)
1590 else /* err == PS_LOCKBUSY */
1593 * Allow time for the server lock to release. if we
1594 * don't do this, we'll often hit a locked-mailbox
1595 * condition and fail.
1601 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1608 /* driver.c ends here */