1 /* $NetBSD: cleanup.h,v 1.9 2020/03/18 19:05:15 christos 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 #include <dsn_mask.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 ARGV *auto_hdrs; /* MTA's own header(s) */ 67 ARGV *hbc_rcpt; /* header/body checks BCC addresses */ 68 int flags; /* processing options, status flags */ 69 int tflags; /* User- or MTA-requested tracing */ 70 int qmgr_opts; /* qmgr processing options */ 71 int errs; /* any badness experienced */ 72 int err_mask; /* allowed badness */ 73 int headers_seen; /* which headers were seen */ 74 int hop_count; /* count of received: headers */ 75 char *resent; /* any resent- header seen */ 76 BH_TABLE *dups; /* recipient dup filter */ 77 void (*action) (struct CLEANUP_STATE *, int, const char *, ssize_t); 78 off_t data_offset; /* start of message content */ 79 off_t body_offset; /* start of body content */ 80 off_t xtra_offset; /* start of extra segment */ 81 off_t cont_length; /* length including Milter edits */ 82 off_t sender_pt_offset; /* replace sender here */ 83 off_t sender_pt_target; /* record after sender address */ 84 off_t append_rcpt_pt_offset; /* append recipient here */ 85 off_t append_rcpt_pt_target; /* target of above record */ 86 off_t append_hdr_pt_offset; /* append header here */ 87 off_t append_hdr_pt_target; /* target of above record */ 88 off_t append_meta_pt_offset; /* append meta record here */ 89 off_t append_meta_pt_target; /* target of above record */ 90 ssize_t rcpt_count; /* recipient count */ 91 char *reason; /* failure reason */ 92 char *smtp_reply; /* failure reason, SMTP-style */ 93 NVTABLE *attr; /* queue file attribute list */ 94 MIME_STATE *mime_state; /* MIME state engine */ 95 int mime_errs; /* MIME error flags */ 96 char *hdr_rewrite_context; /* header rewrite context */ 97 char *filter; /* from header/body patterns */ 98 char *redirect; /* from header/body patterns */ 99 char *dsn_envid; /* DSN envelope ID */ 100 int dsn_ret; /* DSN full/hdrs */ 101 int dsn_notify; /* DSN never/delay/fail/success */ 102 char *dsn_orcpt; /* DSN original recipient */ 103 char *verp_delims; /* VERP delimiters (optional) */ 104 #ifdef DELAY_ACTION 105 int defer_delay; /* deferred delivery */ 106 #endif 107 108 /* 109 * Miscellaneous Milter support. 110 */ 111 MILTERS *milters; /* mail filters */ 112 const char *client_name; /* real or ersatz client */ 113 const char *reverse_name; /* real or ersatz client */ 114 const char *client_addr; /* real or ersatz client */ 115 int client_af; /* real or ersatz client */ 116 const char *client_port; /* real or ersatz client */ 117 const char *server_addr; /* real or ersatz server */ 118 const char *server_port; /* real or ersatz server */ 119 VSTRING *milter_ext_from; /* externalized sender */ 120 VSTRING *milter_ext_rcpt; /* externalized recipient */ 121 VSTRING *milter_err_text; /* milter call-back reply */ 122 HBC_CHECKS *milter_hbc_checks; /* Milter header checks */ 123 VSTRING *milter_hbc_reply; /* Milter header checks reply */ 124 VSTRING *milter_dsn_buf; /* Milter DSN parsing buffer */ 125 126 /* 127 * Support for Milter body replacement requests. 128 */ 129 struct CLEANUP_REGION *free_regions;/* unused regions */ 130 struct CLEANUP_REGION *body_regions;/* regions with body content */ 131 struct CLEANUP_REGION *curr_body_region; 132 133 /* 134 * Internationalization. 135 */ 136 int smtputf8; /* what support is desired */ 137 } CLEANUP_STATE; 138 139 /* 140 * Status flags. Flags 0-15 are reserved for cleanup_user.h. 141 */ 142 #define CLEANUP_FLAG_INRCPT (1<<16) /* Processing recipient records */ 143 #define CLEANUP_FLAG_WARN_SEEN (1<<17) /* REC_TYPE_WARN record seen */ 144 #define CLEANUP_FLAG_END_SEEN (1<<18) /* REC_TYPE_END record seen */ 145 146 /* 147 * Mappings. 148 */ 149 extern MAPS *cleanup_comm_canon_maps; 150 extern MAPS *cleanup_send_canon_maps; 151 extern MAPS *cleanup_rcpt_canon_maps; 152 extern int cleanup_comm_canon_flags; 153 extern int cleanup_send_canon_flags; 154 extern int cleanup_rcpt_canon_flags; 155 extern MAPS *cleanup_header_checks; 156 extern MAPS *cleanup_mimehdr_checks; 157 extern MAPS *cleanup_nesthdr_checks; 158 extern MAPS *cleanup_body_checks; 159 extern MAPS *cleanup_virt_alias_maps; 160 extern ARGV *cleanup_masq_domains; 161 extern STRING_LIST *cleanup_masq_exceptions; 162 extern int cleanup_masq_flags; 163 extern MAPS *cleanup_send_bcc_maps; 164 extern MAPS *cleanup_rcpt_bcc_maps; 165 166 /* 167 * Character filters. 168 */ 169 extern VSTRING *cleanup_reject_chars; 170 extern VSTRING *cleanup_strip_chars; 171 172 /* 173 * Milters. 174 */ 175 extern MILTERS *cleanup_milters; 176 177 /* 178 * Address canonicalization fine control. 179 */ 180 #define CLEANUP_CANON_FLAG_ENV_FROM (1<<0) /* envelope sender */ 181 #define CLEANUP_CANON_FLAG_ENV_RCPT (1<<1) /* envelope recipient */ 182 #define CLEANUP_CANON_FLAG_HDR_FROM (1<<2) /* header sender */ 183 #define CLEANUP_CANON_FLAG_HDR_RCPT (1<<3) /* header recipient */ 184 185 /* 186 * Address masquerading fine control. 187 */ 188 #define CLEANUP_MASQ_FLAG_ENV_FROM (1<<0) /* envelope sender */ 189 #define CLEANUP_MASQ_FLAG_ENV_RCPT (1<<1) /* envelope recipient */ 190 #define CLEANUP_MASQ_FLAG_HDR_FROM (1<<2) /* header sender */ 191 #define CLEANUP_MASQ_FLAG_HDR_RCPT (1<<3) /* header recipient */ 192 193 /* 194 * Restrictions on extension propagation. 195 */ 196 extern int cleanup_ext_prop_mask; 197 198 /* 199 * Saved queue file names, so the files can be removed in case of a fatal 200 * run-time error. 201 */ 202 extern char *cleanup_path; 203 extern VSTRING *cleanup_trace_path; 204 extern VSTRING *cleanup_bounce_path; 205 206 /* 207 * cleanup_state.c 208 */ 209 extern CLEANUP_STATE *cleanup_state_alloc(VSTREAM *); 210 extern void cleanup_state_free(CLEANUP_STATE *); 211 212 /* 213 * cleanup_api.c 214 */ 215 extern CLEANUP_STATE *cleanup_open(VSTREAM *); 216 extern void cleanup_control(CLEANUP_STATE *, int); 217 extern int cleanup_flush(CLEANUP_STATE *); 218 extern void cleanup_free(CLEANUP_STATE *); 219 extern void cleanup_all(void); 220 extern void cleanup_sig(int); 221 extern void cleanup_pre_jail(char *, char **); 222 extern void cleanup_post_jail(char *, char **); 223 extern const CONFIG_INT_TABLE cleanup_int_table[]; 224 extern const CONFIG_BOOL_TABLE cleanup_bool_table[]; 225 extern const CONFIG_STR_TABLE cleanup_str_table[]; 226 extern const CONFIG_TIME_TABLE cleanup_time_table[]; 227 228 #define CLEANUP_RECORD(s, t, b, l) ((s)->action((s), (t), (b), (l))) 229 230 /* 231 * cleanup_out.c 232 */ 233 extern void cleanup_out(CLEANUP_STATE *, int, const char *, ssize_t); 234 extern void cleanup_out_string(CLEANUP_STATE *, int, const char *); 235 extern void PRINTFLIKE(3, 4) cleanup_out_format(CLEANUP_STATE *, int, const char *,...); 236 extern void cleanup_out_header(CLEANUP_STATE *, VSTRING *); 237 238 #define CLEANUP_OUT_BUF(s, t, b) \ 239 cleanup_out((s), (t), vstring_str((b)), VSTRING_LEN((b))) 240 241 #define CLEANUP_OUT_OK(s) \ 242 (!((s)->errs & (s)->err_mask) && !((s)->flags & CLEANUP_FLAG_DISCARD)) 243 244 /* 245 * cleanup_envelope.c 246 */ 247 extern void cleanup_envelope(CLEANUP_STATE *, int, const char *, ssize_t); 248 249 /* 250 * cleanup_message.c 251 */ 252 extern void cleanup_message(CLEANUP_STATE *, int, const char *, ssize_t); 253 254 /* 255 * cleanup_extracted.c 256 */ 257 extern void cleanup_extracted(CLEANUP_STATE *, int, const char *, ssize_t); 258 259 /* 260 * cleanup_final.c 261 */ 262 extern void cleanup_final(CLEANUP_STATE *); 263 264 /* 265 * cleanup_rewrite.c 266 */ 267 extern int cleanup_rewrite_external(const char *, VSTRING *, const char *); 268 extern int cleanup_rewrite_internal(const char *, VSTRING *, const char *); 269 extern int cleanup_rewrite_tree(const char *, TOK822 *); 270 271 /* 272 * cleanup_map11.c 273 */ 274 extern int cleanup_map11_external(CLEANUP_STATE *, VSTRING *, MAPS *, int); 275 extern int cleanup_map11_internal(CLEANUP_STATE *, VSTRING *, MAPS *, int); 276 extern int cleanup_map11_tree(CLEANUP_STATE *, TOK822 *, MAPS *, int); 277 278 /* 279 * cleanup_map1n.c 280 */ 281 ARGV *cleanup_map1n_internal(CLEANUP_STATE *, const char *, MAPS *, int); 282 283 /* 284 * cleanup_masquerade.c 285 */ 286 extern int cleanup_masquerade_external(CLEANUP_STATE *, VSTRING *, ARGV *); 287 extern int cleanup_masquerade_internal(CLEANUP_STATE *, VSTRING *, ARGV *); 288 extern int cleanup_masquerade_tree(CLEANUP_STATE *, TOK822 *, ARGV *); 289 290 /* 291 * cleanup_recipient.c 292 */ 293 extern void cleanup_out_recipient(CLEANUP_STATE *, const char *, int, const char *, const char *); 294 295 /* 296 * cleanup_addr.c. 297 */ 298 extern off_t cleanup_addr_sender(CLEANUP_STATE *, const char *); 299 extern void cleanup_addr_recipient(CLEANUP_STATE *, const char *); 300 extern void cleanup_addr_bcc_dsn(CLEANUP_STATE *, const char *, const char *, int); 301 302 #define NO_DSN_ORCPT ((char *) 0) 303 #define NO_DSN_NOTIFY DSN_NOTIFY_NEVER 304 #define DEF_DSN_NOTIFY (0) 305 306 #define cleanup_addr_bcc(state, addr) \ 307 cleanup_addr_bcc_dsn((state), (addr), NO_DSN_ORCPT, NO_DSN_NOTIFY) 308 309 /* 310 * cleanup_bounce.c. 311 */ 312 extern int cleanup_bounce(CLEANUP_STATE *); 313 314 /* 315 * MSG_STATS compatibility. 316 */ 317 #define CLEANUP_MSG_STATS(stats, state) \ 318 MSG_STATS_INIT1(stats, incoming_arrival, state->arrival_time) 319 320 /* 321 * cleanup_milter.c. 322 */ 323 extern void cleanup_milter_receive(CLEANUP_STATE *, int); 324 extern void cleanup_milter_inspect(CLEANUP_STATE *, MILTERS *); 325 extern void cleanup_milter_emul_mail(CLEANUP_STATE *, MILTERS *, const char *); 326 extern void cleanup_milter_emul_rcpt(CLEANUP_STATE *, MILTERS *, const char *); 327 extern void cleanup_milter_emul_data(CLEANUP_STATE *, MILTERS *); 328 329 #define CLEANUP_MILTER_OK(s) \ 330 (((s)->flags & CLEANUP_FLAG_MILTER) != 0 \ 331 && (s)->errs == 0 && ((s)->flags & CLEANUP_FLAG_DISCARD) == 0) 332 333 /* 334 * cleanup_body_edit.c 335 */ 336 typedef struct CLEANUP_REGION { 337 off_t start; /* start of region */ 338 off_t len; /* length or zero (open-ended) */ 339 off_t write_offs; /* write offset */ 340 struct CLEANUP_REGION *next; /* linkage */ 341 } CLEANUP_REGION; 342 343 extern void cleanup_region_init(CLEANUP_STATE *); 344 extern CLEANUP_REGION *cleanup_region_open(CLEANUP_STATE *, ssize_t); 345 extern void cleanup_region_close(CLEANUP_STATE *, CLEANUP_REGION *); 346 extern CLEANUP_REGION *cleanup_region_return(CLEANUP_STATE *, CLEANUP_REGION *); 347 extern void cleanup_region_done(CLEANUP_STATE *); 348 349 extern int cleanup_body_edit_start(CLEANUP_STATE *); 350 extern int cleanup_body_edit_write(CLEANUP_STATE *, int, VSTRING *); 351 extern int cleanup_body_edit_finish(CLEANUP_STATE *); 352 extern void cleanup_body_edit_free(CLEANUP_STATE *); 353 354 /* 355 * From: header formatting. 356 */ 357 #define HFROM_FORMAT_CODE_STD 0 358 #define HFROM_FORMAT_CODE_OBS 1 359 extern int hfrom_format_code; 360 361 /* LICENSE 362 /* .ad 363 /* .fi 364 /* The Secure Mailer license must be distributed with this software. 365 /* AUTHOR(S) 366 /* Wietse Venema 367 /* IBM T.J. Watson Research 368 /* P.O. Box 704 369 /* Yorktown Heights, NY 10598, USA 370 /* 371 /* Wietse Venema 372 /* Google, Inc. 373 /* 111 8th Avenue 374 /* New York, NY 10011, USA 375 /*--*/ 376