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 static const struct method *protocol;
46 static jmp_buf restart;
50 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
52 static char *shroud; /* string to shroud in debug output, if non-NULL */
53 static int mytimeout; /* value of nonreponse timeout */
55 static int strcrlf(dst, src, count)
56 /* replace LFs with CR-LF; return length of string with replacements */
57 char *dst; /* new string with CR-LFs */
58 char *src; /* original string with LFs */
59 int count; /* length of src */
76 static void vtalarm(timeleft)
77 /* reset the nonresponse-timeout */
80 struct itimerval ntimeout;
82 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_sec = 0;
83 ntimeout.it_value.tv_sec = timeleft;
84 ntimeout.it_value.tv_usec = 0;
85 setitimer(ITIMER_VIRTUAL, &ntimeout, (struct itimerval *)NULL);
88 static void vtalarm_handler (int signal)
89 /* handle server-timeout SIGVTALARM signal */
94 static void reply_hack(buf, host)
95 /* hack message headers so replies will work properly */
96 char *buf; /* header to be hacked */
97 const char *host; /* server hostname */
100 int parendepth, state = 0, tokencount = 0;
101 char mycopy[POPBUFSIZE+1];
103 if (strncmp("From: ", buf, 6)
104 && strncmp("To: ", buf, 4)
105 && strncmp("Reply-", buf, 6)
106 && strncmp("Cc: ", buf, 4)
107 && strncmp("Bcc: ", buf, 5)) {
112 for (from = mycopy; *from; from++)
116 case 0: /* before header colon */
121 case 1: /* we've seen the colon, we're looking for addresses */
124 else if (*from == '(')
129 else if (*from == '<' || isalnum(*from))
131 else if (isspace(*from))
135 case 2: /* found a token boundary -- reset without copying */
136 if (*from != ' ' && *from != '\t')
144 case 3: /* we're in a quoted human name, copy and ignore */
149 case 4: /* we're in a parenthesized human name, copy and ignore */
152 else if (*from == ')')
158 case 5: /* the real work gets done here */
160 * We're in something that might be an address part,
161 * either a bare unquoted/unparenthesized text or text
162 * enclosed in <> as per RFC822.
164 /* if the address part contains an @, don't mess with it */
168 /* If the address token is not properly terminated, ignore it. */
169 else if (*from == ' ' || *from == '\t')
174 * The only lookahead case. If we're looking at space or tab,
175 * we might be looking at a local name immediately followed
178 for (cp = from; isspace(*cp); cp++)
190 * On proper termination with no @, insert hostname.
191 * Case '>' catches <>-enclosed mail IDs. Case ',' catches
192 * comma-separated bare IDs.
194 else if (strchr(">,", *from))
203 /* a single local name alone on the line */
204 else if (*from == '\n' && tokencount == 1)
212 /* everything else, including alphanumerics, just passes through */
215 case 6: /* we're in a remote mail ID, no need to append hostname */
216 if (*from == '>' || *from == ',' || isspace(*from))
221 /* all characters from the old buffer get copied to the new one */
227 static char *nxtaddr(hdr)
228 /* parse addresses in succession out of a specified RFC822 header */
229 char *hdr; /* header line to be parsed, NUL to continue in previous hdr */
231 static char *hp, *tp, address[POPBUFSIZE+1];
236 * Note 1: RFC822 escaping with \ is *not* handled. Note 2: it is
237 * important that this routine not stop on \r, since we use \r as
238 * a marker for RFC822 continuations below.
251 case 0: /* before header colon */
261 case 1: /* we've seen the colon, now grab the address */
262 if (*hp == '\n') /* end of address list */
268 else if (*hp == ',') /* end of address */
273 else if (*hp == '"') /* quoted string */
278 else if (*hp == '(') /* address comment -- ignore */
283 else if (*hp == '<') /* begin <address> */
288 else if (isspace(*hp)) /* ignore space */
290 else /* just take it */
297 case 2: /* we're in a quoted string, copy verbatim */
309 case 3: /* we're in a parenthesized comment, ignore */
320 case 4: /* possible <>-enclosed address */
321 if (*hp == '>') /* end of address */
327 else if (*hp == '<') /* nested <> */
329 else if (*hp == '"') /* quoted address */
334 else /* just copy address */
338 case 5: /* we're in a quoted address, copy verbatim */
339 if (*hp == '\n') /* mismatched quotes */
341 if (*hp != '"') /* just copy it if it isn't a quote */
343 else if (*hp == '"') /* end of quoted string */
359 #ifdef HAVE_GETHOSTBYNAME
362 static int is_host_alias(name, ctl)
363 /* determine whether name is a DNS alias of the hostname */
371 * The first two checks are optimizations that will catch a good
372 * many cases. First, check against the hostname the user specified.
373 * Odds are good this will either be the mailserver's FQDN or a
374 * suffix of it with the mailserver's domain's default host name
375 * omitted. Next, check against the mailserver's FQDN, in case
376 * it's not the same as the declared hostname.
378 * Either of these on a mail address is definitive. Only if the
379 * name doesn't match either is it time to call the bind library.
380 * If this happens odds are good we're looking at an MX name.
382 if (strcmp(name, ctl->servername) == 0)
384 else if (strcmp(name, ctl->canonical_name) == 0)
388 * We treat DNS lookup failure as a negative on the theory that
389 * the mailserver's DNS server is `nearby' and should be able
390 * to respond quickly and reliably. Ergo if we get failure,
391 * the name isn't a mailserver alias.
393 else if ((he = gethostbyname(name)) && strcmp(ctl->canonical_name, he->h_name) == 0)
397 * Search for a name match on MX records pointing to the server
398 * site. These may live far away, so allow a couple of retries.
400 for (i = 0; i < MX_RETRIES; i++)
402 struct mxentry *mxrecords, *mxp;
404 mxrecords = getmxrecords(name);
406 if (mxrecords == (struct mxentry *)NULL)
407 if (h_errno == TRY_AGAIN)
415 for (mxp = mxrecords; mxp->name; mxp++)
416 if (strcmp(name, mxp->name) == 0)
423 void find_server_names(hdr, ctl, xmit_names)
424 /* parse names out of a RFC822 header into an ID list */
425 const char *hdr; /* RFC822 header in question */
426 struct query *ctl; /* list of permissible aliases */
427 struct idlist **xmit_names; /* list of recipient names parsed out */
429 if (hdr == (char *)NULL)
435 if ((cp = nxtaddr(hdr)) != (char *)NULL)
439 if ((atsign = strchr(cp, '@')))
442 * Address has an @. Check to see if the right-hand part
443 * is an alias or MX equivalent of the mailserver. If it's
444 * not, skip this name. If it is, we'll keep going and try
445 * to find a mapping to a client name.
447 if (!is_host_alias(atsign+1, ctl))
452 lname = idpair_find(&ctl->localnames, cp);
453 if (lname != (char *)NULL)
455 if (outlevel == O_VERBOSE)
457 "fetchmail: mapped %s to local %s\n",
459 save_uid(xmit_names, -1, lname);
462 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
465 #endif /* HAVE_GETHOSTBYNAME */
467 static FILE *smtp_open(ctl)
468 /* try to open a socket to the appropriate SMTP server for this query */
471 ctl = ctl->leader; /* go to the SMTP leader for this query */
473 /* if no socket to this host is already set up, try to open one */
474 if (ctl->smtp_sockfp == (FILE *)NULL)
476 if ((ctl->smtp_sockfp = Socket(ctl->smtphost, SMTP_PORT)) == (FILE *)NULL)
477 return((FILE *)NULL);
478 else if (SMTP_ok(ctl->smtp_sockfp, NULL) != SM_OK
479 || SMTP_helo(ctl->smtp_sockfp, ctl->servername) != SM_OK)
481 fclose(ctl->smtp_sockfp);
482 ctl->smtp_sockfp = (FILE *)NULL;
486 return(ctl->smtp_sockfp);
489 static int gen_readmsg (sockfp, len, delimited, ctl)
490 /* read message content and ship to SMTP or MDA */
491 FILE *sockfp; /* to which the server is connected */
492 long len; /* length of message */
493 int delimited; /* does the protocol use a message delimiter? */
494 struct query *ctl; /* query control record */
496 char buf [MSGBUFSIZE+1];
497 char *bufp, *headers, *fromhdr, *tohdr, *cchdr, *bcchdr;
498 int n, oldlen, mboxfd;
499 int inheaders,lines,sizeticker;
502 /* read the message content from the server */
504 headers = fromhdr = tohdr = cchdr = bcchdr = NULL;
508 while (delimited || len > 0)
510 if ((n = SockGets(buf,sizeof(buf),sockfp)) < 0)
512 vtalarm(ctl->timeout);
514 /* write the message size dots */
518 while (sizeticker >= SIZETICKER)
520 if (outlevel > O_SILENT)
522 sizeticker -= SIZETICKER;
527 if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
529 if (delimited && *bufp == '.') {
532 break; /* end of message */
539 reply_hack(bufp, ctl->servername);
543 oldlen = strlen(bufp);
544 headers = malloc(oldlen + 1);
547 (void) strcpy(headers, bufp);
555 * We deal with RFC822 continuation lines here.
556 * Replace previous '\n' with '\r' so nxtaddr
557 * and reply_hack will be able to see past it.
558 * (We know this is safe because SocketGets stripped
559 * out all carriage returns in the read loop above
560 * and we haven't reintroduced any since then.)
561 * We'll undo this before writing the header.
563 if (isspace(bufp[0]))
564 headers[oldlen-1] = '\r';
566 newlen = oldlen + strlen(bufp);
567 headers = realloc(headers, newlen + 1);
570 strcpy(headers + oldlen, bufp);
571 bufp = headers + oldlen;
575 if (!strncasecmp("From:", bufp, 5))
577 else if (!strncasecmp("To:", bufp, 3))
579 else if (!strncasecmp("Cc:", bufp, 3))
581 else if (!strncasecmp("Bcc:", bufp, 4))
586 else if (headers) /* OK, we're at end of headers now */
589 struct idlist *idp, *xmit_names;
591 /* cons up a list of local recipients */
592 xmit_names = (struct idlist *)NULL;
593 #ifdef HAVE_GETHOSTBYNAME
594 /* is this a multidrop box? */
597 /* compute the local address list */
598 find_server_names(tohdr, ctl, &xmit_names);
599 find_server_names(cchdr, ctl, &xmit_names);
600 find_server_names(bcchdr, ctl, &xmit_names);
602 else /* it's a single-drop box, use first localname */
603 #endif /* HAVE_GETHOSTBYNAME */
606 save_uid(&xmit_names, -1, ctl->localnames->id);
609 /* if nothing supplied localnames, default appropriately */
612 save_uid(&xmit_names, -1, ctl->remotename);
614 save_uid(&xmit_names, -1, user);
616 /* time to address the message */
617 if (ctl->mda[0]) /* we have a declared MDA */
623 * We go through this in order to be able to handle very
624 * long lists of users and (re
627 for (idp = xmit_names; idp; idp = idp->next)
629 sp = sargv = (char **)alloca(sizeof(char **) * ctl->mda_argcount+nlocals+2);
630 for (i = 0; i < ctl->mda_argcount; i++)
631 if (strcmp("%s", ctl->mda_argv[i]))
632 *sp++ = ctl->mda_argv[i];
634 for (idp = xmit_names; idp; idp = idp->next)
640 * Arrange to run with user's permissions if we're root.
641 * This will initialize the ownership of any files the
642 * MDA creates properly. (The seteuid call is available
643 * under all BSDs and Linux)
646 #endif /* HAVE_SETEUID */
648 mboxfd = openmailpipe(sargv);
651 /* this will fail quietly if we didn't start as root */
653 #endif /* HAVE_SETEUID */
657 fprintf(stderr, "fetchmail: MDA open failed\n");
663 if (ctl->mda[0] == '\0' && ((sinkfp = smtp_open(ctl)) < 0))
665 free_uid_list(&xmit_names);
666 fprintf(stderr, "fetchmail: SMTP connect failed\n");
670 if (SMTP_from(sinkfp, nxtaddr(fromhdr)) != SM_OK)
672 fprintf(stderr, "fetchmail: SMTP listener is confused\n");
676 for (idp = xmit_names; idp; idp = idp->next)
677 if (SMTP_rcpt(sinkfp, idp->id) != SM_OK)
679 fprintf(stderr, "fetchmail: SMTP listener is upset\n");
684 if (outlevel == O_VERBOSE)
685 fputs("SMTP> ", stderr);
687 free_uid_list(&xmit_names);
689 /* change continuation markers back to regular newlines */
690 for (cp = headers; cp < headers + oldlen; cp++)
693 headers[oldlen++] = '\0';
695 /* replace all LFs with CR-LF before sending to the SMTP server */
698 char *newheaders = malloc(1 + oldlen * 2);
700 if (newheaders == NULL)
702 oldlen = strcrlf(newheaders, headers, oldlen);
704 headers = newheaders;
707 /* write all the headers */
709 n = write(mboxfd,headers,oldlen);
711 n = SockWrite(headers, oldlen, sinkfp);
717 perror("fetchmail: writing RFC822 headers");
720 else if (outlevel == O_VERBOSE)
726 /* SMTP byte-stuffing */
727 if (*bufp == '.' && ctl->mda[0] == 0)
728 SockWrite(".", 1, sinkfp);
730 /* replace all LFs with CR-LF in the line */
733 char *newbufp = malloc(1 + strlen(bufp) * 2);
737 strcrlf(newbufp, bufp, strlen(bufp));
741 /* ship out the text line */
743 n = write(mboxfd,bufp,strlen(bufp));
745 n = SockWrite(bufp, strlen(bufp), sinkfp);
751 perror("fetchmail: writing message text");
754 else if (outlevel == O_VERBOSE)
763 /* close the delivery pipe, we'll reopen before next message */
764 if (closemailpipe(mboxfd))
769 /* write message terminator */
770 if (SMTP_eom(sinkfp) != SM_OK)
772 fputs("fetchmail: SMTP listener refused delivery\n", stderr);
782 kerberos_auth (socket, canonical)
783 /* authenticate to the server host using Kerberos V4 */
784 int socket; /* socket to server host */
785 char *canonical; /* server name */
791 Key_schedule schedule;
794 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
795 rem = (krb_sendauth (0L, socket, ticket, "pop",
797 ((char *) (krb_realmofhost (canonical))),
802 ((struct sockaddr_in *) 0),
803 ((struct sockaddr_in *) 0),
808 fprintf (stderr, "fetchmail: kerberos error %s\n", (krb_get_err_text (rem)));
813 #endif /* KERBEROS_V4 */
815 int do_protocol(ctl, proto)
816 /* retrieve messages from server using given protocol method table */
817 struct query *ctl; /* parsed options with merged-in defaults */
818 const struct method *proto; /* protocol method table */
824 if (ctl->authenticate == A_KERBEROS)
826 fputs("fetchmail: Kerberos support not linked.\n", stderr);
829 #endif /* KERBEROS_V4 */
831 /* lacking methods, there are some options that may fail */
834 /* check for unsupported options */
837 "Option --flush is not supported with %s\n",
841 else if (ctl->fetchall) {
843 "Option --all is not supported with %s\n",
848 if (!proto->getsizes && ctl->limit)
851 "Option --limit is not supported with %s\n",
858 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
861 /* set up the server-nonresponse timeout */
862 sigsave = signal(SIGVTALRM, vtalarm_handler);
863 vtalarm(mytimeout = ctl->timeout);
865 if (setjmp(restart) == 1)
867 "fetchmail: timeout after %d seconds waiting for %s.\n",
868 ctl->timeout, ctl->servername);
871 char buf [POPBUFSIZE+1];
872 int *msgsizes, len, num, count, new, deletions = 0;
875 /* open a socket to the mail server */
876 if ((sockfp = Socket(ctl->servername,
877 ctl->port ? ctl->port : protocol->port))<0)
879 perror("fetchmail, connecting to host");
885 if (ctl->authenticate == A_KERBEROS)
887 ok = (kerberos_auth (fileno(sockfp), ctl->canonical_name));
888 vtalarm(ctl->timeout);
892 #endif /* KERBEROS_V4 */
894 /* accept greeting message from mail server */
895 ok = (protocol->parse_response)(sockfp, buf);
896 vtalarm(ctl->timeout);
900 /* try to get authorized to fetch mail */
901 shroud = ctl->password;
902 ok = (protocol->getauth)(sockfp, ctl, buf);
903 vtalarm(ctl->timeout);
904 shroud = (char *)NULL;
910 /* compute number of messages and number of new messages waiting */
911 if ((protocol->getrange)(sockfp, ctl, &count, &new) != 0)
913 vtalarm(ctl->timeout);
915 /* show user how many messages we downloaded */
916 if (outlevel > O_SILENT)
918 fprintf(stderr, "No mail from %s@%s\n",
923 fprintf(stderr, "%d message%s", count, count > 1 ? "s" : "");
924 if (new != -1 && (count - new) > 0)
925 fprintf(stderr, " (%d seen)", count-new);
932 /* we may need to get sizes in order to check message limits */
933 msgsizes = (int *)NULL;
934 if (!ctl->fetchall && proto->getsizes && ctl->limit)
936 msgsizes = (int *)alloca(sizeof(int) * count);
938 if ((ok = (proto->getsizes)(sockfp, count, msgsizes)) != 0)
944 if (new == -1 || ctl->fetchall)
946 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
951 /* read, forward, and delete messages */
952 for (num = 1; num <= count; num++)
954 int toolarge = msgsizes && msgsizes[num-1]>ctl->limit;
955 int fetch_it = ctl->fetchall ||
956 (!(protocol->is_old && (protocol->is_old)(sockfp,ctl,num)) && !toolarge);
958 /* we may want to reject this message if it's old */
961 if (outlevel > O_SILENT)
963 fprintf(stderr, "skipping message %d", num);
965 fprintf(stderr, " (oversized, %d bytes)", msgsizes[num-1]);
970 /* request a message */
971 (protocol->fetch)(sockfp, num, &len);
972 vtalarm(ctl->timeout);
974 if (outlevel > O_SILENT)
976 fprintf(stderr, "reading message %d", num);
978 fprintf(stderr, " (%d bytes)", len);
979 if (outlevel == O_VERBOSE)
985 /* read the message and ship it to the output sink */
986 ok = gen_readmsg(sockfp,
990 vtalarm(ctl->timeout);
994 /* tell the server we got it OK and resynchronize */
996 (protocol->trail)(sockfp, ctl, num);
1000 * At this point in flow of control, either we've bombed
1001 * on a protocol error or had delivery refused by the SMTP
1002 * server (unlikely -- I've never seen it) or we've seen
1003 * `accepted for delivery' and the message is shipped.
1004 * It's safe to mark the message seen and delete it on the
1008 /* maybe we delete this message now? */
1009 if (protocol->delete
1010 && (fetch_it ? !ctl->keep : ctl->flush))
1013 if (outlevel > O_SILENT)
1014 fprintf(stderr, " flushed\n");
1015 ok = (protocol->delete)(sockfp, ctl, num);
1016 vtalarm(ctl->timeout);
1020 else if (outlevel > O_SILENT)
1022 /* nuke it from the unseen-messages list */
1023 delete_uid(&ctl->newsaved, num);
1024 fprintf(stderr, " not flushed\n");
1028 /* remove all messages flagged for deletion */
1029 if (protocol->expunge_cmd && deletions > 0)
1031 ok = gen_transact(sockfp, protocol->expunge_cmd);
1036 ok = gen_transact(sockfp, protocol->exit_cmd);
1043 ok = gen_transact(sockfp, protocol->exit_cmd);
1051 if (ok != 0 && ok != PS_SOCKET)
1053 gen_transact(sockfp, protocol->exit_cmd);
1058 signal(SIGVTALRM, sigsave);
1064 #if defined(HAVE_STDARG_H)
1065 void gen_send(FILE *sockfp, char *fmt, ... )
1066 /* assemble command in printf(3) style and send to the server */
1069 void gen_send(sockfp, fmt, va_alist)
1070 /* assemble command in printf(3) style and send to the server */
1071 FILE *sockfp; /* socket to which server is connected */
1072 const char *fmt; /* printf-style format */
1076 char buf [POPBUFSIZE+1];
1079 if (protocol->tagged)
1080 (void) sprintf(buf, "%s ", GENSYM);
1084 #if defined(HAVE_STDARG_H)
1089 vsprintf(buf + strlen(buf), fmt, ap);
1092 strcat(buf, "\r\n");
1093 SockWrite(buf, strlen(buf), sockfp);
1095 if (outlevel == O_VERBOSE)
1099 if (shroud && (cp = strstr(buf, shroud)))
1100 memset(cp, '*', strlen(shroud));
1101 fprintf(stderr,"> %s", buf);
1105 #if defined(HAVE_STDARG_H)
1106 int gen_transact(FILE *sockfp, char *fmt, ... )
1107 /* assemble command in printf(3) style, send to server, accept a response */
1110 int gen_transact(sockfp, fmt, va_alist)
1111 /* assemble command in printf(3) style, send to server, accept a response */
1112 FILE *sockfp; /* socket to which server is connected */
1113 const char *fmt; /* printf-style format */
1118 char buf [POPBUFSIZE+1];
1121 if (protocol->tagged)
1122 (void) sprintf(buf, "%s ", GENSYM);
1126 #if defined(HAVE_STDARG_H)
1131 vsprintf(buf + strlen(buf), fmt, ap);
1134 strcat(buf, "\r\n");
1135 SockWrite(buf, strlen(buf), sockfp);
1136 if (outlevel == O_VERBOSE)
1140 if (shroud && (cp = strstr(buf, shroud)))
1141 memset(cp, '*', strlen(shroud));
1142 fprintf(stderr,"> %s", buf);
1145 /* we presume this does its own response echoing */
1146 ok = (protocol->parse_response)(sockfp, buf);
1152 /* driver.c ends here */