1 /* $NetBSD: cleanup.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $ */
9 /* canonicalize and enqueue Postfix message
11 /* \fBcleanup\fR [generic Postfix daemon options]
13 /* The \fBcleanup\fR(8) daemon processes inbound mail, inserts it
14 /* into the \fBincoming\fR mail queue, and informs the queue
15 /* manager of its arrival.
17 /* The \fBcleanup\fR(8) daemon always performs the following transformations:
19 /* Insert missing message headers: (\fBResent-\fR) \fBFrom:\fR,
20 /* \fBTo:\fR, \fBMessage-Id:\fR, and \fBDate:\fR.
22 /* Transform envelope and header addresses to the standard
23 /* \fIuser@fully-qualified-domain\fR form that is expected by other
25 /* This task is delegated to the \fBtrivial-rewrite\fR(8) daemon.
27 /* Eliminate duplicate envelope recipient addresses.
29 /* The following address transformations are optional:
31 /* Optionally, rewrite all envelope and header addresses according
32 /* to the mappings specified in the \fBcanonical\fR(5) lookup tables.
34 /* Optionally, masquerade envelope sender addresses and message
35 /* header addresses (i.e. strip host or domain information below
36 /* all domains listed in the \fBmasquerade_domains\fR parameter,
37 /* except for user names listed in \fBmasquerade_exceptions\fR).
38 /* By default, address masquerading does not affect envelope recipients.
40 /* Optionally, expand envelope recipients according to information
41 /* found in the \fBvirtual\fR(5) lookup tables.
43 /* The \fBcleanup\fR(8) daemon performs sanity checks on the content of
44 /* each message. When it finds a problem, by default it returns a
45 /* diagnostic status to the client, and leaves it up to the client
46 /* to deal with the problem. Alternatively, the client can request
47 /* the \fBcleanup\fR(8) daemon to bounce the message back to the sender
48 /* in case of trouble.
50 /* RFC 822 (ARPA Internet Text Messages)
51 /* RFC 2045 (MIME: Format of Internet Message Bodies)
52 /* RFC 2046 (MIME: Media Types)
53 /* RFC 3463 (Enhanced Status Codes)
54 /* RFC 3464 (Delivery status notifications)
56 /* Problems and transactions are logged to \fBsyslogd\fR(8).
58 /* Table-driven rewriting rules make it hard to express \fBif then
59 /* else\fR and other logical relationships.
60 /* CONFIGURATION PARAMETERS
63 /* Changes to \fBmain.cf\fR are picked up automatically, as
65 /* processes run for only a limited amount of time. Use the command
66 /* "\fBpostfix reload\fR" to speed up a change.
68 /* The text below provides only a parameter summary. See
69 /* \fBpostconf\fR(5) for more details including examples.
70 /* COMPATIBILITY CONTROLS
73 /* .IP "\fBundisclosed_recipients_header (To: undisclosed-recipients:;)\fR"
74 /* Message header that the Postfix \fBcleanup\fR(8) server inserts when a
75 /* message contains no To: or Cc: message header.
77 /* Available in Postfix version 2.1 only:
78 /* .IP "\fBenable_errors_to (no)\fR"
79 /* Report mail delivery errors to the address specified with the
80 /* non-standard Errors-To: message header, instead of the envelope
81 /* sender address (this feature is removed with Postfix version 2.2, is
82 /* turned off by default with Postfix version 2.1, and is always turned on
83 /* with older Postfix versions).
85 /* Available in Postfix version 2.6 and later:
86 /* .IP "\fBalways_add_missing_headers (no)\fR"
87 /* Always add (Resent-) From:, To:, Date: or Message-ID: headers
89 /* BUILT-IN CONTENT FILTERING CONTROLS
92 /* Postfix built-in content filtering is meant to stop a flood of
93 /* worms or viruses. It is not a general content filter.
94 /* .IP "\fBbody_checks (empty)\fR"
95 /* Optional lookup tables for content inspection as specified in
96 /* the \fBbody_checks\fR(5) manual page.
97 /* .IP "\fBheader_checks (empty)\fR"
98 /* Optional lookup tables for content inspection of primary non-MIME
99 /* message headers, as specified in the \fBheader_checks\fR(5) manual page.
101 /* Available in Postfix version 2.0 and later:
102 /* .IP "\fBbody_checks_size_limit (51200)\fR"
103 /* How much text in a message body segment (or attachment, if you
104 /* prefer to use that term) is subjected to body_checks inspection.
105 /* .IP "\fBmime_header_checks ($header_checks)\fR"
106 /* Optional lookup tables for content inspection of MIME related
107 /* message headers, as described in the \fBheader_checks\fR(5) manual page.
108 /* .IP "\fBnested_header_checks ($header_checks)\fR"
109 /* Optional lookup tables for content inspection of non-MIME message
110 /* headers in attached messages, as described in the \fBheader_checks\fR(5)
113 /* Available in Postfix version 2.3 and later:
114 /* .IP "\fBmessage_reject_characters (empty)\fR"
115 /* The set of characters that Postfix will reject in message
117 /* .IP "\fBmessage_strip_characters (empty)\fR"
118 /* The set of characters that Postfix will remove from message
120 /* BEFORE QUEUE MILTER CONTROLS
123 /* As of version 2.3, Postfix supports the Sendmail version 8
124 /* Milter (mail filter) protocol. When mail is not received via
125 /* the smtpd(8) server, the cleanup(8) server will simulate
126 /* SMTP events to the extent that this is possible. For details
127 /* see the MILTER_README document.
128 /* .IP "\fBnon_smtpd_milters (empty)\fR"
129 /* A list of Milter (mail filter) applications for new mail that
130 /* does not arrive via the Postfix \fBsmtpd\fR(8) server.
131 /* .IP "\fBmilter_protocol (6)\fR"
132 /* The mail filter protocol version and optional protocol extensions
133 /* for communication with a Milter application; prior to Postfix 2.6
134 /* the default protocol is 2.
135 /* .IP "\fBmilter_default_action (tempfail)\fR"
136 /* The default action when a Milter (mail filter) application is
137 /* unavailable or mis-configured.
138 /* .IP "\fBmilter_macro_daemon_name ($myhostname)\fR"
139 /* The {daemon_name} macro value for Milter (mail filter) applications.
140 /* .IP "\fBmilter_macro_v ($mail_name $mail_version)\fR"
141 /* The {v} macro value for Milter (mail filter) applications.
142 /* .IP "\fBmilter_connect_timeout (30s)\fR"
143 /* The time limit for connecting to a Milter (mail filter)
144 /* application, and for negotiating protocol options.
145 /* .IP "\fBmilter_command_timeout (30s)\fR"
146 /* The time limit for sending an SMTP command to a Milter (mail
147 /* filter) application, and for receiving the response.
148 /* .IP "\fBmilter_content_timeout (300s)\fR"
149 /* The time limit for sending message content to a Milter (mail
150 /* filter) application, and for receiving the response.
151 /* .IP "\fBmilter_connect_macros (see 'postconf -d' output)\fR"
152 /* The macros that are sent to Milter (mail filter) applications
153 /* after completion of an SMTP connection.
154 /* .IP "\fBmilter_helo_macros (see 'postconf -d' output)\fR"
155 /* The macros that are sent to Milter (mail filter) applications
156 /* after the SMTP HELO or EHLO command.
157 /* .IP "\fBmilter_mail_macros (see 'postconf -d' output)\fR"
158 /* The macros that are sent to Milter (mail filter) applications
159 /* after the SMTP MAIL FROM command.
160 /* .IP "\fBmilter_rcpt_macros (see 'postconf -d' output)\fR"
161 /* The macros that are sent to Milter (mail filter) applications
162 /* after the SMTP RCPT TO command.
163 /* .IP "\fBmilter_data_macros (see 'postconf -d' output)\fR"
164 /* The macros that are sent to version 4 or higher Milter (mail
165 /* filter) applications after the SMTP DATA command.
166 /* .IP "\fBmilter_unknown_command_macros (see 'postconf -d' output)\fR"
167 /* The macros that are sent to version 3 or higher Milter (mail
168 /* filter) applications after an unknown SMTP command.
169 /* .IP "\fBmilter_end_of_data_macros (see 'postconf -d' output)\fR"
170 /* The macros that are sent to Milter (mail filter) applications
171 /* after the message end-of-data.
173 /* Available in Postfix version 2.5 and later:
174 /* .IP "\fBmilter_end_of_header_macros (see 'postconf -d' output)\fR"
175 /* The macros that are sent to Milter (mail filter) applications
176 /* after the end of the message header.
177 /* MIME PROCESSING CONTROLS
180 /* Available in Postfix version 2.0 and later:
181 /* .IP "\fBdisable_mime_input_processing (no)\fR"
182 /* Turn off MIME processing while receiving mail.
183 /* .IP "\fBmime_boundary_length_limit (2048)\fR"
184 /* The maximal length of MIME multipart boundary strings.
185 /* .IP "\fBmime_nesting_limit (100)\fR"
186 /* The maximal recursion level that the MIME processor will handle.
187 /* .IP "\fBstrict_8bitmime (no)\fR"
188 /* Enable both strict_7bit_headers and strict_8bitmime_body.
189 /* .IP "\fBstrict_7bit_headers (no)\fR"
190 /* Reject mail with 8-bit text in message headers.
191 /* .IP "\fBstrict_8bitmime_body (no)\fR"
192 /* Reject 8-bit message body text without 8-bit MIME content encoding
194 /* .IP "\fBstrict_mime_encoding_domain (no)\fR"
195 /* Reject mail with invalid Content-Transfer-Encoding: information
196 /* for the message/* or multipart/* MIME content types.
198 /* Available in Postfix version 2.5 and later:
199 /* .IP "\fBdetect_8bit_encoding_header (yes)\fR"
200 /* Automatically detect 8BITMIME body content by looking at
201 /* Content-Transfer-Encoding: message headers; historically, this
202 /* behavior was hard-coded to be "always on".
203 /* AUTOMATIC BCC RECIPIENT CONTROLS
206 /* Postfix can automatically add BCC (blind carbon copy)
207 /* when mail enters the mail system:
208 /* .IP "\fBalways_bcc (empty)\fR"
209 /* Optional address that receives a "blind carbon copy" of each message
210 /* that is received by the Postfix mail system.
212 /* Available in Postfix version 2.1 and later:
213 /* .IP "\fBsender_bcc_maps (empty)\fR"
214 /* Optional BCC (blind carbon-copy) address lookup tables, indexed
215 /* by sender address.
216 /* .IP "\fBrecipient_bcc_maps (empty)\fR"
217 /* Optional BCC (blind carbon-copy) address lookup tables, indexed by
218 /* recipient address.
219 /* ADDRESS TRANSFORMATION CONTROLS
222 /* Address rewriting is delegated to the \fBtrivial-rewrite\fR(8) daemon.
223 /* The \fBcleanup\fR(8) server implements table driven address mapping.
224 /* .IP "\fBempty_address_recipient (MAILER-DAEMON)\fR"
225 /* The recipient of mail addressed to the null address.
226 /* .IP "\fBcanonical_maps (empty)\fR"
227 /* Optional address mapping lookup tables for message headers and
229 /* .IP "\fBrecipient_canonical_maps (empty)\fR"
230 /* Optional address mapping lookup tables for envelope and header
231 /* recipient addresses.
232 /* .IP "\fBsender_canonical_maps (empty)\fR"
233 /* Optional address mapping lookup tables for envelope and header
235 /* .IP "\fBmasquerade_classes (envelope_sender, header_sender, header_recipient)\fR"
236 /* What addresses are subject to address masquerading.
237 /* .IP "\fBmasquerade_domains (empty)\fR"
238 /* Optional list of domains whose subdomain structure will be stripped
239 /* off in email addresses.
240 /* .IP "\fBmasquerade_exceptions (empty)\fR"
241 /* Optional list of user names that are not subjected to address
242 /* masquerading, even when their address matches $masquerade_domains.
243 /* .IP "\fBpropagate_unmatched_extensions (canonical, virtual)\fR"
244 /* What address lookup tables copy an address extension from the lookup
245 /* key to the lookup result.
247 /* Available before Postfix version 2.0:
248 /* .IP "\fBvirtual_maps (empty)\fR"
249 /* Optional lookup tables with a) names of domains for which all
250 /* addresses are aliased to addresses in other local or remote domains,
251 /* and b) addresses that are aliased to addresses in other local or
254 /* Available in Postfix version 2.0 and later:
255 /* .IP "\fBvirtual_alias_maps ($virtual_maps)\fR"
256 /* Optional lookup tables that alias specific mail addresses or domains
257 /* to other local or remote address.
259 /* Available in Postfix version 2.2 and later:
260 /* .IP "\fBcanonical_classes (envelope_sender, envelope_recipient, header_sender, header_recipient)\fR"
261 /* What addresses are subject to canonical_maps address mapping.
262 /* .IP "\fBrecipient_canonical_classes (envelope_recipient, header_recipient)\fR"
263 /* What addresses are subject to recipient_canonical_maps address
265 /* .IP "\fBsender_canonical_classes (envelope_sender, header_sender)\fR"
266 /* What addresses are subject to sender_canonical_maps address
268 /* .IP "\fBremote_header_rewrite_domain (empty)\fR"
269 /* Don't rewrite message headers from remote clients at all when
270 /* this parameter is empty; otherwise, rewrite message headers and
271 /* append the specified domain name to incomplete addresses.
272 /* RESOURCE AND RATE CONTROLS
275 /* .IP "\fBduplicate_filter_limit (1000)\fR"
276 /* The maximal number of addresses remembered by the address
277 /* duplicate filter for \fBaliases\fR(5) or \fBvirtual\fR(5) alias expansion, or
278 /* for \fBshowq\fR(8) queue displays.
279 /* .IP "\fBheader_size_limit (102400)\fR"
280 /* The maximal amount of memory in bytes for storing a message header.
281 /* .IP "\fBhopcount_limit (50)\fR"
282 /* The maximal number of Received: message headers that is allowed
283 /* in the primary message headers.
284 /* .IP "\fBin_flow_delay (1s)\fR"
285 /* Time to pause before accepting a new message, when the message
286 /* arrival rate exceeds the message delivery rate.
287 /* .IP "\fBmessage_size_limit (10240000)\fR"
288 /* The maximal size in bytes of a message, including envelope information.
290 /* Available in Postfix version 2.0 and later:
291 /* .IP "\fBheader_address_token_limit (10240)\fR"
292 /* The maximal number of address tokens are allowed in an address
294 /* .IP "\fBmime_boundary_length_limit (2048)\fR"
295 /* The maximal length of MIME multipart boundary strings.
296 /* .IP "\fBmime_nesting_limit (100)\fR"
297 /* The maximal recursion level that the MIME processor will handle.
298 /* .IP "\fBqueue_file_attribute_count_limit (100)\fR"
299 /* The maximal number of (name=value) attributes that may be stored
300 /* in a Postfix queue file.
302 /* Available in Postfix version 2.1 and later:
303 /* .IP "\fBvirtual_alias_expansion_limit (1000)\fR"
304 /* The maximal number of addresses that virtual alias expansion produces
305 /* from each original recipient.
306 /* .IP "\fBvirtual_alias_recursion_limit (1000)\fR"
307 /* The maximal nesting depth of virtual alias expansion.
308 /* MISCELLANEOUS CONTROLS
311 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
312 /* The default location of the Postfix main.cf and master.cf
313 /* configuration files.
314 /* .IP "\fBdaemon_timeout (18000s)\fR"
315 /* How much time a Postfix daemon process may take to handle a
316 /* request before it is terminated by a built-in watchdog timer.
317 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
318 /* The maximal number of digits after the decimal point when logging
319 /* sub-second delay values.
320 /* .IP "\fBdelay_warning_time (0h)\fR"
321 /* The time after which the sender receives the message headers of
322 /* mail that is still queued.
323 /* .IP "\fBipc_timeout (3600s)\fR"
324 /* The time limit for sending or receiving information over an internal
325 /* communication channel.
326 /* .IP "\fBmax_idle (100s)\fR"
327 /* The maximum amount of time that an idle Postfix daemon process waits
328 /* for an incoming connection before terminating voluntarily.
329 /* .IP "\fBmax_use (100)\fR"
330 /* The maximal number of incoming connections that a Postfix daemon
331 /* process will service before terminating voluntarily.
332 /* .IP "\fBmyhostname (see 'postconf -d' output)\fR"
333 /* The internet hostname of this mail system.
334 /* .IP "\fBmyorigin ($myhostname)\fR"
335 /* The domain name that locally-posted mail appears to come
336 /* from, and that locally posted mail is delivered to.
337 /* .IP "\fBprocess_id (read-only)\fR"
338 /* The process ID of a Postfix command or daemon process.
339 /* .IP "\fBprocess_name (read-only)\fR"
340 /* The process name of a Postfix command or daemon process.
341 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
342 /* The location of the Postfix top-level queue directory.
343 /* .IP "\fBsoft_bounce (no)\fR"
344 /* Safety net to keep mail queued that would otherwise be returned to
346 /* .IP "\fBsyslog_facility (mail)\fR"
347 /* The syslog facility of Postfix logging.
348 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
349 /* The mail system name that is prepended to the process name in syslog
350 /* records, so that "smtpd" becomes, for example, "postfix/smtpd".
352 /* Available in Postfix version 2.1 and later:
353 /* .IP "\fBenable_original_recipient (yes)\fR"
354 /* Enable support for the X-Original-To message header.
356 /* /etc/postfix/canonical*, canonical mapping table
357 /* /etc/postfix/virtual*, virtual mapping table
359 /* trivial-rewrite(8), address rewriting
360 /* qmgr(8), queue manager
361 /* header_checks(5), message header content inspection
362 /* body_checks(5), body parts content inspection
363 /* canonical(5), canonical address lookup table format
364 /* virtual(5), virtual alias lookup table format
365 /* postconf(5), configuration parameters
366 /* master(5), generic daemon options
367 /* master(8), process manager
368 /* syslogd(8), system logging
372 /* Use "\fBpostconf readme_directory\fR" or
373 /* "\fBpostconf html_directory\fR" to locate this information.
376 /* ADDRESS_REWRITING_README Postfix address manipulation
377 /* CONTENT_INSPECTION_README content inspection
381 /* The Secure Mailer license must be distributed with this software.
384 /* IBM T.J. Watson Research
386 /* Yorktown Heights, NY 10598, USA
389 /* System library. */
391 #include <sys_defs.h>
396 /* Utility library. */
402 /* Global library. */
404 #include <mail_conf.h>
405 #include <cleanup_user.h>
406 #include <mail_proto.h>
407 #include <mail_params.h>
409 #include <rec_type.h>
410 #include <mail_version.h>
412 /* Single-threaded server skeleton. */
414 #include <mail_server.h>
416 /* Application-specific. */
420 /* cleanup_service - process one request to inject a message into the queue */
422 static void cleanup_service(VSTREAM
*src
, char *unused_service
, char **argv
)
424 VSTRING
*buf
= vstring_alloc(100);
425 CLEANUP_STATE
*state
;
431 * Sanity check. This service takes no command-line arguments.
434 msg_fatal("unexpected command-line argument: %s", argv
[0]);
437 * Open a queue file and initialize state.
439 state
= cleanup_open(src
);
442 * Send the queue id to the client. Read client processing options. If we
443 * can't read the client processing options we can pretty much forget
444 * about the whole operation.
446 attr_print(src
, ATTR_FLAG_NONE
,
447 ATTR_TYPE_STR
, MAIL_ATTR_QUEUEID
, state
->queue_id
,
449 if (attr_scan(src
, ATTR_FLAG_STRICT
,
450 ATTR_TYPE_INT
, MAIL_ATTR_FLAGS
, &flags
,
451 ATTR_TYPE_END
) != 1) {
452 state
->errs
|= CLEANUP_STAT_BAD
;
455 cleanup_control(state
, flags
);
458 * XXX Rely on the front-end programs to enforce record size limits.
460 * First, copy the envelope records to the queue file. Then, copy the
461 * message content (headers and body). Finally, attach any information
462 * extracted from message headers.
464 while (CLEANUP_OUT_OK(state
)) {
465 if ((type
= rec_get_raw(src
, buf
, 0, REC_FLAG_NONE
)) < 0) {
466 state
->errs
|= CLEANUP_STAT_BAD
;
469 if (type
== REC_TYPE_PTR
|| type
== REC_TYPE_DTXT
470 || type
== REC_TYPE_DRCP
) {
471 msg_warn("%s: record type %d not allowed - discarding this message",
472 state
->queue_id
, type
);
473 state
->errs
|= CLEANUP_STAT_BAD
;
476 CLEANUP_RECORD(state
, type
, vstring_str(buf
), VSTRING_LEN(buf
));
477 if (type
== REC_TYPE_END
)
482 * Keep reading in case of problems, until the sender is ready to receive
485 if (CLEANUP_OUT_OK(state
) == 0 && type
> 0) {
486 while (type
!= REC_TYPE_END
487 && (type
= rec_get(src
, buf
, 0)) > 0)
492 * Log something to make timeout errors easier to debug.
494 if (vstream_ftimeout(src
))
495 msg_warn("%s: read timeout on %s",
496 state
->queue_id
, VSTREAM_PATH(src
));
499 * Finish this message, and report the result status to the client.
501 status
= cleanup_flush(state
); /* in case state is modified */
502 attr_print(src
, ATTR_FLAG_NONE
,
503 ATTR_TYPE_INT
, MAIL_ATTR_STATUS
, status
,
504 ATTR_TYPE_STR
, MAIL_ATTR_WHY
,
505 (state
->flags
& CLEANUP_FLAG_SMTP_REPLY
)
506 && state
->smtp_reply
? state
->smtp_reply
:
507 state
->reason
? state
->reason
: "",
517 /* pre_accept - see if tables have changed */
519 static void pre_accept(char *unused_name
, char **unused_argv
)
523 if ((table
= dict_changed_name()) != 0) {
524 msg_info("table %s has changed -- restarting", table
);
529 MAIL_VERSION_STAMP_DECLARE
;
531 /* main - the main program */
533 int main(int argc
, char **argv
)
537 * Fingerprint executables and core dumps.
539 MAIL_VERSION_STAMP_ALLOCATE
;
542 * Clean up an incomplete queue file in case of a fatal run-time error,
543 * or after receiving SIGTERM from the master at shutdown time.
545 signal(SIGTERM
, cleanup_sig
);
546 msg_cleanup(cleanup_all
);
549 * Pass control to the single-threaded service skeleton.
551 single_server_main(argc
, argv
, cleanup_service
,
552 MAIL_SERVER_BOOL_TABLE
, cleanup_bool_table
,
553 MAIL_SERVER_INT_TABLE
, cleanup_int_table
,
554 MAIL_SERVER_BOOL_TABLE
, cleanup_bool_table
,
555 MAIL_SERVER_STR_TABLE
, cleanup_str_table
,
556 MAIL_SERVER_TIME_TABLE
, cleanup_time_table
,
557 MAIL_SERVER_PRE_INIT
, cleanup_pre_jail
,
558 MAIL_SERVER_POST_INIT
, cleanup_post_jail
,
559 MAIL_SERVER_PRE_ACCEPT
, pre_accept
,
560 MAIL_SERVER_IN_FLOW_DELAY
,
561 MAIL_SERVER_UNLIMITED
,