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