xref: /netbsd-src/external/ibm-public/postfix/dist/src/tlsproxy/tlsproxy_state.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: tlsproxy_state.c,v 1.1.1.1 2011/03/02 19:32:39 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	tlsproxy_state 3
6 /* SUMMARY
7 /*	Postfix SMTP server
8 /* SYNOPSIS
9 /*	#include <tlsproxy.h>
10 /*
11 /*	TLSP_STATE *tlsp_state_create(service, plaintext_stream)
12 /*	const char *service;
13 /*	VSTREAM	*plaintext_stream;
14 /*
15 /*	void	tlsp_state_free(state)
16 /*	TLSP_STATE *state;
17 /* DESCRIPTION
18 /*	This module provides TLSP_STATE constructor and destructor
19 /*	routines.
20 /*
21 /*	tlsp_state_create() initializes session context.
22 /*
23 /*	tlsp_state_free() destroys session context.
24 /*
25 /*	Arguments:
26 /* .IP service
27 /*	The service name for the TLS library. This argument is copied.
28 /*	The destructor will automatically destroy the string.
29 /* .IP plaintext_stream
30 /*	The VSTREAM between postscreen(8) and tlsproxy(8).
31 /*	The destructor will automatically close the stream.
32 /* .PP
33 /*	Other structure members are set by the application. The
34 /*	text below describes how the TLSP_STATE destructor
35 /*	disposes of them.
36 /* .IP plaintext_buf
37 /*	NBBIO for plaintext I/O.
38 /*	The destructor will automatically turn off read/write/timeout
39 /*	events and destroy the NBBIO.
40 /* .IP ciphertext_fd
41 /*	The file handle for the remote SMTP client socket.
42 /*	The destructor will automatically turn off read/write events
43 /*	and close the file handle.
44 /* .IP ciphertext_timer
45 /*	The destructor will automatically turn off this time event.
46 /* .IP timeout
47 /*	Time limit for plaintext and ciphertext I/O.
48 /* .IP remote_endpt
49 /*	Printable remote endpoint name.
50 /*	The destructor will automatically destroy the string.
51 /* DIAGNOSTICS
52 /*	All errors are fatal.
53 /* LICENSE
54 /* .ad
55 /* .fi
56 /*	The Secure Mailer license must be distributed with this software.
57 /* AUTHOR(S)
58 /*	Wietse Venema
59 /*	IBM T.J. Watson Research
60 /*	P.O. Box 704
61 /*	Yorktown Heights, NY 10598, USA
62 /*--*/
63 
64  /*
65   * System library.
66   */
67 #include <sys_defs.h>
68 
69  /*
70   * Utility library.
71   */
72 #include <msg.h>
73 #include <mymalloc.h>
74 #include <nbbio.h>
75 
76  /*
77   * Master library.
78   */
79 #include <mail_server.h>
80 
81  /*
82   * TLS library.
83   */
84 #ifdef USE_TLS
85 #define TLS_INTERNAL			/* XXX */
86 #include <tls.h>
87 
88  /*
89   * Application-specific.
90   */
91 #include <tlsproxy.h>
92 
93 /* tlsp_state_create - create TLS proxy state object */
94 
95 TLSP_STATE *tlsp_state_create(const char *service,
96 			              VSTREAM *plaintext_stream)
97 {
98     TLSP_STATE *state = (TLSP_STATE *) mymalloc(sizeof(*state));
99 
100     state->flags = TLSP_FLAG_DO_HANDSHAKE;
101     state->service = mystrdup(service);
102     state->plaintext_stream = plaintext_stream;
103     state->plaintext_buf = 0;
104     state->ciphertext_fd = -1;
105     state->ciphertext_timer = 0;
106     state->timeout = -1;
107     state->remote_endpt = 0;
108     state->tls_context = 0;
109 
110     return (state);
111 }
112 
113 /* tlsp_state_free - destroy state objects, connection and events */
114 
115 void    tlsp_state_free(TLSP_STATE *state)
116 {
117     myfree(state->service);
118     if (state->plaintext_buf)			/* turns off plaintext events */
119 	nbbio_free(state->plaintext_buf);
120     event_server_disconnect(state->plaintext_stream);
121     if (state->ciphertext_fd >= 0) {
122 	event_disable_readwrite(state->ciphertext_fd);
123 	(void) close(state->ciphertext_fd);
124     }
125     if (state->ciphertext_timer)
126 	event_cancel_timer(state->ciphertext_timer, (char *) state);
127     if (state->remote_endpt) {
128 	msg_info("DISCONNECT %s", state->remote_endpt);
129 	myfree(state->remote_endpt);
130     }
131     if (state->tls_context)
132 	tls_free_context(state->tls_context);
133     myfree((char *) state);
134 }
135 
136 #endif
137