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