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>
32 #ifdef HAVE_RES_SEARCH
35 #endif /* HAVE_RES_SEARCH */
39 #include <netinet/in.h>
40 #endif /* KERBEROS_V4 */
45 #include "fetchmail.h"
48 /* throw types for runtime errors */
49 #define THROW_TIMEOUT 1 /* server timed out */
50 #define THROW_SIGPIPE 2 /* SIGPIPE on stream socket */
52 /* magic values for the message length array */
53 #define MSGLEN_UNKNOWN 0 /* length unknown (0 is impossible) */
54 #define MSGLEN_INVALID -1 /* length passed back is invalid */
55 #define MSGLEN_TOOLARGE -2 /* message is too large */
56 #define MSGLEN_OLD -3 /* message is old */
58 int pass; /* how many times have we re-polled? */
59 int stage; /* where are we? */
60 int phase; /* where are we, for error-logging purposes? */
61 int batchcount; /* count of messages sent in current batch */
62 flag peek_capable; /* can we peek for better error recovery? */
64 static int timeoutcount; /* count consecutive timeouts */
66 static jmp_buf restart;
68 void set_timeout(int timeleft)
69 /* reset the nonresponse-timeout */
71 #if !defined(__EMX__) && !defined(__BEOS__)
72 struct itimerval ntimeout;
77 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
78 ntimeout.it_value.tv_sec = timeleft;
79 ntimeout.it_value.tv_usec = 0;
80 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
84 static void timeout_handler (int signal)
85 /* handle SIGALRM signal indicating a server timeout */
88 longjmp(restart, THROW_TIMEOUT);
91 static void sigpipe_handler (int signal)
92 /* handle SIGPIPE signal indicating a broken stream socket */
94 longjmp(restart, THROW_SIGPIPE);
98 static int kerberos_auth(socket, canonical, principal)
99 /* authenticate to the server host using Kerberos V4 */
100 int socket; /* socket to server host */
101 char *canonical; /* server name */
108 Key_schedule schedule;
110 char * prin_copy = (char *) NULL;
111 char * prin = (char *) NULL;
112 char * inst = (char *) NULL;
113 char * realm = (char *) NULL;
115 if (principal != (char *)NULL && *principal)
118 prin = prin_copy = xstrdup(principal);
119 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
125 while (*cp && *cp != '@')
135 xalloca(ticket, KTEXT, sizeof (KTEXT_ST));
136 rem = (krb_sendauth (0L, socket, ticket,
138 inst ? inst : canonical,
139 realm ? realm : ((char *) (krb_realmofhost (canonical))),
144 ((struct sockaddr_in *) 0),
145 ((struct sockaddr_in *) 0),
153 report(stderr, _("kerberos error %s\n"), (krb_get_err_text (rem)));
154 return (PS_AUTHFAIL);
158 #endif /* KERBEROS_V4 */
161 static int kerberos5_auth(socket, canonical)
162 /* authenticate to the server host using Kerberos V5 */
163 int socket; /* socket to server host */
164 const char *canonical; /* server name */
166 krb5_error_code retval;
167 krb5_context context;
169 krb5_principal client = NULL, server = NULL;
170 krb5_error *err_ret = NULL;
172 krb5_auth_context auth_context = NULL;
174 krb5_init_context(&context);
175 krb5_init_ets(context);
176 krb5_auth_con_init(context, &auth_context);
178 if (retval = krb5_cc_default(context, &ccdef)) {
179 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
183 if (retval = krb5_cc_get_principal(context, ccdef, &client)) {
184 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
188 if (retval = krb5_sname_to_principal(context, canonical, "pop",
191 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
195 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
196 "KPOPV1.0", client, server,
197 AP_OPTS_MUTUAL_REQUIRED,
198 NULL, /* no data to checksum */
199 0, /* no creds, use ccache instead */
203 NULL); /* don't need reply */
205 krb5_free_principal(context, server);
206 krb5_free_principal(context, client);
207 krb5_auth_con_free(context, auth_context);
211 if (err_ret && err_ret->e_text) {
212 report(stderr, _("krb5_sendauth: %s [server says '%*s'] \n"),
213 error_message(retval),
216 if (err_ret && err_ret->text.length) {
217 report(stderr, _("krb5_sendauth: %s [server says '%*s'] \n"),
218 error_message(retval),
219 err_ret->text.length,
222 krb5_free_error(context, err_ret);
224 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
230 #endif /* KERBEROS_V5 */
232 static void clean_skipped_list(struct idlist **skipped_list)
233 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
235 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
236 prev = current = head = *skipped_list;
242 /* if item has no reference, remove it */
243 if (current && current->val.status.mark == 0)
245 if (current == head) /* remove first item (head) */
247 head = current->next;
248 if (current->id) free(current->id);
250 prev = current = head;
252 else /* remove middle/last item */
256 if (current->id) free(current->id);
261 else /* skip this item */
264 current = current->next;
268 *skipped_list = head;
271 static void send_size_warnings(struct query *ctl)
272 /* send warning mail with skipped msg; reset msg count when user notified */
275 int msg_to_send = FALSE;
276 struct idlist *head=NULL, *current=NULL;
277 int max_warning_poll_count;
283 /* don't start a notification message unless we need to */
284 for (current = head; current; current = current->next)
285 if (current->val.status.num == 0 && current->val.status.mark)
291 * There's no good way to recover if we can't send notification mail,
292 * but it's not a disaster, either, since the skipped mail will not
295 if (open_warning_by_mail(ctl, (struct msgblk *)NULL))
298 _("Subject: Fetchmail oversized-messages warning.\r\n"
300 "The following oversized messages remain on the mail server %s:"),
301 ctl->server.pollname);
303 if (run.poll_interval == 0)
304 max_warning_poll_count = 0;
306 max_warning_poll_count = ctl->warnings/run.poll_interval;
308 /* parse list of skipped msg, adding items to the mail */
309 for (current = head; current; current = current->next)
311 if (current->val.status.num == 0 && current->val.status.mark)
313 nbr = current->val.status.mark;
314 size = atoi(current->id);
316 _("\t%d msg %d octets long skipped by fetchmail.\r\n"),
319 current->val.status.num++;
320 current->val.status.mark = 0;
322 if (current->val.status.num >= max_warning_poll_count)
323 current->val.status.num = 0;
326 close_warning_by_mail(ctl, (struct msgblk *)NULL);
329 static void mark_oversized(int num, struct query *ctl, int *msgsizes)
330 /* mark a message oversized */
332 struct idlist *current=NULL, *tmp=NULL;
336 /* convert sz to string */
338 snprintf(size, sizeof(size),
341 #endif /* HAVE_SNPRINTF */
342 "%d", msgsizes[num-1]);
344 /* build a list of skipped messages
345 * val.id = size of msg (string cnvt)
346 * val.status.num = warning_poll_count
347 * val.status.mask = nbr of msg this size
350 current = ctl->skipped;
352 /* initialise warning_poll_count to the
353 * current value so that all new msg will
354 * be included in the next mail
356 cnt = current ? current->val.status.num : 0;
358 /* if entry exists, increment the count */
359 if (current && str_in_list(¤t, size, FALSE))
361 for ( ; current; current = current->next)
363 if (strcmp(current->id, size) == 0)
365 current->val.status.mark++;
370 /* otherwise, create a new entry */
371 /* initialise with current poll count */
374 tmp = save_str(&ctl->skipped, size, 1);
375 tmp->val.status.num = cnt;
379 static int fetch_messages(int mailserver_socket, struct query *ctl,
380 int count, int *msgsizes, int maxfetch,
381 int *fetches, int *dispatches, int *deletions)
382 /* fetch messages in lockstep mode */
386 for (num = 1; num <= count; num++)
388 flag suppress_delete = FALSE;
389 flag suppress_forward = FALSE;
390 flag suppress_readbody = FALSE;
391 flag retained = FALSE;
393 if (msgsizes[num-1] < 0)
395 if ((msgsizes[num-1] == MSGLEN_TOOLARGE) && !check_only)
396 mark_oversized(num, ctl, msgsizes);
397 if (outlevel > O_SILENT)
400 _("skipping message %d (%d octets)"),
401 num, msgsizes[num-1]);
402 switch (msgsizes[num-1])
406 * Invalid lengths are produced by Post Office/NT's
407 * annoying habit of randomly prepending bogus
408 * LIST items of length -1. Patrick Audley
409 * <paudley@pobox.com> tells us: LIST shows a
410 * size of -1, RETR and TOP return "-ERR
411 * System error - couldn't open message", and
412 * DELE succeeds but doesn't actually delete
415 report_build(stdout, _(" (length -1)"));
417 case MSGLEN_TOOLARGE:
419 _(" (oversized, %d octets)"),
427 flag wholesize = !ctl->server.base_protocol->fetch_body;
429 /* request a message */
430 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
434 /* -1 means we didn't see a size in the response */
435 if (len == -1 && msgsizes)
437 len = msgsizes[num - 1];
441 if (outlevel > O_SILENT)
443 report_build(stdout, _("reading message %d of %d"),
447 report_build(stdout, _(" (%d %soctets)"),
448 len, wholesize ? "" : _("header "));
449 if (outlevel >= O_VERBOSE)
450 report_complete(stdout, "\n");
452 report_complete(stdout, " ");
456 * Read the message headers and ship them to the
459 err = readheaders(mailserver_socket, len, msgsizes[num-1],
461 if (err == PS_RETAINED)
462 suppress_forward = retained = TRUE;
463 else if (err == PS_TRANSIENT)
464 suppress_delete = suppress_forward = TRUE;
465 else if (err == PS_REFUSED)
466 suppress_forward = TRUE;
467 else if (err == PS_TRUNCATED)
468 suppress_readbody = TRUE;
473 * If we're using IMAP4 or something else that
474 * can fetch headers separately from bodies,
475 * it's time to request the body now. This
476 * fetch may be skipped if we got an anti-spam
477 * or other PS_REFUSED error response during
480 if (ctl->server.base_protocol->fetch_body && !suppress_readbody)
482 if (outlevel >= O_VERBOSE && !isafile(1))
488 if ((err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num)))
491 if (!suppress_forward)
493 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len)))
496 * Work around a bug in Novell's
497 * broken GroupWise IMAP server;
498 * its body FETCH response is missing
499 * the required length for the data
500 * string. This violates RFC2060.
503 len = msgsizes[num-1] - msgblk.msglen;
504 if (outlevel > O_SILENT && !wholesize)
505 report_complete(stdout,
506 _(" (%d body octets) "), len);
510 /* process the body now */
513 if (suppress_readbody)
515 /* When readheaders returns PS_TRUNCATED,
516 * the body (which has no content)
517 * has already been read by readheaders,
518 * so we say readbody returned PS_SUCCESS
524 err = readbody(mailserver_socket,
529 if (err == PS_TRANSIENT)
530 suppress_delete = suppress_forward = TRUE;
534 /* tell server we got it OK and resynchronize */
535 if (ctl->server.base_protocol->trail)
537 if (outlevel >= O_VERBOSE && !isafile(1))
543 err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num);
549 /* count # messages forwarded on this pass */
550 if (!suppress_forward)
554 * Check to see if the numbers matched?
556 * Yes, some servers foo this up horribly.
557 * All IMAP servers seem to get it right, and
558 * so does Eudora QPOP at least in 2.xx
561 * Microsoft Exchange gets it completely
562 * wrong, reporting compressed rather than
563 * actual sizes (so the actual length of
564 * message is longer than the reported size).
565 * Another fine example of Microsoft brain death!
567 * Some older POP servers, like the old UCB
568 * POP server and the pre-QPOP QUALCOMM
569 * versions, report a longer size in the LIST
570 * response than actually gets shipped up.
571 * It's unclear what is going on here, as the
572 * QUALCOMM server (at least) seems to be
573 * reporting the on-disk size correctly.
575 if (msgsizes && msgblk.msglen != msgsizes[num-1])
577 if (outlevel >= O_DEBUG)
579 _("message %d was not the expected length (%d actual != %d expected)\n"),
580 num, msgblk.msglen, msgsizes[num-1]);
583 /* end-of-message processing starts here */
584 if (!close_sink(ctl, &msgblk, !suppress_forward))
587 suppress_delete = TRUE;
593 * At this point in flow of control, either
594 * we've bombed on a protocol error or had
595 * delivery refused by the SMTP server
596 * (unlikely -- I've never seen it) or we've
597 * seen `accepted for delivery' and the
598 * message is shipped. It's safe to mark the
599 * message seen and delete it on the server
603 /* tell the UID code we've seen this */
608 for (sdp = ctl->newsaved; sdp; sdp = sdp->next)
609 if ((sdp->val.status.num == num) && (msgsizes[num-1] > 0))
611 sdp->val.status.mark = UID_SEEN;
612 save_str(&ctl->oldsaved, sdp->id,UID_SEEN);
616 /* maybe we delete this message now? */
619 if (outlevel > O_SILENT)
620 report(stdout, _(" retained\n"));
622 else if (ctl->server.base_protocol->delete
624 && ((msgsizes[num-1] > 0) ? !ctl->keep : ctl->flush))
627 if (outlevel > O_SILENT)
628 report_complete(stdout, _(" flushed\n"));
629 err = (ctl->server.base_protocol->delete)(mailserver_socket, ctl, num);
633 delete_str(&ctl->newsaved, num);
634 #endif /* POP3_ENABLE */
636 else if (outlevel > O_SILENT)
637 report_complete(stdout, _(" not flushed\n"));
639 /* perhaps this as many as we're ready to handle */
640 if (maxfetch && maxfetch <= *fetches && *fetches < count)
642 report(stdout, _("fetchlimit %d reached; %d messages left on server\n"),
643 maxfetch, count - *fetches);
651 static int do_session(ctl, proto, maxfetch)
652 /* retrieve messages from server using given protocol method table */
653 struct query *ctl; /* parsed options with merged-in defaults */
654 const struct method *proto; /* protocol method table */
655 const int maxfetch; /* maximum number of messages to fetch */
659 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
661 int err, mailserver_socket = -1;
662 #endif /* HAVE_VOLATILE */
664 void (*pipesave)(int);
665 void (*alrmsave)(int);
667 ctl->server.base_protocol = proto;
671 init_transact(proto);
673 /* set up the server-nonresponse timeout */
674 alrmsave = signal(SIGALRM, timeout_handler);
675 mytimeout = ctl->server.timeout;
677 /* set up the broken-pipe timeout */
678 pipesave = signal(SIGPIPE, sigpipe_handler);
680 if ((js = setjmp(restart)))
682 #ifdef HAVE_SIGPROCMASK
684 * Don't rely on setjmp() to restore the blocked-signal mask.
685 * It does this under BSD but is required not to under POSIX.
687 * If your Unix doesn't have sigprocmask, better hope it has
688 * BSD-like behavior. Otherwise you may see fetchmail get
689 * permanently wedged after a second timeout on a bad read,
690 * because alarm signals were blocked after the first.
694 sigfillset(&allsigs);
695 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
696 #endif /* HAVE_SIGPROCMASK */
698 if (js == THROW_SIGPIPE)
700 signal(SIGPIPE, SIG_IGN);
702 _("SIGPIPE thrown from an MDA or a stream socket error\n"));
706 else if (js == THROW_TIMEOUT)
708 if (phase == OPEN_WAIT)
710 _("timeout after %d seconds waiting to connect to server %s.\n"),
711 ctl->server.timeout, ctl->server.pollname);
712 else if (phase == SERVER_WAIT)
714 _("timeout after %d seconds waiting for server %s.\n"),
715 ctl->server.timeout, ctl->server.pollname);
716 else if (phase == FORWARDING_WAIT)
718 _("timeout after %d seconds waiting for %s.\n"),
720 ctl->mda ? "MDA" : "SMTP");
721 else if (phase == LISTENER_WAIT)
723 _("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
726 _("timeout after %d seconds.\n"), ctl->server.timeout);
729 * If we've exceeded our threshold for consecutive timeouts,
730 * try to notify the user, then mark the connection wedged.
731 * Don't do this if the connection can idle, though; idle
732 * timeouts just mean the frequency of mail is low.
734 if (timeoutcount > MAX_TIMEOUTS
735 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
738 _("Subject: fetchmail sees repeated timeouts\r\n"));
740 _("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\r\n"),
743 ctl->server.truename);
745 _("This could mean that your mailserver is stuck, or that your SMTP\r\n" \
746 "server is wedged, or that your mailbox file on the server has been\r\n" \
747 "corrupted by a server error. You can run `fetchmail -v -v' to\r\n" \
748 "diagnose the problem.\r\n\r\n" \
749 "Fetchmail won't poll this mailbox again until you restart it.\r\n"));
750 close_warning_by_mail(ctl, (struct msgblk *)NULL);
757 /* try to clean up all streams */
759 if (ctl->smtp_socket != -1)
760 SockClose(ctl->smtp_socket);
761 if (mailserver_socket != -1)
762 SockClose(mailserver_socket);
766 char buf[MSGBUFSIZE+1], *realhost;
767 int count, new, bytes, deletions = 0, *msgsizes = NULL;
769 int fetches, dispatches, oldphase;
770 #else /* INET6_ENABLE */
771 int port, fetches, dispatches, oldphase;
772 #endif /* INET6_ENABLE */
775 /* execute pre-initialization command, if any */
776 if (ctl->preconnect && (err = system(ctl->preconnect)))
779 _("pre-connection command failed with status %d\n"), err);
784 /* open a socket to the mail server */
787 set_timeout(mytimeout);
790 port = ctl->server.port ? ctl->server.port : ( ctl->use_ssl ? ctl->server.base_protocol->sslport : ctl->server.base_protocol->port );
792 port = ctl->server.port ? ctl->server.port : ctl->server.base_protocol->port;
794 #endif /* !INET6_ENABLE */
795 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
797 /* allow time for the port to be set up if we have a plugin */
798 if (ctl->server.plugin)
801 if ((mailserver_socket = SockOpen(realhost,
802 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
803 ctl->server.netsec, ctl->server.plugin)) == -1)
804 #else /* INET6_ENABLE */
805 if ((mailserver_socket = SockOpen(realhost, port, NULL, ctl->server.plugin)) == -1)
806 #endif /* INET6_ENABLE */
811 #ifdef HAVE_RES_SEARCH
812 if (err_no != 0 && h_errno != 0)
813 report(stderr, _("internal inconsistency\n"));
816 * Avoid generating a bogus error every poll cycle when we're
817 * in daemon mode but the connection to the outside world
820 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
821 && run.poll_interval))
823 report_build(stderr, _("%s connection to %s failed"),
824 ctl->server.base_protocol->name, ctl->server.pollname);
825 #ifdef HAVE_RES_SEARCH
828 if (h_errno == HOST_NOT_FOUND)
829 strcpy(errbuf, _("host is unknown."));
831 else if (h_errno == NO_ADDRESS)
832 strcpy(errbuf, _("name is valid but has no IP address."));
834 else if (h_errno == NO_RECOVERY)
835 strcpy(errbuf, _("unrecoverable name server error."));
836 else if (h_errno == TRY_AGAIN)
837 strcpy(errbuf, _("temporary name server error."));
840 snprintf(errbuf, sizeof(errbuf),
843 #endif /* HAVE_SNPRINTF */
844 _("unknown DNS error %d."), h_errno);
847 #endif /* HAVE_RES_SEARCH */
848 strcpy(errbuf, strerror(err_no));
849 report_complete(stderr, ": %s\n", errbuf);
853 * Don't use this. It was an attempt to address Debian bug
854 * #47143 (Notify user by mail when pop server nonexistent).
855 * Trouble is, that doesn't work; you trip over the case
856 * where your SLIP or PPP link is down...
858 /* warn the system administrator */
859 if (open_warning_by_mail(ctl, (struct msgblk *)NULL) == 0)
862 _("Subject: Fetchmail unreachable-server warning.\r\n"
864 "Fetchmail could not reach the mail server %s:")
865 ctl->server.pollname);
866 stuff_warning(ctl, errbuf, ctl->server.pollname);
867 close_warning_by_mail(ctl, (struct msgblk *)NULL);
871 #endif /* INET6_ENABLE */
881 /* perform initial SSL handshake on open connection */
882 /* Note: We pass the realhost name over for certificate
883 verification. We may want to make this configurable */
884 if (ctl->use_ssl && SSLOpen(mailserver_socket,ctl->sslkey,ctl->sslcert,ctl->sslproto,ctl->sslcertck,
885 ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
887 report(stderr, _("SSL connection failed.\n"));
893 if (ctl->server.authenticate == A_KERBEROS_V4)
895 set_timeout(mytimeout);
896 err = kerberos_auth(mailserver_socket, ctl->server.truename,
897 ctl->server.principal);
902 #endif /* KERBEROS_V4 */
905 if (ctl->server.authenticate == A_KERBEROS_V5)
907 set_timeout(mytimeout);
908 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
913 #endif /* KERBEROS_V5 */
915 /* accept greeting message from mail server */
916 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
920 /* try to get authorized to fetch mail */
921 stage = STAGE_GETAUTH;
922 if (ctl->server.base_protocol->getauth)
924 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
928 if (err == PS_LOCKBUSY)
929 report(stderr, _("Lock-busy error on %s@%s\n"),
931 ctl->server.truename);
932 else if (err == PS_SERVBUSY)
933 report(stderr, _("Server busy error on %s@%s\n"),
935 ctl->server.truename);
936 else if (err == PS_AUTHFAIL)
938 report(stderr, _("Authorization failure on %s@%s%s\n"),
940 ctl->server.truename,
941 (ctl->wehaveauthed ? _(" (previously authorized)") : "")
945 * If we're running in background, try to mail the
946 * calling user a heads-up about the authentication
947 * failure once it looks like this isn't a fluke
948 * due to the server being temporarily inaccessible.
949 * When we get third succesive failure, we notify the user
950 * but only if we haven't already managed to get
951 * authorization. After that, once we get authorization
952 * we let the user know service is restored.
954 if (run.poll_interval
955 && ctl->wehavesentauthnote
956 && ((ctl->wehaveauthed && ++ctl->authfailcount == 10)
957 || ++ctl->authfailcount == 3)
958 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
960 ctl->wehavesentauthnote = 1;
962 _("Subject: fetchmail authentication failed on %s@%s\r\n"),
963 ctl->remotename, ctl->server.truename);
965 _("Fetchmail could not get mail from %s@%s.\r\n"),
967 ctl->server.truename);
968 if (ctl->wehaveauthed)
969 stuff_warning(ctl, _("\
970 The attempt to get authorization failed.\r\n\
971 Since we have already succeeded in getting authorization for this\r\n\
972 connection, this is probably another failure mode (such as busy server)\r\n\
973 that fetchmail cannot distinguish because the server didn't send a useful\r\n\
976 However, if you HAVE changed you account details since starting the\r\n\
977 fetchmail daemon, you need to stop the daemon, change your configuration\r\n\
978 of fetchmail, and then restart the daemon.\r\n\
980 The fetchmail daemon will continue running and attempt to connect\r\n\
981 at each cycle. No future notifications will be sent until service\r\n\
984 stuff_warning(ctl, _("\
985 The attempt to get authorization failed.\r\n\
986 This probably means your password is invalid, but some servers have\r\n\
987 other failure modes that fetchmail cannot distinguish from this\r\n\
988 because they don't send useful error messages on login failure.\r\n\
990 The fetchmail daemon will continue running and attempt to connect\r\n\
991 at each cycle. No future notifications will be sent until service\r\n\
993 close_warning_by_mail(ctl, (struct msgblk *)NULL);
997 report(stderr, _("Unknown login or authentication error on %s@%s\n"),
999 ctl->server.truename);
1006 * This connection has given us authorization at least once.
1008 * There are dodgy server (clubinternet.fr for example) that
1009 * give spurious authorization failures on patently good
1010 * account/password details, then 5 minutes later let you in!
1012 * This is meant to build in some tolerance of such nasty bits
1015 ctl->wehaveauthed = 1;
1016 /*if (ctl->authfailcount >= 3)*/
1017 if (ctl->wehavesentauthnote)
1019 ctl->wehavesentauthnote = 0;
1021 _("Authorization OK on %s@%s\n"),
1023 ctl->server.truename);
1024 if (!open_warning_by_mail(ctl, (struct msgblk *)NULL))
1027 _("Subject: fetchmail authentication OK on %s@%s\r\n"),
1028 ctl->remotename, ctl->server.truename);
1030 _("Fetchmail was able to log into %s@%s.\r\n"),
1032 ctl->server.truename);
1034 _("Service has been restored.\r\n"));
1035 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1040 * Reporting only after the first three
1041 * consecutive failures, or ten consecutive
1042 * failures after we have managed to get
1045 ctl->authfailcount = 0;
1049 ctl->errcount = fetches = 0;
1051 /* now iterate over each folder selected */
1052 for (idp = ctl->mailboxes; idp; idp = idp->next)
1059 /* reset timeout, in case we did an IDLE */
1060 mytimeout = ctl->server.timeout;
1062 if (outlevel >= O_DEBUG)
1065 report(stdout, _("selecting or re-polling folder %s\n"), idp->id);
1067 report(stdout, _("selecting or re-polling default folder\n"));
1070 /* compute # of messages and number of new messages waiting */
1071 stage = STAGE_GETRANGE;
1072 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &new, &bytes);
1076 /* show user how many messages we downloaded */
1078 #ifdef HAVE_SNPRINTF
1079 (void) snprintf(buf, sizeof(buf),
1082 #endif /* HAVE_SNPRINTF */
1083 _("%s at %s (folder %s)"),
1084 ctl->remotename, ctl->server.truename, idp->id);
1086 #ifdef HAVE_SNPRINTF
1087 (void) snprintf(buf, sizeof(buf),
1090 #endif /* HAVE_SNPRINTF */
1092 ctl->remotename, ctl->server.truename);
1093 if (outlevel > O_SILENT)
1095 if (count == -1) /* only used for ETRN */
1096 report(stdout, _("Polling %s\n"), ctl->server.truename);
1097 else if (count != 0)
1099 if (new != -1 && (count - new) > 0)
1100 report_build(stdout, _("%d %s (%d seen) for %s"),
1101 count, count > 1 ? _("messages") :
1105 report_build(stdout, _("%d %s for %s"),
1106 count, count > 1 ? _("messages") :
1109 report_complete(stdout, ".\n");
1111 report_complete(stdout, _(" (%d octets).\n"), bytes);
1115 /* these are pointless in normal daemon mode */
1116 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1117 report(stdout, _("No mail for %s\n"), buf);
1121 /* very important, this is where we leave the do loop */
1127 if (new == -1 || ctl->fetchall)
1129 fetches = new; /* set error status correctly */
1131 * There used to be a `goto noerror' here, but this
1132 * prevneted checking of multiple folders. This
1133 * comment is a reminder in case I introduced some
1134 * subtle bug by removing it...
1139 flag force_retrieval;
1143 * What forces this code is that in POP2 and
1144 * IMAP2bis you can't fetch a message without
1145 * having it marked `seen'. In POP3 and IMAP4, on the
1146 * other hand, you can (peek_capable is set by
1147 * each driver module to convey this; it's not a
1148 * method constant because of the difference between
1149 * IMAP2bis and IMAP4, and because POP3 doesn't peek
1150 * if fetchall is on).
1152 * The result of being unable to peek is that if there's
1153 * any kind of transient error (DNS lookup failure, or
1154 * sendmail refusing delivery due to process-table limits)
1155 * the message will be marked "seen" on the server without
1156 * having been delivered. This is not a big problem if
1157 * fetchmail is running in foreground, because the user
1158 * will see a "skipped" message when it next runs and get
1161 * But in daemon mode this leads to the message
1162 * being silently ignored forever. This is not
1165 * We compensate for this by checking the error
1166 * count from the previous pass and forcing all
1167 * messages to be considered new if it's nonzero.
1169 force_retrieval = !peek_capable && (ctl->errcount > 0);
1171 /* OK, we're going to gather size info next */
1172 xalloca(msgsizes, int *, sizeof(int) * count);
1173 for (i = 0; i < count; i++)
1174 msgsizes[i] = MSGLEN_UNKNOWN;
1177 * We need the size of each message before it's
1178 * loaded in order to pass it to the ESMTP SIZE
1179 * option. If the protocol has a getsizes method,
1180 * we presume this means it doesn't get reliable
1181 * sizes from message fetch responses.
1183 if (proto->getsizes)
1185 stage = STAGE_GETSIZES;
1186 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1193 for (i = 0; i < count; i++)
1194 bytes += msgsizes[i];
1198 /* mark some messages not to be retrieved */
1199 for (num = 1; num <= count; num++)
1201 if (NUM_NONZERO(ctl->limit) && (msgsizes[num-1] > ctl->limit))
1202 msgsizes[num-1] = MSGLEN_TOOLARGE;
1203 else if (ctl->fetchall || force_retrieval)
1205 else if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num))
1206 msgsizes[num-1] = MSGLEN_OLD;
1209 /* read, forward, and delete messages */
1210 stage = STAGE_FETCH;
1212 /* fetch in lockstep mode */
1213 err = fetch_messages(mailserver_socket, ctl,
1216 &fetches, &dispatches, &deletions);
1220 if (!check_only && ctl->skipped
1221 && run.poll_interval > 0 && !nodetach)
1223 clean_skipped_list(&ctl->skipped);
1224 send_size_warnings(ctl);
1229 * Only re-poll if we either had some actual forwards and
1230 * either allowed deletions and had no errors.
1231 * Otherwise it is far too easy to get into infinite loops.
1233 (dispatches && ctl->server.base_protocol->retry && !ctl->keep && !ctl->errcount);
1237 /* ordinary termination with no errors -- officially log out */
1238 err = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1240 * Hmmmm...arguably this would be incorrect if we had fetches but
1241 * no dispatches (due to oversized messages, etc.)
1244 err = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1245 SockClose(mailserver_socket);
1249 /* we only get here on error */
1250 if (err != 0 && err != PS_SOCKET)
1252 stage = STAGE_LOGOUT;
1253 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1255 SockClose(mailserver_socket);
1258 msg = (const char *)NULL; /* sacrifice to -Wall */
1265 msg = _("missing or bad RFC822 header");
1271 msg = _("client/server synchronization");
1274 msg = _("client/server protocol");
1277 msg = _("lock busy on server");
1280 msg = _("SMTP transaction");
1283 msg = _("DNS lookup");
1286 report(stderr, _("undefined error\n"));
1289 /* no report on PS_MAXFETCH or PS_UNDEFINED or PS_AUTHFAIL */
1290 if (err==PS_SOCKET || err==PS_SYNTAX
1291 || err==PS_IOERR || err==PS_ERROR || err==PS_PROTOCOL
1292 || err==PS_LOCKBUSY || err==PS_SMTP || err==PS_DNS)
1296 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT)
1297 stem = _("%s error while delivering to SMTP host %s\n");
1299 stem = _("%s error while fetching from %s\n");
1300 report(stderr, stem, msg, ctl->server.pollname);
1304 /* execute wrapup command, if any */
1305 if (ctl->postconnect && (err = system(ctl->postconnect)))
1307 report(stderr, _("post-connection command failed with status %d\n"), err);
1308 if (err == PS_SUCCESS)
1312 signal(SIGALRM, alrmsave);
1313 signal(SIGPIPE, pipesave);
1317 int do_protocol(ctl, proto)
1318 /* retrieve messages from server using given protocol method table */
1319 struct query *ctl; /* parsed options with merged-in defaults */
1320 const struct method *proto; /* protocol method table */
1325 if (ctl->server.authenticate == A_KERBEROS_V4)
1327 report(stderr, _("Kerberos V4 support not linked.\n"));
1330 #endif /* KERBEROS_V4 */
1333 if (ctl->server.authenticate == A_KERBEROS_V5)
1335 report(stderr, _("Kerberos V5 support not linked.\n"));
1338 #endif /* KERBEROS_V5 */
1340 /* lacking methods, there are some options that may fail */
1343 /* check for unsupported options */
1346 _("Option --flush is not supported with %s\n"),
1350 else if (ctl->fetchall) {
1352 _("Option --all is not supported with %s\n"),
1357 if (!proto->getsizes && NUM_SPECIFIED(ctl->limit))
1360 _("Option --limit is not supported with %s\n"),
1366 * If no expunge limit or we do expunges within the driver,
1367 * then just do one session, passing in any fetchlimit.
1369 if (proto->retry || !NUM_SPECIFIED(ctl->expunge))
1370 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1372 * There's an expunge limit, and it isn't handled in the driver itself.
1373 * OK; do multiple sessions, each fetching a limited # of messages.
1374 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1375 * (if it was nonzero).
1381 int expunge = NUM_VALUE_OUT(ctl->expunge);
1382 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1385 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1386 expunge = fetchlimit - totalcount;
1387 err = do_session(ctl, proto, expunge);
1388 totalcount += expunge;
1389 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1391 if (err != PS_LOCKBUSY)
1393 else if (lockouts >= MAX_LOCKOUTS)
1395 else /* err == PS_LOCKBUSY */
1398 * Allow time for the server lock to release. if we
1399 * don't do this, we'll often hit a locked-mailbox
1400 * condition and fail.
1406 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1413 /* driver.c ends here */