Home | History | Annotate | Line # | Download | only in smtp
      1 /*	$NetBSD: smtp_state.c,v 1.5 2026/05/09 18:49:20 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 
     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 #ifdef USE_TLS
     89     state->reqtls_level = SMTP_REQTLS_POLICY_ACT_DISABLE;
     90     if (var_log_tls_feature_status)
     91 	state->tls_stats = pol_stats_create();
     92     else
     93 #endif
     94 	state->tls_stats = 0;
     95     if (var_smtp_cache_conn) {
     96 	state->dest_label = vstring_alloc(10);
     97 	state->dest_prop = vstring_alloc(10);
     98 	state->endp_label = vstring_alloc(10);
     99 	state->endp_prop = vstring_alloc(10);
    100 	state->cache_used = htable_create(1);
    101     } else {
    102 	state->dest_label = 0;
    103 	state->dest_prop = 0;
    104 	state->endp_label = 0;
    105 	state->endp_prop = 0;
    106 	state->cache_used = 0;
    107     }
    108     state->why = dsb_create();
    109     state->debug_peer_per_nexthop = 0;
    110     state->logged_line_length_limit = 0;
    111     return (state);
    112 }
    113 
    114 /* smtp_state_free - destroy state */
    115 
    116 void    smtp_state_free(SMTP_STATE *state)
    117 {
    118 #ifdef USE_TLS
    119     /* The TLS policy cache lifetime is one delivery. */
    120     smtp_tls_policy_cache_flush();
    121     if (state->tls_stats)
    122 	pol_stats_free(state->tls_stats);
    123 #endif
    124     vstring_free(state->iterator->request_nexthop);
    125     vstring_free(state->iterator->dest);
    126     vstring_free(state->iterator->host);
    127     vstring_free(state->iterator->addr);
    128     vstring_free(state->iterator->saved_dest);
    129 #ifdef USE_TLSRPT
    130     if (state->tlsrpt)
    131 	trw_free(state->tlsrpt);
    132 #endif
    133     if (state->dest_label)
    134 	vstring_free(state->dest_label);
    135     if (state->dest_prop)
    136 	vstring_free(state->dest_prop);
    137     if (state->endp_label)
    138 	vstring_free(state->endp_label);
    139     if (state->endp_prop)
    140 	vstring_free(state->endp_prop);
    141     if (state->cache_used)
    142 	htable_free(state->cache_used, (void (*) (void *)) 0);
    143     if (state->why)
    144 	dsb_free(state->why);
    145     if (state->debug_peer_per_nexthop)
    146 	debug_peer_restore();
    147 
    148     myfree((void *) state);
    149 }
    150