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, sender,
18/*				dsn_envid, dsn_ret)
19/*	int	flags;
20/*	const char *queue;
21/*	const char *id;
22/*	const char *encoding;
23/*	const char *sender;
24/*	const char *dsn_envid;
25/*	int	dsn_ret;
26/*
27/*	int	defer_warn(flags, queue, id, sender, dsn_envid, dsn_ret)
28/*	int	flags;
29/*	const char *queue;
30/*	const char *id;
31/*	const char *sender;
32/*	const char *dsn_envid;
33/*	int	dsn_ret;
34/* DESCRIPTION
35/*	This module implements a client interface to the defer service,
36/*	which maintains a per-message logfile with status records for
37/*	each recipient whose delivery is deferred, and the dsn_text why.
38/*
39/*	defer_append() appends a record to the per-message defer log,
40/*	with the dsn_text for delayed delivery to the named rcpt,
41/*	updates the address verification service, or updates a message
42/*	delivery record on request by the sender. The flags argument
43/*	determines the action.
44/*	The result is a convenient non-zero value.
45/*	When the fast flush cache is enabled, the fast flush server is
46/*	notified of deferred mail.
47/*
48/*	defer_flush() bounces the specified message to the specified
49/*	sender, including the defer log that was built with defer_append().
50/*	defer_flush() requests that the deferred recipients are deleted
51/*	from the original queue file; the defer logfile is deleted after
52/*	successful completion.
53/*	The result is zero in case of success, non-zero otherwise.
54/*
55/*	defer_warn() sends a warning message that the mail in
56/*	question has been deferred.  The defer log is not deleted,
57/*	and no recipients are deleted from the original queue file.
58/*
59/*	Arguments:
60/* .IP flags
61/*	The bit-wise OR of zero or more of the following (specify
62/*	BOUNCE_FLAG_NONE to explicitly request not special processing):
63/* .RS
64/* .IP BOUNCE_FLAG_CLEAN
65/*	Delete the defer log in case of an error (as in: pretend
66/*	that we never even tried to defer this message).
67/* .IP BOUNCE_FLAG_DELRCPT
68/*	When specified with a flush request, request that
69/*	recipients be deleted from the queue file.
70/*
71/*	Note: the bounce daemon ignores this request when the
72/*	recipient queue file offset is <= 0.
73/* .IP DEL_REQ_FLAG_MTA_VRFY
74/*	The message is an MTA-requested address verification probe.
75/*	Update the address verification database instead of deferring
76/*	mail.
77/* .IP DEL_REQ_FLAG_USR_VRFY
78/*	The message is a user-requested address expansion probe.
79/*	Update the message delivery record instead of deferring
80/*	mail.
81/* .IP DEL_REQ_FLAG_RECORD
82/*	This is a normal message with logged delivery. Update the
83/*	message delivery record and defer mail delivery.
84/* .RE
85/* .IP queue
86/*	The message queue name of the original message file.
87/* .IP id
88/*	The queue id of the original message file.
89/* .IP stats
90/*	Time stamps from different message delivery stages
91/*	and session reuse count.
92/* .IP rcpt
93/*	Recipient information. See recipient_list(3).
94/* .IP relay
95/*	Host we could not talk to.
96/* .IP dsn
97/*	Delivery status. See dsn(3). The specified action is ignored.
98/* .IP encoding
99/*	The body content encoding: MAIL_ATTR_ENC_{7BIT,8BIT,NONE}.
100/* .IP sender
101/*	The sender envelope address.
102/* .IP dsn_envid
103/*	Optional DSN envelope ID.
104/* .IP dsn_ret
105/*	Optional DSN return full/headers option.
106/* .PP
107/*	For convenience, these functions always return a non-zero result.
108/* DIAGNOSTICS
109/*	Warnings: problems connecting to the defer service.
110/*	Fatal: out of memory.
111/* BUGS
112/*	Should be replaced by routines with an attribute-value based
113/*	interface instead of an interface that uses a rigid argument list.
114/* LICENSE
115/* .ad
116/* .fi
117/*	The Secure Mailer license must be distributed with this software.
118/* AUTHOR(S)
119/*	Wietse Venema
120/*	IBM T.J. Watson Research
121/*	P.O. Box 704
122/*	Yorktown Heights, NY 10598, USA
123/*--*/
124
125/* System library. */
126
127#include <sys_defs.h>
128#include <string.h>
129
130/* Utility library. */
131
132#include <msg.h>
133#include <vstring.h>
134
135/* Global library. */
136
137#include <mail_params.h>
138#include <mail_queue.h>
139#include <mail_proto.h>
140#include <flush_clnt.h>
141#include <verify.h>
142#include <dsn_util.h>
143#include <rcpt_print.h>
144#include <dsn_print.h>
145#include <log_adhoc.h>
146#include <trace.h>
147#include <defer.h>
148
149#define STR(x)	vstring_str(x)
150
151/* defer_append - defer message delivery */
152
153int     defer_append(int flags, const char *id, MSG_STATS *stats,
154		             RECIPIENT *rcpt, const char *relay,
155		             DSN *dsn)
156{
157    const char *rcpt_domain;
158    DSN     my_dsn = *dsn;
159    int     status;
160
161    /*
162     * Sanity check.
163     */
164    if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) {
165	msg_warn("defer_append: ignoring dsn code \"%s\"", my_dsn.status);
166	my_dsn.status = "4.0.0";
167    }
168
169    /*
170     * MTA-requested address verification information is stored in the verify
171     * service database.
172     */
173    if (flags & DEL_REQ_FLAG_MTA_VRFY) {
174	my_dsn.action = "undeliverable";
175	status = verify_append(id, stats, rcpt, relay, &my_dsn,
176			       DEL_RCPT_STAT_DEFER);
177	return (status);
178    }
179
180    /*
181     * User-requested address verification information is logged and mailed
182     * to the requesting user.
183     */
184    if (flags & DEL_REQ_FLAG_USR_VRFY) {
185	my_dsn.action = "undeliverable";
186	status = trace_append(flags, id, stats, rcpt, relay, &my_dsn);
187	return (status);
188    }
189
190    /*
191     * Normal mail delivery. May also send a delivery record to the user.
192     *
193     * XXX DSN We write all deferred recipients to the defer logfile regardless
194     * of DSN NOTIFY options, because those options don't apply to mailq(1)
195     * reports or to postmaster notifications.
196     */
197    else {
198
199	/*
200	 * Supply default action.
201	 */
202	my_dsn.action = "delayed";
203
204	if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
205			   ATTR_TYPE_INT, MAIL_ATTR_NREQ, BOUNCE_CMD_APPEND,
206				ATTR_TYPE_INT, MAIL_ATTR_FLAGS, flags,
207				ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
208				ATTR_TYPE_FUNC, rcpt_print, (void *) rcpt,
209				ATTR_TYPE_FUNC, dsn_print, (void *) &my_dsn,
210				ATTR_TYPE_END) != 0)
211	    msg_warn("%s: %s service failure", id, var_defer_service);
212	log_adhoc(id, stats, rcpt, relay, &my_dsn, "deferred");
213
214	/*
215	 * Traced delivery.
216	 */
217	if (flags & DEL_REQ_FLAG_RECORD)
218	    if (trace_append(flags, id, stats, rcpt, relay, &my_dsn) != 0)
219		msg_warn("%s: %s service failure", id, var_trace_service);
220
221	/*
222	 * Notify the fast flush service. XXX Should not this belong in the
223	 * bounce/defer daemon? Well, doing it here is more robust.
224	 */
225	if ((rcpt_domain = strrchr(rcpt->address, '@')) != 0
226	    && *++rcpt_domain != 0)
227	    switch (flush_add(rcpt_domain, id)) {
228	    case FLUSH_STAT_OK:
229	    case FLUSH_STAT_DENY:
230		break;
231	    default:
232		msg_warn("%s: %s service failure", id, var_flush_service);
233		break;
234	    }
235	return (-1);
236    }
237}
238
239/* defer_flush - flush the defer log and deliver to the sender */
240
241int     defer_flush(int flags, const char *queue, const char *id,
242		            const char *encoding, const char *sender,
243		            const char *dsn_envid, int dsn_ret)
244{
245    flags |= BOUNCE_FLAG_DELRCPT;
246
247    if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
248			    ATTR_TYPE_INT, MAIL_ATTR_NREQ, BOUNCE_CMD_FLUSH,
249			    ATTR_TYPE_INT, MAIL_ATTR_FLAGS, flags,
250			    ATTR_TYPE_STR, MAIL_ATTR_QUEUE, queue,
251			    ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
252			    ATTR_TYPE_STR, MAIL_ATTR_ENCODING, encoding,
253			    ATTR_TYPE_STR, MAIL_ATTR_SENDER, sender,
254			    ATTR_TYPE_STR, MAIL_ATTR_DSN_ENVID, dsn_envid,
255			    ATTR_TYPE_INT, MAIL_ATTR_DSN_RET, dsn_ret,
256			    ATTR_TYPE_END) == 0) {
257	return (0);
258    } else {
259	return (-1);
260    }
261}
262
263/* defer_warn - send a copy of the defer log to the sender as a warning bounce
264 * do not flush the log */
265
266int     defer_warn(int flags, const char *queue, const char *id,
267		         const char *sender, const char *envid, int dsn_ret)
268{
269    if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
270			    ATTR_TYPE_INT, MAIL_ATTR_NREQ, BOUNCE_CMD_WARN,
271			    ATTR_TYPE_INT, MAIL_ATTR_FLAGS, flags,
272			    ATTR_TYPE_STR, MAIL_ATTR_QUEUE, queue,
273			    ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
274			    ATTR_TYPE_STR, MAIL_ATTR_SENDER, sender,
275			    ATTR_TYPE_STR, MAIL_ATTR_DSN_ENVID, envid,
276			    ATTR_TYPE_INT, MAIL_ATTR_DSN_RET, dsn_ret,
277			    ATTR_TYPE_END) == 0) {
278	return (0);
279    } else {
280	return (-1);
281    }
282}
283