1 /*	$NetBSD: cleanup_state.c,v 1.1.1.2 2010/06/17 18:06:44 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	cleanup_state 3
6 /* SUMMARY
7 /*	per-message state variables
8 /* SYNOPSIS
9 /*	#include "cleanup.h"
10 /*
11 /*	CLEANUP_STATE *cleanup_state_alloc(src)
12 /*	VSTREAM	*src;
13 /*
14 /*	void	cleanup_state_free(state)
15 /*	CLEANUP_STATE *state;
16 /* DESCRIPTION
17 /*	This module maintains about two dozen state variables
18 /*	that are used by many routines in the course of processing one
19 /*	message.
20 /*
21 /*	cleanup_state_alloc() initializes the per-message state variables.
22 /*
23 /*	cleanup_state_free() cleans up.
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 
35 /* System library. */
36 
37 #include <sys_defs.h>
38 
39 /* Utility library. */
40 
41 #include <mymalloc.h>
42 #include <vstring.h>
43 #include <htable.h>
44 
45 /* Global library. */
46 
47 #include <been_here.h>
48 #include <mail_params.h>
49 #include <mime_state.h>
50 #include <mail_proto.h>
51 
52 /* Milter library. */
53 
54 #include <milter.h>
55 
56 /* Application-specific. */
57 
58 #include "cleanup.h"
59 
60 /* cleanup_state_alloc - initialize global state */
61 
62 CLEANUP_STATE *cleanup_state_alloc(VSTREAM *src)
63 {
64     CLEANUP_STATE *state = (CLEANUP_STATE *) mymalloc(sizeof(*state));
65 
66     state->attr_buf = vstring_alloc(10);
67     state->temp1 = vstring_alloc(10);
68     state->temp2 = vstring_alloc(10);
69     if (cleanup_strip_chars)
70 	state->stripped_buf = vstring_alloc(10);
71     state->src = src;
72     state->dst = 0;
73     state->handle = 0;
74     state->queue_name = 0;
75     state->queue_id = 0;
76     state->arrival_time.tv_sec = state->arrival_time.tv_usec = 0;
77     state->fullname = 0;
78     state->sender = 0;
79     state->recip = 0;
80     state->orig_rcpt = 0;
81     state->return_receipt = 0;
82     state->errors_to = 0;
83     state->flags = 0;
84     state->qmgr_opts = 0;
85     state->errs = 0;
86     state->err_mask = 0;
87     state->headers_seen = 0;
88     state->hop_count = 0;
89     state->resent = "";
90     state->dups = been_here_init(var_dup_filter_limit, BH_FLAG_FOLD);
91     state->action = cleanup_envelope;
92     state->data_offset = -1;
93     state->body_offset = -1;
94     state->xtra_offset = -1;
95     state->cont_length = 0;
96     state->sender_pt_offset = -1;
97     state->sender_pt_target = -1;
98     state->append_rcpt_pt_offset = -1;
99     state->append_rcpt_pt_target = -1;
100     state->append_hdr_pt_offset = -1;
101     state->append_hdr_pt_target = -1;
102     state->append_meta_pt_offset = -1;
103     state->append_meta_pt_target = -1;
104     state->milter_hbc_checks = 0;
105     state->milter_hbc_reply = 0;
106     state->rcpt_count = 0;
107     state->reason = 0;
108     state->smtp_reply = 0;
109     state->attr = nvtable_create(10);
110     nvtable_update(state->attr, MAIL_ATTR_LOG_ORIGIN, MAIL_ATTR_ORG_LOCAL);
111     state->mime_state = 0;
112     state->mime_errs = 0;
113     state->hdr_rewrite_context = MAIL_ATTR_RWR_LOCAL;
114     state->filter = 0;
115     state->redirect = 0;
116     state->dsn_envid = 0;
117     state->dsn_ret = 0;
118     state->dsn_notify = 0;
119     state->dsn_orcpt = 0;
120     state->verp_delims = 0;
121     state->milters = 0;
122     state->client_name = 0;
123     state->reverse_name = 0;
124     state->client_addr = 0;
125     state->client_af = 0;
126     state->client_port = 0;
127     state->milter_ext_from = 0;
128     state->milter_ext_rcpt = 0;
129     state->milter_err_text = 0;
130     state->free_regions = state->body_regions = state->curr_body_region = 0;
131     return (state);
132 }
133 
134 /* cleanup_state_free - destroy global state */
135 
136 void    cleanup_state_free(CLEANUP_STATE *state)
137 {
138     vstring_free(state->attr_buf);
139     vstring_free(state->temp1);
140     vstring_free(state->temp2);
141     if (cleanup_strip_chars)
142 	vstring_free(state->stripped_buf);
143     if (state->fullname)
144 	myfree(state->fullname);
145     if (state->sender)
146 	myfree(state->sender);
147     if (state->recip)
148 	myfree(state->recip);
149     if (state->orig_rcpt)
150 	myfree(state->orig_rcpt);
151     if (state->return_receipt)
152 	myfree(state->return_receipt);
153     if (state->errors_to)
154 	myfree(state->errors_to);
155     if (state->queue_name)
156 	myfree(state->queue_name);
157     if (state->queue_id)
158 	myfree(state->queue_id);
159     been_here_free(state->dups);
160     if (state->reason)
161 	myfree(state->reason);
162     if (state->smtp_reply)
163 	myfree(state->smtp_reply);
164     nvtable_free(state->attr);
165     if (state->mime_state)
166 	mime_state_free(state->mime_state);
167     if (state->filter)
168 	myfree(state->filter);
169     if (state->redirect)
170 	myfree(state->redirect);
171     if (state->dsn_envid)
172 	myfree(state->dsn_envid);
173     if (state->dsn_orcpt)
174 	myfree(state->dsn_orcpt);
175     if (state->verp_delims)
176 	myfree(state->verp_delims);
177     if (state->milters)
178 	milter_free(state->milters);
179     if (state->milter_ext_from)
180 	vstring_free(state->milter_ext_from);
181     if (state->milter_ext_rcpt)
182 	vstring_free(state->milter_ext_rcpt);
183     if (state->milter_err_text)
184 	vstring_free(state->milter_err_text);
185     cleanup_region_done(state);
186     myfree((char *) state);
187 }
188