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 int batchlimit; /* how often to tear down the delivery connection */
56 int fetchlimit; /* how often to tear down the server connection */
57 int batchcount; /* count of messages sent in current batch */
58 int peek_capable; /* can we peek for better error recovery? */
60 static const struct method *protocol;
61 static jmp_buf restart;
65 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
67 static char *shroud; /* string to shroud in debug output, if non-NULL */
68 static int mytimeout; /* value of nonreponse timeout */
70 static int strcrlf(dst, src, count)
71 /* replace LFs with CR-LF; return length of string with replacements */
72 char *dst; /* new string with CR-LFs */
73 char *src; /* original string with LFs */
74 int count; /* length of src */
91 static void vtalarm(int timeleft)
92 /* reset the nonresponse-timeout */
94 struct itimerval ntimeout;
96 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
97 ntimeout.it_value.tv_sec = timeleft;
98 ntimeout.it_value.tv_usec = 0;
99 setitimer(ITIMER_VIRTUAL, &ntimeout, (struct itimerval *)NULL);
102 static void vtalarm_handler (int signal)
103 /* handle server-timeout SIGVTALARM signal */
108 #ifdef HAVE_RES_SEARCH
111 static int is_host_alias(const char *name, struct query *ctl)
112 /* determine whether name is a DNS alias of the hostname */
115 struct mxentry *mxp, *mxrecords;
118 * The first two checks are optimizations that will catch a good
119 * many cases. (1) check against the hostname the user
120 * specified. Odds are good this will either be the mailserver's
121 * FQDN or a suffix of it with the mailserver's domain's default
122 * host name omitted. Then check the rest of the `also known as'
123 * cache accumulated by previous DNS checks. This cache is primed
124 * by the aka list option.
126 * (2) check against the mailserver's FQDN, in case
127 * it's not the same as the declared hostname.
129 * Either of these on a mail address is definitive. Only if the
130 * name doesn't match either is it time to call the bind library.
131 * If this happens odds are good we're looking at an MX name.
133 if (str_in_list(&ctl->lead_server->servernames, name))
135 else if (strcmp(name, ctl->canonical_name) == 0)
139 * We know DNS service was up at the beginning of this poll cycle.
140 * If it's down, our nameserver has crashed. We don't want to try
141 * delivering the current message or anything else from this
142 * mailbox until it's back up.
144 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
146 if (strcmp(ctl->canonical_name, he->h_name) == 0)
154 case HOST_NOT_FOUND: /* specified host is unknown */
155 case NO_ADDRESS: /* valid, but does not have an IP address */
158 case NO_RECOVERY: /* non-recoverable name server error */
159 case TRY_AGAIN: /* temporary error on authoritative server */
161 if (outlevel != O_SILENT)
162 putchar('\n'); /* terminate the progress message */
164 "nameserver failure while looking for `%s' during poll of %s.",
165 name, ctl->servernames->id);
167 longjmp(restart, 2); /* try again next poll cycle */
172 * We're only here if DNS was OK but the gethostbyname() failed
173 * with a HOST_NOT_FOUND or NO_ADDRESS error.
174 * Search for a name match on MX records pointing to the server.
177 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
181 case HOST_NOT_FOUND: /* specified host is unknown */
182 case NO_ADDRESS: /* valid, but does not have an IP address */
186 case NO_RECOVERY: /* non-recoverable name server error */
187 case TRY_AGAIN: /* temporary error on authoritative server */
190 "nameserver failure while looking for `%s' during poll of %s.",
191 name, ctl->servernames->id);
193 longjmp(restart, 2); /* try again next poll cycle */
199 for (mxp = mxrecords; mxp->name; mxp++)
200 if (strcmp(ctl->canonical_name, mxp->name) == 0)
206 /* add this name to relevant server's `also known as' list */
207 save_str(&ctl->lead_server->servernames, -1, name);
211 static void map_name(name, ctl, xmit_names)
212 /* add given name to xmit_names if it matches declared localnames */
213 const char *name; /* name to map */
214 struct query *ctl; /* list of permissible aliases */
215 struct idlist **xmit_names; /* list of recipient names parsed out */
219 lname = idpair_find(&ctl->localnames, name);
220 if (!lname && ctl->wildcard)
223 if (lname != (char *)NULL)
225 if (outlevel == O_VERBOSE)
226 error(0, 0, "mapped %s to local %s", name, lname);
227 save_str(xmit_names, -1, lname);
231 void find_server_names(hdr, ctl, xmit_names)
232 /* parse names out of a RFC822 header into an ID list */
233 const char *hdr; /* RFC822 header in question */
234 struct query *ctl; /* list of permissible aliases */
235 struct idlist **xmit_names; /* list of recipient names parsed out */
237 if (hdr == (char *)NULL)
243 if ((cp = nxtaddr(hdr)) != (char *)NULL)
247 if ((atsign = strchr(cp, '@')))
252 * Does a trailing segment of the hostname match something
253 * on the localdomains list? If so, save the whole name
256 for (idp = ctl->localdomains; idp; idp = idp->next)
260 rhs = atsign + 1 + (strlen(atsign) - strlen(idp->id));
261 if ((rhs[-1] == '.' || rhs[-1] == '@')
262 && strcmp(rhs, idp->id) == 0)
264 if (outlevel == O_VERBOSE)
265 error(0, 0, "passed through %s matching %s",
267 save_str(xmit_names, -1, cp);
273 * Check to see if the right-hand part is an alias
274 * or MX equivalent of the mailserver. If it's
275 * not, skip this name. If it is, we'll keep
276 * going and try to find a mapping to a client name.
278 if (!is_host_alias(atsign+1, ctl))
283 map_name(cp, ctl, xmit_names);
285 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
288 #endif /* HAVE_RES_SEARCH */
290 static FILE *smtp_open(struct query *ctl)
291 /* try to open a socket to the appropriate SMTP server for this query */
295 lead = ctl->lead_smtp; /* go to the SMTP leader for this query */
297 /* maybe it's time to close the socket in order to force delivery */
298 if (batchlimit && lead->smtp_sockfp && batchcount++ == batchlimit)
300 fclose(lead->smtp_sockfp);
301 lead->smtp_sockfp = (FILE *)NULL;
305 /* if no socket to this host is already set up, try to open one */
306 if (lead->smtp_sockfp == (FILE *)NULL)
308 if ((lead->smtp_sockfp = SockOpen(lead->smtphost, SMTP_PORT)) == (FILE *)NULL)
309 return((FILE *)NULL);
310 else if (SMTP_ok(lead->smtp_sockfp) != SM_OK
311 || SMTP_helo(lead->smtp_sockfp, ctl->servernames->id) != SM_OK)
313 fclose(lead->smtp_sockfp);
314 lead->smtp_sockfp = (FILE *)NULL;
318 return(lead->smtp_sockfp);
321 static int gen_readmsg (sockfp, len, delimited, ctl)
322 /* read message content and ship to SMTP or MDA */
323 FILE *sockfp; /* to which the server is connected */
324 long len; /* length of message */
325 int delimited; /* does the protocol use a message delimiter? */
326 struct query *ctl; /* query control record */
328 char buf [MSGBUFSIZE+1];
329 char *bufp, *headers, *fromhdr,*tohdr,*cchdr,*bcchdr,*received_for,*envto;
330 char *fromptr, *toptr;
332 int inheaders,lines,sizeticker;
334 RETSIGTYPE (*sigchld)();
335 #ifdef HAVE_GETHOSTBYNAME
336 char rbuf[HOSTLEN + USERNAMELEN + 4];
337 #endif /* HAVE_GETHOSTBYNAME */
339 /* read the message content from the server */
341 headers = fromhdr = tohdr = cchdr = bcchdr = received_for = envto = NULL;
345 while (delimited || len > 0)
347 if (!SockGets(buf,sizeof(buf),sockfp))
350 vtalarm(ctl->timeout);
352 /* squeeze out all carriage returns */
353 for (fromptr = toptr = buf; *fromptr; fromptr++)
354 if (*fromptr != '\r' && *fromptr != '\n')
358 /* write the message size dots */
362 while (sizeticker >= SIZETICKER)
364 if (outlevel > O_SILENT)
366 sizeticker -= SIZETICKER;
371 if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
373 if (delimited && *bufp == '.') {
376 break; /* end of message */
383 reply_hack(bufp, ctl->servernames->id);
387 oldlen = strlen(bufp);
388 headers = xmalloc(oldlen + 1);
389 (void) strcpy(headers, bufp);
397 * We deal with RFC822 continuation lines here.
398 * Replace previous '\n' with '\r' so nxtaddr
399 * and reply_hack will be able to see past it.
400 * (We know this is safe because SocketGets stripped
401 * out all carriage returns in the read loop above
402 * and we haven't reintroduced any since then.)
403 * We'll undo this before writing the header.
405 if (isspace(bufp[0]))
406 headers[oldlen-1] = '\r';
408 newlen = oldlen + strlen(bufp);
409 headers = realloc(headers, newlen + 1);
412 strcpy(headers + oldlen, bufp);
413 bufp = headers + oldlen;
417 if (!strncasecmp("From:", bufp, 5))
419 else if (!fromhdr && !strncasecmp("Resent-From:", bufp, 12))
421 else if (!fromhdr && !strncasecmp("Apparently-From:", bufp, 16))
423 else if (!strncasecmp("To:", bufp, 3))
425 else if (!strncasecmp("Apparently-To:", bufp, 14))
427 else if (!strncasecmp(ctl->envelope, bufp, 14))
429 else if (!strncasecmp("Cc:", bufp, 3))
431 else if (!strncasecmp("Bcc:", bufp, 4))
433 #ifdef HAVE_RES_SEARCH
434 else if (MULTIDROP(ctl) && !strncasecmp("Received:", bufp, 9))
439 * Try to extract the real envelope addressee. We look here
440 * specifically for the mailserver's Received line.
441 * Note: this will only work for sendmail, or an MTA that
442 * shares sendmail's convention for embedding the envelope
443 * address in the Received line. Sendmail itself only
444 * does this when the mail has a single recipient.
446 if ((ok = strstr(bufp, "by ")) == (char *)NULL)
452 /* extract space-delimited token after "by " */
454 for (sp = ok + 3; !isspace(*sp); sp++)
459 * If it's a DNS name of the mail server, look for the
460 * recipient name after a following "for". Otherwise
463 if (is_host_alias(rbuf, ctl))
464 ok = strstr(sp, "for ");
477 while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
482 /* uh oh -- whitespace here can't be right! */
491 received_for = alloca(strlen(rbuf)+1);
492 strcpy(received_for, rbuf);
493 if (outlevel == O_VERBOSE)
495 "found Received address `%s'",
499 #endif /* HAVE_RES_SEARCH */
503 else if (headers) /* OK, we're at end of headers now */
506 struct idlist *idp, *xmit_names;
507 int good_addresses, bad_addresses;
508 #ifdef HAVE_RES_SEARCH
509 int no_local_matches = FALSE;
510 #endif /* HAVE_RES_SEARCH */
512 /* cons up a list of local recipients */
513 xmit_names = (struct idlist *)NULL;
514 bad_addresses = good_addresses = 0;
515 #ifdef HAVE_RES_SEARCH
516 /* is this a multidrop box? */
519 if (envto) /* We have the actual envelope addressee */
520 find_server_names(envto, ctl, &xmit_names);
521 else if (received_for)
523 * We have the Received for addressee.
524 * It has to be a mailserver address, or we
525 * wouldn't have got here.
527 map_name(received_for, ctl, &xmit_names);
531 * We haven't extracted the envelope address.
532 * So check all the header addresses.
534 find_server_names(tohdr, ctl, &xmit_names);
535 find_server_names(cchdr, ctl, &xmit_names);
536 find_server_names(bcchdr, ctl, &xmit_names);
540 no_local_matches = TRUE;
541 save_str(&xmit_names, -1, user);
542 if (outlevel == O_VERBOSE)
544 "no local matches, forwarding to %s",
548 else /* it's a single-drop box, use first localname */
549 #endif /* HAVE_RES_SEARCH */
550 save_str(&xmit_names, -1, ctl->localnames->id);
552 /* time to address the message */
553 if (ctl->mda[0]) /* we have a declared MDA */
559 * We go through this in order to be able to handle very
560 * long lists of users and (re)implement %s.
562 for (idp = xmit_names; idp; idp = idp->next)
563 length += (strlen(idp->id) + 1);
564 names = (char *)alloca(length);
566 for (idp = xmit_names; idp; idp = idp->next)
568 strcat(names, idp->id);
571 cmd = (char *)alloca(strlen(ctl->mda) + length);
572 sprintf(cmd, ctl->mda, names);
573 if (outlevel == O_VERBOSE)
574 error(0, 0, "about to deliver with: %s", cmd);
578 * Arrange to run with user's permissions if we're root.
579 * This will initialize the ownership of any files the
580 * MDA creates properly. (The seteuid call is available
581 * under all BSDs and Linux)
584 #endif /* HAVE_SETEUID */
586 sinkfp = popen(cmd, "w");
589 /* this will fail quietly if we didn't start as root */
591 #endif /* HAVE_SETEUID */
595 error(0, 0, "MDA open failed");
599 sigchld = signal(SIGCHLD, SIG_DFL);
605 /* build a connection to the SMTP listener */
606 if (ctl->mda[0] == '\0' && ((sinkfp = smtp_open(ctl)) == NULL))
608 free_str_list(&xmit_names);
609 error(0, 0, "SMTP connect failed");
614 * Try to get the SMTP listener to take the header
615 * From address as MAIL FROM (this makes the logging
616 * nicer). If it won't, fall back on the calling-user
617 * ID. This won't affect replies, which use the header
618 * From address anyway.
620 if (!fromhdr || !(ap = nxtaddr(fromhdr)))
622 if (SMTP_from(sinkfp, user) != SM_OK)
623 return(PS_SMTP); /* should never happen */
625 else if (SMTP_from(sinkfp, ap) != SM_OK)
626 if (smtp_response == 571)
629 * SMTP listener explicitly refuses to deliver
630 * mail coming from this address, probably due
631 * to an anti-spam domain exclusion. Respect
634 sinkfp = (FILE *)NULL;
637 else if (SMTP_from(sinkfp, user) != SM_OK)
638 return(PS_SMTP); /* should never happen */
640 /* now list the recipient addressees */
641 for (idp = xmit_names; idp; idp = idp->next)
642 if (SMTP_rcpt(sinkfp, idp->id) == SM_OK)
649 "SMTP listener doesn't like recipient address `%s'", idp->id);
651 if (!good_addresses && SMTP_rcpt(sinkfp, user) != SM_OK)
654 "can't even send to calling user!");
658 /* tell it we're ready to send data */
664 /* change continuation markers back to regular newlines */
665 for (cp = headers; cp < headers + oldlen; cp++)
669 /* replace all LFs with CR-LF before sending to the SMTP server */
672 char *newheaders = xmalloc(1 + oldlen * 2);
674 oldlen = strcrlf(newheaders, headers, oldlen);
676 headers = newheaders;
679 /* write all the headers */
682 n = fwrite(headers, 1, oldlen, sinkfp);
684 n = SockWrite(headers, 1, oldlen, sinkfp);
690 error(0, errno, "writing RFC822 headers");
694 signal(SIGCHLD, sigchld);
698 else if (outlevel == O_VERBOSE)
703 /* write error notifications */
704 #ifdef HAVE_RES_SEARCH
705 if (no_local_matches || bad_addresses)
708 #endif /* HAVE_RES_SEARCH */
711 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
714 (void) strcpy(errhd, "X-Fetchmail-Warning: ");
715 #ifdef HAVE_RES_SEARCH
716 if (no_local_matches)
718 strcat(errhd, "no recipient addresses matched declared local names");
722 #endif /* HAVE_RES_SEARCH */
726 strcat(errhd, "SMTP listener rejected local recipient addresses: ");
727 errlen = strlen(errhd);
728 for (idp = xmit_names; idp; idp = idp->next)
730 errlen += strlen(idp->id) + 2;
732 errmsg = alloca(errlen+3);
733 (void) strcpy(errmsg, errhd);
734 for (idp = xmit_names; idp; idp = idp->next)
737 strcat(errmsg, idp->id);
739 strcat(errmsg, ", ");
743 strcat(errmsg, "\n");
746 fputs(errmsg, sinkfp);
749 free_str_list(&xmit_names);
752 /* SMTP byte-stuffing */
757 SockWrite(bufp, 1, 1, sinkfp);
759 /* replace all LFs with CR-LF in the line */
762 char *newbufp = xmalloc(1 + strlen(bufp) * 2);
764 strcrlf(newbufp, bufp, strlen(bufp));
768 /* ship out the text line */
771 n = fwrite(bufp, 1, strlen(bufp), sinkfp);
773 n = SockWrite(bufp, 1, strlen(bufp), sinkfp);
779 error(0, errno, "writing message text");
783 signal(SIGCHLD, sigchld);
787 else if (outlevel == O_VERBOSE)
794 if (outlevel == O_VERBOSE)
801 /* close the delivery pipe, we'll reopen before next message */
803 signal(SIGCHLD, sigchld);
806 error(0, 0, "MDA exited abnormally or returned nonzero status");
812 /* write message terminator */
813 if (SMTP_eom(sinkfp) != SM_OK)
815 error(0, 0, "SMTP listener refused delivery");
825 kerberos_auth (socket, canonical)
826 /* authenticate to the server host using Kerberos V4 */
827 int socket; /* socket to server host */
828 const char *canonical; /* server name */
834 Key_schedule schedule;
837 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
838 rem = (krb_sendauth (0L, socket, ticket, "pop",
840 ((char *) (krb_realmofhost (canonical))),
845 ((struct sockaddr_in *) 0),
846 ((struct sockaddr_in *) 0),
851 error(0, 0, "kerberos error %s", (krb_get_err_text (rem)));
856 #endif /* KERBEROS_V4 */
858 int do_protocol(ctl, proto)
859 /* retrieve messages from server using given protocol method table */
860 struct query *ctl; /* parsed options with merged-in defaults */
861 const struct method *proto; /* protocol method table */
864 char *msg, *sp, *cp, realname[HOSTLEN];
868 if (ctl->authenticate == A_KERBEROS)
870 error(0, 0, "Kerberos support not linked.");
873 #endif /* KERBEROS_V4 */
875 /* lacking methods, there are some options that may fail */
878 /* check for unsupported options */
881 "Option --flush is not supported with %s",
885 else if (ctl->fetchall) {
887 "Option --all is not supported with %s",
892 if (!proto->getsizes && ctl->limit)
895 "Option --limit is not supported with %s",
902 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
904 error_init(poll_interval == 0 && !logfile);
906 /* set up the server-nonresponse timeout */
907 sigsave = signal(SIGVTALRM, vtalarm_handler);
908 vtalarm(mytimeout = ctl->timeout);
910 if ((js = setjmp(restart)) == 1)
913 "timeout after %d seconds waiting for %s.",
914 ctl->timeout, ctl->servernames->id);
919 /* error message printed at point of longjmp */
924 char buf [POPBUFSIZE+1];
925 int *msgsizes, len, num, count, new, deletions = 0;
927 /* execute pre-initialization command, if any */
928 if (ctl->preconnect[0] && (ok = system(ctl->preconnect)))
930 sprintf(buf, "pre-connection command failed with status %d", ok);
936 /* open a socket to the mail server */
937 if ((sockfp = SockOpen(ctl->servernames->id,
938 ctl->port ? ctl->port : protocol->port)) == NULL)
940 if (errno != EHOSTUNREACH)
941 error(0, errno, "connecting to host");
947 if (ctl->authenticate == A_KERBEROS)
949 ok = kerberos_auth(fileno(sockfp), ctl->canonical_name);
952 vtalarm(ctl->timeout);
954 #endif /* KERBEROS_V4 */
956 /* accept greeting message from mail server */
957 ok = (protocol->parse_response)(sockfp, buf);
960 vtalarm(ctl->timeout);
963 * Try to parse the host's actual name out of the greeting message.
964 * We do this so that the progress messages will make sense even
965 * if the connection is indirected through ssh --- *don't* use
966 * this for error logging as the names in that should correlate
967 * directly back to rc file entries.
969 * This assumes that the first space-delimited token found
970 * that contains at least two dots (with the characters on
971 * each side of the dot alphanumeric to exclude version
972 * numbers) is the hostname. If no such token is found, fall
973 * back on the .fetchmailrc id.
976 for (cp = buf; *cp; cp++)
980 case 0: /* looking for blank-delimited token */
988 case 1: /* looking for first dot */
991 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
995 case 2: /* looking for second dot */
998 else if (*cp == '.' && isalpha(cp[1]) && isalpha(cp[-1]))
1002 case 3: /* looking for trailing space */
1014 char *tp = realname;
1021 strcpy(realname, ctl->servernames->id);
1023 /* try to get authorized to fetch mail */
1024 shroud = ctl->password;
1025 ok = (protocol->getauth)(sockfp, ctl, buf);
1026 shroud = (char *)NULL;
1031 vtalarm(ctl->timeout);
1033 /* compute number of messages and number of new messages waiting */
1034 ok = (protocol->getrange)(sockfp, ctl, &count, &new);
1037 vtalarm(ctl->timeout);
1039 /* show user how many messages we downloaded */
1040 if (outlevel > O_SILENT)
1042 error(0, 0, "No mail from %s@%s",
1047 if (new != -1 && (count - new) > 0)
1048 error(0, 0, "%d message%s (%d seen) from %s@%s.",
1049 count, count > 1 ? "s" : "", count-new,
1053 error(0, 0, "%d message%s from %s@%s.", count, count > 1 ? "s" : "",
1058 /* we may need to get sizes in order to check message limits */
1059 msgsizes = (int *)NULL;
1060 if (!ctl->fetchall && proto->getsizes && ctl->limit)
1062 msgsizes = (int *)alloca(sizeof(int) * count);
1064 ok = (proto->getsizes)(sockfp, count, msgsizes);
1067 vtalarm(ctl->timeout);
1073 if (new == -1 || ctl->fetchall)
1075 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
1081 * What forces this code is that in POP3 and IMAP2BIS you can't
1082 * fetch a message without having it marked `seen'. In IMAP4,
1083 * on the other hand, you can (peek_capable is set to convey
1086 * The result of being unable to peek is that if there's
1087 * any kind of transient error (DNS lookup failure, or
1088 * sendmail refusing delivery due to process-table limits)
1089 * the message will be marked "seen" on the server without
1090 * having been delivered. This is not a big problem if
1091 * fetchmail is running in foreground, because the user
1092 * will see a "skipped" message when it next runs and get
1095 * But in daemon mode this leads to the message being silently
1096 * ignored forever. This is not acceptable.
1098 * We compensate for this by checking the error count from the
1099 * previous pass and forcing all messages to be considered new
1102 int force_retrieval = !peek_capable && (ctl->errcount > 0);
1106 /* read, forward, and delete messages */
1107 for (num = 1; num <= count; num++)
1109 int toolarge = msgsizes && (msgsizes[num-1] > ctl->limit);
1110 int fetch_it = ctl->fetchall ||
1111 (!toolarge && (force_retrieval || !(protocol->is_old && (protocol->is_old)(sockfp,ctl,num))));
1113 /* we may want to reject this message if it's old */
1116 if (outlevel > O_SILENT)
1118 error_build("skipping message %d", num);
1120 error_build(" (oversized, %d bytes)", msgsizes[num-1]);
1125 /* request a message */
1126 ok = (protocol->fetch)(sockfp, ctl, num, &len);
1129 vtalarm(ctl->timeout);
1131 if (outlevel > O_SILENT)
1133 error_build("reading message %d", num);
1135 error_build(" (%d bytes)", len);
1136 if (outlevel == O_VERBOSE)
1137 error_complete(0, 0, "");
1142 /* read the message and ship it to the output sink */
1143 ok = gen_readmsg(sockfp,
1145 protocol->delimited,
1149 vtalarm(ctl->timeout);
1151 /* tell the server we got it OK and resynchronize */
1152 if (protocol->trail)
1154 ok = (protocol->trail)(sockfp, ctl, num);
1157 vtalarm(ctl->timeout);
1162 * At this point in flow of control, either we've bombed
1163 * on a protocol error or had delivery refused by the SMTP
1164 * server (unlikely -- I've never seen it) or we've seen
1165 * `accepted for delivery' and the message is shipped.
1166 * It's safe to mark the message seen and delete it on the
1170 /* maybe we delete this message now? */
1171 if (protocol->delete
1172 && (fetch_it ? !ctl->keep : ctl->flush))
1175 if (outlevel > O_SILENT)
1176 error_complete(0, 0, " flushed");
1177 ok = (protocol->delete)(sockfp, ctl, num);
1180 vtalarm(ctl->timeout);
1181 delete_str(&ctl->newsaved, num);
1183 else if (outlevel > O_SILENT)
1184 error_complete(0, 0, " not flushed");
1186 /* perhaps this as many as we're ready to handle */
1187 if (ctl->fetchlimit && ctl->fetchlimit <= num)
1191 /* remove all messages flagged for deletion */
1192 if (protocol->expunge_cmd && deletions > 0)
1194 ok = gen_transact(sockfp, protocol->expunge_cmd);
1197 vtalarm(ctl->timeout);
1200 ok = gen_transact(sockfp, protocol->exit_cmd);
1208 ok = gen_transact(sockfp, protocol->exit_cmd);
1217 vtalarm(ctl->timeout);
1218 if (ok != 0 && ok != PS_SOCKET)
1219 gen_transact(sockfp, protocol->exit_cmd);
1230 msg = "authorization";
1233 msg = "missing or bad RFC822 header";
1239 msg = "client/server synchronization";
1242 msg = "client/server protocol";
1245 msg = "SMTP transaction";
1248 error(0, 0, "undefined");
1251 if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
1252 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
1253 error(0, 0, "%s error while fetching from %s", msg, ctl->servernames->id);
1256 signal(SIGVTALRM, sigsave);
1260 #if defined(HAVE_STDARG_H)
1261 void gen_send(FILE *sockfp, char *fmt, ... )
1262 /* assemble command in printf(3) style and send to the server */
1264 void gen_send(sockfp, fmt, va_alist)
1265 /* assemble command in printf(3) style and send to the server */
1266 FILE *sockfp; /* socket to which server is connected */
1267 const char *fmt; /* printf-style format */
1271 char buf [POPBUFSIZE+1];
1274 if (protocol->tagged)
1275 (void) sprintf(buf, "%s ", GENSYM);
1279 #if defined(HAVE_STDARG_H)
1284 vsprintf(buf + strlen(buf), fmt, ap);
1287 strcat(buf, "\r\n");
1288 SockWrite(buf, 1, strlen(buf), sockfp);
1290 if (outlevel == O_VERBOSE)
1294 if (shroud && (cp = strstr(buf, shroud)))
1295 memset(cp, '*', strlen(shroud));
1296 buf[strlen(buf)-1] = '\0';
1297 error(0, 0, "%s> %s", protocol->name, buf);
1301 #if defined(HAVE_STDARG_H)
1302 int gen_transact(FILE *sockfp, char *fmt, ... )
1303 /* assemble command in printf(3) style, send to server, accept a response */
1305 int gen_transact(sockfp, fmt, va_alist)
1306 /* assemble command in printf(3) style, send to server, accept a response */
1307 FILE *sockfp; /* socket to which server is connected */
1308 const char *fmt; /* printf-style format */
1313 char buf [POPBUFSIZE+1];
1316 if (protocol->tagged)
1317 (void) sprintf(buf, "%s ", GENSYM);
1321 #if defined(HAVE_STDARG_H)
1326 vsprintf(buf + strlen(buf), fmt, ap);
1329 strcat(buf, "\r\n");
1330 SockWrite(buf, 1, strlen(buf), sockfp);
1332 if (outlevel == O_VERBOSE)
1336 if (shroud && (cp = strstr(buf, shroud)))
1337 memset(cp, '*', strlen(shroud));
1338 buf[strlen(buf)-1] = '\0';
1339 error(0, 0, "%s> %s", protocol->name, buf);
1342 /* we presume this does its own response echoing */
1343 ok = (protocol->parse_response)(sockfp, buf);
1349 /* driver.c ends here */