xref: /netbsd-src/external/ibm-public/postfix/dist/src/tlsproxy/tlsproxy.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: tlsproxy.c,v 1.1.1.1 2011/03/02 19:32:39 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	tlsproxy 8
6 /* SUMMARY
7 /*	Postfix TLS proxy
8 /* SYNOPSIS
9 /*	\fBtlsproxy\fR [generic Postfix daemon options]
10 /* DESCRIPTION
11 /*	The \fBtlsproxy\fR(8) server implements a server-side TLS
12 /*	proxy. It is used by \fBpostscreen\fR(8) to talk SMTP-over-TLS
13 /*	with remote SMTP clients whose whitelist status has expired,
14 /*	but it should also work for non-SMTP protocols.
15 /*
16 /*	Although one \fBtlsproxy\fR(8) process can serve multiple
17 /*	sessions at the same time, it is a good idea to allow the
18 /*	number of processes to increase with load, so that the
19 /*	service remains responsive.
20 /* PROTOCOL EXAMPLE
21 /* .ad
22 /* .fi
23 /*	The example below concerns \fBpostscreen\fR(8). However,
24 /*	the \fBtlsproxy\fR(8) server is agnostic of the application
25 /*	protocol, and the example is easily adapted to other
26 /*	applications.
27 /*
28 /*	The \fBpostscreen\fR(8) server sends the remote SMTP client
29 /*	endpoint string, the requested role (server), and the
30 /*	requested timeout to \fBtlsproxy\fR(8).  \fBpostscreen\fR(8)
31 /*	then receives a "TLS available" indication from \fBtlsproxy\fR(8).
32 /*	If the TLS service is available, \fBpostscreen\fR(8) sends
33 /*	the remote SMTP client file descriptor to \fBtlsproxy\fR(8),
34 /*	and sends the plaintext 220 greeting to the remote SMTP
35 /*	client.  This triggers TLS negotiations between the remote
36 /*	SMTP client and \fBtlsproxy\fR(8).  Upon completion of the
37 /*	TLS-level handshake, \fBtlsproxy\fR(8) translates between
38 /*	plaintext from/to \fBpostscreen\fR(8) and ciphertext to/from
39 /*	the remote SMTP client.
40 /* SECURITY
41 /* .ad
42 /* .fi
43 /*	The \fBtlsproxy\fR(8) server is moderately security-sensitive.
44 /*	It talks to untrusted clients on the network. The process
45 /*	can be run chrooted at fixed low privilege.
46 /* DIAGNOSTICS
47 /*	Problems and transactions are logged to \fBsyslogd\fR(8).
48 /* CONFIGURATION PARAMETERS
49 /* .ad
50 /* .fi
51 /*	Changes to \fBmain.cf\fR are not picked up automatically,
52 /*	as \fBtlsproxy\fR(8) processes may run for a long time
53 /*	depending on mail server load.  Use the command "\fBpostfix
54 /*	reload\fR" to speed up a change.
55 /*
56 /*	The text below provides only a parameter summary. See
57 /*	\fBpostconf\fR(5) for more details including examples.
58 /* STARTTLS SUPPORT CONTROLS
59 /* .ad
60 /* .fi
61 /* .IP "\fBtlsproxy_tls_CAfile ($smtpd_tls_CAfile)\fR"
62 /*	A file containing (PEM format) CA certificates of root CAs
63 /*	trusted to sign either remote SMTP client certificates or intermediate
64 /*	CA certificates.
65 /* .IP "\fBtlsproxy_tls_CApath ($smtpd_tls_CApath)\fR"
66 /*	A directory containing (PEM format) CA certificates of root CAs
67 /*	trusted to sign either remote SMTP client certificates or intermediate
68 /*	CA certificates.
69 /* .IP "\fBtlsproxy_tls_always_issue_session_ids ($smtpd_tls_always_issue_session_ids)\fR"
70 /*	Force the Postfix \fBtlsproxy\fR(8) server to issue a TLS session id,
71 /*	even when TLS session caching is turned off.
72 /* .IP "\fBtlsproxy_tls_ask_ccert ($smtpd_tls_ask_ccert)\fR"
73 /*	Ask a remote SMTP client for a client certificate.
74 /* .IP "\fBtlsproxy_tls_ccert_verifydepth ($smtpd_tls_ccert_verifydepth)\fR"
75 /*	The verification depth for remote SMTP client certificates.
76 /* .IP "\fBtlsproxy_tls_cert_file ($smtpd_tls_cert_file)\fR"
77 /*	File with the Postfix \fBtlsproxy\fR(8) server RSA certificate in PEM
78 /*	format.
79 /* .IP "\fBtlsproxy_tls_ciphers ($smtpd_tls_ciphers)\fR"
80 /*	The minimum TLS cipher grade that the Postfix \fBtlsproxy\fR(8) server
81 /*	will use with opportunistic TLS encryption.
82 /* .IP "\fBtlsproxy_tls_dcert_file ($smtpd_tls_dcert_file)\fR"
83 /*	File with the Postfix \fBtlsproxy\fR(8) server DSA certificate in PEM
84 /*	format.
85 /* .IP "\fBtlsproxy_tls_dh1024_param_file ($smtpd_tls_dh1024_param_file)\fR"
86 /*	File with DH parameters that the Postfix \fBtlsproxy\fR(8) server
87 /*	should use with EDH ciphers.
88 /* .IP "\fBtlsproxy_tls_dh512_param_file ($smtpd_tls_dh512_param_file)\fR"
89 /*	File with DH parameters that the Postfix \fBtlsproxy\fR(8) server
90 /*	should use with EDH ciphers.
91 /* .IP "\fBtlsproxy_tls_dkey_file ($smtpd_tls_dkey_file)\fR"
92 /*	File with the Postfix \fBtlsproxy\fR(8) server DSA private key in PEM
93 /*	format.
94 /* .IP "\fBtlsproxy_tls_eccert_file ($smtpd_tls_eccert_file)\fR"
95 /*	File with the Postfix \fBtlsproxy\fR(8) server ECDSA certificate in
96 /*	PEM format.
97 /* .IP "\fBtlsproxy_tls_eckey_file ($smtpd_tls_eckey_file)\fR"
98 /*	File with the Postfix \fBtlsproxy\fR(8) server ECDSA private key in
99 /*	PEM format.
100 /* .IP "\fBtlsproxy_tls_eecdh_grade ($smtpd_tls_eecdh_grade)\fR"
101 /*	The Postfix \fBtlsproxy\fR(8) server security grade for ephemeral
102 /*	elliptic-curve Diffie-Hellman (EECDH) key exchange.
103 /* .IP "\fBtlsproxy_tls_exclude_ciphers ($smtpd_tls_exclude_ciphers)\fR"
104 /*	List of ciphers or cipher types to exclude from the \fBtlsproxy\fR(8)
105 /*	server cipher list at all TLS security levels.
106 /* .IP "\fBtlsproxy_tls_fingerprint_digest ($smtpd_tls_fingerprint_digest)\fR"
107 /*	The message digest algorithm used to construct client-certificate
108 /*	fingerprints.
109 /* .IP "\fBtlsproxy_tls_key_file ($smtpd_tls_key_file)\fR"
110 /*	File with the Postfix \fBtlsproxy\fR(8) server RSA private key in PEM
111 /*	format.
112 /* .IP "\fBtlsproxy_tls_loglevel ($smtpd_tls_loglevel)\fR"
113 /*	Enable additional Postfix \fBtlsproxy\fR(8) server logging of TLS
114 /*	activity.
115 /* .IP "\fBtlsproxy_tls_mandatory_ciphers ($smtpd_tls_mandatory_ciphers)\fR"
116 /*	The minimum TLS cipher grade that the Postfix \fBtlsproxy\fR(8) server
117 /*	will use with mandatory TLS encryption.
118 /* .IP "\fBtlsproxy_tls_mandatory_exclude_ciphers ($smtpd_tls_mandatory_exclude_ciphers)\fR"
119 /*	Additional list of ciphers or cipher types to exclude from the
120 /*	\fBtlsproxy\fR(8) server cipher list at mandatory TLS security levels.
121 /* .IP "\fBtlsproxy_tls_mandatory_protocols ($smtpd_tls_mandatory_protocols)\fR"
122 /*	The SSL/TLS protocols accepted by the Postfix \fBtlsproxy\fR(8) server
123 /*	with mandatory TLS encryption.
124 /* .IP "\fBtlsproxy_tls_protocols ($smtpd_tls_protocols)\fR"
125 /*	List of TLS protocols that the Postfix \fBtlsproxy\fR(8) server will
126 /*	exclude or include with opportunistic TLS encryption.
127 /* .IP "\fBtlsproxy_tls_req_ccert ($smtpd_tls_req_ccert)\fR"
128 /*	With mandatory TLS encryption, require a trusted remote SMTP
129 /*	client certificate in order to allow TLS connections to proceed.
130 /* .IP "\fBtlsproxy_tls_security_level ($smtpd_tls_security_level)\fR"
131 /*	The SMTP TLS security level for the Postfix \fBtlsproxy\fR(8) server;
132 /*	when a non-empty value is specified, this overrides the obsolete
133 /*	parameters smtpd_use_tls and smtpd_enforce_tls.
134 /* .IP "\fBtlsproxy_tls_session_cache_timeout ($smtpd_tls_session_cache_timeout)\fR"
135 /*	The expiration time of Postfix \fBtlsproxy\fR(8) server TLS session
136 /*	cache information.
137 /* OBSOLETE STARTTLS SUPPORT CONTROLS
138 /* .ad
139 /* .fi
140 /*	These parameters are supported for compatibility with
141 /*	\fBsmtpd\fR(8) legacy parameters.
142 /* .IP "\fBtlsproxy_use_tls ($smtpd_use_tls)\fR"
143 /*	Opportunistic TLS: announce STARTTLS support to SMTP clients,
144 /*	but do not require that clients use TLS encryption.
145 /* .IP "\fBtlsproxy_enforce_tls ($smtpd_enforce_tls)\fR"
146 /*	Mandatory TLS: announce STARTTLS support to SMTP clients, and
147 /*	require that clients use TLS encryption.
148 /* RESOURCE CONTROLS
149 /* .ad
150 /* .fi
151 /* .IP "\fBtlsproxy_watchdog_timeout (10s)\fR"
152 /*	How much time a \fBtlsproxy\fR(8) process may take to process local
153 /*	or remote I/O before it is terminated by a built-in watchdog timer.
154 /* MISCELLANEOUS CONTROLS
155 /* .ad
156 /* .fi
157 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
158 /*	The default location of the Postfix main.cf and master.cf
159 /*	configuration files.
160 /* .IP "\fBprocess_id (read-only)\fR"
161 /*	The process ID of a Postfix command or daemon process.
162 /* .IP "\fBprocess_name (read-only)\fR"
163 /*	The process name of a Postfix command or daemon process.
164 /* .IP "\fBsyslog_facility (mail)\fR"
165 /*	The syslog facility of Postfix logging.
166 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
167 /*	The mail system name that is prepended to the process name in syslog
168 /*	records, so that "smtpd" becomes, for example, "postfix/smtpd".
169 /* SEE ALSO
170 /*	postscreen(8), Postfix zombie blocker
171 /*	smtpd(8), Postfix SMTP server
172 /*	postconf(5), configuration parameters
173 /*	syslogd(5), system logging
174 /* LICENSE
175 /* .ad
176 /* .fi
177 /*	The Secure Mailer license must be distributed with this software.
178 /* HISTORY
179 /* .ad
180 /* .fi
181 /*	This service was introduced with Postfix version 2.8.
182 /* AUTHOR(S)
183 /*	Wietse Venema
184 /*	IBM T.J. Watson Research
185 /*	P.O. Box 704
186 /*	Yorktown Heights, NY 10598, USA
187 /*--*/
188 
189  /*
190   * System library.
191   */
192 #include <sys_defs.h>
193 
194  /*
195   * Utility library.
196   */
197 #include <msg.h>
198 #include <vstream.h>
199 #include <iostuff.h>
200 #include <nbbio.h>
201 #include <mymalloc.h>
202 
203  /*
204   * Global library.
205   */
206 #include <mail_proto.h>
207 #include <mail_params.h>
208 #include <mail_conf.h>
209 #include <mail_version.h>
210 
211  /*
212   * Master library.
213   */
214 #include <mail_server.h>
215 
216  /*
217   * TLS library.
218   */
219 #ifdef USE_TLS
220 #define TLS_INTERNAL			/* XXX */
221 #include <tls.h>
222 #include <tls_proxy.h>
223 
224  /*
225   * Application-specific.
226   */
227 #include <tlsproxy.h>
228 
229  /*
230   * Tunable parameters. We define our clones of the smtpd(8) parameters to
231   * avoid any confusion about which parameters are used by this program.
232   */
233 int     var_smtpd_tls_ccert_vd;
234 int     var_smtpd_tls_loglevel;
235 int     var_smtpd_tls_scache_timeout;
236 bool    var_smtpd_use_tls;
237 bool    var_smtpd_enforce_tls;
238 bool    var_smtpd_tls_ask_ccert;
239 bool    var_smtpd_tls_req_ccert;
240 bool    var_smtpd_tls_set_sessid;
241 char   *var_smtpd_relay_ccerts;
242 char   *var_smtpd_tls_cert_file;
243 char   *var_smtpd_tls_key_file;
244 char   *var_smtpd_tls_dcert_file;
245 char   *var_smtpd_tls_dkey_file;
246 char   *var_smtpd_tls_eccert_file;
247 char   *var_smtpd_tls_eckey_file;
248 char   *var_smtpd_tls_CAfile;
249 char   *var_smtpd_tls_CApath;
250 char   *var_smtpd_tls_ciph;
251 char   *var_smtpd_tls_mand_ciph;
252 char   *var_smtpd_tls_excl_ciph;
253 char   *var_smtpd_tls_mand_excl;
254 char   *var_smtpd_tls_proto;
255 char   *var_smtpd_tls_mand_proto;
256 char   *var_smtpd_tls_dh512_param_file;
257 char   *var_smtpd_tls_dh1024_param_file;
258 char   *var_smtpd_tls_eecdh;
259 char   *var_smtpd_tls_fpt_dgst;
260 char   *var_smtpd_tls_level;
261 
262 int     var_tlsp_tls_ccert_vd;
263 int     var_tlsp_tls_loglevel;
264 int     var_tlsp_tls_scache_timeout;
265 bool    var_tlsp_use_tls;
266 bool    var_tlsp_enforce_tls;
267 bool    var_tlsp_tls_ask_ccert;
268 bool    var_tlsp_tls_req_ccert;
269 bool    var_tlsp_tls_set_sessid;
270 char   *var_tlsp_tls_cert_file;
271 char   *var_tlsp_tls_key_file;
272 char   *var_tlsp_tls_dcert_file;
273 char   *var_tlsp_tls_dkey_file;
274 char   *var_tlsp_tls_eccert_file;
275 char   *var_tlsp_tls_eckey_file;
276 char   *var_tlsp_tls_CAfile;
277 char   *var_tlsp_tls_CApath;
278 char   *var_tlsp_tls_ciph;
279 char   *var_tlsp_tls_mand_ciph;
280 char   *var_tlsp_tls_excl_ciph;
281 char   *var_tlsp_tls_mand_excl;
282 char   *var_tlsp_tls_proto;
283 char   *var_tlsp_tls_mand_proto;
284 char   *var_tlsp_tls_dh512_param_file;
285 char   *var_tlsp_tls_dh1024_param_file;
286 char   *var_tlsp_tls_eecdh;
287 char   *var_tlsp_tls_fpt_dgst;
288 char   *var_tlsp_tls_level;
289 
290 int     var_tlsp_watchdog;
291 
292  /*
293   * TLS per-process status.
294   */
295 static TLS_APPL_STATE *tlsp_server_ctx;
296 static int ask_client_cert;
297 
298  /*
299   * SLMs.
300   */
301 #define STR(x)	vstring_str(x)
302 
303  /*
304   * This code looks simpler than expected. That is the result of a great deal
305   * of effort, mainly in design and analysis.
306   *
307   * By design, postscreen(8) is an event-driven server that must scale up to a
308   * large number of clients. This means that postscreen(8) must avoid doing
309   * CPU-intensive operations such as those in OpenSSL.
310   *
311   * tlsproxy(8) runs the OpenSSL code on behalf of postscreen(8), translating
312   * plaintext SMTP messages from postscreen(8) into SMTP-over-TLS messages to
313   * the remote SMTP client, and vice versa. As long as postscreen(8) does not
314   * receive email messages, the cost of doing TLS operations will be modest.
315   *
316   * Like postscreen(8), one tlsproxy(8) process services multiple remote SMTP
317   * clients. Unlike postscreen(8), there can be more than one tlsproxy(8)
318   * process, although their number is meant to be much smaller than the
319   * number of remote SMTP clients that talk TLS.
320   *
321   * As with postscreen(8), all I/O must be event-driven: encrypted traffic
322   * between tlsproxy(8) and remote SMTP clients, and plaintext traffic
323   * between tlsproxy(8) and postscreen(8). Event-driven plaintext I/O is
324   * straightforward enough that it could be abstracted away with the nbbio(3)
325   * module.
326   *
327   * The event-driven TLS I/O implementation is founded on on-line OpenSSL
328   * documentation, supplemented by statements from OpenSSL developers on
329   * public mailing lists. After some field experience with this code, we may
330   * be able to factor it out as a library module, like nbbio(3), that can
331   * become part of the TLS library.
332   */
333 
334 static void tlsp_ciphertext_event(int, char *);
335 
336 #define TLSP_INIT_TIMEOUT	100
337 
338 /* tlsp_drain - delayed exit after "postfix reload" */
339 
340 static void tlsp_drain(char *unused_service, char **unused_argv)
341 {
342     int     count;
343 
344     /*
345      * After "postfix reload", complete work-in-progress in the background,
346      * instead of dropping already-accepted connections on the floor.
347      *
348      * All error retry counts shall be limited. Instead of blocking here, we
349      * could retry failed fork() operations in the event call-back routines,
350      * but we don't need perfection. The host system is severely overloaded
351      * and service levels are already way down.
352      */
353     for (count = 0; /* see below */ ; count++) {
354 	if (count >= 5) {
355 	    msg_fatal("fork: %m");
356 	} else if (event_server_drain() != 0) {
357 	    msg_warn("fork: %m");
358 	    sleep(1);
359 	    continue;
360 	} else {
361 	    return;
362 	}
363     }
364 }
365 
366 /* tlsp_eval_tls_error - translate TLS "error" result into action */
367 
368 static int tlsp_eval_tls_error(TLSP_STATE *state, int err)
369 {
370     int     ciphertext_fd = state->ciphertext_fd;
371 
372     /*
373      * The ciphertext file descriptor is in non-blocking mode, meaning that
374      * each SSL_accept/connect/read/write/shutdown request may return an
375      * "error" indication that it needs to read or write more ciphertext. The
376      * purpose of this routine is to translate those "error" indications into
377      * the appropriate read/write/timeout event requests.
378      */
379     switch (err) {
380 
381 	/*
382 	 * No error from SSL_read and SSL_write means that the plaintext
383 	 * output buffer is full and that the plaintext input buffer is
384 	 * empty. Stop read/write events on the ciphertext stream. Keep the
385 	 * timer alive as a safety mechanism for the case that the plaintext
386 	 * pseudothreads get stuck.
387 	 */
388     case SSL_ERROR_NONE:
389 	if (state->ssl_last_err != SSL_ERROR_NONE) {
390 	    event_disable_readwrite(ciphertext_fd);
391 	    event_request_timer(tlsp_ciphertext_event, (char *) state,
392 				state->timeout);
393 	    state->ssl_last_err = SSL_ERROR_NONE;
394 	}
395 	return (0);
396 
397 	/*
398 	 * The TLS engine wants to write to the network. Turn on
399 	 * write/timeout events on the ciphertext stream.
400 	 */
401     case SSL_ERROR_WANT_WRITE:
402 	if (state->ssl_last_err == SSL_ERROR_WANT_READ)
403 	    event_disable_readwrite(ciphertext_fd);
404 	if (state->ssl_last_err != SSL_ERROR_WANT_WRITE) {
405 	    event_enable_write(ciphertext_fd, tlsp_ciphertext_event,
406 			       (char *) state);
407 	    state->ssl_last_err = SSL_ERROR_WANT_WRITE;
408 	}
409 	event_request_timer(tlsp_ciphertext_event, (char *) state,
410 			    state->timeout);
411 	return (0);
412 
413 	/*
414 	 * The TLS engine wants to read from the network. Turn on
415 	 * read/timeout events on the ciphertext stream.
416 	 */
417     case SSL_ERROR_WANT_READ:
418 	if (state->ssl_last_err == SSL_ERROR_WANT_WRITE)
419 	    event_disable_readwrite(ciphertext_fd);
420 	if (state->ssl_last_err != SSL_ERROR_WANT_READ) {
421 	    event_enable_read(ciphertext_fd, tlsp_ciphertext_event,
422 			      (char *) state);
423 	    state->ssl_last_err = SSL_ERROR_WANT_READ;
424 	}
425 	event_request_timer(tlsp_ciphertext_event, (char *) state,
426 			    state->timeout);
427 	return (0);
428 
429 	/*
430 	 * Some error. Self-destruct. This automagically cleans up all
431 	 * pending read/write and timeout event requests, making state a
432 	 * dangling pointer.
433 	 */
434     case SSL_ERROR_SSL:
435 	tls_print_errors();
436 	/* FALLTHROUGH */
437     default:
438 	tlsp_state_free(state);
439 	return (-1);
440     }
441 }
442 
443 /* tlsp_strategy - decide what to read or write next. */
444 
445 static void tlsp_strategy(TLSP_STATE *state)
446 {
447     TLS_SESS_STATE *tls_context = state->tls_context;
448     NBBIO  *plaintext_buf;
449     int     ssl_stat;
450     int     ssl_read_err;
451     int     ssl_write_err;
452     int     handshake_err;
453 
454     /*
455      * Be sure to complete the TLS handshake before enabling plain-text I/O.
456      * In case of an unrecoverable error, this automagically cleans up all
457      * pending read/write and timeout event requests.
458      */
459     if (state->flags & TLSP_FLAG_DO_HANDSHAKE) {
460 	ssl_stat = SSL_accept(tls_context->con);
461 	if (ssl_stat != 1) {
462 	    handshake_err = SSL_get_error(tls_context->con, ssl_stat);
463 	    tlsp_eval_tls_error(state, handshake_err);
464 	    /* At this point, state could be a dangling pointer. */
465 	    return;
466 	}
467 	if ((state->tls_context = tls_server_post_accept(tls_context)) == 0) {
468 	    tlsp_state_free(state);
469 	    return;
470 	}
471 	if ((state->req_flags & TLS_PROXY_FLAG_SEND_CONTEXT) != 0
472 	    && (attr_print(state->plaintext_stream, ATTR_FLAG_NONE,
473 			   ATTR_TYPE_FUNC, tls_proxy_context_print,
474 			   (char *) state->tls_context, ATTR_TYPE_END) != 0
475 		|| vstream_fflush(state->plaintext_stream) != 0)) {
476 	    msg_warn("cannot send TLS context: %m");
477 	    tlsp_state_free(state);
478 	    return;
479 	}
480 	state->flags &= ~TLSP_FLAG_DO_HANDSHAKE;
481     }
482 
483     /*
484      * Shutdown and self-destruct after NBBIO error. This automagically
485      * cleans up all pending read/write and timeout event requests. Before
486      * shutting down TLS, we stop all plain-text I/O events but keep the
487      * NBBIO error flags.
488      */
489     plaintext_buf = state->plaintext_buf;
490     if (NBBIO_ERROR_FLAGS(plaintext_buf)) {
491 	if (NBBIO_ACTIVE_FLAGS(plaintext_buf))
492 	    nbbio_disable_readwrite(state->plaintext_buf);
493 	ssl_stat = SSL_shutdown(tls_context->con);
494 	/* XXX Wait for return value 1 if sessions are to be reused? */
495 	if (ssl_stat < 0) {
496 	    handshake_err = SSL_get_error(tls_context->con, ssl_stat);
497 	    tlsp_eval_tls_error(state, handshake_err);
498 	    /* At this point, state could be a dangling pointer. */
499 	    return;
500 	}
501 	tlsp_state_free(state);
502 	return;
503     }
504 
505     /*
506      * Try to move data from the plaintext input buffer to the TLS engine.
507      *
508      * XXX We're supposed to repeat the exact same SSL_write() call arguments
509      * after an SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE result. Rumor has
510      * it that this is because each SSL_write() call reads from the buffer
511      * incrementally, and returns > 0 only after the final byte is processed.
512      * Rumor also has it that setting SSL_MODE_ENABLE_PARTIAL_WRITE and
513      * SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER voids this requirement, and that
514      * repeating the request with an increased request size is OK.
515      * Unfortunately all this is not or poorly documented, and one has to
516      * rely on statements from OpenSSL developers in public mailing archives.
517      */
518     ssl_write_err = SSL_ERROR_NONE;
519     while (NBBIO_READ_PEND(plaintext_buf) > 0) {
520 	ssl_stat = SSL_write(tls_context->con, NBBIO_READ_BUF(plaintext_buf),
521 			     NBBIO_READ_PEND(plaintext_buf));
522 	ssl_write_err = SSL_get_error(tls_context->con, ssl_stat);
523 	if (ssl_write_err != SSL_ERROR_NONE)
524 	    break;
525 	/* Allow the plaintext pseudothread to read more data. */
526 	NBBIO_READ_PEND(plaintext_buf) -= ssl_stat;
527 	if (NBBIO_READ_PEND(plaintext_buf) > 0)
528 	    memmove(NBBIO_READ_BUF(plaintext_buf),
529 		    NBBIO_READ_BUF(plaintext_buf) + ssl_stat,
530 		    NBBIO_READ_PEND(plaintext_buf));
531     }
532 
533     /*
534      * Try to move data from the TLS engine to the plaintext output buffer.
535      * Note: data may arrive as a side effect of calling SSL_write(),
536      * therefore we call SSL_read() after calling SSL_write().
537      *
538      * XXX We're supposed to repeat the exact same SSL_read() call arguments
539      * after an SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE result. This
540      * supposedly means that our plaintext writer must not memmove() the
541      * plaintext output buffer until after the SSL_read() call succeeds. For
542      * now I'll ignore this, because 1) SSL_read() is documented to return
543      * the bytes available, instead of returning > 0 only after the entire
544      * buffer is processed like SSL_write() does; and 2) there is no "read"
545      * equivalent of the SSL_R_BAD_WRITE_RETRY, SSL_MODE_ENABLE_PARTIAL_WRITE
546      * or SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER features.
547      */
548     ssl_read_err = SSL_ERROR_NONE;
549     while (NBBIO_WRITE_PEND(state->plaintext_buf) < NBBIO_BUFSIZE(plaintext_buf)) {
550 	ssl_stat = SSL_read(tls_context->con,
551 			    NBBIO_WRITE_BUF(plaintext_buf)
552 			    + NBBIO_WRITE_PEND(state->plaintext_buf),
553 			    NBBIO_BUFSIZE(plaintext_buf)
554 			    - NBBIO_WRITE_PEND(state->plaintext_buf));
555 	ssl_read_err = SSL_get_error(tls_context->con, ssl_stat);
556 	if (ssl_read_err != SSL_ERROR_NONE)
557 	    break;
558 	NBBIO_WRITE_PEND(plaintext_buf) += ssl_stat;
559     }
560 
561     /*
562      * Try to enable/disable ciphertext read/write events. If SSL_write() was
563      * satisfied, see if SSL_read() wants to do some work. In case of an
564      * unrecoverable error, this automagically destroys the session state
565      * after cleaning up all pending read/write and timeout event requests.
566      */
567     if (tlsp_eval_tls_error(state, ssl_write_err != SSL_ERROR_NONE ?
568 			    ssl_write_err : ssl_read_err) < 0)
569 	return;
570 
571     /*
572      * Try to enable/disable plaintext read/write events. Basically, if we
573      * have nothing to write to the postscreen(8) server, see if there is
574      * something to read. If the write buffer is empty and the read buffer is
575      * full, suspend plaintext I/O until conditions change (but keep the
576      * timer active, as a safety mechanism in case ciphertext I/O gets
577      * stuck).
578      *
579      * XXX In theory, if the client keeps writing fast enough then we would
580      * never read from postscreen(8), and cause postscreen(8) to block. In
581      * practice, postscreen(8) limits the number of client commands, and thus
582      * postscreen(8)'s output will fit in a kernel buffer. This may not be
583      * true in other scenarios where the tlsproxy(8) server could be used.
584      */
585     if (NBBIO_WRITE_PEND(plaintext_buf) > 0) {
586 	if (NBBIO_ACTIVE_FLAGS(plaintext_buf) & NBBIO_FLAG_READ)
587 	    nbbio_disable_readwrite(plaintext_buf);
588 	if ((NBBIO_ACTIVE_FLAGS(plaintext_buf) & NBBIO_FLAG_WRITE) == 0)
589 	    nbbio_enable_write(plaintext_buf, state->timeout);
590     } else if (NBBIO_READ_PEND(plaintext_buf) < NBBIO_BUFSIZE(plaintext_buf)) {
591 	if (NBBIO_ACTIVE_FLAGS(plaintext_buf) & NBBIO_FLAG_WRITE)
592 	    nbbio_disable_readwrite(plaintext_buf);
593 	if ((NBBIO_ACTIVE_FLAGS(plaintext_buf) & NBBIO_FLAG_READ) == 0)
594 	    nbbio_enable_read(plaintext_buf, state->timeout);
595     } else {
596 	if (NBBIO_ACTIVE_FLAGS(plaintext_buf))
597 	    nbbio_slumber(plaintext_buf, state->timeout);
598     }
599 }
600 
601 /* tlsp_plaintext_event - plaintext was read/written */
602 
603 static void tlsp_plaintext_event(int event, char *context)
604 {
605     TLSP_STATE *state = (TLSP_STATE *) context;
606 
607     /*
608      * Safety alert: the plaintext pseudothreads have "slumbered" for too
609      * long (see code above). This means that the ciphertext pseudothreads
610      * are stuck.
611      */
612     if ((NBBIO_ERROR_FLAGS(state->plaintext_buf) & NBBIO_FLAG_TIMEOUT) != 0
613 	&& NBBIO_ACTIVE_FLAGS(state->plaintext_buf) == 0)
614 	msg_warn("deadlock on ciphertext stream for %s", state->remote_endpt);
615 
616     /*
617      * This is easy, because the NBBIO layer has already done the event
618      * decoding and plaintext I/O for us. All we need to do is decide if we
619      * want to read or write more plaintext.
620      */
621     tlsp_strategy(state);
622 }
623 
624 /* tlsp_ciphertext_event - ciphertext is ready to read/write */
625 
626 static void tlsp_ciphertext_event(int event, char *context)
627 {
628     TLSP_STATE *state = (TLSP_STATE *) context;
629 
630     /*
631      * Without a TLS quivalent of the NBBIO layer, we must decode the events
632      * ourselves and do the ciphertext I/O. Then, we can decide if we want to
633      * read or write more ciphertext.
634      */
635     if (event == EVENT_READ || event == EVENT_WRITE) {
636 	tlsp_strategy(state);
637     } else {
638 	if (event == EVENT_TIME && state->ssl_last_err == SSL_ERROR_NONE)
639 	    msg_warn("deadlock on plaintext stream for %s",
640 		     state->remote_endpt);
641 	else
642 	    msg_warn("ciphertext read/write %s for %s",
643 		     event == EVENT_TIME ? "timeout" : "error",
644 		     state->remote_endpt);
645 	tlsp_state_free(state);
646     }
647 }
648 
649 /* tlsp_start_tls - turn on TLS or force disconnect */
650 
651 static void tlsp_start_tls(TLSP_STATE *state)
652 {
653     TLS_SERVER_START_PROPS props;
654     static char *cipher_grade;
655     static VSTRING *cipher_exclusions;
656 
657     /*
658      * The code in this routine is pasted literally from smtpd(8). I am not
659      * going to sanitize this because doing so surely will break things in
660      * unexpected ways.
661      */
662 
663     /*
664      * Perform the before-handshake portion of the per-session initalization.
665      * Pass a null VSTREAM to indicate that this program, will do the
666      * ciphertext I/O, not libtls.
667      *
668      * The cipher grade and exclusions don't change between sessions. Compute
669      * just once and cache.
670      */
671 #define ADD_EXCLUDE(vstr, str) \
672     do { \
673 	if (*(str)) \
674 	    vstring_sprintf_append((vstr), "%s%s", \
675 				   VSTRING_LEN(vstr) ? " " : "", (str)); \
676     } while (0)
677 
678     if (cipher_grade == 0) {
679 	cipher_grade =
680 	    var_tlsp_enforce_tls ? var_tlsp_tls_mand_ciph : var_tlsp_tls_ciph;
681 	cipher_exclusions = vstring_alloc(10);
682 	ADD_EXCLUDE(cipher_exclusions, var_tlsp_tls_excl_ciph);
683 	if (var_tlsp_enforce_tls)
684 	    ADD_EXCLUDE(cipher_exclusions, var_tlsp_tls_mand_excl);
685 	if (ask_client_cert)
686 	    ADD_EXCLUDE(cipher_exclusions, "aNULL");
687     }
688     state->tls_context =
689 	TLS_SERVER_START(&props,
690 			 ctx = tlsp_server_ctx,
691 			 stream = (VSTREAM *) 0,/* unused */
692 			 fd = state->ciphertext_fd,
693 			 log_level = var_tlsp_tls_loglevel,
694 			 timeout = 0,		/* unused */
695 			 requirecert = (var_tlsp_tls_req_ccert
696 					&& var_tlsp_enforce_tls),
697 			 serverid = state->service,
698 			 namaddr = state->remote_endpt,
699 			 cipher_grade = cipher_grade,
700 			 cipher_exclusions = STR(cipher_exclusions),
701 			 fpt_dgst = var_tlsp_tls_fpt_dgst);
702 
703     if (state->tls_context == 0) {
704 	tlsp_state_free(state);
705 	return;
706     }
707 
708     /*
709      * XXX Do we care about TLS session rate limits? Good postscreen(8)
710      * clients will occasionally require the tlsproxy to renew their
711      * whitelist status, but bad clients hammering the server can suck up
712      * lots of CPU cycles. Per-client concurrency limits in postscreen(8)
713      * will divert only naive security "researchers".
714      *
715      * XXX Do we care about certificate verification results? Not as long as
716      * postscreen(8) doesn't actually receive email.
717      */
718 }
719 
720 /* tlsp_get_fd_event - receive final postscreen(8) hand-off information */
721 
722 static void tlsp_get_fd_event(int event, char *context)
723 {
724     const char *myname = "tlsp_get_fd_event";
725     TLSP_STATE *state = (TLSP_STATE *) context;
726     int     plaintext_fd = vstream_fileno(state->plaintext_stream);
727 
728     /*
729      * At this point we still manually manage plaintext read/write/timeout
730      * events. Disable I/O and timer events. Don't assume that the first
731      * plaintext request will be a read.
732      */
733     event_disable_readwrite(plaintext_fd);
734     if (event != EVENT_TIME)
735 	event_cancel_timer(tlsp_get_fd_event, (char *) state);
736 
737     /*
738      * Initialize plaintext-related session state.  Once we have this behind
739      * us, the TLSP_STATE destructor will automagically clean up requests for
740      * read/write/timeout events, which makes error recovery easier.
741      *
742      * Register the plaintext event handler for timer cleanup in the TLSP_STATE
743      * destructor. Insert the NBBIO event-driven I/O layer between the
744      * postscreen(8) server and the TLS engine.
745      */
746     if (event != EVENT_READ
747 	|| (state->ciphertext_fd = LOCAL_RECV_FD(plaintext_fd)) < 0) {
748 	msg_warn("%s: receive SMTP client file descriptor: %m", myname);
749 	tlsp_state_free(state);
750 	return;
751     }
752     non_blocking(state->ciphertext_fd, NON_BLOCKING);
753     state->ciphertext_timer = tlsp_ciphertext_event;
754     state->plaintext_buf = nbbio_create(plaintext_fd,
755 					VSTREAM_BUFSIZE, "postscreen",
756 					tlsp_plaintext_event,
757 					(char *) state);
758 
759     /*
760      * Perform the TLS layer before-handshake initialization. We perform the
761      * remainder after the TLS handshake completes.
762      */
763     tlsp_start_tls(state);
764 
765     /*
766      * Trigger the initial proxy server I/Os.
767      */
768     tlsp_strategy(state);
769 }
770 
771 /* tlsp_get_request_event - receive initial postscreen(8) hand-off info */
772 
773 static void tlsp_get_request_event(int event, char *context)
774 {
775     const char *myname = "tlsp_get_request_event";
776     TLSP_STATE *state = (TLSP_STATE *) context;
777     VSTREAM *plaintext_stream = state->plaintext_stream;
778     int     plaintext_fd = vstream_fileno(plaintext_stream);
779     static VSTRING *remote_endpt;
780     int     req_flags;
781     int     timeout;
782     int     ready;
783 
784     /*
785      * One-time initialization.
786      */
787     if (remote_endpt == 0)
788 	remote_endpt = vstring_alloc(10);
789 
790     /*
791      * At this point we still manually manage plaintext read/write/timeout
792      * events. Turn off timer events. Below we disable read events on error,
793      * and redefine read events on success.
794      */
795     if (event != EVENT_TIME)
796 	event_cancel_timer(tlsp_get_request_event, (char *) state);
797 
798     /*
799      * We must send some data, after receiving the request attributes and
800      * before receiving the remote file descriptor. We can't assume
801      * UNIX-domain socket semantics here.
802      */
803     if (event != EVENT_READ
804 	|| attr_scan(plaintext_stream, ATTR_FLAG_STRICT,
805 		     ATTR_TYPE_STR, MAIL_ATTR_REMOTE_ENDPT, remote_endpt,
806 		     ATTR_TYPE_INT, MAIL_ATTR_FLAGS, &req_flags,
807 		     ATTR_TYPE_INT, MAIL_ATTR_TIMEOUT, &timeout,
808 		     ATTR_TYPE_END) != 3) {
809 	msg_warn("%s: receive request attributes: %m", myname);
810 	event_disable_readwrite(plaintext_fd);
811 	tlsp_state_free(state);
812 	return;
813     }
814 
815     /*
816      * If the requested TLS engine is unavailable, hang up after making sure
817      * that the plaintext peer has received our "sorry" indication.
818      */
819     ready = ((req_flags & TLS_PROXY_FLAG_ROLE_SERVER) != 0
820 	     && tlsp_server_ctx != 0);
821     if (attr_print(plaintext_stream, ATTR_FLAG_NONE,
822 		   ATTR_TYPE_INT, MAIL_ATTR_STATUS, ready,
823 		   ATTR_TYPE_END) != 0
824 	|| vstream_fflush(plaintext_stream) != 0
825 	|| ready == 0) {
826 	read_wait(plaintext_fd, TLSP_INIT_TIMEOUT);	/* XXX */
827 	event_disable_readwrite(plaintext_fd);
828 	tlsp_state_free(state);
829 	return;
830     }
831 
832     /*
833      * XXX We use the same fixed timeout throughout the entire session for
834      * both plaintext and ciphertext communication. This timeout is just a
835      * safety feature; the real timeout will be enforced by our plaintext
836      * peer.
837      */
838     else {
839 	state->remote_endpt = mystrdup(STR(remote_endpt));
840 	msg_info("CONNECT %s %s",
841 		 (req_flags & TLS_PROXY_FLAG_ROLE_SERVER) ? "from" :
842 		 (req_flags & TLS_PROXY_FLAG_ROLE_CLIENT) ? "to" :
843 		 "(bogus_direction)", state->remote_endpt);
844 	state->req_flags = req_flags;
845 	state->timeout = timeout + 10;		/* XXX */
846 	event_enable_read(plaintext_fd, tlsp_get_fd_event, (char *) state);
847 	event_request_timer(tlsp_get_fd_event, (char *) state,
848 			    TLSP_INIT_TIMEOUT);
849 	return;
850     }
851 }
852 
853 /* tlsp_service - handle new client connection */
854 
855 static void tlsp_service(VSTREAM *plaintext_stream,
856 			         char *service,
857 			         char **argv)
858 {
859     TLSP_STATE *state;
860     int     plaintext_fd = vstream_fileno(plaintext_stream);
861 
862     /*
863      * Sanity check. This service takes no command-line arguments.
864      */
865     if (argv[0])
866 	msg_fatal("unexpected command-line argument: %s", argv[0]);
867 
868     /*
869      * This program handles multiple connections, so it must not block. We
870      * use event-driven code for all operations that introduce latency.
871      * Except that attribute lists are sent/received synchronously, once the
872      * socket is found to be ready for transmission.
873      */
874     non_blocking(plaintext_fd, NON_BLOCKING);
875     vstream_control(plaintext_stream,
876 		    VSTREAM_CTL_PATH, "plaintext",
877 		    VSTREAM_CTL_TIMEOUT, 5,
878 		    VSTREAM_CTL_END);
879 
880     /*
881      * Receive postscreen's remote SMTP client address/port and socket.
882      */
883     state = tlsp_state_create(service, plaintext_stream);
884     event_enable_read(plaintext_fd, tlsp_get_request_event, (char *) state);
885     event_request_timer(tlsp_get_request_event, (char *) state,
886 			TLSP_INIT_TIMEOUT);
887 }
888 
889 /* pre_jail_init - pre-jail initialization */
890 
891 static void pre_jail_init(char *unused_name, char **unused_argv)
892 {
893     TLS_SERVER_INIT_PROPS props;
894     const char *cert_file;
895     int     have_server_cert;
896     int     no_server_cert_ok;
897     int     require_server_cert;
898 
899     /*
900      * The code in this routine is pasted literally from smtpd(8). I am not
901      * going to sanitize this because doing so surely will break things in
902      * unexpected ways.
903      */
904     if (*var_tlsp_tls_level) {
905 	switch (tls_level_lookup(var_tlsp_tls_level)) {
906 	default:
907 	    msg_fatal("Invalid TLS level \"%s\"", var_tlsp_tls_level);
908 	    /* NOTREACHED */
909 	    break;
910 	case TLS_LEV_SECURE:
911 	case TLS_LEV_VERIFY:
912 	case TLS_LEV_FPRINT:
913 	    msg_warn("%s: unsupported TLS level \"%s\", using \"encrypt\"",
914 		     VAR_TLSP_TLS_LEVEL, var_tlsp_tls_level);
915 	    /* FALLTHROUGH */
916 	case TLS_LEV_ENCRYPT:
917 	    var_tlsp_enforce_tls = var_tlsp_use_tls = 1;
918 	    break;
919 	case TLS_LEV_MAY:
920 	    var_tlsp_enforce_tls = 0;
921 	    var_tlsp_use_tls = 1;
922 	    break;
923 	case TLS_LEV_NONE:
924 	    var_tlsp_enforce_tls = var_tlsp_use_tls = 0;
925 	    break;
926 	}
927     }
928     var_tlsp_use_tls = var_tlsp_use_tls || var_tlsp_enforce_tls;
929     if (!var_tlsp_use_tls) {
930 	msg_warn("TLS service is requested, but disabled with %s or %s",
931 		 VAR_TLSP_TLS_LEVEL, VAR_TLSP_USE_TLS);
932 	return;
933     }
934 
935     /*
936      * Load TLS keys before dropping privileges.
937      *
938      * Can't use anonymous ciphers if we want client certificates. Must use
939      * anonymous ciphers if we have no certificates.
940      */
941     ask_client_cert = require_server_cert =
942 	(var_tlsp_tls_ask_ccert
943 	 || (var_tlsp_enforce_tls && var_tlsp_tls_req_ccert));
944     if (strcasecmp(var_tlsp_tls_cert_file, "none") == 0) {
945 	no_server_cert_ok = 1;
946 	cert_file = "";
947     } else {
948 	no_server_cert_ok = 0;
949 	cert_file = var_tlsp_tls_cert_file;
950     }
951     have_server_cert =
952 	(*cert_file || *var_tlsp_tls_dcert_file || *var_tlsp_tls_eccert_file);
953 
954     /* Some TLS configuration errors are not show stoppers. */
955     if (!have_server_cert && require_server_cert)
956 	msg_warn("Need a server cert to request client certs");
957     if (!var_tlsp_enforce_tls && var_tlsp_tls_req_ccert)
958 	msg_warn("Can't require client certs unless TLS is required");
959     /* After a show-stopper error, log a warning. */
960     if (have_server_cert || (no_server_cert_ok && !require_server_cert))
961 
962 	/*
963 	 * Large parameter lists are error-prone, so we emulate a language
964 	 * feature that C does not have natively: named parameter lists.
965 	 */
966 	tlsp_server_ctx =
967 	    TLS_SERVER_INIT(&props,
968 			    log_level = var_tlsp_tls_loglevel,
969 			    verifydepth = var_tlsp_tls_ccert_vd,
970 			    cache_type = TLS_MGR_SCACHE_SMTPD,
971 			    scache_timeout = var_tlsp_tls_scache_timeout,
972 			    set_sessid = var_tlsp_tls_set_sessid,
973 			    cert_file = cert_file,
974 			    key_file = var_tlsp_tls_key_file,
975 			    dcert_file = var_tlsp_tls_dcert_file,
976 			    dkey_file = var_tlsp_tls_dkey_file,
977 			    eccert_file = var_tlsp_tls_eccert_file,
978 			    eckey_file = var_tlsp_tls_eckey_file,
979 			    CAfile = var_tlsp_tls_CAfile,
980 			    CApath = var_tlsp_tls_CApath,
981 			    dh1024_param_file
982 			    = var_tlsp_tls_dh1024_param_file,
983 			    dh512_param_file
984 			    = var_tlsp_tls_dh512_param_file,
985 			    eecdh_grade = var_tlsp_tls_eecdh,
986 			    protocols = var_tlsp_enforce_tls ?
987 			    var_tlsp_tls_mand_proto :
988 			    var_tlsp_tls_proto,
989 			    ask_ccert = ask_client_cert,
990 			    fpt_dgst = var_tlsp_tls_fpt_dgst);
991     else
992 	msg_warn("No server certs available. TLS can't be enabled");
993 
994     /*
995      * To maintain sanity, allow partial SSL_write() operations, and allow
996      * SSL_write() buffer pointers to change after a WANT_READ or WANT_WRITE
997      * result. This is based on OpenSSL developers talking on a mailing list,
998      * but is not supported by documentation. If this code stops working then
999      * no-one can be held responsible.
1000      */
1001     if (tlsp_server_ctx)
1002 	SSL_CTX_set_mode(tlsp_server_ctx->ssl_ctx,
1003 			 SSL_MODE_ENABLE_PARTIAL_WRITE
1004 			 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1005 }
1006 
1007 /* post_jail_init - post-jail initialization */
1008 
1009 static void post_jail_init(char *unused_name, char **unused_argv)
1010 {
1011      /* void */ ;
1012 }
1013 
1014 MAIL_VERSION_STAMP_DECLARE;
1015 
1016 /* main - the main program */
1017 
1018 int     main(int argc, char **argv)
1019 {
1020     static const CONFIG_INT_TABLE int_table[] = {
1021 	VAR_SMTPD_TLS_CCERT_VD, DEF_SMTPD_TLS_CCERT_VD, &var_smtpd_tls_ccert_vd, 0, 0,
1022 	VAR_SMTPD_TLS_LOGLEVEL, DEF_SMTPD_TLS_LOGLEVEL, &var_smtpd_tls_loglevel, 0, 0,
1023 	0,
1024     };
1025     static const CONFIG_NINT_TABLE nint_table[] = {
1026 	VAR_TLSP_TLS_CCERT_VD, DEF_TLSP_TLS_CCERT_VD, &var_tlsp_tls_ccert_vd, 0, 0,
1027 	VAR_TLSP_TLS_LOGLEVEL, DEF_TLSP_TLS_LOGLEVEL, &var_tlsp_tls_loglevel, 0, 0,
1028 	0,
1029     };
1030     static const CONFIG_TIME_TABLE time_table[] = {
1031 	VAR_SMTPD_TLS_SCACHTIME, DEF_SMTPD_TLS_SCACHTIME, &var_smtpd_tls_scache_timeout, 0, 0,
1032 	VAR_TLSP_WATCHDOG, DEF_TLSP_WATCHDOG, &var_tlsp_watchdog, 10, 0,
1033 	VAR_TLSP_TLS_SCACHTIME, DEF_TLSP_TLS_SCACHTIME, &var_tlsp_tls_scache_timeout, 0, 0,
1034 	0,
1035     };
1036     static const CONFIG_BOOL_TABLE bool_table[] = {
1037 	VAR_SMTPD_USE_TLS, DEF_SMTPD_USE_TLS, &var_smtpd_use_tls,
1038 	VAR_SMTPD_ENFORCE_TLS, DEF_SMTPD_ENFORCE_TLS, &var_smtpd_enforce_tls,
1039 	VAR_SMTPD_TLS_ACERT, DEF_SMTPD_TLS_ACERT, &var_smtpd_tls_ask_ccert,
1040 	VAR_SMTPD_TLS_RCERT, DEF_SMTPD_TLS_RCERT, &var_smtpd_tls_req_ccert,
1041 	VAR_SMTPD_TLS_SET_SESSID, DEF_SMTPD_TLS_SET_SESSID, &var_smtpd_tls_set_sessid,
1042 	0,
1043     };
1044     static const CONFIG_NBOOL_TABLE nbool_table[] = {
1045 	VAR_TLSP_USE_TLS, DEF_TLSP_USE_TLS, &var_tlsp_use_tls,
1046 	VAR_TLSP_ENFORCE_TLS, DEF_TLSP_ENFORCE_TLS, &var_tlsp_enforce_tls,
1047 	VAR_TLSP_TLS_ACERT, DEF_TLSP_TLS_ACERT, &var_tlsp_tls_ask_ccert,
1048 	VAR_TLSP_TLS_RCERT, DEF_TLSP_TLS_RCERT, &var_tlsp_tls_req_ccert,
1049 	VAR_TLSP_TLS_SET_SESSID, DEF_TLSP_TLS_SET_SESSID, &var_tlsp_tls_set_sessid,
1050 	0,
1051     };
1052     static const CONFIG_STR_TABLE str_table[] = {
1053 	VAR_SMTPD_TLS_CERT_FILE, DEF_SMTPD_TLS_CERT_FILE, &var_smtpd_tls_cert_file, 0, 0,
1054 	VAR_SMTPD_TLS_KEY_FILE, DEF_SMTPD_TLS_KEY_FILE, &var_smtpd_tls_key_file, 0, 0,
1055 	VAR_SMTPD_TLS_DCERT_FILE, DEF_SMTPD_TLS_DCERT_FILE, &var_smtpd_tls_dcert_file, 0, 0,
1056 	VAR_SMTPD_TLS_DKEY_FILE, DEF_SMTPD_TLS_DKEY_FILE, &var_smtpd_tls_dkey_file, 0, 0,
1057 	VAR_SMTPD_TLS_ECCERT_FILE, DEF_SMTPD_TLS_ECCERT_FILE, &var_smtpd_tls_eccert_file, 0, 0,
1058 	VAR_SMTPD_TLS_ECKEY_FILE, DEF_SMTPD_TLS_ECKEY_FILE, &var_smtpd_tls_eckey_file, 0, 0,
1059 	VAR_SMTPD_TLS_CA_FILE, DEF_SMTPD_TLS_CA_FILE, &var_smtpd_tls_CAfile, 0, 0,
1060 	VAR_SMTPD_TLS_CA_PATH, DEF_SMTPD_TLS_CA_PATH, &var_smtpd_tls_CApath, 0, 0,
1061 	VAR_SMTPD_TLS_CIPH, DEF_SMTPD_TLS_CIPH, &var_smtpd_tls_ciph, 1, 0,
1062 	VAR_SMTPD_TLS_MAND_CIPH, DEF_SMTPD_TLS_MAND_CIPH, &var_smtpd_tls_mand_ciph, 1, 0,
1063 	VAR_SMTPD_TLS_EXCL_CIPH, DEF_SMTPD_TLS_EXCL_CIPH, &var_smtpd_tls_excl_ciph, 0, 0,
1064 	VAR_SMTPD_TLS_MAND_EXCL, DEF_SMTPD_TLS_MAND_EXCL, &var_smtpd_tls_mand_excl, 0, 0,
1065 	VAR_SMTPD_TLS_PROTO, DEF_SMTPD_TLS_PROTO, &var_smtpd_tls_proto, 0, 0,
1066 	VAR_SMTPD_TLS_MAND_PROTO, DEF_SMTPD_TLS_MAND_PROTO, &var_smtpd_tls_mand_proto, 0, 0,
1067 	VAR_SMTPD_TLS_512_FILE, DEF_SMTPD_TLS_512_FILE, &var_smtpd_tls_dh512_param_file, 0, 0,
1068 	VAR_SMTPD_TLS_1024_FILE, DEF_SMTPD_TLS_1024_FILE, &var_smtpd_tls_dh1024_param_file, 0, 0,
1069 	VAR_SMTPD_TLS_EECDH, DEF_SMTPD_TLS_EECDH, &var_smtpd_tls_eecdh, 1, 0,
1070 	VAR_SMTPD_TLS_FPT_DGST, DEF_SMTPD_TLS_FPT_DGST, &var_smtpd_tls_fpt_dgst, 1, 0,
1071 	VAR_SMTPD_TLS_LEVEL, DEF_SMTPD_TLS_LEVEL, &var_smtpd_tls_level, 0, 0,
1072 	VAR_TLSP_TLS_CERT_FILE, DEF_TLSP_TLS_CERT_FILE, &var_tlsp_tls_cert_file, 0, 0,
1073 	VAR_TLSP_TLS_KEY_FILE, DEF_TLSP_TLS_KEY_FILE, &var_tlsp_tls_key_file, 0, 0,
1074 	VAR_TLSP_TLS_DCERT_FILE, DEF_TLSP_TLS_DCERT_FILE, &var_tlsp_tls_dcert_file, 0, 0,
1075 	VAR_TLSP_TLS_DKEY_FILE, DEF_TLSP_TLS_DKEY_FILE, &var_tlsp_tls_dkey_file, 0, 0,
1076 	VAR_TLSP_TLS_ECCERT_FILE, DEF_TLSP_TLS_ECCERT_FILE, &var_tlsp_tls_eccert_file, 0, 0,
1077 	VAR_TLSP_TLS_ECKEY_FILE, DEF_TLSP_TLS_ECKEY_FILE, &var_tlsp_tls_eckey_file, 0, 0,
1078 	VAR_TLSP_TLS_CA_FILE, DEF_TLSP_TLS_CA_FILE, &var_tlsp_tls_CAfile, 0, 0,
1079 	VAR_TLSP_TLS_CA_PATH, DEF_TLSP_TLS_CA_PATH, &var_tlsp_tls_CApath, 0, 0,
1080 	VAR_TLSP_TLS_CIPH, DEF_TLSP_TLS_CIPH, &var_tlsp_tls_ciph, 1, 0,
1081 	VAR_TLSP_TLS_MAND_CIPH, DEF_TLSP_TLS_MAND_CIPH, &var_tlsp_tls_mand_ciph, 1, 0,
1082 	VAR_TLSP_TLS_EXCL_CIPH, DEF_TLSP_TLS_EXCL_CIPH, &var_tlsp_tls_excl_ciph, 0, 0,
1083 	VAR_TLSP_TLS_MAND_EXCL, DEF_TLSP_TLS_MAND_EXCL, &var_tlsp_tls_mand_excl, 0, 0,
1084 	VAR_TLSP_TLS_PROTO, DEF_TLSP_TLS_PROTO, &var_tlsp_tls_proto, 0, 0,
1085 	VAR_TLSP_TLS_MAND_PROTO, DEF_TLSP_TLS_MAND_PROTO, &var_tlsp_tls_mand_proto, 0, 0,
1086 	VAR_TLSP_TLS_512_FILE, DEF_TLSP_TLS_512_FILE, &var_tlsp_tls_dh512_param_file, 0, 0,
1087 	VAR_TLSP_TLS_1024_FILE, DEF_TLSP_TLS_1024_FILE, &var_tlsp_tls_dh1024_param_file, 0, 0,
1088 	VAR_TLSP_TLS_EECDH, DEF_TLSP_TLS_EECDH, &var_tlsp_tls_eecdh, 1, 0,
1089 	VAR_TLSP_TLS_FPT_DGST, DEF_TLSP_TLS_FPT_DGST, &var_tlsp_tls_fpt_dgst, 1, 0,
1090 	VAR_TLSP_TLS_LEVEL, DEF_TLSP_TLS_LEVEL, &var_tlsp_tls_level, 0, 0,
1091 	0,
1092     };
1093 
1094     /*
1095      * Fingerprint executables and core dumps.
1096      */
1097     MAIL_VERSION_STAMP_ALLOCATE;
1098 
1099     /*
1100      * Pass control to the single-threaded service skeleton.
1101      */
1102     event_server_main(argc, argv, tlsp_service,
1103 		      MAIL_SERVER_INT_TABLE, int_table,
1104 		      MAIL_SERVER_NINT_TABLE, nint_table,
1105 		      MAIL_SERVER_STR_TABLE, str_table,
1106 		      MAIL_SERVER_BOOL_TABLE, bool_table,
1107 		      MAIL_SERVER_NBOOL_TABLE, nbool_table,
1108 		      MAIL_SERVER_TIME_TABLE, time_table,
1109 		      MAIL_SERVER_PRE_INIT, pre_jail_init,
1110 		      MAIL_SERVER_POST_INIT, post_jail_init,
1111 		      MAIL_SERVER_SLOW_EXIT, tlsp_drain,
1112 		      MAIL_SERVER_WATCHDOG, &var_tlsp_watchdog,
1113 		      0);
1114 }
1115 
1116 #else
1117 
1118 /* tlsp_service - respond to external trigger(s), non-TLS version */
1119 
1120 static void tlsp_service(VSTREAM *stream, char *unused_service,
1121 			         char **unused_argv)
1122 {
1123     msg_info("TLS support is not compiled in -- exiting");
1124     event_server_disconnect(stream);
1125 }
1126 
1127 /* main - the main program */
1128 
1129 int     main(int argc, char **argv)
1130 {
1131 
1132     /*
1133      * We can't simply use msg_fatal() here, because the logging hasn't been
1134      * initialized. The text would disappear because stderr is redirected to
1135      * /dev/null.
1136      *
1137      * We invoke event_server_main() to complete program initialization
1138      * (including logging) and then invoke the tlsp_service() routine to log
1139      * the message that says why this program will not run.
1140      */
1141     event_server_main(argc, argv, tlsp_service,
1142 		      0);
1143 }
1144 
1145 #endif
1146