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