Postfix3.3.1
smtp_proto.c
[詳解]
1 /*++
2 /* NAME
3 /* smtp_proto 3
4 /* SUMMARY
5 /* client SMTP/LMTP protocol
6 /* SYNOPSIS
7 /* #include "smtp.h"
8 /*
9 /* int smtp_helo(state)
10 /* SMTP_STATE *state;
11 /*
12 /* int smtp_xfer(state)
13 /* SMTP_STATE *state;
14 /*
15 /* int smtp_rset(state)
16 /* SMTP_STATE *state;
17 /*
18 /* int smtp_quit(state)
19 /* SMTP_STATE *state;
20 /* DESCRIPTION
21 /* In the subsequent text, SMTP implies LMTP.
22 /* This module implements the client side of the SMTP protocol.
23 /*
24 /* smtp_helo() performs the initial handshake with the SMTP server.
25 /* When TLS is enabled, this includes STARTTLS negotiations.
26 /*
27 /* smtp_xfer() sends message envelope information followed by the
28 /* message data, and finishes the SMTP conversation. These operations
29 /* are combined in one function, in order to implement SMTP pipelining.
30 /* Recipients are marked as "done" in the mail queue file when
31 /* bounced or delivered. The message delivery status is updated
32 /* accordingly.
33 /*
34 /* smtp_rset() sends a single RSET command and waits for the
35 /* response. In case of a negative reply it sets the
36 /* CANT_RSET_THIS_SESSION flag.
37 /*
38 /* smtp_quit() sends a single QUIT command and waits for the
39 /* response if configured to do so. It always turns off connection
40 /* caching.
41 /* DIAGNOSTICS
42 /* smtp_helo(), smtp_xfer(), smtp_rset() and smtp_quit() return
43 /* 0 in case of success, -1 in case of failure. For smtp_xfer(),
44 /* smtp_rset() and smtp_quit(), success means the ability to
45 /* perform an SMTP conversation, not necessarily the ability
46 /* to deliver mail, or the achievement of server happiness.
47 /*
48 /* In case of a rejected or failed connection, a connection
49 /* is marked as "bad, do not cache". Otherwise, connection
50 /* caching may be turned off (without being marked "bad") at
51 /* the discretion of the code that implements the individual
52 /* protocol steps.
53 /*
54 /* Warnings: corrupt message file. A corrupt message is marked
55 /* as "corrupt" by changing its queue file permissions.
56 /* BUGS
57 /* Some SMTP servers will abort when the number of recipients
58 /* for one message exceeds their capacity. This behavior violates
59 /* the SMTP protocol.
60 /* The only way around this is to limit the number of recipients
61 /* per transaction to an artificially-low value.
62 /* SEE ALSO
63 /* smtp(3h) internal data structures
64 /* smtp_chat(3) query/reply SMTP support
65 /* smtp_trouble(3) error handlers
66 /* LICENSE
67 /* .ad
68 /* .fi
69 /* The Secure Mailer license must be distributed with this software.
70 /* AUTHOR(S)
71 /* Wietse Venema
72 /* IBM T.J. Watson Research
73 /* P.O. Box 704
74 /* Yorktown Heights, NY 10598, USA
75 /*
76 /* Wietse Venema
77 /* Google, Inc.
78 /* 111 8th Avenue
79 /* New York, NY 10011, USA
80 /*
81 /* Pipelining code in cooperation with:
82 /* Jon Ribbens
83 /* Oaktree Internet Solutions Ltd.,
84 /* Internet House,
85 /* Canal Basin,
86 /* Coventry,
87 /* CV1 4LY, United Kingdom.
88 /*
89 /* Connection caching in cooperation with:
90 /* Victor Duchovni
91 /* Morgan Stanley
92 /*
93 /* TLS support originally by:
94 /* Lutz Jaenicke
95 /* BTU Cottbus
96 /* Allgemeine Elektrotechnik
97 /* Universitaetsplatz 3-4
98 /* D-03044 Cottbus, Germany
99 /*--*/
100 
101 /* System library. */
102 
103 #include <sys_defs.h>
104 #include <sys/stat.h>
105 #include <sys/socket.h> /* shutdown(2) */
106 #include <netinet/in.h> /* ntohs() */
107 #include <string.h>
108 #include <unistd.h>
109 #include <stdlib.h> /* 44BSD stdarg.h uses abort() */
110 #include <stdarg.h>
111 #include <time.h>
112 
113 #ifdef STRCASECMP_IN_STRINGS_H
114 #include <strings.h>
115 #endif
116 
117 /* Utility library. */
118 
119 #include <msg.h>
120 #include <vstring.h>
121 #include <vstream.h>
122 #include <vstring_vstream.h>
123 #include <stringops.h>
124 #include <mymalloc.h>
125 #include <iostuff.h>
126 #include <split_at.h>
127 #include <name_code.h>
128 #include <name_mask.h>
129 
130 /* Global library. */
131 
132 #include <mail_params.h>
133 #include <smtp_stream.h>
134 #include <mail_queue.h>
135 #include <recipient_list.h>
136 #include <deliver_request.h>
137 #include <defer.h>
138 #include <bounce.h>
139 #include <record.h>
140 #include <rec_type.h>
141 #include <off_cvt.h>
142 #include <mark_corrupt.h>
143 #include <quote_821_local.h>
144 #include <quote_822_local.h>
145 #include <mail_proto.h>
146 #include <mime_state.h>
147 #include <ehlo_mask.h>
148 #include <maps.h>
149 #include <tok822.h>
150 #include <mail_addr_map.h>
151 #include <ext_prop.h>
152 #include <namadr_list.h>
153 #include <match_parent_style.h>
154 #include <lex_822.h>
155 #include <dsn_mask.h>
156 #include <xtext.h>
157 #include <uxtext.h>
158 #include <smtputf8.h>
159 
160 /* Application-specific. */
161 
162 #include "smtp.h"
163 #include "smtp_sasl.h"
164 
165  /*
166  * Sender and receiver state. A session does not necessarily go through a
167  * linear progression, but states are guaranteed to not jump backwards.
168  * Normal sessions go from MAIL->RCPT->DATA->DOT->QUIT->LAST. The states
169  * MAIL, RCPT, and DATA may also be followed by ABORT->QUIT->LAST.
170  *
171  * When connection caching is enabled, the QUIT state is suppressed. Normal
172  * sessions proceed as MAIL->RCPT->DATA->DOT->LAST, while aborted sessions
173  * end with ABORT->LAST. The connection is left open for a limited time. An
174  * RSET probe should be sent before attempting to reuse an open connection
175  * for a new transaction.
176  *
177  * The code to send an RSET probe is a special case with its own initial state
178  * and with its own dedicated state transitions. The session proceeds as
179  * RSET->LAST. This code is kept inside the main protocol engine for
180  * consistent error handling and error reporting. It is not to be confused
181  * with the code that sends RSET to abort a mail transaction in progress.
182  *
183  * The code to send QUIT without message delivery transaction jumps into the
184  * main state machine. If this introduces complications, then we should
185  * introduce a second QUIT state with its own dedicated state transitions,
186  * just like we did for RSET probes.
187  *
188  * By default, the receiver skips the QUIT response. Some SMTP servers
189  * disconnect after responding to ".", and some SMTP servers wait before
190  * responding to QUIT.
191  *
192  * Client states that are associated with sending mail (up to and including
193  * SMTP_STATE_DOT) must have smaller numerical values than the non-sending
194  * states (SMTP_STATE_ABORT .. SMTP_STATE_LAST).
195  */
196 #define SMTP_STATE_XFORWARD_NAME_ADDR 0
197 #define SMTP_STATE_XFORWARD_PROTO_HELO 1
198 #define SMTP_STATE_MAIL 2
199 #define SMTP_STATE_RCPT 3
200 #define SMTP_STATE_DATA 4
201 #define SMTP_STATE_DOT 5
202 #define SMTP_STATE_ABORT 6
203 #define SMTP_STATE_RSET 7
204 #define SMTP_STATE_QUIT 8
205 #define SMTP_STATE_LAST 9
206 
208  &var_smtp_xfwd_tmout, /* name/addr */
209  &var_smtp_xfwd_tmout, /* helo/proto */
217 };
218 
220  "sending XFORWARD name/address",
221  "sending XFORWARD protocol/helo_name",
222  "sending MAIL FROM",
223  "sending RCPT TO",
224  "sending DATA command",
225  "sending end of data -- message may be sent more than once",
226  "sending final RSET",
227  "sending RSET probe",
228  "sending QUIT",
229 };
230 
232  "XFORWARD name/address command",
233  "XFORWARD helo/protocol command",
234  "MAIL FROM command",
235  "RCPT TO command",
236  "DATA command",
237  "end of DATA command",
238  "final RSET command",
239  "RSET probe",
240  "QUIT command",
241 };
242 
243  /*
244  * Note: MIME downgrade never happens for mail that must be delivered with
245  * SMTPUTF8 (the sender requested SMTPUTF8, AND the delivery request
246  * involves at least one UTF-8 envelope address or header value.
247  */
248 #define SMTP_MIME_DOWNGRADE(session, request) \
249  (var_disable_mime_oconv == 0 \
250  && (session->features & SMTP_FEATURE_8BITMIME) == 0 \
251  && strcmp(request->encoding, MAIL_ATTR_ENC_7BIT) != 0)
252 
253 #ifdef USE_TLS
254 
255 static int smtp_start_tls(SMTP_STATE *);
256 
257 #endif
258 
259  /*
260  * Call-back information for header/body checks. We don't provide call-backs
261  * for actions that change the message delivery time or destination.
262  */
263 static void smtp_hbc_logger(void *, const char *, const char *, const char *, const char *);
264 static void smtp_text_out(void *, int, const char *, ssize_t, off_t);
265 
267  smtp_hbc_logger,
268  smtp_text_out,
269 };
270 
271 static int smtp_vrfy_tgt;
272 
273 /* smtp_vrfy_init - initialize */
274 
275 void smtp_vrfy_init(void)
276 {
277  static const NAME_CODE vrfy_init_table[] = {
280  0,
281  };
282 
283  if ((smtp_vrfy_tgt = name_code(vrfy_init_table, NAME_CODE_FLAG_NONE,
284  var_smtp_vrfy_tgt)) == 0)
285  msg_fatal("bad protocol stage: \"%s = %s\"",
287 }
288 
289 /* smtp_helo - perform initial handshake with SMTP server */
290 
292 {
293  const char *myname = "smtp_helo";
294  SMTP_SESSION *session = state->session;
295  DELIVER_REQUEST *request = state->request;
296  SMTP_ITERATOR *iter = state->iterator;
297  SMTP_RESP *resp;
298  SMTP_RESP fake;
299  int except;
300  char *lines;
301  char *words;
302  char *word;
303  int n;
304  static const NAME_CODE xforward_features[] = {
312  0, 0,
313  };
314  const char *ehlo_words;
315  int discard_mask;
316  static const NAME_MASK pix_bug_table[] = {
319  0,
320  };
321  const char *pix_bug_words;
322  const char *pix_bug_source;
323  int pix_bug_mask;
324 
325 #ifdef USE_TLS
326  int saved_features = session->features;
327  int tls_helo_status;
328 
329 #endif
330  const char *NOCLOBBER where;
331 
332  /*
333  * Skip the plaintext SMTP handshake when connecting in SMTPS mode.
334  */
335 #ifdef USE_TLS
337  && (state->misc_flags & SMTP_MISC_FLAG_IN_STARTTLS) == 0) {
338  /* XXX Mix-up of per-session and per-request flags. */
340  tls_helo_status = smtp_start_tls(state);
342  return (tls_helo_status);
343  }
344 #endif
345 
346  /*
347  * Prepare for disaster.
348  */
351  if ((except = vstream_setjmp(state->session->stream)) != 0)
352  return (smtp_stream_except(state, except, where));
353 
354  /*
355  * If not recursing after STARTTLS, examine the server greeting banner
356  * and decide if we are going to send EHLO as the next command.
357  */
359  || (state->misc_flags & SMTP_MISC_FLAG_IN_STARTTLS) == 0) {
360 
361  /*
362  * Read and parse the server's SMTP greeting banner.
363  */
364  where = "receiving the initial server greeting";
365  switch ((resp = smtp_chat_resp(session))->code / 100) {
366  case 2:
367  break;
368  case 5:
370  STR(resp->dsn_buf)[0] = '4';
371  /* FALLTHROUGH */
372  default:
373  return (smtp_site_fail(state, STR(iter->host), resp,
374  "host %s refused to talk to me: %s",
375  session->namaddr,
376  translit(resp->str, "\n", " ")));
377  }
378 
379  /*
380  * If the policy table specifies a bogus TLS security level, fail
381  * now.
382  */
383 #ifdef USE_TLS
384  if (state->tls->level == TLS_LEV_INVALID)
385  /* Warning is already logged. */
386  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
387  SMTP_RESP_FAKE(&fake, "4.7.0"),
388  "client TLS configuration problem"));
389 #endif
390 
391  /*
392  * XXX Some PIX firewall versions require flush before ".<CR><LF>" so
393  * it does not span a packet boundary. This hurts performance so it
394  * is not on by default.
395  */
396  if (resp->str[strspn(resp->str, "20 *\t\n")] == 0) {
397  /* Best effort only. Ignore errors. */
398  if (smtp_pix_bug_maps != 0
399  && (pix_bug_words =
401  STR(iter->addr), 0)) != 0) {
402  pix_bug_source = VAR_LMTP_SMTP(PIX_BUG_MAPS);
403  } else {
404  pix_bug_words = var_smtp_pix_bug_words;
405  pix_bug_source = VAR_LMTP_SMTP(PIX_BUG_WORDS);
406  }
407  if (*pix_bug_words) {
408  pix_bug_mask = name_mask_opt(pix_bug_source, pix_bug_table,
409  pix_bug_words,
411  if ((pix_bug_mask & SMTP_FEATURE_PIX_DELAY_DOTCRLF)
412  && request->msg_stats.incoming_arrival.tv_sec
414  pix_bug_mask &= ~SMTP_FEATURE_PIX_DELAY_DOTCRLF;
415  msg_info("%s: enabling PIX workarounds: %s for %s",
416  request->queue_id,
417  str_name_mask("pix workaround bitmask",
418  pix_bug_table, pix_bug_mask),
419  session->namaddrport);
420  session->features |= pix_bug_mask;
421  }
422  }
423 
424  /*
425  * See if we are talking to ourself. This should not be possible with
426  * the way we implement DNS lookups. However, people are known to
427  * sometimes screw up the naming service. And, mailer loops are still
428  * possible when our own mailer routing tables are mis-configured.
429  */
430  words = resp->str;
431  (void) mystrtok(&words, "- \t\n");
432  for (n = 0; (word = mystrtok(&words, " \t\n")) != 0; n++) {
433  if (n == 0 && strcasecmp(word, var_myhostname) == 0) {
435  msg_warn("host %s greeted me with my own hostname %s",
436  session->namaddrport, var_myhostname);
437  } else if (strcasecmp(word, "ESMTP") == 0)
438  session->features |= SMTP_FEATURE_ESMTP;
439  }
440  if (smtp_mode) {
442  && (session->features & SMTP_FEATURE_PIX_NO_ESMTP) == 0)
443  session->features |= SMTP_FEATURE_ESMTP;
445  || (session->features & SMTP_FEATURE_PIX_NO_ESMTP) != 0)
446  session->features &= ~SMTP_FEATURE_ESMTP;
447  } else {
448  session->features |= SMTP_FEATURE_ESMTP;
449  }
450  }
451 
452  /*
453  * If recursing after STARTTLS, there is no server greeting banner.
454  * Always send EHLO as the next command.
455  */
456  else {
457  session->features |= SMTP_FEATURE_ESMTP;
458  }
459 
460  /*
461  * Return the compliment. Fall back to SMTP if our ESMTP recognition
462  * heuristic failed.
463  */
464  if (smtp_mode) {
465  where = "performing the EHLO handshake";
466  if (session->features & SMTP_FEATURE_ESMTP) {
467  smtp_chat_cmd(session, "EHLO %s", var_smtp_helo_name);
468  if ((resp = smtp_chat_resp(session))->code / 100 != 2) {
469  if (resp->code == 421)
470  return (smtp_site_fail(state, STR(iter->host), resp,
471  "host %s refused to talk to me: %s",
472  session->namaddr,
473  translit(resp->str, "\n", " ")));
474  else
475  session->features &= ~SMTP_FEATURE_ESMTP;
476  }
477  }
478  if ((session->features & SMTP_FEATURE_ESMTP) == 0) {
479  where = "performing the HELO handshake";
480  smtp_chat_cmd(session, "HELO %s", var_smtp_helo_name);
481  if ((resp = smtp_chat_resp(session))->code / 100 != 2)
482  return (smtp_site_fail(state, STR(iter->host), resp,
483  "host %s refused to talk to me: %s",
484  session->namaddr,
485  translit(resp->str, "\n", " ")));
486  }
487  } else {
488  where = "performing the LHLO handshake";
489  smtp_chat_cmd(session, "LHLO %s", var_smtp_helo_name);
490  if ((resp = smtp_chat_resp(session))->code / 100 != 2)
491  return (smtp_site_fail(state, STR(iter->host), resp,
492  "host %s refused to talk to me: %s",
493  session->namaddr,
494  translit(resp->str, "\n", " ")));
495  }
496 
497  /*
498  * No early returns allowed, to ensure consistent handling of TLS and
499  * SASL policies.
500  */
501  if (session->features & SMTP_FEATURE_ESMTP) {
502 
503  /*
504  * Determine what server EHLO keywords to ignore, typically to avoid
505  * inter-operability problems.
506  */
507  if (smtp_ehlo_dis_maps == 0
508  || (ehlo_words = maps_find(smtp_ehlo_dis_maps,
509  STR(iter->addr), 0)) == 0)
510  ehlo_words = var_smtp_ehlo_dis_words;
512  msg_warn("%s: %s map lookup error for %s",
513  session->state->request->queue_id,
514  smtp_ehlo_dis_maps->title, STR(iter->addr));
516  }
517  discard_mask = ehlo_mask(ehlo_words);
518  if (discard_mask && !(discard_mask & EHLO_MASK_SILENT))
519  msg_info("discarding EHLO keywords: %s",
520  str_ehlo_mask(discard_mask));
521 
522  /*
523  * Pick up some useful features offered by the SMTP server. XXX Until
524  * we have a portable routine to convert from string to off_t with
525  * proper overflow detection, ignore the message size limit
526  * advertised by the SMTP server. Otherwise, we might do the wrong
527  * thing when the server advertises a really huge message size limit.
528  *
529  * XXX Allow for "code (SP|-) ehlo-keyword (SP|=) ehlo-param...",
530  * because MicroSoft implemented AUTH based on an old draft.
531  */
532  lines = resp->str;
533  for (n = 0; (words = mystrtok(&lines, "\n")) != 0; /* see below */ ) {
534  if (mystrtok(&words, "- ")
535  && (word = mystrtok(&words, " \t=")) != 0) {
536  if (n == 0) {
537  if (session->helo != 0)
538  myfree(session->helo);
539 
540  /*
541  * XXX: Keep the original case: we don't expect a single
542  * SMTP server to randomly change the case of its helo
543  * response. If different capitalization is detected, we
544  * should assume disjoint TLS caches.
545  */
546  session->helo = mystrdup(word);
547  if (strcasecmp(word, var_myhostname) == 0
548  && (state->misc_flags & SMTP_MISC_FLAG_LOOP_DETECT) != 0) {
549  msg_warn("host %s replied to HELO/EHLO"
550  " with my own hostname %s",
551  session->namaddrport, var_myhostname);
552  if (session->features & SMTP_FEATURE_BEST_MX)
553  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
554  SMTP_RESP_FAKE(&fake, "5.4.6"),
555  "mail for %s loops back to myself",
556  request->nexthop));
557  else
558  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
559  SMTP_RESP_FAKE(&fake, "4.4.6"),
560  "mail for %s loops back to myself",
561  request->nexthop));
562  }
563  } else if (strcasecmp(word, "8BITMIME") == 0) {
564  if ((discard_mask & EHLO_MASK_8BITMIME) == 0)
565  session->features |= SMTP_FEATURE_8BITMIME;
566  } else if (strcasecmp(word, "PIPELINING") == 0) {
567  if ((discard_mask & EHLO_MASK_PIPELINING) == 0)
568  session->features |= SMTP_FEATURE_PIPELINING;
569  } else if (strcasecmp(word, "XFORWARD") == 0) {
570  if ((discard_mask & EHLO_MASK_XFORWARD) == 0)
571  while ((word = mystrtok(&words, " \t")) != 0)
572  session->features |=
573  name_code(xforward_features,
574  NAME_CODE_FLAG_NONE, word);
575  } else if (strcasecmp(word, "SIZE") == 0) {
576  if ((discard_mask & EHLO_MASK_SIZE) == 0) {
577  session->features |= SMTP_FEATURE_SIZE;
578  if ((word = mystrtok(&words, " \t")) != 0) {
579  if (!alldig(word))
580  msg_warn("bad EHLO SIZE limit \"%s\" from %s",
581  word, session->namaddrport);
582  else
583  session->size_limit = off_cvt_string(word);
584  }
585  }
586 #ifdef USE_TLS
587  } else if (strcasecmp(word, "STARTTLS") == 0) {
588  /* Ignored later if we already sent STARTTLS. */
589  if ((discard_mask & EHLO_MASK_STARTTLS) == 0)
590  session->features |= SMTP_FEATURE_STARTTLS;
591 #endif
592 #ifdef USE_SASL_AUTH
593  } else if (var_smtp_sasl_enable
594  && strcasecmp(word, "AUTH") == 0) {
595  if ((discard_mask & EHLO_MASK_AUTH) == 0)
596  smtp_sasl_helo_auth(session, words);
597 #endif
598  } else if (strcasecmp(word, "DSN") == 0) {
599  if ((discard_mask & EHLO_MASK_DSN) == 0)
600  session->features |= SMTP_FEATURE_DSN;
601  } else if (strcasecmp(word, "SMTPUTF8") == 0) {
602  if ((discard_mask & EHLO_MASK_SMTPUTF8) == 0)
603  session->features |= SMTP_FEATURE_SMTPUTF8;
604  }
605  n++;
606  }
607  }
608  }
609  if (msg_verbose)
610  msg_info("server features: 0x%x size %.0f",
611  session->features, (double) session->size_limit);
612 
613  /*
614  * Decide if this delivery requires SMTPUTF8 server support.
615  *
616  * For now, we require that the remote SMTP server supports SMTPUTF8 when
617  * the sender requested SMTPUTF8 support.
618  *
619  * XXX EAI Refine this to: the sender requested SMTPUTF8 support AND the
620  * delivery request involves at least one UTF-8 envelope address or
621  * header value.
622  *
623  * If the sender requested SMTPUTF8 support but the delivery request
624  * involves no UTF-8 envelope address or header value, then we could
625  * still deliver such mail to a non-SMTPUTF8 server, except that we must
626  * either uxtext-encode ORCPT parameters or not send them. We cannot
627  * encode the ORCPT in xtext, because legacy SMTP requires that the
628  * unencoded address consist entirely of printable (graphic and white
629  * space) characters from the US-ASCII repertoire (RFC 3461 section 4). A
630  * correct uxtext encoder will produce a result that an xtext decoder
631  * will pass through unchanged.
632  *
633  * XXX Should we try to encode headers with RFC 2047 when delivering to a
634  * non-SMTPUTF8 server? That could make life easier for mailing lists.
635  */
636 #define DELIVERY_REQUIRES_SMTPUTF8 \
637  ((request->smtputf8 & SMTPUTF8_FLAG_REQUESTED) \
638  && (request->smtputf8 & ~SMTPUTF8_FLAG_REQUESTED))
639 
640  /*
641  * Require that the server supports SMTPUTF8 when delivery requires
642  * SMTPUTF8.
643  *
644  * Fix 20140706: moved this before negotiating TLS, AUTH, and so on.
645  */
646  if ((session->features & SMTP_FEATURE_SMTPUTF8) == 0
648  return (smtp_mesg_fail(state, DSN_BY_LOCAL_MTA,
649  SMTP_RESP_FAKE(&fake, "5.6.7"),
650  "SMTPUTF8 is required, "
651  "but was not offered by host %s",
652  session->namaddr));
653 
654  /*
655  * Fix 20140706: don't do silly things when the remote server announces
656  * SMTPUTF8 but not 8BITMIME support. Our primary mission is to deliver
657  * mail, not to force people into compliance.
658  */
659  if ((session->features & SMTP_FEATURE_SMTPUTF8) != 0
660  && (session->features & SMTP_FEATURE_8BITMIME) == 0) {
661  msg_info("host %s offers SMTPUTF8 support, but not 8BITMIME",
662  session->namaddr);
663  session->features |= SMTP_FEATURE_8BITMIME;
664  }
665 
666  /*
667  * We use SMTP command pipelining if the server said it supported it.
668  * Since we use blocking I/O, RFC 2197 says that we should inspect the
669  * TCP window size and not send more than this amount of information.
670  * Unfortunately this information is unavailable using the sockets
671  * interface. However, we *can* get the TCP send buffer size on the local
672  * TCP/IP stack. We should be able to fill this buffer without being
673  * blocked, and then the kernel will effectively do non-blocking I/O for
674  * us by automatically writing out the contents of its send buffer while
675  * we are reading in the responses. In addition to TCP buffering we have
676  * to be aware of application-level buffering by the vstream module,
677  * which is limited to a couple kbytes.
678  *
679  * XXX No need to do this before and after STARTTLS, but it's not a big deal
680  * if we do.
681  *
682  * XXX When TLS is turned on, the SMTP-level writes will be encapsulated as
683  * TLS messages. Thus, the TCP-level payload will be larger than the
684  * SMTP-level payload. This has implications for the PIPELINING engine.
685  *
686  * To avoid deadlock, the PIPELINING engine needs to request a TCP send
687  * buffer size that can hold the unacknowledged commands plus the TLS
688  * encapsulation overhead.
689  *
690  * The PIPELINING engine keeps the unacknowledged command size <= the
691  * default VSTREAM buffer size (to avoid small-write performance issues
692  * when the VSTREAM buffer size is at its default size). With a default
693  * VSTREAM buffer size of 4096 there is no reason to increase the
694  * unacknowledged command size as the TCP MSS increases. It's safer to
695  * spread the remote SMTP server's recipient processing load over time,
696  * than dumping a very large recipient list all at once.
697  *
698  * For TLS encapsulation overhead we make a conservative guess: take the
699  * current protocol overhead of ~40 bytes, double the number for future
700  * proofing (~80 bytes), then round up the result to the nearest power of
701  * 2 (128 bytes). Plus, be prepared for worst-case compression that
702  * expands data by 1 kbyte, so that the worst-case SMTP payload per TLS
703  * message becomes 15 kbytes.
704  */
705 #define PIPELINING_BUFSIZE VSTREAM_BUFSIZE
706 #ifdef USE_TLS
707 #define TLS_WORST_PAYLOAD 16384
708 #define TLS_WORST_COMP_OVERHD 1024
709 #define TLS_WORST_PROTO_OVERHD 128
710 #define TLS_WORST_SMTP_PAYLOAD (TLS_WORST_PAYLOAD - TLS_WORST_COMP_OVERHD)
711 #define TLS_WORST_TOTAL_OVERHD (TLS_WORST_COMP_OVERHD + TLS_WORST_PROTO_OVERHD)
712 #endif
713 
714  if (session->features & SMTP_FEATURE_PIPELINING) {
715  SOCKOPT_SIZE optlen;
716  int tcp_bufsize;
717  int enc_overhead = 0;
718 
719  optlen = sizeof(tcp_bufsize);
720  if (getsockopt(vstream_fileno(session->stream), SOL_SOCKET,
721  SO_SNDBUF, (char *) &tcp_bufsize, &optlen) < 0)
722  msg_fatal("%s: getsockopt: %m", myname);
723 #ifdef USE_TLS
725  enc_overhead +=
726  (1 + (PIPELINING_BUFSIZE - 1)
727  / TLS_WORST_SMTP_PAYLOAD) * TLS_WORST_TOTAL_OVERHD;
728 #endif
729  if (tcp_bufsize < PIPELINING_BUFSIZE + enc_overhead) {
730  tcp_bufsize = PIPELINING_BUFSIZE + enc_overhead;
731  if (setsockopt(vstream_fileno(session->stream), SOL_SOCKET,
732  SO_SNDBUF, (char *) &tcp_bufsize, optlen) < 0)
733  msg_fatal("%s: setsockopt: %m", myname);
734  }
735  if (msg_verbose)
736  msg_info("Using %s PIPELINING, TCP send buffer size is %d, "
737  "PIPELINING buffer size is %d",
738  smtp_mode ? "ESMTP" : "LMTP",
739  tcp_bufsize, PIPELINING_BUFSIZE);
740  }
741 #ifdef USE_TLS
742 
743  /*
744  * Skip this part if we already sent STARTTLS.
745  */
746  if ((state->misc_flags & SMTP_MISC_FLAG_IN_STARTTLS) == 0) {
747 
748  /*
749  * Optionally log unused STARTTLS opportunities.
750  */
751  if ((session->features & SMTP_FEATURE_STARTTLS) &&
753  state->tls->level <= TLS_LEV_NONE)
754  msg_info("Host offered STARTTLS: [%s]", STR(iter->host));
755 
756  /*
757  * Decide whether or not to send STARTTLS.
758  */
759  if ((session->features & SMTP_FEATURE_STARTTLS) != 0
760  && smtp_tls_ctx != 0 && state->tls->level >= TLS_LEV_MAY) {
761 
762  /*
763  * Prepare for disaster.
764  */
767  if ((except = vstream_setjmp(state->session->stream)) != 0)
768  return (smtp_stream_except(state, except,
769  "receiving the STARTTLS response"));
770 
771  /*
772  * Send STARTTLS. Recurse when the server accepts STARTTLS, after
773  * resetting the SASL and EHLO features lists.
774  *
775  * Reset the SASL mechanism list to avoid spurious warnings.
776  *
777  * Use the smtp_sasl_tls_security_options feature to allow SASL
778  * mechanisms that may not be allowed with plain-text
779  * connections.
780  */
781  smtp_chat_cmd(session, "STARTTLS");
782  if ((resp = smtp_chat_resp(session))->code / 100 == 2) {
783 #ifdef USE_SASL_AUTH
784  if (session->features & SMTP_FEATURE_AUTH)
785  smtp_sasl_cleanup(session);
786 #endif
787  session->features = saved_features;
788  /* XXX Mix-up of per-session and per-request flags. */
790  tls_helo_status = smtp_start_tls(state);
792  return (tls_helo_status);
793  }
794 
795  /*
796  * Give up if we must use TLS but the server rejects STARTTLS
797  * although support for it was announced in the EHLO response.
798  */
799  session->features &= ~SMTP_FEATURE_STARTTLS;
800  if (TLS_REQUIRED(state->tls->level))
801  return (smtp_site_fail(state, STR(iter->host), resp,
802  "TLS is required, but host %s refused to start TLS: %s",
803  session->namaddr,
804  translit(resp->str, "\n", " ")));
805  /* Else try to continue in plain-text mode. */
806  }
807 
808  /*
809  * Give up if we must use TLS but can't for various reasons.
810  *
811  * 200412 Be sure to provide the default clause at the bottom of this
812  * block. When TLS is required we must never, ever, end up in
813  * plain-text mode.
814  */
815  if (TLS_REQUIRED(state->tls->level)) {
816  if (!(session->features & SMTP_FEATURE_STARTTLS)) {
817  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
818  SMTP_RESP_FAKE(&fake, "4.7.4"),
819  "TLS is required, but was not offered by host %s",
820  session->namaddr));
821  } else if (smtp_tls_ctx == 0) {
822  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
823  SMTP_RESP_FAKE(&fake, "4.7.5"),
824  "TLS is required, but our TLS engine is unavailable"));
825  } else {
826  msg_warn("%s: TLS is required but unavailable, don't know why",
827  myname);
828  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
829  SMTP_RESP_FAKE(&fake, "4.7.0"),
830  "TLS is required, but unavailable"));
831  }
832  }
833  }
834 #endif
835 #ifdef USE_SASL_AUTH
836  if (var_smtp_sasl_enable && (session->features & SMTP_FEATURE_AUTH))
837  return (smtp_sasl_helo_login(state));
838 #endif
839 
840  return (0);
841 }
842 
843 #ifdef USE_TLS
844 
845 /* smtp_start_tls - turn on TLS and recurse into the HELO dialog */
846 
847 static int smtp_start_tls(SMTP_STATE *state)
848 {
849  SMTP_SESSION *session = state->session;
850  SMTP_ITERATOR *iter = state->iterator;
851  TLS_CLIENT_START_PROPS tls_props;
852  VSTRING *serverid;
853  SMTP_RESP fake;
854 
855  /*
856  * Turn off SMTP connection caching. When the TLS handshake succeeds, we
857  * can't reuse the SMTP connection. Reason: we can't turn off TLS in one
858  * process, save the connection to the cache which is shared with all
859  * SMTP clients, migrate the connection to another SMTP client, and
860  * resume TLS there. When the TLS handshake fails, we can't reuse the
861  * SMTP connection either, because the conversation is in an unknown
862  * state.
863  */
865 
866  /*
867  * The following assumes sites that use TLS in a perverse configuration:
868  * multiple hosts per hostname, or even multiple hosts per IP address.
869  * All this without a shared TLS session cache, and they still want to
870  * use TLS session caching???
871  *
872  * The TLS session cache records the trust chain verification status of
873  * cached sessions. Different transports may have different CAfile or
874  * CApath settings, perhaps to allow authenticated connections to sites
875  * with private CA certs without trusting said private certs for other
876  * sites. So we cannot assume that a trust chain valid for one transport
877  * is valid for another. Therefore the client session id must include
878  * either the transport name or the values of CAfile and CApath. We use
879  * the transport name.
880  *
881  * XXX: We store only one session per lookup key. Ideally the the key maps
882  * 1-to-1 to a server TLS session cache. We use the IP address, port and
883  * ehlo response name to build a lookup key that works for split caches
884  * (that announce distinct names) behind a load balancer.
885  *
886  * XXX: The TLS library will salt the serverid with further details of the
887  * protocol and cipher requirements including the server ehlo response.
888  * Deferring the helo to the digested suffix results in more predictable
889  * SSL session lookup key lengths.
890  */
891  serverid = vstring_alloc(10);
892  smtp_key_prefix(serverid, "&", state->iterator, SMTP_KEY_FLAG_SERVICE
893  | SMTP_KEY_FLAG_NEXTHOP /* With port */
896 
897  /*
898  * As of Postfix 2.5, tls_client_start() tries hard to always complete
899  * the TLS handshake. It records the verification and match status in the
900  * resulting TLScontext. It is now up to the application to abort the TLS
901  * connection if it chooses.
902  *
903  * XXX When tls_client_start() fails then we don't know what state the SMTP
904  * connection is in, so we give up on this connection even if we are not
905  * required to use TLS.
906  *
907  * Large parameter lists are error-prone, so we emulate a language feature
908  * that C does not have natively: named parameter lists.
909  */
910  session->tls_context =
911  TLS_CLIENT_START(&tls_props,
912  ctx = smtp_tls_ctx,
913  stream = session->stream,
914  timeout = var_smtp_starttls_tmout,
915  tls_level = state->tls->level,
916  nexthop = session->tls_nexthop,
917  host = STR(iter->host),
918  namaddr = session->namaddrport,
919  serverid = vstring_str(serverid),
920  helo = session->helo,
921  protocols = state->tls->protocols,
922  cipher_grade = state->tls->grade,
923  cipher_exclusions
924  = vstring_str(state->tls->exclusions),
925  matchargv = state->tls->matchargv,
926  mdalg = var_smtp_tls_fpt_dgst,
927  dane = state->tls->dane);
928  vstring_free(serverid);
929 
930  if (session->tls_context == 0) {
931 
932  /*
933  * We must avoid further I/O, the peer is in an undefined state.
934  */
935  (void) vstream_fpurge(session->stream, VSTREAM_PURGE_BOTH);
937 
938  /*
939  * If TLS is optional, try delivery to the same server over a
940  * plaintext connection. Otherwise we would defer mail forever with
941  * destinations that have no alternate MX host.
942  *
943  * Don't fall back to plaintext if we were willing to use SASL-over-TLS
944  * authentication. If the server doesn't announce SASL support over
945  * plaintext connections, then we don't want delivery to fail with
946  * "relay access denied".
947  *
948  * If TLS is opportunistic, don't throttle the destination, otherwise if
949  * the mail is volume is high enough we may have difficulty ever
950  * draining even the deferred mail, as new mail provides a constant
951  * stream of negative feedback.
952  */
955  return (smtp_misc_fail(state, state->tls->level == TLS_LEV_MAY ?
958  SMTP_RESP_FAKE(&fake, "4.7.5"),
959  "Cannot start TLS: handshake failure"));
960  }
961 
962  /*
963  * If we are verifying the server certificate and are not happy with the
964  * result, abort the delivery here. We have a usable TLS session with the
965  * server, so no need to disable I/O, ... we can even be polite and send
966  * "QUIT".
967  *
968  * See src/tls/tls_level.c and src/tls/tls.h. Levels above "encrypt" require
969  * matching. Levels >= "dane" require CA or DNSSEC trust.
970  *
971  * When DANE TLSA records specify an end-entity certificate, the trust and
972  * match bits always coincide, but it is fine to report the wrong
973  * end-entity certificate as untrusted rather than unmatched.
974  */
975  if (TLS_MUST_TRUST(state->tls->level))
976  if (!TLS_CERT_IS_TRUSTED(session->tls_context))
977  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
978  SMTP_RESP_FAKE(&fake, "4.7.5"),
979  "Server certificate not trusted"));
980  if (TLS_MUST_MATCH(state->tls->level))
981  if (!TLS_CERT_IS_MATCHED(session->tls_context))
982  return (smtp_site_fail(state, DSN_BY_LOCAL_MTA,
983  SMTP_RESP_FAKE(&fake, "4.7.5"),
984  "Server certificate not verified"));
985 
986  /* At this point there must not be any pending plaintext. */
988 
989  /*
990  * At this point we have to re-negotiate the "EHLO" to reget the
991  * feature-list.
992  */
993  return (smtp_helo(state));
994 }
995 
996 #endif
997 
998 /* smtp_hbc_logger - logging call-back for header/body checks */
999 
1000 static void smtp_hbc_logger(void *context, const char *action,
1001  const char *where, const char *content,
1002  const char *text)
1003 {
1004  const SMTP_STATE *state = (SMTP_STATE *) context;
1005 
1006  if (*text) {
1007  msg_info("%s: %s: %s %.60s: %s",
1008  state->request->queue_id, action, where, content, text);
1009  } else {
1010  msg_info("%s: %s: %s %.60s",
1011  state->request->queue_id, action, where, content);
1012  }
1013 }
1014 
1015 /* smtp_text_out - output one header/body record */
1016 
1017 static void smtp_text_out(void *context, int rec_type,
1018  const char *text, ssize_t len,
1019  off_t unused_offset)
1020 {
1021  SMTP_STATE *state = (SMTP_STATE *) context;
1022  SMTP_SESSION *session = state->session;
1023  ssize_t data_left;
1024  const char *data_start;
1025 
1026  /*
1027  * Deal with an impedance mismatch between Postfix queue files (record
1028  * length <= $message_line_length_limit) and SMTP (DATA record length <=
1029  * $smtp_line_length_limit). The code below does a little too much work
1030  * when the SMTP line length limit is disabled, but it avoids code
1031  * duplication, and thus, it avoids testing and maintenance problems.
1032  */
1033  data_left = len;
1034  data_start = text;
1035  do {
1036  if (state->space_left == var_smtp_line_limit
1037  && data_left > 0 && *data_start == '.')
1038  smtp_fputc('.', session->stream);
1039  if (var_smtp_line_limit > 0 && data_left >= state->space_left) {
1040  smtp_fputs(data_start, state->space_left, session->stream);
1041  data_start += state->space_left;
1042  data_left -= state->space_left;
1044  if (data_left > 0 || rec_type == REC_TYPE_CONT) {
1045  smtp_fputc(' ', session->stream);
1046  state->space_left -= 1;
1047  }
1048  } else {
1049  if (rec_type == REC_TYPE_CONT) {
1050  smtp_fwrite(data_start, data_left, session->stream);
1051  state->space_left -= data_left;
1052  } else {
1053  smtp_fputs(data_start, data_left, session->stream);
1055  }
1056  break;
1057  }
1058  } while (data_left > 0);
1059 }
1060 
1061 /* smtp_format_out - output one header/body record */
1062 
1063 static void PRINTFLIKE(3, 4) smtp_format_out(void *, int, const char *,...);
1064 
1065 static void smtp_format_out(void *context, int rec_type, const char *fmt,...)
1066 {
1067  static VSTRING *vp;
1068  va_list ap;
1069 
1070  if (vp == 0)
1071  vp = vstring_alloc(100);
1072  va_start(ap, fmt);
1073  vstring_vsprintf(vp, fmt, ap);
1074  va_end(ap);
1075  smtp_text_out(context, rec_type, vstring_str(vp), VSTRING_LEN(vp), 0);
1076 }
1077 
1078 /* smtp_header_out - output one message header */
1079 
1080 static void smtp_header_out(void *context, int unused_header_class,
1081  const HEADER_OPTS *unused_info,
1082  VSTRING *buf, off_t offset)
1083 {
1084  char *start = vstring_str(buf);
1085  char *line;
1086  char *next_line;
1087 
1088  /*
1089  * This code destroys the header. We could try to avoid clobbering it,
1090  * but we're not going to use the data any further.
1091  */
1092  for (line = start; line; line = next_line) {
1093  next_line = split_at(line, '\n');
1094  smtp_text_out(context, REC_TYPE_NORM, line, next_line ?
1095  next_line - line - 1 : strlen(line), offset);
1096  }
1097 }
1098 
1099 /* smtp_header_rewrite - rewrite message header before output */
1100 
1101 static void smtp_header_rewrite(void *context, int header_class,
1102  const HEADER_OPTS *header_info,
1103  VSTRING *buf, off_t offset)
1104 {
1105  SMTP_STATE *state = (SMTP_STATE *) context;
1106  int did_rewrite = 0;
1107  char *line;
1108  char *start;
1109  char *next_line;
1110  char *end_line;
1111  char *result;
1112 
1113  /*
1114  * Apply optional header filtering.
1115  */
1116  if (smtp_header_checks) {
1117  result = hbc_header_checks(context, smtp_header_checks, header_class,
1118  header_info, buf, offset);
1119  if (result == 0)
1120  return;
1121  if (result == HBC_CHECKS_STAT_ERROR) {
1122  msg_warn("%s: smtp header checks lookup error",
1123  state->request->queue_id);
1125  }
1126  if (result != STR(buf)) {
1127  vstring_strcpy(buf, result);
1128  myfree(result);
1129  }
1130  }
1131 
1132  /*
1133  * Rewrite primary header addresses that match the smtp_generic_maps. The
1134  * cleanup server already enforces that all headers have proper lengths
1135  * and that all addresses are in proper form, so we don't have to repeat
1136  * that.
1137  */
1138  if (smtp_generic_maps && header_info && header_class == MIME_HDR_PRIMARY
1139  && (header_info->flags & (HDR_OPT_SENDER | HDR_OPT_RECIP)) != 0) {
1140  TOK822 *tree;
1141  TOK822 **addr_list;
1142  TOK822 **tpp;
1143 
1144  tree = tok822_parse(vstring_str(buf)
1145  + strlen(header_info->name) + 1);
1146  addr_list = tok822_grep(tree, TOK822_ADDR);
1147  for (tpp = addr_list; *tpp; tpp++)
1148  did_rewrite |= smtp_map11_tree(tpp[0], smtp_generic_maps,
1150  if (did_rewrite) {
1151  vstring_truncate(buf, strlen(header_info->name));
1152  vstring_strcat(buf, ": ");
1153  tok822_externalize(buf, tree, TOK822_STR_HEAD);
1154  }
1155  myfree((void *) addr_list);
1156  tok822_free_tree(tree);
1157  }
1158 
1159  /*
1160  * Pass through unmodified headers without reconstruction.
1161  */
1162  if (did_rewrite == 0) {
1163  smtp_header_out(context, header_class, header_info, buf, offset);
1164  return;
1165  }
1166 
1167  /*
1168  * A rewritten address list contains one address per line. The code below
1169  * replaces newlines by spaces, to fit as many addresses on a line as
1170  * possible (without rearranging the order of addresses). Prepending
1171  * white space to the beginning of lines is delegated to the output
1172  * routine.
1173  *
1174  * Code derived from cleanup_fold_header().
1175  */
1176  for (line = start = vstring_str(buf); line != 0; line = next_line) {
1177  end_line = line + strcspn(line, "\n");
1178  if (line > start) {
1179  if (end_line - start < 70) { /* TAB counts as one */
1180  line[-1] = ' ';
1181  } else {
1182  start = line;
1183  }
1184  }
1185  next_line = *end_line ? end_line + 1 : 0;
1186  }
1187 
1188  /*
1189  * Prepend a tab to continued header lines that went through the address
1190  * rewriting machinery. Just like smtp_header_out(), this code destroys
1191  * the header. We could try to avoid clobbering it, but we're not going
1192  * to use the data any further.
1193  *
1194  * Code derived from cleanup_out_header().
1195  */
1196  for (line = start = vstring_str(buf); line != 0; line = next_line) {
1197  next_line = split_at(line, '\n');
1198  if (line == start || IS_SPACE_TAB(*line)) {
1199  smtp_text_out(state, REC_TYPE_NORM, line, next_line ?
1200  next_line - line - 1 : strlen(line), offset);
1201  } else {
1202  smtp_format_out(state, REC_TYPE_NORM, "\t%s", line);
1203  }
1204  }
1205 }
1206 
1207 /* smtp_body_rewrite - rewrite message body before output */
1208 
1209 static void smtp_body_rewrite(void *context, int type,
1210  const char *buf, ssize_t len,
1211  off_t offset)
1212 {
1213  SMTP_STATE *state = (SMTP_STATE *) context;
1214  char *result;
1215 
1216  /*
1217  * Apply optional body filtering.
1218  */
1219  if (smtp_body_checks) {
1220  result = hbc_body_checks(context, smtp_body_checks, buf, len, offset);
1221  if (result == buf) {
1222  smtp_text_out(state, type, buf, len, offset);
1223  } else if (result == HBC_CHECKS_STAT_ERROR) {
1224  msg_warn("%s: smtp body checks lookup error",
1225  state->request->queue_id);
1227  } else if (result != 0) {
1228  smtp_text_out(state, type, result, strlen(result), offset);
1229  myfree(result);
1230  }
1231  }
1232 }
1233 
1234 /* smtp_mime_fail - MIME problem */
1235 
1236 static void smtp_mime_fail(SMTP_STATE *state, int mime_errs)
1237 {
1238  const MIME_STATE_DETAIL *detail;
1239  SMTP_RESP fake;
1240 
1241  detail = mime_state_detail(mime_errs);
1243  SMTP_RESP_FAKE(&fake, detail->dsn),
1244  "%s", detail->text);
1245 }
1246 
1247 /* smtp_loop - exercise the SMTP protocol engine */
1248 
1249 static int smtp_loop(SMTP_STATE *state, NOCLOBBER int send_state,
1250  NOCLOBBER int recv_state)
1251 {
1252  const char *myname = "smtp_loop";
1253  DELIVER_REQUEST *request = state->request;
1254  SMTP_SESSION *session = state->session;
1255  SMTP_ITERATOR *iter = state->iterator;
1256  SMTP_RESP *resp;
1257  RECIPIENT *rcpt;
1258  VSTRING *next_command = vstring_alloc(100);
1259  int *NOCLOBBER survivors = 0;
1260  NOCLOBBER int next_state;
1261  NOCLOBBER int next_rcpt;
1262  NOCLOBBER int send_rcpt;
1263  NOCLOBBER int recv_rcpt;
1264  NOCLOBBER int nrcpt;
1265  NOCLOBBER int recv_done;
1266  int except;
1267  int rec_type;
1268  NOCLOBBER int prev_type = 0;
1269  NOCLOBBER int mail_from_rejected;
1270  NOCLOBBER int downgrading;
1271  int mime_errs;
1272  SMTP_RESP fake;
1273  int fail_status;
1274 
1275  /*
1276  * Macros for readability.
1277  */
1278 #define REWRITE_ADDRESS(dst, src) do { \
1279  vstring_strcpy(dst, src); \
1280  if (*(src) && smtp_generic_maps) \
1281  smtp_map11_internal(dst, smtp_generic_maps, \
1282  smtp_ext_prop_mask & EXT_PROP_GENERIC); \
1283  } while (0)
1284 
1285 #define QUOTE_ADDRESS(dst, src) do { \
1286  if (*(src) && var_smtp_quote_821_env) { \
1287  quote_821_local(dst, src); \
1288  } else { \
1289  vstring_strcpy(dst, src); \
1290  } \
1291  } while (0)
1292 
1293  /* Caution: changes to RETURN() also affect code outside the main loop. */
1294 
1295 #define RETURN(x) do { \
1296  if (recv_state != SMTP_STATE_LAST) \
1297  DONT_CACHE_THIS_SESSION; \
1298  vstring_free(next_command); \
1299  if (survivors) \
1300  myfree((void *) survivors); \
1301  if (session->mime_state) \
1302  session->mime_state = mime_state_free(session->mime_state); \
1303  return (x); \
1304  } while (0)
1305 
1306 #define SENDER_IS_AHEAD \
1307  (recv_state < send_state || recv_rcpt != send_rcpt)
1308 
1309 #define SENDER_IN_WAIT_STATE \
1310  (send_state == SMTP_STATE_DOT || send_state == SMTP_STATE_LAST)
1311 
1312 #define SENDING_MAIL \
1313  (recv_state <= SMTP_STATE_DOT)
1314 
1315 #define CANT_RSET_THIS_SESSION \
1316  (session->features |= SMTP_FEATURE_RSET_REJECTED)
1317 
1318  /*
1319  * Pipelining support requires two loops: one loop for sending and one
1320  * for receiving. Each loop has its own independent state. Most of the
1321  * time the sender can run ahead of the receiver by as much as the TCP
1322  * send buffer permits. There are only two places where the sender must
1323  * wait for status information from the receiver: once after sending DATA
1324  * and once after sending QUIT.
1325  *
1326  * The sender state advances until the TCP send buffer would overflow, or
1327  * until the sender needs status information from the receiver. At that
1328  * point the receiver starts processing responses. Once the receiver has
1329  * caught up with the sender, the sender resumes sending commands. If the
1330  * receiver detects a serious problem (MAIL FROM rejected, all RCPT TO
1331  * commands rejected, DATA rejected) it forces the sender to abort the
1332  * SMTP dialog with RSET and QUIT.
1333  */
1334  nrcpt = 0;
1335  next_rcpt = send_rcpt = recv_rcpt = recv_done = 0;
1336  mail_from_rejected = 0;
1337 
1338  /*
1339  * Prepare for disaster. This should not be needed because the design
1340  * guarantees that no output is flushed before smtp_chat_resp() is
1341  * called.
1342  *
1343  * 1) Every SMTP command fits entirely in a VSTREAM output buffer.
1344  *
1345  * 2) smtp_loop() never invokes smtp_chat_cmd() without making sure that
1346  * there is sufficient space for the command in the output buffer.
1347  *
1348  * 3) smtp_loop() flushes the output buffer to avoid server timeouts.
1349  *
1350  * Changing any of these would violate the design, and would likely break
1351  * SMTP pipelining.
1352  *
1353  * We set up the error handler anyway (only upon entry to avoid wasting
1354  * resources) because 1) there is code below that expects that VSTREAM
1355  * timeouts are enabled, and 2) this allows us to detect if someone broke
1356  * Postfix by introducing spurious flush before read operations.
1357  */
1358  if (send_state < SMTP_STATE_XFORWARD_NAME_ADDR
1359  || send_state > SMTP_STATE_QUIT)
1360  msg_panic("%s: bad sender state %d (receiver state %d)",
1361  myname, send_state, recv_state);
1362  smtp_stream_setup(session->stream, *xfer_timeouts[send_state],
1364  if ((except = vstream_setjmp(session->stream)) != 0) {
1365  msg_warn("smtp_proto: spurious flush before read in send state %d",
1366  send_state);
1367  RETURN(SENDING_MAIL ? smtp_stream_except(state, except,
1368  xfer_states[send_state]) : -1);
1369  }
1370 
1371  /*
1372  * The main protocol loop.
1373  */
1374  do {
1375 
1376  /*
1377  * Build the next command.
1378  */
1379  switch (send_state) {
1380 
1381  /*
1382  * Sanity check.
1383  */
1384  default:
1385  msg_panic("%s: bad sender state %d", myname, send_state);
1386 
1387  /*
1388  * Build the XFORWARD command. With properly sanitized
1389  * information, the command length stays within the 512 byte
1390  * command line length limit.
1391  *
1392  * XXX smtpd_xforward_preset() initializes some fields as "unknown"
1393  * and some as null; historically, pickup(8) does not send any of
1394  * these, and the queue manager presets absent fields to "not
1395  * available" except for the rewrite context which is preset to
1396  * local by way of migration aid. These definitions need to be
1397  * centralized for maintainability.
1398  */
1399 #ifndef CAN_FORWARD_CLIENT_NAME
1400 #define _ATTR_AVAIL_AND_KNOWN_(val) \
1401  (DEL_REQ_ATTR_AVAIL(val) && strcasecmp((val), "unknown"))
1402 #define CAN_FORWARD_CLIENT_NAME _ATTR_AVAIL_AND_KNOWN_
1403 #define CAN_FORWARD_CLIENT_ADDR _ATTR_AVAIL_AND_KNOWN_
1404 #define CAN_FORWARD_CLIENT_PORT _ATTR_AVAIL_AND_KNOWN_
1405 #define CAN_FORWARD_PROTO_NAME _ATTR_AVAIL_AND_KNOWN_
1406 #define CAN_FORWARD_HELO_NAME DEL_REQ_ATTR_AVAIL
1407 #define CAN_FORWARD_IDENT_NAME DEL_REQ_ATTR_AVAIL
1408 #define CAN_FORWARD_RWR_CONTEXT DEL_REQ_ATTR_AVAIL
1409 #endif
1410 
1412  vstring_strcpy(next_command, XFORWARD_CMD);
1413  if ((session->features & SMTP_FEATURE_XFORWARD_NAME)
1414  && CAN_FORWARD_CLIENT_NAME(request->client_name)) {
1415  vstring_strcat(next_command, " " XFORWARD_NAME "=");
1416  xtext_quote_append(next_command, request->client_name, "");
1417  }
1418  if ((session->features & SMTP_FEATURE_XFORWARD_ADDR)
1419  && CAN_FORWARD_CLIENT_ADDR(request->client_addr)) {
1420  vstring_strcat(next_command, " " XFORWARD_ADDR "=");
1421  xtext_quote_append(next_command, request->client_addr, "");
1422  }
1423  if ((session->features & SMTP_FEATURE_XFORWARD_PORT)
1424  && CAN_FORWARD_CLIENT_PORT(request->client_port)) {
1425  vstring_strcat(next_command, " " XFORWARD_PORT "=");
1426  xtext_quote_append(next_command, request->client_port, "");
1427  }
1428  if (session->send_proto_helo)
1429  next_state = SMTP_STATE_XFORWARD_PROTO_HELO;
1430  else
1431  next_state = SMTP_STATE_MAIL;
1432  break;
1433 
1435  vstring_strcpy(next_command, XFORWARD_CMD);
1436  if ((session->features & SMTP_FEATURE_XFORWARD_PROTO)
1437  && CAN_FORWARD_PROTO_NAME(request->client_proto)) {
1438  vstring_strcat(next_command, " " XFORWARD_PROTO "=");
1439  xtext_quote_append(next_command, request->client_proto, "");
1440  }
1441  if ((session->features & SMTP_FEATURE_XFORWARD_HELO)
1442  && CAN_FORWARD_HELO_NAME(request->client_helo)) {
1443  vstring_strcat(next_command, " " XFORWARD_HELO "=");
1444  xtext_quote_append(next_command, request->client_helo, "");
1445  }
1446  if ((session->features & SMTP_FEATURE_XFORWARD_IDENT)
1447  && CAN_FORWARD_IDENT_NAME(request->log_ident)) {
1448  vstring_strcat(next_command, " " XFORWARD_IDENT "=");
1449  xtext_quote_append(next_command, request->log_ident, "");
1450  }
1451  if ((session->features & SMTP_FEATURE_XFORWARD_DOMAIN)
1453  vstring_strcat(next_command, " " XFORWARD_DOMAIN "=");
1454  xtext_quote_append(next_command,
1455  strcmp(request->rewrite_context, MAIL_ATTR_RWR_LOCAL) ?
1457  }
1458  next_state = SMTP_STATE_MAIL;
1459  break;
1460 
1461  /*
1462  * Build the MAIL FROM command.
1463  */
1464  case SMTP_STATE_MAIL:
1465  request->msg_stats.reuse_count = session->reuse_count;
1466  GETTIMEOFDAY(&request->msg_stats.conn_setup_done);
1467  REWRITE_ADDRESS(session->scratch2, request->sender);
1468  QUOTE_ADDRESS(session->scratch, vstring_str(session->scratch2));
1469  vstring_sprintf(next_command, "MAIL FROM:<%s>",
1470  vstring_str(session->scratch));
1471  /* XXX Don't announce SIZE if we're going to MIME downgrade. */
1472  if (session->features & SMTP_FEATURE_SIZE /* RFC 1870 */
1473  && !SMTP_MIME_DOWNGRADE(session, request))
1474  vstring_sprintf_append(next_command, " SIZE=%lu",
1475  request->data_size);
1476  if (session->features & SMTP_FEATURE_8BITMIME) { /* RFC 1652 */
1477  if (strcmp(request->encoding, MAIL_ATTR_ENC_8BIT) == 0)
1478  vstring_strcat(next_command, " BODY=8BITMIME");
1479  else if (strcmp(request->encoding, MAIL_ATTR_ENC_7BIT) == 0)
1480  vstring_strcat(next_command, " BODY=7BIT");
1481  else if (strcmp(request->encoding, MAIL_ATTR_ENC_NONE) != 0)
1482  msg_warn("%s: unknown content encoding: %s",
1483  request->queue_id, request->encoding);
1484  }
1485  if (session->features & SMTP_FEATURE_DSN) {
1486  if (request->dsn_envid[0]) {
1487  vstring_sprintf_append(next_command, " ENVID=");
1488  xtext_quote_append(next_command, request->dsn_envid, "+=");
1489  }
1490  if (request->dsn_ret)
1491  vstring_sprintf_append(next_command, " RET=%s",
1492  dsn_ret_str(request->dsn_ret));
1493  }
1494 
1495  /*
1496  * Request SMTPUTF8 when the remote SMTP server supports SMTPUTF8
1497  * and the sender requested SMTPUTF8 support.
1498  *
1499  * If the sender requested SMTPUTF8 but the remote SMTP server does
1500  * not support SMTPUTF8, then we have already determined earlier
1501  * that delivering this message without SMTPUTF8 will not break
1502  * the SMTPUTF8 promise that was made to the sender.
1503  */
1504  if ((session->features & SMTP_FEATURE_SMTPUTF8) != 0
1505  && (request->smtputf8 & SMTPUTF8_FLAG_REQUESTED) != 0)
1506  vstring_strcat(next_command, " SMTPUTF8");
1507 
1508  /*
1509  * We authenticate the local MTA only, but not the sender.
1510  */
1511 #ifdef USE_SASL_AUTH
1514  && (session->features & SMTP_FEATURE_AUTH))
1515  vstring_strcat(next_command, " AUTH=<>");
1516 #endif
1517 
1518  /*
1519  * CVE-2009-3555 (TLS renegotiation). Try to detect a mail
1520  * hijacking attack that prepends malicious EHLO/MAIL/RCPT/DATA
1521  * commands to our TLS session.
1522  *
1523  * For the attack to succeed, the remote SMTP server must reply to
1524  * the malicious EHLO/MAIL/RCPT/DATA commands after completing
1525  * TLS (re)negotiation, so that the replies arrive in our TLS
1526  * session (otherwise the Postfix SMTP client would time out
1527  * waiting for an answer). With some luck we can detect this
1528  * specific attack as a server MAIL reply that arrives before we
1529  * send our own MAIL command.
1530  *
1531  * We don't apply this test to the HELO command because the result
1532  * would be very timing sensitive, and we don't apply this test
1533  * to RCPT and DATA replies because these may be pipelined for
1534  * legitimate reasons.
1535  */
1536 #ifdef USE_TLS
1538  && (state->misc_flags & SMTP_MISC_FLAG_IN_STARTTLS) != 0
1539  && (vstream_peek(session->stream) > 0
1540  || peekfd(vstream_fileno(session->stream)) > 0))
1542 #endif
1543 
1544  /*
1545  * We now return to our regular broadcast.
1546  */
1547  next_state = SMTP_STATE_RCPT;
1548  break;
1549 
1550  /*
1551  * Build one RCPT TO command before we have seen the MAIL FROM
1552  * response.
1553  */
1554  case SMTP_STATE_RCPT:
1555  rcpt = request->rcpt_list.info + send_rcpt;
1556  REWRITE_ADDRESS(session->scratch2, rcpt->address);
1557  QUOTE_ADDRESS(session->scratch, vstring_str(session->scratch2));
1558  vstring_sprintf(next_command, "RCPT TO:<%s>",
1559  vstring_str(session->scratch));
1560  if (session->features & SMTP_FEATURE_DSN) {
1561  /* XXX DSN xtext encode address value not type. */
1562  const char *orcpt_type_addr = rcpt->dsn_orcpt;
1563 
1564  /* Fix 20140706: don't use empty rcpt->orig_addr. */
1565  if (orcpt_type_addr[0] == 0 && rcpt->orig_addr[0] != 0) {
1566  quote_822_local(session->scratch, rcpt->orig_addr);
1567  vstring_sprintf(session->scratch2, "%s;%s",
1568  /* Fix 20140707: sender must request SMTPUTF8. */
1569  (request->smtputf8 != 0
1570  && !allascii(vstring_str(session->scratch))) ?
1571  "utf-8" : "rfc822",
1572  vstring_str(session->scratch));
1573  orcpt_type_addr = vstring_str(session->scratch2);
1574  }
1575  if (orcpt_type_addr[0] != 0) {
1576  /* Fix 20140706: don't send unquoted ORCPT. */
1577  /* Fix 20140707: quoting method must match orcpt type. */
1578  /* Fix 20140707: handle uxtext encoder errors. */
1579  if (strncasecmp(orcpt_type_addr, "utf-8;", 6) == 0) {
1580  if (uxtext_quote(session->scratch,
1581  orcpt_type_addr, "+=") != 0)
1582  vstring_sprintf_append(next_command, " ORCPT=%s",
1583  vstring_str(session->scratch));
1584  } else {
1585  xtext_quote(session->scratch, orcpt_type_addr, "=");
1586  vstring_sprintf_append(next_command, " ORCPT=%s",
1587  vstring_str(session->scratch));
1588  }
1589  }
1590  if (rcpt->dsn_notify)
1591  vstring_sprintf_append(next_command, " NOTIFY=%s",
1592  dsn_notify_str(rcpt->dsn_notify));
1593  }
1594  if ((next_rcpt = send_rcpt + 1) == SMTP_RCPT_LEFT(state))
1595  next_state = (DEL_REQ_TRACE_ONLY(request->flags)
1596  && smtp_vrfy_tgt == SMTP_STATE_RCPT) ?
1598  break;
1599 
1600  /*
1601  * Build the DATA command before we have seen all the RCPT TO
1602  * responses.
1603  */
1604  case SMTP_STATE_DATA:
1605  vstring_strcpy(next_command, "DATA");
1606  next_state = SMTP_STATE_DOT;
1607  break;
1608 
1609  /*
1610  * Build the "." command after we have seen the DATA response
1611  * (DATA is a protocol synchronization point).
1612  *
1613  * Changing the connection caching state here is safe because it
1614  * affects none of the not-yet processed replies to
1615  * already-generated commands.
1616  */
1617  case SMTP_STATE_DOT:
1618  vstring_strcpy(next_command, ".");
1621  next_state = THIS_SESSION_IS_CACHED ?
1623  break;
1624 
1625  /*
1626  * The SMTP_STATE_ABORT sender state is entered by the sender
1627  * when it has verified all recipients; or it is entered by the
1628  * receiver when all recipients are verified or rejected, and is
1629  * then left before the bottom of the main loop.
1630  *
1631  * Changing the connection caching state here is safe because there
1632  * are no not-yet processed replies to already-generated
1633  * commands.
1634  */
1635  case SMTP_STATE_ABORT:
1636  vstring_strcpy(next_command, "RSET");
1639  next_state = THIS_SESSION_IS_CACHED ?
1641  break;
1642 
1643  /*
1644  * Build the RSET command. This is entered as initial state from
1645  * smtp_rset() and has its own dedicated state transitions. It is
1646  * used to find out the status of a cached session before
1647  * attempting mail delivery.
1648  */
1649  case SMTP_STATE_RSET:
1650  vstring_strcpy(next_command, "RSET");
1651  next_state = SMTP_STATE_LAST;
1652  break;
1653 
1654  /*
1655  * Build the QUIT command before we have seen the "." or RSET
1656  * response. This is entered as initial state from smtp_quit(),
1657  * or is reached near the end of any non-cached session.
1658  *
1659  * Changing the connection caching state here is safe. If this
1660  * command is pipelined together with a preceding command, then
1661  * connection caching was already turned off. Do not clobber the
1662  * "bad connection" flag.
1663  */
1664  case SMTP_STATE_QUIT:
1665  vstring_strcpy(next_command, "QUIT");
1666  next_state = SMTP_STATE_LAST;
1669  break;
1670 
1671  /*
1672  * The final sender state has no action associated with it.
1673  */
1674  case SMTP_STATE_LAST:
1675  VSTRING_RESET(next_command);
1676  break;
1677  }
1678  VSTRING_TERMINATE(next_command);
1679 
1680  /*
1681  * Process responses until the receiver has caught up. Vstreams
1682  * automatically flush buffered output when reading new data.
1683  *
1684  * Flush unsent output if command pipelining is off or if no I/O
1685  * happened for a while. This limits the accumulation of client-side
1686  * delays in pipelined sessions.
1687  *
1688  * The PIPELINING engine will flush the VSTREAM buffer if the sender
1689  * could otherwise produce more output than fits the PIPELINING
1690  * buffer. This generally works because we know exactly how much
1691  * output we produced since the last time that the sender and
1692  * receiver synchronized the SMTP state. However this logic is not
1693  * applicable after the sender enters the DATA phase, where it does
1694  * not synchronize with the receiver until the <CR><LF>.<CR><LF>.
1695  * Thus, the PIPELINING engine no longer knows how much data is
1696  * pending in the TCP send buffer. For this reason, if PIPELINING is
1697  * enabled, we always pipeline QUIT after <CR><LF>.<CR><LF>. This is
1698  * safe because once the receiver reads <CR><LF>.<CR><LF>, its TCP
1699  * stack either has already received the QUIT<CR><LF>, or else it
1700  * acknowledges all bytes up to and including <CR><LF>.<CR><LF>,
1701  * making room in the sender's TCP stack for QUIT<CR><LF>.
1702  */
1703 #define CHECK_PIPELINING_BUFSIZE \
1704  (recv_state != SMTP_STATE_DOT || send_state != SMTP_STATE_QUIT)
1705 
1707  || (SENDER_IS_AHEAD
1708  && ((session->features & SMTP_FEATURE_PIPELINING) == 0
1710  && (VSTRING_LEN(next_command) + 2
1712  > PIPELINING_BUFSIZE))
1713  || time((time_t *) 0)
1714  - vstream_ftime(session->stream) > 10))) {
1715  while (SENDER_IS_AHEAD) {
1716 
1717  /*
1718  * Sanity check.
1719  */
1720  if (recv_state < SMTP_STATE_XFORWARD_NAME_ADDR
1721  || recv_state > SMTP_STATE_QUIT)
1722  msg_panic("%s: bad receiver state %d (sender state %d)",
1723  myname, recv_state, send_state);
1724 
1725  /*
1726  * Receive the next server response. Use the proper timeout,
1727  * and log the proper client state in case of trouble.
1728  *
1729  * XXX If we lose the connection before sending end-of-data,
1730  * find out if the server sent a premature end-of-data reply.
1731  * If this read attempt fails, report "lost connection while
1732  * sending message body", not "lost connection while sending
1733  * end-of-data".
1734  *
1735  * "except" becomes zero just above the protocol loop, and stays
1736  * zero or triggers an early return from the loop. In just
1737  * one case: loss of the connection when sending the message
1738  * body, we record the exception, and keep processing in the
1739  * hope of detecting a premature 5XX. We must be careful to
1740  * not clobber this non-zero value once it is set. The
1741  * variable need not survive longjmp() calls, since the only
1742  * setjmp() which does not return early is the one sets this
1743  * condition, subquent failures always return early.
1744  */
1745 #define LOST_CONNECTION_INSIDE_DATA (except == SMTP_ERR_EOF)
1746 
1747  smtp_stream_setup(session->stream, *xfer_timeouts[recv_state],
1750  if (vstream_setjmp(session->stream) != 0)
1752  "sending message body"));
1753  } else {
1754  if ((except = vstream_setjmp(session->stream)) != 0)
1755  RETURN(SENDING_MAIL ? smtp_stream_except(state, except,
1756  xfer_states[recv_state]) : -1);
1757  }
1758  resp = smtp_chat_resp(session);
1759 
1760  /*
1761  * Process the response.
1762  */
1763  switch (recv_state) {
1764 
1765  /*
1766  * Process the XFORWARD response.
1767  */
1769  if (resp->code / 100 != 2)
1770  msg_warn("host %s said: %s (in reply to %s)",
1771  session->namaddrport,
1772  translit(resp->str, "\n", " "),
1774  if (session->send_proto_helo)
1775  recv_state = SMTP_STATE_XFORWARD_PROTO_HELO;
1776  else
1777  recv_state = SMTP_STATE_MAIL;
1778  break;
1779 
1781  if (resp->code / 100 != 2)
1782  msg_warn("host %s said: %s (in reply to %s)",
1783  session->namaddrport,
1784  translit(resp->str, "\n", " "),
1786  recv_state = SMTP_STATE_MAIL;
1787  break;
1788 
1789  /*
1790  * Process the MAIL FROM response. When the server
1791  * rejects the sender, set the mail_from_rejected flag so
1792  * that the receiver may apply a course correction.
1793  */
1794  case SMTP_STATE_MAIL:
1795  if (resp->code / 100 != 2) {
1796  smtp_mesg_fail(state, STR(iter->host), resp,
1797  "host %s said: %s (in reply to %s)",
1798  session->namaddr,
1799  translit(resp->str, "\n", " "),
1801  mail_from_rejected = 1;
1802  }
1803 
1804  /*
1805  * CVE-2009-3555 (TLS renegotiation). Whatever it was
1806  * that arrived before we sent our MAIL FROM command, it
1807  * was not a fatal-level TLS alert message. It could be a
1808  * warning-level TLS alert message, or a ChangeCipherSpec
1809  * message, but such messages are not normally sent in
1810  * the middle of a TLS session. We disconnect and try
1811  * again later.
1812  */
1813 #ifdef USE_TLS
1815  && (session->features & SMTP_FEATURE_EARLY_TLS_MAIL_REPLY)) {
1817  SMTP_RESP_FAKE(&fake, "4.7.0"),
1818  "unexpected server message");
1819  msg_warn("server %s violates %s policy",
1820  session->namaddr,
1821  VAR_LMTP_SMTP(TLS_BLK_EARLY_MAIL_REPLY));
1822  mail_from_rejected = 1;
1823  }
1824 #endif
1825 
1826  /*
1827  * We now return to our regular broadcast.
1828  */
1829  recv_state = SMTP_STATE_RCPT;
1830  break;
1831 
1832  /*
1833  * Process one RCPT TO response. If MAIL FROM was
1834  * rejected, ignore RCPT TO responses: all recipients are
1835  * dead already. When all recipients are rejected the
1836  * receiver may apply a course correction.
1837  *
1838  * XXX 2821: Section 4.5.3.1 says that a 552 RCPT TO reply
1839  * must be treated as if the server replied with 452.
1840  * However, this causes "too much mail data" to be
1841  * treated as a recoverable error, which is wrong. I'll
1842  * stick with RFC 821.
1843  */
1844  case SMTP_STATE_RCPT:
1845  if (!mail_from_rejected) {
1846 #ifdef notdef
1847  if (resp->code == 552) {
1848  resp->code = 452;
1849  resp->dsn[0] = '4';
1850  }
1851 #endif
1852  rcpt = request->rcpt_list.info + recv_rcpt;
1853  if (resp->code / 100 == 2) {
1854  if (!smtp_mode) {
1855  if (survivors == 0)
1856  survivors = (int *)
1857  mymalloc(request->rcpt_list.len
1858  * sizeof(int));
1859  survivors[nrcpt] = recv_rcpt;
1860  }
1861  ++nrcpt;
1862  /* If trace-only, mark the recipient done. */
1863  if (DEL_REQ_TRACE_ONLY(request->flags)
1864  && smtp_vrfy_tgt == SMTP_STATE_RCPT) {
1865  translit(resp->str, "\n", " ");
1866  smtp_rcpt_done(state, resp, rcpt);
1867  }
1868  } else {
1869  smtp_rcpt_fail(state, rcpt, STR(iter->host), resp,
1870  "host %s said: %s (in reply to %s)",
1871  session->namaddr,
1872  translit(resp->str, "\n", " "),
1874  }
1875  }
1876  /* If trace-only, send RSET instead of DATA. */
1877  if (++recv_rcpt == SMTP_RCPT_LEFT(state))
1878  recv_state = (DEL_REQ_TRACE_ONLY(request->flags)
1879  && smtp_vrfy_tgt == SMTP_STATE_RCPT) ?
1881  /* XXX Also: record if non-delivering session. */
1882  break;
1883 
1884  /*
1885  * Process the DATA response. When the server rejects
1886  * DATA, set nrcpt to a negative value so that the
1887  * receiver can apply a course correction.
1888  */
1889  case SMTP_STATE_DATA:
1890  recv_state = SMTP_STATE_DOT;
1891  if (resp->code / 100 != 3) {
1892  if (nrcpt > 0)
1893  smtp_mesg_fail(state, STR(iter->host), resp,
1894  "host %s said: %s (in reply to %s)",
1895  session->namaddr,
1896  translit(resp->str, "\n", " "),
1898  nrcpt = -1;
1899  }
1900 
1901  /*
1902  * In the case of a successful address probe with target
1903  * equal to DATA, the remote server is now in the DATA
1904  * state, and therefore we must not make any further
1905  * attempt to send or receive on this connection. This
1906  * means that we cannot not reuse the general-purpose
1907  * course-correction logic below which sends RSET (and
1908  * perhaps QUIT). Instead we "jump" straight to the exit
1909  * and force an unceremonious disconnect.
1910  */
1911  else if (DEL_REQ_TRACE_ONLY(request->flags)
1912  && smtp_vrfy_tgt == SMTP_STATE_DATA) {
1913  for (nrcpt = 0; nrcpt < recv_rcpt; nrcpt++) {
1914  rcpt = request->rcpt_list.info + nrcpt;
1915  if (!SMTP_RCPT_ISMARKED(rcpt)) {
1916  translit(resp->str, "\n", " ");
1917  SMTP_RESP_SET_DSN(resp, "2.0.0");
1918  smtp_rcpt_done(state, resp, rcpt);
1919  }
1920  }
1922  send_state = recv_state = SMTP_STATE_LAST;
1923  }
1924  break;
1925 
1926  /*
1927  * Process the end of message response. Ignore the
1928  * response when no recipient was accepted: all
1929  * recipients are dead already, and the next receiver
1930  * state is SMTP_STATE_LAST/QUIT regardless. Otherwise,
1931  * if the message transfer fails, bounce all remaining
1932  * recipients, else cross off the recipients that were
1933  * delivered.
1934  */
1935  case SMTP_STATE_DOT:
1936  GETTIMEOFDAY(&request->msg_stats.deliver_done);
1937  if (smtp_mode) {
1938  if (nrcpt > 0) {
1939  if (resp->code / 100 != 2) {
1940  smtp_mesg_fail(state, STR(iter->host), resp,
1941  "host %s said: %s (in reply to %s)",
1942  session->namaddr,
1943  translit(resp->str, "\n", " "),
1945  } else {
1946  for (nrcpt = 0; nrcpt < recv_rcpt; nrcpt++) {
1947  rcpt = request->rcpt_list.info + nrcpt;
1948  if (!SMTP_RCPT_ISMARKED(rcpt)) {
1949  translit(resp->str, "\n", " ");
1950  smtp_rcpt_done(state, resp, rcpt);
1951  }
1952  }
1953  }
1954  }
1955  }
1956 
1957  /*
1958  * With LMTP we have one response per accepted RCPT TO
1959  * command. Stay in the SMTP_STATE_DOT state until we
1960  * have collected all responses.
1961  */
1962  else {
1963  if (nrcpt > 0) {
1964  rcpt = request->rcpt_list.info
1965  + survivors[recv_done++];
1966  if (resp->code / 100 != 2) {
1967  smtp_rcpt_fail(state, rcpt, STR(iter->host), resp,
1968  "host %s said: %s (in reply to %s)",
1969  session->namaddr,
1970  translit(resp->str, "\n", " "),
1972  } else {
1973  translit(resp->str, "\n", " ");
1974  smtp_rcpt_done(state, resp, rcpt);
1975  }
1976  }
1977  if (msg_verbose)
1978  msg_info("%s: got %d of %d end-of-data replies",
1979  myname, recv_done, nrcpt);
1980  if (recv_done < nrcpt)
1981  break;
1982  }
1983 
1984  /*
1985  * XXX Do not change the connection caching state here,
1986  * even if the connection caching timer expired between
1987  * generating the command and processing the reply,
1988  * otherwise the sender and receiver loops get out of
1989  * sync. The caller will call smtp_quit() if appropriate.
1990  */
1993  recv_state = SMTP_STATE_LAST;
1994  else
1995  recv_state = SMTP_STATE_QUIT;
1996  break;
1997 
1998  /*
1999  * Receive the RSET response.
2000  *
2001  * The SMTP_STATE_ABORT sender state is entered by the
2002  * sender when it has verified all recipients; or it is
2003  * entered by the receiver when all recipients are
2004  * verified or rejected, and is then left before the
2005  * bottom of the main loop.
2006  *
2007  * XXX Do not change the connection caching state here, even
2008  * if the server rejected RSET or if the connection
2009  * caching timer expired between generating the command
2010  * and processing the reply, otherwise the sender and
2011  * receiver loops get out of sync. The caller will call
2012  * smtp_quit() if appropriate.
2013  */
2014  case SMTP_STATE_ABORT:
2015  recv_state = (var_skip_quit_resp || THIS_SESSION_IS_CACHED ?
2017  break;
2018 
2019  /*
2020  * This is the initial receiver state from smtp_rset().
2021  * It is used to find out the status of a cached session
2022  * before attempting mail delivery.
2023  */
2024  case SMTP_STATE_RSET:
2025  if (resp->code / 100 != 2)
2027  recv_state = SMTP_STATE_LAST;
2028  break;
2029 
2030  /*
2031  * Receive, but otherwise ignore, the QUIT response.
2032  */
2033  case SMTP_STATE_QUIT:
2034  recv_state = SMTP_STATE_LAST;
2035  break;
2036  }
2037  }
2038 
2039  /*
2040  * At this point, the sender and receiver are fully synchronized.
2041  */
2042 
2043  /*
2044  * We know the server response to every command that was sent.
2045  * Apply a course correction if necessary: the sender wants to
2046  * send RCPT TO but MAIL FROM was rejected; the sender wants to
2047  * send DATA but all recipients were rejected; the sender wants
2048  * to deliver the message but DATA was rejected.
2049  */
2050  if ((send_state == SMTP_STATE_RCPT && mail_from_rejected)
2051  || (send_state == SMTP_STATE_DATA && nrcpt == 0)
2052  || (send_state == SMTP_STATE_DOT && nrcpt < 0)) {
2053  send_state = recv_state = SMTP_STATE_ABORT;
2054  send_rcpt = recv_rcpt = 0;
2055  vstring_strcpy(next_command, "RSET");
2058  next_state = THIS_SESSION_IS_CACHED ?
2060  /* XXX Also: record if non-delivering session. */
2061  next_rcpt = 0;
2062  }
2063  }
2064 
2065  /*
2066  * Make the next sender state the current sender state.
2067  */
2068  if (send_state == SMTP_STATE_LAST)
2069  continue;
2070 
2071  /*
2072  * Special case if the server accepted the DATA command. If the
2073  * server accepted at least one recipient send the entire message.
2074  * Otherwise, just send "." as per RFC 2197.
2075  *
2076  * XXX If there is a hard MIME error while downgrading to 7-bit mail,
2077  * disconnect ungracefully, because there is no other way to cancel a
2078  * transaction in progress.
2079  */
2080  if (send_state == SMTP_STATE_DOT && nrcpt > 0) {
2081 
2084 
2085  if ((except = vstream_setjmp(session->stream)) == 0) {
2086 
2087  if (vstream_fseek(state->src, request->data_offset, SEEK_SET) < 0)
2088  msg_fatal("seek queue file: %m");
2089 
2090  downgrading = SMTP_MIME_DOWNGRADE(session, request);
2091 
2092  /*
2093  * XXX Don't downgrade just because generic_maps is turned
2094  * on.
2095  */
2096 #define SMTP_ANY_CHECKS (smtp_header_checks || smtp_body_checks)
2097 
2098  if (downgrading || smtp_generic_maps || SMTP_ANY_CHECKS)
2099  session->mime_state = mime_state_alloc(downgrading ?
2102  SMTP_ANY_CHECKS == 0 ?
2104  0,
2106  || smtp_header_checks ?
2107  smtp_header_rewrite :
2108  smtp_header_out,
2109  (MIME_STATE_ANY_END) 0,
2111  smtp_body_rewrite :
2112  smtp_text_out,
2113  (MIME_STATE_ANY_END) 0,
2115  (void *) state);
2117 
2118  while ((rec_type = rec_get(state->src, session->scratch, 0)) > 0) {
2119  if (rec_type != REC_TYPE_NORM && rec_type != REC_TYPE_CONT)
2120  break;
2121  if (session->mime_state == 0) {
2122  smtp_text_out((void *) state, rec_type,
2123  vstring_str(session->scratch),
2124  VSTRING_LEN(session->scratch),
2125  (off_t) 0);
2126  } else {
2127  mime_errs =
2128  mime_state_update(session->mime_state, rec_type,
2129  vstring_str(session->scratch),
2130  VSTRING_LEN(session->scratch));
2131  if (mime_errs) {
2132  smtp_mime_fail(state, mime_errs);
2133  RETURN(0);
2134  }
2135  }
2136  prev_type = rec_type;
2137  }
2138 
2139  if (session->mime_state) {
2140 
2141  /*
2142  * The cleanup server normally ends MIME content with a
2143  * normal text record. The following code is needed to
2144  * flush an internal buffer when someone submits 8-bit
2145  * mail not ending in newline via /usr/sbin/sendmail
2146  * while MIME input processing is turned off, and MIME
2147  * 8bit->7bit conversion is requested upon delivery.
2148  *
2149  * Or some error while doing generic address mapping.
2150  */
2151  mime_errs =
2152  mime_state_update(session->mime_state, rec_type, "", 0);
2153  if (mime_errs) {
2154  smtp_mime_fail(state, mime_errs);
2155  RETURN(0);
2156  }
2157  } else if (prev_type == REC_TYPE_CONT) /* missing newline */
2158  smtp_fputs("", 0, session->stream);
2159  if (session->features & SMTP_FEATURE_PIX_DELAY_DOTCRLF) {
2160  smtp_flush(session->stream);/* hurts performance */
2161  sleep(var_smtp_pix_delay); /* not to mention this */
2162  }
2163  if (vstream_ferror(state->src))
2164  msg_fatal("queue file read error");
2165  if (rec_type != REC_TYPE_XTRA) {
2166  msg_warn("%s: bad record type: %d in message content",
2167  request->queue_id, rec_type);
2168  fail_status = smtp_mesg_fail(state, DSN_BY_LOCAL_MTA,
2169  SMTP_RESP_FAKE(&fake, "5.3.0"),
2170  "unreadable mail queue entry");
2171  /* Bailing out, abort stream with prejudice */
2172  (void) vstream_fpurge(session->stream, VSTREAM_PURGE_BOTH);
2174  /* If bounce_append() succeeded, status is still 0 */
2175  if (state->status == 0)
2176  (void) mark_corrupt(state->src);
2177  /* Don't override smtp_mesg_fail() here. */
2178  RETURN(fail_status);
2179  }
2180  } else {
2182  RETURN(smtp_stream_except(state, except,
2183  "sending message body"));
2184 
2185  /*
2186  * We will clear the stream error flag to try and read a
2187  * premature 5XX response, so it is important to flush any
2188  * unwritten data. Otherwise, we will try to flush it again
2189  * before reading, which may incur an unnecessary delay and
2190  * will prevent the reading of any response that is not
2191  * already buffered (bundled with the DATA 354 response).
2192  *
2193  * Not much point in sending QUIT at this point, skip right to
2194  * SMTP_STATE_LAST. The read engine above will likewise avoid
2195  * looking for a QUIT response.
2196  */
2197  (void) vstream_fpurge(session->stream, VSTREAM_PURGE_WRITE);
2198  next_state = SMTP_STATE_LAST;
2199  }
2200  }
2201 
2202  /*
2203  * Copy the next command to the buffer and update the sender state.
2204  */
2205  if (except == 0) {
2206  smtp_chat_cmd(session, "%s", vstring_str(next_command));
2207  } else {
2209  }
2210  send_state = next_state;
2211  send_rcpt = next_rcpt;
2212  } while (recv_state != SMTP_STATE_LAST);
2213  RETURN(0);
2214 }
2215 
2216 /* smtp_xfer - send a batch of envelope information and the message data */
2217 
2219 {
2220  DELIVER_REQUEST *request = state->request;
2221  SMTP_SESSION *session = state->session;
2222  SMTP_RESP fake;
2223  int send_state;
2224  int recv_state;
2225  int send_name_addr;
2226  int result;
2227 
2228  /*
2229  * Sanity check. Recipients should be unmarked at this point.
2230  */
2231  if (SMTP_RCPT_LEFT(state) <= 0)
2232  msg_panic("smtp_xfer: bad recipient count: %d",
2233  SMTP_RCPT_LEFT(state));
2234  if (SMTP_RCPT_ISMARKED(request->rcpt_list.info))
2235  msg_panic("smtp_xfer: bad recipient status: %d",
2236  request->rcpt_list.info->u.status);
2237 
2238  /*
2239  * See if we should even try to send this message at all. This code sits
2240  * here rather than in the EHLO processing code, because of SMTP
2241  * connection caching.
2242  */
2243  if (session->size_limit > 0 && session->size_limit < request->data_size) {
2245  SMTP_RESP_FAKE(&fake, "5.3.4"),
2246  "message size %lu exceeds size limit %.0f of server %s",
2247  request->data_size, (double) session->size_limit,
2248  session->namaddr);
2249  /* Redundant. We abort this delivery attempt. */
2251  return (0);
2252  }
2253 
2254  /*
2255  * Use XFORWARD to forward the origin of this email message across an
2256  * SMTP-based content filter. Send client attribute information only if
2257  * it exists (i.e. remote submission). Local submissions have no client
2258  * attributes; the mail will appear to originate from the content filter
2259  * which is acceptable.
2260  */
2261  send_name_addr =
2263  && (((session->features & SMTP_FEATURE_XFORWARD_NAME)
2264  && CAN_FORWARD_CLIENT_NAME(request->client_name))
2265  || ((session->features & SMTP_FEATURE_XFORWARD_ADDR)
2266  && CAN_FORWARD_CLIENT_ADDR(request->client_addr))
2267  || ((session->features & SMTP_FEATURE_XFORWARD_PORT)
2268  && CAN_FORWARD_CLIENT_PORT(request->client_port)));
2269  session->send_proto_helo =
2271  && (((session->features & SMTP_FEATURE_XFORWARD_PROTO)
2272  && CAN_FORWARD_PROTO_NAME(request->client_proto))
2273  || ((session->features & SMTP_FEATURE_XFORWARD_HELO)
2274  && CAN_FORWARD_HELO_NAME(request->client_helo))
2275  || ((session->features & SMTP_FEATURE_XFORWARD_IDENT)
2276  && CAN_FORWARD_IDENT_NAME(request->log_ident))
2277  || ((session->features & SMTP_FEATURE_XFORWARD_DOMAIN)
2279  if (send_name_addr)
2280  recv_state = send_state = SMTP_STATE_XFORWARD_NAME_ADDR;
2281  else if (session->send_proto_helo)
2282  recv_state = send_state = SMTP_STATE_XFORWARD_PROTO_HELO;
2283  else
2284  recv_state = send_state = SMTP_STATE_MAIL;
2285 
2286  /*
2287  * Remember this session's "normal completion", even if the server 4xx-ed
2288  * some or all recipients. Connection or handshake errors with a later MX
2289  * host should not cause this destination be marked as unreachable.
2290  */
2291  result = smtp_loop(state, send_state, recv_state);
2292 
2293  if (result == 0
2294  /* Just in case */
2295  && vstream_ferror(session->stream) == 0
2296  && vstream_feof(session->stream) == 0)
2298 
2299  return (result);
2300 }
2301 
2302 /* smtp_rset - send a lone RSET command */
2303 
2305 {
2306 
2307  /*
2308  * This works because SMTP_STATE_RSET is a dedicated sender/recipient
2309  * entry state, with SMTP_STATE_LAST as next sender/recipient state.
2310  */
2311  return (smtp_loop(state, SMTP_STATE_RSET, SMTP_STATE_RSET));
2312 }
2313 
2314 /* smtp_quit - send a lone QUIT command */
2315 
2317 {
2318 
2319  /*
2320  * This works because SMTP_STATE_QUIT is the last state with a sender
2321  * action, with SMTP_STATE_LAST as the next sender/recipient state.
2322  */
2323  return (smtp_loop(state, SMTP_STATE_QUIT, var_skip_quit_resp ?
2325 }
#define SMTP_KEY_FLAG_HOSTNAME
Definition: smtp.h:614
int msg_verbose
Definition: msg.c:177
VSTRING * dsn_buf
Definition: smtp.h:501
#define IS_SPACE_TAB(ch)
Definition: lex_822.h:17
#define SMTP_FEATURE_8BITMIME
Definition: smtp.h:205
const char * orig_addr
#define XFORWARD_NAME
Definition: mail_proto.h:252
ssize_t space_left
Definition: smtp.h:149
int status
Definition: smtp.h:148
#define SMTP_VRFY_TGT_RCPT
Definition: mail_params.h:2870
bool var_smtp_always_ehlo
Definition: smtp.c:858
#define SMTP_FEATURE_DSN
Definition: smtp.h:218
bool var_smtp_tls_wrappermode
Definition: smtp.c:897
#define SMTP_FEATURE_EARLY_TLS_MAIL_REPLY
Definition: smtp.h:222
void myfree(void *ptr)
Definition: mymalloc.c:207
int smtp_ext_prop_mask
Definition: smtp.c:970
SMTP_ITERATOR iterator[1]
Definition: smtp.h:154
off_t size_limit
Definition: smtp.h:317
char * str
Definition: smtp.h:500
RECIPIENT_LIST rcpt_list
struct SMTP_SESSION * session
Definition: smtp.h:147
#define THIS_SESSION_IS_CACHED
Definition: smtp.h:420
#define NOCLOBBER
Definition: sys_defs.h:1670
char * mystrdup(const char *str)
Definition: mymalloc.c:225
#define EHLO_MASK_XFORWARD
Definition: ehlo_mask.h:27
#define str_name_mask(tag, table, mask)
Definition: name_mask.h:51
#define CAN_FORWARD_PROTO_NAME
#define EHLO_MASK_STARTTLS
Definition: ehlo_mask.h:24
#define TOK822_ADDR
Definition: tok822.h:46
VSTREAM * src
Definition: smtp.h:144
#define EHLO_MASK_SMTPUTF8
Definition: ehlo_mask.h:30
MIME_STATE * mime_state_alloc(int flags, MIME_STATE_HEAD_OUT head_out, MIME_STATE_ANY_END head_end, MIME_STATE_BODY_OUT body_out, MIME_STATE_ANY_END body_end, MIME_STATE_ERR_PRINT err_print, void *context)
Definition: mime_state.c:493
#define NAME_MASK_IGNORE
Definition: name_mask.h:34
const char * address
TOK822 ** tok822_grep(TOK822 *, int)
Definition: tok822_tree.c:292
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
const char * dsn
Definition: smtp.h:499
int smtp_mode
Definition: smtp.c:963
#define vstring_str(vp)
Definition: vstring.h:71
int var_smtp_xfwd_tmout
Definition: smtp.c:842
#define XFORWARD_DOM_LOCAL
Definition: mail_proto.h:259
#define SMTP_FEATURE_SIZE
Definition: smtp.h:207
#define PIX_BUG_DISABLE_ESMTP
Definition: mail_params.h:1175
VSTRING * xtext_quote_append(VSTRING *quoted, const char *unquoted, const char *special)
Definition: xtext.c:78
int * xfer_timeouts[SMTP_STATE_LAST]
Definition: smtp_proto.c:207
#define SMTP_RESP_FAKE(resp, _dsn)
Definition: smtp.h:512
MAPS * smtp_pix_bug_maps
Definition: smtp.c:972
off_t off_cvt_string(const char *str)
Definition: off_cvt.c:71
DELIVER_REQUEST * request
Definition: smtp.h:146
Definition: tok822.h:27
#define vstream_longjmp(stream, val)
Definition: vstream.h:249
int var_smtp_starttls_tmout
#define tok822_parse(cp)
Definition: tok822.h:84
#define XFORWARD_PORT
Definition: mail_proto.h:254
HBC_CALL_BACKS smtp_hbc_callbacks[1]
Definition: smtp_proto.c:266
#define DEL_REQ_TRACE_ONLY(f)
bool var_smtp_tls_note_starttls_offer
#define EHLO_MASK_8BITMIME
Definition: ehlo_mask.h:17
VSTRING * vstring_truncate(VSTRING *vp, ssize_t len)
Definition: vstring.c:415
int misc_flags
Definition: smtp.h:143
int var_smtp_quit_tmout
Definition: smtp.c:849
#define MAIL_ATTR_RWR_LOCAL
Definition: mail_proto.h:166
#define EHLO_MASK_DSN
Definition: ehlo_mask.h:29
#define TLS_LEV_NONE
Definition: tls.h:43
#define SMTP_MISC_FLAG_IN_STARTTLS
Definition: smtp.h:242
char * xfer_request[SMTP_STATE_LAST]
Definition: smtp_proto.c:231
#define SMTP_KEY_FLAG_SERVICE
Definition: smtp.h:610
char * translit(char *, const char *, const char *)
Definition: translit.c:40
#define CAN_FORWARD_RWR_CONTEXT
int var_smtp_data1_tmout
Definition: smtp.c:846
#define SMTP_FEATURE_XFORWARD_IDENT
Definition: smtp.h:223
int alldig(const char *string)
Definition: alldig.c:38
#define vstream_setjmp(stream)
Definition: vstream.h:248
#define RETRY_AS_PLAINTEXT
Definition: smtp.h:489
char * mystrtok(char **src, const char *sep)
Definition: mystrtok.c:54
int smtp_sasl_helo_login(SMTP_STATE *)
#define vstream_ftime(vp)
Definition: vstream.h:127
#define VSTRING_LEN(vp)
Definition: vstring.h:72
int var_smtp_helo_tmout
Definition: smtp.c:841
#define CAN_FORWARD_CLIENT_PORT
int var_smtp_data0_tmout
Definition: smtp.c:845
#define SMTP_STATE_XFORWARD_NAME_ADDR
Definition: smtp_proto.c:196
#define MIME_OPT_DISABLE_MIME
Definition: mime_state.h:47
int strncasecmp(const char *s1, const char *s2, size_t n)
Definition: strcasecmp.c:52
#define SMTP_FEATURE_STARTTLS
Definition: smtp.h:208
MSG_STATS msg_stats
#define XFORWARD_PROTO
Definition: mail_proto.h:255
#define HDR_OPT_SENDER
Definition: header_opts.h:63
VSTRING * vstring_strcpy(VSTRING *vp, const char *src)
Definition: vstring.c:431
TOK822 * tok822_free_tree(TOK822 *)
Definition: tok822_tree.c:262
void smtp_vrfy_init(void)
Definition: smtp_proto.c:275
#define VSTRING_TERMINATE(vp)
Definition: vstring.h:74
int var_smtp_line_limit
Definition: smtp.c:874
int const char * fmt
const char * str_ehlo_mask(int mask_bits)
Definition: ehlo_mask.c:100
const char * name
Definition: header_opts.h:17
#define SMTPUTF8_FLAG_REQUESTED
Definition: smtputf8.h:97
int smtp_xfer(SMTP_STATE *state)
Definition: smtp_proto.c:2218
#define TOK822_STR_HEAD
Definition: tok822.h:92
int smtp_map11_tree(TOK822 *, MAPS *, int)
Definition: smtp_map11.c:109
#define SENDER_IN_WAIT_STATE
#define SMTP_FEATURE_BEST_MX
Definition: smtp.h:215
#define CAN_FORWARD_HELO_NAME
#define SMTP_RESP_SET_DSN(resp, _dsn)
Definition: smtp.h:520
VSTRING * vstring_sprintf_append(VSTRING *vp, const char *format,...)
Definition: vstring.c:624
VSTRING * vstring_vsprintf(VSTRING *vp, const char *format, va_list ap)
Definition: vstring.c:614
bool var_smtp_dummy_mail_auth
Definition: smtp.c:950
#define SMTP_NOTHROTTLE
Definition: smtp.h:572
#define HDR_OPT_RECIP
Definition: header_opts.h:64
#define TLS_MUST_TRUST(l)
Definition: tls.h:55
struct timeval conn_setup_done
Definition: msg_stats.h:59
#define MAIL_ATTR_ENC_NONE
Definition: mail_proto.h:205
void smtp_sasl_helo_auth(SMTP_SESSION *, const char *)
#define SOCKOPT_SIZE
Definition: sys_defs.h:1415
int mime_state_update(MIME_STATE *state, int rec_type, const char *text, ssize_t len)
Definition: mime_state.c:755
#define SMTP_ERR_EOF
Definition: smtp_stream.h:30
#define SMTP_MISC_FLAG_COMPLETE_SESSION
Definition: smtp.h:248
int smtp_misc_fail(SMTP_STATE *state, int throttle, const char *mta_name, SMTP_RESP *resp, const char *format,...)
Definition: smtp_trouble.c:331
#define REC_TYPE_CONT
Definition: rec_type.h:58
#define DONT_CACHE_THIS_SESSION
Definition: smtp.h:437
bool var_smtp_rec_deadline
Definition: smtp.c:949
int reuse_count
Definition: smtp.h:326
char * xfer_states[SMTP_STATE_LAST]
Definition: smtp_proto.c:219
void smtp_fputc(int ch, VSTREAM *stream)
Definition: smtp_stream.c:432
#define EHLO_MASK_SIZE
Definition: ehlo_mask.h:19
VSTRING * uxtext_quote(VSTRING *quoted, const char *unquoted, const char *special)
Definition: uxtext.c:143
VSTRING * host
Definition: smtp.h:54
#define EHLO_MASK_SILENT
Definition: ehlo_mask.h:31
#define CAN_FORWARD_IDENT_NAME
char * title
Definition: maps.h:23
#define SMTP_STATE_RCPT
Definition: smtp_proto.c:199
void(* MIME_STATE_ANY_END)(void *)
Definition: mime_state.h:30
#define THIS_SESSION_IS_EXPIRED
Definition: smtp.h:423
#define VSTRING_RESET(vp)
Definition: vstring.h:77
#define SMTP_FEATURE_XFORWARD_DOMAIN
Definition: smtp.h:214
#define STR(x)
Definition: anvil.c:518
#define VSTREAM_PURGE_BOTH
Definition: vstream.h:90
#define MIME_HDR_PRIMARY
Definition: mime_state.h:80
bool var_smtp_tls_blk_early_mail_reply
#define SMTP_RCPT_ISMARKED(rcpt)
Definition: smtp.h:559
VSTRING * addr
Definition: smtp.h:55
void msg_warn(const char *fmt,...)
Definition: msg.c:215
char * hbc_header_checks(void *context, HBC_CHECKS *hbc, int header_class, const HEADER_OPTS *hdr_opts, VSTRING *header, off_t offset)
int smtp_quit(SMTP_STATE *state)
Definition: smtp_proto.c:2316
VSTREAM * stream
Definition: smtp.h:305
int send_proto_helo
Definition: smtp.h:323
void smtp_stream_setup(VSTREAM *stream, int maxtime, int enable_deadline)
Definition: smtp_stream.c:209
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
void smtp_fputs(const char *cp, ssize_t todo, VSTREAM *stream)
Definition: smtp_stream.c:383
#define NAME_CODE_FLAG_NONE
Definition: name_code.h:22
#define CANT_RSET_THIS_SESSION
int var_smtp_rset_tmout
Definition: smtp.c:848
#define REWRITE_ADDRESS(dst, src)
const char * dsn_notify_str(int mask)
Definition: dsn_mask.c:118
#define VAR_LMTP_SMTP(x)
Definition: smtp.h:671
VSTRING * tok822_externalize(VSTRING *, TOK822 *, int)
Definition: tok822_parse.c:270
bool var_smtp_never_ehlo
Definition: smtp.c:859
#define DSN_BY_LOCAL_MTA
Definition: smtp.h:518
#define SMTP_STATE_ABORT
Definition: smtp_proto.c:202
struct timeval incoming_arrival
Definition: msg_stats.h:56
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
Definition: vstring.c:602
#define XFORWARD_CMD
Definition: mail_proto.h:251
const MIME_STATE_DETAIL * mime_state_detail(int error_code)
Definition: mime_state.c:1163
#define TLS_REQUIRED(l)
Definition: tls.h:53
void smtp_fwrite(const char *cp, ssize_t todo, VSTREAM *stream)
Definition: smtp_stream.c:408
int var_smtp_mail_tmout
Definition: smtp.c:843
#define SMTP_FEATURE_XFORWARD_HELO
Definition: smtp.h:213
#define XFORWARD_HELO
Definition: mail_proto.h:256
bool var_skip_quit_resp
Definition: smtp.c:854
#define SMTP_FEATURE_PIPELINING
Definition: smtp.h:206
#define CAN_FORWARD_CLIENT_ADDR
MAPS * smtp_generic_maps
Definition: smtp.c:969
#define smtp_mesg_fail(state, mta, resp,...)
Definition: smtp.h:583
#define SMTP_FEATURE_XFORWARD_PORT
Definition: smtp.h:221
#define EXT_PROP_GENERIC
Definition: ext_prop.h:22
#define allascii(s)
Definition: stringops.h:66
int name_code(const NAME_CODE *table, int flags, const char *name)
Definition: name_code.c:65
struct MIME_STATE * mime_state
Definition: smtp.h:321
#define SMTP_STATE_XFORWARD_PROTO_HELO
Definition: smtp_proto.c:197
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
char * var_smtp_vrfy_tgt
Definition: smtp.c:868
#define XFORWARD_DOM_REMOTE
Definition: mail_proto.h:260
const char * dsn
Definition: mime_state.h:62
#define VAR_SMTP_VRFY_TGT
Definition: mail_params.h:2874
off_t vstream_fseek(VSTREAM *stream, off_t offset, int whence)
Definition: vstream.c:1093
#define DONT_USE_FORBIDDEN_SESSION
Definition: smtp.h:443
#define SMTP_FEATURE_PIX_NO_ESMTP
Definition: smtp.h:219
char * var_smtp_ehlo_dis_words
Definition: smtp.c:888
char * helo
Definition: smtp.h:308
ssize_t peekfd(int)
Definition: peekfd.c:60
int var_smtp_data2_tmout
Definition: smtp.c:847
#define SMTP_VRFY_TGT_DATA
Definition: mail_params.h:2871
#define LOST_CONNECTION_INSIDE_DATA
#define SMTP_MIME_DOWNGRADE(session, request)
Definition: smtp_proto.c:248
#define RETURN(x)
int error
Definition: maps.h:25
void smtp_flush(VSTREAM *stream)
Definition: smtp_stream.c:228
#define vstream_peek(vp)
Definition: vstream.h:232
#define SMTP_FEATURE_AUTH
Definition: smtp.h:209
#define VSTREAM_BST_OUT_PEND
Definition: vstream.h:230
HBC_CHECKS * smtp_header_checks
Definition: smtp.c:973
#define MIME_OPT_DOWNGRADE
Definition: mime_state.h:41
#define SMTP_RCPT_LEFT(state)
Definition: smtp.h:561
#define CHECK_PIPELINING_BUFSIZE
VSTRING * scratch
Definition: smtp.h:313
SMTP_STATE * state
Definition: smtp.h:346
#define quote_822_local(dst, src)
int const char SMTP_RESP const char void RECIPIENT const char SMTP_RESP const char int smtp_stream_except(SMTP_STATE *, int, const char *)
Definition: smtp_trouble.c:417
#define vstream_feof(vp)
Definition: vstream.h:121
int strcasecmp(const char *s1, const char *s2)
Definition: strcasecmp.c:41
#define XFORWARD_DOMAIN
Definition: mail_proto.h:258
#define HBC_CHECKS_STAT_ERROR
#define SMTP_FEATURE_SMTPUTF8
Definition: smtp.h:224
const char * dsn_orcpt
#define SMTP_KEY_FLAG_NEXTHOP
Definition: smtp.h:613
const char * text
Definition: mime_state.h:63
#define REC_TYPE_XTRA
Definition: rec_type.h:62
void(* MIME_STATE_ERR_PRINT)(void *, int, const char *, ssize_t)
Definition: mime_state.h:31
bool var_smtp_skip_5xx_greeting
Definition: smtp.c:852
#define SMTP_ERR_DATA
Definition: smtp_stream.h:34
void smtp_chat_cmd(SMTP_SESSION *session, const char *fmt,...)
Definition: smtp_chat.c:181
void smtp_sasl_cleanup(SMTP_SESSION *)
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
char * var_smtp_tls_fpt_dgst
#define SMTP_STATE_DATA
Definition: smtp_proto.c:200
RECIPIENT * info
#define SMTP_KEY_FLAG_ADDR
Definition: smtp.h:615
#define SMTP_STATE_QUIT
Definition: smtp_proto.c:204
#define TLS_MUST_MATCH(l)
Definition: tls.h:54
char * split_at(char *string, int delimiter)
Definition: split_at.c:53
#define vstream_fileno(vp)
Definition: vstream.h:115
int mark_corrupt(VSTREAM *src)
Definition: mark_corrupt.c:47
VSTRING * xtext_quote(VSTRING *quoted, const char *unquoted, const char *special)
Definition: xtext.c:98
#define SMTP_FEATURE_PIX_DELAY_DOTCRLF
Definition: smtp.h:220
int code
Definition: smtp.h:498
union RECIPIENT::@1 u
#define TLS_LEV_MAY
Definition: tls.h:44
char * namaddr
Definition: smtp.h:307
#define SMTP_MISC_FLAG_LOOP_DETECT
Definition: smtp.h:241
#define NAME_MASK_ANY_CASE
Definition: name_mask.h:28
SMTP_RESP * smtp_chat_resp(SMTP_SESSION *)
Definition: smtp_chat.c:235
#define SMTP_THROTTLE
Definition: smtp.h:571
#define REC_TYPE_NORM
Definition: rec_type.h:59
#define PRINTFLIKE(x, y)
Definition: sys_defs.h:1600
MAPS * smtp_ehlo_dis_maps
Definition: smtp.c:968
#define TLS_LEV_INVALID
Definition: tls.h:41
void smtp_rcpt_done(SMTP_STATE *, SMTP_RESP *, RECIPIENT *)
Definition: smtp_rcpt.c:137
VSTRING * scratch2
Definition: smtp.h:314
int reuse_count
Definition: msg_stats.h:61
void smtp_rcpt_fail(SMTP_STATE *state, RECIPIENT *rcpt, const char *mta_name, SMTP_RESP *resp, const char *format,...)
Definition: smtp_trouble.c:354
#define XFORWARD_ADDR
Definition: mail_proto.h:253
int smtp_rset(SMTP_STATE *state)
Definition: smtp_proto.c:2304
#define XFORWARD_IDENT
Definition: mail_proto.h:257
int smtp_helo(SMTP_STATE *state)
Definition: smtp_proto.c:291
#define MIME_OPT_REPORT_NESTING
Definition: mime_state.h:48
#define SMTP_STATE_RSET
Definition: smtp_proto.c:203
char * var_myhostname
Definition: mail_params.c:223
char * hbc_body_checks(void *context, HBC_CHECKS *hbc, const char *line, ssize_t len, off_t offset)
int features
Definition: smtp.h:316
ssize_t vstream_bufstat(VSTREAM *vp, int command)
Definition: vstream.c:1539
struct timeval deliver_done
Definition: msg_stats.h:60
#define EHLO_MASK_AUTH
Definition: ehlo_mask.h:22
#define MAIL_ATTR_ENC_8BIT
Definition: mail_proto.h:203
#define rec_get(fp, buf, limit)
Definition: record.h:56
#define VSTREAM_PURGE_WRITE
Definition: vstream.h:89
int vstream_fpurge(VSTREAM *stream, int direction)
Definition: vstream.c:1041
const char * maps_find(MAPS *maps, const char *name, int flags)
Definition: maps.c:162
#define vstream_ferror(vp)
Definition: vstream.h:120
const char * dsn_ret_str(int code)
Definition: dsn_mask.c:97
#define smtp_site_fail(state, mta, resp,...)
Definition: smtp.h:581
#define SMTP_STATE_LAST
Definition: smtp_proto.c:205
bool var_smtp_sasl_enable
Definition: smtp.c:863
#define SMTP_ANY_CHECKS
#define PIPELINING_BUFSIZE
HBC_CHECKS * smtp_body_checks
Definition: smtp.c:974
#define SENDER_IS_AHEAD
char * var_smtp_pix_bug_words
Definition: smtp.c:938
char * namaddrport
Definition: smtp.h:310
int var_smtp_rcpt_tmout
Definition: smtp.c:844
char * smtp_key_prefix(VSTRING *, const char *, SMTP_ITERATOR *, int)
Definition: smtp_key.c:146
#define name_mask_opt(tag, table, str, flags)
Definition: name_mask.h:46
#define SMTP_STATE_MAIL
Definition: smtp_proto.c:198
#define CAN_FORWARD_CLIENT_NAME
#define SENDING_MAIL
#define QUOTE_ADDRESS(dst, src)
VSTRING * vstring_strcat(VSTRING *vp, const char *src)
Definition: vstring.c:459
int var_smtp_pix_thresh
Definition: smtp.c:870
#define SMTP_FEATURE_XFORWARD_PROTO
Definition: smtp.h:212
#define SMTP_FEATURE_ESMTP
Definition: smtp.h:204
#define SMTP_STATE_DOT
Definition: smtp_proto.c:201
#define SMTP_FEATURE_XFORWARD_ADDR
Definition: smtp.h:211
#define SMTP_FEATURE_XFORWARD_NAME
Definition: smtp.h:210
#define MAIL_ATTR_ENC_7BIT
Definition: mail_proto.h:204
#define EHLO_MASK_PIPELINING
Definition: ehlo_mask.h:18
int ehlo_mask(const char *mask_str)
Definition: ehlo_mask.c:86
void * mymalloc(ssize_t len)
Definition: mymalloc.c:150
#define PLAINTEXT_FALLBACK_OK_AFTER_STARTTLS_FAILURE
Definition: smtp.h:471
bool var_smtp_send_xforward
Definition: smtp.c:879
#define DELIVERY_REQUIRES_SMTPUTF8
char * var_smtp_helo_name
Definition: smtp.c:875
void msg_info(const char *fmt,...)
Definition: msg.c:199
int var_smtp_pix_delay
Definition: smtp.c:873
#define PIX_BUG_DELAY_DOTCRLF
Definition: mail_params.h:1176