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