5 /* bounce_notify_service 3
7 /* send non-delivery report to sender, server side
9 /* #include "bounce_service.h"
11 /* int bounce_notify_service(flags, queue_name, queue_id, encoding,
12 /* sender, dsn_envid, dsn_ret, templates)
20 /* BOUNCE_TEMPLATES *templates;
22 /* This module implements the server side of the bounce_flush()
23 /* (send bounce message) request.
25 /* When a message bounces, a full copy is sent to the originator,
26 /* and an optional copy of the diagnostics with message headers is
27 /* sent to the postmaster. The result is non-zero when the operation
28 /* should be tried again. Otherwise, the logfile is removed.
30 /* When a bounce is sent, the sender address is the empty
31 /* address. When a bounce bounces, an optional double bounce
32 /* with the entire undeliverable mail is sent to the postmaster,
33 /* with as sender address the double bounce address.
35 /* Fatal error: error opening existing file.
38 /* bounce(3) basic bounce service client interface
42 /* The Secure Mailer license must be distributed with this software.
45 /* IBM T.J. Watson Research
47 /* Yorktown Heights, NY 10598, USA
58 #ifdef STRCASECMP_IN_STRINGS_H
62 /* Utility library. */
66 #include <name_mask.h>
70 #include <mail_params.h>
71 #include <mail_queue.h>
72 #include <post_mail.h>
73 #include <mail_addr.h>
74 #include <mail_error.h>
78 /* Application-specific. */
80 #include "bounce_service.h"
82 #define STR vstring_str
84 /* bounce_notify_service - send a bounce */
86 int bounce_notify_service(int flags
, char *service
, char *queue_name
,
87 char *queue_id
, char *encoding
,
88 char *recipient
, char *dsn_envid
,
89 int dsn_ret
, BOUNCE_TEMPLATES
*ts
)
91 BOUNCE_INFO
*bounce_info
;
92 int bounce_status
= 1;
93 int postmaster_status
= 1;
95 int notify_mask
= name_mask(VAR_NOTIFY_CLASSES
, mail_error_masks
,
97 VSTRING
*new_id
= vstring_alloc(10);
102 * Initialize. Open queue file, bounce log, etc.
104 * XXX DSN The bounce service produces RFC 3464-style "failed mail" reports
105 * from information in two following types of logfile:
107 * 1 - bounce: this file is used for RFC 3464-style reports of permanent
108 * delivery errors by the bounce(8) service. This reports to the sender
109 * all recipients that have no DSN NOTIFY information (compatibility) and
110 * all recipients that have DSN NOTIFY=FAILURE; this reports to
111 * postmaster all recipients, if postmaster notification is enabled.
113 * 2 - defer: this file is used for three types of report:
115 * 2a) RFC 3464-style "mail is too old" reports by the bounce(8) service.
116 * This reports to the sender all recipients that have no DSN NOTIFY
117 * information (compatibility) and all recipients that have DSN
118 * NOTIFY=FAILURE; this reports to postmaster all recipients, if
119 * postmaster notification is enabled.
121 * Other reports that other servers produce from the defer logfile:
123 * 2b) On-demand reports of all delayed deliveries by the showq(8) service
124 * and mailq(1) command. This reports all recipients that have a
125 * transient delivery error.
127 * 2c) RFC 3464-style "delayed mail" notifications by the defer(8) service.
128 * This reports to the sender all recipients that have no DSN NOTIFY
129 * information (compatibility) and all recipients that have DSN
130 * NOTIFY=DELAY; this reports to postmaster all recipients, if postmaster
131 * notification is enabled.
133 bounce_info
= bounce_mail_init(service
, queue_name
, queue_id
,
134 encoding
, dsn_envid
, ts
->failure
);
136 #define NULL_SENDER MAIL_ADDR_EMPTY /* special address */
137 #define NULL_TRACE_FLAGS 0
140 * The choice of sender address depends on the recipient address. For a
141 * single bounce (a non-delivery notification to the message originator),
142 * the sender address is the empty string. For a double bounce (typically
143 * a failed single bounce, or a postmaster notification that was produced
144 * by any of the mail processes) the sender address is defined by the
145 * var_double_bounce_sender configuration variable. When a double bounce
146 * cannot be delivered, the queue manager blackholes the resulting triple
151 * Double bounce failed. Never send a triple bounce.
153 * However, this does not prevent double bounces from bouncing on other
154 * systems. In order to cope with this, either the queue manager must
155 * recognize the double-bounce recipient address and discard mail, or
156 * every delivery agent must recognize the double-bounce sender address
157 * and substitute something else so mail does not come back at us.
159 if (strcasecmp(recipient
, mail_addr_double_bounce()) == 0) {
160 msg_warn("%s: undeliverable postmaster notification discarded",
166 * Single bounce failed. Optionally send a double bounce to postmaster,
167 * subject to notify_classes restrictions.
169 #define ANY_BOUNCE (MAIL_ERROR_2BOUNCE | MAIL_ERROR_BOUNCE)
170 #define SEND_POSTMASTER_ANY_BOUNCE_NOTICE (notify_mask & ANY_BOUNCE)
172 else if (*recipient
== 0) {
173 if (!SEND_POSTMASTER_ANY_BOUNCE_NOTICE
) {
176 postmaster
= var_2bounce_rcpt
;
177 if ((bounce
= post_mail_fopen_nowait(mail_addr_double_bounce(),
179 INT_FILT_MASK_BOUNCE
,
184 * Double bounce to Postmaster. This is the last opportunity
185 * for this message to be delivered. Send the text with
186 * reason for the bounce, and the headers of the original
187 * message. Don't bother sending the boiler-plate text.
190 if (bounce_header(bounce
, bounce_info
, postmaster
,
191 POSTMASTER_COPY
) == 0
192 && (count
= bounce_diagnostic_log(bounce
, bounce_info
,
193 DSN_NOTIFY_OVERRIDE
)) > 0
194 && bounce_header_dsn(bounce
, bounce_info
) == 0
195 && bounce_diagnostic_dsn(bounce
, bounce_info
,
196 DSN_NOTIFY_OVERRIDE
) > 0) {
197 bounce_original(bounce
, bounce_info
, DSN_RET_FULL
);
198 bounce_status
= post_mail_fclose(bounce
);
199 if (bounce_status
== 0)
200 msg_info("%s: postmaster non-delivery notification: %s",
201 queue_id
, STR(new_id
));
203 /* No applicable recipients found - cancel this notice. */
204 (void) vstream_fclose(bounce
);
213 * Non-bounce failed. Send a single bounce to the sender, subject to DSN
214 * NOTIFY restrictions.
217 if ((bounce
= post_mail_fopen_nowait(NULL_SENDER
, recipient
,
218 INT_FILT_MASK_BOUNCE
,
223 * Send the bounce message header, some boilerplate text that
224 * pretends that we are a polite mail system, the text with
225 * reason for the bounce, and a copy of the original message.
228 if (bounce_header(bounce
, bounce_info
, recipient
,
229 NO_POSTMASTER_COPY
) == 0
230 && bounce_boilerplate(bounce
, bounce_info
) == 0
231 && (count
= bounce_diagnostic_log(bounce
, bounce_info
,
232 DSN_NOTIFY_FAILURE
)) > 0
233 && bounce_header_dsn(bounce
, bounce_info
) == 0
234 && bounce_diagnostic_dsn(bounce
, bounce_info
,
235 DSN_NOTIFY_FAILURE
) > 0) {
236 bounce_original(bounce
, bounce_info
, dsn_ret
?
237 dsn_ret
: DSN_RET_FULL
);
238 bounce_status
= post_mail_fclose(bounce
);
239 if (bounce_status
== 0)
240 msg_info("%s: sender non-delivery notification: %s",
241 queue_id
, STR(new_id
));
243 /* No applicable recipients found - cancel this notice. */
244 (void) vstream_fclose(bounce
);
251 * Optionally, send a postmaster notice, subject to notify_classes
254 * This postmaster notice is not critical, so if it fails don't
255 * retransmit the bounce that we just generated, just log a warning.
257 #define SEND_POSTMASTER_SINGLE_BOUNCE_NOTICE (notify_mask & MAIL_ERROR_BOUNCE)
259 if (bounce_status
== 0 && SEND_POSTMASTER_SINGLE_BOUNCE_NOTICE
260 && strcasecmp(recipient
, mail_addr_double_bounce()) != 0) {
263 * Send the text with reason for the bounce, and the headers of
264 * the original message. Don't bother sending the boiler-plate
265 * text. This postmaster notice is not critical, so if it fails
266 * don't retransmit the bounce that we just generated, just log a
269 postmaster
= var_bounce_rcpt
;
270 if ((bounce
= post_mail_fopen_nowait(mail_addr_double_bounce(),
272 INT_FILT_MASK_BOUNCE
,
276 if (bounce_header(bounce
, bounce_info
, postmaster
,
277 POSTMASTER_COPY
) == 0
278 && (count
= bounce_diagnostic_log(bounce
, bounce_info
,
279 DSN_NOTIFY_OVERRIDE
)) > 0
280 && bounce_header_dsn(bounce
, bounce_info
) == 0
281 && bounce_diagnostic_dsn(bounce
, bounce_info
,
282 DSN_NOTIFY_OVERRIDE
) > 0) {
283 bounce_original(bounce
, bounce_info
, DSN_RET_HDRS
);
284 postmaster_status
= post_mail_fclose(bounce
);
285 if (postmaster_status
== 0)
286 msg_info("%s: postmaster non-delivery notification: %s",
287 queue_id
, STR(new_id
));
289 /* No applicable recipients found - cancel this notice. */
290 (void) vstream_fclose(bounce
);
292 postmaster_status
= 0;
295 if (postmaster_status
)
296 msg_warn("%s: postmaster notice failed while bouncing to %s",
297 queue_id
, recipient
);
302 * Optionally, delete the recipients from the queue file.
304 if (bounce_status
== 0 && (flags
& BOUNCE_FLAG_DELRCPT
))
305 bounce_delrcpt(bounce_info
);
308 * Examine the completion status. Delete the bounce log file only when
309 * the bounce was posted successfully, and only if we are bouncing for
310 * real, not just warning.
312 if (bounce_status
== 0 && mail_queue_remove(service
, queue_id
)
314 msg_fatal("remove %s %s: %m", service
, queue_id
);
319 bounce_mail_free(bounce_info
);
320 vstring_free(new_id
);
322 return (bounce_status
);