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