Postfix3.3.1
smtpd_state.c
[詳解]
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 /* Wietse Venema
39 /* Google, Inc.
40 /* 111 8th Avenue
41 /* New York, NY 10011, USA
42 /*
43 /* TLS support originally by:
44 /* Lutz Jaenicke
45 /* BTU Cottbus
46 /* Allgemeine Elektrotechnik
47 /* Universitaetsplatz 3-4
48 /* D-03044 Cottbus, Germany
49 /*--*/
50 
51 /* System library. */
52 
53 #include <sys_defs.h>
54 
55 /* Utility library. */
56 
57 #include <events.h>
58 #include <mymalloc.h>
59 #include <vstream.h>
60 #include <name_mask.h>
61 #include <msg.h>
62 
63 /* Global library. */
64 
65 #include <cleanup_user.h>
66 #include <mail_params.h>
67 #include <mail_error.h>
68 #include <mail_proto.h>
69 
70 /* Application-specific. */
71 
72 #include "smtpd.h"
73 #include "smtpd_chat.h"
74 #include "smtpd_sasl_glue.h"
75 
76 /* smtpd_state_init - initialize after connection establishment */
77 
78 void smtpd_state_init(SMTPD_STATE *state, VSTREAM *stream,
79  const char *service)
80 {
81 
82  /*
83  * Initialize the state information for this connection, and fill in the
84  * connection-specific fields.
85  */
86  state->flags = 0;
87  state->err = CLEANUP_STAT_OK;
88  state->client = stream;
89  state->service = mystrdup(service);
90  state->buffer = vstring_alloc(100);
91  state->addr_buf = vstring_alloc(100);
92  state->conn_count = state->conn_rate = 0;
93  state->error_count = 0;
94  state->error_mask = 0;
97  state->helo_name = 0;
98  state->queue_id = 0;
99  state->cleanup = 0;
100  state->dest = 0;
101  state->rcpt_count = 0;
102  state->access_denied = 0;
103  state->history = 0;
104  state->reason = 0;
105  state->sender = 0;
106  state->verp_delims = 0;
107  state->recipient = 0;
108  state->etrn_name = 0;
110  state->where = SMTPD_AFTER_CONNECT;
111  state->recursion = 0;
112  state->msg_size = 0;
113  state->act_size = 0;
114  state->junk_cmds = 0;
115  state->rcpt_overshoot = 0;
116  state->defer_if_permit_client = 0;
117  state->defer_if_permit_helo = 0;
118  state->defer_if_permit_sender = 0;
119  state->defer_if_reject.dsn = 0;
120  state->defer_if_reject.reason = 0;
121  state->defer_if_permit.dsn = 0;
122  state->defer_if_permit.reason = 0;
123  state->discard = 0;
124  state->expand_buf = 0;
125  state->prepend = 0;
126  state->proxy = 0;
127  state->proxy_mail = 0;
128  state->saved_filter = 0;
129  state->saved_redirect = 0;
130  state->saved_bcc = 0;
131  state->saved_flags = 0;
132 #ifdef DELAY_ACTION
133  state->saved_delay = 0;
134 #endif
135  state->instance = vstring_alloc(10);
136  state->seqno = 0;
137  state->rewrite_context = 0;
138 #if 0
139  state->ehlo_discard_mask = ~0;
140 #else
141  state->ehlo_discard_mask = 0;
142 #endif
143  state->dsn_envid = 0;
144  state->dsn_buf = vstring_alloc(100);
145  state->dsn_orcpt_buf = vstring_alloc(100);
146 #ifdef USE_TLS
147 #ifdef USE_TLSPROXY
148  state->tlsproxy = 0;
149 #endif
150  state->tls_context = 0;
151 #endif
152 
153 
154  /*
155  * Minimal initialization to support external authentication (e.g.,
156  * XCLIENT) without having to enable SASL in main.cf.
157  */
158 #ifdef USE_SASL_AUTH
159  if (SMTPD_STAND_ALONE(state))
162  smtpd_sasl_state_init(state);
163 #endif
164 
165  state->milter_argv = 0;
166  state->milter_argc = 0;
167  state->milters = 0;
168 
169  /*
170  * Initialize peer information.
171  */
172  smtpd_peer_init(state);
173 
174  /*
175  * Initialize xforward information.
176  */
177  smtpd_xforward_init(state);
178 
179  /*
180  * Initialize the conversation history.
181  */
182  smtpd_chat_reset(state);
183 
184  state->ehlo_argv = 0;
185  state->ehlo_buf = 0;
186 }
187 
188 /* smtpd_state_reset - cleanup after disconnect */
189 
191 {
192 
193  /*
194  * When cleaning up, touch only those fields that smtpd_state_init()
195  * filled in. The other fields are taken care of by their own
196  * "destructor" functions.
197  */
198  if (state->service)
199  myfree(state->service);
200  if (state->buffer)
201  vstring_free(state->buffer);
202  if (state->addr_buf)
203  vstring_free(state->addr_buf);
204  if (state->access_denied)
205  myfree(state->access_denied);
206  if (state->protocol)
207  myfree(state->protocol);
208  smtpd_peer_reset(state);
209 
210  /*
211  * Buffers that are created on the fly and that may be shared among mail
212  * deliveries within the same SMTP session.
213  */
214  if (state->defer_if_permit.dsn)
216  if (state->defer_if_permit.reason)
218  if (state->defer_if_reject.dsn)
220  if (state->defer_if_reject.reason)
222  if (state->expand_buf)
223  vstring_free(state->expand_buf);
224  if (state->instance)
225  vstring_free(state->instance);
226  if (state->dsn_buf)
227  vstring_free(state->dsn_buf);
228  if (state->dsn_orcpt_buf)
229  vstring_free(state->dsn_orcpt_buf);
230 #if (defined(USE_TLS) && defined(USE_TLSPROXY))
231  if (state->tlsproxy) /* still open after longjmp */
232  vstream_fclose(state->tlsproxy);
233 #endif
234 }
#define SMTPD_STAND_ALONE(state)
Definition: smtpd.h:309
void myfree(void *ptr)
Definition: mymalloc.c:207
int defer_if_permit_sender
Definition: smtpd.h:139
VSTRING * ehlo_buf
Definition: smtpd.h:190
int rcpt_overshoot
Definition: smtpd.h:114
char * mystrdup(const char *str)
Definition: mymalloc.c:225
bool var_smtpd_sasl_enable
Definition: smtpd.c:1274
VSTRING * buffer
Definition: smtpd.h:71
char * saved_redirect
Definition: smtpd.h:142
int junk_cmds
Definition: smtpd.h:113
off_t act_size
Definition: smtpd.h:112
void smtpd_peer_init(SMTPD_STATE *state)
Definition: smtpd_peer.c:573
char * queue_id
Definition: smtpd.h:96
void smtpd_state_init(SMTPD_STATE *state, VSTREAM *stream, const char *service)
Definition: smtpd_state.c:78
char * saved_filter
Definition: smtpd.h:141
int ehlo_discard_mask
Definition: smtpd.h:152
VSTREAM * cleanup
Definition: smtpd.h:97
int seqno
Definition: smtpd.h:151
#define CLEANUP_STAT_OK
Definition: cleanup_user.h:56
VSTRING * reason
Definition: smtpd.h:48
#define SMTPD_AFTER_CONNECT
Definition: smtpd.h:224
VSTRING * dsn
Definition: smtpd.h:49
const NAME_MASK mail_error_masks[]
Definition: mail_error.c:70
void smtpd_chat_reset(SMTPD_STATE *state)
Definition: smtpd_chat.c:99
ssize_t milter_argc
Definition: smtpd.h:183
char * rewrite_context
Definition: smtpd.h:115
char * dsn_envid
Definition: smtpd.h:153
VSTRING * expand_buf
Definition: smtpd.h:148
char * helo_name
Definition: smtpd.h:95
char * proxy_mail
Definition: smtpd.h:162
char * reason
Definition: smtpd.h:102
VSTREAM * client
Definition: smtpd.h:70
VSTRING * addr_buf
Definition: smtpd.h:72
MAIL_STREAM * dest
Definition: smtpd.h:98
SMTPD_DEFER defer_if_permit
Definition: smtpd.h:136
char * var_notify_classes
Definition: bounce.c:193
int vstream_fclose(VSTREAM *stream)
Definition: vstream.c:1268
void smtpd_sasl_state_init(SMTPD_STATE *)
char * access_denied
Definition: smtpd.h:100
int notify_mask
Definition: smtpd.h:94
VSTRING * dsn_orcpt_buf
Definition: smtpd.h:156
ARGV * saved_bcc
Definition: smtpd.h:143
off_t msg_size
Definition: smtpd.h:111
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
void smtpd_peer_reset(SMTPD_STATE *state)
Definition: smtpd_peer.c:631
char * sender
Definition: smtpd.h:103
#define name_mask(tag, table, str)
Definition: name_mask.h:49
#define MAIL_PROTO_SMTP
Definition: mail_proto.h:30
void smtpd_xforward_init(SMTPD_STATE *)
char * etrn_name
Definition: smtpd.h:107
SMTPD_DEFER defer_if_reject
Definition: smtpd.h:135
ARGV * prepend
Definition: smtpd.h:149
char * where
Definition: smtpd.h:109
#define VAR_NOTIFY_CLASSES
Definition: mail_params.h:72
int error_mask
Definition: smtpd.h:93
void smtpd_state_reset(SMTPD_STATE *state)
Definition: smtpd_state.c:190
int defer_if_permit_client
Definition: smtpd.h:137
int defer_if_permit_helo
Definition: smtpd.h:138
const char ** milter_argv
Definition: smtpd.h:182
ARGV * ehlo_argv
Definition: smtpd.h:191
struct SMTPD_PROXY * proxy
Definition: smtpd.h:161
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
int err
Definition: smtpd.h:69
int rcpt_count
Definition: smtpd.h:99
MILTERS * milters
Definition: smtpd.h:185
int discard
Definition: smtpd.h:140
char * recipient
Definition: smtpd.h:106
int flags
Definition: smtpd.h:68
VSTRING * dsn_buf
Definition: smtpd.h:155
int conn_count
Definition: smtpd.h:90
#define smtpd_sasl_set_inactive(s)
char * protocol
Definition: smtpd.h:108
int recursion
Definition: smtpd.h:110
int error_count
Definition: smtpd.h:92
int saved_flags
Definition: smtpd.h:144
char * service
Definition: smtpd.h:73
ARGV * history
Definition: smtpd.h:101
char * verp_delims
Definition: smtpd.h:105
VSTRING * instance
Definition: smtpd.h:150
int conn_rate
Definition: smtpd.h:91