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)
19 #if defined(HAVE_UNISTD_H)
22 #if defined(HAVE_SYS_ITIMER_H)
23 #include <sys/itimer.h>
28 #ifdef HAVE_NET_SOCKET_H
29 #include <net/socket.h>
35 #if defined(HAVE_RES_SEARCH) || defined(HAVE_GETHOSTBYNAME)
38 #endif /* defined(HAVE_RES_SEARCH) || defined(HAVE_GETHOSTBYNAME) */
42 #include <netinet/in.h>
43 #endif /* KERBEROS_V4 */
48 #include "fetchmail.h"
51 /* throw types for runtime errors */
52 #define THROW_TIMEOUT 1 /* server timed out */
53 #define THROW_SIGPIPE 2 /* SIGPIPE on stream socket */
55 /* magic values for the message length array */
56 #define MSGLEN_UNKNOWN 0 /* length unknown (0 is impossible) */
57 #define MSGLEN_INVALID -1 /* length passed back is invalid */
58 #define MSGLEN_TOOLARGE -2 /* message is too large */
59 #define MSGLEN_OLD -3 /* message is old */
61 int pass; /* how many times have we re-polled? */
62 int stage; /* where are we? */
63 int phase; /* where are we, for error-logging purposes? */
64 int batchcount; /* count of messages sent in current batch */
65 flag peek_capable; /* can we peek for better error recovery? */
67 static int timeoutcount; /* count consecutive timeouts */
69 static jmp_buf restart;
71 void set_timeout(int timeleft)
72 /* reset the nonresponse-timeout */
74 #if !defined(__EMX__) && !defined(__BEOS__)
75 struct itimerval ntimeout;
80 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
81 ntimeout.it_value.tv_sec = timeleft;
82 ntimeout.it_value.tv_usec = 0;
83 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
87 static void timeout_handler (int signal)
88 /* handle SIGALRM signal indicating a server timeout */
91 longjmp(restart, THROW_TIMEOUT);
94 static void sigpipe_handler (int signal)
95 /* handle SIGPIPE signal indicating a broken stream socket */
97 longjmp(restart, THROW_SIGPIPE);
100 /* ignore SIGALRM signal indicating a timeout during cleanup */
101 static void cleanup_timeout_handler (int signal) { }
103 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
105 static int cleanupSockClose (int fd)
106 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
108 void (*alrmsave)(int);
109 alrmsave = signal(SIGALRM, cleanup_timeout_handler);
110 set_timeout(CLEANUP_TIMEOUT);
113 signal(SIGALRM, alrmsave);
117 static int kerberos_auth(socket, canonical, principal)
118 /* authenticate to the server host using Kerberos V4 */
119 int socket; /* socket to server host */
120 char *canonical; /* server name */
127 Key_schedule schedule;
129 char * prin_copy = (char *) NULL;
130 char * prin = (char *) NULL;
131 char * inst = (char *) NULL;
132 char * realm = (char *) NULL;
134 if (principal != (char *)NULL && *principal)
137 prin = prin_copy = xstrdup(principal);
138 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
144 while (*cp && *cp != '@')
154 xalloca(ticket, KTEXT, sizeof (KTEXT_ST));
155 rem = (krb_sendauth (0L, socket, ticket,
157 inst ? inst : canonical,
158 realm ? realm : ((char *) (krb_realmofhost (canonical))),
163 ((struct sockaddr_in *) 0),
164 ((struct sockaddr_in *) 0),
172 report(stderr, GT_("kerberos error %s\n"), (krb_get_err_text (rem)));
173 return (PS_AUTHFAIL);
177 #endif /* KERBEROS_V4 */
180 static int kerberos5_auth(socket, canonical)
181 /* authenticate to the server host using Kerberos V5 */
182 int socket; /* socket to server host */
183 const char *canonical; /* server name */
185 krb5_error_code retval;
186 krb5_context context;
188 krb5_principal client = NULL, server = NULL;
189 krb5_error *err_ret = NULL;
191 krb5_auth_context auth_context = NULL;
193 krb5_init_context(&context);
194 krb5_init_ets(context);
195 krb5_auth_con_init(context, &auth_context);
197 if (retval = krb5_cc_default(context, &ccdef)) {
198 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
202 if (retval = krb5_cc_get_principal(context, ccdef, &client)) {
203 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
207 if (retval = krb5_sname_to_principal(context, canonical, "pop",
210 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
214 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
215 "KPOPV1.0", client, server,
216 AP_OPTS_MUTUAL_REQUIRED,
217 NULL, /* no data to checksum */
218 0, /* no creds, use ccache instead */
222 NULL); /* don't need reply */
224 krb5_free_principal(context, server);
225 krb5_free_principal(context, client);
226 krb5_auth_con_free(context, auth_context);
230 if (err_ret && err_ret->e_text) {
231 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
232 error_message(retval),
235 if (err_ret && err_ret->text.length) {
236 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
237 error_message(retval),
238 err_ret->text.length,
241 krb5_free_error(context, err_ret);
243 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
249 #endif /* KERBEROS_V5 */
251 static void clean_skipped_list(struct idlist **skipped_list)
252 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
254 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
255 prev = current = head = *skipped_list;
261 /* if item has no reference, remove it */
262 if (current && current->val.status.mark == 0)
264 if (current == head) /* remove first item (head) */
266 head = current->next;
267 if (current->id) free(current->id);
269 prev = current = head;
271 else /* remove middle/last item */
275 if (current->id) free(current->id);
280 else /* skip this item */
283 current = current->next;
287 *skipped_list = head;
290 static void send_size_warnings(struct query *ctl)
291 /* send warning mail with skipped msg; reset msg count when user notified */
294 int msg_to_send = FALSE;
295 struct idlist *head=NULL, *current=NULL;
296 int max_warning_poll_count;
302 /* don't start a notification message unless we need to */
303 for (current = head; current; current = current->next)
304 if (current->val.status.num == 0 && current->val.status.mark)
310 * There's no good way to recover if we can't send notification mail,
311 * but it's not a disaster, either, since the skipped mail will not
314 if (open_warning_by_mail(ctl, (struct msgblk *)NULL))
317 GT_("Subject: Fetchmail oversized-messages warning.\r\n"
319 "The following oversized messages remain on the mail server %s:"),
320 ctl->server.pollname);
322 if (run.poll_interval == 0)
323 max_warning_poll_count = 0;
325 max_warning_poll_count = ctl->warnings/run.poll_interval;
327 /* parse list of skipped msg, adding items to the mail */
328 for (current = head; current; current = current->next)
330 if (current->val.status.num == 0 && current->val.status.mark)
332 nbr = current->val.status.mark;
333 size = atoi(current->id);
335 GT_("\t%d msg %d octets long skipped by fetchmail.\r\n"),
338 current->val.status.num++;
339 current->val.status.mark = 0;
341 if (current->val.status.num >= max_warning_poll_count)
342 current->val.status.num = 0;
345 close_warning_by_mail(ctl, (struct msgblk *)NULL);
348 static void mark_oversized(struct query *ctl, int num, int size)
349 /* mark a message oversized */
351 struct idlist *current=NULL, *tmp=NULL;
355 /* convert size to string */
357 snprintf(sizestr, sizeof(sizestr),
360 #endif /* HAVE_SNPRINTF */
363 /* build a list of skipped messages
364 * val.id = size of msg (string cnvt)
365 * val.status.num = warning_poll_count
366 * val.status.mask = nbr of msg this size
369 current = ctl->skipped;
371 /* initialise warning_poll_count to the
372 * current value so that all new msg will
373 * be included in the next mail
375 cnt = current ? current->val.status.num : 0;
377 /* if entry exists, increment the count */
378 if (current && str_in_list(¤t, sizestr, FALSE))
380 for ( ; current; current = current->next)
382 if (strcmp(current->id, sizestr) == 0)
384 current->val.status.mark++;
389 /* otherwise, create a new entry */
390 /* initialise with current poll count */
393 tmp = save_str(&ctl->skipped, sizestr, 1);
394 tmp->val.status.num = cnt;
398 static int fetch_messages(int mailserver_socket, struct query *ctl,
399 int count, int *msgsizes, int *msgcodes, int maxfetch,
400 int *fetches, int *dispatches, int *deletions)
401 /* fetch messages in lockstep mode */
405 for (num = 1; num <= count; num++)
407 flag suppress_delete = FALSE;
408 flag suppress_forward = FALSE;
409 flag suppress_readbody = FALSE;
410 flag retained = FALSE;
412 if (msgcodes[num-1] < 0)
414 if ((msgcodes[num-1] == MSGLEN_TOOLARGE) && !check_only)
415 mark_oversized(ctl, num, msgsizes[num-1]);
416 if (outlevel > O_SILENT)
419 GT_("skipping message %s@%s:%d (%d octets)"),
420 ctl->remotename, ctl->server.truename, num,
422 switch (msgcodes[num-1])
426 * Invalid lengths are produced by Post Office/NT's
427 * annoying habit of randomly prepending bogus
428 * LIST items of length -1. Patrick Audley
429 * <paudley@pobox.com> tells us: LIST shows a
430 * size of -1, RETR and TOP return "-ERR
431 * System error - couldn't open message", and
432 * DELE succeeds but doesn't actually delete
435 report_build(stdout, GT_(" (length -1)"));
437 case MSGLEN_TOOLARGE:
439 GT_(" (oversized, %d octets)"),
447 flag wholesize = !ctl->server.base_protocol->fetch_body;
449 /* request a message */
450 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
451 if (err == PS_TRANSIENT) /* server is probably Exchange */
454 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)"),
455 ctl->remotename, ctl->server.truename, num,
462 /* -1 means we didn't see a size in the response */
465 len = msgsizes[num - 1];
469 if (outlevel > O_SILENT)
471 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
472 ctl->remotename, ctl->server.truename,
476 report_build(stdout, GT_(" (%d %soctets)"),
477 len, wholesize ? "" : GT_("header "));
478 if (outlevel >= O_VERBOSE)
479 report_complete(stdout, "\n");
481 report_complete(stdout, " ");
485 * Read the message headers and ship them to the
488 err = readheaders(mailserver_socket, len, msgsizes[num-1],
490 if (err == PS_RETAINED)
491 suppress_forward = retained = TRUE;
492 else if (err == PS_TRANSIENT)
493 suppress_delete = suppress_forward = TRUE;
494 else if (err == PS_REFUSED)
495 suppress_forward = TRUE;
496 else if (err == PS_TRUNCATED)
497 suppress_readbody = TRUE;
502 * If we're using IMAP4 or something else that
503 * can fetch headers separately from bodies,
504 * it's time to request the body now. This
505 * fetch may be skipped if we got an anti-spam
506 * or other PS_REFUSED error response during
509 if (ctl->server.base_protocol->fetch_body && !suppress_readbody)
511 if (outlevel >= O_VERBOSE && !isafile(1))
517 if ((err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num)))
520 if (!suppress_forward)
522 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len)))
525 * Work around a bug in Novell's
526 * broken GroupWise IMAP server;
527 * its body FETCH response is missing
528 * the required length for the data
529 * string. This violates RFC2060.
532 len = msgsizes[num-1] - msgblk.msglen;
533 if (outlevel > O_SILENT && !wholesize)
534 report_complete(stdout,
535 GT_(" (%d body octets) "), len);
539 /* process the body now */
542 if (suppress_readbody)
544 /* When readheaders returns PS_TRUNCATED,
545 * the body (which has no content)
546 * has already been read by readheaders,
547 * so we say readbody returned PS_SUCCESS
553 err = readbody(mailserver_socket,
558 if (err == PS_TRANSIENT)
559 suppress_delete = suppress_forward = TRUE;
563 /* tell server we got it OK and resynchronize */
564 if (ctl->server.base_protocol->trail)
566 if (outlevel >= O_VERBOSE && !isafile(1))
572 err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num);
578 /* count # messages forwarded on this pass */
579 if (!suppress_forward)
583 * Check to see if the numbers matched?
585 * Yes, some servers foo this up horribly.
586 * All IMAP servers seem to get it right, and
587 * so does Eudora QPOP at least in 2.xx
590 * Microsoft Exchange gets it completely
591 * wrong, reporting compressed rather than
592 * actual sizes (so the actual length of
593 * message is longer than the reported size).
594 * Another fine example of Microsoft brain death!
596 * Some older POP servers, like the old UCB
597 * POP server and the pre-QPOP QUALCOMM
598 * versions, report a longer size in the LIST
599 * response than actually gets shipped up.
600 * It's unclear what is going on here, as the
601 * QUALCOMM server (at least) seems to be
602 * reporting the on-disk size correctly.
604 if (msgblk.msglen != msgsizes[num-1])
606 if (outlevel >= O_DEBUG)
608 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
609 ctl->remotename, ctl->server.truename, num,
610 msgblk.msglen, msgsizes[num-1]);
613 /* end-of-message processing starts here */
614 if (!close_sink(ctl, &msgblk, !suppress_forward))
617 suppress_delete = TRUE;
623 * At this point in flow of control, either
624 * we've bombed on a protocol error or had
625 * delivery refused by the SMTP server
626 * (unlikely -- I've never seen it) or we've
627 * seen `accepted for delivery' and the
628 * message is shipped. It's safe to mark the
629 * message seen and delete it on the server
633 /* tell the UID code we've seen this */
638 for (sdp = ctl->newsaved; sdp; sdp = sdp->next)
639 if ((sdp->val.status.num == num) && (msgcodes[num-1] >= 0))
641 sdp->val.status.mark = UID_SEEN;
642 save_str(&ctl->oldsaved, sdp->id,UID_SEEN);
646 /* maybe we delete this message now? */
649 if (outlevel > O_SILENT)
650 report(stdout, GT_(" retained\n"));
652 else if (ctl->server.base_protocol->delete
654 && ((msgcodes[num-1] >= 0) ? !ctl->keep : ctl->flush))
657 if (outlevel > O_SILENT)
658 report_complete(stdout, GT_(" flushed\n"));
659 err = (ctl->server.base_protocol->delete)(mailserver_socket, ctl, num);
663 delete_str(&ctl->newsaved, num);
664 #endif /* POP3_ENABLE */
666 else if (outlevel > O_SILENT)
667 report_complete(stdout, GT_(" not flushed\n"));
669 /* perhaps this as many as we're ready to handle */
670 if (maxfetch && maxfetch <= *fetches && *fetches < count)
672 report(stdout, GT_("fetchlimit %d reached; %d messages left on server %s account %s\n"),
673 maxfetch, count - *fetches, ctl->server.truename, ctl->remotename);
681 static int do_session(ctl, proto, maxfetch)
682 /* retrieve messages from server using given protocol method table */
683 struct query *ctl; /* parsed options with merged-in defaults */
684 const struct method *proto; /* protocol method table */
685 const int maxfetch; /* maximum number of messages to fetch */
689 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
691 int err, mailserver_socket = -1;
692 #endif /* HAVE_VOLATILE */
694 void (*pipesave)(int);
695 void (*alrmsave)(int);
697 ctl->server.base_protocol = proto;
701 init_transact(proto);
703 /* set up the server-nonresponse timeout */
704 alrmsave = signal(SIGALRM, timeout_handler);
705 mytimeout = ctl->server.timeout;
707 /* set up the broken-pipe timeout */
708 pipesave = signal(SIGPIPE, sigpipe_handler);
710 if ((js = setjmp(restart)))
712 #ifdef HAVE_SIGPROCMASK
714 * Don't rely on setjmp() to restore the blocked-signal mask.
715 * It does this under BSD but is required not to under POSIX.
717 * If your Unix doesn't have sigprocmask, better hope it has
718 * BSD-like behavior. Otherwise you may see fetchmail get
719 * permanently wedged after a second timeout on a bad read,
720 * because alarm signals were blocked after the first.
724 sigfillset(&allsigs);
725 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
726 #endif /* HAVE_SIGPROCMASK */
728 if (js == THROW_SIGPIPE)
730 signal(SIGPIPE, SIG_IGN);
732 GT_("SIGPIPE thrown from an MDA or a stream socket error\n"));
736 else if (js == THROW_TIMEOUT)
738 if (phase == OPEN_WAIT)
740 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
741 ctl->server.timeout, ctl->server.pollname);
742 else if (phase == SERVER_WAIT)
744 GT_("timeout after %d seconds waiting for server %s.\n"),
745 ctl->server.timeout, ctl->server.pollname);
746 else if (phase == FORWARDING_WAIT)
748 GT_("timeout after %d seconds waiting for %s.\n"),
750 ctl->mda ? "MDA" : "SMTP");
751 else if (phase == LISTENER_WAIT)
753 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
756 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
759 * If we've exceeded our threshold for consecutive timeouts,
760 * try to notify the user, then mark the connection wedged.
761 * Don't do this if the connection can idle, though; idle
762 * timeouts just mean the frequency of mail is low.
764 if (timeoutcount > MAX_TIMEOUTS
765 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
768 GT_("Subject: fetchmail sees repeated timeouts\r\n"));
770 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\r\n"),
773 ctl->server.truename);
775 GT_("This could mean that your mailserver is stuck, or that your SMTP\r\n" \
776 "server is wedged, or that your mailbox file on the server has been\r\n" \
777 "corrupted by a server error. You can run `fetchmail -v -v' to\r\n" \
778 "diagnose the problem.\r\n\r\n" \
779 "Fetchmail won't poll this mailbox again until you restart it.\r\n"));
780 close_warning_by_mail(ctl, (struct msgblk *)NULL);
787 /* try to clean up all streams */
789 if (ctl->smtp_socket != -1)
790 cleanupSockClose(ctl->smtp_socket);
791 if (mailserver_socket != -1)
792 cleanupSockClose(mailserver_socket);
796 char buf[MSGBUFSIZE+1], *realhost;
797 int count, new, bytes, deletions = 0;
798 int *msgsizes = (int *)NULL;
799 int *msgcodes = (int *)NULL;
801 int fetches, dispatches, oldphase;
802 #else /* INET6_ENABLE */
803 int port, fetches, dispatches, oldphase;
804 #endif /* INET6_ENABLE */
807 /* execute pre-initialization command, if any */
808 if (ctl->preconnect && (err = system(ctl->preconnect)))
811 GT_("pre-connection command failed with status %d\n"), err);
816 /* open a socket to the mail server */
819 set_timeout(mytimeout);
822 port = ctl->server.port ? ctl->server.port : ( ctl->use_ssl ? ctl->server.base_protocol->sslport : ctl->server.base_protocol->port );
824 port = ctl->server.port ? ctl->server.port : ctl->server.base_protocol->port;
826 #endif /* !INET6_ENABLE */
829 /* If either the pollname or vianame are "hesiod" we want to
830 lookup the user's hesiod pobox host */
831 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
832 struct hes_postoffice *hes_p;
833 hes_p = hes_getmailhost(ctl->remotename);
834 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
835 free(ctl->server.queryname);
836 ctl->server.queryname = xstrdup(hes_p->po_host);
838 free(ctl->server.via);
839 ctl->server.via = xstrdup(hes_p->po_host);
842 GT_("couldn't find HESIOD pobox for %s\n"),
848 #ifdef HAVE_GETHOSTBYNAME
850 * Canonicalize the server truename for later use. This also
851 * functions as a probe for whether the mailserver is accessible.
852 * We try it on each poll cycle until we get a result. This way,
853 * fetchmail won't fail if started up when the network is inaccessible.
855 if (ctl->server.dns && !ctl->server.trueaddr)
857 if (ctl->server.lead_server)
859 char *leadname = ctl->server.lead_server->truename;
861 /* prevent core dump from ill-formed or duplicate entry */
864 report(stderr, GT_("Lead server has no name.\n"));
871 ctl->server.truename = xstrdup(leadname);
875 struct hostent *namerec;
878 * Get the host's IP, so we can report it like this:
880 * Received: from hostname [10.0.0.1]
883 namerec = gethostbyname(ctl->server.queryname);
884 if (namerec == (struct hostent *)NULL)
887 GT_("couldn't find canonical DNS name of %s\n"),
888 ctl->server.pollname);
896 ctl->server.truename=xstrdup((char *)namerec->h_name);
897 ctl->server.trueaddr=xmalloc(namerec->h_length);
898 memcpy(ctl->server.trueaddr,
899 namerec->h_addr_list[0],
904 #endif /* HAVE_GETHOSTBYNAME */
906 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
908 /* allow time for the port to be set up if we have a plugin */
909 if (ctl->server.plugin)
912 if ((mailserver_socket = SockOpen(realhost,
913 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
914 ctl->server.netsec, ctl->server.plugin)) == -1)
915 #else /* INET6_ENABLE */
916 if ((mailserver_socket = SockOpen(realhost, port, NULL, ctl->server.plugin)) == -1)
917 #endif /* INET6_ENABLE */
922 #ifdef HAVE_RES_SEARCH
923 if (err_no != 0 && h_errno != 0)
924 report(stderr, GT_("internal inconsistency\n"));
927 * Avoid generating a bogus error every poll cycle when we're
928 * in daemon mode but the connection to the outside world
931 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
932 && run.poll_interval))
934 report_build(stderr, GT_("%s connection to %s failed"),
935 ctl->server.base_protocol->name, ctl->server.pollname);
936 #ifdef HAVE_RES_SEARCH
939 if (h_errno == HOST_NOT_FOUND)
940 strcpy(errbuf, GT_("host is unknown."));
942 else if (h_errno == NO_ADDRESS)
943 strcpy(errbuf, GT_("name is valid but has no IP address."));
945 else if (h_errno == NO_RECOVERY)
946 strcpy(errbuf, GT_("unrecoverable name server error."));
947 else if (h_errno == TRY_AGAIN)
948 strcpy(errbuf, GT_("temporary name server error."));
951 snprintf(errbuf, sizeof(errbuf),
954 #endif /* HAVE_SNPRINTF */
955 GT_("unknown DNS error %d."), h_errno);
958 #endif /* HAVE_RES_SEARCH */
959 strcpy(errbuf, strerror(err_no));
960 report_complete(stderr, ": %s\n", errbuf);
964 * Don't use this. It was an attempt to address Debian bug
965 * #47143 (Notify user by mail when pop server nonexistent).
966 * Trouble is, that doesn't work; you trip over the case
967 * where your SLIP or PPP link is down...
969 /* warn the system administrator */
970 if (open_warning_by_mail(ctl, (struct msgblk *)NULL) == 0)
973 GT_("Subject: Fetchmail unreachable-server warning.\r\n"
975 "Fetchmail could not reach the mail server %s:")
976 ctl->server.pollname);
977 stuff_warning(ctl, errbuf, ctl->server.pollname);
978 close_warning_by_mail(ctl, (struct msgblk *)NULL);
982 #endif /* INET6_ENABLE */
992 /* perform initial SSL handshake on open connection */
993 /* Note: We pass the realhost name over for certificate
994 verification. We may want to make this configurable */
995 if (ctl->use_ssl && SSLOpen(mailserver_socket,ctl->sslcert,ctl->sslkey,ctl->sslproto,ctl->sslcertck,
996 ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
998 report(stderr, GT_("SSL connection failed.\n"));
1004 if (ctl->server.authenticate == A_KERBEROS_V4)
1006 set_timeout(mytimeout);
1007 err = kerberos_auth(mailserver_socket, ctl->server.truename,
1008 ctl->server.principal);
1013 #endif /* KERBEROS_V4 */
1016 if (ctl->server.authenticate == A_KERBEROS_V5)
1018 set_timeout(mytimeout);
1019 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1024 #endif /* KERBEROS_V5 */
1026 /* accept greeting message from mail server */
1027 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1031 /* try to get authorized to fetch mail */
1032 stage = STAGE_GETAUTH;
1033 if (ctl->server.base_protocol->getauth)
1035 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1039 if (err == PS_LOCKBUSY)
1040 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1042 ctl->server.truename);
1043 else if (err == PS_SERVBUSY)
1044 report(stderr, GT_("Server busy error on %s@%s\n"),
1046 ctl->server.truename);
1047 else if (err == PS_AUTHFAIL)
1049 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1051 ctl->server.truename,
1052 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1056 * If we're running in background, try to mail the
1057 * calling user a heads-up about the authentication
1058 * failure once it looks like this isn't a fluke
1059 * due to the server being temporarily inaccessible.
1060 * When we get third succesive failure, we notify the user
1061 * but only if we haven't already managed to get
1062 * authorization. After that, once we get authorization
1063 * we let the user know service is restored.
1065 if (run.poll_interval
1066 && ctl->wehavesentauthnote
1067 && ((ctl->wehaveauthed && ++ctl->authfailcount == 10)
1068 || ++ctl->authfailcount == 3)
1069 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
1071 ctl->wehavesentauthnote = 1;
1073 GT_("Subject: fetchmail authentication failed on %s@%s\r\n"),
1074 ctl->remotename, ctl->server.truename);
1076 GT_("Fetchmail could not get mail from %s@%s.\r\n"),
1078 ctl->server.truename);
1079 if (ctl->wehaveauthed)
1080 stuff_warning(ctl, GT_("\
1081 The attempt to get authorization failed.\r\n\
1082 Since we have already succeeded in getting authorization for this\r\n\
1083 connection, this is probably another failure mode (such as busy server)\r\n\
1084 that fetchmail cannot distinguish because the server didn't send a useful\r\n\
1087 However, if you HAVE changed you account details since starting the\r\n\
1088 fetchmail daemon, you need to stop the daemon, change your configuration\r\n\
1089 of fetchmail, and then restart the daemon.\r\n\
1091 The fetchmail daemon will continue running and attempt to connect\r\n\
1092 at each cycle. No future notifications will be sent until service\r\n\
1095 stuff_warning(ctl, GT_("\
1096 The attempt to get authorization failed.\r\n\
1097 This probably means your password is invalid, but some servers have\r\n\
1098 other failure modes that fetchmail cannot distinguish from this\r\n\
1099 because they don't send useful error messages on login failure.\r\n\
1101 The fetchmail daemon will continue running and attempt to connect\r\n\
1102 at each cycle. No future notifications will be sent until service\r\n\
1104 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1108 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1110 ctl->server.truename);
1117 * This connection has given us authorization at least once.
1119 * There are dodgy server (clubinternet.fr for example) that
1120 * give spurious authorization failures on patently good
1121 * account/password details, then 5 minutes later let you in!
1123 * This is meant to build in some tolerance of such nasty bits
1126 ctl->wehaveauthed = 1;
1127 /*if (ctl->authfailcount >= 3)*/
1128 if (ctl->wehavesentauthnote)
1130 ctl->wehavesentauthnote = 0;
1132 GT_("Authorization OK on %s@%s\n"),
1134 ctl->server.truename);
1135 if (!open_warning_by_mail(ctl, (struct msgblk *)NULL))
1138 GT_("Subject: fetchmail authentication OK on %s@%s\r\n"),
1139 ctl->remotename, ctl->server.truename);
1141 GT_("Fetchmail was able to log into %s@%s.\r\n"),
1143 ctl->server.truename);
1145 GT_("Service has been restored.\r\n"));
1146 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1151 * Reporting only after the first three
1152 * consecutive failures, or ten consecutive
1153 * failures after we have managed to get
1156 ctl->authfailcount = 0;
1160 ctl->errcount = fetches = 0;
1162 /* now iterate over each folder selected */
1163 for (idp = ctl->mailboxes; idp; idp = idp->next)
1170 /* reset timeout, in case we did an IDLE */
1171 mytimeout = ctl->server.timeout;
1173 if (outlevel >= O_DEBUG)
1176 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1178 report(stdout, GT_("selecting or re-polling default folder\n"));
1181 /* compute # of messages and number of new messages waiting */
1182 stage = STAGE_GETRANGE;
1183 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &new, &bytes);
1187 /* show user how many messages we downloaded */
1189 #ifdef HAVE_SNPRINTF
1190 (void) snprintf(buf, sizeof(buf),
1193 #endif /* HAVE_SNPRINTF */
1194 GT_("%s at %s (folder %s)"),
1195 ctl->remotename, ctl->server.truename, idp->id);
1197 #ifdef HAVE_SNPRINTF
1198 (void) snprintf(buf, sizeof(buf),
1201 #endif /* HAVE_SNPRINTF */
1203 ctl->remotename, ctl->server.truename);
1204 if (outlevel > O_SILENT)
1206 if (count == -1) /* only used for ETRN */
1207 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1208 else if (count != 0)
1210 if (new != -1 && (count - new) > 0)
1211 report_build(stdout, GT_("%d %s (%d seen) for %s"),
1212 count, count > 1 ? GT_("messages") :
1216 report_build(stdout, GT_("%d %s for %s"),
1217 count, count > 1 ? GT_("messages") :
1218 GT_("message"), buf);
1220 report_complete(stdout, ".\n");
1222 report_complete(stdout, GT_(" (%d octets).\n"), bytes);
1226 /* these are pointless in normal daemon mode */
1227 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1228 report(stdout, GT_("No mail for %s\n"), buf);
1232 /* very important, this is where we leave the do loop */
1238 if (new == -1 || ctl->fetchall)
1240 fetches = new; /* set error status correctly */
1242 * There used to be a `goto noerror' here, but this
1243 * prevented checking of multiple folders. This
1244 * comment is a reminder in case I introduced some
1245 * subtle bug by removing it...
1250 flag force_retrieval;
1254 * What forces this code is that in POP2 and
1255 * IMAP2bis you can't fetch a message without
1256 * having it marked `seen'. In POP3 and IMAP4, on the
1257 * other hand, you can (peek_capable is set by
1258 * each driver module to convey this; it's not a
1259 * method constant because of the difference between
1260 * IMAP2bis and IMAP4, and because POP3 doesn't peek
1261 * if fetchall is on).
1263 * The result of being unable to peek is that if there's
1264 * any kind of transient error (DNS lookup failure, or
1265 * sendmail refusing delivery due to process-table limits)
1266 * the message will be marked "seen" on the server without
1267 * having been delivered. This is not a big problem if
1268 * fetchmail is running in foreground, because the user
1269 * will see a "skipped" message when it next runs and get
1272 * But in daemon mode this leads to the message
1273 * being silently ignored forever. This is not
1276 * We compensate for this by checking the error
1277 * count from the previous pass and forcing all
1278 * messages to be considered new if it's nonzero.
1280 force_retrieval = !peek_capable && (ctl->errcount > 0);
1282 /* OK, we're going to gather size info next */
1283 xalloca(msgsizes, int *, sizeof(int) * count);
1284 xalloca(msgcodes, int *, sizeof(int) * count);
1285 for (i = 0; i < count; i++)
1286 msgcodes[i] = MSGLEN_UNKNOWN;
1289 * We need the size of each message before it's
1290 * loaded in order to pass it to the ESMTP SIZE
1291 * option. If the protocol has a getsizes method,
1292 * we presume this means it doesn't get reliable
1293 * sizes from message fetch responses.
1295 if (proto->getsizes)
1297 stage = STAGE_GETSIZES;
1298 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1305 for (i = 0; i < count; i++)
1306 bytes += msgsizes[i];
1310 /* mark some messages not to be retrieved */
1311 for (num = 1; num <= count; num++)
1313 if (NUM_NONZERO(ctl->limit) && (msgsizes[num-1] > ctl->limit))
1314 msgcodes[num-1] = MSGLEN_TOOLARGE;
1315 else if (ctl->fetchall || force_retrieval)
1317 else if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num))
1318 msgcodes[num-1] = MSGLEN_OLD;
1321 /* read, forward, and delete messages */
1322 stage = STAGE_FETCH;
1324 /* fetch in lockstep mode */
1325 err = fetch_messages(mailserver_socket, ctl,
1326 count, msgsizes, msgcodes,
1328 &fetches, &dispatches, &deletions);
1332 if (!check_only && ctl->skipped
1333 && run.poll_interval > 0 && !nodetach)
1335 clean_skipped_list(&ctl->skipped);
1336 send_size_warnings(ctl);
1341 * Only re-poll if we either had some actual forwards and
1342 * either allowed deletions and had no errors.
1343 * Otherwise it is far too easy to get into infinite loops.
1345 (dispatches && ctl->server.base_protocol->retry && !ctl->keep && !ctl->errcount);
1349 /* ordinary termination with no errors -- officially log out */
1350 err = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1352 * Hmmmm...arguably this would be incorrect if we had fetches but
1353 * no dispatches (due to oversized messages, etc.)
1356 err = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1357 cleanupSockClose(mailserver_socket);
1361 /* we only get here on error */
1362 if (err != 0 && err != PS_SOCKET)
1364 stage = STAGE_LOGOUT;
1365 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1367 cleanupSockClose(mailserver_socket);
1370 msg = (const char *)NULL; /* sacrifice to -Wall */
1374 msg = GT_("socket");
1377 msg = GT_("missing or bad RFC822 header");
1383 msg = GT_("client/server synchronization");
1386 msg = GT_("client/server protocol");
1389 msg = GT_("lock busy on server");
1392 msg = GT_("SMTP transaction");
1395 msg = GT_("DNS lookup");
1398 report(stderr, GT_("undefined error\n"));
1401 /* no report on PS_MAXFETCH or PS_UNDEFINED or PS_AUTHFAIL */
1402 if (err==PS_SOCKET || err==PS_SYNTAX
1403 || err==PS_IOERR || err==PS_ERROR || err==PS_PROTOCOL
1404 || err==PS_LOCKBUSY || err==PS_SMTP || err==PS_DNS)
1408 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT)
1409 stem = GT_("%s error while delivering to SMTP host %s\n");
1411 stem = GT_("%s error while fetching from %s\n");
1412 report(stderr, stem, msg, ctl->server.pollname);
1416 /* execute wrapup command, if any */
1417 if (ctl->postconnect && (err = system(ctl->postconnect)))
1419 report(stderr, GT_("post-connection command failed with status %d\n"), err);
1420 if (err == PS_SUCCESS)
1424 signal(SIGALRM, alrmsave);
1425 signal(SIGPIPE, pipesave);
1429 int do_protocol(ctl, proto)
1430 /* retrieve messages from server using given protocol method table */
1431 struct query *ctl; /* parsed options with merged-in defaults */
1432 const struct method *proto; /* protocol method table */
1437 if (ctl->server.authenticate == A_KERBEROS_V4)
1439 report(stderr, GT_("Kerberos V4 support not linked.\n"));
1442 #endif /* KERBEROS_V4 */
1445 if (ctl->server.authenticate == A_KERBEROS_V5)
1447 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1450 #endif /* KERBEROS_V5 */
1452 /* lacking methods, there are some options that may fail */
1455 /* check for unsupported options */
1458 GT_("Option --flush is not supported with %s\n"),
1462 else if (ctl->fetchall) {
1464 GT_("Option --all is not supported with %s\n"),
1469 if (!proto->getsizes && NUM_SPECIFIED(ctl->limit))
1472 GT_("Option --limit is not supported with %s\n"),
1478 * If no expunge limit or we do expunges within the driver,
1479 * then just do one session, passing in any fetchlimit.
1481 if (proto->retry || !NUM_SPECIFIED(ctl->expunge))
1482 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1484 * There's an expunge limit, and it isn't handled in the driver itself.
1485 * OK; do multiple sessions, each fetching a limited # of messages.
1486 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1487 * (if it was nonzero).
1493 int expunge = NUM_VALUE_OUT(ctl->expunge);
1494 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1497 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1498 expunge = fetchlimit - totalcount;
1499 err = do_session(ctl, proto, expunge);
1500 totalcount += expunge;
1501 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1503 if (err != PS_LOCKBUSY)
1505 else if (lockouts >= MAX_LOCKOUTS)
1507 else /* err == PS_LOCKBUSY */
1510 * Allow time for the server lock to release. if we
1511 * don't do this, we'll often hit a locked-mailbox
1512 * condition and fail.
1518 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1525 /* driver.c ends here */