1 /*        $NetBSD: smtp_state.c,v 1.4 2025/02/25 19:15: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  /*
56   * TLS library.
57   */
58 #if defined(USE_TLS) && defined(USE_TLSRPT)
59 #include <tlsrpt_wrapper.h>
60 #endif
61 
62 /* Application-specific. */
63 
64 #include "smtp.h"
65 #include "smtp_sasl.h"
66 
67 /* smtp_state_alloc - initialize */
68 
smtp_state_alloc(void)69 SMTP_STATE *smtp_state_alloc(void)
70 {
71     SMTP_STATE *state = (SMTP_STATE *) mymalloc(sizeof(*state));
72 
73     state->misc_flags = 0;
74     state->src = 0;
75     state->service = 0;
76     state->request = 0;
77     state->session = 0;
78     state->status = 0;
79     state->space_left = 0;
80     state->iterator->request_nexthop = vstring_alloc(100);
81     state->iterator->dest = vstring_alloc(100);
82     state->iterator->host = vstring_alloc(100);
83     state->iterator->addr = vstring_alloc(100);
84     state->iterator->saved_dest = vstring_alloc(100);
85 #ifdef USE_TLSRPT
86     state->tlsrpt = 0;
87 #endif
88     if (var_smtp_cache_conn) {
89           state->dest_label = vstring_alloc(10);
90           state->dest_prop = vstring_alloc(10);
91           state->endp_label = vstring_alloc(10);
92           state->endp_prop = vstring_alloc(10);
93           state->cache_used = htable_create(1);
94     } else {
95           state->dest_label = 0;
96           state->dest_prop = 0;
97           state->endp_label = 0;
98           state->endp_prop = 0;
99           state->cache_used = 0;
100     }
101     state->why = dsb_create();
102     state->debug_peer_per_nexthop = 0;
103     state->logged_line_length_limit = 0;
104     return (state);
105 }
106 
107 /* smtp_state_free - destroy state */
108 
smtp_state_free(SMTP_STATE * state)109 void    smtp_state_free(SMTP_STATE *state)
110 {
111 #ifdef USE_TLS
112     /* The TLS policy cache lifetime is one delivery. */
113     smtp_tls_policy_cache_flush();
114 #endif
115     vstring_free(state->iterator->request_nexthop);
116     vstring_free(state->iterator->dest);
117     vstring_free(state->iterator->host);
118     vstring_free(state->iterator->addr);
119     vstring_free(state->iterator->saved_dest);
120 #ifdef USE_TLSRPT
121     if (state->tlsrpt)
122           trw_free(state->tlsrpt);
123 #endif
124     if (state->dest_label)
125           vstring_free(state->dest_label);
126     if (state->dest_prop)
127           vstring_free(state->dest_prop);
128     if (state->endp_label)
129           vstring_free(state->endp_label);
130     if (state->endp_prop)
131           vstring_free(state->endp_prop);
132     if (state->cache_used)
133           htable_free(state->cache_used, (void (*) (void *)) 0);
134     if (state->why)
135           dsb_free(state->why);
136     if (state->debug_peer_per_nexthop)
137           debug_peer_restore();
138 
139     myfree((void *) state);
140 }
141