xref: /netbsd-src/external/ibm-public/postfix/dist/src/postscreen/postscreen.c (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1 /*	$NetBSD: postscreen.c,v 1.3 2020/03/18 19:05:19 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	postscreen 8
6 /* SUMMARY
7 /*	Postfix zombie blocker
8 /* SYNOPSIS
9 /*	\fBpostscreen\fR [generic Postfix daemon options]
10 /* DESCRIPTION
11 /*	The Postfix \fBpostscreen\fR(8) server provides additional
12 /*	protection against mail server overload. One \fBpostscreen\fR(8)
13 /*	process handles multiple inbound SMTP connections, and decides
14 /*	which clients may talk to a Postfix SMTP server process.
15 /*	By keeping spambots away, \fBpostscreen\fR(8) leaves more
16 /*	SMTP server processes available for legitimate clients, and
17 /*	delays the onset of server overload conditions.
18 /*
19 /*	This program should not be used on SMTP ports that receive
20 /*	mail from end-user clients (MUAs). In a typical deployment,
21 /*	\fBpostscreen\fR(8) handles the MX service on TCP port 25, and
22 /*	\fBsmtpd\fR(8) receives mail from MUAs on the \fBsubmission\fR
23 /*	service (TCP port 587) which requires client authentication.
24 /*	Alternatively, a site could set up a dedicated, non-postscreen,
25 /*	"port 25" server that provides \fBsubmission\fR service and
26 /*	client authentication, but no MX service.
27 /*
28 /*	\fBpostscreen\fR(8) maintains a temporary whitelist for
29 /*	clients that have passed a number of tests.  When an SMTP
30 /*	client IP address is whitelisted, \fBpostscreen\fR(8) hands
31 /*	off the connection immediately to a Postfix SMTP server
32 /*	process. This minimizes the overhead for legitimate mail.
33 /*
34 /*	By default, \fBpostscreen\fR(8) logs statistics and hands
35 /*	off each connection to a Postfix SMTP server process, while
36 /*	excluding clients in mynetworks from all tests (primarily,
37 /*	to avoid problems with non-standard SMTP implementations
38 /*	in network appliances).  This default mode blocks no clients,
39 /*	and is useful for non-destructive testing.
40 /*
41 /*	In a typical production setting, \fBpostscreen\fR(8) is
42 /*	configured to reject mail from clients that fail one or
43 /*	more tests. \fBpostscreen\fR(8) logs rejected mail with the
44 /*	client address, helo, sender and recipient information.
45 /*
46 /*	\fBpostscreen\fR(8) is not an SMTP proxy; this is intentional.
47 /*	The purpose is to keep spambots away from Postfix SMTP
48 /*	server processes, while minimizing overhead for legitimate
49 /*	traffic.
50 /* SECURITY
51 /* .ad
52 /* .fi
53 /*	The \fBpostscreen\fR(8) server is moderately security-sensitive.
54 /*	It talks to untrusted clients on the network. The process
55 /*	can be run chrooted at fixed low privilege.
56 /* STANDARDS
57 /*	RFC 821 (SMTP protocol)
58 /*	RFC 1123 (Host requirements)
59 /*	RFC 1652 (8bit-MIME transport)
60 /*	RFC 1869 (SMTP service extensions)
61 /*	RFC 1870 (Message Size Declaration)
62 /*	RFC 1985 (ETRN command)
63 /*	RFC 2034 (SMTP Enhanced Status Codes)
64 /*	RFC 2821 (SMTP protocol)
65 /*	Not: RFC 2920 (SMTP Pipelining)
66 /*	RFC 3030 (CHUNKING without BINARYMIME)
67 /*	RFC 3207 (STARTTLS command)
68 /*	RFC 3461 (SMTP DSN Extension)
69 /*	RFC 3463 (Enhanced Status Codes)
70 /*	RFC 5321 (SMTP protocol, including multi-line 220 banners)
71 /* DIAGNOSTICS
72 /*	Problems and transactions are logged to \fBsyslogd\fR(8)
73 /*	or \fBpostlogd\fR(8).
74 /* BUGS
75 /*	The \fBpostscreen\fR(8) built-in SMTP protocol engine
76 /*	currently does not announce support for AUTH, XCLIENT or
77 /*	XFORWARD.
78 /*	If you need to make these services available
79 /*	on port 25, then do not enable the optional "after 220
80 /*	server greeting" tests.
81 /*
82 /*	The optional "after 220 server greeting" tests may result in
83 /*	unexpected delivery delays from senders that retry email delivery
84 /*	from a different IP address.  Reason: after passing these tests a
85 /*	new client must disconnect, and reconnect from the same IP
86 /*	address before it can deliver mail. See POSTSCREEN_README, section
87 /*	"Tests after the 220 SMTP server greeting", for a discussion.
88 /* CONFIGURATION PARAMETERS
89 /* .ad
90 /* .fi
91 /*	Changes to main.cf are not picked up automatically, as
92 /*	\fBpostscreen\fR(8) processes may run for several hours.
93 /*	Use the command "postfix reload" after a configuration
94 /*	change.
95 /*
96 /*	The text below provides only a parameter summary. See
97 /*	\fBpostconf\fR(5) for more details including examples.
98 /*
99 /*	NOTE: Some \fBpostscreen\fR(8) parameters implement
100 /*	stress-dependent behavior.  This is supported only when the
101 /*	default parameter value is stress-dependent (that is, it
102 /*	looks like ${stress?{X}:{Y}}, or it is the $\fIname\fR
103 /*	of an smtpd parameter with a stress-dependent default).
104 /*	Other parameters always evaluate as if the \fBstress\fR
105 /*	parameter value is the empty string.
106 /* COMPATIBILITY CONTROLS
107 /* .ad
108 /* .fi
109 /* .IP "\fBpostscreen_command_filter ($smtpd_command_filter)\fR"
110 /*	A mechanism to transform commands from remote SMTP clients.
111 /* .IP "\fBpostscreen_discard_ehlo_keyword_address_maps ($smtpd_discard_ehlo_keyword_address_maps)\fR"
112 /*	Lookup tables, indexed by the remote SMTP client address, with
113 /*	case insensitive lists of EHLO keywords (pipelining, starttls, auth,
114 /*	etc.) that the \fBpostscreen\fR(8) server will not send in the EHLO response
115 /*	to a remote SMTP client.
116 /* .IP "\fBpostscreen_discard_ehlo_keywords ($smtpd_discard_ehlo_keywords)\fR"
117 /*	A case insensitive list of EHLO keywords (pipelining, starttls,
118 /*	auth, etc.) that the \fBpostscreen\fR(8) server will not send in the EHLO
119 /*	response to a remote SMTP client.
120 /* .PP
121 /*	Available in Postfix version 3.1 and later:
122 /* .IP "\fBdns_ncache_ttl_fix_enable (no)\fR"
123 /*	Enable a workaround for future libc incompatibility.
124 /* .PP
125 /*	Available in Postfix version 3.4 and later:
126 /* .IP "\fBpostscreen_reject_footer_maps ($smtpd_reject_footer_maps)\fR"
127 /*	Optional lookup table for information that is appended after a 4XX
128 /*	or 5XX \fBpostscreen\fR(8) server response.
129 /* TROUBLE SHOOTING CONTROLS
130 /* .ad
131 /* .fi
132 /* .IP "\fBpostscreen_expansion_filter (see 'postconf -d' output)\fR"
133 /*	List of characters that are permitted in postscreen_reject_footer
134 /*	attribute expansions.
135 /* .IP "\fBpostscreen_reject_footer ($smtpd_reject_footer)\fR"
136 /*	Optional information that is appended after a 4XX or 5XX
137 /*	\fBpostscreen\fR(8) server
138 /*	response.
139 /* .IP "\fBsoft_bounce (no)\fR"
140 /*	Safety net to keep mail queued that would otherwise be returned to
141 /*	the sender.
142 /* BEFORE-POSTSCREEN PROXY AGENT
143 /* .ad
144 /* .fi
145 /*	Available in Postfix version 2.10 and later:
146 /* .IP "\fBpostscreen_upstream_proxy_protocol (empty)\fR"
147 /*	The name of the proxy protocol used by an optional before-postscreen
148 /*	proxy agent.
149 /* .IP "\fBpostscreen_upstream_proxy_timeout (5s)\fR"
150 /*	The time limit for the proxy protocol specified with the
151 /*	postscreen_upstream_proxy_protocol parameter.
152 /* PERMANENT WHITE/BLACKLIST TEST
153 /* .ad
154 /* .fi
155 /*	This test is executed immediately after a remote SMTP client
156 /*	connects. If a client is permanently whitelisted, the client
157 /*	will be handed off immediately to a Postfix SMTP server
158 /*	process.
159 /* .IP "\fBpostscreen_access_list (permit_mynetworks)\fR"
160 /*	Permanent white/blacklist for remote SMTP client IP addresses.
161 /* .IP "\fBpostscreen_blacklist_action (ignore)\fR"
162 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client is
163 /*	permanently blacklisted with the postscreen_access_list parameter.
164 /* MAIL EXCHANGER POLICY TESTS
165 /* .ad
166 /* .fi
167 /*	When \fBpostscreen\fR(8) is configured to monitor all primary
168 /*	and backup MX addresses, it can refuse to whitelist clients
169 /*	that connect to a backup MX address only. For small sites,
170 /*	this requires configuring primary and backup MX addresses
171 /*	on the same MTA. Larger sites would have to share the
172 /*	\fBpostscreen\fR(8) cache between primary and backup MTAs,
173 /*	which would introduce a common point of failure.
174 /* .IP "\fBpostscreen_whitelist_interfaces (static:all)\fR"
175 /*	A list of local \fBpostscreen\fR(8) server IP addresses where a
176 /*	non-whitelisted remote SMTP client can obtain \fBpostscreen\fR(8)'s temporary
177 /*	whitelist status.
178 /* BEFORE 220 GREETING TESTS
179 /* .ad
180 /* .fi
181 /*	These tests are executed before the remote SMTP client
182 /*	receives the "220 servername" greeting. If no tests remain
183 /*	after the successful completion of this phase, the client
184 /*	will be handed off immediately to a Postfix SMTP server
185 /*	process.
186 /* .IP "\fBdnsblog_service_name (dnsblog)\fR"
187 /*	The name of the \fBdnsblog\fR(8) service entry in master.cf.
188 /* .IP "\fBpostscreen_dnsbl_action (ignore)\fR"
189 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client's combined
190 /*	DNSBL score is equal to or greater than a threshold (as defined
191 /*	with the postscreen_dnsbl_sites and postscreen_dnsbl_threshold
192 /*	parameters).
193 /* .IP "\fBpostscreen_dnsbl_reply_map (empty)\fR"
194 /*	A mapping from actual DNSBL domain name which includes a secret
195 /*	password, to the DNSBL domain name that postscreen will reply with
196 /*	when it rejects mail.
197 /* .IP "\fBpostscreen_dnsbl_sites (empty)\fR"
198 /*	Optional list of DNS white/blacklist domains, filters and weight
199 /*	factors.
200 /* .IP "\fBpostscreen_dnsbl_threshold (1)\fR"
201 /*	The inclusive lower bound for blocking a remote SMTP client, based on
202 /*	its combined DNSBL score as defined with the postscreen_dnsbl_sites
203 /*	parameter.
204 /* .IP "\fBpostscreen_greet_action (ignore)\fR"
205 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client speaks
206 /*	before its turn within the time specified with the postscreen_greet_wait
207 /*	parameter.
208 /* .IP "\fBpostscreen_greet_banner ($smtpd_banner)\fR"
209 /*	The \fItext\fR in the optional "220-\fItext\fR..." server
210 /*	response that
211 /*	\fBpostscreen\fR(8) sends ahead of the real Postfix SMTP server's "220
212 /*	text..." response, in an attempt to confuse bad SMTP clients so
213 /*	that they speak before their turn (pre-greet).
214 /* .IP "\fBpostscreen_greet_wait (normal: 6s, overload: 2s)\fR"
215 /*	The amount of time that \fBpostscreen\fR(8) will wait for an SMTP
216 /*	client to send a command before its turn, and for DNS blocklist
217 /*	lookup results to arrive (default: up to 2 seconds under stress,
218 /*	up to 6 seconds otherwise).
219 /* .IP "\fBsmtpd_service_name (smtpd)\fR"
220 /*	The internal service that \fBpostscreen\fR(8) hands off allowed
221 /*	connections to.
222 /* .PP
223 /*	Available in Postfix version 2.11 and later:
224 /* .IP "\fBpostscreen_dnsbl_whitelist_threshold (0)\fR"
225 /*	Allow a remote SMTP client to skip "before" and "after 220
226 /*	greeting" protocol tests, based on its combined DNSBL score as
227 /*	defined with the postscreen_dnsbl_sites parameter.
228 /* .PP
229 /*	Available in Postfix version 3.0 and later:
230 /* .IP "\fBpostscreen_dnsbl_timeout (10s)\fR"
231 /*	The time limit for DNSBL or DNSWL lookups.
232 /* AFTER 220 GREETING TESTS
233 /* .ad
234 /* .fi
235 /*	These tests are executed after the remote SMTP client
236 /*	receives the "220 servername" greeting. If a client passes
237 /*	all tests during this phase, it will receive a 4XX response
238 /*	to all RCPT TO commands. After the client reconnects, it
239 /*	will be allowed to talk directly to a Postfix SMTP server
240 /*	process.
241 /* .IP "\fBpostscreen_bare_newline_action (ignore)\fR"
242 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client sends
243 /*	a bare newline character, that is, a newline not preceded by carriage
244 /*	return.
245 /* .IP "\fBpostscreen_bare_newline_enable (no)\fR"
246 /*	Enable "bare newline" SMTP protocol tests in the \fBpostscreen\fR(8)
247 /*	server.
248 /* .IP "\fBpostscreen_disable_vrfy_command ($disable_vrfy_command)\fR"
249 /*	Disable the SMTP VRFY command in the \fBpostscreen\fR(8) daemon.
250 /* .IP "\fBpostscreen_forbidden_commands ($smtpd_forbidden_commands)\fR"
251 /*	List of commands that the \fBpostscreen\fR(8) server considers in
252 /*	violation of the SMTP protocol.
253 /* .IP "\fBpostscreen_helo_required ($smtpd_helo_required)\fR"
254 /*	Require that a remote SMTP client sends HELO or EHLO before
255 /*	commencing a MAIL transaction.
256 /* .IP "\fBpostscreen_non_smtp_command_action (drop)\fR"
257 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client sends
258 /*	non-SMTP commands as specified with the postscreen_forbidden_commands
259 /*	parameter.
260 /* .IP "\fBpostscreen_non_smtp_command_enable (no)\fR"
261 /*	Enable "non-SMTP command" tests in the \fBpostscreen\fR(8) server.
262 /* .IP "\fBpostscreen_pipelining_action (enforce)\fR"
263 /*	The action that \fBpostscreen\fR(8) takes when a remote SMTP client
264 /*	sends
265 /*	multiple commands instead of sending one command and waiting for
266 /*	the server to respond.
267 /* .IP "\fBpostscreen_pipelining_enable (no)\fR"
268 /*	Enable "pipelining" SMTP protocol tests in the \fBpostscreen\fR(8)
269 /*	server.
270 /* CACHE CONTROLS
271 /* .ad
272 /* .fi
273 /* .IP "\fBpostscreen_cache_cleanup_interval (12h)\fR"
274 /*	The amount of time between \fBpostscreen\fR(8) cache cleanup runs.
275 /* .IP "\fBpostscreen_cache_map (btree:$data_directory/postscreen_cache)\fR"
276 /*	Persistent storage for the \fBpostscreen\fR(8) server decisions.
277 /* .IP "\fBpostscreen_cache_retention_time (7d)\fR"
278 /*	The amount of time that \fBpostscreen\fR(8) will cache an expired
279 /*	temporary whitelist entry before it is removed.
280 /* .IP "\fBpostscreen_bare_newline_ttl (30d)\fR"
281 /*	The amount of time that \fBpostscreen\fR(8) will use the result from
282 /*	a successful "bare newline" SMTP protocol test.
283 /* .IP "\fBpostscreen_dnsbl_max_ttl (${postscreen_dnsbl_ttl?{$postscreen_dnsbl_ttl}:{1}}h)\fR"
284 /*	The maximum amount of time that \fBpostscreen\fR(8) will use the
285 /*	result from a successful DNS-based reputation test before a
286 /*	client IP address is required to pass that test again.
287 /* .IP "\fBpostscreen_dnsbl_min_ttl (60s)\fR"
288 /*	The minimum amount of time that \fBpostscreen\fR(8) will use the
289 /*	result from a successful DNS-based reputation test before a
290 /*	client IP address is required to pass that test again.
291 /* .IP "\fBpostscreen_greet_ttl (1d)\fR"
292 /*	The amount of time that \fBpostscreen\fR(8) will use the result from
293 /*	a successful PREGREET test.
294 /* .IP "\fBpostscreen_non_smtp_command_ttl (30d)\fR"
295 /*	The amount of time that \fBpostscreen\fR(8) will use the result from
296 /*	a successful "non_smtp_command" SMTP protocol test.
297 /* .IP "\fBpostscreen_pipelining_ttl (30d)\fR"
298 /*	The amount of time that \fBpostscreen\fR(8) will use the result from
299 /*	a successful "pipelining" SMTP protocol test.
300 /* RESOURCE CONTROLS
301 /* .ad
302 /* .fi
303 /* .IP "\fBline_length_limit (2048)\fR"
304 /*	Upon input, long lines are chopped up into pieces of at most
305 /*	this length; upon delivery, long lines are reconstructed.
306 /* .IP "\fBpostscreen_client_connection_count_limit ($smtpd_client_connection_count_limit)\fR"
307 /*	How many simultaneous connections any remote SMTP client is
308 /*	allowed to have
309 /*	with the \fBpostscreen\fR(8) daemon.
310 /* .IP "\fBpostscreen_command_count_limit (20)\fR"
311 /*	The limit on the total number of commands per SMTP session for
312 /*	\fBpostscreen\fR(8)'s built-in SMTP protocol engine.
313 /* .IP "\fBpostscreen_command_time_limit (normal: 300s, overload: 10s)\fR"
314 /*	The time limit to read an entire command line with \fBpostscreen\fR(8)'s
315 /*	built-in SMTP protocol engine.
316 /* .IP "\fBpostscreen_post_queue_limit ($default_process_limit)\fR"
317 /*	The number of clients that can be waiting for service from a
318 /*	real Postfix SMTP server process.
319 /* .IP "\fBpostscreen_pre_queue_limit ($default_process_limit)\fR"
320 /*	The number of non-whitelisted clients that can be waiting for
321 /*	a decision whether they will receive service from a real Postfix
322 /*	SMTP server
323 /*	process.
324 /* .IP "\fBpostscreen_watchdog_timeout (10s)\fR"
325 /*	How much time a \fBpostscreen\fR(8) process may take to respond to
326 /*	a remote SMTP client command or to perform a cache operation before it
327 /*	is terminated by a built-in watchdog timer.
328 /* STARTTLS CONTROLS
329 /* .ad
330 /* .fi
331 /* .IP "\fBpostscreen_tls_security_level ($smtpd_tls_security_level)\fR"
332 /*	The SMTP TLS security level for the \fBpostscreen\fR(8) server; when
333 /*	a non-empty value is specified, this overrides the obsolete parameters
334 /*	postscreen_use_tls and postscreen_enforce_tls.
335 /* .IP "\fBtlsproxy_service_name (tlsproxy)\fR"
336 /*	The name of the \fBtlsproxy\fR(8) service entry in master.cf.
337 /* OBSOLETE STARTTLS SUPPORT CONTROLS
338 /* .ad
339 /* .fi
340 /*	These parameters are supported for compatibility with
341 /*	\fBsmtpd\fR(8) legacy parameters.
342 /* .IP "\fBpostscreen_use_tls ($smtpd_use_tls)\fR"
343 /*	Opportunistic TLS: announce STARTTLS support to remote SMTP clients,
344 /*	but do not require that clients use TLS encryption.
345 /* .IP "\fBpostscreen_enforce_tls ($smtpd_enforce_tls)\fR"
346 /*	Mandatory TLS: announce STARTTLS support to remote SMTP clients, and
347 /*	require that clients use TLS encryption.
348 /* MISCELLANEOUS CONTROLS
349 /* .ad
350 /* .fi
351 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
352 /*	The default location of the Postfix main.cf and master.cf
353 /*	configuration files.
354 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
355 /*	The maximal number of digits after the decimal point when logging
356 /*	sub-second delay values.
357 /* .IP "\fBcommand_directory (see 'postconf -d' output)\fR"
358 /*	The location of all postfix administrative commands.
359 /* .IP "\fBmax_idle (100s)\fR"
360 /*	The maximum amount of time that an idle Postfix daemon process waits
361 /*	for an incoming connection before terminating voluntarily.
362 /* .IP "\fBprocess_id (read-only)\fR"
363 /*	The process ID of a Postfix command or daemon process.
364 /* .IP "\fBprocess_name (read-only)\fR"
365 /*	The process name of a Postfix command or daemon process.
366 /* .IP "\fBsyslog_facility (mail)\fR"
367 /*	The syslog facility of Postfix logging.
368 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
369 /*	A prefix that is prepended to the process name in syslog
370 /*	records, so that, for example, "smtpd" becomes "prefix/smtpd".
371 /* .PP
372 /*	Available in Postfix 3.3 and later:
373 /* .IP "\fBservice_name (read-only)\fR"
374 /*	The master.cf service name of a Postfix daemon process.
375 /* .PP
376 /*	Available in Postfix 3.5 and later:
377 /* .IP "\fBinfo_log_address_format (external)\fR"
378 /*	The email address form that will be used in non-debug logging
379 /*	(info, warning, etc.).
380 /* SEE ALSO
381 /*	smtpd(8), Postfix SMTP server
382 /*	tlsproxy(8), Postfix TLS proxy server
383 /*	dnsblog(8), DNS black/whitelist logger
384 /*	postlogd(8), Postfix logging
385 /*	syslogd(8), system logging
386 /* README FILES
387 /* .ad
388 /* .fi
389 /*	Use "\fBpostconf readme_directory\fR" or "\fBpostconf
390 /*	html_directory\fR" to locate this information.
391 /* .nf
392 /* .na
393 /*	POSTSCREEN_README, Postfix Postscreen Howto
394 /* LICENSE
395 /* .ad
396 /* .fi
397 /*	The Secure Mailer license must be distributed with this software.
398 /* HISTORY
399 /* .ad
400 /* .fi
401 /*	This service was introduced with Postfix version 2.8.
402 /*
403 /*	Many ideas in \fBpostscreen\fR(8) were explored in earlier
404 /*	work by Michael Tokarev, in OpenBSD spamd, and in MailChannels
405 /*	Traffic Control.
406 /* AUTHOR(S)
407 /*	Wietse Venema
408 /*	IBM T.J. Watson Research
409 /*	P.O. Box 704
410 /*	Yorktown Heights, NY 10598, USA
411 /*
412 /*	Wietse Venema
413 /*	Google, Inc.
414 /*	111 8th Avenue
415 /*	New York, NY 10011, USA
416 /*--*/
417 
418 /* System library. */
419 
420 #include <sys_defs.h>
421 #include <sys/stat.h>
422 #include <stdlib.h>
423 
424 /* Utility library. */
425 
426 #include <msg.h>
427 #include <mymalloc.h>
428 #include <events.h>
429 #include <myaddrinfo.h>
430 #include <dict_cache.h>
431 #include <set_eugid.h>
432 #include <vstream.h>
433 #include <name_code.h>
434 #include <inet_proto.h>
435 
436 /* Global library. */
437 
438 #include <mail_conf.h>
439 #include <mail_params.h>
440 #include <mail_version.h>
441 #include <mail_proto.h>
442 #include <data_redirect.h>
443 #include <string_list.h>
444 
445 /* Master server protocols. */
446 
447 #include <mail_server.h>
448 
449 /* Application-specific. */
450 
451 #include <postscreen.h>
452 
453  /*
454   * Configuration parameters.
455   */
456 char   *var_smtpd_service;
457 char   *var_smtpd_banner;
458 bool    var_disable_vrfy_cmd;
459 bool    var_helo_required;
460 
461 char   *var_smtpd_cmd_filter;
462 char   *var_psc_cmd_filter;
463 
464 char   *var_smtpd_forbid_cmds;
465 char   *var_psc_forbid_cmds;
466 
467 char   *var_smtpd_ehlo_dis_words;
468 char   *var_smtpd_ehlo_dis_maps;
469 char   *var_psc_ehlo_dis_words;
470 char   *var_psc_ehlo_dis_maps;
471 
472 char   *var_smtpd_tls_level;
473 bool    var_smtpd_use_tls;
474 bool    var_smtpd_enforce_tls;
475 char   *var_psc_tls_level;
476 bool    var_psc_use_tls;
477 bool    var_psc_enforce_tls;
478 
479 bool    var_psc_disable_vrfy;
480 bool    var_psc_helo_required;
481 
482 char   *var_psc_cache_map;
483 int     var_psc_cache_scan;
484 int     var_psc_cache_ret;
485 int     var_psc_post_queue_limit;
486 int     var_psc_pre_queue_limit;
487 int     var_psc_watchdog;
488 
489 char   *var_psc_acl;
490 char   *var_psc_blist_action;
491 
492 char   *var_psc_greet_ttl;
493 int     var_psc_greet_wait;
494 
495 char   *var_psc_pregr_banner;
496 char   *var_psc_pregr_action;
497 int     var_psc_pregr_ttl;
498 
499 char   *var_psc_dnsbl_sites;
500 char   *var_psc_dnsbl_reply;
501 int     var_psc_dnsbl_thresh;
502 int     var_psc_dnsbl_wthresh;
503 char   *var_psc_dnsbl_action;
504 int     var_psc_dnsbl_min_ttl;
505 int     var_psc_dnsbl_max_ttl;
506 int     var_psc_dnsbl_tmout;
507 
508 bool    var_psc_pipel_enable;
509 char   *var_psc_pipel_action;
510 int     var_psc_pipel_ttl;
511 
512 bool    var_psc_nsmtp_enable;
513 char   *var_psc_nsmtp_action;
514 int     var_psc_nsmtp_ttl;
515 
516 bool    var_psc_barlf_enable;
517 char   *var_psc_barlf_action;
518 int     var_psc_barlf_ttl;
519 
520 int     var_psc_cmd_count;
521 int     var_psc_cmd_time;
522 
523 char   *var_dnsblog_service;
524 char   *var_tlsproxy_service;
525 
526 char   *var_smtpd_rej_footer;
527 char   *var_psc_rej_footer;
528 char   *var_psc_rej_ftr_maps;
529 
530 int     var_smtpd_cconn_limit;
531 int     var_psc_cconn_limit;
532 
533 char   *var_smtpd_exp_filter;
534 char   *var_psc_exp_filter;
535 
536 char   *var_psc_wlist_if;
537 char   *var_psc_uproxy_proto;
538 int     var_psc_uproxy_tmout;
539 
540  /*
541   * Global variables.
542   */
543 int     psc_check_queue_length;		/* connections being checked */
544 int     psc_post_queue_length;		/* being sent to real SMTPD */
545 DICT_CACHE *psc_cache_map;		/* cache table handle */
546 VSTRING *psc_temp;			/* scratchpad */
547 char   *psc_smtpd_service_name;		/* path to real SMTPD */
548 int     psc_pregr_action;		/* PSC_ACT_DROP/ENFORCE/etc */
549 int     psc_dnsbl_action;		/* PSC_ACT_DROP/ENFORCE/etc */
550 int     psc_pipel_action;		/* PSC_ACT_DROP/ENFORCE/etc */
551 int     psc_nsmtp_action;		/* PSC_ACT_DROP/ENFORCE/etc */
552 int     psc_barlf_action;		/* PSC_ACT_DROP/ENFORCE/etc */
553 int     psc_min_ttl;			/* Update with new tests! */
554 STRING_LIST *psc_forbid_cmds;		/* CONNECT GET POST */
555 int     psc_stress_greet_wait;		/* stressed greet wait */
556 int     psc_normal_greet_wait;		/* stressed greet wait */
557 int     psc_stress_cmd_time_limit;	/* stressed command limit */
558 int     psc_normal_cmd_time_limit;	/* normal command time limit */
559 int     psc_stress;			/* stress level */
560 int     psc_lowat_check_queue_length;	/* stress low-water mark */
561 int     psc_hiwat_check_queue_length;	/* stress high-water mark */
562 DICT   *psc_dnsbl_reply;		/* DNSBL name mapper */
563 HTABLE *psc_client_concurrency;		/* per-client concurrency */
564 
565  /*
566   * Local variables and functions.
567   */
568 static ARGV *psc_acl;			/* permanent white/backlist */
569 static int psc_blist_action;		/* PSC_ACT_DROP/ENFORCE/etc */
570 static ADDR_MATCH_LIST *psc_wlist_if;	/* whitelist interfaces */
571 
572 static void psc_endpt_lookup_done(int, VSTREAM *,
573 			             MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *,
574 			            MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *);
575 
576 /* psc_dump - dump some statistics before exit */
577 
578 static void psc_dump(char *unused_service, char **unused_argv)
579 {
580 
581     /*
582      * Dump preliminary cache cleanup statistics when the process commits
583      * suicide while a cache cleanup run is in progress. We can't currently
584      * distinguish between "postfix reload" (we should restart) or "maximal
585      * idle time reached" (we could finish the cache cleanup first).
586      */
587     if (psc_cache_map) {
588 	dict_cache_close(psc_cache_map);
589 	psc_cache_map = 0;
590     }
591 }
592 
593 /* psc_drain - delayed exit after "postfix reload" */
594 
595 static void psc_drain(char *unused_service, char **unused_argv)
596 {
597     int     count;
598 
599     /*
600      * After "postfix reload", complete work-in-progress in the background,
601      * instead of dropping already-accepted connections on the floor.
602      *
603      * Unfortunately we must close all writable tables, so we can't store or
604      * look up reputation information. The reason is that we don't have any
605      * multi-writer safety guarantees. We also can't use the single-writer
606      * proxywrite service, because its latency guarantees are too weak.
607      *
608      * All error retry counts shall be limited. Instead of blocking here, we
609      * could retry failed fork() operations in the event call-back routines,
610      * but we don't need perfection. The host system is severely overloaded
611      * and service levels are already way down.
612      *
613      * XXX Some Berkeley DB versions break with close-after-fork. Every new
614      * version is an improvement over its predecessor.
615      *
616      * XXX Don't assume that it is OK to share the same LMDB lockfile descriptor
617      * between different processes.
618      */
619     if (psc_cache_map != 0			/* XXX && psc_cache_map
620 	    requires locking */ ) {
621 	dict_cache_close(psc_cache_map);
622 	psc_cache_map = 0;
623     }
624     for (count = 0; /* see below */ ; count++) {
625 	if (count >= 5) {
626 	    msg_fatal("fork: %m");
627 	} else if (event_server_drain() != 0) {
628 	    msg_warn("fork: %m");
629 	    sleep(1);
630 	    continue;
631 	} else {
632 	    return;
633 	}
634     }
635 }
636 
637 /* psc_service - handle new client connection */
638 
639 static void psc_service(VSTREAM *smtp_client_stream,
640 			        char *unused_service,
641 			        char **unused_argv)
642 {
643 
644     /*
645      * For sanity, require that at least one of INET or INET6 is enabled.
646      * Otherwise, we can't look up interface information, and we can't
647      * convert names or addresses.
648      */
649     if (inet_proto_info()->ai_family_list[0] == 0)
650 	msg_fatal("all network protocols are disabled (%s = %s)",
651 		  VAR_INET_PROTOCOLS, var_inet_protocols);
652 
653     /*
654      * This program handles all incoming connections, so it must not block.
655      * We use event-driven code for all operations that introduce latency.
656      *
657      * Note: instead of using VSTREAM-level timeouts, we enforce limits on the
658      * total amount of time to receive a complete SMTP command line.
659      */
660     non_blocking(vstream_fileno(smtp_client_stream), NON_BLOCKING);
661 
662     /*
663      * Look up the remote SMTP client address and port.
664      */
665     psc_endpt_lookup(smtp_client_stream, psc_endpt_lookup_done);
666 }
667 
668 /* psc_endpt_lookup_done - endpoint lookup completed */
669 
670 static void psc_endpt_lookup_done(int endpt_status,
671 				          VSTREAM *smtp_client_stream,
672 				          MAI_HOSTADDR_STR *smtp_client_addr,
673 				          MAI_SERVPORT_STR *smtp_client_port,
674 				          MAI_HOSTADDR_STR *smtp_server_addr,
675 				          MAI_SERVPORT_STR *smtp_server_port)
676 {
677     const char *myname = "psc_endpt_lookup_done";
678     PSC_STATE *state;
679     const char *stamp_str;
680     int     saved_flags;
681 
682     /*
683      * Best effort - if this non-blocking write(2) fails, so be it.
684      */
685     if (endpt_status < 0) {
686 	(void) write(vstream_fileno(smtp_client_stream),
687 		     "421 4.3.2 No system resources\r\n",
688 		     sizeof("421 4.3.2 No system resources\r\n") - 1);
689 	event_server_disconnect(smtp_client_stream);
690 	return;
691     }
692     if (msg_verbose > 1)
693 	msg_info("%s: sq=%d cq=%d connect from [%s]:%s",
694 		 myname, psc_post_queue_length, psc_check_queue_length,
695 		 smtp_client_addr->buf, smtp_client_port->buf);
696 
697     msg_info("CONNECT from [%s]:%s to [%s]:%s",
698 	     smtp_client_addr->buf, smtp_client_port->buf,
699 	     smtp_server_addr->buf, smtp_server_port->buf);
700 
701     /*
702      * Bundle up all the loose session pieces. This zeroes all flags and time
703      * stamps.
704      */
705     state = psc_new_session_state(smtp_client_stream, smtp_client_addr->buf,
706 				  smtp_client_port->buf,
707 				  smtp_server_addr->buf,
708 				  smtp_server_port->buf);
709 
710     /*
711      * Reply with 421 when the client has too many open connections.
712      */
713     if (var_psc_cconn_limit > 0
714 	&& state->client_info->concurrency > var_psc_cconn_limit) {
715 	msg_info("NOQUEUE: reject: CONNECT from [%s]:%s: too many connections",
716 		 state->smtp_client_addr, state->smtp_client_port);
717 	PSC_DROP_SESSION_STATE(state,
718 			       "421 4.7.0 Error: too many connections\r\n");
719 	return;
720     }
721 
722     /*
723      * Reply with 421 when we can't forward more connections.
724      */
725     if (var_psc_post_queue_limit > 0
726 	&& psc_post_queue_length >= var_psc_post_queue_limit) {
727 	msg_info("NOQUEUE: reject: CONNECT from [%s]:%s: all server ports busy",
728 		 state->smtp_client_addr, state->smtp_client_port);
729 	PSC_DROP_SESSION_STATE(state,
730 			       "421 4.3.2 All server ports are busy\r\n");
731 	return;
732     }
733 
734     /*
735      * The permanent white/blacklist has highest precedence.
736      */
737     if (psc_acl != 0) {
738 	switch (psc_acl_eval(state, psc_acl, VAR_PSC_ACL)) {
739 
740 	    /*
741 	     * Permanently blacklisted.
742 	     */
743 	case PSC_ACL_ACT_BLACKLIST:
744 	    msg_info("BLACKLISTED [%s]:%s", PSC_CLIENT_ADDR_PORT(state));
745 	    PSC_FAIL_SESSION_STATE(state, PSC_STATE_FLAG_BLIST_FAIL);
746 	    switch (psc_blist_action) {
747 	    case PSC_ACT_DROP:
748 		PSC_DROP_SESSION_STATE(state,
749 			     "521 5.3.2 Service currently unavailable\r\n");
750 		return;
751 	    case PSC_ACT_ENFORCE:
752 		PSC_ENFORCE_SESSION_STATE(state,
753 			     "550 5.3.2 Service currently unavailable\r\n");
754 		break;
755 	    case PSC_ACT_IGNORE:
756 		PSC_UNFAIL_SESSION_STATE(state, PSC_STATE_FLAG_BLIST_FAIL);
757 
758 		/*
759 		 * Not: PSC_PASS_SESSION_STATE. Repeat this test the next
760 		 * time.
761 		 */
762 		break;
763 	    default:
764 		msg_panic("%s: unknown blacklist action value %d",
765 			  myname, psc_blist_action);
766 	    }
767 	    break;
768 
769 	    /*
770 	     * Permanently whitelisted.
771 	     */
772 	case PSC_ACL_ACT_WHITELIST:
773 	    msg_info("WHITELISTED [%s]:%s", PSC_CLIENT_ADDR_PORT(state));
774 	    psc_conclude(state);
775 	    return;
776 
777 	    /*
778 	     * Other: dunno (don't know) or error.
779 	     */
780 	default:
781 	    break;
782 	}
783     }
784 
785     /*
786      * The temporary whitelist (i.e. the postscreen cache) has the lowest
787      * precedence. This cache contains information about the results of prior
788      * tests. Whitelist the client when all enabled test results are still
789      * valid.
790      */
791     if ((state->flags & PSC_STATE_MASK_ANY_FAIL) == 0
792 	&& state->client_info->concurrency == 1
793 	&& psc_cache_map != 0
794 	&& (stamp_str = psc_cache_lookup(psc_cache_map, state->smtp_client_addr)) != 0) {
795 	saved_flags = state->flags;
796 	psc_parse_tests(state, stamp_str, event_time());
797 	state->flags |= saved_flags;
798 	if (msg_verbose)
799 	    msg_info("%s: cached + recent flags: %s",
800 		     myname, psc_print_state_flags(state->flags, myname));
801 	if ((state->flags & PSC_STATE_MASK_ANY_TODO_FAIL) == 0) {
802 	    msg_info("PASS OLD [%s]:%s", PSC_CLIENT_ADDR_PORT(state));
803 	    psc_conclude(state);
804 	    return;
805 	}
806     } else if (state->client_info->concurrency > 1) {
807 	saved_flags = state->flags;
808 	psc_todo_tests(state, event_time());
809 	state->flags |= saved_flags;
810 	if (msg_verbose)
811 	    msg_info("%s: new + recent flags: %s",
812 		     myname, psc_print_state_flags(state->flags, myname));
813     } else {
814 	saved_flags = state->flags;
815 	psc_new_tests(state);
816 	state->flags |= saved_flags;
817 	if (msg_verbose)
818 	    msg_info("%s: new + recent flags: %s",
819 		     myname, psc_print_state_flags(state->flags, myname));
820     }
821 
822     /*
823      * Don't whitelist clients that connect to backup MX addresses. Fail
824      * "closed" on error.
825      */
826     if (addr_match_list_match(psc_wlist_if, smtp_server_addr->buf) == 0) {
827 	state->flags |= (PSC_STATE_FLAG_WLIST_FAIL | PSC_STATE_FLAG_NOFORWARD);
828 	msg_info("WHITELIST VETO [%s]:%s", PSC_CLIENT_ADDR_PORT(state));
829     }
830 
831     /*
832      * Reply with 421 when we can't analyze more connections. That also means
833      * no deep protocol tests when the noforward flag is raised.
834      */
835     if (var_psc_pre_queue_limit > 0
836 	&& psc_check_queue_length - psc_post_queue_length
837 	>= var_psc_pre_queue_limit) {
838 	msg_info("reject: connect from [%s]:%s: all screening ports busy",
839 		 state->smtp_client_addr, state->smtp_client_port);
840 	PSC_DROP_SESSION_STATE(state,
841 			       "421 4.3.2 All screening ports are busy\r\n");
842 	return;
843     }
844 
845     /*
846      * If the client has no up-to-date results for some tests, do those tests
847      * first. Otherwise, skip the tests and hand off the connection.
848      */
849     if (state->flags & PSC_STATE_MASK_EARLY_TODO)
850 	psc_early_tests(state);
851     else if (state->flags & (PSC_STATE_MASK_SMTPD_TODO | PSC_STATE_FLAG_NOFORWARD))
852 	psc_smtpd_tests(state);
853     else
854 	psc_conclude(state);
855 }
856 
857 /* psc_cache_validator - validate one cache entry */
858 
859 static int psc_cache_validator(const char *client_addr,
860 			               const char *stamp_str,
861 			               void *unused_context)
862 {
863     PSC_STATE dummy_state;
864     PSC_CLIENT_INFO dummy_client_info;
865 
866     /*
867      * This function is called by the cache cleanup pseudo thread.
868      *
869      * When an entry is removed from the cache, the client will be reported as
870      * "NEW" in the next session where it passes all tests again. To avoid
871      * silly logging we remove the cache entry only after all tests have
872      * expired longer ago than the cache retention time.
873      */
874     dummy_state.client_info = &dummy_client_info;
875     psc_parse_tests(&dummy_state, stamp_str, event_time() - var_psc_cache_ret);
876     return ((dummy_state.flags & PSC_STATE_MASK_ANY_TODO) == 0);
877 }
878 
879 /* pre_jail_init - pre-jail initialization */
880 
881 static void pre_jail_init(char *unused_name, char **unused_argv)
882 {
883     VSTRING *redirect;
884 
885     /*
886      * Open read-only maps before dropping privilege, for consistency with
887      * other Postfix daemons.
888      */
889     psc_acl_pre_jail_init(var_mynetworks, VAR_PSC_ACL);
890     if (*var_psc_acl)
891 	psc_acl = psc_acl_parse(var_psc_acl, VAR_PSC_ACL);
892     /* Ignore smtpd_forbid_cmds lookup errors. Non-critical feature. */
893     if (*var_psc_forbid_cmds)
894 	psc_forbid_cmds = string_list_init(VAR_PSC_FORBID_CMDS,
895 					   MATCH_FLAG_RETURN,
896 					   var_psc_forbid_cmds);
897     if (*var_psc_dnsbl_reply)
898 	psc_dnsbl_reply = dict_open(var_psc_dnsbl_reply, O_RDONLY,
899 				    DICT_FLAG_DUP_WARN);
900 
901     /*
902      * Never, ever, get killed by a master signal, as that would corrupt the
903      * database when we're in the middle of an update.
904      */
905     if (setsid() < 0)
906 	msg_warn("setsid: %m");
907 
908     /*
909      * Security: don't create root-owned files that contain untrusted data.
910      * And don't create Postfix-owned files in root-owned directories,
911      * either. We want a correct relationship between (file or directory)
912      * ownership and (file or directory) content. To open files before going
913      * to jail, temporarily drop root privileges.
914      */
915     SAVE_AND_SET_EUGID(var_owner_uid, var_owner_gid);
916     redirect = vstring_alloc(100);
917 
918     /*
919      * Keep state in persistent external map. As a safety measure we sync the
920      * database on each update. This hurts on LINUX file systems that sync
921      * all dirty disk blocks whenever any application invokes fsync().
922      *
923      * Start the cache maintenance pseudo thread after dropping privileges.
924      */
925 #define PSC_DICT_OPEN_FLAGS (DICT_FLAG_DUP_REPLACE | DICT_FLAG_SYNC_UPDATE | \
926 	    DICT_FLAG_OPEN_LOCK)
927 
928     if (*var_psc_cache_map)
929 	psc_cache_map =
930 	    dict_cache_open(data_redirect_map(redirect, var_psc_cache_map),
931 			    O_CREAT | O_RDWR, PSC_DICT_OPEN_FLAGS);
932 
933     /*
934      * Clean up and restore privilege.
935      */
936     vstring_free(redirect);
937     RESTORE_SAVED_EUGID();
938 
939     /*
940      * Initialize the dummy SMTP engine.
941      */
942     psc_smtpd_pre_jail_init();
943 }
944 
945 /* pre_accept - see if tables have changed */
946 
947 static void pre_accept(char *unused_name, char **unused_argv)
948 {
949     static time_t last_event_time;
950     time_t  new_event_time;
951     const char *name;
952 
953     /*
954      * If some table has changed then stop accepting new connections. Don't
955      * check the tables more than once a second.
956      */
957     new_event_time = event_time();
958     if (new_event_time >= last_event_time + 1
959 	&& (name = dict_changed_name()) != 0) {
960 	msg_info("table %s has changed - finishing in the background", name);
961 	event_server_drain();
962     } else {
963 	last_event_time = new_event_time;
964     }
965 }
966 
967 /* post_jail_init - post-jail initialization */
968 
969 static void post_jail_init(char *unused_name, char **unused_argv)
970 {
971     const NAME_CODE actions[] = {
972 	PSC_NAME_ACT_DROP, PSC_ACT_DROP,
973 	PSC_NAME_ACT_ENFORCE, PSC_ACT_ENFORCE,
974 	PSC_NAME_ACT_IGNORE, PSC_ACT_IGNORE,
975 	PSC_NAME_ACT_CONT, PSC_ACT_IGNORE,	/* compatibility */
976 	0, -1,
977     };
978     int     cache_flags;
979     const char *tmp;
980 
981     /*
982      * This routine runs after the skeleton code has entered the chroot jail.
983      * Prevent automatic process suicide after a limited number of client
984      * requests. It is OK to terminate after a limited amount of idle time.
985      */
986     var_use_limit = 0;
987 
988     /*
989      * Workaround for parameters whose values may contain "$", and that have
990      * a default of "$parametername". Not sure if it would be a good idea to
991      * always to this in the mail_conf_raw(3) module.
992      */
993     if (*var_psc_rej_footer == '$'
994 	&& mail_conf_lookup(var_psc_rej_footer + 1)) {
995 	tmp = mail_conf_eval_once(var_psc_rej_footer);
996 	myfree(var_psc_rej_footer);
997 	var_psc_rej_footer = mystrdup(tmp);
998     }
999     if (*var_psc_exp_filter == '$'
1000 	&& mail_conf_lookup(var_psc_exp_filter + 1)) {
1001 	tmp = mail_conf_eval_once(var_psc_exp_filter);
1002 	myfree(var_psc_exp_filter);
1003 	var_psc_exp_filter = mystrdup(tmp);
1004     }
1005 
1006     /*
1007      * Other one-time initialization.
1008      */
1009     psc_temp = vstring_alloc(10);
1010     vstring_sprintf(psc_temp, "%s/%s", MAIL_CLASS_PRIVATE, var_smtpd_service);
1011     psc_smtpd_service_name = mystrdup(STR(psc_temp));
1012     psc_dnsbl_init();
1013     psc_early_init();
1014     psc_smtpd_init();
1015 
1016     if ((psc_blist_action = name_code(actions, NAME_CODE_FLAG_NONE,
1017 				      var_psc_blist_action)) < 0)
1018 	msg_fatal("bad %s value: %s", VAR_PSC_BLIST_ACTION,
1019 		  var_psc_blist_action);
1020     if ((psc_dnsbl_action = name_code(actions, NAME_CODE_FLAG_NONE,
1021 				      var_psc_dnsbl_action)) < 0)
1022 	msg_fatal("bad %s value: %s", VAR_PSC_DNSBL_ACTION,
1023 		  var_psc_dnsbl_action);
1024     if ((psc_pregr_action = name_code(actions, NAME_CODE_FLAG_NONE,
1025 				      var_psc_pregr_action)) < 0)
1026 	msg_fatal("bad %s value: %s", VAR_PSC_PREGR_ACTION,
1027 		  var_psc_pregr_action);
1028     if ((psc_pipel_action = name_code(actions, NAME_CODE_FLAG_NONE,
1029 				      var_psc_pipel_action)) < 0)
1030 	msg_fatal("bad %s value: %s", VAR_PSC_PIPEL_ACTION,
1031 		  var_psc_pipel_action);
1032     if ((psc_nsmtp_action = name_code(actions, NAME_CODE_FLAG_NONE,
1033 				      var_psc_nsmtp_action)) < 0)
1034 	msg_fatal("bad %s value: %s", VAR_PSC_NSMTP_ACTION,
1035 		  var_psc_nsmtp_action);
1036     if ((psc_barlf_action = name_code(actions, NAME_CODE_FLAG_NONE,
1037 				      var_psc_barlf_action)) < 0)
1038 	msg_fatal("bad %s value: %s", VAR_PSC_BARLF_ACTION,
1039 		  var_psc_barlf_action);
1040     /* Fail "closed" on error. */
1041     psc_wlist_if = addr_match_list_init(VAR_PSC_WLIST_IF, MATCH_FLAG_RETURN,
1042 					var_psc_wlist_if);
1043 
1044     /*
1045      * Start the cache maintenance pseudo thread last. Early cleanup makes
1046      * verbose logging more informative (we get positive confirmation that
1047      * the cleanup thread runs).
1048      */
1049     cache_flags = DICT_CACHE_FLAG_STATISTICS;
1050     if (msg_verbose > 1)
1051 	cache_flags |= DICT_CACHE_FLAG_VERBOSE;
1052     if (psc_cache_map != 0 && var_psc_cache_scan > 0)
1053 	dict_cache_control(psc_cache_map,
1054 			   CA_DICT_CACHE_CTL_FLAGS(cache_flags),
1055 			   CA_DICT_CACHE_CTL_INTERVAL(var_psc_cache_scan),
1056 			   CA_DICT_CACHE_CTL_VALIDATOR(psc_cache_validator),
1057 			   CA_DICT_CACHE_CTL_CONTEXT((void *) 0),
1058 			   CA_DICT_CACHE_CTL_END);
1059 
1060     /*
1061      * Pre-compute the minimal and maximal TTL.
1062      */
1063     psc_min_ttl =
1064 	PSC_MIN(PSC_MIN(var_psc_pregr_ttl, var_psc_dnsbl_min_ttl),
1065 		PSC_MIN(PSC_MIN(var_psc_pipel_ttl, var_psc_nsmtp_ttl),
1066 			var_psc_barlf_ttl));
1067 
1068     /*
1069      * Pre-compute the stress and normal command time limits.
1070      */
1071     mail_conf_update(VAR_STRESS, "yes");
1072     psc_stress_cmd_time_limit =
1073 	get_mail_conf_time(VAR_PSC_CMD_TIME, DEF_PSC_CMD_TIME, 1, 0);
1074     psc_stress_greet_wait =
1075 	get_mail_conf_time(VAR_PSC_GREET_WAIT, DEF_PSC_GREET_WAIT, 1, 0);
1076 
1077     mail_conf_update(VAR_STRESS, "");
1078     psc_normal_cmd_time_limit =
1079 	get_mail_conf_time(VAR_PSC_CMD_TIME, DEF_PSC_CMD_TIME, 1, 0);
1080     psc_normal_greet_wait =
1081 	get_mail_conf_time(VAR_PSC_GREET_WAIT, DEF_PSC_GREET_WAIT, 1, 0);
1082 
1083     psc_lowat_check_queue_length = .7 * var_psc_pre_queue_limit;
1084     psc_hiwat_check_queue_length = .9 * var_psc_pre_queue_limit;
1085     if (msg_verbose)
1086 	msg_info(VAR_PSC_CMD_TIME ": stress=%d normal=%d lowat=%d hiwat=%d",
1087 		 psc_stress_cmd_time_limit, psc_normal_cmd_time_limit,
1088 		 psc_lowat_check_queue_length, psc_hiwat_check_queue_length);
1089 
1090     if (psc_lowat_check_queue_length == 0)
1091 	msg_panic("compiler error: 0.7 * %d = %d", var_psc_pre_queue_limit,
1092 		  psc_lowat_check_queue_length);
1093     if (psc_hiwat_check_queue_length == 0)
1094 	msg_panic("compiler error: 0.9 * %d = %d", var_psc_pre_queue_limit,
1095 		  psc_hiwat_check_queue_length);
1096 
1097     /*
1098      * Per-client concurrency.
1099      */
1100     psc_client_concurrency = htable_create(var_psc_pre_queue_limit);
1101 }
1102 
1103 MAIL_VERSION_STAMP_DECLARE;
1104 
1105 /* main - pass control to the multi-threaded skeleton */
1106 
1107 int     main(int argc, char **argv)
1108 {
1109 
1110     /*
1111      * List smtpd(8) parameters before any postscreen(8) parameters that have
1112      * defaults dependencies on them.
1113      */
1114     static const CONFIG_STR_TABLE str_table[] = {
1115 	VAR_SMTPD_SERVICE, DEF_SMTPD_SERVICE, &var_smtpd_service, 1, 0,
1116 	VAR_SMTPD_BANNER, DEF_SMTPD_BANNER, &var_smtpd_banner, 1, 0,
1117 	VAR_SMTPD_FORBID_CMDS, DEF_SMTPD_FORBID_CMDS, &var_smtpd_forbid_cmds, 0, 0,
1118 	VAR_SMTPD_EHLO_DIS_WORDS, DEF_SMTPD_EHLO_DIS_WORDS, &var_smtpd_ehlo_dis_words, 0, 0,
1119 	VAR_SMTPD_EHLO_DIS_MAPS, DEF_SMTPD_EHLO_DIS_MAPS, &var_smtpd_ehlo_dis_maps, 0, 0,
1120 	VAR_SMTPD_TLS_LEVEL, DEF_SMTPD_TLS_LEVEL, &var_smtpd_tls_level, 0, 0,
1121 	VAR_SMTPD_CMD_FILTER, DEF_SMTPD_CMD_FILTER, &var_smtpd_cmd_filter, 0, 0,
1122 	VAR_PSC_CACHE_MAP, DEF_PSC_CACHE_MAP, &var_psc_cache_map, 0, 0,
1123 	VAR_PSC_PREGR_BANNER, DEF_PSC_PREGR_BANNER, &var_psc_pregr_banner, 0, 0,
1124 	VAR_PSC_PREGR_ACTION, DEF_PSC_PREGR_ACTION, &var_psc_pregr_action, 1, 0,
1125 	VAR_PSC_DNSBL_SITES, DEF_PSC_DNSBL_SITES, &var_psc_dnsbl_sites, 0, 0,
1126 	VAR_PSC_DNSBL_ACTION, DEF_PSC_DNSBL_ACTION, &var_psc_dnsbl_action, 1, 0,
1127 	VAR_PSC_PIPEL_ACTION, DEF_PSC_PIPEL_ACTION, &var_psc_pipel_action, 1, 0,
1128 	VAR_PSC_NSMTP_ACTION, DEF_PSC_NSMTP_ACTION, &var_psc_nsmtp_action, 1, 0,
1129 	VAR_PSC_BARLF_ACTION, DEF_PSC_BARLF_ACTION, &var_psc_barlf_action, 1, 0,
1130 	VAR_PSC_ACL, DEF_PSC_ACL, &var_psc_acl, 0, 0,
1131 	VAR_PSC_BLIST_ACTION, DEF_PSC_BLIST_ACTION, &var_psc_blist_action, 1, 0,
1132 	VAR_PSC_FORBID_CMDS, DEF_PSC_FORBID_CMDS, &var_psc_forbid_cmds, 0, 0,
1133 	VAR_PSC_EHLO_DIS_WORDS, DEF_PSC_EHLO_DIS_WORDS, &var_psc_ehlo_dis_words, 0, 0,
1134 	VAR_PSC_EHLO_DIS_MAPS, DEF_PSC_EHLO_DIS_MAPS, &var_psc_ehlo_dis_maps, 0, 0,
1135 	VAR_PSC_DNSBL_REPLY, DEF_PSC_DNSBL_REPLY, &var_psc_dnsbl_reply, 0, 0,
1136 	VAR_PSC_TLS_LEVEL, DEF_PSC_TLS_LEVEL, &var_psc_tls_level, 0, 0,
1137 	VAR_PSC_CMD_FILTER, DEF_PSC_CMD_FILTER, &var_psc_cmd_filter, 0, 0,
1138 	VAR_DNSBLOG_SERVICE, DEF_DNSBLOG_SERVICE, &var_dnsblog_service, 1, 0,
1139 	VAR_TLSPROXY_SERVICE, DEF_TLSPROXY_SERVICE, &var_tlsproxy_service, 1, 0,
1140 	VAR_PSC_WLIST_IF, DEF_PSC_WLIST_IF, &var_psc_wlist_if, 0, 0,
1141 	VAR_PSC_UPROXY_PROTO, DEF_PSC_UPROXY_PROTO, &var_psc_uproxy_proto, 0, 0,
1142 	VAR_PSC_REJ_FTR_MAPS, DEF_PSC_REJ_FTR_MAPS, &var_psc_rej_ftr_maps, 0, 0,
1143 	0,
1144     };
1145     static const CONFIG_INT_TABLE int_table[] = {
1146 	VAR_PSC_DNSBL_THRESH, DEF_PSC_DNSBL_THRESH, &var_psc_dnsbl_thresh, 0, 0,
1147 	VAR_PSC_DNSBL_WTHRESH, DEF_PSC_DNSBL_WTHRESH, &var_psc_dnsbl_wthresh, 0, 0,
1148 	VAR_PSC_CMD_COUNT, DEF_PSC_CMD_COUNT, &var_psc_cmd_count, 1, 0,
1149 	VAR_SMTPD_CCONN_LIMIT, DEF_SMTPD_CCONN_LIMIT, &var_smtpd_cconn_limit, 0, 0,
1150 	0,
1151     };
1152     static const CONFIG_NINT_TABLE nint_table[] = {
1153 	VAR_PSC_POST_QLIMIT, DEF_PSC_POST_QLIMIT, &var_psc_post_queue_limit, 5, 0,
1154 	VAR_PSC_PRE_QLIMIT, DEF_PSC_PRE_QLIMIT, &var_psc_pre_queue_limit, 10, 0,
1155 	VAR_PSC_CCONN_LIMIT, DEF_PSC_CCONN_LIMIT, &var_psc_cconn_limit, 0, 0,
1156 	0,
1157     };
1158     static const CONFIG_TIME_TABLE time_table[] = {
1159 	VAR_PSC_CMD_TIME, DEF_PSC_CMD_TIME, &var_psc_cmd_time, 1, 0,
1160 	VAR_PSC_GREET_WAIT, DEF_PSC_GREET_WAIT, &var_psc_greet_wait, 1, 0,
1161 	VAR_PSC_PREGR_TTL, DEF_PSC_PREGR_TTL, &var_psc_pregr_ttl, 1, 0,
1162 	VAR_PSC_DNSBL_MIN_TTL, DEF_PSC_DNSBL_MIN_TTL, &var_psc_dnsbl_min_ttl, 1, 0,
1163 	VAR_PSC_DNSBL_MAX_TTL, DEF_PSC_DNSBL_MAX_TTL, &var_psc_dnsbl_max_ttl, 1, 0,
1164 	VAR_PSC_PIPEL_TTL, DEF_PSC_PIPEL_TTL, &var_psc_pipel_ttl, 1, 0,
1165 	VAR_PSC_NSMTP_TTL, DEF_PSC_NSMTP_TTL, &var_psc_nsmtp_ttl, 1, 0,
1166 	VAR_PSC_BARLF_TTL, DEF_PSC_BARLF_TTL, &var_psc_barlf_ttl, 1, 0,
1167 	VAR_PSC_CACHE_RET, DEF_PSC_CACHE_RET, &var_psc_cache_ret, 1, 0,
1168 	VAR_PSC_CACHE_SCAN, DEF_PSC_CACHE_SCAN, &var_psc_cache_scan, 0, 0,
1169 	VAR_PSC_WATCHDOG, DEF_PSC_WATCHDOG, &var_psc_watchdog, 10, 0,
1170 	VAR_PSC_UPROXY_TMOUT, DEF_PSC_UPROXY_TMOUT, &var_psc_uproxy_tmout, 1, 0,
1171 	VAR_PSC_DNSBL_TMOUT, DEF_PSC_DNSBL_TMOUT, &var_psc_dnsbl_tmout, 1, 0,
1172 
1173 	0,
1174     };
1175     static const CONFIG_BOOL_TABLE bool_table[] = {
1176 	VAR_HELO_REQUIRED, DEF_HELO_REQUIRED, &var_helo_required,
1177 	VAR_DISABLE_VRFY_CMD, DEF_DISABLE_VRFY_CMD, &var_disable_vrfy_cmd,
1178 	VAR_SMTPD_USE_TLS, DEF_SMTPD_USE_TLS, &var_smtpd_use_tls,
1179 	VAR_SMTPD_ENFORCE_TLS, DEF_SMTPD_ENFORCE_TLS, &var_smtpd_enforce_tls,
1180 	VAR_PSC_PIPEL_ENABLE, DEF_PSC_PIPEL_ENABLE, &var_psc_pipel_enable,
1181 	VAR_PSC_NSMTP_ENABLE, DEF_PSC_NSMTP_ENABLE, &var_psc_nsmtp_enable,
1182 	VAR_PSC_BARLF_ENABLE, DEF_PSC_BARLF_ENABLE, &var_psc_barlf_enable,
1183 	0,
1184     };
1185     static const CONFIG_RAW_TABLE raw_table[] = {
1186 	VAR_SMTPD_REJ_FOOTER, DEF_SMTPD_REJ_FOOTER, &var_smtpd_rej_footer, 0, 0,
1187 	VAR_PSC_REJ_FOOTER, DEF_PSC_REJ_FOOTER, &var_psc_rej_footer, 0, 0,
1188 	VAR_SMTPD_EXP_FILTER, DEF_SMTPD_EXP_FILTER, &var_smtpd_exp_filter, 1, 0,
1189 	VAR_PSC_EXP_FILTER, DEF_PSC_EXP_FILTER, &var_psc_exp_filter, 1, 0,
1190 	0,
1191     };
1192     static const CONFIG_NBOOL_TABLE nbool_table[] = {
1193 	VAR_PSC_HELO_REQUIRED, DEF_PSC_HELO_REQUIRED, &var_psc_helo_required,
1194 	VAR_PSC_DISABLE_VRFY, DEF_PSC_DISABLE_VRFY, &var_psc_disable_vrfy,
1195 	VAR_PSC_USE_TLS, DEF_PSC_USE_TLS, &var_psc_use_tls,
1196 	VAR_PSC_ENFORCE_TLS, DEF_PSC_ENFORCE_TLS, &var_psc_enforce_tls,
1197 	0,
1198     };
1199 
1200     /*
1201      * Fingerprint executables and core dumps.
1202      */
1203     MAIL_VERSION_STAMP_ALLOCATE;
1204 
1205     event_server_main(argc, argv, psc_service,
1206 		      CA_MAIL_SERVER_STR_TABLE(str_table),
1207 		      CA_MAIL_SERVER_INT_TABLE(int_table),
1208 		      CA_MAIL_SERVER_NINT_TABLE(nint_table),
1209 		      CA_MAIL_SERVER_TIME_TABLE(time_table),
1210 		      CA_MAIL_SERVER_BOOL_TABLE(bool_table),
1211 		      CA_MAIL_SERVER_RAW_TABLE(raw_table),
1212 		      CA_MAIL_SERVER_NBOOL_TABLE(nbool_table),
1213 		      CA_MAIL_SERVER_PRE_INIT(pre_jail_init),
1214 		      CA_MAIL_SERVER_POST_INIT(post_jail_init),
1215 		      CA_MAIL_SERVER_PRE_ACCEPT(pre_accept),
1216 		      CA_MAIL_SERVER_SOLITARY,
1217 		      CA_MAIL_SERVER_SLOW_EXIT(psc_drain),
1218 		      CA_MAIL_SERVER_EXIT(psc_dump),
1219 		      CA_MAIL_SERVER_WATCHDOG(&var_psc_watchdog),
1220 		      0);
1221 }
1222