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"
56 /* throw types for runtime errors */
57 #define THROW_TIMEOUT 1 /* server timed out */
59 /* magic values for the message length array */
60 #define MSGLEN_UNKNOWN 0 /* length unknown (0 is impossible) */
61 #define MSGLEN_INVALID -1 /* length passed back is invalid */
62 #define MSGLEN_TOOLARGE -2 /* message is too large */
63 #define MSGLEN_OLD -3 /* message is old */
65 int pass; /* how many times have we re-polled? */
66 int stage; /* where are we? */
67 int phase; /* where are we, for error-logging purposes? */
68 int batchcount; /* count of messages sent in current batch */
69 flag peek_capable; /* can we peek for better error recovery? */
70 int mailserver_socket_temp = -1; /* socket to free if connect timeout */
72 struct addrinfo *ai0, *ai1; /* clean these up after signal */
74 static volatile int timeoutcount = 0; /* count consecutive timeouts */
75 static volatile int idletimeout = 0; /* timeout occured in idle stage? */
77 static sigjmp_buf restart;
79 int is_idletimeout(void)
80 /* last timeout occured in idle stage? */
85 void resetidletimeout(void)
90 void set_timeout(int timeleft)
91 /* reset the nonresponse-timeout */
93 #if !defined(__EMX__) && !defined(__BEOS__)
94 struct itimerval ntimeout;
99 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
100 ntimeout.it_value.tv_sec = timeleft;
101 ntimeout.it_value.tv_usec = 0;
102 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
106 static RETSIGTYPE timeout_handler (int signal)
107 /* handle SIGALRM signal indicating a server timeout */
110 if(stage != STAGE_IDLE) {
112 /* XXX FIXME: this siglongjmp must die - it's not safe to be
113 * called from a function handler and breaks, for instance,
115 siglongjmp(restart, THROW_TIMEOUT);
120 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
122 static int cleanupSockClose (int fd)
123 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
126 SIGHANDLERTYPE alrmsave;
127 alrmsave = set_signal_handler(SIGALRM, null_signal_handler);
128 set_timeout(CLEANUP_TIMEOUT);
129 scerror = SockClose(fd);
131 set_signal_handler(SIGALRM, alrmsave);
136 static int kerberos_auth(socket, canonical, principal)
137 /* authenticate to the server host using Kerberos V4 */
138 int socket; /* socket to server host */
139 char *canonical; /* server name */
145 Key_schedule schedule;
147 char * prin_copy = (char *) NULL;
148 char * prin = (char *) NULL;
149 char * inst = (char *) NULL;
150 char * realm = (char *) NULL;
152 if (principal != (char *)NULL && *principal)
155 prin = prin_copy = xstrdup(principal);
156 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
162 while (*cp && *cp != '@')
172 ticket = xmalloc(sizeof (KTEXT_ST));
173 rem = (krb_sendauth (0L, socket, ticket,
175 inst ? inst : canonical,
176 realm ? realm : ((char *) (krb_realmofhost (canonical))),
181 ((struct sockaddr_in *) 0),
182 ((struct sockaddr_in *) 0),
191 report(stderr, GT_("kerberos error %s\n"), (krb_get_err_text (rem)));
192 return (PS_AUTHFAIL);
196 #endif /* KERBEROS_V4 */
199 static int kerberos5_auth(socket, canonical)
200 /* authenticate to the server host using Kerberos V5 */
201 int socket; /* socket to server host */
202 const char *canonical; /* server name */
204 krb5_error_code retval;
205 krb5_context context;
207 krb5_principal client = NULL, server = NULL;
208 krb5_error *err_ret = NULL;
210 krb5_auth_context auth_context = NULL;
212 krb5_init_context(&context);
213 krb5_auth_con_init(context, &auth_context);
215 if ((retval = krb5_cc_default(context, &ccdef))) {
216 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
220 if ((retval = krb5_cc_get_principal(context, ccdef, &client))) {
221 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
225 if ((retval = krb5_sname_to_principal(context, canonical, "pop",
228 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
232 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
233 "KPOPV1.0", client, server,
234 AP_OPTS_MUTUAL_REQUIRED,
235 NULL, /* no data to checksum */
236 0, /* no creds, use ccache instead */
240 NULL); /* don't need reply */
242 krb5_free_principal(context, server);
243 krb5_free_principal(context, client);
244 krb5_auth_con_free(context, auth_context);
248 if (err_ret && err_ret->e_text) {
249 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
250 error_message(retval),
253 if (err_ret && err_ret->text.length) {
254 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
255 error_message(retval),
256 err_ret->text.length,
259 krb5_free_error(context, err_ret);
261 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
267 #endif /* KERBEROS_V5 */
269 static void clean_skipped_list(struct idlist **skipped_list)
270 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
272 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
273 prev = current = head = *skipped_list;
279 /* if item has no reference, remove it */
280 if (current && current->val.status.mark == 0)
282 if (current == head) /* remove first item (head) */
284 head = current->next;
285 if (current->id) free(current->id);
287 prev = current = head;
289 else /* remove middle/last item */
293 if (current->id) free(current->id);
298 else /* skip this item */
301 current = current->next;
305 *skipped_list = head;
308 static void send_size_warnings(struct query *ctl)
309 /* send warning mail with skipped msg; reset msg count when user notified */
312 int msg_to_send = FALSE;
313 struct idlist *head=NULL, *current=NULL;
314 int max_warning_poll_count;
320 /* don't start a notification message unless we need to */
321 for (current = head; current; current = current->next)
322 if (current->val.status.num == 0 && current->val.status.mark)
328 * There's no good way to recover if we can't send notification mail,
329 * but it's not a disaster, either, since the skipped mail will not
332 if (open_warning_by_mail(ctl))
334 stuff_warning(iana_charset, ctl,
335 GT_("Subject: Fetchmail oversized-messages warning"));
336 stuff_warning(NULL, ctl, "%s", "");
338 stuff_warning(NULL, ctl,
339 GT_("The following oversized messages were deleted on server %s account %s:"),
340 ctl->server.pollname, ctl->remotename);
342 stuff_warning(NULL, ctl,
343 GT_("The following oversized messages remain on server %s account %s:"),
344 ctl->server.pollname, ctl->remotename);
346 stuff_warning(NULL, ctl, "%s", "");
348 if (run.poll_interval == 0)
349 max_warning_poll_count = 0;
351 max_warning_poll_count = ctl->warnings/run.poll_interval;
353 /* parse list of skipped msg, adding items to the mail */
354 for (current = head; current; current = current->next)
356 if (current->val.status.num == 0 && current->val.status.mark)
358 nbr = current->val.status.mark;
359 size = atoi(current->id);
361 stuff_warning(NULL, ctl,
362 ngettext(" %d message %d octets long deleted by fetchmail.",
363 " %d messages %d octets long deleted by fetchmail.", nbr),
366 stuff_warning(NULL, ctl,
367 ngettext(" %d message %d octets long skipped by fetchmail.",
368 " %d messages %d octets long skipped by fetchmail.", nbr),
371 current->val.status.num++;
372 current->val.status.mark = 0;
374 if (current->val.status.num >= max_warning_poll_count)
375 current->val.status.num = 0;
378 stuff_warning(NULL, ctl, "%s", "");
380 close_warning_by_mail(ctl, (struct msgblk *)NULL);
383 static void mark_oversized(struct query *ctl, int size)
384 /* mark a message oversized */
386 struct idlist *current=NULL, *tmp=NULL;
390 /* convert size to string */
391 snprintf(sizestr, sizeof(sizestr), "%d", size);
393 /* build a list of skipped messages
394 * val.id = size of msg (string cnvt)
395 * val.status.num = warning_poll_count
396 * val.status.mask = nbr of msg this size
399 current = ctl->skipped;
401 /* initialise warning_poll_count to the
402 * current value so that all new msg will
403 * be included in the next mail
405 cnt = current ? current->val.status.num : 0;
407 /* if entry exists, increment the count */
408 if (current && (tmp = str_in_list(¤t, sizestr, FALSE)))
410 tmp->val.status.mark++;
412 /* otherwise, create a new entry */
413 /* initialise with current poll count */
416 tmp = save_str(&ctl->skipped, sizestr, 1);
417 tmp->val.status.num = cnt;
421 static int fetch_messages(int mailserver_socket, struct query *ctl,
422 int count, int **msgsizes, int maxfetch,
423 int *fetches, int *dispatches, int *deletions)
424 /* fetch messages in lockstep mode */
426 flag force_retrieval;
427 int num, firstnum = 1, lastnum = 0, err, len;
428 int fetchsizelimit = ctl->fetchsizelimit;
430 int initialfetches = *fetches;
432 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit))
434 /* for POP3, we can get the size of one mail only! Unfortunately, this
435 * protocol specific test cannot be done elsewhere as the protocol
436 * could be "auto". */
437 switch (ctl->server.protocol)
439 case P_POP3: case P_APOP: case P_RPOP:
443 /* Time to allocate memory to store the sizes */
445 *msgsizes = (int *)xmalloc(sizeof(int) * fetchsizelimit);
449 * What forces this code is that in POP2 and
450 * IMAP2bis you can't fetch a message without
451 * having it marked `seen'. In POP3 and IMAP4, on the
452 * other hand, you can (peek_capable is set by
453 * each driver module to convey this; it's not a
454 * method constant because of the difference between
455 * IMAP2bis and IMAP4, and because POP3 doesn't peek
456 * if fetchall is on).
458 * The result of being unable to peek is that if there's
459 * any kind of transient error (DNS lookup failure, or
460 * sendmail refusing delivery due to process-table limits)
461 * the message will be marked "seen" on the server without
462 * having been delivered. This is not a big problem if
463 * fetchmail is running in foreground, because the user
464 * will see a "skipped" message when it next runs and get
467 * But in daemon mode this leads to the message
468 * being silently ignored forever. This is not
471 * We compensate for this by checking the error
472 * count from the previous pass and forcing all
473 * messages to be considered new if it's nonzero.
475 force_retrieval = !peek_capable && (ctl->errcount > 0);
477 for (num = 1; num <= count; num++)
479 flag suppress_delete = FALSE;
480 flag suppress_forward = FALSE;
481 flag suppress_readbody = FALSE;
482 flag retained = FALSE;
483 int msgcode = MSGLEN_UNKNOWN;
485 /* check if the message is old
486 * Note: the size of the message may not be known here */
487 if (ctl->fetchall || force_retrieval)
489 else if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num))
490 msgcode = MSGLEN_OLD;
491 if (msgcode == MSGLEN_OLD)
493 /* To avoid flooding the syslog when using --keep,
494 * report "Skipped message" only when:
495 * 1) --verbose is on, or
496 * 2) fetchmail does not use syslog
498 if ( (outlevel >= O_VERBOSE) ||
499 (outlevel > O_SILENT && !run.use_syslog)
503 GT_("skipping message %s@%s:%d"),
504 ctl->remotename, ctl->server.truename, num);
510 if (ctl->server.base_protocol->getpartialsizes && NUM_NONZERO(fetchsizelimit) &&
513 /* Instead of getting the sizes of all mails at the start, we get
514 * the sizes in blocks of fetchsizelimit. This leads to better
515 * performance when there are too many mails (say, 10000) in
516 * the mailbox and either we are not getting all the mails at
517 * one go (--fetchlimit 100) or there is a frequent socket
518 * error while just getting the sizes of all mails! */
521 int oldstage = stage;
523 lastnum = num + fetchsizelimit - 1;
526 for (i = 0; i < fetchsizelimit; i++)
529 stage = STAGE_GETSIZES;
530 err = (ctl->server.base_protocol->getpartialsizes)(mailserver_socket, num, lastnum, *msgsizes);
537 msgsize = *msgsizes ? (*msgsizes)[num-firstnum] : 0;
539 /* check if the message is oversized */
540 if (NUM_NONZERO(ctl->limit) && (msgsize > ctl->limit))
541 msgcode = MSGLEN_TOOLARGE;
542 /* else if (msgsize == 512)
543 msgcode = MSGLEN_OLD; (hmh) sample code to skip message */
547 if (msgcode == MSGLEN_TOOLARGE)
549 mark_oversized(ctl, msgsize);
550 if (!ctl->limitflush)
551 suppress_delete = TRUE;
553 if (outlevel > O_SILENT)
555 /* old messages are already handled above */
557 GT_("skipping message %s@%s:%d (%d octets)"),
558 ctl->remotename, ctl->server.truename, num,
564 * Invalid lengths are produced by Post Office/NT's
565 * annoying habit of randomly prepending bogus
566 * LIST items of length -1. Patrick Audley
567 * <paudley@pobox.com> tells us: LIST shows a
568 * size of -1, RETR and TOP return "-ERR
569 * System error - couldn't open message", and
570 * DELE succeeds but doesn't actually delete
573 report_build(stdout, GT_(" (length -1)"));
575 case MSGLEN_TOOLARGE:
576 report_build(stdout, GT_(" (oversized)"));
583 /* XXX FIXME: make this one variable, wholesize and
584 separatefetchbody query the same variable just with
586 flag wholesize = !ctl->server.base_protocol->fetch_body;
587 flag separatefetchbody = (ctl->server.base_protocol->fetch_body) ? TRUE : FALSE;
589 /* request a message */
590 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
591 if (err == PS_TRANSIENT) /* server is probably Exchange */
594 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)\n"),
595 ctl->remotename, ctl->server.truename, num,
602 /* -1 means we didn't see a size in the response */
609 if (outlevel > O_SILENT)
611 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
612 ctl->remotename, ctl->server.truename,
616 report_build(stdout, wholesize ? GT_(" (%d octets)")
617 : GT_(" (%d header octets)"), len);
618 if (outlevel >= O_VERBOSE)
619 report_complete(stdout, "\n");
623 * Read the message headers and ship them to the
626 err = readheaders(mailserver_socket, len, msgsize,
628 /* pass the suppress_readbody flag only if the underlying
629 * protocol does not fetch the body separately */
630 separatefetchbody ? 0 : &suppress_readbody);
631 if (err == PS_RETAINED)
632 suppress_forward = suppress_delete = retained = TRUE;
633 else if (err == PS_TRANSIENT)
634 suppress_delete = suppress_forward = TRUE;
635 else if (err == PS_REFUSED)
636 suppress_forward = TRUE;
640 /* tell server we got it OK and resynchronize */
641 if (separatefetchbody && ctl->server.base_protocol->trail)
643 if (outlevel >= O_VERBOSE && !is_a_file(1) && !run.use_syslog)
649 if ((err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, tag)))
653 /* do not read the body which is not being forwarded only if
654 * the underlying protocol allows the body to be fetched
656 if (separatefetchbody && suppress_forward)
657 suppress_readbody = TRUE;
660 * If we're using IMAP4 or something else that
661 * can fetch headers separately from bodies,
662 * it's time to request the body now. This
663 * fetch may be skipped if we got an anti-spam
664 * or other PS_REFUSED error response during
667 if (!suppress_readbody)
669 if (separatefetchbody)
672 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len)))
675 * Work around a bug in Novell's
676 * broken GroupWise IMAP server;
677 * its body FETCH response is missing
678 * the required length for the data
679 * string. This violates RFC2060.
682 len = msgsize - msgblk.msglen;
683 if (outlevel > O_SILENT && !wholesize)
685 GT_(" (%d body octets)"), len);
688 /* process the body now */
689 err = readbody(mailserver_socket,
693 if (err == PS_TRANSIENT)
694 suppress_delete = suppress_forward = TRUE;
698 /* tell server we got it OK and resynchronize */
699 if (ctl->server.base_protocol->trail)
701 if (outlevel >= O_VERBOSE && !is_a_file(1) && !run.use_syslog)
707 err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, tag);
713 /* count # messages forwarded on this pass */
714 if (!suppress_forward)
718 * Check to see if the numbers matched?
720 * Yes, some servers foo this up horribly.
721 * All IMAP servers seem to get it right, and
722 * so does Eudora QPOP at least in 2.xx
725 * Microsoft Exchange gets it completely
726 * wrong, reporting compressed rather than
727 * actual sizes (so the actual length of
728 * message is longer than the reported size).
729 * Another fine example of Microsoft brain death!
731 * Some older POP servers, like the old UCB
732 * POP server and the pre-QPOP QUALCOMM
733 * versions, report a longer size in the LIST
734 * response than actually gets shipped up.
735 * It's unclear what is going on here, as the
736 * QUALCOMM server (at least) seems to be
737 * reporting the on-disk size correctly.
739 * qmail-pop3d also goofs up message sizes and does not
740 * count the line end characters properly.
742 if (msgblk.msglen != msgsize)
744 if (outlevel >= O_DEBUG)
746 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
747 ctl->remotename, ctl->server.truename, num,
748 msgblk.msglen, msgsize);
751 /* end-of-message processing starts here */
752 if (!close_sink(ctl, &msgblk, !suppress_forward))
755 suppress_delete = TRUE;
763 * At this point in flow of control, either
764 * we've bombed on a protocol error or had
765 * delivery refused by the SMTP server
766 * (unlikely -- I've never seen it) or we've
767 * seen `accepted for delivery' and the
768 * message is shipped. It's safe to mark the
769 * message seen and delete it on the server
773 /* maybe we delete this message now? */
776 if (outlevel > O_SILENT)
777 report_complete(stdout, GT_(" retained\n"));
779 else if (ctl->server.base_protocol->delete_msg
781 && ((msgcode >= 0 && !ctl->keep)
782 || (msgcode == MSGLEN_OLD && ctl->flush)
783 || (msgcode == MSGLEN_TOOLARGE && ctl->limitflush)))
786 if (outlevel > O_SILENT)
787 report_complete(stdout, GT_(" flushed\n"));
788 err = (ctl->server.base_protocol->delete_msg)(mailserver_socket, ctl, num);
794 if ( (outlevel >= O_VERBOSE) ||
795 /* To avoid flooding the syslog when using --keep,
796 * report "Skipped message" only when:
797 * 1) --verbose is on, or
798 * 2) fetchmail does not use syslog, or
799 * 3) the message was skipped for some other
800 * reason than just being old.
802 (outlevel > O_SILENT && (!run.use_syslog || msgcode != MSGLEN_OLD))
804 report_complete(stdout, GT_(" not flushed\n"));
806 /* maybe we mark this message as seen now? */
807 if (ctl->server.base_protocol->mark_seen
809 && (msgcode >= 0 && ctl->keep))
811 err = (ctl->server.base_protocol->mark_seen)(mailserver_socket, ctl, num);
817 /* perhaps this as many as we're ready to handle */
818 if (maxfetch && maxfetch <= *fetches && num < count)
820 int remcount = count - (*fetches - initialfetches);
822 ngettext("fetchlimit %d reached; %d message left on server %s account %s\n",
823 "fetchlimit %d reached; %d messages left on server %s account %s\n", remcount),
824 maxfetch, remcount, ctl->server.truename, ctl->remotename);
827 } /* for (num = 1; num <= count; num++) */
832 /* retrieve messages from server using given protocol method table */
833 static int do_session(
834 /* parsed options with merged-in defaults */
836 /* protocol method table */
837 const struct method *proto,
838 /* maximum number of messages to fetch */
841 static int *msgsizes;
842 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
844 int deletions = 0, js;
846 SIGHANDLERTYPE alrmsave;
848 ctl->server.base_protocol = proto;
853 init_transact(proto);
855 /* set up the server-nonresponse timeout */
856 alrmsave = set_signal_handler(SIGALRM, timeout_handler);
857 mytimeout = ctl->server.timeout;
859 if ((js = sigsetjmp(restart,1)))
861 /* exception caught */
864 sigfillset(&allsigs);
865 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
868 fm_freeaddrinfo(ai0); ai0 = NULL;
872 fm_freeaddrinfo(ai1); ai1 = NULL;
875 if (js == THROW_TIMEOUT)
877 if (phase == OPEN_WAIT)
879 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
880 ctl->server.timeout, ctl->server.pollname);
881 else if (phase == SERVER_WAIT)
883 GT_("timeout after %d seconds waiting for server %s.\n"),
884 ctl->server.timeout, ctl->server.pollname);
885 else if (phase == FORWARDING_WAIT)
887 GT_("timeout after %d seconds waiting for %s.\n"),
889 ctl->mda ? "MDA" : "SMTP");
890 else if (phase == LISTENER_WAIT)
892 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
895 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
898 * If we've exceeded our threshold for consecutive timeouts,
899 * try to notify the user, then mark the connection wedged.
900 * Don't do this if the connection can idle, though; idle
901 * timeouts just mean the frequency of mail is low.
903 if (timeoutcount > MAX_TIMEOUTS
904 && !open_warning_by_mail(ctl))
906 stuff_warning(iana_charset, ctl,
907 GT_("Subject: fetchmail sees repeated timeouts"));
908 stuff_warning(NULL, ctl, "%s", "");
909 stuff_warning(NULL, ctl,
910 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\n"),
912 ctl->remotename, ctl->server.truename);
913 stuff_warning(NULL, ctl,
914 GT_("This could mean that your mailserver is stuck, or that your SMTP\n" \
915 "server is wedged, or that your mailbox file on the server has been\n" \
916 "corrupted by a server error. You can run `fetchmail -v -v' to\n" \
917 "diagnose the problem.\n\n" \
918 "Fetchmail won't poll this mailbox again until you restart it.\n"));
919 close_warning_by_mail(ctl, (struct msgblk *)NULL);
929 /* sigsetjmp returned zero -> normal operation */
930 char buf[MSGBUFSIZE+1], *realhost;
931 int count, newm, bytes;
932 int fetches, dispatches, oldphase;
935 /* execute pre-initialization command, if any */
936 if (ctl->preconnect && (err = system(ctl->preconnect)))
938 if (WIFSIGNALED(err))
940 GT_("pre-connection command terminated with signal %d\n"), WTERMSIG(err));
943 GT_("pre-connection command failed with status %d\n"), WEXITSTATUS(err));
948 /* open a socket to the mail server */
951 set_timeout(mytimeout);
953 #ifdef HAVE_PKG_hesiod
954 /* If either the pollname or vianame are "hesiod" we want to
955 lookup the user's hesiod pobox host */
956 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
957 struct hes_postoffice *hes_p;
958 hes_p = hes_getmailhost(ctl->remotename);
959 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
960 free(ctl->server.queryname);
961 ctl->server.queryname = xstrdup(hes_p->po_host);
963 free(ctl->server.via);
964 ctl->server.via = xstrdup(hes_p->po_host);
967 GT_("couldn't find HESIOD pobox for %s\n"),
974 * Canonicalize the server truename for later use. This also
975 * functions as a probe for whether the mailserver is accessible.
976 * We try it on each poll cycle until we get a result. This way,
977 * fetchmail won't fail if started up when the network is inaccessible.
979 if (ctl->server.dns && !ctl->server.trueaddr)
981 if (ctl->server.lead_server)
983 char *leadname = ctl->server.lead_server->truename;
985 /* prevent core dump from ill-formed or duplicate entry */
988 report(stderr, GT_("Lead server has no name.\n"));
995 xfree(ctl->server.truename);
996 ctl->server.truename = xstrdup(leadname);
1000 struct addrinfo hints, *res;
1003 memset(&hints, 0, sizeof(hints));
1004 hints.ai_socktype = SOCK_STREAM;
1005 hints.ai_family = AF_UNSPEC;
1006 hints.ai_flags = AI_CANONNAME;
1008 error = fm_getaddrinfo(ctl->server.queryname, NULL, &hints, &res);
1012 GT_("couldn't find canonical DNS name of %s (%s): %s\n"),
1013 ctl->server.pollname, ctl->server.queryname,
1014 gai_strerror(error));
1022 xfree(ctl->server.truename);
1023 /* Older FreeBSD versions return NULL in ai_canonname
1024 * if they cannot canonicalize, rather than copying
1025 * the queryname here, as IEEE Std 1003.1-2001
1026 * requires. Work around NULL. */
1027 if (res->ai_canonname != NULL) {
1028 ctl->server.truename = xstrdup(res->ai_canonname);
1030 ctl->server.truename = xstrdup(ctl->server.queryname);
1032 ctl->server.trueaddr = (struct sockaddr *)xmalloc(res->ai_addrlen);
1033 ctl->server.trueaddr_len = res->ai_addrlen;
1034 memcpy(ctl->server.trueaddr, res->ai_addr, res->ai_addrlen);
1035 fm_freeaddrinfo(res);
1040 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
1042 /* allow time for the port to be set up if we have a plugin */
1043 if (ctl->server.plugin)
1045 if ((mailserver_socket = SockOpen(realhost,
1046 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
1047 ctl->server.plugin, &ai0)) == -1)
1049 char errbuf[BUFSIZ];
1052 * Avoid generating a bogus error every poll cycle when we're
1053 * in daemon mode but the connection to the outside world
1056 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
1057 && run.poll_interval))
1059 report_build(stderr, GT_("%s connection to %s failed"),
1060 ctl->server.base_protocol->name, ctl->server.pollname);
1061 strlcpy(errbuf, strerror(err_no), sizeof(errbuf));
1062 report_complete(stderr, ": %s\n", errbuf);
1066 * Don't use this. It was an attempt to address Debian bug
1067 * #47143 (Notify user by mail when pop server nonexistent).
1068 * Trouble is, that doesn't work; you trip over the case
1069 * where your SLIP or PPP link is down...
1071 /* warn the system administrator */
1072 if (open_warning_by_mail(ctl) == 0)
1074 stuff_warning(iana_charset, ctl,
1075 GT_("Subject: Fetchmail unreachable-server warning."));
1076 stuff_warning(NULL, ctl, "");
1077 stuff_warning(NULL, ctl, GT_("Fetchmail could not reach the mail server %s:"),
1078 ctl->server.pollname);
1079 stuff_warning(NULL, ctl, errbuf, ctl->server.pollname);
1080 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1091 /* Save the socket opened. Useful if Fetchmail hangs on SSLOpen
1092 * because the socket can be closed.
1094 mailserver_socket_temp = mailserver_socket;
1095 set_timeout(mytimeout);
1097 /* perform initial SSL handshake on open connection */
1099 SSLOpen(mailserver_socket, ctl->sslcert, ctl->sslkey,
1100 ctl->sslproto, ctl->sslcertck, ctl->sslcertpath,
1101 ctl->sslfingerprint, ctl->sslcommonname ?
1102 ctl->sslcommonname : realhost, ctl->server.pollname,
1103 &ctl->remotename) == -1)
1105 report(stderr, GT_("SSL connection failed.\n"));
1110 /* Fetchmail didn't hang on SSLOpen,
1111 * then no need to set mailserver_socket_temp
1113 mailserver_socket_temp = -1;
1116 /* A timeout is still defined before SSLOpen,
1117 * then Fetchmail hanging on SSLOpen is handled.
1122 if (ctl->server.authenticate == A_KERBEROS_V4 && (strcasecmp(proto->name,"IMAP") != 0))
1124 set_timeout(mytimeout);
1125 err = kerberos_auth(mailserver_socket, ctl->server.truename,
1126 ctl->server.principal);
1131 #endif /* KERBEROS_V4 */
1134 if (ctl->server.authenticate == A_KERBEROS_V5)
1136 set_timeout(mytimeout);
1137 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1142 #endif /* KERBEROS_V5 */
1144 /* accept greeting message from mail server */
1145 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1149 /* try to get authorized to fetch mail */
1150 stage = STAGE_GETAUTH;
1151 if (ctl->server.base_protocol->getauth)
1153 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1157 if (err == PS_LOCKBUSY)
1158 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1160 ctl->server.truename);
1161 else if (err == PS_SERVBUSY)
1162 report(stderr, GT_("Server busy error on %s@%s\n"),
1164 ctl->server.truename);
1165 else if (err == PS_AUTHFAIL)
1167 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1169 ctl->server.truename,
1170 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1174 * If we're running in background, try to mail the
1175 * calling user a heads-up about the authentication
1176 * failure once it looks like this isn't a fluke
1177 * due to the server being temporarily inaccessible.
1178 * When we get third succesive failure, we notify the user
1179 * but only if we haven't already managed to get
1180 * authorization. After that, once we get authorization
1181 * we let the user know service is restored.
1183 if (run.poll_interval
1184 && !ctl->wehavesentauthnote
1185 && ((ctl->wehaveauthed && ++ctl->authfailcount >= 10)
1186 || (!ctl->wehaveauthed && ++ctl->authfailcount >= 3))
1187 && !open_warning_by_mail(ctl))
1189 ctl->wehavesentauthnote = 1;
1190 stuff_warning(iana_charset, ctl,
1191 GT_("Subject: fetchmail authentication failed on %s@%s"),
1192 ctl->remotename, ctl->server.truename);
1193 stuff_warning(NULL, ctl, "%s", "");
1194 stuff_warning(NULL, ctl,
1195 GT_("Fetchmail could not get mail from %s@%s.\n"),
1197 ctl->server.truename);
1198 if (ctl->wehaveauthed) {
1199 stuff_warning(NULL, ctl, GT_("\
1200 The attempt to get authorization failed.\n\
1201 Since we have already succeeded in getting authorization for this\n\
1202 connection, this is probably another failure mode (such as busy server)\n\
1203 that fetchmail cannot distinguish because the server didn't send a useful\n\
1205 stuff_warning(NULL, ctl, GT_("\
1207 However, if you HAVE changed your account details since starting the\n\
1208 fetchmail daemon, you need to stop the daemon, change your configuration\n\
1209 of fetchmail, and then restart the daemon.\n\
1211 The fetchmail daemon will continue running and attempt to connect\n\
1212 at each cycle. No future notifications will be sent until service\n\
1215 stuff_warning(NULL, ctl, GT_("\
1216 The attempt to get authorization failed.\n\
1217 This probably means your password is invalid, but some servers have\n\
1218 other failure modes that fetchmail cannot distinguish from this\n\
1219 because they don't send useful error messages on login failure.\n\
1221 The fetchmail daemon will continue running and attempt to connect\n\
1222 at each cycle. No future notifications will be sent until service\n\
1225 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1228 else if (err == PS_REPOLL)
1230 if (outlevel >= O_VERBOSE)
1231 report(stderr, GT_("Repoll immediately on %s@%s\n"),
1233 ctl->server.truename);
1236 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1238 ctl->server.truename);
1245 * This connection has given us authorization at least once.
1247 * There are dodgy server (clubinternet.fr for example) that
1248 * give spurious authorization failures on patently good
1249 * account/password details, then 5 minutes later let you in!
1251 * This is meant to build in some tolerance of such nasty bits
1254 ctl->wehaveauthed = 1;
1255 /*if (ctl->authfailcount >= 3)*/
1256 if (ctl->wehavesentauthnote)
1258 ctl->wehavesentauthnote = 0;
1260 GT_("Authorization OK on %s@%s\n"),
1262 ctl->server.truename);
1263 if (!open_warning_by_mail(ctl))
1265 stuff_warning(iana_charset, ctl,
1266 GT_("Subject: fetchmail authentication OK on %s@%s"),
1267 ctl->remotename, ctl->server.truename);
1268 stuff_warning(NULL, ctl, "%s", "");
1269 stuff_warning(NULL, ctl,
1270 GT_("Fetchmail was able to log into %s@%s.\n"),
1272 ctl->server.truename);
1273 stuff_warning(NULL, ctl,
1274 GT_("Service has been restored.\n"));
1275 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1280 * Reporting only after the first three
1281 * consecutive failures, or ten consecutive
1282 * failures after we have managed to get
1285 ctl->authfailcount = 0;
1289 ctl->errcount = fetches = 0;
1291 /* now iterate over each folder selected */
1292 for (idp = ctl->mailboxes; idp; idp = idp->next)
1294 ctl->folder = idp->id;
1300 /* reset timeout, in case we did an IDLE */
1301 mytimeout = ctl->server.timeout;
1303 if (outlevel >= O_DEBUG)
1306 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1308 report(stdout, GT_("selecting or re-polling default folder\n"));
1311 /* compute # of messages and number of new messages waiting */
1312 stage = STAGE_GETRANGE;
1313 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &newm, &bytes);
1317 /* show user how many messages we downloaded */
1319 (void) snprintf(buf, sizeof(buf),
1320 GT_("%s at %s (folder %s)"),
1321 ctl->remotename, ctl->server.pollname, idp->id);
1323 (void) snprintf(buf, sizeof(buf), GT_("%s at %s"),
1324 ctl->remotename, ctl->server.pollname);
1325 if (outlevel > O_SILENT)
1327 if (count == -1) /* only used for ETRN */
1328 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1329 else if (count != 0)
1331 if (newm != -1 && (count - newm) > 0)
1332 report_build(stdout, ngettext("%d message (%d %s) for %s", "%d messages (%d %s) for %s", (unsigned long)count),
1335 ngettext("seen", "seen", (unsigned long)count-newm),
1338 report_build(stdout, ngettext("%d message for %s",
1339 "%d messages for %s",
1343 report_complete(stdout, ".\n");
1345 report_complete(stdout, GT_(" (%d octets).\n"), bytes);
1349 /* these are pointless in normal daemon mode */
1350 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1351 report(stdout, GT_("No mail for %s\n"), buf);
1355 /* very important, this is where we leave the do loop */
1361 if (newm == -1 || ctl->fetchall)
1363 fetches = newm; /* set error status correctly */
1365 * There used to be a `goto noerror' here, but this
1366 * prevented checking of multiple folders. This
1367 * comment is a reminder in case I introduced some
1368 * subtle bug by removing it...
1376 * Don't trust the message count passed by the server.
1377 * Without this check, it might be possible to do a
1378 * DNS-spoofing attack that would pass back a ridiculous
1379 * count, and allocate a malloc area that would overlap
1380 * a portion of the stack.
1382 if ((unsigned)count > INT_MAX/sizeof(int))
1384 report(stderr, GT_("bogus message count!"));
1390 * We need the size of each message before it's
1391 * loaded in order to pass it to the ESMTP SIZE
1392 * option. If the protocol has a getsizes method,
1393 * we presume this means it doesn't get reliable
1394 * sizes from message fetch responses.
1396 * If the protocol supports getting sizes of subset of
1397 * messages, we skip this step now.
1399 if (proto->getsizes &&
1400 !(proto->getpartialsizes && NUM_NONZERO(ctl->fetchsizelimit)))
1403 msgsizes = (int *)xmalloc(sizeof(int) * count);
1404 for (i = 0; i < count; i++)
1407 stage = STAGE_GETSIZES;
1408 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1415 for (i = 0; i < count; i++)
1416 bytes += msgsizes[i];
1420 /* read, forward, and delete messages */
1421 stage = STAGE_FETCH;
1423 /* fetch in lockstep mode */
1424 err = fetch_messages(mailserver_socket, ctl,
1427 &fetches, &dispatches, &deletions);
1428 if (err != PS_SUCCESS && err != PS_MAXFETCH)
1431 if (!check_only && ctl->skipped
1432 && run.poll_interval > 0 && !nodetach)
1434 clean_skipped_list(&ctl->skipped);
1435 send_size_warnings(ctl);
1439 /* end-of-mailbox processing before we repoll or switch to another one */
1440 if (ctl->server.base_protocol->end_mailbox_poll)
1442 tmperr = (ctl->server.base_protocol->end_mailbox_poll)(mailserver_socket, ctl);
1448 /* Return now if we have reached the fetchlimit */
1449 if (maxfetch && maxfetch <= fetches)
1453 * Only repoll if we either had some actual forwards
1454 * or are idling for new mails and had no errors.
1455 * Otherwise it is far too easy to get into infinite loops.
1457 (ctl->server.base_protocol->retry && (dispatches || ctl->idle) && !ctl->errcount);
1460 /* XXX: From this point onwards, preserve err unless a new error has occurred */
1463 /* PS_SUCCESS, PS_MAXFETCH: ordinary termination with no errors -- officially log out */
1464 stage = STAGE_LOGOUT;
1465 tmperr = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1466 if (tmperr != PS_SUCCESS)
1469 * Hmmmm...arguably this would be incorrect if we had fetches but
1470 * no dispatches (due to oversized messages, etc.)
1472 else if (err == PS_SUCCESS && fetches == 0)
1475 * Close all SMTP delivery sockets. For optimum performance
1476 * we'd like to hold them open til end of run, but (1) this
1477 * loses if our poll interval is longer than the MTA's
1478 * inactivity timeout, and (2) some MTAs (like smail) don't
1479 * deliver after each message, but rather queue up mail and
1480 * wait to actually deliver it until the input socket is
1483 * don't send QUIT for ODMR case because we're acting as a
1484 * proxy between the SMTP server and client.
1486 smtp_close(ctl, ctl->server.protocol != P_ODMR);
1487 cleanupSockClose(mailserver_socket);
1491 /* we only get here on error */
1492 if (err != 0 && err != PS_SOCKET && err != PS_REPOLL)
1494 stage = STAGE_LOGOUT;
1495 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1498 /* try to clean up all streams */
1501 * Sending SMTP QUIT on signal is theoretically nice, but led
1502 * to a subtle bug. If fetchmail was terminated by signal
1503 * while it was shipping message text, it would hang forever
1504 * waiting for a command acknowledge. In theory we could
1505 * enable the QUIT only outside of the message send. In
1506 * practice, we don't care. All mailservers hang up on a
1507 * dropped TCP/IP connection anyway.
1510 if (mailserver_socket != -1) {
1511 cleanupSockClose(mailserver_socket);
1512 mailserver_socket = -1;
1514 /* If there was a connect timeout, the socket should be closed.
1515 * mailserver_socket_temp contains the socket to close.
1517 if (mailserver_socket_temp != -1) {
1518 cleanupSockClose(mailserver_socket_temp);
1519 mailserver_socket_temp = -1;
1523 /* no report on PS_AUTHFAIL */
1528 msg = GT_("socket");
1531 msg = GT_("missing or bad RFC822 header");
1537 msg = GT_("client/server synchronization");
1540 msg = GT_("client/server protocol");
1543 msg = GT_("lock busy on server");
1546 msg = GT_("SMTP transaction");
1549 msg = GT_("DNS lookup");
1552 msg = GT_("undefined");
1556 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT
1558 report(stderr, GT_("%s error while fetching from %s@%s and delivering to SMTP host %s\n"),
1559 msg, ctl->remotename, ctl->server.pollname,
1560 ctl->smtphost ? ctl->smtphost : GT_("unknown"));
1562 report(stderr, GT_("%s error while fetching from %s@%s\n"),
1563 msg, ctl->remotename, ctl->server.pollname);
1570 /* execute wrapup command, if any */
1571 if (ctl->postconnect && (tmperr = system(ctl->postconnect)))
1573 if (WIFSIGNALED(tmperr))
1574 report(stderr, GT_("post-connection command terminated with signal %d\n"), WTERMSIG(tmperr));
1576 report(stderr, GT_("post-connection command failed with status %d\n"), WEXITSTATUS(tmperr));
1577 if (err == PS_SUCCESS)
1581 set_timeout(0); /* cancel any pending alarm */
1582 set_signal_handler(SIGALRM, alrmsave);
1586 /** retrieve messages from server using given protocol method table */
1587 int do_protocol(struct query *ctl /** parsed options with merged-in defaults */,
1588 const struct method *proto /** protocol method table */)
1593 if (ctl->server.authenticate == A_KERBEROS_V4)
1595 report(stderr, GT_("Kerberos V4 support not linked.\n"));
1598 #endif /* KERBEROS_V4 */
1601 if (ctl->server.authenticate == A_KERBEROS_V5)
1603 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1606 #endif /* KERBEROS_V5 */
1608 /* lacking methods, there are some options that may fail */
1611 /* check for unsupported options */
1614 GT_("Option --flush is not supported with %s\n"),
1618 else if (ctl->fetchall) {
1620 GT_("Option --all is not supported with %s\n"),
1625 if (!(proto->getsizes || proto->getpartialsizes)
1626 && NUM_SPECIFIED(ctl->limit))
1629 GT_("Option --limit is not supported with %s\n"),
1635 * If no expunge limit or we do expunges within the driver,
1636 * then just do one session, passing in any fetchlimit.
1638 if ((ctl->keep && !ctl->flush) ||
1639 proto->retry || !NUM_SPECIFIED(ctl->expunge))
1640 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1642 * There's an expunge limit, and it isn't handled in the driver itself.
1643 * OK; do multiple sessions, each fetching a limited # of messages.
1644 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1645 * (if it was nonzero).
1651 int expunge = NUM_VALUE_OUT(ctl->expunge);
1652 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1655 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1656 expunge = fetchlimit - totalcount;
1657 err = do_session(ctl, proto, expunge);
1658 totalcount += expunge;
1659 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1661 if (err != PS_LOCKBUSY)
1663 else if (lockouts >= MAX_LOCKOUTS)
1665 else /* err == PS_LOCKBUSY */
1668 * Allow time for the server lock to release. if we
1669 * don't do this, we'll often hit a locked-mailbox
1670 * condition and fail.
1676 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1683 /* driver.c ends here */