xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtpd/smtpd_state.c (revision 7863ba460b0a05b553c754e5dbc29247dddec322)
1 /*	$NetBSD: smtpd_state.c,v 1.1.1.5 2013/01/02 18:59:10 tron Exp $	*/
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 
75 void    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 
151     /*
152      * Minimal initialization to support external authentication (e.g.,
153      * XCLIENT) without having to enable SASL in main.cf.
154      */
155 #ifdef USE_SASL_AUTH
156     if (SMTPD_STAND_ALONE(state))
157 	var_smtpd_sasl_enable = 0;
158     smtpd_sasl_set_inactive(state);
159     smtpd_sasl_state_init(state);
160 #endif
161 
162     state->milter_argv = 0;
163     state->milter_argc = 0;
164 
165     /*
166      * Initialize peer information.
167      */
168     smtpd_peer_init(state);
169 
170     /*
171      * Initialize xforward information.
172      */
173     smtpd_xforward_init(state);
174 
175     /*
176      * Initialize the conversation history.
177      */
178     smtpd_chat_reset(state);
179 
180     state->ehlo_argv = 0;
181     state->ehlo_buf = 0;
182 }
183 
184 /* smtpd_state_reset - cleanup after disconnect */
185 
186 void    smtpd_state_reset(SMTPD_STATE *state)
187 {
188 
189     /*
190      * When cleaning up, touch only those fields that smtpd_state_init()
191      * filled in. The other fields are taken care of by their own
192      * "destructor" functions.
193      */
194     if (state->service)
195 	myfree(state->service);
196     if (state->buffer)
197 	vstring_free(state->buffer);
198     if (state->addr_buf)
199 	vstring_free(state->addr_buf);
200     if (state->access_denied)
201 	myfree(state->access_denied);
202     if (state->protocol)
203 	myfree(state->protocol);
204     smtpd_peer_reset(state);
205 
206     /*
207      * Buffers that are created on the fly and that may be shared among mail
208      * deliveries within the same SMTP session.
209      */
210     if (state->defer_if_permit.dsn)
211 	vstring_free(state->defer_if_permit.dsn);
212     if (state->defer_if_permit.reason)
213 	vstring_free(state->defer_if_permit.reason);
214     if (state->defer_if_reject.dsn)
215 	vstring_free(state->defer_if_reject.dsn);
216     if (state->defer_if_reject.reason)
217 	vstring_free(state->defer_if_reject.reason);
218     if (state->expand_buf)
219 	vstring_free(state->expand_buf);
220     if (state->instance)
221 	vstring_free(state->instance);
222     if (state->dsn_buf)
223 	vstring_free(state->dsn_buf);
224     if (state->dsn_orcpt_buf)
225 	vstring_free(state->dsn_orcpt_buf);
226 #if (defined(USE_TLS) && defined(USE_TLSPROXY))
227     if (state->tlsproxy)			/* still open after longjmp */
228 	vstream_fclose(state->tlsproxy);
229 #endif
230 }
231