5 /* bounce_one_service 3
7 /* send non-delivery report to sender, server side
9 /* #include "bounce_service.h"
11 /* int bounce_one_service(flags, queue_name, queue_id, encoding,
12 /* orig_sender, envid, ret,
13 /* rcpt_buf, dsn_buf, templates)
21 /* RCPT_BUF *rcpt_buf;
23 /* BOUNCE_TEMPLATES *templates;
25 /* This module implements the server side of the bounce_one()
26 /* (send bounce message for one recipient) request.
28 /* When a message bounces, a full copy is sent to the originator,
29 /* and an optional copy of the diagnostics with message headers is
30 /* sent to the postmaster. The result is non-zero when the operation
31 /* should be tried again.
33 /* When a bounce is sent, the sender address is the empty
34 /* address. When a bounce bounces, an optional double bounce
35 /* with the entire undeliverable mail is sent to the postmaster,
36 /* with as sender address the double bounce address.
38 /* Fatal error: error opening existing file.
41 /* bounce(3) basic bounce service client interface
45 /* The Secure Mailer license must be distributed with this software.
48 /* IBM T.J. Watson Research
50 /* Yorktown Heights, NY 10598, USA
61 #ifdef STRCASECMP_IN_STRINGS_H
65 /* Utility library. */
69 #include <name_mask.h>
73 #include <mail_params.h>
74 #include <post_mail.h>
75 #include <mail_addr.h>
76 #include <mail_error.h>
80 /* Application-specific. */
82 #include "bounce_service.h"
84 #define STR vstring_str
86 /* bounce_one_service - send a bounce for one recipient */
88 int bounce_one_service(int flags
, char *queue_name
, char *queue_id
,
89 char *encoding
, char *orig_sender
,
90 char *dsn_envid
, int dsn_ret
,
91 RCPT_BUF
*rcpt_buf
, DSN_BUF
*dsn_buf
,
94 BOUNCE_INFO
*bounce_info
;
95 int bounce_status
= 1;
96 int postmaster_status
= 1;
98 int notify_mask
= name_mask(VAR_NOTIFY_CLASSES
, mail_error_masks
,
100 VSTRING
*new_id
= vstring_alloc(10);
103 * Initialize. Open queue file, bounce log, etc.
105 bounce_info
= bounce_mail_one_init(queue_name
, queue_id
, encoding
,
106 dsn_envid
, rcpt_buf
, dsn_buf
,
109 #define NULL_SENDER MAIL_ADDR_EMPTY /* special address */
110 #define NULL_TRACE_FLAGS 0
113 * The choice of bounce sender address depends on the original sender
114 * address. For a single bounce (a non-delivery notification to the
115 * message originator), the sender address is the empty string. For a
116 * double bounce (typically a failed single bounce, or a postmaster
117 * notification that was produced by any of the mail processes) the
118 * sender address is defined by the var_double_bounce_sender
119 * configuration variable. When a double bounce cannot be delivered, the
120 * queue manager blackholes the resulting triple bounce message.
124 * Double bounce failed. Never send a triple bounce.
126 * However, this does not prevent double bounces from bouncing on other
127 * systems. In order to cope with this, either the queue manager must
128 * recognize the double-bounce original sender address and discard mail,
129 * or every delivery agent must recognize the double-bounce sender
130 * address and substitute something else so mail does not come back at
133 if (strcasecmp(orig_sender
, mail_addr_double_bounce()) == 0) {
134 msg_warn("%s: undeliverable postmaster notification discarded",
140 * Single bounce failed. Optionally send a double bounce to postmaster,
141 * subject to notify_classes restrictions.
143 #define ANY_BOUNCE (MAIL_ERROR_2BOUNCE | MAIL_ERROR_BOUNCE)
144 #define SEND_POSTMASTER_ANY_BOUNCE_NOTICE (notify_mask & ANY_BOUNCE)
146 else if (*orig_sender
== 0) {
147 if (!SEND_POSTMASTER_ANY_BOUNCE_NOTICE
) {
150 if ((bounce
= post_mail_fopen_nowait(mail_addr_double_bounce(),
152 INT_FILT_MASK_BOUNCE
,
157 * Double bounce to Postmaster. This is the last opportunity
158 * for this message to be delivered. Send the text with
159 * reason for the bounce, and the headers of the original
160 * message. Don't bother sending the boiler-plate text.
162 if (!bounce_header(bounce
, bounce_info
, var_2bounce_rcpt
,
164 && bounce_recipient_log(bounce
, bounce_info
) == 0
165 && bounce_header_dsn(bounce
, bounce_info
) == 0
166 && bounce_recipient_dsn(bounce
, bounce_info
) == 0)
167 bounce_original(bounce
, bounce_info
, DSN_RET_FULL
);
168 bounce_status
= post_mail_fclose(bounce
);
169 if (bounce_status
== 0)
170 msg_info("%s: postmaster non-delivery notification: %s",
171 queue_id
, STR(new_id
));
177 * Non-bounce failed. Send a single bounce, subject to DSN NOTIFY
181 RECIPIENT
*rcpt
= &bounce_info
->rcpt_buf
->rcpt
;
183 if (rcpt
->dsn_notify
!= 0 /* compat */
184 && (rcpt
->dsn_notify
& DSN_NOTIFY_FAILURE
) == 0) {
187 if ((bounce
= post_mail_fopen_nowait(NULL_SENDER
, orig_sender
,
188 INT_FILT_MASK_BOUNCE
,
193 * Send the bounce message header, some boilerplate text that
194 * pretends that we are a polite mail system, the text with
195 * reason for the bounce, and a copy of the original message.
197 if (bounce_header(bounce
, bounce_info
, orig_sender
,
198 NO_POSTMASTER_COPY
) == 0
199 && bounce_boilerplate(bounce
, bounce_info
) == 0
200 && bounce_recipient_log(bounce
, bounce_info
) == 0
201 && bounce_header_dsn(bounce
, bounce_info
) == 0
202 && bounce_recipient_dsn(bounce
, bounce_info
) == 0)
203 bounce_original(bounce
, bounce_info
, dsn_ret
?
204 dsn_ret
: DSN_RET_FULL
);
205 bounce_status
= post_mail_fclose(bounce
);
206 if (bounce_status
== 0)
207 msg_info("%s: sender non-delivery notification: %s",
208 queue_id
, STR(new_id
));
213 * Optionally send a postmaster notice, subject to notify_classes
216 * This postmaster notice is not critical, so if it fails don't
217 * retransmit the bounce that we just generated, just log a warning.
219 #define SEND_POSTMASTER_SINGLE_BOUNCE_NOTICE (notify_mask & MAIL_ERROR_BOUNCE)
221 if (bounce_status
== 0 && SEND_POSTMASTER_SINGLE_BOUNCE_NOTICE
222 && strcasecmp(orig_sender
, mail_addr_double_bounce()) != 0) {
225 * Send the text with reason for the bounce, and the headers of
226 * the original message. Don't bother sending the boiler-plate
227 * text. This postmaster notice is not critical, so if it fails
228 * don't retransmit the bounce that we just generated, just log a
231 if ((bounce
= post_mail_fopen_nowait(mail_addr_double_bounce(),
233 INT_FILT_MASK_BOUNCE
,
236 if (bounce_header(bounce
, bounce_info
, var_bounce_rcpt
,
237 POSTMASTER_COPY
) == 0
238 && bounce_recipient_log(bounce
, bounce_info
) == 0
239 && bounce_header_dsn(bounce
, bounce_info
) == 0
240 && bounce_recipient_dsn(bounce
, bounce_info
) == 0)
241 bounce_original(bounce
, bounce_info
, DSN_RET_HDRS
);
242 postmaster_status
= post_mail_fclose(bounce
);
243 if (postmaster_status
== 0)
244 msg_info("%s: postmaster non-delivery notification: %s",
245 queue_id
, STR(new_id
));
247 if (postmaster_status
)
248 msg_warn("%s: postmaster notice failed while bouncing to %s",
249 queue_id
, orig_sender
);
254 * Optionally, delete the recipient from the queue file.
256 if (bounce_status
== 0 && (flags
& BOUNCE_FLAG_DELRCPT
))
257 bounce_delrcpt_one(bounce_info
);
262 bounce_mail_free(bounce_info
);
263 vstring_free(new_id
);
265 return (bounce_status
);