xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtp/smtp.c (revision 62f324d0121177eaf2e0384f92fd9ca2a751c795)
1 /*	$NetBSD: smtp.c,v 1.6 2013/01/02 19:18:35 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	smtp 8
6 /* SUMMARY
7 /*	Postfix SMTP+LMTP client
8 /* SYNOPSIS
9 /*	\fBsmtp\fR [generic Postfix daemon options]
10 /* DESCRIPTION
11 /*	The Postfix SMTP+LMTP client implements the SMTP and LMTP mail
12 /*	delivery protocols. It processes message delivery requests from
13 /*	the queue manager. Each request specifies a queue file, a sender
14 /*	address, a domain or host to deliver to, and recipient information.
15 /*	This program expects to be run from the \fBmaster\fR(8) process
16 /*	manager.
17 /*
18 /*	The SMTP+LMTP client updates the queue file and marks recipients
19 /*	as finished, or it informs the queue manager that delivery should
20 /*	be tried again at a later time. Delivery status reports are sent
21 /*	to the \fBbounce\fR(8), \fBdefer\fR(8) or \fBtrace\fR(8) daemon as
22 /*	appropriate.
23 /*
24 /*	The SMTP+LMTP client looks up a list of mail exchanger addresses for
25 /*	the destination host, sorts the list by preference, and connects
26 /*	to each listed address until it finds a server that responds.
27 /*
28 /*	When a server is not reachable, or when mail delivery fails due
29 /*	to a recoverable error condition, the SMTP+LMTP client will try to
30 /*	deliver the mail to an alternate host.
31 /*
32 /*	After a successful mail transaction, a connection may be saved
33 /*	to the \fBscache\fR(8) connection cache server, so that it
34 /*	may be used by any SMTP+LMTP client for a subsequent transaction.
35 /*
36 /*	By default, connection caching is enabled temporarily for
37 /*	destinations that have a high volume of mail in the active
38 /*	queue. Connection caching can be enabled permanently for
39 /*	specific destinations.
40 /* SMTP DESTINATION SYNTAX
41 /* .ad
42 /* .fi
43 /*	SMTP destinations have the following form:
44 /* .IP \fIdomainname\fR
45 /* .IP \fIdomainname\fR:\fIport\fR
46 /*	Look up the mail exchangers for the specified domain, and
47 /*	connect to the specified port (default: \fBsmtp\fR).
48 /* .IP [\fIhostname\fR]
49 /* .IP [\fIhostname\fR]:\fIport\fR
50 /*	Look up the address(es) of the specified host, and connect to
51 /*	the specified port (default: \fBsmtp\fR).
52 /* .IP [\fIaddress\fR]
53 /* .IP [\fIaddress\fR]:\fIport\fR
54 /*	Connect to the host at the specified address, and connect
55 /*	to the specified port (default: \fBsmtp\fR). An IPv6 address
56 /*	must be formatted as [\fBipv6\fR:\fIaddress\fR].
57 /* LMTP DESTINATION SYNTAX
58 /* .ad
59 /* .fi
60 /*      LMTP destinations have the following form:
61 /* .IP \fBunix\fR:\fIpathname\fR
62 /*      Connect to the local UNIX-domain server that is bound to the specified
63 /*      \fIpathname\fR. If the process runs chrooted, an absolute pathname
64 /*      is interpreted relative to the Postfix queue directory.
65 /* .IP \fBinet\fR:\fIhostname\fR
66 /* .IP \fBinet\fB:\fIhostname\fR:\fIport\fR
67 /* .IP \fBinet\fR:[\fIaddress\fR]
68 /* .IP \fBinet\fR:[\fIaddress\fR]:\fIport\fR
69 /*      Connect to the specified TCP port on the specified local or
70 /*      remote host. If no port is specified, connect to the port defined as
71 /*      \fBlmtp\fR in \fBservices\fR(4).
72 /*      If no such service is found, the \fBlmtp_tcp_port\fR configuration
73 /*      parameter (default value of 24) will be used.
74 /*	An IPv6 address must be formatted as [\fBipv6\fR:\fIaddress\fR].
75 /* .PP
76 /* SECURITY
77 /* .ad
78 /* .fi
79 /*	The SMTP+LMTP client is moderately security-sensitive. It
80 /*	talks to SMTP or LMTP servers and to DNS servers on the
81 /*	network. The SMTP+LMTP client can be run chrooted at fixed
82 /*	low privilege.
83 /* STANDARDS
84 /*	RFC 821 (SMTP protocol)
85 /*	RFC 822 (ARPA Internet Text Messages)
86 /*	RFC 1651 (SMTP service extensions)
87 /*	RFC 1652 (8bit-MIME transport)
88 /*	RFC 1870 (Message Size Declaration)
89 /*	RFC 2033 (LMTP protocol)
90 /*	RFC 2034 (SMTP Enhanced Error Codes)
91 /*	RFC 2045 (MIME: Format of Internet Message Bodies)
92 /*	RFC 2046 (MIME: Media Types)
93 /*	RFC 2554 (AUTH command)
94 /*	RFC 2821 (SMTP protocol)
95 /*	RFC 2920 (SMTP Pipelining)
96 /*	RFC 3207 (STARTTLS command)
97 /*	RFC 3461 (SMTP DSN Extension)
98 /*	RFC 3463 (Enhanced Status Codes)
99 /*	RFC 4954 (AUTH command)
100 /* DIAGNOSTICS
101 /*	Problems and transactions are logged to \fBsyslogd\fR(8).
102 /*	Corrupted message files are marked so that the queue manager can
103 /*	move them to the \fBcorrupt\fR queue for further inspection.
104 /*
105 /*	Depending on the setting of the \fBnotify_classes\fR parameter,
106 /*	the postmaster is notified of bounces, protocol problems, and of
107 /*	other trouble.
108 /* BUGS
109 /*	SMTP and LMTP connection caching does not work with TLS. The necessary
110 /*	support for TLS object passivation and re-activation does not
111 /*	exist without closing the session, which defeats the purpose.
112 /*
113 /*	SMTP and LMTP connection caching assumes that SASL credentials
114 /*	are valid for all destinations that map onto the same IP
115 /*	address and TCP port.
116 /* CONFIGURATION PARAMETERS
117 /* .ad
118 /* .fi
119 /*	Before Postfix version 2.3, the LMTP client is a separate
120 /*	program that implements only a subset of the functionality
121 /*	available with SMTP: there is no support for TLS, and
122 /*	connections are cached in-process, making it ineffective
123 /*	when the client is used for multiple domains.
124 /*
125 /*	Most smtp_\fIxxx\fR configuration parameters have an
126 /*	lmtp_\fIxxx\fR "mirror" parameter for the equivalent LMTP
127 /*	feature. This document describes only those LMTP-related
128 /*	parameters that aren't simply "mirror" parameters.
129 /*
130 /*	Changes to \fBmain.cf\fR are picked up automatically, as \fBsmtp\fR(8)
131 /*	processes run for only a limited amount of time. Use the command
132 /*	"\fBpostfix reload\fR" to speed up a change.
133 /*
134 /*	The text below provides only a parameter summary. See
135 /*	\fBpostconf\fR(5) for more details including examples.
136 /* COMPATIBILITY CONTROLS
137 /* .ad
138 /* .fi
139 /* .IP "\fBignore_mx_lookup_error (no)\fR"
140 /*	Ignore DNS MX lookups that produce no response.
141 /* .IP "\fBsmtp_always_send_ehlo (yes)\fR"
142 /*	Always send EHLO at the start of an SMTP session.
143 /* .IP "\fBsmtp_never_send_ehlo (no)\fR"
144 /*	Never send EHLO at the start of an SMTP session.
145 /* .IP "\fBsmtp_defer_if_no_mx_address_found (no)\fR"
146 /*	Defer mail delivery when no MX record resolves to an IP address.
147 /* .IP "\fBsmtp_line_length_limit (998)\fR"
148 /*	The maximal length of message header and body lines that Postfix
149 /*	will send via SMTP.
150 /* .IP "\fBsmtp_pix_workaround_delay_time (10s)\fR"
151 /*	How long the Postfix SMTP client pauses before sending
152 /*	".<CR><LF>" in order to work around the PIX firewall
153 /*	"<CR><LF>.<CR><LF>" bug.
154 /* .IP "\fBsmtp_pix_workaround_threshold_time (500s)\fR"
155 /*	How long a message must be queued before the Postfix SMTP client
156 /*	turns on the PIX firewall "<CR><LF>.<CR><LF>"
157 /*	bug workaround for delivery through firewalls with "smtp fixup"
158 /*	mode turned on.
159 /* .IP "\fBsmtp_pix_workarounds (disable_esmtp, delay_dotcrlf)\fR"
160 /*	A list that specifies zero or more workarounds for CISCO PIX
161 /*	firewall bugs.
162 /* .IP "\fBsmtp_pix_workaround_maps (empty)\fR"
163 /*	Lookup tables, indexed by the remote SMTP server address, with
164 /*	per-destination workarounds for CISCO PIX firewall bugs.
165 /* .IP "\fBsmtp_quote_rfc821_envelope (yes)\fR"
166 /*	Quote addresses in Postfix SMTP client MAIL FROM and RCPT TO commands
167 /*	as required
168 /*	by RFC 2821.
169 /* .IP "\fBsmtp_reply_filter (empty)\fR"
170 /*	A mechanism to transform replies from remote SMTP servers one
171 /*	line at a time.
172 /* .IP "\fBsmtp_skip_5xx_greeting (yes)\fR"
173 /*	Skip remote SMTP servers that greet with a 5XX status code (go away,
174 /*	do
175 /*	not try again later).
176 /* .IP "\fBsmtp_skip_quit_response (yes)\fR"
177 /*	Do not wait for the response to the SMTP QUIT command.
178 /* .PP
179 /*	Available in Postfix version 2.0 and earlier:
180 /* .IP "\fBsmtp_skip_4xx_greeting (yes)\fR"
181 /*	Skip SMTP servers that greet with a 4XX status code (go away, try
182 /*	again later).
183 /* .PP
184 /*	Available in Postfix version 2.2 and later:
185 /* .IP "\fBsmtp_discard_ehlo_keyword_address_maps (empty)\fR"
186 /*	Lookup tables, indexed by the remote SMTP server address, with
187 /*	case insensitive lists of EHLO keywords (pipelining, starttls, auth,
188 /*	etc.) that the Postfix SMTP client will ignore in the EHLO response from a
189 /*	remote SMTP server.
190 /* .IP "\fBsmtp_discard_ehlo_keywords (empty)\fR"
191 /*	A case insensitive list of EHLO keywords (pipelining, starttls,
192 /*	auth, etc.) that the Postfix SMTP client will ignore in the EHLO
193 /*	response from a remote SMTP server.
194 /* .IP "\fBsmtp_generic_maps (empty)\fR"
195 /*	Optional lookup tables that perform address rewriting in the
196 /*	Postfix SMTP client, typically to transform a locally valid address into
197 /*	a globally valid address when sending mail across the Internet.
198 /* .PP
199 /*	Available in Postfix version 2.2.9 and later:
200 /* .IP "\fBsmtp_cname_overrides_servername (version dependent)\fR"
201 /*	Allow DNS CNAME records to override the servername that the
202 /*	Postfix SMTP client uses for logging, SASL password lookup, TLS
203 /*	policy decisions, or TLS certificate verification.
204 /* .PP
205 /*	Available in Postfix version 2.3 and later:
206 /* .IP "\fBlmtp_discard_lhlo_keyword_address_maps (empty)\fR"
207 /*	Lookup tables, indexed by the remote LMTP server address, with
208 /*	case insensitive lists of LHLO keywords (pipelining, starttls,
209 /*	auth, etc.) that the Postfix LMTP client will ignore in the LHLO
210 /*	response
211 /*	from a remote LMTP server.
212 /* .IP "\fBlmtp_discard_lhlo_keywords (empty)\fR"
213 /*	A case insensitive list of LHLO keywords (pipelining, starttls,
214 /*	auth, etc.) that the Postfix LMTP client will ignore in the LHLO
215 /*	response
216 /*	from a remote LMTP server.
217 /* .PP
218 /*	Available in Postfix version 2.4.4 and later:
219 /* .IP "\fBsend_cyrus_sasl_authzid (no)\fR"
220 /*	When authenticating to a remote SMTP or LMTP server with the
221 /*	default setting "no", send no SASL authoriZation ID (authzid); send
222 /*	only the SASL authentiCation ID (authcid) plus the authcid's password.
223 /* .PP
224 /*      Available in Postfix version 2.5 and later:
225 /* .IP "\fBsmtp_header_checks (empty)\fR"
226 /*	Restricted \fBheader_checks\fR(5) tables for the Postfix SMTP client.
227 /* .IP "\fBsmtp_mime_header_checks (empty)\fR"
228 /*	Restricted \fBmime_header_checks\fR(5) tables for the Postfix SMTP
229 /*	client.
230 /* .IP "\fBsmtp_nested_header_checks (empty)\fR"
231 /*	Restricted \fBnested_header_checks\fR(5) tables for the Postfix SMTP
232 /*	client.
233 /* .IP "\fBsmtp_body_checks (empty)\fR"
234 /*	Restricted \fBbody_checks\fR(5) tables for the Postfix SMTP client.
235 /* .PP
236 /*	Available in Postfix version 2.6 and later:
237 /* .IP "\fBtcp_windowsize (0)\fR"
238 /*	An optional workaround for routers that break TCP window scaling.
239 /* .PP
240 /*	Available in Postfix version 2.8 and later:
241 /* .IP "\fBsmtp_dns_resolver_options (empty)\fR"
242 /*	DNS Resolver options for the Postfix SMTP client.
243 /* .PP
244 /*	Available in Postfix version 2.9 and later:
245 /* .IP "\fBsmtp_per_record_deadline (no)\fR"
246 /*	Change the behavior of the smtp_*_timeout time limits, from a
247 /*	time limit per read or write system call, to a time limit to send
248 /*	or receive a complete record (an SMTP command line, SMTP response
249 /*	line, SMTP message content line, or TLS protocol message).
250 /* .IP "\fBsmtp_send_dummy_mail_auth (no)\fR"
251 /*	Whether or not to append the "AUTH=<>" option to the MAIL
252 /*	FROM command in SASL-authenticated SMTP sessions.
253 /* MIME PROCESSING CONTROLS
254 /* .ad
255 /* .fi
256 /*	Available in Postfix version 2.0 and later:
257 /* .IP "\fBdisable_mime_output_conversion (no)\fR"
258 /*	Disable the conversion of 8BITMIME format to 7BIT format.
259 /* .IP "\fBmime_boundary_length_limit (2048)\fR"
260 /*	The maximal length of MIME multipart boundary strings.
261 /* .IP "\fBmime_nesting_limit (100)\fR"
262 /*	The maximal recursion level that the MIME processor will handle.
263 /* EXTERNAL CONTENT INSPECTION CONTROLS
264 /* .ad
265 /* .fi
266 /*	Available in Postfix version 2.1 and later:
267 /* .IP "\fBsmtp_send_xforward_command (no)\fR"
268 /*	Send the non-standard XFORWARD command when the Postfix SMTP server
269 /*	EHLO response announces XFORWARD support.
270 /* SASL AUTHENTICATION CONTROLS
271 /* .ad
272 /* .fi
273 /* .IP "\fBsmtp_sasl_auth_enable (no)\fR"
274 /*	Enable SASL authentication in the Postfix SMTP client.
275 /* .IP "\fBsmtp_sasl_password_maps (empty)\fR"
276 /*	Optional Postfix SMTP client lookup tables with one username:password
277 /*	entry
278 /*	per remote hostname or domain, or sender address when sender-dependent
279 /*	authentication is enabled.
280 /* .IP "\fBsmtp_sasl_security_options (noplaintext, noanonymous)\fR"
281 /*	Postfix SMTP client SASL security options; as of Postfix 2.3
282 /*	the list of available
283 /*	features depends on the SASL client implementation that is selected
284 /*	with \fBsmtp_sasl_type\fR.
285 /* .PP
286 /*	Available in Postfix version 2.2 and later:
287 /* .IP "\fBsmtp_sasl_mechanism_filter (empty)\fR"
288 /*	If non-empty, a Postfix SMTP client filter for the remote SMTP
289 /*	server's list of offered SASL mechanisms.
290 /* .PP
291 /*	Available in Postfix version 2.3 and later:
292 /* .IP "\fBsmtp_sender_dependent_authentication (no)\fR"
293 /*	Enable sender-dependent authentication in the Postfix SMTP client; this is
294 /*	available only with SASL authentication, and disables SMTP connection
295 /*	caching to ensure that mail from different senders will use the
296 /*	appropriate credentials.
297 /* .IP "\fBsmtp_sasl_path (empty)\fR"
298 /*	Implementation-specific information that the Postfix SMTP client
299 /*	passes through to
300 /*	the SASL plug-in implementation that is selected with
301 /*	\fBsmtp_sasl_type\fR.
302 /* .IP "\fBsmtp_sasl_type (cyrus)\fR"
303 /*	The SASL plug-in type that the Postfix SMTP client should use
304 /*	for authentication.
305 /* .PP
306 /*	Available in Postfix version 2.5 and later:
307 /* .IP "\fBsmtp_sasl_auth_cache_name (empty)\fR"
308 /*	An optional table to prevent repeated SASL authentication
309 /*	failures with the same remote SMTP server hostname, username and
310 /*	password.
311 /* .IP "\fBsmtp_sasl_auth_cache_time (90d)\fR"
312 /*	The maximal age of an smtp_sasl_auth_cache_name entry before it
313 /*	is removed.
314 /* .IP "\fBsmtp_sasl_auth_soft_bounce (yes)\fR"
315 /*	When a remote SMTP server rejects a SASL authentication request
316 /*	with a 535 reply code, defer mail delivery instead of returning
317 /*	mail as undeliverable.
318 /* .PP
319 /*	Available in Postfix version 2.9 and later:
320 /* .IP "\fBsmtp_send_dummy_mail_auth (no)\fR"
321 /*	Whether or not to append the "AUTH=<>" option to the MAIL
322 /*	FROM command in SASL-authenticated SMTP sessions.
323 /* STARTTLS SUPPORT CONTROLS
324 /* .ad
325 /* .fi
326 /*	Detailed information about STARTTLS configuration may be found
327 /*	in the TLS_README document.
328 /* .IP "\fBsmtp_tls_security_level (empty)\fR"
329 /*	The default SMTP TLS security level for the Postfix SMTP client;
330 /*	when a non-empty value is specified, this overrides the obsolete
331 /*	parameters smtp_use_tls, smtp_enforce_tls, and smtp_tls_enforce_peername.
332 /* .IP "\fBsmtp_sasl_tls_security_options ($smtp_sasl_security_options)\fR"
333 /*	The SASL authentication security options that the Postfix SMTP
334 /*	client uses for TLS encrypted SMTP sessions.
335 /* .IP "\fBsmtp_starttls_timeout (300s)\fR"
336 /*	Time limit for Postfix SMTP client write and read operations
337 /*	during TLS startup and shutdown handshake procedures.
338 /* .IP "\fBsmtp_tls_CAfile (empty)\fR"
339 /*	A file containing CA certificates of root CAs trusted to sign
340 /*	either remote SMTP server certificates or intermediate CA certificates.
341 /* .IP "\fBsmtp_tls_CApath (empty)\fR"
342 /*	Directory with PEM format certificate authority certificates
343 /*	that the Postfix SMTP client uses to verify a remote SMTP server
344 /*	certificate.
345 /* .IP "\fBsmtp_tls_cert_file (empty)\fR"
346 /*	File with the Postfix SMTP client RSA certificate in PEM format.
347 /* .IP "\fBsmtp_tls_mandatory_ciphers (medium)\fR"
348 /*	The minimum TLS cipher grade that the Postfix SMTP client will
349 /*	use with
350 /*	mandatory TLS encryption.
351 /* .IP "\fBsmtp_tls_exclude_ciphers (empty)\fR"
352 /*	List of ciphers or cipher types to exclude from the Postfix
353 /*	SMTP client cipher
354 /*	list at all TLS security levels.
355 /* .IP "\fBsmtp_tls_mandatory_exclude_ciphers (empty)\fR"
356 /*	Additional list of ciphers or cipher types to exclude from the
357 /*	Postfix SMTP client cipher list at mandatory TLS security levels.
358 /* .IP "\fBsmtp_tls_dcert_file (empty)\fR"
359 /*	File with the Postfix SMTP client DSA certificate in PEM format.
360 /* .IP "\fBsmtp_tls_dkey_file ($smtp_tls_dcert_file)\fR"
361 /*	File with the Postfix SMTP client DSA private key in PEM format.
362 /* .IP "\fBsmtp_tls_key_file ($smtp_tls_cert_file)\fR"
363 /*	File with the Postfix SMTP client RSA private key in PEM format.
364 /* .IP "\fBsmtp_tls_loglevel (0)\fR"
365 /*	Enable additional Postfix SMTP client logging of TLS activity.
366 /* .IP "\fBsmtp_tls_note_starttls_offer (no)\fR"
367 /*	Log the hostname of a remote SMTP server that offers STARTTLS,
368 /*	when TLS is not already enabled for that server.
369 /* .IP "\fBsmtp_tls_policy_maps (empty)\fR"
370 /*	Optional lookup tables with the Postfix SMTP client TLS security
371 /*	policy by next-hop destination; when a non-empty value is specified,
372 /*	this overrides the obsolete smtp_tls_per_site parameter.
373 /* .IP "\fBsmtp_tls_mandatory_protocols (!SSLv2)\fR"
374 /*	List of SSL/TLS protocols that the Postfix SMTP client will use with
375 /*	mandatory TLS encryption.
376 /* .IP "\fBsmtp_tls_scert_verifydepth (9)\fR"
377 /*	The verification depth for remote SMTP server certificates.
378 /* .IP "\fBsmtp_tls_secure_cert_match (nexthop, dot-nexthop)\fR"
379 /*	How the Postfix SMTP client verifies the server certificate
380 /*	peername for the
381 /*	"secure" TLS security level.
382 /* .IP "\fBsmtp_tls_session_cache_database (empty)\fR"
383 /*	Name of the file containing the optional Postfix SMTP client
384 /*	TLS session cache.
385 /* .IP "\fBsmtp_tls_session_cache_timeout (3600s)\fR"
386 /*	The expiration time of Postfix SMTP client TLS session cache
387 /*	information.
388 /* .IP "\fBsmtp_tls_verify_cert_match (hostname)\fR"
389 /*	How the Postfix SMTP client verifies the server certificate
390 /*	peername for the
391 /*	"verify" TLS security level.
392 /* .IP "\fBtls_daemon_random_bytes (32)\fR"
393 /*	The number of pseudo-random bytes that an \fBsmtp\fR(8) or \fBsmtpd\fR(8)
394 /*	process requests from the \fBtlsmgr\fR(8) server in order to seed its
395 /*	internal pseudo random number generator (PRNG).
396 /* .IP "\fBtls_high_cipherlist (ALL:!EXPORT:!LOW:!MEDIUM:+RC4:@STRENGTH)\fR"
397 /*	The OpenSSL cipherlist for "HIGH" grade ciphers.
398 /* .IP "\fBtls_medium_cipherlist (ALL:!EXPORT:!LOW:+RC4:@STRENGTH)\fR"
399 /*	The OpenSSL cipherlist for "MEDIUM" or higher grade ciphers.
400 /* .IP "\fBtls_low_cipherlist (ALL:!EXPORT:+RC4:@STRENGTH)\fR"
401 /*	The OpenSSL cipherlist for "LOW" or higher grade ciphers.
402 /* .IP "\fBtls_export_cipherlist (ALL:+RC4:@STRENGTH)\fR"
403 /*	The OpenSSL cipherlist for "EXPORT" or higher grade ciphers.
404 /* .IP "\fBtls_null_cipherlist (eNULL:!aNULL)\fR"
405 /*	The OpenSSL cipherlist for "NULL" grade ciphers that provide
406 /*	authentication without encryption.
407 /* .PP
408 /*	Available in Postfix version 2.4 and later:
409 /* .IP "\fBsmtp_sasl_tls_verified_security_options ($smtp_sasl_tls_security_options)\fR"
410 /*	The SASL authentication security options that the Postfix SMTP
411 /*	client uses for TLS encrypted SMTP sessions with a verified server
412 /*	certificate.
413 /* .PP
414 /*	Available in Postfix version 2.5 and later:
415 /* .IP "\fBsmtp_tls_fingerprint_cert_match (empty)\fR"
416 /*	List of acceptable remote SMTP server certificate fingerprints for
417 /*	the "fingerprint" TLS security level (\fBsmtp_tls_security_level\fR =
418 /*	fingerprint).
419 /* .IP "\fBsmtp_tls_fingerprint_digest (md5)\fR"
420 /*	The message digest algorithm used to construct remote SMTP server
421 /*	certificate fingerprints.
422 /* .PP
423 /*	Available in Postfix version 2.6 and later:
424 /* .IP "\fBsmtp_tls_protocols (!SSLv2)\fR"
425 /*	List of TLS protocols that the Postfix SMTP client will exclude or
426 /*	include with opportunistic TLS encryption.
427 /* .IP "\fBsmtp_tls_ciphers (export)\fR"
428 /*	The minimum TLS cipher grade that the Postfix SMTP client
429 /*	will use with opportunistic TLS encryption.
430 /* .IP "\fBsmtp_tls_eccert_file (empty)\fR"
431 /*	File with the Postfix SMTP client ECDSA certificate in PEM format.
432 /* .IP "\fBsmtp_tls_eckey_file ($smtp_tls_eccert_file)\fR"
433 /*	File with the Postfix SMTP client ECDSA private key in PEM format.
434 /* .PP
435 /*	Available in Postfix version 2.7 and later:
436 /* .IP "\fBsmtp_tls_block_early_mail_reply (no)\fR"
437 /*	Try to detect a mail hijacking attack based on a TLS protocol
438 /*	vulnerability (CVE-2009-3555), where an attacker prepends malicious
439 /*	HELO, MAIL, RCPT, DATA commands to a Postfix SMTP client TLS session.
440 /* .PP
441 /*	Available in Postfix version 2.8 and later:
442 /* .IP "\fBtls_disable_workarounds (see 'postconf -d' output)\fR"
443 /*	List or bit-mask of OpenSSL bug work-arounds to disable.
444 /* OBSOLETE STARTTLS CONTROLS
445 /* .ad
446 /* .fi
447 /*	The following configuration parameters exist for compatibility
448 /*	with Postfix versions before 2.3. Support for these will
449 /*	be removed in a future release.
450 /* .IP "\fBsmtp_use_tls (no)\fR"
451 /*	Opportunistic mode: use TLS when a remote SMTP server announces
452 /*	STARTTLS support, otherwise send the mail in the clear.
453 /* .IP "\fBsmtp_enforce_tls (no)\fR"
454 /*	Enforcement mode: require that remote SMTP servers use TLS
455 /*	encryption, and never send mail in the clear.
456 /* .IP "\fBsmtp_tls_enforce_peername (yes)\fR"
457 /*	With mandatory TLS encryption, require that the remote SMTP
458 /*	server hostname matches the information in the remote SMTP server
459 /*	certificate.
460 /* .IP "\fBsmtp_tls_per_site (empty)\fR"
461 /*	Optional lookup tables with the Postfix SMTP client TLS usage
462 /*	policy by next-hop destination and by remote SMTP server hostname.
463 /* .IP "\fBsmtp_tls_cipherlist (empty)\fR"
464 /*	Obsolete Postfix < 2.3 control for the Postfix SMTP client TLS
465 /*	cipher list.
466 /* RESOURCE AND RATE CONTROLS
467 /* .ad
468 /* .fi
469 /* .IP "\fBsmtp_destination_concurrency_limit ($default_destination_concurrency_limit)\fR"
470 /*	The maximal number of parallel deliveries to the same destination
471 /*	via the smtp message delivery transport.
472 /* .IP "\fBsmtp_destination_recipient_limit ($default_destination_recipient_limit)\fR"
473 /*	The maximal number of recipients per message for the smtp
474 /*	message delivery transport.
475 /* .IP "\fBsmtp_connect_timeout (30s)\fR"
476 /*	The Postfix SMTP client time limit for completing a TCP connection, or
477 /*	zero (use the operating system built-in time limit).
478 /* .IP "\fBsmtp_helo_timeout (300s)\fR"
479 /*	The Postfix SMTP client time limit for sending the HELO or EHLO command,
480 /*	and for receiving the initial remote SMTP server response.
481 /* .IP "\fBlmtp_lhlo_timeout (300s)\fR"
482 /*	The Postfix LMTP client time limit for sending the LHLO command,
483 /*	and for receiving the initial remote LMTP server response.
484 /* .IP "\fBsmtp_xforward_timeout (300s)\fR"
485 /*	The Postfix SMTP client time limit for sending the XFORWARD command,
486 /*	and for receiving the remote SMTP server response.
487 /* .IP "\fBsmtp_mail_timeout (300s)\fR"
488 /*	The Postfix SMTP client time limit for sending the MAIL FROM command,
489 /*	and for receiving the remote SMTP server response.
490 /* .IP "\fBsmtp_rcpt_timeout (300s)\fR"
491 /*	The Postfix SMTP client time limit for sending the SMTP RCPT TO
492 /*	command, and for receiving the remote SMTP server response.
493 /* .IP "\fBsmtp_data_init_timeout (120s)\fR"
494 /*	The Postfix SMTP client time limit for sending the SMTP DATA command,
495 /*	and for receiving the remote SMTP server response.
496 /* .IP "\fBsmtp_data_xfer_timeout (180s)\fR"
497 /*	The Postfix SMTP client time limit for sending the SMTP message content.
498 /* .IP "\fBsmtp_data_done_timeout (600s)\fR"
499 /*	The Postfix SMTP client time limit for sending the SMTP ".", and
500 /*	for receiving the remote SMTP server response.
501 /* .IP "\fBsmtp_quit_timeout (300s)\fR"
502 /*	The Postfix SMTP client time limit for sending the QUIT command,
503 /*	and for receiving the remote SMTP server response.
504 /* .PP
505 /*	Available in Postfix version 2.1 and later:
506 /* .IP "\fBsmtp_mx_address_limit (5)\fR"
507 /*	The maximal number of MX (mail exchanger) IP addresses that can
508 /*	result from Postfix SMTP client mail exchanger lookups, or zero (no
509 /*	limit).
510 /* .IP "\fBsmtp_mx_session_limit (2)\fR"
511 /*	The maximal number of SMTP sessions per delivery request before
512 /*	the Postfix SMTP client
513 /*	gives up or delivers to a fall-back relay host, or zero (no
514 /*	limit).
515 /* .IP "\fBsmtp_rset_timeout (20s)\fR"
516 /*	The Postfix SMTP client time limit for sending the RSET command,
517 /*	and for receiving the remote SMTP server response.
518 /* .PP
519 /*	Available in Postfix version 2.2 and earlier:
520 /* .IP "\fBlmtp_cache_connection (yes)\fR"
521 /*	Keep Postfix LMTP client connections open for up to $max_idle
522 /*	seconds.
523 /* .PP
524 /*	Available in Postfix version 2.2 and later:
525 /* .IP "\fBsmtp_connection_cache_destinations (empty)\fR"
526 /*	Permanently enable SMTP connection caching for the specified
527 /*	destinations.
528 /* .IP "\fBsmtp_connection_cache_on_demand (yes)\fR"
529 /*	Temporarily enable SMTP connection caching while a destination
530 /*	has a high volume of mail in the active queue.
531 /* .IP "\fBsmtp_connection_reuse_time_limit (300s)\fR"
532 /*	The amount of time during which Postfix will use an SMTP
533 /*	connection repeatedly.
534 /* .IP "\fBsmtp_connection_cache_time_limit (2s)\fR"
535 /*	When SMTP connection caching is enabled, the amount of time that
536 /*	an unused SMTP client socket is kept open before it is closed.
537 /* .PP
538 /*	Available in Postfix version 2.3 and later:
539 /* .IP "\fBconnection_cache_protocol_timeout (5s)\fR"
540 /*	Time limit for connection cache connect, send or receive
541 /*	operations.
542 /* .PP
543 /*	Available in Postfix version 2.9 and later:
544 /* .IP "\fBsmtp_per_record_deadline (no)\fR"
545 /*	Change the behavior of the smtp_*_timeout time limits, from a
546 /*	time limit per read or write system call, to a time limit to send
547 /*	or receive a complete record (an SMTP command line, SMTP response
548 /*	line, SMTP message content line, or TLS protocol message).
549 /* TROUBLE SHOOTING CONTROLS
550 /* .ad
551 /* .fi
552 /* .IP "\fBdebug_peer_level (2)\fR"
553 /*	The increment in verbose logging level when a remote client or
554 /*	server matches a pattern in the debug_peer_list parameter.
555 /* .IP "\fBdebug_peer_list (empty)\fR"
556 /*	Optional list of remote client or server hostname or network
557 /*	address patterns that cause the verbose logging level to increase
558 /*	by the amount specified in $debug_peer_level.
559 /* .IP "\fBerror_notice_recipient (postmaster)\fR"
560 /*	The recipient of postmaster notifications about mail delivery
561 /*	problems that are caused by policy, resource, software or protocol
562 /*	errors.
563 /* .IP "\fBinternal_mail_filter_classes (empty)\fR"
564 /*	What categories of Postfix-generated mail are subject to
565 /*	before-queue content inspection by non_smtpd_milters, header_checks
566 /*	and body_checks.
567 /* .IP "\fBnotify_classes (resource, software)\fR"
568 /*	The list of error classes that are reported to the postmaster.
569 /* MISCELLANEOUS CONTROLS
570 /* .ad
571 /* .fi
572 /* .IP "\fBbest_mx_transport (empty)\fR"
573 /*	Where the Postfix SMTP client should deliver mail when it detects
574 /*	a "mail loops back to myself" error condition.
575 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
576 /*	The default location of the Postfix main.cf and master.cf
577 /*	configuration files.
578 /* .IP "\fBdaemon_timeout (18000s)\fR"
579 /*	How much time a Postfix daemon process may take to handle a
580 /*	request before it is terminated by a built-in watchdog timer.
581 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
582 /*	The maximal number of digits after the decimal point when logging
583 /*	sub-second delay values.
584 /* .IP "\fBdisable_dns_lookups (no)\fR"
585 /*	Disable DNS lookups in the Postfix SMTP and LMTP clients.
586 /* .IP "\fBinet_interfaces (all)\fR"
587 /*	The network interface addresses that this mail system receives
588 /*	mail on.
589 /* .IP "\fBinet_protocols (all)\fR"
590 /*	The Internet protocols Postfix will attempt to use when making
591 /*	or accepting connections.
592 /* .IP "\fBipc_timeout (3600s)\fR"
593 /*	The time limit for sending or receiving information over an internal
594 /*	communication channel.
595 /* .IP "\fBlmtp_assume_final (no)\fR"
596 /*	When a remote LMTP server announces no DSN support, assume that
597 /*	the
598 /*	server performs final delivery, and send "delivered" delivery status
599 /*	notifications instead of "relayed".
600 /* .IP "\fBlmtp_tcp_port (24)\fR"
601 /*	The default TCP port that the Postfix LMTP client connects to.
602 /* .IP "\fBmax_idle (100s)\fR"
603 /*	The maximum amount of time that an idle Postfix daemon process waits
604 /*	for an incoming connection before terminating voluntarily.
605 /* .IP "\fBmax_use (100)\fR"
606 /*	The maximal number of incoming connections that a Postfix daemon
607 /*	process will service before terminating voluntarily.
608 /* .IP "\fBprocess_id (read-only)\fR"
609 /*	The process ID of a Postfix command or daemon process.
610 /* .IP "\fBprocess_name (read-only)\fR"
611 /*	The process name of a Postfix command or daemon process.
612 /* .IP "\fBproxy_interfaces (empty)\fR"
613 /*	The network interface addresses that this mail system receives mail
614 /*	on by way of a proxy or network address translation unit.
615 /* .IP "\fBsmtp_address_preference (any)\fR"
616 /*	The address type ("ipv6", "ipv4" or "any") that the Postfix
617 /*	SMTP client will try first, when a destination has IPv6 and IPv4
618 /*	addresses with equal MX preference.
619 /* .IP "\fBsmtp_bind_address (empty)\fR"
620 /*	An optional numerical network address that the Postfix SMTP client
621 /*	should bind to when making an IPv4 connection.
622 /* .IP "\fBsmtp_bind_address6 (empty)\fR"
623 /*	An optional numerical network address that the Postfix SMTP client
624 /*	should bind to when making an IPv6 connection.
625 /* .IP "\fBsmtp_helo_name ($myhostname)\fR"
626 /*	The hostname to send in the SMTP EHLO or HELO command.
627 /* .IP "\fBlmtp_lhlo_name ($myhostname)\fR"
628 /*	The hostname to send in the LMTP LHLO command.
629 /* .IP "\fBsmtp_host_lookup (dns)\fR"
630 /*	What mechanisms the Postfix SMTP client uses to look up a host's IP
631 /*	address.
632 /* .IP "\fBsmtp_randomize_addresses (yes)\fR"
633 /*	Randomize the order of equal-preference MX host addresses.
634 /* .IP "\fBsyslog_facility (mail)\fR"
635 /*	The syslog facility of Postfix logging.
636 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
637 /*	The mail system name that is prepended to the process name in syslog
638 /*	records, so that "smtpd" becomes, for example, "postfix/smtpd".
639 /* .PP
640 /*	Available with Postfix 2.2 and earlier:
641 /* .IP "\fBfallback_relay (empty)\fR"
642 /*	Optional list of relay hosts for SMTP destinations that can't be
643 /*	found or that are unreachable.
644 /* .PP
645 /*	Available with Postfix 2.3 and later:
646 /* .IP "\fBsmtp_fallback_relay ($fallback_relay)\fR"
647 /*	Optional list of relay hosts for SMTP destinations that can't be
648 /*	found or that are unreachable.
649 /* SEE ALSO
650 /*	generic(5), output address rewriting
651 /*	header_checks(5), message header content inspection
652 /*	body_checks(5), body parts content inspection
653 /*	qmgr(8), queue manager
654 /*	bounce(8), delivery status reports
655 /*	scache(8), connection cache server
656 /*	postconf(5), configuration parameters
657 /*	master(5), generic daemon options
658 /*	master(8), process manager
659 /*	tlsmgr(8), TLS session and PRNG management
660 /*	syslogd(8), system logging
661 /* README FILES
662 /* .ad
663 /* .fi
664 /*	Use "\fBpostconf readme_directory\fR" or
665 /*	"\fBpostconf html_directory\fR" to locate this information.
666 /* .na
667 /* .nf
668 /*	SASL_README, Postfix SASL howto
669 /*	TLS_README, Postfix STARTTLS howto
670 /* LICENSE
671 /* .ad
672 /* .fi
673 /*	The Secure Mailer license must be distributed with this software.
674 /* AUTHOR(S)
675 /*	Wietse Venema
676 /*	IBM T.J. Watson Research
677 /*	P.O. Box 704
678 /*	Yorktown Heights, NY 10598, USA
679 /*
680 /*	Command pipelining in cooperation with:
681 /*	Jon Ribbens
682 /*	Oaktree Internet Solutions Ltd.,
683 /*	Internet House,
684 /*	Canal Basin,
685 /*	Coventry,
686 /*	CV1 4LY, United Kingdom.
687 /*
688 /*	SASL support originally by:
689 /*	Till Franke
690 /*	SuSE Rhein/Main AG
691 /*	65760 Eschborn, Germany
692 /*
693 /*	TLS support originally by:
694 /*	Lutz Jaenicke
695 /*	BTU Cottbus
696 /*	Allgemeine Elektrotechnik
697 /*	Universitaetsplatz 3-4
698 /*	D-03044 Cottbus, Germany
699 /*
700 /*	Revised TLS and SMTP connection cache support by:
701 /*	Victor Duchovni
702 /*	Morgan Stanley
703 /*--*/
704 
705 /* System library. */
706 
707 #include <sys_defs.h>
708 #include <unistd.h>
709 #include <stdlib.h>
710 #include <string.h>
711 #include <fcntl.h>
712 #include <dict.h>
713 #include <stringops.h>
714 
715 /* Utility library. */
716 
717 #include <msg.h>
718 #include <mymalloc.h>
719 #include <name_mask.h>
720 #include <name_code.h>
721 
722 /* Global library. */
723 
724 #include <deliver_request.h>
725 #include <mail_params.h>
726 #include <mail_version.h>
727 #include <mail_conf.h>
728 #include <debug_peer.h>
729 #include <flush_clnt.h>
730 #include <scache.h>
731 #include <string_list.h>
732 #include <maps.h>
733 #include <ext_prop.h>
734 
735 /* DNS library. */
736 
737 #include <dns.h>
738 
739 /* Single server skeleton. */
740 
741 #include <mail_server.h>
742 
743 /* Application-specific. */
744 
745 #include "smtp.h"
746 #include "smtp_sasl.h"
747 
748  /*
749   * Tunable parameters. These have compiled-in defaults that can be overruled
750   * by settings in the global Postfix configuration file.
751   */
752 int     var_smtp_conn_tmout;
753 int     var_smtp_helo_tmout;
754 int     var_smtp_xfwd_tmout;
755 int     var_smtp_mail_tmout;
756 int     var_smtp_rcpt_tmout;
757 int     var_smtp_data0_tmout;
758 int     var_smtp_data1_tmout;
759 int     var_smtp_data2_tmout;
760 int     var_smtp_rset_tmout;
761 int     var_smtp_quit_tmout;
762 char   *var_inet_interfaces;
763 char   *var_notify_classes;
764 int     var_smtp_skip_5xx_greeting;
765 int     var_ign_mx_lookup_err;
766 int     var_skip_quit_resp;
767 char   *var_fallback_relay;
768 char   *var_bestmx_transp;
769 char   *var_error_rcpt;
770 int     var_smtp_always_ehlo;
771 int     var_smtp_never_ehlo;
772 char   *var_smtp_sasl_opts;
773 char   *var_smtp_sasl_path;
774 char   *var_smtp_sasl_passwd;
775 bool    var_smtp_sasl_enable;
776 char   *var_smtp_sasl_mechs;
777 char   *var_smtp_sasl_type;
778 char   *var_smtp_bind_addr;
779 char   *var_smtp_bind_addr6;
780 bool    var_smtp_rand_addr;
781 int     var_smtp_pix_thresh;
782 int     var_smtp_pix_delay;
783 int     var_smtp_line_limit;
784 char   *var_smtp_helo_name;
785 char   *var_smtp_host_lookup;
786 bool    var_smtp_quote_821_env;
787 bool    var_smtp_defer_mxaddr;
788 bool    var_smtp_send_xforward;
789 int     var_smtp_mxaddr_limit;
790 int     var_smtp_mxsess_limit;
791 int     var_smtp_cache_conn;
792 int     var_smtp_reuse_time;
793 char   *var_smtp_cache_dest;
794 char   *var_scache_service;		/* You can now leave this here. */
795 bool    var_smtp_cache_demand;
796 char   *var_smtp_ehlo_dis_words;
797 char   *var_smtp_ehlo_dis_maps;
798 char   *var_smtp_addr_pref;
799 
800 char   *var_smtp_tls_level;
801 bool    var_smtp_use_tls;
802 bool    var_smtp_enforce_tls;
803 char   *var_smtp_tls_per_site;
804 char   *var_smtp_tls_policy;
805 
806 #ifdef USE_TLS
807 char   *var_smtp_sasl_tls_opts;
808 char   *var_smtp_sasl_tlsv_opts;
809 int     var_smtp_starttls_tmout;
810 char   *var_smtp_tls_CAfile;
811 char   *var_smtp_tls_CApath;
812 char   *var_smtp_tls_cert_file;
813 char   *var_smtp_tls_mand_ciph;
814 char   *var_smtp_tls_excl_ciph;
815 char   *var_smtp_tls_mand_excl;
816 char   *var_smtp_tls_dcert_file;
817 char   *var_smtp_tls_dkey_file;
818 bool    var_smtp_tls_enforce_peername;
819 char   *var_smtp_tls_key_file;
820 char   *var_smtp_tls_loglevel;
821 bool    var_smtp_tls_note_starttls_offer;
822 char   *var_smtp_tls_mand_proto;
823 char   *var_smtp_tls_sec_cmatch;
824 int     var_smtp_tls_scert_vd;
825 char   *var_smtp_tls_vfy_cmatch;
826 char   *var_smtp_tls_fpt_cmatch;
827 char   *var_smtp_tls_fpt_dgst;
828 char   *var_smtp_tls_proto;
829 char   *var_smtp_tls_ciph;
830 char   *var_smtp_tls_eccert_file;
831 char   *var_smtp_tls_eckey_file;
832 bool    var_smtp_tls_blk_early_mail_reply;
833 
834 #endif
835 
836 char   *var_smtp_generic_maps;
837 char   *var_prop_extension;
838 bool    var_smtp_sender_auth;
839 char   *var_lmtp_tcp_port;
840 int     var_scache_proto_tmout;
841 bool    var_smtp_cname_overr;
842 char   *var_smtp_pix_bug_words;
843 char   *var_smtp_pix_bug_maps;
844 char   *var_cyrus_conf_path;
845 char   *var_smtp_head_chks;
846 char   *var_smtp_mime_chks;
847 char   *var_smtp_nest_chks;
848 char   *var_smtp_body_chks;
849 char   *var_smtp_resp_filter;
850 bool    var_lmtp_assume_final;
851 char   *var_smtp_dns_res_opt;
852 bool    var_smtp_rec_deadline;
853 bool    var_smtp_dummy_mail_auth;
854 
855  /* Special handling of 535 AUTH errors. */
856 char   *var_smtp_sasl_auth_cache_name;
857 int     var_smtp_sasl_auth_cache_time;
858 bool    var_smtp_sasl_auth_soft_bounce;
859 
860  /*
861   * Global variables.
862   */
863 int     smtp_host_lookup_mask;
864 STRING_LIST *smtp_cache_dest;
865 SCACHE *smtp_scache;
866 MAPS   *smtp_ehlo_dis_maps;
867 MAPS   *smtp_generic_maps;
868 int     smtp_ext_prop_mask;
869 unsigned smtp_dns_res_opt;
870 MAPS   *smtp_pix_bug_maps;
871 HBC_CHECKS *smtp_header_checks;		/* limited header checks */
872 HBC_CHECKS *smtp_body_checks;		/* limited body checks */
873 
874 #ifdef USE_TLS
875 
876  /*
877   * OpenSSL client state (opaque handle)
878   */
879 TLS_APPL_STATE *smtp_tls_ctx;
880 
881 #endif
882 
883  /*
884   * IPv6 preference.
885   */
886 static int smtp_addr_pref;
887 
888 /* deliver_message - deliver message with extreme prejudice */
889 
890 static int deliver_message(const char *service, DELIVER_REQUEST *request)
891 {
892     SMTP_STATE *state;
893     int     result;
894 
895     if (msg_verbose)
896 	msg_info("deliver_message: from %s", request->sender);
897 
898     /*
899      * Sanity checks. The smtp server is unprivileged and chrooted, so we can
900      * afford to distribute the data censoring code, instead of having it all
901      * in one place.
902      */
903     if (request->nexthop[0] == 0)
904 	msg_fatal("empty nexthop hostname");
905     if (request->rcpt_list.len <= 0)
906 	msg_fatal("recipient count: %d", request->rcpt_list.len);
907 
908     /*
909      * Initialize. Bundle all information about the delivery request, so that
910      * we can produce understandable diagnostics when something goes wrong
911      * many levels below. The alternative would be to make everything global.
912      */
913     state = smtp_state_alloc();
914     state->request = request;
915     state->src = request->fp;
916     state->service = service;
917     state->misc_flags |= smtp_addr_pref;
918     SMTP_RCPT_INIT(state);
919 
920     /*
921      * Establish an SMTP session and deliver this message to all requested
922      * recipients. At the end, notify the postmaster of any protocol errors.
923      * Optionally deliver mail locally when this machine is the best mail
924      * exchanger.
925      */
926     result = smtp_connect(state);
927 
928     /*
929      * Clean up.
930      */
931     smtp_state_free(state);
932 
933     return (result);
934 }
935 
936 /* smtp_service - perform service for client */
937 
938 static void smtp_service(VSTREAM *client_stream, char *service, char **argv)
939 {
940     DELIVER_REQUEST *request;
941     int     status;
942 
943     /*
944      * Sanity check. This service takes no command-line arguments.
945      */
946     if (argv[0])
947 	msg_fatal("unexpected command-line argument: %s", argv[0]);
948 
949     /*
950      * This routine runs whenever a client connects to the UNIX-domain socket
951      * dedicated to remote SMTP delivery service. What we see below is a
952      * little protocol to (1) tell the queue manager that we are ready, (2)
953      * read a request from the queue manager, and (3) report the completion
954      * status of that request. All connection-management stuff is handled by
955      * the common code in single_server.c.
956      */
957     if ((request = deliver_request_read(client_stream)) != 0) {
958 	status = deliver_message(service, request);
959 	deliver_request_done(client_stream, request, status);
960     }
961 }
962 
963 /* post_init - post-jail initialization */
964 
965 static void post_init(char *unused_name, char **unused_argv)
966 {
967     static const NAME_MASK lookup_masks[] = {
968 	SMTP_HOST_LOOKUP_DNS, SMTP_HOST_FLAG_DNS,
969 	SMTP_HOST_LOOKUP_NATIVE, SMTP_HOST_FLAG_NATIVE,
970 	0,
971     };
972     static const NAME_MASK dns_res_opt_masks[] = {
973 	SMTP_DNS_RES_OPT_DEFNAMES, RES_DEFNAMES,
974 	SMTP_DNS_RES_OPT_DNSRCH, RES_DNSRCH,
975 	0,
976     };
977 
978     /*
979      * Select hostname lookup mechanisms.
980      */
981     if (var_disable_dns)
982 	smtp_host_lookup_mask = SMTP_HOST_FLAG_NATIVE;
983     else
984 	smtp_host_lookup_mask = name_mask(VAR_SMTP_HOST_LOOKUP, lookup_masks,
985 					  var_smtp_host_lookup);
986     if (msg_verbose)
987 	msg_info("host name lookup methods: %s",
988 		 str_name_mask(VAR_SMTP_HOST_LOOKUP, lookup_masks,
989 			       smtp_host_lookup_mask));
990 
991     /*
992      * Session cache instance.
993      */
994     if (*var_smtp_cache_dest || var_smtp_cache_demand)
995 #if 0
996 	smtp_scache = scache_multi_create();
997 #else
998 	smtp_scache = scache_clnt_create(var_scache_service,
999 					 var_scache_proto_tmout,
1000 					 var_ipc_idle_limit,
1001 					 var_ipc_ttl_limit);
1002 #endif
1003 
1004     /*
1005      * Select DNS query flags.
1006      */
1007     smtp_dns_res_opt = name_mask(VAR_SMTP_DNS_RES_OPT, dns_res_opt_masks,
1008 				 var_smtp_dns_res_opt);
1009 }
1010 
1011 /* pre_init - pre-jail initialization */
1012 
1013 static void pre_init(char *unused_name, char **unused_argv)
1014 {
1015     int     use_tls;
1016     static const NAME_CODE addr_pref_map[] = {
1017 	INET_PROTO_NAME_IPV6, SMTP_MISC_FLAG_PREF_IPV6,
1018 	INET_PROTO_NAME_IPV4, SMTP_MISC_FLAG_PREF_IPV4,
1019 	INET_PROTO_NAME_ANY, 0,
1020 	0, -1,
1021     };
1022 
1023     /*
1024      * Turn on per-peer debugging.
1025      */
1026     debug_peer_init();
1027 
1028     /*
1029      * SASL initialization.
1030      */
1031     if (var_smtp_sasl_enable)
1032 #ifdef USE_SASL_AUTH
1033 	smtp_sasl_initialize();
1034 #else
1035 	msg_warn("%s is true, but SASL support is not compiled in",
1036 		 VAR_SMTP_SASL_ENABLE);
1037 #endif
1038 
1039     if (*var_smtp_tls_level != 0)
1040 #ifdef USE_TLS
1041 	switch (tls_level_lookup(var_smtp_tls_level)) {
1042 	case TLS_LEV_SECURE:
1043 	case TLS_LEV_VERIFY:
1044 	case TLS_LEV_FPRINT:
1045 	case TLS_LEV_ENCRYPT:
1046 	    var_smtp_use_tls = var_smtp_enforce_tls = 1;
1047 	    break;
1048 	case TLS_LEV_MAY:
1049 	    var_smtp_use_tls = 1;
1050 	    var_smtp_enforce_tls = 0;
1051 	    break;
1052 	case TLS_LEV_NONE:
1053 	    var_smtp_use_tls = var_smtp_enforce_tls = 0;
1054 	    break;
1055 	default:
1056 	    /* tls_level_lookup() logs no warning. */
1057 	    /* session_tls_init() assumes that var_smtp_tls_level is sane. */
1058 	    msg_fatal("Invalid TLS level \"%s\"", var_smtp_tls_level);
1059 	}
1060 #endif
1061     use_tls = (var_smtp_use_tls || var_smtp_enforce_tls);
1062 
1063     /*
1064      * Initialize the TLS data before entering the chroot jail
1065      */
1066     if (use_tls || var_smtp_tls_per_site[0] || var_smtp_tls_policy[0]) {
1067 #ifdef USE_TLS
1068 	TLS_CLIENT_INIT_PROPS props;
1069 	int     using_smtp = (strcmp(var_procname, "smtp") == 0);
1070 
1071 	/*
1072 	 * We get stronger type safety and a cleaner interface by combining
1073 	 * the various parameters into a single tls_client_props structure.
1074 	 *
1075 	 * Large parameter lists are error-prone, so we emulate a language
1076 	 * feature that C does not have natively: named parameter lists.
1077 	 */
1078 	smtp_tls_ctx =
1079 	    TLS_CLIENT_INIT(&props,
1080 			    log_param = using_smtp ?
1081 			    VAR_SMTP_TLS_LOGLEVEL : VAR_LMTP_TLS_LOGLEVEL,
1082 			    log_level = var_smtp_tls_loglevel,
1083 			    verifydepth = var_smtp_tls_scert_vd,
1084 			    cache_type = using_smtp ?
1085 			    TLS_MGR_SCACHE_SMTP : TLS_MGR_SCACHE_LMTP,
1086 			    cert_file = var_smtp_tls_cert_file,
1087 			    key_file = var_smtp_tls_key_file,
1088 			    dcert_file = var_smtp_tls_dcert_file,
1089 			    dkey_file = var_smtp_tls_dkey_file,
1090 			    eccert_file = var_smtp_tls_eccert_file,
1091 			    eckey_file = var_smtp_tls_eckey_file,
1092 			    CAfile = var_smtp_tls_CAfile,
1093 			    CApath = var_smtp_tls_CApath,
1094 			    fpt_dgst = var_smtp_tls_fpt_dgst);
1095 	smtp_tls_list_init();
1096 #else
1097 	msg_warn("TLS has been selected, but TLS support is not compiled in");
1098 #endif
1099     }
1100 
1101     /*
1102      * Flush client.
1103      */
1104     flush_init();
1105 
1106     /*
1107      * Session cache domain list.
1108      */
1109     if (*var_smtp_cache_dest)
1110 	smtp_cache_dest = string_list_init(MATCH_FLAG_RETURN, var_smtp_cache_dest);
1111 
1112     /*
1113      * EHLO keyword filter.
1114      */
1115     if (*var_smtp_ehlo_dis_maps)
1116 	smtp_ehlo_dis_maps = maps_create(VAR_SMTP_EHLO_DIS_MAPS,
1117 					 var_smtp_ehlo_dis_maps,
1118 					 DICT_FLAG_LOCK);
1119 
1120     /*
1121      * PIX bug workarounds.
1122      */
1123     if (*var_smtp_pix_bug_maps)
1124 	smtp_pix_bug_maps = maps_create(VAR_SMTP_PIX_BUG_MAPS,
1125 					var_smtp_pix_bug_maps,
1126 					DICT_FLAG_LOCK);
1127 
1128     /*
1129      * Generic maps.
1130      */
1131     if (*var_prop_extension)
1132 	smtp_ext_prop_mask =
1133 	    ext_prop_mask(VAR_PROP_EXTENSION, var_prop_extension);
1134     if (*var_smtp_generic_maps)
1135 	smtp_generic_maps =
1136 	    maps_create(VAR_SMTP_GENERIC_MAPS, var_smtp_generic_maps,
1137 			DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX);
1138 
1139     /*
1140      * Header/body checks.
1141      */
1142     smtp_header_checks = hbc_header_checks_create(
1143 				     VAR_SMTP_HEAD_CHKS, var_smtp_head_chks,
1144 				     VAR_SMTP_MIME_CHKS, var_smtp_mime_chks,
1145 				     VAR_SMTP_NEST_CHKS, var_smtp_nest_chks,
1146 						  smtp_hbc_callbacks);
1147     smtp_body_checks = hbc_body_checks_create(
1148 				     VAR_SMTP_BODY_CHKS, var_smtp_body_chks,
1149 					      smtp_hbc_callbacks);
1150 
1151     /*
1152      * Server reply filter.
1153      */
1154     if (*var_smtp_resp_filter)
1155 	smtp_chat_resp_filter =
1156 	    dict_open(var_smtp_resp_filter, O_RDONLY,
1157 		      DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX);
1158 
1159     /*
1160      * Address family preference.
1161      */
1162     if (*var_smtp_addr_pref) {
1163 	smtp_addr_pref = name_code(addr_pref_map, NAME_CODE_FLAG_NONE,
1164 				   var_smtp_addr_pref);
1165 	if (smtp_addr_pref < 0)
1166 	    msg_fatal("bad %s value: %s", VAR_SMTP_ADDR_PREF, var_smtp_addr_pref);
1167     }
1168 }
1169 
1170 /* pre_accept - see if tables have changed */
1171 
1172 static void pre_accept(char *unused_name, char **unused_argv)
1173 {
1174     const char *table;
1175 
1176     if ((table = dict_changed_name()) != 0) {
1177 	msg_info("table %s has changed -- restarting", table);
1178 	exit(0);
1179     }
1180 }
1181 
1182 MAIL_VERSION_STAMP_DECLARE;
1183 
1184 /* main - pass control to the single-threaded skeleton */
1185 
1186 int     main(int argc, char **argv)
1187 {
1188 #include "smtp_params.c"
1189 #include "lmtp_params.c"
1190     int     smtp_mode;
1191 
1192     /*
1193      * Fingerprint executables and core dumps.
1194      */
1195     MAIL_VERSION_STAMP_ALLOCATE;
1196 
1197     /*
1198      * XXX At this point, var_procname etc. are not initialized.
1199      */
1200     smtp_mode = (strcmp(sane_basename((VSTRING *) 0, argv[0]), "smtp") == 0);
1201 
1202     /*
1203      * Initialize with the LMTP or SMTP parameter name space.
1204      */
1205     single_server_main(argc, argv, smtp_service,
1206 		       MAIL_SERVER_TIME_TABLE, smtp_mode ?
1207 		       smtp_time_table : lmtp_time_table,
1208 		       MAIL_SERVER_INT_TABLE, smtp_mode ?
1209 		       smtp_int_table : lmtp_int_table,
1210 		       MAIL_SERVER_STR_TABLE, smtp_mode ?
1211 		       smtp_str_table : lmtp_str_table,
1212 		       MAIL_SERVER_BOOL_TABLE, smtp_mode ?
1213 		       smtp_bool_table : lmtp_bool_table,
1214 		       MAIL_SERVER_PRE_INIT, pre_init,
1215 		       MAIL_SERVER_POST_INIT, post_init,
1216 		       MAIL_SERVER_PRE_ACCEPT, pre_accept,
1217 		       0);
1218 }
1219