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