xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtp/smtp_state.c (revision 67b9b338a7386232ac596b5fd0cd5a9cc8a03c71)
1 /*	$NetBSD: smtp_state.c,v 1.3 2022/10/08 16:12:49 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	smtp_state 3
6 /* SUMMARY
7 /*	initialize/cleanup shared state
8 /* SYNOPSIS
9 /*	#include "smtp.h"
10 /*
11 /*	SMTP_STATE *smtp_state_alloc()
12 /*
13 /*	void	smtp_state_free(state)
14 /*	SMTP_STATE *state;
15 /* DESCRIPTION
16 /*	smtp_state_init() initializes the shared state, and allocates
17 /*	memory for buffers etc.
18 /*
19 /*	smtp_cleanup() destroys memory allocated by smtp_state_init().
20 /* STANDARDS
21 /* DIAGNOSTICS
22 /* BUGS
23 /* SEE ALSO
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /*	The Secure Mailer license must be distributed with this software.
28 /* AUTHOR(S)
29 /*	Wietse Venema
30 /*	IBM T.J. Watson Research
31 /*	P.O. Box 704
32 /*	Yorktown Heights, NY 10598, USA
33 /*
34 /*	Wietse Venema
35 /*	Google, Inc.
36 /*	111 8th Avenue
37 /*	New York, NY 10011, USA
38 /*--*/
39 
40 /* System library. */
41 
42 #include <sys_defs.h>
43 
44 /* Utility library. */
45 
46 #include <mymalloc.h>
47 #include <vstring.h>
48 #include <msg.h>
49 
50 /* Global library. */
51 
52 #include <mail_params.h>
53 #include <debug_peer.h>
54 
55 /* Application-specific. */
56 
57 #include "smtp.h"
58 #include "smtp_sasl.h"
59 
60 /* smtp_state_alloc - initialize */
61 
smtp_state_alloc(void)62 SMTP_STATE *smtp_state_alloc(void)
63 {
64     SMTP_STATE *state = (SMTP_STATE *) mymalloc(sizeof(*state));
65 
66     state->misc_flags = 0;
67     state->src = 0;
68     state->service = 0;
69     state->request = 0;
70     state->session = 0;
71     state->status = 0;
72     state->space_left = 0;
73     state->iterator->request_nexthop = vstring_alloc(100);
74     state->iterator->dest = vstring_alloc(100);
75     state->iterator->host = vstring_alloc(100);
76     state->iterator->addr = vstring_alloc(100);
77     state->iterator->saved_dest = vstring_alloc(100);
78     if (var_smtp_cache_conn) {
79 	state->dest_label = vstring_alloc(10);
80 	state->dest_prop = vstring_alloc(10);
81 	state->endp_label = vstring_alloc(10);
82 	state->endp_prop = vstring_alloc(10);
83 	state->cache_used = htable_create(1);
84     } else {
85 	state->dest_label = 0;
86 	state->dest_prop = 0;
87 	state->endp_label = 0;
88 	state->endp_prop = 0;
89 	state->cache_used = 0;
90     }
91     state->why = dsb_create();
92     state->debug_peer_per_nexthop = 0;
93     state->logged_line_length_limit = 0;
94     return (state);
95 }
96 
97 /* smtp_state_free - destroy state */
98 
smtp_state_free(SMTP_STATE * state)99 void    smtp_state_free(SMTP_STATE *state)
100 {
101 #ifdef USE_TLS
102     /* The TLS policy cache lifetime is one delivery. */
103     smtp_tls_policy_cache_flush();
104 #endif
105     vstring_free(state->iterator->request_nexthop);
106     vstring_free(state->iterator->dest);
107     vstring_free(state->iterator->host);
108     vstring_free(state->iterator->addr);
109     vstring_free(state->iterator->saved_dest);
110     if (state->dest_label)
111 	vstring_free(state->dest_label);
112     if (state->dest_prop)
113 	vstring_free(state->dest_prop);
114     if (state->endp_label)
115 	vstring_free(state->endp_label);
116     if (state->endp_prop)
117 	vstring_free(state->endp_prop);
118     if (state->cache_used)
119 	htable_free(state->cache_used, (void (*) (void *)) 0);
120     if (state->why)
121 	dsb_free(state->why);
122     if (state->debug_peer_per_nexthop)
123 	debug_peer_restore();
124 
125     myfree((void *) state);
126 }
127