Postfix3.3.1
cleanup.c
[詳解]
1 /*++
2 /* NAME
3 /* cleanup 8
4 /* SUMMARY
5 /* canonicalize and enqueue Postfix message
6 /* SYNOPSIS
7 /* \fBcleanup\fR [generic Postfix daemon options]
8 /* DESCRIPTION
9 /* The \fBcleanup\fR(8) daemon processes inbound mail, inserts it
10 /* into the \fBincoming\fR mail queue, and informs the queue
11 /* manager of its arrival.
12 /*
13 /* The \fBcleanup\fR(8) daemon always performs the following transformations:
14 /* .IP \(bu
15 /* Insert missing message headers: (\fBResent-\fR) \fBFrom:\fR,
16 /* \fBTo:\fR, \fBMessage-Id:\fR, and \fBDate:\fR.
17 /* .IP \(bu
18 /* Transform envelope and header addresses to the standard
19 /* \fIuser@fully-qualified-domain\fR form that is expected by other
20 /* Postfix programs.
21 /* This task is delegated to the \fBtrivial-rewrite\fR(8) daemon.
22 /* .IP \(bu
23 /* Eliminate duplicate envelope recipient addresses.
24 /* .IP \(bu
25 /* Remove message headers: \fBBcc\fR, \fBContent-Length\fR,
26 /* \fBResent-Bcc\fR, \fBReturn-Path\fR.
27 /* .PP
28 /* The following address transformations are optional:
29 /* .IP \(bu
30 /* Optionally, rewrite all envelope and header addresses according
31 /* to the mappings specified in the \fBcanonical\fR(5) lookup tables.
32 /* .IP \(bu
33 /* Optionally, masquerade envelope sender addresses and message
34 /* header addresses (i.e. strip host or domain information below
35 /* all domains listed in the \fBmasquerade_domains\fR parameter,
36 /* except for user names listed in \fBmasquerade_exceptions\fR).
37 /* By default, address masquerading does not affect envelope recipients.
38 /* .IP \(bu
39 /* Optionally, expand envelope recipients according to information
40 /* found in the \fBvirtual\fR(5) lookup tables.
41 /* .PP
42 /* The \fBcleanup\fR(8) daemon performs sanity checks on the content of
43 /* each message. When it finds a problem, by default it returns a
44 /* diagnostic status to the client, and leaves it up to the client
45 /* to deal with the problem. Alternatively, the client can request
46 /* the \fBcleanup\fR(8) daemon to bounce the message back to the sender
47 /* in case of trouble.
48 /* STANDARDS
49 /* RFC 822 (ARPA Internet Text Messages)
50 /* RFC 2045 (MIME: Format of Internet Message Bodies)
51 /* RFC 2046 (MIME: Media Types)
52 /* RFC 2822 (Internet Message Format)
53 /* RFC 3463 (Enhanced Status Codes)
54 /* RFC 3464 (Delivery status notifications)
55 /* RFC 5322 (Internet Message Format)
56 /* DIAGNOSTICS
57 /* Problems and transactions are logged to \fBsyslogd\fR(8).
58 /* BUGS
59 /* Table-driven rewriting rules make it hard to express \fBif then
60 /* else\fR and other logical relationships.
61 /* CONFIGURATION PARAMETERS
62 /* .ad
63 /* .fi
64 /* Changes to \fBmain.cf\fR are picked up automatically, as
65 /* \fBcleanup\fR(8)
66 /* processes run for only a limited amount of time. Use the command
67 /* "\fBpostfix reload\fR" to speed up a change.
68 /*
69 /* The text below provides only a parameter summary. See
70 /* \fBpostconf\fR(5) for more details including examples.
71 /* COMPATIBILITY CONTROLS
72 /* .ad
73 /* .fi
74 /* .IP "\fBundisclosed_recipients_header (see 'postconf -d' output)\fR"
75 /* Message header that the Postfix \fBcleanup\fR(8) server inserts when a
76 /* message contains no To: or Cc: message header.
77 /* .PP
78 /* Available in Postfix version 2.1 only:
79 /* .IP "\fBenable_errors_to (no)\fR"
80 /* Report mail delivery errors to the address specified with the
81 /* non-standard Errors-To: message header, instead of the envelope
82 /* sender address (this feature is removed with Postfix version 2.2, is
83 /* turned off by default with Postfix version 2.1, and is always turned on
84 /* with older Postfix versions).
85 /* .PP
86 /* Available in Postfix version 2.6 and later:
87 /* .IP "\fBalways_add_missing_headers (no)\fR"
88 /* Always add (Resent-) From:, To:, Date: or Message-ID: headers
89 /* when not present.
90 /* .PP
91 /* Available in Postfix version 2.9 and later:
92 /* .IP "\fBenable_long_queue_ids (no)\fR"
93 /* Enable long, non-repeating, queue IDs (queue file names).
94 /* .PP
95 /* Available in Postfix version 3.0 and later:
96 /* .IP "\fBmessage_drop_headers (bcc, content-length, resent-bcc, return-path)\fR"
97 /* Names of message headers that the \fBcleanup\fR(8) daemon will remove
98 /* after applying \fBheader_checks\fR(5) and before invoking Milter applications.
99 /* BUILT-IN CONTENT FILTERING CONTROLS
100 /* .ad
101 /* .fi
102 /* Postfix built-in content filtering is meant to stop a flood of
103 /* worms or viruses. It is not a general content filter.
104 /* .IP "\fBbody_checks (empty)\fR"
105 /* Optional lookup tables for content inspection as specified in
106 /* the \fBbody_checks\fR(5) manual page.
107 /* .IP "\fBheader_checks (empty)\fR"
108 /* Optional lookup tables for content inspection of primary non-MIME
109 /* message headers, as specified in the \fBheader_checks\fR(5) manual page.
110 /* .PP
111 /* Available in Postfix version 2.0 and later:
112 /* .IP "\fBbody_checks_size_limit (51200)\fR"
113 /* How much text in a message body segment (or attachment, if you
114 /* prefer to use that term) is subjected to body_checks inspection.
115 /* .IP "\fBmime_header_checks ($header_checks)\fR"
116 /* Optional lookup tables for content inspection of MIME related
117 /* message headers, as described in the \fBheader_checks\fR(5) manual page.
118 /* .IP "\fBnested_header_checks ($header_checks)\fR"
119 /* Optional lookup tables for content inspection of non-MIME message
120 /* headers in attached messages, as described in the \fBheader_checks\fR(5)
121 /* manual page.
122 /* .PP
123 /* Available in Postfix version 2.3 and later:
124 /* .IP "\fBmessage_reject_characters (empty)\fR"
125 /* The set of characters that Postfix will reject in message
126 /* content.
127 /* .IP "\fBmessage_strip_characters (empty)\fR"
128 /* The set of characters that Postfix will remove from message
129 /* content.
130 /* BEFORE QUEUE MILTER CONTROLS
131 /* .ad
132 /* .fi
133 /* As of version 2.3, Postfix supports the Sendmail version 8
134 /* Milter (mail filter) protocol. When mail is not received via
135 /* the smtpd(8) server, the cleanup(8) server will simulate
136 /* SMTP events to the extent that this is possible. For details
137 /* see the MILTER_README document.
138 /* .IP "\fBnon_smtpd_milters (empty)\fR"
139 /* A list of Milter (mail filter) applications for new mail that
140 /* does not arrive via the Postfix \fBsmtpd\fR(8) server.
141 /* .IP "\fBmilter_protocol (6)\fR"
142 /* The mail filter protocol version and optional protocol extensions
143 /* for communication with a Milter application; prior to Postfix 2.6
144 /* the default protocol is 2.
145 /* .IP "\fBmilter_default_action (tempfail)\fR"
146 /* The default action when a Milter (mail filter) application is
147 /* unavailable or mis-configured.
148 /* .IP "\fBmilter_macro_daemon_name ($myhostname)\fR"
149 /* The {daemon_name} macro value for Milter (mail filter) applications.
150 /* .IP "\fBmilter_macro_v ($mail_name $mail_version)\fR"
151 /* The {v} macro value for Milter (mail filter) applications.
152 /* .IP "\fBmilter_connect_timeout (30s)\fR"
153 /* The time limit for connecting to a Milter (mail filter)
154 /* application, and for negotiating protocol options.
155 /* .IP "\fBmilter_command_timeout (30s)\fR"
156 /* The time limit for sending an SMTP command to a Milter (mail
157 /* filter) application, and for receiving the response.
158 /* .IP "\fBmilter_content_timeout (300s)\fR"
159 /* The time limit for sending message content to a Milter (mail
160 /* filter) application, and for receiving the response.
161 /* .IP "\fBmilter_connect_macros (see 'postconf -d' output)\fR"
162 /* The macros that are sent to Milter (mail filter) applications
163 /* after completion of an SMTP connection.
164 /* .IP "\fBmilter_helo_macros (see 'postconf -d' output)\fR"
165 /* The macros that are sent to Milter (mail filter) applications
166 /* after the SMTP HELO or EHLO command.
167 /* .IP "\fBmilter_mail_macros (see 'postconf -d' output)\fR"
168 /* The macros that are sent to Milter (mail filter) applications
169 /* after the SMTP MAIL FROM command.
170 /* .IP "\fBmilter_rcpt_macros (see 'postconf -d' output)\fR"
171 /* The macros that are sent to Milter (mail filter) applications
172 /* after the SMTP RCPT TO command.
173 /* .IP "\fBmilter_data_macros (see 'postconf -d' output)\fR"
174 /* The macros that are sent to version 4 or higher Milter (mail
175 /* filter) applications after the SMTP DATA command.
176 /* .IP "\fBmilter_unknown_command_macros (see 'postconf -d' output)\fR"
177 /* The macros that are sent to version 3 or higher Milter (mail
178 /* filter) applications after an unknown SMTP command.
179 /* .IP "\fBmilter_end_of_data_macros (see 'postconf -d' output)\fR"
180 /* The macros that are sent to Milter (mail filter) applications
181 /* after the message end-of-data.
182 /* .PP
183 /* Available in Postfix version 2.5 and later:
184 /* .IP "\fBmilter_end_of_header_macros (see 'postconf -d' output)\fR"
185 /* The macros that are sent to Milter (mail filter) applications
186 /* after the end of the message header.
187 /* .PP
188 /* Available in Postfix version 2.7 and later:
189 /* .IP "\fBmilter_header_checks (empty)\fR"
190 /* Optional lookup tables for content inspection of message headers
191 /* that are produced by Milter applications.
192 /* .PP
193 /* Available in Postfix version 3.1 and later:
194 /* .IP "\fBmilter_macro_defaults (empty)\fR"
195 /* Optional list of \fIname=value\fR pairs that specify default
196 /* values for arbitrary macros that Postfix may send to Milter
197 /* applications.
198 /* MIME PROCESSING CONTROLS
199 /* .ad
200 /* .fi
201 /* Available in Postfix version 2.0 and later:
202 /* .IP "\fBdisable_mime_input_processing (no)\fR"
203 /* Turn off MIME processing while receiving mail.
204 /* .IP "\fBmime_boundary_length_limit (2048)\fR"
205 /* The maximal length of MIME multipart boundary strings.
206 /* .IP "\fBmime_nesting_limit (100)\fR"
207 /* The maximal recursion level that the MIME processor will handle.
208 /* .IP "\fBstrict_8bitmime (no)\fR"
209 /* Enable both strict_7bit_headers and strict_8bitmime_body.
210 /* .IP "\fBstrict_7bit_headers (no)\fR"
211 /* Reject mail with 8-bit text in message headers.
212 /* .IP "\fBstrict_8bitmime_body (no)\fR"
213 /* Reject 8-bit message body text without 8-bit MIME content encoding
214 /* information.
215 /* .IP "\fBstrict_mime_encoding_domain (no)\fR"
216 /* Reject mail with invalid Content-Transfer-Encoding: information
217 /* for the message/* or multipart/* MIME content types.
218 /* .PP
219 /* Available in Postfix version 2.5 and later:
220 /* .IP "\fBdetect_8bit_encoding_header (yes)\fR"
221 /* Automatically detect 8BITMIME body content by looking at
222 /* Content-Transfer-Encoding: message headers; historically, this
223 /* behavior was hard-coded to be "always on".
224 /* AUTOMATIC BCC RECIPIENT CONTROLS
225 /* .ad
226 /* .fi
227 /* Postfix can automatically add BCC (blind carbon copy)
228 /* when mail enters the mail system:
229 /* .IP "\fBalways_bcc (empty)\fR"
230 /* Optional address that receives a "blind carbon copy" of each message
231 /* that is received by the Postfix mail system.
232 /* .PP
233 /* Available in Postfix version 2.1 and later:
234 /* .IP "\fBsender_bcc_maps (empty)\fR"
235 /* Optional BCC (blind carbon-copy) address lookup tables, indexed
236 /* by sender address.
237 /* .IP "\fBrecipient_bcc_maps (empty)\fR"
238 /* Optional BCC (blind carbon-copy) address lookup tables, indexed by
239 /* recipient address.
240 /* ADDRESS TRANSFORMATION CONTROLS
241 /* .ad
242 /* .fi
243 /* Address rewriting is delegated to the \fBtrivial-rewrite\fR(8) daemon.
244 /* The \fBcleanup\fR(8) server implements table driven address mapping.
245 /* .IP "\fBempty_address_recipient (MAILER-DAEMON)\fR"
246 /* The recipient of mail addressed to the null address.
247 /* .IP "\fBcanonical_maps (empty)\fR"
248 /* Optional address mapping lookup tables for message headers and
249 /* envelopes.
250 /* .IP "\fBrecipient_canonical_maps (empty)\fR"
251 /* Optional address mapping lookup tables for envelope and header
252 /* recipient addresses.
253 /* .IP "\fBsender_canonical_maps (empty)\fR"
254 /* Optional address mapping lookup tables for envelope and header
255 /* sender addresses.
256 /* .IP "\fBmasquerade_classes (envelope_sender, header_sender, header_recipient)\fR"
257 /* What addresses are subject to address masquerading.
258 /* .IP "\fBmasquerade_domains (empty)\fR"
259 /* Optional list of domains whose subdomain structure will be stripped
260 /* off in email addresses.
261 /* .IP "\fBmasquerade_exceptions (empty)\fR"
262 /* Optional list of user names that are not subjected to address
263 /* masquerading, even when their address matches $masquerade_domains.
264 /* .IP "\fBpropagate_unmatched_extensions (canonical, virtual)\fR"
265 /* What address lookup tables copy an address extension from the lookup
266 /* key to the lookup result.
267 /* .PP
268 /* Available before Postfix version 2.0:
269 /* .IP "\fBvirtual_maps (empty)\fR"
270 /* Optional lookup tables with a) names of domains for which all
271 /* addresses are aliased to addresses in other local or remote domains,
272 /* and b) addresses that are aliased to addresses in other local or
273 /* remote domains.
274 /* .PP
275 /* Available in Postfix version 2.0 and later:
276 /* .IP "\fBvirtual_alias_maps ($virtual_maps)\fR"
277 /* Optional lookup tables that alias specific mail addresses or domains
278 /* to other local or remote address.
279 /* .PP
280 /* Available in Postfix version 2.2 and later:
281 /* .IP "\fBcanonical_classes (envelope_sender, envelope_recipient, header_sender, header_recipient)\fR"
282 /* What addresses are subject to canonical_maps address mapping.
283 /* .IP "\fBrecipient_canonical_classes (envelope_recipient, header_recipient)\fR"
284 /* What addresses are subject to recipient_canonical_maps address
285 /* mapping.
286 /* .IP "\fBsender_canonical_classes (envelope_sender, header_sender)\fR"
287 /* What addresses are subject to sender_canonical_maps address
288 /* mapping.
289 /* .IP "\fBremote_header_rewrite_domain (empty)\fR"
290 /* Don't rewrite message headers from remote clients at all when
291 /* this parameter is empty; otherwise, rewrite message headers and
292 /* append the specified domain name to incomplete addresses.
293 /* RESOURCE AND RATE CONTROLS
294 /* .ad
295 /* .fi
296 /* .IP "\fBduplicate_filter_limit (1000)\fR"
297 /* The maximal number of addresses remembered by the address
298 /* duplicate filter for \fBaliases\fR(5) or \fBvirtual\fR(5) alias expansion, or
299 /* for \fBshowq\fR(8) queue displays.
300 /* .IP "\fBheader_size_limit (102400)\fR"
301 /* The maximal amount of memory in bytes for storing a message header.
302 /* .IP "\fBhopcount_limit (50)\fR"
303 /* The maximal number of Received: message headers that is allowed
304 /* in the primary message headers.
305 /* .IP "\fBin_flow_delay (1s)\fR"
306 /* Time to pause before accepting a new message, when the message
307 /* arrival rate exceeds the message delivery rate.
308 /* .IP "\fBmessage_size_limit (10240000)\fR"
309 /* The maximal size in bytes of a message, including envelope information.
310 /* .PP
311 /* Available in Postfix version 2.0 and later:
312 /* .IP "\fBheader_address_token_limit (10240)\fR"
313 /* The maximal number of address tokens are allowed in an address
314 /* message header.
315 /* .IP "\fBmime_boundary_length_limit (2048)\fR"
316 /* The maximal length of MIME multipart boundary strings.
317 /* .IP "\fBmime_nesting_limit (100)\fR"
318 /* The maximal recursion level that the MIME processor will handle.
319 /* .IP "\fBqueue_file_attribute_count_limit (100)\fR"
320 /* The maximal number of (name=value) attributes that may be stored
321 /* in a Postfix queue file.
322 /* .PP
323 /* Available in Postfix version 2.1 and later:
324 /* .IP "\fBvirtual_alias_expansion_limit (1000)\fR"
325 /* The maximal number of addresses that virtual alias expansion produces
326 /* from each original recipient.
327 /* .IP "\fBvirtual_alias_recursion_limit (1000)\fR"
328 /* The maximal nesting depth of virtual alias expansion.
329 /* .PP
330 /* Available in Postfix version 3.0 and later:
331 /* .IP "\fBvirtual_alias_address_length_limit (1000)\fR"
332 /* The maximal length of an email address after virtual alias expansion.
333 /* SMTPUTF8 CONTROLS
334 /* .ad
335 /* .fi
336 /* Preliminary SMTPUTF8 support is introduced with Postfix 3.0.
337 /* .IP "\fBsmtputf8_enable (yes)\fR"
338 /* Enable preliminary SMTPUTF8 support for the protocols described
339 /* in RFC 6531..6533.
340 /* .IP "\fBsmtputf8_autodetect_classes (sendmail, verify)\fR"
341 /* Detect that a message requires SMTPUTF8 support for the specified
342 /* mail origin classes.
343 /* .PP
344 /* Available in Postfix version 3.2 and later:
345 /* .IP "\fBenable_idna2003_compatibility (no)\fR"
346 /* Enable 'transitional' compatibility between IDNA2003 and IDNA2008,
347 /* when converting UTF-8 domain names to/from the ASCII form that is
348 /* used for DNS lookups.
349 /* MISCELLANEOUS CONTROLS
350 /* .ad
351 /* .fi
352 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
353 /* The default location of the Postfix main.cf and master.cf
354 /* configuration files.
355 /* .IP "\fBdaemon_timeout (18000s)\fR"
356 /* How much time a Postfix daemon process may take to handle a
357 /* request before it is terminated by a built-in watchdog timer.
358 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
359 /* The maximal number of digits after the decimal point when logging
360 /* sub-second delay values.
361 /* .IP "\fBdelay_warning_time (0h)\fR"
362 /* The time after which the sender receives a copy of the message
363 /* headers of mail that is still queued.
364 /* .IP "\fBipc_timeout (3600s)\fR"
365 /* The time limit for sending or receiving information over an internal
366 /* communication channel.
367 /* .IP "\fBmax_idle (100s)\fR"
368 /* The maximum amount of time that an idle Postfix daemon process waits
369 /* for an incoming connection before terminating voluntarily.
370 /* .IP "\fBmax_use (100)\fR"
371 /* The maximal number of incoming connections that a Postfix daemon
372 /* process will service before terminating voluntarily.
373 /* .IP "\fBmyhostname (see 'postconf -d' output)\fR"
374 /* The internet hostname of this mail system.
375 /* .IP "\fBmyorigin ($myhostname)\fR"
376 /* The domain name that locally-posted mail appears to come
377 /* from, and that locally posted mail is delivered to.
378 /* .IP "\fBprocess_id (read-only)\fR"
379 /* The process ID of a Postfix command or daemon process.
380 /* .IP "\fBprocess_name (read-only)\fR"
381 /* The process name of a Postfix command or daemon process.
382 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
383 /* The location of the Postfix top-level queue directory.
384 /* .IP "\fBsoft_bounce (no)\fR"
385 /* Safety net to keep mail queued that would otherwise be returned to
386 /* the sender.
387 /* .IP "\fBsyslog_facility (mail)\fR"
388 /* The syslog facility of Postfix logging.
389 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
390 /* A prefix that is prepended to the process name in syslog
391 /* records, so that, for example, "smtpd" becomes "prefix/smtpd".
392 /* .PP
393 /* Available in Postfix version 2.1 and later:
394 /* .IP "\fBenable_original_recipient (yes)\fR"
395 /* Enable support for the original recipient address after an
396 /* address is rewritten to a different address (for example with
397 /* aliasing or with canonical mapping).
398 /* .PP
399 /* Available in Postfix 3.3 and later:
400 /* .IP "\fBservice_name (read-only)\fR"
401 /* The master.cf service name of a Postfix daemon process.
402 /* FILES
403 /* /etc/postfix/canonical*, canonical mapping table
404 /* /etc/postfix/virtual*, virtual mapping table
405 /* SEE ALSO
406 /* trivial-rewrite(8), address rewriting
407 /* qmgr(8), queue manager
408 /* header_checks(5), message header content inspection
409 /* body_checks(5), body parts content inspection
410 /* canonical(5), canonical address lookup table format
411 /* virtual(5), virtual alias lookup table format
412 /* postconf(5), configuration parameters
413 /* master(5), generic daemon options
414 /* master(8), process manager
415 /* syslogd(8), system logging
416 /* README FILES
417 /* .ad
418 /* .fi
419 /* Use "\fBpostconf readme_directory\fR" or
420 /* "\fBpostconf html_directory\fR" to locate this information.
421 /* .na
422 /* .nf
423 /* ADDRESS_REWRITING_README Postfix address manipulation
424 /* CONTENT_INSPECTION_README content inspection
425 /* LICENSE
426 /* .ad
427 /* .fi
428 /* The Secure Mailer license must be distributed with this software.
429 /* AUTHOR(S)
430 /* Wietse Venema
431 /* IBM T.J. Watson Research
432 /* P.O. Box 704
433 /* Yorktown Heights, NY 10598, USA
434 /*
435 /* Wietse Venema
436 /* Google, Inc.
437 /* 111 8th Avenue
438 /* New York, NY 10011, USA
439 /*--*/
440 
441 /* System library. */
442 
443 #include <sys_defs.h>
444 #include <signal.h>
445 #include <unistd.h>
446 #include <stdlib.h>
447 
448 /* Utility library. */
449 
450 #include <msg.h>
451 #include <vstring.h>
452 #include <dict.h>
453 
454 /* Global library. */
455 
456 #include <mail_conf.h>
457 #include <cleanup_user.h>
458 #include <mail_proto.h>
459 #include <mail_params.h>
460 #include <record.h>
461 #include <rec_type.h>
462 #include <mail_version.h>
463 
464 /* Single-threaded server skeleton. */
465 
466 #include <mail_server.h>
467 
468 /* Application-specific. */
469 
470 #include "cleanup.h"
471 
472 /* cleanup_service - process one request to inject a message into the queue */
473 
474 static void cleanup_service(VSTREAM *src, char *unused_service, char **argv)
475 {
476  VSTRING *buf = vstring_alloc(100);
477  CLEANUP_STATE *state;
478  int flags;
479  int type = 0;
480  int status;
481 
482  /*
483  * Sanity check. This service takes no command-line arguments.
484  */
485  if (argv[0])
486  msg_fatal("unexpected command-line argument: %s", argv[0]);
487 
488  /*
489  * Open a queue file and initialize state.
490  */
491  state = cleanup_open(src);
492 
493  /*
494  * Send the queue id to the client. Read client processing options. If we
495  * can't read the client processing options we can pretty much forget
496  * about the whole operation.
497  */
500  ATTR_TYPE_END);
501  if (attr_scan(src, ATTR_FLAG_STRICT,
503  ATTR_TYPE_END) != 1) {
504  state->errs |= CLEANUP_STAT_BAD;
505  flags = 0;
506  }
507  cleanup_control(state, flags);
508 
509  /*
510  * XXX Rely on the front-end programs to enforce record size limits.
511  *
512  * First, copy the envelope records to the queue file. Then, copy the
513  * message content (headers and body). Finally, attach any information
514  * extracted from message headers.
515  */
516  while (CLEANUP_OUT_OK(state)) {
517  if ((type = rec_get_raw(src, buf, 0, REC_FLAG_NONE)) < 0) {
518  state->errs |= CLEANUP_STAT_BAD;
519  break;
520  }
521  if (REC_GET_HIDDEN_TYPE(type)) {
522  msg_warn("%s: record type %d not allowed - discarding this message",
523  state->queue_id, type);
524  state->errs |= CLEANUP_STAT_BAD;
525  break;
526  }
527  CLEANUP_RECORD(state, type, vstring_str(buf), VSTRING_LEN(buf));
528  if (type == REC_TYPE_END)
529  break;
530  }
531 
532  /*
533  * Keep reading in case of problems, until the sender is ready to receive
534  * our status report.
535  */
536  if (CLEANUP_OUT_OK(state) == 0 && type > 0) {
537  while (type != REC_TYPE_END
538  && (type = rec_get_raw(src, buf, 0, REC_FLAG_NONE)) > 0) {
539  if (type == REC_TYPE_MILT_COUNT) {
540  int milter_count = atoi(vstring_str(buf));
541 
542  /* Avoid deadlock. */
543  if (milter_count >= 0)
544  cleanup_milter_receive(state, milter_count);
545  }
546  }
547  }
548 
549  /*
550  * Log something to make timeout errors easier to debug.
551  */
552  if (vstream_ftimeout(src))
553  msg_warn("%s: read timeout on %s",
554  state->queue_id, VSTREAM_PATH(src));
555 
556  /*
557  * Finish this message, and report the result status to the client.
558  */
559  status = cleanup_flush(state); /* in case state is modified */
563  (state->flags & CLEANUP_FLAG_SMTP_REPLY)
564  && state->smtp_reply ? state->smtp_reply :
565  state->reason ? state->reason : ""),
566  ATTR_TYPE_END);
567  cleanup_free(state);
568 
569  /*
570  * Cleanup.
571  */
572  vstring_free(buf);
573 }
574 
575 /* pre_accept - see if tables have changed */
576 
577 static void pre_accept(char *unused_name, char **unused_argv)
578 {
579  const char *table;
580 
581  if ((table = dict_changed_name()) != 0) {
582  msg_info("table %s has changed -- restarting", table);
583  exit(0);
584  }
585 }
586 
588 
589 /* main - the main program */
590 
591 int main(int argc, char **argv)
592 {
593 
594  /*
595  * Fingerprint executables and core dumps.
596  */
598 
599  /*
600  * Clean up an incomplete queue file in case of a fatal run-time error,
601  * or after receiving SIGTERM from the master at shutdown time.
602  */
603  signal(SIGTERM, cleanup_sig);
605 
606  /*
607  * Pass control to the single-threaded service skeleton.
608  */
609  single_server_main(argc, argv, cleanup_service,
616  CA_MAIL_SERVER_PRE_ACCEPT(pre_accept),
619  0);
620 }
#define ATTR_FLAG_NONE
Definition: attr.h:98
#define CA_MAIL_SERVER_UNLIMITED
Definition: mail_server.h:70
#define CA_MAIL_SERVER_BOOL_TABLE(v)
Definition: mail_server.h:58
#define CA_MAIL_SERVER_STR_TABLE(v)
Definition: mail_server.h:57
#define vstring_str(vp)
Definition: vstring.h:71
char * reason
Definition: cleanup.h:89
void cleanup_post_jail(char *, char **)
Definition: cleanup_init.c:434
#define CA_MAIL_SERVER_IN_FLOW_DELAY
Definition: mail_server.h:74
#define REC_FLAG_NONE
Definition: record.h:45
#define RECV_ATTR_INT(name, val)
Definition: attr.h:71
#define ATTR_TYPE_END
Definition: attr.h:39
#define VSTREAM_PATH(vp)
Definition: vstream.h:126
#define REC_GET_HIDDEN_TYPE(t)
Definition: record.h:53
CLEANUP_STATE * cleanup_open(VSTREAM *)
Definition: cleanup_api.c:132
#define REC_TYPE_END
Definition: rec_type.h:77
#define VSTRING_LEN(vp)
Definition: vstring.h:72
#define REC_TYPE_MILT_COUNT
Definition: rec_type.h:75
const CONFIG_TIME_TABLE cleanup_time_table[]
Definition: cleanup_init.c:194
#define CA_MAIL_SERVER_POST_INIT(v)
Definition: mail_server.h:65
#define CA_MAIL_SERVER_INT_TABLE(v)
Definition: mail_server.h:56
int rec_get_raw(VSTREAM *stream, VSTRING *buf, ssize_t maxsize, int flags)
Definition: record.c:236
const CONFIG_INT_TABLE cleanup_int_table[]
Definition: cleanup_init.c:176
#define attr_print
Definition: attr.h:109
const CONFIG_BOOL_TABLE cleanup_bool_table[]
Definition: cleanup_init.c:187
int cleanup_flush(CLEANUP_STATE *)
Definition: cleanup_api.c:208
void cleanup_all(void)
Definition: cleanup_init.c:288
const char * dict_changed_name(void)
Definition: dict.c:583
#define vstream_ftimeout(vp)
Definition: vstream.h:124
#define MAIL_ATTR_WHY
Definition: mail_proto.h:135
void msg_warn(const char *fmt,...)
Definition: msg.c:215
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
const CONFIG_STR_TABLE cleanup_str_table[]
Definition: cleanup_init.c:202
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
void cleanup_free(CLEANUP_STATE *)
Definition: cleanup_api.c:379
char * queue_id
Definition: cleanup.h:56
#define CA_MAIL_SERVER_TIME_TABLE(v)
Definition: mail_server.h:59
#define MAIL_ATTR_STATUS
Definition: mail_proto.h:126
MAIL_VERSION_STAMP_DECLARE
Definition: cleanup.c:587
#define SEND_ATTR_INT(name, val)
Definition: attr.h:63
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
void cleanup_milter_receive(CLEANUP_STATE *, int)
#define CLEANUP_STAT_BAD
Definition: cleanup_user.h:57
void cleanup_sig(int)
Definition: cleanup_init.c:295
#define CLEANUP_OUT_OK(s)
Definition: cleanup.h:239
int main(int argc, char **argv)
Definition: cleanup.c:591
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
#define CLEANUP_RECORD(s, t, b, l)
Definition: cleanup.h:226
NORETURN single_server_main(int, char **, SINGLE_SERVER_FN,...)
#define MAIL_ATTR_QUEUEID
Definition: mail_proto.h:130
void cleanup_control(CLEANUP_STATE *, int)
Definition: cleanup_api.c:184
#define CA_MAIL_SERVER_PRE_ACCEPT(v)
Definition: mail_server.h:68
char * smtp_reply
Definition: cleanup.h:90
#define attr_scan
Definition: attr.h:111
#define SEND_ATTR_STR(name, val)
Definition: attr.h:64
#define MAIL_ATTR_FLAGS
Definition: mail_proto.h:128
MSG_CLEANUP_FN msg_cleanup(MSG_CLEANUP_FN cleanup_fn)
Definition: msg.c:317
#define CLEANUP_FLAG_SMTP_REPLY
Definition: cleanup_user.h:25
#define CA_MAIL_SERVER_PRE_INIT(v)
Definition: mail_server.h:64
#define ATTR_FLAG_STRICT
Definition: attr.h:103
void cleanup_pre_jail(char *, char **)
Definition: cleanup_init.c:321
void msg_info(const char *fmt,...)
Definition: msg.c:199