2 * driver.c -- generic driver for mail fetch method protocols
4 * Copyright 1996 by Eric S. Raymond
6 * For license terms, see the file COPYING in this directory.
15 #if defined(STDC_HEADERS)
18 #if defined(HAVE_UNISTD_H)
21 #if defined(HAVE_STDARG_H)
26 #if defined(HAVE_ALLOCA_H)
32 #ifdef HAVE_GETHOSTBYNAME
35 #endif /* HAVE_GETHOSTBYNAME */
40 #include <netinet/in.h>
42 #endif /* KERBEROS_V4 */
44 #include "fetchmail.h"
48 /* BSD portability hack...I know, this is an ugly place to put it */
49 #if !defined(SIGCHLD) && defined(SIGCLD)
50 #define SIGCHLD SIGCLD
53 #define SMTP_PORT 25 /* standard SMTP service port */
55 extern char *strstr(); /* needed on sysV68 R3V7.1. */
57 int batchlimit; /* how often to tear down the delivery connection */
58 int fetchlimit; /* how often to tear down the server connection */
59 int batchcount; /* count of messages sent in current batch */
60 int peek_capable; /* can we peek for better error recovery? */
62 static const struct method *protocol;
63 static jmp_buf restart;
67 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
69 static char *shroud; /* string to shroud in debug output, if non-NULL */
70 static int mytimeout; /* value of nonreponse timeout */
72 static void vtalarm(int timeleft)
73 /* reset the nonresponse-timeout */
75 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_VIRTUAL, &ntimeout, (struct itimerval *)NULL);
83 static void vtalarm_handler (int signal)
84 /* handle server-timeout SIGVTALARM signal */
89 #ifdef HAVE_RES_SEARCH
92 static int is_host_alias(const char *name, struct query *ctl)
93 /* determine whether name is a DNS alias of the hostname */
96 struct mxentry *mxp, *mxrecords;
99 * The first two checks are optimizations that will catch a good
100 * many cases. (1) check against the hostname the user
101 * specified. Odds are good this will either be the mailserver's
102 * FQDN or a suffix of it with the mailserver's domain's default
103 * host name omitted. Then check the rest of the `also known as'
104 * cache accumulated by previous DNS checks. This cache is primed
105 * by the aka list option.
107 * (2) check against the mailserver's FQDN, in case
108 * it's not the same as the declared hostname.
110 * Either of these on a mail address is definitive. Only if the
111 * name doesn't match either is it time to call the bind library.
112 * If this happens odds are good we're looking at an MX name.
114 if (str_in_list(&ctl->server.lead_server->names, name))
116 else if (strcmp(name, ctl->server.canonical_name) == 0)
118 else if (!ctl->server.dns)
122 * We know DNS service was up at the beginning of this poll cycle.
123 * If it's down, our nameserver has crashed. We don't want to try
124 * delivering the current message or anything else from this
125 * mailbox until it's back up.
127 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
129 if (strcmp(ctl->server.canonical_name, he->h_name) == 0)
137 case HOST_NOT_FOUND: /* specified host is unknown */
138 case NO_ADDRESS: /* valid, but does not have an IP address */
141 case NO_RECOVERY: /* non-recoverable name server error */
142 case TRY_AGAIN: /* temporary error on authoritative server */
144 if (outlevel != O_SILENT)
145 putchar('\n'); /* terminate the progress message */
147 "nameserver failure while looking for `%s' during poll of %s.",
148 name, ctl->server.names->id);
150 longjmp(restart, 2); /* try again next poll cycle */
155 * We're only here if DNS was OK but the gethostbyname() failed
156 * with a HOST_NOT_FOUND or NO_ADDRESS error.
157 * Search for a name match on MX records pointing to the server.
160 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
164 case HOST_NOT_FOUND: /* specified host is unknown */
165 case NO_ADDRESS: /* valid, but does not have an IP address */
169 case NO_RECOVERY: /* non-recoverable name server error */
170 case TRY_AGAIN: /* temporary error on authoritative server */
173 "nameserver failure while looking for `%s' during poll of %s.",
174 name, ctl->server.names->id);
176 longjmp(restart, 2); /* try again next poll cycle */
182 for (mxp = mxrecords; mxp->name; mxp++)
183 if (strcmp(ctl->server.canonical_name, mxp->name) == 0)
189 /* add this name to relevant server's `also known as' list */
190 save_str(&ctl->server.lead_server->names, -1, name);
194 static void map_name(name, ctl, xmit_names)
195 /* add given name to xmit_names if it matches declared localnames */
196 const char *name; /* name to map */
197 struct query *ctl; /* list of permissible aliases */
198 struct idlist **xmit_names; /* list of recipient names parsed out */
202 lname = idpair_find(&ctl->localnames, name);
203 if (!lname && ctl->wildcard)
206 if (lname != (char *)NULL)
208 if (outlevel == O_VERBOSE)
209 error(0, 0, "mapped %s to local %s", name, lname);
210 save_str(xmit_names, -1, lname);
214 void find_server_names(hdr, ctl, xmit_names)
215 /* parse names out of a RFC822 header into an ID list */
216 const char *hdr; /* RFC822 header in question */
217 struct query *ctl; /* list of permissible aliases */
218 struct idlist **xmit_names; /* list of recipient names parsed out */
220 if (hdr == (char *)NULL)
226 if ((cp = nxtaddr(hdr)) != (char *)NULL)
230 if ((atsign = strchr(cp, '@')))
235 * Does a trailing segment of the hostname match something
236 * on the localdomains list? If so, save the whole name
239 for (idp = ctl->server.localdomains; idp; idp = idp->next)
243 rhs = atsign + 1 + (strlen(atsign) - strlen(idp->id));
244 if ((rhs[-1] == '.' || rhs[-1] == '@')
245 && strcmp(rhs, idp->id) == 0)
247 if (outlevel == O_VERBOSE)
248 error(0, 0, "passed through %s matching %s",
250 save_str(xmit_names, -1, cp);
256 * Check to see if the right-hand part is an alias
257 * or MX equivalent of the mailserver. If it's
258 * not, skip this name. If it is, we'll keep
259 * going and try to find a mapping to a client name.
261 if (!is_host_alias(atsign+1, ctl))
266 map_name(cp, ctl, xmit_names);
268 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
272 char *parse_received(struct query *ctl, char *bufp)
276 static char rbuf[HOSTLEN + USERNAMELEN + 4];
279 * Try to extract the real envelope addressee. We look here
280 * specifically for the mailserver's Received line.
281 * Note: this will only work for sendmail, or an MTA that
282 * shares sendmail's convention for embedding the envelope
283 * address in the Received line. Sendmail itself only
284 * does this when the mail has a single recipient.
286 if ((ok = strstr(bufp, "by ")) == (char *)NULL)
292 /* extract space-delimited token after "by " */
293 for (sp = ok + 3; isspace(*sp); sp++)
296 for (; !isspace(*sp); sp++)
301 * If it's a DNS name of the mail server, look for the
302 * recipient name after a following "for". Otherwise
305 if (is_host_alias(rbuf, ctl))
306 ok = strstr(sp, "for ");
319 while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
324 /* uh oh -- whitespace here can't be right! */
335 if (outlevel == O_VERBOSE)
336 error(0, 0, "found Received address `%s'", rbuf);
340 #endif /* HAVE_RES_SEARCH */
342 static FILE *smtp_open(struct query *ctl)
343 /* try to open a socket to the appropriate SMTP server for this query */
347 lead = ctl->lead_smtp; /* go to the SMTP leader for this query */
349 /* maybe it's time to close the socket in order to force delivery */
350 if (batchlimit && lead->smtp_sockfp && batchcount++ == batchlimit)
352 fclose(lead->smtp_sockfp);
353 lead->smtp_sockfp = (FILE *)NULL;
358 * RFC 1123 requires that the domain name in HELO address is a
359 * "valid principal domain name" for the client host. We
360 * violate this with malice aforethought in order to make the
361 * Received headers and logging look right.
363 * In fact this code relies on the RFC1123 requirement that the
364 * SMTP listener must accept messages even if verification of the
365 * HELO name fails (RFC1123 section 5.2.5, paragraph 2).
368 /* if no socket to this host is already set up, try to open ESMTP */
369 if (lead->smtp_sockfp == (FILE *)NULL)
371 if ((lead->smtp_sockfp = SockOpen(lead->smtphost, SMTP_PORT)) == (FILE *)NULL)
372 return((FILE *)NULL);
373 else if (SMTP_ok(lead->smtp_sockfp) != SM_OK
374 || SMTP_ehlo(lead->smtp_sockfp,
375 ctl->server.names->id,
376 &lead->server.esmtp_options) != SM_OK)
379 * RFC 1869 warns that some listeners hang up on a failed EHLO,
380 * so it's safest not to assume the socket will still be good.
382 fclose(lead->smtp_sockfp);
383 lead->smtp_sockfp = (FILE *)NULL;
387 /* if opening for ESMTP failed, try SMTP */
388 if (lead->smtp_sockfp == (FILE *)NULL)
390 if ((lead->smtp_sockfp = SockOpen(lead->smtphost, SMTP_PORT)) == (FILE *)NULL)
391 return((FILE *)NULL);
392 else if (SMTP_ok(lead->smtp_sockfp) != SM_OK
393 || SMTP_helo(lead->smtp_sockfp, ctl->server.names->id) != SM_OK)
395 fclose(lead->smtp_sockfp);
396 lead->smtp_sockfp = (FILE *)NULL;
400 return(lead->smtp_sockfp);
403 static int gen_readmsg(sockfp, len, delimited, ctl, realname)
404 /* read message content and ship to SMTP or MDA */
405 FILE *sockfp; /* to which the server is connected */
406 long len; /* length of message */
407 int delimited; /* does the protocol use a message delimiter? */
408 struct query *ctl; /* query control record */
409 char *realname; /* real name of host */
411 char buf [MSGBUFSIZE+1];
412 int from_offs, to_offs, cc_offs, bcc_offs, ctt_offs, env_offs;
413 char *headers, *received_for;
414 int n, oldlen, ch, sizeticker, delete_ok;
416 RETSIGTYPE (*sigchld)();
417 #ifdef HAVE_GETHOSTBYNAME
418 char rbuf[HOSTLEN + USERNAMELEN + 4];
419 #endif /* HAVE_GETHOSTBYNAME */
421 struct idlist *idp, *xmit_names;
422 int good_addresses, bad_addresses;
423 #ifdef HAVE_RES_SEARCH
424 int no_local_matches = FALSE;
425 #endif /* HAVE_RES_SEARCH */
430 /* read message headers */
431 headers = received_for = NULL;
432 from_offs = to_offs = cc_offs = bcc_offs = ctt_offs = env_offs = -1;
438 line = xmalloc(sizeof(buf));
441 if (!SockGets(buf, sizeof(buf)-1, sockfp))
443 vtalarm(ctl->server.timeout);
444 /* leave extra room for reply_hack to play with */
445 line = realloc(line, strlen(line) + strlen(buf) + HOSTLEN + 1);
447 if (line[0] == '\r' && line[1] == '\n')
450 /* we may need to grab RFC822 continuations */
451 ((ch = SockPeek(sockfp)) == ' ' || ch == '\t');
453 /* write the message size dots */
454 if ((outlevel>O_SILENT && outlevel<O_VERBOSE) && (n=strlen(line)) > 0)
457 while (sizeticker >= SIZETICKER)
460 sizeticker -= SIZETICKER;
465 /* check for end of headers; don't save terminating line */
466 if (line[0] == '\r' && line[1] == '\n')
473 reply_hack(line, realname);
477 oldlen = strlen(line);
478 headers = xmalloc(oldlen + 1);
479 (void) strcpy(headers, line);
487 newlen = oldlen + strlen(line);
488 headers = realloc(headers, newlen + 1);
491 strcpy(headers + oldlen, line);
493 line = headers + oldlen;
497 if (from_offs == -1 && !strncasecmp("From:", line, 5))
498 from_offs = (line - headers);
499 else if (from_offs == -1 && !strncasecmp("Resent-From:", line, 12))
500 from_offs = (line - headers);
501 else if (from_offs == -1 && !strncasecmp("Apparently-From:", line, 16))
502 from_offs = (line - headers);
504 else if (!strncasecmp("To:", line, 3))
505 to_offs = (line - headers);
507 else if (env_offs == -1 && !strncasecmp(ctl->server.envelope,
509 strlen(ctl->server.envelope)))
510 env_offs = (line - headers);
512 else if (!strncasecmp("Cc:", line, 3))
513 cc_offs = (line - headers);
515 else if (!strncasecmp("Bcc:", line, 4))
516 bcc_offs = (line - headers);
518 else if (!strncasecmp("Content-Transfer-Encoding:", line, 26))
519 ctt_offs = (line - headers);
521 #ifdef HAVE_RES_SEARCH
522 else if (MULTIDROP(ctl) && !received_for && !strncasecmp("Received:", line, 9))
523 received_for = parse_received(ctl, line);
524 #endif /* HAVE_RES_SEARCH */
528 * Hack time. If the first line of the message was blank, with no headers
529 * (this happens occasionally due to bad gatewaying software) cons up
530 * a set of fake headers.
532 * If you modify the fake header template below, be sure you don't
533 * make either From or To address @-less, otherwise the reply_hack
534 * logic will do bad things.
536 if (headers == (char *)NULL)
539 "From: <FETCHMAIL-DAEMON@%s>\r\nTo: %s@localhost\r\nSubject: Headerless mail from %s's mailbox on %s\r\n",
540 fetchmailhost, user, ctl->remotename, realname);
541 headers = xstrdup(buf);
545 * We can now process message headers before reading the text.
546 * In fact we have to, as this will tell us where to forward to.
549 /* cons up a list of local recipients */
550 xmit_names = (struct idlist *)NULL;
551 bad_addresses = good_addresses = 0;
552 #ifdef HAVE_RES_SEARCH
553 /* is this a multidrop box? */
556 if (env_offs > -1) /* We have the actual envelope addressee */
557 find_server_names(headers + env_offs, ctl, &xmit_names);
558 else if (received_for)
560 * We have the Received for addressee.
561 * It has to be a mailserver address, or we
562 * wouldn't have got here.
564 map_name(received_for, ctl, &xmit_names);
568 * We haven't extracted the envelope address.
569 * So check all the header addresses.
572 find_server_names(headers + to_offs, ctl, &xmit_names);
574 find_server_names(headers + cc_offs, ctl, &xmit_names);
576 find_server_names(headers + bcc_offs, ctl, &xmit_names);
580 no_local_matches = TRUE;
581 save_str(&xmit_names, -1, user);
582 if (outlevel == O_VERBOSE)
584 "no local matches, forwarding to %s",
588 else /* it's a single-drop box, use first localname */
589 #endif /* HAVE_RES_SEARCH */
590 save_str(&xmit_names, -1, ctl->localnames->id);
592 /* time to address the message */
593 if (ctl->mda) /* we have a declared MDA */
599 * We go through this in order to be able to handle very
600 * long lists of users and (re)implement %s.
602 for (idp = xmit_names; idp; idp = idp->next)
603 length += (strlen(idp->id) + 1);
604 names = (char *)alloca(length);
606 for (idp = xmit_names; idp; idp = idp->next)
608 strcat(names, idp->id);
611 cmd = (char *)alloca(strlen(ctl->mda) + length);
612 sprintf(cmd, ctl->mda, names);
613 if (outlevel == O_VERBOSE)
614 error(0, 0, "about to deliver with: %s", cmd);
618 * Arrange to run with user's permissions if we're root.
619 * This will initialize the ownership of any files the
620 * MDA creates properly. (The seteuid call is available
621 * under all BSDs and Linux)
624 #endif /* HAVE_SETEUID */
626 sinkfp = popen(cmd, "w");
629 /* this will fail quietly if we didn't start as root */
631 #endif /* HAVE_SETEUID */
635 error(0, 0, "MDA open failed");
639 sigchld = signal(SIGCHLD, SIG_DFL);
643 char *ap, *ctt, options[MSGBUFSIZE];
646 /* build a connection to the SMTP listener */
647 if (!ctl->mda && ((sinkfp = smtp_open(ctl)) == NULL))
649 free_str_list(&xmit_names);
650 error(0, 0, "SMTP connect failed");
655 * Compute ESMTP options. It's a kluge to use nxtaddr()
656 * here because the contents of the Content-Transfer-Encoding
657 * headers isn't semantically an address. But it has the
658 * desired tokenizing effect.
661 if ((ctl->server.esmtp_options & ESMTP_8BITMIME)
663 && (ctt = nxtaddr(headers + ctt_offs)))
664 if (!strcasecmp(ctt,"7BIT"))
665 sprintf(options, " BODY=7BIT", ctt);
666 else if (!strcasecmp(ctt,"8BIT"))
667 sprintf(options, " BODY=8BITMIME", ctt);
668 if ((ctl->server.esmtp_options & ESMTP_SIZE) && !delimited)
669 sprintf(options + strlen(options), " SIZE=%d", len);
672 * Try to get the SMTP listener to take the header
673 * From address as MAIL FROM (this makes the logging
674 * nicer). If it won't, fall back on the calling-user
675 * ID. This won't affect replies, which use the header
676 * From address anyway.
678 * RFC 1123 requires that the domain name part of the
679 * MAIL FROM address be "canonicalized", that is a
680 * FQDN or MX but not a CNAME. We'll assume the From
681 * header is already in this form here (it certainly
682 * is if rewrite is on). RFC 1123 is silent on whether
683 * a nonexistent hostname part is considered canonical.
685 * This is a potential problem if the MTAs further
686 * upstream didn't pass canonicalized From lines, *and*
687 * the local SMTP listener insists on them.
689 if (from_offs == -1 || !(ap = nxtaddr(headers + from_offs)))
691 if (SMTP_from(sinkfp, ap, options) != SM_OK)
693 int smtperr = atoi(smtp_response);
696 error(0, 0, "SMTP error: %s", smtp_response);
699 * There'a one problem with this flow of control;
700 * there's no way to avoid reading the whole message
701 * off the server, even if the MAIL FROM response
702 * tells us that it's just to be discarded. We could
703 * fix this under IMAP by reading headers first, then
704 * trying to issue the MAIL FROM, and *then* reading
705 * the body...but POP3 can't do this.
710 case 571: /* unsolicited email refused */
712 * SMTP listener explicitly refuses to deliver
713 * mail coming from this address, probably due
714 * to an anti-spam domain exclusion. Respect
715 * this. Don't try to ship the message, and
716 * don't prevent it from being deleted.
718 sinkfp = (FILE *)NULL;
721 case 452: /* insufficient system storage */
723 * Temporary out-of-queue-space condition on the
724 * ESMTP server. Don't try to ship the message,
725 * and suppress deletion so it can be retried on
726 * a future retrieval cycle.
729 sinkfp = (FILE *)NULL;
730 SMTP_rset(sockfp); /* required by RFC1870 */
733 case 552: /* message exceeds fixed maximum message size */
735 * Permanent no-go condition on the
736 * ESMTP server. Don't try to ship the message,
737 * and allow it to be deleted.
739 sinkfp = (FILE *)NULL;
740 SMTP_rset(sockfp); /* required by RFC1870 */
743 default: /* retry with invoking user's address */
744 if (SMTP_from(sinkfp, user, options) != SM_OK)
746 error(0,0,"SMTP error: %s", smtp_response);
747 return(PS_SMTP); /* should never happen */
753 * Now list the recipient addressees
755 * RFC 1123 requires that the domain name part of the
756 * RCPT TO address be "canonicalized", that is a FQDN
757 * or MX but not a CNAME. RFC1123 doesn't say whether
758 * the FQDN part can be null (as it frequently will be
759 * here), but it's hard to see how this could cause a
762 for (idp = xmit_names; idp; idp = idp->next)
763 if (SMTP_rcpt(sinkfp, idp->id) == SM_OK)
770 "SMTP listener doesn't like recipient address `%s'", idp->id);
772 if (!good_addresses && SMTP_rcpt(sinkfp, user) != SM_OK)
775 "can't even send to calling user!");
779 /* tell it we're ready to send data */
785 /* we may need to strip carriage returns */
790 for (sp = tp = headers; *sp; sp++)
797 /* write all the headers */
801 n = fwrite(headers, 1, strlen(headers), sinkfp);
803 n = SockWrite(headers, 1, strlen(headers), sinkfp);
809 error(0, errno, "writing RFC822 headers");
813 signal(SIGCHLD, sigchld);
817 else if (outlevel == O_VERBOSE)
823 /* write error notifications */
824 #ifdef HAVE_RES_SEARCH
825 if (no_local_matches || bad_addresses)
828 #endif /* HAVE_RES_SEARCH */
831 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
834 (void) strcpy(errhd, "X-Fetchmail-Warning: ");
835 #ifdef HAVE_RES_SEARCH
836 if (no_local_matches)
838 strcat(errhd, "no recipient addresses matched declared local names");
842 #endif /* HAVE_RES_SEARCH */
846 strcat(errhd, "SMTP listener rejected local recipient addresses: ");
847 errlen = strlen(errhd);
848 for (idp = xmit_names; idp; idp = idp->next)
850 errlen += strlen(idp->id) + 2;
852 errmsg = alloca(errlen+3);
853 (void) strcpy(errmsg, errhd);
854 for (idp = xmit_names; idp; idp = idp->next)
857 strcat(errmsg, idp->id);
859 strcat(errmsg, ", ");
863 strcat(errmsg, "\n");
865 /* we may need to strip carriage returns */
870 for (sp = tp = errmsg; *sp; sp++)
876 /* ship out the error line */
880 fwrite(errmsg, 1, strlen(errmsg), sinkfp);
882 SockWrite(errmsg, 1, strlen(errmsg), sinkfp);
886 free_str_list(&xmit_names);
888 /* issue the delimiter line */
894 SockWrite("\r\n", 1, 2, sinkfp);
898 * Body processing starts here
901 /* pass through the text lines */
902 while (delimited || len > 0)
904 if (!SockGets(buf, sizeof(buf)-1, sockfp))
906 vtalarm(ctl->server.timeout);
908 /* write the message size dots */
909 if ((n = strlen(buf)) > 0)
912 while (sizeticker >= SIZETICKER)
914 if (outlevel > O_SILENT)
916 sizeticker -= SIZETICKER;
921 /* check for end of message */
922 if (delimited && *buf == '.')
923 if (buf[1] == '\r' && buf[2] == '\n')
926 /* ship out the text line */
929 /* SMTP byte-stuffing */
934 SockWrite(buf, 1, 1, sinkfp);
936 /* we may need to strip carriage returns */
941 for (sp = tp = buf; *sp; sp++)
947 /* ship the text line */
949 n = fwrite(buf, 1, strlen(buf), sinkfp);
951 n = SockWrite(buf, 1, strlen(buf), sinkfp);
955 error(0, errno, "writing message text");
959 signal(SIGCHLD, sigchld);
963 else if (outlevel == O_VERBOSE)
969 * End-of-message processing starts here
972 if (outlevel == O_VERBOSE)
979 /* close the delivery pipe, we'll reopen before next message */
981 signal(SIGCHLD, sigchld);
984 error(0, 0, "MDA exited abnormally or returned nonzero status");
990 /* write message terminator */
991 if (SMTP_eom(sinkfp) != SM_OK)
993 error(0, 0, "SMTP listener refused delivery");
994 return(PS_TRANSIENT);
998 return(delete_ok ? PS_SUCCESS : PS_TRANSIENT);
1003 kerberos_auth (socket, canonical)
1004 /* authenticate to the server host using Kerberos V4 */
1005 int socket; /* socket to server host */
1006 const char *canonical; /* server name */
1008 char * host_primary;
1012 Key_schedule schedule;
1015 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
1016 rem = (krb_sendauth (0L, socket, ticket, "pop",
1018 ((char *) (krb_realmofhost (canonical))),
1019 ((unsigned long) 0),
1023 ((struct sockaddr_in *) 0),
1024 ((struct sockaddr_in *) 0),
1027 if (rem != KSUCCESS)
1029 error(0, 0, "kerberos error %s", (krb_get_err_text (rem)));
1034 #endif /* KERBEROS_V4 */
1036 int do_protocol(ctl, proto)
1037 /* retrieve messages from server using given protocol method table */
1038 struct query *ctl; /* parsed options with merged-in defaults */
1039 const struct method *proto; /* protocol method table */
1042 char *msg, *sp, *cp, realname[HOSTLEN];
1046 if (ctl->server.authenticate == A_KERBEROS)
1048 error(0, 0, "Kerberos support not linked.");
1051 #endif /* KERBEROS_V4 */
1053 /* lacking methods, there are some options that may fail */
1056 /* check for unsupported options */
1059 "Option --flush is not supported with %s",
1063 else if (ctl->fetchall) {
1065 "Option --all is not supported with %s",
1070 if (!proto->getsizes && ctl->limit)
1073 "Option --limit is not supported with %s",
1080 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
1082 error_init(poll_interval == 0 && !logfile);
1084 /* set up the server-nonresponse timeout */
1085 sigsave = signal(SIGVTALRM, vtalarm_handler);
1086 vtalarm(mytimeout = ctl->server.timeout);
1088 if ((js = setjmp(restart)) == 1)
1091 "timeout after %d seconds waiting for %s.",
1092 ctl->server.timeout, ctl->server.names->id);
1097 /* error message printed at point of longjmp */
1102 char buf [POPBUFSIZE+1];
1103 int *msgsizes, len, num, count, new, deletions = 0;
1105 /* execute pre-initialization command, if any */
1106 if (ctl->preconnect && (ok = system(ctl->preconnect)))
1108 sprintf(buf, "pre-connection command failed with status %d", ok);
1114 /* open a socket to the mail server */
1115 if (!(sockfp = SockOpen(ctl->server.names->id,
1116 ctl->server.port ? ctl->server.port : protocol->port)))
1118 #ifndef EHOSTUNREACH
1119 #define EHOSTUNREACH (-1)
1121 if (errno != EHOSTUNREACH)
1122 error(0, errno, "connecting to host");
1128 if (ctl->authenticate == A_KERBEROS)
1130 ok = kerberos_auth(fileno(sockfp), ctl->server.canonical_name);
1133 vtalarm(ctl->server.timeout);
1135 #endif /* KERBEROS_V4 */
1137 /* accept greeting message from mail server */
1138 ok = (protocol->parse_response)(sockfp, buf);
1141 vtalarm(ctl->server.timeout);
1144 * Try to parse the host's actual name out of the greeting
1145 * message. We do this so that the progress messages will
1146 * make sense even if the connection is indirected through
1147 * ssh. *Do* use this for hacking reply headers, but *don't*
1148 * use it for error logging, as the names in the log should
1149 * correlate directly back to rc file entries.
1151 * This assumes that the first space-delimited token found
1152 * that contains at least two dots (with the characters on
1153 * each side of the dot alphanumeric to exclude version
1154 * numbers) is the hostname. The hostname candidate may not
1155 * contain @ -- if it does it's probably a mailserver
1156 * maintainer's name. If no such token is found, fall back on
1157 * the .fetchmailrc id.
1160 for (cp = buf; *cp; cp++)
1164 case 0: /* skip to end of current token */
1169 case 1: /* look for blank-delimited token */
1177 case 2: /* look for first dot */
1180 else if (*cp == ' ')
1182 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1186 case 3: /* look for second dot */
1189 else if (*cp == ' ')
1191 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1195 case 4: /* look for trailing space */
1198 else if (*cp == ' ')
1209 char *tp = realname;
1216 strcpy(realname, ctl->server.names->id);
1218 /* try to get authorized to fetch mail */
1219 if (protocol->getauth)
1221 shroud = ctl->password;
1222 ok = (protocol->getauth)(sockfp, ctl, buf);
1223 shroud = (char *)NULL;
1228 error(0, 0, "Authorization failure on %s@%s",
1233 vtalarm(ctl->server.timeout);
1236 /* compute number of messages and number of new messages waiting */
1237 ok = (protocol->getrange)(sockfp, ctl, &count, &new);
1240 vtalarm(ctl->server.timeout);
1242 /* show user how many messages we downloaded */
1243 if (outlevel > O_SILENT)
1244 if (count == -1) /* only used for ETRN */
1245 error(0, 0, "Polling %s@%s",
1248 else if (count == 0)
1249 error(0, 0, "No mail at %s@%s",
1254 if (new != -1 && (count - new) > 0)
1255 error(0, 0, "%d message%s (%d seen) at %s@%s.",
1256 count, count > 1 ? "s" : "", count-new,
1260 error(0, 0, "%d message%s at %s@%s.",
1261 count, count > 1 ? "s" : "",
1266 /* we may need to get sizes in order to check message limits */
1267 msgsizes = (int *)NULL;
1268 if (!ctl->fetchall && proto->getsizes && ctl->limit)
1270 msgsizes = (int *)alloca(sizeof(int) * count);
1272 ok = (proto->getsizes)(sockfp, count, msgsizes);
1275 vtalarm(ctl->server.timeout);
1280 if (new == -1 || ctl->fetchall)
1282 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
1287 int force_retrieval, fetches;
1290 * What forces this code is that in POP3 and IMAP2BIS you can't
1291 * fetch a message without having it marked `seen'. In IMAP4,
1292 * on the other hand, you can (peek_capable is set to convey
1295 * The result of being unable to peek is that if there's
1296 * any kind of transient error (DNS lookup failure, or
1297 * sendmail refusing delivery due to process-table limits)
1298 * the message will be marked "seen" on the server without
1299 * having been delivered. This is not a big problem if
1300 * fetchmail is running in foreground, because the user
1301 * will see a "skipped" message when it next runs and get
1304 * But in daemon mode this leads to the message being silently
1305 * ignored forever. This is not acceptable.
1307 * We compensate for this by checking the error count from the
1308 * previous pass and forcing all messages to be considered new
1311 force_retrieval = !peek_capable && (ctl->errcount > 0);
1313 ctl->errcount = fetches = 0;
1315 /* read, forward, and delete messages */
1316 for (num = 1; num <= count; num++)
1318 int toolarge = msgsizes && (msgsizes[num-1] > ctl->limit);
1319 int fetch_it = ctl->fetchall ||
1320 (!toolarge && (force_retrieval || !(protocol->is_old && (protocol->is_old)(sockfp,ctl,num))));
1321 int suppress_delete = FALSE;
1323 /* we may want to reject this message if it's old */
1326 if (outlevel > O_SILENT)
1328 error_build("skipping message %d", num);
1330 error_build(" (oversized, %d bytes)", msgsizes[num-1]);
1335 /* request a message */
1336 ok = (protocol->fetch)(sockfp, ctl, num, &len);
1339 vtalarm(ctl->server.timeout);
1341 if (outlevel > O_SILENT)
1343 error_build("reading message %d", num);
1345 error_build(" (%d bytes)", len);
1346 if (outlevel == O_VERBOSE)
1347 error_complete(0, 0, "");
1352 /* read the message and ship it to the output sink */
1353 ok = gen_readmsg(sockfp,
1355 protocol->delimited,
1358 if (ok == PS_TRANSIENT)
1359 suppress_delete = TRUE;
1362 vtalarm(ctl->server.timeout);
1364 /* tell the server we got it OK and resynchronize */
1365 if (protocol->trail)
1367 ok = (protocol->trail)(sockfp, ctl, num);
1370 vtalarm(ctl->server.timeout);
1377 * At this point in flow of control, either we've bombed
1378 * on a protocol error or had delivery refused by the SMTP
1379 * server (unlikely -- I've never seen it) or we've seen
1380 * `accepted for delivery' and the message is shipped.
1381 * It's safe to mark the message seen and delete it on the
1385 /* maybe we delete this message now? */
1386 if (protocol->delete
1388 && (fetch_it ? !ctl->keep : ctl->flush))
1391 if (outlevel > O_SILENT)
1392 error_complete(0, 0, " flushed");
1393 ok = (protocol->delete)(sockfp, ctl, num);
1396 vtalarm(ctl->server.timeout);
1397 delete_str(&ctl->newsaved, num);
1399 else if (outlevel > O_SILENT)
1400 error_complete(0, 0, " not flushed");
1402 /* perhaps this as many as we're ready to handle */
1403 if (ctl->fetchlimit && ctl->fetchlimit <= fetches)
1407 ok = gen_transact(sockfp, protocol->exit_cmd);
1415 ok = gen_transact(sockfp, protocol->exit_cmd);
1424 vtalarm(ctl->server.timeout);
1425 if (ok != 0 && ok != PS_SOCKET)
1426 gen_transact(sockfp, protocol->exit_cmd);
1437 msg = "authorization";
1440 msg = "missing or bad RFC822 header";
1446 msg = "client/server synchronization";
1449 msg = "client/server protocol";
1452 msg = "SMTP transaction";
1455 error(0, 0, "undefined");
1458 if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
1459 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
1460 error(0, 0, "%s error while fetching from %s", msg, ctl->server.names->id);
1463 signal(SIGVTALRM, sigsave);
1467 #if defined(HAVE_STDARG_H)
1468 void gen_send(FILE *sockfp, char *fmt, ... )
1469 /* assemble command in printf(3) style and send to the server */
1471 void gen_send(sockfp, fmt, va_alist)
1472 /* assemble command in printf(3) style and send to the server */
1473 FILE *sockfp; /* socket to which server is connected */
1474 const char *fmt; /* printf-style format */
1478 char buf [POPBUFSIZE+1];
1481 if (protocol->tagged)
1482 (void) sprintf(buf, "%s ", GENSYM);
1486 #if defined(HAVE_STDARG_H)
1491 vsprintf(buf + strlen(buf), fmt, ap);
1494 strcat(buf, "\r\n");
1495 SockWrite(buf, 1, strlen(buf), sockfp);
1497 if (outlevel == O_VERBOSE)
1501 if (shroud && (cp = strstr(buf, shroud)))
1505 sp = cp + strlen(shroud);
1511 buf[strlen(buf)-2] = '\0';
1512 error(0, 0, "%s> %s", protocol->name, buf);
1516 int gen_recv(sockfp, buf, size)
1517 /* get one line of input from the server */
1518 FILE *sockfp; /* socket to which server is connected */
1519 char *buf; /* buffer to receive input */
1520 int size; /* length of buffer */
1522 if (!SockGets(buf, size, sockfp))
1526 if (buf[strlen(buf)-1] == '\n')
1527 buf[strlen(buf)-1] = '\0';
1528 if (buf[strlen(buf)-1] == '\r')
1529 buf[strlen(buf)-1] = '\r';
1530 if (outlevel == O_VERBOSE)
1531 error(0, 0, "%s< %s", protocol->name, buf);
1536 #if defined(HAVE_STDARG_H)
1537 int gen_transact(FILE *sockfp, char *fmt, ... )
1538 /* assemble command in printf(3) style, send to server, accept a response */
1540 int gen_transact(sockfp, fmt, va_alist)
1541 /* assemble command in printf(3) style, send to server, accept a response */
1542 FILE *sockfp; /* socket to which server is connected */
1543 const char *fmt; /* printf-style format */
1548 char buf [POPBUFSIZE+1];
1551 if (protocol->tagged)
1552 (void) sprintf(buf, "%s ", GENSYM);
1556 #if defined(HAVE_STDARG_H)
1561 vsprintf(buf + strlen(buf), fmt, ap);
1564 strcat(buf, "\r\n");
1565 SockWrite(buf, 1, strlen(buf), sockfp);
1567 if (outlevel == O_VERBOSE)
1571 if (shroud && (cp = strstr(buf, shroud)))
1575 sp = cp + strlen(shroud);
1581 buf[strlen(buf)-1] = '\0';
1582 error(0, 0, "%s> %s", protocol->name, buf);
1585 /* we presume this does its own response echoing */
1586 ok = (protocol->parse_response)(sockfp, buf);
1592 /* driver.c ends here */