xref: /netbsd-src/external/ibm-public/postfix/dist/src/bounce/bounce_trace_service.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: bounce_trace_service.c,v 1.1.1.1 2009/06/23 10:08:42 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	bounce_trace_service 3
6 /* SUMMARY
7 /*	send status report to sender, server side
8 /* SYNOPSIS
9 /*	#include "bounce_service.h"
10 /*
11 /*	int     bounce_trace_service(flags, queue_name, queue_id, encoding,
12 /*					sender, char *envid, int ret, templates)
13 /*	int	flags;
14 /*	char	*queue_name;
15 /*	char	*queue_id;
16 /*	char	*encoding;
17 /*	char	*sender;
18 /*	char	*envid;
19 /*	int	ret;
20 /*	BOUNCE_TEMPLATES *templates;
21 /* DESCRIPTION
22 /*	This module implements the server side of the trace_flush()
23 /*	(send delivery notice) request. The logfile
24 /*	is removed after the notice is posted.
25 /*
26 /*	A status report includes a prelude with human-readable text,
27 /*	a DSN-style report, and the email message that was subject of
28 /*	the status report.
29 /*
30 /*	When a status report is sent, the sender address is the empty
31 /*	address.
32 /* DIAGNOSTICS
33 /*	Fatal error: error opening existing file.
34 /* BUGS
35 /* SEE ALSO
36 /*	bounce(3) basic bounce service client interface
37 /* LICENSE
38 /* .ad
39 /* .fi
40 /*	The Secure Mailer license must be distributed with this software.
41 /* AUTHOR(S)
42 /*	Wietse Venema
43 /*	IBM T.J. Watson Research
44 /*	P.O. Box 704
45 /*	Yorktown Heights, NY 10598, USA
46 /*--*/
47 
48 /* System library. */
49 
50 #include <sys_defs.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <ctype.h>
55 
56 /* Utility library. */
57 
58 #include <msg.h>
59 #include <vstream.h>
60 
61 /* Global library. */
62 
63 #include <mail_params.h>
64 #include <mail_queue.h>
65 #include <post_mail.h>
66 #include <mail_addr.h>
67 #include <mail_error.h>
68 #include <dsn_mask.h>
69 #include <deliver_request.h>		/* USR_VRFY and RECORD flags */
70 
71 /* Application-specific. */
72 
73 #include "bounce_service.h"
74 
75 #define STR vstring_str
76 
77 /* bounce_trace_service - send a delivery status notice */
78 
79 int     bounce_trace_service(int flags, char *service, char *queue_name,
80 			             char *queue_id, char *encoding,
81 			             char *recipient, char *dsn_envid,
82 			             int unused_dsn_ret,
83 			             BOUNCE_TEMPLATES *ts)
84 {
85     BOUNCE_INFO *bounce_info;
86     int     bounce_status = 1;
87     VSTREAM *bounce;
88     VSTRING *new_id = vstring_alloc(10);
89     int     count;
90 
91     /*
92      * Initialize. Open queue file, bounce log, etc.
93      *
94      * XXX DSN The trace service produces information from the trace logfile
95      * which is used for three types of reports:
96      *
97      * a) "what-if" reports that show what would happen without actually
98      * delivering mail (sendmail -bv).
99      *
100      * b) A report of actual deliveries (sendmail -v).
101      *
102      * c) DSN NOTIFY=SUCCESS reports of successful delivery ("delivered",
103      * "expanded" or "relayed").
104      */
105 #define NON_DSN_FLAGS (DEL_REQ_FLAG_USR_VRFY | DEL_REQ_FLAG_RECORD)
106 
107     bounce_info = bounce_mail_init(service, queue_name, queue_id,
108 				   encoding, dsn_envid,
109 				   flags & NON_DSN_FLAGS ?
110 				   ts->verify : ts->success);
111 
112     /*
113      * XXX With multi-recipient mail some queue file recipients may have
114      * NOTIFY=SUCCESS and others not. Depending on what subset of recipients
115      * are delivered, a trace file may or may not be created. Even when the
116      * last partial delivery attempt had no NOTIFY=SUCCESS recipients, a
117      * trace file may still exist from a previous partial delivery attempt.
118      * So as long as any recipient in the original queue file had
119      * NOTIFY=SUCCESS we have to always look for the trace file and be
120      * prepared for the file not to exist.
121      *
122      * See also comments in qmgr/qmgr_active.c.
123      */
124     if (bounce_info->log_handle == 0) {
125 	if (msg_verbose)
126 	    msg_info("%s: no trace file -- not sending a notification",
127 		     queue_id);
128 	bounce_mail_free(bounce_info);
129 	return (0);
130     }
131 #define NULL_SENDER		MAIL_ADDR_EMPTY	/* special address */
132 #define NULL_TRACE_FLAGS	0
133 
134     /*
135      * Send a single bounce with a template message header, some boilerplate
136      * text that pretends that we are a polite mail system, the text with
137      * per-recipient status, and a copy of the original message.
138      *
139      * XXX DSN We use the same trace file for "what-if", "verbose delivery" and
140      * "success" delivery reports. This saves file system overhead because
141      * there are fewer potential left-over files to remove up when we create
142      * a new queue file.
143      */
144     if ((bounce = post_mail_fopen_nowait(NULL_SENDER, recipient,
145 					 INT_FILT_MASK_BOUNCE,
146 					 NULL_TRACE_FLAGS,
147 					 new_id)) != 0) {
148 	count = -1;
149 	if (bounce_header(bounce, bounce_info, recipient,
150 			  NO_POSTMASTER_COPY) == 0
151 	    && bounce_boilerplate(bounce, bounce_info) == 0
152 	    && (count = bounce_diagnostic_log(bounce, bounce_info,
153 					      DSN_NOTIFY_OVERRIDE)) > 0
154 	    && bounce_header_dsn(bounce, bounce_info) == 0
155 	    && bounce_diagnostic_dsn(bounce, bounce_info,
156 				     DSN_NOTIFY_OVERRIDE) > 0) {
157 	    bounce_original(bounce, bounce_info, DSN_RET_HDRS);
158 	    bounce_status = post_mail_fclose(bounce);
159 	    if (bounce_status == 0)
160 		msg_info("%s: sender delivery status notification: %s",
161 			 queue_id, STR(new_id));
162 	} else {
163 	    (void) vstream_fclose(bounce);
164 	    if (count == 0)
165 		bounce_status = 0;
166 	}
167     }
168 
169     /*
170      * Examine the completion status. Delete the trace log file only when the
171      * status notice was posted successfully.
172      */
173     if (bounce_status == 0 && mail_queue_remove(service, queue_id)
174 	&& errno != ENOENT)
175 	msg_fatal("remove %s %s: %m", service, queue_id);
176 
177     /*
178      * Cleanup.
179      */
180     bounce_mail_free(bounce_info);
181     vstring_free(new_id);
182 
183     return (bounce_status);
184 }
185