xref: /netbsd-src/external/ibm-public/postfix/dist/src/discard/discard.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1*33881f77Schristos /*	$NetBSD: discard.c,v 1.3 2020/03/18 19:05:15 christos Exp $	*/
241fbaed0Stron 
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /*	discard 8
641fbaed0Stron /* SUMMARY
741fbaed0Stron /*	Postfix discard mail delivery agent
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /*	\fBdiscard\fR [generic Postfix daemon options]
1041fbaed0Stron /* DESCRIPTION
1141fbaed0Stron /*	The Postfix \fBdiscard\fR(8) delivery agent processes
1241fbaed0Stron /*	delivery requests from
1341fbaed0Stron /*	the queue manager. Each request specifies a queue file, a sender
1416d67a18Stron /*	address, a next-hop destination that is treated as the reason for
1541fbaed0Stron /*	discarding the mail, and recipient information.
1641fbaed0Stron /*	The reason may be prefixed with an RFC 3463-compatible detail code.
1741fbaed0Stron /*	This program expects to be run from the \fBmaster\fR(8) process
1841fbaed0Stron /*	manager.
1941fbaed0Stron /*
2041fbaed0Stron /*	The \fBdiscard\fR(8) delivery agent pretends to deliver all recipients
2116d67a18Stron /*	in the delivery request, logs the "next-hop" destination
2216d67a18Stron /*	as the reason for discarding the mail, updates the
2316d67a18Stron /*	queue file, and either marks recipients as finished or informs the
2441fbaed0Stron /*	queue manager that delivery should be tried again at a later time.
2541fbaed0Stron /*
2641fbaed0Stron /*	Delivery status reports are sent to the \fBtrace\fR(8)
2741fbaed0Stron /*	daemon as appropriate.
2841fbaed0Stron /* SECURITY
2941fbaed0Stron /* .ad
3041fbaed0Stron /* .fi
3141fbaed0Stron /*	The \fBdiscard\fR(8) mailer is not security-sensitive. It does not talk
3241fbaed0Stron /*	to the network, and can be run chrooted at fixed low privilege.
3341fbaed0Stron /* STANDARDS
3416d67a18Stron /*	RFC 3463 (Enhanced Status Codes)
3541fbaed0Stron /* DIAGNOSTICS
36*33881f77Schristos /*	Problems and transactions are logged to \fBsyslogd\fR(8)
37*33881f77Schristos /*	or \fBpostlogd\fR(8).
3841fbaed0Stron /*
3941fbaed0Stron /*	Depending on the setting of the \fBnotify_classes\fR parameter,
4041fbaed0Stron /*	the postmaster is notified of bounces and of other trouble.
4141fbaed0Stron /* CONFIGURATION PARAMETERS
4241fbaed0Stron /* .ad
4341fbaed0Stron /* .fi
4441fbaed0Stron /*	Changes to \fBmain.cf\fR are picked up automatically as \fBdiscard\fR(8)
4541fbaed0Stron /*	processes run for only a limited amount of time. Use the command
4641fbaed0Stron /*	"\fBpostfix reload\fR" to speed up a change.
4741fbaed0Stron /*
4841fbaed0Stron /*	The text below provides only a parameter summary. See
4941fbaed0Stron /*	\fBpostconf\fR(5) for more details including examples.
5041fbaed0Stron /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
5141fbaed0Stron /*	The default location of the Postfix main.cf and master.cf
5241fbaed0Stron /*	configuration files.
5341fbaed0Stron /* .IP "\fBdaemon_timeout (18000s)\fR"
5441fbaed0Stron /*	How much time a Postfix daemon process may take to handle a
5541fbaed0Stron /*	request before it is terminated by a built-in watchdog timer.
5641fbaed0Stron /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
5741fbaed0Stron /*	The maximal number of digits after the decimal point when logging
5841fbaed0Stron /*	sub-second delay values.
5941fbaed0Stron /* .IP "\fBdouble_bounce_sender (double-bounce)\fR"
6041fbaed0Stron /*	The sender address of postmaster notifications that are generated
6141fbaed0Stron /*	by the mail system.
6241fbaed0Stron /* .IP "\fBipc_timeout (3600s)\fR"
6341fbaed0Stron /*	The time limit for sending or receiving information over an internal
6441fbaed0Stron /*	communication channel.
6541fbaed0Stron /* .IP "\fBmax_idle (100s)\fR"
6641fbaed0Stron /*	The maximum amount of time that an idle Postfix daemon process waits
6741fbaed0Stron /*	for an incoming connection before terminating voluntarily.
6841fbaed0Stron /* .IP "\fBmax_use (100)\fR"
6941fbaed0Stron /*	The maximal number of incoming connections that a Postfix daemon
7041fbaed0Stron /*	process will service before terminating voluntarily.
7141fbaed0Stron /* .IP "\fBprocess_id (read-only)\fR"
7241fbaed0Stron /*	The process ID of a Postfix command or daemon process.
7341fbaed0Stron /* .IP "\fBprocess_name (read-only)\fR"
7441fbaed0Stron /*	The process name of a Postfix command or daemon process.
7541fbaed0Stron /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
7641fbaed0Stron /*	The location of the Postfix top-level queue directory.
7741fbaed0Stron /* .IP "\fBsyslog_facility (mail)\fR"
7841fbaed0Stron /*	The syslog facility of Postfix logging.
7941fbaed0Stron /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
80*33881f77Schristos /*	A prefix that is prepended to the process name in syslog
81*33881f77Schristos /*	records, so that, for example, "smtpd" becomes "prefix/smtpd".
82*33881f77Schristos /* .PP
83*33881f77Schristos /*	Available in Postfix 3.3 and later:
84*33881f77Schristos /* .IP "\fBservice_name (read-only)\fR"
85*33881f77Schristos /*	The master.cf service name of a Postfix daemon process.
8641fbaed0Stron /* SEE ALSO
8741fbaed0Stron /*	qmgr(8), queue manager
8841fbaed0Stron /*	bounce(8), delivery status reports
8941fbaed0Stron /*	error(8), Postfix error delivery agent
9041fbaed0Stron /*	postconf(5), configuration parameters
9141fbaed0Stron /*	master(5), generic daemon options
9241fbaed0Stron /*	master(8), process manager
93*33881f77Schristos /*	postlogd(8), Postfix logging
9441fbaed0Stron /*	syslogd(8), system logging
9541fbaed0Stron /* LICENSE
9641fbaed0Stron /* .ad
9741fbaed0Stron /* .fi
9841fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
9941fbaed0Stron /* HISTORY
10041fbaed0Stron /*	This service was introduced with Postfix version 2.2.
10141fbaed0Stron /* AUTHOR(S)
10241fbaed0Stron /*	Victor Duchovni
10341fbaed0Stron /*	Morgan Stanley
10441fbaed0Stron /*
10541fbaed0Stron /*	Based on code by:
10641fbaed0Stron /*	Wietse Venema
10741fbaed0Stron /*	IBM T.J. Watson Research
10841fbaed0Stron /*	P.O. Box 704
10941fbaed0Stron /*	Yorktown Heights, NY 10598, USA
110e262b48eSchristos /*
111e262b48eSchristos /*	Wietse Venema
112e262b48eSchristos /*	Google, Inc.
113e262b48eSchristos /*	111 8th Avenue
114e262b48eSchristos /*	New York, NY 10011, USA
11541fbaed0Stron /*--*/
11641fbaed0Stron 
11741fbaed0Stron /* System library. */
11841fbaed0Stron 
11941fbaed0Stron #include <sys_defs.h>
12041fbaed0Stron #include <unistd.h>
12141fbaed0Stron #include <stdlib.h>
12241fbaed0Stron 
12341fbaed0Stron /* Utility library. */
12441fbaed0Stron 
12541fbaed0Stron #include <msg.h>
12641fbaed0Stron #include <vstream.h>
12741fbaed0Stron 
12841fbaed0Stron /* Global library. */
12941fbaed0Stron 
13041fbaed0Stron #include <deliver_request.h>
13141fbaed0Stron #include <mail_queue.h>
13241fbaed0Stron #include <bounce.h>
13341fbaed0Stron #include <deliver_completed.h>
13441fbaed0Stron #include <flush_clnt.h>
13541fbaed0Stron #include <sent.h>
13641fbaed0Stron #include <dsn_util.h>
13741fbaed0Stron #include <mail_version.h>
13841fbaed0Stron 
13941fbaed0Stron /* Single server skeleton. */
14041fbaed0Stron 
14141fbaed0Stron #include <mail_server.h>
14241fbaed0Stron 
14341fbaed0Stron /* deliver_message - deliver message with extreme prejudice */
14441fbaed0Stron 
deliver_message(DELIVER_REQUEST * request)14541fbaed0Stron static int deliver_message(DELIVER_REQUEST *request)
14641fbaed0Stron {
14741fbaed0Stron     const char *myname = "deliver_message";
14841fbaed0Stron     VSTREAM *src;
14941fbaed0Stron     int     result = 0;
15041fbaed0Stron     int     status;
15141fbaed0Stron     RECIPIENT *rcpt;
15241fbaed0Stron     int     nrcpt;
15341fbaed0Stron     DSN_SPLIT dp;
15441fbaed0Stron     DSN     dsn;
15541fbaed0Stron 
15641fbaed0Stron     if (msg_verbose)
15741fbaed0Stron 	msg_info("deliver_message: from %s", request->sender);
15841fbaed0Stron 
15941fbaed0Stron     /*
16041fbaed0Stron      * Sanity checks.
16141fbaed0Stron      */
16241fbaed0Stron     if (request->nexthop[0] == 0)
16341fbaed0Stron 	msg_fatal("empty nexthop hostname");
16441fbaed0Stron     if (request->rcpt_list.len <= 0)
16541fbaed0Stron 	msg_fatal("recipient count: %d", request->rcpt_list.len);
16641fbaed0Stron 
16741fbaed0Stron     /*
16841fbaed0Stron      * Open the queue file. Opening the file can fail for a variety of
16941fbaed0Stron      * reasons, such as the system running out of resources. Instead of
17041fbaed0Stron      * throwing away mail, we're raising a fatal error which forces the mail
17141fbaed0Stron      * system to back off, and retry later.
17241fbaed0Stron      */
17341fbaed0Stron     src = mail_queue_open(request->queue_name, request->queue_id,
17441fbaed0Stron 			  O_RDWR, 0);
17541fbaed0Stron     if (src == 0)
17641fbaed0Stron 	msg_fatal("%s: open %s %s: %m", myname,
17741fbaed0Stron 		  request->queue_name, request->queue_id);
17841fbaed0Stron     if (msg_verbose)
17941fbaed0Stron 	msg_info("%s: file %s", myname, VSTREAM_PATH(src));
18041fbaed0Stron 
18141fbaed0Stron     /*
18241fbaed0Stron      * Discard all recipients.
18341fbaed0Stron      */
18441fbaed0Stron #define BOUNCE_FLAGS(request) DEL_REQ_TRACE_FLAGS(request->flags)
18541fbaed0Stron 
18641fbaed0Stron     dsn_split(&dp, "2.0.0", request->nexthop);
18741fbaed0Stron     (void) DSN_SIMPLE(&dsn, DSN_STATUS(dp.dsn), dp.text);
18841fbaed0Stron     for (nrcpt = 0; nrcpt < request->rcpt_list.len; nrcpt++) {
18941fbaed0Stron 	rcpt = request->rcpt_list.info + nrcpt;
19041fbaed0Stron 	status = sent(BOUNCE_FLAGS(request), request->queue_id,
19141fbaed0Stron 		      &request->msg_stats, rcpt, "none", &dsn);
19241fbaed0Stron 	if (status == 0 && (request->flags & DEL_REQ_FLAG_SUCCESS))
19341fbaed0Stron 	    deliver_completed(src, rcpt->offset);
19441fbaed0Stron 	result |= status;
19541fbaed0Stron     }
19641fbaed0Stron 
19741fbaed0Stron     /*
19841fbaed0Stron      * Clean up.
19941fbaed0Stron      */
20041fbaed0Stron     if (vstream_fclose(src))
20141fbaed0Stron 	msg_warn("close %s %s: %m", request->queue_name, request->queue_id);
20241fbaed0Stron 
20341fbaed0Stron     return (result);
20441fbaed0Stron }
20541fbaed0Stron 
20641fbaed0Stron /* discard_service - perform service for client */
20741fbaed0Stron 
discard_service(VSTREAM * client_stream,char * unused_service,char ** argv)20841fbaed0Stron static void discard_service(VSTREAM *client_stream, char *unused_service, char **argv)
20941fbaed0Stron {
21041fbaed0Stron     DELIVER_REQUEST *request;
21141fbaed0Stron     int     status;
21241fbaed0Stron 
21341fbaed0Stron     /*
21441fbaed0Stron      * Sanity check. This service takes no command-line arguments.
21541fbaed0Stron      */
21641fbaed0Stron     if (argv[0])
21741fbaed0Stron 	msg_fatal("unexpected command-line argument: %s", argv[0]);
21841fbaed0Stron 
21941fbaed0Stron     /*
22041fbaed0Stron      * This routine runs whenever a client connects to the UNIX-domain socket
22141fbaed0Stron      * dedicated to the discard mailer. What we see below is a little
22241fbaed0Stron      * protocol to (1) tell the queue manager that we are ready, (2) read a
22341fbaed0Stron      * request from the queue manager, and (3) report the completion status
22441fbaed0Stron      * of that request. All connection-management stuff is handled by the
22541fbaed0Stron      * common code in single_server.c.
22641fbaed0Stron      */
22741fbaed0Stron     if ((request = deliver_request_read(client_stream)) != 0) {
22841fbaed0Stron 	status = deliver_message(request);
22941fbaed0Stron 	deliver_request_done(client_stream, request, status);
23041fbaed0Stron     }
23141fbaed0Stron }
23241fbaed0Stron 
23341fbaed0Stron /* pre_init - pre-jail initialization */
23441fbaed0Stron 
pre_init(char * unused_name,char ** unused_argv)23541fbaed0Stron static void pre_init(char *unused_name, char **unused_argv)
23641fbaed0Stron {
23741fbaed0Stron     flush_init();
23841fbaed0Stron }
23941fbaed0Stron 
24041fbaed0Stron MAIL_VERSION_STAMP_DECLARE;
24141fbaed0Stron 
24241fbaed0Stron /* main - pass control to the single-threaded skeleton */
24341fbaed0Stron 
main(int argc,char ** argv)24441fbaed0Stron int     main(int argc, char **argv)
24541fbaed0Stron {
24641fbaed0Stron 
24741fbaed0Stron     /*
24841fbaed0Stron      * Fingerprint executables and core dumps.
24941fbaed0Stron      */
25041fbaed0Stron     MAIL_VERSION_STAMP_ALLOCATE;
25141fbaed0Stron 
25241fbaed0Stron     single_server_main(argc, argv, discard_service,
253e262b48eSchristos 		       CA_MAIL_SERVER_PRE_INIT(pre_init),
25441fbaed0Stron 		       0);
25541fbaed0Stron }
256