1 /*++
2 /* NAME
3 /* defer 3
4 /* SUMMARY
5 /* defer service client interface
6 /* SYNOPSIS
7 /* #include <defer.h>
8 /*
9 /* int defer_append(flags, id, stats, rcpt, relay, dsn)
10 /* int flags;
11 /* const char *id;
12 /* MSG_STATS *stats;
13 /* RECIPIENT *rcpt;
14 /* const char *relay;
15 /* DSN *dsn;
16 /*
17 /* int defer_flush(flags, queue, id, encoding, smtputf8, sender,
18 /* dsn_envid, dsn_ret)
19 /* int flags;
20 /* const char *queue;
21 /* const char *id;
22 /* const char *encoding;
23 /* int smtputf8;
24 /* const char *sender;
25 /* const char *dsn_envid;
26 /* int dsn_ret;
27 /*
28 /* int defer_warn(flags, queue, id, encoding, smtputf8, sender,
29 dsn_envid, dsn_ret)
30 /* int flags;
31 /* const char *queue;
32 /* const char *id;
33 /* const char *encoding;
34 /* int smtputf8;
35 /* const char *sender;
36 /* const char *dsn_envid;
37 /* int dsn_ret;
38 /*
39 /* int defer_one(flags, queue, id, encoding, smtputf8, sender,
40 /* dsn_envid, ret, stats, recipient, relay, dsn)
41 /* int flags;
42 /* const char *queue;
43 /* const char *id;
44 /* const char *encoding;
45 /* int smtputf8;
46 /* const char *sender;
47 /* const char *dsn_envid;
48 /* int dsn_ret;
49 /* MSG_STATS *stats;
50 /* RECIPIENT *rcpt;
51 /* const char *relay;
52 /* DSN *dsn;
53 /* INTERNAL API
54 /* int defer_append_intern(flags, id, stats, rcpt, relay, dsn)
55 /* int flags;
56 /* const char *id;
57 /* MSG_STATS *stats;
58 /* RECIPIENT *rcpt;
59 /* const char *relay;
60 /* DESCRIPTION
61 /* This module implements a client interface to the defer service,
62 /* which maintains a per-message logfile with status records for
63 /* each recipient whose delivery is deferred, and the dsn_text why.
64 /*
65 /* defer_append() appends a record to the per-message defer log,
66 /* with the dsn_text for delayed delivery to the named rcpt,
67 /* updates the address verification service, or updates a message
68 /* delivery record on request by the sender. The flags argument
69 /* determines the action.
70 /* The result is a convenient non-zero value.
71 /* When the fast flush cache is enabled, the fast flush server is
72 /* notified of deferred mail.
73 /*
74 /* defer_flush() bounces the specified message to the specified
75 /* sender, including the defer log that was built with defer_append().
76 /* defer_flush() requests that the deferred recipients are deleted
77 /* from the original queue file; the defer logfile is deleted after
78 /* successful completion.
79 /* The result is zero in case of success, non-zero otherwise.
80 /*
81 /* defer_warn() sends a warning message that the mail in
82 /* question has been deferred. The defer log is not deleted,
83 /* and no recipients are deleted from the original queue file.
84 /*
85 /* defer_one() implements dsn_filter(3) compatibility for the
86 /* bounce_one() routine.
87 /*
88 /* defer_append_intern() is for use after the DSN filter.
89 /*
90 /* Arguments:
91 /* .IP flags
92 /* The bit-wise OR of zero or more of the following (specify
93 /* BOUNCE_FLAG_NONE to explicitly request not special processing):
94 /* .RS
95 /* .IP BOUNCE_FLAG_CLEAN
96 /* Delete the defer log in case of an error (as in: pretend
97 /* that we never even tried to defer this message).
98 /* .IP BOUNCE_FLAG_DELRCPT
99 /* When specified with a flush request, request that
100 /* recipients be deleted from the queue file.
101 /*
102 /* Note: the bounce daemon ignores this request when the
103 /* recipient queue file offset is <= 0.
104 /* .IP DEL_REQ_FLAG_MTA_VRFY
105 /* The message is an MTA-requested address verification probe.
106 /* Update the address verification database instead of deferring
107 /* mail.
108 /* .IP DEL_REQ_FLAG_USR_VRFY
109 /* The message is a user-requested address expansion probe.
110 /* Update the message delivery record instead of deferring
111 /* mail.
112 /* .IP DEL_REQ_FLAG_RECORD
113 /* This is a normal message with logged delivery. Update the
114 /* message delivery record and defer mail delivery.
115 /* .RE
116 /* .IP queue
117 /* The message queue name of the original message file.
118 /* .IP id
119 /* The queue id of the original message file.
120 /* .IP stats
121 /* Time stamps from different message delivery stages
122 /* and session reuse count.
123 /* .IP rcpt
124 /* Recipient information. See recipient_list(3).
125 /* .IP relay
126 /* Host we could not talk to.
127 /* .IP dsn
128 /* Delivery status. See dsn(3). The specified action is ignored.
129 /* .IP encoding
130 /* The body content encoding: MAIL_ATTR_ENC_{7BIT,8BIT,NONE}.
131 /* .IP smtputf8
132 /* The level of SMTPUTF8 support (to be defined).
133 /* .IP sender
134 /* The sender envelope address.
135 /* .IP dsn_envid
136 /* Optional DSN envelope ID.
137 /* .IP dsn_ret
138 /* Optional DSN return full/headers option.
139 /* .PP
140 /* For convenience, these functions always return a non-zero result.
141 /* DIAGNOSTICS
142 /* Warnings: problems connecting to the defer service.
143 /* Fatal: out of memory.
144 /* BUGS
145 /* Should be replaced by routines with an attribute-value based
146 /* interface instead of an interface that uses a rigid argument list.
147 /* LICENSE
148 /* .ad
149 /* .fi
150 /* The Secure Mailer license must be distributed with this software.
151 /* AUTHOR(S)
152 /* Wietse Venema
153 /* IBM T.J. Watson Research
154 /* P.O. Box 704
155 /* Yorktown Heights, NY 10598, USA
156 /*
157 /* Wietse Venema
158 /* Google, Inc.
159 /* 111 8th Avenue
160 /* New York, NY 10011, USA
161 /*--*/
162
163 /* System library. */
164
165 #include <sys_defs.h>
166 #include <string.h>
167
168 /* Utility library. */
169
170 #include <msg.h>
171 #include <vstring.h>
172
173 /* Global library. */
174
175 #define DSN_INTERN
176 #include <mail_params.h>
177 #include <mail_queue.h>
178 #include <mail_proto.h>
179 #include <flush_clnt.h>
180 #include <verify.h>
181 #include <dsn_util.h>
182 #include <rcpt_print.h>
183 #include <dsn_print.h>
184 #include <log_adhoc.h>
185 #include <trace.h>
186 #include <defer.h>
187
188 #define STR(x) vstring_str(x)
189
190 /* defer_append - defer message delivery */
191
defer_append(int flags,const char * id,MSG_STATS * stats,RECIPIENT * rcpt,const char * relay,DSN * dsn)192 int defer_append(int flags, const char *id, MSG_STATS *stats,
193 RECIPIENT *rcpt, const char *relay,
194 DSN *dsn)
195 {
196 DSN my_dsn = *dsn;
197 DSN *dsn_res;
198
199 /*
200 * Sanity check.
201 */
202 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) {
203 msg_warn("defer_append: ignoring dsn code \"%s\"", my_dsn.status);
204 my_dsn.status = "4.0.0";
205 }
206
207 /*
208 * DSN filter (Postfix 3.0).
209 */
210 if (delivery_status_filter != 0
211 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) {
212 if (dsn_res->status[0] == '5')
213 return (bounce_append_intern(flags, id, stats, rcpt, relay, dsn_res));
214 my_dsn = *dsn_res;
215 }
216 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn));
217 }
218
219 /* defer_append_intern - defer message delivery */
220
defer_append_intern(int flags,const char * id,MSG_STATS * stats,RECIPIENT * rcpt,const char * relay,DSN * dsn)221 int defer_append_intern(int flags, const char *id, MSG_STATS *stats,
222 RECIPIENT *rcpt, const char *relay,
223 DSN *dsn)
224 {
225 const char *rcpt_domain;
226 DSN my_dsn = *dsn;
227 int status;
228
229 /*
230 * MTA-requested address verification information is stored in the verify
231 * service database.
232 */
233 if (flags & DEL_REQ_FLAG_MTA_VRFY) {
234 my_dsn.action = "undeliverable";
235 status = verify_append(id, stats, rcpt, relay, &my_dsn,
236 DEL_RCPT_STAT_DEFER);
237 return (status);
238 }
239
240 /*
241 * User-requested address verification information is logged and mailed
242 * to the requesting user.
243 */
244 if (flags & DEL_REQ_FLAG_USR_VRFY) {
245 my_dsn.action = "undeliverable";
246 status = trace_append(flags, id, stats, rcpt, relay, &my_dsn);
247 return (status);
248 }
249
250 /*
251 * Normal mail delivery. May also send a delivery record to the user.
252 *
253 * XXX DSN We write all deferred recipients to the defer logfile regardless
254 * of DSN NOTIFY options, because those options don't apply to mailq(1)
255 * reports or to postmaster notifications.
256 */
257 else {
258
259 /*
260 * Supply default action.
261 */
262 my_dsn.action = "delayed";
263
264 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
265 MAIL_ATTR_PROTO_BOUNCE,
266 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_APPEND),
267 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags),
268 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id),
269 SEND_ATTR_FUNC(rcpt_print, (const void *) rcpt),
270 SEND_ATTR_FUNC(dsn_print, (const void *) &my_dsn),
271 ATTR_TYPE_END) != 0)
272 msg_warn("%s: %s service failure", id, var_defer_service);
273 log_adhoc(id, stats, rcpt, relay, &my_dsn, "deferred");
274
275 /*
276 * Traced delivery.
277 */
278 if (flags & DEL_REQ_FLAG_RECORD)
279 if (trace_append(flags, id, stats, rcpt, relay, &my_dsn) != 0)
280 msg_warn("%s: %s service failure", id, var_trace_service);
281
282 /*
283 * Notify the fast flush service. XXX Should not this belong in the
284 * bounce/defer daemon? Well, doing it here is more robust.
285 */
286 if ((rcpt_domain = strrchr(rcpt->address, '@')) != 0
287 && *++rcpt_domain != 0)
288 switch (flush_add(rcpt_domain, id)) {
289 case FLUSH_STAT_OK:
290 case FLUSH_STAT_DENY:
291 break;
292 default:
293 msg_warn("%s: %s service failure", id, var_flush_service);
294 break;
295 }
296 return (-1);
297 }
298 }
299
300 /* defer_flush - flush the defer log and deliver to the sender */
301
defer_flush(int flags,const char * queue,const char * id,const char * encoding,int smtputf8,const char * sender,const char * dsn_envid,int dsn_ret)302 int defer_flush(int flags, const char *queue, const char *id,
303 const char *encoding, int smtputf8,
304 const char *sender, const char *dsn_envid,
305 int dsn_ret)
306 {
307 flags |= BOUNCE_FLAG_DELRCPT;
308
309 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
310 MAIL_ATTR_PROTO_BOUNCE,
311 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_FLUSH),
312 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags),
313 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue),
314 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id),
315 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding),
316 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8),
317 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender),
318 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, dsn_envid),
319 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret),
320 ATTR_TYPE_END) == 0) {
321 return (0);
322 } else {
323 return (-1);
324 }
325 }
326
327 /* defer_warn - send a copy of the defer log to the sender as a warning bounce
328 * do not flush the log */
329
defer_warn(int flags,const char * queue,const char * id,const char * encoding,int smtputf8,const char * sender,const char * envid,int dsn_ret)330 int defer_warn(int flags, const char *queue, const char *id,
331 const char *encoding, int smtputf8,
332 const char *sender, const char *envid, int dsn_ret)
333 {
334 if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
335 MAIL_ATTR_PROTO_BOUNCE,
336 SEND_ATTR_INT(MAIL_ATTR_NREQ, BOUNCE_CMD_WARN),
337 SEND_ATTR_INT(MAIL_ATTR_FLAGS, flags),
338 SEND_ATTR_STR(MAIL_ATTR_QUEUE, queue),
339 SEND_ATTR_STR(MAIL_ATTR_QUEUEID, id),
340 SEND_ATTR_STR(MAIL_ATTR_ENCODING, encoding),
341 SEND_ATTR_INT(MAIL_ATTR_SMTPUTF8, smtputf8),
342 SEND_ATTR_STR(MAIL_ATTR_SENDER, sender),
343 SEND_ATTR_STR(MAIL_ATTR_DSN_ENVID, envid),
344 SEND_ATTR_INT(MAIL_ATTR_DSN_RET, dsn_ret),
345 ATTR_TYPE_END) == 0) {
346 return (0);
347 } else {
348 return (-1);
349 }
350 }
351
352 /* defer_one - defer mail for one recipient */
353
defer_one(int flags,const char * queue,const char * id,const char * encoding,int smtputf8,const char * sender,const char * dsn_envid,int dsn_ret,MSG_STATS * stats,RECIPIENT * rcpt,const char * relay,DSN * dsn)354 int defer_one(int flags, const char *queue, const char *id,
355 const char *encoding, int smtputf8,
356 const char *sender, const char *dsn_envid,
357 int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt,
358 const char *relay, DSN *dsn)
359 {
360 DSN my_dsn = *dsn;
361 DSN *dsn_res;
362
363 /*
364 * Sanity check.
365 */
366 if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) {
367 msg_warn("defer_one: ignoring dsn code \"%s\"", my_dsn.status);
368 my_dsn.status = "4.0.0";
369 }
370
371 /*
372 * DSN filter (Postfix 3.0).
373 */
374 if (delivery_status_filter != 0
375 && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) {
376 if (dsn_res->status[0] == '5')
377 return (bounce_one_intern(flags, queue, id, encoding, smtputf8,
378 sender, dsn_envid, dsn_ret, stats,
379 rcpt, relay, dsn_res));
380 my_dsn = *dsn_res;
381 }
382 return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn));
383 }
384