Postfix3.3.1
error.c
[詳解]
1 /*++
2 /* NAME
3 /* error 8
4 /* SUMMARY
5 /* Postfix error/retry mail delivery agent
6 /* SYNOPSIS
7 /* \fBerror\fR [generic Postfix daemon options]
8 /* DESCRIPTION
9 /* The Postfix \fBerror\fR(8) delivery agent processes delivery
10 /* requests from
11 /* the queue manager. Each request specifies a queue file, a sender
12 /* address, the reason for non-delivery (specified as the
13 /* next-hop destination), and recipient information.
14 /* The reason may be prefixed with an RFC 3463-compatible detail code;
15 /* if none is specified a default 4.0.0 or 5.0.0 code is used instead.
16 /* This program expects to be run from the \fBmaster\fR(8) process
17 /* manager.
18 /*
19 /* Depending on the service name in master.cf, \fBerror\fR
20 /* or \fBretry\fR, the server bounces or defers all recipients
21 /* in the delivery request using the "next-hop" information
22 /* as the reason for non-delivery. The \fBretry\fR service name is
23 /* supported as of Postfix 2.4.
24 /*
25 /* Delivery status reports are sent to the \fBbounce\fR(8),
26 /* \fBdefer\fR(8) or \fBtrace\fR(8) daemon as appropriate.
27 /* SECURITY
28 /* .ad
29 /* .fi
30 /* The \fBerror\fR(8) mailer is not security-sensitive. It does not talk
31 /* to the network, and can be run chrooted at fixed low privilege.
32 /* STANDARDS
33 /* RFC 3463 (Enhanced Status Codes)
34 /* DIAGNOSTICS
35 /* Problems and transactions are logged to \fBsyslogd\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 \fBerror\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 "\fB2bounce_notice_recipient (postmaster)\fR"
49 /* The recipient of undeliverable mail that cannot be returned to
50 /* the sender.
51 /* .IP "\fBbounce_notice_recipient (postmaster)\fR"
52 /* The recipient of postmaster notifications with the message headers
53 /* of mail that Postfix did not deliver and of SMTP conversation
54 /* transcripts of mail that Postfix did not receive.
55 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
56 /* The default location of the Postfix main.cf and master.cf
57 /* configuration files.
58 /* .IP "\fBdaemon_timeout (18000s)\fR"
59 /* How much time a Postfix daemon process may take to handle a
60 /* request before it is terminated by a built-in watchdog timer.
61 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
62 /* The maximal number of digits after the decimal point when logging
63 /* sub-second delay values.
64 /* .IP "\fBdouble_bounce_sender (double-bounce)\fR"
65 /* The sender address of postmaster notifications that are generated
66 /* by the mail system.
67 /* .IP "\fBipc_timeout (3600s)\fR"
68 /* The time limit for sending or receiving information over an internal
69 /* communication channel.
70 /* .IP "\fBmax_idle (100s)\fR"
71 /* The maximum amount of time that an idle Postfix daemon process waits
72 /* for an incoming connection before terminating voluntarily.
73 /* .IP "\fBmax_use (100)\fR"
74 /* The maximal number of incoming connections that a Postfix daemon
75 /* process will service before terminating voluntarily.
76 /* .IP "\fBnotify_classes (resource, software)\fR"
77 /* The list of error classes that are reported to the postmaster.
78 /* .IP "\fBprocess_id (read-only)\fR"
79 /* The process ID of a Postfix command or daemon process.
80 /* .IP "\fBprocess_name (read-only)\fR"
81 /* The process name of a Postfix command or daemon process.
82 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
83 /* The location of the Postfix top-level queue directory.
84 /* .IP "\fBsyslog_facility (mail)\fR"
85 /* The syslog facility of Postfix logging.
86 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
87 /* A prefix that is prepended to the process name in syslog
88 /* records, so that, for example, "smtpd" becomes "prefix/smtpd".
89 /* .PP
90 /* Available in Postfix 3.3 and later:
91 /* .IP "\fBservice_name (read-only)\fR"
92 /* The master.cf service name of a Postfix daemon process.
93 /* SEE ALSO
94 /* qmgr(8), queue manager
95 /* bounce(8), delivery status reports
96 /* discard(8), Postfix discard delivery agent
97 /* postconf(5), configuration parameters
98 /* master(5), generic daemon options
99 /* master(8), process manager
100 /* syslogd(8), system logging
101 /* LICENSE
102 /* .ad
103 /* .fi
104 /* The Secure Mailer license must be distributed with this software.
105 /* AUTHOR(S)
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 <defer.h>
134 #include <deliver_completed.h>
135 #include <flush_clnt.h>
136 #include <dsn_util.h>
137 #include <sys_exits.h>
138 #include <mail_proto.h>
139 #include <mail_version.h>
140 
141 /* Single server skeleton. */
142 
143 #include <mail_server.h>
144 
145 /* deliver_message - deliver message with extreme prejudice */
146 
147 static int deliver_message(DELIVER_REQUEST *request, const char *def_dsn,
148  int (*append) (int, const char *, MSG_STATS *, RECIPIENT *,
149  const char *, DSN *))
150 {
151  const char *myname = "deliver_message";
152  VSTREAM *src;
153  int result = 0;
154  int status;
155  RECIPIENT *rcpt;
156  int nrcpt;
157  DSN_SPLIT dp;
158  DSN dsn;
159 
160  if (msg_verbose)
161  msg_info("deliver_message: from %s", request->sender);
162 
163  /*
164  * Sanity checks.
165  */
166  if (request->nexthop[0] == 0)
167  msg_fatal("empty nexthop hostname");
168  if (request->rcpt_list.len <= 0)
169  msg_fatal("recipient count: %d", request->rcpt_list.len);
170 
171  /*
172  * Open the queue file. Opening the file can fail for a variety of
173  * reasons, such as the system running out of resources. Instead of
174  * throwing away mail, we're raising a fatal error which forces the mail
175  * system to back off, and retry later.
176  */
177  src = mail_queue_open(request->queue_name, request->queue_id,
178  O_RDWR, 0);
179  if (src == 0)
180  msg_fatal("%s: open %s %s: %m", myname,
181  request->queue_name, request->queue_id);
182  if (msg_verbose)
183  msg_info("%s: file %s", myname, VSTREAM_PATH(src));
184 
185  /*
186  * Bounce/defer/whatever all recipients.
187  */
188 #define BOUNCE_FLAGS(request) DEL_REQ_TRACE_FLAGS(request->flags)
189 
190  dsn_split(&dp, def_dsn, request->nexthop);
191  (void) DSN_SIMPLE(&dsn, DSN_STATUS(dp.dsn), dp.text);
192  for (nrcpt = 0; nrcpt < request->rcpt_list.len; nrcpt++) {
193  rcpt = request->rcpt_list.info + nrcpt;
194  status = append(BOUNCE_FLAGS(request), request->queue_id,
195  &request->msg_stats, rcpt, "none", &dsn);
196  if (status == 0)
197  deliver_completed(src, rcpt->offset);
198  result |= status;
199  }
200 
201  /*
202  * Clean up.
203  */
204  if (vstream_fclose(src))
205  msg_warn("close %s %s: %m", request->queue_name, request->queue_id);
206 
207  return (result);
208 }
209 
210 /* error_service - perform service for client */
211 
212 static void error_service(VSTREAM *client_stream, char *service, char **argv)
213 {
214  DELIVER_REQUEST *request;
215  int status;
216 
217  /*
218  * Sanity check. This service takes no command-line arguments.
219  */
220  if (argv[0])
221  msg_fatal("unexpected command-line argument: %s", argv[0]);
222 
223  /*
224  * This routine runs whenever a client connects to the UNIX-domain socket
225  * dedicated to the error mailer. What we see below is a little protocol
226  * to (1) tell the queue manager that we are ready, (2) read a request
227  * from the queue manager, and (3) report the completion status of that
228  * request. All connection-management stuff is handled by the common code
229  * in single_server.c.
230  */
231  if ((request = deliver_request_read(client_stream)) != 0) {
232  if (strcmp(service, MAIL_SERVICE_ERROR) == 0)
233  status = deliver_message(request, "5.0.0", bounce_append);
234  else if (strcmp(service, MAIL_SERVICE_RETRY) == 0)
235  status = deliver_message(request, "4.0.0", defer_append);
236  else
237  msg_fatal("bad error service name: %s", service);
238  deliver_request_done(client_stream, request, status);
239  }
240 }
241 
242 /* pre_init - pre-jail initialization */
243 
244 static void pre_init(char *unused_name, char **unused_argv)
245 {
246  flush_init();
247 }
248 
250 
251 /* main - pass control to the single-threaded skeleton */
252 
253 int main(int argc, char **argv)
254 {
255 
256  /*
257  * Fingerprint executables and core dumps.
258  */
260 
261  single_server_main(argc, argv, error_service,
262  CA_MAIL_SERVER_PRE_INIT(pre_init),
263  0);
264 }
int msg_verbose
Definition: msg.c:177
RECIPIENT_LIST rcpt_list
DSN_SPLIT * dsn_split(DSN_SPLIT *dp, const char *def_dsn, const char *text)
Definition: dsn_util.c:138
#define MAIL_SERVICE_ERROR
Definition: mail_proto.h:52
#define DSN_SIMPLE(dsn, _status, _reason)
Definition: dsn.h:41
void deliver_completed(VSTREAM *stream, long offset)
#define VSTREAM_PATH(vp)
Definition: vstream.h:126
#define DSN_STATUS(dsn_buf)
Definition: dsn_util.h:46
void flush_init(void)
Definition: flush_clnt.c:104
MSG_STATS msg_stats
int bounce_append(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: bounce.c:222
#define BOUNCE_FLAGS(request)
const char * text
Definition: dsn_util.h:55
int vstream_fclose(VSTREAM *stream)
Definition: vstream.c:1268
VSTREAM * mail_queue_open(const char *queue_name, const char *queue_id, int flags, mode_t mode)
Definition: mail_queue.c:424
void msg_warn(const char *fmt,...)
Definition: msg.c:215
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
DSN_STAT dsn
Definition: dsn_util.h:54
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
#define MAIL_SERVICE_RETRY
Definition: mail_proto.h:53
MAIL_VERSION_STAMP_DECLARE
Definition: error.c:249
int deliver_request_done(VSTREAM *stream, DELIVER_REQUEST *request, int status)
int main(int argc, char **argv)
Definition: error.c:253
Definition: dsn.h:17
RECIPIENT * info
NORETURN single_server_main(int, char **, SINGLE_SERVER_FN,...)
int defer_append(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: defer.c:187
DELIVER_REQUEST * deliver_request_read(VSTREAM *stream)
#define CA_MAIL_SERVER_PRE_INIT(v)
Definition: mail_server.h:64
void msg_info(const char *fmt,...)
Definition: msg.c:199