1 /* $NetBSD: smtpd_state.c,v 1.1.1.3 2011/03/02 19:32:38 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->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->msg_size = 0; 109 state->act_size = 0; 110 state->junk_cmds = 0; 111 state->rcpt_overshoot = 0; 112 state->defer_if_permit_client = 0; 113 state->defer_if_permit_helo = 0; 114 state->defer_if_permit_sender = 0; 115 state->defer_if_reject.dsn = 0; 116 state->defer_if_reject.reason = 0; 117 state->defer_if_permit.dsn = 0; 118 state->defer_if_permit.reason = 0; 119 state->discard = 0; 120 state->expand_buf = 0; 121 state->prepend = 0; 122 state->proxy = 0; 123 state->proxy_mail = 0; 124 state->saved_filter = 0; 125 state->saved_redirect = 0; 126 state->saved_bcc = 0; 127 state->saved_flags = 0; 128 #ifdef DELAY_ACTION 129 state->saved_delay = 0; 130 #endif 131 state->instance = vstring_alloc(10); 132 state->seqno = 0; 133 state->rewrite_context = 0; 134 #if 0 135 state->ehlo_discard_mask = ~0; 136 #else 137 state->ehlo_discard_mask = 0; 138 #endif 139 state->dsn_envid = 0; 140 state->dsn_buf = vstring_alloc(100); 141 state->dsn_orcpt_buf = vstring_alloc(100); 142 #ifdef USE_TLS 143 #ifdef USE_TLSPROXY 144 state->tlsproxy = 0; 145 #endif 146 state->tls_context = 0; 147 #endif 148 149 #ifdef USE_SASL_AUTH 150 if (SMTPD_STAND_ALONE(state)) 151 var_smtpd_sasl_enable = 0; 152 smtpd_sasl_set_inactive(state); 153 #endif 154 155 state->milter_argv = 0; 156 state->milter_argc = 0; 157 158 /* 159 * Initialize peer information. 160 */ 161 smtpd_peer_init(state); 162 163 /* 164 * Initialize xforward information. 165 */ 166 smtpd_xforward_init(state); 167 168 /* 169 * Initialize the conversation history. 170 */ 171 smtpd_chat_reset(state); 172 } 173 174 /* smtpd_state_reset - cleanup after disconnect */ 175 176 void smtpd_state_reset(SMTPD_STATE *state) 177 { 178 179 /* 180 * When cleaning up, touch only those fields that smtpd_state_init() 181 * filled in. The other fields are taken care of by their own 182 * "destructor" functions. 183 */ 184 if (state->service) 185 myfree(state->service); 186 if (state->buffer) 187 vstring_free(state->buffer); 188 if (state->addr_buf) 189 vstring_free(state->addr_buf); 190 if (state->access_denied) 191 myfree(state->access_denied); 192 if (state->protocol) 193 myfree(state->protocol); 194 smtpd_peer_reset(state); 195 196 /* 197 * Buffers that are created on the fly and that may be shared among mail 198 * deliveries within the same SMTP session. 199 */ 200 if (state->defer_if_permit.dsn) 201 vstring_free(state->defer_if_permit.dsn); 202 if (state->defer_if_permit.reason) 203 vstring_free(state->defer_if_permit.reason); 204 if (state->defer_if_reject.dsn) 205 vstring_free(state->defer_if_reject.dsn); 206 if (state->defer_if_reject.reason) 207 vstring_free(state->defer_if_reject.reason); 208 if (state->expand_buf) 209 vstring_free(state->expand_buf); 210 if (state->instance) 211 vstring_free(state->instance); 212 if (state->dsn_buf) 213 vstring_free(state->dsn_buf); 214 if (state->dsn_orcpt_buf) 215 vstring_free(state->dsn_orcpt_buf); 216 #if (defined(USE_TLS) && defined(USE_TLSPROXY)) 217 if (state->tlsproxy) /* still open after longjmp */ 218 vstream_fclose(state->tlsproxy); 219 #endif 220 } 221