Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / bounce / bounce_one_service.c
blobc65a74805bc107ce67905e158053a338ca0bc0ad
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* bounce_one_service 3
6 /* SUMMARY
7 /* send non-delivery report to sender, server side
8 /* SYNOPSIS
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)
14 /* int flags;
15 /* char *queue_name;
16 /* char *queue_id;
17 /* char *encoding;
18 /* char *orig_sender;
19 /* char *envid;
20 /* int ret;
21 /* RCPT_BUF *rcpt_buf;
22 /* DSN_BUF *dsn_buf;
23 /* BOUNCE_TEMPLATES *templates;
24 /* DESCRIPTION
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.
37 /* DIAGNOSTICS
38 /* Fatal error: error opening existing file.
39 /* BUGS
40 /* SEE ALSO
41 /* bounce(3) basic bounce service client interface
42 /* LICENSE
43 /* .ad
44 /* .fi
45 /* The Secure Mailer license must be distributed with this software.
46 /* AUTHOR(S)
47 /* Wietse Venema
48 /* IBM T.J. Watson Research
49 /* P.O. Box 704
50 /* Yorktown Heights, NY 10598, USA
51 /*--*/
53 /* System library. */
55 #include <sys_defs.h>
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <string.h>
59 #include <ctype.h>
61 #ifdef STRCASECMP_IN_STRINGS_H
62 #include <strings.h>
63 #endif
65 /* Utility library. */
67 #include <msg.h>
68 #include <vstream.h>
69 #include <name_mask.h>
71 /* Global library. */
73 #include <mail_params.h>
74 #include <post_mail.h>
75 #include <mail_addr.h>
76 #include <mail_error.h>
77 #include <bounce.h>
78 #include <dsn_mask.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,
92 BOUNCE_TEMPLATES *ts)
94 BOUNCE_INFO *bounce_info;
95 int bounce_status = 1;
96 int postmaster_status = 1;
97 VSTREAM *bounce;
98 int notify_mask = name_mask(VAR_NOTIFY_CLASSES, mail_error_masks,
99 var_notify_classes);
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,
107 ts->failure);
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
131 * us.
133 if (strcasecmp(orig_sender, mail_addr_double_bounce()) == 0) {
134 msg_warn("%s: undeliverable postmaster notification discarded",
135 queue_id);
136 bounce_status = 0;
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) {
148 bounce_status = 0;
149 } else {
150 if ((bounce = post_mail_fopen_nowait(mail_addr_double_bounce(),
151 var_2bounce_rcpt,
152 INT_FILT_MASK_BOUNCE,
153 NULL_TRACE_FLAGS,
154 new_id)) != 0) {
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,
163 POSTMASTER_COPY)
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
178 * restrictions.
180 else {
181 RECIPIENT *rcpt = &bounce_info->rcpt_buf->rcpt;
183 if (rcpt->dsn_notify != 0 /* compat */
184 && (rcpt->dsn_notify & DSN_NOTIFY_FAILURE) == 0) {
185 bounce_status = 0;
186 } else {
187 if ((bounce = post_mail_fopen_nowait(NULL_SENDER, orig_sender,
188 INT_FILT_MASK_BOUNCE,
189 NULL_TRACE_FLAGS,
190 new_id)) != 0) {
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
214 * restrictions.
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
229 * warning.
231 if ((bounce = post_mail_fopen_nowait(mail_addr_double_bounce(),
232 var_bounce_rcpt,
233 INT_FILT_MASK_BOUNCE,
234 NULL_TRACE_FLAGS,
235 new_id)) != 0) {
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);
260 * Cleanup.
262 bounce_mail_free(bounce_info);
263 vstring_free(new_id);
265 return (bounce_status);