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.
14 #if defined(STDC_HEADERS)
17 #if defined(HAVE_UNISTD_H)
20 #if defined(HAVE_STDARG_H)
28 #ifdef HAVE_GETHOSTBYNAME
31 #endif /* HAVE_GETHOSTBYNAME */
36 #include <netinet/in.h> /* must be included before "socket.h".*/
38 #endif /* KERBEROS_V4 */
40 #include "fetchmail.h"
43 #define SMTP_PORT 25 /* standard SMTP service port */
45 int batchlimit; /* how often to tear down the delivery connection */
46 int batchcount; /* count of messages sent in current batch */
48 static const struct method *protocol;
49 static jmp_buf restart;
53 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
55 static char *shroud; /* string to shroud in debug output, if non-NULL */
56 static int mytimeout; /* value of nonreponse timeout */
58 static int strcrlf(dst, src, count)
59 /* replace LFs with CR-LF; return length of string with replacements */
60 char *dst; /* new string with CR-LFs */
61 char *src; /* original string with LFs */
62 int count; /* length of src */
79 static void vtalarm(int timeleft)
80 /* reset the nonresponse-timeout */
82 struct itimerval ntimeout;
84 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_sec = 0;
85 ntimeout.it_value.tv_sec = timeleft;
86 ntimeout.it_value.tv_usec = 0;
87 setitimer(ITIMER_VIRTUAL, &ntimeout, (struct itimerval *)NULL);
90 static void vtalarm_handler (int signal)
91 /* handle server-timeout SIGVTALARM signal */
96 #ifdef HAVE_RES_SEARCH
99 static int is_host_alias(const char *name, struct query *ctl)
100 /* determine whether name is a DNS alias of the hostname */
103 struct mxentry *mxp, *mxrecords;
107 * The first two checks are optimizations that will catch a good
108 * many cases. First, check against the hostname the user specified.
109 * Odds are good this will either be the mailserver's FQDN or a
110 * suffix of it with the mailserver's domain's default host name
111 * omitted. Next, check against the mailserver's FQDN, in case
112 * it's not the same as the declared hostname.
114 * Either of these on a mail address is definitive. Only if the
115 * name doesn't match either is it time to call the bind library.
116 * If this happens odds are good we're looking at an MX name.
118 if (strcmp(name, ctl->servername) == 0)
120 else if (strcmp(name, ctl->canonical_name) == 0)
124 * We know DNS service was up at the beginning of this poll cycle.
125 * If it's down, our nameserver has crashed. We don't want to try
126 * delivering the current message or anything else from this
127 * mailbox until it's back up.
129 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
130 return(strcmp(ctl->canonical_name, he->h_name) == 0);
134 case HOST_NOT_FOUND: /* specified host is unknown */
135 case NO_ADDRESS: /* valid, but does not have an IP address */
138 case NO_RECOVERY: /* non-recoverable name server error */
139 case TRY_AGAIN: /* temporary error on authoritative server */
141 longjmp(restart, 2); /* try again next poll cycle */
146 * We're only here if DNS was OK but the gethostbyname() failed
147 * with a HOST_NOT_FOUND or NO_ADDRESS error.
148 * Search for a name match on MX records pointing to the server.
151 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
154 case HOST_NOT_FOUND: /* specified host is unknown */
157 case NO_ADDRESS: /* valid, but does not have an IP address */
158 for (mxp = mxrecords; mxp->name; mxp++)
159 if (strcmp(name, mxp->name) == 0)
163 case NO_RECOVERY: /* non-recoverable name server error */
164 case TRY_AGAIN: /* temporary error on authoritative server */
166 longjmp(restart, 2); /* try again next poll cycle */
173 void find_server_names(hdr, ctl, xmit_names)
174 /* parse names out of a RFC822 header into an ID list */
175 const char *hdr; /* RFC822 header in question */
176 struct query *ctl; /* list of permissible aliases */
177 struct idlist **xmit_names; /* list of recipient names parsed out */
179 if (hdr == (char *)NULL)
185 if ((cp = nxtaddr(hdr)) != (char *)NULL)
189 if ((atsign = strchr(cp, '@')))
192 * Address has an @. Check to see if the right-hand part
193 * is an alias or MX equivalent of the mailserver. If it's
194 * not, skip this name. If it is, we'll keep going and try
195 * to find a mapping to a client name.
197 if (!is_host_alias(atsign+1, ctl))
202 lname = idpair_find(&ctl->localnames, cp);
203 if (lname != (char *)NULL)
205 if (outlevel == O_VERBOSE)
207 "fetchmail: mapped %s to local %s\n",
209 save_uid(xmit_names, -1, lname);
212 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
215 #endif /* HAVE_RES_SEARCH */
217 static FILE *smtp_open(struct query *ctl)
218 /* try to open a socket to the appropriate SMTP server for this query */
220 ctl = ctl->leader; /* go to the SMTP leader for this query */
222 /* maybe it's time to close the socket in order to force delivery */
223 if (batchlimit && ctl->smtp_sockfp && batchcount++ == batchlimit)
225 fclose(ctl->smtp_sockfp);
226 ctl->smtp_sockfp = (FILE *)NULL;
230 /* if no socket to this host is already set up, try to open one */
231 if (ctl->smtp_sockfp == (FILE *)NULL)
233 if ((ctl->smtp_sockfp = Socket(ctl->smtphost, SMTP_PORT)) == (FILE *)NULL)
234 return((FILE *)NULL);
235 else if (SMTP_ok(ctl->smtp_sockfp, NULL) != SM_OK
236 || SMTP_helo(ctl->smtp_sockfp, ctl->servername) != SM_OK)
238 fclose(ctl->smtp_sockfp);
239 ctl->smtp_sockfp = (FILE *)NULL;
243 return(ctl->smtp_sockfp);
246 static int gen_readmsg (sockfp, len, delimited, ctl)
247 /* read message content and ship to SMTP or MDA */
248 FILE *sockfp; /* to which the server is connected */
249 long len; /* length of message */
250 int delimited; /* does the protocol use a message delimiter? */
251 struct query *ctl; /* query control record */
253 char buf [MSGBUFSIZE+1];
254 char *bufp, *headers, *fromhdr, *tohdr, *cchdr, *bcchdr;
255 int n, oldlen, mboxfd;
256 int inheaders,lines,sizeticker;
259 /* read the message content from the server */
261 headers = fromhdr = tohdr = cchdr = bcchdr = NULL;
265 while (delimited || len > 0)
267 if ((n = SockGets(buf,sizeof(buf),sockfp)) < 0)
269 vtalarm(ctl->timeout);
271 /* write the message size dots */
275 while (sizeticker >= SIZETICKER)
277 if (outlevel > O_SILENT)
279 sizeticker -= SIZETICKER;
284 if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
286 if (delimited && *bufp == '.') {
289 break; /* end of message */
296 reply_hack(bufp, ctl->servername);
300 oldlen = strlen(bufp);
301 headers = xmalloc(oldlen + 1);
302 (void) strcpy(headers, bufp);
310 * We deal with RFC822 continuation lines here.
311 * Replace previous '\n' with '\r' so nxtaddr
312 * and reply_hack will be able to see past it.
313 * (We know this is safe because SocketGets stripped
314 * out all carriage returns in the read loop above
315 * and we haven't reintroduced any since then.)
316 * We'll undo this before writing the header.
318 if (isspace(bufp[0]))
319 headers[oldlen-1] = '\r';
321 newlen = oldlen + strlen(bufp);
322 headers = realloc(headers, newlen + 1);
325 strcpy(headers + oldlen, bufp);
326 bufp = headers + oldlen;
330 if (!strncasecmp("From:", bufp, 5))
332 else if (!fromhdr && !strncasecmp("Resent-From:", bufp, 12))
334 else if (!fromhdr && !strncasecmp("Apparently-From:", bufp, 16))
336 else if (!strncasecmp("To:", bufp, 3))
338 else if (!strncasecmp("To:", bufp, 3))
340 else if (!strncasecmp("Cc:", bufp, 3))
342 else if (!strncasecmp("Bcc:", bufp, 4))
347 else if (headers) /* OK, we're at end of headers now */
350 struct idlist *idp, *xmit_names;
351 int good_addresses, bad_addresses;
352 #ifdef HAVE_RES_SEARCH
353 int no_local_matches = FALSE;
354 #endif /* HAVE_RES_SEARCH */
356 /* cons up a list of local recipients */
357 xmit_names = (struct idlist *)NULL;
358 bad_addresses = good_addresses = 0;
359 #ifdef HAVE_RES_SEARCH
360 /* is this a multidrop box? */
363 /* compute the local address list */
364 find_server_names(tohdr, ctl, &xmit_names);
365 find_server_names(cchdr, ctl, &xmit_names);
366 find_server_names(bcchdr, ctl, &xmit_names);
368 no_local_matches = TRUE;
370 else /* it's a single-drop box, use first localname */
371 #endif /* HAVE_RES_SEARCH */
374 save_uid(&xmit_names, -1, ctl->localnames->id);
377 /* if nothing supplied localnames, default appropriately */
380 save_uid(&xmit_names, -1, user);
381 if (outlevel == O_VERBOSE)
383 "fetchmail: no local matches, forwarding to %s\n",
387 /* time to address the message */
388 if (ctl->mda[0]) /* we have a declared MDA */
394 * We go through this in order to be able to handle very
395 * long lists of users and (re)implement %s.
397 for (idp = xmit_names; idp; idp = idp->next)
399 sp = sargv = (char **)alloca(sizeof(char **) * ctl->mda_argcount+nlocals+2);
400 for (i = 0; i < ctl->mda_argcount; i++)
401 if (strcmp("%s", ctl->mda_argv[i]))
402 *sp++ = ctl->mda_argv[i];
404 for (idp = xmit_names; idp; idp = idp->next)
410 * Arrange to run with user's permissions if we're root.
411 * This will initialize the ownership of any files the
412 * MDA creates properly. (The seteuid call is available
413 * under all BSDs and Linux)
416 #endif /* HAVE_SETEUID */
418 mboxfd = openmailpipe(sargv);
421 /* this will fail quietly if we didn't start as root */
423 #endif /* HAVE_SETEUID */
427 fprintf(stderr, "fetchmail: MDA open failed\n");
435 if (ctl->mda[0] == '\0' && ((sinkfp = smtp_open(ctl)) < 0))
437 free_uid_list(&xmit_names);
438 fprintf(stderr, "fetchmail: SMTP connect failed\n");
444 fprintf(stderr, "fetchmail: I see no From header\n");
448 if (SMTP_from(sinkfp, ap = nxtaddr(fromhdr)) != SM_OK)
450 fprintf(stderr, "fetchmail: SMTP listener doesn't like the From address `%s'\n", ap);
454 for (idp = xmit_names; idp; idp = idp->next)
455 if (SMTP_rcpt(sinkfp, idp->id) == SM_OK)
462 "fetchmail: SMTP listener doesn't like recipient address `%s'\n", idp->id);
464 if (!good_addresses && SMTP_rcpt(sinkfp, user) != SM_OK)
467 "fetchmail: can't even send to calling user!\n");
472 if (outlevel == O_VERBOSE)
473 fputs("SMTP> ", stderr);
476 /* change continuation markers back to regular newlines */
477 for (cp = headers; cp < headers + oldlen; cp++)
481 /* replace all LFs with CR-LF before sending to the SMTP server */
484 char *newheaders = xmalloc(1 + oldlen * 2);
486 oldlen = strcrlf(newheaders, headers, oldlen);
488 headers = newheaders;
491 /* write all the headers */
493 n = write(mboxfd,headers,oldlen);
495 n = SockWrite(headers, oldlen, sinkfp);
501 perror("fetchmail: writing RFC822 headers");
504 else if (outlevel == O_VERBOSE)
509 /* write error notifications */
510 #ifdef HAVE_RES_SEARCH
511 if (no_local_matches || bad_addresses)
514 #endif /* HAVE_RES_SEARCH */
517 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
520 (void) strcpy(errhd, "X-Fetchmail-Warning: ");
521 #ifdef HAVE_RES_SEARCH
522 if (no_local_matches)
524 strcat(errhd, "no recipient addresses matched declared local names");
528 #endif /* HAVE_RES_SEARCH */
532 strcat(errhd, "SMTP listener rejected local recipient addresses: ");
533 errlen = strlen(errhd);
534 for (idp = xmit_names; idp; idp = idp->next)
536 errlen += strlen(idp->id) + 2;
538 errmsg = alloca(errlen+3);
539 (void) strcpy(errmsg, errhd);
540 for (idp = xmit_names; idp; idp = idp->next)
543 strcat(errmsg, idp->id);
545 strcat(errmsg, ", ");
549 strcat(errmsg, "\n");
552 write(mboxfd, errmsg, strlen(errmsg));
554 SockWrite(errmsg, strlen(errmsg), sinkfp);
557 free_uid_list(&xmit_names);
560 /* SMTP byte-stuffing */
561 if (*bufp == '.' && ctl->mda[0] == 0)
562 SockWrite(".", 1, sinkfp);
564 /* replace all LFs with CR-LF in the line */
567 char *newbufp = xmalloc(1 + strlen(bufp) * 2);
569 strcrlf(newbufp, bufp, strlen(bufp));
573 /* ship out the text line */
575 n = write(mboxfd,bufp,strlen(bufp));
577 n = SockWrite(bufp, strlen(bufp), sinkfp);
583 perror("fetchmail: writing message text");
586 else if (outlevel == O_VERBOSE)
595 /* close the delivery pipe, we'll reopen before next message */
596 if (closemailpipe(mboxfd))
601 /* write message terminator */
602 if (SMTP_eom(sinkfp) != SM_OK)
604 fputs("fetchmail: SMTP listener refused delivery\n", stderr);
614 kerberos_auth (int socket, canonical)
615 /* authenticate to the server host using Kerberos V4 */
616 int socket; /* socket to server host */
617 const char *canonical; /* server name */
623 Key_schedule schedule;
626 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
627 rem = (krb_sendauth (0L, socket, ticket, "pop",
629 ((char *) (krb_realmofhost (canonical))),
634 ((struct sockaddr_in *) 0),
635 ((struct sockaddr_in *) 0),
640 fprintf (stderr, "fetchmail: kerberos error %s\n", (krb_get_err_text (rem)));
645 #endif /* KERBEROS_V4 */
647 int do_protocol(ctl, proto)
648 /* retrieve messages from server using given protocol method table */
649 struct query *ctl; /* parsed options with merged-in defaults */
650 const struct method *proto; /* protocol method table */
656 if (ctl->authenticate == A_KERBEROS)
658 fputs("fetchmail: Kerberos support not linked.\n", stderr);
661 #endif /* KERBEROS_V4 */
663 /* lacking methods, there are some options that may fail */
666 /* check for unsupported options */
669 "Option --flush is not supported with %s\n",
673 else if (ctl->fetchall) {
675 "Option --all is not supported with %s\n",
680 if (!proto->getsizes && ctl->limit)
683 "Option --limit is not supported with %s\n",
690 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
693 /* set up the server-nonresponse timeout */
694 sigsave = signal(SIGVTALRM, vtalarm_handler);
695 vtalarm(mytimeout = ctl->timeout);
697 if ((js = setjmp(restart)) == 1)
700 "fetchmail: timeout after %d seconds waiting for %s.\n",
701 ctl->timeout, ctl->servername);
707 "fetchmail: nameserver evaporated during poll of %s.\n",
713 char buf [POPBUFSIZE+1];
714 int *msgsizes, len, num, count, new, deletions = 0;
717 /* open a socket to the mail server */
718 if ((sockfp = Socket(ctl->servername,
719 ctl->port ? ctl->port : protocol->port)) == NULL)
721 perror("fetchmail, connecting to host");
727 if (ctl->authenticate == A_KERBEROS)
729 ok = (kerberos_auth (fileno(sockfp), ctl->canonical_name));
730 vtalarm(ctl->timeout);
734 #endif /* KERBEROS_V4 */
736 /* accept greeting message from mail server */
737 ok = (protocol->parse_response)(sockfp, buf);
738 vtalarm(ctl->timeout);
742 /* try to get authorized to fetch mail */
743 shroud = ctl->password;
744 ok = (protocol->getauth)(sockfp, ctl, buf);
745 vtalarm(ctl->timeout);
746 shroud = (char *)NULL;
752 /* compute number of messages and number of new messages waiting */
753 if ((protocol->getrange)(sockfp, ctl, &count, &new) != 0)
755 vtalarm(ctl->timeout);
757 /* show user how many messages we downloaded */
758 if (outlevel > O_SILENT)
760 fprintf(stderr, "No mail from %s@%s\n",
765 fprintf(stderr, "%d message%s", count, count > 1 ? "s" : "");
766 if (new != -1 && (count - new) > 0)
767 fprintf(stderr, " (%d seen)", count-new);
774 /* we may need to get sizes in order to check message limits */
775 msgsizes = (int *)NULL;
776 if (!ctl->fetchall && proto->getsizes && ctl->limit)
778 msgsizes = (int *)alloca(sizeof(int) * count);
780 if ((ok = (proto->getsizes)(sockfp, count, msgsizes)) != 0)
786 if (new == -1 || ctl->fetchall)
788 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
793 /* read, forward, and delete messages */
794 for (num = 1; num <= count; num++)
796 int toolarge = msgsizes && msgsizes[num-1]>ctl->limit;
797 int fetch_it = ctl->fetchall ||
798 (!(protocol->is_old && (protocol->is_old)(sockfp,ctl,num)) && !toolarge);
800 /* we may want to reject this message if it's old */
803 if (outlevel > O_SILENT)
805 fprintf(stderr, "skipping message %d", num);
807 fprintf(stderr, " (oversized, %d bytes)", msgsizes[num-1]);
812 /* request a message */
813 (protocol->fetch)(sockfp, num, &len);
814 vtalarm(ctl->timeout);
816 if (outlevel > O_SILENT)
818 fprintf(stderr, "reading message %d", num);
820 fprintf(stderr, " (%d bytes)", len);
821 if (outlevel == O_VERBOSE)
827 /* read the message and ship it to the output sink */
828 ok = gen_readmsg(sockfp,
832 vtalarm(ctl->timeout);
836 /* tell the server we got it OK and resynchronize */
838 (protocol->trail)(sockfp, ctl, num);
842 * At this point in flow of control, either we've bombed
843 * on a protocol error or had delivery refused by the SMTP
844 * server (unlikely -- I've never seen it) or we've seen
845 * `accepted for delivery' and the message is shipped.
846 * It's safe to mark the message seen and delete it on the
850 /* maybe we delete this message now? */
852 && (fetch_it ? !ctl->keep : ctl->flush))
855 if (outlevel > O_SILENT)
856 fprintf(stderr, " flushed\n");
857 ok = (protocol->delete)(sockfp, ctl, num);
858 vtalarm(ctl->timeout);
861 delete_uid(&ctl->newsaved, num);
863 else if (outlevel > O_SILENT)
864 fprintf(stderr, " not flushed\n");
867 /* remove all messages flagged for deletion */
868 if (protocol->expunge_cmd && deletions > 0)
870 ok = gen_transact(sockfp, protocol->expunge_cmd);
875 ok = gen_transact(sockfp, protocol->exit_cmd);
882 ok = gen_transact(sockfp, protocol->exit_cmd);
890 if (ok != 0 && ok != PS_SOCKET)
892 gen_transact(sockfp, protocol->exit_cmd);
900 fputs("fetchmail: socket", stderr);
903 fputs("fetchmail: authorization", stderr);
906 fputs("fetchmail: missing or bad RFC822 header", stderr);
909 fputs("fetchmail: MDA", stderr);
912 fputs("fetchmail: client/server synchronization", stderr);
915 fputs("fetchmail: client/server protocol", stderr);
918 fputs("fetchmail: SMTP transaction", stderr);
921 fputs("fetchmail: undefined", stderr);
924 if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
925 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
926 fprintf(stderr, " error while talking to %s\n", ctl->servername);
929 signal(SIGVTALRM, sigsave);
933 #if defined(HAVE_STDARG_H)
934 void gen_send(FILE *sockfp, char *fmt, ... )
935 /* assemble command in printf(3) style and send to the server */
938 void gen_send(sockfp, fmt, va_alist)
939 /* assemble command in printf(3) style and send to the server */
940 FILE *sockfp; /* socket to which server is connected */
941 const char *fmt; /* printf-style format */
945 char buf [POPBUFSIZE+1];
948 if (protocol->tagged)
949 (void) sprintf(buf, "%s ", GENSYM);
953 #if defined(HAVE_STDARG_H)
958 vsprintf(buf + strlen(buf), fmt, ap);
962 SockWrite(buf, strlen(buf), sockfp);
964 if (outlevel == O_VERBOSE)
968 if (shroud && (cp = strstr(buf, shroud)))
969 memset(cp, '*', strlen(shroud));
970 fprintf(stderr,"> %s", buf);
974 #if defined(HAVE_STDARG_H)
975 int gen_transact(FILE *sockfp, char *fmt, ... )
976 /* assemble command in printf(3) style, send to server, accept a response */
979 int gen_transact(sockfp, fmt, va_alist)
980 /* assemble command in printf(3) style, send to server, accept a response */
981 FILE *sockfp; /* socket to which server is connected */
982 const char *fmt; /* printf-style format */
987 char buf [POPBUFSIZE+1];
990 if (protocol->tagged)
991 (void) sprintf(buf, "%s ", GENSYM);
995 #if defined(HAVE_STDARG_H)
1000 vsprintf(buf + strlen(buf), fmt, ap);
1003 strcat(buf, "\r\n");
1004 SockWrite(buf, strlen(buf), sockfp);
1005 if (outlevel == O_VERBOSE)
1009 if (shroud && (cp = strstr(buf, shroud)))
1010 memset(cp, '*', strlen(shroud));
1011 fprintf(stderr,"> %s", buf);
1014 /* we presume this does its own response echoing */
1015 ok = (protocol->parse_response)(sockfp, buf);
1021 /* driver.c ends here */