1 /* $NetBSD: cleanup_init.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $ */
9 /* cleanup callable interface, initializations
11 /* #include "cleanup.h"
13 /* CONFIG_BOOL_TABLE cleanup_bool_table[];
15 /* CONFIG_INT_TABLE cleanup_int_table[];
17 /* CONFIG_BOOL_TABLE cleanup_bool_table[];
19 /* CONFIG_STR_TABLE cleanup_str_table[];
21 /* CONFIG_TIME_TABLE cleanup_time_table[];
23 /* void cleanup_pre_jail(service_name, argv)
24 /* char *service_name;
27 /* void cleanup_post_jail(service_name, argv)
28 /* char *service_name;
31 /* char *cleanup_path;
32 /* VSTRING *cleanup_trace_path;
36 /* void cleanup_sig(sigval)
39 /* This module implements a callable interface to the cleanup service
40 /* for one-time initializations that must be done before any message
41 /* processing can take place.
43 /* cleanup_{bool,int,str,time}_table[] specify configuration
44 /* parameters that must be initialized before calling any functions
45 /* in this module. These tables satisfy the interface as specified in
48 /* cleanup_pre_jail() and cleanup_post_jail() perform mandatory
49 /* initializations before and after the process enters the optional
50 /* chroot jail. These functions satisfy the interface as specified
51 /* in single_service(3).
53 /* cleanup_path is either a null pointer or it is the name of a queue
54 /* file that currently is being written. This information is used
55 /* by cleanup_all() to remove incomplete files after a fatal error,
56 /* or by cleanup_sig() after arrival of a SIGTERM signal.
58 /* cleanup_trace_path is either a null pointer or the pathname of a
59 /* trace logfile with DSN SUCCESS notifications. This information is
60 /* used to remove a trace file when the mail transaction is canceled.
62 /* cleanup_all() must be called in case of fatal error, in order
63 /* to remove an incomplete queue file.
65 /* cleanup_sig() must be called in case of SIGTERM, in order
66 /* to remove an incomplete queue file.
68 /* Problems and transactions are logged to \fBsyslogd\fR(8).
70 /* cleanup_api(3) cleanup callable interface, message processing
74 /* The Secure Mailer license must be distributed with this software.
77 /* IBM T.J. Watson Research
79 /* Yorktown Heights, NY 10598, USA
88 /* Utility library. */
92 #include <name_mask.h>
93 #include <stringops.h>
97 #include <mail_addr.h>
98 #include <mail_params.h>
99 #include <mail_version.h> /* milter_macro_v */
100 #include <ext_prop.h>
101 #include <flush_clnt.h>
103 /* Application-specific. */
108 * Global state: any queue files that we have open, so that the error
109 * handler can clean up in case of trouble.
111 char *cleanup_path
; /* queue file name */
114 * Another piece of global state: pathnames of partial bounce or trace
115 * logfiles that need to be cleaned up when the cleanup request is aborted.
117 VSTRING
*cleanup_trace_path
;
120 * Tunable parameters.
122 int var_hopcount_limit
; /* max mailer hop count */
123 char *var_canonical_maps
; /* common canonical maps */
124 char *var_send_canon_maps
; /* sender canonical maps */
125 char *var_rcpt_canon_maps
; /* recipient canonical maps */
126 char *var_canon_classes
; /* what to canonicalize */
127 char *var_send_canon_classes
; /* what sender to canonicalize */
128 char *var_rcpt_canon_classes
; /* what recipient to canonicalize */
129 char *var_virt_alias_maps
; /* virtual alias maps */
130 char *var_masq_domains
; /* masquerade domains */
131 char *var_masq_exceptions
; /* users not masqueraded */
132 char *var_header_checks
; /* primary header checks */
133 char *var_mimehdr_checks
; /* mime header checks */
134 char *var_nesthdr_checks
; /* nested header checks */
135 char *var_body_checks
; /* any body checks */
136 int var_dup_filter_limit
; /* recipient dup filter */
137 bool var_enable_orcpt
; /* Include orcpt in dup filter? */
138 char *var_empty_addr
; /* destination of bounced bounces */
139 int var_delay_warn_time
; /* delay that triggers warning */
140 char *var_prop_extension
; /* propagate unmatched extension */
141 char *var_always_bcc
; /* big brother */
142 char *var_rcpt_witheld
; /* recipients not disclosed */
143 bool var_canon_env_rcpt
; /* canonicalize envelope recipient */
144 char *var_masq_classes
; /* what to masquerade */
145 int var_qattr_count_limit
; /* named attribute limit */
146 int var_virt_recur_limit
; /* maximum virtual alias recursion */
147 int var_virt_expan_limit
; /* maximum virtual alias expansion */
148 int var_body_check_len
; /* when to stop body scan */
149 char *var_send_bcc_maps
; /* sender auto-bcc maps */
150 char *var_rcpt_bcc_maps
; /* recipient auto-bcc maps */
151 char *var_remote_rwr_domain
; /* header-only surrogate */
152 char *var_msg_reject_chars
; /* reject these characters */
153 char *var_msg_strip_chars
; /* strip these characters */
154 int var_verp_bounce_off
; /* don't verp the bounces */
155 int var_milt_conn_time
; /* milter connect/handshake timeout */
156 int var_milt_cmd_time
; /* milter command timeout */
157 int var_milt_msg_time
; /* milter content timeout */
158 char *var_milt_protocol
; /* Sendmail 8 milter protocol */
159 char *var_milt_def_action
; /* default milter action */
160 char *var_milt_daemon_name
; /* {daemon_name} macro value */
161 char *var_milt_v
; /* {v} macro value */
162 char *var_milt_conn_macros
; /* connect macros */
163 char *var_milt_helo_macros
; /* HELO macros */
164 char *var_milt_mail_macros
; /* MAIL FROM macros */
165 char *var_milt_rcpt_macros
; /* RCPT TO macros */
166 char *var_milt_data_macros
; /* DATA macros */
167 char *var_milt_eoh_macros
; /* end-of-header macros */
168 char *var_milt_eod_macros
; /* end-of-data macros */
169 char *var_milt_unk_macros
; /* unknown command macros */
170 char *var_cleanup_milters
; /* non-SMTP mail */
171 int var_auto_8bit_enc_hdr
; /* auto-detect 8bit encoding header */
172 int var_always_add_hdrs
; /* always add missing headers */
174 CONFIG_INT_TABLE cleanup_int_table
[] = {
175 VAR_HOPCOUNT_LIMIT
, DEF_HOPCOUNT_LIMIT
, &var_hopcount_limit
, 1, 0,
176 VAR_DUP_FILTER_LIMIT
, DEF_DUP_FILTER_LIMIT
, &var_dup_filter_limit
, 0, 0,
177 VAR_QATTR_COUNT_LIMIT
, DEF_QATTR_COUNT_LIMIT
, &var_qattr_count_limit
, 1, 0,
178 VAR_VIRT_RECUR_LIMIT
, DEF_VIRT_RECUR_LIMIT
, &var_virt_recur_limit
, 1, 0,
179 VAR_VIRT_EXPAN_LIMIT
, DEF_VIRT_EXPAN_LIMIT
, &var_virt_expan_limit
, 1, 0,
180 VAR_BODY_CHECK_LEN
, DEF_BODY_CHECK_LEN
, &var_body_check_len
, 0, 0,
184 CONFIG_BOOL_TABLE cleanup_bool_table
[] = {
185 VAR_ENABLE_ORCPT
, DEF_ENABLE_ORCPT
, &var_enable_orcpt
,
186 VAR_VERP_BOUNCE_OFF
, DEF_VERP_BOUNCE_OFF
, &var_verp_bounce_off
,
187 VAR_AUTO_8BIT_ENC_HDR
, DEF_AUTO_8BIT_ENC_HDR
, &var_auto_8bit_enc_hdr
,
188 VAR_ALWAYS_ADD_HDRS
, DEF_ALWAYS_ADD_HDRS
, &var_always_add_hdrs
,
192 CONFIG_TIME_TABLE cleanup_time_table
[] = {
193 VAR_DELAY_WARN_TIME
, DEF_DELAY_WARN_TIME
, &var_delay_warn_time
, 0, 0,
194 VAR_MILT_CONN_TIME
, DEF_MILT_CONN_TIME
, &var_milt_conn_time
, 1, 0,
195 VAR_MILT_CMD_TIME
, DEF_MILT_CMD_TIME
, &var_milt_cmd_time
, 1, 0,
196 VAR_MILT_MSG_TIME
, DEF_MILT_MSG_TIME
, &var_milt_msg_time
, 1, 0,
200 CONFIG_STR_TABLE cleanup_str_table
[] = {
201 VAR_CANONICAL_MAPS
, DEF_CANONICAL_MAPS
, &var_canonical_maps
, 0, 0,
202 VAR_SEND_CANON_MAPS
, DEF_SEND_CANON_MAPS
, &var_send_canon_maps
, 0, 0,
203 VAR_RCPT_CANON_MAPS
, DEF_RCPT_CANON_MAPS
, &var_rcpt_canon_maps
, 0, 0,
204 VAR_CANON_CLASSES
, DEF_CANON_CLASSES
, &var_canon_classes
, 1, 0,
205 VAR_SEND_CANON_CLASSES
, DEF_SEND_CANON_CLASSES
, &var_send_canon_classes
, 1, 0,
206 VAR_RCPT_CANON_CLASSES
, DEF_RCPT_CANON_CLASSES
, &var_rcpt_canon_classes
, 1, 0,
207 VAR_VIRT_ALIAS_MAPS
, DEF_VIRT_ALIAS_MAPS
, &var_virt_alias_maps
, 0, 0,
208 VAR_MASQ_DOMAINS
, DEF_MASQ_DOMAINS
, &var_masq_domains
, 0, 0,
209 VAR_EMPTY_ADDR
, DEF_EMPTY_ADDR
, &var_empty_addr
, 1, 0,
210 VAR_MASQ_EXCEPTIONS
, DEF_MASQ_EXCEPTIONS
, &var_masq_exceptions
, 0, 0,
211 VAR_HEADER_CHECKS
, DEF_HEADER_CHECKS
, &var_header_checks
, 0, 0,
212 VAR_MIMEHDR_CHECKS
, DEF_MIMEHDR_CHECKS
, &var_mimehdr_checks
, 0, 0,
213 VAR_NESTHDR_CHECKS
, DEF_NESTHDR_CHECKS
, &var_nesthdr_checks
, 0, 0,
214 VAR_BODY_CHECKS
, DEF_BODY_CHECKS
, &var_body_checks
, 0, 0,
215 VAR_PROP_EXTENSION
, DEF_PROP_EXTENSION
, &var_prop_extension
, 0, 0,
216 VAR_ALWAYS_BCC
, DEF_ALWAYS_BCC
, &var_always_bcc
, 0, 0,
217 VAR_RCPT_WITHELD
, DEF_RCPT_WITHELD
, &var_rcpt_witheld
, 0, 0,
218 VAR_MASQ_CLASSES
, DEF_MASQ_CLASSES
, &var_masq_classes
, 0, 0,
219 VAR_SEND_BCC_MAPS
, DEF_SEND_BCC_MAPS
, &var_send_bcc_maps
, 0, 0,
220 VAR_RCPT_BCC_MAPS
, DEF_RCPT_BCC_MAPS
, &var_rcpt_bcc_maps
, 0, 0,
221 VAR_REM_RWR_DOMAIN
, DEF_REM_RWR_DOMAIN
, &var_remote_rwr_domain
, 0, 0,
222 VAR_MSG_REJECT_CHARS
, DEF_MSG_REJECT_CHARS
, &var_msg_reject_chars
, 0, 0,
223 VAR_MSG_STRIP_CHARS
, DEF_MSG_STRIP_CHARS
, &var_msg_strip_chars
, 0, 0,
224 VAR_MILT_PROTOCOL
, DEF_MILT_PROTOCOL
, &var_milt_protocol
, 1, 0,
225 VAR_MILT_DEF_ACTION
, DEF_MILT_DEF_ACTION
, &var_milt_def_action
, 1, 0,
226 VAR_MILT_DAEMON_NAME
, DEF_MILT_DAEMON_NAME
, &var_milt_daemon_name
, 1, 0,
227 VAR_MILT_V
, DEF_MILT_V
, &var_milt_v
, 1, 0,
228 VAR_MILT_CONN_MACROS
, DEF_MILT_CONN_MACROS
, &var_milt_conn_macros
, 0, 0,
229 VAR_MILT_HELO_MACROS
, DEF_MILT_HELO_MACROS
, &var_milt_helo_macros
, 0, 0,
230 VAR_MILT_MAIL_MACROS
, DEF_MILT_MAIL_MACROS
, &var_milt_mail_macros
, 0, 0,
231 VAR_MILT_RCPT_MACROS
, DEF_MILT_RCPT_MACROS
, &var_milt_rcpt_macros
, 0, 0,
232 VAR_MILT_DATA_MACROS
, DEF_MILT_DATA_MACROS
, &var_milt_data_macros
, 0, 0,
233 VAR_MILT_EOH_MACROS
, DEF_MILT_EOH_MACROS
, &var_milt_eoh_macros
, 0, 0,
234 VAR_MILT_EOD_MACROS
, DEF_MILT_EOD_MACROS
, &var_milt_eod_macros
, 0, 0,
235 VAR_MILT_UNK_MACROS
, DEF_MILT_UNK_MACROS
, &var_milt_unk_macros
, 0, 0,
236 VAR_CLEANUP_MILTERS
, DEF_CLEANUP_MILTERS
, &var_cleanup_milters
, 0, 0,
243 MAPS
*cleanup_comm_canon_maps
;
244 MAPS
*cleanup_send_canon_maps
;
245 MAPS
*cleanup_rcpt_canon_maps
;
246 int cleanup_comm_canon_flags
;
247 int cleanup_send_canon_flags
;
248 int cleanup_rcpt_canon_flags
;
249 MAPS
*cleanup_header_checks
;
250 MAPS
*cleanup_mimehdr_checks
;
251 MAPS
*cleanup_nesthdr_checks
;
252 MAPS
*cleanup_body_checks
;
253 MAPS
*cleanup_virt_alias_maps
;
254 ARGV
*cleanup_masq_domains
;
255 STRING_LIST
*cleanup_masq_exceptions
;
256 int cleanup_masq_flags
;
257 MAPS
*cleanup_send_bcc_maps
;
258 MAPS
*cleanup_rcpt_bcc_maps
;
263 VSTRING
*cleanup_reject_chars
;
264 VSTRING
*cleanup_strip_chars
;
267 * Address extension propagation restrictions.
269 int cleanup_ext_prop_mask
;
274 MILTERS
*cleanup_milters
;
276 /* cleanup_all - callback for the runtime error handler */
278 void cleanup_all(void)
283 /* cleanup_sig - callback for the SIGTERM handler */
285 void cleanup_sig(int sig
)
289 * msg_fatal() is safe against calling itself recursively, but signals
292 * XXX While running as a signal handler, can't ask the memory manager to
293 * release VSTRING storage.
295 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
) {
296 if (cleanup_trace_path
) {
297 (void) REMOVE(vstring_str(cleanup_trace_path
));
298 cleanup_trace_path
= 0;
301 (void) REMOVE(cleanup_path
);
309 /* cleanup_pre_jail - initialize before entering the chroot jail */
311 void cleanup_pre_jail(char *unused_name
, char **unused_argv
)
313 static const NAME_MASK send_canon_class_table
[] = {
314 CANON_CLASS_ENV_FROM
, CLEANUP_CANON_FLAG_ENV_FROM
,
315 CANON_CLASS_HDR_FROM
, CLEANUP_CANON_FLAG_HDR_FROM
,
318 static const NAME_MASK rcpt_canon_class_table
[] = {
319 CANON_CLASS_ENV_RCPT
, CLEANUP_CANON_FLAG_ENV_RCPT
,
320 CANON_CLASS_HDR_RCPT
, CLEANUP_CANON_FLAG_HDR_RCPT
,
323 static const NAME_MASK canon_class_table
[] = {
324 CANON_CLASS_ENV_FROM
, CLEANUP_CANON_FLAG_ENV_FROM
,
325 CANON_CLASS_ENV_RCPT
, CLEANUP_CANON_FLAG_ENV_RCPT
,
326 CANON_CLASS_HDR_FROM
, CLEANUP_CANON_FLAG_HDR_FROM
,
327 CANON_CLASS_HDR_RCPT
, CLEANUP_CANON_FLAG_HDR_RCPT
,
330 static const NAME_MASK masq_class_table
[] = {
331 MASQ_CLASS_ENV_FROM
, CLEANUP_MASQ_FLAG_ENV_FROM
,
332 MASQ_CLASS_ENV_RCPT
, CLEANUP_MASQ_FLAG_ENV_RCPT
,
333 MASQ_CLASS_HDR_FROM
, CLEANUP_MASQ_FLAG_HDR_FROM
,
334 MASQ_CLASS_HDR_RCPT
, CLEANUP_MASQ_FLAG_HDR_RCPT
,
338 if (*var_canonical_maps
)
339 cleanup_comm_canon_maps
=
340 maps_create(VAR_CANONICAL_MAPS
, var_canonical_maps
,
341 DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
);
342 if (*var_send_canon_maps
)
343 cleanup_send_canon_maps
=
344 maps_create(VAR_SEND_CANON_MAPS
, var_send_canon_maps
,
345 DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
);
346 if (*var_rcpt_canon_maps
)
347 cleanup_rcpt_canon_maps
=
348 maps_create(VAR_RCPT_CANON_MAPS
, var_rcpt_canon_maps
,
349 DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
);
350 if (*var_virt_alias_maps
)
351 cleanup_virt_alias_maps
= maps_create(VAR_VIRT_ALIAS_MAPS
,
354 | DICT_FLAG_FOLD_FIX
);
355 if (*var_canon_classes
)
356 cleanup_comm_canon_flags
=
357 name_mask(VAR_CANON_CLASSES
, canon_class_table
,
359 if (*var_send_canon_classes
)
360 cleanup_send_canon_flags
=
361 name_mask(VAR_CANON_CLASSES
, send_canon_class_table
,
362 var_send_canon_classes
);
363 if (*var_rcpt_canon_classes
)
364 cleanup_rcpt_canon_flags
=
365 name_mask(VAR_CANON_CLASSES
, rcpt_canon_class_table
,
366 var_rcpt_canon_classes
);
367 if (*var_masq_domains
)
368 cleanup_masq_domains
= argv_split(var_masq_domains
, " ,\t\r\n");
369 if (*var_header_checks
)
370 cleanup_header_checks
=
371 maps_create(VAR_HEADER_CHECKS
, var_header_checks
, DICT_FLAG_LOCK
);
372 if (*var_mimehdr_checks
)
373 cleanup_mimehdr_checks
=
374 maps_create(VAR_MIMEHDR_CHECKS
, var_mimehdr_checks
, DICT_FLAG_LOCK
);
375 if (*var_nesthdr_checks
)
376 cleanup_nesthdr_checks
=
377 maps_create(VAR_NESTHDR_CHECKS
, var_nesthdr_checks
, DICT_FLAG_LOCK
);
378 if (*var_body_checks
)
379 cleanup_body_checks
=
380 maps_create(VAR_BODY_CHECKS
, var_body_checks
, DICT_FLAG_LOCK
);
381 if (*var_masq_exceptions
)
382 cleanup_masq_exceptions
=
383 string_list_init(MATCH_FLAG_NONE
, var_masq_exceptions
);
384 if (*var_masq_classes
)
385 cleanup_masq_flags
= name_mask(VAR_MASQ_CLASSES
, masq_class_table
,
387 if (*var_send_bcc_maps
)
388 cleanup_send_bcc_maps
=
389 maps_create(VAR_SEND_BCC_MAPS
, var_send_bcc_maps
,
390 DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
);
391 if (*var_rcpt_bcc_maps
)
392 cleanup_rcpt_bcc_maps
=
393 maps_create(VAR_RCPT_BCC_MAPS
, var_rcpt_bcc_maps
,
394 DICT_FLAG_LOCK
| DICT_FLAG_FOLD_FIX
);
395 if (*var_cleanup_milters
)
396 cleanup_milters
= milter_create(var_cleanup_milters
,
402 var_milt_conn_macros
,
403 var_milt_helo_macros
,
404 var_milt_mail_macros
,
405 var_milt_rcpt_macros
,
406 var_milt_data_macros
,
409 var_milt_unk_macros
);
414 /* cleanup_post_jail - initialize after entering the chroot jail */
416 void cleanup_post_jail(char *unused_name
, char **unused_argv
)
420 * Optionally set the file size resource limit. XXX This limits the
421 * message content to somewhat less than requested, because the total
422 * queue file size also includes envelope information. Unless people set
423 * really low limit, the difference is going to matter only when a queue
424 * file has lots of recipients.
426 if (var_message_limit
> 0)
427 set_file_limit((off_t
) var_message_limit
);
430 * Control how unmatched extensions are propagated.
432 cleanup_ext_prop_mask
=
433 ext_prop_mask(VAR_PROP_EXTENSION
, var_prop_extension
);
436 * Setup the filters for characters that should be rejected, and for
437 * characters that should be removed.
439 if (*var_msg_reject_chars
) {
440 cleanup_reject_chars
= vstring_alloc(strlen(var_msg_reject_chars
));
441 unescape(cleanup_reject_chars
, var_msg_reject_chars
);
443 if (*var_msg_strip_chars
) {
444 cleanup_strip_chars
= vstring_alloc(strlen(var_msg_strip_chars
));
445 unescape(cleanup_strip_chars
, var_msg_strip_chars
);