Postfix3.3.1
defer.c
[詳解]
1 /*++
2 /* NAME
3 /* defer 3
4 /* SUMMARY
5 /* defer service client interface
6 /* SYNOPSIS
7 /* #include <defer.h>
8 /*
9 /* int defer_append(flags, id, stats, rcpt, relay, dsn)
10 /* int flags;
11 /* const char *id;
12 /* MSG_STATS *stats;
13 /* RECIPIENT *rcpt;
14 /* const char *relay;
15 /* DSN *dsn;
16 /*
17 /* int defer_flush(flags, queue, id, encoding, smtputf8, sender,
18 /* dsn_envid, dsn_ret)
19 /* int flags;
20 /* const char *queue;
21 /* const char *id;
22 /* const char *encoding;
23 /* int smtputf8;
24 /* const char *sender;
25 /* const char *dsn_envid;
26 /* int dsn_ret;
27 /*
28 /* int defer_warn(flags, queue, id, encoding, smtputf8, sender,
29  dsn_envid, dsn_ret)
30 /* int flags;
31 /* const char *queue;
32 /* const char *id;
33 /* const char *encoding;
34 /* int smtputf8;
35 /* const char *sender;
36 /* const char *dsn_envid;
37 /* int dsn_ret;
38 /*
39 /* int defer_one(flags, queue, id, encoding, smtputf8, sender,
40 /* dsn_envid, ret, stats, recipient, relay, dsn)
41 /* int flags;
42 /* const char *queue;
43 /* const char *id;
44 /* const char *encoding;
45 /* int smtputf8;
46 /* const char *sender;
47 /* const char *dsn_envid;
48 /* int dsn_ret;
49 /* MSG_STATS *stats;
50 /* RECIPIENT *rcpt;
51 /* const char *relay;
52 /* DSN *dsn;
53 /* INTERNAL API
54 /* int defer_append_intern(flags, id, stats, rcpt, relay, dsn)
55 /* int flags;
56 /* const char *id;
57 /* MSG_STATS *stats;
58 /* RECIPIENT *rcpt;
59 /* const char *relay;
60 /* DESCRIPTION
61 /* This module implements a client interface to the defer service,
62 /* which maintains a per-message logfile with status records for
63 /* each recipient whose delivery is deferred, and the dsn_text why.
64 /*
65 /* defer_append() appends a record to the per-message defer log,
66 /* with the dsn_text for delayed delivery to the named rcpt,
67 /* updates the address verification service, or updates a message
68 /* delivery record on request by the sender. The flags argument
69 /* determines the action.
70 /* The result is a convenient non-zero value.
71 /* When the fast flush cache is enabled, the fast flush server is
72 /* notified of deferred mail.
73 /*
74 /* defer_flush() bounces the specified message to the specified
75 /* sender, including the defer log that was built with defer_append().
76 /* defer_flush() requests that the deferred recipients are deleted
77 /* from the original queue file; the defer logfile is deleted after
78 /* successful completion.
79 /* The result is zero in case of success, non-zero otherwise.
80 /*
81 /* defer_warn() sends a warning message that the mail in
82 /* question has been deferred. The defer log is not deleted,
83 /* and no recipients are deleted from the original queue file.
84 /*
85 /* defer_one() implements dsn_filter(3) compatibility for the
86 /* bounce_one() routine.
87 /*
88 /* defer_append_intern() is for use after the DSN filter.
89 /*
90 /* Arguments:
91 /* .IP flags
92 /* The bit-wise OR of zero or more of the following (specify
93 /* BOUNCE_FLAG_NONE to explicitly request not special processing):
94 /* .RS
95 /* .IP BOUNCE_FLAG_CLEAN
96 /* Delete the defer log in case of an error (as in: pretend
97 /* that we never even tried to defer this message).
98 /* .IP BOUNCE_FLAG_DELRCPT
99 /* When specified with a flush request, request that
100 /* recipients be deleted from the queue file.
101 /*
102 /* Note: the bounce daemon ignores this request when the
103 /* recipient queue file offset is <= 0.
104 /* .IP DEL_REQ_FLAG_MTA_VRFY
105 /* The message is an MTA-requested address verification probe.
106 /* Update the address verification database instead of deferring
107 /* mail.
108 /* .IP DEL_REQ_FLAG_USR_VRFY
109 /* The message is a user-requested address expansion probe.
110 /* Update the message delivery record instead of deferring
111 /* mail.
112 /* .IP DEL_REQ_FLAG_RECORD
113 /* This is a normal message with logged delivery. Update the
114 /* message delivery record and defer mail delivery.
115 /* .RE
116 /* .IP queue
117 /* The message queue name of the original message file.
118 /* .IP id
119 /* The queue id of the original message file.
120 /* .IP stats
121 /* Time stamps from different message delivery stages
122 /* and session reuse count.
123 /* .IP rcpt
124 /* Recipient information. See recipient_list(3).
125 /* .IP relay
126 /* Host we could not talk to.
127 /* .IP dsn
128 /* Delivery status. See dsn(3). The specified action is ignored.
129 /* .IP encoding
130 /* The body content encoding: MAIL_ATTR_ENC_{7BIT,8BIT,NONE}.
131 /* .IP smtputf8
132 /* The level of SMTPUTF8 support (to be defined).
133 /* .IP sender
134 /* The sender envelope address.
135 /* .IP dsn_envid
136 /* Optional DSN envelope ID.
137 /* .IP dsn_ret
138 /* Optional DSN return full/headers option.
139 /* .PP
140 /* For convenience, these functions always return a non-zero result.
141 /* DIAGNOSTICS
142 /* Warnings: problems connecting to the defer service.
143 /* Fatal: out of memory.
144 /* BUGS
145 /* Should be replaced by routines with an attribute-value based
146 /* interface instead of an interface that uses a rigid argument list.
147 /* LICENSE
148 /* .ad
149 /* .fi
150 /* The Secure Mailer license must be distributed with this software.
151 /* AUTHOR(S)
152 /* Wietse Venema
153 /* IBM T.J. Watson Research
154 /* P.O. Box 704
155 /* Yorktown Heights, NY 10598, USA
156 /*--*/
157 
158 /* System library. */
159 
160 #include <sys_defs.h>
161 #include <string.h>
162 
163 /* Utility library. */
164 
165 #include <msg.h>
166 #include <vstring.h>
167 
168 /* Global library. */
169 
170 #define DSN_INTERN
171 #include <mail_params.h>
172 #include <mail_queue.h>
173 #include <mail_proto.h>
174 #include <flush_clnt.h>
175 #include <verify.h>
176 #include <dsn_util.h>
177 #include <rcpt_print.h>
178 #include <dsn_print.h>
179 #include <log_adhoc.h>
180 #include <trace.h>
181 #include <defer.h>
182 
183 #define STR(x) vstring_str(x)
184 
185 /* defer_append - defer message delivery */
186 
187 int defer_append(int flags, const char *id, MSG_STATS *stats,
188  RECIPIENT *rcpt, const char *relay,
189  DSN *dsn)
190 {
191  DSN my_dsn = *dsn;
192  DSN *dsn_res;
193 
194  /*
195  * Sanity check.
196  */
197  if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) {
198  msg_warn("defer_append: ignoring dsn code \"%s\"", my_dsn.status);
199  my_dsn.status = "4.0.0";
200  }
201 
202  /*
203  * DSN filter (Postfix 3.0).
204  */
205  if (delivery_status_filter != 0
206  && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) {
207  if (dsn_res->status[0] == '5')
208  return (bounce_append_intern(flags, id, stats, rcpt, relay, dsn_res));
209  my_dsn = *dsn_res;
210  }
211  return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn));
212 }
213 
214 /* defer_append_intern - defer message delivery */
215 
216 int defer_append_intern(int flags, const char *id, MSG_STATS *stats,
217  RECIPIENT *rcpt, const char *relay,
218  DSN *dsn)
219 {
220  const char *rcpt_domain;
221  DSN my_dsn = *dsn;
222  int status;
223 
224  /*
225  * MTA-requested address verification information is stored in the verify
226  * service database.
227  */
228  if (flags & DEL_REQ_FLAG_MTA_VRFY) {
229  my_dsn.action = "undeliverable";
230  status = verify_append(id, stats, rcpt, relay, &my_dsn,
232  return (status);
233  }
234 
235  /*
236  * User-requested address verification information is logged and mailed
237  * to the requesting user.
238  */
239  if (flags & DEL_REQ_FLAG_USR_VRFY) {
240  my_dsn.action = "undeliverable";
241  status = trace_append(flags, id, stats, rcpt, relay, &my_dsn);
242  return (status);
243  }
244 
245  /*
246  * Normal mail delivery. May also send a delivery record to the user.
247  *
248  * XXX DSN We write all deferred recipients to the defer logfile regardless
249  * of DSN NOTIFY options, because those options don't apply to mailq(1)
250  * reports or to postmaster notifications.
251  */
252  else {
253 
254  /*
255  * Supply default action.
256  */
257  my_dsn.action = "delayed";
258 
263  SEND_ATTR_FUNC(rcpt_print, (void *) rcpt),
264  SEND_ATTR_FUNC(dsn_print, (void *) &my_dsn),
265  ATTR_TYPE_END) != 0)
266  msg_warn("%s: %s service failure", id, var_defer_service);
267  log_adhoc(id, stats, rcpt, relay, &my_dsn, "deferred");
268 
269  /*
270  * Traced delivery.
271  */
272  if (flags & DEL_REQ_FLAG_RECORD)
273  if (trace_append(flags, id, stats, rcpt, relay, &my_dsn) != 0)
274  msg_warn("%s: %s service failure", id, var_trace_service);
275 
276  /*
277  * Notify the fast flush service. XXX Should not this belong in the
278  * bounce/defer daemon? Well, doing it here is more robust.
279  */
280  if ((rcpt_domain = strrchr(rcpt->address, '@')) != 0
281  && *++rcpt_domain != 0)
282  switch (flush_add(rcpt_domain, id)) {
283  case FLUSH_STAT_OK:
284  case FLUSH_STAT_DENY:
285  break;
286  default:
287  msg_warn("%s: %s service failure", id, var_flush_service);
288  break;
289  }
290  return (-1);
291  }
292 }
293 
294 /* defer_flush - flush the defer log and deliver to the sender */
295 
296 int defer_flush(int flags, const char *queue, const char *id,
297  const char *encoding, int smtputf8,
298  const char *sender, const char *dsn_envid,
299  int dsn_ret)
300 {
301  flags |= BOUNCE_FLAG_DELRCPT;
302 
313  ATTR_TYPE_END) == 0) {
314  return (0);
315  } else {
316  return (-1);
317  }
318 }
319 
320 /* defer_warn - send a copy of the defer log to the sender as a warning bounce
321  * do not flush the log */
322 
323 int defer_warn(int flags, const char *queue, const char *id,
324  const char *encoding, int smtputf8,
325  const char *sender, const char *envid, int dsn_ret)
326 {
337  ATTR_TYPE_END) == 0) {
338  return (0);
339  } else {
340  return (-1);
341  }
342 }
343 
344 /* defer_one - defer mail for one recipient */
345 
346 int defer_one(int flags, const char *queue, const char *id,
347  const char *encoding, int smtputf8,
348  const char *sender, const char *dsn_envid,
349  int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt,
350  const char *relay, DSN *dsn)
351 {
352  DSN my_dsn = *dsn;
353  DSN *dsn_res;
354 
355  /*
356  * Sanity check.
357  */
358  if (my_dsn.status[0] != '4' || !dsn_valid(my_dsn.status)) {
359  msg_warn("defer_one: ignoring dsn code \"%s\"", my_dsn.status);
360  my_dsn.status = "4.0.0";
361  }
362 
363  /*
364  * DSN filter (Postfix 3.0).
365  */
366  if (delivery_status_filter != 0
367  && (dsn_res = dsn_filter_lookup(delivery_status_filter, &my_dsn)) != 0) {
368  if (dsn_res->status[0] == '5')
369  return (bounce_one_intern(flags, queue, id, encoding, smtputf8,
370  sender, dsn_envid, dsn_ret, stats,
371  rcpt, relay, dsn_res));
372  my_dsn = *dsn_res;
373  }
374  return (defer_append_intern(flags, id, stats, rcpt, relay, &my_dsn));
375 }
int dsn_print(ATTR_PRINT_MASTER_FN print_fn, VSTREAM *fp, int flags, void *ptr)
Definition: dsn_print.c:48
DSN_FILTER * delivery_status_filter
Definition: bounce.c:218
size_t dsn_valid(const char *text)
Definition: dsn_util.c:112
#define MAIL_ATTR_ENCODING
Definition: mail_proto.h:202
const char * address
const char * action
Definition: dsn.h:19
int trace_append(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: trace.c:106
#define ATTR_TYPE_END
Definition: attr.h:39
int defer_flush(int flags, const char *queue, const char *id, const char *encoding, int smtputf8, const char *sender, const char *dsn_envid, int dsn_ret)
Definition: defer.c:296
#define DEL_REQ_FLAG_USR_VRFY
#define BOUNCE_CMD_APPEND
Definition: bounce.h:43
#define MAIL_ATTR_QUEUE
Definition: mail_proto.h:129
int defer_one(int flags, const char *queue, const char *id, const char *encoding, int smtputf8, const char *sender, const char *dsn_envid, int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: defer.c:346
int bounce_append_intern(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: bounce.c:252
int flush_add(const char *site, const char *queue_id)
Definition: flush_clnt.c:225
int verify_append(const char *queue_id, MSG_STATS *stats, RECIPIENT *recipient, const char *relay, DSN *dsn, int vrfy_stat)
Definition: verify.c:92
#define MAIL_ATTR_NREQ
Definition: mail_proto.h:125
char * var_trace_service
Definition: mail_params.c:311
#define MAIL_ATTR_SMTPUTF8
Definition: mail_proto.h:277
void msg_warn(const char *fmt,...)
Definition: msg.c:215
#define MAIL_ATTR_DSN_RET
Definition: mail_proto.h:274
#define BOUNCE_CMD_WARN
Definition: bounce.h:45
#define FLUSH_STAT_DENY
Definition: flush_clnt.h:39
char * var_defer_service
Definition: mail_params.c:303
int mail_command_client(const char *class, const char *name,...)
#define MAIL_ATTR_DSN_ENVID
Definition: mail_proto.h:273
#define SEND_ATTR_INT(name, val)
Definition: attr.h:63
int defer_append_intern(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: defer.c:216
void log_adhoc(const char *id, MSG_STATS *stats, RECIPIENT *recipient, const char *relay, DSN *dsn, const char *status)
Definition: log_adhoc.c:82
const char * status
Definition: dsn.h:18
#define SEND_ATTR_FUNC(func, val)
Definition: attr.h:69
int bounce_one_intern(int flags, const char *queue, const char *id, const char *encoding, int smtputf8, const char *sender, const char *dsn_envid, int dsn_ret, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: bounce.c:444
Definition: dsn.h:17
#define BOUNCE_CMD_FLUSH
Definition: bounce.h:44
char * var_flush_service
Definition: mail_params.c:309
#define MAIL_ATTR_SENDER
Definition: mail_proto.h:131
int defer_append(int flags, const char *id, MSG_STATS *stats, RECIPIENT *rcpt, const char *relay, DSN *dsn)
Definition: defer.c:187
#define FLUSH_STAT_OK
Definition: flush_clnt.h:37
int rcpt_print(ATTR_PRINT_MASTER_FN print_fn, VSTREAM *fp, int flags, void *ptr)
Definition: rcpt_print.c:51
#define MAIL_ATTR_QUEUEID
Definition: mail_proto.h:130
#define DEL_RCPT_STAT_DEFER
#define BOUNCE_FLAG_DELRCPT
Definition: bounce.h:62
#define SEND_ATTR_STR(name, val)
Definition: attr.h:64
#define MAIL_ATTR_FLAGS
Definition: mail_proto.h:128
#define MAIL_CLASS_PRIVATE
Definition: mail_proto.h:96
#define DEL_REQ_FLAG_MTA_VRFY
#define DEL_REQ_FLAG_RECORD
DSN * dsn_filter_lookup(DSN_FILTER *fp, DSN *dsn)
Definition: dsn_filter.c:123
int defer_warn(int flags, const char *queue, const char *id, const char *encoding, int smtputf8, const char *sender, const char *envid, int dsn_ret)
Definition: defer.c:323