1/*++
2/* NAME
3/*	smtpd_state 3
4/* SUMMARY
5/*	Postfix SMTP server
6/* SYNOPSIS
7/*	#include "smtpd.h"
8/*
9/*	void	smtpd_state_init(state, stream, service)
10/*	SMTPD_STATE *state;
11/*	VSTREAM *stream;
12/*	const char *service;
13/*
14/*	void	smtpd_state_reset(state)
15/*	SMTPD_STATE *state;
16/* DESCRIPTION
17/*	smtpd_state_init() initializes session context.
18/*
19/*	smtpd_state_reset() cleans up session context.
20/*
21/*	Arguments:
22/* .IP state
23/*	Session context.
24/* .IP stream
25/*	Stream connected to peer. The stream is not copied.
26/* DIAGNOSTICS
27/*	All errors are fatal.
28/* LICENSE
29/* .ad
30/* .fi
31/*	The Secure Mailer license must be distributed with this software.
32/* AUTHOR(S)
33/*	Wietse Venema
34/*	IBM T.J. Watson Research
35/*	P.O. Box 704
36/*	Yorktown Heights, NY 10598, USA
37/*
38/*	TLS support originally by:
39/*	Lutz Jaenicke
40/*	BTU Cottbus
41/*	Allgemeine Elektrotechnik
42/*	Universitaetsplatz 3-4
43/*	D-03044 Cottbus, Germany
44/*--*/
45
46/* System library. */
47
48#include <sys_defs.h>
49
50/* Utility library. */
51
52#include <events.h>
53#include <mymalloc.h>
54#include <vstream.h>
55#include <name_mask.h>
56#include <msg.h>
57
58/* Global library. */
59
60#include <cleanup_user.h>
61#include <mail_params.h>
62#include <mail_error.h>
63#include <mail_proto.h>
64
65/* Application-specific. */
66
67#include "smtpd.h"
68#include "smtpd_chat.h"
69#include "smtpd_sasl_glue.h"
70#include "smtpd_binary.h"				/* APPLE - RFC 3030 */
71
72/* smtpd_state_init - initialize after connection establishment */
73
74void    smtpd_state_init(SMTPD_STATE *state, VSTREAM *stream,
75			         const char *service)
76{
77
78    /*
79     * Initialize the state information for this connection, and fill in the
80     * connection-specific fields.
81     */
82    state->flags = 0;
83    state->err = CLEANUP_STAT_OK;
84    state->client = stream;
85    state->service = mystrdup(service);
86    state->buffer = vstring_alloc(100);
87    state->addr_buf = vstring_alloc(100);
88    state->conn_count = state->conn_rate = 0;
89    state->error_count = 0;
90    state->error_mask = 0;
91    state->notify_mask = name_mask(VAR_NOTIFY_CLASSES, mail_error_masks,
92				   var_notify_classes);
93    state->helo_name = 0;
94    state->queue_id = 0;
95    state->cleanup = 0;
96    state->dest = 0;
97    state->rcpt_count = 0;
98    state->access_denied = 0;
99    state->history = 0;
100    state->reason = 0;
101    state->sender = 0;
102    state->verp_delims = 0;
103    state->recipient = 0;
104    state->etrn_name = 0;
105    state->protocol = mystrdup(MAIL_PROTO_SMTP);
106    state->where = SMTPD_AFTER_CONNECT;
107    state->recursion = 0;
108    state->chunking = 0;				/* APPLE - RFC 3030 */
109    state->chunking_context = 0;			/* APPLE - RFC 3030 */
110    state->msg_size = 0;
111    state->act_size = 0;
112    state->junk_cmds = 0;
113    state->rcpt_overshoot = 0;
114    state->defer_if_permit_client = 0;
115    state->defer_if_permit_helo = 0;
116    state->defer_if_permit_sender = 0;
117    state->defer_if_reject.dsn = 0;
118    state->defer_if_reject.reason = 0;
119    state->defer_if_permit.dsn = 0;
120    state->defer_if_permit.reason = 0;
121    state->discard = 0;
122    state->expand_buf = 0;
123    state->prepend = 0;
124    state->proxy = 0;
125    state->proxy_mail = 0;
126    state->saved_filter = 0;
127    state->saved_redirect = 0;
128    state->saved_bcc = 0;
129    state->saved_flags = 0;
130#ifdef DELAY_ACTION
131    state->saved_delay = 0;
132#endif
133    state->instance = vstring_alloc(10);
134    state->seqno = 0;
135    state->rewrite_context = 0;
136#if 0
137    state->ehlo_discard_mask = ~0;
138#else
139    state->ehlo_discard_mask = 0;
140#endif
141    state->dsn_envid = 0;
142    state->dsn_buf = vstring_alloc(100);
143    state->dsn_orcpt_buf = vstring_alloc(100);
144#ifdef USE_TLS
145#ifdef USE_TLSPROXY
146    state->tlsproxy = 0;
147#endif
148    state->tls_context = 0;
149#endif
150
151
152    /*
153     * Minimal initialization to support external authentication (e.g.,
154     * XCLIENT) without having to enable SASL in main.cf.
155     */
156#ifdef USE_SASL_AUTH
157    if (SMTPD_STAND_ALONE(state))
158	var_smtpd_sasl_enable = 0;
159    smtpd_sasl_set_inactive(state);
160    smtpd_sasl_state_init(state);
161#endif
162
163    state->milter_argv = 0;
164    state->milter_argc = 0;
165
166    /*
167     * Initialize peer information.
168     */
169    smtpd_peer_init(state);
170
171    /*
172     * Initialize xforward information.
173     */
174    smtpd_xforward_init(state);
175
176    /*
177     * Initialize the conversation history.
178     */
179    smtpd_chat_reset(state);
180
181    state->ehlo_argv = 0;
182    state->ehlo_buf = 0;
183}
184
185/* smtpd_state_reset - cleanup after disconnect */
186
187void    smtpd_state_reset(SMTPD_STATE *state)
188{
189
190    /*
191     * When cleaning up, touch only those fields that smtpd_state_init()
192     * filled in. The other fields are taken care of by their own
193     * "destructor" functions.
194     */
195    if (state->service)
196	myfree(state->service);
197    if (state->buffer)
198	vstring_free(state->buffer);
199    if (state->addr_buf)
200	vstring_free(state->addr_buf);
201    if (state->access_denied)
202	myfree(state->access_denied);
203    if (state->protocol)
204	myfree(state->protocol);
205    smtpd_peer_reset(state);
206
207    /*
208     * Buffers that are created on the fly and that may be shared among mail
209     * deliveries within the same SMTP session.
210     */
211    if (state->defer_if_permit.dsn)
212	vstring_free(state->defer_if_permit.dsn);
213    if (state->defer_if_permit.reason)
214	vstring_free(state->defer_if_permit.reason);
215    if (state->defer_if_reject.dsn)
216	vstring_free(state->defer_if_reject.dsn);
217    if (state->defer_if_reject.reason)
218	vstring_free(state->defer_if_reject.reason);
219    if (state->expand_buf)
220	vstring_free(state->expand_buf);
221    if (state->instance)
222	vstring_free(state->instance);
223    if (state->dsn_buf)
224	vstring_free(state->dsn_buf);
225    if (state->dsn_orcpt_buf)
226	vstring_free(state->dsn_orcpt_buf);
227#if (defined(USE_TLS) && defined(USE_TLSPROXY))
228    if (state->tlsproxy)			/* still open after longjmp */
229	vstream_fclose(state->tlsproxy);
230#endif
231}
232