xref: /netbsd-src/external/ibm-public/postfix/dist/src/cleanup/cleanup.h (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: cleanup.h,v 1.2 2009/06/23 11:41:06 tron Exp $	*/
2 
3 /*	$NetBSD: cleanup.h,v 1.2 2009/06/23 11:41:06 tron Exp $	*/
4 
5 /*++
6 /* NAME
7 /*	cleanup 3h
8 /* SUMMARY
9 /*	canonicalize and enqueue message
10 /* SYNOPSIS
11 /*	#include "cleanup.h"
12 /* DESCRIPTION
13 /* .nf
14 
15  /*
16   * System library.
17   */
18 #include <sys/time.h>
19 
20  /*
21   * Utility library.
22   */
23 #include <vstring.h>
24 #include <vstream.h>
25 #include <argv.h>
26 #include <nvtable.h>
27 
28  /*
29   * Global library.
30   */
31 #include <maps.h>
32 #include <tok822.h>
33 #include <been_here.h>
34 #include <mail_stream.h>
35 #include <mail_conf.h>
36 #include <mime_state.h>
37 #include <string_list.h>
38 #include <cleanup_user.h>
39 
40  /*
41   * Milter library.
42   */
43 #include <milter.h>
44 
45  /*
46   * These state variables are accessed by many functions, and there is only
47   * one instance of each per message.
48   */
49 typedef struct CLEANUP_STATE {
50     VSTRING *attr_buf;			/* storage for named attribute */
51     VSTRING *temp1;			/* scratch buffer, local use only */
52     VSTRING *temp2;			/* scratch buffer, local use only */
53     VSTRING *stripped_buf;		/* character stripped input */
54     VSTREAM *src;			/* current input stream */
55     VSTREAM *dst;			/* current output stream */
56     MAIL_STREAM *handle;		/* mail stream handle */
57     char   *queue_name;			/* queue name */
58     char   *queue_id;			/* queue file basename */
59     struct timeval arrival_time;	/* arrival time */
60     char   *fullname;			/* envelope sender full name */
61     char   *sender;			/* envelope sender address */
62     char   *recip;			/* envelope recipient address */
63     char   *orig_rcpt;			/* original recipient address */
64     char   *return_receipt;		/* return-receipt address */
65     char   *errors_to;			/* errors-to address */
66     int     flags;			/* processing options, status flags */
67     int     qmgr_opts;			/* qmgr processing options */
68     int     errs;			/* any badness experienced */
69     int     err_mask;			/* allowed badness */
70     int     headers_seen;		/* which headers were seen */
71     int     hop_count;			/* count of received: headers */
72     char   *resent;			/* any resent- header seen */
73     BH_TABLE *dups;			/* recipient dup filter */
74     void    (*action) (struct CLEANUP_STATE *, int, const char *, ssize_t);
75     off_t   data_offset;		/* start of message content */
76     off_t   body_offset;		/* start of body content */
77     off_t   xtra_offset;		/* start of extra segment */
78     off_t   cont_length;		/* length including Milter edits */
79     off_t   sender_pt_offset;		/* replace sender here */
80     off_t   sender_pt_target;		/* record after sender address */
81     off_t   append_rcpt_pt_offset;	/* append recipient here */
82     off_t   append_rcpt_pt_target;	/* target of above record */
83     off_t   append_hdr_pt_offset;	/* append header here */
84     off_t   append_hdr_pt_target;	/* target of above record */
85     ssize_t rcpt_count;			/* recipient count */
86     char   *reason;			/* failure reason */
87     char   *smtp_reply;			/* failure reason, SMTP-style */
88     NVTABLE *attr;			/* queue file attribute list */
89     MIME_STATE *mime_state;		/* MIME state engine */
90     int     mime_errs;			/* MIME error flags */
91     char   *hdr_rewrite_context;	/* header rewrite context */
92     char   *filter;			/* from header/body patterns */
93     char   *redirect;			/* from header/body patterns */
94     char   *dsn_envid;			/* DSN envelope ID */
95     int     dsn_ret;			/* DSN full/hdrs */
96     int     dsn_notify;			/* DSN never/delay/fail/success */
97     char   *dsn_orcpt;			/* DSN original recipient */
98     char   *verp_delims;		/* VERP delimiters (optional) */
99 #ifdef DELAY_ACTION
100     int     defer_delay;		/* deferred delivery */
101 #endif
102 
103     /*
104      * Miscellaneous Milter support.
105      */
106     MILTERS *milters;			/* mail filters */
107     const char *client_name;		/* real or ersatz client */
108     const char *reverse_name;		/* real or ersatz client */
109     const char *client_addr;		/* real or ersatz client */
110     int     client_af;			/* real or ersatz client */
111     const char *client_port;		/* real or ersatz client */
112     VSTRING *milter_ext_from;		/* externalized sender */
113     VSTRING *milter_ext_rcpt;		/* externalized recipient */
114     VSTRING *milter_err_text;		/* milter call-back reply */
115 
116     /*
117      * Support for Milter body replacement requests.
118      */
119     struct CLEANUP_REGION *free_regions;/* unused regions */
120     struct CLEANUP_REGION *body_regions;/* regions with body content */
121     struct CLEANUP_REGION *curr_body_region;
122 } CLEANUP_STATE;
123 
124  /*
125   * Status flags. Flags 0-15 are reserved for cleanup_user.h.
126   */
127 #define CLEANUP_FLAG_INRCPT	(1<<16)	/* Processing recipient records */
128 #define CLEANUP_FLAG_WARN_SEEN	(1<<17)	/* REC_TYPE_WARN record seen */
129 #define CLEANUP_FLAG_END_SEEN	(1<<18)	/* REC_TYPE_END record seen */
130 
131  /*
132   * Mappings.
133   */
134 extern MAPS *cleanup_comm_canon_maps;
135 extern MAPS *cleanup_send_canon_maps;
136 extern MAPS *cleanup_rcpt_canon_maps;
137 extern int cleanup_comm_canon_flags;
138 extern int cleanup_send_canon_flags;
139 extern int cleanup_rcpt_canon_flags;
140 extern MAPS *cleanup_header_checks;
141 extern MAPS *cleanup_mimehdr_checks;
142 extern MAPS *cleanup_nesthdr_checks;
143 extern MAPS *cleanup_body_checks;
144 extern MAPS *cleanup_virt_alias_maps;
145 extern ARGV *cleanup_masq_domains;
146 extern STRING_LIST *cleanup_masq_exceptions;
147 extern int cleanup_masq_flags;
148 extern MAPS *cleanup_send_bcc_maps;
149 extern MAPS *cleanup_rcpt_bcc_maps;
150 
151  /*
152   * Character filters.
153   */
154 extern VSTRING *cleanup_reject_chars;
155 extern VSTRING *cleanup_strip_chars;
156 
157  /*
158   * Milters.
159   */
160 extern MILTERS *cleanup_milters;
161 
162  /*
163   * Address canonicalization fine control.
164   */
165 #define CLEANUP_CANON_FLAG_ENV_FROM	(1<<0)	/* envelope sender */
166 #define CLEANUP_CANON_FLAG_ENV_RCPT	(1<<1)	/* envelope recipient */
167 #define CLEANUP_CANON_FLAG_HDR_FROM	(1<<2)	/* header sender */
168 #define CLEANUP_CANON_FLAG_HDR_RCPT	(1<<3)	/* header recipient */
169 
170  /*
171   * Address masquerading fine control.
172   */
173 #define CLEANUP_MASQ_FLAG_ENV_FROM	(1<<0)	/* envelope sender */
174 #define CLEANUP_MASQ_FLAG_ENV_RCPT	(1<<1)	/* envelope recipient */
175 #define CLEANUP_MASQ_FLAG_HDR_FROM	(1<<2)	/* header sender */
176 #define CLEANUP_MASQ_FLAG_HDR_RCPT	(1<<3)	/* header recipient */
177 
178  /*
179   * Restrictions on extension propagation.
180   */
181 extern int cleanup_ext_prop_mask;
182 
183  /*
184   * Saved queue file names, so the files can be removed in case of a fatal
185   * run-time error.
186   */
187 extern char *cleanup_path;
188 extern VSTRING *cleanup_trace_path;
189 extern VSTRING *cleanup_bounce_path;
190 
191  /*
192   * cleanup_state.c
193   */
194 extern CLEANUP_STATE *cleanup_state_alloc(VSTREAM *);
195 extern void cleanup_state_free(CLEANUP_STATE *);
196 
197  /*
198   * cleanup_api.c
199   */
200 extern CLEANUP_STATE *cleanup_open(VSTREAM *);
201 extern void cleanup_control(CLEANUP_STATE *, int);
202 extern int cleanup_flush(CLEANUP_STATE *);
203 extern void cleanup_free(CLEANUP_STATE *);
204 extern void cleanup_all(void);
205 extern void cleanup_sig(int);
206 extern void cleanup_pre_jail(char *, char **);
207 extern void cleanup_post_jail(char *, char **);
208 extern CONFIG_BOOL_TABLE cleanup_bool_table[];
209 extern CONFIG_INT_TABLE cleanup_int_table[];
210 extern CONFIG_BOOL_TABLE cleanup_bool_table[];
211 extern CONFIG_STR_TABLE cleanup_str_table[];
212 extern CONFIG_TIME_TABLE cleanup_time_table[];
213 
214 #define CLEANUP_RECORD(s, t, b, l)	((s)->action((s), (t), (b), (l)))
215 
216  /*
217   * cleanup_out.c
218   */
219 extern void cleanup_out(CLEANUP_STATE *, int, const char *, ssize_t);
220 extern void cleanup_out_string(CLEANUP_STATE *, int, const char *);
221 extern void PRINTFLIKE(3, 4) cleanup_out_format(CLEANUP_STATE *, int, const char *,...);
222 extern void cleanup_out_header(CLEANUP_STATE *, VSTRING *);
223 
224 #define CLEANUP_OUT_BUF(s, t, b) \
225 	cleanup_out((s), (t), vstring_str((b)), VSTRING_LEN((b)))
226 
227 #define CLEANUP_OUT_OK(s) \
228 	(!((s)->errs & (s)->err_mask) && !((s)->flags & CLEANUP_FLAG_DISCARD))
229 
230  /*
231   * cleanup_envelope.c
232   */
233 extern void cleanup_envelope(CLEANUP_STATE *, int, const char *, ssize_t);
234 
235  /*
236   * cleanup_message.c
237   */
238 extern void cleanup_message(CLEANUP_STATE *, int, const char *, ssize_t);
239 
240  /*
241   * cleanup_extracted.c
242   */
243 extern void cleanup_extracted(CLEANUP_STATE *, int, const char *, ssize_t);
244 
245  /*
246   * cleanup_final.c
247   */
248 extern void cleanup_final(CLEANUP_STATE *);
249 
250  /*
251   * cleanup_rewrite.c
252   */
253 extern int cleanup_rewrite_external(const char *, VSTRING *, const char *);
254 extern int cleanup_rewrite_internal(const char *, VSTRING *, const char *);
255 extern int cleanup_rewrite_tree(const char *, TOK822 *);
256 
257  /*
258   * cleanup_map11.c
259   */
260 extern int cleanup_map11_external(CLEANUP_STATE *, VSTRING *, MAPS *, int);
261 extern int cleanup_map11_internal(CLEANUP_STATE *, VSTRING *, MAPS *, int);
262 extern int cleanup_map11_tree(CLEANUP_STATE *, TOK822 *, MAPS *, int);
263 
264  /*
265   * cleanup_map1n.c
266   */
267 ARGV   *cleanup_map1n_internal(CLEANUP_STATE *, const char *, MAPS *, int);
268 
269  /*
270   * cleanup_masquerade.c
271   */
272 extern int cleanup_masquerade_external(VSTRING *, ARGV *);
273 extern int cleanup_masquerade_internal(VSTRING *, ARGV *);
274 extern int cleanup_masquerade_tree(TOK822 *, ARGV *);
275 
276  /*
277   * cleanup_recipient.c
278   */
279 extern void cleanup_out_recipient(CLEANUP_STATE *, const char *, int, const char *, const char *);
280 
281  /*
282   * cleanup_addr.c.
283   */
284 extern void cleanup_addr_sender(CLEANUP_STATE *, const char *);
285 extern void cleanup_addr_recipient(CLEANUP_STATE *, const char *);
286 extern void cleanup_addr_bcc(CLEANUP_STATE *, const char *);
287 
288  /*
289   * cleanup_bounce.c.
290   */
291 extern int cleanup_bounce(CLEANUP_STATE *);
292 
293  /*
294   * MSG_STATS compatibility.
295   */
296 #define CLEANUP_MSG_STATS(stats, state) \
297     MSG_STATS_INIT1(stats, incoming_arrival, state->arrival_time)
298 
299  /*
300   * cleanup_milter.c.
301   */
302 extern void cleanup_milter_receive(CLEANUP_STATE *, int);
303 extern void cleanup_milter_inspect(CLEANUP_STATE *, MILTERS *);
304 extern void cleanup_milter_emul_mail(CLEANUP_STATE *, MILTERS *, const char *);
305 extern void cleanup_milter_emul_rcpt(CLEANUP_STATE *, MILTERS *, const char *);
306 extern void cleanup_milter_emul_data(CLEANUP_STATE *, MILTERS *);
307 
308 #define CLEANUP_MILTER_OK(s) \
309     (((s)->flags & CLEANUP_FLAG_MILTER) != 0 \
310 	&& (s)->errs == 0 && ((s)->flags & CLEANUP_FLAG_DISCARD) == 0)
311 
312  /*
313   * cleanup_body_edit.c
314   */
315 typedef struct CLEANUP_REGION {
316     off_t   start;			/* start of region */
317     off_t   len;			/* length or zero (open-ended) */
318     off_t   write_offs;			/* write offset */
319     struct CLEANUP_REGION *next;	/* linkage */
320 } CLEANUP_REGION;
321 
322 extern void cleanup_region_init(CLEANUP_STATE *);
323 extern CLEANUP_REGION *cleanup_region_open(CLEANUP_STATE *, ssize_t);
324 extern void cleanup_region_close(CLEANUP_STATE *, CLEANUP_REGION *);
325 extern CLEANUP_REGION *cleanup_region_return(CLEANUP_STATE *, CLEANUP_REGION *);
326 extern void cleanup_region_done(CLEANUP_STATE *);
327 
328 extern int cleanup_body_edit_start(CLEANUP_STATE *);
329 extern int cleanup_body_edit_write(CLEANUP_STATE *, int, VSTRING *);
330 extern int cleanup_body_edit_finish(CLEANUP_STATE *);
331 extern void cleanup_body_edit_free(CLEANUP_STATE *);
332 
333 /* LICENSE
334 /* .ad
335 /* .fi
336 /*	The Secure Mailer license must be distributed with this software.
337 /* AUTHOR(S)
338 /*	Wietse Venema
339 /*	IBM T.J. Watson Research
340 /*	P.O. Box 704
341 /*	Yorktown Heights, NY 10598, USA
342 /*--*/
343