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.
14 #if defined(STDC_HEADERS)
17 #if defined(HAVE_UNISTD_H)
20 #if defined(HAVE_STDARG_H)
25 #if defined(HAVE_ALLOCA_H)
28 #if defined(HAVE_SYS_ITIMER_H)
29 #include <sys/itimer.h>
34 #ifdef HAVE_GETHOSTBYNAME
37 #endif /* HAVE_GETHOSTBYNAME */
46 #include <netinet/in.h>
48 #endif /* KERBEROS_V4 */
49 #include "fetchmail.h"
53 /* BSD portability hack...I know, this is an ugly place to put it */
54 #if !defined(SIGCHLD) && defined(SIGCLD)
55 #define SIGCHLD SIGCLD
58 #define SMTP_PORT 25 /* standard SMTP service port */
60 extern char *strstr(); /* needed on sysV68 R3V7.1. */
62 int fetchlimit; /* how often to tear down the server connection */
63 int batchcount; /* count of messages sent in current batch */
64 bool peek_capable; /* can we peek for better error recovery? */
66 static const struct method *protocol;
67 static jmp_buf restart;
71 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
73 static char *shroud; /* string to shroud in debug output, if non-NULL */
74 static int mytimeout; /* value of nonreponse timeout */
76 static void set_timeout(int timeleft)
77 /* reset the nonresponse-timeout */
79 struct itimerval ntimeout;
81 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
82 ntimeout.it_value.tv_sec = timeleft;
83 ntimeout.it_value.tv_usec = 0;
84 setitimer(ITIMER_REAL, &ntimeout, (struct itimerval *)NULL);
87 static void timeout_handler (int signal)
88 /* handle server-timeout SIGALRM signal */
95 #define XMIT_ANTISPAM 3
96 static int accept_count, reject_count;
98 #ifdef HAVE_RES_SEARCH
101 static int is_host_alias(const char *name, struct query *ctl)
102 /* determine whether name is a DNS alias of the hostname */
105 struct mxentry *mxp, *mxrecords;
108 * The first two checks are optimizations that will catch a good
109 * many cases. (1) check against the hostname the user
110 * specified. Odds are good this will either be the mailserver's
111 * FQDN or a suffix of it with the mailserver's domain's default
112 * host name omitted. Then check the rest of the `also known as'
113 * cache accumulated by previous DNS checks. This cache is primed
114 * by the aka list option.
116 * (2) check against the mailserver's FQDN, in case
117 * it's not the same as the declared hostname.
119 * Either of these on a mail address is definitive. Only if the
120 * name doesn't match either is it time to call the bind library.
121 * If this happens odds are good we're looking at an MX name.
123 if (str_in_list(&ctl->server.lead_server->names, name))
125 else if (strcmp(name, ctl->server.canonical_name) == 0)
127 else if (!ctl->server.dns)
131 * We know DNS service was up at the beginning of this poll cycle.
132 * If it's down, our nameserver has crashed. We don't want to try
133 * delivering the current message or anything else from this
134 * mailbox until it's back up.
136 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
138 if (strcmp(ctl->server.canonical_name, he->h_name) == 0)
146 case HOST_NOT_FOUND: /* specified host is unknown */
147 case NO_ADDRESS: /* valid, but does not have an IP address */
150 case NO_RECOVERY: /* non-recoverable name server error */
151 case TRY_AGAIN: /* temporary error on authoritative server */
153 if (outlevel != O_SILENT)
154 putchar('\n'); /* terminate the progress message */
156 "nameserver failure while looking for `%s' during poll of %s.",
157 name, ctl->server.names->id);
163 * We're only here if DNS was OK but the gethostbyname() failed
164 * with a HOST_NOT_FOUND or NO_ADDRESS error.
165 * Search for a name match on MX records pointing to the server.
168 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
172 case HOST_NOT_FOUND: /* specified host is unknown */
173 case NO_ADDRESS: /* valid, but does not have an IP address */
177 case NO_RECOVERY: /* non-recoverable name server error */
178 case TRY_AGAIN: /* temporary error on authoritative server */
181 "nameserver failure while looking for `%s' during poll of %s.",
182 name, ctl->server.names->id);
189 for (mxp = mxrecords; mxp->name; mxp++)
190 if (strcmp(ctl->server.canonical_name, mxp->name) == 0)
196 /* add this name to relevant server's `also known as' list */
197 save_str(&ctl->server.lead_server->names, -1, name);
201 static void map_name(name, ctl, xmit_names)
202 /* add given name to xmit_names if it matches declared localnames */
203 const char *name; /* name to map */
204 struct query *ctl; /* list of permissible aliases */
205 struct idlist **xmit_names; /* list of recipient names parsed out */
209 lname = idpair_find(&ctl->localnames, name);
210 if (!lname && ctl->wildcard)
213 if (lname != (char *)NULL)
215 if (outlevel == O_VERBOSE)
216 error(0, 0, "mapped %s to local %s", name, lname);
217 save_str(xmit_names, XMIT_ACCEPT, lname);
222 void find_server_names(hdr, ctl, xmit_names)
223 /* parse names out of a RFC822 header into an ID list */
224 const char *hdr; /* RFC822 header in question */
225 struct query *ctl; /* list of permissible aliases */
226 struct idlist **xmit_names; /* list of recipient names parsed out */
228 if (hdr == (char *)NULL)
234 if ((cp = nxtaddr(hdr)) != (char *)NULL)
238 if ((atsign = strchr(cp, '@')))
243 * Does a trailing segment of the hostname match something
244 * on the localdomains list? If so, save the whole name
247 for (idp = ctl->server.localdomains; idp; idp = idp->next)
251 rhs = atsign + (strlen(atsign) - strlen(idp->id));
252 if ((rhs[-1] == '.' || rhs[-1] == '@')
253 && strcasecmp(rhs, idp->id) == 0)
255 if (outlevel == O_VERBOSE)
256 error(0, 0, "passed through %s matching %s",
258 save_str(xmit_names, XMIT_ACCEPT, cp);
265 * Check to see if the right-hand part is an alias
266 * or MX equivalent of the mailserver. If it's
267 * not, skip this name. If it is, we'll keep
268 * going and try to find a mapping to a client name.
270 if (!is_host_alias(atsign+1, ctl))
272 save_str(xmit_names, XMIT_REJECT, cp);
279 map_name(cp, ctl, xmit_names);
281 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
285 char *parse_received(struct query *ctl, char *bufp)
286 /* try to extract real addressee from the Received line */
289 static char rbuf[HOSTLEN + USERNAMELEN + 4];
292 * Try to extract the real envelope addressee. We look here
293 * specifically for the mailserver's Received line.
294 * Note: this will only work for sendmail, or an MTA that
295 * shares sendmail's convention for embedding the envelope
296 * address in the Received line. Sendmail itself only
297 * does this when the mail has a single recipient.
299 if ((ok = strstr(bufp, "by ")) == (char *)NULL)
305 /* extract space-delimited token after "by " */
306 for (sp = ok + 3; isspace(*sp); sp++)
309 for (; !isspace(*sp); sp++)
314 * If it's a DNS name of the mail server, look for the
315 * recipient name after a following "for". Otherwise
318 if (is_host_alias(rbuf, ctl))
319 ok = strstr(sp, "for ");
332 while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
337 /* uh oh -- whitespace here can't be right! */
348 if (outlevel == O_VERBOSE)
349 error(0, 0, "found Received address `%s'", rbuf);
353 #endif /* HAVE_RES_SEARCH */
355 int smtp_open(struct query *ctl)
356 /* try to open a socket to the appropriate SMTP server for this query */
360 /* maybe it's time to close the socket in order to force delivery */
361 if (ctl->batchlimit > 0 && (ctl->smtp_socket != -1) && batchcount++ == ctl->batchlimit)
363 close(ctl->smtp_socket);
364 ctl->smtp_socket = -1;
368 /* run down the SMTP hunt list looking for a server that's up */
369 for (idp = ctl->smtphunt; idp; idp = idp->next)
372 * RFC 1123 requires that the domain name in HELO address is a
373 * "valid principal domain name" for the client host. We
374 * violate this with malice aforethought in order to make the
375 * Received headers and logging look right.
377 * In fact this code relies on the RFC1123 requirement that the
378 * SMTP listener must accept messages even if verification of the
379 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
382 /* if no socket to this host is already set up, try to open ESMTP */
383 if (ctl->smtp_socket == -1)
385 if ((ctl->smtp_socket = SockOpen(idp->id,SMTP_PORT)) == -1)
387 else if (SMTP_ok(ctl->smtp_socket) != SM_OK
388 || SMTP_ehlo(ctl->smtp_socket,
389 ctl->server.names->id,
390 &ctl->server.esmtp_options) != SM_OK)
393 * RFC 1869 warns that some listeners hang up on a failed EHLO,
394 * so it's safest not to assume the socket will still be good.
396 close(ctl->smtp_socket);
397 ctl->smtp_socket = -1;
401 ctl->smtphost = idp->id;
406 /* if opening for ESMTP failed, try SMTP */
407 if (ctl->smtp_socket == -1)
409 if ((ctl->smtp_socket = SockOpen(idp->id,SMTP_PORT)) == -1)
411 else if (SMTP_ok(ctl->smtp_socket) != SM_OK
412 || SMTP_helo(ctl->smtp_socket, ctl->server.names->id) != SM_OK)
414 close(ctl->smtp_socket);
415 ctl->smtp_socket = -1;
419 ctl->smtphost = idp->id;
425 return(ctl->smtp_socket);
428 /* these are shared by readheaders and readbody */
430 static RETSIGTYPE (*sigchld)();
431 static int sizeticker;
433 static int readheaders(sock, len, ctl, realname)
434 /* read message headers and ship to SMTP or MDA */
435 int sock; /* to which the server is connected */
436 long len; /* length of message */
437 struct query *ctl; /* query control record */
438 char *realname; /* real name of host */
440 char buf[MSGBUFSIZE+1], return_path[MSGBUFSIZE+1];
441 int from_offs, ctt_offs, env_offs, addressoffs[128], next_address;
442 char *headers, *received_for;
443 int n, linelen, oldlen, ch, remaining;
445 struct idlist *idp, *xmit_names;
446 bool good_addresses, bad_addresses, has_nuls;
447 #ifdef HAVE_RES_SEARCH
448 bool no_local_matches = FALSE;
449 #endif /* HAVE_RES_SEARCH */
452 next_address = sizeticker = 0;
454 return_path[0] = '\0';
455 olderrs = ctl->errcount;
457 /* read message headers */
458 headers = received_for = NULL;
459 from_offs = ctt_offs = env_offs = -1;
461 for (remaining = len; remaining > 0; remaining -= linelen)
465 line = xmalloc(sizeof(buf));
469 if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1)
473 /* lines may not be properly CRLF terminated; fix this for qmail */
476 cp = buf + strlen(buf) - 1;
477 if (*cp == '\n' && (cp == buf || cp[-1] != '\r'))
485 set_timeout(ctl->server.timeout);
486 /* leave extra room for reply_hack to play with */
487 line = (char *) realloc(line, strlen(line) + strlen(buf) + HOSTLEN + 1);
489 if (line[0] == '\r' && line[1] == '\n')
492 /* we may need to grab RFC822 continuations */
493 ((ch = SockPeek(sock)) == ' ' || ch == '\t');
495 /* write the message size dots */
496 if ((outlevel > O_SILENT && outlevel < O_VERBOSE) && linelen > 0)
498 sizeticker += linelen;
499 while (sizeticker >= SIZETICKER)
502 sizeticker -= SIZETICKER;
506 if (linelen != strlen(line))
509 /* check for end of headers; don't save terminating line */
510 if (line[0] == '\r' && line[1] == '\n')
517 * This code prevents fetchmail from becoming an accessory after
518 * the fact to upstream sendmails with the `E' option on. This
519 * can result in an escaped Unix From_ line at the beginning of
520 * the headers. If fetchmail just passes it through, the client
521 * listener may think the message has *no* headers (since the first)
522 * line it sees doesn't look RFC822-conformant) and fake up a set.
524 * What the user would see in this case is bogus (synthesized)
525 * headers, followed by a blank line, followed by the >From,
526 * followed by the real headers, followed by a blank line,
529 * We forestall this lossage by tossing anything that looks
530 * like an escaped From_ line in headers. These aren't RFC822
531 * so our conscience is clear...
533 if (!strncasecmp(line, ">From ", 6))
540 * OK, this is messy. If we're forwarding by SMTP, it's the
541 * SMTP-receiver's job (according to RFC821, page 22, section
542 * 4.1.1) to generate a Return-Path line on final delivery.
543 * The trouble is, we've already got one because the
544 * mailserver's SMTP thought *it* was responsible for final
547 * Stash away the contents of Return-Path for use in generating
548 * MAIL FROM later on, then prevent the header from being saved
549 * with the others. In effect, we strip it off here.
551 * If the SMTP server conforms to the standards, and fetchmail gets the
552 * envelope sender from the Return-Path, the new Return-Path should be
553 * exactly the same as the original one.
555 if (!ctl->mda && !strncasecmp("Return-Path:", line, 12))
557 strcpy(return_path, nxtaddr(line));
563 reply_hack(line, realname);
567 oldlen = strlen(line);
568 headers = xmalloc(oldlen + 1);
569 (void) strcpy(headers, line);
577 newlen = oldlen + strlen(line);
578 headers = (char *) realloc(headers, newlen + 1);
581 strcpy(headers + oldlen, line);
583 line = headers + oldlen;
587 if (from_offs == -1 && !strncasecmp("From:", line, 5))
588 from_offs = (line - headers);
589 else if (from_offs == -1 && !strncasecmp("Resent-From:", line, 12))
590 from_offs = (line - headers);
591 else if (from_offs == -1 && !strncasecmp("Apparently-From:", line, 16))
592 from_offs = (line - headers);
594 else if (ctl->server.envelope != STRING_DISABLED && env_offs == -1
595 && !strncasecmp(ctl->server.envelope,
597 strlen(ctl->server.envelope)))
598 env_offs = (line - headers);
600 else if (!strncasecmp("To:", line, 3))
602 if (next_address >= sizeof(addressoffs)/sizeof(addressoffs[0]))
603 error(PS_UNDEFINED, errno, "too many destination headers");
604 addressoffs[next_address++] = (line - headers);
607 else if (!strncasecmp("Cc:", line, 3))
609 if (next_address >= sizeof(addressoffs)/sizeof(addressoffs[0]))
610 error(PS_UNDEFINED, errno, "too many destination headers");
611 addressoffs[next_address++] = (line - headers);
614 else if (!strncasecmp("Bcc:", line, 4))
616 if (next_address >= sizeof(addressoffs)/sizeof(addressoffs[0]))
617 error(PS_UNDEFINED, errno, "too many destination headers");
618 addressoffs[next_address++] = (line - headers);
621 else if (!strncasecmp("Content-Transfer-Encoding:", line, 26))
622 ctt_offs = (line - headers);
624 #ifdef HAVE_RES_SEARCH
625 else if (ctl->server.envelope != STRING_DISABLED && MULTIDROP(ctl) && !received_for && !strncasecmp("Received:", line, 9))
626 received_for = parse_received(ctl, line);
627 #endif /* HAVE_RES_SEARCH */
631 * Hack time. If the first line of the message was blank, with no headers
632 * (this happens occasionally due to bad gatewaying software) cons up
633 * a set of fake headers.
635 * If you modify the fake header template below, be sure you don't
636 * make either From or To address @-less, otherwise the reply_hack
637 * logic will do bad things.
639 if (headers == (char *)NULL)
642 "From: <FETCHMAIL-DAEMON@%s>\r\nTo: %s@localhost\r\nSubject: Headerless mail from %s's mailbox on %s\r\n",
643 fetchmailhost, user, ctl->remotename, realname);
644 headers = xstrdup(buf);
648 * We can now process message headers before reading the text.
649 * In fact we have to, as this will tell us where to forward to.
652 /* cons up a list of local recipients */
653 xmit_names = (struct idlist *)NULL;
654 bad_addresses = good_addresses = accept_count = reject_count = 0;
655 #ifdef HAVE_RES_SEARCH
656 /* is this a multidrop box? */
659 if (env_offs > -1) /* We have the actual envelope addressee */
660 find_server_names(headers + env_offs, ctl, &xmit_names);
661 else if (received_for)
663 * We have the Received for addressee.
664 * It has to be a mailserver address, or we
665 * wouldn't have got here.
667 map_name(received_for, ctl, &xmit_names);
673 * We haven't extracted the envelope address.
674 * So check all the header addresses.
676 for (i = 0; i < next_address; i++)
677 find_server_names(headers + addressoffs[i], ctl, &xmit_names);
681 no_local_matches = TRUE;
682 save_str(&xmit_names, XMIT_ACCEPT, user);
683 if (outlevel == O_VERBOSE)
685 "no local matches, forwarding to %s",
689 else /* it's a single-drop box, use first localname */
690 #endif /* HAVE_RES_SEARCH */
691 save_str(&xmit_names, XMIT_ACCEPT, ctl->localnames->id);
695 * Time to either address the message or decide we can't deliver it yet.
697 if (ctl->errcount > olderrs) /* there were DNS errors above */
699 if (outlevel == O_VERBOSE)
700 error(0,0, "forwarding and deletion suppressed due to DNS errors");
702 return(PS_TRANSIENT);
704 else if (ctl->mda) /* we have a declared MDA */
710 * We go through this in order to be able to handle very
711 * long lists of users and (re)implement %s.
713 for (idp = xmit_names; idp; idp = idp->next)
714 if (idp->val.num == XMIT_ACCEPT)
715 length += (strlen(idp->id) + 1);
716 names = (char *)alloca(length);
718 for (idp = xmit_names; idp; idp = idp->next)
719 if (idp->val.num == XMIT_ACCEPT)
721 strcat(names, idp->id);
724 cmd = (char *)alloca(strlen(ctl->mda) + length);
725 sprintf(cmd, ctl->mda, names);
726 if (outlevel == O_VERBOSE)
727 error(0, 0, "about to deliver with: %s", cmd);
731 * Arrange to run with user's permissions if we're root.
732 * This will initialize the ownership of any files the
733 * MDA creates properly. (The seteuid call is available
734 * under all BSDs and Linux)
737 #endif /* HAVE_SETEUID */
739 sinkfp = popen(cmd, "w");
742 /* this will fail quietly if we didn't start as root */
744 #endif /* HAVE_SETEUID */
748 error(0, -1, "MDA open failed");
752 sigchld = signal(SIGCHLD, SIG_DFL);
756 char *ap, *ctt, options[MSGBUFSIZE];
758 /* build a connection to the SMTP listener */
759 if ((smtp_open(ctl) == -1))
761 free_str_list(&xmit_names);
762 error(0, -1, "SMTP connect to %s failed",
763 ctl->smtphost ? ctl->smtphost : "localhost");
768 * Compute ESMTP options. It's a kluge to use nxtaddr()
769 * here because the contents of the Content-Transfer-Encoding
770 * headers isn't semantically an address. But it has the
771 * desired tokenizing effect.
774 if ((ctl->server.esmtp_options & ESMTP_8BITMIME)
776 && (ctt = nxtaddr(headers + ctt_offs)))
777 if (!strcasecmp(ctt,"7BIT"))
778 sprintf(options, " BODY=7BIT");
779 else if (!strcasecmp(ctt,"8BIT"))
780 sprintf(options, " BODY=8BITMIME");
781 if ((ctl->server.esmtp_options & ESMTP_SIZE))
782 sprintf(options + strlen(options), " SIZE=%ld", len);
785 * If there is a Return-Path address on the message, this was
786 * almost certainly the MAIL FROM address given the originating
787 * sendmail. This is the best thing to use for logging the
788 * message origin (it sets up the right behavior for bounces and
789 * mailing lists). Otherwise, take the From address.
791 * Try to get the SMTP listener to take the Return-Path or
792 * From address as MAIL FROM . If it won't, fall back on the
793 * calling-user ID. This won't affect replies, which use the
794 * header From address anyway.
796 * RFC 1123 requires that the domain name part of the
797 * MAIL FROM address be "canonicalized", that is a
798 * FQDN or MX but not a CNAME. We'll assume the From
799 * header is already in this form here (it certainly
800 * is if rewrite is on). RFC 1123 is silent on whether
801 * a nonexistent hostname part is considered canonical.
803 * This is a potential problem if the MTAs further upstream
804 * didn't pass canonicalized From/Return-Path lines, *and* the
805 * local SMTP listener insists on them.
810 else if (from_offs == -1 || !(ap = nxtaddr(headers + from_offs)))
812 if (SMTP_from(ctl->smtp_socket, ap, options) != SM_OK)
814 int smtperr = atoi(smtp_response);
817 * Suppress error message only if the response specifically
818 * means `excluded for policy reasons'. We *should* see
819 * an error when the return code is less specific.
821 if (smtperr >= 400 && smtperr != 571)
822 error(0, -1, "SMTP error: %s", smtp_response);
826 case 571: /* sendmail's "unsolicited email refused" */
827 case 501: /* exim's old antispam response */
828 case 550: /* exim's new antispam response (temporary) */
830 * SMTP listener explicitly refuses to deliver
831 * mail coming from this address, probably due
832 * to an anti-spam domain exclusion. Respect
833 * this. Don't try to ship the message, and
834 * don't prevent it from being deleted.
839 case 452: /* insufficient system storage */
841 * Temporary out-of-queue-space condition on the
842 * ESMTP server. Don't try to ship the message,
843 * and suppress deletion so it can be retried on
844 * a future retrieval cycle.
846 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
848 return(PS_TRANSIENT);
850 case 552: /* message exceeds fixed maximum message size */
852 * Permanent no-go condition on the
853 * ESMTP server. Don't try to ship the message,
854 * and allow it to be deleted.
856 SMTP_rset(ctl->smtp_socket); /* required by RFC1870 */
860 default: /* retry with invoking user's address */
861 if (SMTP_from(ctl->smtp_socket, user, options) != SM_OK)
863 error(0, -1, "SMTP error: %s", smtp_response);
865 return(PS_SMTP); /* should never happen */
871 * Now list the recipient addressees
873 * RFC 1123 requires that the domain name part of the
874 * RCPT TO address be "canonicalized", that is a FQDN
875 * or MX but not a CNAME. RFC1123 doesn't say whether
876 * the FQDN part can be null (as it frequently will be
877 * here), but it's hard to see how this could cause a
880 for (idp = xmit_names; idp; idp = idp->next)
881 if (idp->val.num == XMIT_ACCEPT)
882 if (SMTP_rcpt(ctl->smtp_socket, idp->id) == SM_OK)
887 idp->val.num = XMIT_ANTISPAM;
889 "SMTP listener doesn't like recipient address `%s'", idp->id);
891 if (!good_addresses && SMTP_rcpt(ctl->smtp_socket, user) != SM_OK)
894 "can't even send to calling user!");
899 /* tell it we're ready to send data */
900 SMTP_data(ctl->smtp_socket);
903 /* we may need to strip carriage returns */
908 for (sp = tp = headers; *sp; sp++)
915 /* write all the headers */
917 if (ctl->mda && sinkfp)
918 n = fwrite(headers, 1, strlen(headers), sinkfp);
919 else if (ctl->smtp_socket != -1)
920 n = SockWrite(ctl->smtp_socket, headers, strlen(headers));
925 error(0, errno, "writing RFC822 headers");
929 signal(SIGCHLD, sigchld);
933 else if (outlevel == O_VERBOSE)
936 /* write error notifications */
937 #ifdef HAVE_RES_SEARCH
938 if (no_local_matches || has_nuls || bad_addresses)
940 if (has_nuls || bad_addresses)
941 #endif /* HAVE_RES_SEARCH */
944 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
947 (void) strcpy(errhd, "X-Fetchmail-Warning: ");
948 #ifdef HAVE_RES_SEARCH
949 if (no_local_matches)
951 if (reject_count != 1)
952 strcat(errhd, "no recipient addresses matched declared local names");
955 for (idp = xmit_names; idp; idp = idp->next)
956 if (idp->val.num == XMIT_REJECT)
958 sprintf(errhd+strlen(errhd), "recipient address %s didn't match any local name", idp->id);
961 #endif /* HAVE_RES_SEARCH */
965 if (errhd[sizeof("X-Fetchmail-Warning: ")])
967 strcat(errhd, "message has embedded NULs");
972 if (errhd[sizeof("X-Fetchmail-Warning: ")])
974 strcat(errhd, "SMTP listener rejected local recipient addresses: ");
975 errlen = strlen(errhd);
976 for (idp = xmit_names; idp; idp = idp->next)
977 if (idp->val.num == XMIT_ANTISPAM)
978 errlen += strlen(idp->id) + 2;
980 errmsg = alloca(errlen+3);
981 (void) strcpy(errmsg, errhd);
982 for (idp = xmit_names; idp; idp = idp->next)
983 if (idp->val.num == XMIT_ANTISPAM)
985 strcat(errmsg, idp->id);
987 strcat(errmsg, ", ");
992 if (ctl->mda && !ctl->forcecr)
993 strcat(errmsg, "\n");
995 strcat(errmsg, "\r\n");
997 /* we may need to strip carriage returns */
1002 for (sp = tp = errmsg; *sp; sp++)
1008 /* ship out the error line */
1012 fwrite(errmsg, sizeof(char), strlen(errmsg), sinkfp);
1014 SockWrite(ctl->smtp_socket, errmsg, strlen(errmsg));
1018 free_str_list(&xmit_names);
1020 /* issue the delimiter line */
1021 if (sinkfp && ctl->mda)
1022 fputc('\n', sinkfp);
1023 else if (ctl->smtp_socket != -1)
1026 SockWrite(ctl->smtp_socket, "\n", 1);
1028 SockWrite(ctl->smtp_socket, "\r\n", 2);
1034 static int readbody(sock, ctl, forward, len, delimited)
1035 /* read and dispose of a message body presented on sock */
1036 struct query *ctl; /* query control record */
1037 int sock; /* to which the server is connected */
1038 int len; /* length of message */
1039 bool forward; /* TRUE to forward */
1040 bool delimited; /* does the protocol use a message delimiter? */
1043 char buf[MSGBUFSIZE+1], *cp;
1045 /* pass through the text lines */
1046 while (delimited || len > 0)
1048 if ((linelen = SockRead(sock, buf, sizeof(buf)-1)) == -1)
1050 set_timeout(ctl->server.timeout);
1052 /* write the message size dots */
1055 sizeticker += linelen;
1056 while (sizeticker >= SIZETICKER)
1058 if (outlevel > O_SILENT)
1060 sizeticker -= SIZETICKER;
1065 /* fix messages that have only \n line-termination (for qmail) */
1068 cp = buf + strlen(buf) - 1;
1069 if (*cp == '\n' && (cp == buf || cp[-1] != '\r'))
1077 /* check for end of message */
1078 if (delimited && *buf == '.')
1079 if (buf[1] == '\r' && buf[2] == '\n')
1082 /* ship out the text line */
1088 * SMTP byte-stuffing. We only do this if the protocol does *not*
1089 * use .<CR><LF> as EOM. If it does, the server will already have
1090 * decorated any . lines it sends back up.
1092 if (!delimited && *buf == '.')
1093 if (sinkfp && ctl->mda)
1095 else if (ctl->smtp_socket != -1)
1096 SockWrite(ctl->smtp_socket, buf, 1);
1098 /* we may need to strip carriage returns */
1103 for (sp = tp = buf; *sp; sp++)
1111 n = fwrite(buf, 1, strlen(buf), sinkfp);
1112 else if (ctl->smtp_socket != -1)
1113 n = SockWrite(ctl->smtp_socket, buf, strlen(buf));
1117 error(0, errno, "writing message text");
1121 signal(SIGCHLD, sigchld);
1125 else if (outlevel == O_VERBOSE)
1135 kerberos_auth (socket, canonical)
1136 /* authenticate to the server host using Kerberos V4 */
1137 int socket; /* socket to server host */
1138 const char *canonical; /* server name */
1140 char * host_primary;
1144 Key_schedule schedule;
1147 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
1148 rem = (krb_sendauth (0L, socket, ticket, "pop",
1150 ((char *) (krb_realmofhost (canonical))),
1151 ((unsigned long) 0),
1155 ((struct sockaddr_in *) 0),
1156 ((struct sockaddr_in *) 0),
1159 if (rem != KSUCCESS)
1161 error(0, -1, "kerberos error %s", (krb_get_err_text (rem)));
1166 #endif /* KERBEROS_V4 */
1168 int do_protocol(ctl, proto)
1169 /* retrieve messages from server using given protocol method table */
1170 struct query *ctl; /* parsed options with merged-in defaults */
1171 const struct method *proto; /* protocol method table */
1173 int ok, js, pst, sock = -1;
1174 char *msg, *cp, realname[HOSTLEN];
1178 if (ctl->server.preauthenticate == A_KERBEROS_V4)
1180 error(0, -1, "Kerberos V4 support not linked.");
1183 #endif /* KERBEROS_V4 */
1185 /* lacking methods, there are some options that may fail */
1188 /* check for unsupported options */
1191 "Option --flush is not supported with %s",
1195 else if (ctl->fetchall) {
1197 "Option --all is not supported with %s",
1202 if (!proto->getsizes && ctl->limit)
1205 "Option --limit is not supported with %s",
1212 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
1214 error_init(poll_interval == 0 && !logfile);
1216 /* set up the server-nonresponse timeout */
1217 sigsave = signal(SIGALRM, timeout_handler);
1218 set_timeout(mytimeout = ctl->server.timeout);
1220 if ((js = setjmp(restart)) == 1)
1223 "timeout after %d seconds waiting for %s.",
1224 ctl->server.timeout, ctl->server.names->id);
1225 if (ctl->smtp_socket != -1)
1226 close(ctl->smtp_socket);
1233 char buf [POPBUFSIZE+1], *sp;
1234 int *msgsizes, len, num, count, new, deletions = 0;
1238 /* execute pre-initialization command, if any */
1239 if (ctl->preconnect && (ok = system(ctl->preconnect)))
1241 sprintf(buf, "pre-connection command failed with status %d", ok);
1247 /* open a socket to the mail server */
1248 port = ctl->server.port ? ctl->server.port : protocol->port;
1249 if ((sock = SockOpen(ctl->server.names->id, port)) == -1)
1251 #ifndef EHOSTUNREACH
1252 #define EHOSTUNREACH (-1)
1254 if (outlevel == O_VERBOSE || errno != EHOSTUNREACH)
1255 error(0, errno, "connecting to host");
1261 if (ctl->server.preauthenticate == A_KERBEROS_V4)
1263 ok = kerberos_auth(sock, ctl->server.canonical_name);
1266 set_timeout(ctl->server.timeout);
1268 #endif /* KERBEROS_V4 */
1270 /* accept greeting message from mail server */
1271 ok = (protocol->parse_response)(sock, buf);
1274 set_timeout(ctl->server.timeout);
1277 * Try to parse the host's actual name out of the greeting
1278 * message. We do this so that the progress messages will
1279 * make sense even if the connection is indirected through
1280 * ssh. *Do* use this for hacking reply headers, but *don't*
1281 * use it for error logging, as the names in the log should
1282 * correlate directly back to rc file entries.
1284 * This assumes that the first space-delimited token found
1285 * that contains at least two dots (with the characters on
1286 * each side of the dot alphanumeric to exclude version
1287 * numbers) is the hostname. The hostname candidate may not
1288 * contain @ -- if it does it's probably a mailserver
1289 * maintainer's name. If no such token is found, fall back on
1290 * the .fetchmailrc id.
1293 sp = (char *)NULL; /* sacrifice to -Wall */
1294 for (cp = buf; *cp; cp++)
1298 case 0: /* skip to end of current token */
1303 case 1: /* look for blank-delimited token */
1311 case 2: /* look for first dot */
1314 else if (*cp == ' ')
1316 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1320 case 3: /* look for second dot */
1323 else if (*cp == ' ')
1325 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1329 case 4: /* look for trailing space */
1332 else if (*cp == ' ')
1343 char *tp = realname;
1350 strcpy(realname, ctl->server.names->id);
1352 /* try to get authorized to fetch mail */
1353 if (protocol->getauth)
1355 shroud = ctl->password;
1356 ok = (protocol->getauth)(sock, ctl, buf);
1357 shroud = (char *)NULL;
1362 error(0, -1, "Authorization failure on %s@%s",
1367 set_timeout(ctl->server.timeout);
1370 ctl->errcount = fetches = 0;
1372 /* now iterate over each folder selected */
1373 for (idp = ctl->mailboxes; idp; idp = idp->next)
1375 if (outlevel >= O_VERBOSE)
1377 error(0, 0, "selecting folder %s");
1379 error(0, 0, "selecting default folder");
1381 /* compute number of messages and number of new messages waiting */
1382 ok = (protocol->getrange)(sock, ctl, idp->id, &count, &new);
1385 set_timeout(ctl->server.timeout);
1387 /* show user how many messages we downloaded */
1389 (void) sprintf(buf, "%s@%s:%s",
1390 ctl->remotename, realname, idp->id);
1392 (void) sprintf(buf, "%s@%s", ctl->remotename, realname);
1393 if (outlevel > O_SILENT)
1394 if (count == -1) /* only used for ETRN */
1395 error(0, 0, "Polling %s", buf);
1396 else if (count == 0)
1397 error(0, 0, "No mail at %s", buf);
1400 if (new != -1 && (count - new) > 0)
1401 error(0, 0, "%d message%s (%d seen) at %s.",
1402 count, count > 1 ? "s" : "", count-new, buf);
1404 error(0, 0, "%d message%s at %s.",
1405 count, count > 1 ? "s" : "", buf);
1410 if (new == -1 || ctl->fetchall)
1412 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
1417 bool force_retrieval;
1420 * What forces this code is that in POP3 and IMAP2BIS you can't
1421 * fetch a message without having it marked `seen'. In IMAP4,
1422 * on the other hand, you can (peek_capable is set to convey
1425 * The result of being unable to peek is that if there's
1426 * any kind of transient error (DNS lookup failure, or
1427 * sendmail refusing delivery due to process-table limits)
1428 * the message will be marked "seen" on the server without
1429 * having been delivered. This is not a big problem if
1430 * fetchmail is running in foreground, because the user
1431 * will see a "skipped" message when it next runs and get
1434 * But in daemon mode this leads to the message being silently
1435 * ignored forever. This is not acceptable.
1437 * We compensate for this by checking the error count from the
1438 * previous pass and forcing all messages to be considered new
1441 force_retrieval = !peek_capable && (ctl->errcount > 0);
1444 * We may need to get sizes in order to check message
1445 * limits. Or it may be forced because the fetch methods
1446 * don't return reliable sizes.
1448 msgsizes = (int *)NULL;
1449 if (proto->getsizes && (proto->force_getsizes || ctl->limit > 0))
1451 msgsizes = (int *)alloca(sizeof(int) * count);
1453 ok = (proto->getsizes)(sock, count, msgsizes);
1456 set_timeout(ctl->server.timeout);
1459 /* read, forward, and delete messages */
1460 for (num = 1; num <= count; num++)
1462 bool toolarge = (ctl->limit > 0)
1463 && msgsizes && (msgsizes[num-1] > ctl->limit);
1464 bool fetch_it = !toolarge
1465 && (ctl->fetchall || force_retrieval || !(protocol->is_old && (protocol->is_old)(sock,ctl,num)));
1466 bool suppress_delete = FALSE;
1467 bool suppress_forward = FALSE;
1469 /* we may want to reject this message if it's old */
1472 if (outlevel > O_SILENT)
1474 error_build("skipping message %d", num);
1476 error_build(" (oversized, %d bytes)", msgsizes[num-1]);
1481 bool wholesize = !protocol->fetch_body;
1483 /* request a message */
1484 ok = (protocol->fetch_headers)(sock, ctl, num, &len);
1487 set_timeout(ctl->server.timeout);
1489 /* -1 means we didn't see a size in the response */
1490 if (len == -1 && msgsizes)
1492 len = msgsizes[num - 1];
1496 if (outlevel > O_SILENT)
1498 error_build("reading message %d", num);
1501 error_build(" (%d %sbytes)",
1502 len, wholesize ? "" : "header ");
1503 if (outlevel == O_VERBOSE)
1504 error_complete(0, 0, "");
1510 * Read the message headers and ship them to the
1513 ok = readheaders(sock, len, ctl, realname);
1514 if (ok == PS_TRANSIENT)
1515 suppress_delete = TRUE;
1516 else if (ok == PS_REFUSED)
1517 suppress_forward = TRUE;
1520 set_timeout(ctl->server.timeout);
1523 * If we're using IMAP4 or something else that
1524 * can fetch headers separately from bodies,
1525 * it's time to request the body now. This
1526 * fetch may be skipped if we got an anti-spam
1527 * or other PS_REFUSED error response during
1530 if (protocol->fetch_body)
1532 if ((ok = (protocol->trail)(sock, ctl, num)))
1534 set_timeout(ctl->server.timeout);
1536 if (!suppress_forward)
1538 if ((ok=(protocol->fetch_body)(sock,ctl,num,&len)))
1540 if (outlevel > O_SILENT && !msgsizes)
1541 error_build(" (%d body bytes) ", len);
1542 set_timeout(ctl->server.timeout);
1546 /* process the body now */
1553 protocol->delimited);
1554 if (ok == PS_TRANSIENT)
1555 suppress_delete = TRUE;
1558 set_timeout(ctl->server.timeout);
1560 /* tell server we got it OK and resynchronize */
1561 if (protocol->trail)
1563 ok = (protocol->trail)(sock, ctl, num);
1566 set_timeout(ctl->server.timeout);
1570 /* end-of-message processing starts here */
1571 if (outlevel == O_VERBOSE)
1572 fputc('\n', stderr);
1578 /* close the delivery pipe, we'll reopen before next message */
1579 rc = pclose(sinkfp);
1580 signal(SIGCHLD, sigchld);
1583 error(0, -1, "MDA exited abnormally or returned nonzero status");
1587 else if (!suppress_forward)
1589 /* write message terminator */
1590 if (SMTP_eom(ctl->smtp_socket) != SM_OK)
1592 error(0, -1, "SMTP listener refused delivery");
1594 suppress_delete = TRUE;
1602 * At this point in flow of control, either we've bombed
1603 * on a protocol error or had delivery refused by the SMTP
1604 * server (unlikely -- I've never seen it) or we've seen
1605 * `accepted for delivery' and the message is shipped.
1606 * It's safe to mark the message seen and delete it on the
1610 /* maybe we delete this message now? */
1611 if (protocol->delete
1613 && (fetch_it ? !ctl->keep : ctl->flush))
1616 if (outlevel > O_SILENT)
1617 error_complete(0, 0, " flushed");
1618 ok = (protocol->delete)(sock, ctl, num);
1621 set_timeout(ctl->server.timeout);
1622 delete_str(&ctl->newsaved, num);
1624 else if (outlevel > O_SILENT)
1625 error_complete(0, 0, " not flushed");
1627 /* perhaps this as many as we're ready to handle */
1628 if (ctl->fetchlimit > 0 && ctl->fetchlimit <= fetches)
1635 set_timeout(ctl->server.timeout);
1636 ok = gen_transact(sock, protocol->exit_cmd);
1638 ok = (fetches > 0) ? PS_SUCCESS : PS_NOMAIL;
1644 set_timeout(ctl->server.timeout);
1645 if (ok != 0 && ok != PS_SOCKET)
1646 gen_transact(sock, protocol->exit_cmd);
1651 msg = (char *)NULL; /* sacrifice to -Wall */
1658 msg = "authorization";
1661 msg = "missing or bad RFC822 header";
1667 msg = "client/server synchronization";
1670 msg = "client/server protocol";
1673 msg = "SMTP transaction";
1676 error(0, 0, "undefined");
1679 if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
1680 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
1681 error(0, -1, "%s error while fetching from %s", msg, ctl->server.names->id);
1684 signal(SIGALRM, sigsave);
1688 #if defined(HAVE_STDARG_H)
1689 void gen_send(int sock, char *fmt, ... )
1690 /* assemble command in printf(3) style and send to the server */
1692 void gen_send(sock, fmt, va_alist)
1693 /* assemble command in printf(3) style and send to the server */
1694 int sock; /* socket to which server is connected */
1695 const char *fmt; /* printf-style format */
1699 char buf [POPBUFSIZE+1];
1702 if (protocol->tagged)
1703 (void) sprintf(buf, "%s ", GENSYM);
1707 #if defined(HAVE_STDARG_H)
1712 vsprintf(buf + strlen(buf), fmt, ap);
1715 strcat(buf, "\r\n");
1716 SockWrite(sock, buf, strlen(buf));
1718 if (outlevel == O_VERBOSE)
1722 if (shroud && shroud[0] && (cp = strstr(buf, shroud)))
1726 sp = cp + strlen(shroud);
1732 buf[strlen(buf)-2] = '\0';
1733 error(0, 0, "%s> %s", protocol->name, buf);
1737 int gen_recv(sock, buf, size)
1738 /* get one line of input from the server */
1739 int sock; /* socket to which server is connected */
1740 char *buf; /* buffer to receive input */
1741 int size; /* length of buffer */
1743 if (SockRead(sock, buf, size) == -1)
1747 if (buf[strlen(buf)-1] == '\n')
1748 buf[strlen(buf)-1] = '\0';
1749 if (buf[strlen(buf)-1] == '\r')
1750 buf[strlen(buf)-1] = '\r';
1751 if (outlevel == O_VERBOSE)
1752 error(0, 0, "%s< %s", protocol->name, buf);
1757 #if defined(HAVE_STDARG_H)
1758 int gen_transact(int sock, char *fmt, ... )
1759 /* assemble command in printf(3) style, send to server, accept a response */
1761 int gen_transact(int sock, fmt, va_alist)
1762 /* assemble command in printf(3) style, send to server, accept a response */
1763 int sock; /* socket to which server is connected */
1764 const char *fmt; /* printf-style format */
1769 char buf [POPBUFSIZE+1];
1772 if (protocol->tagged)
1773 (void) sprintf(buf, "%s ", GENSYM);
1777 #if defined(HAVE_STDARG_H)
1782 vsprintf(buf + strlen(buf), fmt, ap);
1785 strcat(buf, "\r\n");
1786 SockWrite(sock, buf, strlen(buf));
1788 if (outlevel == O_VERBOSE)
1792 if (shroud && shroud[0] && (cp = strstr(buf, shroud)))
1796 sp = cp + strlen(shroud);
1802 buf[strlen(buf)-1] = '\0';
1803 error(0, 0, "%s> %s", protocol->name, buf);
1806 /* we presume this does its own response echoing */
1807 ok = (protocol->parse_response)(sock, buf);
1808 set_timeout(mytimeout);
1813 /* driver.c ends here */