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.
15 #endif /* HAVE_MEMORY_H */
16 #if defined(STDC_HEADERS)
20 #if defined(HAVE_UNISTD_H)
23 #if defined(HAVE_SYS_ITIMER_H)
24 #include <sys/itimer.h>
27 #ifdef HAVE_SYS_WAIT_H
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NET_SOCKET_H
35 #include <net/socket.h>
38 #ifdef HAVE_PKG_hesiod
46 #include <netinet/in.h>
47 #endif /* KERBEROS_V4 */
52 #include "fetchmail.h"
53 #include "getaddrinfo.h"
58 /* throw types for runtime errors */
59 #define THROW_TIMEOUT 1 /* server timed out */
61 /* magic values for the message length array */
62 #define MSGLEN_UNKNOWN 0 /* length unknown (0 is impossible) */
63 #define MSGLEN_INVALID -1 /* length passed back is invalid */
64 #define MSGLEN_TOOLARGE -2 /* message is too large */
65 #define MSGLEN_OLD -3 /* message is old */
67 int pass; /* how many times have we re-polled? */
68 int stage; /* where are we? */
69 int phase; /* where are we, for error-logging purposes? */
70 int batchcount; /* count of messages sent in current batch */
71 flag peek_capable; /* can we peek for better error recovery? */
72 int mailserver_socket_temp = -1; /* socket to free if connect timeout */
74 struct addrinfo *ai0, *ai1; /* clean these up after signal */
76 static volatile int timeoutcount = 0; /* count consecutive timeouts */
77 static volatile int idletimeout = 0; /* timeout occured in idle stage? */
79 static sigjmp_buf restart;
81 int is_idletimeout(void)
82 /* last timeout occured in idle stage? */
87 void resetidletimeout(void)
92 void set_timeout(int timeleft)
93 /* reset the nonresponse-timeout */
95 #if !defined(__EMX__) && !defined(__BEOS__)
96 struct itimerval ntimeout;
101 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
102 ntimeout.it_value.tv_sec = timeleft;
103 ntimeout.it_value.tv_usec = 0;
104 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
108 static RETSIGTYPE timeout_handler (int signal)
109 /* handle SIGALRM signal indicating a server timeout */
112 if(stage != STAGE_IDLE) {
114 /* XXX FIXME: this siglongjmp must die - it's not safe to be
115 * called from a function handler and breaks, for instance,
117 siglongjmp(restart, THROW_TIMEOUT);
122 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
124 static int cleanupSockClose (int fd)
125 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
128 SIGHANDLERTYPE alrmsave;
129 alrmsave = set_signal_handler(SIGALRM, null_signal_handler);
130 set_timeout(CLEANUP_TIMEOUT);
131 scerror = SockClose(fd);
133 set_signal_handler(SIGALRM, alrmsave);
138 static int kerberos_auth(socket, canonical, principal)
139 /* authenticate to the server host using Kerberos V4 */
140 int socket; /* socket to server host */
141 char *canonical; /* server name */
147 Key_schedule schedule;
149 char * prin_copy = (char *) NULL;
150 char * prin = (char *) NULL;
151 char * inst = (char *) NULL;
152 char * realm = (char *) NULL;
154 if (principal != (char *)NULL && *principal)
157 prin = prin_copy = xstrdup(principal);
158 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
164 while (*cp && *cp != '@')
174 ticket = xmalloc(sizeof (KTEXT_ST));
175 rem = (krb_sendauth (0L, socket, ticket,
177 inst ? inst : canonical,
178 realm ? realm : ((char *) (krb_realmofhost (canonical))),
183 ((struct sockaddr_in *) 0),
184 ((struct sockaddr_in *) 0),
193 report(stderr, GT_("kerberos error %s\n"), (krb_get_err_text (rem)));
194 return (PS_AUTHFAIL);
198 #endif /* KERBEROS_V4 */
201 static int kerberos5_auth(socket, canonical)
202 /* authenticate to the server host using Kerberos V5 */
203 int socket; /* socket to server host */
204 const char *canonical; /* server name */
206 krb5_error_code retval;
207 krb5_context context;
209 krb5_principal client = NULL, server = NULL;
210 krb5_error *err_ret = NULL;
212 krb5_auth_context auth_context = NULL;
214 krb5_init_context(&context);
215 krb5_auth_con_init(context, &auth_context);
217 if ((retval = krb5_cc_default(context, &ccdef))) {
218 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
222 if ((retval = krb5_cc_get_principal(context, ccdef, &client))) {
223 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
227 if ((retval = krb5_sname_to_principal(context, canonical, "pop",
230 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
234 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
235 "KPOPV1.0", client, server,
236 AP_OPTS_MUTUAL_REQUIRED,
237 NULL, /* no data to checksum */
238 0, /* no creds, use ccache instead */
242 NULL); /* don't need reply */
244 krb5_free_principal(context, server);
245 krb5_free_principal(context, client);
246 krb5_auth_con_free(context, auth_context);
250 if (err_ret && err_ret->e_text) {
251 char *t = err_ret->e_text;
252 char *tt = sdump(t, strlen(t));
253 report(stderr, GT_("krb5_sendauth: %s [server says '%s']\n"),
254 error_message(retval), tt);
257 if (err_ret && err_ret->text.length) {
258 char *tt = sdump(err_ret->text.data, err_ret->text.length);
259 report(stderr, GT_("krb5_sendauth: %s [server says '%s']\n"),
260 error_message(retval), tt);
263 krb5_free_error(context, err_ret);
265 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
271 #endif /* KERBEROS_V5 */
273 static void clean_skipped_list(struct idlist **skipped_list)
274 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
276 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
277 prev = current = head = *skipped_list;
283 /* if item has no reference, remove it */
284 if (current && current->val.status.mark == 0)
286 if (current == head) /* remove first item (head) */
288 head = current->next;
289 if (current->id) free(current->id);
291 prev = current = head;
293 else /* remove middle/last item */
297 if (current->id) free(current->id);
302 else /* skip this item */
305 current = current->next;
309 *skipped_list = head;
312 static void send_size_warnings(struct query *ctl)
313 /* send warning mail with skipped msg; reset msg count when user notified */
316 int msg_to_send = FALSE;
317 struct idlist *head=NULL, *current=NULL;
318 int max_warning_poll_count;
324 /* don't start a notification message unless we need to */
325 for (current = head; current; current = current->next)
326 if (current->val.status.num == 0 && current->val.status.mark)
332 * There's no good way to recover if we can't send notification mail,
333 * but it's not a disaster, either, since the skipped mail will not
336 if (open_warning_by_mail(ctl))
338 stuff_warning(iana_charset, ctl,
339 GT_("Subject: Fetchmail oversized-messages warning"));
340 stuff_warning(NULL, ctl, "%s", "");
342 stuff_warning(NULL, ctl,
343 GT_("The following oversized messages were deleted on server %s account %s:"),
344 ctl->server.pollname, ctl->remotename);
346 stuff_warning(NULL, ctl,
347 GT_("The following oversized messages remain on server %s account %s:"),
348 ctl->server.pollname, ctl->remotename);
350 stuff_warning(NULL, ctl, "%s", "");
352 if (run.poll_interval == 0)
353 max_warning_poll_count = 0;
355 max_warning_poll_count = ctl->warnings/run.poll_interval;
357 /* parse list of skipped msg, adding items to the mail */
358 for (current = head; current; current = current->next)
360 if (current->val.status.num == 0 && current->val.status.mark)
362 nbr = current->val.status.mark;
363 size = atoi(current->id);
365 stuff_warning(NULL, ctl,
366 ngettext(" %d message %d octets long deleted by fetchmail.",
367 " %d messages %d octets long deleted by fetchmail.", nbr),
370 stuff_warning(NULL, ctl,
371 ngettext(" %d message %d octets long skipped by fetchmail.",
372 " %d messages %d octets long skipped by fetchmail.", nbr),
375 current->val.status.num++;
376 current->val.status.mark = 0;
378 if (current->val.status.num >= max_warning_poll_count)
379 current->val.status.num = 0;
382 stuff_warning(NULL, ctl, "%s", "");
384 close_warning_by_mail(ctl, (struct msgblk *)NULL);
387 static void mark_oversized(struct query *ctl, int size)
388 /* mark a message oversized */
390 struct idlist *current=NULL, *tmp=NULL;
394 /* convert size to string */
395 snprintf(sizestr, sizeof(sizestr), "%d", size);
397 /* build a list of skipped messages
398 * val.id = size of msg (string cnvt)
399 * val.status.num = warning_poll_count
400 * val.status.mask = nbr of msg this size
403 current = ctl->skipped;
405 /* initialise warning_poll_count to the
406 * current value so that all new msg will
407 * be included in the next mail
409 cnt = current ? current->val.status.num : 0;
411 /* if entry exists, increment the count */
412 if (current && (tmp = str_in_list(¤t, sizestr, FALSE)))
414 tmp->val.status.mark++;
416 /* otherwise, create a new entry */
417 /* initialise with current poll count */
420 tmp = save_str(&ctl->skipped, sizestr, 1);
421 tmp->val.status.num = cnt;
425 static int eat_trailer(int sock, struct query *ctl)
427 /* we only need this LF if we're printing ticker dots
428 * AND we are dumping protocol traces. */
429 if (outlevel >= O_VERBOSE && want_progress()) fputc('\n', stdout);
430 return (ctl->server.base_protocol->trail)(sock, ctl, tag);
433 static int fetch_messages(int mailserver_socket, struct query *ctl,
434 int count, int **msgsizes, int maxfetch,
435 int *fetches, int *dispatches, int *deletions)
436 /* fetch messages in lockstep mode */
438 flag force_retrieval;
439 int num, firstnum = 1, lastnum = 0, err, len;
440 int fetchsizelimit = ctl->fetchsizelimit;
442 int initialfetches = *fetches;
444 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit))
446 /* for POP3, we can get the size of one mail only! Unfortunately, this
447 * protocol specific test cannot be done elsewhere as the protocol
448 * could be "auto". */
449 switch (ctl->server.protocol)
451 case P_POP3: case P_APOP: case P_RPOP:
455 /* Time to allocate memory to store the sizes */
457 *msgsizes = (int *)xmalloc(sizeof(int) * fetchsizelimit);
461 * What forces this code is that in POP2 and
462 * IMAP2bis you can't fetch a message without
463 * having it marked `seen'. In POP3 and IMAP4, on the
464 * other hand, you can (peek_capable is set by
465 * each driver module to convey this; it's not a
466 * method constant because of the difference between
467 * IMAP2bis and IMAP4, and because POP3 doesn't peek
468 * if fetchall is on).
470 * The result of being unable to peek is that if there's
471 * any kind of transient error (DNS lookup failure, or
472 * sendmail refusing delivery due to process-table limits)
473 * the message will be marked "seen" on the server without
474 * having been delivered. This is not a big problem if
475 * fetchmail is running in foreground, because the user
476 * will see a "skipped" message when it next runs and get
479 * But in daemon mode this leads to the message
480 * being silently ignored forever. This is not
483 * We compensate for this by checking the error
484 * count from the previous pass and forcing all
485 * messages to be considered new if it's nonzero.
487 force_retrieval = !peek_capable && (ctl->errcount > 0);
489 for (num = 1; num <= count; num++)
491 flag suppress_delete = FALSE;
492 flag suppress_forward = FALSE;
493 flag suppress_readbody = FALSE;
494 flag retained = FALSE;
495 int msgcode = MSGLEN_UNKNOWN;
497 /* check if the message is old
498 * Note: the size of the message may not be known here */
499 if (ctl->fetchall || force_retrieval) {
502 if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num)) {
503 msgcode = MSGLEN_OLD;
506 if (msgcode == MSGLEN_OLD)
508 /* To avoid flooding the syslog when using --keep,
509 * report "Skipped message" only when:
510 * 1) --verbose is on, or
511 * 2) fetchmail does not use syslog
513 if ( (outlevel >= O_VERBOSE) ||
514 (outlevel > O_SILENT && !run.use_syslog)
518 GT_("skipping message %s@%s:%d"),
519 ctl->remotename, ctl->server.truename, num);
525 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit) &&
528 /* Instead of getting the sizes of all mails at the start, we get
529 * the sizes in blocks of fetchsizelimit. This leads to better
530 * performance when there are too many mails (say, 10000) in
531 * the mailbox and either we are not getting all the mails at
532 * one go (--fetchlimit 100) or there is a frequent socket
533 * error while just getting the sizes of all mails! */
536 int oldstage = stage;
538 lastnum = num + fetchsizelimit - 1;
541 for (i = 0; i < fetchsizelimit; i++)
544 stage = STAGE_GETSIZES;
545 err = (ctl->server.base_protocol->getpartialsizes)(mailserver_socket, num, lastnum, *msgsizes);
552 msgsize = *msgsizes ? (*msgsizes)[num-firstnum] : 0;
554 /* check if the message is oversized */
555 if (NUM_NONZERO(ctl->limit) && (msgsize > ctl->limit))
556 msgcode = MSGLEN_TOOLARGE;
557 /* else if (msgsize == 512)
558 msgcode = MSGLEN_OLD; (hmh) sample code to skip message */
562 if (msgcode == MSGLEN_TOOLARGE)
564 mark_oversized(ctl, msgsize);
565 if (!ctl->limitflush)
566 suppress_delete = TRUE;
568 if (outlevel > O_SILENT)
570 /* old messages are already handled above */
572 GT_("skipping message %s@%s:%d (%d octets)"),
573 ctl->remotename, ctl->server.truename, num,
579 * Invalid lengths are produced by Post Office/NT's
580 * annoying habit of randomly prepending bogus
581 * LIST items of length -1. Patrick Audley
582 * <paudley@pobox.com> tells us: LIST shows a
583 * size of -1, RETR and TOP return "-ERR
584 * System error - couldn't open message", and
585 * DELE succeeds but doesn't actually delete
588 report_build(stdout, GT_(" (length -1)"));
590 case MSGLEN_TOOLARGE:
591 report_build(stdout, GT_(" (oversized)"));
598 /* XXX FIXME: make this one variable, wholesize and
599 separatefetchbody query the same variable just with
601 flag wholesize = !ctl->server.base_protocol->fetch_body;
602 flag separatefetchbody = (ctl->server.base_protocol->fetch_body) ? TRUE : FALSE;
604 /* request a message */
605 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
606 if (err == PS_TRANSIENT) /* server is probably Exchange */
609 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)\n"),
610 ctl->remotename, ctl->server.truename, num,
617 /* -1 means we didn't see a size in the response */
624 if (outlevel > O_SILENT)
626 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
627 ctl->remotename, ctl->server.truename,
631 report_build(stdout, wholesize ? GT_(" (%d octets)")
632 : GT_(" (%d header octets)"), len);
633 if (want_progress()) {
634 /* flush and add a blank to append ticker dots */
635 report_flush(stdout);
641 * Read the message headers and ship them to the
644 err = readheaders(mailserver_socket, len, msgsize,
646 /* pass the suppress_readbody flag only if the underlying
647 * protocol does not fetch the body separately */
648 separatefetchbody ? 0 : &suppress_readbody);
650 if (err == PS_RETAINED)
651 suppress_forward = suppress_delete = retained = TRUE;
652 else if (err == PS_TRANSIENT)
653 suppress_delete = suppress_forward = TRUE;
654 else if (err == PS_REFUSED)
655 suppress_forward = TRUE;
659 /* tell server we got it OK and resynchronize */
660 if (separatefetchbody && ctl->server.base_protocol->trail)
662 err = eat_trailer(mailserver_socket, ctl);
663 if (err) return(err);
666 /* do not read the body which is not being forwarded only if
667 * the underlying protocol allows the body to be fetched
669 if (separatefetchbody && suppress_forward)
670 suppress_readbody = TRUE;
673 * If we're using IMAP4 or something else that
674 * can fetch headers separately from bodies,
675 * it's time to request the body now. This
676 * fetch may be skipped if we got an anti-spam
677 * or other PS_REFUSED error response during
680 if (!suppress_readbody)
682 if (separatefetchbody)
685 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len))) {
686 if (err == PS_ERROR && ctl->retrieveerror) {
688 * Mark a message with a protocol error as seen.
689 * This can be used to see which messages we've attempted
690 * to download, but failed.
692 if (ctl->retrieveerror == REMARKSEEN) {
693 if ((ctl->server.base_protocol->mark_seen)(mailserver_socket,ctl,num)) {
698 if (ctl->retrieveerror != REABORT) {
700 * Do not abort download session. Continue with the next message.
702 * Prevents a malformed message from blocking all other messages
703 * behind it in the mailbox from being downloaded.
705 * Reconnect to SMTP to force this incomplete message to be dropped.
706 * Required because we've already begun the DATA portion of the
707 * interaction with the SMTP server (commands are ignored/
708 * considered part of the message data).
710 abort_message_sink(ctl);
712 // Ensure we don't delete the failed message from the server.
713 suppress_delete = TRUE;
715 // Bookkeeping required before next message can be downloaded.
724 * Work around a bug in Novell's
725 * broken GroupWise IMAP server;
726 * its body FETCH response is missing
727 * the required length for the data
728 * string. This violates RFC2060.
731 len = msgsize - msgblk.msglen;
733 if (outlevel > O_SILENT)
735 GT_(" (%d body octets)"), len);
736 if (want_progress()) {
737 report_flush(stdout);
743 /* process the body now */
744 err = readbody(mailserver_socket,
749 if (err == PS_TRANSIENT)
750 suppress_delete = suppress_forward = TRUE;
754 /* tell server we got it OK and resynchronize */
755 if (ctl->server.base_protocol->trail) {
756 err = eat_trailer(mailserver_socket, ctl);
757 if (err) return(err);
761 /* count # messages forwarded on this pass */
762 if (!suppress_forward)
766 * Check to see if the numbers matched?
768 * Yes, some servers foo this up horribly.
769 * All IMAP servers seem to get it right, and
770 * so does Eudora QPOP at least in 2.xx
773 * Microsoft Exchange gets it completely
774 * wrong, reporting compressed rather than
775 * actual sizes (so the actual length of
776 * message is longer than the reported size).
777 * Another fine example of Microsoft brain death!
779 * Some older POP servers, like the old UCB
780 * POP server and the pre-QPOP QUALCOMM
781 * versions, report a longer size in the LIST
782 * response than actually gets shipped up.
783 * It's unclear what is going on here, as the
784 * QUALCOMM server (at least) seems to be
785 * reporting the on-disk size correctly.
787 * qmail-pop3d also goofs up message sizes and does not
788 * count the line end characters properly.
790 if (msgblk.msglen != msgsize)
792 if (outlevel >= O_DEBUG)
794 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
795 ctl->remotename, ctl->server.truename, num,
796 msgblk.msglen, msgsize);
799 /* end-of-message processing starts here */
800 if (!close_sink(ctl, &msgblk, !suppress_forward))
803 suppress_delete = TRUE;
811 * At this point in flow of control,
812 * either we've bombed on a protocol error
813 * or had delivery refused by the SMTP server
814 * or we've seen `accepted for delivery' and the message is shipped.
815 * It's safe to mark the message seen and delete it on the server now.
818 /* in softbounce mode, suppress deletion and marking as seen */
819 if (suppress_forward)
820 suppress_delete = suppress_delete || run.softbounce;
822 /* maybe we delete this message now? */
825 if (outlevel > O_SILENT)
826 report_complete(stdout, GT_(" retained\n"));
828 else if (ctl->server.base_protocol->delete_msg
830 && ((msgcode >= 0 && !ctl->keep)
831 || (msgcode == MSGLEN_OLD && ctl->flush)
832 || (msgcode == MSGLEN_TOOLARGE && ctl->limitflush)))
835 if (outlevel > O_SILENT)
836 report_complete(stdout, GT_(" flushed\n"));
837 err = (ctl->server.base_protocol->delete_msg)(mailserver_socket, ctl, num);
843 if ( (outlevel >= O_VERBOSE) ||
844 /* To avoid flooding the syslog when using --keep,
845 * report "Skipped message" only when:
846 * 1) --verbose is on, or
847 * 2) fetchmail does not use syslog, or
848 * 3) the message was skipped for some other
849 * reason than just being old.
851 (outlevel > O_SILENT && (!run.use_syslog || msgcode != MSGLEN_OLD))
853 report_complete(stdout, GT_(" not flushed\n"));
855 /* maybe we mark this message as seen now? */
856 if (ctl->server.base_protocol->mark_seen
858 && (msgcode >= 0 && ctl->keep))
860 err = (ctl->server.base_protocol->mark_seen)(mailserver_socket, ctl, num);
866 /* perhaps this as many as we're ready to handle */
867 if (maxfetch && maxfetch <= *fetches && num < count)
869 int remcount = count - (*fetches - initialfetches);
871 ngettext("fetchlimit %d reached; %d message left on server %s account %s\n",
872 "fetchlimit %d reached; %d messages left on server %s account %s\n", remcount),
873 maxfetch, remcount, ctl->server.truename, ctl->remotename);
876 } /* for (num = 1; num <= count; num++) */
881 /* retrieve messages from server using given protocol method table */
882 static int do_session(
883 /* parsed options with merged-in defaults */
885 /* protocol method table */
886 const struct method *proto,
887 /* maximum number of messages to fetch */
890 static int *msgsizes;
891 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
893 int deletions = 0, js;
895 SIGHANDLERTYPE alrmsave;
897 ctl->server.base_protocol = proto;
902 init_transact(proto);
904 /* set up the server-nonresponse timeout */
905 alrmsave = set_signal_handler(SIGALRM, timeout_handler);
906 mytimeout = ctl->server.timeout;
908 if ((js = sigsetjmp(restart,1)))
910 /* exception caught */
913 sigfillset(&allsigs);
914 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
917 fm_freeaddrinfo(ai0); ai0 = NULL;
921 fm_freeaddrinfo(ai1); ai1 = NULL;
924 if (js == THROW_TIMEOUT)
926 if (phase == OPEN_WAIT)
928 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
929 ctl->server.timeout, ctl->server.pollname);
930 else if (phase == SERVER_WAIT)
932 GT_("timeout after %d seconds waiting for server %s.\n"),
933 ctl->server.timeout, ctl->server.pollname);
934 else if (phase == FORWARDING_WAIT)
936 GT_("timeout after %d seconds waiting for %s.\n"),
938 ctl->mda ? "MDA" : "SMTP");
939 else if (phase == LISTENER_WAIT)
941 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
944 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
947 * If we've exceeded our threshold for consecutive timeouts,
948 * try to notify the user, then mark the connection wedged.
949 * Don't do this if the connection can idle, though; idle
950 * timeouts just mean the frequency of mail is low.
952 if (timeoutcount > MAX_TIMEOUTS
953 && !open_warning_by_mail(ctl))
955 stuff_warning(iana_charset, ctl,
956 GT_("Subject: fetchmail sees repeated timeouts"));
957 stuff_warning(NULL, ctl, "%s", "");
958 stuff_warning(NULL, ctl,
959 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\n"),
961 ctl->remotename, ctl->server.truename);
962 stuff_warning(NULL, ctl,
963 GT_("This could mean that your mailserver is stuck, or that your SMTP\n" \
964 "server is wedged, or that your mailbox file on the server has been\n" \
965 "corrupted by a server error. You can run `fetchmail -v -v' to\n" \
966 "diagnose the problem.\n\n" \
967 "Fetchmail won't poll this mailbox again until you restart it.\n"));
968 close_warning_by_mail(ctl, (struct msgblk *)NULL);
978 /* sigsetjmp returned zero -> normal operation */
979 char buf[MSGBUFSIZE+1], *realhost;
980 int count, newm, bytes;
981 int fetches, dispatches, oldphase;
984 /* execute pre-initialization command, if any */
985 if (ctl->preconnect && (err = system(ctl->preconnect)))
987 if (WIFSIGNALED(err))
989 GT_("pre-connection command terminated with signal %d\n"), WTERMSIG(err));
992 GT_("pre-connection command failed with status %d\n"), WEXITSTATUS(err));
997 /* open a socket to the mail server */
1000 set_timeout(mytimeout);
1002 #ifdef HAVE_PKG_hesiod
1003 /* If either the pollname or vianame are "hesiod" we want to
1004 lookup the user's hesiod pobox host */
1005 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
1006 struct hes_postoffice *hes_p;
1007 hes_p = hes_getmailhost(ctl->remotename);
1008 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
1009 free(ctl->server.queryname);
1010 ctl->server.queryname = xstrdup(hes_p->po_host);
1011 if (ctl->server.via)
1012 free(ctl->server.via);
1013 ctl->server.via = xstrdup(hes_p->po_host);
1016 GT_("couldn't find HESIOD pobox for %s\n"),
1023 * Canonicalize the server truename for later use. This also
1024 * functions as a probe for whether the mailserver is accessible.
1025 * We try it on each poll cycle until we get a result. This way,
1026 * fetchmail won't fail if started up when the network is inaccessible.
1028 if (ctl->server.dns && !ctl->server.trueaddr)
1030 if (ctl->server.lead_server)
1032 char *leadname = ctl->server.lead_server->truename;
1034 /* prevent core dump from ill-formed or duplicate entry */
1037 report(stderr, GT_("Lead server has no name.\n"));
1044 xfree(ctl->server.truename);
1045 ctl->server.truename = xstrdup(leadname);
1049 struct addrinfo hints, *res;
1052 memset(&hints, 0, sizeof(hints));
1053 hints.ai_socktype = SOCK_STREAM;
1054 hints.ai_family = AF_UNSPEC;
1055 hints.ai_flags = AI_CANONNAME;
1056 #ifdef AI_ADDRCONFIG
1057 hints.ai_flags |= AI_ADDRCONFIG;
1060 error = fm_getaddrinfo(ctl->server.queryname, NULL, &hints, &res);
1064 GT_("couldn't find canonical DNS name of %s (%s): %s\n"),
1065 ctl->server.pollname, ctl->server.queryname,
1066 gai_strerror(error));
1074 xfree(ctl->server.truename);
1075 /* Older FreeBSD versions return NULL in ai_canonname
1076 * if they cannot canonicalize, rather than copying
1077 * the queryname here, as IEEE Std 1003.1-2001
1078 * requires. Work around NULL. */
1079 if (res->ai_canonname != NULL) {
1080 ctl->server.truename = xstrdup(res->ai_canonname);
1082 ctl->server.truename = xstrdup(ctl->server.queryname);
1084 ctl->server.trueaddr = (struct sockaddr *)xmalloc(res->ai_addrlen);
1085 ctl->server.trueaddr_len = res->ai_addrlen;
1086 memcpy(ctl->server.trueaddr, res->ai_addr, res->ai_addrlen);
1087 fm_freeaddrinfo(res);
1092 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
1094 /* allow time for the port to be set up if we have a plugin */
1095 if (ctl->server.plugin)
1097 if ((mailserver_socket = SockOpen(realhost,
1098 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
1099 ctl->server.plugin, &ai0)) == -1)
1101 char errbuf[BUFSIZ];
1104 * Avoid generating a bogus error every poll cycle when we're
1105 * in daemon mode but the connection to the outside world
1108 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
1109 && run.poll_interval))
1111 report_build(stderr, GT_("%s connection to %s failed"),
1112 ctl->server.base_protocol->name, ctl->server.pollname);
1113 strlcpy(errbuf, strerror(err_no), sizeof(errbuf));
1114 report_complete(stderr, ": %s\n", errbuf);
1118 * Don't use this. It was an attempt to address Debian bug
1119 * #47143 (Notify user by mail when pop server nonexistent).
1120 * Trouble is, that doesn't work; you trip over the case
1121 * where your SLIP or PPP link is down...
1123 /* warn the system administrator */
1124 if (open_warning_by_mail(ctl) == 0)
1126 stuff_warning(iana_charset, ctl,
1127 GT_("Subject: Fetchmail unreachable-server warning."));
1128 stuff_warning(NULL, ctl, "");
1129 stuff_warning(NULL, ctl, GT_("Fetchmail could not reach the mail server %s:"),
1130 ctl->server.pollname);
1131 stuff_warning(NULL, ctl, errbuf, ctl->server.pollname);
1132 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1143 /* Save the socket opened. Useful if Fetchmail hangs on SSLOpen
1144 * because the socket can be closed.
1146 mailserver_socket_temp = mailserver_socket;
1147 set_timeout(mytimeout);
1149 /* perform initial SSL handshake on open connection */
1151 SSLOpen(mailserver_socket, ctl->sslcert, ctl->sslkey,
1152 ctl->sslproto, ctl->sslcertck,
1153 ctl->sslcertfile, ctl->sslcertpath,
1154 ctl->sslfingerprint, ctl->sslcommonname ?
1155 ctl->sslcommonname : realhost, ctl->server.pollname,
1156 &ctl->remotename) == -1)
1158 report(stderr, GT_("SSL connection failed.\n"));
1163 /* Fetchmail didn't hang on SSLOpen,
1164 * then no need to set mailserver_socket_temp
1166 mailserver_socket_temp = -1;
1169 /* A timeout is still defined before SSLOpen,
1170 * then Fetchmail hanging on SSLOpen is handled.
1175 if (ctl->server.authenticate == A_KERBEROS_V4 && (strcasecmp(proto->name,"IMAP") != 0))
1177 set_timeout(mytimeout);
1178 err = kerberos_auth(mailserver_socket, ctl->server.truename,
1179 ctl->server.principal);
1184 #endif /* KERBEROS_V4 */
1187 if (ctl->server.authenticate == A_KERBEROS_V5)
1189 set_timeout(mytimeout);
1190 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1195 #endif /* KERBEROS_V5 */
1197 /* accept greeting message from mail server */
1198 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1202 /* try to get authorized to fetch mail */
1203 stage = STAGE_GETAUTH;
1204 if (ctl->server.base_protocol->getauth)
1206 set_timeout(mytimeout);
1207 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1212 if (err == PS_LOCKBUSY)
1213 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1215 ctl->server.truename);
1216 else if (err == PS_SERVBUSY)
1217 report(stderr, GT_("Server busy error on %s@%s\n"),
1219 ctl->server.truename);
1220 else if (err == PS_AUTHFAIL)
1222 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1224 ctl->server.truename,
1225 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1229 * If we're running in background, try to mail the
1230 * calling user a heads-up about the authentication
1231 * failure once it looks like this isn't a fluke
1232 * due to the server being temporarily inaccessible.
1233 * When we get third succesive failure, we notify the user
1234 * but only if we haven't already managed to get
1235 * authorization. After that, once we get authorization
1236 * we let the user know service is restored.
1238 if (run.poll_interval
1239 && !ctl->wehavesentauthnote
1240 && ((ctl->wehaveauthed && ++ctl->authfailcount >= 10)
1241 || (!ctl->wehaveauthed && ++ctl->authfailcount >= 3))
1242 && !open_warning_by_mail(ctl))
1244 ctl->wehavesentauthnote = 1;
1245 stuff_warning(iana_charset, ctl,
1246 GT_("Subject: fetchmail authentication failed on %s@%s"),
1247 ctl->remotename, ctl->server.truename);
1248 stuff_warning(NULL, ctl, "%s", "");
1249 stuff_warning(NULL, ctl,
1250 GT_("Fetchmail could not get mail from %s@%s.\n"),
1252 ctl->server.truename);
1253 if (ctl->wehaveauthed) {
1254 stuff_warning(NULL, ctl, GT_("\
1255 The attempt to get authorization failed.\n\
1256 Since we have already succeeded in getting authorization for this\n\
1257 connection, this is probably another failure mode (such as busy server)\n\
1258 that fetchmail cannot distinguish because the server didn't send a useful\n\
1260 stuff_warning(NULL, ctl, GT_("\
1262 However, if you HAVE changed your account details since starting the\n\
1263 fetchmail daemon, you need to stop the daemon, change your configuration\n\
1264 of fetchmail, and then restart the daemon.\n\
1266 The fetchmail daemon will continue running and attempt to connect\n\
1267 at each cycle. No future notifications will be sent until service\n\
1270 stuff_warning(NULL, ctl, GT_("\
1271 The attempt to get authorization failed.\n\
1272 This probably means your password is invalid, but some servers have\n\
1273 other failure modes that fetchmail cannot distinguish from this\n\
1274 because they don't send useful error messages on login failure.\n\
1276 The fetchmail daemon will continue running and attempt to connect\n\
1277 at each cycle. No future notifications will be sent until service\n\
1280 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1283 else if (err == PS_REPOLL)
1285 if (outlevel >= O_VERBOSE)
1286 report(stderr, GT_("Repoll immediately on %s@%s\n"),
1288 ctl->server.truename);
1291 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1293 ctl->server.truename);
1300 * This connection has given us authorization at least once.
1302 * There are dodgy server (clubinternet.fr for example) that
1303 * give spurious authorization failures on patently good
1304 * account/password details, then 5 minutes later let you in!
1306 * This is meant to build in some tolerance of such nasty bits
1309 ctl->wehaveauthed = 1;
1310 /*if (ctl->authfailcount >= 3)*/
1311 if (ctl->wehavesentauthnote)
1313 ctl->wehavesentauthnote = 0;
1315 GT_("Authorization OK on %s@%s\n"),
1317 ctl->server.truename);
1318 if (!open_warning_by_mail(ctl))
1320 stuff_warning(iana_charset, ctl,
1321 GT_("Subject: fetchmail authentication OK on %s@%s"),
1322 ctl->remotename, ctl->server.truename);
1323 stuff_warning(NULL, ctl, "%s", "");
1324 stuff_warning(NULL, ctl,
1325 GT_("Fetchmail was able to log into %s@%s.\n"),
1327 ctl->server.truename);
1328 stuff_warning(NULL, ctl,
1329 GT_("Service has been restored.\n"));
1330 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1335 * Reporting only after the first three
1336 * consecutive failures, or ten consecutive
1337 * failures after we have managed to get
1340 ctl->authfailcount = 0;
1344 ctl->errcount = fetches = 0;
1346 /* now iterate over each folder selected */
1347 for (idp = ctl->mailboxes; idp; idp = idp->next)
1349 ctl->folder = idp->id;
1355 /* reset timeout, in case we did an IDLE */
1356 mytimeout = ctl->server.timeout;
1358 if (outlevel >= O_DEBUG)
1361 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1363 report(stdout, GT_("selecting or re-polling default folder\n"));
1366 /* compute # of messages and number of new messages waiting */
1367 stage = STAGE_GETRANGE;
1368 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &newm, &bytes);
1372 /* show user how many messages we downloaded */
1374 (void) snprintf(buf, sizeof(buf),
1375 GT_("%s at %s (folder %s)"),
1376 ctl->remotename, ctl->server.pollname, idp->id);
1378 (void) snprintf(buf, sizeof(buf), GT_("%s at %s"),
1379 ctl->remotename, ctl->server.pollname);
1380 if (outlevel > O_SILENT)
1382 if (count == -1) /* only used for ETRN */
1383 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1384 else if (count != 0)
1386 if (newm != -1 && (count - newm) > 0)
1387 report_build(stdout, ngettext("%d message (%d %s) for %s", "%d messages (%d %s) for %s", (unsigned long)count),
1390 ngettext("seen", "seen", (unsigned long)count-newm),
1393 report_build(stdout, ngettext("%d message for %s",
1394 "%d messages for %s",
1398 report_complete(stdout, ".\n");
1400 report_complete(stdout, GT_(" (%d octets).\n"), bytes);
1404 /* these are pointless in normal daemon mode */
1405 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1406 report(stdout, GT_("No mail for %s\n"), buf);
1410 /* very important, this is where we leave the do loop */
1416 if (newm == -1 || ctl->fetchall)
1418 fetches = newm; /* set error status correctly */
1420 * There used to be a `goto noerror' here, but this
1421 * prevented checking of multiple folders. This
1422 * comment is a reminder in case I introduced some
1423 * subtle bug by removing it...
1431 * Don't trust the message count passed by the server.
1432 * Without this check, it might be possible to do a
1433 * DNS-spoofing attack that would pass back a ridiculous
1434 * count, and allocate a malloc area that would overlap
1435 * a portion of the stack.
1437 if ((unsigned)count > INT_MAX/sizeof(int))
1439 report(stderr, GT_("bogus message count!"));
1445 * We need the size of each message before it's
1446 * loaded in order to pass it to the ESMTP SIZE
1447 * option. If the protocol has a getsizes method,
1448 * we presume this means it doesn't get reliable
1449 * sizes from message fetch responses.
1451 * If the protocol supports getting sizes of subset of
1452 * messages, we skip this step now.
1454 if (proto->getsizes &&
1455 !(proto->getpartialsizes && NUM_NONZERO(ctl->fetchsizelimit)))
1458 msgsizes = (int *)xmalloc(sizeof(int) * count);
1459 for (i = 0; i < count; i++)
1462 stage = STAGE_GETSIZES;
1463 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1470 for (i = 0; i < count; i++)
1471 bytes += msgsizes[i];
1475 /* read, forward, and delete messages */
1476 stage = STAGE_FETCH;
1478 /* fetch in lockstep mode */
1479 err = fetch_messages(mailserver_socket, ctl,
1482 &fetches, &dispatches, &deletions);
1483 if (err != PS_SUCCESS && err != PS_MAXFETCH)
1486 if (!check_only && ctl->skipped
1487 && run.poll_interval > 0 && !nodetach)
1489 clean_skipped_list(&ctl->skipped);
1490 send_size_warnings(ctl);
1494 /* end-of-mailbox processing before we repoll or switch to another one */
1495 if (ctl->server.base_protocol->end_mailbox_poll)
1497 tmperr = (ctl->server.base_protocol->end_mailbox_poll)(mailserver_socket, ctl);
1503 /* Return now if we have reached the fetchlimit */
1504 if (maxfetch && maxfetch <= fetches)
1508 * Only repoll if we either had some actual forwards
1509 * or are idling for new mails and had no errors.
1510 * Otherwise it is far too easy to get into infinite loops.
1512 (ctl->server.base_protocol->retry && (dispatches || ctl->idle) && !ctl->errcount);
1515 /* XXX: From this point onwards, preserve err unless a new error has occurred */
1518 /* PS_SUCCESS, PS_MAXFETCH: ordinary termination with no errors -- officially log out */
1519 stage = STAGE_LOGOUT;
1520 tmperr = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1521 if (tmperr != PS_SUCCESS)
1524 * Hmmmm...arguably this would be incorrect if we had fetches but
1525 * no dispatches (due to oversized messages, etc.)
1527 else if (err == PS_SUCCESS && fetches == 0)
1530 * Close all SMTP delivery sockets. For optimum performance
1531 * we'd like to hold them open til end of run, but (1) this
1532 * loses if our poll interval is longer than the MTA's
1533 * inactivity timeout, and (2) some MTAs (like smail) don't
1534 * deliver after each message, but rather queue up mail and
1535 * wait to actually deliver it until the input socket is
1538 * don't send QUIT for ODMR case because we're acting as a
1539 * proxy between the SMTP server and client.
1541 smtp_close(ctl, ctl->server.protocol != P_ODMR);
1542 cleanupSockClose(mailserver_socket);
1546 /* we only get here on error */
1547 if (err != 0 && err != PS_SOCKET && err != PS_REPOLL)
1549 stage = STAGE_LOGOUT;
1550 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1553 /* try to clean up all streams */
1556 * Sending SMTP QUIT on signal is theoretically nice, but led
1557 * to a subtle bug. If fetchmail was terminated by signal
1558 * while it was shipping message text, it would hang forever
1559 * waiting for a command acknowledge. In theory we could
1560 * enable the QUIT only outside of the message send. In
1561 * practice, we don't care. All mailservers hang up on a
1562 * dropped TCP/IP connection anyway.
1565 if (mailserver_socket != -1) {
1566 cleanupSockClose(mailserver_socket);
1568 /* If there was a connect timeout, the socket should be closed.
1569 * mailserver_socket_temp contains the socket to close.
1571 if (mailserver_socket_temp != -1) {
1572 cleanupSockClose(mailserver_socket_temp);
1573 mailserver_socket_temp = -1;
1577 /* no report on PS_AUTHFAIL */
1582 msg = GT_("socket");
1585 msg = GT_("missing or bad RFC822 header");
1591 msg = GT_("client/server synchronization");
1594 msg = GT_("client/server protocol");
1597 msg = GT_("lock busy on server");
1600 msg = GT_("SMTP transaction");
1603 msg = GT_("DNS lookup");
1606 msg = GT_("undefined");
1610 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT
1612 report(stderr, GT_("%s error while fetching from %s@%s and delivering to SMTP host %s\n"),
1613 msg, ctl->remotename, ctl->server.pollname,
1614 ctl->smtphost ? ctl->smtphost : GT_("unknown"));
1616 report(stderr, GT_("%s error while fetching from %s@%s\n"),
1617 msg, ctl->remotename, ctl->server.pollname);
1624 /* execute wrapup command, if any */
1625 if (ctl->postconnect && (tmperr = system(ctl->postconnect)))
1627 if (WIFSIGNALED(tmperr))
1628 report(stderr, GT_("post-connection command terminated with signal %d\n"), WTERMSIG(tmperr));
1630 report(stderr, GT_("post-connection command failed with status %d\n"), WEXITSTATUS(tmperr));
1631 if (err == PS_SUCCESS)
1635 set_timeout(0); /* cancel any pending alarm */
1636 set_signal_handler(SIGALRM, alrmsave);
1640 /** retrieve messages from server using given protocol method table */
1641 int do_protocol(struct query *ctl /** parsed options with merged-in defaults */,
1642 const struct method *proto /** protocol method table */)
1647 if (ctl->server.authenticate == A_KERBEROS_V4)
1649 report(stderr, GT_("Kerberos V4 support not linked.\n"));
1652 #endif /* KERBEROS_V4 */
1655 if (ctl->server.authenticate == A_KERBEROS_V5)
1657 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1660 #endif /* KERBEROS_V5 */
1662 /* lacking methods, there are some options that may fail */
1665 /* check for unsupported options */
1668 GT_("Option --flush is not supported with %s\n"),
1672 else if (ctl->fetchall) {
1674 GT_("Option --all is not supported with %s\n"),
1679 if (!(proto->getsizes || proto->getpartialsizes)
1680 && NUM_SPECIFIED(ctl->limit))
1683 GT_("Option --limit is not supported with %s\n"),
1689 * If no expunge limit or we do expunges within the driver,
1690 * then just do one session, passing in any fetchlimit.
1692 if ((ctl->keep && !ctl->flush) ||
1693 proto->retry || !NUM_SPECIFIED(ctl->expunge))
1694 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1696 * There's an expunge limit, and it isn't handled in the driver itself.
1697 * OK; do multiple sessions, each fetching a limited # of messages.
1698 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1699 * (if it was nonzero).
1705 int expunge = NUM_VALUE_OUT(ctl->expunge);
1706 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1709 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1710 expunge = fetchlimit - totalcount;
1711 err = do_session(ctl, proto, expunge);
1712 totalcount += expunge;
1713 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1715 if (err != PS_LOCKBUSY)
1717 else if (lockouts >= MAX_LOCKOUTS)
1719 else /* err == PS_LOCKBUSY */
1722 * Allow time for the server lock to release. if we
1723 * don't do this, we'll often hit a locked-mailbox
1724 * condition and fail.
1730 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1737 /* driver.c ends here */