1 /* $NetBSD: cleanup.h,v 1.10 2022/10/08 16:12:45 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 VSTRING *milter_dsn_buf; /* Milter DSN parsing buffer */ 123 124 /* 125 * Support for Milter body replacement requests. 126 */ 127 struct CLEANUP_REGION *free_regions;/* unused regions */ 128 struct CLEANUP_REGION *body_regions;/* regions with body content */ 129 struct CLEANUP_REGION *curr_body_region; 130 131 /* 132 * Internationalization. 133 */ 134 int smtputf8; /* what support is desired */ 135 } CLEANUP_STATE; 136 137 /* 138 * Status flags. Flags 0-15 are reserved for cleanup_user.h. 139 */ 140 #define CLEANUP_FLAG_INRCPT (1<<16) /* Processing recipient records */ 141 #define CLEANUP_FLAG_WARN_SEEN (1<<17) /* REC_TYPE_WARN record seen */ 142 #define CLEANUP_FLAG_END_SEEN (1<<18) /* REC_TYPE_END record seen */ 143 144 /* 145 * Mappings. 146 */ 147 extern MAPS *cleanup_comm_canon_maps; 148 extern MAPS *cleanup_send_canon_maps; 149 extern MAPS *cleanup_rcpt_canon_maps; 150 extern int cleanup_comm_canon_flags; 151 extern int cleanup_send_canon_flags; 152 extern int cleanup_rcpt_canon_flags; 153 extern MAPS *cleanup_header_checks; 154 extern MAPS *cleanup_mimehdr_checks; 155 extern MAPS *cleanup_nesthdr_checks; 156 extern MAPS *cleanup_body_checks; 157 extern MAPS *cleanup_virt_alias_maps; 158 extern ARGV *cleanup_masq_domains; 159 extern STRING_LIST *cleanup_masq_exceptions; 160 extern int cleanup_masq_flags; 161 extern MAPS *cleanup_send_bcc_maps; 162 extern MAPS *cleanup_rcpt_bcc_maps; 163 164 /* 165 * Character filters. 166 */ 167 extern VSTRING *cleanup_reject_chars; 168 extern VSTRING *cleanup_strip_chars; 169 170 /* 171 * Milters. 172 */ 173 extern MILTERS *cleanup_milters; 174 175 /* 176 * Address canonicalization fine control. 177 */ 178 #define CLEANUP_CANON_FLAG_ENV_FROM (1<<0) /* envelope sender */ 179 #define CLEANUP_CANON_FLAG_ENV_RCPT (1<<1) /* envelope recipient */ 180 #define CLEANUP_CANON_FLAG_HDR_FROM (1<<2) /* header sender */ 181 #define CLEANUP_CANON_FLAG_HDR_RCPT (1<<3) /* header recipient */ 182 183 /* 184 * Address masquerading fine control. 185 */ 186 #define CLEANUP_MASQ_FLAG_ENV_FROM (1<<0) /* envelope sender */ 187 #define CLEANUP_MASQ_FLAG_ENV_RCPT (1<<1) /* envelope recipient */ 188 #define CLEANUP_MASQ_FLAG_HDR_FROM (1<<2) /* header sender */ 189 #define CLEANUP_MASQ_FLAG_HDR_RCPT (1<<3) /* header recipient */ 190 191 /* 192 * Restrictions on extension propagation. 193 */ 194 extern int cleanup_ext_prop_mask; 195 196 /* 197 * Saved queue file names, so the files can be removed in case of a fatal 198 * run-time error. 199 */ 200 extern char *cleanup_path; 201 extern VSTRING *cleanup_trace_path; 202 extern VSTRING *cleanup_bounce_path; 203 204 /* 205 * cleanup_state.c 206 */ 207 extern CLEANUP_STATE *cleanup_state_alloc(VSTREAM *); 208 extern void cleanup_state_free(CLEANUP_STATE *); 209 210 /* 211 * cleanup_api.c 212 */ 213 extern CLEANUP_STATE *cleanup_open(VSTREAM *); 214 extern void cleanup_control(CLEANUP_STATE *, int); 215 extern int cleanup_flush(CLEANUP_STATE *); 216 extern void cleanup_free(CLEANUP_STATE *); 217 extern void cleanup_all(void); 218 extern void cleanup_sig(int); 219 extern void cleanup_pre_jail(char *, char **); 220 extern void cleanup_post_jail(char *, char **); 221 extern const CONFIG_INT_TABLE cleanup_int_table[]; 222 extern const CONFIG_BOOL_TABLE cleanup_bool_table[]; 223 extern const CONFIG_STR_TABLE cleanup_str_table[]; 224 extern const CONFIG_TIME_TABLE cleanup_time_table[]; 225 226 #define CLEANUP_RECORD(s, t, b, l) ((s)->action((s), (t), (b), (l))) 227 228 /* 229 * cleanup_out.c 230 */ 231 extern void cleanup_out(CLEANUP_STATE *, int, const char *, ssize_t); 232 extern void cleanup_out_string(CLEANUP_STATE *, int, const char *); 233 extern void PRINTFLIKE(3, 4) cleanup_out_format(CLEANUP_STATE *, int, const char *,...); 234 extern void cleanup_out_header(CLEANUP_STATE *, VSTRING *); 235 236 #define CLEANUP_OUT_BUF(s, t, b) \ 237 cleanup_out((s), (t), vstring_str((b)), VSTRING_LEN((b))) 238 239 #define CLEANUP_OUT_OK(s) \ 240 (!((s)->errs & (s)->err_mask) && !((s)->flags & CLEANUP_FLAG_DISCARD)) 241 242 /* 243 * cleanup_envelope.c 244 */ 245 extern void cleanup_envelope(CLEANUP_STATE *, int, const char *, ssize_t); 246 247 /* 248 * cleanup_message.c 249 */ 250 extern void cleanup_message(CLEANUP_STATE *, int, const char *, ssize_t); 251 252 /* 253 * cleanup_extracted.c 254 */ 255 extern void cleanup_extracted(CLEANUP_STATE *, int, const char *, ssize_t); 256 257 /* 258 * cleanup_final.c 259 */ 260 extern void cleanup_final(CLEANUP_STATE *); 261 262 /* 263 * cleanup_rewrite.c 264 */ 265 extern int cleanup_rewrite_external(const char *, VSTRING *, const char *); 266 extern int cleanup_rewrite_internal(const char *, VSTRING *, const char *); 267 extern int cleanup_rewrite_tree(const char *, TOK822 *); 268 269 /* 270 * cleanup_map11.c 271 */ 272 extern int cleanup_map11_external(CLEANUP_STATE *, VSTRING *, MAPS *, int); 273 extern int cleanup_map11_internal(CLEANUP_STATE *, VSTRING *, MAPS *, int); 274 extern int cleanup_map11_tree(CLEANUP_STATE *, TOK822 *, MAPS *, int); 275 276 /* 277 * cleanup_map1n.c 278 */ 279 ARGV *cleanup_map1n_internal(CLEANUP_STATE *, const char *, MAPS *, int); 280 281 /* 282 * cleanup_masquerade.c 283 */ 284 extern int cleanup_masquerade_external(CLEANUP_STATE *, VSTRING *, ARGV *); 285 extern int cleanup_masquerade_internal(CLEANUP_STATE *, VSTRING *, ARGV *); 286 extern int cleanup_masquerade_tree(CLEANUP_STATE *, TOK822 *, ARGV *); 287 288 /* 289 * cleanup_recipient.c 290 */ 291 extern void cleanup_out_recipient(CLEANUP_STATE *, const char *, int, const char *, const char *); 292 293 /* 294 * cleanup_addr.c. 295 */ 296 extern off_t cleanup_addr_sender(CLEANUP_STATE *, const char *); 297 extern void cleanup_addr_recipient(CLEANUP_STATE *, const char *); 298 extern void cleanup_addr_bcc_dsn(CLEANUP_STATE *, const char *, const char *, int); 299 300 #define NO_DSN_ORCPT ((char *) 0) 301 #define NO_DSN_NOTIFY DSN_NOTIFY_NEVER 302 #define DEF_DSN_NOTIFY (0) 303 304 #define cleanup_addr_bcc(state, addr) \ 305 cleanup_addr_bcc_dsn((state), (addr), NO_DSN_ORCPT, NO_DSN_NOTIFY) 306 307 /* 308 * cleanup_bounce.c. 309 */ 310 extern int cleanup_bounce(CLEANUP_STATE *); 311 312 /* 313 * MSG_STATS compatibility. 314 */ 315 #define CLEANUP_MSG_STATS(stats, state) \ 316 MSG_STATS_INIT1(stats, incoming_arrival, state->arrival_time) 317 318 /* 319 * cleanup_milter.c. 320 */ 321 extern void cleanup_milter_header_checks_init(void); 322 extern void cleanup_milter_receive(CLEANUP_STATE *, int); 323 extern void cleanup_milter_inspect(CLEANUP_STATE *, MILTERS *); 324 extern void cleanup_milter_emul_mail(CLEANUP_STATE *, MILTERS *, const char *); 325 extern void cleanup_milter_emul_rcpt(CLEANUP_STATE *, MILTERS *, const char *); 326 extern void cleanup_milter_emul_data(CLEANUP_STATE *, MILTERS *); 327 328 #define CLEANUP_MILTER_OK(s) \ 329 (((s)->flags & CLEANUP_FLAG_MILTER) != 0 \ 330 && (s)->errs == 0 && ((s)->flags & CLEANUP_FLAG_DISCARD) == 0) 331 332 /* 333 * cleanup_body_edit.c 334 */ 335 typedef struct CLEANUP_REGION { 336 off_t start; /* start of region */ 337 off_t len; /* length or zero (open-ended) */ 338 off_t write_offs; /* write offset */ 339 struct CLEANUP_REGION *next; /* linkage */ 340 } CLEANUP_REGION; 341 342 extern void cleanup_region_init(CLEANUP_STATE *); 343 extern CLEANUP_REGION *cleanup_region_open(CLEANUP_STATE *, ssize_t); 344 extern void cleanup_region_close(CLEANUP_STATE *, CLEANUP_REGION *); 345 extern CLEANUP_REGION *cleanup_region_return(CLEANUP_STATE *, CLEANUP_REGION *); 346 extern void cleanup_region_done(CLEANUP_STATE *); 347 348 extern int cleanup_body_edit_start(CLEANUP_STATE *); 349 extern int cleanup_body_edit_write(CLEANUP_STATE *, int, VSTRING *); 350 extern int cleanup_body_edit_finish(CLEANUP_STATE *); 351 extern void cleanup_body_edit_free(CLEANUP_STATE *); 352 353 /* 354 * From: header formatting. 355 */ 356 extern int cleanup_hfrom_format; 357 358 /* LICENSE 359 /* .ad 360 /* .fi 361 /* The Secure Mailer license must be distributed with this software. 362 /* AUTHOR(S) 363 /* Wietse Venema 364 /* IBM T.J. Watson Research 365 /* P.O. Box 704 366 /* Yorktown Heights, NY 10598, USA 367 /* 368 /* Wietse Venema 369 /* Google, Inc. 370 /* 111 8th Avenue 371 /* New York, NY 10011, USA 372 /*--*/ 373