Home | History | Annotate | Line # | Download | only in smtpd
      1 /*	$NetBSD: smtpd.c,v 1.22 2026/05/09 18:49:20 christos Exp $	*/
      2 
      3 /*++
      4 /* NAME
      5 /*	smtpd 8
      6 /* SUMMARY
      7 /*	Postfix SMTP server
      8 /* SYNOPSIS
      9 /*	\fBsmtpd\fR [generic Postfix daemon options]
     10 /*
     11 /*	\fBsendmail -bs\fR
     12 /* DESCRIPTION
     13 /*	The SMTP server accepts network connection requests
     14 /*	and performs zero or more SMTP transactions per connection.
     15 /*	Each received message is piped through the \fBcleanup\fR(8)
     16 /*	daemon, and is placed into the \fBincoming\fR queue as one
     17 /*	single queue file.  For this mode of operation, the program
     18 /*	expects to be run from the \fBmaster\fR(8) process manager.
     19 /*
     20 /*	Alternatively, the SMTP server be can run in stand-alone
     21 /*	mode; this is traditionally obtained with "\fBsendmail
     22 /*	-bs\fR".  When the SMTP server runs stand-alone with non
     23 /*	$\fBmail_owner\fR privileges, it receives mail even while
     24 /*	the mail system is not running, deposits messages directly
     25 /*	into the \fBmaildrop\fR queue, and disables the SMTP server's
     26 /*	access policies. As of Postfix version 2.3, the SMTP server
     27 /*	refuses to receive mail from the network when it runs with
     28 /*	non $\fBmail_owner\fR privileges.
     29 /*
     30 /*	The SMTP server implements a variety of policies for connection
     31 /*	requests, and for parameters given to \fBHELO, ETRN, MAIL FROM, VRFY\fR
     32 /*	and \fBRCPT TO\fR commands. They are detailed below and in the
     33 /*	\fBmain.cf\fR configuration file.
     34 /* SECURITY
     35 /* .ad
     36 /* .fi
     37 /*	The SMTP server is moderately security-sensitive. It talks to SMTP
     38 /*	clients and to DNS servers on the network. The SMTP server can be
     39 /*	run chrooted at fixed low privilege.
     40 /* STANDARDS
     41 /*	RFC 821 (SMTP protocol)
     42 /*	RFC 1123 (Host requirements)
     43 /*	RFC 1652 (8bit-MIME transport)
     44 /*	RFC 1869 (SMTP service extensions)
     45 /*	RFC 1870 (Message size declaration)
     46 /*	RFC 1985 (ETRN command)
     47 /*	RFC 2034 (SMTP enhanced status codes)
     48 /*	RFC 2554 (AUTH command)
     49 /*	RFC 2821 (SMTP protocol)
     50 /*	RFC 2920 (SMTP pipelining)
     51 /*	RFC 3030 (CHUNKING without BINARYMIME)
     52 /*	RFC 3207 (STARTTLS command)
     53 /*	RFC 3461 (SMTP DSN extension)
     54 /*	RFC 3463 (Enhanced status codes)
     55 /*	RFC 3848 (ESMTP transmission types)
     56 /*	RFC 4409 (Message submission)
     57 /*	RFC 4954 (AUTH command)
     58 /*	RFC 5321 (SMTP protocol)
     59 /*	RFC 6531 (Internationalized SMTP)
     60 /*	RFC 6533 (Internationalized Delivery Status Notifications)
     61 /*	RFC 7505 ("Null MX" No Service Resource Record)
     62 /*	RFC 8689 (SMTP REQUIRETLS extension)
     63 /* DIAGNOSTICS
     64 /*	Problems and transactions are logged to \fBsyslogd\fR(8)
     65 /*	or \fBpostlogd\fR(8).
     66 /*
     67 /*	Depending on the setting of the \fBnotify_classes\fR parameter,
     68 /*	the postmaster is notified of bounces, protocol problems,
     69 /*	policy violations, and of other trouble.
     70 /* CONFIGURATION PARAMETERS
     71 /* .ad
     72 /* .fi
     73 /*	Changes to \fBmain.cf\fR are picked up automatically, as \fBsmtpd\fR(8)
     74 /*	processes run for only a limited amount of time. Use the command
     75 /*	"\fBpostfix reload\fR" to speed up a change.
     76 /*
     77 /*	The text below provides only a parameter summary. See
     78 /*	\fBpostconf\fR(5) for more details including examples.
     79 /* COMPATIBILITY CONTROLS
     80 /* .ad
     81 /* .fi
     82 /*	The following parameters work around implementation errors in other
     83 /*	software, and/or allow you to override standards in order to prevent
     84 /*	undesirable use.
     85 /* .ad
     86 /* .fi
     87 /* .IP "\fBbroken_sasl_auth_clients (no)\fR"
     88 /*	Enable interoperability with remote SMTP clients that implement an obsolete
     89 /*	version of the AUTH command (RFC 4954).
     90 /* .IP "\fBdisable_vrfy_command (no)\fR"
     91 /*	Disable the SMTP VRFY command.
     92 /* .IP "\fBsmtpd_noop_commands (empty)\fR"
     93 /*	List of commands that the Postfix SMTP server replies to with "250
     94 /*	Ok", without doing any syntax checks and without changing state.
     95 /* .IP "\fBstrict_rfc821_envelopes (no)\fR"
     96 /*	Require that addresses received in SMTP MAIL FROM and RCPT TO
     97 /*	commands are enclosed with <>, and that those addresses do
     98 /*	not contain RFC 822 style comments or phrases.
     99 /* .PP
    100 /*	Available in Postfix version 2.1 and later:
    101 /* .IP "\fBsmtpd_reject_unlisted_sender (no)\fR"
    102 /*	Request that the Postfix SMTP server rejects mail from unknown
    103 /*	sender addresses, even when no explicit reject_unlisted_sender
    104 /*	access restriction is specified.
    105 /* .IP "\fBsmtpd_sasl_exceptions_networks (empty)\fR"
    106 /*	What remote SMTP clients the Postfix SMTP server will not offer
    107 /*	AUTH support to.
    108 /* .PP
    109 /*	Available in Postfix version 2.2 and later:
    110 /* .IP "\fBsmtpd_discard_ehlo_keyword_address_maps (empty)\fR"
    111 /*	Lookup tables, indexed by the remote SMTP client address, with
    112 /*	case insensitive lists of EHLO keywords (pipelining, starttls, auth,
    113 /*	etc.) that the Postfix SMTP server will not send in the EHLO response
    114 /*	to a
    115 /*	remote SMTP client.
    116 /* .IP "\fBsmtpd_discard_ehlo_keywords (empty)\fR"
    117 /*	A case insensitive list of EHLO keywords (pipelining, starttls,
    118 /*	auth, etc.) that the Postfix SMTP server will not send in the EHLO
    119 /*	response
    120 /*	to a remote SMTP client.
    121 /* .IP "\fBsmtpd_delay_open_until_valid_rcpt (yes)\fR"
    122 /*	Postpone the start of an SMTP mail transaction until a valid
    123 /*	RCPT TO command is received.
    124 /* .PP
    125 /*	Available in Postfix version 2.3 and later:
    126 /* .IP "\fBsmtpd_tls_always_issue_session_ids (yes)\fR"
    127 /*	Force the Postfix SMTP server to issue a TLS session id, even
    128 /*	when TLS session caching is turned off (smtpd_tls_session_cache_database
    129 /*	is empty).
    130 /* .PP
    131 /*	Available in Postfix version 2.6 and later:
    132 /* .IP "\fBtcp_windowsize (0)\fR"
    133 /*	An optional workaround for routers that break TCP window scaling.
    134 /* .PP
    135 /*	Available in Postfix version 2.7 and later:
    136 /* .IP "\fBsmtpd_command_filter (empty)\fR"
    137 /*	A mechanism to transform commands from remote SMTP clients.
    138 /* .PP
    139 /*	Available in Postfix version 2.9 - 3.6:
    140 /* .IP "\fBsmtpd_per_record_deadline (normal: no, overload: yes)\fR"
    141 /*	Change the behavior of the smtpd_timeout and smtpd_starttls_timeout
    142 /*	time limits, from a
    143 /*	time limit per read or write system call, to a time limit to send
    144 /*	or receive a complete record (an SMTP command line, SMTP response
    145 /*	line, SMTP message content line, or TLS protocol message).
    146 /* .PP
    147 /*	Available in Postfix version 3.0 and later:
    148 /* .IP "\fBsmtpd_dns_reply_filter (empty)\fR"
    149 /*	Optional filter for Postfix SMTP server DNS lookup results.
    150 /* .PP
    151 /*	Available in Postfix 3.5 and later:
    152 /* .IP "\fBinfo_log_address_format (external)\fR"
    153 /*	The email address form that will be used in non-debug logging
    154 /*	(info, warning, etc.).
    155 /* .PP
    156 /*	Available in Postfix version 3.6 and later:
    157 /* .IP "\fBsmtpd_relay_before_recipient_restrictions (see 'postconf -d' output)\fR"
    158 /*	Evaluate smtpd_relay_restrictions before smtpd_recipient_restrictions.
    159 /* .IP "\fBknown_tcp_ports (lmtp=24, smtp=25, smtps=submissions=465, submission=587)\fR"
    160 /*	Optional setting that avoids lookups in the \fBservices\fR(5) database.
    161 /* .PP
    162 /*	Available in Postfix version 3.7 and later:
    163 /* .IP "\fBsmtpd_per_request_deadline (normal: no, overload: yes)\fR"
    164 /*	Change the behavior of the smtpd_timeout and smtpd_starttls_timeout
    165 /*	time limits, from a time limit per plaintext or TLS read or write
    166 /*	call, to a combined time limit for receiving a complete SMTP request
    167 /*	and for sending a complete SMTP response.
    168 /* .IP "\fBsmtpd_min_data_rate (500)\fR"
    169 /*	The minimum plaintext data transfer rate in bytes/second for
    170 /*	DATA and BDAT requests, when deadlines are enabled with
    171 /*	smtpd_per_request_deadline.
    172 /* ADDRESS REWRITING CONTROLS
    173 /* .ad
    174 /* .fi
    175 /*	See the ADDRESS_REWRITING_README document for a detailed
    176 /*	discussion of Postfix address rewriting.
    177 /* .IP "\fBreceive_override_options (empty)\fR"
    178 /*	Enable or disable recipient validation, built-in content
    179 /*	filtering, or address mapping.
    180 /* .PP
    181 /*	Available in Postfix version 2.2 and later:
    182 /* .IP "\fBlocal_header_rewrite_clients (permit_inet_interfaces)\fR"
    183 /*	Rewrite or add message headers in mail from these clients,
    184 /*	updating incomplete addresses with the domain name in $myorigin or
    185 /*	$mydomain, and adding missing headers.
    186 /* BEFORE-SMTPD PROXY AGENT
    187 /* .ad
    188 /* .fi
    189 /*	Available in Postfix version 2.10 and later:
    190 /* .IP "\fBsmtpd_upstream_proxy_protocol (empty)\fR"
    191 /*	The name of the proxy protocol used by an optional before-smtpd
    192 /*	proxy agent.
    193 /* .IP "\fBsmtpd_upstream_proxy_timeout (5s)\fR"
    194 /*	The time limit for the proxy protocol specified with the
    195 /*	smtpd_upstream_proxy_protocol parameter.
    196 /* AFTER QUEUE EXTERNAL CONTENT INSPECTION CONTROLS
    197 /* .ad
    198 /* .fi
    199 /*	As of version 1.0, Postfix can be configured to send new mail to
    200 /*	an external content filter AFTER the mail is queued. This content
    201 /*	filter is expected to inject mail back into a (Postfix or other)
    202 /*	MTA for further delivery. See the FILTER_README document for details.
    203 /* .IP "\fBcontent_filter (empty)\fR"
    204 /*	After the message is queued, send the entire message to the
    205 /*	specified \fItransport:destination\fR.
    206 /* BEFORE QUEUE EXTERNAL CONTENT INSPECTION CONTROLS
    207 /* .ad
    208 /* .fi
    209 /*	As of version 2.1, the Postfix SMTP server can be configured
    210 /*	to send incoming mail to a real-time SMTP-based content filter
    211 /*	BEFORE mail is queued.  This content filter is expected to inject
    212 /*	mail back into Postfix.  See the SMTPD_PROXY_README document for
    213 /*	details on how to configure and operate this feature.
    214 /* .IP "\fBsmtpd_proxy_filter (empty)\fR"
    215 /*	The hostname and TCP port of the mail filtering proxy server.
    216 /* .IP "\fBsmtpd_proxy_ehlo ($myhostname)\fR"
    217 /*	How the Postfix SMTP server announces itself to the proxy filter.
    218 /* .IP "\fBsmtpd_proxy_options (empty)\fR"
    219 /*	List of options that control how the Postfix SMTP server
    220 /*	communicates with a before-queue content filter.
    221 /* .IP "\fBsmtpd_proxy_timeout (100s)\fR"
    222 /*	The time limit for connecting to a proxy filter and for sending or
    223 /*	receiving information.
    224 /* BEFORE QUEUE MILTER CONTROLS
    225 /* .ad
    226 /* .fi
    227 /*	As of version 2.3, Postfix supports the Sendmail version 8
    228 /*	Milter (mail filter) protocol. These content filters run
    229 /*	outside Postfix. They can inspect the SMTP command stream
    230 /*	and the message content, and can request modifications before
    231 /*	mail is queued. For details see the MILTER_README document.
    232 /* .IP "\fBsmtpd_milters (empty)\fR"
    233 /*	A list of Milter (mail filter) applications for new mail that
    234 /*	arrives via the Postfix \fBsmtpd\fR(8) server.
    235 /* .IP "\fBmilter_protocol (6)\fR"
    236 /*	The mail filter protocol version and optional protocol extensions
    237 /*	for communication with a Milter application; prior to Postfix 2.6
    238 /*	the default protocol is 2.
    239 /* .IP "\fBmilter_default_action (Postfix >= 3.11: shutdown; Postfix < 3.11: tempfail)\fR"
    240 /*	The default action when a Milter (mail filter) response is
    241 /*	unavailable (for example, bad Postfix configuration or Milter
    242 /*	failure).
    243 /* .IP "\fBmilter_macro_daemon_name ($myhostname)\fR"
    244 /*	The {daemon_name} macro value for Milter (mail filter) applications.
    245 /* .IP "\fBmilter_macro_v ($mail_name $mail_version)\fR"
    246 /*	The {v} macro value for Milter (mail filter) applications.
    247 /* .IP "\fBmilter_connect_timeout (30s)\fR"
    248 /*	The time limit for connecting to a Milter (mail filter)
    249 /*	application, and for negotiating protocol options.
    250 /* .IP "\fBmilter_command_timeout (30s)\fR"
    251 /*	The time limit for sending an SMTP command to a Milter (mail
    252 /*	filter) application, and for receiving the response.
    253 /* .IP "\fBmilter_content_timeout (300s)\fR"
    254 /*	The time limit for sending message content to a Milter (mail
    255 /*	filter) application, and for receiving the response.
    256 /* .IP "\fBmilter_connect_macros (see 'postconf -d' output)\fR"
    257 /*	The macros that are sent to Milter (mail filter) applications
    258 /*	after completion of an SMTP connection.
    259 /* .IP "\fBmilter_helo_macros (see 'postconf -d' output)\fR"
    260 /*	The macros that are sent to Milter (mail filter) applications
    261 /*	after the SMTP HELO or EHLO command.
    262 /* .IP "\fBmilter_mail_macros (see 'postconf -d' output)\fR"
    263 /*	The macros that are sent to Milter (mail filter) applications
    264 /*	after the SMTP MAIL FROM command.
    265 /* .IP "\fBmilter_rcpt_macros (see 'postconf -d' output)\fR"
    266 /*	The macros that are sent to Milter (mail filter) applications
    267 /*	after the SMTP RCPT TO command.
    268 /* .IP "\fBmilter_data_macros (see 'postconf -d' output)\fR"
    269 /*	The macros that are sent to version 4 or higher Milter (mail
    270 /*	filter) applications after the SMTP DATA command.
    271 /* .IP "\fBmilter_unknown_command_macros (see 'postconf -d' output)\fR"
    272 /*	The macros that are sent to version 3 or higher Milter (mail
    273 /*	filter) applications after an unknown SMTP command.
    274 /* .IP "\fBmilter_end_of_header_macros (see 'postconf -d' output)\fR"
    275 /*	The macros that are sent to Milter (mail filter) applications
    276 /*	after the end of the message header.
    277 /* .IP "\fBmilter_end_of_data_macros (see 'postconf -d' output)\fR"
    278 /*	The macros that are sent to Milter (mail filter) applications
    279 /*	after the message end-of-data.
    280 /* .PP
    281 /*	Available in Postfix version 3.1 and later:
    282 /* .IP "\fBmilter_macro_defaults (empty)\fR"
    283 /*	Optional list of \fIname=value\fR pairs that specify default
    284 /*	values for arbitrary macros that Postfix may send to Milter
    285 /*	applications.
    286 /* .PP
    287 /*	Available in Postfix version 3.2 and later:
    288 /* .IP "\fBsmtpd_milter_maps (empty)\fR"
    289 /*	Lookup tables with Milter settings per remote SMTP client IP
    290 /*	address.
    291 /* GENERAL CONTENT INSPECTION CONTROLS
    292 /* .ad
    293 /* .fi
    294 /*	The following parameters are applicable for both built-in
    295 /*	and external content filters.
    296 /* .PP
    297 /*	Available in Postfix version 2.1 and later:
    298 /* .IP "\fBreceive_override_options (empty)\fR"
    299 /*	Enable or disable recipient validation, built-in content
    300 /*	filtering, or address mapping.
    301 /* EXTERNAL CONTENT INSPECTION CONTROLS
    302 /* .ad
    303 /* .fi
    304 /*	The following parameters are applicable for both before-queue
    305 /*	and after-queue content filtering.
    306 /* .PP
    307 /*	Available in Postfix version 2.1 and later:
    308 /* .IP "\fBsmtpd_authorized_xforward_hosts (empty)\fR"
    309 /*	What remote SMTP clients are allowed to use the XFORWARD feature.
    310 /* SASL AUTHENTICATION CONTROLS
    311 /* .ad
    312 /* .fi
    313 /*	Postfix SASL support (RFC 4954) can be used to authenticate remote
    314 /*	SMTP clients to the Postfix SMTP server, and to authenticate the
    315 /*	Postfix SMTP client to a remote SMTP server.
    316 /*	See the SASL_README document for details.
    317 /* .IP "\fBbroken_sasl_auth_clients (no)\fR"
    318 /*	Enable interoperability with remote SMTP clients that implement an obsolete
    319 /*	version of the AUTH command (RFC 4954).
    320 /* .IP "\fBsmtpd_sasl_auth_enable (no)\fR"
    321 /*	Enable SASL authentication in the Postfix SMTP server.
    322 /* .IP "\fBsmtpd_sasl_local_domain (empty)\fR"
    323 /*	The name of the Postfix SMTP server's local SASL authentication
    324 /*	realm.
    325 /* .IP "\fBsmtpd_sasl_security_options (noanonymous)\fR"
    326 /*	Postfix SMTP server SASL security options; as of Postfix 2.3
    327 /*	the list of available
    328 /*	features depends on the SASL server implementation that is selected
    329 /*	with \fBsmtpd_sasl_type\fR.
    330 /* .IP "\fBsmtpd_sender_login_maps (empty)\fR"
    331 /*	Optional lookup table with the SASL login names that own the
    332 /*	envelope sender
    333 /*	(MAIL FROM) addresses.
    334 /* .PP
    335 /*	Available in Postfix version 2.1 and later:
    336 /* .IP "\fBsmtpd_sasl_exceptions_networks (empty)\fR"
    337 /*	What remote SMTP clients the Postfix SMTP server will not offer
    338 /*	AUTH support to.
    339 /* .PP
    340 /*	Available in Postfix version 2.1 and 2.2:
    341 /* .IP "\fBsmtpd_sasl_application_name (smtpd)\fR"
    342 /*	The application name that the Postfix SMTP server uses for SASL
    343 /*	server initialization.
    344 /* .PP
    345 /*	Available in Postfix version 2.3 and later:
    346 /* .IP "\fBsmtpd_sasl_authenticated_header (no)\fR"
    347 /*	Report the SASL authenticated user name in the \fBsmtpd\fR(8) Received
    348 /*	message header.
    349 /* .IP "\fBsmtpd_sasl_path (smtpd)\fR"
    350 /*	Implementation-specific information that the Postfix SMTP server
    351 /*	passes through to
    352 /*	the SASL plug-in implementation that is selected with
    353 /*	\fBsmtpd_sasl_type\fR.
    354 /* .IP "\fBsmtpd_sasl_type (cyrus)\fR"
    355 /*	The SASL plug-in type that the Postfix SMTP server should use
    356 /*	for authentication.
    357 /* .PP
    358 /*	Available in Postfix version 2.5 and later:
    359 /* .IP "\fBcyrus_sasl_config_path (empty)\fR"
    360 /*	Search path for Cyrus SASL application configuration files,
    361 /*	currently used only to locate the $smtpd_sasl_path.conf file.
    362 /* .PP
    363 /*	Available in Postfix version 2.11 and later:
    364 /* .IP "\fBsmtpd_sasl_service (smtp)\fR"
    365 /*	The service name that is passed to the SASL plug-in that is
    366 /*	selected with \fBsmtpd_sasl_type\fR and \fBsmtpd_sasl_path\fR.
    367 /* .PP
    368 /*	Available in Postfix version 3.4 and later:
    369 /* .IP "\fBsmtpd_sasl_response_limit (12288)\fR"
    370 /*	The maximum length of a SASL client's response to a server challenge.
    371 /* .PP
    372 /*	Available in Postfix 3.6 and later:
    373 /* .IP "\fBsmtpd_sasl_mechanism_filter (!external, static:rest)\fR"
    374 /*	If non-empty, a filter for the SASL mechanism names that the
    375 /*	Postfix SMTP server will announce in the EHLO response.
    376 /* TLS SUPPORT CONTROLS
    377 /* .ad
    378 /* .fi
    379 /*	Detailed information about STARTTLS configuration may be
    380 /*	found in the TLS_README document.
    381 /* .IP "\fBsmtpd_tls_security_level (empty)\fR"
    382 /*	The SMTP TLS security level for the Postfix SMTP server; when
    383 /*	a non-empty value is specified, this overrides the obsolete parameters
    384 /*	smtpd_use_tls and smtpd_enforce_tls.
    385 /* .IP "\fBsmtpd_sasl_tls_security_options ($smtpd_sasl_security_options)\fR"
    386 /*	The SASL authentication security options that the Postfix SMTP
    387 /*	server uses for TLS encrypted SMTP sessions.
    388 /* .IP "\fBsmtpd_starttls_timeout (see 'postconf -d' output)\fR"
    389 /*	The time limit for Postfix SMTP server write and read operations
    390 /*	during TLS startup and shutdown handshake procedures.
    391 /* .IP "\fBsmtpd_tls_CAfile (empty)\fR"
    392 /*	A file containing (PEM format) CA certificates of root CAs trusted
    393 /*	to sign either remote SMTP client certificates or intermediate CA
    394 /*	certificates.
    395 /* .IP "\fBsmtpd_tls_CApath (empty)\fR"
    396 /*	A directory containing (PEM format) CA certificates of root CAs
    397 /*	trusted to sign either remote SMTP client certificates or intermediate CA
    398 /*	certificates.
    399 /* .IP "\fBsmtpd_tls_always_issue_session_ids (yes)\fR"
    400 /*	Force the Postfix SMTP server to issue a TLS session id, even
    401 /*	when TLS session caching is turned off (smtpd_tls_session_cache_database
    402 /*	is empty).
    403 /* .IP "\fBsmtpd_tls_ask_ccert (no)\fR"
    404 /*	Ask a remote SMTP client for a client certificate.
    405 /* .IP "\fBsmtpd_tls_auth_only (no)\fR"
    406 /*	When TLS encryption is optional in the Postfix SMTP server, do
    407 /*	not announce or accept SASL authentication over unencrypted
    408 /*	connections.
    409 /* .IP "\fBsmtpd_tls_ccert_verifydepth (9)\fR"
    410 /*	The verification depth for remote SMTP client certificates.
    411 /* .IP "\fBsmtpd_tls_cert_file (empty)\fR"
    412 /*	File with the Postfix SMTP server RSA certificate in PEM format.
    413 /* .IP "\fBsmtpd_tls_exclude_ciphers (empty)\fR"
    414 /*	List of ciphers or cipher types to exclude from the SMTP server
    415 /*	cipher list at all TLS security levels.
    416 /* .IP "\fBsmtpd_tls_dcert_file (empty)\fR"
    417 /*	File with the Postfix SMTP server DSA certificate in PEM format.
    418 /* .IP "\fBsmtpd_tls_dh1024_param_file (empty)\fR"
    419 /*	File with DH parameters that the Postfix SMTP server should
    420 /*	use with non-export EDH ciphers.
    421 /* .IP "\fBsmtpd_tls_dh512_param_file (empty)\fR"
    422 /*	File with DH parameters that the Postfix SMTP server should
    423 /*	use with export-grade EDH ciphers.
    424 /* .IP "\fBsmtpd_tls_dkey_file ($smtpd_tls_dcert_file)\fR"
    425 /*	File with the Postfix SMTP server DSA private key in PEM format.
    426 /* .IP "\fBsmtpd_tls_key_file ($smtpd_tls_cert_file)\fR"
    427 /*	File with the Postfix SMTP server RSA private key in PEM format.
    428 /* .IP "\fBsmtpd_tls_loglevel (0)\fR"
    429 /*	Enable additional Postfix SMTP server logging of TLS activity.
    430 /* .IP "\fBsmtpd_tls_mandatory_ciphers (medium)\fR"
    431 /*	The minimum TLS cipher grade that the Postfix SMTP server will
    432 /*	use with mandatory TLS encryption.
    433 /* .IP "\fBsmtpd_tls_mandatory_exclude_ciphers (empty)\fR"
    434 /*	Additional list of ciphers or cipher types to exclude from the
    435 /*	Postfix SMTP server cipher list at mandatory TLS security levels.
    436 /* .IP "\fBsmtpd_tls_mandatory_protocols (see 'postconf -d' output)\fR"
    437 /*	TLS protocols accepted by the Postfix SMTP server with mandatory TLS
    438 /*	encryption.
    439 /* .IP "\fBsmtpd_tls_received_header (no)\fR"
    440 /*	Request that the Postfix SMTP server produces Received:  message
    441 /*	headers that include information about the protocol and cipher used,
    442 /*	as well as the remote SMTP client CommonName and client certificate issuer
    443 /*	CommonName.
    444 /* .IP "\fBsmtpd_tls_req_ccert (no)\fR"
    445 /*	With mandatory TLS encryption, require a trusted remote SMTP client
    446 /*	certificate in order to allow TLS connections to proceed.
    447 /* .IP "\fBsmtpd_tls_wrappermode (no)\fR"
    448 /*	Run the Postfix SMTP server in TLS "wrapper" mode,
    449 /*	instead of using the STARTTLS command.
    450 /* .IP "\fBtls_daemon_random_bytes (32)\fR"
    451 /*	The number of pseudo-random bytes that an \fBsmtp\fR(8) or \fBsmtpd\fR(8)
    452 /*	process requests from the \fBtlsmgr\fR(8) server in order to seed its
    453 /*	internal pseudo random number generator (PRNG).
    454 /* .IP "\fBtls_high_cipherlist (see 'postconf -d' output)\fR"
    455 /*	The OpenSSL cipherlist for "high" grade ciphers.
    456 /* .IP "\fBtls_medium_cipherlist (see 'postconf -d' output)\fR"
    457 /*	The OpenSSL cipherlist for "medium" or higher grade ciphers.
    458 /* .IP "\fBtls_null_cipherlist (eNULL:!aNULL)\fR"
    459 /*	The OpenSSL cipherlist for "NULL" grade ciphers that provide
    460 /*	authentication without encryption.
    461 /* .PP
    462 /*	Available in Postfix version 2.3..3.7:
    463 /* .IP "\fBtls_low_cipherlist (see 'postconf -d' output)\fR"
    464 /*	The OpenSSL cipherlist for "low" or higher grade ciphers.
    465 /* .IP "\fBtls_export_cipherlist (see 'postconf -d' output)\fR"
    466 /*	The OpenSSL cipherlist for "export" or higher grade ciphers.
    467 /* .PP
    468 /*	Available in Postfix version 2.5 and later:
    469 /* .IP "\fBsmtpd_tls_fingerprint_digest (see 'postconf -d' output)\fR"
    470 /*	The message digest algorithm to construct remote SMTP client-certificate
    471 /*	fingerprints or public key fingerprints (Postfix 2.9 and later) for
    472 /*	\fBcheck_ccert_access\fR and \fBpermit_tls_clientcerts\fR.
    473 /* .PP
    474 /*	Available in Postfix version 2.6 and later:
    475 /* .IP "\fBsmtpd_tls_protocols (see 'postconf -d' output)\fR"
    476 /*	TLS protocols accepted by the Postfix SMTP server with opportunistic
    477 /*	TLS encryption.
    478 /* .IP "\fBsmtpd_tls_ciphers (medium)\fR"
    479 /*	The minimum TLS cipher grade that the Postfix SMTP server
    480 /*	will use with opportunistic TLS encryption.
    481 /* .IP "\fBsmtpd_tls_eccert_file (empty)\fR"
    482 /*	File with the Postfix SMTP server ECDSA certificate in PEM format.
    483 /* .IP "\fBsmtpd_tls_eckey_file ($smtpd_tls_eccert_file)\fR"
    484 /*	File with the Postfix SMTP server ECDSA private key in PEM format.
    485 /* .IP "\fBsmtpd_tls_eecdh_grade (see 'postconf -d' output)\fR"
    486 /*	The Postfix SMTP server security grade for ephemeral elliptic-curve
    487 /*	Diffie-Hellman (EECDH) key exchange.
    488 /* .IP "\fBtls_eecdh_strong_curve (prime256v1)\fR"
    489 /*	The elliptic curve used by the Postfix SMTP server for sensibly
    490 /*	strong
    491 /*	ephemeral ECDH key exchange.
    492 /* .IP "\fBtls_eecdh_ultra_curve (secp384r1)\fR"
    493 /*	The elliptic curve used by the Postfix SMTP server for maximally
    494 /*	strong
    495 /*	ephemeral ECDH key exchange.
    496 /* .PP
    497 /*	Available in Postfix version 2.8 and later:
    498 /* .IP "\fBtls_preempt_cipherlist (no)\fR"
    499 /*	With SSLv3 and later, use the Postfix SMTP server's cipher
    500 /*	preference order instead of the remote client's cipher preference
    501 /*	order.
    502 /* .IP "\fBtls_disable_workarounds (see 'postconf -d' output)\fR"
    503 /*	List or bit-mask of OpenSSL bug work-arounds to disable.
    504 /* .PP
    505 /*	Available in Postfix version 2.11 and later:
    506 /* .IP "\fBtlsmgr_service_name (tlsmgr)\fR"
    507 /*	The name of the \fBtlsmgr\fR(8) service entry in master.cf.
    508 /* .PP
    509 /*	Available in Postfix version 3.0 and later:
    510 /* .IP "\fBtls_session_ticket_cipher (Postfix >= 3.0: aes-256-cbc, Postfix < 3.0: aes-128-cbc)\fR"
    511 /*	Algorithm used to encrypt RFC5077 TLS session tickets.
    512 /* .PP
    513 /*	Available in Postfix version 3.2 and later:
    514 /* .IP "\fBtls_eecdh_auto_curves (see 'postconf -d' output)\fR"
    515 /*	The prioritized list of elliptic curves, that should be enabled in the
    516 /*	Postfix SMTP client and server.
    517 /* .PP
    518 /*	Available in Postfix version 3.4 and later:
    519 /* .IP "\fBsmtpd_tls_chain_files (empty)\fR"
    520 /*	List of one or more PEM files, each holding one or more private keys
    521 /*	directly followed by a corresponding certificate chain.
    522 /* .IP "\fBtls_server_sni_maps (empty)\fR"
    523 /*	Optional lookup tables that map names received from remote SMTP
    524 /*	clients via the TLS Server Name Indication (SNI) extension to the
    525 /*	appropriate keys and certificate chains.
    526 /* .PP
    527 /*	Available in Postfix 3.5, 3.4.6, 3.3.5, 3.2.10, 3.1.13 and later:
    528 /* .IP "\fBtls_fast_shutdown_enable (yes)\fR"
    529 /*	A workaround for implementations that hang Postfix while shutting
    530 /*	down a TLS session, until Postfix times out.
    531 /* .PP
    532 /*	Available in Postfix version 3.8 and later:
    533 /* .IP "\fBtls_ffdhe_auto_groups (see 'postconf -d' output)\fR"
    534 /*	The prioritized list of finite-field Diffie-Hellman ephemeral
    535 /*	(FFDHE) key exchange groups supported by the Postfix SMTP client and
    536 /*	server.
    537 /* .PP
    538 /*	Available in Postfix 3.9, 3.8.1, 3.7.6, 3.6.10, 3.5.20 and later:
    539 /* .IP "\fBtls_config_file (default)\fR"
    540 /*	Optional configuration file with baseline OpenSSL settings.
    541 /* .IP "\fBtls_config_name (empty)\fR"
    542 /*	The application name passed by Postfix to OpenSSL library
    543 /*	initialization functions.
    544 /* .PP
    545 /*	Available in Postfix version 3.9 and later:
    546 /* .IP "\fBsmtpd_tls_enable_rpk (no)\fR"
    547 /*	Request that remote SMTP clients send an RFC7250 raw public key
    548 /*	instead of an X.509 certificate, when asking for or requiring client
    549 /*	authentication.
    550 /* .PP
    551 /*	Available in Postfix version 3.11 and later:
    552 /* .IP "\fBrequiretls_enable (yes)\fR"
    553 /*	Enable support for the ESMTP verb "REQUIRETLS" in the "MAIL
    554 /*	FROM" command.
    555 /* .IP "\fBrequiretls_esmtp_header (yes)\fR"
    556 /*	Record the ESMTP REQUIRETLS request in a "Require-TLS-ESMTP:
    557 /*	yes" message header.
    558 /* OBSOLETE TLS CONTROLS
    559 /* .ad
    560 /* .fi
    561 /*	The following configuration parameters exist for compatibility
    562 /*	with Postfix versions before 2.3. Support for these will
    563 /*	be removed in a future release.
    564 /* .IP "\fBsmtpd_use_tls (no)\fR"
    565 /*	Opportunistic TLS: announce STARTTLS support to remote SMTP clients,
    566 /*	but do not require that clients use TLS encryption.
    567 /* .IP "\fBsmtpd_enforce_tls (no)\fR"
    568 /*	Mandatory TLS: announce STARTTLS support to remote SMTP clients,
    569 /*	and reject all plaintext commands except HELO, EHLO, XCLIENT,
    570 /*	STARTTLS, NOOP, QUIT, and (Postfix >= 3.9) HELP.
    571 /* .IP "\fBsmtpd_tls_cipherlist (empty)\fR"
    572 /*	Obsolete Postfix < 2.3 control for the Postfix SMTP server TLS
    573 /*	cipher list.
    574 /* SMTPUTF8 CONTROLS
    575 /* .ad
    576 /* .fi
    577 /*	Preliminary SMTPUTF8 support is introduced with Postfix 3.0.
    578 /* .IP "\fBsmtputf8_enable (yes)\fR"
    579 /*	Enable preliminary SMTPUTF8 support for the protocols described
    580 /*	in RFC 6531, RFC 6532, and RFC 6533.
    581 /* .IP "\fBstrict_smtputf8 (no)\fR"
    582 /*	Enable stricter enforcement of the SMTPUTF8 protocol.
    583 /* .IP "\fBsmtputf8_autodetect_classes (sendmail, verify)\fR"
    584 /*	Detect that a message requires SMTPUTF8 support for the specified
    585 /*	mail origin classes.
    586 /* .PP
    587 /*	Available in Postfix version 3.2 and later:
    588 /* .IP "\fBenable_idna2003_compatibility (no)\fR"
    589 /*	Enable 'transitional' compatibility between IDNA2003 and IDNA2008,
    590 /*	when converting UTF-8 domain names to/from the ASCII form that is
    591 /*	used for DNS lookups.
    592 /* VERP SUPPORT CONTROLS
    593 /* .ad
    594 /* .fi
    595 /*	With VERP style delivery, each recipient of a message receives a
    596 /*	customized copy of the message with his/her own recipient address
    597 /*	encoded in the envelope sender address.  The VERP_README file
    598 /*	describes configuration and operation details of Postfix support
    599 /*	for variable envelope return path addresses.  VERP style delivery
    600 /*	is requested with the SMTP XVERP command or with the "sendmail
    601 /*	-V" command-line option and is available in Postfix version 1.1
    602 /*	and later.
    603 /* .IP "\fBdefault_verp_delimiters (+=)\fR"
    604 /*	The two default VERP delimiter characters.
    605 /* .IP "\fBverp_delimiter_filter (-=+)\fR"
    606 /*	The characters Postfix accepts as VERP delimiter characters on the
    607 /*	Postfix \fBsendmail\fR(1) command line and in SMTP commands.
    608 /* .PP
    609 /*	Available in Postfix version 1.1 and 2.0:
    610 /* .IP "\fBauthorized_verp_clients ($mynetworks)\fR"
    611 /*	What remote SMTP clients are allowed to specify the XVERP command.
    612 /* .PP
    613 /*	Available in Postfix version 2.1 and later:
    614 /* .IP "\fBsmtpd_authorized_verp_clients ($authorized_verp_clients)\fR"
    615 /*	What remote SMTP clients are allowed to specify the XVERP command.
    616 /* TROUBLE SHOOTING CONTROLS
    617 /* .ad
    618 /* .fi
    619 /*	The DEBUG_README document describes how to debug parts of the
    620 /*	Postfix mail system. The methods vary from making the software log
    621 /*	a lot of detail, to running some daemon processes under control of
    622 /*	a call tracer or debugger.
    623 /* .IP "\fBdebug_peer_level (2)\fR"
    624 /*	The increment in verbose logging level when a nexthop destination,
    625 /*	remote client or server name or network address matches a pattern
    626 /*	given with the debug_peer_list parameter.
    627 /* .IP "\fBdebug_peer_list (empty)\fR"
    628 /*	Optional list of nexthop destination, remote client or server
    629 /*	name or network address patterns that, if matched, cause the verbose
    630 /*	logging level to increase by the amount specified in $debug_peer_level.
    631 /* .IP "\fBerror_notice_recipient (postmaster)\fR"
    632 /*	The recipient of postmaster notifications about mail delivery
    633 /*	problems that are caused by policy, resource, software or protocol
    634 /*	errors.
    635 /* .IP "\fBinternal_mail_filter_classes (empty)\fR"
    636 /*	What categories of Postfix-generated mail are subject to
    637 /*	before-queue content inspection by non_smtpd_milters, header_checks
    638 /*	and body_checks.
    639 /* .IP "\fBnotify_classes (resource, software)\fR"
    640 /*	The list of error classes that are reported to the postmaster.
    641 /* .IP "\fBsmtpd_reject_footer (empty)\fR"
    642 /*	Optional information that is appended after each Postfix SMTP
    643 /*	server
    644 /*	4XX or 5XX response.
    645 /* .IP "\fBsoft_bounce (no)\fR"
    646 /*	Safety net to keep mail queued that would otherwise be returned to
    647 /*	the sender.
    648 /* .PP
    649 /*	Available in Postfix version 2.1 and later:
    650 /* .IP "\fBsmtpd_authorized_xclient_hosts (empty)\fR"
    651 /*	What remote SMTP clients are allowed to use the XCLIENT feature.
    652 /* .PP
    653 /*	Available in Postfix version 2.10 and later:
    654 /* .IP "\fBsmtpd_log_access_permit_actions (empty)\fR"
    655 /*	Enable logging of the named "permit" actions in SMTP server
    656 /*	access lists (by default, the SMTP server logs "reject" actions but
    657 /*	not "permit" actions).
    658 /* KNOWN VERSUS UNKNOWN RECIPIENT CONTROLS
    659 /* .ad
    660 /* .fi
    661 /*	As of Postfix version 2.0, the SMTP server rejects mail for
    662 /*	unknown recipients. This prevents the mail queue from clogging up
    663 /*	with undeliverable MAILER-DAEMON messages. Additional information
    664 /*	on this topic is in the LOCAL_RECIPIENT_README and ADDRESS_CLASS_README
    665 /*	documents.
    666 /* .IP "\fBshow_user_unknown_table_name (yes)\fR"
    667 /*	Display the name of the recipient table in the "User unknown"
    668 /*	responses.
    669 /* .IP "\fBcanonical_maps (empty)\fR"
    670 /*	Optional address mapping lookup tables for message headers and
    671 /*	envelopes.
    672 /* .IP "\fBrecipient_canonical_maps (empty)\fR"
    673 /*	Optional address mapping lookup tables for envelope and header
    674 /*	recipient addresses.
    675 /* .IP "\fBsender_canonical_maps (empty)\fR"
    676 /*	Optional address mapping lookup tables for envelope and header
    677 /*	sender addresses.
    678 /* .PP
    679 /*	Parameters concerning known/unknown local recipients:
    680 /* .IP "\fBmydestination ($myhostname, localhost.$mydomain, localhost)\fR"
    681 /*	The list of domains that are delivered via the $local_transport
    682 /*	mail delivery transport.
    683 /* .IP "\fBinet_interfaces (all)\fR"
    684 /*	The local network interface addresses that this mail system
    685 /*	receives mail on.
    686 /* .IP "\fBproxy_interfaces (empty)\fR"
    687 /*	The remote network interface addresses that this mail system receives mail
    688 /*	on by way of a proxy or network address translation unit.
    689 /* .IP "\fBinet_protocols (see 'postconf -d' output)\fR"
    690 /*	The Internet protocols Postfix will attempt to use when making
    691 /*	or accepting connections.
    692 /* .IP "\fBlocal_recipient_maps (proxy:unix:passwd.byname $alias_maps)\fR"
    693 /*	Lookup tables with all names or addresses of valid local
    694 /*	recipients.
    695 /* .IP "\fBunknown_local_recipient_reject_code (550)\fR"
    696 /*	The numerical Postfix SMTP server response code when a recipient
    697 /*	address is local, and $local_recipient_maps specifies a list of
    698 /*	lookup tables that does not match the recipient.
    699 /* .PP
    700 /*	Parameters concerning known/unknown recipients of relay destinations:
    701 /* .IP "\fBrelay_domains (Postfix >= 3.0: empty, Postfix < 3.0: $mydestination)\fR"
    702 /*	What destination domains (and subdomains thereof) this system
    703 /*	will relay mail to.
    704 /* .IP "\fBrelay_recipient_maps (empty)\fR"
    705 /*	Optional lookup tables with all valid addresses in the domains
    706 /*	that match $relay_domains.
    707 /* .IP "\fBunknown_relay_recipient_reject_code (550)\fR"
    708 /*	The numerical Postfix SMTP server reply code when a recipient
    709 /*	address matches $relay_domains, and relay_recipient_maps specifies
    710 /*	a list of lookup tables that does not match the recipient address.
    711 /* .PP
    712 /*	Parameters concerning known/unknown recipients in virtual alias
    713 /*	domains:
    714 /* .IP "\fBvirtual_alias_domains ($virtual_alias_maps)\fR"
    715 /*	Postfix is the final destination for the specified list of virtual
    716 /*	alias domains, that is, domains for which all addresses are aliased
    717 /*	to addresses in other local or remote domains.
    718 /* .IP "\fBvirtual_alias_maps ($virtual_maps)\fR"
    719 /*	Optional lookup tables that are often searched with a full email
    720 /*	address (including domain) and that apply to all recipients: \fBlocal\fR(8),
    721 /*	virtual, and remote; this is unlike alias_maps that are only searched
    722 /*	with an email address localpart (no domain) and that apply
    723 /*	only to \fBlocal\fR(8) recipients.
    724 /* .IP "\fBunknown_virtual_alias_reject_code (550)\fR"
    725 /*	The Postfix SMTP server reply code when a recipient address matches
    726 /*	$virtual_alias_domains, and $virtual_alias_maps specifies a list
    727 /*	of lookup tables that does not match the recipient address.
    728 /* .PP
    729 /*	Parameters concerning known/unknown recipients in virtual mailbox
    730 /*	domains:
    731 /* .IP "\fBvirtual_mailbox_domains ($virtual_mailbox_maps)\fR"
    732 /*	Postfix is the final destination for the specified list of domains;
    733 /*	mail is delivered via the $virtual_transport mail delivery transport.
    734 /* .IP "\fBvirtual_mailbox_maps (empty)\fR"
    735 /*	Optional lookup tables with all valid addresses in the domains that
    736 /*	match $virtual_mailbox_domains.
    737 /* .IP "\fBunknown_virtual_mailbox_reject_code (550)\fR"
    738 /*	The Postfix SMTP server reply code when a recipient address matches
    739 /*	$virtual_mailbox_domains, and $virtual_mailbox_maps specifies a list
    740 /*	of lookup tables that does not match the recipient address.
    741 /* RESOURCE AND RATE CONTROLS
    742 /* .ad
    743 /* .fi
    744 /*	The following parameters limit resource usage by the SMTP
    745 /*	server and/or control client request rates.
    746 /* .IP "\fBline_length_limit (2048)\fR"
    747 /*	Upon input, long lines are chopped up into pieces of at most
    748 /*	this length; upon delivery, long lines are reconstructed.
    749 /* .IP "\fBqueue_minfree (0)\fR"
    750 /*	The minimal amount of free space in bytes in the queue file system
    751 /*	that is needed to receive mail.
    752 /* .IP "\fBmessage_size_limit (10240000)\fR"
    753 /*	The maximal size in bytes of a message, including envelope information.
    754 /* .IP "\fBsmtpd_recipient_limit (1000)\fR"
    755 /*	The maximal number of recipients that the Postfix SMTP server
    756 /*	accepts per message delivery request.
    757 /* .IP "\fBsmtpd_timeout (normal: 300s, overload: 10s)\fR"
    758 /*	When the Postfix SMTP server wants to send an SMTP server
    759 /*	response, how long the Postfix SMTP server will wait for an underlying
    760 /*	network write operation to complete; and when the Postfix SMTP
    761 /*	server Postfix wants to receive an SMTP client request, how long
    762 /*	the Postfix SMTP server will wait for an underlying network read
    763 /*	operation to complete.
    764 /* .IP "\fBsmtpd_history_flush_threshold (100)\fR"
    765 /*	The maximal number of lines in the Postfix SMTP server command history
    766 /*	before it is flushed upon receipt of EHLO, RSET, or end of DATA.
    767 /* .PP
    768 /*	Available in Postfix version 2.3 and later:
    769 /* .IP "\fBsmtpd_peername_lookup (yes)\fR"
    770 /*	Attempt to look up the remote SMTP client hostname, and verify that
    771 /*	the name matches the client IP address.
    772 /* .PP
    773 /*	The per SMTP client connection count and request rate limits are
    774 /*	implemented in co-operation with the \fBanvil\fR(8) service, and
    775 /*	are available in Postfix version 2.2 and later.
    776 /* .IP "\fBsmtpd_client_connection_count_limit (50)\fR"
    777 /*	How many simultaneous connections any client is allowed to
    778 /*	make to this service.
    779 /* .IP "\fBsmtpd_client_connection_rate_limit (0)\fR"
    780 /*	The maximal number of connection attempts any client is allowed to
    781 /*	make to this service per time unit.
    782 /* .IP "\fBsmtpd_client_message_rate_limit (0)\fR"
    783 /*	The maximal number of message delivery requests that any client is
    784 /*	allowed to make to this service per time unit, regardless of whether
    785 /*	or not Postfix actually accepts those messages.
    786 /* .IP "\fBsmtpd_client_recipient_rate_limit (0)\fR"
    787 /*	The maximal number of recipient addresses that any client is allowed
    788 /*	to send to this service per time unit, regardless of whether or not
    789 /*	Postfix actually accepts those recipients.
    790 /* .IP "\fBsmtpd_client_event_limit_exceptions ($mynetworks)\fR"
    791 /*	Clients that are excluded from smtpd_client_*_count/rate_limit
    792 /*	restrictions.
    793 /* .PP
    794 /*	Available in Postfix version 2.3 and later:
    795 /* .IP "\fBsmtpd_client_new_tls_session_rate_limit (0)\fR"
    796 /*	The maximal number of new (i.e., uncached) TLS sessions that a
    797 /*	remote SMTP client is allowed to negotiate with this service per
    798 /*	time unit.
    799 /* .PP
    800 /*	Available in Postfix version 2.9 - 3.6:
    801 /* .IP "\fBsmtpd_per_record_deadline (normal: no, overload: yes)\fR"
    802 /*	Change the behavior of the smtpd_timeout and smtpd_starttls_timeout
    803 /*	time limits, from a
    804 /*	time limit per read or write system call, to a time limit to send
    805 /*	or receive a complete record (an SMTP command line, SMTP response
    806 /*	line, SMTP message content line, or TLS protocol message).
    807 /* .PP
    808 /*	Available in Postfix version 3.1 and later:
    809 /* .IP "\fBsmtpd_client_auth_rate_limit (0)\fR"
    810 /*	The maximal number of AUTH commands that any client is allowed to
    811 /*	send to this service per time unit, regardless of whether or not
    812 /*	Postfix actually accepts those commands.
    813 /* .PP
    814 /*	Available in Postfix version 3.7 and later:
    815 /* .IP "\fBsmtpd_per_request_deadline (normal: no, overload: yes)\fR"
    816 /*	Change the behavior of the smtpd_timeout and smtpd_starttls_timeout
    817 /*	time limits, from a time limit per plaintext or TLS read or write
    818 /*	call, to a combined time limit for receiving a complete SMTP request
    819 /*	and for sending a complete SMTP response.
    820 /* .IP "\fBsmtpd_min_data_rate (500)\fR"
    821 /*	The minimum plaintext data transfer rate in bytes/second for
    822 /*	DATA and BDAT requests, when deadlines are enabled with
    823 /*	smtpd_per_request_deadline.
    824 /* .IP "\fBheader_from_format (standard)\fR"
    825 /*	The format of the Postfix-generated \fBFrom:\fR header.
    826 /* .PP
    827 /*	Available in Postfix version 3.8 and later:
    828 /* .IP "\fBsmtpd_client_ipv4_prefix_length (32)\fR"
    829 /*	Aggregate smtpd_client_*_count and smtpd_client_*_rate statistics
    830 /*	by IPv4 network blocks with the specified network prefix.
    831 /* .IP "\fBsmtpd_client_ipv6_prefix_length (84)\fR"
    832 /*	Aggregate smtpd_client_*_count and smtpd_client_*_rate statistics
    833 /*	by IPv6 network blocks with the specified network prefix.
    834 /* .PP
    835 /*	Available in Postfix 3.9, 3.8.1, 3.7.6, 3.6.10, 3.5.20 and later:
    836 /* .IP "\fBsmtpd_forbid_unauth_pipelining (Postfix >= 3.9: yes)\fR"
    837 /*	Disconnect remote SMTP clients that violate RFC 2920 (or 5321)
    838 /*	command pipelining constraints.
    839 /* .PP
    840 /*	Available in Postfix 3.9, 3.8.4, 3.7.9, 3.6.13, 3.5.23 and later:
    841 /* .IP "\fBsmtpd_forbid_bare_newline (Postfix >= 3.9: normalize)\fR"
    842 /*	Reject or restrict input lines from an SMTP client that end in
    843 /*	<LF> instead of the standard <CR><LF>.
    844 /* .IP "\fBsmtpd_forbid_bare_newline_exclusions ($mynetworks)\fR"
    845 /*	Exclude the specified clients from smtpd_forbid_bare_newline
    846 /*	enforcement.
    847 /* .PP
    848 /*	Available in Postfix 3.9, 3.8.5, 3.7.10, 3.6.14, 3.5.24 and
    849 /*	later:
    850 /* .IP "\fBsmtpd_forbid_bare_newline_reject_code (550)\fR"
    851 /*	The numerical Postfix SMTP server response code when rejecting a
    852 /*	request with "smtpd_forbid_bare_newline = reject".
    853 /* TARPIT CONTROLS
    854 /* .ad
    855 /* .fi
    856 /*	When a remote SMTP client makes errors, the Postfix SMTP server
    857 /*	can insert delays before responding. This can help to slow down
    858 /*	run-away software.  The behavior is controlled by an error counter
    859 /*	that counts the number of errors within an SMTP session that a
    860 /*	client makes without delivering mail.
    861 /* .IP "\fBsmtpd_error_sleep_time (1s)\fR"
    862 /*	With Postfix version 2.1 and later: the SMTP server response delay after
    863 /*	a client has made more than $smtpd_soft_error_limit errors, and
    864 /*	fewer than $smtpd_hard_error_limit errors, without delivering mail.
    865 /* .IP "\fBsmtpd_soft_error_limit (10)\fR"
    866 /*	The number of errors a remote SMTP client is allowed to make without
    867 /*	delivering mail before the Postfix SMTP server slows down all its
    868 /*	responses.
    869 /* .IP "\fBsmtpd_hard_error_limit (normal: 20, overload: 1)\fR"
    870 /*	The maximal number of errors a remote SMTP client is allowed to
    871 /*	make without delivering mail.
    872 /* .IP "\fBsmtpd_junk_command_limit (normal: 100, overload: 1)\fR"
    873 /*	The number of junk commands (NOOP, VRFY, ETRN or RSET) that a remote
    874 /*	SMTP client can send before the Postfix SMTP server starts to
    875 /*	increment the error counter with each junk command.
    876 /* .PP
    877 /*	Available in Postfix version 2.1 and later:
    878 /* .IP "\fBsmtpd_recipient_overshoot_limit (1000)\fR"
    879 /*	The number of recipients that a remote SMTP client can send in
    880 /*	excess of the limit specified with $smtpd_recipient_limit, before
    881 /*	the Postfix SMTP server increments the per-session error count
    882 /*	for each excess recipient.
    883 /* ACCESS POLICY DELEGATION CONTROLS
    884 /* .ad
    885 /* .fi
    886 /*	As of version 2.1, Postfix can be configured to delegate access
    887 /*	policy decisions to an external server that runs outside Postfix.
    888 /*	See the file SMTPD_POLICY_README for more information.
    889 /* .IP "\fBsmtpd_policy_service_max_idle (300s)\fR"
    890 /*	The time after which an idle SMTPD policy service connection is
    891 /*	closed.
    892 /* .IP "\fBsmtpd_policy_service_max_ttl (1000s)\fR"
    893 /*	The time after which an active SMTPD policy service connection is
    894 /*	closed.
    895 /* .IP "\fBsmtpd_policy_service_timeout (100s)\fR"
    896 /*	The time limit for connecting to, writing to, or receiving from a
    897 /*	delegated SMTPD policy server.
    898 /* .PP
    899 /*	Available in Postfix version 3.0 and later:
    900 /* .IP "\fBsmtpd_policy_service_default_action (451 4.3.5 Server configuration problem)\fR"
    901 /*	The default action when an SMTPD policy service request fails.
    902 /* .IP "\fBsmtpd_policy_service_request_limit (0)\fR"
    903 /*	The maximal number of requests per SMTPD policy service connection,
    904 /*	or zero (no limit).
    905 /* .IP "\fBsmtpd_policy_service_try_limit (2)\fR"
    906 /*	The maximal number of attempts to send an SMTPD policy service
    907 /*	request before giving up.
    908 /* .IP "\fBsmtpd_policy_service_retry_delay (1s)\fR"
    909 /*	The delay between attempts to resend a failed SMTPD policy
    910 /*	service request.
    911 /* .PP
    912 /*	Available in Postfix version 3.1 and later:
    913 /* .IP "\fBsmtpd_policy_service_policy_context (empty)\fR"
    914 /*	Optional information that the Postfix SMTP server specifies in
    915 /*	the "policy_context" attribute of a policy service request (originally,
    916 /*	to share the same service endpoint among multiple check_policy_service
    917 /*	clients).
    918 /* ACCESS CONTROLS
    919 /* .ad
    920 /* .fi
    921 /*	The SMTPD_ACCESS_README document gives an introduction to all the
    922 /*	SMTP server access control features.
    923 /* .IP "\fBsmtpd_delay_reject (yes)\fR"
    924 /*	Wait until the RCPT TO command before evaluating
    925 /*	$smtpd_client_restrictions, $smtpd_helo_restrictions and
    926 /*	$smtpd_sender_restrictions, or wait until the ETRN command before
    927 /*	evaluating $smtpd_client_restrictions and $smtpd_helo_restrictions.
    928 /* .IP "\fBparent_domain_matches_subdomains (see 'postconf -d' output)\fR"
    929 /*	A list of Postfix features where the pattern "example.com" also
    930 /*	matches subdomains of example.com,
    931 /*	instead of requiring an explicit ".example.com" pattern.
    932 /* .IP "\fBsmtpd_client_restrictions (empty)\fR"
    933 /*	Optional restrictions that the Postfix SMTP server applies in the
    934 /*	context of a client connection request.
    935 /* .IP "\fBsmtpd_helo_required (no)\fR"
    936 /*	Require that a remote SMTP client introduces itself with the HELO
    937 /*	or EHLO command before sending the MAIL command or other commands
    938 /*	that require EHLO negotiation.
    939 /* .IP "\fBsmtpd_helo_restrictions (empty)\fR"
    940 /*	Optional restrictions that the Postfix SMTP server applies in the
    941 /*	context of a client HELO command.
    942 /* .IP "\fBsmtpd_sender_restrictions (empty)\fR"
    943 /*	Optional restrictions that the Postfix SMTP server applies in the
    944 /*	context of a client MAIL FROM command.
    945 /* .IP "\fBsmtpd_recipient_restrictions (see 'postconf -d' output)\fR"
    946 /*	Optional restrictions that the Postfix SMTP server applies in the
    947 /*	context of a client RCPT TO command, after smtpd_relay_restrictions.
    948 /* .IP "\fBsmtpd_etrn_restrictions (empty)\fR"
    949 /*	Optional restrictions that the Postfix SMTP server applies in the
    950 /*	context of a client ETRN command.
    951 /* .IP "\fBallow_untrusted_routing (no)\fR"
    952 /*	Forward mail with sender-specified routing (user[@%!]remote[@%!]site)
    953 /*	from untrusted clients to destinations matching $relay_domains.
    954 /* .IP "\fBsmtpd_restriction_classes (empty)\fR"
    955 /*	User-defined aliases for groups of access restrictions.
    956 /* .IP "\fBsmtpd_null_access_lookup_key (<>)\fR"
    957 /*	The lookup key to be used in SMTP \fBaccess\fR(5) tables instead of the
    958 /*	null sender address.
    959 /* .IP "\fBpermit_mx_backup_networks (empty)\fR"
    960 /*	Restrict the use of the permit_mx_backup SMTP access feature to
    961 /*	only domains whose primary MX hosts match the listed networks.
    962 /* .PP
    963 /*	Available in Postfix version 2.0 and later:
    964 /* .IP "\fBsmtpd_data_restrictions (empty)\fR"
    965 /*	Optional access restrictions that the Postfix SMTP server applies
    966 /*	in the context of the SMTP DATA command.
    967 /* .IP "\fBsmtpd_expansion_filter (see 'postconf -d' output)\fR"
    968 /*	What characters are allowed in $name expansions of RBL reply
    969 /*	templates.
    970 /* .PP
    971 /*	Available in Postfix version 2.1 and later:
    972 /* .IP "\fBsmtpd_reject_unlisted_sender (no)\fR"
    973 /*	Request that the Postfix SMTP server rejects mail from unknown
    974 /*	sender addresses, even when no explicit reject_unlisted_sender
    975 /*	access restriction is specified.
    976 /* .IP "\fBsmtpd_reject_unlisted_recipient (yes)\fR"
    977 /*	Request that the Postfix SMTP server rejects mail for unknown
    978 /*	recipient addresses, even when no explicit reject_unlisted_recipient
    979 /*	access restriction is specified.
    980 /* .PP
    981 /*	Available in Postfix version 2.2 and later:
    982 /* .IP "\fBsmtpd_end_of_data_restrictions (empty)\fR"
    983 /*	Optional access restrictions that the Postfix SMTP server
    984 /*	applies in the context of the SMTP END-OF-DATA command.
    985 /* .PP
    986 /*	Available in Postfix version 2.10 and later:
    987 /* .IP "\fBsmtpd_relay_restrictions (permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination)\fR"
    988 /*	Access restrictions for mail relay control that the Postfix
    989 /*	SMTP server applies in the context of the RCPT TO command, before
    990 /*	smtpd_recipient_restrictions.
    991 /* SENDER AND RECIPIENT ADDRESS VERIFICATION CONTROLS
    992 /* .ad
    993 /* .fi
    994 /*	Postfix version 2.1 introduces sender and recipient address verification.
    995 /*	This feature is implemented by sending probe email messages that
    996 /*	are not actually delivered.
    997 /*	This feature is requested via the reject_unverified_sender and
    998 /*	reject_unverified_recipient access restrictions.  The status of
    999 /*	verification probes is maintained by the \fBverify\fR(8) server.
   1000 /*	See the file ADDRESS_VERIFICATION_README for information
   1001 /*	about how to configure and operate the Postfix sender/recipient
   1002 /*	address verification service.
   1003 /* .IP "\fBaddress_verify_poll_count (normal: 3, overload: 1)\fR"
   1004 /*	How many times to query the \fBverify\fR(8) service for the completion
   1005 /*	of an address verification request in progress.
   1006 /* .IP "\fBaddress_verify_poll_delay (3s)\fR"
   1007 /*	The delay between queries for the completion of an address
   1008 /*	verification request in progress.
   1009 /* .IP "\fBaddress_verify_sender ($double_bounce_sender)\fR"
   1010 /*	The sender address to use in address verification probes; prior
   1011 /*	to Postfix 2.5 the default was "postmaster".
   1012 /* .IP "\fBunverified_sender_reject_code (450)\fR"
   1013 /*	The numerical Postfix SMTP server response code when a recipient
   1014 /*	address is rejected by the reject_unverified_sender restriction.
   1015 /* .IP "\fBunverified_recipient_reject_code (450)\fR"
   1016 /*	The numerical Postfix SMTP server response when a recipient address
   1017 /*	is rejected by the reject_unverified_recipient restriction.
   1018 /* .PP
   1019 /*	Available in Postfix version 2.6 and later:
   1020 /* .IP "\fBunverified_sender_defer_code (450)\fR"
   1021 /*	The numerical Postfix SMTP server response code when a sender address
   1022 /*	probe fails due to a temporary error condition.
   1023 /* .IP "\fBunverified_recipient_defer_code (450)\fR"
   1024 /*	The numerical Postfix SMTP server response when a recipient address
   1025 /*	probe fails due to a temporary error condition.
   1026 /* .IP "\fBunverified_sender_reject_reason (empty)\fR"
   1027 /*	The Postfix SMTP server's reply when rejecting mail with
   1028 /*	reject_unverified_sender.
   1029 /* .IP "\fBunverified_recipient_reject_reason (empty)\fR"
   1030 /*	The Postfix SMTP server's reply when rejecting mail with
   1031 /*	reject_unverified_recipient.
   1032 /* .IP "\fBunverified_sender_tempfail_action ($reject_tempfail_action)\fR"
   1033 /*	The Postfix SMTP server's action when reject_unverified_sender
   1034 /*	fails due to a temporary error condition.
   1035 /* .IP "\fBunverified_recipient_tempfail_action ($reject_tempfail_action)\fR"
   1036 /*	The Postfix SMTP server's action when reject_unverified_recipient
   1037 /*	fails due to a temporary error condition.
   1038 /* .PP
   1039 /*	Available with Postfix 2.9 and later:
   1040 /* .IP "\fBaddress_verify_sender_ttl (0s)\fR"
   1041 /*	The time between changes in the time-dependent portion of address
   1042 /*	verification probe sender addresses.
   1043 /* ACCESS CONTROL RESPONSES
   1044 /* .ad
   1045 /* .fi
   1046 /*	The following parameters control numerical SMTP reply codes
   1047 /*	and/or text responses.
   1048 /* .IP "\fBaccess_map_reject_code (554)\fR"
   1049 /*	The numerical Postfix SMTP server response code for
   1050 /*	an \fBaccess\fR(5) map "reject" action.
   1051 /* .IP "\fBdefer_code (450)\fR"
   1052 /*	The numerical Postfix SMTP server response code when a remote SMTP
   1053 /*	client request is rejected by the "defer" restriction.
   1054 /* .IP "\fBinvalid_hostname_reject_code (501)\fR"
   1055 /*	The numerical Postfix SMTP server response code when the client
   1056 /*	HELO or EHLO command parameter is rejected by the reject_invalid_helo_hostname
   1057 /*	restriction.
   1058 /* .IP "\fBmaps_rbl_reject_code (554)\fR"
   1059 /*	The numerical Postfix SMTP server response code when a remote SMTP
   1060 /*	client request is blocked by the reject_rbl_client, reject_rhsbl_client,
   1061 /*	reject_rhsbl_reverse_client, reject_rhsbl_sender or
   1062 /*	reject_rhsbl_recipient restriction.
   1063 /* .IP "\fBnon_fqdn_reject_code (504)\fR"
   1064 /*	The numerical Postfix SMTP server reply code when a client request
   1065 /*	is rejected by the reject_non_fqdn_helo_hostname, reject_non_fqdn_sender
   1066 /*	or reject_non_fqdn_recipient restriction.
   1067 /* .IP "\fBplaintext_reject_code (450)\fR"
   1068 /*	The numerical Postfix SMTP server response code when a request
   1069 /*	is rejected by the \fBreject_plaintext_session\fR restriction.
   1070 /* .IP "\fBreject_code (554)\fR"
   1071 /*	The numerical Postfix SMTP server response code when a remote SMTP
   1072 /*	client request is rejected by the "reject" restriction.
   1073 /* .IP "\fBrelay_domains_reject_code (554)\fR"
   1074 /*	The numerical Postfix SMTP server response code when a client
   1075 /*	request is rejected by the reject_unauth_destination recipient
   1076 /*	restriction.
   1077 /* .IP "\fBunknown_address_reject_code (450)\fR"
   1078 /*	The numerical response code when the Postfix SMTP server rejects a
   1079 /*	sender or recipient address because its domain is unknown.
   1080 /* .IP "\fBunknown_client_reject_code (450)\fR"
   1081 /*	The numerical Postfix SMTP server response code when a client
   1082 /*	without valid address <=> name mapping is rejected by the
   1083 /*	reject_unknown_client_hostname restriction.
   1084 /* .IP "\fBunknown_hostname_reject_code (450)\fR"
   1085 /*	The numerical Postfix SMTP server response code when the hostname
   1086 /*	specified with the HELO or EHLO command is rejected by the
   1087 /*	reject_unknown_helo_hostname restriction.
   1088 /* .PP
   1089 /*	Available in Postfix version 2.0 and later:
   1090 /* .IP "\fBdefault_rbl_reply (see 'postconf -d' output)\fR"
   1091 /*	The default Postfix SMTP server response template for a request that is
   1092 /*	rejected by an RBL-based restriction.
   1093 /* .IP "\fBmulti_recipient_bounce_reject_code (550)\fR"
   1094 /*	The numerical Postfix SMTP server response code when a remote SMTP
   1095 /*	client request is blocked by the reject_multi_recipient_bounce
   1096 /*	restriction.
   1097 /* .IP "\fBrbl_reply_maps (empty)\fR"
   1098 /*	Optional lookup tables with RBL or RHSBL response templates.
   1099 /* .PP
   1100 /*	Available in Postfix version 2.6 and later:
   1101 /* .IP "\fBaccess_map_defer_code (450)\fR"
   1102 /*	The numerical Postfix SMTP server response code for
   1103 /*	an \fBaccess\fR(5) map "defer" action, including "defer_if_permit"
   1104 /*	or "defer_if_reject".
   1105 /* .IP "\fBreject_tempfail_action (defer_if_permit)\fR"
   1106 /*	The Postfix SMTP server's action when a reject-type restriction
   1107 /*	fails due to a temporary error condition.
   1108 /* .IP "\fBunknown_helo_hostname_tempfail_action ($reject_tempfail_action)\fR"
   1109 /*	The Postfix SMTP server's action when reject_unknown_helo_hostname
   1110 /*	fails due to a temporary error condition.
   1111 /* .IP "\fBunknown_address_tempfail_action ($reject_tempfail_action)\fR"
   1112 /*	The Postfix SMTP server's action when reject_unknown_sender_domain
   1113 /*	or reject_unknown_recipient_domain fail due to a temporary error
   1114 /*	condition.
   1115 /* MISCELLANEOUS CONTROLS
   1116 /* .ad
   1117 /* .fi
   1118 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
   1119 /*	The default location of the Postfix main.cf and master.cf
   1120 /*	configuration files.
   1121 /* .IP "\fBdaemon_timeout (18000s)\fR"
   1122 /*	How much time a Postfix daemon process may take to handle a
   1123 /*	request before it is terminated by a built-in watchdog timer.
   1124 /* .IP "\fBcommand_directory (see 'postconf -d' output)\fR"
   1125 /*	The location of all postfix administrative commands.
   1126 /* .IP "\fBdouble_bounce_sender (double-bounce)\fR"
   1127 /*	The sender address of postmaster notifications that are generated
   1128 /*	by the mail system.
   1129 /* .IP "\fBipc_timeout (3600s)\fR"
   1130 /*	The time limit for sending or receiving information over an internal
   1131 /*	communication channel.
   1132 /* .IP "\fBmail_name (Postfix)\fR"
   1133 /*	The mail system name that is displayed in Received: headers, in
   1134 /*	the SMTP greeting banner, and in bounced mail.
   1135 /* .IP "\fBmail_owner (postfix)\fR"
   1136 /*	The UNIX system account that owns the Postfix queue and most Postfix
   1137 /*	daemon processes.
   1138 /* .IP "\fBmax_idle (100s)\fR"
   1139 /*	The maximum amount of time that an idle Postfix daemon process waits
   1140 /*	for an incoming connection before terminating voluntarily.
   1141 /* .IP "\fBmax_use (100)\fR"
   1142 /*	The maximal number of incoming connections that a Postfix daemon
   1143 /*	process will service before terminating voluntarily.
   1144 /* .IP "\fBmyhostname (see 'postconf -d' output)\fR"
   1145 /*	The internet hostname of this mail system.
   1146 /* .IP "\fBmynetworks (see 'postconf -d' output)\fR"
   1147 /*	The list of "trusted" remote SMTP clients that have more privileges than
   1148 /*	"strangers".
   1149 /* .IP "\fBmyorigin ($myhostname)\fR"
   1150 /*	The domain name that locally-posted mail appears to come
   1151 /*	from, and that locally posted mail is delivered to.
   1152 /* .IP "\fBprocess_id (read-only)\fR"
   1153 /*	The process ID of a Postfix command or daemon process.
   1154 /* .IP "\fBprocess_name (read-only)\fR"
   1155 /*	The process name of a Postfix command or daemon process.
   1156 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
   1157 /*	The location of the Postfix top-level queue directory.
   1158 /* .IP "\fBrecipient_delimiter (empty)\fR"
   1159 /*	The set of characters that can separate an email address
   1160 /*	localpart, user name, or a .forward file name from its extension.
   1161 /* .IP "\fBsmtpd_banner ($myhostname ESMTP $mail_name)\fR"
   1162 /*	The text that follows the 220 status code in the SMTP greeting
   1163 /*	banner.
   1164 /* .IP "\fBsyslog_facility (mail)\fR"
   1165 /*	The syslog facility of Postfix logging.
   1166 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
   1167 /*	A prefix that is prepended to the process name in syslog
   1168 /*	records, so that, for example, "smtpd" becomes "prefix/smtpd".
   1169 /* .PP
   1170 /*	Available in Postfix version 2.2 and later:
   1171 /* .IP "\fBsmtpd_forbidden_commands (CONNECT GET POST regexp:{{/^[^A-Z]/ Bogus}})\fR"
   1172 /*	List of commands that cause the Postfix SMTP server to immediately
   1173 /*	terminate the session with a 221 code.
   1174 /* .PP
   1175 /*	Available in Postfix version 2.5 and later:
   1176 /* .IP "\fBsmtpd_client_port_logging (no)\fR"
   1177 /*	Enable logging of the remote SMTP client port in addition to
   1178 /*	the hostname and IP address.
   1179 /* .PP
   1180 /*	Available in Postfix 3.3 and later:
   1181 /* .IP "\fBservice_name (read-only)\fR"
   1182 /*	The master.cf service name of a Postfix daemon process.
   1183 /* .PP
   1184 /*	Available in Postfix 3.4 and later:
   1185 /* .IP "\fBsmtpd_reject_footer_maps (empty)\fR"
   1186 /*	Lookup tables, indexed by the complete Postfix SMTP server 4xx or
   1187 /*	5xx response, with reject footer templates.
   1188 /* .PP
   1189 /*	Available in Postfix 3.10 and later:
   1190 /* .IP "\fBsmtpd_hide_client_session (no)\fR"
   1191 /*	Do not include SMTP client session information in the Postfix
   1192 /*	SMTP server's Received: message header.
   1193 /* .PP
   1194 /*	Available in Postfix version 3.11 and later:
   1195 /* .IP "\fBsmtpd_reject_filter_maps (empty)\fR"
   1196 /*	An optional filter that can replace a reject response from the
   1197 /*	Postfix SMTP server itself, or from a program that replies through
   1198 /*	the Postfix SMTP server.
   1199 /* SEE ALSO
   1200 /*	anvil(8), connection/rate limiting
   1201 /*	cleanup(8), message canonicalization
   1202 /*	tlsmgr(8), TLS session and PRNG management
   1203 /*	trivial-rewrite(8), address resolver
   1204 /*	verify(8), address verification service
   1205 /*	postconf(5), configuration parameters
   1206 /*	master(5), generic daemon options
   1207 /*	master(8), process manager
   1208 /*	postlogd(8), Postfix logging
   1209 /*	syslogd(8), system logging
   1210 /* README FILES
   1211 /* .ad
   1212 /* .fi
   1213 /*	Use "\fBpostconf readme_directory\fR" or
   1214 /*	"\fBpostconf html_directory\fR" to locate this information.
   1215 /* .na
   1216 /* .nf
   1217 /*	ADDRESS_CLASS_README, blocking unknown hosted or relay recipients
   1218 /*	ADDRESS_REWRITING_README, Postfix address manipulation
   1219 /*	BDAT_README, Postfix CHUNKING support
   1220 /*	FILTER_README, external after-queue content filter
   1221 /*	LOCAL_RECIPIENT_README, blocking unknown local recipients
   1222 /*	MILTER_README, before-queue mail filter applications
   1223 /*	SMTPD_ACCESS_README, built-in access policies
   1224 /*	SMTPD_POLICY_README, external policy server
   1225 /*	SMTPD_PROXY_README, external before-queue content filter
   1226 /*	SASL_README, Postfix SASL howto
   1227 /*	TLS_README, Postfix STARTTLS howto
   1228 /*	VERP_README, Postfix XVERP extension
   1229 /*	XCLIENT_README, Postfix XCLIENT extension
   1230 /*	XFORWARD_README, Postfix XFORWARD extension
   1231 /* LICENSE
   1232 /* .ad
   1233 /* .fi
   1234 /*	The Secure Mailer license must be distributed with this software.
   1235 /* AUTHOR(S)
   1236 /*	Wietse Venema
   1237 /*	IBM T.J. Watson Research
   1238 /*	P.O. Box 704
   1239 /*	Yorktown Heights, NY 10598, USA
   1240 /*
   1241 /*	Wietse Venema
   1242 /*	Google, Inc.
   1243 /*	111 8th Avenue
   1244 /*	New York, NY 10011, USA
   1245 /*
   1246 /*	Wietse Venema
   1247 /*	porcupine.org
   1248 /*
   1249 /*	SASL support originally by:
   1250 /*	Till Franke
   1251 /*	SuSE Rhein/Main AG
   1252 /*	65760 Eschborn, Germany
   1253 /*
   1254 /*	TLS support originally by:
   1255 /*	Lutz Jaenicke
   1256 /*	BTU Cottbus
   1257 /*	Allgemeine Elektrotechnik
   1258 /*	Universitaetsplatz 3-4
   1259 /*	D-03044 Cottbus, Germany
   1260 /*
   1261 /*	Revised TLS support by:
   1262 /*	Victor Duchovni
   1263 /*	Morgan Stanley
   1264 /*--*/
   1265 
   1266 /* System library. */
   1267 
   1268 #include <sys_defs.h>
   1269 #include <sys/socket.h>
   1270 #include <sys/stat.h>
   1271 #include <netinet/in.h>
   1272 #include <arpa/inet.h>
   1273 #include <netdb.h>
   1274 #include <string.h>
   1275 #include <stdio.h>			/* remove() */
   1276 #include <unistd.h>
   1277 #include <stdlib.h>
   1278 #include <errno.h>
   1279 #include <ctype.h>
   1280 #include <signal.h>
   1281 #include <stddef.h>			/* offsetof() */
   1282 
   1283 #ifdef STRCASECMP_IN_STRINGS_H
   1284 #include <strings.h>
   1285 #endif
   1286 
   1287 /* Utility library. */
   1288 
   1289 #include <msg.h>
   1290 #include <mymalloc.h>
   1291 #include <vstring.h>
   1292 #include <vstream.h>
   1293 #include <vstring_vstream.h>
   1294 #include <stringops.h>
   1295 #include <events.h>
   1296 #include <smtp_stream.h>
   1297 #include <valid_hostname.h>
   1298 #include <dict.h>
   1299 #include <watchdog.h>
   1300 #include <iostuff.h>
   1301 #include <split_at.h>
   1302 #include <name_code.h>
   1303 #include <inet_proto.h>
   1304 
   1305 /* Global library. */
   1306 
   1307 #include <mail_params.h>
   1308 #include <mail_version.h>		/* milter_macro_v */
   1309 #include <record.h>
   1310 #include <rec_type.h>
   1311 #include <mail_proto.h>
   1312 #include <cleanup_user.h>
   1313 #include <mail_date.h>
   1314 #include <mail_conf.h>
   1315 #include <off_cvt.h>
   1316 #include <debug_peer.h>
   1317 #include <mail_error.h>
   1318 #include <flush_clnt.h>
   1319 #include <mail_stream.h>
   1320 #include <mail_queue.h>
   1321 #include <tok822.h>
   1322 #include <verp_sender.h>
   1323 #include <string_list.h>
   1324 #include <quote_822_local.h>
   1325 #include <lex_822.h>
   1326 #include <namadr_list.h>
   1327 #include <input_transp.h>
   1328 #include <is_header.h>
   1329 #include <anvil_clnt.h>
   1330 #include <flush_clnt.h>
   1331 #include <ehlo_mask.h>			/* ehlo filter */
   1332 #include <maps.h>			/* ehlo filter */
   1333 #include <valid_mailhost_addr.h>
   1334 #include <dsn_mask.h>
   1335 #include <xtext.h>
   1336 #include <uxtext.h>
   1337 #include <tls_proxy.h>
   1338 #include <verify_sender_addr.h>
   1339 #include <smtputf8.h>
   1340 #include <match_parent_style.h>
   1341 #include <normalize_mailhost_addr.h>
   1342 #include <info_log_addr_form.h>
   1343 #include <hfrom_format.h>
   1344 
   1345 /* Single-threaded server skeleton. */
   1346 
   1347 #include <mail_server.h>
   1348 
   1349 /* Mail filter library. */
   1350 
   1351 #include <milter.h>
   1352 
   1353 /* DNS library. */
   1354 
   1355 #include <dns.h>
   1356 
   1357 /* Application-specific */
   1358 
   1359 #include <smtpd_token.h>
   1360 #include <smtpd.h>
   1361 #include <smtpd_check.h>
   1362 #include <smtpd_chat.h>
   1363 #include <smtpd_sasl_proto.h>
   1364 #include <smtpd_sasl_glue.h>
   1365 #include <smtpd_proxy.h>
   1366 #include <smtpd_milter.h>
   1367 #include <smtpd_expand.h>
   1368 
   1369 #include "pfilter.h"
   1370 
   1371  /*
   1372   * Tunable parameters. Make sure that there is some bound on the length of
   1373   * an SMTP command, so that the mail system stays in control even when a
   1374   * malicious client sends commands of unreasonable length (qmail-dos-1).
   1375   * Make sure there is some bound on the number of recipients, so that the
   1376   * mail system stays in control even when a malicious client sends an
   1377   * unreasonable number of recipients (qmail-dos-2).
   1378   */
   1379 int     var_smtpd_rcpt_limit;
   1380 int     var_smtpd_tmout;
   1381 int     var_smtpd_soft_erlim;
   1382 int     var_smtpd_hard_erlim;
   1383 long    var_queue_minfree;		/* XXX use off_t */
   1384 char   *var_smtpd_banner;
   1385 char   *var_notify_classes;
   1386 char   *var_client_checks;
   1387 char   *var_helo_checks;
   1388 char   *var_mail_checks;
   1389 char   *var_relay_checks;
   1390 char   *var_rcpt_checks;
   1391 char   *var_etrn_checks;
   1392 char   *var_data_checks;
   1393 char   *var_eod_checks;
   1394 int     var_unk_client_code;
   1395 int     var_bad_name_code;
   1396 int     var_unk_name_code;
   1397 int     var_unk_addr_code;
   1398 int     var_relay_code;
   1399 int     var_maps_rbl_code;
   1400 int     var_map_reject_code;
   1401 int     var_map_defer_code;
   1402 char   *var_maps_rbl_domains;
   1403 char   *var_rbl_reply_maps;
   1404 bool    var_helo_required;
   1405 int     var_reject_code;
   1406 int     var_defer_code;
   1407 int     var_smtpd_err_sleep;
   1408 int     var_non_fqdn_code;
   1409 char   *var_bounce_rcpt;
   1410 char   *var_error_rcpt;
   1411 bool    var_smtpd_delay_reject;
   1412 char   *var_rest_classes;
   1413 bool    var_strict_rfc821_env;
   1414 bool    var_disable_vrfy_cmd;
   1415 char   *var_canonical_maps;
   1416 char   *var_send_canon_maps;
   1417 char   *var_rcpt_canon_maps;
   1418 char   *var_virt_alias_maps;
   1419 char   *var_virt_mailbox_maps;
   1420 char   *var_alias_maps;
   1421 char   *var_local_rcpt_maps;
   1422 bool    var_allow_untrust_route;
   1423 int     var_smtpd_junk_cmd_limit;
   1424 int     var_smtpd_rcpt_overlim;
   1425 bool    var_smtpd_sasl_enable;
   1426 bool    var_smtpd_sasl_auth_hdr;
   1427 char   *var_smtpd_sasl_opts;
   1428 char   *var_smtpd_sasl_path;
   1429 char   *var_smtpd_sasl_service;
   1430 char   *var_cyrus_conf_path;
   1431 char   *var_smtpd_sasl_realm;
   1432 int     var_smtpd_sasl_resp_limit;
   1433 char   *var_smtpd_sasl_exceptions_networks;
   1434 char   *var_smtpd_sasl_type;
   1435 char   *var_smtpd_sasl_mech_filter;
   1436 char   *var_filter_xport;
   1437 bool    var_broken_auth_clients;
   1438 char   *var_perm_mx_networks;
   1439 char   *var_smtpd_snd_auth_maps;
   1440 char   *var_smtpd_noop_cmds;
   1441 char   *var_smtpd_null_key;
   1442 int     var_smtpd_hist_thrsh;
   1443 char   *var_smtpd_exp_filter;
   1444 char   *var_def_rbl_reply;
   1445 int     var_unv_from_rcode;
   1446 int     var_unv_rcpt_rcode;
   1447 int     var_unv_from_dcode;
   1448 int     var_unv_rcpt_dcode;
   1449 char   *var_unv_from_why;
   1450 char   *var_unv_rcpt_why;
   1451 int     var_mul_rcpt_code;
   1452 char   *var_relay_rcpt_maps;
   1453 int     var_local_rcpt_code;
   1454 int     var_virt_alias_code;
   1455 int     var_virt_mailbox_code;
   1456 int     var_relay_rcpt_code;
   1457 char   *var_verp_clients;
   1458 bool    var_show_unk_rcpt_table;
   1459 int     var_verify_poll_count;
   1460 int     var_verify_poll_delay;
   1461 char   *var_smtpd_proxy_filt;
   1462 int     var_smtpd_proxy_tmout;
   1463 char   *var_smtpd_proxy_ehlo;
   1464 char   *var_smtpd_proxy_opts;
   1465 char   *var_input_transp;
   1466 int     var_smtpd_policy_tmout;
   1467 int     var_smtpd_policy_req_limit;
   1468 int     var_smtpd_policy_try_limit;
   1469 int     var_smtpd_policy_try_delay;
   1470 char   *var_smtpd_policy_def_action;
   1471 char   *var_smtpd_policy_context;
   1472 int     var_smtpd_policy_idle;
   1473 int     var_smtpd_policy_ttl;
   1474 char   *var_xclient_hosts;
   1475 char   *var_xforward_hosts;
   1476 bool    var_smtpd_rej_unl_from;
   1477 bool    var_smtpd_rej_unl_rcpt;
   1478 char   *var_smtpd_forbid_cmds;
   1479 int     var_smtpd_crate_limit;
   1480 int     var_smtpd_cconn_limit;
   1481 int     var_smtpd_cmail_limit;
   1482 int     var_smtpd_crcpt_limit;
   1483 int     var_smtpd_cntls_limit;
   1484 int     var_smtpd_cauth_limit;
   1485 int     var_smtpd_cipv4_prefix;
   1486 int     var_smtpd_cipv6_prefix;
   1487 char   *var_smtpd_hoggers;
   1488 char   *var_local_rwr_clients;
   1489 char   *var_smtpd_ehlo_dis_words;
   1490 char   *var_smtpd_ehlo_dis_maps;
   1491 
   1492 char   *var_smtpd_tls_level;
   1493 bool    var_smtpd_use_tls;
   1494 bool    var_smtpd_enforce_tls;
   1495 bool    var_smtpd_tls_wrappermode;
   1496 bool    var_smtpd_tls_auth_only;
   1497 char   *var_smtpd_cmd_filter;
   1498 char   *var_smtpd_rej_footer;
   1499 char   *var_smtpd_rej_ftr_maps;
   1500 char   *var_smtpd_reject_filter_maps;
   1501 char   *var_smtpd_acl_perm_log;
   1502 char   *var_smtpd_dns_re_filter;
   1503 
   1504 #ifdef USE_TLS
   1505 char   *var_smtpd_relay_ccerts;
   1506 char   *var_smtpd_sasl_tls_opts;
   1507 int     var_smtpd_starttls_tmout;
   1508 char   *var_smtpd_tls_CAfile;
   1509 char   *var_smtpd_tls_CApath;
   1510 bool    var_smtpd_tls_ask_ccert;
   1511 int     var_smtpd_tls_ccert_vd;
   1512 char   *var_smtpd_tls_cert_file;
   1513 char   *var_smtpd_tls_mand_ciph;
   1514 char   *var_smtpd_tls_excl_ciph;
   1515 char   *var_smtpd_tls_mand_excl;
   1516 char   *var_smtpd_tls_dcert_file;
   1517 char   *var_smtpd_tls_dh1024_param_file;
   1518 char   *var_smtpd_tls_dh512_param_file;
   1519 char   *var_smtpd_tls_dkey_file;
   1520 char   *var_smtpd_tls_key_file;
   1521 char   *var_smtpd_tls_loglevel;
   1522 char   *var_smtpd_tls_mand_proto;
   1523 bool    var_smtpd_tls_received_header;
   1524 bool    var_smtpd_tls_req_ccert;
   1525 bool    var_smtpd_tls_set_sessid;
   1526 char   *var_smtpd_tls_fpt_dgst;
   1527 char   *var_smtpd_tls_ciph;
   1528 char   *var_smtpd_tls_proto;
   1529 char   *var_smtpd_tls_eecdh;
   1530 char   *var_smtpd_tls_eccert_file;
   1531 char   *var_smtpd_tls_eckey_file;
   1532 char   *var_smtpd_tls_chain_files;
   1533 bool    var_smtpd_tls_enable_rpk;
   1534 
   1535 #endif
   1536 
   1537 bool    var_smtpd_peername_lookup;
   1538 int     var_plaintext_code;
   1539 bool    var_smtpd_delay_open;
   1540 char   *var_smtpd_milters;
   1541 char   *var_smtpd_milter_maps;
   1542 int     var_milt_conn_time;
   1543 int     var_milt_cmd_time;
   1544 int     var_milt_msg_time;
   1545 char   *var_milt_protocol;
   1546 char   *var_milt_def_action;
   1547 char   *var_milt_daemon_name;
   1548 char   *var_milt_v;
   1549 char   *var_milt_conn_macros;
   1550 char   *var_milt_helo_macros;
   1551 char   *var_milt_mail_macros;
   1552 char   *var_milt_rcpt_macros;
   1553 char   *var_milt_data_macros;
   1554 char   *var_milt_eoh_macros;
   1555 char   *var_milt_eod_macros;
   1556 char   *var_milt_unk_macros;
   1557 char   *var_milt_macro_deflts;
   1558 bool    var_smtpd_client_port_log;
   1559 bool    var_smtpd_forbid_unauth_pipe;
   1560 char   *var_stress;
   1561 
   1562 char   *var_reject_tmpf_act;
   1563 char   *var_unk_name_tf_act;
   1564 char   *var_unk_addr_tf_act;
   1565 char   *var_unv_rcpt_tf_act;
   1566 char   *var_unv_from_tf_act;
   1567 
   1568 int     smtpd_proxy_opts;
   1569 
   1570 #ifdef USE_TLSPROXY
   1571 char   *var_tlsproxy_service;
   1572 
   1573 #endif
   1574 
   1575 char   *var_smtpd_uproxy_proto;
   1576 int     var_smtpd_uproxy_tmout;
   1577 bool    var_relay_before_rcpt_checks;
   1578 bool    var_smtpd_req_deadline;
   1579 int     var_smtpd_min_data_rate;
   1580 char   *var_hfrom_format;
   1581 char   *var_smtpd_forbid_bare_lf;
   1582 char   *var_smtpd_forbid_bare_lf_excl;
   1583 int     var_smtpd_forbid_bare_lf_code;
   1584 static int bare_lf_mask;
   1585 static NAMADR_LIST *bare_lf_excl;
   1586 bool    var_smtpd_hide_client_session;
   1587 bool    var_reqtls_esmtp_hdr;
   1588 
   1589  /*
   1590   * Silly little macros.
   1591   */
   1592 #define STR(x)	vstring_str(x)
   1593 #define LEN(x)	VSTRING_LEN(x)
   1594 
   1595  /*
   1596   * EHLO keyword filter
   1597   */
   1598 static MAPS *ehlo_discard_maps;
   1599 
   1600  /*
   1601   * Per-client Milter support.
   1602   */
   1603 static MAPS *smtpd_milter_maps;
   1604 static void setup_milters(SMTPD_STATE *);
   1605 static void teardown_milters(SMTPD_STATE *);
   1606 
   1607  /*
   1608   * VERP command name.
   1609   */
   1610 #define VERP_CMD	"XVERP"
   1611 #define VERP_CMD_LEN	5
   1612 
   1613 static NAMADR_LIST *verp_clients;
   1614 
   1615  /*
   1616   * XCLIENT command. Access control is cached, so that XCLIENT can't override
   1617   * its own access control.
   1618   */
   1619 static NAMADR_LIST *xclient_hosts;
   1620 static int xclient_allowed;		/* XXX should be SMTPD_STATE member */
   1621 
   1622  /*
   1623   * XFORWARD command. Access control is cached.
   1624   */
   1625 static NAMADR_LIST *xforward_hosts;
   1626 static int xforward_allowed;		/* XXX should be SMTPD_STATE member */
   1627 
   1628  /*
   1629   * Client connection and rate limiting.
   1630   */
   1631 ANVIL_CLNT *anvil_clnt;
   1632 static NAMADR_LIST *hogger_list;
   1633 
   1634  /*
   1635   * Other application-specific globals.
   1636   */
   1637 int     smtpd_input_transp_mask;
   1638 
   1639  /*
   1640   * Forward declarations.
   1641   */
   1642 static void helo_reset(SMTPD_STATE *);
   1643 static void mail_reset(SMTPD_STATE *);
   1644 static void rcpt_reset(SMTPD_STATE *);
   1645 static void chat_reset(SMTPD_STATE *, int);
   1646 
   1647 #ifdef USE_TLS
   1648 static void tls_reset(SMTPD_STATE *);
   1649 
   1650 #endif
   1651 
   1652  /*
   1653   * This filter is applied after printable().
   1654   */
   1655 #define NEUTER_CHARACTERS " <>()\\\";@"
   1656 
   1657  /*
   1658   * Reasons for losing the client.
   1659   */
   1660 #define REASON_TIMEOUT		"timeout"
   1661 #define REASON_LOST_CONNECTION	"lost connection"
   1662 #define REASON_ERROR_LIMIT	"too many errors"
   1663 
   1664 #ifdef USE_TLS
   1665 
   1666  /*
   1667   * TLS initialization status.
   1668   */
   1669 #ifndef USE_TLSPROXY
   1670 static TLS_APPL_STATE *smtpd_tls_ctx;
   1671 static int ask_client_cert;
   1672 
   1673 #endif					/* USE_TLSPROXY */
   1674 #endif
   1675 
   1676  /*
   1677   * SMTP command mapping for broken clients.
   1678   */
   1679 static DICT *smtpd_cmd_filter;
   1680 
   1681  /*
   1682   * Parsed header_from_format setting.
   1683   */
   1684 int     smtpd_hfrom_format;
   1685 
   1686  /*
   1687   * Bare LF and End-of-DATA controls (bare CR is handled elsewhere).
   1688   *
   1689   * At the smtp_get*() line reader level, setting any of these flags in the
   1690   * smtp_detect_bare_lf variable enables the detection of bare newlines. The
   1691   * line reader will set the same flags in the smtp_got_bare_lf variable
   1692   * after it detects a bare newline, otherwise it clears smtp_got_bare_lf.
   1693   *
   1694   * At the SMTP command level, the flags in smtp_got_bare_lf control whether
   1695   * commands ending in a bare newline are rejected.
   1696   *
   1697   * At the DATA and BDAT content level, the flags in smtp_got_bare_lf control
   1698   * whether the standard End-of-DATA sequence CRLF.CRLF is required, and
   1699   * whether lines ending in bare newlines are rejected.
   1700   *
   1701   * Postfix implements "delayed reject" after detecting a bare newline in BDAT
   1702   * or DATA content. The SMTP server delays a REJECT response until the
   1703   * command is finished, instead of replying and hanging up immediately. The
   1704   * End-of-DATA detection is secured with BARE_LF_FLAG_WANT_STD_EOD.
   1705   */
   1706 #define BARE_LF_FLAG_WANT_STD_EOD	(1<<0)	/* Require CRLF.CRLF */
   1707 #define BARE_LF_FLAG_REPLY_REJECT	(1<<1)	/* Reject bare newline */
   1708 #define BARE_LF_FLAG_NOTE_LOG		(1<<2)	/* Note bare newline */
   1709 
   1710 #define IS_BARE_LF_WANT_STD_EOD(m)	((m) & BARE_LF_FLAG_WANT_STD_EOD)
   1711 #define IS_BARE_LF_REPLY_REJECT(m)	((m) & BARE_LF_FLAG_REPLY_REJECT)
   1712 #define IS_BARE_LF_NOTE_LOG(m)		((m) & BARE_LF_FLAG_NOTE_LOG)
   1713 
   1714 static const NAME_CODE bare_lf_mask_table[] = {
   1715     "normalize", BARE_LF_FLAG_WANT_STD_EOD,	/* Default */
   1716     "yes", BARE_LF_FLAG_WANT_STD_EOD,	/* Migration aid */
   1717     "note", BARE_LF_FLAG_WANT_STD_EOD | BARE_LF_FLAG_NOTE_LOG,
   1718     "reject", BARE_LF_FLAG_WANT_STD_EOD | BARE_LF_FLAG_REPLY_REJECT,
   1719     "no", 0,
   1720     0, -1,				/* error */
   1721 };
   1722 
   1723 #ifdef USE_SASL_AUTH
   1724 
   1725  /*
   1726   * SASL exceptions.
   1727   */
   1728 static NAMADR_LIST *sasl_exceptions_networks;
   1729 
   1730 /* sasl_client_exception - can we offer AUTH for this client */
   1731 
   1732 static int sasl_client_exception(SMTPD_STATE *state)
   1733 {
   1734     int     match;
   1735 
   1736     /*
   1737      * This is to work around a Netscape mail client bug where it tries to
   1738      * use AUTH if available, even if user has not configured it. Returns
   1739      * TRUE if AUTH should be offered in the EHLO.
   1740      */
   1741     if (sasl_exceptions_networks == 0)
   1742 	return (0);
   1743 
   1744     if ((match = namadr_list_match(sasl_exceptions_networks,
   1745 				   state->name, state->addr)) == 0)
   1746 	match = sasl_exceptions_networks->error;
   1747 
   1748     if (msg_verbose)
   1749 	msg_info("sasl_exceptions: %s, match=%d",
   1750 		 state->namaddr, match);
   1751 
   1752     return (match);
   1753 }
   1754 
   1755 #endif
   1756 
   1757 /* smtpd_whatsup - gather available evidence for logging */
   1758 
   1759 static const char *smtpd_whatsup(SMTPD_STATE *state)
   1760 {
   1761     static VSTRING *buf = 0;
   1762 
   1763     if (buf == 0)
   1764 	buf = vstring_alloc(100);
   1765     else
   1766 	VSTRING_RESET(buf);
   1767     if (state->sender)
   1768 	vstring_sprintf_append(buf, " from=<%s>",
   1769 			       info_log_addr_form_sender(state->sender));
   1770     if (state->recipient)
   1771 	vstring_sprintf_append(buf, " to=<%s>",
   1772 			    info_log_addr_form_recipient(state->recipient));
   1773     if (state->protocol)
   1774 	vstring_sprintf_append(buf, " proto=%s", state->protocol);
   1775     if (state->helo_name)
   1776 	vstring_sprintf_append(buf, " helo=<%s>", state->helo_name);
   1777 #ifdef USE_SASL_AUTH
   1778     if (state->sasl_username)
   1779 	vstring_sprintf_append(buf, " sasl_username=<%s>",
   1780 			       state->sasl_username);
   1781 #endif
   1782     return (STR(buf));
   1783 }
   1784 
   1785 /* collapse_args - put arguments together again */
   1786 
   1787 static void collapse_args(int argc, SMTPD_TOKEN *argv)
   1788 {
   1789     int     i;
   1790 
   1791     for (i = 1; i < argc; i++) {
   1792 	vstring_strcat(argv[0].vstrval, " ");
   1793 	vstring_strcat(argv[0].vstrval, argv[i].strval);
   1794     }
   1795     argv[0].strval = STR(argv[0].vstrval);
   1796 }
   1797 
   1798 /* check_milter_reply - process reply from Milter */
   1799 
   1800 static const char *check_milter_reply(SMTPD_STATE *state, const char *reply)
   1801 {
   1802     const char *queue_id = state->queue_id ? state->queue_id : "NOQUEUE";
   1803     const char *action;
   1804     const char *text;
   1805 
   1806     /*
   1807      * The syntax of user-specified SMTP replies is checked by the Milter
   1808      * module, because the replies are also used in the cleanup server.
   1809      * Automatically disconnect after 421 (shutdown) reply. The Sendmail 8
   1810      * Milter quarantine action is not final, so it is not included in
   1811      * MILTER_SKIP_FLAGS.
   1812      */
   1813 #define MILTER_SKIP_FLAGS (CLEANUP_FLAG_DISCARD)
   1814 
   1815     switch (reply[0]) {
   1816     case 'H':
   1817 	if (state->saved_flags & CLEANUP_FLAG_HOLD)
   1818 	    return (0);
   1819 	state->saved_flags |= CLEANUP_FLAG_HOLD;
   1820 	action = "milter-hold";
   1821 	text = reply[1] ? reply + 1 : "milter triggers HOLD action";
   1822 	reply = 0;
   1823 	break;
   1824     case 'D':
   1825 	if (state->saved_flags & CLEANUP_FLAG_DISCARD)
   1826 	    return (0);
   1827 	state->saved_flags |= CLEANUP_FLAG_DISCARD;
   1828 	action = "milter-discard";
   1829 	reply = 0;
   1830 	text = "milter triggers DISCARD action";
   1831 	break;
   1832     case 'S':
   1833 	state->error_mask |= MAIL_ERROR_POLICY;
   1834 	action = "milter-reject";
   1835 	reply = "421 4.7.0 Server closing connection";
   1836 	text = 0;
   1837 	break;
   1838     case '4':
   1839     case '5':
   1840 	state->error_mask |= MAIL_ERROR_POLICY;
   1841 	action = "milter-reject";
   1842 	text = 0;
   1843 	break;
   1844     default:
   1845 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   1846 	action = "reject";
   1847 	reply = "421 4.3.5 Server configuration error";
   1848 	text = 0;
   1849 	break;
   1850     }
   1851     msg_info("%s: %s: %s from %s: %s;%s", queue_id, action, state->where,
   1852 	     state->namaddr, reply ? reply : text, smtpd_whatsup(state));
   1853     return (reply);
   1854 }
   1855 
   1856 /* helo_cmd - process HELO command */
   1857 
   1858 static int helo_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   1859 {
   1860     const char *err;
   1861 
   1862     /*
   1863      * RFC 2034: the text part of all 2xx, 4xx, and 5xx SMTP responses other
   1864      * than the initial greeting and any response to HELO or EHLO are
   1865      * prefaced with a status code as defined in RFC 3463.
   1866      */
   1867     if (argc < 2) {
   1868 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   1869 	smtpd_chat_reply(state, "501 Syntax: HELO hostname");
   1870 	return (-1);
   1871     }
   1872     if (argc > 2)
   1873 	collapse_args(argc - 1, argv + 1);
   1874     if (SMTPD_STAND_ALONE(state) == 0
   1875 	&& var_smtpd_delay_reject == 0
   1876 	&& (err = smtpd_check_helo(state, argv[1].strval)) != 0) {
   1877 	smtpd_chat_reply(state, "%s", err);
   1878 	return (-1);
   1879     }
   1880 
   1881     /*
   1882      * XXX Sendmail compatibility: if a Milter rejects CONNECT, EHLO, or
   1883      * HELO, reply with 250 except in case of 421 (disconnect). The reply
   1884      * persists so it will apply to MAIL FROM and to other commands such as
   1885      * AUTH, STARTTLS, and VRFY.
   1886      */
   1887 #define PUSH_STRING(old, curr, new)	{ char *old = (curr); (curr) = (new);
   1888 #define POP_STRING(old, curr)		(curr) = old; }
   1889 
   1890     if (state->milters != 0
   1891 	&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0
   1892      && (err = milter_helo_event(state->milters, argv[1].strval, 0)) != 0) {
   1893 	/* Log reject etc. with correct HELO information. */
   1894 	PUSH_STRING(saved_helo, state->helo_name, argv[1].strval);
   1895 	err = check_milter_reply(state, err);
   1896 	POP_STRING(saved_helo, state->helo_name);
   1897 	if (err != 0 && strncmp(err, "421", 3) == 0) {
   1898 	    smtpd_chat_reply(state, "%s", err);
   1899 	    return (-1);
   1900 	}
   1901     }
   1902     if (state->helo_name != 0)
   1903 	helo_reset(state);
   1904     chat_reset(state, var_smtpd_hist_thrsh);
   1905     mail_reset(state);
   1906     rcpt_reset(state);
   1907     state->helo_name = mystrdup(printable(argv[1].strval, '?'));
   1908     neuter(state->helo_name, NEUTER_CHARACTERS, '?');
   1909     /* Downgrading the protocol name breaks the unauthorized pipelining test. */
   1910     if (strcasecmp(state->protocol, MAIL_PROTO_ESMTP) != 0
   1911 	&& strcasecmp(state->protocol, MAIL_PROTO_SMTP) != 0) {
   1912 	myfree(state->protocol);
   1913 	state->protocol = mystrdup(MAIL_PROTO_SMTP);
   1914     }
   1915     smtpd_chat_reply(state, "250 %s", var_myhostname);
   1916     return (0);
   1917 }
   1918 
   1919 /* cant_announce_feature - explain and terminate this session */
   1920 
   1921 static NORETURN cant_announce_feature(SMTPD_STATE *state, const char *feature)
   1922 {
   1923     msg_warn("don't know if EHLO feature %s should be announced to %s",
   1924 	     feature, state->namaddr);
   1925     vstream_longjmp(state->client, SMTP_ERR_DATA);
   1926 }
   1927 
   1928 /* cant_permit_command - explain and terminate this session */
   1929 
   1930 static NORETURN cant_permit_command(SMTPD_STATE *state, const char *command)
   1931 {
   1932     msg_warn("don't know if command %s should be allowed from %s",
   1933 	     command, state->namaddr);
   1934     vstream_longjmp(state->client, SMTP_ERR_DATA);
   1935 }
   1936 
   1937 /* ehlo_cmd - process EHLO command */
   1938 
   1939 static int ehlo_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   1940 {
   1941     const char *err;
   1942     int     discard_mask;
   1943     char  **cpp;
   1944 
   1945     /*
   1946      * XXX 2821 new feature: Section 4.1.4 specifies that a server must clear
   1947      * all buffers and reset the state exactly as if a RSET command had been
   1948      * issued.
   1949      *
   1950      * RFC 2034: the text part of all 2xx, 4xx, and 5xx SMTP responses other
   1951      * than the initial greeting and any response to HELO or EHLO are
   1952      * prefaced with a status code as defined in RFC 3463.
   1953      */
   1954     if (argc < 2) {
   1955 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   1956 	smtpd_chat_reply(state, "501 Syntax: EHLO hostname");
   1957 	return (-1);
   1958     }
   1959     if (argc > 2)
   1960 	collapse_args(argc - 1, argv + 1);
   1961     if (SMTPD_STAND_ALONE(state) == 0
   1962 	&& var_smtpd_delay_reject == 0
   1963 	&& (err = smtpd_check_helo(state, argv[1].strval)) != 0) {
   1964 	smtpd_chat_reply(state, "%s", err);
   1965 	return (-1);
   1966     }
   1967 
   1968     /*
   1969      * XXX Sendmail compatibility: if a Milter 5xx rejects CONNECT, EHLO, or
   1970      * HELO, reply with ENHANCEDSTATUSCODES except in case of immediate
   1971      * disconnect. The reply persists so it will apply to MAIL FROM and to
   1972      * other commands such as AUTH, STARTTLS, and VRFY.
   1973      */
   1974     err = 0;
   1975     if (state->milters != 0
   1976 	&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0
   1977      && (err = milter_helo_event(state->milters, argv[1].strval, 1)) != 0) {
   1978 	/* Log reject etc. with correct HELO information. */
   1979 	PUSH_STRING(saved_helo, state->helo_name, argv[1].strval);
   1980 	err = check_milter_reply(state, err);
   1981 	POP_STRING(saved_helo, state->helo_name);
   1982 	if (err != 0 && strncmp(err, "421", 3) == 0) {
   1983 	    smtpd_chat_reply(state, "%s", err);
   1984 	    return (-1);
   1985 	}
   1986     }
   1987     if (state->helo_name != 0)
   1988 	helo_reset(state);
   1989     chat_reset(state, var_smtpd_hist_thrsh);
   1990     mail_reset(state);
   1991     rcpt_reset(state);
   1992     state->helo_name = mystrdup(printable(argv[1].strval, '?'));
   1993     neuter(state->helo_name, NEUTER_CHARACTERS, '?');
   1994 
   1995     /*
   1996      * XXX reject_unauth_pipelining depends on the following. If the user
   1997      * sends EHLO then we announce PIPELINING and we can't accuse them of
   1998      * using pipelining in places where it is allowed.
   1999      *
   2000      * XXX The reject_unauth_pipelining test needs to change and also account
   2001      * for mechanisms that disable PIPELINING selectively.
   2002      */
   2003     if (strcasecmp(state->protocol, MAIL_PROTO_ESMTP) != 0) {
   2004 	myfree(state->protocol);
   2005 	state->protocol = mystrdup(MAIL_PROTO_ESMTP);
   2006     }
   2007 
   2008     /*
   2009      * Build the EHLO response, producing no output until we know what to
   2010      * send - this simplifies exception handling. The CRLF record boundaries
   2011      * don't exist at this level in the code, so we represent multi-line
   2012      * output as an array of single-line responses.
   2013      */
   2014 #define EHLO_APPEND(state, cmd) \
   2015     do { \
   2016 	vstring_sprintf((state)->ehlo_buf, (cmd)); \
   2017 	argv_add((state)->ehlo_argv, STR((state)->ehlo_buf), (char *) 0); \
   2018     } while (0)
   2019 
   2020 #define EHLO_APPEND1(state, cmd, arg) \
   2021     do { \
   2022 	vstring_sprintf((state)->ehlo_buf, (cmd), (arg)); \
   2023 	argv_add((state)->ehlo_argv, STR((state)->ehlo_buf), (char *) 0); \
   2024     } while (0)
   2025 
   2026     /*
   2027      * XXX Sendmail compatibility: if a Milter 5XX rejects CONNECT, EHLO, or
   2028      * HELO, reply with ENHANCEDSTATUSCODES only. The reply persists so it
   2029      * will apply to MAIL FROM, but we currently don't have a proper
   2030      * mechanism to apply Milter rejects to AUTH, STARTTLS, VRFY, and other
   2031      * commands while still allowing HELO/EHLO.
   2032      */
   2033     discard_mask = state->ehlo_discard_mask;
   2034     if (err != 0 && err[0] == '5')
   2035 	discard_mask |= ~EHLO_MASK_ENHANCEDSTATUSCODES;
   2036     if ((discard_mask & EHLO_MASK_ENHANCEDSTATUSCODES) == 0)
   2037 	if (discard_mask && !(discard_mask & EHLO_MASK_SILENT))
   2038 	    msg_info("discarding EHLO keywords: %s", str_ehlo_mask(discard_mask));
   2039     if (ehlo_discard_maps && ehlo_discard_maps->error) {
   2040 	msg_warn("don't know what EHLO features to announce to %s",
   2041 		 state->namaddr);
   2042 	vstream_longjmp(state->client, SMTP_ERR_DATA);
   2043     }
   2044 
   2045     /*
   2046      * These may still exist after a prior exception.
   2047      */
   2048     if (state->ehlo_argv == 0) {
   2049 	state->ehlo_argv = argv_alloc(10);
   2050 	state->ehlo_buf = vstring_alloc(10);
   2051     } else
   2052 	argv_truncate(state->ehlo_argv, 0);
   2053 
   2054     EHLO_APPEND1(state, "%s", var_myhostname);
   2055     if ((discard_mask & EHLO_MASK_PIPELINING) == 0)
   2056 	EHLO_APPEND(state, "PIPELINING");
   2057     if ((discard_mask & EHLO_MASK_SIZE) == 0) {
   2058 	if (ENFORCING_SIZE_LIMIT(var_message_limit))
   2059 	    EHLO_APPEND1(state, "SIZE %lu",
   2060 			 (unsigned long) var_message_limit);	/* XXX */
   2061 	else
   2062 	    EHLO_APPEND(state, "SIZE");
   2063     }
   2064     if ((discard_mask & EHLO_MASK_VRFY) == 0)
   2065 	if (var_disable_vrfy_cmd == 0)
   2066 	    EHLO_APPEND(state, SMTPD_CMD_VRFY);
   2067     if ((discard_mask & EHLO_MASK_ETRN) == 0)
   2068 	EHLO_APPEND(state, SMTPD_CMD_ETRN);
   2069 #ifdef USE_TLS
   2070     if ((discard_mask & EHLO_MASK_STARTTLS) == 0)
   2071 	if (var_smtpd_use_tls && (!state->tls_context))
   2072 	    EHLO_APPEND(state, SMTPD_CMD_STARTTLS);
   2073 #endif
   2074 #ifdef USE_SASL_AUTH
   2075 #ifndef AUTH_CMD
   2076 #define AUTH_CMD	"AUTH"
   2077 #endif
   2078     if ((discard_mask & EHLO_MASK_AUTH) == 0) {
   2079 	if (smtpd_sasl_is_active(state) && !sasl_client_exception(state)) {
   2080 	    EHLO_APPEND1(state, "AUTH %s", state->sasl_mechanism_list);
   2081 	    if (var_broken_auth_clients)
   2082 		EHLO_APPEND1(state, "AUTH=%s", state->sasl_mechanism_list);
   2083 	} else if (sasl_exceptions_networks && sasl_exceptions_networks->error)
   2084 	    cant_announce_feature(state, AUTH_CMD);
   2085     }
   2086 #define XCLIENT_LOGIN_KLUDGE	" " XCLIENT_LOGIN
   2087 #else
   2088 #define XCLIENT_LOGIN_KLUDGE	""
   2089 #endif
   2090     if ((discard_mask & EHLO_MASK_VERP) == 0) {
   2091 	if (namadr_list_match(verp_clients, state->name, state->addr))
   2092 	    EHLO_APPEND(state, VERP_CMD);
   2093 	else if (verp_clients && verp_clients->error)
   2094 	    cant_announce_feature(state, VERP_CMD);
   2095     }
   2096     /* XCLIENT must not override its own access control. */
   2097     if ((discard_mask & EHLO_MASK_XCLIENT) == 0) {
   2098 	if (xclient_allowed)
   2099 	    EHLO_APPEND(state, XCLIENT_CMD
   2100 			" " XCLIENT_NAME " " XCLIENT_ADDR
   2101 			" " XCLIENT_PROTO " " XCLIENT_HELO
   2102 			" " XCLIENT_REVERSE_NAME " " XCLIENT_PORT
   2103 			XCLIENT_LOGIN_KLUDGE
   2104 			" " XCLIENT_DESTADDR
   2105 			" " XCLIENT_DESTPORT);
   2106 	else if (xclient_hosts && xclient_hosts->error)
   2107 	    cant_announce_feature(state, XCLIENT_CMD);
   2108     }
   2109     if ((discard_mask & EHLO_MASK_XFORWARD) == 0) {
   2110 	if (xforward_allowed)
   2111 	    EHLO_APPEND(state, XFORWARD_CMD
   2112 			" " XFORWARD_NAME " " XFORWARD_ADDR
   2113 			" " XFORWARD_PROTO " " XFORWARD_HELO
   2114 			" " XFORWARD_DOMAIN " " XFORWARD_PORT
   2115 			" " XFORWARD_IDENT);
   2116 	else if (xforward_hosts && xforward_hosts->error)
   2117 	    cant_announce_feature(state, XFORWARD_CMD);
   2118     }
   2119     if ((discard_mask & EHLO_MASK_ENHANCEDSTATUSCODES) == 0)
   2120 	EHLO_APPEND(state, "ENHANCEDSTATUSCODES");
   2121     if ((discard_mask & EHLO_MASK_8BITMIME) == 0)
   2122 	EHLO_APPEND(state, "8BITMIME");
   2123     if ((discard_mask & EHLO_MASK_DSN) == 0)
   2124 	EHLO_APPEND(state, "DSN");
   2125     if (var_smtputf8_enable && (discard_mask & EHLO_MASK_SMTPUTF8) == 0)
   2126 	EHLO_APPEND(state, "SMTPUTF8");
   2127     if ((discard_mask & EHLO_MASK_CHUNKING) == 0)
   2128 	EHLO_APPEND(state, "CHUNKING");
   2129 #ifdef USE_TLS
   2130     if (var_reqtls_enable && (discard_mask & EHLO_MASK_REQTLS) == 0
   2131 	&& state->tls_context != 0)
   2132 	EHLO_APPEND(state, "REQUIRETLS");
   2133 #endif
   2134 
   2135     /*
   2136      * Send the reply.
   2137      */
   2138     for (cpp = state->ehlo_argv->argv; *cpp; cpp++)
   2139 	smtpd_chat_reply(state, "250%c%s", cpp[1] ? '-' : ' ', *cpp);
   2140 
   2141     /*
   2142      * Clean up.
   2143      */
   2144     argv_free(state->ehlo_argv);
   2145     state->ehlo_argv = 0;
   2146     vstring_free(state->ehlo_buf);
   2147     state->ehlo_buf = 0;
   2148 
   2149     return (0);
   2150 }
   2151 
   2152 /* helo_reset - reset HELO/EHLO command stuff */
   2153 
   2154 static void helo_reset(SMTPD_STATE *state)
   2155 {
   2156     if (state->helo_name) {
   2157 	myfree(state->helo_name);
   2158 	state->helo_name = 0;
   2159 	if (state->milters != 0)
   2160 	    milter_abort(state->milters);
   2161     }
   2162     if (state->ehlo_argv) {
   2163 	argv_free(state->ehlo_argv);
   2164 	state->ehlo_argv = 0;
   2165     }
   2166     if (state->ehlo_buf) {
   2167 	vstring_free(state->ehlo_buf);
   2168 	state->ehlo_buf = 0;
   2169     }
   2170 }
   2171 
   2172 #ifdef USE_SASL_AUTH
   2173 
   2174 /* smtpd_sasl_auth_cmd_wrapper - smtpd_sasl_auth_cmd front-end */
   2175 
   2176 static int smtpd_sasl_auth_cmd_wrapper(SMTPD_STATE *state, int argc,
   2177 				               SMTPD_TOKEN *argv)
   2178 {
   2179     int     rate;
   2180 
   2181     if (SMTPD_STAND_ALONE(state) == 0
   2182 	&& !xclient_allowed
   2183 	&& anvil_clnt
   2184 	&& var_smtpd_cauth_limit > 0
   2185 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   2186 	&& anvil_clnt_auth(anvil_clnt, state->service, state->anvil_range,
   2187 			   &rate) == ANVIL_STAT_OK
   2188 	&& rate > var_smtpd_cauth_limit) {
   2189 	state->error_mask |= MAIL_ERROR_POLICY;
   2190 	msg_warn("AUTH command rate limit exceeded: %d from %s for service %s",
   2191 		 rate, state->namaddr, state->service);
   2192 	smtpd_chat_reply(state,
   2193 			 "450 4.7.1 Error: too many AUTH commands from %s",
   2194 			 state->addr);
   2195 	return (-1);
   2196     }
   2197     return (smtpd_sasl_auth_cmd(state, argc, argv));
   2198 }
   2199 
   2200 #endif
   2201 
   2202 /* mail_open_stream - open mail queue file or IPC stream */
   2203 
   2204 static int mail_open_stream(SMTPD_STATE *state)
   2205 {
   2206 
   2207     /*
   2208      * Connect to the before-queue filter when one is configured. The MAIL
   2209      * FROM and RCPT TO commands are forwarded as received (including DSN
   2210      * attributes), with the following exceptions:
   2211      *
   2212      * - No forwarding of the REQUIRETLS VERB in MAIL FROM.
   2213      *
   2214      * - The before-filter smtpd process handles all authentication, encryption,
   2215      * access control and relay control.
   2216      *
   2217      * - The before-filter smtpd process does not forward blocked commands.
   2218      *
   2219      * If the after-filter smtp server does not support some of Postfix's ESMTP
   2220      * features, then they must be turned off in the before-filter smtpd
   2221      * process with the smtpd_discard_ehlo_keywords feature.
   2222      */
   2223     if (state->proxy_mail) {
   2224 	int     message_proxy_opts = smtpd_proxy_opts;
   2225 
   2226 	if ((state->flags & SMTPD_FLAG_REQTLS) && var_reqtls_esmtp_hdr)
   2227 	    message_proxy_opts |= SMTPD_PROXY_FLAG_REQTLS_HDR;
   2228 	if (smtpd_proxy_create(state, message_proxy_opts, var_smtpd_proxy_filt,
   2229 			       var_smtpd_proxy_tmout, var_smtpd_proxy_ehlo,
   2230 			       state->proxy_mail) != 0) {
   2231 	    smtpd_chat_reply(state, "%s", STR(state->proxy->reply));
   2232 	    smtpd_proxy_free(state);
   2233 	    return (-1);
   2234 	}
   2235     }
   2236 
   2237     /*
   2238      * If running from the master or from inetd, connect to the cleanup
   2239      * service.
   2240      *
   2241      * XXX 2821: An SMTP server is not allowed to "clean up" mail except in the
   2242      * case of original submissions.
   2243      *
   2244      * We implement this by distinguishing between mail that we are willing to
   2245      * rewrite (the local rewrite context) and mail from elsewhere.
   2246      */
   2247     else if (SMTPD_STAND_ALONE(state) == 0) {
   2248 	int     cleanup_flags;
   2249 
   2250 	cleanup_flags = input_transp_cleanup(CLEANUP_FLAG_MASK_EXTERNAL,
   2251 					     smtpd_input_transp_mask)
   2252 	    | CLEANUP_FLAG_SMTP_REPLY;
   2253 	if (state->flags & SMTPD_FLAG_SMTPUTF8)
   2254 	    cleanup_flags |= CLEANUP_FLAG_SMTPUTF8;
   2255 	else
   2256 	    cleanup_flags |= smtputf8_autodetect(MAIL_SRC_MASK_SMTPD);
   2257 	if (state->flags & SMTPD_FLAG_REQTLS)
   2258 	    cleanup_flags |= CLEANUP_FLAG_REQTLS;
   2259 	state->dest = mail_stream_service(MAIL_CLASS_PUBLIC,
   2260 					  var_cleanup_service);
   2261 	if (state->dest == 0
   2262 	    || attr_print(state->dest->stream, ATTR_FLAG_NONE,
   2263 			  SEND_ATTR_INT(MAIL_ATTR_FLAGS, cleanup_flags),
   2264 			  ATTR_TYPE_END) != 0)
   2265 	    msg_fatal("unable to connect to the %s %s service",
   2266 		      MAIL_CLASS_PUBLIC, var_cleanup_service);
   2267     }
   2268 
   2269     /*
   2270      * Otherwise, pipe the message through the privileged postdrop helper.
   2271      * XXX Make postdrop a manifest constant.
   2272      */
   2273     else {
   2274 	char   *postdrop_command;
   2275 
   2276 	postdrop_command = concatenate(var_command_dir, "/postdrop",
   2277 			      msg_verbose ? " -v" : (char *) 0, (char *) 0);
   2278 	state->dest = mail_stream_command(postdrop_command);
   2279 	if (state->dest == 0)
   2280 	    msg_fatal("unable to execute %s", postdrop_command);
   2281 	myfree(postdrop_command);
   2282     }
   2283 
   2284     /*
   2285      * Record the time of arrival, the SASL-related stuff if applicable, the
   2286      * sender envelope address, some session information, and some additional
   2287      * attributes.
   2288      *
   2289      * XXX Send Milter information first, because this will hang when cleanup
   2290      * goes into "throw away" mode. Also, cleanup needs to know early on
   2291      * whether or not it has to do its own SMTP event emulation.
   2292      *
   2293      * XXX At this point we send only dummy information to keep the cleanup
   2294      * server from using its non_smtpd_milters settings. We have to send
   2295      * up-to-date Milter information after DATA so that the cleanup server
   2296      * knows the actual Milter state.
   2297      */
   2298     if (state->dest) {
   2299 	state->cleanup = state->dest->stream;
   2300 	state->queue_id = mystrdup(state->dest->id);
   2301 	if (SMTPD_STAND_ALONE(state) == 0) {
   2302 	    if (state->milters != 0
   2303 		&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0)
   2304 		/* Send place-holder smtpd_milters list. */
   2305 		(void) milter_dummy(state->milters, state->cleanup);
   2306 	    rec_fprintf(state->cleanup, REC_TYPE_TIME, REC_TYPE_TIME_FORMAT,
   2307 			REC_TYPE_TIME_ARG(state->arrival_time));
   2308 	    if (*var_filter_xport)
   2309 		rec_fprintf(state->cleanup, REC_TYPE_FILT, "%s", var_filter_xport);
   2310 	    if (FORWARD_IDENT(state))
   2311 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2312 			    MAIL_ATTR_LOG_IDENT, FORWARD_IDENT(state));
   2313 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2314 			MAIL_ATTR_RWR_CONTEXT, FORWARD_DOMAIN(state));
   2315 #ifdef USE_SASL_AUTH
   2316 	    /* Make external authentication painless (e.g., XCLIENT). */
   2317 	    if (state->sasl_method)
   2318 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2319 			    MAIL_ATTR_SASL_METHOD, state->sasl_method);
   2320 	    if (state->sasl_username)
   2321 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2322 			    MAIL_ATTR_SASL_USERNAME, state->sasl_username);
   2323 	    if (state->sasl_sender)
   2324 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2325 			    MAIL_ATTR_SASL_SENDER, state->sasl_sender);
   2326 #endif
   2327 
   2328 	    /*
   2329 	     * Record DSN related information that was received with the MAIL
   2330 	     * FROM command.
   2331 	     *
   2332 	     * RFC 3461 Section 5.2.1. If no ENVID parameter was included in the
   2333 	     * MAIL command when the message was received, the ENVID
   2334 	     * parameter MUST NOT be supplied when the message is relayed.
   2335 	     * Ditto for the RET parameter.
   2336 	     *
   2337 	     * In other words, we can't simply make up our default ENVID or RET
   2338 	     * values. We have to remember whether the client sent any.
   2339 	     *
   2340 	     * We store DSN information as named attribute records so that we
   2341 	     * don't have to pollute the queue file with records that are
   2342 	     * incompatible with past Postfix versions. Preferably, people
   2343 	     * should be able to back out from an upgrade without losing
   2344 	     * mail.
   2345 	     */
   2346 	    if (state->dsn_envid)
   2347 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2348 			    MAIL_ATTR_DSN_ENVID, state->dsn_envid);
   2349 	    if (state->dsn_ret)
   2350 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%d",
   2351 			    MAIL_ATTR_DSN_RET, state->dsn_ret);
   2352 	}
   2353 	rec_fputs(state->cleanup, REC_TYPE_FROM, state->sender);
   2354 	if (state->encoding != 0)
   2355 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2356 			MAIL_ATTR_ENCODING, state->encoding);
   2357 
   2358 	/*
   2359 	 * Store client attributes.
   2360 	 */
   2361 	if (SMTPD_STAND_ALONE(state) == 0) {
   2362 
   2363 	    /*
   2364 	     * Attributes for logging, also used for XFORWARD.
   2365 	     *
   2366 	     * We store all client attributes, including ones with unknown
   2367 	     * values. Otherwise, an unknown client hostname would be treated
   2368 	     * as a non-existent hostname (i.e. local submission).
   2369 	     */
   2370 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2371 			MAIL_ATTR_LOG_CLIENT_NAME, FORWARD_NAME(state));
   2372 	    /* XXX Note: state->rfc_addr, not state->addr. */
   2373 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2374 			MAIL_ATTR_LOG_CLIENT_ADDR, FORWARD_ADDR(state));
   2375 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2376 			MAIL_ATTR_LOG_CLIENT_PORT, FORWARD_PORT(state));
   2377 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2378 			MAIL_ATTR_LOG_ORIGIN, FORWARD_NAMADDR(state));
   2379 	    if (FORWARD_HELO(state))
   2380 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2381 			    MAIL_ATTR_LOG_HELO_NAME, FORWARD_HELO(state));
   2382 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2383 			MAIL_ATTR_LOG_PROTO_NAME, FORWARD_PROTO(state));
   2384 
   2385 	    /*
   2386 	     * Attributes with actual client information. These are used by
   2387 	     * the smtpd Milter client for policy decisions. Mail that is
   2388 	     * requeued with "postsuper -r" is not subject to processing by
   2389 	     * the cleanup Milter client, because a) it has already been
   2390 	     * filtered, and b) we don't have sufficient information to
   2391 	     * reproduce the exact same SMTP events and Sendmail macros that
   2392 	     * the smtpd Milter client received when the message originally
   2393 	     * arrived in Postfix.
   2394 	     */
   2395 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2396 			MAIL_ATTR_ACT_CLIENT_NAME, state->name);
   2397 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2398 		    MAIL_ATTR_ACT_REVERSE_CLIENT_NAME, state->reverse_name);
   2399 	    /* XXX Note: state->addr, not state->rfc_addr. */
   2400 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2401 			MAIL_ATTR_ACT_CLIENT_ADDR, state->addr);
   2402 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2403 			MAIL_ATTR_ACT_CLIENT_PORT, state->port);
   2404 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2405 			MAIL_ATTR_ACT_SERVER_ADDR, state->dest_addr);
   2406 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2407 			MAIL_ATTR_ACT_SERVER_PORT, state->dest_port);
   2408 	    if (state->helo_name)
   2409 		rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2410 			    MAIL_ATTR_ACT_HELO_NAME, state->helo_name);
   2411 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s",
   2412 			MAIL_ATTR_ACT_PROTO_NAME, state->protocol);
   2413 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%u",
   2414 			MAIL_ATTR_ACT_CLIENT_AF, state->addr_family);
   2415 
   2416 	    /*
   2417 	     * Don't send client certificate down the pipeline unless it is
   2418 	     * a) verified or b) just a fingerprint.
   2419 	     */
   2420 	}
   2421 	if (state->verp_delims)
   2422 	    rec_fputs(state->cleanup, REC_TYPE_VERP, state->verp_delims);
   2423     }
   2424 
   2425     /*
   2426      * Log the queue ID with the message origin.
   2427      */
   2428 #define PRINT_OR_NULL(cond, str) \
   2429 	    ((cond) ? (str) : "")
   2430 #define PRINT2_OR_NULL(cond, name, value) \
   2431 	    PRINT_OR_NULL((cond), (name)), PRINT_OR_NULL((cond), (value))
   2432 
   2433     msg_info("%s: client=%s%s%s%s%s%s%s%s%s%s%s",
   2434 	     (state->queue_id ? state->queue_id : "NOQUEUE"),
   2435 	     state->namaddr,
   2436 #ifdef USE_SASL_AUTH
   2437 	     PRINT2_OR_NULL(state->sasl_method,
   2438 			    ", sasl_method=", state->sasl_method),
   2439 	     PRINT2_OR_NULL(state->sasl_username,
   2440 			    ", sasl_username=", state->sasl_username),
   2441     /* This is safe because state->sasl_sender is xtext-encoded. */
   2442 	     PRINT2_OR_NULL(state->sasl_sender,
   2443 			    ", sasl_sender=", state->sasl_sender),
   2444 #else
   2445 	     "", "", "", "", "", "",
   2446 #endif
   2447     /* Insert transaction TLS status here. */
   2448 	     PRINT2_OR_NULL(HAVE_FORWARDED_IDENT(state),
   2449 			    ", orig_queue_id=", FORWARD_IDENT(state)),
   2450 	     PRINT2_OR_NULL(HAVE_FORWARDED_CLIENT_ATTR(state),
   2451 			    ", orig_client=", FORWARD_NAMADDR(state)));
   2452     return (0);
   2453 }
   2454 
   2455 /* extract_addr - extract address from rubble */
   2456 
   2457 static int extract_addr(SMTPD_STATE *state, SMTPD_TOKEN *arg,
   2458 			        int allow_empty_addr, int strict_rfc821,
   2459 			        int smtputf8)
   2460 {
   2461     const char *myname = "extract_addr";
   2462     TOK822 *tree;
   2463     TOK822 *tp;
   2464     TOK822 *addr = 0;
   2465     int     naddr;
   2466     int     non_addr;
   2467     int     err = 0;
   2468     char   *junk = 0;
   2469     char   *text;
   2470     char   *colon;
   2471 
   2472     /*
   2473      * Special case.
   2474      */
   2475 #define PERMIT_EMPTY_ADDR	1
   2476 #define REJECT_EMPTY_ADDR	0
   2477 
   2478     /*
   2479      * Some mailers send RFC822-style address forms (with comments and such)
   2480      * in SMTP envelopes. We cannot blame users for this: the blame is with
   2481      * programmers violating the RFC, and with sendmail for being permissive.
   2482      *
   2483      * XXX The SMTP command tokenizer must leave the address in externalized
   2484      * (quoted) form, so that the address parser can correctly extract the
   2485      * address from surrounding junk.
   2486      *
   2487      * XXX We have only one address parser, written according to the rules of
   2488      * RFC 822. That standard differs subtly from RFC 821.
   2489      */
   2490     if (msg_verbose)
   2491 	msg_info("%s: input: %s", myname, STR(arg->vstrval));
   2492     if (STR(arg->vstrval)[0] == '<'
   2493 	&& STR(arg->vstrval)[LEN(arg->vstrval) - 1] == '>') {
   2494 	junk = text = mystrndup(STR(arg->vstrval) + 1, LEN(arg->vstrval) - 2);
   2495     } else
   2496 	text = STR(arg->vstrval);
   2497 
   2498     /*
   2499      * Truncate deprecated route address form.
   2500      */
   2501     if (*text == '@' && (colon = strchr(text, ':')) != 0)
   2502 	text = colon + 1;
   2503     tree = tok822_parse(text);
   2504 
   2505     if (junk)
   2506 	myfree(junk);
   2507 
   2508     /*
   2509      * Find trouble.
   2510      */
   2511     for (naddr = non_addr = 0, tp = tree; tp != 0; tp = tp->next) {
   2512 	if (tp->type == TOK822_ADDR) {
   2513 	    addr = tp;
   2514 	    naddr += 1;				/* count address forms */
   2515 	} else if (tp->type == '<' || tp->type == '>') {
   2516 	     /* void */ ;			/* ignore brackets */
   2517 	} else {
   2518 	    non_addr += 1;			/* count non-address forms */
   2519 	}
   2520     }
   2521 
   2522     /*
   2523      * Report trouble. XXX Should log a warning only if we are going to
   2524      * sleep+reject so that attackers can't flood our logfiles.
   2525      *
   2526      * XXX Unfortunately, the sleep-before-reject feature had to be abandoned
   2527      * (at least for small error counts) because servers were DOS-ing
   2528      * themselves when flooded by backscatter traffic.
   2529      */
   2530     if (naddr > 1
   2531 	|| (strict_rfc821 && (non_addr || *STR(arg->vstrval) != '<'))) {
   2532 	msg_warn("Illegal address syntax from %s in %s command: %s",
   2533 		 state->namaddr, state->where,
   2534 		 printable(STR(arg->vstrval), '?'));
   2535 	err = 1;
   2536     }
   2537 
   2538     /*
   2539      * Don't overwrite the input with the extracted address. We need the
   2540      * original (external) form in case the client does not send ORCPT
   2541      * information; and error messages are more accurate if we log the
   2542      * unmodified form. We need the internal form for all other purposes.
   2543      */
   2544     if (addr)
   2545 	tok822_internalize(state->addr_buf, addr->head, TOK822_STR_DEFL);
   2546     else
   2547 	vstring_strcpy(state->addr_buf, "");
   2548 
   2549     /*
   2550      * Report trouble. XXX Should log a warning only if we are going to
   2551      * sleep+reject so that attackers can't flood our logfiles. Log the
   2552      * original address.
   2553      */
   2554     if (err == 0)
   2555 	if ((STR(state->addr_buf)[0] == 0 && !allow_empty_addr)
   2556 	    || (strict_rfc821 && STR(state->addr_buf)[0] == '@')
   2557 	    || (SMTPD_STAND_ALONE(state) == 0
   2558 	     && smtpd_check_addr(strcmp(state->where, SMTPD_CMD_MAIL) == 0 ?
   2559 				 state->recipient : state->sender,
   2560 				 STR(state->addr_buf), smtputf8) != 0)) {
   2561 	    msg_warn("Illegal address syntax from %s in %s command: %s",
   2562 		     state->namaddr, state->where,
   2563 		     printable(STR(arg->vstrval), '?'));
   2564 	    err = 1;
   2565 	}
   2566 
   2567     /*
   2568      * Cleanup.
   2569      */
   2570     tok822_free_tree(tree);
   2571     if (msg_verbose)
   2572 	msg_info("%s: in: %s, result: %s",
   2573 		 myname, STR(arg->vstrval), STR(state->addr_buf));
   2574     return (err);
   2575 }
   2576 
   2577 /* milter_argv - impedance adapter */
   2578 
   2579 static const char **milter_argv(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   2580 {
   2581     int     n;
   2582     ssize_t len = argc + 1;
   2583 
   2584     if (state->milter_argc < len) {
   2585 	if (state->milter_argc > 0)
   2586 	    state->milter_argv = (const char **)
   2587 		myrealloc((void *) state->milter_argv,
   2588 			  sizeof(const char *) * len);
   2589 	else
   2590 	    state->milter_argv = (const char **)
   2591 		mymalloc(sizeof(const char *) * len);
   2592 	state->milter_argc = len;
   2593     }
   2594     for (n = 0; n < argc; n++)
   2595 	state->milter_argv[n] = argv[n].strval;
   2596     state->milter_argv[n] = 0;
   2597     return (state->milter_argv);
   2598 }
   2599 
   2600 /* mail_cmd - process MAIL command */
   2601 
   2602 static int mail_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   2603 {
   2604     const char *err;
   2605     int     narg;
   2606     char   *arg;
   2607     char   *verp_delims = 0;
   2608     int     rate;
   2609     int     dsn_envid = 0;
   2610 
   2611     state->flags &= ~SMTPD_FLAGS_PER_MESSAGE;
   2612     state->encoding = 0;
   2613     state->dsn_ret = 0;
   2614 
   2615     /*
   2616      * Sanity checks.
   2617      *
   2618      * XXX 2821 pedantism: Section 4.1.2 says that SMTP servers that receive a
   2619      * command in which invalid character codes have been employed, and for
   2620      * which there are no other reasons for rejection, MUST reject that
   2621      * command with a 501 response. Postfix attempts to be 8-bit clean.
   2622      */
   2623     if (var_helo_required && state->helo_name == 0) {
   2624 	state->error_mask |= MAIL_ERROR_POLICY;
   2625 	smtpd_chat_reply(state, "503 5.5.1 Error: send HELO/EHLO first");
   2626 	return (-1);
   2627     }
   2628     if (SMTPD_IN_MAIL_TRANSACTION(state)) {
   2629 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   2630 	smtpd_chat_reply(state, "503 5.5.1 Error: nested MAIL command");
   2631 	return (-1);
   2632     }
   2633     /* Don't accept MAIL after out-of-order BDAT. */
   2634     if (SMTPD_PROCESSING_BDAT(state)) {
   2635 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   2636 	smtpd_chat_reply(state, "503 5.5.1 Error: MAIL after BDAT");
   2637 	return (-1);
   2638     }
   2639     if (argc < 3
   2640 	|| strcasecmp(argv[1].strval, "from:") != 0) {
   2641 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   2642 	smtpd_chat_reply(state, "501 5.5.4 Syntax: MAIL FROM:<address>");
   2643 	return (-1);
   2644     }
   2645 
   2646     /*
   2647      * XXX The client event count/rate control must be consistent in its use
   2648      * of client address information in connect and disconnect events. For
   2649      * now we exclude xclient authorized hosts from event count/rate control.
   2650      */
   2651     if (SMTPD_STAND_ALONE(state) == 0
   2652 	&& !xclient_allowed
   2653 	&& anvil_clnt
   2654 	&& var_smtpd_cmail_limit > 0
   2655 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   2656 	&& anvil_clnt_mail(anvil_clnt, state->service, state->anvil_range,
   2657 			   &rate) == ANVIL_STAT_OK
   2658 	&& rate > var_smtpd_cmail_limit) {
   2659 	state->error_mask |= MAIL_ERROR_POLICY;
   2660 	smtpd_chat_reply(state, "450 4.7.1 Error: too much mail from %s",
   2661 			 state->addr);
   2662 	msg_warn("Message delivery request rate limit exceeded: %d from %s for service %s",
   2663 		 rate, state->namaddr, state->service);
   2664 	return (-1);
   2665     }
   2666     if (argv[2].tokval == SMTPD_TOK_ERROR) {
   2667 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   2668 	smtpd_chat_reply(state, "501 5.1.7 Bad sender address syntax");
   2669 	return (-1);
   2670     }
   2671 
   2672     /*
   2673      * XXX The sender address comes first, but the optional SMTPUTF8
   2674      * parameter determines what address syntax is permitted. We must process
   2675      * this parameter early.
   2676      */
   2677     if (var_smtputf8_enable
   2678 	&& (state->ehlo_discard_mask & EHLO_MASK_SMTPUTF8) == 0) {
   2679 	for (narg = 3; narg < argc; narg++) {
   2680 	    arg = argv[narg].strval;
   2681 	    if (strcasecmp(arg, "SMTPUTF8") == 0) {	/* RFC 6531 */
   2682 		/* Fix 20161206: allow UTF8 in smtpd_sender_restrictions. */
   2683 		state->flags |= SMTPD_FLAG_SMTPUTF8;
   2684 		break;
   2685 	    }
   2686 	}
   2687     }
   2688     if (extract_addr(state, argv + 2, PERMIT_EMPTY_ADDR,
   2689 		     var_strict_rfc821_env,
   2690 		     state->flags & SMTPD_FLAG_SMTPUTF8) != 0) {
   2691 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   2692 	smtpd_chat_reply(state, "501 5.1.7 Bad sender address syntax");
   2693 	return (-1);
   2694     }
   2695     for (narg = 3; narg < argc; narg++) {
   2696 	arg = argv[narg].strval;
   2697 	if (strcasecmp(arg, "BODY=8BITMIME") == 0) {	/* RFC 1652 */
   2698 	    state->encoding = MAIL_ATTR_ENC_8BIT;
   2699 	} else if (strcasecmp(arg, "BODY=7BIT") == 0) {	/* RFC 1652 */
   2700 	    state->encoding = MAIL_ATTR_ENC_7BIT;
   2701 	} else if (strncasecmp(arg, "SIZE=", 5) == 0) {	/* RFC 1870 */
   2702 	    /* Reject non-numeric size. */
   2703 	    if (!alldig(arg + 5)) {
   2704 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   2705 		smtpd_chat_reply(state, "501 5.5.4 Bad message size syntax");
   2706 		return (-1);
   2707 	    }
   2708 	    /* Reject size overflow. */
   2709 	    if ((state->msg_size = off_cvt_string(arg + 5)) < 0) {
   2710 		state->error_mask |= MAIL_ERROR_POLICY;
   2711 		smtpd_chat_reply(state, "552 5.3.4 Message size exceeds file system imposed limit");
   2712 		return (-1);
   2713 	    }
   2714 	} else if (var_smtputf8_enable
   2715 		   && (state->ehlo_discard_mask & EHLO_MASK_SMTPUTF8) == 0
   2716 		   && strcasecmp(arg, "SMTPUTF8") == 0) {	/* RFC 6531 */
   2717 	     /* Already processed early. */ ;
   2718 #ifdef USE_TLS
   2719 	} else if (var_reqtls_enable
   2720 		   && state->tls_context != 0
   2721 		   && (state->ehlo_discard_mask & EHLO_MASK_REQTLS) == 0
   2722 		   && strcasecmp(arg, "REQUIRETLS") == 0) {	/* RFC 8689 */
   2723 	    state->flags |= SMTPD_FLAG_REQTLS;
   2724 #endif
   2725 #ifdef USE_SASL_AUTH
   2726 	} else if (strncasecmp(arg, "AUTH=", 5) == 0) {
   2727 	    if ((err = smtpd_sasl_mail_opt(state, arg + 5)) != 0) {
   2728 		smtpd_chat_reply(state, "%s", err);
   2729 		return (-1);
   2730 	    }
   2731 #endif
   2732 	} else if (namadr_list_match(verp_clients, state->name, state->addr)
   2733 		   && strncasecmp(arg, VERP_CMD, VERP_CMD_LEN) == 0
   2734 		   && (arg[VERP_CMD_LEN] == '=' || arg[VERP_CMD_LEN] == 0)) {
   2735 	    if (arg[VERP_CMD_LEN] == 0) {
   2736 		verp_delims = var_verp_delims;
   2737 	    } else {
   2738 		verp_delims = arg + VERP_CMD_LEN + 1;
   2739 		if (verp_delims_verify(verp_delims) != 0) {
   2740 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   2741 		    smtpd_chat_reply(state,
   2742 			 "501 5.5.4 Error: %s needs two characters from %s",
   2743 				     VERP_CMD, var_verp_filter);
   2744 		    return (-1);
   2745 		}
   2746 	    }
   2747 	} else if (strncasecmp(arg, "RET=", 4) == 0) {	/* RFC 3461 */
   2748 	    /* Sanitized on input. */
   2749 	    if (state->ehlo_discard_mask & EHLO_MASK_DSN) {
   2750 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   2751 		smtpd_chat_reply(state, "501 5.7.1 DSN support is disabled");
   2752 		return (-1);
   2753 	    }
   2754 	    if (state->dsn_ret
   2755 		|| (state->dsn_ret = dsn_ret_code(arg + 4)) == 0) {
   2756 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   2757 		smtpd_chat_reply(state,
   2758 				 "501 5.5.4 Bad RET parameter syntax");
   2759 		return (-1);
   2760 	    }
   2761 	} else if (strncasecmp(arg, "ENVID=", 6) == 0) {	/* RFC 3461 */
   2762 	    /* Sanitized by bounce server. */
   2763 	    if (state->ehlo_discard_mask & EHLO_MASK_DSN) {
   2764 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   2765 		smtpd_chat_reply(state, "501 5.7.1 DSN support is disabled");
   2766 		return (-1);
   2767 	    }
   2768 	    if (dsn_envid
   2769 		|| xtext_unquote(state->dsn_buf, arg + 6) == 0
   2770 		|| !allprint(STR(state->dsn_buf))) {
   2771 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   2772 		smtpd_chat_reply(state, "501 5.5.4 Bad ENVID parameter syntax");
   2773 		return (-1);
   2774 	    }
   2775 	    dsn_envid = 1;
   2776 	} else {
   2777 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   2778 	    smtpd_chat_reply(state, "555 5.5.4 Unsupported option: %s", arg);
   2779 	    return (-1);
   2780 	}
   2781     }
   2782     /* Fix 20161205: show the envelope sender in reject logging. */
   2783     PUSH_STRING(saved_sender, state->sender, STR(state->addr_buf));
   2784     err = smtpd_check_size(state, state->msg_size);
   2785     POP_STRING(saved_sender, state->sender);
   2786     if (err != 0) {
   2787 	smtpd_chat_reply(state, "%s", err);
   2788 	return (-1);
   2789     }
   2790     if (verp_delims && STR(state->addr_buf)[0] == 0) {
   2791 	smtpd_chat_reply(state, "503 5.5.4 Error: %s requires non-null sender",
   2792 			 VERP_CMD);
   2793 	return (-1);
   2794     }
   2795     if (SMTPD_STAND_ALONE(state) == 0) {
   2796 	const char *verify_sender;
   2797 
   2798 	/*
   2799 	 * XXX Don't reject the address when we're probed with our own
   2800 	 * address verification sender address. Otherwise, some timeout or
   2801 	 * some UCE block may result in mutual negative caching, making it
   2802 	 * painful to get the mail through. Unfortunately we still have to
   2803 	 * send the address to the Milters otherwise they may bail out with a
   2804 	 * "missing recipient" protocol error.
   2805 	 */
   2806 	verify_sender = valid_verify_sender_addr(STR(state->addr_buf));
   2807 	if (verify_sender != 0)
   2808 	    vstring_strcpy(state->addr_buf, verify_sender);
   2809     }
   2810     if (SMTPD_STAND_ALONE(state) == 0
   2811 	&& var_smtpd_delay_reject == 0
   2812 	&& (err = smtpd_check_mail(state, STR(state->addr_buf))) != 0) {
   2813 	/* XXX Reset access map side effects. */
   2814 	mail_reset(state);
   2815 	smtpd_chat_reply(state, "%s", err);
   2816 	return (-1);
   2817     }
   2818     if (state->milters != 0
   2819 	&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0) {
   2820 	state->flags |= SMTPD_FLAG_NEED_MILTER_ABORT;
   2821 	PUSH_STRING(saved_sender, state->sender, STR(state->addr_buf));
   2822 	err = milter_mail_event(state->milters,
   2823 				milter_argv(state, argc - 2, argv + 2));
   2824 	if (err != 0) {
   2825 	    /* Log reject etc. with correct sender information. */
   2826 	    err = check_milter_reply(state, err);
   2827 	}
   2828 	POP_STRING(saved_sender, state->sender);
   2829 	if (err != 0) {
   2830 	    /* XXX Reset access map side effects. */
   2831 	    mail_reset(state);
   2832 	    smtpd_chat_reply(state, "%s", err);
   2833 	    return (-1);
   2834 	}
   2835     }
   2836     if (SMTPD_STAND_ALONE(state) == 0) {
   2837 	err = smtpd_check_rewrite(state);
   2838 	if (err != 0) {
   2839 	    /* XXX Reset access map side effects. */
   2840 	    mail_reset(state);
   2841 	    smtpd_chat_reply(state, "%s", err);
   2842 	    return (-1);
   2843 	}
   2844     }
   2845 
   2846     /*
   2847      * Historically, Postfix does not forbid 8-bit envelope localparts.
   2848      * Changing this would be a compatibility break. That can't happen in the
   2849      * foreseeable future.
   2850      */
   2851     if ((var_strict_smtputf8 || warn_compat_break_smtputf8_enable)
   2852 	&& (state->flags & SMTPD_FLAG_SMTPUTF8) == 0
   2853 	&& *STR(state->addr_buf) && !allascii(STR(state->addr_buf))) {
   2854 	if (var_strict_smtputf8) {
   2855 	    smtpd_chat_reply(state, "553 5.6.7 Must declare SMTPUTF8 to "
   2856 			     "send unicode address");
   2857 	    return (-1);
   2858 	}
   2859 
   2860 	/*
   2861 	 * Not: #ifndef NO_EAI. They must configure SMTPUTF8_ENABLE=no if a
   2862 	 * warning message is logged, so that they don't suddenly start to
   2863 	 * lose mail after Postfix is built with EAI support.
   2864 	 */
   2865 	if (warn_compat_break_smtputf8_enable)
   2866 	    msg_info("using backwards-compatible default setting "
   2867 		     VAR_SMTPUTF8_ENABLE "=no to accept non-ASCII sender "
   2868 		     "address \"%s\" from %s", STR(state->addr_buf),
   2869 		     state->namaddr);
   2870     }
   2871 
   2872     /*
   2873      * Check the queue file space, if applicable. The optional before-filter
   2874      * speed-adjust buffers use disk space. However, we don't know if they
   2875      * compete for storage space with the after-filter queue, so we can't
   2876      * simply bump up the free space requirement to 2.5 * message_size_limit.
   2877      */
   2878     if (!USE_SMTPD_PROXY(state)
   2879 	|| (smtpd_proxy_opts & SMTPD_PROXY_FLAG_SPEED_ADJUST)) {
   2880 	if (SMTPD_STAND_ALONE(state) == 0
   2881 	    && (err = smtpd_check_queue(state)) != 0) {
   2882 	    /* XXX Reset access map side effects. */
   2883 	    mail_reset(state);
   2884 	    smtpd_chat_reply(state, "%s", err);
   2885 	    return (-1);
   2886 	}
   2887     }
   2888 
   2889     /*
   2890      * No more early returns. The mail transaction is in progress.
   2891      */
   2892     GETTIMEOFDAY(&state->arrival_time);
   2893     state->sender = mystrdup(STR(state->addr_buf));
   2894     vstring_sprintf(state->instance, "%x.%lx.%lx.%x",
   2895 		    var_pid, (unsigned long) state->arrival_time.tv_sec,
   2896 	       (unsigned long) state->arrival_time.tv_usec, state->seqno++);
   2897     if (verp_delims)
   2898 	state->verp_delims = mystrdup(verp_delims);
   2899     if (dsn_envid)
   2900 	state->dsn_envid = mystrdup(STR(state->dsn_buf));
   2901     if (USE_SMTPD_PROXY(state)) {
   2902 	if (state->flags & SMTPD_FLAG_REQTLS) {
   2903 	    vstring_sprintf(state->buffer, "%s %s%s", argv[0].strval,
   2904 			    argv[1].strval, argv[2].strval);
   2905 	    for (narg = 3; narg < argc; narg++) {
   2906 		arg = argv[narg].strval;
   2907 		if (strcasecmp(arg, "REQUIRETLS") == 0)
   2908 		    continue;
   2909 		vstring_sprintf_append(state->buffer, " %s", arg);
   2910 	    }
   2911 	}
   2912 	state->proxy_mail = mystrdup(STR(state->buffer));
   2913     }
   2914     if (var_smtpd_delay_open == 0 && mail_open_stream(state) < 0) {
   2915 	/* XXX Reset access map side effects. */
   2916 	mail_reset(state);
   2917 	return (-1);
   2918     }
   2919     smtpd_chat_reply(state, "250 2.1.0 Ok");
   2920     return (0);
   2921 }
   2922 
   2923 /* mail_reset - reset MAIL command stuff */
   2924 
   2925 static void mail_reset(SMTPD_STATE *state)
   2926 {
   2927     state->msg_size = 0;
   2928     state->act_size = 0;
   2929     state->flags &= SMTPD_MASK_MAIL_KEEP;
   2930 
   2931     /*
   2932      * Unceremoniously close the pipe to the cleanup service. The cleanup
   2933      * service will delete the queue file when it detects a premature
   2934      * end-of-file condition on input.
   2935      */
   2936     if (state->cleanup != 0) {
   2937 	mail_stream_cleanup(state->dest);
   2938 	state->dest = 0;
   2939 	state->cleanup = 0;
   2940     }
   2941     state->err = 0;
   2942     if (state->queue_id != 0) {
   2943 	myfree(state->queue_id);
   2944 	state->queue_id = 0;
   2945     }
   2946     if (state->sender) {
   2947 	myfree(state->sender);
   2948 	state->sender = 0;
   2949     }
   2950     /* WeiYu Wu: need to undo milter_mail_event() state change. */
   2951     if (state->flags & SMTPD_FLAG_NEED_MILTER_ABORT) {
   2952 	milter_abort(state->milters);
   2953 	state->flags &= ~SMTPD_FLAG_NEED_MILTER_ABORT;
   2954     }
   2955     if (state->verp_delims) {
   2956 	myfree(state->verp_delims);
   2957 	state->verp_delims = 0;
   2958     }
   2959     if (state->proxy_mail) {
   2960 	myfree(state->proxy_mail);
   2961 	state->proxy_mail = 0;
   2962     }
   2963     if (state->saved_filter) {
   2964 	myfree(state->saved_filter);
   2965 	state->saved_filter = 0;
   2966     }
   2967     if (state->saved_redirect) {
   2968 	myfree(state->saved_redirect);
   2969 	state->saved_redirect = 0;
   2970     }
   2971     if (state->saved_bcc) {
   2972 	argv_free(state->saved_bcc);
   2973 	state->saved_bcc = 0;
   2974     }
   2975     state->saved_flags = 0;
   2976 #ifdef DELAY_ACTION
   2977     state->saved_delay = 0;
   2978 #endif
   2979 #ifdef USE_SASL_AUTH
   2980     if (state->sasl_sender)
   2981 	smtpd_sasl_mail_reset(state);
   2982 #endif
   2983     state->discard = 0;
   2984     VSTRING_RESET(state->instance);
   2985     VSTRING_TERMINATE(state->instance);
   2986 
   2987     if (state->proxy)
   2988 	smtpd_proxy_free(state);
   2989     if (state->xforward.flags)
   2990 	smtpd_xforward_reset(state);
   2991     if (state->prepend)
   2992 	state->prepend = argv_free(state->prepend);
   2993     if (state->dsn_envid) {
   2994 	myfree(state->dsn_envid);
   2995 	state->dsn_envid = 0;
   2996     }
   2997     if (state->milter_argv) {
   2998 	myfree((void *) state->milter_argv);
   2999 	state->milter_argv = 0;
   3000 	state->milter_argc = 0;
   3001     }
   3002 
   3003     /*
   3004      * BDAT.
   3005      */
   3006     state->bdat_state = SMTPD_BDAT_STAT_NONE;
   3007     if (state->bdat_get_stream) {
   3008 	(void) vstream_fclose(state->bdat_get_stream);
   3009 	state->bdat_get_stream = 0;
   3010     }
   3011     if (state->bdat_get_buffer)
   3012 	VSTRING_RESET(state->bdat_get_buffer);
   3013 }
   3014 
   3015 /* rcpt_cmd - process RCPT TO command */
   3016 
   3017 static int rcpt_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   3018 {
   3019     SMTPD_PROXY *proxy;
   3020     const char *err;
   3021     int     narg;
   3022     char   *arg;
   3023     int     rate;
   3024     const char *dsn_orcpt_addr = 0;
   3025     ssize_t dsn_orcpt_addr_len = 0;
   3026     const char *dsn_orcpt_type = 0;
   3027     int     dsn_notify = 0;
   3028     const char *coded_addr;
   3029     const char *milter_err;
   3030 
   3031     /*
   3032      * Sanity checks.
   3033      *
   3034      * XXX 2821 pedantism: Section 4.1.2 says that SMTP servers that receive a
   3035      * command in which invalid character codes have been employed, and for
   3036      * which there are no other reasons for rejection, MUST reject that
   3037      * command with a 501 response. So much for the principle of "be liberal
   3038      * in what you accept, be strict in what you send".
   3039      */
   3040     if (!SMTPD_IN_MAIL_TRANSACTION(state)) {
   3041 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3042 	smtpd_chat_reply(state, "503 5.5.1 Error: need MAIL command");
   3043 	return (-1);
   3044     }
   3045     /* Don't accept RCPT after BDAT. */
   3046     if (SMTPD_PROCESSING_BDAT(state)) {
   3047 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3048 	smtpd_chat_reply(state, "503 5.5.1 Error: RCPT after BDAT");
   3049 	return (-1);
   3050     }
   3051     if (argc < 3
   3052 	|| strcasecmp(argv[1].strval, "to:") != 0) {
   3053 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3054 	smtpd_chat_reply(state, "501 5.5.4 Syntax: RCPT TO:<address>");
   3055 	return (-1);
   3056     }
   3057 
   3058     /*
   3059      * XXX The client event count/rate control must be consistent in its use
   3060      * of client address information in connect and disconnect events. For
   3061      * now we exclude xclient authorized hosts from event count/rate control.
   3062      */
   3063     if (SMTPD_STAND_ALONE(state) == 0
   3064 	&& !xclient_allowed
   3065 	&& anvil_clnt
   3066 	&& var_smtpd_crcpt_limit > 0
   3067 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   3068 	&& anvil_clnt_rcpt(anvil_clnt, state->service, state->anvil_range,
   3069 			   &rate) == ANVIL_STAT_OK
   3070 	&& rate > var_smtpd_crcpt_limit) {
   3071 	state->error_mask |= MAIL_ERROR_POLICY;
   3072 	msg_warn("Recipient address rate limit exceeded: %d from %s for service %s",
   3073 		 rate, state->namaddr, state->service);
   3074 	smtpd_chat_reply(state, "450 4.7.1 Error: too many recipients from %s",
   3075 			 state->addr);
   3076 	return (-1);
   3077     }
   3078     if (argv[2].tokval == SMTPD_TOK_ERROR) {
   3079 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3080 	smtpd_chat_reply(state, "501 5.1.3 Bad recipient address syntax");
   3081 	return (-1);
   3082     }
   3083     if (extract_addr(state, argv + 2, REJECT_EMPTY_ADDR, var_strict_rfc821_env,
   3084 		     state->flags & SMTPD_FLAG_SMTPUTF8) != 0) {
   3085 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3086 	smtpd_chat_reply(state, "501 5.1.3 Bad recipient address syntax");
   3087 	return (-1);
   3088     }
   3089     for (narg = 3; narg < argc; narg++) {
   3090 	arg = argv[narg].strval;
   3091 	if (strncasecmp(arg, "NOTIFY=", 7) == 0) {	/* RFC 3461 */
   3092 	    /* Sanitized on input. */
   3093 	    if (state->ehlo_discard_mask & EHLO_MASK_DSN) {
   3094 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   3095 		smtpd_chat_reply(state, "501 5.7.1 DSN support is disabled");
   3096 		return (-1);
   3097 	    }
   3098 	    if (dsn_notify || (dsn_notify = dsn_notify_mask(arg + 7)) == 0) {
   3099 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   3100 		smtpd_chat_reply(state,
   3101 			    "501 5.5.4 Error: Bad NOTIFY parameter syntax");
   3102 		return (-1);
   3103 	    }
   3104 	} else if (strncasecmp(arg, "ORCPT=", 6) == 0) {	/* RFC 3461 */
   3105 	    /* Sanitized by bounce server. */
   3106 	    if (state->ehlo_discard_mask & EHLO_MASK_DSN) {
   3107 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   3108 		smtpd_chat_reply(state, "501 5.7.1 DSN support is disabled");
   3109 		return (-1);
   3110 	    }
   3111 	    vstring_strcpy(state->dsn_orcpt_buf, arg + 6);
   3112 	    if (dsn_orcpt_addr
   3113 	     || (coded_addr = split_at(STR(state->dsn_orcpt_buf), ';')) == 0
   3114 		|| *(dsn_orcpt_type = STR(state->dsn_orcpt_buf)) == 0
   3115 		|| (strcasecmp(dsn_orcpt_type, "utf-8") == 0 ?
   3116 		    uxtext_unquote(state->dsn_buf, coded_addr) == 0 :
   3117 		    xtext_unquote(state->dsn_buf, coded_addr) == 0)) {
   3118 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   3119 		smtpd_chat_reply(state,
   3120 			     "501 5.5.4 Error: Bad ORCPT parameter syntax");
   3121 		return (-1);
   3122 	    }
   3123 	    dsn_orcpt_addr = STR(state->dsn_buf);
   3124 	    dsn_orcpt_addr_len = LEN(state->dsn_buf);
   3125 	} else {
   3126 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   3127 	    smtpd_chat_reply(state, "555 5.5.4 Unsupported option: %s", arg);
   3128 	    return (-1);
   3129 	}
   3130     }
   3131     if (var_smtpd_rcpt_limit && state->rcpt_count >= var_smtpd_rcpt_limit) {
   3132 	smtpd_chat_reply(state, "452 4.5.3 Error: too many recipients");
   3133 	if (state->rcpt_overshoot++ < var_smtpd_rcpt_overlim)
   3134 	    return (0);
   3135 	state->error_mask |= MAIL_ERROR_POLICY;
   3136 	return (-1);
   3137     }
   3138 
   3139     /*
   3140      * Historically, Postfix does not forbid 8-bit envelope localparts.
   3141      * Changing this would be a compatibility break. That can't happen in the
   3142      * foreseeable future.
   3143      */
   3144     if ((var_strict_smtputf8 || warn_compat_break_smtputf8_enable)
   3145 	&& (state->flags & SMTPD_FLAG_SMTPUTF8) == 0
   3146 	&& *STR(state->addr_buf) && !allascii(STR(state->addr_buf))) {
   3147 	if (var_strict_smtputf8) {
   3148 	    smtpd_chat_reply(state, "553 5.6.7 Must declare SMTPUTF8 to "
   3149 			     "send unicode address");
   3150 	    return (-1);
   3151 	}
   3152 
   3153 	/*
   3154 	 * Not: #ifndef NO_EAI. They must configure SMTPUTF8_ENABLE=no if a
   3155 	 * warning message is logged, so that they don't suddenly start to
   3156 	 * lose mail after Postfix is built with EAI support.
   3157 	 */
   3158 	if (warn_compat_break_smtputf8_enable)
   3159 	    msg_info("using backwards-compatible default setting "
   3160 		     VAR_SMTPUTF8_ENABLE "=no to accept non-ASCII recipient "
   3161 		     "address \"%s\" from %s", STR(state->addr_buf),
   3162 		     state->namaddr);
   3163     }
   3164     if (SMTPD_STAND_ALONE(state) == 0) {
   3165 	const char *verify_sender;
   3166 
   3167 	/*
   3168 	 * XXX Don't reject the address when we're probed with our own
   3169 	 * address verification sender address. Otherwise, some timeout or
   3170 	 * some UCE block may result in mutual negative caching, making it
   3171 	 * painful to get the mail through. Unfortunately we still have to
   3172 	 * send the address to the Milters otherwise they may bail out with a
   3173 	 * "missing recipient" protocol error.
   3174 	 */
   3175 	verify_sender = valid_verify_sender_addr(STR(state->addr_buf));
   3176 	if (verify_sender != 0) {
   3177 	    vstring_strcpy(state->addr_buf, verify_sender);
   3178 	    err = 0;
   3179 	} else {
   3180 	    err = smtpd_check_rcpt(state, STR(state->addr_buf));
   3181 	}
   3182 	if (state->milters != 0
   3183 	    && (state->saved_flags & MILTER_SKIP_FLAGS) == 0) {
   3184 	    PUSH_STRING(saved_rcpt, state->recipient, STR(state->addr_buf));
   3185 	    state->milter_reject_text = err;
   3186 	    milter_err = milter_rcpt_event(state->milters,
   3187 					   err == 0 ? MILTER_FLAG_NONE :
   3188 					   MILTER_FLAG_WANT_RCPT_REJ,
   3189 				    milter_argv(state, argc - 2, argv + 2));
   3190 	    if (err == 0 && milter_err != 0) {
   3191 		/* Log reject etc. with correct recipient information. */
   3192 		err = check_milter_reply(state, milter_err);
   3193 	    }
   3194 	    POP_STRING(saved_rcpt, state->recipient);
   3195 	}
   3196 	if (err != 0) {
   3197 	    smtpd_chat_reply(state, "%s", err);
   3198 	    return (-1);
   3199 	}
   3200     }
   3201 
   3202     /*
   3203      * Don't access the proxy, queue file, or queue file writer process until
   3204      * we have a valid recipient address.
   3205      */
   3206     if (state->proxy == 0 && state->cleanup == 0 && mail_open_stream(state) < 0)
   3207 	return (-1);
   3208 
   3209     /*
   3210      * Proxy the recipient. OK, so we lied. If the real-time proxy rejects
   3211      * the recipient then we can have a proxy connection without having
   3212      * accepted a recipient.
   3213      */
   3214     proxy = state->proxy;
   3215     if (proxy != 0 && proxy->cmd(state, SMTPD_PROX_WANT_OK,
   3216 				 "%s", STR(state->buffer)) != 0) {
   3217 	smtpd_chat_reply(state, "%s", STR(proxy->reply));
   3218 	return (-1);
   3219     }
   3220 
   3221     /*
   3222      * Store the recipient. Remember the first one.
   3223      *
   3224      * Flush recipients to maintain a stiffer coupling with the next stage and
   3225      * to better utilize parallelism.
   3226      *
   3227      * RFC 3461 Section 5.2.1: If the NOTIFY parameter was not supplied for a
   3228      * recipient when the message was received, the NOTIFY parameter MUST NOT
   3229      * be supplied for that recipient when the message is relayed.
   3230      *
   3231      * In other words, we can't simply make up our default NOTIFY value. We have
   3232      * to remember whether the client sent any.
   3233      *
   3234      * RFC 3461 Section 5.2.1: If no ORCPT parameter was present when the
   3235      * message was received, an ORCPT parameter MAY be added to the RCPT
   3236      * command when the message is relayed.  If an ORCPT parameter is added
   3237      * by the relaying MTA, it MUST contain the recipient address from the
   3238      * RCPT command used when the message was received by that MTA.
   3239      *
   3240      * In other words, it is OK to make up our own DSN original recipient when
   3241      * the client didn't send one. Although the RFC mentions mail relaying
   3242      * only, we also make up our own original recipient for the purpose of
   3243      * final delivery. For now, we do this here, rather than on the fly.
   3244      *
   3245      * XXX We use REC_TYPE_ATTR for DSN-related recipient attributes even though
   3246      * 1) REC_TYPE_ATTR is not meant for multiple instances of the same named
   3247      * attribute, and 2) mixing REC_TYPE_ATTR with REC_TYPE_(not attr)
   3248      * requires that we map attributes with rec_attr_map() in order to
   3249      * simplify the recipient record processing loops in the cleanup and qmgr
   3250      * servers.
   3251      *
   3252      * Another possibility, yet to be explored, is to leave the additional
   3253      * recipient information in the queue file and just pass queue file
   3254      * offsets along with the delivery request. This is a trade off between
   3255      * memory allocation versus numeric conversion overhead.
   3256      *
   3257      * Since we have no record grouping mechanism, all recipient-specific
   3258      * parameters must be sent to the cleanup server before the actual
   3259      * recipient address.
   3260      */
   3261     state->rcpt_count++;
   3262     if (state->recipient == 0)
   3263 	state->recipient = mystrdup(STR(state->addr_buf));
   3264     if (state->cleanup) {
   3265 	/* Note: RFC(2)821 externalized address! */
   3266 	if (dsn_orcpt_addr == 0) {
   3267 	    dsn_orcpt_type = "rfc822";
   3268 	    dsn_orcpt_addr = argv[2].strval;
   3269 	    dsn_orcpt_addr_len = strlen(argv[2].strval);
   3270 	    if (dsn_orcpt_addr[0] == '<'
   3271 		&& dsn_orcpt_addr[dsn_orcpt_addr_len - 1] == '>') {
   3272 		dsn_orcpt_addr += 1;
   3273 		dsn_orcpt_addr_len -= 2;
   3274 	    }
   3275 	}
   3276 	if (dsn_notify)
   3277 	    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%d",
   3278 			MAIL_ATTR_DSN_NOTIFY, dsn_notify);
   3279 	rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%s;%.*s",
   3280 		    MAIL_ATTR_DSN_ORCPT, dsn_orcpt_type,
   3281 		    (int) dsn_orcpt_addr_len, dsn_orcpt_addr);
   3282 	rec_fputs(state->cleanup, REC_TYPE_RCPT, STR(state->addr_buf));
   3283 	vstream_fflush(state->cleanup);
   3284     }
   3285     smtpd_chat_reply(state, "250 2.1.5 Ok");
   3286     return (0);
   3287 }
   3288 
   3289 /* rcpt_reset - reset RCPT stuff */
   3290 
   3291 static void rcpt_reset(SMTPD_STATE *state)
   3292 {
   3293     if (state->recipient) {
   3294 	myfree(state->recipient);
   3295 	state->recipient = 0;
   3296     }
   3297     state->rcpt_count = 0;
   3298     /* XXX Must flush the command history. */
   3299     state->rcpt_overshoot = 0;
   3300 }
   3301 
   3302 #if 0
   3303 
   3304 /* rfc2047_comment_encode - encode comment string */
   3305 
   3306 static VSTRING *rfc2047_comment_encode(const char *str, const char *charset)
   3307 {
   3308     VSTRING *buf = vstring_alloc(30);
   3309     const unsigned char *cp;
   3310     int     ch;
   3311 
   3312     /*
   3313      * XXX This is problematic code.
   3314      *
   3315      * XXX Most of the RFC 2047 "especials" are not special in RFC*822 comments,
   3316      * but we encode them anyway to avoid complaints.
   3317      *
   3318      * XXX In Received: header comments we enclose peer and issuer common names
   3319      * with "" quotes (inherited from the Lutz Jaenicke patch). This is the
   3320      * cause of several quirks.
   3321      *
   3322      * 1) We encode text that contains the " character, even though that
   3323      * character is not special for RFC*822 comments.
   3324      *
   3325      * 2) We ignore the recommended limit of 75 characters per encoded word,
   3326      * because long comments look ugly when folded in-between quotes.
   3327      *
   3328      * 3) We encode the enclosing quotes, to avoid producing invalid encoded
   3329      * words. Microsoft abuses RFC 2047 encoding with attachment names, but
   3330      * we have no information on what decoders do with malformed encoding in
   3331      * comments. This means the comments are Jaenicke-compatible only after
   3332      * decoding.
   3333      */
   3334 #define ESPECIALS "()<>@,;:\"/[]?.="		/* Special in RFC 2047 */
   3335 #define QSPECIALS "_" ESPECIALS			/* Special in RFC 2047 'Q' */
   3336 #define CSPECIALS "\\\"()"			/* Special in our comments */
   3337 
   3338     /* Don't encode if not needed. */
   3339     for (cp = (unsigned char *) str; /* see below */ ; ++cp) {
   3340 	if ((ch = *cp) == 0) {
   3341 	    vstring_sprintf(buf, "\"%s\"", str);
   3342 	    return (buf);
   3343 	}
   3344 	if (!ISPRINT(ch) || strchr(CSPECIALS, ch))
   3345 	    break;
   3346     }
   3347 
   3348     /*
   3349      * Use quoted-printable (like) encoding with spaces mapped to underscore.
   3350      */
   3351     vstring_sprintf(buf, "=?%s?Q?=%02X", charset, '"');
   3352     for (cp = (unsigned char *) str; (ch = *cp) != 0; ++cp) {
   3353 	if (!ISPRINT(ch) || strchr(QSPECIALS CSPECIALS, ch)) {
   3354 	    vstring_sprintf_append(buf, "=%02X", ch);
   3355 	} else if (ch == ' ') {
   3356 	    VSTRING_ADDCH(buf, '_');
   3357 	} else {
   3358 	    VSTRING_ADDCH(buf, ch);
   3359 	}
   3360     }
   3361     vstring_sprintf_append(buf, "=%02X?=", '"');
   3362     return (buf);
   3363 }
   3364 
   3365 #endif
   3366 
   3367 /* comment_sanitize - clean up comment string */
   3368 
   3369 static void comment_sanitize(VSTRING *comment_string)
   3370 {
   3371     unsigned char *cp;
   3372     int     ch;
   3373     int     pc;
   3374 
   3375     /*
   3376      * Postfix Received: headers can be configured to include a comment with
   3377      * the CN (CommonName) of the peer and its issuer, or the login name of a
   3378      * SASL authenticated user. To avoid problems with RFC 822 etc. syntax,
   3379      * we limit this information to printable ASCII text, and neutralize
   3380      * characters that affect comment parsing: the backslash and unbalanced
   3381      * parentheses.
   3382      */
   3383     for (pc = 0, cp = (unsigned char *) STR(comment_string); (ch = *cp) != 0; cp++) {
   3384 	if (!ISASCII(ch) || !ISPRINT(ch) || ch == '\\') {
   3385 	    *cp = '?';
   3386 	} else if (ch == '(') {
   3387 	    pc++;
   3388 	} else if (ch == ')') {
   3389 	    if (pc > 0)
   3390 		pc--;
   3391 	    else
   3392 		*cp = '?';
   3393 	}
   3394     }
   3395     while (pc-- > 0)
   3396 	VSTRING_ADDCH(comment_string, ')');
   3397     VSTRING_TERMINATE(comment_string);
   3398 }
   3399 
   3400 static void common_pre_message_handling(SMTPD_STATE *state,
   3401 	          int (*out_record) (VSTREAM *, int, const char *, ssize_t),
   3402 	              int (*out_fprintf) (VSTREAM *, int, const char *,...),
   3403 				        VSTREAM *out_stream, int out_error);
   3404 static void receive_data_message(SMTPD_STATE *state,
   3405 	          int (*out_record) (VSTREAM *, int, const char *, ssize_t),
   3406 	              int (*out_fprintf) (VSTREAM *, int, const char *,...),
   3407 				         VSTREAM *out_stream, int out_error);
   3408 static int common_post_message_handling(SMTPD_STATE *state);
   3409 
   3410 /* data_cmd - process DATA command */
   3411 
   3412 static int data_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *unused_argv)
   3413 {
   3414     SMTPD_PROXY *proxy;
   3415     const char *err;
   3416     int     (*out_record) (VSTREAM *, int, const char *, ssize_t);
   3417     int     (*out_fprintf) (VSTREAM *, int, const char *,...);
   3418     VSTREAM *out_stream;
   3419     int     out_error;
   3420 
   3421     /*
   3422      * Sanity checks. With ESMTP command pipelining the client can send DATA
   3423      * before all recipients are rejected, so don't report that as a protocol
   3424      * error.
   3425      */
   3426     if (SMTPD_PROCESSING_BDAT(state)) {
   3427 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3428 	smtpd_chat_reply(state, "503 5.5.1 Error: DATA after BDAT");
   3429 	return (-1);
   3430     }
   3431     if (state->rcpt_count == 0) {
   3432 	if (!SMTPD_IN_MAIL_TRANSACTION(state)) {
   3433 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   3434 	    smtpd_chat_reply(state, "503 5.5.1 Error: need RCPT command");
   3435 	} else {
   3436 	    smtpd_chat_reply(state, "554 5.5.1 Error: no valid recipients");
   3437 	}
   3438 	return (-1);
   3439     }
   3440     if (argc != 1) {
   3441 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3442 	smtpd_chat_reply(state, "501 5.5.4 Syntax: DATA");
   3443 	return (-1);
   3444     }
   3445     if (SMTPD_STAND_ALONE(state) == 0 && (err = smtpd_check_data(state)) != 0) {
   3446 	smtpd_chat_reply(state, "%s", err);
   3447 	return (-1);
   3448     }
   3449     if (state->milters != 0
   3450 	&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0
   3451 	&& (err = milter_data_event(state->milters)) != 0
   3452 	&& (err = check_milter_reply(state, err)) != 0) {
   3453 	smtpd_chat_reply(state, "%s", err);
   3454 	return (-1);
   3455     }
   3456     proxy = state->proxy;
   3457     if (proxy != 0 && proxy->cmd(state, SMTPD_PROX_WANT_MORE,
   3458 				 "%s", STR(state->buffer)) != 0) {
   3459 	smtpd_chat_reply(state, "%s", STR(proxy->reply));
   3460 	return (-1);
   3461     }
   3462 
   3463     /*
   3464      * One level of indirection to choose between normal or proxied
   3465      * operation. We want to avoid massive code duplication within tons of
   3466      * if-else clauses.
   3467      */
   3468     if (proxy) {
   3469 	out_stream = proxy->stream;
   3470 	out_record = proxy->rec_put;
   3471 	out_fprintf = proxy->rec_fprintf;
   3472 	out_error = CLEANUP_STAT_PROXY;
   3473     } else {
   3474 	out_stream = state->cleanup;
   3475 	out_record = rec_put;
   3476 	out_fprintf = rec_fprintf;
   3477 	out_error = CLEANUP_STAT_WRITE;
   3478     }
   3479     common_pre_message_handling(state, out_record, out_fprintf,
   3480 				out_stream, out_error);
   3481     smtpd_chat_reply(state, "354 End data with <CR><LF>.<CR><LF>");
   3482     state->where = SMTPD_AFTER_DATA;
   3483     receive_data_message(state, out_record, out_fprintf, out_stream, out_error);
   3484     return common_post_message_handling(state);
   3485 }
   3486 
   3487 /* common_pre_message_handling - finish envelope and open message segment */
   3488 
   3489 static void common_pre_message_handling(SMTPD_STATE *state,
   3490 	          int (*out_record) (VSTREAM *, int, const char *, ssize_t),
   3491 	              int (*out_fprintf) (VSTREAM *, int, const char *,...),
   3492 					        VSTREAM *out_stream,
   3493 					        int out_error)
   3494 {
   3495     SMTPD_PROXY *proxy = state->proxy;
   3496     char  **cpp;
   3497     const char *rfc3848_sess = "";
   3498     const char *rfc3848_auth = "";
   3499     const char *with_verb = " with ";
   3500     const char *with_protocol = (state->flags & SMTPD_FLAG_SMTPUTF8) ?
   3501     "UTF8SMTP" : state->protocol;
   3502     const char *id_verb = state->cleanup ? " id " : "";
   3503     const char *id_value = state->cleanup ? state->queue_id : "";
   3504 
   3505 #ifdef USE_TLS
   3506     VSTRING *peer_CN;
   3507     VSTRING *issuer_CN;
   3508 
   3509 #endif
   3510 #ifdef USE_SASL_AUTH
   3511     VSTRING *username;
   3512 
   3513 #endif
   3514 
   3515     /*
   3516      * Flush out a first batch of access table actions that are delegated to
   3517      * the cleanup server, and that may trigger before we accept the first
   3518      * valid recipient. There will be more after end-of-data.
   3519      *
   3520      * Terminate the message envelope segment. Start the message content
   3521      * segment, and prepend our own Received: header. If there is only one
   3522      * recipient, list the recipient address.
   3523      */
   3524     if (state->cleanup) {
   3525 	if (SMTPD_STAND_ALONE(state) == 0) {
   3526 	    if (state->milters != 0
   3527 		&& (state->saved_flags & MILTER_SKIP_FLAGS) == 0)
   3528 		/* Send actual smtpd_milters list. */
   3529 		(void) milter_send(state->milters, state->cleanup);
   3530 	    if (state->saved_flags)
   3531 		rec_fprintf(state->cleanup, REC_TYPE_FLGS, "%d",
   3532 			    state->saved_flags);
   3533 	}
   3534 	rec_fputs(state->cleanup, REC_TYPE_MESG, "");
   3535     }
   3536 
   3537     /*
   3538      * PREPEND message headers above our own Received: header.
   3539      */
   3540     if (state->prepend)
   3541 	for (cpp = state->prepend->argv; *cpp; cpp++)
   3542 	    out_fprintf(out_stream, REC_TYPE_NORM, "%s", *cpp);
   3543 
   3544     /*
   3545      * Suppress our own Received: header in the unlikely case that we are an
   3546      * intermediate proxy.
   3547      */
   3548     if (!proxy || state->xforward.flags == 0) {
   3549 	if (!var_smtpd_hide_client_session) {
   3550 	    out_fprintf(out_stream, REC_TYPE_NORM,
   3551 			"Received: from %s (%s [%s])",
   3552 			state->helo_name ? state->helo_name : state->name,
   3553 			state->name, state->rfc_addr);
   3554 
   3555 #define VSTRING_STRDUP(s) vstring_strcpy(vstring_alloc(strlen(s) + 1), (s))
   3556 
   3557 #ifdef USE_TLS
   3558 	    if (var_smtpd_tls_received_header && state->tls_context) {
   3559 		int     cont = 0;
   3560 
   3561 		vstring_sprintf(state->buffer,
   3562 				"\t(using %s with cipher %s (%d/%d bits)",
   3563 				state->tls_context->protocol,
   3564 				state->tls_context->cipher_name,
   3565 				state->tls_context->cipher_usebits,
   3566 				state->tls_context->cipher_algbits);
   3567 		if (state->tls_context->kex_name && *state->tls_context->kex_name) {
   3568 		    out_record(out_stream, REC_TYPE_NORM, STR(state->buffer),
   3569 			       LEN(state->buffer));
   3570 		    vstring_sprintf(state->buffer, "\t key-exchange %s",
   3571 				    state->tls_context->kex_name);
   3572 		    if (state->tls_context->kex_curve
   3573 			&& *state->tls_context->kex_curve)
   3574 			vstring_sprintf_append(state->buffer, " (%s)",
   3575 					     state->tls_context->kex_curve);
   3576 		    else if (state->tls_context->kex_bits > 0)
   3577 			vstring_sprintf_append(state->buffer, " (%d bits)",
   3578 					       state->tls_context->kex_bits);
   3579 		    cont = 1;
   3580 		}
   3581 		if (state->tls_context->srvr_sig_name
   3582 		    && *state->tls_context->srvr_sig_name) {
   3583 		    if (cont) {
   3584 			vstring_sprintf_append(state->buffer, " server-signature %s",
   3585 					 state->tls_context->srvr_sig_name);
   3586 		    } else {
   3587 			out_record(out_stream, REC_TYPE_NORM, STR(state->buffer),
   3588 				   LEN(state->buffer));
   3589 			vstring_sprintf(state->buffer, "\t server-signature %s",
   3590 					state->tls_context->srvr_sig_name);
   3591 		    }
   3592 		    if (state->tls_context->srvr_sig_curve
   3593 			&& *state->tls_context->srvr_sig_curve)
   3594 			vstring_sprintf_append(state->buffer, " (%s%s)",
   3595 					 state->tls_context->srvr_sig_curve,
   3596 					       state->tls_context->stoc_rpk ?
   3597 					       " raw public key" : "");
   3598 		    else if (state->tls_context->srvr_sig_bits > 0)
   3599 			vstring_sprintf_append(state->buffer, " (%d bit%s)",
   3600 					  state->tls_context->srvr_sig_bits,
   3601 					       state->tls_context->stoc_rpk ?
   3602 					       " raw public key" : "s");
   3603 		    if (state->tls_context->srvr_sig_dgst
   3604 			&& *state->tls_context->srvr_sig_dgst)
   3605 			vstring_sprintf_append(state->buffer, " server-digest %s",
   3606 					 state->tls_context->srvr_sig_dgst);
   3607 		}
   3608 		if (state->tls_context->clnt_sig_name
   3609 		    && *state->tls_context->clnt_sig_name) {
   3610 		    out_record(out_stream, REC_TYPE_NORM, STR(state->buffer),
   3611 			       LEN(state->buffer));
   3612 		    vstring_sprintf(state->buffer, "\t client-signature %s",
   3613 				    state->tls_context->clnt_sig_name);
   3614 		    if (state->tls_context->clnt_sig_curve
   3615 			&& *state->tls_context->clnt_sig_curve)
   3616 			vstring_sprintf_append(state->buffer, " (%s%s)",
   3617 					 state->tls_context->clnt_sig_curve,
   3618 					       state->tls_context->ctos_rpk ?
   3619 					       " raw public key" : "");
   3620 		    else if (state->tls_context->clnt_sig_bits > 0)
   3621 			vstring_sprintf_append(state->buffer, " (%d bit%s)",
   3622 					  state->tls_context->clnt_sig_bits,
   3623 					       state->tls_context->ctos_rpk ?
   3624 					       " raw public key" : "s");
   3625 		    if (state->tls_context->clnt_sig_dgst
   3626 			&& *state->tls_context->clnt_sig_dgst)
   3627 			vstring_sprintf_append(state->buffer, " client-digest %s",
   3628 					 state->tls_context->clnt_sig_dgst);
   3629 		}
   3630 		out_fprintf(out_stream, REC_TYPE_NORM, "%s)", STR(state->buffer));
   3631 		if (TLS_CERT_IS_PRESENT(state->tls_context)) {
   3632 		    peer_CN = VSTRING_STRDUP(state->tls_context->peer_CN);
   3633 		    comment_sanitize(peer_CN);
   3634 		    issuer_CN = VSTRING_STRDUP(state->tls_context->issuer_CN ?
   3635 					state->tls_context->issuer_CN : "");
   3636 		    comment_sanitize(issuer_CN);
   3637 		    out_fprintf(out_stream, REC_TYPE_NORM,
   3638 				"\t(Client CN \"%s\", Issuer \"%s\" (%s))",
   3639 				STR(peer_CN), STR(issuer_CN),
   3640 				TLS_CERT_IS_TRUSTED(state->tls_context) ?
   3641 				"verified OK" : "not verified");
   3642 		    vstring_free(issuer_CN);
   3643 		    vstring_free(peer_CN);
   3644 		} else if (TLS_RPK_IS_PRESENT(state->tls_context)) {
   3645 		    out_fprintf(out_stream, REC_TYPE_NORM,
   3646 				"\t(Client RPK %s digest %s)",
   3647 				var_smtpd_tls_fpt_dgst,
   3648 				state->tls_context->peer_pkey_fprint);
   3649 		} else if (var_smtpd_tls_ask_ccert)
   3650 		    out_fprintf(out_stream, REC_TYPE_NORM,
   3651 				"\t(Client did not present a certificate)");
   3652 		else
   3653 		    out_fprintf(out_stream, REC_TYPE_NORM,
   3654 				"\t(No client certificate requested)");
   3655 	    }
   3656 	    /* RFC 3848 is defined for ESMTP only. */
   3657 	    if (state->tls_context != 0
   3658 		&& strcmp(state->protocol, MAIL_PROTO_ESMTP) == 0)
   3659 		rfc3848_sess = "S";
   3660 #endif
   3661 #ifdef USE_SASL_AUTH
   3662 	    if (var_smtpd_sasl_auth_hdr && state->sasl_username) {
   3663 		username = VSTRING_STRDUP(state->sasl_username);
   3664 		comment_sanitize(username);
   3665 		out_fprintf(out_stream, REC_TYPE_NORM,
   3666 			    "\t(Authenticated sender: %s)", STR(username));
   3667 		vstring_free(username);
   3668 	    }
   3669 	    /* RFC 3848 is defined for ESMTP only. */
   3670 	    if (state->sasl_username
   3671 		&& strcmp(state->protocol, MAIL_PROTO_ESMTP) == 0)
   3672 		rfc3848_auth = "A";
   3673 #endif
   3674 	} else {
   3675 	    with_verb = "";
   3676 	    with_protocol = "";
   3677 	}
   3678 	if (state->rcpt_count == 1 && state->recipient) {
   3679 	    out_fprintf(out_stream, REC_TYPE_NORM,
   3680 			"%sby %s (%s)%s%s%s%s%s%s",
   3681 			var_smtpd_hide_client_session ? "Received: " : "\t",
   3682 			var_myhostname, var_mail_name,
   3683 			with_verb, with_protocol, rfc3848_sess,
   3684 			rfc3848_auth, id_verb, id_value);
   3685 	    quote_822_local(state->buffer, state->recipient);
   3686 	    out_fprintf(out_stream, REC_TYPE_NORM,
   3687 			"\tfor <%s>; %s", STR(state->buffer),
   3688 			mail_date(state->arrival_time.tv_sec));
   3689 	} else {
   3690 	    out_fprintf(out_stream, REC_TYPE_NORM,
   3691 			"%sby %s (%s)%s%s%s%s%s%s;",
   3692 			var_smtpd_hide_client_session ? "Received: " : "\t",
   3693 			var_myhostname, var_mail_name,
   3694 			with_verb, with_protocol, rfc3848_sess,
   3695 			rfc3848_auth, id_verb, id_value);
   3696 	    out_fprintf(out_stream, REC_TYPE_NORM,
   3697 			"\t%s", mail_date(state->arrival_time.tv_sec));
   3698 	}
   3699 #ifdef RECEIVED_ENVELOPE_FROM
   3700 	quote_822_local(state->buffer, state->sender);
   3701 	out_fprintf(out_stream, REC_TYPE_NORM,
   3702 		    "\t(envelope-from %s)", STR(state->buffer));
   3703 #endif
   3704     }
   3705 }
   3706 
   3707 /* receive_data_message - finish envelope and open message segment */
   3708 
   3709 static void receive_data_message(SMTPD_STATE *state,
   3710 	          int (*out_record) (VSTREAM *, int, const char *, ssize_t),
   3711 	              int (*out_fprintf) (VSTREAM *, int, const char *,...),
   3712 				         VSTREAM *out_stream,
   3713 				         int out_error)
   3714 {
   3715     SMTPD_PROXY *proxy = state->proxy;
   3716     char   *start;
   3717     int     len;
   3718     int     curr_rec_type;
   3719     int     prev_rec_type;
   3720     int     first = 1;
   3721     int     prev_got_bare_lf = 0;
   3722 
   3723     /*
   3724      * If deadlines are enabled, increase the time budget as message content
   3725      * arrives.
   3726      */
   3727     smtp_stream_setup(state->client, var_smtpd_tmout, var_smtpd_req_deadline,
   3728 		      var_smtpd_min_data_rate);
   3729 
   3730     /*
   3731      * Copy the message content. If the cleanup process has a problem, keep
   3732      * reading until the remote stops sending, then complain. Produce typed
   3733      * records from the SMTP stream so we can handle data that spans buffers.
   3734      *
   3735      * XXX Force an empty record when the queue file content begins with
   3736      * whitespace, so that it won't be considered as being part of our own
   3737      * Received: header. What an ugly Kluge.
   3738      *
   3739      * XXX Deal with UNIX-style From_ lines at the start of message content
   3740      * because sendmail permits it.
   3741      */
   3742     for (prev_rec_type = 0; /* void */ ; prev_rec_type = curr_rec_type,
   3743 	 prev_got_bare_lf = smtp_got_bare_lf) {
   3744 	if (smtp_get(state->buffer, state->client, var_line_limit,
   3745 		     SMTP_GET_FLAG_NONE) == '\n')
   3746 	    curr_rec_type = REC_TYPE_NORM;
   3747 	else
   3748 	    curr_rec_type = REC_TYPE_CONT;
   3749 	if (IS_BARE_LF_REPLY_REJECT(smtp_got_bare_lf))
   3750 	    state->err |= CLEANUP_STAT_BARE_LF;
   3751 	else if (IS_BARE_LF_NOTE_LOG(smtp_got_bare_lf))
   3752 	    state->notes |= SMTPD_NOTE_BARE_LF;
   3753 	start = vstring_str(state->buffer);
   3754 	len = VSTRING_LEN(state->buffer);
   3755 	if (first) {
   3756 	    if (strncmp(start + strspn(start, ">"), "From ", 5) == 0) {
   3757 		out_fprintf(out_stream, curr_rec_type,
   3758 			    "X-Mailbox-Line: %s", start);
   3759 		continue;
   3760 	    }
   3761 	    first = 0;
   3762 	    if (len > 0 && IS_SPACE_TAB(start[0]))
   3763 		out_record(out_stream, REC_TYPE_NORM, "", 0);
   3764 	}
   3765 	if (prev_rec_type != REC_TYPE_CONT && *start == '.') {
   3766 	    if (len == 1 && IS_BARE_LF_WANT_STD_EOD(smtp_detect_bare_lf)
   3767 		&& (smtp_got_bare_lf || prev_got_bare_lf))
   3768 		/* Do not store or send to proxy filter. */
   3769 		continue;
   3770 	    if (proxy == 0 ? (++start, --len) == 0 : len == 1)
   3771 		break;
   3772 	}
   3773 	if (state->err == CLEANUP_STAT_OK) {
   3774 	    if (ENFORCING_SIZE_LIMIT(var_message_limit)
   3775 		&& var_message_limit - state->act_size < len + 2) {
   3776 		state->err = CLEANUP_STAT_SIZE;
   3777 		msg_warn("%s: queue file size limit exceeded",
   3778 			 state->queue_id ? state->queue_id : "NOQUEUE");
   3779 	    } else {
   3780 		state->act_size += len + 2;
   3781 		if (out_record(out_stream, curr_rec_type, start, len) < 0)
   3782 		    state->err = out_error;
   3783 	    }
   3784 	}
   3785     }
   3786     state->where = SMTPD_AFTER_EOM;
   3787 }
   3788 
   3789 /* common_post_message_handling - commit message or report error */
   3790 
   3791 static int common_post_message_handling(SMTPD_STATE *state)
   3792 {
   3793     SMTPD_PROXY *proxy = state->proxy;
   3794     const char *err;
   3795     VSTRING *why = 0;
   3796     int     saved_err;
   3797     const CLEANUP_STAT_DETAIL *detail;
   3798 
   3799 #define IS_SMTP_REJECT(s) \
   3800 	(((s)[0] == '4' || (s)[0] == '5') \
   3801 	 && ISDIGIT((s)[1]) && ISDIGIT((s)[2]) \
   3802 	 && ((s)[3] == '\0' || (s)[3] == ' ' || (s)[3] == '-'))
   3803 
   3804     if (state->err == CLEANUP_STAT_OK
   3805 	&& SMTPD_STAND_ALONE(state) == 0
   3806 	&& (err = smtpd_check_eod(state)) != 0) {
   3807 	smtpd_chat_reply(state, "%s", err);
   3808 	if (proxy) {
   3809 	    smtpd_proxy_close(state);
   3810 	} else {
   3811 	    mail_stream_cleanup(state->dest);
   3812 	    state->dest = 0;
   3813 	    state->cleanup = 0;
   3814 	}
   3815 	return (-1);
   3816     }
   3817 
   3818     /*
   3819      * Send the end of DATA and finish the proxy connection. Set the
   3820      * CLEANUP_STAT_PROXY error flag in case of trouble.
   3821      */
   3822     if (proxy) {
   3823 	if (state->err == CLEANUP_STAT_OK) {
   3824 	    (void) proxy->cmd(state, SMTPD_PROX_WANT_ANY, ".");
   3825 	    if (state->err == CLEANUP_STAT_OK &&
   3826 		*STR(proxy->reply) != '2')
   3827 		state->err = CLEANUP_STAT_CONT;
   3828 	}
   3829     }
   3830 
   3831     /*
   3832      * Flush out access table actions that are delegated to the cleanup
   3833      * server. There is similar code at the beginning of the DATA command.
   3834      *
   3835      * Send the end-of-segment markers and finish the queue file record stream.
   3836      */
   3837     else {
   3838 	if (state->err == CLEANUP_STAT_OK) {
   3839 	    rec_fputs(state->cleanup, REC_TYPE_XTRA, "");
   3840 	    if (state->saved_filter)
   3841 		rec_fprintf(state->cleanup, REC_TYPE_FILT, "%s",
   3842 			    state->saved_filter);
   3843 	    if (state->saved_redirect)
   3844 		rec_fprintf(state->cleanup, REC_TYPE_RDR, "%s",
   3845 			    state->saved_redirect);
   3846 	    if (state->saved_bcc) {
   3847 		char  **cpp;
   3848 
   3849 		for (cpp = state->saved_bcc->argv; *cpp; cpp++) {
   3850 		    rec_fprintf(state->cleanup, REC_TYPE_RCPT, "%s",
   3851 				*cpp);
   3852 		    rec_fprintf(state->cleanup, REC_TYPE_ATTR, "%s=%d",
   3853 				MAIL_ATTR_DSN_NOTIFY, DSN_NOTIFY_NEVER);
   3854 		}
   3855 	    }
   3856 	    if (state->saved_flags)
   3857 		rec_fprintf(state->cleanup, REC_TYPE_FLGS, "%d",
   3858 			    state->saved_flags);
   3859 #ifdef DELAY_ACTION
   3860 	    if (state->saved_delay)
   3861 		rec_fprintf(state->cleanup, REC_TYPE_DELAY, "%d",
   3862 			    state->saved_delay);
   3863 #endif
   3864 	    if (vstream_ferror(state->cleanup))
   3865 		state->err = CLEANUP_STAT_WRITE;
   3866 	}
   3867 	if (state->err == CLEANUP_STAT_OK)
   3868 	    if (rec_fputs(state->cleanup, REC_TYPE_END, "") < 0
   3869 		|| vstream_fflush(state->cleanup))
   3870 		state->err = CLEANUP_STAT_WRITE;
   3871 	if (state->err == 0) {
   3872 	    why = vstring_alloc(10);
   3873 	    state->err = mail_stream_finish(state->dest, why);
   3874 	    if (IS_SMTP_REJECT(STR(why)))
   3875 		printable_except(STR(why), ' ', "\r\n");
   3876 	    else
   3877 		printable(STR(why), ' ');
   3878 	} else
   3879 	    mail_stream_cleanup(state->dest);
   3880 	state->dest = 0;
   3881 	state->cleanup = 0;
   3882     }
   3883 
   3884     /*
   3885      * XXX If we lose the cleanup server while it is editing a queue file,
   3886      * the Postfix SMTP server will be out of sync with Milter applications.
   3887      * Sending an ABORT to the Milters is not sufficient to restore
   3888      * synchronization, because there may be any number of Milter replies
   3889      * already in flight. Destroying and recreating the Milters (and faking
   3890      * the connect and ehlo events) is too much trouble for testing and
   3891      * maintenance. Workaround: force the Postfix SMTP server to hang up with
   3892      * a 421 response in the rare case that the cleanup server breaks AND
   3893      * that the remote SMTP client continues the session after end-of-data.
   3894      *
   3895      * XXX Should use something other than CLEANUP_STAT_WRITE when we lose
   3896      * contact with the cleanup server. This requires changes to the
   3897      * mail_stream module and its users (smtpd, qmqpd, perhaps sendmail).
   3898      *
   3899      * XXX See exception below in code that overrides state->access_denied for
   3900      * compliance with RFC 2821 Sec 3.1.
   3901      */
   3902     if (state->milters != 0 && (state->err & CLEANUP_STAT_WRITE) != 0)
   3903 	state->access_denied = mystrdup("421 4.3.0 Mail system error");
   3904 
   3905     /*
   3906      * Handle any errors. One message may suffer from multiple errors, so
   3907      * complain only about the most severe error. Forgive any previous client
   3908      * errors when a message was received successfully.
   3909      *
   3910      * See also: qmqpd.c
   3911      */
   3912     if (state->err == CLEANUP_STAT_OK) {
   3913 	state->error_count = 0;
   3914 	state->error_mask = 0;
   3915 	state->junk_cmds = 0;
   3916 	if (proxy)
   3917 	    smtpd_chat_reply(state, "%s", STR(proxy->reply));
   3918 	else if (SMTPD_PROCESSING_BDAT(state))
   3919 	    smtpd_chat_reply(state,
   3920 			     "250 2.0.0 Ok: %ld bytes queued as %s",
   3921 			     (long) state->act_size, state->queue_id);
   3922 	else
   3923 	    smtpd_chat_reply(state,
   3924 			     "250 2.0.0 Ok: queued as %s", state->queue_id);
   3925     } else if ((state->err & CLEANUP_STAT_BARE_LF) != 0) {
   3926 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   3927 	log_whatsup(state, "reject", "bare <LF> received");
   3928 	smtpd_chat_reply(state, "%d 5.5.2 %s Error: bare <LF> received",
   3929 			 var_smtpd_forbid_bare_lf_code, var_myhostname);
   3930     } else if (why && IS_SMTP_REJECT(STR(why))) {
   3931 	state->error_mask |= MAIL_ERROR_POLICY;
   3932 	smtpd_chat_reply(state, "%s", STR(why));
   3933     } else if ((state->err & CLEANUP_STAT_DEFER) != 0) {
   3934 	state->error_mask |= MAIL_ERROR_POLICY;
   3935 	detail = cleanup_stat_detail(CLEANUP_STAT_DEFER);
   3936 	if (why && LEN(why) > 0) {
   3937 	    /* Allow address-specific DSN status in header/body_checks. */
   3938 	    smtpd_chat_reply(state, "%d %s", detail->smtp, STR(why));
   3939 	} else {
   3940 	    smtpd_chat_reply(state, "%d %s Error: %s",
   3941 			     detail->smtp, detail->dsn, detail->text);
   3942 	}
   3943     } else if ((state->err & CLEANUP_STAT_BAD) != 0) {
   3944 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   3945 	detail = cleanup_stat_detail(CLEANUP_STAT_BAD);
   3946 	smtpd_chat_reply(state, "%d %s Error: internal error %d",
   3947 			 detail->smtp, detail->dsn, state->err);
   3948     } else if ((state->err & CLEANUP_STAT_SIZE) != 0) {
   3949 	state->error_mask |= MAIL_ERROR_BOUNCE;
   3950 	detail = cleanup_stat_detail(CLEANUP_STAT_SIZE);
   3951 	smtpd_chat_reply(state, "%d %s Error: %s",
   3952 			 detail->smtp, detail->dsn, detail->text);
   3953     } else if ((state->err & CLEANUP_STAT_HOPS) != 0) {
   3954 	state->error_mask |= MAIL_ERROR_BOUNCE;
   3955 	detail = cleanup_stat_detail(CLEANUP_STAT_HOPS);
   3956 	smtpd_chat_reply(state, "%d %s Error: %s",
   3957 			 detail->smtp, detail->dsn, detail->text);
   3958     } else if ((state->err & CLEANUP_STAT_CONT) != 0) {
   3959 	state->error_mask |= MAIL_ERROR_POLICY;
   3960 	detail = cleanup_stat_detail(CLEANUP_STAT_CONT);
   3961 	if (proxy) {
   3962 	    smtpd_chat_reply(state, "%s", STR(proxy->reply));
   3963 	} else if (why && LEN(why) > 0) {
   3964 	    /* Allow address-specific DSN status in header/body_checks. */
   3965 	    smtpd_chat_reply(state, "%d %s", detail->smtp, STR(why));
   3966 	} else {
   3967 	    smtpd_chat_reply(state, "%d %s Error: %s",
   3968 			     detail->smtp, detail->dsn, detail->text);
   3969 	}
   3970     } else if ((state->err & CLEANUP_STAT_WRITE) != 0) {
   3971 	state->error_mask |= MAIL_ERROR_RESOURCE;
   3972 	detail = cleanup_stat_detail(CLEANUP_STAT_WRITE);
   3973 	smtpd_chat_reply(state, "%d %s Error: %s",
   3974 			 detail->smtp, detail->dsn, detail->text);
   3975     } else if ((state->err & CLEANUP_STAT_PROXY) != 0) {
   3976 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   3977 	smtpd_chat_reply(state, "%s", STR(proxy->reply));
   3978     } else {
   3979 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   3980 	detail = cleanup_stat_detail(CLEANUP_STAT_BAD);
   3981 	smtpd_chat_reply(state, "%d %s Error: internal error %d",
   3982 			 detail->smtp, detail->dsn, state->err);
   3983     }
   3984 
   3985     /*
   3986      * By popular command: the proxy's end-of-data reply.
   3987      */
   3988     if (proxy)
   3989 	msg_info("proxy-%s: %s: %s;%s",
   3990 		 (state->err == CLEANUP_STAT_OK) ? "accept" : "reject",
   3991 		 state->where, STR(proxy->reply), smtpd_whatsup(state));
   3992 
   3993     /*
   3994      * Cleanup. The client may send another MAIL command.
   3995      */
   3996     saved_err = state->err;
   3997     chat_reset(state, var_smtpd_hist_thrsh);
   3998     mail_reset(state);
   3999     rcpt_reset(state);
   4000     if (why)
   4001 	vstring_free(why);
   4002     return (saved_err);
   4003 }
   4004 
   4005 /* skip_bdat - skip content and respond to BDAT error */
   4006 
   4007 static int skip_bdat(SMTPD_STATE *state, off_t chunk_size,
   4008 		             bool final_chunk, const char *format,...)
   4009 {
   4010     va_list ap;
   4011     off_t   done;
   4012     off_t   len;
   4013 
   4014     /*
   4015      * Read and discard content from the remote SMTP client. TODO: drop the
   4016      * connection in case of overload.
   4017      */
   4018     for (done = 0; done < chunk_size; done += len) {
   4019 	if ((len = chunk_size - done) > VSTREAM_BUFSIZE)
   4020 	    len = VSTREAM_BUFSIZE;
   4021 	smtp_fread_buf(state->buffer, len, state->client);
   4022     }
   4023 
   4024     /*
   4025      * Send the response to the remote SMTP client.
   4026      */
   4027     va_start(ap, format);
   4028     vsmtpd_chat_reply(state, format, ap);
   4029     va_end(ap);
   4030 
   4031     /*
   4032      * Reset state, or drop subsequent BDAT payloads until BDAT LAST or RSET.
   4033      */
   4034     if (final_chunk)
   4035 	mail_reset(state);
   4036     else
   4037 	state->bdat_state = SMTPD_BDAT_STAT_ERROR;
   4038     return (-1);
   4039 }
   4040 
   4041 /* bdat_cmd - process BDAT command */
   4042 
   4043 static int bdat_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   4044 {
   4045     SMTPD_PROXY *proxy;
   4046     const char *err;
   4047     off_t   chunk_size;
   4048     bool    final_chunk;
   4049     off_t   done;
   4050     off_t   read_len;
   4051     char   *start;
   4052     int     len;
   4053     int     curr_rec_type;
   4054     int     (*out_record) (VSTREAM *, int, const char *, ssize_t);
   4055     int     (*out_fprintf) (VSTREAM *, int, const char *,...);
   4056     VSTREAM *out_stream;
   4057     int     out_error;
   4058 
   4059     /*
   4060      * Hang up if the BDAT command is disabled. The next input would be raw
   4061      * message content and that would trigger lots of command errors.
   4062      */
   4063     if (state->ehlo_discard_mask & EHLO_MASK_CHUNKING) {
   4064 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4065 	smtpd_chat_reply(state, "521 5.5.1 Error: command not implemented");
   4066 	return (-1);
   4067     }
   4068 
   4069     /*
   4070      * Hang up if the BDAT command is malformed. The next input would be raw
   4071      * message content and that would trigger lots of command errors.
   4072      */
   4073     if (argc < 2 || argc > 3 || !alldig(argv[1].strval)
   4074 	|| (chunk_size = off_cvt_string(argv[1].strval)) < 0
   4075 	|| ((final_chunk = (argc == 3))
   4076 	    && strcasecmp(argv[2].strval, "LAST") != 0)) {
   4077 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4078 	msg_warn("%s: malformed BDAT command syntax from %s: %.100s",
   4079 		 state->queue_id ? state->queue_id : "NOQUEUE",
   4080 		 state->namaddr, printable(vstring_str(state->buffer), '?'));
   4081 	smtpd_chat_reply(state, "521 5.5.4 Syntax: BDAT count [LAST]");
   4082 	return (-1);
   4083     }
   4084 
   4085     /*
   4086      * If deadlines are enabled, increase the time budget as message content
   4087      * arrives.
   4088      */
   4089     smtp_stream_setup(state->client, var_smtpd_tmout, var_smtpd_req_deadline,
   4090 		      var_smtpd_min_data_rate);
   4091 
   4092     /*
   4093      * Block abuse involving empty chunks (alternatively, we could count
   4094      * "BDAT 0" as a "NOOP", but then we would have to refactor the code that
   4095      * enforces the junk command limit). Clients that send a message as a
   4096      * sequence of "BDAT 1" should not be a problem: the Postfix BDAT
   4097      * implementation should be efficient enough to handle that.
   4098      */
   4099     if (chunk_size == 0 && !final_chunk) {
   4100 	msg_warn("%s: null BDAT request from %s",
   4101 		 state->queue_id ? state->queue_id : "NOQUEUE",
   4102 		 state->namaddr);
   4103 	return skip_bdat(state, chunk_size, final_chunk,
   4104 			 "551 5.7.1 Null BDAT request");
   4105     }
   4106 
   4107     /*
   4108      * BDAT commands may be pipelined within a MAIL transaction. After a BDAT
   4109      * request fails, keep accepting BDAT requests and skipping BDAT payloads
   4110      * to maintain synchronization with the remote SMTP client, until the
   4111      * client sends BDAT LAST or RSET.
   4112      */
   4113     if (state->bdat_state == SMTPD_BDAT_STAT_ERROR)
   4114 	return skip_bdat(state, chunk_size, final_chunk,
   4115 			 "551 5.0.0 Discarded %ld bytes after earlier error",
   4116 			 (long) chunk_size);
   4117 
   4118     /*
   4119      * Special handling for the first BDAT command in a MAIL transaction,
   4120      * treating it as a kind of "DATA" command for the purpose of policy
   4121      * evaluation.
   4122      */
   4123     if (!SMTPD_PROCESSING_BDAT(state)) {
   4124 
   4125 	/*
   4126 	 * With ESMTP command pipelining a client may send BDAT before the
   4127 	 * server has replied to all RCPT commands. For this reason we cannot
   4128 	 * treat BDAT without valid recipients as a protocol error.  Worse,
   4129 	 * RFC 3030 does not discuss the role of BDAT commands in RFC 2920
   4130 	 * command groups (batches of commands that may be sent without
   4131 	 * waiting for a response to each individual command). Therefore we
   4132 	 * have to allow for clients that pipeline the entire SMTP session
   4133 	 * after EHLO, including multiple MAIL transactions.
   4134 	 */
   4135 	if (state->rcpt_count == 0) {
   4136 	    if (!SMTPD_IN_MAIL_TRANSACTION(state)) {
   4137 		/* TODO: maybe remove this from the DATA and BDAT handlers. */
   4138 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   4139 		return skip_bdat(state, chunk_size, final_chunk,
   4140 				 "503 5.5.1 Error: need RCPT command");
   4141 	    } else {
   4142 		return skip_bdat(state, chunk_size, final_chunk,
   4143 				 "554 5.5.1 Error: no valid recipients");
   4144 	    }
   4145 	}
   4146 	if (SMTPD_STAND_ALONE(state) == 0
   4147 	    && (err = smtpd_check_data(state)) != 0) {
   4148 	    return skip_bdat(state, chunk_size, final_chunk, "%s", err);
   4149 	}
   4150 	if (state->milters != 0
   4151 	    && (state->saved_flags & MILTER_SKIP_FLAGS) == 0
   4152 	    && (err = milter_data_event(state->milters)) != 0
   4153 	    && (err = check_milter_reply(state, err)) != 0) {
   4154 	    return skip_bdat(state, chunk_size, final_chunk, "%s", err);
   4155 	}
   4156 	proxy = state->proxy;
   4157 	if (proxy != 0 && proxy->cmd(state, SMTPD_PROX_WANT_MORE,
   4158 				     SMTPD_CMD_DATA) != 0) {
   4159 	    return skip_bdat(state, chunk_size, final_chunk,
   4160 			     "%s", STR(proxy->reply));
   4161 	}
   4162     }
   4163     /* Block too large chunks. */
   4164     if (ENFORCING_SIZE_LIMIT(var_message_limit)
   4165 	&& state->act_size > var_message_limit - chunk_size) {
   4166 	state->error_mask |= MAIL_ERROR_POLICY;
   4167 	msg_warn("%s: BDAT request from %s exceeds message size limit",
   4168 		 state->queue_id ? state->queue_id : "NOQUEUE",
   4169 		 state->namaddr);
   4170 	return skip_bdat(state, chunk_size, final_chunk,
   4171 			 "552 5.3.4 Chunk exceeds message size limit");
   4172     }
   4173 
   4174     /*
   4175      * One level of indirection to choose between normal or proxied
   4176      * operation. We want to avoid massive code duplication within tons of
   4177      * if-else clauses. TODO: store this in its own data structure, or in
   4178      * SMTPD_STATE.
   4179      */
   4180     proxy = state->proxy;
   4181     if (proxy) {
   4182 	out_stream = proxy->stream;
   4183 	out_record = proxy->rec_put;
   4184 	out_fprintf = proxy->rec_fprintf;
   4185 	out_error = CLEANUP_STAT_PROXY;
   4186     } else {
   4187 	out_stream = state->cleanup;
   4188 	out_record = rec_put;
   4189 	out_fprintf = rec_fprintf;
   4190 	out_error = CLEANUP_STAT_WRITE;
   4191     }
   4192     if (!SMTPD_PROCESSING_BDAT(state)) {
   4193 	common_pre_message_handling(state, out_record, out_fprintf,
   4194 				    out_stream, out_error);
   4195 	if (state->bdat_get_buffer == 0)
   4196 	    state->bdat_get_buffer = vstring_alloc(VSTREAM_BUFSIZE);
   4197 	else
   4198 	    VSTRING_RESET(state->bdat_get_buffer);
   4199 	state->bdat_prev_rec_type = 0;
   4200     }
   4201     state->bdat_state = SMTPD_BDAT_STAT_OK;
   4202     state->where = SMTPD_AFTER_BDAT;
   4203 
   4204     /*
   4205      * Copy the message content. If the cleanup process has a problem, keep
   4206      * reading until the remote stops sending, then complain. Produce typed
   4207      * records from the SMTP stream so we can handle data that spans buffers.
   4208      */
   4209 
   4210     /*
   4211      * Instead of reading the entire BDAT chunk into memory, read the chunk
   4212      * one fragment at a time. The loops below always make one iteration, to
   4213      * avoid code duplication for the "BDAT 0 LAST" case (empty chunk).
   4214      */
   4215     done = 0;
   4216     do {
   4217 
   4218 	/*
   4219 	 * Do not skip the smtp_fread_buf() call if read_len == 0. We still
   4220 	 * need the side effects which include resetting the buffer write
   4221 	 * position. Skipping the call would invalidate the buffer state.
   4222 	 *
   4223 	 * Caution: smtp_fread_buf() will long jump after EOF or timeout.
   4224 	 */
   4225 	if ((read_len = chunk_size - done) > VSTREAM_BUFSIZE)
   4226 	    read_len = VSTREAM_BUFSIZE;
   4227 	smtp_fread_buf(state->buffer, read_len, state->client);
   4228 	state->bdat_get_stream = vstream_memreopen(
   4229 			   state->bdat_get_stream, state->buffer, O_RDONLY);
   4230 
   4231 	/*
   4232 	 * Read lines from the fragment. The last line may continue in the
   4233 	 * next fragment, or in the next chunk.
   4234 	 *
   4235 	 * If smtp_get_noexcept() stopped after var_line_limit bytes and did not
   4236 	 * emit a queue file record, then that means smtp_get_noexcept()
   4237 	 * stopped after CR and hit EOF as it tried to find out if the next
   4238 	 * byte is LF. In that case, read the first byte from the next
   4239 	 * fragment or chunk, and if that first byte is LF, then
   4240 	 * smtp_get_noexcept() strips off the trailing CRLF and returns '\n'
   4241 	 * as it always does after reading a complete line.
   4242 	 */
   4243 	do {
   4244 	    int     can_read = var_line_limit - LEN(state->bdat_get_buffer);
   4245 
   4246 	    if (smtp_get_noexcept(state->bdat_get_buffer,
   4247 				  state->bdat_get_stream,
   4248 				  can_read > 0 ? can_read : 1,	/* Peek one */
   4249 				  SMTP_GET_FLAG_APPEND) == '\n') {
   4250 		/* Stopped at end-of-line. */
   4251 		curr_rec_type = REC_TYPE_NORM;
   4252 	    } else if (LEN(state->bdat_get_buffer) > var_line_limit) {
   4253 		/* Undo peeking, and output the buffer as REC_TYPE_CONT. */
   4254 		vstream_ungetc(state->bdat_get_stream,
   4255 			       vstring_end(state->bdat_get_buffer)[-1]);
   4256 		vstring_truncate(state->bdat_get_buffer,
   4257 				 LEN(state->bdat_get_buffer) - 1);
   4258 		curr_rec_type = REC_TYPE_CONT;
   4259 	    } else if (!vstream_feof(state->bdat_get_stream)) {
   4260 		/* Stopped at var_line_limit. */
   4261 		curr_rec_type = REC_TYPE_CONT;
   4262 	    } else if (VSTRING_LEN(state->bdat_get_buffer) > 0
   4263 		       && final_chunk && read_len == chunk_size - done) {
   4264 		/* Stopped at final chunk end; handle missing end-of-line. */
   4265 		curr_rec_type = REC_TYPE_NORM;
   4266 	    } else {
   4267 		/* Stopped at fragment end; empty buffer or not at chunk end. */
   4268 		/* Skip the out_record() and VSTRING_RESET() calls below. */
   4269 		break;
   4270 	    }
   4271 	    if (IS_BARE_LF_REPLY_REJECT(smtp_got_bare_lf))
   4272 		state->err |= CLEANUP_STAT_BARE_LF;
   4273 	    else if (IS_BARE_LF_NOTE_LOG(smtp_got_bare_lf))
   4274 		state->notes |= SMTPD_NOTE_BARE_LF;
   4275 	    start = vstring_str(state->bdat_get_buffer);
   4276 	    len = VSTRING_LEN(state->bdat_get_buffer);
   4277 	    if (state->err == CLEANUP_STAT_OK) {
   4278 		if (ENFORCING_SIZE_LIMIT(var_message_limit)
   4279 		    && var_message_limit - state->act_size < len + 2) {
   4280 		    state->err = CLEANUP_STAT_SIZE;
   4281 		    msg_warn("%s: queue file size limit exceeded",
   4282 			     state->queue_id ? state->queue_id : "NOQUEUE");
   4283 		} else {
   4284 		    state->act_size += len + 2;
   4285 		    if (*start == '.' && proxy != 0
   4286 			&& state->bdat_prev_rec_type != REC_TYPE_CONT)
   4287 			if (out_record(out_stream, REC_TYPE_CONT, ".", 1) < 0)
   4288 			    state->err = out_error;
   4289 		    if (state->err == CLEANUP_STAT_OK
   4290 			&& out_record(out_stream, curr_rec_type,
   4291 				      vstring_str(state->bdat_get_buffer),
   4292 				   VSTRING_LEN(state->bdat_get_buffer)) < 0)
   4293 			state->err = out_error;
   4294 		}
   4295 	    }
   4296 	    VSTRING_RESET(state->bdat_get_buffer);
   4297 	    state->bdat_prev_rec_type = curr_rec_type;
   4298 	} while (!vstream_feof(state->bdat_get_stream));
   4299 	done += read_len;
   4300     } while (done < chunk_size);
   4301 
   4302     /*
   4303      * Special handling for BDAT LAST (successful or unsuccessful).
   4304      */
   4305     if (final_chunk) {
   4306 	state->where = SMTPD_AFTER_EOM;
   4307 	return common_post_message_handling(state);
   4308     }
   4309 
   4310     /*
   4311      * Unsuccessful non-final BDAT command. common_post_message_handling()
   4312      * resets all MAIL transaction state including BDAT state. To avoid
   4313      * useless error messages due to pipelined BDAT commands, enter the
   4314      * SMTPD_BDAT_STAT_ERROR state to accept BDAT commands and skip BDAT
   4315      * payloads.
   4316      */
   4317     else if (state->err != CLEANUP_STAT_OK) {
   4318 	/* NOT: state->where = SMTPD_AFTER_EOM; */
   4319 	(void) common_post_message_handling(state);
   4320 	state->bdat_state = SMTPD_BDAT_STAT_ERROR;
   4321 	return (-1);
   4322     }
   4323 
   4324     /*
   4325      * Successful non-final BDAT command.
   4326      */
   4327     else {
   4328 	smtpd_chat_reply(state, "250 2.0.0 Ok: %ld bytes", (long) chunk_size);
   4329 	return (0);
   4330     }
   4331 }
   4332 
   4333 /* rset_cmd - process RSET */
   4334 
   4335 static int rset_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *unused_argv)
   4336 {
   4337 
   4338     /*
   4339      * Sanity checks.
   4340      */
   4341     if (argc != 1) {
   4342 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4343 	smtpd_chat_reply(state, "501 5.5.4 Syntax: RSET");
   4344 	return (-1);
   4345     }
   4346 
   4347     /*
   4348      * Restore state to right after HELO/EHLO command.
   4349      */
   4350     chat_reset(state, var_smtpd_hist_thrsh);
   4351     mail_reset(state);
   4352     rcpt_reset(state);
   4353     smtpd_chat_reply(state, "250 2.0.0 Ok");
   4354     return (0);
   4355 }
   4356 
   4357 /* noop_cmd - process NOOP */
   4358 
   4359 static int noop_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *unused_argv)
   4360 {
   4361 
   4362     /*
   4363      * XXX 2821 incompatibility: Section 4.1.1.9 says that NOOP can have a
   4364      * parameter string which is to be ignored. NOOP instructions with
   4365      * parameters? Go figure.
   4366      *
   4367      * RFC 2821 violates RFC 821, which says that NOOP takes no parameters.
   4368      */
   4369 #ifdef RFC821_SYNTAX
   4370 
   4371     /*
   4372      * Sanity checks.
   4373      */
   4374     if (argc != 1) {
   4375 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4376 	smtpd_chat_reply(state, "501 5.5.4 Syntax: NOOP");
   4377 	return (-1);
   4378     }
   4379 #endif
   4380     smtpd_chat_reply(state, "250 2.0.0 Ok");
   4381     return (0);
   4382 }
   4383 
   4384 /* vrfy_cmd - process VRFY */
   4385 
   4386 static int vrfy_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   4387 {
   4388     const char *err = 0;
   4389     int     rate;
   4390     int     smtputf8 = 0;
   4391     int     saved_flags;
   4392 
   4393     /*
   4394      * The SMTP standard (RFC 821) disallows unquoted special characters in
   4395      * the VRFY argument. Common practice violates the standard, however.
   4396      * Postfix accommodates common practice where it violates the standard.
   4397      *
   4398      * XXX Impedance mismatch! The SMTP command tokenizer preserves quoting,
   4399      * whereas the recipient restrictions checks expect unquoted (internal)
   4400      * address forms. Therefore we must parse out the address, or we must
   4401      * stop doing recipient restriction checks and lose the opportunity to
   4402      * say "user unknown" at the SMTP port.
   4403      *
   4404      * XXX 2821 incompatibility and brain damage: Section 4.5.1 requires that
   4405      * VRFY is implemented. RFC 821 specifies that VRFY is optional. It gets
   4406      * even worse: section 3.5.3 says that a 502 (command recognized but not
   4407      * implemented) reply is not fully compliant.
   4408      *
   4409      * Thus, an RFC 2821 compliant implementation cannot refuse to supply
   4410      * information in reply to VRFY queries. That is simply bogus. The only
   4411      * reply we could supply is a generic 252 reply. This causes spammers to
   4412      * add tons of bogus addresses to their mailing lists (spam harvesting by
   4413      * trying out large lists of potential recipient names with VRFY).
   4414      */
   4415 #define SLOPPY	0
   4416 
   4417     if (var_disable_vrfy_cmd) {
   4418 	state->error_mask |= MAIL_ERROR_POLICY;
   4419 	smtpd_chat_reply(state, "502 5.5.1 VRFY command is disabled");
   4420 	return (-1);
   4421     }
   4422     /* Fix 20140707: handle missing address. */
   4423     if (var_smtputf8_enable
   4424 	&& (state->ehlo_discard_mask & EHLO_MASK_SMTPUTF8) == 0
   4425 	&& argc > 1 && strcasecmp(argv[argc - 1].strval, "SMTPUTF8") == 0) {
   4426 	argc--;					/* RFC 6531 */
   4427 	smtputf8 = 1;
   4428     }
   4429     if (argc < 2) {
   4430 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4431 	smtpd_chat_reply(state, "501 5.5.4 Syntax: VRFY address%s",
   4432 			 var_smtputf8_enable ? " [SMTPUTF8]" : "");
   4433 	return (-1);
   4434     }
   4435 
   4436     /*
   4437      * XXX The client event count/rate control must be consistent in its use
   4438      * of client address information in connect and disconnect events. For
   4439      * now we exclude xclient authorized hosts from event count/rate control.
   4440      */
   4441     if (SMTPD_STAND_ALONE(state) == 0
   4442 	&& !xclient_allowed
   4443 	&& anvil_clnt
   4444 	&& var_smtpd_crcpt_limit > 0
   4445 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   4446 	&& anvil_clnt_rcpt(anvil_clnt, state->service, state->anvil_range,
   4447 			   &rate) == ANVIL_STAT_OK
   4448 	&& rate > var_smtpd_crcpt_limit) {
   4449 	state->error_mask |= MAIL_ERROR_POLICY;
   4450 	msg_warn("Recipient address rate limit exceeded: %d from %s for service %s",
   4451 		 rate, state->namaddr, state->service);
   4452 	smtpd_chat_reply(state, "450 4.7.1 Error: too many recipients from %s",
   4453 			 state->addr);
   4454 	return (-1);
   4455     }
   4456     if (state->milters != 0 && (err = milter_other_event(state->milters)) != 0
   4457 	&& (err[0] == '5' || err[0] == '4')) {
   4458 	state->error_mask |= MAIL_ERROR_POLICY;
   4459 	smtpd_chat_reply(state, "%s", err);
   4460 	return (-1);
   4461     }
   4462     if (argc > 2)
   4463 	collapse_args(argc - 1, argv + 1);
   4464     if (extract_addr(state, argv + 1, REJECT_EMPTY_ADDR, SLOPPY, smtputf8) != 0) {
   4465 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4466 	smtpd_chat_reply(state, "501 5.1.3 Bad recipient address syntax");
   4467 	return (-1);
   4468     }
   4469     /* Fix 20140707: Check the VRFY command. */
   4470     if (smtputf8 == 0 && var_strict_smtputf8) {
   4471 	if (*STR(state->addr_buf) && !allascii(STR(state->addr_buf))) {
   4472 	    mail_reset(state);
   4473 	    smtpd_chat_reply(state, "553 5.6.7 Must declare SMTPUTF8 to send unicode address");
   4474 	    return (-1);
   4475 	}
   4476     }
   4477     /* Use state->addr_buf, with the unquoted result from extract_addr() */
   4478     if (SMTPD_STAND_ALONE(state) == 0) {
   4479 	/* Fix 20161206: allow UTF8 in smtpd_recipient_restrictions. */
   4480 	saved_flags = state->flags;
   4481 	if (smtputf8)
   4482 	    state->flags |= SMTPD_FLAG_SMTPUTF8;
   4483 	err = smtpd_check_rcpt(state, STR(state->addr_buf));
   4484 	state->flags = saved_flags;
   4485 	if (err != 0) {
   4486 	    smtpd_chat_reply(state, "%s", err);
   4487 	    return (-1);
   4488 	}
   4489     }
   4490 
   4491     /*
   4492      * XXX 2821 new feature: Section 3.5.1 requires that the VRFY response is
   4493      * either "full name <user@domain>" or "user@domain". Postfix replies
   4494      * with the string that was provided by the client, whether or not it is
   4495      * in fully qualified domain form and the address is in <>.
   4496      *
   4497      * Reply code 250 is reserved for the case where the address is verified;
   4498      * reply code 252 should be used when no definitive certainty exists.
   4499      */
   4500     smtpd_chat_reply(state, "252 2.0.0 %s", argv[1].strval);
   4501     return (0);
   4502 }
   4503 
   4504 /* etrn_cmd - process ETRN command */
   4505 
   4506 static int etrn_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   4507 {
   4508     const char *err;
   4509 
   4510     /*
   4511      * Sanity checks.
   4512      */
   4513     if (var_helo_required && state->helo_name == 0) {
   4514 	state->error_mask |= MAIL_ERROR_POLICY;
   4515 	smtpd_chat_reply(state, "503 Error: send HELO/EHLO first");
   4516 	return (-1);
   4517     }
   4518     if (state->milters != 0 && (err = milter_other_event(state->milters)) != 0
   4519 	&& (err[0] == '5' || err[0] == '4')) {
   4520 	state->error_mask |= MAIL_ERROR_POLICY;
   4521 	smtpd_chat_reply(state, "%s", err);
   4522 	return (-1);
   4523     }
   4524     if (SMTPD_IN_MAIL_TRANSACTION(state)) {
   4525 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4526 	smtpd_chat_reply(state, "503 Error: MAIL transaction in progress");
   4527 	return (-1);
   4528     }
   4529     if (argc != 2) {
   4530 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4531 	smtpd_chat_reply(state, "500 Syntax: ETRN domain");
   4532 	return (-1);
   4533     }
   4534     if (argv[1].strval[0] == '@' || argv[1].strval[0] == '#')
   4535 	argv[1].strval++;
   4536 
   4537     /*
   4538      * As an extension to RFC 1985 we also allow an RFC 2821 address literal
   4539      * enclosed in [].
   4540      *
   4541      * XXX There does not appear to be an ETRN parameter to indicate that the
   4542      * domain name is UTF-8.
   4543      */
   4544     if (!valid_hostname(argv[1].strval, DONT_GRIPE)
   4545 	&& !valid_mailhost_literal(argv[1].strval, DONT_GRIPE)) {
   4546 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4547 	smtpd_chat_reply(state, "501 Error: invalid parameter syntax");
   4548 	return (-1);
   4549     }
   4550 
   4551     /*
   4552      * XXX The implementation borrows heavily from the code that implements
   4553      * UCE restrictions. These typically return 450 or 550 when a request is
   4554      * rejected. RFC 1985 requires that 459 be sent when the server refuses
   4555      * to perform the request.
   4556      */
   4557     if (SMTPD_STAND_ALONE(state)) {
   4558 	msg_warn("do not use ETRN in \"sendmail -bs\" mode");
   4559 	smtpd_chat_reply(state, "458 Unable to queue messages");
   4560 	return (-1);
   4561     }
   4562     if ((err = smtpd_check_etrn(state, argv[1].strval)) != 0) {
   4563 	smtpd_chat_reply(state, "%s", err);
   4564 	return (-1);
   4565     }
   4566     switch (flush_send_site(argv[1].strval)) {
   4567     case FLUSH_STAT_OK:
   4568 	smtpd_chat_reply(state, "250 Queuing started");
   4569 	return (0);
   4570     case FLUSH_STAT_DENY:
   4571 	msg_warn("reject: ETRN %.100s... from %s",
   4572 		 argv[1].strval, state->namaddr);
   4573 	smtpd_chat_reply(state, "459 <%s>: service unavailable",
   4574 			 argv[1].strval);
   4575 	return (-1);
   4576     case FLUSH_STAT_BAD:
   4577 	msg_warn("bad ETRN %.100s... from %s", argv[1].strval, state->namaddr);
   4578 	smtpd_chat_reply(state, "458 Unable to queue messages");
   4579 	return (-1);
   4580     default:
   4581 	msg_warn("unable to talk to fast flush service");
   4582 	smtpd_chat_reply(state, "458 Unable to queue messages");
   4583 	return (-1);
   4584     }
   4585 }
   4586 
   4587 /* quit_cmd - process QUIT command */
   4588 
   4589 static int quit_cmd(SMTPD_STATE *state, int unused_argc, SMTPD_TOKEN *unused_argv)
   4590 {
   4591     int     out_pending = vstream_bufstat(state->client, VSTREAM_BST_OUT_PEND);
   4592 
   4593     /*
   4594      * Don't bother checking the syntax.
   4595      */
   4596     smtpd_chat_reply(state, "221 2.0.0 Bye");
   4597 
   4598     /*
   4599      * When the "." and quit replies are pipelined, make sure they are
   4600      * flushed now, to avoid repeated mail deliveries in case of a crash in
   4601      * the "clean up before disconnect" code.
   4602      *
   4603      * XXX When this was added in Postfix 2.1 we used vstream_fflush(). As of
   4604      * Postfix 2.3 we use smtp_flush() for better error reporting.
   4605      */
   4606     if (out_pending > 0)
   4607 	smtp_flush(state->client);
   4608     return (0);
   4609 }
   4610 
   4611 /* xclient_cmd - override SMTP client attributes */
   4612 
   4613 static int xclient_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   4614 {
   4615     SMTPD_TOKEN *argp;
   4616     char   *raw_value;
   4617     char   *attr_value;
   4618     char   *attr_name;
   4619     int     update_namaddr = 0;
   4620     int     name_status;
   4621     static const NAME_CODE peer_codes[] = {
   4622 	XCLIENT_UNAVAILABLE, SMTPD_PEER_CODE_PERM,
   4623 	XCLIENT_TEMPORARY, SMTPD_PEER_CODE_TEMP,
   4624 	0, SMTPD_PEER_CODE_OK,
   4625     };
   4626     static const NAME_CODE proto_names[] = {
   4627 	MAIL_PROTO_SMTP, 1,
   4628 	MAIL_PROTO_ESMTP, 2,
   4629 	0, -1,
   4630     };
   4631     int     got_helo = 0;
   4632     int     got_proto = 0;
   4633 
   4634 #ifdef USE_SASL_AUTH
   4635     int     got_login = 0;
   4636     char   *saved_username;
   4637 
   4638 #endif
   4639 
   4640     /*
   4641      * Sanity checks.
   4642      *
   4643      * XXX The XCLIENT command will override its own access control, so that
   4644      * connection count/rate restrictions can be correctly simulated.
   4645      */
   4646     if (SMTPD_IN_MAIL_TRANSACTION(state)) {
   4647 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4648 	smtpd_chat_reply(state, "503 5.5.1 Error: MAIL transaction in progress");
   4649 	return (-1);
   4650     }
   4651     if (argc < 2) {
   4652 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   4653 	smtpd_chat_reply(state, "501 5.5.4 Syntax: %s attribute=value...",
   4654 			 XCLIENT_CMD);
   4655 	return (-1);
   4656     }
   4657     if (xclient_hosts && xclient_hosts->error)
   4658 	cant_permit_command(state, XCLIENT_CMD);
   4659     if (!xclient_allowed) {
   4660 	state->error_mask |= MAIL_ERROR_POLICY;
   4661 	smtpd_chat_reply(state, "550 5.7.0 Error: insufficient authorization");
   4662 	return (-1);
   4663     }
   4664 #define STREQ(x,y)	(strcasecmp((x), (y)) == 0)
   4665 
   4666     /*
   4667      * Initialize.
   4668      */
   4669     if (state->expand_buf == 0)
   4670 	state->expand_buf = vstring_alloc(100);
   4671 
   4672     /*
   4673      * Iterate over all attribute=value elements.
   4674      */
   4675     for (argp = argv + 1; argp < argv + argc; argp++) {
   4676 	attr_name = argp->strval;
   4677 
   4678 	if ((raw_value = split_at(attr_name, '=')) == 0 || *raw_value == 0) {
   4679 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4680 	    smtpd_chat_reply(state, "501 5.5.4 Error: attribute=value expected");
   4681 	    return (-1);
   4682 	}
   4683 	if (strlen(raw_value) > 255) {
   4684 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4685 	    smtpd_chat_reply(state, "501 5.5.4 Error: attribute value too long");
   4686 	    return (-1);
   4687 	}
   4688 
   4689 	/*
   4690 	 * Backwards compatibility: Postfix prior to version 2.3 does not
   4691 	 * xtext encode attribute values.
   4692 	 */
   4693 	attr_value = xtext_unquote(state->expand_buf, raw_value) ?
   4694 	    STR(state->expand_buf) : raw_value;
   4695 
   4696 	/*
   4697 	 * For safety's sake mask non-printable characters. We'll do more
   4698 	 * specific censoring later.
   4699 	 */
   4700 	printable(attr_value, '?');
   4701 
   4702 #define UPDATE_STR(s, v) do { \
   4703 	const char *_v = (v); \
   4704 	if (s) myfree(s); \
   4705 	(s) = (_v) ? mystrdup(_v) : 0; \
   4706     } while(0)
   4707 
   4708 	/*
   4709 	 * NAME=substitute SMTP client hostname (and reverse/forward name, in
   4710 	 * case of success). Also updates the client hostname lookup status
   4711 	 * code.
   4712 	 */
   4713 	if (STREQ(attr_name, XCLIENT_NAME)) {
   4714 	    name_status = name_code(peer_codes, NAME_CODE_FLAG_NONE, attr_value);
   4715 	    if (name_status != SMTPD_PEER_CODE_OK) {
   4716 		attr_value = CLIENT_NAME_UNKNOWN;
   4717 	    } else {
   4718 		/* XXX EAI */
   4719 		if (!valid_hostname(attr_value, DONT_GRIPE)) {
   4720 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4721 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4722 				     XCLIENT_NAME, attr_value);
   4723 		    return (-1);
   4724 		}
   4725 	    }
   4726 	    state->name_status = name_status;
   4727 	    UPDATE_STR(state->name, attr_value);
   4728 	    update_namaddr = 1;
   4729 	    if (name_status == SMTPD_PEER_CODE_OK) {
   4730 		UPDATE_STR(state->reverse_name, attr_value);
   4731 		state->reverse_name_status = name_status;
   4732 	    }
   4733 	}
   4734 
   4735 	/*
   4736 	 * REVERSE_NAME=substitute SMTP client reverse hostname. Also updates
   4737 	 * the client reverse hostname lookup status code.
   4738 	 */
   4739 	else if (STREQ(attr_name, XCLIENT_REVERSE_NAME)) {
   4740 	    name_status = name_code(peer_codes, NAME_CODE_FLAG_NONE, attr_value);
   4741 	    if (name_status != SMTPD_PEER_CODE_OK) {
   4742 		attr_value = CLIENT_NAME_UNKNOWN;
   4743 	    } else {
   4744 		/* XXX EAI */
   4745 		if (!valid_hostname(attr_value, DONT_GRIPE)) {
   4746 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4747 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4748 				     XCLIENT_REVERSE_NAME, attr_value);
   4749 		    return (-1);
   4750 		}
   4751 	    }
   4752 	    state->reverse_name_status = name_status;
   4753 	    UPDATE_STR(state->reverse_name, attr_value);
   4754 	}
   4755 
   4756 	/*
   4757 	 * ADDR=substitute SMTP client network address.
   4758 	 */
   4759 	else if (STREQ(attr_name, XCLIENT_ADDR)) {
   4760 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE)) {
   4761 		attr_value = CLIENT_ADDR_UNKNOWN;
   4762 		UPDATE_STR(state->addr, attr_value);
   4763 		UPDATE_STR(state->rfc_addr, attr_value);
   4764 	    } else {
   4765 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   4766 		if (normalize_mailhost_addr(attr_value, &state->rfc_addr,
   4767 					    &state->addr,
   4768 					    &state->addr_family) < 0) {
   4769 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4770 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4771 				     XCLIENT_ADDR, attr_value);
   4772 		    return (-1);
   4773 		}
   4774 	    }
   4775 	    update_namaddr = 1;
   4776 	}
   4777 
   4778 	/*
   4779 	 * PORT=substitute SMTP client port number.
   4780 	 */
   4781 	else if (STREQ(attr_name, XCLIENT_PORT)) {
   4782 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE)) {
   4783 		attr_value = CLIENT_PORT_UNKNOWN;
   4784 	    } else {
   4785 		if (!alldig(attr_value)
   4786 		    || strlen(attr_value) > sizeof("65535") - 1) {
   4787 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4788 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4789 				     XCLIENT_PORT, attr_value);
   4790 		    return (-1);
   4791 		}
   4792 	    }
   4793 	    UPDATE_STR(state->port, attr_value);
   4794 	    update_namaddr = 1;
   4795 	}
   4796 
   4797 	/*
   4798 	 * HELO=substitute SMTP client HELO parameter. Censor special
   4799 	 * characters that could mess up message headers.
   4800 	 */
   4801 	else if (STREQ(attr_name, XCLIENT_HELO)) {
   4802 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE)) {
   4803 		attr_value = CLIENT_HELO_UNKNOWN;
   4804 	    } else {
   4805 		if (strlen(attr_value) > VALID_HOSTNAME_LEN) {
   4806 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4807 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4808 				     XCLIENT_HELO, attr_value);
   4809 		    return (-1);
   4810 		}
   4811 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   4812 	    }
   4813 	    UPDATE_STR(state->helo_name, attr_value);
   4814 	    got_helo = 1;
   4815 	}
   4816 
   4817 	/*
   4818 	 * PROTO=SMTP protocol name.
   4819 	 */
   4820 	else if (STREQ(attr_name, XCLIENT_PROTO)) {
   4821 	    if (name_code(proto_names, NAME_CODE_FLAG_NONE, attr_value) < 0) {
   4822 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   4823 		smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4824 				 XCLIENT_PROTO, attr_value);
   4825 		return (-1);
   4826 	    }
   4827 	    UPDATE_STR(state->protocol, uppercase(attr_value));
   4828 	    got_proto = 1;
   4829 	}
   4830 
   4831 	/*
   4832 	 * LOGIN=sasl_username. Sets the authentication method as XCLIENT.
   4833 	 * This can be used even if SASL authentication is turned off in
   4834 	 * main.cf. We can't make it easier than that.
   4835 	 */
   4836 #ifdef USE_SASL_AUTH
   4837 	else if (STREQ(attr_name, XCLIENT_LOGIN)) {
   4838 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE) == 0) {
   4839 		smtpd_sasl_auth_extern(state, attr_value, XCLIENT_CMD);
   4840 		got_login = 1;
   4841 	    }
   4842 	}
   4843 #endif
   4844 
   4845 	/*
   4846 	 * DESTADDR=substitute SMTP server network address.
   4847 	 */
   4848 	else if (STREQ(attr_name, XCLIENT_DESTADDR)) {
   4849 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE)) {
   4850 		attr_value = SERVER_ADDR_UNKNOWN;
   4851 		UPDATE_STR(state->dest_addr, attr_value);
   4852 	    } else {
   4853 #define NO_NORM_RFC_ADDR		((char **) 0)
   4854 #define NO_NORM_ADDR_FAMILY	((int *) 0)
   4855 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   4856 		if (normalize_mailhost_addr(attr_value, NO_NORM_RFC_ADDR,
   4857 					    &state->dest_addr,
   4858 					    NO_NORM_ADDR_FAMILY) < 0) {
   4859 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4860 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4861 				     XCLIENT_DESTADDR, attr_value);
   4862 		    return (-1);
   4863 		}
   4864 	    }
   4865 	    /* XXX Require same address family as client address. */
   4866 	}
   4867 
   4868 	/*
   4869 	 * DESTPORT=substitute SMTP server port number.
   4870 	 */
   4871 	else if (STREQ(attr_name, XCLIENT_DESTPORT)) {
   4872 	    if (STREQ(attr_value, XCLIENT_UNAVAILABLE)) {
   4873 		attr_value = SERVER_PORT_UNKNOWN;
   4874 	    } else {
   4875 		if (!alldig(attr_value)
   4876 		    || strlen(attr_value) > sizeof("65535") - 1) {
   4877 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4878 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   4879 				     XCLIENT_DESTPORT, attr_value);
   4880 		    return (-1);
   4881 		}
   4882 	    }
   4883 	    UPDATE_STR(state->dest_port, attr_value);
   4884 	}
   4885 
   4886 	/*
   4887 	 * Unknown attribute name. Complain.
   4888 	 */
   4889 	else {
   4890 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   4891 	    smtpd_chat_reply(state, "501 5.5.4 Bad %s attribute name: %s",
   4892 			     XCLIENT_CMD, attr_name);
   4893 	    return (-1);
   4894 	}
   4895     }
   4896 
   4897     /*
   4898      * Update the combined name and address when either has changed.
   4899      */
   4900     if (update_namaddr) {
   4901 	if (state->namaddr)
   4902 	    myfree(state->namaddr);
   4903 	state->namaddr =
   4904 	    SMTPD_BUILD_NAMADDRPORT(state->name, state->addr, state->port);
   4905     }
   4906 
   4907     /*
   4908      * XXX Compatibility: when the client issues XCLIENT then we have to go
   4909      * back to initial server greeting stage, otherwise we can't correctly
   4910      * simulate smtpd_client_restrictions (with smtpd_delay_reject=0) and
   4911      * Milter connect restrictions.
   4912      *
   4913      * XXX Compatibility: for accurate simulation we must also reset the HELO
   4914      * information. We keep the information if it was specified in the
   4915      * XCLIENT command.
   4916      *
   4917      * XXX The client connection count/rate control must be consistent in its
   4918      * use of client address information in connect and disconnect events. We
   4919      * re-evaluate xclient so that we correctly simulate connection
   4920      * concurrency and connection rate restrictions.
   4921      *
   4922      * XXX Duplicated from smtpd_proto().
   4923      */
   4924     xclient_allowed =
   4925 	namadr_list_match(xclient_hosts, state->name, state->addr);
   4926     smtp_detect_bare_lf = (SMTPD_STAND_ALONE((state)) == 0 && bare_lf_mask
   4927 	    && !namadr_list_match(bare_lf_excl, state->name, state->addr)) ?
   4928 	bare_lf_mask : 0;
   4929     /* NOT: tls_reset() */
   4930     if (got_helo == 0)
   4931 	helo_reset(state);
   4932     if (got_proto == 0 && strcasecmp(state->protocol, MAIL_PROTO_SMTP) != 0) {
   4933 	myfree(state->protocol);
   4934 	state->protocol = mystrdup(MAIL_PROTO_SMTP);
   4935     }
   4936 #ifdef USE_SASL_AUTH
   4937     /* XXX What if they send the parameters via multiple commands? */
   4938     if (got_login == 0)
   4939 	smtpd_sasl_auth_reset(state);
   4940     if (smtpd_sasl_is_active(state)) {
   4941 	if (got_login)
   4942 	    saved_username = mystrdup(state->sasl_username);
   4943 	smtpd_sasl_deactivate(state);
   4944 #ifdef USE_TLS
   4945 	if (state->tls_context != 0)		/* TLS from XCLIENT proxy? */
   4946 	    smtpd_sasl_activate(state, VAR_SMTPD_SASL_TLS_OPTS,
   4947 				var_smtpd_sasl_tls_opts);
   4948 	else
   4949 #endif
   4950 	    smtpd_sasl_activate(state, VAR_SMTPD_SASL_OPTS,
   4951 				var_smtpd_sasl_opts);
   4952 	if (got_login) {
   4953 	    smtpd_sasl_auth_extern(state, saved_username, XCLIENT_CMD);
   4954 	    myfree(saved_username);
   4955 	}
   4956     }
   4957 #endif
   4958     chat_reset(state, 0);
   4959     mail_reset(state);
   4960     rcpt_reset(state);
   4961     if (state->milters)
   4962 	milter_disc_event(state->milters);
   4963     /* Following duplicates the top-level connect/disconnect handler. */
   4964     teardown_milters(state);
   4965     setup_milters(state);
   4966     vstream_longjmp(state->client, SMTP_ERR_NONE);
   4967     return (0);
   4968 }
   4969 
   4970 /* xforward_cmd - forward logging attributes */
   4971 
   4972 static int xforward_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *argv)
   4973 {
   4974     SMTPD_TOKEN *argp;
   4975     char   *raw_value;
   4976     char   *attr_value;
   4977     char   *attr_name;
   4978     int     updated = 0;
   4979     static const NAME_CODE xforward_flags[] = {
   4980 	XFORWARD_NAME, SMTPD_STATE_XFORWARD_NAME,
   4981 	XFORWARD_ADDR, SMTPD_STATE_XFORWARD_ADDR,
   4982 	XFORWARD_PORT, SMTPD_STATE_XFORWARD_PORT,
   4983 	XFORWARD_PROTO, SMTPD_STATE_XFORWARD_PROTO,
   4984 	XFORWARD_HELO, SMTPD_STATE_XFORWARD_HELO,
   4985 	XFORWARD_IDENT, SMTPD_STATE_XFORWARD_IDENT,
   4986 	XFORWARD_DOMAIN, SMTPD_STATE_XFORWARD_DOMAIN,
   4987 	0, 0,
   4988     };
   4989     static const char *context_name[] = {
   4990 	MAIL_ATTR_RWR_LOCAL,		/* Postfix internal form */
   4991 	MAIL_ATTR_RWR_REMOTE,		/* Postfix internal form */
   4992     };
   4993     static const NAME_CODE xforward_to_context[] = {
   4994 	XFORWARD_DOM_LOCAL, 0,		/* XFORWARD representation */
   4995 	XFORWARD_DOM_REMOTE, 1,		/* XFORWARD representation */
   4996 	0, -1,
   4997     };
   4998     int     flag;
   4999     int     context_code;
   5000 
   5001     /*
   5002      * Sanity checks.
   5003      */
   5004     if (SMTPD_IN_MAIL_TRANSACTION(state)) {
   5005 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   5006 	smtpd_chat_reply(state, "503 5.5.1 Error: MAIL transaction in progress");
   5007 	return (-1);
   5008     }
   5009     if (argc < 2) {
   5010 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   5011 	smtpd_chat_reply(state, "501 5.5.4 Syntax: %s attribute=value...",
   5012 			 XFORWARD_CMD);
   5013 	return (-1);
   5014     }
   5015     if (xforward_hosts && xforward_hosts->error)
   5016 	cant_permit_command(state, XFORWARD_CMD);
   5017     if (!xforward_allowed) {
   5018 	state->error_mask |= MAIL_ERROR_POLICY;
   5019 	smtpd_chat_reply(state, "550 5.7.0 Error: insufficient authorization");
   5020 	return (-1);
   5021     }
   5022 
   5023     /*
   5024      * Initialize.
   5025      */
   5026     if (state->xforward.flags == 0)
   5027 	smtpd_xforward_preset(state);
   5028     if (state->expand_buf == 0)
   5029 	state->expand_buf = vstring_alloc(100);
   5030 
   5031     /*
   5032      * Iterate over all attribute=value elements.
   5033      */
   5034     for (argp = argv + 1; argp < argv + argc; argp++) {
   5035 	attr_name = argp->strval;
   5036 
   5037 	if ((raw_value = split_at(attr_name, '=')) == 0 || *raw_value == 0) {
   5038 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5039 	    smtpd_chat_reply(state, "501 5.5.4 Error: attribute=value expected");
   5040 	    return (-1);
   5041 	}
   5042 	if (strlen(raw_value) > 255) {
   5043 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5044 	    smtpd_chat_reply(state, "501 5.5.4 Error: attribute value too long");
   5045 	    return (-1);
   5046 	}
   5047 
   5048 	/*
   5049 	 * Backwards compatibility: Postfix prior to version 2.3 does not
   5050 	 * xtext encode attribute values.
   5051 	 */
   5052 	attr_value = xtext_unquote(state->expand_buf, raw_value) ?
   5053 	    STR(state->expand_buf) : raw_value;
   5054 
   5055 	/*
   5056 	 * For safety's sake mask non-printable characters. We'll do more
   5057 	 * specific censoring later.
   5058 	 */
   5059 	printable(attr_value, '?');
   5060 
   5061 	flag = name_code(xforward_flags, NAME_CODE_FLAG_NONE, attr_name);
   5062 	switch (flag) {
   5063 
   5064 	    /*
   5065 	     * NAME=up-stream host name, not necessarily in the DNS. Censor
   5066 	     * special characters that could mess up message headers.
   5067 	     */
   5068 	case SMTPD_STATE_XFORWARD_NAME:
   5069 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5070 		attr_value = CLIENT_NAME_UNKNOWN;
   5071 	    } else {
   5072 		/* XXX EAI */
   5073 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   5074 		if (!valid_hostname(attr_value, DONT_GRIPE)) {
   5075 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5076 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   5077 				     XFORWARD_NAME, attr_value);
   5078 		    return (-1);
   5079 		}
   5080 	    }
   5081 	    UPDATE_STR(state->xforward.name, attr_value);
   5082 	    break;
   5083 
   5084 	    /*
   5085 	     * ADDR=up-stream host network address, not necessarily on the
   5086 	     * Internet. Censor special characters that could mess up message
   5087 	     * headers.
   5088 	     */
   5089 	case SMTPD_STATE_XFORWARD_ADDR:
   5090 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5091 		attr_value = CLIENT_ADDR_UNKNOWN;
   5092 		UPDATE_STR(state->xforward.addr, attr_value);
   5093 	    } else {
   5094 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   5095 		if (normalize_mailhost_addr(attr_value,
   5096 					    &state->xforward.rfc_addr,
   5097 					    &state->xforward.addr,
   5098 					    NO_NORM_ADDR_FAMILY) < 0) {
   5099 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5100 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   5101 				     XFORWARD_ADDR, attr_value);
   5102 		    return (-1);
   5103 		}
   5104 	    }
   5105 	    break;
   5106 
   5107 	    /*
   5108 	     * PORT=up-stream port number.
   5109 	     */
   5110 	case SMTPD_STATE_XFORWARD_PORT:
   5111 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5112 		attr_value = CLIENT_PORT_UNKNOWN;
   5113 	    } else {
   5114 		if (!alldig(attr_value)
   5115 		    || strlen(attr_value) > sizeof("65535") - 1) {
   5116 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5117 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   5118 				     XFORWARD_PORT, attr_value);
   5119 		    return (-1);
   5120 		}
   5121 	    }
   5122 	    UPDATE_STR(state->xforward.port, attr_value);
   5123 	    break;
   5124 
   5125 	    /*
   5126 	     * HELO=hostname that the up-stream MTA introduced itself with
   5127 	     * (not necessarily SMTP HELO). Censor special characters that
   5128 	     * could mess up message headers.
   5129 	     */
   5130 	case SMTPD_STATE_XFORWARD_HELO:
   5131 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5132 		attr_value = CLIENT_HELO_UNKNOWN;
   5133 	    } else {
   5134 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   5135 	    }
   5136 	    UPDATE_STR(state->xforward.helo_name, attr_value);
   5137 	    break;
   5138 
   5139 	    /*
   5140 	     * PROTO=up-stream protocol, not necessarily SMTP or ESMTP.
   5141 	     * Censor special characters that could mess up message headers.
   5142 	     */
   5143 	case SMTPD_STATE_XFORWARD_PROTO:
   5144 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5145 		attr_value = CLIENT_PROTO_UNKNOWN;
   5146 	    } else {
   5147 		if (strlen(attr_value) > 64) {
   5148 		    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5149 		    smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   5150 				     XFORWARD_PROTO, attr_value);
   5151 		    return (-1);
   5152 		}
   5153 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   5154 	    }
   5155 	    UPDATE_STR(state->xforward.protocol, attr_value);
   5156 	    break;
   5157 
   5158 	    /*
   5159 	     * IDENT=local message identifier on the up-stream MTA. Censor
   5160 	     * special characters that could mess up logging or macro
   5161 	     * expansions.
   5162 	     */
   5163 	case SMTPD_STATE_XFORWARD_IDENT:
   5164 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE)) {
   5165 		attr_value = CLIENT_IDENT_UNKNOWN;
   5166 	    } else {
   5167 		neuter(attr_value, NEUTER_CHARACTERS, '?');
   5168 	    }
   5169 	    UPDATE_STR(state->xforward.ident, attr_value);
   5170 	    break;
   5171 
   5172 	    /*
   5173 	     * DOMAIN=local or remote.
   5174 	     */
   5175 	case SMTPD_STATE_XFORWARD_DOMAIN:
   5176 	    if (STREQ(attr_value, XFORWARD_UNAVAILABLE))
   5177 		attr_value = XFORWARD_DOM_LOCAL;
   5178 	    if ((context_code = name_code(xforward_to_context,
   5179 					  NAME_CODE_FLAG_NONE,
   5180 					  attr_value)) < 0) {
   5181 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   5182 		smtpd_chat_reply(state, "501 5.5.4 Bad %s syntax: %s",
   5183 				 XFORWARD_DOMAIN, attr_value);
   5184 		return (-1);
   5185 	    }
   5186 	    UPDATE_STR(state->xforward.domain, context_name[context_code]);
   5187 	    break;
   5188 
   5189 	    /*
   5190 	     * Unknown attribute name. Complain.
   5191 	     */
   5192 	default:
   5193 	    state->error_mask |= MAIL_ERROR_PROTOCOL;
   5194 	    smtpd_chat_reply(state, "501 5.5.4 Bad %s attribute name: %s",
   5195 			     XFORWARD_CMD, attr_name);
   5196 	    return (-1);
   5197 	}
   5198 	updated |= flag;
   5199     }
   5200     state->xforward.flags |= updated;
   5201 
   5202     /*
   5203      * Update the combined name and address when either has changed. Use only
   5204      * the name when no address is available.
   5205      */
   5206     if (updated & (SMTPD_STATE_XFORWARD_NAME | SMTPD_STATE_XFORWARD_ADDR
   5207 		   | SMTPD_STATE_XFORWARD_PORT)) {
   5208 	if (state->xforward.namaddr)
   5209 	    myfree(state->xforward.namaddr);
   5210 	state->xforward.namaddr =
   5211 	    IS_AVAIL_CLIENT_ADDR(state->xforward.addr) ?
   5212 	    SMTPD_BUILD_NAMADDRPORT(state->xforward.name,
   5213 				    state->xforward.addr,
   5214 				    state->xforward.port) :
   5215 	    mystrdup(state->xforward.name);
   5216     }
   5217     smtpd_chat_reply(state, "250 2.0.0 Ok");
   5218     return (0);
   5219 }
   5220 
   5221 /* chat_reset - notify postmaster and reset conversation log */
   5222 
   5223 static void chat_reset(SMTPD_STATE *state, int threshold)
   5224 {
   5225 
   5226     /*
   5227      * Notify the postmaster if there were errors. This usually indicates a
   5228      * client configuration problem, or that someone is trying nasty things.
   5229      * Either is significant enough to bother the postmaster. XXX Can't
   5230      * report problems when running in stand-alone mode: postmaster notices
   5231      * require availability of the cleanup service.
   5232      */
   5233     if (state->history != 0 && state->history->argc > threshold) {
   5234 	if (SMTPD_STAND_ALONE(state) == 0
   5235 	    && (state->error_mask & state->notify_mask))
   5236 	    smtpd_chat_notify(state);
   5237 	state->error_mask = 0;
   5238 	smtpd_chat_reset(state);
   5239     }
   5240 }
   5241 
   5242 #ifdef USE_TLS
   5243 
   5244 /* smtpd_start_tls - turn on TLS or force disconnect */
   5245 
   5246 static void smtpd_start_tls(SMTPD_STATE *state)
   5247 {
   5248     int     rate;
   5249     int     cert_present;
   5250     int     requirecert;
   5251 
   5252 #ifdef USE_TLSPROXY
   5253 
   5254     /*
   5255      * This is non-production code, for tlsproxy(8) load testing only. It
   5256      * implements enough to enable some Postfix features that depend on TLS
   5257      * encryption.
   5258      *
   5259      * To insert tlsproxy(8) between this process and the SMTP client, we swap
   5260      * the file descriptors between the state->tlsproxy and state->client
   5261      * VSTREAMS, so that we don't lose all the user-configurable
   5262      * state->client attributes (such as longjump buffers or timeouts).
   5263      *
   5264      * As we implement tlsproxy support in the Postfix SMTP client we should
   5265      * develop a usable abstraction that encapsulates this stream plumbing in
   5266      * a library module.
   5267      */
   5268     vstream_control(state->tlsproxy, CA_VSTREAM_CTL_DOUBLE, CA_VSTREAM_CTL_END);
   5269     vstream_control(state->client, CA_VSTREAM_CTL_SWAP_FD(state->tlsproxy),
   5270 		    CA_VSTREAM_CTL_END);
   5271     (void) vstream_fclose(state->tlsproxy);	/* direct-to-client stream! */
   5272     state->tlsproxy = 0;
   5273 
   5274     /*
   5275      * After plumbing the plaintext stream, receive the TLS context object.
   5276      * For this we must use the same VSTREAM buffer that we also use to
   5277      * receive subsequent SMTP commands. The attribute protocol is robust
   5278      * enough that an adversary cannot inject their own bogus TLS context
   5279      * attributes into the stream.
   5280      */
   5281     state->tls_context = tls_proxy_context_receive(state->client);
   5282 
   5283     /*
   5284      * XXX Maybe it is better to send this information to tlsproxy(8) when
   5285      * requesting service, effectively making a remote tls_server_start()
   5286      * call.
   5287      */
   5288     requirecert = (var_smtpd_tls_req_ccert && var_smtpd_enforce_tls);
   5289 
   5290 #else						/* USE_TLSPROXY */
   5291     TLS_SERVER_START_PROPS props;
   5292     static char *cipher_grade;
   5293     static VSTRING *cipher_exclusions;
   5294 
   5295     /*
   5296      * Wrapper mode uses a dedicated port and always requires TLS.
   5297      *
   5298      * XXX In non-wrapper mode, it is possible to require client certificate
   5299      * verification without requiring TLS. Since certificates can be verified
   5300      * only while TLS is turned on, this means that Postfix will happily
   5301      * perform SMTP transactions when the client does not use the STARTTLS
   5302      * command. For this reason, Postfix does not require client certificate
   5303      * verification unless TLS is required.
   5304      *
   5305      * The cipher grade and exclusions don't change between sessions. Compute
   5306      * just once and cache.
   5307      */
   5308 #define ADD_EXCLUDE(vstr, str) \
   5309     do { \
   5310 	if (*(str)) \
   5311 	    vstring_sprintf_append((vstr), "%s%s", \
   5312 				   VSTRING_LEN(vstr) ? " " : "", (str)); \
   5313     } while (0)
   5314 
   5315     if (cipher_grade == 0) {
   5316 	cipher_grade = var_smtpd_enforce_tls ?
   5317 	    var_smtpd_tls_mand_ciph : var_smtpd_tls_ciph;
   5318 	cipher_exclusions = vstring_alloc(10);
   5319 	ADD_EXCLUDE(cipher_exclusions, var_smtpd_tls_excl_ciph);
   5320 	if (var_smtpd_enforce_tls)
   5321 	    ADD_EXCLUDE(cipher_exclusions, var_smtpd_tls_mand_excl);
   5322 	if (ask_client_cert)
   5323 	    ADD_EXCLUDE(cipher_exclusions, "aNULL");
   5324     }
   5325 
   5326     /*
   5327      * Perform the TLS handshake now. Check the client certificate
   5328      * requirements later, if necessary.
   5329      */
   5330     requirecert = (var_smtpd_tls_req_ccert && var_smtpd_enforce_tls);
   5331 
   5332     state->tls_context =
   5333 	TLS_SERVER_START(&props,
   5334 			 ctx = smtpd_tls_ctx,
   5335 			 stream = state->client,
   5336 			 fd = -1,
   5337 			 timeout = var_smtpd_starttls_tmout,
   5338 			 enable_rpk = var_smtpd_tls_enable_rpk,
   5339 			 requirecert = requirecert,
   5340 			 serverid = state->service,
   5341 			 namaddr = state->namaddr,
   5342 			 cipher_grade = cipher_grade,
   5343 			 cipher_exclusions = STR(cipher_exclusions),
   5344 			 mdalg = var_smtpd_tls_fpt_dgst);
   5345 
   5346 #endif						/* USE_TLSPROXY */
   5347 
   5348     /*
   5349      * For new (i.e. not re-used) TLS sessions, increment the client's new
   5350      * TLS session rate counter. We enforce the limit here only for human
   5351      * factors reasons (reduce the WTF factor), even though it is too late to
   5352      * save the CPU that was already burnt on PKI ops. The real safety
   5353      * mechanism applies with future STARTTLS commands (or wrappermode
   5354      * connections), prior to the SSL handshake.
   5355      *
   5356      * XXX The client event count/rate control must be consistent in its use of
   5357      * client address information in connect and disconnect events. For now
   5358      * we exclude xclient authorized hosts from event count/rate control.
   5359      */
   5360     if (var_smtpd_cntls_limit > 0
   5361      && (state->tls_context == 0 || state->tls_context->session_reused == 0)
   5362 	&& SMTPD_STAND_ALONE(state) == 0
   5363 	&& !xclient_allowed
   5364 	&& anvil_clnt
   5365 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   5366 	&& anvil_clnt_newtls(anvil_clnt, state->service, state->anvil_range,
   5367 			     &rate) == ANVIL_STAT_OK
   5368 	&& rate > var_smtpd_cntls_limit) {
   5369 	state->error_mask |= MAIL_ERROR_POLICY;
   5370 	msg_warn("New TLS session rate limit exceeded: %d from %s for service %s",
   5371 		 rate, state->namaddr, state->service);
   5372 	if (state->tls_context)
   5373 	    smtpd_chat_reply(state,
   5374 		    "421 4.7.0 %s Error: too many new TLS sessions from %s",
   5375 			     var_myhostname, state->namaddr);
   5376 	/* XXX Use regular return to signal end of session. */
   5377 	vstream_longjmp(state->client, SMTP_ERR_QUIET);
   5378     }
   5379 
   5380     /*
   5381      * When the TLS handshake fails, the conversation is in an unknown state.
   5382      * There is nothing we can do except to disconnect from the client.
   5383      */
   5384     if (state->tls_context == 0)
   5385 	vstream_longjmp(state->client, SMTP_ERR_EOF);
   5386 
   5387     /*
   5388      * If we are requiring verified client certs, enforce the constraint
   5389      * here. We have a usable TLS session with the client, so no need to
   5390      * disable I/O, ...  we can even be polite and send "421 ...".
   5391      */
   5392     if (requirecert && TLS_CERT_IS_TRUSTED(state->tls_context) == 0) {
   5393 
   5394 	/*
   5395 	 * In non-wrappermode, fetch the next command (should be EHLO). Reply
   5396 	 * with 421, then disconnect (as a side-effect of replying with 421).
   5397 	 */
   5398 	cert_present = TLS_CERT_IS_PRESENT(state->tls_context);
   5399 	msg_info("NOQUEUE: abort: TLS from %s: %s",
   5400 		 state->namaddr, cert_present ?
   5401 		 "Client certificate not trusted" :
   5402 		 "No client certificate presented");
   5403 	if (var_smtpd_tls_wrappermode == 0)
   5404 	    smtpd_chat_query(state);
   5405 	smtpd_chat_reply(state, "421 4.7.1 %s Error: %s",
   5406 			 var_myhostname, cert_present ?
   5407 			 "Client certificate not trusted" :
   5408 			 "No client certificate presented");
   5409 	state->error_mask |= MAIL_ERROR_POLICY;
   5410 	return;
   5411     }
   5412 
   5413     /*
   5414      * When TLS is turned on, we may offer AUTH methods that would not be
   5415      * offered within a plain-text session.
   5416      *
   5417      * XXX Always refresh SASL the mechanism list after STARTTLS. Dovecot
   5418      * responses may depend on whether the SMTP connection is encrypted.
   5419      */
   5420 #ifdef USE_SASL_AUTH
   5421     if (var_smtpd_sasl_enable) {
   5422 	/* Non-wrappermode, presumably. */
   5423 	if (smtpd_sasl_is_active(state)) {
   5424 	    smtpd_sasl_auth_reset(state);
   5425 	    smtpd_sasl_deactivate(state);
   5426 	}
   5427 	/* Wrappermode and non-wrappermode. */
   5428 	if (smtpd_sasl_is_active(state) == 0)
   5429 	    smtpd_sasl_activate(state, VAR_SMTPD_SASL_TLS_OPTS,
   5430 				var_smtpd_sasl_tls_opts);
   5431     }
   5432 #endif
   5433 }
   5434 
   5435 /* starttls_cmd - respond to STARTTLS */
   5436 
   5437 static int starttls_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *unused_argv)
   5438 {
   5439     const char *err;
   5440     int     rate;
   5441 
   5442     if (argc != 1) {
   5443 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   5444 	smtpd_chat_reply(state, "501 5.5.4 Syntax: STARTTLS");
   5445 	return (-1);
   5446     }
   5447     if (state->milters != 0 && (err = milter_other_event(state->milters)) != 0) {
   5448 	if (err[0] == '5') {
   5449 	    state->error_mask |= MAIL_ERROR_POLICY;
   5450 	    smtpd_chat_reply(state, "%s", err);
   5451 	    return (-1);
   5452 	}
   5453 	/* Sendmail compatibility: map 4xx into 454. */
   5454 	else if (err[0] == '4') {
   5455 	    state->error_mask |= MAIL_ERROR_POLICY;
   5456 	    smtpd_chat_reply(state, "454 4.3.0 Try again later");
   5457 	    return (-1);
   5458 	}
   5459     }
   5460     if (state->tls_context != 0) {
   5461 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   5462 	smtpd_chat_reply(state, "554 5.5.1 Error: TLS already active");
   5463 	return (-1);
   5464     }
   5465     if (var_smtpd_use_tls == 0
   5466 	|| (state->ehlo_discard_mask & EHLO_MASK_STARTTLS)) {
   5467 	state->error_mask |= MAIL_ERROR_PROTOCOL;
   5468 	smtpd_chat_reply(state, "502 5.5.1 Error: command not implemented");
   5469 	return (-1);
   5470     }
   5471 #ifdef USE_TLSPROXY
   5472 
   5473     /*
   5474      * Note: state->tlsproxy is left open when smtp_flush() calls longjmp(),
   5475      * so we garbage-collect the VSTREAM in smtpd_state_reset().
   5476      */
   5477 #define PROXY_OPEN_FLAGS \
   5478 	(TLS_PROXY_FLAG_ROLE_SERVER | TLS_PROXY_FLAG_SEND_CONTEXT)
   5479 
   5480     state->tlsproxy =
   5481 	tls_proxy_legacy_open(var_tlsproxy_service, PROXY_OPEN_FLAGS,
   5482 			      state->client, state->addr,
   5483 			      state->port, var_smtpd_tmout,
   5484 			      state->service);
   5485     if (state->tlsproxy == 0) {
   5486 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   5487 	/* RFC 3207 Section 4. */
   5488 	smtpd_chat_reply(state, "454 4.7.0 TLS not available due to local problem");
   5489 	return (-1);
   5490     }
   5491 #else						/* USE_TLSPROXY */
   5492     if (smtpd_tls_ctx == 0) {
   5493 	state->error_mask |= MAIL_ERROR_SOFTWARE;
   5494 	/* RFC 3207 Section 4. */
   5495 	smtpd_chat_reply(state, "454 4.7.0 TLS not available due to local problem");
   5496 	return (-1);
   5497     }
   5498 #endif						/* USE_TLSPROXY */
   5499 
   5500     /*
   5501      * Enforce TLS handshake rate limit when this client negotiated too many
   5502      * new TLS sessions in the recent past.
   5503      *
   5504      * XXX The client event count/rate control must be consistent in its use of
   5505      * client address information in connect and disconnect events. For now
   5506      * we exclude xclient authorized hosts from event count/rate control.
   5507      */
   5508     if (var_smtpd_cntls_limit > 0
   5509 	&& SMTPD_STAND_ALONE(state) == 0
   5510 	&& !xclient_allowed
   5511 	&& anvil_clnt
   5512 	&& !namadr_list_match(hogger_list, state->name, state->addr)
   5513 	&& anvil_clnt_newtls_stat(anvil_clnt, state->service,
   5514 				  state->anvil_range, &rate) == ANVIL_STAT_OK
   5515 	&& rate > var_smtpd_cntls_limit) {
   5516 	state->error_mask |= MAIL_ERROR_POLICY;
   5517 	msg_warn("Refusing STARTTLS request from %s for service %s",
   5518 		 state->namaddr, state->service);
   5519 	smtpd_chat_reply(state,
   5520 		       "454 4.7.0 Error: too many new TLS sessions from %s",
   5521 			 state->namaddr);
   5522 #ifdef USE_TLSPROXY
   5523 	(void) vstream_fclose(state->tlsproxy);
   5524 	state->tlsproxy = 0;
   5525 #endif
   5526 	return (-1);
   5527     }
   5528     smtpd_chat_reply(state, "220 2.0.0 Ready to start TLS");
   5529     /* Flush before we switch read/write routines or file descriptors. */
   5530     smtp_flush(state->client);
   5531     /* At this point there must not be any pending plaintext. */
   5532     vstream_fpurge(state->client, VSTREAM_PURGE_BOTH);
   5533 
   5534     /*
   5535      * Reset all inputs to the initial state.
   5536      *
   5537      * XXX RFC 2487 does not forbid the use of STARTTLS while mail transfer is
   5538      * in progress, so we have to allow it even when it makes no sense.
   5539      */
   5540     helo_reset(state);
   5541     mail_reset(state);
   5542     rcpt_reset(state);
   5543 
   5544     /*
   5545      * Turn on TLS, using code that is shared with TLS wrapper mode. This
   5546      * code does not return when the handshake fails.
   5547      */
   5548     smtpd_start_tls(state);
   5549     return (0);
   5550 }
   5551 
   5552 /* tls_reset - undo STARTTLS */
   5553 
   5554 static void tls_reset(SMTPD_STATE *state)
   5555 {
   5556     int     failure = 0;
   5557 
   5558     /*
   5559      * Don't waste time when we lost contact.
   5560      */
   5561     if (state->tls_context) {
   5562 	if (vstream_feof(state->client) || vstream_ferror(state->client))
   5563 	    failure = 1;
   5564 	vstream_fflush(state->client);		/* NOT: smtp_flush() */
   5565 #ifdef USE_TLSPROXY
   5566 	tls_proxy_context_free(state->tls_context);
   5567 #else
   5568 	tls_server_stop(smtpd_tls_ctx, state->client, var_smtpd_starttls_tmout,
   5569 			failure, state->tls_context);
   5570 #endif
   5571 	state->tls_context = 0;
   5572     }
   5573 }
   5574 
   5575 #endif
   5576 
   5577 /* milter_unknown_reply_override - common code for unknown/unimplemented case */
   5578 
   5579 static int milter_unknown_reply_override(SMTPD_STATE *state)
   5580 {
   5581     const char *err;
   5582 
   5583     /*
   5584      * XXX The Postfix libmilter implementation does not indicate whether it
   5585      * returns:
   5586      *
   5587      * 1) A generic "command rejected" etc. reply text that is generated by the
   5588      * Postfix milter library, when the Sendmail milter library replies with
   5589      * SMFI_REJECT or SMFI_TEMPFAIL.
   5590      *
   5591      * 2) A custom reply text that is generated by a milter application, when
   5592      * the Sendmail milter library replies with SMFI_REPLY.
   5593      *
   5594      * As a compromise, we will not override the Postfix SMTP server's specific
   5595      * "5XX Unknown command" etc. reply.
   5596      *
   5597      * We're not calling check_milter_reply() because that has unwanted side
   5598      * effects including logging and replying with 421.
   5599      */
   5600     if (state->milters != 0
   5601 	&& (err = milter_unknown_event(state->milters,
   5602 				       STR(state->buffer))) != 0) {
   5603 	if (err[0] == '4') {
   5604 	    smtpd_chat_reply(state, "%s", err);
   5605 	    return (1);
   5606 	} else if (strchr("HDS5", err[0]) == 0) {
   5607 	    msg_warn("unexpected SMFIC_UNKNOWN response: %s", err);
   5608 	}
   5609     }
   5610     return (0);
   5611 }
   5612 
   5613 /* unimpl_cmd - dummy for functionality that is not compiled in */
   5614 
   5615 static int unimpl_cmd(SMTPD_STATE *state, int argc, SMTPD_TOKEN *unused_argv)
   5616 {
   5617 
   5618     /*
   5619      * When a connection is closed we want to log the request counts for
   5620      * unimplemented STARTTLS or AUTH commands separately, instead of logging
   5621      * those commands as "unknown". By handling unimplemented commands with
   5622      * this dummy function, we avoid messing up the command processing loop.
   5623      * Note: the xxfi_unknown() Milter callback has only two valid returns:
   5624      * it must either tempfail or reject.
   5625      */
   5626     state->error_mask |= MAIL_ERROR_PROTOCOL;
   5627     if (milter_unknown_reply_override(state) == 0)
   5628 	smtpd_chat_reply(state, "502 5.5.1 Error: command not implemented");
   5629     return (-1);
   5630 }
   5631 
   5632  /*
   5633   * The table of all SMTP commands that we know. Set the junk limit flag on
   5634   * any command that can be repeated an arbitrary number of times without
   5635   * triggering a tarpit delay of some sort.
   5636   */
   5637 typedef struct SMTPD_CMD {
   5638     char   *name;
   5639     int     (*action) (SMTPD_STATE *, int, SMTPD_TOKEN *);
   5640     int     flags;
   5641     int     success_count;
   5642     int     total_count;
   5643 } SMTPD_CMD;
   5644 
   5645  /*
   5646   * Per RFC 2920: "In particular, the commands RSET, MAIL FROM, SEND FROM,
   5647   * SOML FROM, SAML FROM, and RCPT TO can all appear anywhere in a pipelined
   5648   * command group. The EHLO, DATA, VRFY, EXPN, TURN, QUIT, and NOOP commands
   5649   * can only appear as the last command in a group". RFC 3030 allows BDAT
   5650   * commands to be pipelined as well.
   5651   */
   5652 #define SMTPD_CMD_FLAG_LIMIT	(1<<0)	/* limit usage */
   5653 #define SMTPD_CMD_FLAG_PRE_TLS	(1<<1)	/* allow before STARTTLS */
   5654 #define SMTPD_CMD_FLAG_LAST	(1<<2)	/* last in PIPELINING command group */
   5655 
   5656 static int help_cmd(SMTPD_STATE *, int, SMTPD_TOKEN *);
   5657 
   5658 static SMTPD_CMD smtpd_cmd_table[] = {
   5659     {SMTPD_CMD_HELO, helo_cmd, SMTPD_CMD_FLAG_LIMIT | SMTPD_CMD_FLAG_PRE_TLS | SMTPD_CMD_FLAG_LAST,},
   5660     {SMTPD_CMD_EHLO, ehlo_cmd, SMTPD_CMD_FLAG_LIMIT | SMTPD_CMD_FLAG_PRE_TLS | SMTPD_CMD_FLAG_LAST,},
   5661     {SMTPD_CMD_XCLIENT, xclient_cmd, SMTPD_CMD_FLAG_PRE_TLS},
   5662     {SMTPD_CMD_XFORWARD, xforward_cmd,},
   5663 #ifdef USE_TLS
   5664     {SMTPD_CMD_STARTTLS, starttls_cmd, SMTPD_CMD_FLAG_PRE_TLS,},
   5665 #else
   5666     {SMTPD_CMD_STARTTLS, unimpl_cmd, SMTPD_CMD_FLAG_PRE_TLS,},
   5667 #endif
   5668 #ifdef USE_SASL_AUTH
   5669     {SMTPD_CMD_AUTH, smtpd_sasl_auth_cmd_wrapper,},
   5670 #else
   5671     {SMTPD_CMD_AUTH, unimpl_cmd,},
   5672 #endif
   5673     {SMTPD_CMD_MAIL, mail_cmd,},
   5674     {SMTPD_CMD_RCPT, rcpt_cmd,},
   5675     {SMTPD_CMD_DATA, data_cmd, SMTPD_CMD_FLAG_LAST,},
   5676     {SMTPD_CMD_BDAT, bdat_cmd,},
   5677     {SMTPD_CMD_RSET, rset_cmd, SMTPD_CMD_FLAG_LIMIT,},
   5678     {SMTPD_CMD_NOOP, noop_cmd, SMTPD_CMD_FLAG_LIMIT | SMTPD_CMD_FLAG_PRE_TLS | SMTPD_CMD_FLAG_LAST,},
   5679     {SMTPD_CMD_VRFY, vrfy_cmd, SMTPD_CMD_FLAG_LIMIT | SMTPD_CMD_FLAG_LAST,},
   5680     {SMTPD_CMD_ETRN, etrn_cmd, SMTPD_CMD_FLAG_LIMIT,},
   5681     {SMTPD_CMD_QUIT, quit_cmd, SMTPD_CMD_FLAG_PRE_TLS,},
   5682     {SMTPD_CMD_HELP, help_cmd, SMTPD_CMD_FLAG_PRE_TLS,},
   5683 #ifdef TEST_SMTPD_UNIMPL
   5684     {"unimpl", unimpl_cmd,},
   5685 #endif
   5686     {0,},
   5687 };
   5688 
   5689  /*
   5690   * In addition to counting unknown commands, the last table element also
   5691   * counts malformed commands (which aren't looked up in the command table).
   5692   */
   5693 #define LAST_TABLE_PTR(table) ((table) + sizeof(table)/sizeof(*(table)) - 1)
   5694 static SMTPD_CMD *smtpd_cmdp_unknown = LAST_TABLE_PTR(smtpd_cmd_table);
   5695 
   5696 static STRING_LIST *smtpd_noop_cmds;
   5697 static STRING_LIST *smtpd_forbid_cmds;
   5698 
   5699 /* help_cmd - process HELP command */
   5700 
   5701 static int help_cmd(SMTPD_STATE *state, int unused_argc, SMTPD_TOKEN *unused_argv)
   5702 {
   5703     ARGV   *argv = argv_alloc(sizeof(smtpd_cmd_table)
   5704 			      / sizeof(*smtpd_cmd_table));
   5705     VSTRING *buf = vstring_alloc(100);
   5706     SMTPD_CMD *cmdp;
   5707 
   5708     /*
   5709      * Return a list of implemented commands.
   5710      *
   5711      * The HELP command does not suppress commands that can be dynamically
   5712      * disabled in the EHLO response or through access control. That would
   5713      * require refactoring the EHLO feature-suppression and per-feature
   5714      * access control, so that they can be reused (not duplicated) here.
   5715      *
   5716      * The HELP command does not provide information that makes Postfix easier
   5717      * to fingerprint, such as software name, version, or build information.
   5718      */
   5719     for (cmdp = smtpd_cmd_table; cmdp->name != 0; cmdp++)
   5720 	if (cmdp->action != unimpl_cmd)
   5721 	    argv_add(argv, cmdp->name, ARGV_END);
   5722     argv_sort(argv);
   5723     smtpd_chat_reply(state, "214 2.0.0 Commands: %s",
   5724 		     argv_join(buf, argv, ' '));
   5725     vstring_free(buf);
   5726     argv_free(argv);
   5727     return (0);
   5728 }
   5729 
   5730 /* smtpd_flag_ill_pipelining - flag pipelining protocol violation */
   5731 
   5732 static int smtpd_flag_ill_pipelining(SMTPD_STATE *state)
   5733 {
   5734 
   5735     /*
   5736      * This code will not return after I/O error, timeout, or EOF. VSTREAM
   5737      * exceptions must be enabled in advance with smtp_stream_setup().
   5738      */
   5739     if (vstream_peek(state->client) == 0
   5740 	&& peekfd(vstream_fileno(state->client)) > 0)
   5741 	(void) vstream_ungetc(state->client, smtp_fgetc(state->client));
   5742     if (vstream_peek(state->client) > 0) {
   5743 	if (state->expand_buf == 0)
   5744 	    state->expand_buf = vstring_alloc(100);
   5745 	escape(state->expand_buf, vstream_peek_data(state->client),
   5746 	       vstream_peek(state->client) < 100 ?
   5747 	       vstream_peek(state->client) : 100);
   5748 	msg_info("improper command pipelining after %s from %s: %s",
   5749 		 state->where, state->namaddr, STR(state->expand_buf));
   5750 	state->flags |= SMTPD_FLAG_ILL_PIPELINING;
   5751 	return (1);
   5752     }
   5753     return (0);
   5754 }
   5755 
   5756 /* smtpd_proto - talk the SMTP protocol */
   5757 
   5758 static void smtpd_proto(SMTPD_STATE *state)
   5759 {
   5760     int     argc;
   5761     SMTPD_TOKEN *argv;
   5762     SMTPD_CMD *cmdp;
   5763     const char *ehlo_words;
   5764     const char *err;
   5765     int     status;
   5766     const char *cp;
   5767 
   5768 #ifdef USE_TLS
   5769     int     tls_rate;
   5770 
   5771 #endif
   5772 
   5773     /*
   5774      * Print a greeting banner and run the state machine. Read SMTP commands
   5775      * one line at a time. According to the standard, a sender or recipient
   5776      * address could contain an escaped newline. I think this is perverse,
   5777      * and anyone depending on this is really asking for trouble.
   5778      *
   5779      * In case of mail protocol trouble, the program jumps back to this place,
   5780      * so that it can perform the necessary cleanup before talking to the
   5781      * next client. The setjmp/longjmp primitives are like a sharp tool: use
   5782      * with care. I would certainly recommend against the use of
   5783      * setjmp/longjmp in programs that change privilege levels.
   5784      *
   5785      * In case of file system trouble the program terminates after logging the
   5786      * error and after informing the client. In all other cases (out of
   5787      * memory, panic) the error is logged, and the msg_cleanup() exit handler
   5788      * cleans up, but no attempt is made to inform the client of the nature
   5789      * of the problem.
   5790      *
   5791      * With deadlines enabled, do not increase the time budget while receiving a
   5792      * command, because that would give an attacker too much time.
   5793      */
   5794     vstream_control(state->client, VSTREAM_CTL_EXCEPT, VSTREAM_CTL_END);
   5795     while ((status = vstream_setjmp(state->client)) == SMTP_ERR_NONE)
   5796 	 /* void */ ;
   5797     smtp_stream_setup(state->client, var_smtpd_tmout, var_smtpd_req_deadline, 0);
   5798     switch (status) {
   5799 
   5800     default:
   5801 	msg_panic("smtpd_proto: unknown error reading from %s",
   5802 		  state->namaddr);
   5803 	break;
   5804 
   5805     case SMTP_ERR_TIME:
   5806 	state->reason = REASON_TIMEOUT;
   5807 	if (vstream_setjmp(state->client) == 0)
   5808 	    smtpd_chat_reply(state, "421 4.4.2 %s Error: timeout exceeded",
   5809 			     var_myhostname);
   5810 	break;
   5811 
   5812     case SMTP_ERR_EOF:
   5813 	state->reason = REASON_LOST_CONNECTION;
   5814 	break;
   5815 
   5816     case SMTP_ERR_QUIET:
   5817 	break;
   5818 
   5819     case SMTP_ERR_DATA:
   5820 	msg_info("%s: reject: %s from %s: "
   5821 		 "421 4.3.0 %s Server local data error",
   5822 		 (state->queue_id ? state->queue_id : "NOQUEUE"),
   5823 		 state->where, state->namaddr, var_myhostname);
   5824 	state->error_mask |= MAIL_ERROR_DATA;
   5825 	if (vstream_setjmp(state->client) == 0)
   5826 	    smtpd_chat_reply(state, "421 4.3.0 %s Server local data error",
   5827 			     var_myhostname);
   5828 	break;
   5829 
   5830     case 0:
   5831 
   5832 	/*
   5833 	 * Don't bother doing anything if some pre-SMTP handshake (haproxy)
   5834 	 * did not work out.
   5835 	 */
   5836 	if (state->flags & SMTPD_FLAG_HANGUP) {
   5837 	    smtpd_chat_reply(state, "421 4.3.0 %s Server local error",
   5838 			     var_myhostname);
   5839 	    break;
   5840 	}
   5841 
   5842 	/*
   5843 	 * In TLS wrapper mode, turn on TLS using code that is shared with
   5844 	 * the STARTTLS command. This code does not return when the handshake
   5845 	 * fails.
   5846 	 *
   5847 	 * Enforce TLS handshake rate limit when this client negotiated too many
   5848 	 * new TLS sessions in the recent past.
   5849 	 *
   5850 	 * XXX This means we don't complete a TLS handshake just to tell the
   5851 	 * client that we don't provide service. TLS wrapper mode is
   5852 	 * obsolete, so we don't have to provide perfect support.
   5853 	 */
   5854 #ifdef USE_TLS
   5855 	if (SMTPD_STAND_ALONE(state) == 0 && var_smtpd_tls_wrappermode
   5856 	    && state->tls_context == 0) {
   5857 #ifdef USE_TLSPROXY
   5858 	    /* We garbage-collect the VSTREAM in smtpd_state_reset() */
   5859 	    state->tlsproxy =
   5860 		tls_proxy_legacy_open(var_tlsproxy_service,
   5861 				      PROXY_OPEN_FLAGS,
   5862 				      state->client, state->addr,
   5863 				      state->port, var_smtpd_tmout,
   5864 				      state->service);
   5865 	    if (state->tlsproxy == 0) {
   5866 		msg_warn("Wrapper-mode request dropped from %s for service %s."
   5867 		       " TLS context initialization failed. For details see"
   5868 			 " earlier warnings in your logs.",
   5869 			 state->namaddr, state->service);
   5870 		break;
   5871 	    }
   5872 #else						/* USE_TLSPROXY */
   5873 	    if (smtpd_tls_ctx == 0) {
   5874 		msg_warn("Wrapper-mode request dropped from %s for service %s."
   5875 		       " TLS context initialization failed. For details see"
   5876 			 " earlier warnings in your logs.",
   5877 			 state->namaddr, state->service);
   5878 		break;
   5879 	    }
   5880 #endif						/* USE_TLSPROXY */
   5881 	    if (var_smtpd_cntls_limit > 0
   5882 		&& !xclient_allowed
   5883 		&& anvil_clnt
   5884 		&& !namadr_list_match(hogger_list, state->name, state->addr)
   5885 		&& anvil_clnt_newtls_stat(anvil_clnt, state->service,
   5886 			     state->anvil_range, &tls_rate) == ANVIL_STAT_OK
   5887 		&& tls_rate > var_smtpd_cntls_limit) {
   5888 		state->error_mask |= MAIL_ERROR_POLICY;
   5889 		msg_warn("Refusing TLS service request from %s for service %s",
   5890 			 state->namaddr, state->service);
   5891 		break;
   5892 	    }
   5893 	    smtpd_start_tls(state);
   5894 	}
   5895 #endif
   5896 
   5897 	/*
   5898 	 * If the client spoke before the server sends the initial greeting,
   5899 	 * raise a flag and log the content of the protocol violation. This
   5900 	 * check MUST NOT apply to TLS wrappermode connections.
   5901 	 */
   5902 	if (SMTPD_STAND_ALONE(state) == 0
   5903 	    && vstream_context(state->client) == 0	/* not postscreen */
   5904 	    && (state->flags & SMTPD_FLAG_ILL_PIPELINING) == 0
   5905 	    && smtpd_flag_ill_pipelining(state)
   5906 	    && var_smtpd_forbid_unauth_pipe) {
   5907 	    smtpd_chat_reply(state,
   5908 			  "554 5.5.0 Error: SMTP protocol synchronization");
   5909 	    break;
   5910 	}
   5911 
   5912 	/*
   5913 	 * XXX The client connection count/rate control must be consistent in
   5914 	 * its use of client address information in connect and disconnect
   5915 	 * events. For now we exclude xclient authorized hosts from
   5916 	 * connection count/rate control.
   5917 	 *
   5918 	 * XXX Must send connect/disconnect events to the anvil server even when
   5919 	 * this service is not connection count or rate limited, otherwise it
   5920 	 * will discard client message or recipient rate information too
   5921 	 * early or too late.
   5922 	 */
   5923 	if (SMTPD_STAND_ALONE(state) == 0
   5924 	    && !xclient_allowed
   5925 	    && anvil_clnt
   5926 	    && !namadr_list_match(hogger_list, state->name, state->addr)
   5927 	    && anvil_clnt_connect(anvil_clnt, state->service,
   5928 				  state->anvil_range, &state->conn_count,
   5929 				  &state->conn_rate)
   5930 	    == ANVIL_STAT_OK) {
   5931 	    if (var_smtpd_cconn_limit > 0
   5932 		&& state->conn_count > var_smtpd_cconn_limit) {
   5933 		state->error_mask |= MAIL_ERROR_POLICY;
   5934 		msg_warn("Connection concurrency limit exceeded: %d from %s for service %s",
   5935 			 state->conn_count, state->namaddr, state->service);
   5936 		smtpd_chat_reply(state, "421 4.7.0 %s Error: too many connections from %s",
   5937 				 var_myhostname, state->addr);
   5938 		break;
   5939 	    }
   5940 	    if (var_smtpd_crate_limit > 0
   5941 		&& state->conn_rate > var_smtpd_crate_limit) {
   5942 		msg_warn("Connection rate limit exceeded: %d from %s for service %s",
   5943 			 state->conn_rate, state->namaddr, state->service);
   5944 		smtpd_chat_reply(state, "421 4.7.0 %s Error: too many connections from %s",
   5945 				 var_myhostname, state->addr);
   5946 		break;
   5947 	    }
   5948 	}
   5949 
   5950 	/*
   5951 	 * Determine what server ESMTP features to suppress, typically to
   5952 	 * avoid inter-operability problems. Moved up so we don't send 421
   5953 	 * immediately after sending the initial server response.
   5954 	 */
   5955 	if (ehlo_discard_maps == 0
   5956 	|| (ehlo_words = maps_find(ehlo_discard_maps, state->addr, 0)) == 0)
   5957 	    ehlo_words = var_smtpd_ehlo_dis_words;
   5958 	state->ehlo_discard_mask = ehlo_mask(ehlo_words);
   5959 
   5960 	/* XXX We use the real client for connect access control. */
   5961 	if (SMTPD_STAND_ALONE(state) == 0
   5962 	    && var_smtpd_delay_reject == 0
   5963 	    && (err = smtpd_check_client(state)) != 0) {
   5964 	    state->error_mask |= MAIL_ERROR_POLICY;
   5965 	    state->access_denied = mystrdup(err);
   5966 	    smtpd_chat_reply(state, "%s", state->access_denied);
   5967 	    state->error_count++;
   5968 	}
   5969 
   5970 	/*
   5971 	 * RFC 2034: the text part of all 2xx, 4xx, and 5xx SMTP responses
   5972 	 * other than the initial greeting and any response to HELO or EHLO
   5973 	 * are prefaced with a status code as defined in RFC 3463.
   5974 	 */
   5975 
   5976 	/*
   5977 	 * XXX If a Milter rejects CONNECT, reply with 220 except in case of
   5978 	 * hard reject or 421 (disconnect). The reply persists so it will
   5979 	 * apply to MAIL FROM and to other commands such as AUTH, STARTTLS,
   5980 	 * and VRFY. Note: after a Milter CONNECT reject, we must not reject
   5981 	 * HELO or EHLO, but we do change the feature list that is announced
   5982 	 * in the EHLO response.
   5983 	 */
   5984 	else {
   5985 	    err = 0;
   5986 	    if (state->milters != 0) {
   5987 		milter_macro_callback(state->milters, smtpd_milter_eval,
   5988 				      (void *) state);
   5989 		if ((err = milter_conn_event(state->milters, state->name,
   5990 					     state->addr,
   5991 				  strcmp(state->port, CLIENT_PORT_UNKNOWN) ?
   5992 					     state->port : "0",
   5993 					     state->addr_family)) != 0)
   5994 		    err = check_milter_reply(state, err);
   5995 	    }
   5996 	    if (err && err[0] == '5') {
   5997 		state->error_mask |= MAIL_ERROR_POLICY;
   5998 		smtpd_chat_reply(state, "554 %s ESMTP not accepting connections",
   5999 				 var_myhostname);
   6000 		state->error_count++;
   6001 	    } else if (err && strncmp(err, "421", 3) == 0) {
   6002 		state->error_mask |= MAIL_ERROR_POLICY;
   6003 		smtpd_chat_reply(state, "421 %s Service unavailable - try again later",
   6004 				 var_myhostname);
   6005 		/* Not: state->error_count++; */
   6006 	    } else {
   6007 		smtpd_chat_reply(state, "220 %s", var_smtpd_banner);
   6008 	    }
   6009 	}
   6010 
   6011 	/*
   6012 	 * SASL initialization for plaintext mode.
   6013 	 *
   6014 	 * XXX Backwards compatibility: allow AUTH commands when the AUTH
   6015 	 * announcement is suppressed via smtpd_sasl_exceptions_networks.
   6016 	 *
   6017 	 * XXX Safety: don't enable SASL with "smtpd_tls_auth_only = yes" and
   6018 	 * non-TLS build.
   6019 	 */
   6020 #ifdef USE_SASL_AUTH
   6021 	if (var_smtpd_sasl_enable && smtpd_sasl_is_active(state) == 0
   6022 #ifdef USE_TLS
   6023 	    && state->tls_context == 0 && !var_smtpd_tls_auth_only
   6024 #else
   6025 	    && var_smtpd_tls_auth_only == 0
   6026 #endif
   6027 	    )
   6028 	    smtpd_sasl_activate(state, VAR_SMTPD_SASL_OPTS,
   6029 				var_smtpd_sasl_opts);
   6030 #endif
   6031 
   6032 	/*
   6033 	 * The command read/execute loop.
   6034 	 */
   6035 	for (;;) {
   6036 	    if (state->flags & SMTPD_FLAG_HANGUP)
   6037 		break;
   6038 	    smtp_stream_setup(state->client, var_smtpd_tmout,
   6039 			      var_smtpd_req_deadline, 0);
   6040 	    if (state->error_count >= var_smtpd_hard_erlim) {
   6041 		state->reason = REASON_ERROR_LIMIT;
   6042 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   6043 		smtpd_chat_reply(state, "421 4.7.0 %s Error: too many errors",
   6044 				 var_myhostname);
   6045 		pfilter_notify(1, vstream_fileno(state->client));
   6046 		break;
   6047 	    }
   6048 	    watchdog_pat();
   6049 	    smtpd_chat_query(state);
   6050 	    if (IS_BARE_LF_REPLY_REJECT(smtp_got_bare_lf)) {
   6051 		log_whatsup(state, "reject", "bare <LF> received");
   6052 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   6053 		smtpd_chat_reply(state, "%d 5.5.2 %s Error: bare <LF> received",
   6054 			     var_smtpd_forbid_bare_lf_code, var_myhostname);
   6055 		break;
   6056 	    }
   6057 	    if (IS_BARE_LF_NOTE_LOG(smtp_got_bare_lf))
   6058 		state->notes |= SMTPD_NOTE_BARE_LF;
   6059 	    /* Safety: protect internal interfaces against malformed UTF-8. */
   6060 	    if (var_smtputf8_enable
   6061 		&& valid_utf8_stringz(STR(state->buffer)) == 0) {
   6062 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   6063 		smtpd_chat_reply(state, "500 5.5.2 Error: bad UTF-8 syntax");
   6064 		state->error_count++;
   6065 		state->where = SMTPD_CMD_UNKNOWN;
   6066 		smtpd_cmdp_unknown->total_count += 1;
   6067 		continue;
   6068 	    }
   6069 	    /* Move into smtpd_chat_query() and update session transcript. */
   6070 	    if (smtpd_cmd_filter != 0) {
   6071 		for (cp = STR(state->buffer); *cp && IS_SPACE_TAB(*cp); cp++)
   6072 		     /* void */ ;
   6073 		if ((cp = dict_get(smtpd_cmd_filter, cp)) != 0) {
   6074 		    msg_info("%s: replacing command \"%.100s\" with \"%.100s\"",
   6075 			     state->namaddr, STR(state->buffer), cp);
   6076 		    vstring_strcpy(state->buffer, cp);
   6077 		} else if (smtpd_cmd_filter->error != 0) {
   6078 		    msg_warn("%s:%s lookup error for \"%.100s\"",
   6079 			     smtpd_cmd_filter->type, smtpd_cmd_filter->name,
   6080 			     printable(STR(state->buffer), '?'));
   6081 		    vstream_longjmp(state->client, SMTP_ERR_DATA);
   6082 		}
   6083 	    }
   6084 	    if ((argc = smtpd_token(vstring_str(state->buffer), &argv)) == 0) {
   6085 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   6086 		smtpd_chat_reply(state, "500 5.5.2 Error: bad syntax");
   6087 		state->error_count++;
   6088 		state->where = SMTPD_CMD_UNKNOWN;
   6089 		smtpd_cmdp_unknown->total_count += 1;
   6090 		continue;
   6091 	    }
   6092 	    /* Ignore smtpd_noop_cmds lookup errors. Non-critical feature. */
   6093 	    if (*var_smtpd_noop_cmds
   6094 		&& string_list_match(smtpd_noop_cmds, argv[0].strval)) {
   6095 		smtpd_chat_reply(state, "250 2.0.0 Ok");
   6096 		if (state->junk_cmds++ > var_smtpd_junk_cmd_limit)
   6097 		    state->error_count++;
   6098 		/* XXX We can't count these. */
   6099 		continue;
   6100 	    }
   6101 	    for (cmdp = smtpd_cmd_table; cmdp->name != 0; cmdp++)
   6102 		if (strcasecmp(argv[0].strval, cmdp->name) == 0)
   6103 		    break;
   6104 	    cmdp->total_count += 1;
   6105 	    /* Ignore smtpd_forbid_cmds lookup errors. Non-critical feature. */
   6106 	    if (cmdp->name == 0) {
   6107 		state->where = SMTPD_CMD_UNKNOWN;
   6108 		if (is_header(argv[0].strval)
   6109 		    || (*var_smtpd_forbid_cmds
   6110 		 && string_list_match(smtpd_forbid_cmds, argv[0].strval))) {
   6111 		    VSTRING *escape_buf = vstring_alloc(100);
   6112 
   6113 		    msg_warn("non-SMTP command from %s: %.100s",
   6114 			     state->namaddr,
   6115 			     vstring_str(escape(escape_buf,
   6116 						vstring_str(state->buffer),
   6117 					      VSTRING_LEN(state->buffer))));
   6118 		    smtpd_chat_reply(state, "221 2.7.0 Error: I can break rules, too. Goodbye.");
   6119 		    vstring_free(escape_buf);
   6120 		    break;
   6121 		}
   6122 	    }
   6123 	    /* XXX We use the real client for connect access control. */
   6124 	    if (state->access_denied && cmdp->action != quit_cmd) {
   6125 		/* XXX Exception for Milter override. */
   6126 		if (strncmp(state->access_denied + 1, "21", 2) == 0) {
   6127 		    smtpd_chat_reply(state, "%s", state->access_denied);
   6128 		    continue;
   6129 		}
   6130 		smtpd_chat_reply(state, "503 5.7.0 Error: access denied for %s",
   6131 				 state->namaddr);	/* RFC 2821 Sec 3.1 */
   6132 		state->error_count++;
   6133 		continue;
   6134 	    }
   6135 	    /* state->access_denied == 0 || cmdp->action == quit_cmd */
   6136 	    if (cmdp->name == 0) {
   6137 		if (milter_unknown_reply_override(state) == 0)
   6138 		    smtpd_chat_reply(state,
   6139 				 "500 5.5.2 Error: command not recognized");
   6140 		state->error_mask |= MAIL_ERROR_PROTOCOL;
   6141 		state->error_count++;
   6142 		continue;
   6143 	    }
   6144 #ifdef USE_TLS
   6145 	    if (var_smtpd_enforce_tls &&
   6146 		!state->tls_context &&
   6147 		(cmdp->flags & SMTPD_CMD_FLAG_PRE_TLS) == 0) {
   6148 		smtpd_chat_reply(state,
   6149 			   "530 5.7.0 Must issue a STARTTLS command first");
   6150 		state->error_count++;
   6151 		continue;
   6152 	    }
   6153 #endif
   6154 	    state->where = cmdp->name;
   6155 	    if (SMTPD_STAND_ALONE(state) == 0
   6156 		&& (strcasecmp(state->protocol, MAIL_PROTO_ESMTP) != 0
   6157 		    || (cmdp->flags & SMTPD_CMD_FLAG_LAST))
   6158 		&& (state->flags & SMTPD_FLAG_ILL_PIPELINING) == 0
   6159 		&& smtpd_flag_ill_pipelining(state)
   6160 		&& var_smtpd_forbid_unauth_pipe) {
   6161 		smtpd_chat_reply(state,
   6162 			  "554 5.5.0 Error: SMTP protocol synchronization");
   6163 		break;
   6164 	    }
   6165 	    if (cmdp->action(state, argc, argv) != 0)
   6166 		state->error_count++;
   6167 	    else
   6168 		cmdp->success_count += 1;
   6169 	    if ((cmdp->flags & SMTPD_CMD_FLAG_LIMIT)
   6170 		&& state->junk_cmds++ > var_smtpd_junk_cmd_limit)
   6171 		state->error_count++;
   6172 	    if (cmdp->action == quit_cmd)
   6173 		break;
   6174 	}
   6175 	break;
   6176     }
   6177 
   6178     /*
   6179      * XXX The client connection count/rate control must be consistent in its
   6180      * use of client address information in connect and disconnect events.
   6181      * For now we exclude xclient authorized hosts from connection count/rate
   6182      * control.
   6183      *
   6184      * XXX Must send connect/disconnect events to the anvil server even when
   6185      * this service is not connection count or rate limited, otherwise it
   6186      * will discard client message or recipient rate information too early or
   6187      * too late.
   6188      */
   6189     if (SMTPD_STAND_ALONE(state) == 0
   6190 	&& !xclient_allowed
   6191 	&& anvil_clnt
   6192 	&& !namadr_list_match(hogger_list, state->name, state->addr))
   6193 	anvil_clnt_disconnect(anvil_clnt, state->service, state->anvil_range);
   6194 
   6195     /*
   6196      * Log abnormal session termination, in case postmaster notification has
   6197      * been turned off. In the log, indicate the last recognized state before
   6198      * things went wrong. Don't complain about clients that go away without
   6199      * sending QUIT. Log the byte count after DATA to help diagnose MTU
   6200      * troubles.
   6201      */
   6202     if (state->reason && state->where) {
   6203 	const char *queue_id_or_noqueue = (state->queue_id ?
   6204 					   state->queue_id : "NOQUEUE");
   6205 
   6206 	if (strcmp(state->where, SMTPD_AFTER_DATA) == 0) {
   6207 	    msg_info("%s: %s after %s (%lu bytes) from %s",	/* 2.5 compat */
   6208 		     queue_id_or_noqueue,
   6209 		     state->reason, SMTPD_CMD_DATA,	/* 2.5 compat */
   6210 		     (long) (state->act_size + vstream_peek(state->client)),
   6211 		     state->namaddr);
   6212 	} else if (strcmp(state->where, SMTPD_AFTER_BDAT) == 0) {
   6213 	    msg_info("%s: %s after %s (%lu bytes) from %s",
   6214 		     queue_id_or_noqueue,
   6215 		     state->reason, SMTPD_CMD_BDAT,
   6216 		     (long) (state->act_size + VSTRING_LEN(state->buffer)
   6217 			     + VSTRING_LEN(state->bdat_get_buffer)),
   6218 		     state->namaddr);
   6219 	} else if (strcmp(state->where, SMTPD_AFTER_EOM)
   6220 		   || strcmp(state->reason, REASON_LOST_CONNECTION)) {
   6221 	    msg_info("%s: %s after %s from %s",
   6222 		     queue_id_or_noqueue,
   6223 		     state->reason, state->where, state->namaddr);
   6224 	    if (strcmp(state->where, SMTPD_CMD_AUTH) == 0)
   6225 		pfilter_notify(1, vstream_fileno(state->client));
   6226 	}
   6227     }
   6228 
   6229     /*
   6230      * Cleanup whatever information the client gave us during the SMTP
   6231      * dialog.
   6232      *
   6233      * XXX Duplicated in xclient_cmd().
   6234      */
   6235 #ifdef USE_TLS
   6236     tls_reset(state);
   6237 #endif
   6238     helo_reset(state);
   6239 #ifdef USE_SASL_AUTH
   6240     smtpd_sasl_auth_reset(state);
   6241     if (smtpd_sasl_is_active(state)) {
   6242 	smtpd_sasl_deactivate(state);
   6243     }
   6244 #endif
   6245     chat_reset(state, 0);
   6246     mail_reset(state);
   6247     rcpt_reset(state);
   6248     if (state->milters)
   6249 	milter_disc_event(state->milters);
   6250 }
   6251 
   6252 /* smtpd_format_cmd_stats - format per-command statistics */
   6253 
   6254 static char *smtpd_format_cmd_stats(SMTPD_STATE *state)
   6255 {
   6256     SMTPD_CMD *cmdp;
   6257     int     all_success = 0;
   6258     int     all_total = 0;
   6259     VSTRING *buf = state->buffer;
   6260 
   6261     /*
   6262      * Log the statistics. Note that this loop produces no output when no
   6263      * command was received. We address that after the loop.
   6264      */
   6265     VSTRING_RESET(buf);
   6266     for (cmdp = smtpd_cmd_table; /* see below */ ; cmdp++) {
   6267 	if (cmdp->total_count > 0) {
   6268 	    vstring_sprintf_append(buf, " %s=%d",
   6269 				   cmdp->name ? cmdp->name : "unknown",
   6270 				   cmdp->success_count);
   6271 	    if (cmdp->success_count != cmdp->total_count)
   6272 		vstring_sprintf_append(buf, "/%d", cmdp->total_count);
   6273 	    all_success += cmdp->success_count;
   6274 	    all_total += cmdp->total_count;
   6275 	}
   6276 	if (cmdp->name == 0)
   6277 	    break;
   6278     }
   6279 
   6280     /*
   6281      * Reset the per-command counters.
   6282      *
   6283      * Fix 20190621: the command counter resetting code was moved from the SMTP
   6284      * protocol handler to this place, because the protocol handler was never
   6285      * called after HaProxy handshake error, causing stale numbers to be
   6286      * logged.
   6287      */
   6288     for (cmdp = smtpd_cmd_table; /* see below */ ; cmdp++) {
   6289 	cmdp->success_count = cmdp->total_count = 0;
   6290 	if (cmdp->name == 0)
   6291 	    break;
   6292     }
   6293 
   6294     /*
   6295      * Log total numbers, so that logfile analyzers will see something even
   6296      * if the above loop produced no output. When no commands were received
   6297      * log "0/0" to simplify the identification of abnormal sessions: any
   6298      * statistics with [0-9]/ indicate that there was a problem.
   6299      */
   6300     vstring_sprintf_append(buf, " commands=%d", all_success);
   6301     if (all_success != all_total || all_total == 0)
   6302 	vstring_sprintf_append(buf, "/%d", all_total);
   6303 
   6304     /*
   6305      * Log aggregated warnings.
   6306      */
   6307     if (state->notes & SMTPD_NOTE_BARE_LF)
   6308 	vstring_sprintf_append(buf, " notes=bare_lf");
   6309 
   6310     return (lowercase(STR(buf)));
   6311 }
   6312 
   6313 /* setup_milters - set up Milters after a connection is established */
   6314 
   6315 static void setup_milters(SMTPD_STATE *state)
   6316 {
   6317     const char *milter_string;
   6318 
   6319     /*
   6320      * Postcondition: either state->milters is set, or the
   6321      * INPUT_TRANSP_MILTER flag is passed down-stream.
   6322      */
   6323     if (SMTPD_STAND_ALONE(state) == 0
   6324 	&& (smtpd_input_transp_mask & INPUT_TRANSP_MILTER) == 0
   6325 	&& ((smtpd_milter_maps
   6326 	     && (milter_string =
   6327 		 maps_find(smtpd_milter_maps, state->addr, 0)) != 0)
   6328 	    || *(milter_string = var_smtpd_milters) != 0)
   6329 	&& strcasecmp(milter_string, SMTPD_MILTERS_DISABLE) != 0) {
   6330 	state->milters = milter_create(milter_string,
   6331 				       var_milt_conn_time,
   6332 				       var_milt_cmd_time,
   6333 				       var_milt_msg_time,
   6334 				       var_milt_protocol,
   6335 				       var_milt_def_action,
   6336 				       var_milt_conn_macros,
   6337 				       var_milt_helo_macros,
   6338 				       var_milt_mail_macros,
   6339 				       var_milt_rcpt_macros,
   6340 				       var_milt_data_macros,
   6341 				       var_milt_eoh_macros,
   6342 				       var_milt_eod_macros,
   6343 				       var_milt_unk_macros,
   6344 				       var_milt_macro_deflts);
   6345     }
   6346 
   6347     /*
   6348      * Safety: disable non_smtpd_milters when not sending our own mail filter
   6349      * list. Otherwise the next stage could handle this message as a local
   6350      * submission.
   6351      */
   6352     if (state->milters == 0)
   6353 	smtpd_input_transp_mask |= INPUT_TRANSP_MILTER;
   6354 }
   6355 
   6356 /* teardown_milters - release resources */
   6357 
   6358 static void teardown_milters(SMTPD_STATE *state)
   6359 {
   6360     if (state->milters) {
   6361 	milter_free(state->milters);
   6362 	state->milters = 0;
   6363     }
   6364     smtpd_input_transp_mask =
   6365 	input_transp_mask(VAR_INPUT_TRANSP, var_input_transp);
   6366 }
   6367 
   6368 
   6369 /* smtpd_service - service one client */
   6370 
   6371 static void smtpd_service(VSTREAM *stream, char *service, char **argv)
   6372 {
   6373     SMTPD_STATE state;
   6374 
   6375     /*
   6376      * Sanity check. This service takes no command-line arguments.
   6377      */
   6378     if (argv[0])
   6379 	msg_fatal("unexpected command-line argument: %s", argv[0]);
   6380 
   6381     /*
   6382      * For sanity, require that at least one of INET or INET6 is enabled.
   6383      * Otherwise, we can't look up interface information, and we can't
   6384      * convert names or addresses.
   6385      */
   6386     if (SMTPD_STAND_ALONE_STREAM(stream) == 0
   6387 	&& inet_proto_info()->ai_family_list[0] == 0)
   6388 	msg_fatal("all network protocols are disabled (%s = %s)",
   6389 		  VAR_INET_PROTOCOLS, var_inet_protocols);
   6390 
   6391     /*
   6392      * This routine runs when a client has connected to our network port, or
   6393      * when the smtp server is run in stand-alone mode (input from pipe).
   6394      *
   6395      * Look up and sanitize the peer name, then initialize some connection-
   6396      * specific state. When the name service is hosed, hostname lookup will
   6397      * take a while. This is why I always run a local name server on critical
   6398      * machines.
   6399      */
   6400     smtpd_state_init(&state, stream, service);
   6401     msg_info("connect from %s", state.namaddr);
   6402 
   6403     /*
   6404      * Disable TLS when running in stand-alone mode via "sendmail -bs".
   6405      */
   6406     if (SMTPD_STAND_ALONE((&state))) {
   6407 	var_smtpd_use_tls = 0;
   6408 	var_smtpd_enforce_tls = 0;
   6409 	var_smtpd_tls_auth_only = 0;
   6410     }
   6411 
   6412     /*
   6413      * XCLIENT must not override its own access control.
   6414      */
   6415     xclient_allowed = SMTPD_STAND_ALONE((&state)) == 0 &&
   6416 	namadr_list_match(xclient_hosts, state.name, state.addr);
   6417 
   6418     /*
   6419      * Overriding XFORWARD access control makes no sense, either.
   6420      */
   6421     xforward_allowed = SMTPD_STAND_ALONE((&state)) == 0 &&
   6422 	namadr_list_match(xforward_hosts, state.name, state.addr);
   6423 
   6424     /*
   6425      * Reject or normalize bare LF, with compatibility exclusions.
   6426      */
   6427     smtp_detect_bare_lf = (SMTPD_STAND_ALONE((&state)) == 0 && bare_lf_mask
   6428 	      && !namadr_list_match(bare_lf_excl, state.name, state.addr)) ?
   6429 	bare_lf_mask : 0;
   6430 
   6431     /*
   6432      * See if we need to turn on verbose logging for this client.
   6433      */
   6434     debug_peer_check(state.name, state.addr);
   6435 
   6436     /*
   6437      * Set up Milters, or disable Milters down-stream.
   6438      */
   6439     setup_milters(&state);			/* duplicates xclient_cmd */
   6440 
   6441     /*
   6442      * Provide the SMTP service.
   6443      */
   6444     smtpd_proto(&state);
   6445 
   6446     /*
   6447      * After the client has gone away, clean up whatever we have set up at
   6448      * connection time.
   6449      */
   6450     msg_info("disconnect from %s%s", state.namaddr,
   6451 	     smtpd_format_cmd_stats(&state));
   6452     teardown_milters(&state);			/* duplicates xclient_cmd */
   6453     smtpd_state_reset(&state);
   6454     debug_peer_restore();
   6455 }
   6456 
   6457 /* pre_accept - see if tables have changed */
   6458 
   6459 static void pre_accept(char *unused_name, char **unused_argv)
   6460 {
   6461     const char *table;
   6462 
   6463     if ((table = dict_changed_name()) != 0) {
   6464 	msg_info("table %s has changed -- restarting", table);
   6465 	exit(0);
   6466     }
   6467 }
   6468 
   6469 /* pre_jail_init - pre-jail initialization */
   6470 
   6471 static void pre_jail_init(char *unused_name, char **unused_argv)
   6472 {
   6473 
   6474     /*
   6475      * Initialize denylist/etc. patterns before entering the chroot jail, in
   6476      * case they specify a filename pattern.
   6477      */
   6478     smtpd_noop_cmds = string_list_init(VAR_SMTPD_NOOP_CMDS, MATCH_FLAG_RETURN,
   6479 				       var_smtpd_noop_cmds);
   6480     smtpd_forbid_cmds = string_list_init(VAR_SMTPD_FORBID_CMDS,
   6481 					 MATCH_FLAG_RETURN,
   6482 					 var_smtpd_forbid_cmds);
   6483     verp_clients = namadr_list_init(VAR_VERP_CLIENTS, MATCH_FLAG_RETURN,
   6484 				    var_verp_clients);
   6485     xclient_hosts = namadr_list_init(VAR_XCLIENT_HOSTS, MATCH_FLAG_RETURN,
   6486 				     var_xclient_hosts);
   6487     xforward_hosts = namadr_list_init(VAR_XFORWARD_HOSTS, MATCH_FLAG_RETURN,
   6488 				      var_xforward_hosts);
   6489     hogger_list = namadr_list_init(VAR_SMTPD_HOGGERS, MATCH_FLAG_RETURN
   6490 				   | match_parent_style(VAR_SMTPD_HOGGERS),
   6491 				   var_smtpd_hoggers);
   6492     bare_lf_excl = namadr_list_init(VAR_SMTPD_FORBID_BARE_LF_EXCL,
   6493 				    MATCH_FLAG_RETURN
   6494 				    | match_parent_style(VAR_MYNETWORKS),
   6495 				    var_smtpd_forbid_bare_lf_excl);
   6496     if ((bare_lf_mask = name_code(bare_lf_mask_table, NAME_CODE_FLAG_NONE,
   6497 				  var_smtpd_forbid_bare_lf)) < 0)
   6498 	msg_fatal("bad parameter value: '%s = %s'",
   6499 		  VAR_SMTPD_FORBID_BARE_LF, var_smtpd_forbid_bare_lf);
   6500 
   6501     /*
   6502      * Open maps before dropping privileges so we can read passwords etc.
   6503      *
   6504      * XXX We should not do this in stand-alone (sendmail -bs) mode, but we
   6505      * can't use SMTPD_STAND_ALONE(state) here. This means "sendmail -bs"
   6506      * will try to connect to proxymap when invoked by root for mail
   6507      * submission. To fix, we would have to pass stand-alone mode information
   6508      * via different means. For now we have to tell people not to run mail
   6509      * clients as root.
   6510      */
   6511     if (getuid() == 0 || getuid() == var_owner_uid)
   6512 	smtpd_check_init();
   6513     smtpd_expand_init();
   6514     debug_peer_init();
   6515 
   6516     if (var_smtpd_sasl_enable)
   6517 #ifdef USE_SASL_AUTH
   6518 	smtpd_sasl_initialize();
   6519 
   6520     if (*var_smtpd_sasl_exceptions_networks)
   6521 	sasl_exceptions_networks =
   6522 	    namadr_list_init(VAR_SMTPD_SASL_EXCEPTIONS_NETWORKS,
   6523 			     MATCH_FLAG_RETURN,
   6524 			     var_smtpd_sasl_exceptions_networks);
   6525 #else
   6526 	msg_warn("%s is true, but SASL support is not compiled in",
   6527 		 VAR_SMTPD_SASL_ENABLE);
   6528 #endif
   6529 
   6530     if (*var_smtpd_cmd_filter)
   6531 	smtpd_cmd_filter = dict_open(var_smtpd_cmd_filter, O_RDONLY,
   6532 				     DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX);
   6533 
   6534     /*
   6535      * XXX Temporary fix to pretend that we consistently implement TLS
   6536      * security levels. We implement only a subset for now. If we implement
   6537      * more levels, wrappermode should override only weaker TLS security
   6538      * levels.
   6539      *
   6540      * Note: tls_level_lookup() logs no warning.
   6541      */
   6542     if (!var_smtpd_tls_wrappermode && *var_smtpd_tls_level) {
   6543 	switch (tls_level_lookup(var_smtpd_tls_level)) {
   6544 	default:
   6545 	    msg_fatal("Invalid TLS level \"%s\"", var_smtpd_tls_level);
   6546 	    /* NOTREACHED */
   6547 	    break;
   6548 	case TLS_LEV_SECURE:
   6549 	case TLS_LEV_VERIFY:
   6550 	case TLS_LEV_FPRINT:
   6551 	    msg_warn("%s: unsupported TLS level \"%s\", using \"encrypt\"",
   6552 		     VAR_SMTPD_TLS_LEVEL, var_smtpd_tls_level);
   6553 	    /* FALLTHROUGH */
   6554 	case TLS_LEV_ENCRYPT:
   6555 	    var_smtpd_enforce_tls = var_smtpd_use_tls = 1;
   6556 	    break;
   6557 	case TLS_LEV_MAY:
   6558 	    var_smtpd_enforce_tls = 0;
   6559 	    var_smtpd_use_tls = 1;
   6560 	    break;
   6561 	case TLS_LEV_NONE:
   6562 	    var_smtpd_enforce_tls = var_smtpd_use_tls = 0;
   6563 	    break;
   6564 	}
   6565     }
   6566 
   6567     /*
   6568      * With TLS wrapper mode, we run on a dedicated port and turn on TLS
   6569      * before actually speaking the SMTP protocol. This implies TLS enforce
   6570      * mode.
   6571      *
   6572      * With non-wrapper mode, TLS enforce mode implies that we don't advertise
   6573      * AUTH before the client issues STARTTLS.
   6574      */
   6575     var_smtpd_enforce_tls = var_smtpd_tls_wrappermode || var_smtpd_enforce_tls;
   6576     var_smtpd_tls_auth_only = var_smtpd_tls_auth_only || var_smtpd_enforce_tls;
   6577     var_smtpd_use_tls = var_smtpd_use_tls || var_smtpd_enforce_tls;
   6578 
   6579     /*
   6580      * Keys can only be loaded when running with suitable permissions. When
   6581      * called from "sendmail -bs" this is not the case, so we must not
   6582      * announce STARTTLS support.
   6583      */
   6584     if (getuid() == 0 || getuid() == var_owner_uid) {
   6585 	if (var_smtpd_use_tls) {
   6586 #ifdef USE_TLS
   6587 #ifndef USE_TLSPROXY
   6588 	    TLS_SERVER_INIT_PROPS props;
   6589 	    const char *cert_file;
   6590 	    int     have_server_cert;
   6591 	    int     no_server_cert_ok;
   6592 	    int     require_server_cert;
   6593 
   6594 	    /*
   6595 	     * Can't use anonymous ciphers if we want client certificates.
   6596 	     * Must use anonymous ciphers if we have no certificates.
   6597 	     *
   6598 	     * XXX: Ugh! Too many booleans!
   6599 	     */
   6600 	    ask_client_cert = require_server_cert =
   6601 		(var_smtpd_tls_ask_ccert
   6602 		 || (var_smtpd_enforce_tls && var_smtpd_tls_req_ccert));
   6603 	    if (strcasecmp(var_smtpd_tls_cert_file, "none") == 0) {
   6604 		no_server_cert_ok = 1;
   6605 		cert_file = "";
   6606 	    } else {
   6607 		no_server_cert_ok = 0;
   6608 		cert_file = var_smtpd_tls_cert_file;
   6609 	    }
   6610 	    have_server_cert = *cert_file != 0;
   6611 	    have_server_cert |= *var_smtpd_tls_eccert_file != 0;
   6612 	    have_server_cert |= *var_smtpd_tls_dcert_file != 0;
   6613 
   6614 	    if (*var_smtpd_tls_chain_files != 0) {
   6615 		if (!have_server_cert)
   6616 		    have_server_cert = 1;
   6617 		else
   6618 		    msg_warn("Both %s and one or more of the legacy "
   6619 			     " %s, %s or %s are non-empty; the legacy "
   6620 			     " parameters will be ignored",
   6621 			     VAR_SMTPD_TLS_CHAIN_FILES,
   6622 			     VAR_SMTPD_TLS_CERT_FILE,
   6623 			     VAR_SMTPD_TLS_ECCERT_FILE,
   6624 			     VAR_SMTPD_TLS_DCERT_FILE);
   6625 	    }
   6626 	    /* Some TLS configuration errors are not show stoppers. */
   6627 	    if (!have_server_cert && require_server_cert)
   6628 		msg_warn("Need a server cert to request client certs");
   6629 	    if (!var_smtpd_enforce_tls && var_smtpd_tls_req_ccert)
   6630 		msg_warn("Can't require client certs unless TLS is required");
   6631 	    /* After a show-stopper error, reply with 454 to STARTTLS. */
   6632 	    if (have_server_cert
   6633 		|| (no_server_cert_ok && !require_server_cert)) {
   6634 
   6635 		tls_pre_jail_init(TLS_ROLE_SERVER);
   6636 
   6637 		/*
   6638 		 * Large parameter lists are error-prone, so we emulate a
   6639 		 * language feature that C does not have natively: named
   6640 		 * parameter lists.
   6641 		 */
   6642 		smtpd_tls_ctx =
   6643 		    TLS_SERVER_INIT(&props,
   6644 				    log_param = VAR_SMTPD_TLS_LOGLEVEL,
   6645 				    log_level = var_smtpd_tls_loglevel,
   6646 				    verifydepth = var_smtpd_tls_ccert_vd,
   6647 				    cache_type = TLS_MGR_SCACHE_SMTPD,
   6648 				    set_sessid = var_smtpd_tls_set_sessid,
   6649 				    chain_files = var_smtpd_tls_chain_files,
   6650 				    cert_file = cert_file,
   6651 				    key_file = var_smtpd_tls_key_file,
   6652 				    dcert_file = var_smtpd_tls_dcert_file,
   6653 				    dkey_file = var_smtpd_tls_dkey_file,
   6654 				    eccert_file = var_smtpd_tls_eccert_file,
   6655 				    eckey_file = var_smtpd_tls_eckey_file,
   6656 				    CAfile = var_smtpd_tls_CAfile,
   6657 				    CApath = var_smtpd_tls_CApath,
   6658 				    dh1024_param_file
   6659 				    = var_smtpd_tls_dh1024_param_file,
   6660 				    dh512_param_file
   6661 				    = var_smtpd_tls_dh512_param_file,
   6662 				    eecdh_grade = var_smtpd_tls_eecdh,
   6663 				    protocols = var_smtpd_enforce_tls ?
   6664 				    var_smtpd_tls_mand_proto :
   6665 				    var_smtpd_tls_proto,
   6666 				    ask_ccert = ask_client_cert,
   6667 				    mdalg = var_smtpd_tls_fpt_dgst);
   6668 	    } else {
   6669 		msg_warn("No server certs available. TLS won't be enabled");
   6670 	    }
   6671 #endif						/* USE_TLSPROXY */
   6672 #else
   6673 	    msg_warn("TLS has been selected, but TLS support is not compiled in");
   6674 #endif
   6675 	}
   6676     }
   6677 
   6678     /*
   6679      * flush client.
   6680      */
   6681     flush_init();
   6682 
   6683     /*
   6684      * EHLO keyword filter.
   6685      */
   6686     if (*var_smtpd_ehlo_dis_maps)
   6687 	ehlo_discard_maps = maps_create(VAR_SMTPD_EHLO_DIS_MAPS,
   6688 					var_smtpd_ehlo_dis_maps,
   6689 					DICT_FLAG_LOCK);
   6690 
   6691     /*
   6692      * Per-client Milter support.
   6693      */
   6694     if (*var_smtpd_milter_maps)
   6695 	smtpd_milter_maps = maps_create(VAR_SMTPD_MILTER_MAPS,
   6696 					var_smtpd_milter_maps,
   6697 					DICT_FLAG_LOCK);
   6698 
   6699     /*
   6700      * DNS reply filter.
   6701      */
   6702     if (*var_smtpd_dns_re_filter)
   6703 	dns_rr_filter_compile(VAR_SMTPD_DNS_RE_FILTER,
   6704 			      var_smtpd_dns_re_filter);
   6705 
   6706     /*
   6707      * Reject filter and footer.
   6708      */
   6709     if (*var_smtpd_rej_ftr_maps || *var_smtpd_reject_filter_maps)
   6710 	smtpd_chat_pre_jail_init();
   6711 }
   6712 
   6713 /* post_jail_init - post-jail initialization */
   6714 
   6715 static void post_jail_init(char *unused_name, char **unused_argv)
   6716 {
   6717 
   6718     /*
   6719      * Initialize the receive transparency options: do we want unknown
   6720      * recipient checks, address mapping, header_body_checks?.
   6721      */
   6722     smtpd_input_transp_mask =
   6723     input_transp_mask(VAR_INPUT_TRANSP, var_input_transp);
   6724 
   6725     /*
   6726      * Initialize before-queue filter options: do we want speed-matching
   6727      * support so that the entire message is received before we contact a
   6728      * before-queue content filter?
   6729      */
   6730     if (*var_smtpd_proxy_filt)
   6731 	smtpd_proxy_opts =
   6732 	    smtpd_proxy_parse_opts(VAR_SMTPD_PROXY_OPTS, var_smtpd_proxy_opts);
   6733 
   6734     /*
   6735      * Sanity checks. The queue_minfree value should be at least as large as
   6736      * (process_limit * message_size_limit) but that is unpractical, so we
   6737      * arbitrarily pick a small multiple of the per-message size limit. This
   6738      * helps to avoid many unneeded (re)transmissions.
   6739      */
   6740     if (ENFORCING_SIZE_LIMIT(var_queue_minfree)
   6741 	&& ENFORCING_SIZE_LIMIT(var_message_limit)
   6742 	&& var_queue_minfree / 1.5 < var_message_limit)
   6743 	msg_warn("%s(%lu) should be at least 1.5*%s(%lu)",
   6744 		 VAR_QUEUE_MINFREE, (unsigned long) var_queue_minfree,
   6745 		 VAR_MESSAGE_LIMIT, (unsigned long) var_message_limit);
   6746 
   6747     /*
   6748      * Connection rate management.
   6749      */
   6750     if (var_smtpd_crate_limit || var_smtpd_cconn_limit
   6751 	|| var_smtpd_cmail_limit || var_smtpd_crcpt_limit
   6752 	|| var_smtpd_cntls_limit || var_smtpd_cauth_limit)
   6753 	anvil_clnt = anvil_clnt_create();
   6754 
   6755     /*
   6756      * header_from_format support, for	postmaster notifications.
   6757      */
   6758     smtpd_hfrom_format = hfrom_format_parse(VAR_HFROM_FORMAT, var_hfrom_format);
   6759 }
   6760 
   6761 MAIL_VERSION_STAMP_DECLARE;
   6762 
   6763 /* main - the main program */
   6764 
   6765 int     main(int argc, char **argv)
   6766 {
   6767     static const CONFIG_NINT_TABLE nint_table[] = {
   6768 	VAR_SMTPD_SOFT_ERLIM, DEF_SMTPD_SOFT_ERLIM, &var_smtpd_soft_erlim, 1, 0,
   6769 	VAR_SMTPD_HARD_ERLIM, DEF_SMTPD_HARD_ERLIM, &var_smtpd_hard_erlim, 1, 0,
   6770 	VAR_SMTPD_JUNK_CMD, DEF_SMTPD_JUNK_CMD, &var_smtpd_junk_cmd_limit, 1, 0,
   6771 	VAR_VERIFY_POLL_COUNT, DEF_VERIFY_POLL_COUNT, &var_verify_poll_count, 1, 0,
   6772 	0,
   6773     };
   6774     static const CONFIG_INT_TABLE int_table[] = {
   6775 	VAR_SMTPD_RCPT_LIMIT, DEF_SMTPD_RCPT_LIMIT, &var_smtpd_rcpt_limit, 1, 0,
   6776 	VAR_UNK_CLIENT_CODE, DEF_UNK_CLIENT_CODE, &var_unk_client_code, 0, 0,
   6777 	VAR_BAD_NAME_CODE, DEF_BAD_NAME_CODE, &var_bad_name_code, 0, 0,
   6778 	VAR_UNK_NAME_CODE, DEF_UNK_NAME_CODE, &var_unk_name_code, 0, 0,
   6779 	VAR_UNK_ADDR_CODE, DEF_UNK_ADDR_CODE, &var_unk_addr_code, 0, 0,
   6780 	VAR_RELAY_CODE, DEF_RELAY_CODE, &var_relay_code, 0, 0,
   6781 	VAR_MAPS_RBL_CODE, DEF_MAPS_RBL_CODE, &var_maps_rbl_code, 0, 0,
   6782 	VAR_MAP_REJECT_CODE, DEF_MAP_REJECT_CODE, &var_map_reject_code, 0, 0,
   6783 	VAR_MAP_DEFER_CODE, DEF_MAP_DEFER_CODE, &var_map_defer_code, 0, 0,
   6784 	VAR_REJECT_CODE, DEF_REJECT_CODE, &var_reject_code, 0, 0,
   6785 	VAR_DEFER_CODE, DEF_DEFER_CODE, &var_defer_code, 0, 0,
   6786 	VAR_NON_FQDN_CODE, DEF_NON_FQDN_CODE, &var_non_fqdn_code, 0, 0,
   6787 	VAR_SMTPD_RCPT_OVERLIM, DEF_SMTPD_RCPT_OVERLIM, &var_smtpd_rcpt_overlim, 1, 0,
   6788 	VAR_SMTPD_HIST_THRSH, DEF_SMTPD_HIST_THRSH, &var_smtpd_hist_thrsh, 1, 0,
   6789 	VAR_UNV_FROM_RCODE, DEF_UNV_FROM_RCODE, &var_unv_from_rcode, 200, 599,
   6790 	VAR_UNV_RCPT_RCODE, DEF_UNV_RCPT_RCODE, &var_unv_rcpt_rcode, 200, 599,
   6791 	VAR_UNV_FROM_DCODE, DEF_UNV_FROM_DCODE, &var_unv_from_dcode, 200, 499,
   6792 	VAR_UNV_RCPT_DCODE, DEF_UNV_RCPT_DCODE, &var_unv_rcpt_dcode, 200, 499,
   6793 	VAR_MUL_RCPT_CODE, DEF_MUL_RCPT_CODE, &var_mul_rcpt_code, 0, 0,
   6794 	VAR_LOCAL_RCPT_CODE, DEF_LOCAL_RCPT_CODE, &var_local_rcpt_code, 0, 0,
   6795 	VAR_VIRT_ALIAS_CODE, DEF_VIRT_ALIAS_CODE, &var_virt_alias_code, 0, 0,
   6796 	VAR_VIRT_MAILBOX_CODE, DEF_VIRT_MAILBOX_CODE, &var_virt_mailbox_code, 0, 0,
   6797 	VAR_RELAY_RCPT_CODE, DEF_RELAY_RCPT_CODE, &var_relay_rcpt_code, 0, 0,
   6798 	VAR_PLAINTEXT_CODE, DEF_PLAINTEXT_CODE, &var_plaintext_code, 0, 0,
   6799 	VAR_SMTPD_FORBID_BARE_LF_CODE, DEF_SMTPD_FORBID_BARE_LF_CODE, &var_smtpd_forbid_bare_lf_code, 500, 599,
   6800 	VAR_SMTPD_CRATE_LIMIT, DEF_SMTPD_CRATE_LIMIT, &var_smtpd_crate_limit, 0, 0,
   6801 	VAR_SMTPD_CCONN_LIMIT, DEF_SMTPD_CCONN_LIMIT, &var_smtpd_cconn_limit, 0, 0,
   6802 	VAR_SMTPD_CMAIL_LIMIT, DEF_SMTPD_CMAIL_LIMIT, &var_smtpd_cmail_limit, 0, 0,
   6803 	VAR_SMTPD_CRCPT_LIMIT, DEF_SMTPD_CRCPT_LIMIT, &var_smtpd_crcpt_limit, 0, 0,
   6804 	VAR_SMTPD_CNTLS_LIMIT, DEF_SMTPD_CNTLS_LIMIT, &var_smtpd_cntls_limit, 0, 0,
   6805 	VAR_SMTPD_CAUTH_LIMIT, DEF_SMTPD_CAUTH_LIMIT, &var_smtpd_cauth_limit, 0, 0,
   6806 	VAR_SMTPD_CIPV4_PREFIX, DEF_SMTPD_CIPV4_PREFIX, &var_smtpd_cipv4_prefix, 0, MAX_SMTPD_CIPV4_PREFIX,
   6807 	VAR_SMTPD_CIPV6_PREFIX, DEF_SMTPD_CIPV6_PREFIX, &var_smtpd_cipv6_prefix, 0, MAX_SMTPD_CIPV6_PREFIX,
   6808 #ifdef USE_TLS
   6809 	VAR_SMTPD_TLS_CCERT_VD, DEF_SMTPD_TLS_CCERT_VD, &var_smtpd_tls_ccert_vd, 0, 0,
   6810 #endif
   6811 	VAR_SMTPD_SASL_RESP_LIMIT, DEF_SMTPD_SASL_RESP_LIMIT, &var_smtpd_sasl_resp_limit, DEF_SMTPD_SASL_RESP_LIMIT, 0,
   6812 	VAR_SMTPD_POLICY_REQ_LIMIT, DEF_SMTPD_POLICY_REQ_LIMIT, &var_smtpd_policy_req_limit, 0, 0,
   6813 	VAR_SMTPD_POLICY_TRY_LIMIT, DEF_SMTPD_POLICY_TRY_LIMIT, &var_smtpd_policy_try_limit, 1, 0,
   6814 	VAR_SMTPD_MIN_DATA_RATE, DEF_SMTPD_MIN_DATA_RATE, &var_smtpd_min_data_rate, 1, 0,
   6815 	0,
   6816     };
   6817     static const CONFIG_LONG_TABLE long_table[] = {
   6818 	VAR_QUEUE_MINFREE, DEF_QUEUE_MINFREE, &var_queue_minfree, 0, 0,
   6819 	0,
   6820     };
   6821     static const CONFIG_TIME_TABLE time_table[] = {
   6822 	VAR_SMTPD_TMOUT, DEF_SMTPD_TMOUT, &var_smtpd_tmout, 1, 0,
   6823 	VAR_SMTPD_ERR_SLEEP, DEF_SMTPD_ERR_SLEEP, &var_smtpd_err_sleep, 0, 0,
   6824 	VAR_SMTPD_PROXY_TMOUT, DEF_SMTPD_PROXY_TMOUT, &var_smtpd_proxy_tmout, 1, 0,
   6825 	VAR_VERIFY_POLL_DELAY, DEF_VERIFY_POLL_DELAY, &var_verify_poll_delay, 1, 0,
   6826 	VAR_SMTPD_POLICY_TMOUT, DEF_SMTPD_POLICY_TMOUT, &var_smtpd_policy_tmout, 1, 0,
   6827 	VAR_SMTPD_POLICY_IDLE, DEF_SMTPD_POLICY_IDLE, &var_smtpd_policy_idle, 1, 0,
   6828 	VAR_SMTPD_POLICY_TTL, DEF_SMTPD_POLICY_TTL, &var_smtpd_policy_ttl, 1, 0,
   6829 #ifdef USE_TLS
   6830 	VAR_SMTPD_STARTTLS_TMOUT, DEF_SMTPD_STARTTLS_TMOUT, &var_smtpd_starttls_tmout, 1, 0,
   6831 #endif
   6832 	VAR_MILT_CONN_TIME, DEF_MILT_CONN_TIME, &var_milt_conn_time, 1, 0,
   6833 	VAR_MILT_CMD_TIME, DEF_MILT_CMD_TIME, &var_milt_cmd_time, 1, 0,
   6834 	VAR_MILT_MSG_TIME, DEF_MILT_MSG_TIME, &var_milt_msg_time, 1, 0,
   6835 	VAR_VERIFY_SENDER_TTL, DEF_VERIFY_SENDER_TTL, &var_verify_sender_ttl, 0, 0,
   6836 	VAR_SMTPD_UPROXY_TMOUT, DEF_SMTPD_UPROXY_TMOUT, &var_smtpd_uproxy_tmout, 1, 0,
   6837 	VAR_SMTPD_POLICY_TRY_DELAY, DEF_SMTPD_POLICY_TRY_DELAY, &var_smtpd_policy_try_delay, 1, 0,
   6838 	0,
   6839     };
   6840     static const CONFIG_BOOL_TABLE bool_table[] = {
   6841 	VAR_HELO_REQUIRED, DEF_HELO_REQUIRED, &var_helo_required,
   6842 	VAR_SMTPD_DELAY_REJECT, DEF_SMTPD_DELAY_REJECT, &var_smtpd_delay_reject,
   6843 	VAR_STRICT_RFC821_ENV, DEF_STRICT_RFC821_ENV, &var_strict_rfc821_env,
   6844 	VAR_DISABLE_VRFY_CMD, DEF_DISABLE_VRFY_CMD, &var_disable_vrfy_cmd,
   6845 	VAR_ALLOW_UNTRUST_ROUTE, DEF_ALLOW_UNTRUST_ROUTE, &var_allow_untrust_route,
   6846 	VAR_SMTPD_SASL_ENABLE, DEF_SMTPD_SASL_ENABLE, &var_smtpd_sasl_enable,
   6847 	VAR_SMTPD_SASL_AUTH_HDR, DEF_SMTPD_SASL_AUTH_HDR, &var_smtpd_sasl_auth_hdr,
   6848 	VAR_BROKEN_AUTH_CLNTS, DEF_BROKEN_AUTH_CLNTS, &var_broken_auth_clients,
   6849 	VAR_SHOW_UNK_RCPT_TABLE, DEF_SHOW_UNK_RCPT_TABLE, &var_show_unk_rcpt_table,
   6850 	VAR_SMTPD_REJ_UNL_FROM, DEF_SMTPD_REJ_UNL_FROM, &var_smtpd_rej_unl_from,
   6851 	VAR_SMTPD_REJ_UNL_RCPT, DEF_SMTPD_REJ_UNL_RCPT, &var_smtpd_rej_unl_rcpt,
   6852 	VAR_SMTPD_USE_TLS, DEF_SMTPD_USE_TLS, &var_smtpd_use_tls,
   6853 	VAR_SMTPD_ENFORCE_TLS, DEF_SMTPD_ENFORCE_TLS, &var_smtpd_enforce_tls,
   6854 	VAR_SMTPD_TLS_WRAPPER, DEF_SMTPD_TLS_WRAPPER, &var_smtpd_tls_wrappermode,
   6855 	VAR_SMTPD_TLS_AUTH_ONLY, DEF_SMTPD_TLS_AUTH_ONLY, &var_smtpd_tls_auth_only,
   6856 #ifdef USE_TLS
   6857 	VAR_SMTPD_TLS_ACERT, DEF_SMTPD_TLS_ACERT, &var_smtpd_tls_ask_ccert,
   6858 	VAR_SMTPD_TLS_RCERT, DEF_SMTPD_TLS_RCERT, &var_smtpd_tls_req_ccert,
   6859 	VAR_SMTPD_TLS_ENABLE_RPK, DEF_SMTPD_TLS_ENABLE_RPK, &var_smtpd_tls_enable_rpk,
   6860 	VAR_SMTPD_TLS_RECHEAD, DEF_SMTPD_TLS_RECHEAD, &var_smtpd_tls_received_header,
   6861 	VAR_SMTPD_TLS_SET_SESSID, DEF_SMTPD_TLS_SET_SESSID, &var_smtpd_tls_set_sessid,
   6862 #endif
   6863 	VAR_SMTPD_PEERNAME_LOOKUP, DEF_SMTPD_PEERNAME_LOOKUP, &var_smtpd_peername_lookup,
   6864 	VAR_SMTPD_DELAY_OPEN, DEF_SMTPD_DELAY_OPEN, &var_smtpd_delay_open,
   6865 	VAR_SMTPD_CLIENT_PORT_LOG, DEF_SMTPD_CLIENT_PORT_LOG, &var_smtpd_client_port_log,
   6866 	VAR_SMTPD_FORBID_UNAUTH_PIPE, DEF_SMTPD_FORBID_UNAUTH_PIPE, &var_smtpd_forbid_unauth_pipe,
   6867 	0,
   6868     };
   6869     static const CONFIG_NBOOL_TABLE nbool_table[] = {
   6870 	VAR_RELAY_BEFORE_RCPT_CHECKS, DEF_RELAY_BEFORE_RCPT_CHECKS, &var_relay_before_rcpt_checks,
   6871 	VAR_SMTPD_REQ_DEADLINE, DEF_SMTPD_REQ_DEADLINE, &var_smtpd_req_deadline,
   6872 	VAR_SMTPD_HIDE_CLIENT_SESSION, DEF_SMTPD_HIDE_CLIENT_SESSION, &var_smtpd_hide_client_session,
   6873 	VAR_REQTLS_ESMTP_HDR, DEF_REQTLS_ESMTP_HDR, &var_reqtls_esmtp_hdr,
   6874 	0,
   6875     };
   6876     static const CONFIG_STR_TABLE str_table[] = {
   6877 	VAR_SMTPD_BANNER, DEF_SMTPD_BANNER, &var_smtpd_banner, 1, 0,
   6878 	VAR_NOTIFY_CLASSES, DEF_NOTIFY_CLASSES, &var_notify_classes, 0, 0,
   6879 	VAR_CLIENT_CHECKS, DEF_CLIENT_CHECKS, &var_client_checks, 0, 0,
   6880 	VAR_HELO_CHECKS, DEF_HELO_CHECKS, &var_helo_checks, 0, 0,
   6881 	VAR_MAIL_CHECKS, DEF_MAIL_CHECKS, &var_mail_checks, 0, 0,
   6882 	VAR_RELAY_CHECKS, DEF_RELAY_CHECKS, &var_relay_checks, 0, 0,
   6883 	VAR_RCPT_CHECKS, DEF_RCPT_CHECKS, &var_rcpt_checks, 0, 0,
   6884 	VAR_ETRN_CHECKS, DEF_ETRN_CHECKS, &var_etrn_checks, 0, 0,
   6885 	VAR_DATA_CHECKS, DEF_DATA_CHECKS, &var_data_checks, 0, 0,
   6886 	VAR_EOD_CHECKS, DEF_EOD_CHECKS, &var_eod_checks, 0, 0,
   6887 	VAR_MAPS_RBL_DOMAINS, DEF_MAPS_RBL_DOMAINS, &var_maps_rbl_domains, 0, 0,
   6888 	VAR_RBL_REPLY_MAPS, DEF_RBL_REPLY_MAPS, &var_rbl_reply_maps, 0, 0,
   6889 	VAR_BOUNCE_RCPT, DEF_BOUNCE_RCPT, &var_bounce_rcpt, 1, 0,
   6890 	VAR_ERROR_RCPT, DEF_ERROR_RCPT, &var_error_rcpt, 1, 0,
   6891 	VAR_REST_CLASSES, DEF_REST_CLASSES, &var_rest_classes, 0, 0,
   6892 	VAR_CANONICAL_MAPS, DEF_CANONICAL_MAPS, &var_canonical_maps, 0, 0,
   6893 	VAR_SEND_CANON_MAPS, DEF_SEND_CANON_MAPS, &var_send_canon_maps, 0, 0,
   6894 	VAR_RCPT_CANON_MAPS, DEF_RCPT_CANON_MAPS, &var_rcpt_canon_maps, 0, 0,
   6895 	VAR_VIRT_ALIAS_MAPS, DEF_VIRT_ALIAS_MAPS, &var_virt_alias_maps, 0, 0,
   6896 	VAR_VIRT_MAILBOX_MAPS, DEF_VIRT_MAILBOX_MAPS, &var_virt_mailbox_maps, 0, 0,
   6897 	VAR_ALIAS_MAPS, DEF_ALIAS_MAPS, &var_alias_maps, 0, 0,
   6898 	VAR_LOCAL_RCPT_MAPS, DEF_LOCAL_RCPT_MAPS, &var_local_rcpt_maps, 0, 0,
   6899 	VAR_SMTPD_SASL_OPTS, DEF_SMTPD_SASL_OPTS, &var_smtpd_sasl_opts, 0, 0,
   6900 	VAR_SMTPD_SASL_PATH, DEF_SMTPD_SASL_PATH, &var_smtpd_sasl_path, 1, 0,
   6901 	VAR_SMTPD_SASL_SERVICE, DEF_SMTPD_SASL_SERVICE, &var_smtpd_sasl_service, 1, 0,
   6902 	VAR_CYRUS_CONF_PATH, DEF_CYRUS_CONF_PATH, &var_cyrus_conf_path, 0, 0,
   6903 	VAR_SMTPD_SASL_REALM, DEF_SMTPD_SASL_REALM, &var_smtpd_sasl_realm, 0, 0,
   6904 	VAR_SMTPD_SASL_EXCEPTIONS_NETWORKS, DEF_SMTPD_SASL_EXCEPTIONS_NETWORKS, &var_smtpd_sasl_exceptions_networks, 0, 0,
   6905 	VAR_FILTER_XPORT, DEF_FILTER_XPORT, &var_filter_xport, 0, 0,
   6906 	VAR_PERM_MX_NETWORKS, DEF_PERM_MX_NETWORKS, &var_perm_mx_networks, 0, 0,
   6907 	VAR_SMTPD_SND_AUTH_MAPS, DEF_SMTPD_SND_AUTH_MAPS, &var_smtpd_snd_auth_maps, 0, 0,
   6908 	VAR_SMTPD_NOOP_CMDS, DEF_SMTPD_NOOP_CMDS, &var_smtpd_noop_cmds, 0, 0,
   6909 	VAR_SMTPD_FORBID_CMDS, DEF_SMTPD_FORBID_CMDS, &var_smtpd_forbid_cmds, 0, 0,
   6910 	VAR_SMTPD_NULL_KEY, DEF_SMTPD_NULL_KEY, &var_smtpd_null_key, 0, 0,
   6911 	VAR_RELAY_RCPT_MAPS, DEF_RELAY_RCPT_MAPS, &var_relay_rcpt_maps, 0, 0,
   6912 	VAR_VERIFY_SENDER, DEF_VERIFY_SENDER, &var_verify_sender, 0, 0,
   6913 	VAR_VERP_CLIENTS, DEF_VERP_CLIENTS, &var_verp_clients, 0, 0,
   6914 	VAR_SMTPD_PROXY_FILT, DEF_SMTPD_PROXY_FILT, &var_smtpd_proxy_filt, 0, 0,
   6915 	VAR_SMTPD_PROXY_EHLO, DEF_SMTPD_PROXY_EHLO, &var_smtpd_proxy_ehlo, 0, 0,
   6916 	VAR_SMTPD_PROXY_OPTS, DEF_SMTPD_PROXY_OPTS, &var_smtpd_proxy_opts, 0, 0,
   6917 	VAR_INPUT_TRANSP, DEF_INPUT_TRANSP, &var_input_transp, 0, 0,
   6918 	VAR_XCLIENT_HOSTS, DEF_XCLIENT_HOSTS, &var_xclient_hosts, 0, 0,
   6919 	VAR_XFORWARD_HOSTS, DEF_XFORWARD_HOSTS, &var_xforward_hosts, 0, 0,
   6920 	VAR_SMTPD_HOGGERS, DEF_SMTPD_HOGGERS, &var_smtpd_hoggers, 0, 0,
   6921 	VAR_LOC_RWR_CLIENTS, DEF_LOC_RWR_CLIENTS, &var_local_rwr_clients, 0, 0,
   6922 	VAR_SMTPD_EHLO_DIS_WORDS, DEF_SMTPD_EHLO_DIS_WORDS, &var_smtpd_ehlo_dis_words, 0, 0,
   6923 	VAR_SMTPD_EHLO_DIS_MAPS, DEF_SMTPD_EHLO_DIS_MAPS, &var_smtpd_ehlo_dis_maps, 0, 0,
   6924 #ifdef USE_TLS
   6925 	VAR_RELAY_CCERTS, DEF_RELAY_CCERTS, &var_smtpd_relay_ccerts, 0, 0,
   6926 	VAR_SMTPD_SASL_TLS_OPTS, DEF_SMTPD_SASL_TLS_OPTS, &var_smtpd_sasl_tls_opts, 0, 0,
   6927 	VAR_SMTPD_TLS_CHAIN_FILES, DEF_SMTPD_TLS_CHAIN_FILES, &var_smtpd_tls_chain_files, 0, 0,
   6928 	VAR_SMTPD_TLS_CERT_FILE, DEF_SMTPD_TLS_CERT_FILE, &var_smtpd_tls_cert_file, 0, 0,
   6929 	VAR_SMTPD_TLS_KEY_FILE, DEF_SMTPD_TLS_KEY_FILE, &var_smtpd_tls_key_file, 0, 0,
   6930 	VAR_SMTPD_TLS_DCERT_FILE, DEF_SMTPD_TLS_DCERT_FILE, &var_smtpd_tls_dcert_file, 0, 0,
   6931 	VAR_SMTPD_TLS_DKEY_FILE, DEF_SMTPD_TLS_DKEY_FILE, &var_smtpd_tls_dkey_file, 0, 0,
   6932 	VAR_SMTPD_TLS_ECCERT_FILE, DEF_SMTPD_TLS_ECCERT_FILE, &var_smtpd_tls_eccert_file, 0, 0,
   6933 	VAR_SMTPD_TLS_ECKEY_FILE, DEF_SMTPD_TLS_ECKEY_FILE, &var_smtpd_tls_eckey_file, 0, 0,
   6934 	VAR_SMTPD_TLS_CA_FILE, DEF_SMTPD_TLS_CA_FILE, &var_smtpd_tls_CAfile, 0, 0,
   6935 	VAR_SMTPD_TLS_CA_PATH, DEF_SMTPD_TLS_CA_PATH, &var_smtpd_tls_CApath, 0, 0,
   6936 	VAR_SMTPD_TLS_CIPH, DEF_SMTPD_TLS_CIPH, &var_smtpd_tls_ciph, 1, 0,
   6937 	VAR_SMTPD_TLS_MAND_CIPH, DEF_SMTPD_TLS_MAND_CIPH, &var_smtpd_tls_mand_ciph, 1, 0,
   6938 	VAR_SMTPD_TLS_EXCL_CIPH, DEF_SMTPD_TLS_EXCL_CIPH, &var_smtpd_tls_excl_ciph, 0, 0,
   6939 	VAR_SMTPD_TLS_MAND_EXCL, DEF_SMTPD_TLS_MAND_EXCL, &var_smtpd_tls_mand_excl, 0, 0,
   6940 	VAR_SMTPD_TLS_PROTO, DEF_SMTPD_TLS_PROTO, &var_smtpd_tls_proto, 0, 0,
   6941 	VAR_SMTPD_TLS_MAND_PROTO, DEF_SMTPD_TLS_MAND_PROTO, &var_smtpd_tls_mand_proto, 0, 0,
   6942 	VAR_SMTPD_TLS_512_FILE, DEF_SMTPD_TLS_512_FILE, &var_smtpd_tls_dh512_param_file, 0, 0,
   6943 	VAR_SMTPD_TLS_1024_FILE, DEF_SMTPD_TLS_1024_FILE, &var_smtpd_tls_dh1024_param_file, 0, 0,
   6944 	VAR_SMTPD_TLS_EECDH, DEF_SMTPD_TLS_EECDH, &var_smtpd_tls_eecdh, 1, 0,
   6945 	VAR_SMTPD_TLS_FPT_DGST, DEF_SMTPD_TLS_FPT_DGST, &var_smtpd_tls_fpt_dgst, 1, 0,
   6946 	VAR_SMTPD_TLS_LOGLEVEL, DEF_SMTPD_TLS_LOGLEVEL, &var_smtpd_tls_loglevel, 0, 0,
   6947 #endif
   6948 	VAR_SMTPD_TLS_LEVEL, DEF_SMTPD_TLS_LEVEL, &var_smtpd_tls_level, 0, 0,
   6949 	VAR_SMTPD_SASL_TYPE, DEF_SMTPD_SASL_TYPE, &var_smtpd_sasl_type, 1, 0,
   6950 	VAR_SMTPD_SASL_MECH_FILTER, DEF_SMTPD_SASL_MECH_FILTER, &var_smtpd_sasl_mech_filter, 0, 0,
   6951 	VAR_SMTPD_MILTERS, DEF_SMTPD_MILTERS, &var_smtpd_milters, 0, 0,
   6952 	VAR_MILT_CONN_MACROS, DEF_MILT_CONN_MACROS, &var_milt_conn_macros, 0, 0,
   6953 	VAR_MILT_HELO_MACROS, DEF_MILT_HELO_MACROS, &var_milt_helo_macros, 0, 0,
   6954 	VAR_MILT_MAIL_MACROS, DEF_MILT_MAIL_MACROS, &var_milt_mail_macros, 0, 0,
   6955 	VAR_MILT_RCPT_MACROS, DEF_MILT_RCPT_MACROS, &var_milt_rcpt_macros, 0, 0,
   6956 	VAR_MILT_DATA_MACROS, DEF_MILT_DATA_MACROS, &var_milt_data_macros, 0, 0,
   6957 	VAR_MILT_EOH_MACROS, DEF_MILT_EOH_MACROS, &var_milt_eoh_macros, 0, 0,
   6958 	VAR_MILT_EOD_MACROS, DEF_MILT_EOD_MACROS, &var_milt_eod_macros, 0, 0,
   6959 	VAR_MILT_UNK_MACROS, DEF_MILT_UNK_MACROS, &var_milt_unk_macros, 0, 0,
   6960 	VAR_MILT_PROTOCOL, DEF_MILT_PROTOCOL, &var_milt_protocol, 1, 0,
   6961 	VAR_MILT_DEF_ACTION, DEF_MILT_DEF_ACTION, &var_milt_def_action, 1, 0,
   6962 	VAR_MILT_DAEMON_NAME, DEF_MILT_DAEMON_NAME, &var_milt_daemon_name, 1, 0,
   6963 	VAR_MILT_V, DEF_MILT_V, &var_milt_v, 1, 0,
   6964 	VAR_MILT_MACRO_DEFLTS, DEF_MILT_MACRO_DEFLTS, &var_milt_macro_deflts, 0, 0,
   6965 	VAR_SMTPD_MILTER_MAPS, DEF_SMTPD_MILTER_MAPS, &var_smtpd_milter_maps, 0, 0,
   6966 	VAR_STRESS, DEF_STRESS, &var_stress, 0, 0,
   6967 	VAR_UNV_FROM_WHY, DEF_UNV_FROM_WHY, &var_unv_from_why, 0, 0,
   6968 	VAR_UNV_RCPT_WHY, DEF_UNV_RCPT_WHY, &var_unv_rcpt_why, 0, 0,
   6969 	VAR_REJECT_TMPF_ACT, DEF_REJECT_TMPF_ACT, &var_reject_tmpf_act, 1, 0,
   6970 	VAR_UNK_NAME_TF_ACT, DEF_UNK_NAME_TF_ACT, &var_unk_name_tf_act, 1, 0,
   6971 	VAR_UNK_ADDR_TF_ACT, DEF_UNK_ADDR_TF_ACT, &var_unk_addr_tf_act, 1, 0,
   6972 	VAR_UNV_RCPT_TF_ACT, DEF_UNV_RCPT_TF_ACT, &var_unv_rcpt_tf_act, 1, 0,
   6973 	VAR_UNV_FROM_TF_ACT, DEF_UNV_FROM_TF_ACT, &var_unv_from_tf_act, 1, 0,
   6974 	VAR_SMTPD_CMD_FILTER, DEF_SMTPD_CMD_FILTER, &var_smtpd_cmd_filter, 0, 0,
   6975 #ifdef USE_TLSPROXY
   6976 	VAR_TLSPROXY_SERVICE, DEF_TLSPROXY_SERVICE, &var_tlsproxy_service, 1, 0,
   6977 #endif
   6978 	VAR_SMTPD_ACL_PERM_LOG, DEF_SMTPD_ACL_PERM_LOG, &var_smtpd_acl_perm_log, 0, 0,
   6979 	VAR_SMTPD_UPROXY_PROTO, DEF_SMTPD_UPROXY_PROTO, &var_smtpd_uproxy_proto, 0, 0,
   6980 	VAR_SMTPD_POLICY_DEF_ACTION, DEF_SMTPD_POLICY_DEF_ACTION, &var_smtpd_policy_def_action, 1, 0,
   6981 	VAR_SMTPD_POLICY_CONTEXT, DEF_SMTPD_POLICY_CONTEXT, &var_smtpd_policy_context, 0, 0,
   6982 	VAR_SMTPD_DNS_RE_FILTER, DEF_SMTPD_DNS_RE_FILTER, &var_smtpd_dns_re_filter, 0, 0,
   6983 	VAR_SMTPD_REJ_FTR_MAPS, DEF_SMTPD_REJ_FTR_MAPS, &var_smtpd_rej_ftr_maps, 0, 0,
   6984 	VAR_SMTPD_REJECT_FILTER_MAPS, DEF_SMTPD_REJECT_FILTER_MAPS, &var_smtpd_reject_filter_maps, 0, 0,
   6985 	VAR_HFROM_FORMAT, DEF_HFROM_FORMAT, &var_hfrom_format, 1, 0,
   6986 	VAR_SMTPD_FORBID_BARE_LF_EXCL, DEF_SMTPD_FORBID_BARE_LF_EXCL, &var_smtpd_forbid_bare_lf_excl, 0, 0,
   6987 	VAR_SMTPD_FORBID_BARE_LF, DEF_SMTPD_FORBID_BARE_LF, &var_smtpd_forbid_bare_lf, 1, 0,
   6988 	0,
   6989     };
   6990     static const CONFIG_RAW_TABLE raw_table[] = {
   6991 	VAR_SMTPD_EXP_FILTER, DEF_SMTPD_EXP_FILTER, &var_smtpd_exp_filter, 1, 0,
   6992 	VAR_DEF_RBL_REPLY, DEF_DEF_RBL_REPLY, &var_def_rbl_reply, 1, 0,
   6993 	VAR_SMTPD_REJ_FOOTER, DEF_SMTPD_REJ_FOOTER, &var_smtpd_rej_footer, 0, 0,
   6994 	0,
   6995     };
   6996 
   6997     /*
   6998      * Fingerprint executables and core dumps.
   6999      */
   7000     MAIL_VERSION_STAMP_ALLOCATE;
   7001 
   7002     /*
   7003      * Pass control to the single-threaded service skeleton.
   7004      */
   7005     single_server_main(argc, argv, smtpd_service,
   7006 		       CA_MAIL_SERVER_NINT_TABLE(nint_table),
   7007 		       CA_MAIL_SERVER_INT_TABLE(int_table),
   7008 		       CA_MAIL_SERVER_LONG_TABLE(long_table),
   7009 		       CA_MAIL_SERVER_STR_TABLE(str_table),
   7010 		       CA_MAIL_SERVER_RAW_TABLE(raw_table),
   7011 		       CA_MAIL_SERVER_BOOL_TABLE(bool_table),
   7012 		       CA_MAIL_SERVER_NBOOL_TABLE(nbool_table),
   7013 		       CA_MAIL_SERVER_TIME_TABLE(time_table),
   7014 		       CA_MAIL_SERVER_PRE_INIT(pre_jail_init),
   7015 		       CA_MAIL_SERVER_PRE_ACCEPT(pre_accept),
   7016 		       CA_MAIL_SERVER_POST_INIT(post_jail_init),
   7017 		       0);
   7018 }
   7019