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>
28 #ifdef HAVE_SYS_WAIT_H
32 #ifdef HAVE_NET_SOCKET_H
33 #include <net/socket.h>
39 #if defined(HAVE_RES_SEARCH) || defined(HAVE_GETHOSTBYNAME)
42 #endif /* defined(HAVE_RES_SEARCH) || defined(HAVE_GETHOSTBYNAME) */
46 #include <netinet/in.h>
47 #endif /* KERBEROS_V4 */
52 #include "fetchmail.h"
55 /* throw types for runtime errors */
56 #define THROW_TIMEOUT 1 /* server timed out */
57 #define THROW_SIGPIPE 2 /* SIGPIPE on stream socket */
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 static int timeoutcount; /* count consecutive timeouts */
73 static int idletimeout; /* timeout occured in idle stage? */
75 static jmp_buf restart;
77 int isidletimeout(void)
78 /* last timeout occured in idle stage? */
83 void set_timeout(int timeleft)
84 /* reset the nonresponse-timeout */
86 #if !defined(__EMX__) && !defined(__BEOS__)
87 struct itimerval ntimeout;
94 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
95 ntimeout.it_value.tv_sec = timeleft;
96 ntimeout.it_value.tv_usec = 0;
97 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
101 static RETSIGTYPE timeout_handler (int signal)
102 /* handle SIGALRM signal indicating a server timeout */
104 if(stage != STAGE_IDLE) {
106 longjmp(restart, THROW_TIMEOUT);
111 static RETSIGTYPE sigpipe_handler (int signal)
112 /* handle SIGPIPE signal indicating a broken stream socket */
114 longjmp(restart, THROW_SIGPIPE);
117 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
119 static int cleanupSockClose (int fd)
120 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
123 SIGHANDLERTYPE alrmsave;
124 alrmsave = set_signal_handler(SIGALRM, null_signal_handler);
125 set_timeout(CLEANUP_TIMEOUT);
126 scerror = SockClose(fd);
128 set_signal_handler(SIGALRM, alrmsave);
133 static int kerberos_auth(socket, canonical, principal)
134 /* authenticate to the server host using Kerberos V4 */
135 int socket; /* socket to server host */
136 char *canonical; /* server name */
143 Key_schedule schedule;
145 char * prin_copy = (char *) NULL;
146 char * prin = (char *) NULL;
147 char * inst = (char *) NULL;
148 char * realm = (char *) NULL;
150 if (principal != (char *)NULL && *principal)
153 prin = prin_copy = xstrdup(principal);
154 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
160 while (*cp && *cp != '@')
170 xalloca(ticket, KTEXT, sizeof (KTEXT_ST));
171 rem = (krb_sendauth (0L, socket, ticket,
173 inst ? inst : canonical,
174 realm ? realm : ((char *) (krb_realmofhost (canonical))),
179 ((struct sockaddr_in *) 0),
180 ((struct sockaddr_in *) 0),
188 report(stderr, GT_("kerberos error %s\n"), (krb_get_err_text (rem)));
189 return (PS_AUTHFAIL);
193 #endif /* KERBEROS_V4 */
196 static int kerberos5_auth(socket, canonical)
197 /* authenticate to the server host using Kerberos V5 */
198 int socket; /* socket to server host */
199 const char *canonical; /* server name */
201 krb5_error_code retval;
202 krb5_context context;
204 krb5_principal client = NULL, server = NULL;
205 krb5_error *err_ret = NULL;
207 krb5_auth_context auth_context = NULL;
209 krb5_init_context(&context);
210 krb5_init_ets(context);
211 krb5_auth_con_init(context, &auth_context);
213 if (retval = krb5_cc_default(context, &ccdef)) {
214 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
218 if (retval = krb5_cc_get_principal(context, ccdef, &client)) {
219 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
223 if (retval = krb5_sname_to_principal(context, canonical, "pop",
226 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
230 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
231 "KPOPV1.0", client, server,
232 AP_OPTS_MUTUAL_REQUIRED,
233 NULL, /* no data to checksum */
234 0, /* no creds, use ccache instead */
238 NULL); /* don't need reply */
240 krb5_free_principal(context, server);
241 krb5_free_principal(context, client);
242 krb5_auth_con_free(context, auth_context);
246 if (err_ret && err_ret->e_text) {
247 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
248 error_message(retval),
251 if (err_ret && err_ret->text.length) {
252 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
253 error_message(retval),
254 err_ret->text.length,
257 krb5_free_error(context, err_ret);
259 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
265 #endif /* KERBEROS_V5 */
267 static void clean_skipped_list(struct idlist **skipped_list)
268 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
270 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
271 prev = current = head = *skipped_list;
277 /* if item has no reference, remove it */
278 if (current && current->val.status.mark == 0)
280 if (current == head) /* remove first item (head) */
282 head = current->next;
283 if (current->id) free(current->id);
285 prev = current = head;
287 else /* remove middle/last item */
291 if (current->id) free(current->id);
296 else /* skip this item */
299 current = current->next;
303 *skipped_list = head;
306 static void send_size_warnings(struct query *ctl)
307 /* send warning mail with skipped msg; reset msg count when user notified */
310 int msg_to_send = FALSE;
311 struct idlist *head=NULL, *current=NULL;
312 int max_warning_poll_count;
318 /* don't start a notification message unless we need to */
319 for (current = head; current; current = current->next)
320 if (current->val.status.num == 0 && current->val.status.mark)
326 * There's no good way to recover if we can't send notification mail,
327 * but it's not a disaster, either, since the skipped mail will not
330 if (open_warning_by_mail(ctl, (struct msgblk *)NULL))
333 GT_("Subject: Fetchmail oversized-messages warning.\n"
335 "The following oversized messages remain on the mail server %s:"),
336 ctl->server.pollname);
338 if (run.poll_interval == 0)
339 max_warning_poll_count = 0;
341 max_warning_poll_count = ctl->warnings/run.poll_interval;
343 /* parse list of skipped msg, adding items to the mail */
344 for (current = head; current; current = current->next)
346 if (current->val.status.num == 0 && current->val.status.mark)
348 nbr = current->val.status.mark;
349 size = atoi(current->id);
351 GT_("\t%d msg %d octets long skipped by fetchmail.\n"),
354 current->val.status.num++;
355 current->val.status.mark = 0;
357 if (current->val.status.num >= max_warning_poll_count)
358 current->val.status.num = 0;
361 close_warning_by_mail(ctl, (struct msgblk *)NULL);
364 static void mark_oversized(struct query *ctl, int num, int size)
365 /* mark a message oversized */
367 struct idlist *current=NULL, *tmp=NULL;
371 /* convert size to string */
373 snprintf(sizestr, sizeof(sizestr),
376 #endif /* HAVE_SNPRINTF */
379 /* build a list of skipped messages
380 * val.id = size of msg (string cnvt)
381 * val.status.num = warning_poll_count
382 * val.status.mask = nbr of msg this size
385 current = ctl->skipped;
387 /* initialise warning_poll_count to the
388 * current value so that all new msg will
389 * be included in the next mail
391 cnt = current ? current->val.status.num : 0;
393 /* if entry exists, increment the count */
394 if (current && str_in_list(¤t, sizestr, FALSE))
396 for ( ; current; current = current->next)
398 if (strcmp(current->id, sizestr) == 0)
400 current->val.status.mark++;
405 /* otherwise, create a new entry */
406 /* initialise with current poll count */
409 tmp = save_str(&ctl->skipped, sizestr, 1);
410 tmp->val.status.num = cnt;
414 static int fetch_messages(int mailserver_socket, struct query *ctl,
415 int count, int *msgsizes, int *msgcodes, int maxfetch,
416 int *fetches, int *dispatches, int *deletions)
417 /* fetch messages in lockstep mode */
421 for (num = 1; num <= count; num++)
423 flag suppress_delete = FALSE;
424 flag suppress_forward = FALSE;
425 flag suppress_readbody = FALSE;
426 flag retained = FALSE;
428 if (msgcodes[num-1] < 0)
430 if ((msgcodes[num-1] == MSGLEN_TOOLARGE) && !check_only)
431 mark_oversized(ctl, num, msgsizes[num-1]);
432 /* To avoid flooding the syslog when using --keep,
433 * report "Skipped message" only when:
434 * 1) --verbose is on, or
435 * 2) fetchmail does not use syslog, or
436 * 3) the message was skipped for some other
437 * reason than being old.
439 if ( (outlevel >= O_VERBOSE) ||
440 (outlevel > O_SILENT && (!run.use_syslog || msgcodes[num-1] != MSGLEN_OLD))
444 GT_("skipping message %s@%s:%d (%d octets)"),
445 ctl->remotename, ctl->server.truename, num,
447 switch (msgcodes[num-1])
451 * Invalid lengths are produced by Post Office/NT's
452 * annoying habit of randomly prepending bogus
453 * LIST items of length -1. Patrick Audley
454 * <paudley@pobox.com> tells us: LIST shows a
455 * size of -1, RETR and TOP return "-ERR
456 * System error - couldn't open message", and
457 * DELE succeeds but doesn't actually delete
460 report_build(stdout, GT_(" (length -1)"));
462 case MSGLEN_TOOLARGE:
463 report_build(stdout, GT_(" (oversized)"));
470 flag wholesize = !ctl->server.base_protocol->fetch_body;
472 /* request a message */
473 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
474 if (err == PS_TRANSIENT) /* server is probably Exchange */
477 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)\n"),
478 ctl->remotename, ctl->server.truename, num,
485 /* -1 means we didn't see a size in the response */
488 len = msgsizes[num - 1];
492 if (outlevel > O_SILENT)
494 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
495 ctl->remotename, ctl->server.truename,
499 report_build(stdout, GT_(" (%d %soctets)"),
500 len, wholesize ? "" : GT_("header "));
501 if (outlevel >= O_VERBOSE)
502 report_complete(stdout, "\n");
504 report_complete(stdout, " ");
508 * Read the message headers and ship them to the
511 err = readheaders(mailserver_socket, len, msgsizes[num-1],
513 if (err == PS_RETAINED)
515 suppress_forward = suppress_delete = retained = TRUE;
516 /* do not read the body only if the underlying protocol
517 * allows the body to be fetched separately */
518 if (ctl->server.base_protocol->fetch_body)
519 suppress_readbody = TRUE;
521 else if (err == PS_TRANSIENT)
523 suppress_delete = suppress_forward = TRUE;
524 if (ctl->server.base_protocol->fetch_body)
525 suppress_readbody = TRUE;
527 else if (err == PS_REFUSED)
529 suppress_forward = TRUE;
530 if (ctl->server.base_protocol->fetch_body)
531 suppress_readbody = TRUE;
533 else if (err == PS_TRUNCATED)
535 if (ctl->server.base_protocol->fetch_body)
536 suppress_readbody = TRUE;
537 len = 0; /* suppress body processing */
543 * If we're using IMAP4 or something else that
544 * can fetch headers separately from bodies,
545 * it's time to request the body now. This
546 * fetch may be skipped if we got an anti-spam
547 * or other PS_REFUSED error response during
550 if (ctl->server.base_protocol->fetch_body && !suppress_readbody)
552 if (outlevel >= O_VERBOSE && !isafile(1))
558 if ((err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num)))
561 if (!suppress_forward)
563 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len)))
566 * Work around a bug in Novell's
567 * broken GroupWise IMAP server;
568 * its body FETCH response is missing
569 * the required length for the data
570 * string. This violates RFC2060.
573 len = msgsizes[num-1] - msgblk.msglen;
574 if (outlevel > O_SILENT && !wholesize)
575 report_complete(stdout,
576 GT_(" (%d body octets) "), len);
580 /* process the body now */
583 if (suppress_readbody)
589 err = readbody(mailserver_socket,
594 if (err == PS_TRANSIENT)
595 suppress_delete = suppress_forward = TRUE;
599 /* tell server we got it OK and resynchronize */
600 if (ctl->server.base_protocol->trail)
602 if (outlevel >= O_VERBOSE && !isafile(1))
608 err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num);
614 /* count # messages forwarded on this pass */
615 if (!suppress_forward)
619 * Check to see if the numbers matched?
621 * Yes, some servers foo this up horribly.
622 * All IMAP servers seem to get it right, and
623 * so does Eudora QPOP at least in 2.xx
626 * Microsoft Exchange gets it completely
627 * wrong, reporting compressed rather than
628 * actual sizes (so the actual length of
629 * message is longer than the reported size).
630 * Another fine example of Microsoft brain death!
632 * Some older POP servers, like the old UCB
633 * POP server and the pre-QPOP QUALCOMM
634 * versions, report a longer size in the LIST
635 * response than actually gets shipped up.
636 * It's unclear what is going on here, as the
637 * QUALCOMM server (at least) seems to be
638 * reporting the on-disk size correctly.
640 if (msgblk.msglen != msgsizes[num-1])
642 if (outlevel >= O_DEBUG)
644 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
645 ctl->remotename, ctl->server.truename, num,
646 msgblk.msglen, msgsizes[num-1]);
649 /* end-of-message processing starts here */
650 if (!close_sink(ctl, &msgblk, !suppress_forward))
653 suppress_delete = TRUE;
660 * At this point in flow of control, either
661 * we've bombed on a protocol error or had
662 * delivery refused by the SMTP server
663 * (unlikely -- I've never seen it) or we've
664 * seen `accepted for delivery' and the
665 * message is shipped. It's safe to mark the
666 * message seen and delete it on the server
670 /* maybe we delete this message now? */
673 if (outlevel > O_SILENT)
674 report(stdout, GT_(" retained\n"));
676 else if (ctl->server.base_protocol->delete
678 && ((msgcodes[num-1] >= 0) ? !ctl->keep : ctl->flush))
681 if (outlevel > O_SILENT)
682 report_complete(stdout, GT_(" flushed\n"));
683 err = (ctl->server.base_protocol->delete)(mailserver_socket, ctl, num);
689 if ( (outlevel >= O_VERBOSE) ||
690 /* To avoid flooding the syslog when using --keep,
691 * report "Skipped message" only when:
692 * 1) --verbose is on, or
693 * 2) fetchmail does not use syslog, or
694 * 3) the message was skipped for some other
695 * reason than just being old.
697 (outlevel > O_SILENT && (!run.use_syslog || msgcodes[num-1] != MSGLEN_OLD))
699 report_complete(stdout, GT_(" not flushed\n"));
701 /* maybe we mark this message as seen now? */
702 if (ctl->server.base_protocol->mark_seen
704 && (msgcodes[num-1] >= 0 && ctl->keep))
706 err = (ctl->server.base_protocol->mark_seen)(mailserver_socket, ctl, num);
712 /* perhaps this as many as we're ready to handle */
713 if (maxfetch && maxfetch <= *fetches && *fetches < count)
715 report(stdout, GT_("fetchlimit %d reached; %d messages left on server %s account %s\n"),
716 maxfetch, count - *fetches, ctl->server.truename, ctl->remotename);
724 static int do_session(ctl, proto, maxfetch)
725 /* retrieve messages from server using given protocol method table */
726 struct query *ctl; /* parsed options with merged-in defaults */
727 const struct method *proto; /* protocol method table */
728 const int maxfetch; /* maximum number of messages to fetch */
732 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
734 int err, mailserver_socket = -1;
735 #endif /* HAVE_VOLATILE */
737 SIGHANDLERTYPE pipesave;
738 SIGHANDLERTYPE alrmsave;
740 ctl->server.base_protocol = proto;
744 init_transact(proto);
746 /* set up the server-nonresponse timeout */
747 alrmsave = set_signal_handler(SIGALRM, timeout_handler);
748 mytimeout = ctl->server.timeout;
750 /* set up the broken-pipe timeout */
751 pipesave = set_signal_handler(SIGPIPE, sigpipe_handler);
753 if ((js = setjmp(restart)))
755 #ifdef HAVE_SIGPROCMASK
757 * Don't rely on setjmp() to restore the blocked-signal mask.
758 * It does this under BSD but is required not to under POSIX.
760 * If your Unix doesn't have sigprocmask, better hope it has
761 * BSD-like behavior. Otherwise you may see fetchmail get
762 * permanently wedged after a second timeout on a bad read,
763 * because alarm signals were blocked after the first.
767 sigfillset(&allsigs);
768 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
769 #endif /* HAVE_SIGPROCMASK */
771 if (js == THROW_SIGPIPE)
773 set_signal_handler(SIGPIPE, SIG_IGN);
775 GT_("SIGPIPE thrown from an MDA or a stream socket error\n"));
778 else if (js == THROW_TIMEOUT)
780 if (phase == OPEN_WAIT)
782 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
783 ctl->server.timeout, ctl->server.pollname);
784 else if (phase == SERVER_WAIT)
786 GT_("timeout after %d seconds waiting for server %s.\n"),
787 ctl->server.timeout, ctl->server.pollname);
788 else if (phase == FORWARDING_WAIT)
790 GT_("timeout after %d seconds waiting for %s.\n"),
792 ctl->mda ? "MDA" : "SMTP");
793 else if (phase == LISTENER_WAIT)
795 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
798 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
801 * If we've exceeded our threshold for consecutive timeouts,
802 * try to notify the user, then mark the connection wedged.
803 * Don't do this if the connection can idle, though; idle
804 * timeouts just mean the frequency of mail is low.
806 if (timeoutcount > MAX_TIMEOUTS
807 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
810 GT_("Subject: fetchmail sees repeated timeouts\n"));
812 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\n"),
815 ctl->server.truename);
817 GT_("This could mean that your mailserver is stuck, or that your SMTP\n" \
818 "server is wedged, or that your mailbox file on the server has been\n" \
819 "corrupted by a server error. You can run `fetchmail -v -v' to\n" \
820 "diagnose the problem.\n\n" \
821 "Fetchmail won't poll this mailbox again until you restart it.\n"));
822 close_warning_by_mail(ctl, (struct msgblk *)NULL);
832 char buf[MSGBUFSIZE+1], *realhost;
833 int count, new, bytes, deletions = 0;
834 int *msgsizes = (int *)NULL;
835 int *msgcodes = (int *)NULL;
837 int fetches, dispatches, oldphase;
838 #else /* INET6_ENABLE */
839 int port, fetches, dispatches, oldphase;
840 #endif /* INET6_ENABLE */
843 /* execute pre-initialization command, if any */
844 if (ctl->preconnect && (err = system(ctl->preconnect)))
847 GT_("pre-connection command failed with status %d\n"), err);
852 /* open a socket to the mail server */
855 set_timeout(mytimeout);
858 port = ctl->server.port ? ctl->server.port : ( ctl->use_ssl ? ctl->server.base_protocol->sslport : ctl->server.base_protocol->port );
860 port = ctl->server.port ? ctl->server.port : ctl->server.base_protocol->port;
862 #endif /* !INET6_ENABLE */
865 /* If either the pollname or vianame are "hesiod" we want to
866 lookup the user's hesiod pobox host */
867 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
868 struct hes_postoffice *hes_p;
869 hes_p = hes_getmailhost(ctl->remotename);
870 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
871 free(ctl->server.queryname);
872 ctl->server.queryname = xstrdup(hes_p->po_host);
874 free(ctl->server.via);
875 ctl->server.via = xstrdup(hes_p->po_host);
878 GT_("couldn't find HESIOD pobox for %s\n"),
884 #ifdef HAVE_GETHOSTBYNAME
886 * Canonicalize the server truename for later use. This also
887 * functions as a probe for whether the mailserver is accessible.
888 * We try it on each poll cycle until we get a result. This way,
889 * fetchmail won't fail if started up when the network is inaccessible.
891 if (ctl->server.dns && !ctl->server.trueaddr)
893 if (ctl->server.lead_server)
895 char *leadname = ctl->server.lead_server->truename;
897 /* prevent core dump from ill-formed or duplicate entry */
900 report(stderr, GT_("Lead server has no name.\n"));
907 ctl->server.truename = xstrdup(leadname);
911 struct hostent *namerec;
914 * Get the host's IP, so we can report it like this:
916 * Received: from hostname [10.0.0.1]
919 namerec = gethostbyname(ctl->server.queryname);
920 if (namerec == (struct hostent *)NULL)
923 GT_("couldn't find canonical DNS name of %s (%s)\n"),
924 ctl->server.pollname, ctl->server.queryname);
932 ctl->server.truename=xstrdup((char *)namerec->h_name);
933 ctl->server.trueaddr=xmalloc(namerec->h_length);
934 memcpy(ctl->server.trueaddr,
935 namerec->h_addr_list[0],
940 #endif /* HAVE_GETHOSTBYNAME */
942 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
944 /* allow time for the port to be set up if we have a plugin */
945 if (ctl->server.plugin)
948 if ((mailserver_socket = SockOpen(realhost,
949 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
950 ctl->server.netsec, ctl->server.plugin)) == -1)
951 #else /* INET6_ENABLE */
952 if ((mailserver_socket = SockOpen(realhost, port, NULL, ctl->server.plugin)) == -1)
953 #endif /* INET6_ENABLE */
958 #ifdef HAVE_RES_SEARCH
959 if (err_no != 0 && h_errno != 0)
960 report(stderr, GT_("internal inconsistency\n"));
963 * Avoid generating a bogus error every poll cycle when we're
964 * in daemon mode but the connection to the outside world
967 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
968 && run.poll_interval))
970 report_build(stderr, GT_("%s connection to %s failed"),
971 ctl->server.base_protocol->name, ctl->server.pollname);
972 #ifdef HAVE_RES_SEARCH
975 if (h_errno == HOST_NOT_FOUND)
976 strcpy(errbuf, GT_("host is unknown."));
978 else if (h_errno == NO_ADDRESS)
979 strcpy(errbuf, GT_("name is valid but has no IP address."));
981 else if (h_errno == NO_RECOVERY)
982 strcpy(errbuf, GT_("unrecoverable name server error."));
983 else if (h_errno == TRY_AGAIN)
984 strcpy(errbuf, GT_("temporary name server error."));
987 snprintf(errbuf, sizeof(errbuf),
990 #endif /* HAVE_SNPRINTF */
991 GT_("unknown DNS error %d."), h_errno);
994 #endif /* HAVE_RES_SEARCH */
995 strcpy(errbuf, strerror(err_no));
996 report_complete(stderr, ": %s\n", errbuf);
1000 * Don't use this. It was an attempt to address Debian bug
1001 * #47143 (Notify user by mail when pop server nonexistent).
1002 * Trouble is, that doesn't work; you trip over the case
1003 * where your SLIP or PPP link is down...
1005 /* warn the system administrator */
1006 if (open_warning_by_mail(ctl, (struct msgblk *)NULL) == 0)
1009 GT_("Subject: Fetchmail unreachable-server warning.\n"
1011 "Fetchmail could not reach the mail server %s:")
1012 ctl->server.pollname);
1013 stuff_warning(ctl, errbuf, ctl->server.pollname);
1014 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1018 #endif /* INET6_ENABLE */
1026 /* Save the socket opened. Usefull if Fetchmail hangs on SSLOpen
1027 * because the socket can be closed
1029 mailserver_socket_temp = mailserver_socket;
1030 set_timeout(mytimeout);
1032 /* perform initial SSL handshake on open connection */
1033 /* Note: We pass the realhost name over for certificate
1034 verification. We may want to make this configurable */
1035 if (ctl->use_ssl && SSLOpen(mailserver_socket,ctl->sslcert,ctl->sslkey,ctl->sslproto,ctl->sslcertck,
1036 ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
1038 report(stderr, GT_("SSL connection failed.\n"));
1042 /* Fetchmail didn't hang on SSLOpen,
1043 * then no need to set mailserver_socket_temp
1045 mailserver_socket_temp = -1;
1048 /* A timeout is still defined before SSLOpen,
1049 * then Fetchmail hanging on SSLOpen is handled.
1054 if (ctl->server.authenticate == A_KERBEROS_V4 && (strcasecmp(proto->name,"IMAP") != 0))
1056 set_timeout(mytimeout);
1057 err = kerberos_auth(mailserver_socket, ctl->server.truename,
1058 ctl->server.principal);
1063 #endif /* KERBEROS_V4 */
1066 if (ctl->server.authenticate == A_KERBEROS_V5)
1068 set_timeout(mytimeout);
1069 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1074 #endif /* KERBEROS_V5 */
1076 /* accept greeting message from mail server */
1077 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1081 /* try to get authorized to fetch mail */
1082 stage = STAGE_GETAUTH;
1083 if (ctl->server.base_protocol->getauth)
1085 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1089 if (err == PS_LOCKBUSY)
1090 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1092 ctl->server.truename);
1093 else if (err == PS_SERVBUSY)
1094 report(stderr, GT_("Server busy error on %s@%s\n"),
1096 ctl->server.truename);
1097 else if (err == PS_AUTHFAIL)
1099 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1101 ctl->server.truename,
1102 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1106 * If we're running in background, try to mail the
1107 * calling user a heads-up about the authentication
1108 * failure once it looks like this isn't a fluke
1109 * due to the server being temporarily inaccessible.
1110 * When we get third succesive failure, we notify the user
1111 * but only if we haven't already managed to get
1112 * authorization. After that, once we get authorization
1113 * we let the user know service is restored.
1115 if (run.poll_interval
1116 && !ctl->wehavesentauthnote
1117 && ((ctl->wehaveauthed && ++ctl->authfailcount >= 10)
1118 || (!ctl->wehaveauthed && ++ctl->authfailcount >= 3))
1119 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
1121 ctl->wehavesentauthnote = 1;
1123 GT_("Subject: fetchmail authentication failed on %s@%s\n"),
1124 ctl->remotename, ctl->server.truename);
1126 GT_("Fetchmail could not get mail from %s@%s.\n"),
1128 ctl->server.truename);
1129 if (ctl->wehaveauthed)
1130 stuff_warning(ctl, GT_("\
1131 The attempt to get authorization failed.\n\
1132 Since we have already succeeded in getting authorization for this\n\
1133 connection, this is probably another failure mode (such as busy server)\n\
1134 that fetchmail cannot distinguish because the server didn't send a useful\n\
1137 However, if you HAVE changed your account details since starting the\n\
1138 fetchmail daemon, you need to stop the daemon, change your configuration\n\
1139 of fetchmail, and then restart the daemon.\n\
1141 The fetchmail daemon will continue running and attempt to connect\n\
1142 at each cycle. No future notifications will be sent until service\n\
1145 stuff_warning(ctl, GT_("\
1146 The attempt to get authorization failed.\n\
1147 This probably means your password is invalid, but some servers have\n\
1148 other failure modes that fetchmail cannot distinguish from this\n\
1149 because they don't send useful error messages on login failure.\n\
1151 The fetchmail daemon will continue running and attempt to connect\n\
1152 at each cycle. No future notifications will be sent until service\n\
1154 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1157 else if (err == PS_REPOLL)
1159 if (outlevel >= O_VERBOSE)
1160 report(stderr, GT_("Repoll immediately on %s@%s\n"),
1162 ctl->server.truename);
1165 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1167 ctl->server.truename);
1174 * This connection has given us authorization at least once.
1176 * There are dodgy server (clubinternet.fr for example) that
1177 * give spurious authorization failures on patently good
1178 * account/password details, then 5 minutes later let you in!
1180 * This is meant to build in some tolerance of such nasty bits
1183 ctl->wehaveauthed = 1;
1184 /*if (ctl->authfailcount >= 3)*/
1185 if (ctl->wehavesentauthnote)
1187 ctl->wehavesentauthnote = 0;
1189 GT_("Authorization OK on %s@%s\n"),
1191 ctl->server.truename);
1192 if (!open_warning_by_mail(ctl, (struct msgblk *)NULL))
1195 GT_("Subject: fetchmail authentication OK on %s@%s\n"),
1196 ctl->remotename, ctl->server.truename);
1198 GT_("Fetchmail was able to log into %s@%s.\n"),
1200 ctl->server.truename);
1202 GT_("Service has been restored.\n"));
1203 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1208 * Reporting only after the first three
1209 * consecutive failures, or ten consecutive
1210 * failures after we have managed to get
1213 ctl->authfailcount = 0;
1217 ctl->errcount = fetches = 0;
1219 /* now iterate over each folder selected */
1220 for (idp = ctl->mailboxes; idp; idp = idp->next)
1227 /* reset timeout, in case we did an IDLE */
1228 mytimeout = ctl->server.timeout;
1230 if (outlevel >= O_DEBUG)
1233 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1235 report(stdout, GT_("selecting or re-polling default folder\n"));
1238 /* compute # of messages and number of new messages waiting */
1239 stage = STAGE_GETRANGE;
1240 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &new, &bytes);
1244 /* show user how many messages we downloaded */
1246 #ifdef HAVE_SNPRINTF
1247 (void) snprintf(buf, sizeof(buf),
1250 #endif /* HAVE_SNPRINTF */
1251 GT_("%s at %s (folder %s)"),
1252 ctl->remotename, ctl->server.truename, idp->id);
1254 #ifdef HAVE_SNPRINTF
1255 (void) snprintf(buf, sizeof(buf),
1258 #endif /* HAVE_SNPRINTF */
1260 ctl->remotename, ctl->server.truename);
1261 if (outlevel > O_SILENT)
1263 if (count == -1) /* only used for ETRN */
1264 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1265 else if (count != 0)
1267 if (new != -1 && (count - new) > 0)
1268 report_build(stdout, GT_("%d %s (%d seen) for %s"),
1269 count, count > 1 ? GT_("messages") :
1273 report_build(stdout, GT_("%d %s for %s"),
1274 count, count > 1 ? GT_("messages") :
1275 GT_("message"), buf);
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 (new == -1 || ctl->fetchall)
1297 fetches = new; /* 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...
1307 flag force_retrieval;
1311 * What forces this code is that in POP2 and
1312 * IMAP2bis you can't fetch a message without
1313 * having it marked `seen'. In POP3 and IMAP4, on the
1314 * other hand, you can (peek_capable is set by
1315 * each driver module to convey this; it's not a
1316 * method constant because of the difference between
1317 * IMAP2bis and IMAP4, and because POP3 doesn't peek
1318 * if fetchall is on).
1320 * The result of being unable to peek is that if there's
1321 * any kind of transient error (DNS lookup failure, or
1322 * sendmail refusing delivery due to process-table limits)
1323 * the message will be marked "seen" on the server without
1324 * having been delivered. This is not a big problem if
1325 * fetchmail is running in foreground, because the user
1326 * will see a "skipped" message when it next runs and get
1329 * But in daemon mode this leads to the message
1330 * being silently ignored forever. This is not
1333 * We compensate for this by checking the error
1334 * count from the previous pass and forcing all
1335 * messages to be considered new if it's nonzero.
1337 force_retrieval = !peek_capable && (ctl->errcount > 0);
1340 * Don't trust the message count passed by the server.
1341 * Without this check, it might be possible to do a
1342 * DNS-spoofing attack that would pass back a ridiculous
1343 * count, and allocate a malloc area that would overlap
1344 * a portion of the stack.
1346 if (count > INT_MAX/sizeof(int))
1348 report(stderr, GT_("bogus message count!"));
1349 return(PS_PROTOCOL);
1352 /* OK, we're going to gather size info next */
1353 xalloca(msgsizes, int *, sizeof(int) * count);
1354 xalloca(msgcodes, int *, sizeof(int) * count);
1355 for (i = 0; i < count; i++) {
1357 msgcodes[i] = MSGLEN_UNKNOWN;
1361 * We need the size of each message before it's
1362 * loaded in order to pass it to the ESMTP SIZE
1363 * option. If the protocol has a getsizes method,
1364 * we presume this means it doesn't get reliable
1365 * sizes from message fetch responses.
1367 if (proto->getsizes)
1369 stage = STAGE_GETSIZES;
1370 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1377 for (i = 0; i < count; i++)
1378 bytes += msgsizes[i];
1382 /* mark some messages not to be retrieved */
1383 for (num = 1; num <= count; num++)
1385 if (NUM_NONZERO(ctl->limit) && (msgsizes[num-1] > ctl->limit))
1386 msgcodes[num-1] = MSGLEN_TOOLARGE;
1387 else if (ctl->fetchall || force_retrieval)
1389 else if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num))
1390 msgcodes[num-1] = MSGLEN_OLD;
1391 /* else if (msgsizes[num-1] == 512)
1392 msgcodes[num-1] = MSGLEN_OLD; (hmh) sample code to skip message */
1395 /* read, forward, and delete messages */
1396 stage = STAGE_FETCH;
1398 /* fetch in lockstep mode */
1399 err = fetch_messages(mailserver_socket, ctl,
1400 count, msgsizes, msgcodes,
1402 &fetches, &dispatches, &deletions);
1406 if (!check_only && ctl->skipped
1407 && run.poll_interval > 0 && !nodetach)
1409 clean_skipped_list(&ctl->skipped);
1410 send_size_warnings(ctl);
1415 * Only re-poll if we either had some actual forwards and
1416 * either allowed deletions and had no errors.
1417 * Otherwise it is far too easy to get into infinite loops.
1419 (dispatches && ctl->server.base_protocol->retry && !ctl->keep && !ctl->errcount);
1423 /* ordinary termination with no errors -- officially log out */
1424 err = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1426 * Hmmmm...arguably this would be incorrect if we had fetches but
1427 * no dispatches (due to oversized messages, etc.)
1430 err = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1431 cleanupSockClose(mailserver_socket);
1435 /* we only get here on error */
1436 if (err != 0 && err != PS_SOCKET && err != PS_REPOLL)
1438 stage = STAGE_LOGOUT;
1439 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1442 /* try to clean up all streams */
1445 if (mailserver_socket != -1) {
1446 cleanupSockClose(mailserver_socket);
1447 mailserver_socket = -1;
1449 /* If there was a connect timeout, the socket should be closed.
1450 * mailserver_socket_temp contains the socket to close.
1452 if (mailserver_socket_temp != -1) {
1453 cleanupSockClose(mailserver_socket_temp);
1454 mailserver_socket_temp = -1;
1458 msg = (const char *)NULL; /* sacrifice to -Wall */
1462 msg = GT_("socket");
1465 msg = GT_("missing or bad RFC822 header");
1471 msg = GT_("client/server synchronization");
1474 msg = GT_("client/server protocol");
1477 msg = GT_("lock busy on server");
1480 msg = GT_("SMTP transaction");
1483 msg = GT_("DNS lookup");
1486 report(stderr, GT_("undefined error\n"));
1489 /* no report on PS_MAXFETCH or PS_UNDEFINED or PS_AUTHFAIL */
1490 if (err==PS_SOCKET || err==PS_SYNTAX
1491 || err==PS_IOERR || err==PS_ERROR || err==PS_PROTOCOL
1492 || err==PS_LOCKBUSY || err==PS_SMTP || err==PS_DNS)
1496 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT)
1497 stem = GT_("%s error while delivering to SMTP host %s\n");
1499 stem = GT_("%s error while fetching from %s\n");
1500 report(stderr, stem, msg, ctl->server.pollname);
1504 /* execute wrapup command, if any */
1505 if (ctl->postconnect && (err = system(ctl->postconnect)))
1507 report(stderr, GT_("post-connection command failed with status %d\n"), err);
1508 if (err == PS_SUCCESS)
1512 set_timeout(0); /* cancel any pending alarm */
1513 set_signal_handler(SIGALRM, alrmsave);
1514 set_signal_handler(SIGPIPE, pipesave);
1518 int do_protocol(ctl, proto)
1519 /* retrieve messages from server using given protocol method table */
1520 struct query *ctl; /* parsed options with merged-in defaults */
1521 const struct method *proto; /* protocol method table */
1526 if (ctl->server.authenticate == A_KERBEROS_V4)
1528 report(stderr, GT_("Kerberos V4 support not linked.\n"));
1531 #endif /* KERBEROS_V4 */
1534 if (ctl->server.authenticate == A_KERBEROS_V5)
1536 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1539 #endif /* KERBEROS_V5 */
1541 /* lacking methods, there are some options that may fail */
1544 /* check for unsupported options */
1547 GT_("Option --flush is not supported with %s\n"),
1551 else if (ctl->fetchall) {
1553 GT_("Option --all is not supported with %s\n"),
1558 if (!proto->getsizes && NUM_SPECIFIED(ctl->limit))
1561 GT_("Option --limit is not supported with %s\n"),
1567 * If no expunge limit or we do expunges within the driver,
1568 * then just do one session, passing in any fetchlimit.
1570 if (proto->retry || !NUM_SPECIFIED(ctl->expunge))
1571 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1573 * There's an expunge limit, and it isn't handled in the driver itself.
1574 * OK; do multiple sessions, each fetching a limited # of messages.
1575 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1576 * (if it was nonzero).
1582 int expunge = NUM_VALUE_OUT(ctl->expunge);
1583 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1586 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1587 expunge = fetchlimit - totalcount;
1588 err = do_session(ctl, proto, expunge);
1589 totalcount += expunge;
1590 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1592 if (err != PS_LOCKBUSY)
1594 else if (lockouts >= MAX_LOCKOUTS)
1596 else /* err == PS_LOCKBUSY */
1599 * Allow time for the server lock to release. if we
1600 * don't do this, we'll often hit a locked-mailbox
1601 * condition and fail.
1607 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1614 /* driver.c ends here */