xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtpd/smtpd_state.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1*33881f77Schristos /*	$NetBSD: smtpd_state.c,v 1.2 2020/03/18 19:05:20 christos Exp $	*/
241fbaed0Stron 
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /*	smtpd_state 3
641fbaed0Stron /* SUMMARY
741fbaed0Stron /*	Postfix SMTP server
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /*	#include "smtpd.h"
1041fbaed0Stron /*
1141fbaed0Stron /*	void	smtpd_state_init(state, stream, service)
1241fbaed0Stron /*	SMTPD_STATE *state;
1341fbaed0Stron /*	VSTREAM *stream;
1441fbaed0Stron /*	const char *service;
1541fbaed0Stron /*
1641fbaed0Stron /*	void	smtpd_state_reset(state)
1741fbaed0Stron /*	SMTPD_STATE *state;
1841fbaed0Stron /* DESCRIPTION
1941fbaed0Stron /*	smtpd_state_init() initializes session context.
2041fbaed0Stron /*
2141fbaed0Stron /*	smtpd_state_reset() cleans up session context.
2241fbaed0Stron /*
2341fbaed0Stron /*	Arguments:
2441fbaed0Stron /* .IP state
2541fbaed0Stron /*	Session context.
2641fbaed0Stron /* .IP stream
2741fbaed0Stron /*	Stream connected to peer. The stream is not copied.
2841fbaed0Stron /* DIAGNOSTICS
2941fbaed0Stron /*	All errors are fatal.
3041fbaed0Stron /* LICENSE
3141fbaed0Stron /* .ad
3241fbaed0Stron /* .fi
3341fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
3441fbaed0Stron /* AUTHOR(S)
3541fbaed0Stron /*	Wietse Venema
3641fbaed0Stron /*	IBM T.J. Watson Research
3741fbaed0Stron /*	P.O. Box 704
3841fbaed0Stron /*	Yorktown Heights, NY 10598, USA
3941fbaed0Stron /*
40f3bc92a4Schristos /*	Wietse Venema
41f3bc92a4Schristos /*	Google, Inc.
42f3bc92a4Schristos /*	111 8th Avenue
43f3bc92a4Schristos /*	New York, NY 10011, USA
44f3bc92a4Schristos /*
4541fbaed0Stron /*	TLS support originally by:
4641fbaed0Stron /*	Lutz Jaenicke
4741fbaed0Stron /*	BTU Cottbus
4841fbaed0Stron /*	Allgemeine Elektrotechnik
4941fbaed0Stron /*	Universitaetsplatz 3-4
5041fbaed0Stron /*	D-03044 Cottbus, Germany
5141fbaed0Stron /*--*/
5241fbaed0Stron 
5341fbaed0Stron /* System library. */
5441fbaed0Stron 
5541fbaed0Stron #include <sys_defs.h>
5641fbaed0Stron 
5741fbaed0Stron /* Utility library. */
5841fbaed0Stron 
5941fbaed0Stron #include <events.h>
6041fbaed0Stron #include <mymalloc.h>
6141fbaed0Stron #include <vstream.h>
6241fbaed0Stron #include <name_mask.h>
6341fbaed0Stron #include <msg.h>
6441fbaed0Stron 
6541fbaed0Stron /* Global library. */
6641fbaed0Stron 
6741fbaed0Stron #include <cleanup_user.h>
6841fbaed0Stron #include <mail_params.h>
6941fbaed0Stron #include <mail_error.h>
7041fbaed0Stron #include <mail_proto.h>
7141fbaed0Stron 
7241fbaed0Stron /* Application-specific. */
7341fbaed0Stron 
7441fbaed0Stron #include "smtpd.h"
7541fbaed0Stron #include "smtpd_chat.h"
7641fbaed0Stron #include "smtpd_sasl_glue.h"
7741fbaed0Stron 
7841fbaed0Stron /* smtpd_state_init - initialize after connection establishment */
7941fbaed0Stron 
smtpd_state_init(SMTPD_STATE * state,VSTREAM * stream,const char * service)8041fbaed0Stron void    smtpd_state_init(SMTPD_STATE *state, VSTREAM *stream,
8141fbaed0Stron 			         const char *service)
8241fbaed0Stron {
8341fbaed0Stron 
8441fbaed0Stron     /*
8541fbaed0Stron      * Initialize the state information for this connection, and fill in the
8641fbaed0Stron      * connection-specific fields.
8741fbaed0Stron      */
8841fbaed0Stron     state->flags = 0;
8941fbaed0Stron     state->err = CLEANUP_STAT_OK;
9041fbaed0Stron     state->client = stream;
9141fbaed0Stron     state->service = mystrdup(service);
9241fbaed0Stron     state->buffer = vstring_alloc(100);
9341fbaed0Stron     state->addr_buf = vstring_alloc(100);
9401b50dc7Stron     state->conn_count = state->conn_rate = 0;
9541fbaed0Stron     state->error_count = 0;
9641fbaed0Stron     state->error_mask = 0;
9741fbaed0Stron     state->notify_mask = name_mask(VAR_NOTIFY_CLASSES, mail_error_masks,
9841fbaed0Stron 				   var_notify_classes);
9941fbaed0Stron     state->helo_name = 0;
10041fbaed0Stron     state->queue_id = 0;
10141fbaed0Stron     state->cleanup = 0;
10241fbaed0Stron     state->dest = 0;
10341fbaed0Stron     state->rcpt_count = 0;
10441fbaed0Stron     state->access_denied = 0;
10541fbaed0Stron     state->history = 0;
10641fbaed0Stron     state->reason = 0;
10741fbaed0Stron     state->sender = 0;
10841fbaed0Stron     state->verp_delims = 0;
10941fbaed0Stron     state->recipient = 0;
11041fbaed0Stron     state->etrn_name = 0;
11141fbaed0Stron     state->protocol = mystrdup(MAIL_PROTO_SMTP);
11241fbaed0Stron     state->where = SMTPD_AFTER_CONNECT;
11341fbaed0Stron     state->recursion = 0;
11441fbaed0Stron     state->msg_size = 0;
11541fbaed0Stron     state->act_size = 0;
11641fbaed0Stron     state->junk_cmds = 0;
11741fbaed0Stron     state->rcpt_overshoot = 0;
11841fbaed0Stron     state->defer_if_permit_client = 0;
11941fbaed0Stron     state->defer_if_permit_helo = 0;
12041fbaed0Stron     state->defer_if_permit_sender = 0;
12141fbaed0Stron     state->defer_if_reject.dsn = 0;
12241fbaed0Stron     state->defer_if_reject.reason = 0;
12341fbaed0Stron     state->defer_if_permit.dsn = 0;
12441fbaed0Stron     state->defer_if_permit.reason = 0;
12541fbaed0Stron     state->discard = 0;
12641fbaed0Stron     state->expand_buf = 0;
12741fbaed0Stron     state->prepend = 0;
12841fbaed0Stron     state->proxy = 0;
12941fbaed0Stron     state->proxy_mail = 0;
13041fbaed0Stron     state->saved_filter = 0;
13141fbaed0Stron     state->saved_redirect = 0;
13241fbaed0Stron     state->saved_bcc = 0;
13341fbaed0Stron     state->saved_flags = 0;
13441fbaed0Stron #ifdef DELAY_ACTION
13541fbaed0Stron     state->saved_delay = 0;
13641fbaed0Stron #endif
13741fbaed0Stron     state->instance = vstring_alloc(10);
13841fbaed0Stron     state->seqno = 0;
13941fbaed0Stron     state->rewrite_context = 0;
14041fbaed0Stron #if 0
14141fbaed0Stron     state->ehlo_discard_mask = ~0;
14241fbaed0Stron #else
14341fbaed0Stron     state->ehlo_discard_mask = 0;
14441fbaed0Stron #endif
14541fbaed0Stron     state->dsn_envid = 0;
14641fbaed0Stron     state->dsn_buf = vstring_alloc(100);
14741fbaed0Stron     state->dsn_orcpt_buf = vstring_alloc(100);
14841fbaed0Stron #ifdef USE_TLS
149ff6d749dStron #ifdef USE_TLSPROXY
150ff6d749dStron     state->tlsproxy = 0;
151ff6d749dStron #endif
15241fbaed0Stron     state->tls_context = 0;
15341fbaed0Stron #endif
15441fbaed0Stron 
155a30b880eStron     /*
156a30b880eStron      * Minimal initialization to support external authentication (e.g.,
157a30b880eStron      * XCLIENT) without having to enable SASL in main.cf.
158a30b880eStron      */
15941fbaed0Stron #ifdef USE_SASL_AUTH
16041fbaed0Stron     if (SMTPD_STAND_ALONE(state))
16141fbaed0Stron 	var_smtpd_sasl_enable = 0;
16241fbaed0Stron     smtpd_sasl_set_inactive(state);
163a30b880eStron     smtpd_sasl_state_init(state);
16441fbaed0Stron #endif
16541fbaed0Stron 
16641fbaed0Stron     state->milter_argv = 0;
16741fbaed0Stron     state->milter_argc = 0;
168f3bc92a4Schristos     state->milters = 0;
16941fbaed0Stron 
17041fbaed0Stron     /*
17141fbaed0Stron      * Initialize peer information.
17241fbaed0Stron      */
17341fbaed0Stron     smtpd_peer_init(state);
17441fbaed0Stron 
17541fbaed0Stron     /*
17641fbaed0Stron      * Initialize xforward information.
17741fbaed0Stron      */
17841fbaed0Stron     smtpd_xforward_init(state);
17941fbaed0Stron 
18041fbaed0Stron     /*
18141fbaed0Stron      * Initialize the conversation history.
18241fbaed0Stron      */
18341fbaed0Stron     smtpd_chat_reset(state);
184a30b880eStron 
185a30b880eStron     state->ehlo_argv = 0;
186a30b880eStron     state->ehlo_buf = 0;
187f3bc92a4Schristos 
188f3bc92a4Schristos     /*
189f3bc92a4Schristos      * BDAT.
190f3bc92a4Schristos      */
191f3bc92a4Schristos     state->bdat_state = SMTPD_BDAT_STAT_NONE;
192f3bc92a4Schristos     state->bdat_get_stream = 0;
193f3bc92a4Schristos     state->bdat_get_buffer = 0;
19441fbaed0Stron }
19541fbaed0Stron 
19641fbaed0Stron /* smtpd_state_reset - cleanup after disconnect */
19741fbaed0Stron 
smtpd_state_reset(SMTPD_STATE * state)19841fbaed0Stron void    smtpd_state_reset(SMTPD_STATE *state)
19941fbaed0Stron {
20041fbaed0Stron 
20141fbaed0Stron     /*
20241fbaed0Stron      * When cleaning up, touch only those fields that smtpd_state_init()
20341fbaed0Stron      * filled in. The other fields are taken care of by their own
20441fbaed0Stron      * "destructor" functions.
20541fbaed0Stron      */
20641fbaed0Stron     if (state->service)
20741fbaed0Stron 	myfree(state->service);
20841fbaed0Stron     if (state->buffer)
20941fbaed0Stron 	vstring_free(state->buffer);
21041fbaed0Stron     if (state->addr_buf)
21141fbaed0Stron 	vstring_free(state->addr_buf);
21241fbaed0Stron     if (state->access_denied)
21341fbaed0Stron 	myfree(state->access_denied);
21441fbaed0Stron     if (state->protocol)
21541fbaed0Stron 	myfree(state->protocol);
21641fbaed0Stron     smtpd_peer_reset(state);
21741fbaed0Stron 
21841fbaed0Stron     /*
21941fbaed0Stron      * Buffers that are created on the fly and that may be shared among mail
22041fbaed0Stron      * deliveries within the same SMTP session.
22141fbaed0Stron      */
22241fbaed0Stron     if (state->defer_if_permit.dsn)
22341fbaed0Stron 	vstring_free(state->defer_if_permit.dsn);
22441fbaed0Stron     if (state->defer_if_permit.reason)
22541fbaed0Stron 	vstring_free(state->defer_if_permit.reason);
22641fbaed0Stron     if (state->defer_if_reject.dsn)
22741fbaed0Stron 	vstring_free(state->defer_if_reject.dsn);
22841fbaed0Stron     if (state->defer_if_reject.reason)
22941fbaed0Stron 	vstring_free(state->defer_if_reject.reason);
23041fbaed0Stron     if (state->expand_buf)
23141fbaed0Stron 	vstring_free(state->expand_buf);
23241fbaed0Stron     if (state->instance)
23341fbaed0Stron 	vstring_free(state->instance);
23441fbaed0Stron     if (state->dsn_buf)
23541fbaed0Stron 	vstring_free(state->dsn_buf);
23641fbaed0Stron     if (state->dsn_orcpt_buf)
23741fbaed0Stron 	vstring_free(state->dsn_orcpt_buf);
238ff6d749dStron #if (defined(USE_TLS) && defined(USE_TLSPROXY))
239ff6d749dStron     if (state->tlsproxy)			/* still open after longjmp */
240ff6d749dStron 	vstream_fclose(state->tlsproxy);
241ff6d749dStron #endif
242f3bc92a4Schristos 
243f3bc92a4Schristos     /*
244f3bc92a4Schristos      * BDAT.
245f3bc92a4Schristos      */
246f3bc92a4Schristos     if (state->bdat_get_stream)
247f3bc92a4Schristos 	(void) vstream_fclose(state->bdat_get_stream);
248f3bc92a4Schristos     if (state->bdat_get_buffer)
249f3bc92a4Schristos 	vstring_free(state->bdat_get_buffer);
25041fbaed0Stron }
251