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