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.
13 #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 int mboxfd; /* desc to which retrieved message will be written */
53 static char *shroud; /* string to shroud in debug output, if non-NULL */
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 alarm_handler (int signal)
77 /* handle server-timeout signal */
82 static void reply_hack(buf, host)
83 /* hack message headers so replies will work properly */
84 char *buf; /* header to be hacked */
85 const char *host; /* server hostname */
88 int parendepth, state = 0, tokencount = 0;
89 char mycopy[POPBUFSIZE+1];
91 if (strncmp("From: ", buf, 6)
92 && strncmp("To: ", buf, 4)
93 && strncmp("Reply-", buf, 6)
94 && strncmp("Cc: ", buf, 4)
95 && strncmp("Bcc: ", buf, 5)) {
100 for (from = mycopy; *from; from++)
104 case 0: /* before header colon */
109 case 1: /* we've seen the colon, we're looking for addresses */
112 else if (*from == '(')
117 else if (*from == '<' || isalnum(*from))
119 else if (isspace(*from))
121 else if (*from == ',')
125 case 2: /* found a token boundary -- reset without copying */
126 if (*from != ' ' && *from != '\t')
134 case 3: /* we're in a quoted human name, copy and ignore */
139 case 4: /* we're in a parenthesized human name, copy and ignore */
142 else if (*from == ')')
148 case 5: /* the real work gets done here */
150 * We're in something that might be an address part,
151 * either a bare unquoted/unparenthesized text or text
152 * enclosed in <> as per RFC822.
154 /* if the address part contains an @, don't mess with it */
158 /* If the address token is not properly terminated, ignore it. */
159 else if (*from == ' ' || *from == '\t')
164 * The only lookahead case. If we're looking at space or tab,
165 * we might be looking at a local name immediately followed
168 for (cp = from; isspace(*cp); cp++)
180 * On proper termination with no @, insert hostname.
181 * Case '>' catches <>-enclosed mail IDs. Case ',' catches
182 * comma-separated bare IDs.
184 else if (strchr(">,", *from))
192 /* a single local name alone on the line */
193 else if (*from == '\n' && tokencount == 0)
201 /* everything else, including alphanumerics, just passes through */
204 case 6: /* we're in a remote mail ID, no need to append hostname */
205 if (*from == '>' || *from == ',' || isspace(*from))
210 /* all characters from the old buffer get copied to the new one */
216 static char *nxtaddr(hdr)
217 /* parse addresses in succession out of a specified RFC822 header */
218 char *hdr; /* header line to be parsed, NUL to continue in previous hdr */
220 static char *hp, *tp, address[POPBUFSIZE+1];
225 * Note 1: RFC822 escaping with \ is *not* handled. Note 2: it is
226 * important that this routine not stop on \r, since we use \r as
227 * a marker for RFC822 continuations below.
240 case 0: /* before header colon */
250 case 1: /* we've seen the colon, now grab the address */
251 if (*hp == '\n') /* end of address list */
257 else if (*hp == ',') /* end of address */
262 else if (*hp == '"') /* quoted string */
267 else if (*hp == '(') /* address comment -- ignore */
272 else if (*hp == '<') /* begin <address> */
277 else if (isspace(*hp)) /* ignore space */
279 else /* just take it */
286 case 2: /* we're in a quoted string, copy verbatim */
298 case 3: /* we're in a parenthesized comment, ignore */
309 case 4: /* possible <>-enclosed address */
310 if (*hp == '>') /* end of address */
316 else if (*hp == '<') /* nested <> */
318 else if (*hp == '"') /* quoted address */
323 else /* just copy address */
327 case 5: /* we're in a quoted address, copy verbatim */
328 if (*hp == '\n') /* mismatched quotes */
330 if (*hp != '"') /* just copy it if it isn't a quote */
332 else if (*hp == '"') /* end of quoted string */
348 #ifdef HAVE_GETHOSTBYNAME
351 static int is_host_alias(name, ctl)
352 /* determine whether name is a DNS alias of the hostname */
360 * The first two checks are optimizations that will catch a good
361 * many cases. First, check against the hostname the user specified.
362 * Odds are good this will either be the mailserver's FQDN or a
363 * suffix of it with the mailserver's domain's default host name
364 * omitted. Next, check against the mailserver's FQDN, in case
365 * it's not the same as the declared hostname.
367 * Either of these on a mail address is definitive. Only if the
368 * name doesn't match either is it time to call the bind library.
369 * If this happens odds are good we're looking at an MX name.
371 if (strcmp(name, ctl->servername) == 0)
373 else if (strcmp(name, ctl->canonical_name) == 0)
377 * We treat DNS lookup failure as a negative on the theory that
378 * the mailserver's DNS server is `nearby' and should be able
379 * to respond quickly and reliably. Ergo if we get failure,
380 * the name isn't a mailserver alias.
382 else if ((he = gethostbyname(name)) && strcmp(ctl->canonical_name, he->h_name) == 0)
386 * Search for a name match on MX records pointing to the server
387 * site. These may live far away, so allow a couple of retries.
389 for (i = 0; i < MX_RETRIES; i++)
391 struct mxentry *mxrecords, *mxp;
393 mxrecords = getmxrecords(name);
395 if (mxrecords == (struct mxentry *)NULL)
396 if (h_errno == TRY_AGAIN)
404 for (mxp = mxrecords; mxp->name; mxp++)
405 if (strcmp(name, mxp->name) == 0)
412 void find_server_names(hdr, ctl, xmit_names)
413 /* parse names out of a RFC822 header into an ID list */
414 const char *hdr; /* RFC822 header in question */
415 struct query *ctl; /* list of permissible aliases */
416 struct idlist **xmit_names; /* list of recipient names parsed out */
418 if (hdr == (char *)NULL)
424 if ((cp = nxtaddr(hdr)) != (char *)NULL)
426 char *atsign = strchr(cp, '@');
433 if (!is_host_alias(atsign+1, ctl))
437 lname = idpair_find(&ctl->localnames, cp);
438 if (lname != (char *)NULL)
440 if (outlevel == O_VERBOSE)
442 "fetchmail: mapped %s to local %s\n",
444 save_uid(xmit_names, -1, lname);
447 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
450 #endif /* HAVE_GETHOSTBYNAME */
452 static int gen_readmsg (socket, len, delimited, ctl)
453 /* read message content and ship to SMTP or MDA */
454 int socket; /* to which the server is connected */
455 long len; /* length of message */
456 int delimited; /* does the protocol use a message delimiter? */
457 struct query *ctl; /* query control record */
459 char buf [MSGBUFSIZE+1];
460 char *bufp, *headers, *fromhdr, *tohdr, *cchdr, *bcchdr;
462 int inheaders,lines,sizeticker;
464 /* read the message content from the server */
466 headers = fromhdr = tohdr = cchdr = bcchdr = NULL;
470 while (delimited || len > 0)
472 if ((n = SockGets(socket,buf,sizeof(buf))) < 0)
475 /* write the message size dots */
479 while (sizeticker >= SIZETICKER)
481 if (outlevel > O_SILENT)
483 sizeticker -= SIZETICKER;
488 if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
490 if (delimited && *bufp == '.') {
493 break; /* end of message */
500 reply_hack(bufp, ctl->servername);
504 oldlen = strlen(bufp);
505 headers = malloc(oldlen + 1);
508 (void) strcpy(headers, bufp);
516 * We deal with RFC822 continuation lines here.
517 * Replace previous '\n' with '\r' so nxtaddr
518 * and reply_hack will be able to see past it.
519 * (We know this safe because SocketGets stripped
520 * out all carriage returns in the read loop above
521 * and we haven't reintroduced any since then.)
522 * We'll undo this before writing the header.
524 if (isspace(bufp[0]))
525 headers[oldlen-1] = '\r';
527 newlen = oldlen + strlen(bufp);
528 headers = realloc(headers, newlen + 1);
531 strcpy(headers + oldlen, bufp);
532 bufp = headers + oldlen;
536 if (!strncasecmp("From:", bufp, 5))
538 else if (!strncasecmp("To:", bufp, 3))
540 else if (!strncasecmp("Cc:", bufp, 3))
542 else if (!strncasecmp("Bcc:", bufp, 4))
547 else if (headers) /* OK, we're at end of headers now */
550 struct idlist *idp, *xmit_names;
552 /* cons up a list of local recipients */
553 xmit_names = (struct idlist *)NULL;
554 #ifdef HAVE_GETHOSTBYNAME
555 /* is this a multidrop box? */
558 /* compute the local address list */
559 find_server_names(tohdr, ctl, &xmit_names);
560 find_server_names(cchdr, ctl, &xmit_names);
561 find_server_names(bcchdr, ctl, &xmit_names);
563 else /* it's a single-drop box, use first localname */
564 #endif /* HAVE_GETHOSTBYNAME */
567 save_uid(&xmit_names, -1, ctl->localnames->id);
570 /* if nothing supplied localnames, default appropriately */
573 save_uid(&xmit_names, -1, ctl->remotename);
575 save_uid(&xmit_names, -1, user);
577 /* time to address the message */
578 if (ctl->mda[0]) /* we have a declared MDA */
584 * We go through this in order to be able to handle very
585 * long lists of users.
587 for (idp = xmit_names; idp; idp = idp->next)
589 sp = sargv = (char **)alloca(ctl->mda_argcount+nlocals+2);
590 for (i = 0; i < ctl->mda_argcount; i++)
591 *sp++ = ctl->mda_argv[i];
592 for (idp = xmit_names; idp; idp = idp->next)
598 * Arrange to run with user's permissions if we're root.
599 * This will initialize the ownership of any files the
600 * MDA creates properly. (The seteuid call is available
601 * under all BSDs and Linux)
604 #endif /* HAVE_SETEUID */
606 mboxfd = openmailpipe(sargv);
609 /* this will fail quietly if we didn't start as root */
611 #endif /* HAVE_SETEUID */
618 if (ctl->mda[0] == '\0')
619 if ((mboxfd = Socket(ctl->smtphost, SMTP_PORT)) < 0
620 || SMTP_ok(mboxfd, NULL) != SM_OK
621 || SMTP_helo(mboxfd, ctl->servername) != SM_OK)
625 free_uid_list(&xmit_names);
629 if (SMTP_from(mboxfd, nxtaddr(fromhdr)) != SM_OK)
632 for (idp = xmit_names; idp; idp = idp->next)
633 if (SMTP_rcpt(mboxfd, idp->id) != SM_OK)
637 if (outlevel == O_VERBOSE)
638 fputs("SMTP> ", stderr);
640 free_uid_list(&xmit_names);
642 /* change continuation markers back to regular newlines */
643 for (cp = headers; cp < headers + oldlen; cp++)
647 /* replace all LFs with CR-LF before sending to the SMTP server */
650 char *newheaders = malloc(1 + oldlen * 2);
652 if (newheaders == NULL)
654 oldlen = strcrlf(newheaders, headers, oldlen);
656 headers = newheaders;
658 if (write(mboxfd,headers,oldlen) < 0)
662 perror("gen_readmsg: writing RFC822 headers");
665 else if (outlevel == O_VERBOSE)
671 /* SMTP byte-stuffing */
672 if (*bufp == '.' && ctl->mda[0] == 0)
673 write(mboxfd, ".", 1);
675 /* write this line to the file after replacing all LFs with CR-LF */
678 char *newbufp = malloc(1 + strlen(bufp) * 2);
682 strcrlf(newbufp, bufp, strlen(bufp));
685 n = write(mboxfd,bufp,strlen(bufp));
690 perror("gen_readmsg: writing message text");
693 else if (outlevel == O_VERBOSE)
702 /* close the delivery pipe, we'll reopen before next message */
703 if (closemailpipe(mboxfd))
708 /* write message terminator */
709 if (SMTP_eom(mboxfd) != SM_OK)
718 kerberos_auth (socket, canonical)
719 /* authenticate to the server host using Kerberos V4 */
720 int socket; /* socket to server host */
721 char *canonical; /* server name */
727 Key_schedule schedule;
730 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
731 rem = (krb_sendauth (0L, socket, ticket, "pop",
733 ((char *) (krb_realmofhost (canonical))),
738 ((struct sockaddr_in *) 0),
739 ((struct sockaddr_in *) 0),
744 fprintf (stderr, "fetchmail: kerberos error %s\n", (krb_get_err_text (rem)));
749 #endif /* KERBEROS_V4 */
751 int do_protocol(ctl, proto)
752 /* retrieve messages from server using given protocol method table */
753 struct query *ctl; /* parsed options with merged-in defaults */
754 const struct method *proto; /* protocol method table */
760 if (ctl->authenticate == A_KERBEROS)
762 fputs("fetchmail: Kerberos support not linked.\n", stderr);
765 #endif /* KERBEROS_V4 */
767 /* lacking methods, there are some options that may fail */
770 /* check for unsupported options */
773 "Option --flush is not supported with %s\n",
777 else if (ctl->fetchall) {
779 "Option --all is not supported with %s\n",
784 if (!proto->getsizes && ctl->limit)
787 "Option --limit is not supported with %s\n",
794 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
798 if (setjmp(restart) == 1)
800 "fetchmail: timeout after %d seconds waiting for %s.\n",
801 ctl->timeout, ctl->servername);
804 char buf [POPBUFSIZE+1];
805 int *msgsizes, socket, len, num, count, new, deletions = 0;
807 /* set up the server-nonresponse timeout */
808 sigsave = signal(SIGALRM, alarm_handler);
811 /* open a socket to the mail server */
812 if ((socket = Socket(ctl->servername,
813 ctl->port ? ctl->port : protocol->port))<0)
815 perror("fetchmail, connecting to host");
821 if (ctl->authenticate == A_KERBEROS)
823 ok = (kerberos_auth (socket, ctl->canonical_name));
827 #endif /* KERBEROS_V4 */
829 /* accept greeting message from mail server */
830 ok = (protocol->parse_response)(socket, buf);
834 /* try to get authorized to fetch mail */
835 shroud = ctl->password;
836 ok = (protocol->getauth)(socket, ctl, buf);
837 shroud = (char *)NULL;
843 /* compute number of messages and number of new messages waiting */
844 if ((protocol->getrange)(socket, ctl, &count, &new) != 0)
847 /* show user how many messages we downloaded */
848 if (outlevel > O_SILENT)
850 fprintf(stderr, "No mail from %s@%s\n",
855 fprintf(stderr, "%d message%s", count, count > 1 ? "s" : "");
856 if (new != -1 && (count - new) > 0)
857 fprintf(stderr, " (%d seen)", count-new);
864 /* we may need to get sizes in order to check message limits */
865 msgsizes = (int *)NULL;
866 if (!ctl->fetchall && proto->getsizes && ctl->limit)
868 msgsizes = (int *)alloca(sizeof(int) * count);
870 if ((ok = (proto->getsizes)(socket, count, msgsizes)) != 0)
876 if (new == -1 || ctl->fetchall)
878 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
883 /* read, forward, and delete messages */
884 for (num = 1; num <= count; num++)
886 int toolarge = msgsizes && msgsizes[num-1]>ctl->limit;
887 int fetch_it = ctl->fetchall ||
888 (!(protocol->is_old && (protocol->is_old)(socket,ctl,num)) && !toolarge);
890 /* we may want to reject this message if it's old */
893 if (outlevel > O_SILENT)
895 fprintf(stderr, "skipping message %d", num);
897 fprintf(stderr, " (oversized, %d bytes)", msgsizes[num-1]);
902 /* request a message */
903 (protocol->fetch)(socket, num, &len);
905 if (outlevel > O_SILENT)
907 fprintf(stderr, "reading message %d", num);
909 fprintf(stderr, " (%d bytes)", len);
910 if (outlevel == O_VERBOSE)
917 * If we're forwarding via SMTP, mboxfd is initialized
918 * at this point (it was set at start of retrieval).
919 * If we're using an MDA it's not set -- gen_readmsg()
920 * may have to parse message headers to know what
921 * delivery addresses should be passed to the MDA
924 /* read the message and ship it to the output sink */
925 ok = gen_readmsg(socket,
932 /* tell the server we got it OK and resynchronize */
934 (protocol->trail)(socket, ctl, num);
938 * At this point in flow of control, either we've bombed
939 * on a protocol error or had delivery refused by the SMTP
940 * server (unlikely -- I've never seen it) or we've seen
941 * `accepted for delivery' and the message is shipped.
942 * It's safe to mark the message seen and delete it on the
946 /* maybe we delete this message now? */
948 && (fetch_it ? !ctl->keep : ctl->flush))
951 if (outlevel > O_SILENT)
952 fprintf(stderr, " flushed\n");
953 ok = (protocol->delete)(socket, ctl, num);
957 else if (outlevel > O_SILENT)
959 /* nuke it from the unseen-messages list */
960 delete_uid(&ctl->newsaved, num);
961 fprintf(stderr, " not flushed\n");
965 /* remove all messages flagged for deletion */
966 if (protocol->expunge_cmd && deletions > 0)
968 ok = gen_transact(socket, protocol->expunge_cmd);
973 ok = gen_transact(socket, protocol->exit_cmd);
980 ok = gen_transact(socket, protocol->exit_cmd);
988 if (ok != 0 && ok != PS_SOCKET)
990 gen_transact(socket, protocol->exit_cmd);
996 signal(SIGALRM, sigsave);
1009 #if defined(HAVE_STDARG_H)
1010 void gen_send(int socket, char *fmt, ... )
1011 /* assemble command in printf(3) style and send to the server */
1014 void gen_send(socket, fmt, va_alist)
1015 /* assemble command in printf(3) style and send to the server */
1016 int socket; /* socket to which server is connected */
1017 const char *fmt; /* printf-style format */
1021 char buf [POPBUFSIZE+1];
1024 if (protocol->tagged)
1025 (void) sprintf(buf, "%s ", GENSYM);
1029 #if defined(HAVE_STDARG_H)
1034 vsprintf(buf + strlen(buf), fmt, ap);
1037 SockPuts(socket, buf);
1039 if (outlevel == O_VERBOSE)
1043 if (shroud && (cp = strstr(buf, shroud)))
1044 memset(cp, '*', strlen(shroud));
1045 fprintf(stderr,"> %s\n", buf);
1049 #if defined(HAVE_STDARG_H)
1050 int gen_transact(int socket, char *fmt, ... )
1051 /* assemble command in printf(3) style, send to server, accept a response */
1054 int gen_transact(socket, fmt, va_alist)
1055 /* assemble command in printf(3) style, send to server, accept a response */
1056 int socket; /* socket to which server is connected */
1057 const char *fmt; /* printf-style format */
1062 char buf [POPBUFSIZE+1];
1065 if (protocol->tagged)
1066 (void) sprintf(buf, "%s ", GENSYM);
1070 #if defined(HAVE_STDARG_H)
1075 vsprintf(buf + strlen(buf), fmt, ap);
1078 SockPuts(socket, buf);
1079 if (outlevel == O_VERBOSE)
1083 if (shroud && (cp = strstr(buf, shroud)))
1084 memset(cp, '*', strlen(shroud));
1085 fprintf(stderr,"> %s\n", buf);
1088 /* we presume this does its own response echoing */
1089 ok = (protocol->parse_response)(socket, buf);
1094 /* driver.c ends here */