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; /* socket to free if connect timeout */
72 static int timeoutcount; /* count consecutive timeouts */
74 static jmp_buf restart;
76 void set_timeout(int timeleft)
77 /* reset the nonresponse-timeout */
79 #if !defined(__EMX__) && !defined(__BEOS__)
80 struct itimerval ntimeout;
85 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
86 ntimeout.it_value.tv_sec = timeleft;
87 ntimeout.it_value.tv_usec = 0;
88 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
92 static void timeout_handler (int signal)
93 /* handle SIGALRM signal indicating a server timeout */
96 longjmp(restart, THROW_TIMEOUT);
99 static void sigpipe_handler (int signal)
100 /* handle SIGPIPE signal indicating a broken stream socket */
102 longjmp(restart, THROW_SIGPIPE);
105 /* ignore SIGALRM signal indicating a timeout during cleanup */
106 static void cleanup_timeout_handler (int signal) { }
108 #define CLEANUP_TIMEOUT 60 /* maximum timeout during cleanup */
110 static int cleanupSockClose (int fd)
111 /* close sockets in maximum CLEANUP_TIMEOUT seconds during cleanup */
114 void (*alrmsave)(int);
115 alrmsave = signal(SIGALRM, cleanup_timeout_handler);
116 set_timeout(CLEANUP_TIMEOUT);
117 scerror = SockClose(fd);
119 signal(SIGALRM, alrmsave);
124 static int kerberos_auth(socket, canonical, principal)
125 /* authenticate to the server host using Kerberos V4 */
126 int socket; /* socket to server host */
127 char *canonical; /* server name */
134 Key_schedule schedule;
136 char * prin_copy = (char *) NULL;
137 char * prin = (char *) NULL;
138 char * inst = (char *) NULL;
139 char * realm = (char *) NULL;
141 if (principal != (char *)NULL && *principal)
144 prin = prin_copy = xstrdup(principal);
145 for (cp = prin_copy; *cp && *cp != '.'; ++cp)
151 while (*cp && *cp != '@')
161 xalloca(ticket, KTEXT, sizeof (KTEXT_ST));
162 rem = (krb_sendauth (0L, socket, ticket,
164 inst ? inst : canonical,
165 realm ? realm : ((char *) (krb_realmofhost (canonical))),
170 ((struct sockaddr_in *) 0),
171 ((struct sockaddr_in *) 0),
179 report(stderr, GT_("kerberos error %s\n"), (krb_get_err_text (rem)));
180 return (PS_AUTHFAIL);
184 #endif /* KERBEROS_V4 */
187 static int kerberos5_auth(socket, canonical)
188 /* authenticate to the server host using Kerberos V5 */
189 int socket; /* socket to server host */
190 const char *canonical; /* server name */
192 krb5_error_code retval;
193 krb5_context context;
195 krb5_principal client = NULL, server = NULL;
196 krb5_error *err_ret = NULL;
198 krb5_auth_context auth_context = NULL;
200 krb5_init_context(&context);
201 krb5_init_ets(context);
202 krb5_auth_con_init(context, &auth_context);
204 if (retval = krb5_cc_default(context, &ccdef)) {
205 report(stderr, "krb5_cc_default: %s\n", error_message(retval));
209 if (retval = krb5_cc_get_principal(context, ccdef, &client)) {
210 report(stderr, "krb5_cc_get_principal: %s\n", error_message(retval));
214 if (retval = krb5_sname_to_principal(context, canonical, "pop",
217 report(stderr, "krb5_sname_to_principal: %s\n", error_message(retval));
221 retval = krb5_sendauth(context, &auth_context, (krb5_pointer) &socket,
222 "KPOPV1.0", client, server,
223 AP_OPTS_MUTUAL_REQUIRED,
224 NULL, /* no data to checksum */
225 0, /* no creds, use ccache instead */
229 NULL); /* don't need reply */
231 krb5_free_principal(context, server);
232 krb5_free_principal(context, client);
233 krb5_auth_con_free(context, auth_context);
237 if (err_ret && err_ret->e_text) {
238 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
239 error_message(retval),
242 if (err_ret && err_ret->text.length) {
243 report(stderr, GT_("krb5_sendauth: %s [server says '%*s'] \n"),
244 error_message(retval),
245 err_ret->text.length,
248 krb5_free_error(context, err_ret);
250 report(stderr, "krb5_sendauth: %s\n", error_message(retval));
256 #endif /* KERBEROS_V5 */
258 static void clean_skipped_list(struct idlist **skipped_list)
259 /* struct "idlist" contains no "prev" ptr; we must remove unused items first */
261 struct idlist *current=NULL, *prev=NULL, *tmp=NULL, *head=NULL;
262 prev = current = head = *skipped_list;
268 /* if item has no reference, remove it */
269 if (current && current->val.status.mark == 0)
271 if (current == head) /* remove first item (head) */
273 head = current->next;
274 if (current->id) free(current->id);
276 prev = current = head;
278 else /* remove middle/last item */
282 if (current->id) free(current->id);
287 else /* skip this item */
290 current = current->next;
294 *skipped_list = head;
297 static void send_size_warnings(struct query *ctl)
298 /* send warning mail with skipped msg; reset msg count when user notified */
301 int msg_to_send = FALSE;
302 struct idlist *head=NULL, *current=NULL;
303 int max_warning_poll_count;
309 /* don't start a notification message unless we need to */
310 for (current = head; current; current = current->next)
311 if (current->val.status.num == 0 && current->val.status.mark)
317 * There's no good way to recover if we can't send notification mail,
318 * but it's not a disaster, either, since the skipped mail will not
321 if (open_warning_by_mail(ctl, (struct msgblk *)NULL))
324 GT_("Subject: Fetchmail oversized-messages warning.\r\n"
326 "The following oversized messages remain on the mail server %s:"),
327 ctl->server.pollname);
329 if (run.poll_interval == 0)
330 max_warning_poll_count = 0;
332 max_warning_poll_count = ctl->warnings/run.poll_interval;
334 /* parse list of skipped msg, adding items to the mail */
335 for (current = head; current; current = current->next)
337 if (current->val.status.num == 0 && current->val.status.mark)
339 nbr = current->val.status.mark;
340 size = atoi(current->id);
342 GT_("\t%d msg %d octets long skipped by fetchmail.\r\n"),
345 current->val.status.num++;
346 current->val.status.mark = 0;
348 if (current->val.status.num >= max_warning_poll_count)
349 current->val.status.num = 0;
352 close_warning_by_mail(ctl, (struct msgblk *)NULL);
355 static void mark_oversized(struct query *ctl, int num, int size)
356 /* mark a message oversized */
358 struct idlist *current=NULL, *tmp=NULL;
362 /* convert size to string */
364 snprintf(sizestr, sizeof(sizestr),
367 #endif /* HAVE_SNPRINTF */
370 /* build a list of skipped messages
371 * val.id = size of msg (string cnvt)
372 * val.status.num = warning_poll_count
373 * val.status.mask = nbr of msg this size
376 current = ctl->skipped;
378 /* initialise warning_poll_count to the
379 * current value so that all new msg will
380 * be included in the next mail
382 cnt = current ? current->val.status.num : 0;
384 /* if entry exists, increment the count */
385 if (current && str_in_list(¤t, sizestr, FALSE))
387 for ( ; current; current = current->next)
389 if (strcmp(current->id, sizestr) == 0)
391 current->val.status.mark++;
396 /* otherwise, create a new entry */
397 /* initialise with current poll count */
400 tmp = save_str(&ctl->skipped, sizestr, 1);
401 tmp->val.status.num = cnt;
405 static int fetch_messages(int mailserver_socket, struct query *ctl,
406 int count, int *msgsizes, int *msgcodes, int maxfetch,
407 int *fetches, int *dispatches, int *deletions)
408 /* fetch messages in lockstep mode */
412 for (num = 1; num <= count; num++)
414 flag suppress_delete = FALSE;
415 flag suppress_forward = FALSE;
416 flag suppress_readbody = FALSE;
417 flag retained = FALSE;
419 if (msgcodes[num-1] < 0)
421 if ((msgcodes[num-1] == MSGLEN_TOOLARGE) && !check_only)
422 mark_oversized(ctl, num, msgsizes[num-1]);
423 if (outlevel > O_SILENT)
426 GT_("skipping message %s@%s:%d (%d octets)"),
427 ctl->remotename, ctl->server.truename, num,
429 switch (msgcodes[num-1])
433 * Invalid lengths are produced by Post Office/NT's
434 * annoying habit of randomly prepending bogus
435 * LIST items of length -1. Patrick Audley
436 * <paudley@pobox.com> tells us: LIST shows a
437 * size of -1, RETR and TOP return "-ERR
438 * System error - couldn't open message", and
439 * DELE succeeds but doesn't actually delete
442 report_build(stdout, GT_(" (length -1)"));
444 case MSGLEN_TOOLARGE:
446 GT_(" (oversized, %d octets)"),
454 flag wholesize = !ctl->server.base_protocol->fetch_body;
456 /* request a message */
457 err = (ctl->server.base_protocol->fetch_headers)(mailserver_socket,ctl,num, &len);
458 if (err == PS_TRANSIENT) /* server is probably Exchange */
461 GT_("couldn't fetch headers, message %s@%s:%d (%d octets)"),
462 ctl->remotename, ctl->server.truename, num,
469 /* -1 means we didn't see a size in the response */
472 len = msgsizes[num - 1];
476 if (outlevel > O_SILENT)
478 report_build(stdout, GT_("reading message %s@%s:%d of %d"),
479 ctl->remotename, ctl->server.truename,
483 report_build(stdout, GT_(" (%d %soctets)"),
484 len, wholesize ? "" : GT_("header "));
485 if (outlevel >= O_VERBOSE)
486 report_complete(stdout, "\n");
488 report_complete(stdout, " ");
492 * Read the message headers and ship them to the
495 err = readheaders(mailserver_socket, len, msgsizes[num-1],
497 if (err == PS_RETAINED)
498 suppress_readbody = suppress_forward = suppress_delete = retained = TRUE;
499 else if (err == PS_TRANSIENT)
500 suppress_delete = suppress_forward = TRUE;
501 else if (err == PS_REFUSED)
502 suppress_forward = TRUE;
505 * readheaders does not read the body when it
506 * hits a non-header. It has been recently
507 * fixed to return PS_TRUNCATED (properly) when
508 * that happens, but apparently fixing that bug
509 * opened this one here (which looks like an
510 * inproper fix from some ancient thinko)
512 else if (err == PS_TRUNCATED)
513 suppress_readbody = TRUE;
517 else if (err && err != PS_TRUNCATED)
522 * If we're using IMAP4 or something else that
523 * can fetch headers separately from bodies,
524 * it's time to request the body now. This
525 * fetch may be skipped if we got an anti-spam
526 * or other PS_REFUSED error response during
529 if (ctl->server.base_protocol->fetch_body && !suppress_readbody)
531 if (outlevel >= O_VERBOSE && !isafile(1))
537 if ((err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num)))
540 if (!suppress_forward)
542 if ((err=(ctl->server.base_protocol->fetch_body)(mailserver_socket,ctl,num,&len)))
545 * Work around a bug in Novell's
546 * broken GroupWise IMAP server;
547 * its body FETCH response is missing
548 * the required length for the data
549 * string. This violates RFC2060.
552 len = msgsizes[num-1] - msgblk.msglen;
553 if (outlevel > O_SILENT && !wholesize)
554 report_complete(stdout,
555 GT_(" (%d body octets) "), len);
559 /* process the body now */
562 if (suppress_readbody)
568 err = readbody(mailserver_socket,
573 if (err == PS_TRANSIENT)
574 suppress_delete = suppress_forward = TRUE;
578 /* tell server we got it OK and resynchronize */
579 if (ctl->server.base_protocol->trail)
581 if (outlevel >= O_VERBOSE && !isafile(1))
587 err = (ctl->server.base_protocol->trail)(mailserver_socket, ctl, num);
593 /* count # messages forwarded on this pass */
594 if (!suppress_forward)
598 * Check to see if the numbers matched?
600 * Yes, some servers foo this up horribly.
601 * All IMAP servers seem to get it right, and
602 * so does Eudora QPOP at least in 2.xx
605 * Microsoft Exchange gets it completely
606 * wrong, reporting compressed rather than
607 * actual sizes (so the actual length of
608 * message is longer than the reported size).
609 * Another fine example of Microsoft brain death!
611 * Some older POP servers, like the old UCB
612 * POP server and the pre-QPOP QUALCOMM
613 * versions, report a longer size in the LIST
614 * response than actually gets shipped up.
615 * It's unclear what is going on here, as the
616 * QUALCOMM server (at least) seems to be
617 * reporting the on-disk size correctly.
619 if (msgblk.msglen != msgsizes[num-1])
621 if (outlevel >= O_DEBUG)
623 GT_("message %s@%s:%d was not the expected length (%d actual != %d expected)\n"),
624 ctl->remotename, ctl->server.truename, num,
625 msgblk.msglen, msgsizes[num-1]);
628 /* end-of-message processing starts here */
629 if (!close_sink(ctl, &msgblk, !suppress_forward))
632 suppress_delete = TRUE;
639 * At this point in flow of control, either
640 * we've bombed on a protocol error or had
641 * delivery refused by the SMTP server
642 * (unlikely -- I've never seen it) or we've
643 * seen `accepted for delivery' and the
644 * message is shipped. It's safe to mark the
645 * message seen and delete it on the server
650 * Tell the UID code we've seen this.
651 * Matthias Andree: only register the UID if we could actually
652 * forward this mail. If we omit this !suppress_delete check,
653 * fetchmail will never retry mail that the local listener
654 * refused temporarily.
656 if (ctl->newsaved && !suppress_delete)
660 for (sdp = ctl->newsaved; sdp; sdp = sdp->next)
661 if ((sdp->val.status.num == num) && (msgcodes[num-1] >= 0))
663 sdp->val.status.mark = UID_SEEN;
664 save_str(&ctl->oldsaved, sdp->id,UID_SEEN);
668 /* maybe we delete this message now? */
671 if (outlevel > O_SILENT)
672 report(stdout, GT_(" retained\n"));
674 else if (ctl->server.base_protocol->delete
676 && ((msgcodes[num-1] >= 0) ? !ctl->keep : ctl->flush))
679 if (outlevel > O_SILENT)
680 report_complete(stdout, GT_(" flushed\n"));
681 err = (ctl->server.base_protocol->delete)(mailserver_socket, ctl, num);
685 delete_str(&ctl->newsaved, num);
686 #endif /* POP3_ENABLE */
688 else if (outlevel > O_SILENT)
689 report_complete(stdout, GT_(" not flushed\n"));
691 /* perhaps this as many as we're ready to handle */
692 if (maxfetch && maxfetch <= *fetches && *fetches < count)
694 report(stdout, GT_("fetchlimit %d reached; %d messages left on server %s account %s\n"),
695 maxfetch, count - *fetches, ctl->server.truename, ctl->remotename);
703 static int do_session(ctl, proto, maxfetch)
704 /* retrieve messages from server using given protocol method table */
705 struct query *ctl; /* parsed options with merged-in defaults */
706 const struct method *proto; /* protocol method table */
707 const int maxfetch; /* maximum number of messages to fetch */
711 volatile int err, mailserver_socket = -1; /* pacifies -Wall */
713 int err, mailserver_socket = -1;
714 #endif /* HAVE_VOLATILE */
716 void (*pipesave)(int);
717 void (*alrmsave)(int);
719 ctl->server.base_protocol = proto;
723 init_transact(proto);
725 /* set up the server-nonresponse timeout */
726 alrmsave = signal(SIGALRM, timeout_handler);
727 mytimeout = ctl->server.timeout;
729 /* set up the broken-pipe timeout */
730 pipesave = signal(SIGPIPE, sigpipe_handler);
732 if ((js = setjmp(restart)))
734 #ifdef HAVE_SIGPROCMASK
736 * Don't rely on setjmp() to restore the blocked-signal mask.
737 * It does this under BSD but is required not to under POSIX.
739 * If your Unix doesn't have sigprocmask, better hope it has
740 * BSD-like behavior. Otherwise you may see fetchmail get
741 * permanently wedged after a second timeout on a bad read,
742 * because alarm signals were blocked after the first.
746 sigfillset(&allsigs);
747 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
748 #endif /* HAVE_SIGPROCMASK */
750 /* If there was a connect timeout, the socket should be closed.
751 * mailserver_socket_temp contains the socket to close.
753 mailserver_socket = mailserver_socket_temp;
755 if (js == THROW_SIGPIPE)
757 signal(SIGPIPE, SIG_IGN);
759 GT_("SIGPIPE thrown from an MDA or a stream socket error\n"));
764 else if (js == THROW_TIMEOUT)
766 if (phase == OPEN_WAIT)
768 GT_("timeout after %d seconds waiting to connect to server %s.\n"),
769 ctl->server.timeout, ctl->server.pollname);
770 else if (phase == SERVER_WAIT)
772 GT_("timeout after %d seconds waiting for server %s.\n"),
773 ctl->server.timeout, ctl->server.pollname);
774 else if (phase == FORWARDING_WAIT)
776 GT_("timeout after %d seconds waiting for %s.\n"),
778 ctl->mda ? "MDA" : "SMTP");
779 else if (phase == LISTENER_WAIT)
781 GT_("timeout after %d seconds waiting for listener to respond.\n"), ctl->server.timeout);
784 GT_("timeout after %d seconds.\n"), ctl->server.timeout);
787 * If we've exceeded our threshold for consecutive timeouts,
788 * try to notify the user, then mark the connection wedged.
789 * Don't do this if the connection can idle, though; idle
790 * timeouts just mean the frequency of mail is low.
792 if (timeoutcount > MAX_TIMEOUTS
793 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
796 GT_("Subject: fetchmail sees repeated timeouts\r\n"));
798 GT_("Fetchmail saw more than %d timeouts while attempting to get mail from %s@%s.\r\n"),
801 ctl->server.truename);
803 GT_("This could mean that your mailserver is stuck, or that your SMTP\r\n" \
804 "server is wedged, or that your mailbox file on the server has been\r\n" \
805 "corrupted by a server error. You can run `fetchmail -v -v' to\r\n" \
806 "diagnose the problem.\r\n\r\n" \
807 "Fetchmail won't poll this mailbox again until you restart it.\r\n"));
808 close_warning_by_mail(ctl, (struct msgblk *)NULL);
815 /* try to clean up all streams */
818 if (mailserver_socket != -1) {
819 cleanupSockClose(mailserver_socket);
820 mailserver_socket = -1;
825 char buf[MSGBUFSIZE+1], *realhost;
826 int count, new, bytes, deletions = 0;
827 int *msgsizes = (int *)NULL;
828 int *msgcodes = (int *)NULL;
830 int fetches, dispatches, oldphase;
831 #else /* INET6_ENABLE */
832 int port, fetches, dispatches, oldphase;
833 #endif /* INET6_ENABLE */
836 /* execute pre-initialization command, if any */
837 if (ctl->preconnect && (err = system(ctl->preconnect)))
840 GT_("pre-connection command failed with status %d\n"), err);
845 /* open a socket to the mail server */
848 set_timeout(mytimeout);
851 port = ctl->server.port ? ctl->server.port : ( ctl->use_ssl ? ctl->server.base_protocol->sslport : ctl->server.base_protocol->port );
853 port = ctl->server.port ? ctl->server.port : ctl->server.base_protocol->port;
855 #endif /* !INET6_ENABLE */
858 /* If either the pollname or vianame are "hesiod" we want to
859 lookup the user's hesiod pobox host */
860 if (!strcasecmp(ctl->server.queryname, "hesiod")) {
861 struct hes_postoffice *hes_p;
862 hes_p = hes_getmailhost(ctl->remotename);
863 if (hes_p != NULL && strcmp(hes_p->po_type, "POP") == 0) {
864 free(ctl->server.queryname);
865 ctl->server.queryname = xstrdup(hes_p->po_host);
867 free(ctl->server.via);
868 ctl->server.via = xstrdup(hes_p->po_host);
871 GT_("couldn't find HESIOD pobox for %s\n"),
877 #ifdef HAVE_GETHOSTBYNAME
879 * Canonicalize the server truename for later use. This also
880 * functions as a probe for whether the mailserver is accessible.
881 * We try it on each poll cycle until we get a result. This way,
882 * fetchmail won't fail if started up when the network is inaccessible.
884 if (ctl->server.dns && !ctl->server.trueaddr)
886 if (ctl->server.lead_server)
888 char *leadname = ctl->server.lead_server->truename;
890 /* prevent core dump from ill-formed or duplicate entry */
893 report(stderr, GT_("Lead server has no name.\n"));
900 ctl->server.truename = xstrdup(leadname);
904 struct hostent *namerec;
907 * Get the host's IP, so we can report it like this:
909 * Received: from hostname [10.0.0.1]
912 namerec = gethostbyname(ctl->server.queryname);
913 if (namerec == (struct hostent *)NULL)
916 GT_("couldn't find canonical DNS name of %s\n"),
917 ctl->server.pollname);
925 ctl->server.truename=xstrdup((char *)namerec->h_name);
926 ctl->server.trueaddr=xmalloc(namerec->h_length);
927 memcpy(ctl->server.trueaddr,
928 namerec->h_addr_list[0],
933 #endif /* HAVE_GETHOSTBYNAME */
935 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
937 /* allow time for the port to be set up if we have a plugin */
938 if (ctl->server.plugin)
941 if ((mailserver_socket = SockOpen(realhost,
942 ctl->server.service ? ctl->server.service : ( ctl->use_ssl ? ctl->server.base_protocol->sslservice : ctl->server.base_protocol->service ),
943 ctl->server.netsec, ctl->server.plugin)) == -1)
944 #else /* INET6_ENABLE */
945 if ((mailserver_socket = SockOpen(realhost, port, NULL, ctl->server.plugin)) == -1)
946 #endif /* INET6_ENABLE */
951 #ifdef HAVE_RES_SEARCH
952 if (err_no != 0 && h_errno != 0)
953 report(stderr, GT_("internal inconsistency\n"));
956 * Avoid generating a bogus error every poll cycle when we're
957 * in daemon mode but the connection to the outside world
960 if (!((err_no == EHOSTUNREACH || err_no == ENETUNREACH)
961 && run.poll_interval))
963 report_build(stderr, GT_("%s connection to %s failed"),
964 ctl->server.base_protocol->name, ctl->server.pollname);
965 #ifdef HAVE_RES_SEARCH
968 if (h_errno == HOST_NOT_FOUND)
969 strcpy(errbuf, GT_("host is unknown."));
971 else if (h_errno == NO_ADDRESS)
972 strcpy(errbuf, GT_("name is valid but has no IP address."));
974 else if (h_errno == NO_RECOVERY)
975 strcpy(errbuf, GT_("unrecoverable name server error."));
976 else if (h_errno == TRY_AGAIN)
977 strcpy(errbuf, GT_("temporary name server error."));
980 snprintf(errbuf, sizeof(errbuf),
983 #endif /* HAVE_SNPRINTF */
984 GT_("unknown DNS error %d."), h_errno);
987 #endif /* HAVE_RES_SEARCH */
988 strcpy(errbuf, strerror(err_no));
989 report_complete(stderr, ": %s\n", errbuf);
993 * Don't use this. It was an attempt to address Debian bug
994 * #47143 (Notify user by mail when pop server nonexistent).
995 * Trouble is, that doesn't work; you trip over the case
996 * where your SLIP or PPP link is down...
998 /* warn the system administrator */
999 if (open_warning_by_mail(ctl, (struct msgblk *)NULL) == 0)
1002 GT_("Subject: Fetchmail unreachable-server warning.\r\n"
1004 "Fetchmail could not reach the mail server %s:")
1005 ctl->server.pollname);
1006 stuff_warning(ctl, errbuf, ctl->server.pollname);
1007 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1011 #endif /* INET6_ENABLE */
1019 /* Save the socket opened. Usefull if Fetchmail hangs on SSLOpen
1020 * because the socket can be closed
1022 mailserver_socket_temp = mailserver_socket;
1023 set_timeout(mytimeout);
1025 /* perform initial SSL handshake on open connection */
1026 /* Note: We pass the realhost name over for certificate
1027 verification. We may want to make this configurable */
1028 if (ctl->use_ssl && SSLOpen(mailserver_socket,ctl->sslcert,ctl->sslkey,ctl->sslproto,ctl->sslcertck,
1029 ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
1031 report(stderr, GT_("SSL connection failed.\n"));
1035 /* Fetchmail didn't hang on SSLOpen,
1036 * then no need to set mailserver_socket_temp
1038 mailserver_socket_temp = -1;
1041 /* A timeout is still defined before SSLOpen,
1042 * then Fetchmail hanging on SSLOpen is handled.
1047 if (ctl->server.authenticate == A_KERBEROS_V4 && (strcasecmp(proto->name,"IMAP") != 0))
1049 set_timeout(mytimeout);
1050 err = kerberos_auth(mailserver_socket, ctl->server.truename,
1051 ctl->server.principal);
1056 #endif /* KERBEROS_V4 */
1059 if (ctl->server.authenticate == A_KERBEROS_V5)
1061 set_timeout(mytimeout);
1062 err = kerberos5_auth(mailserver_socket, ctl->server.truename);
1067 #endif /* KERBEROS_V5 */
1069 /* accept greeting message from mail server */
1070 err = (ctl->server.base_protocol->parse_response)(mailserver_socket, buf);
1074 /* try to get authorized to fetch mail */
1075 stage = STAGE_GETAUTH;
1076 if (ctl->server.base_protocol->getauth)
1078 err = (ctl->server.base_protocol->getauth)(mailserver_socket, ctl, buf);
1082 if (err == PS_LOCKBUSY)
1083 report(stderr, GT_("Lock-busy error on %s@%s\n"),
1085 ctl->server.truename);
1086 else if (err == PS_SERVBUSY)
1087 report(stderr, GT_("Server busy error on %s@%s\n"),
1089 ctl->server.truename);
1090 else if (err == PS_AUTHFAIL)
1092 report(stderr, GT_("Authorization failure on %s@%s%s\n"),
1094 ctl->server.truename,
1095 (ctl->wehaveauthed ? GT_(" (previously authorized)") : "")
1099 * If we're running in background, try to mail the
1100 * calling user a heads-up about the authentication
1101 * failure once it looks like this isn't a fluke
1102 * due to the server being temporarily inaccessible.
1103 * When we get third succesive failure, we notify the user
1104 * but only if we haven't already managed to get
1105 * authorization. After that, once we get authorization
1106 * we let the user know service is restored.
1108 if (run.poll_interval
1109 && ctl->wehavesentauthnote
1110 && ((ctl->wehaveauthed && ++ctl->authfailcount == 10)
1111 || ++ctl->authfailcount == 3)
1112 && !open_warning_by_mail(ctl, (struct msgblk *)NULL))
1114 ctl->wehavesentauthnote = 1;
1116 GT_("Subject: fetchmail authentication failed on %s@%s\r\n"),
1117 ctl->remotename, ctl->server.truename);
1119 GT_("Fetchmail could not get mail from %s@%s.\r\n"),
1121 ctl->server.truename);
1122 if (ctl->wehaveauthed)
1123 stuff_warning(ctl, GT_("\
1124 The attempt to get authorization failed.\r\n\
1125 Since we have already succeeded in getting authorization for this\r\n\
1126 connection, this is probably another failure mode (such as busy server)\r\n\
1127 that fetchmail cannot distinguish because the server didn't send a useful\r\n\
1130 However, if you HAVE changed you account details since starting the\r\n\
1131 fetchmail daemon, you need to stop the daemon, change your configuration\r\n\
1132 of fetchmail, and then restart the daemon.\r\n\
1134 The fetchmail daemon will continue running and attempt to connect\r\n\
1135 at each cycle. No future notifications will be sent until service\r\n\
1138 stuff_warning(ctl, GT_("\
1139 The attempt to get authorization failed.\r\n\
1140 This probably means your password is invalid, but some servers have\r\n\
1141 other failure modes that fetchmail cannot distinguish from this\r\n\
1142 because they don't send useful error messages on login failure.\r\n\
1144 The fetchmail daemon will continue running and attempt to connect\r\n\
1145 at each cycle. No future notifications will be sent until service\r\n\
1147 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1150 else if (err == PS_REPOLL)
1152 report(stderr, GT_("Repoll immediately on %s@%s\n"),
1154 ctl->server.truename);
1157 report(stderr, GT_("Unknown login or authentication error on %s@%s\n"),
1159 ctl->server.truename);
1166 * This connection has given us authorization at least once.
1168 * There are dodgy server (clubinternet.fr for example) that
1169 * give spurious authorization failures on patently good
1170 * account/password details, then 5 minutes later let you in!
1172 * This is meant to build in some tolerance of such nasty bits
1175 ctl->wehaveauthed = 1;
1176 /*if (ctl->authfailcount >= 3)*/
1177 if (ctl->wehavesentauthnote)
1179 ctl->wehavesentauthnote = 0;
1181 GT_("Authorization OK on %s@%s\n"),
1183 ctl->server.truename);
1184 if (!open_warning_by_mail(ctl, (struct msgblk *)NULL))
1187 GT_("Subject: fetchmail authentication OK on %s@%s\r\n"),
1188 ctl->remotename, ctl->server.truename);
1190 GT_("Fetchmail was able to log into %s@%s.\r\n"),
1192 ctl->server.truename);
1194 GT_("Service has been restored.\r\n"));
1195 close_warning_by_mail(ctl, (struct msgblk *)NULL);
1200 * Reporting only after the first three
1201 * consecutive failures, or ten consecutive
1202 * failures after we have managed to get
1205 ctl->authfailcount = 0;
1209 ctl->errcount = fetches = 0;
1211 /* now iterate over each folder selected */
1212 for (idp = ctl->mailboxes; idp; idp = idp->next)
1219 /* reset timeout, in case we did an IDLE */
1220 mytimeout = ctl->server.timeout;
1222 if (outlevel >= O_DEBUG)
1225 report(stdout, GT_("selecting or re-polling folder %s\n"), idp->id);
1227 report(stdout, GT_("selecting or re-polling default folder\n"));
1230 /* compute # of messages and number of new messages waiting */
1231 stage = STAGE_GETRANGE;
1232 err = (ctl->server.base_protocol->getrange)(mailserver_socket, ctl, idp->id, &count, &new, &bytes);
1236 /* show user how many messages we downloaded */
1238 #ifdef HAVE_SNPRINTF
1239 (void) snprintf(buf, sizeof(buf),
1242 #endif /* HAVE_SNPRINTF */
1243 GT_("%s at %s (folder %s)"),
1244 ctl->remotename, ctl->server.truename, idp->id);
1246 #ifdef HAVE_SNPRINTF
1247 (void) snprintf(buf, sizeof(buf),
1250 #endif /* HAVE_SNPRINTF */
1252 ctl->remotename, ctl->server.truename);
1253 if (outlevel > O_SILENT)
1255 if (count == -1) /* only used for ETRN */
1256 report(stdout, GT_("Polling %s\n"), ctl->server.truename);
1257 else if (count != 0)
1259 if (new != -1 && (count - new) > 0)
1260 report_build(stdout, GT_("%d %s (%d seen) for %s"),
1261 count, count > 1 ? GT_("messages") :
1265 report_build(stdout, GT_("%d %s for %s"),
1266 count, count > 1 ? GT_("messages") :
1267 GT_("message"), buf);
1269 report_complete(stdout, ".\n");
1271 report_complete(stdout, GT_(" (%d octets).\n"), bytes);
1275 /* these are pointless in normal daemon mode */
1276 if (pass == 1 && (run.poll_interval == 0 || outlevel >= O_VERBOSE))
1277 report(stdout, GT_("No mail for %s\n"), buf);
1281 /* very important, this is where we leave the do loop */
1287 if (new == -1 || ctl->fetchall)
1289 fetches = new; /* set error status correctly */
1291 * There used to be a `goto noerror' here, but this
1292 * prevented checking of multiple folders. This
1293 * comment is a reminder in case I introduced some
1294 * subtle bug by removing it...
1299 flag force_retrieval;
1303 * What forces this code is that in POP2 and
1304 * IMAP2bis you can't fetch a message without
1305 * having it marked `seen'. In POP3 and IMAP4, on the
1306 * other hand, you can (peek_capable is set by
1307 * each driver module to convey this; it's not a
1308 * method constant because of the difference between
1309 * IMAP2bis and IMAP4, and because POP3 doesn't peek
1310 * if fetchall is on).
1312 * The result of being unable to peek is that if there's
1313 * any kind of transient error (DNS lookup failure, or
1314 * sendmail refusing delivery due to process-table limits)
1315 * the message will be marked "seen" on the server without
1316 * having been delivered. This is not a big problem if
1317 * fetchmail is running in foreground, because the user
1318 * will see a "skipped" message when it next runs and get
1321 * But in daemon mode this leads to the message
1322 * being silently ignored forever. This is not
1325 * We compensate for this by checking the error
1326 * count from the previous pass and forcing all
1327 * messages to be considered new if it's nonzero.
1329 force_retrieval = !peek_capable && (ctl->errcount > 0);
1332 * Don't trust the message count passed by the server.
1333 * Without this check, it might be possible to do a
1334 * DNS-spoofing attack that would pass back a ridiculous
1335 * count, and allocate a malloc area that would overlap
1336 * a portion of the stack.
1338 if (count > INT_MAX/sizeof(int))
1340 report(stderr, GT_("bogus message count!"));
1341 return(PS_PROTOCOL);
1344 /* OK, we're going to gather size info next */
1345 xalloca(msgsizes, int *, sizeof(int) * count);
1346 xalloca(msgcodes, int *, sizeof(int) * count);
1347 for (i = 0; i < count; i++)
1348 msgcodes[i] = MSGLEN_UNKNOWN;
1351 * We need the size of each message before it's
1352 * loaded in order to pass it to the ESMTP SIZE
1353 * option. If the protocol has a getsizes method,
1354 * we presume this means it doesn't get reliable
1355 * sizes from message fetch responses.
1357 if (proto->getsizes)
1359 stage = STAGE_GETSIZES;
1360 err = (proto->getsizes)(mailserver_socket, count, msgsizes);
1367 for (i = 0; i < count; i++)
1368 bytes += msgsizes[i];
1372 /* mark some messages not to be retrieved */
1373 for (num = 1; num <= count; num++)
1375 if (NUM_NONZERO(ctl->limit) && (msgsizes[num-1] > ctl->limit))
1376 msgcodes[num-1] = MSGLEN_TOOLARGE;
1377 else if (ctl->fetchall || force_retrieval)
1379 else if (ctl->server.base_protocol->is_old && (ctl->server.base_protocol->is_old)(mailserver_socket,ctl,num))
1380 msgcodes[num-1] = MSGLEN_OLD;
1381 /* else if (msgsizes[num-1] == 512)
1382 msgcodes[num-1] = MSGLEN_OLD; (hmh) sample code to skip message */
1385 /* read, forward, and delete messages */
1386 stage = STAGE_FETCH;
1388 /* fetch in lockstep mode */
1389 err = fetch_messages(mailserver_socket, ctl,
1390 count, msgsizes, msgcodes,
1392 &fetches, &dispatches, &deletions);
1396 if (!check_only && ctl->skipped
1397 && run.poll_interval > 0 && !nodetach)
1399 clean_skipped_list(&ctl->skipped);
1400 send_size_warnings(ctl);
1405 * Only re-poll if we either had some actual forwards and
1406 * either allowed deletions and had no errors.
1407 * Otherwise it is far too easy to get into infinite loops.
1409 (dispatches && ctl->server.base_protocol->retry && !ctl->keep && !ctl->errcount);
1413 /* ordinary termination with no errors -- officially log out */
1414 err = (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1416 * Hmmmm...arguably this would be incorrect if we had fetches but
1417 * no dispatches (due to oversized messages, etc.)
1420 err = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1421 cleanupSockClose(mailserver_socket);
1425 /* we only get here on error */
1426 if (err != 0 && err != PS_SOCKET && err != PS_REPOLL)
1428 stage = STAGE_LOGOUT;
1429 (ctl->server.base_protocol->logout_cmd)(mailserver_socket, ctl);
1431 cleanupSockClose(mailserver_socket);
1434 msg = (const char *)NULL; /* sacrifice to -Wall */
1438 msg = GT_("socket");
1441 msg = GT_("missing or bad RFC822 header");
1447 msg = GT_("client/server synchronization");
1450 msg = GT_("client/server protocol");
1453 msg = GT_("lock busy on server");
1456 msg = GT_("SMTP transaction");
1459 msg = GT_("DNS lookup");
1462 report(stderr, GT_("undefined error\n"));
1465 /* no report on PS_MAXFETCH or PS_UNDEFINED or PS_AUTHFAIL */
1466 if (err==PS_SOCKET || err==PS_SYNTAX
1467 || err==PS_IOERR || err==PS_ERROR || err==PS_PROTOCOL
1468 || err==PS_LOCKBUSY || err==PS_SMTP || err==PS_DNS)
1472 if (phase == FORWARDING_WAIT || phase == LISTENER_WAIT)
1473 stem = GT_("%s error while delivering to SMTP host %s\n");
1475 stem = GT_("%s error while fetching from %s\n");
1476 report(stderr, stem, msg, ctl->server.pollname);
1480 /* execute wrapup command, if any */
1481 if (ctl->postconnect && (err = system(ctl->postconnect)))
1483 report(stderr, GT_("post-connection command failed with status %d\n"), err);
1484 if (err == PS_SUCCESS)
1488 signal(SIGALRM, alrmsave);
1489 signal(SIGPIPE, pipesave);
1493 int do_protocol(ctl, proto)
1494 /* retrieve messages from server using given protocol method table */
1495 struct query *ctl; /* parsed options with merged-in defaults */
1496 const struct method *proto; /* protocol method table */
1501 if (ctl->server.authenticate == A_KERBEROS_V4)
1503 report(stderr, GT_("Kerberos V4 support not linked.\n"));
1506 #endif /* KERBEROS_V4 */
1509 if (ctl->server.authenticate == A_KERBEROS_V5)
1511 report(stderr, GT_("Kerberos V5 support not linked.\n"));
1514 #endif /* KERBEROS_V5 */
1516 /* lacking methods, there are some options that may fail */
1519 /* check for unsupported options */
1522 GT_("Option --flush is not supported with %s\n"),
1526 else if (ctl->fetchall) {
1528 GT_("Option --all is not supported with %s\n"),
1533 if (!proto->getsizes && NUM_SPECIFIED(ctl->limit))
1536 GT_("Option --limit is not supported with %s\n"),
1542 * If no expunge limit or we do expunges within the driver,
1543 * then just do one session, passing in any fetchlimit.
1545 if (proto->retry || !NUM_SPECIFIED(ctl->expunge))
1546 return(do_session(ctl, proto, NUM_VALUE_OUT(ctl->fetchlimit)));
1548 * There's an expunge limit, and it isn't handled in the driver itself.
1549 * OK; do multiple sessions, each fetching a limited # of messages.
1550 * Stop if the total count of retrieved messages exceeds ctl->fetchlimit
1551 * (if it was nonzero).
1557 int expunge = NUM_VALUE_OUT(ctl->expunge);
1558 int fetchlimit = NUM_VALUE_OUT(ctl->fetchlimit);
1561 if (fetchlimit > 0 && (expunge == 0 || expunge > fetchlimit - totalcount))
1562 expunge = fetchlimit - totalcount;
1563 err = do_session(ctl, proto, expunge);
1564 totalcount += expunge;
1565 if (NUM_SPECIFIED(ctl->fetchlimit) && totalcount >= fetchlimit)
1567 if (err != PS_LOCKBUSY)
1569 else if (lockouts >= MAX_LOCKOUTS)
1571 else /* err == PS_LOCKBUSY */
1574 * Allow time for the server lock to release. if we
1575 * don't do this, we'll often hit a locked-mailbox
1576 * condition and fail.
1582 (err == PS_MAXFETCH || err == PS_LOCKBUSY);
1589 /* driver.c ends here */