summaryrefslogtreecommitdiffstats
path: root/src/smtpd/smtpd_state.c
blob: f2f5f899c68a429283260dbe5f0b6fffb0c2e077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*++
/* NAME
/*	smtpd_state 3
/* SUMMARY
/*	Postfix SMTP server
/* SYNOPSIS
/*	#include "smtpd.h"
/*
/*	void	smtpd_state_init(state, stream, service)
/*	SMTPD_STATE *state;
/*	VSTREAM *stream;
/*	const char *service;
/*
/*	void	smtpd_state_reset(state)
/*	SMTPD_STATE *state;
/* DESCRIPTION
/*	smtpd_state_init() initializes session context.
/*
/*	smtpd_state_reset() cleans up session context.
/*
/*	Arguments:
/* .IP state
/*	Session context.
/* .IP stream
/*	Stream connected to peer. The stream is not copied.
/* DIAGNOSTICS
/*	All errors are fatal.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*
/*	TLS support originally by:
/*	Lutz Jaenicke
/*	BTU Cottbus
/*	Allgemeine Elektrotechnik
/*	Universitaetsplatz 3-4
/*	D-03044 Cottbus, Germany
/*--*/

/* System library. */

#include <sys_defs.h>

/* Utility library. */

#include <events.h>
#include <mymalloc.h>
#include <vstream.h>
#include <name_mask.h>
#include <msg.h>

/* Global library. */

#include <cleanup_user.h>
#include <mail_params.h>
#include <mail_error.h>
#include <mail_proto.h>

/* Application-specific. */

#include "smtpd.h"
#include "smtpd_chat.h"
#include "smtpd_sasl_glue.h"

/* smtpd_state_init - initialize after connection establishment */

void    smtpd_state_init(SMTPD_STATE *state, VSTREAM *stream,
			         const char *service)
{

    /*
     * Initialize the state information for this connection, and fill in the
     * connection-specific fields.
     */
    state->flags = 0;
    state->err = CLEANUP_STAT_OK;
    state->client = stream;
    state->service = mystrdup(service);
    state->buffer = vstring_alloc(100);
    state->addr_buf = vstring_alloc(100);
    state->conn_count = state->conn_rate = 0;
    state->error_count = 0;
    state->error_mask = 0;
    state->notify_mask = name_mask(VAR_NOTIFY_CLASSES, mail_error_masks,
				   var_notify_classes);
    state->helo_name = 0;
    state->queue_id = 0;
    state->cleanup = 0;
    state->dest = 0;
    state->rcpt_count = 0;
    state->access_denied = 0;
    state->history = 0;
    state->reason = 0;
    state->sender = 0;
    state->verp_delims = 0;
    state->recipient = 0;
    state->etrn_name = 0;
    state->protocol = mystrdup(MAIL_PROTO_SMTP);
    state->where = SMTPD_AFTER_CONNECT;
    state->recursion = 0;
    state->msg_size = 0;
    state->act_size = 0;
    state->junk_cmds = 0;
    state->rcpt_overshoot = 0;
    state->defer_if_permit_client = 0;
    state->defer_if_permit_helo = 0;
    state->defer_if_permit_sender = 0;
    state->defer_if_reject.dsn = 0;
    state->defer_if_reject.reason = 0;
    state->defer_if_permit.dsn = 0;
    state->defer_if_permit.reason = 0;
    state->discard = 0;
    state->expand_buf = 0;
    state->prepend = 0;
    state->proxy = 0;
    state->proxy_mail = 0;
    state->saved_filter = 0;
    state->saved_redirect = 0;
    state->saved_bcc = 0;
    state->saved_flags = 0;
#ifdef DELAY_ACTION
    state->saved_delay = 0;
#endif
    state->instance = vstring_alloc(10);
    state->seqno = 0;
    state->rewrite_context = 0;
#if 0
    state->ehlo_discard_mask = ~0;
#else
    state->ehlo_discard_mask = 0;
#endif
    state->dsn_envid = 0;
    state->dsn_buf = vstring_alloc(100);
    state->dsn_orcpt_buf = vstring_alloc(100);
#ifdef USE_TLS
#ifdef USE_TLSPROXY
    state->tlsproxy = 0;
#endif
    state->tls_context = 0;
#endif

    /*
     * Minimal initialization to support external authentication (e.g.,
     * XCLIENT) without having to enable SASL in main.cf.
     */
#ifdef USE_SASL_AUTH
    if (SMTPD_STAND_ALONE(state))
	var_smtpd_sasl_enable = 0;
    smtpd_sasl_set_inactive(state);
    smtpd_sasl_state_init(state);
#endif

    state->milter_argv = 0;
    state->milter_argc = 0;
    state->milters = 0;

    /*
     * Initialize peer information.
     */
    smtpd_peer_init(state);

    /*
     * Initialize xforward information.
     */
    smtpd_xforward_init(state);

    /*
     * Initialize the conversation history.
     */
    smtpd_chat_reset(state);

    state->ehlo_argv = 0;
    state->ehlo_buf = 0;

    /*
     * BDAT.
     */
    state->bdat_state = SMTPD_BDAT_STAT_NONE;
    state->bdat_get_stream = 0;
    state->bdat_get_buffer = 0;
}

/* smtpd_state_reset - cleanup after disconnect */

void    smtpd_state_reset(SMTPD_STATE *state)
{

    /*
     * When cleaning up, touch only those fields that smtpd_state_init()
     * filled in. The other fields are taken care of by their own
     * "destructor" functions.
     */
    if (state->service)
	myfree(state->service);
    if (state->buffer)
	vstring_free(state->buffer);
    if (state->addr_buf)
	vstring_free(state->addr_buf);
    if (state->access_denied)
	myfree(state->access_denied);
    if (state->protocol)
	myfree(state->protocol);
    smtpd_peer_reset(state);

    /*
     * Buffers that are created on the fly and that may be shared among mail
     * deliveries within the same SMTP session.
     */
    if (state->defer_if_permit.dsn)
	vstring_free(state->defer_if_permit.dsn);
    if (state->defer_if_permit.reason)
	vstring_free(state->defer_if_permit.reason);
    if (state->defer_if_reject.dsn)
	vstring_free(state->defer_if_reject.dsn);
    if (state->defer_if_reject.reason)
	vstring_free(state->defer_if_reject.reason);
    if (state->expand_buf)
	vstring_free(state->expand_buf);
    if (state->instance)
	vstring_free(state->instance);
    if (state->dsn_buf)
	vstring_free(state->dsn_buf);
    if (state->dsn_orcpt_buf)
	vstring_free(state->dsn_orcpt_buf);
#if (defined(USE_TLS) && defined(USE_TLSPROXY))
    if (state->tlsproxy)			/* still open after longjmp */
	vstream_fclose(state->tlsproxy);
#endif

    /*
     * BDAT.
     */
    if (state->bdat_get_stream)
	(void) vstream_fclose(state->bdat_get_stream);
    if (state->bdat_get_buffer)
	vstring_free(state->bdat_get_buffer);
}