2 * \file transact.c -- transaction primitives for the fetchmail driver loop
4 * Copyright 2001 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
14 #endif /* HAVE_MEMORY_H */
15 #if defined(STDC_HEADERS)
18 #if defined(HAVE_UNISTD_H)
21 #if defined(HAVE_STDARG_H)
29 #ifdef HAVE_NET_SOCKET_H
30 #include <net/socket.h>
32 #include <sys/socket.h>
38 #include "fetchmail.h"
40 /** Macro to clamp the argument so it is >= INT_MIN. */
41 #define _FIX_INT_MIN(x) ((x) < INT_MIN ? INT_MIN : (x))
42 /** Macro to clamp the argument so it is <= INT_MAX. */
43 #define _FIX_INT_MAX(x) ((x) > INT_MAX ? INT_MAX : (x))
44 /** Macro to clamp the argument so it is representable as an int. */
45 #define CAST_TO_INT(x) ((int)(_FIX_INT_MIN(_FIX_INT_MAX(x))))
46 /** Macro to clamp the unsigned argument so it is representable as an int. */
47 #define UCAST_TO_INT(x) ((int)(_FIX_INT_MAX(x)))
49 /* global variables: please reinitialize them explicitly for proper
50 * working in daemon mode */
52 /* session variables initialized in init_transact() */
53 int suppress_tags = FALSE; /**< emit tags in the protocol? */
54 char tag[TAGLEN]; /**< buffer for the tag */
55 static int tagnum; /**< local counter for the tag */
56 /** Macro to generate the tag and store it in #tag. */
57 #define GENSYM (sprintf(tag, "A%04d", ++tagnum % TAGMOD), tag)
58 static const struct method *protocol; /**< description of the protocol used for the current poll */
59 char shroud[PASSWORDLEN*2+3]; /**< string to shroud in debug output */
61 /* session variables initialized in do_session() */
62 int mytimeout; /**< value of nonreponse timeout */
64 /* mail variables initialized in readheaders() */
65 struct msgblk msgblk; /**< stores attributes of the currently processed message */
66 static int accept_count /** count of accepted recipients */, reject_count /** count of rejected recipients */;
68 /** add given address to xmit_names if it exactly matches a full address
69 * \returns nonzero if matched */
70 static int map_address(const char *addr,/**< address to match */
71 struct query *ctl, /**< contains list of aliases */
72 struct idlist **xmit_names /**< list of recipient names */)
76 lname = idpair_find(&ctl->localnames, addr);
78 if (outlevel >= O_DEBUG)
79 report(stdout, GT_("mapped address %s to local %s\n"), addr, lname);
80 save_str(xmit_names, lname, XMIT_ACCEPT);
86 /** add given name to xmit_names if it matches declared localnames */
87 static void map_name(const char *name, struct query *ctl, struct idlist **xmit_names)
88 /** \param name name to map */
89 /** \param ctl list of permissible aliases */
90 /** \param xmit_names list of recipient names parsed out */
94 lname = idpair_find(&ctl->localnames, name);
95 if (!lname && ctl->wildcard)
98 if (lname != (char *)NULL)
100 if (outlevel >= O_DEBUG)
101 report(stdout, GT_("mapped %s to local %s\n"), name, lname);
102 save_str(xmit_names, lname, XMIT_ACCEPT);
107 static void find_server_names(const char *hdr,
109 struct idlist **xmit_names)
110 /** parse names out of a RFC822 header into an ID list */
111 /** \param hdr RFC822 header in question */
112 /** \param ctl list of permissible aliases */
113 /** \param xmit_names list of recipient names parsed out */
115 if (hdr == (char *)NULL)
121 for (cp = nxtaddr(hdr); cp != NULL; cp = nxtaddr(NULL))
126 * Handle empty address from a To: header containing only
133 * If the name of the user begins with a qmail virtual
134 * domain prefix, ignore the prefix. Doing this here
135 * means qvirtual will work either with ordinary name
136 * mapping or with a localdomains option.
138 if (ctl->server.qvirtual)
140 int sl = strlen(ctl->server.qvirtual);
142 if (!strncasecmp((char *)cp, ctl->server.qvirtual, sl))
146 if ((atsign = strchr((char *)cp, '@'))) {
149 /* try to match full address first, this takes
150 * precedence over localdomains and alias mappings */
151 if (map_address(cp, ctl, xmit_names))
155 * Does a trailing segment of the hostname match something
156 * on the localdomains list? If so, save the whole name
159 for (idp = ctl->server.localdomains; idp; idp = idp->next) {
162 rhs = atsign + (strlen(atsign) - strlen(idp->id));
164 (rhs[-1] == '.' || rhs[-1] == '@') &&
165 strcasecmp(rhs, idp->id) == 0)
167 if (outlevel >= O_DEBUG)
168 report(stdout, GT_("passed through %s matching %s\n"),
170 save_str(xmit_names, (const char *)cp, XMIT_ACCEPT);
176 /* if we matched a local domain, idp != NULL */
180 * Check to see if the right-hand part is an alias
181 * or MX equivalent of the mailserver. If it's
182 * not, skip this name. If it is, we'll keep
183 * going and try to find a mapping to a client name.
185 if (!is_host_alias(atsign+1, ctl, &ai0))
187 save_str(xmit_names, cp, XMIT_REJECT);
193 map_name(cp, ctl, xmit_names);
201 * Return zero on a syntactically invalid address, nz on a valid one.
203 * This used to be strchr(a, '.'), but it turns out that lines like this
205 * Received: from punt-1.mail.demon.net by mailstore for markb@example.com
206 * id 938765929:10:27223:2; Fri, 01 Oct 99 08:18:49 GMT
208 * are not uncommon. So now we just check that the following token is
209 * not itself an email address.
211 #define VALID_ADDRESS(a) (!strchr((a), '@'))
213 /** write \a value into \a rbuf, indexed by \a tp, if there is
214 * sufficient room left. */
215 #define RBUF_WRITE(value) do { if (tp < rbuf+sizeof(rbuf)-1) *tp++=(value); } while(0)
217 /** Try to extract real address from the Received line.
218 * If a valid Received: line is found, we return the full address in
219 * a buffer which can be parsed from nxtaddr(). This is to ensure that
220 * the local domain part of the address can be passed along in
221 * find_server_names() if it contains one.
222 * Note: We should return a dummy header containing the address
223 * which makes nxtaddr() behave correctly.
225 static char *parse_received(struct query *ctl, char *bufp)
227 char *base, *ok = (char *)NULL;
228 static char rbuf[HOSTLEN + USERNAMELEN + 4];
229 struct addrinfo *ai0;
232 * Try to extract the real envelope addressee. We look here
233 * specifically for the mailserver's Received line.
234 * Note: this will only work for sendmail, or an MTA that
235 * shares sendmail's convention for embedding the envelope
236 * address in the Received line. Sendmail itself only
237 * does this when the mail has a single recipient.
239 if (outlevel >= O_DEBUG)
240 report(stdout, GT_("analyzing Received line:\n%s"), bufp);
242 /* search for whitepace-surrounded "by" followed by valid address */
243 for (base = bufp; ; base = ok + 2)
245 if (!(ok = strstr(base, "by")))
247 else if (!isspace((unsigned char)ok[-1]) || !isspace((unsigned char)ok[2]))
253 /* extract space-delimited token after "by" */
254 for (sp = ok + 2; isspace((unsigned char)*sp); sp++)
257 for (; *sp && !isspace((unsigned char)*sp); sp++)
261 /* look for valid address */
262 if (VALID_ADDRESS(rbuf))
265 ok = sp - 1; /* arrange to skip this token */
271 * If it's a DNS name of the mail server, look for the
272 * recipient name after a following "for". Otherwise
275 if (is_host_alias(rbuf, ctl, &ai0))
277 if (outlevel >= O_DEBUG)
279 GT_("line accepted, %s is an alias of the mailserver\n"), rbuf);
283 if (outlevel >= O_DEBUG)
285 GT_("line rejected, %s is not an alias of the mailserver\n"),
290 /* search for whitepace-surrounded "for" followed by xxxx@yyyy */
291 for (base = ok + 4 + strlen(rbuf); ; base = ok + 2)
293 if (!(ok = strstr(base, "for")))
295 else if (!isspace((unsigned char)ok[-1]) || !isspace((unsigned char)ok[3]))
301 /* extract space-delimited token after "for" */
302 for (sp = ok + 3; isspace((unsigned char)*sp); sp++)
305 for (; !isspace((unsigned char)*sp); sp++)
309 if (strchr(rbuf, '@'))
312 ok = sp - 1; /* arrange to skip this token */
317 flag want_gt = FALSE;
320 /* char after "for" could be space or a continuation newline */
321 for (sp = ok + 4; isspace((unsigned char)*sp); sp++)
324 RBUF_WRITE(':'); /* Here is the hack. This is to be friends */
325 RBUF_WRITE(' '); /* with nxtaddr()... */
331 while (*sp == '@') /* skip routes */
332 while (*sp && *sp++ != ':')
335 && (want_gt ? (*sp != '>') : !isspace((unsigned char)*sp))
337 if (!isspace((unsigned char)*sp))
344 /* uh oh -- whitespace here can't be right! */
350 if (strlen(rbuf) <= 3) /* apparently nothing has been found */
358 if (outlevel >= O_DEBUG)
359 report(stdout, GT_("no Received address found\n"));
364 if (outlevel >= O_DEBUG) {
365 char *lf = rbuf + strlen(rbuf)-1;
367 if (outlevel >= O_DEBUG)
368 report(stdout, GT_("found Received address `%s'\n"), rbuf+2);
375 /* shared by readheaders and readbody */
376 static int sizeticker; /**< internal state variable for print_ticker() */
378 /** Print ticker based on a amount of data transferred of \a bytes.
379 * Increments \a *tickervar by \a bytes, and if it exceeds
380 * \a SIZETICKER, print a dot and reduce *tickervar by \a SIZETICKER. */
381 static void print_ticker(int *tickervar, int bytes)
384 while (*tickervar >= SIZETICKER)
391 *tickervar -= SIZETICKER;
395 /** Check if \a s is equal to a LF or CR LF sequence, followed by a NUL
396 * byte. \todo FIXME merge this with end_of_header? */
397 #define EMPTYLINE(s) (((s)[0] == '\r' && (s)[1] == '\n' && (s)[2] == '\0') \
398 || ((s)[0] == '\n' && (s)[1] == '\0'))
400 /** Check if \a s is an empty line. Accept "\r*\n" as EOH in order to be bulletproof against broken survers */
401 static int end_of_header (const char *s)
405 return (s[0] == '\n' && s[1] == '\0');
408 /** read message headers and ship to SMTP or MDA */
409 int readheaders(int sock,
414 flag *suppress_readbody)
415 /** \param sock to which the server is connected */
416 /** \param fetchlen length of message according to fetch response */
417 /** \param reallen length of message according to getsizes */
418 /** \param ctl query control record */
419 /** \param num index of message */
420 /** \param suppress_readbody output: whether call to readbody() should be supressed */
425 struct addrblk *next;
427 struct addrblk *to_addrchain = NULL;
428 struct addrblk **to_chainptr = &to_addrchain;
429 struct addrblk *resent_to_addrchain = NULL;
430 struct addrblk **resent_to_chainptr = &resent_to_addrchain;
432 char buf[MSGBUFSIZE+1];
433 int from_offs, reply_to_offs, resent_from_offs;
434 int app_from_offs, sender_offs, resent_sender_offs;
436 char *received_for, *rcv, *cp;
437 static char *delivered_to = NULL;
438 int n, oldlen, ch, remaining, skipcount;
440 int delivered_to_count;
442 flag no_local_matches = FALSE;
444 int olderrs, good_addresses, bad_addresses;
445 int retain_mail = 0, refuse_mail = 0;
446 flag already_has_return_path = FALSE;
450 msgblk.return_path[0] = '\0';
451 olderrs = ctl->errcount;
453 /* read message headers */
454 msgblk.reallen = reallen;
457 * We used to free the header block unconditionally at the end of
458 * readheaders, but it turns out that if close_sink() hits an error
459 * condition the code for sending bouncemail will actually look
460 * at the freed storage and coredump...
462 xfree(msgblk.headers);
463 free_str_list(&msgblk.recipients);
466 /* initially, no message digest */
467 memset(ctl->digest, '\0', sizeof(ctl->digest));
470 from_offs = reply_to_offs = resent_from_offs = app_from_offs =
471 sender_offs = resent_sender_offs = env_offs = -1;
475 delivered_to_count = 0;
478 for (remaining = fetchlen; remaining > 0 || protocol->delimited; )
482 line = (char *)xmalloc(sizeof(buf));
489 set_timeout(mytimeout);
490 if ((n = SockRead(sock, buf, sizeof(buf)-1)) == -1) {
498 * Smash out any NULs, they could wreak havoc later on.
499 * Some network stacks seem to generate these at random,
500 * especially (according to reports) at the beginning of the
501 * first read. NULs are illegal in RFC822 format.
503 for (sp = tp = buf; sp < buf + n; sp++)
516 * Try to gracefully handle the case where the length of a
517 * line exceeds MSGBUFSIZE.
519 if (n && buf[n-1] != '\n')
521 rline = (char *) realloc(line, linelen + 1);
528 memcpy(line + linelen - n, buf, n);
529 line[linelen] = '\0';
530 ch = ' '; /* So the next iteration starts */
534 /* lines may not be properly CRLF terminated; fix this for qmail */
535 /* we don't want to overflow the buffer here */
536 if (ctl->forcecr && buf[n-1]=='\n' && (n==1 || buf[n-2]!='\r'))
539 rline = (char *) realloc(line, linelen + 2);
546 memcpy(line + linelen - n, buf, n - 1);
547 tcp = line + linelen - 1;
551 /* n++; - not used later on */
556 rline = (char *) realloc(line, linelen + 1);
563 memcpy(line + linelen - n, buf, n + 1);
566 /* check for end of headers */
567 if (end_of_header(line))
570 if (linelen != strlen (line))
573 goto process_headers;
577 * Check for end of message immediately. If one of your folders
578 * has been mangled, the delimiter may occur directly after the
581 if (protocol->delimited && line[0] == '.' && EMPTYLINE(line+1))
583 if (suppress_readbody)
584 *suppress_readbody = TRUE;
585 goto eoh; /* above */
589 * At least one brain-dead website (netmind.com) is known to
590 * send out robotmail that's missing the RFC822 delimiter blank
591 * line before the body! Without this check fetchmail segfaults.
592 * With it, we treat such messages as spam and refuse them.
594 * Frederic Marchal reported in February 2006 that hotmail
595 * or something improperly wrapped a very long TO header
596 * (wrapped without inserting whitespace in the continuation
597 * line) and found that this code thus refused a message
598 * that should have been delivered.
600 * XXX FIXME: we should probably wrap the message up as
601 * message/rfc822 attachment and forward to postmaster (Rob
605 && !ctl->server.badheader == BHACCEPT
606 && !isspace((unsigned char)line[0])
607 && !strchr(line, ':'))
609 if (linelen != strlen (line))
611 if (outlevel > O_SILENT)
613 GT_("incorrect header line found - see manpage for bad-header option\n"));
614 if (outlevel >= O_VERBOSE)
615 report (stdout, GT_("line: %s"), line);
619 /* check for RFC822 continuations */
620 set_timeout(mytimeout);
624 (ch == ' ' || ch == '\t'); /* continuation to next line? */
626 /* write the message size dots */
627 if ((outlevel > O_SILENT && outlevel < O_VERBOSE) && linelen > 0)
629 print_ticker(&sizeticker, linelen);
633 * Decode MIME encoded headers. We MUST do this before
634 * looking at the Content-Type / Content-Transfer-Encoding
635 * headers (RFC 2046).
637 if ( ctl->mimedecode )
641 /* the line is now shorter. So we retrace back till we find
642 * our terminating combination \n\0, we move backwards to
643 * make sure that we don't catch some \n\0 stored in the
644 * decoded part of the message */
645 for (tcp = line + linelen - 1; tcp > line && (*tcp != 0 || tcp[-1] != '\n'); tcp--) { }
646 if (tcp > line) linelen = tcp - line;
650 /* skip processing if we are going to retain or refuse this mail */
651 if (retain_mail || refuse_mail)
657 /* we see an ordinary (non-header, non-message-delimiter) line */
658 if (linelen != strlen (line))
662 * The University of Washington IMAP server (the reference
663 * implementation of IMAP4 written by Mark Crispin) relies
664 * on being able to keep base-UID information in a special
665 * message at the head of the mailbox. This message should
666 * neither be deleted nor forwarded.
668 * An example for such a message is (keep this in so people
669 * find it when looking where the special code is to handle the
672 * From MAILER-DAEMON Wed Nov 23 11:38:42 2005
673 * Date: 23 Nov 2005 11:38:42 +0100
674 * From: Mail System Internal Data <MAILER-DAEMON@mail.example.org>
675 * Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
676 * Message-ID: <1132742322@mail.example.org>
677 * X-IMAP: 1132742306 0000000001
680 * This text is part of the internal format of your mail folder, and is not
681 * a real message. It is created automatically by the mail system software.
682 * If deleted, important folder data will be lost, and it will be re-created
683 * with the data reset to initial values.
685 * This message is only visible if a POP3 server that is unaware
686 * of these UWIMAP messages is used besides UWIMAP or PINE.
688 * We will just check if the first message in the mailbox has an
693 * We disable this check under POP2 because there's no way to
694 * prevent deletion of the message. So at least we ought to
695 * forward it to the user so he or she will have some clue
696 * that things have gone awry.
698 if (servport("pop2") != servport(protocol->service))
699 #endif /* POP2_ENABLE */
700 if (num == 1 && !strncasecmp(line, "X-IMAP:", 7)) {
707 * This code prevents fetchmail from becoming an accessory after
708 * the fact to upstream sendmails with the `E' option on. It also
709 * copes with certain brain-dead POP servers (like NT's) that pass
710 * through Unix from_ lines.
712 * Either of these bugs can result in a non-RFC822 line at the
713 * beginning of the headers. If fetchmail just passes it
714 * through, the client listener may think the message has *no*
715 * headers (since the first) line it sees doesn't look
716 * RFC822-conformant) and fake up a set.
718 * What the user would see in this case is bogus (synthesized)
719 * headers, followed by a blank line, followed by the >From,
720 * followed by the real headers, followed by a blank line,
723 * We forestall this lossage by tossing anything that looks
724 * like an escaped or passed-through From_ line in headers.
725 * These aren't RFC822 so our conscience is clear...
727 if (!strncasecmp(line, ">From ", 6) || !strncasecmp(line, "From ", 5))
734 * We remove all Delivered-To: headers if dropdelivered is set
735 * - special care must be taken if Delivered-To: is also used
736 * as envelope at the same time.
738 * This is to avoid false mail loops errors when delivering
739 * local messages to and from a Postfix or qmail mailserver.
741 if (ctl->dropdelivered && !strncasecmp(line, "Delivered-To:", 13))
744 ctl->server.envelope == STRING_DISABLED ||
745 !ctl->server.envelope ||
746 strcasecmp(ctl->server.envelope, "Delivered-To") ||
747 delivered_to_count != ctl->server.envskip)
751 delivered_to_count++;
756 * If we see a Status line, it may have been inserted by an MUA
757 * on the mail host, or it may have been inserted by the server
758 * program after the headers in the transaction stream. This
759 * can actually hose some new-mail notifiers such as xbuffy,
760 * which assumes any Status line came from a *local* MDA and
761 * therefore indicates that the message has been seen.
763 * Some buggy POP servers (including at least the 3.3(20)
764 * version of the one distributed with IMAP) insert empty
765 * Status lines in the transaction stream; we'll chuck those
766 * unconditionally. Nonempty ones get chucked if the user
767 * turns on the dropstatus flag.
772 if (!strncasecmp(line, "Status:", 7))
774 else if (!strncasecmp(line, "X-Mozilla-Status:", 17))
779 while (*tcp && isspace((unsigned char)*tcp)) tcp++;
780 if (!*tcp || ctl->dropstatus)
789 line = reply_hack(line, ctl->server.truename, &linelen);
792 * OK, this is messy. If we're forwarding by SMTP, it's the
793 * SMTP-receiver's job (according to RFC821, page 22, section
794 * 4.1.1) to generate a Return-Path line on final delivery.
795 * The trouble is, we've already got one because the
796 * mailserver's SMTP thought *it* was responsible for final
799 * Stash away the contents of Return-Path (as modified by reply_hack)
800 * for use in generating MAIL FROM later on, then prevent the header
801 * from being saved with the others. In effect, we strip it off here.
803 * If the SMTP server conforms to the standards, and fetchmail gets the
804 * envelope sender from the Return-Path, the new Return-Path should be
805 * exactly the same as the original one.
807 * We do *not* want to ignore empty Return-Path headers. These should
808 * be passed through as a way of indicating that a message should
809 * not trigger bounces if delivery fails. What we *do* need to do is
810 * make sure we never try to rewrite such a blank Return-Path. We
811 * handle this with a check for <> in the rewrite logic above.
813 * Also, if an email has multiple Return-Path: headers, we only
814 * read the first occurance, as some spam email has more than one
818 if ((already_has_return_path==FALSE) && !strncasecmp("Return-Path:", line, 12) && (cp = nxtaddr(line)))
820 char nulladdr[] = "<>";
821 already_has_return_path = TRUE;
822 if (cp[0]=='\0') /* nxtaddr() strips the brackets... */
824 strncpy(msgblk.return_path, cp, sizeof(msgblk.return_path));
825 msgblk.return_path[sizeof(msgblk.return_path)-1] = '\0';
835 msgblk.headers = (char *)xmalloc(oldlen + 1);
836 (void) memcpy(msgblk.headers, line, linelen);
837 msgblk.headers[oldlen] = '\0';
839 line = msgblk.headers;
846 newlen = oldlen + linelen;
847 newhdrs = (char *) realloc(msgblk.headers, newlen + 1);
848 if (newhdrs == NULL) {
852 msgblk.headers = newhdrs;
853 memcpy(msgblk.headers + oldlen, line, linelen);
854 msgblk.headers[newlen] = '\0';
856 line = msgblk.headers + oldlen;
860 /* find offsets of various special headers */
861 if (!strncasecmp("From:", line, 5))
862 from_offs = (line - msgblk.headers);
863 else if (!strncasecmp("Reply-To:", line, 9))
864 reply_to_offs = (line - msgblk.headers);
865 else if (!strncasecmp("Resent-From:", line, 12))
866 resent_from_offs = (line - msgblk.headers);
867 else if (!strncasecmp("Apparently-From:", line, 16))
868 app_from_offs = (line - msgblk.headers);
870 * Netscape 4.7 puts "Sender: zap" in mail headers. Perverse...
872 * But a literal reading of RFC822 sec. 4.4.2 supports the idea
873 * that Sender: *doesn't* have to be a working email address.
875 * The definition of the Sender header in RFC822 says, in
876 * part, "The Sender mailbox specification includes a word
877 * sequence which must correspond to a specific agent (i.e., a
878 * human user or a computer program) rather than a standard
879 * address." That implies that the contents of the Sender
880 * field don't need to be a legal email address at all So
881 * ignore any Sender or Resent-Sender lines unless they
884 * (RFC2822 says the contents of Sender must be a valid mailbox
885 * address, which is also what RFC822 4.4.4 implies.)
887 else if (!strncasecmp("Sender:", line, 7) && (strchr(line, '@') || strchr(line, '!')))
888 sender_offs = (line - msgblk.headers);
889 else if (!strncasecmp("Resent-Sender:", line, 14) && (strchr(line, '@') || strchr(line, '!')))
890 resent_sender_offs = (line - msgblk.headers);
893 else if (!strncasecmp("Message-Id:", line, 11))
895 if (ctl->server.uidl)
899 line[IDLEN+12] = 0; /* prevent stack overflow */
900 sscanf(line+12, "%s", id);
901 if (!str_find( &ctl->newsaved, num))
903 struct idlist *newl = save_str(&ctl->newsaved,id,UID_SEEN);
904 newl->val.status.num = num;
908 #endif /* __UNUSED__ */
910 /* if multidrop is on, gather addressee headers */
913 if (!strncasecmp("To:", line, 3)
914 || !strncasecmp("Cc:", line, 3)
915 || !strncasecmp("Bcc:", line, 4)
916 || !strncasecmp("Apparently-To:", line, 14))
918 *to_chainptr = (struct addrblk *)xmalloc(sizeof(struct addrblk));
919 (*to_chainptr)->offset = (line - msgblk.headers);
920 to_chainptr = &(*to_chainptr)->next;
924 else if (!strncasecmp("Resent-To:", line, 10)
925 || !strncasecmp("Resent-Cc:", line, 10)
926 || !strncasecmp("Resent-Bcc:", line, 11))
928 *resent_to_chainptr = (struct addrblk *)xmalloc(sizeof(struct addrblk));
929 (*resent_to_chainptr)->offset = (line - msgblk.headers);
930 resent_to_chainptr = &(*resent_to_chainptr)->next;
931 *resent_to_chainptr = NULL;
934 else if (ctl->server.envelope != STRING_DISABLED)
936 if (ctl->server.envelope
937 && strcasecmp(ctl->server.envelope, "Received"))
939 if (env_offs == -1 && !strncasecmp(ctl->server.envelope,
941 strlen(ctl->server.envelope)))
943 if (skipcount++ < ctl->server.envskip)
945 env_offs = (line - msgblk.headers);
948 else if (!received_for && !strncasecmp("Received:", line, 9))
950 if (skipcount++ < ctl->server.envskip)
952 received_for = parse_received(ctl, line);
967 * This is the duplicate-message killer code.
969 * When mail delivered to a multidrop mailbox on the server is
970 * addressed to multiple people on the client machine, there will
971 * be one copy left in the box for each recipient. This is not a
972 * problem if we have the actual recipient address to dispatch on
973 * (e.g. because we've mined it out of sendmail trace headers, or
974 * a qmail Delivered-To line, or a declared sender envelope line).
976 * But if we're mining addressees out of the To/Cc/Bcc fields, and
977 * if the mail is addressed to N people, each recipient will
978 * get N copies. This is bad when N > 1.
980 * Foil this by suppressing all but one copy of a message with a
981 * given set of headers.
983 * Note: This implementation only catches runs of successive
984 * messages with the same ID, but that should be good
985 * enough. A more general implementation would have to store
986 * ever-growing lists of seen message-IDs; in a long-running
987 * daemon this would turn into a memory leak even if the
988 * implementation were perfect.
990 * Don't mess with this code casually. It would be way too easy
991 * to break it in a way that blackholed mail. Better to pass
992 * the occasional duplicate than to do that...
995 * The real fix however is to insist on Delivered-To: or similar
996 * headers and require that one copy per recipient be dropped.
997 * Everything else breaks sooner or later.
999 if (MULTIDROP(ctl) && msgblk.headers)
1004 MD5Update(&context, (unsigned char *)msgblk.headers, strlen(msgblk.headers));
1005 MD5Final(ctl->digest, &context);
1007 if (!received_for && env_offs == -1 && !delivered_to)
1010 * Hmmm...can MD5 ever yield all zeroes as a hash value?
1011 * If so there is a one in 18-quadrillion chance this
1012 * code will incorrectly nuke the first message.
1014 if (!memcmp(ctl->lastdigest, ctl->digest, DIGESTLEN))
1017 memcpy(ctl->lastdigest, ctl->digest, DIGESTLEN);
1021 * Hack time. If the first line of the message was blank, with no headers
1022 * (this happens occasionally due to bad gatewaying software) cons up
1023 * a set of fake headers.
1025 * If you modify the fake header template below, be sure you don't
1026 * make either From or To address @-less, otherwise the reply_hack
1027 * logic will do bad things.
1029 if (msgblk.headers == (char *)NULL)
1031 snprintf(buf, sizeof(buf),
1032 "From: FETCHMAIL-DAEMON\r\n"
1034 "Subject: Headerless mail from %s's mailbox on %s\r\n",
1035 user, fetchmailhost, ctl->remotename, ctl->server.truename);
1036 msgblk.headers = xstrdup(buf);
1040 * We can now process message headers before reading the text.
1041 * In fact we have to, as this will tell us where to forward to.
1044 /* Check for MIME headers indicating possible 8-bit data */
1045 ctl->mimemsg = MimeBodyType(msgblk.headers, ctl->mimedecode);
1048 if (ctl->server.sdps && sdps_envfrom)
1050 /* We have the real envelope return-path, stored out of band by
1051 * SDPS - that's more accurate than any header is going to be.
1053 strlcpy(msgblk.return_path, sdps_envfrom, sizeof(msgblk.return_path));
1056 #endif /* SDPS_ENABLE */
1058 * If there is a Return-Path address on the message, this was
1059 * almost certainly the MAIL FROM address given the originating
1060 * sendmail. This is the best thing to use for logging the
1061 * message origin (it sets up the right behavior for bounces and
1062 * mailing lists). Otherwise, fall down to the next available
1063 * envelope address (which is the most probable real sender).
1064 * *** The order is important! ***
1065 * This is especially useful when receiving mailing list
1066 * messages in multidrop mode. if a local address doesn't
1067 * exist, the bounce message won't be returned blindly to the
1068 * author or to the list itself but rather to the list manager
1069 * (ex: specified by "Sender:") which is much less annoying. This
1070 * is true for most mailing list packages.
1072 if( !msgblk.return_path[0] ){
1074 if (resent_sender_offs >= 0 && (ap = nxtaddr(msgblk.headers + resent_sender_offs)));
1075 else if (sender_offs >= 0 && (ap = nxtaddr(msgblk.headers + sender_offs)));
1076 else if (resent_from_offs >= 0 && (ap = nxtaddr(msgblk.headers + resent_from_offs)));
1077 else if (from_offs >= 0 && (ap = nxtaddr(msgblk.headers + from_offs)));
1078 else if (reply_to_offs >= 0 && (ap = nxtaddr(msgblk.headers + reply_to_offs)));
1079 else if (app_from_offs >= 0 && (ap = nxtaddr(msgblk.headers + app_from_offs))) {}
1080 /* multi-line MAIL FROM addresses confuse SMTP terribly */
1081 if (ap && !strchr(ap, '\n')) {
1082 strncpy(msgblk.return_path, ap, sizeof(msgblk.return_path));
1083 msgblk.return_path[sizeof(msgblk.return_path)-1] = '\0';
1087 /* cons up a list of local recipients */
1088 msgblk.recipients = (struct idlist *)NULL;
1089 accept_count = reject_count = 0;
1090 /* is this a multidrop box? */
1094 if (ctl->server.sdps && sdps_envto)
1096 /* We have the real envelope recipient, stored out of band by
1097 * SDPS - that's more accurate than any header is going to be.
1099 find_server_names(sdps_envto, ctl, &msgblk.recipients);
1102 #endif /* SDPS_ENABLE */
1103 if (env_offs > -1) { /* We have the actual envelope addressee */
1104 if (outlevel >= O_DEBUG) {
1105 const char *tmps = msgblk.headers + env_offs;
1106 size_t l = strcspn(tmps, "\r\n");
1107 report(stdout, GT_("Parsing envelope \"%s\" names \"%-.*s\"\n"), ctl->server.envelope, UCAST_TO_INT(l), tmps);
1109 find_server_names(msgblk.headers + env_offs, ctl, &msgblk.recipients);
1111 else if (delivered_to && ctl->server.envelope != STRING_DISABLED &&
1112 ctl->server.envelope && !strcasecmp(ctl->server.envelope, "Delivered-To"))
1114 if (outlevel >= O_DEBUG) {
1115 const char *tmps = delivered_to + 2 + strlen(ctl->server.envelope);
1116 size_t l = strcspn(tmps, "\r\n");
1117 report(stdout, GT_("Parsing envelope \"%s\" names \"%-.*s\"\n"), ctl->server.envelope, UCAST_TO_INT(l), tmps);
1119 find_server_names(delivered_to, ctl, &msgblk.recipients);
1120 xfree(delivered_to);
1121 } else if (received_for) {
1123 * We have the Received for addressee.
1124 * It has to be a mailserver address, or we
1125 * wouldn't have got here.
1126 * We use find_server_names() to let local
1127 * hostnames go through.
1129 if (outlevel >= O_DEBUG) {
1130 const char *tmps = received_for + 2;
1131 size_t l = strcspn(tmps, "\r\n");
1132 report(stdout, GT_("Parsing Received names \"%-.*s\"\n"), UCAST_TO_INT(l), tmps);
1134 find_server_names(received_for, ctl, &msgblk.recipients);
1137 * We haven't extracted the envelope address.
1138 * So check all the "Resent-To" header addresses if
1139 * they exist. If and only if they don't, consider
1140 * the "To" addresses.
1142 register struct addrblk *nextptr;
1143 if (outlevel >= O_DEBUG)
1144 report(stdout, GT_("No envelope recipient found, resorting to header guessing.\n"));
1145 if (resent_to_addrchain) {
1146 /* delete the "To" chain and substitute it
1147 * with the "Resent-To" list
1149 while (to_addrchain) {
1150 nextptr = to_addrchain->next;
1152 to_addrchain = nextptr;
1154 to_addrchain = resent_to_addrchain;
1155 resent_to_addrchain = NULL;
1157 /* now look for remaining adresses */
1158 while (to_addrchain) {
1159 if (outlevel >= O_DEBUG) {
1160 const char *tmps = msgblk.headers+to_addrchain->offset;
1161 size_t l = strcspn(tmps, "\r\n");
1162 report(stdout, GT_("Guessing from header \"%-.*s\".\n"), UCAST_TO_INT(l), tmps);
1165 find_server_names(msgblk.headers+to_addrchain->offset, ctl, &msgblk.recipients);
1166 nextptr = to_addrchain->next;
1168 to_addrchain = nextptr;
1173 no_local_matches = TRUE;
1174 save_str(&msgblk.recipients, run.postmaster, XMIT_ACCEPT);
1175 if (outlevel >= O_DEBUG)
1177 GT_("no local matches, forwarding to %s\n"),
1181 else /* it's a single-drop box, use first localname */
1182 save_str(&msgblk.recipients, ctl->localnames->id, XMIT_ACCEPT);
1186 * Time to either address the message or decide we can't deliver it yet.
1188 if (ctl->errcount > olderrs) /* there were DNS errors above */
1190 if (outlevel >= O_DEBUG)
1192 GT_("forwarding and deletion suppressed due to DNS errors\n"));
1193 return(PS_TRANSIENT);
1197 /* set up stuffline() so we can deliver the message body through it */
1198 if ((n = open_sink(ctl, &msgblk,
1199 &good_addresses, &bad_addresses)) != PS_SUCCESS)
1207 * Some server/sendmail combinations cause problems when our
1208 * synthetic Received line is before the From header. Cope
1211 if ((rcv = strstr(msgblk.headers, "Received:")) == (char *)NULL)
1212 rcv = msgblk.headers;
1213 /* handle ">Received:" lines too */
1214 while (rcv > msgblk.headers && rcv[-1] != '\n')
1216 if (rcv > msgblk.headers)
1221 n = stuffline(ctl, msgblk.headers);
1224 if (!run.invisible && n != -1)
1226 /* utter any per-message Received information we need here */
1227 if (ctl->server.trueaddr) {
1231 e = getnameinfo(ctl->server.trueaddr, ctl->server.trueaddr_len,
1232 saddr, sizeof(saddr), NULL, 0,
1235 snprintf(saddr, sizeof(saddr), "(%-.*s)", (int)(sizeof(saddr) - 3), gai_strerror(e));
1236 snprintf(buf, sizeof(buf),
1237 "Received: from %s [%s]\r\n",
1238 ctl->server.truename, saddr);
1240 snprintf(buf, sizeof(buf),
1241 "Received: from %s\r\n", ctl->server.truename);
1243 n = stuffline(ctl, buf);
1247 * We SHOULD (RFC-2821 sec. 4.4/p. 53) make sure to only use
1248 * IANA registered protocol names here.
1250 snprintf(buf, sizeof(buf),
1251 "\tby %s with %s (fetchmail-%s",
1255 if (ctl->server.tracepolls)
1257 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1258 " polling %s account %s",
1259 ctl->server.pollname,
1262 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1266 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), ")\r\n");
1267 n = stuffline(ctl, buf);
1271 if (good_addresses == 0)
1273 snprintf(buf+1, sizeof(buf)-1, "for <%s> (by default); ",
1274 rcpt_address (ctl, run.postmaster, 0));
1276 else if (good_addresses == 1)
1278 for (idp = msgblk.recipients; idp; idp = idp->next)
1279 if (idp->val.status.mark == XMIT_ACCEPT)
1280 break; /* only report first address */
1282 snprintf(buf+1, sizeof(buf)-1,
1283 "for <%s>", rcpt_address (ctl, idp->id, 1));
1284 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf)-1,
1286 MULTIDROP(ctl) ? "multi-drop" : "single-drop");
1291 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s\r\n",
1293 n = stuffline(ctl, buf);
1299 n = stuffline(ctl, rcv); /* ship out rest of msgblk.headers */
1303 report(stdout, GT_("writing RFC822 msgblk.headers\n"));
1308 if (want_progress())
1311 /* write error notifications */
1312 if (no_local_matches || has_nuls || bad_addresses)
1315 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
1318 strlcpy(errhd, "X-Fetchmail-Warning: ", sizeof(errhd));
1319 if (no_local_matches)
1321 if (reject_count != 1)
1322 strlcat(errhd, GT_("no recipient addresses matched declared local names"), sizeof(errhd));
1325 for (idp = msgblk.recipients; idp; idp = idp->next)
1326 if (idp->val.status.mark == XMIT_REJECT)
1328 snprintf(errhd+strlen(errhd), sizeof(errhd)-strlen(errhd),
1329 GT_("recipient address %s didn't match any local name"), idp->id);
1335 if (errhd[sizeof("X-Fetchmail-Warning: ")])
1336 snprintf(errhd+strlen(errhd), sizeof(errhd)-strlen(errhd), "; ");
1337 snprintf(errhd+strlen(errhd), sizeof(errhd)-strlen(errhd),
1338 GT_("message has embedded NULs"));
1343 if (errhd[sizeof("X-Fetchmail-Warning: ")])
1344 snprintf(errhd+strlen(errhd), sizeof(errhd)-strlen(errhd), "; ");
1345 snprintf(errhd+strlen(errhd), sizeof(errhd)-strlen(errhd),
1346 GT_("SMTP listener rejected local recipient addresses: "));
1347 errlen = strlen(errhd);
1348 for (idp = msgblk.recipients; idp; idp = idp->next)
1349 if (idp->val.status.mark == XMIT_RCPTBAD)
1350 errlen += strlen(idp->id) + 2;
1352 errmsg = (char *)xmalloc(errlen + 3);
1353 strcpy(errmsg, errhd);
1354 for (idp = msgblk.recipients; idp; idp = idp->next)
1355 if (idp->val.status.mark == XMIT_RCPTBAD)
1357 strcat(errmsg, idp->id);
1359 strcat(errmsg, ", ");
1364 strcat(errmsg, "\r\n");
1366 /* ship out the error line */
1367 stuffline(ctl, errmsg);
1369 if (errmsg != errhd)
1373 /* issue the delimiter line */
1378 n = stuffline(ctl, buf);
1380 if ((size_t)n == strlen(buf))
1386 int readbody(int sock, struct query *ctl, flag forward, int len)
1387 /** read and dispose of a message body presented on \a sock */
1388 /** \param ctl query control record */
1389 /** \param sock to which the server is connected */
1390 /** \param forward TRUE to forward */
1391 /** \param len length of message */
1394 char buf[MSGBUFSIZE+4];
1396 flag issoftline = FALSE;
1399 * Pass through the text lines in the body.
1401 * Yes, this wants to be ||, not &&. The problem is that in the most
1402 * important delimited protocol, POP3, the length is not reliable.
1403 * As usual, the problem is Microsoft brain damage; see FAQ item S2.
1404 * So, for delimited protocols we need to ignore the length here and
1405 * instead drop out of the loop with a break statement when we see
1406 * the message delimiter.
1408 while (protocol->delimited || len > 0)
1410 set_timeout(mytimeout);
1411 /* XXX FIXME: for undelimited protocols that ship the size, such
1412 * as IMAP, we might want to use the count of remaining characters
1413 * instead of the buffer size -- not for fetchmail 6.3.X though */
1414 if ((linelen = SockRead(sock, inbufp, sizeof(buf)-4-(inbufp-buf)))==-1)
1422 /* write the message size dots */
1425 print_ticker(&sizeticker, linelen);
1428 /* Mike Jones, Manchester University, 2006:
1429 * "To fix IMAP MIME Messages in which fetchmail adds the remainder of
1430 * the IMAP packet including the ')' character (part of the IMAP)
1431 * Protocol causing the addition of an extra MIME boundary locally."
1433 * However, we shouldn't do this for delimited protocols:
1434 * many POP3 servers (Microsoft, qmail) goof up message sizes
1435 * so we might end truncating messages prematurely.
1437 if (!protocol->delimited && linelen > len) {
1443 /* check for end of message */
1444 if (protocol->delimited && *inbufp == '.')
1446 if (EMPTYLINE(inbufp+1))
1449 msgblk.msglen--; /* subtract the size of the dot escape */
1452 msgblk.msglen += linelen;
1454 if (ctl->mimedecode && (ctl->mimemsg & MSG_NEEDS_DECODE)) {
1455 issoftline = UnMimeBodyline(&inbufp, protocol->delimited, issoftline);
1456 if (issoftline && (sizeof(buf)-1-(inbufp-buf) < 200))
1459 * Soft linebreak, but less than 200 bytes left in
1460 * input buffer. Rather than doing a buffer overrun,
1461 * ignore the soft linebreak, NL-terminate data and
1462 * deliver what we have now.
1463 * (Who writes lines longer than 2K anyway?)
1465 *inbufp = '\n'; *(inbufp+1) = '\0';
1470 /* ship out the text line */
1471 if (forward && (!issoftline))
1476 /* guard against very long lines */
1477 buf[MSGBUFSIZE+1] = '\r';
1478 buf[MSGBUFSIZE+2] = '\n';
1479 buf[MSGBUFSIZE+3] = '\0';
1481 n = stuffline(ctl, buf);
1485 report(stdout, GT_("error writing message text\n"));
1489 else if (want_progress())
1500 void init_transact(const struct method *proto)
1501 /** initialize state for the send and receive functions */
1503 suppress_tags = FALSE;
1505 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
1510 /** shroud a password in the given buffer */
1511 static void enshroud(char *buf)
1515 if (shroud[0] && (cp = strstr(buf, shroud)))
1519 sp = cp + strlen(shroud);
1527 #if defined(HAVE_STDARG_H)
1528 /** assemble command in printf(3) style and send to the server */
1529 void gen_send(int sock, const char *fmt, ... )
1531 void gen_send(sock, fmt, va_alist)
1532 int sock; /** socket to which server is connected */
1533 const char *fmt; /** printf-style format */
1537 char buf [MSGBUFSIZE+1];
1540 if (protocol->tagged && !suppress_tags)
1541 snprintf(buf, sizeof(buf) - 2, "%s ", GENSYM);
1545 #if defined(HAVE_STDARG_H)
1550 vsnprintf(buf + strlen(buf), sizeof(buf)-2-strlen(buf), fmt, ap);
1553 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "\r\n");
1554 SockWrite(sock, buf, strlen(buf));
1556 if (outlevel >= O_MONITOR)
1559 buf[strlen(buf)-2] = '\0';
1560 report(stdout, "%s> %s\n", protocol->name, buf);
1564 /** get one line of input from the server */
1565 int gen_recv(int sock /** socket to which server is connected */,
1566 char *buf /** buffer to receive input */,
1567 int size /** length of buffer */)
1570 int oldphase = phase; /* we don't have to be re-entrant */
1572 phase = SERVER_WAIT;
1573 set_timeout(mytimeout);
1574 if (SockRead(sock, buf, size) == -1)
1578 if(is_idletimeout())
1581 return(PS_IDLETIMEOUT);
1590 if (n > 0 && buf[n-1] == '\n')
1592 if (n > 0 && buf[n-1] == '\r')
1594 if (outlevel >= O_MONITOR)
1595 report(stdout, "%s< %s\n", protocol->name, buf);
1601 /** \addtogroup gen_recv_split
1603 * gen_recv_split() splits the response from a server which is too
1604 * long to fit into the buffer into multiple lines. If the prefix is
1605 * set as "MY FEATURES" and the response from the server is too long
1606 * to fit in the buffer, as in:
1608 * "MY FEATURES ABC DEF GHI JKLMNOPQRS TU VWX YZ"
1610 * Repeated calls to gen_recv_split() may return:
1612 * "MY FEATURES ABC DEF GHI"
1613 * "MY FEATURES JKLMNOPQRS"
1614 * "MY FEATURES TU VWX YZ"
1616 * A response not beginning with the prefix "MY FEATURES" will not be
1620 * - Declare a variable of type struct RecvSplit
1621 * - Call gen_recv_split_init() once
1622 * - Call gen_recv_split() in a loop, preferably with the same buffer
1623 * size as the "buf" array in struct RecvSplit
1626 static void overrun(const char *f, size_t l) __attribute__((noreturn));
1628 /** Internal error report function. If this happens, the calling site
1629 * needs to be adjusted to set a shorter prefix, or the prefix capacity
1630 * needs to be raised in struct RecvSplit. */
1631 static void overrun(const char *f, size_t l)
1633 report(stderr, GT_("Buffer too small. This is a bug in the caller of %s:%lu.\n"), f, (unsigned long)l);
1637 /** Initialize \a rs for later use by gen_recv_split. */
1638 void gen_recv_split_init (const char *prefix /** prefix to match/repeat */,
1639 struct RecvSplit *rs /** structure to be initialized */)
1641 if (strlcpy(rs->prefix, prefix, sizeof(rs->prefix)) > sizeof(rs->prefix))
1642 overrun(__FILE__, __LINE__);
1647 /** Function to split replies at blanks, and duplicate prefix.
1648 * gen_recv_split_init() must be called before this can be used. */
1649 int gen_recv_split(int sock /** socket to which server is connected */,
1650 char *buf /** buffer to receive input */,
1651 int size /** length of buffer, must be the same for all calls */,
1652 struct RecvSplit *rs /** cached information across calls */)
1655 int foundnewline = 0;
1657 int oldphase = phase; /* we don't have to be re-entrant */
1661 /* if this is not our first call, prepare the buffer */
1665 * if this condition is not met, we lose data
1666 * because the cached data does not fit into the buffer.
1667 * this cannot happen if size is the same throughout all calls.
1669 assert(strlen(rs->prefix) + strlen(rs->buf) + 1 <= (size_t)size);
1671 if ((strlcpy(buf, rs->prefix, size) >= (size_t)size)
1672 || (strlcat(buf, rs->buf, size) >= (size_t)size)) {
1673 overrun(__FILE__, __LINE__);
1677 /* clear the cache for the next call */
1682 if ((size_t)size > n) {
1685 phase = SERVER_WAIT;
1686 set_timeout(mytimeout);
1687 rr = SockRead(sock, buf + n, size - n);
1695 if (n > 0 && buf[n-1] == '\n')
1700 if (n > 0 && buf[n-1] == '\r')
1703 if (foundnewline /* we have found a complete line */
1704 || strncasecmp(buf, rs->prefix, strlen(rs->prefix)) /* mismatch in prefix */
1705 || !(p = strrchr(buf, ' ')) /* no space found in response */
1706 || p < buf + strlen(rs->prefix)) /* space is at the wrong location */
1708 if (outlevel >= O_MONITOR)
1709 report(stdout, "%s< %s\n", protocol->name, buf);
1713 /* we are ready to cache some information now. */
1715 if (strlcpy(rs->buf, p, sizeof(rs->buf)) >= sizeof(rs->buf)) {
1716 overrun(__FILE__, __LINE__);
1718 *p = '\0'; /* chop off what we've cached */
1719 if (outlevel >= O_MONITOR)
1720 report(stdout, "%s< %s\n", protocol->name, buf);
1721 if (outlevel >= O_DEBUG)
1722 report(stdout, "%s< %s%s...\n", protocol->name, rs->prefix, rs->buf);
1727 #if defined(HAVE_STDARG_H)
1728 int gen_transact(int sock, const char *fmt, ... )
1730 int gen_transact(int sock, fmt, va_alist)
1731 int sock; /** socket to which server is connected */
1732 const char *fmt; /** printf-style format */
1735 /** assemble command in printf(3) style, send to server, fetch a response */
1738 char buf [MSGBUFSIZE+1];
1740 int oldphase = phase; /* we don't have to be re-entrant */
1742 phase = SERVER_WAIT;
1744 if (protocol->tagged && !suppress_tags)
1745 snprintf(buf, sizeof(buf) - 2, "%s ", GENSYM);
1749 #if defined(HAVE_STDARG_H)
1754 vsnprintf(buf + strlen(buf), sizeof(buf)-2-strlen(buf), fmt, ap);
1757 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "\r\n");
1758 ok = SockWrite(sock, buf, strlen(buf));
1759 if (ok == -1 || (size_t)ok != strlen(buf)) {
1760 /* short write, bail out */
1764 if (outlevel >= O_MONITOR)
1767 buf[strlen(buf)-2] = '\0';
1768 report(stdout, "%s> %s\n", protocol->name, buf);
1771 /* we presume this does its own response echoing */
1772 ok = (protocol->parse_response)(sock, buf);
1778 /* transact.c ends here */