1 /*++
2 /* NAME
3 /*	discard 8
4 /* SUMMARY
5 /*	Postfix discard mail delivery agent
6 /* SYNOPSIS
7 /*	\fBdiscard\fR [generic Postfix daemon options]
8 /* DESCRIPTION
9 /*	The Postfix \fBdiscard\fR(8) delivery agent processes
10 /*	delivery requests from
11 /*	the queue manager. Each request specifies a queue file, a sender
12 /*	address, a next-hop destination that is treated as the reason for
13 /*	discarding the mail, and recipient information.
14 /*	The reason may be prefixed with an RFC 3463-compatible detail code.
15 /*	This program expects to be run from the \fBmaster\fR(8) process
16 /*	manager.
17 /*
18 /*	The \fBdiscard\fR(8) delivery agent pretends to deliver all recipients
19 /*	in the delivery request, logs the "next-hop" destination
20 /*	as the reason for discarding the mail, updates the
21 /*	queue file, and either marks recipients as finished or informs the
22 /*	queue manager that delivery should be tried again at a later time.
23 /*
24 /*	Delivery status reports are sent to the \fBtrace\fR(8)
25 /*	daemon as appropriate.
26 /* SECURITY
27 /* .ad
28 /* .fi
29 /*	The \fBdiscard\fR(8) mailer is not security-sensitive. It does not talk
30 /*	to the network, and can be run chrooted at fixed low privilege.
31 /* STANDARDS
32 /*	RFC 3463 (Enhanced Status Codes)
33 /* DIAGNOSTICS
34 /*	Problems and transactions are logged to \fBsyslogd\fR(8)
35 /*	or \fBpostlogd\fR(8).
36 /*
37 /*	Depending on the setting of the \fBnotify_classes\fR parameter,
38 /*	the postmaster is notified of bounces and of other trouble.
39 /* CONFIGURATION PARAMETERS
40 /* .ad
41 /* .fi
42 /*	Changes to \fBmain.cf\fR are picked up automatically as \fBdiscard\fR(8)
43 /*	processes run for only a limited amount of time. Use the command
44 /*	"\fBpostfix reload\fR" to speed up a change.
45 /*
46 /*	The text below provides only a parameter summary. See
47 /*	\fBpostconf\fR(5) for more details including examples.
48 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
49 /*	The default location of the Postfix main.cf and master.cf
50 /*	configuration files.
51 /* .IP "\fBdaemon_timeout (18000s)\fR"
52 /*	How much time a Postfix daemon process may take to handle a
53 /*	request before it is terminated by a built-in watchdog timer.
54 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
55 /*	The maximal number of digits after the decimal point when logging
56 /*	sub-second delay values.
57 /* .IP "\fBdouble_bounce_sender (double-bounce)\fR"
58 /*	The sender address of postmaster notifications that are generated
59 /*	by the mail system.
60 /* .IP "\fBipc_timeout (3600s)\fR"
61 /*	The time limit for sending or receiving information over an internal
62 /*	communication channel.
63 /* .IP "\fBmax_idle (100s)\fR"
64 /*	The maximum amount of time that an idle Postfix daemon process waits
65 /*	for an incoming connection before terminating voluntarily.
66 /* .IP "\fBmax_use (100)\fR"
67 /*	The maximal number of incoming connections that a Postfix daemon
68 /*	process will service before terminating voluntarily.
69 /* .IP "\fBprocess_id (read-only)\fR"
70 /*	The process ID of a Postfix command or daemon process.
71 /* .IP "\fBprocess_name (read-only)\fR"
72 /*	The process name of a Postfix command or daemon process.
73 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
74 /*	The location of the Postfix top-level queue directory.
75 /* .IP "\fBsyslog_facility (mail)\fR"
76 /*	The syslog facility of Postfix logging.
77 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
78 /*	A prefix that is prepended to the process name in syslog
79 /*	records, so that, for example, "smtpd" becomes "prefix/smtpd".
80 /* .PP
81 /*	Available in Postfix 3.3 and later:
82 /* .IP "\fBservice_name (read-only)\fR"
83 /*	The master.cf service name of a Postfix daemon process.
84 /* SEE ALSO
85 /*	qmgr(8), queue manager
86 /*	bounce(8), delivery status reports
87 /*	error(8), Postfix error delivery agent
88 /*	postconf(5), configuration parameters
89 /*	master(5), generic daemon options
90 /*	master(8), process manager
91 /*	postlogd(8), Postfix logging
92 /*	syslogd(8), system logging
93 /* LICENSE
94 /* .ad
95 /* .fi
96 /*	The Secure Mailer license must be distributed with this software.
97 /* HISTORY
98 /*	This service was introduced with Postfix version 2.2.
99 /* AUTHOR(S)
100 /*	Victor Duchovni
101 /*	Morgan Stanley
102 /*
103 /*	Based on code by:
104 /*	Wietse Venema
105 /*	IBM T.J. Watson Research
106 /*	P.O. Box 704
107 /*	Yorktown Heights, NY 10598, USA
108 /*
109 /*	Wietse Venema
110 /*	Google, Inc.
111 /*	111 8th Avenue
112 /*	New York, NY 10011, USA
113 /*--*/
114 
115 /* System library. */
116 
117 #include <sys_defs.h>
118 #include <unistd.h>
119 #include <stdlib.h>
120 
121 /* Utility library. */
122 
123 #include <msg.h>
124 #include <vstream.h>
125 
126 /* Global library. */
127 
128 #include <deliver_request.h>
129 #include <mail_queue.h>
130 #include <bounce.h>
131 #include <deliver_completed.h>
132 #include <flush_clnt.h>
133 #include <sent.h>
134 #include <dsn_util.h>
135 #include <mail_version.h>
136 
137 /* Single server skeleton. */
138 
139 #include <mail_server.h>
140 
141 /* deliver_message - deliver message with extreme prejudice */
142 
deliver_message(DELIVER_REQUEST * request)143 static int deliver_message(DELIVER_REQUEST *request)
144 {
145     const char *myname = "deliver_message";
146     VSTREAM *src;
147     int     result = 0;
148     int     status;
149     RECIPIENT *rcpt;
150     int     nrcpt;
151     DSN_SPLIT dp;
152     DSN     dsn;
153 
154     if (msg_verbose)
155 	msg_info("deliver_message: from %s", request->sender);
156 
157     /*
158      * Sanity checks.
159      */
160     if (request->nexthop[0] == 0)
161 	msg_fatal("empty nexthop hostname");
162     if (request->rcpt_list.len <= 0)
163 	msg_fatal("recipient count: %d", request->rcpt_list.len);
164 
165     /*
166      * Open the queue file. Opening the file can fail for a variety of
167      * reasons, such as the system running out of resources. Instead of
168      * throwing away mail, we're raising a fatal error which forces the mail
169      * system to back off, and retry later.
170      */
171     src = mail_queue_open(request->queue_name, request->queue_id,
172 			  O_RDWR, 0);
173     if (src == 0)
174 	msg_fatal("%s: open %s %s: %m", myname,
175 		  request->queue_name, request->queue_id);
176     if (msg_verbose)
177 	msg_info("%s: file %s", myname, VSTREAM_PATH(src));
178 
179     /*
180      * Discard all recipients.
181      */
182 #define BOUNCE_FLAGS(request) DEL_REQ_TRACE_FLAGS(request->flags)
183 
184     dsn_split(&dp, "2.0.0", request->nexthop);
185     (void) DSN_SIMPLE(&dsn, DSN_STATUS(dp.dsn), dp.text);
186     for (nrcpt = 0; nrcpt < request->rcpt_list.len; nrcpt++) {
187 	rcpt = request->rcpt_list.info + nrcpt;
188 	status = sent(BOUNCE_FLAGS(request), request->queue_id,
189 		      &request->msg_stats, rcpt, "none", &dsn);
190 	if (status == 0 && (request->flags & DEL_REQ_FLAG_SUCCESS))
191 	    deliver_completed(src, rcpt->offset);
192 	result |= status;
193     }
194 
195     /*
196      * Clean up.
197      */
198     if (vstream_fclose(src))
199 	msg_warn("close %s %s: %m", request->queue_name, request->queue_id);
200 
201     return (result);
202 }
203 
204 /* discard_service - perform service for client */
205 
discard_service(VSTREAM * client_stream,char * unused_service,char ** argv)206 static void discard_service(VSTREAM *client_stream, char *unused_service, char **argv)
207 {
208     DELIVER_REQUEST *request;
209     int     status;
210 
211     /*
212      * Sanity check. This service takes no command-line arguments.
213      */
214     if (argv[0])
215 	msg_fatal("unexpected command-line argument: %s", argv[0]);
216 
217     /*
218      * This routine runs whenever a client connects to the UNIX-domain socket
219      * dedicated to the discard mailer. What we see below is a little
220      * protocol to (1) tell the queue manager that we are ready, (2) read a
221      * request from the queue manager, and (3) report the completion status
222      * of that request. All connection-management stuff is handled by the
223      * common code in single_server.c.
224      */
225     if ((request = deliver_request_read(client_stream)) != 0) {
226 	status = deliver_message(request);
227 	deliver_request_done(client_stream, request, status);
228     }
229 }
230 
231 /* pre_init - pre-jail initialization */
232 
pre_init(char * unused_name,char ** unused_argv)233 static void pre_init(char *unused_name, char **unused_argv)
234 {
235     flush_init();
236 }
237 
238 MAIL_VERSION_STAMP_DECLARE;
239 
240 /* main - pass control to the single-threaded skeleton */
241 
main(int argc,char ** argv)242 int     main(int argc, char **argv)
243 {
244 
245     /*
246      * Fingerprint executables and core dumps.
247      */
248     MAIL_VERSION_STAMP_ALLOCATE;
249 
250     single_server_main(argc, argv, discard_service,
251 		       CA_MAIL_SERVER_PRE_INIT(pre_init),
252 		       0);
253 }
254