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