9 /* #include <bounce_log.h>
13 /* /* No public members. */
17 /* BOUNCE_LOG *bounce_log_open(queue, id, flags, mode)
23 /* BOUNCE_LOG *bounce_log_read(bp, rcpt, dsn)
28 /* void bounce_log_rewind(bp)
31 /* void bounce_log_close(bp)
34 /* This module implements a bounce/defer logfile API. Information
35 /* is sanitized for control and non-ASCII characters. Fields not
36 /* present in input are represented by empty strings.
38 /* bounce_log_open() opens the named bounce or defer logfile
39 /* and returns a handle that must be used for further access.
40 /* The result is a null pointer if the file cannot be opened.
41 /* The caller is expected to inspect the errno code and deal
44 /* bounce_log_read() reads the next record from the bounce or defer
45 /* logfile (skipping over and warning about malformed data)
46 /* and breaks out the recipient address, the recipient status
47 /* and the text that explains why the recipient was undeliverable.
48 /* bounce_log_read() returns a null pointer when no recipient was read,
49 /* otherwise it returns its argument.
51 /* bounce_log_rewind() is a helper that seeks to the first recipient
52 /* in an open bounce or defer logfile (skipping over recipients that
53 /* are marked as done). The result is 0 in case of success, -1 in case
56 /* bounce_log_close() closes an open bounce or defer logfile and
57 /* releases memory for the specified handle. The result is non-zero
58 /* in case of I/O errors.
62 /* The bounce or defer queue name.
64 /* The message queue id of bounce or defer logfile. This
65 /* file has the same name as the original message file.
67 /* File open flags, as with open(2).
69 /* File permissions, as with open(2).
71 /* Recipient buffer. The RECIPIENT member is updated.
73 /* Delivery status information. The DSN member is updated.
77 /* The Secure Mailer license must be distributed with this software.
80 /* IBM T.J. Watson Research
82 /* Yorktown Heights, NY 10598, USA
93 /* Utility library. */
99 #include <vstring_vstream.h>
100 #include <stringops.h>
102 /* Global library. */
104 #include <mail_params.h>
105 #include <mail_proto.h>
106 #include <mail_queue.h>
107 #include <dsn_mask.h>
108 #include <bounce_log.h>
110 /* Application-specific. */
112 #define STR(x) vstring_str(x)
114 /* bounce_log_open - open bounce read stream */
116 BOUNCE_LOG
*bounce_log_open(const char *queue_name
, const char *queue_id
,
117 int flags
, mode_t mode
)
122 #define STREQ(x,y) (strcmp((x),(y)) == 0)
125 * Logfiles may contain a mixture of old-style (<recipient>: text) and
126 * new-style entries with multiple attributes per recipient.
128 * Kluge up default DSN status and action for old-style logfiles.
130 if ((fp
= mail_queue_open(queue_name
, queue_id
, flags
, mode
)) == 0) {
133 bp
= (BOUNCE_LOG
*) mymalloc(sizeof(*bp
));
135 bp
->buf
= vstring_alloc(100);
136 if (STREQ(queue_name
, MAIL_QUEUE_DEFER
)) {
137 bp
->compat_status
= mystrdup("4.0.0");
138 bp
->compat_action
= mystrdup("delayed");
140 bp
->compat_status
= mystrdup("5.0.0");
141 bp
->compat_action
= mystrdup("failed");
147 /* bounce_log_read - read one record from bounce log file */
149 BOUNCE_LOG
*bounce_log_read(BOUNCE_LOG
*bp
, RCPT_BUF
*rcpt_buf
,
158 * Our trivial logfile parser state machine.
160 #define START 0 /* still searching */
161 #define FOUND 1 /* in logfile entry */
167 rcpb_reset(rcpt_buf
);
171 * Support mixed logfile formats to make migration easier. The same file
172 * can start with old-style records and end with new-style records. With
173 * backwards compatibility, we even have old format followed by new
174 * format within the same logfile entry!
177 if ((vstring_get_nonl(bp
->buf
, bp
->fp
) == VSTREAM_EOF
))
181 * Logfile entries are separated by blank lines. Even the old ad-hoc
182 * logfile format has a blank line after the last record. This means
183 * we can safely use blank lines to detect the start and end of
186 if (STR(bp
->buf
)[0] == 0) {
194 * Sanitize. XXX This needs to be done more carefully with new-style
197 cp
= printable(STR(bp
->buf
), '?');
203 * New style logfile entries are in "name = value" format.
213 * Split into name and value.
215 if ((err
= split_nameval(cp
, &name
, &value
)) != 0) {
216 msg_warn("%s: malformed record: %s", VSTREAM_PATH(bp
->fp
), err
);
221 * Save attribute value.
223 if (STREQ(name
, MAIL_ATTR_RECIP
)) {
224 vstring_strcpy(rcpt_buf
->address
, *value
?
225 value
: "(MAILER-DAEMON)");
226 } else if (STREQ(name
, MAIL_ATTR_ORCPT
)) {
227 vstring_strcpy(rcpt_buf
->orig_addr
, *value
?
228 value
: "(MAILER-DAEMON)");
229 } else if (STREQ(name
, MAIL_ATTR_DSN_ORCPT
)) {
230 vstring_strcpy(rcpt_buf
->dsn_orcpt
, value
);
231 } else if (STREQ(name
, MAIL_ATTR_DSN_NOTIFY
)) {
232 if ((notify
= atoi(value
)) > 0 && DSN_NOTIFY_OK(notify
))
233 rcpt_buf
->dsn_notify
= notify
;
234 } else if (STREQ(name
, MAIL_ATTR_OFFSET
)) {
235 if ((offset
= atol(value
)) > 0)
236 rcpt_buf
->offset
= offset
;
237 } else if (STREQ(name
, MAIL_ATTR_DSN_STATUS
)) {
238 vstring_strcpy(dsn_buf
->status
, value
);
239 } else if (STREQ(name
, MAIL_ATTR_DSN_ACTION
)) {
240 vstring_strcpy(dsn_buf
->action
, value
);
241 } else if (STREQ(name
, MAIL_ATTR_DSN_DTYPE
)) {
242 vstring_strcpy(dsn_buf
->dtype
, value
);
243 } else if (STREQ(name
, MAIL_ATTR_DSN_DTEXT
)) {
244 vstring_strcpy(dsn_buf
->dtext
, value
);
245 } else if (STREQ(name
, MAIL_ATTR_DSN_MTYPE
)) {
246 vstring_strcpy(dsn_buf
->mtype
, value
);
247 } else if (STREQ(name
, MAIL_ATTR_DSN_MNAME
)) {
248 vstring_strcpy(dsn_buf
->mname
, value
);
249 } else if (STREQ(name
, MAIL_ATTR_WHY
)) {
250 vstring_strcpy(dsn_buf
->reason
, value
);
252 msg_warn("%s: unknown attribute name: %s, ignored",
253 VSTREAM_PATH(bp
->fp
), name
);
259 * Old-style logfile record. Find the recipient address.
262 msg_warn("%s: malformed record: %.30s...",
263 VSTREAM_PATH(bp
->fp
), cp
);
267 if ((cp
= strstr(recipient
, ">: ")) == 0) {
268 msg_warn("%s: malformed record: %.30s...",
269 VSTREAM_PATH(bp
->fp
), cp
);
273 vstring_strcpy(rcpt_buf
->address
, *recipient
?
274 recipient
: "(MAILER-DAEMON)");
277 * Find the text that explains why mail was not deliverable.
280 while (*text
&& ISSPACE(*text
))
282 vstring_strcpy(dsn_buf
->reason
, text
);
286 * Specify place holders for missing fields. See also DSN_FROM_DSN_BUF()
287 * and RECIPIENT_FROM_RCPT_BUF() for null and non-null fields.
289 #define BUF_NODATA(buf) (STR(buf)[0] == 0)
290 #define BUF_ASSIGN(buf, text) vstring_strcpy((buf), (text))
292 if (BUF_NODATA(rcpt_buf
->address
))
293 BUF_ASSIGN(rcpt_buf
->address
, "(recipient address unavailable)");
294 if (BUF_NODATA(dsn_buf
->status
))
295 BUF_ASSIGN(dsn_buf
->status
, bp
->compat_status
);
296 if (BUF_NODATA(dsn_buf
->action
))
297 BUF_ASSIGN(dsn_buf
->action
, bp
->compat_action
);
298 if (BUF_NODATA(dsn_buf
->reason
))
299 BUF_ASSIGN(dsn_buf
->reason
, "(description unavailable)");
300 (void) RECIPIENT_FROM_RCPT_BUF(rcpt_buf
);
301 (void) DSN_FROM_DSN_BUF(dsn_buf
);
305 /* bounce_log_close - close bounce reader stream */
307 int bounce_log_close(BOUNCE_LOG
*bp
)
311 ret
= vstream_fclose(bp
->fp
);
312 vstring_free(bp
->buf
);
313 myfree(bp
->compat_status
);
314 myfree(bp
->compat_action
);