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