7
/* #include <bounce_log.h>
11
/* /* public members... */
12
/* const char *recipient;
13
/* const char *orig_rcpt;
15
/* const char *dsn_status;
16
/* const char *dsn_action;
21
/* BOUNCE_LOG *bounce_log_open(queue, id, flags, mode)
27
/* BOUNCE_LOG *bounce_log_read(bp)
30
/* void bounce_log_rewind(bp)
33
/* BOUNCE_LOG *bounce_log_forge(orig_rcpt, recipient, rcpt_offset,
34
/* dsn_status, dsn_action, why)
35
/* const char *orig_rcpt;
36
/* const char *recipient;
38
/* const char *dsn_status;
39
/* const char *dsn_action;
42
/* void bounce_log_close(bp)
45
/* This module implements a bounce/defer logfile API. Information
46
/* is sanitized for control and non-ASCII characters.
48
/* bounce_log_open() opens the named bounce or defer logfile
49
/* and returns a handle that must be used for further access.
50
/* The result is a null pointer if the file cannot be opened.
51
/* The caller is expected to inspect the errno code and deal
54
/* bounce_log_read() reads the next record from the bounce or defer
55
/* logfile (skipping over and warning about malformed data)
56
/* and breaks out the recipient address, the recipient status
57
/* and the text that explains why the recipient was undeliverable.
58
/* bounce_log_read() returns a null pointer when no recipient was read,
59
/* otherwise it returns its argument.
61
/* bounce_log_rewind() is a helper that seeks to the first recipient
62
/* in an open bounce or defer logfile (skipping over recipients that
63
/* are marked as done). The result is 0 in case of success, -1 in case
66
/* bounce_log_forge() forges one recipient status record
67
/* without actually accessing a logfile.
68
/* The result cannot be used for any logfile access operation
69
/* and must be disposed of by passing it to bounce_log_close().
71
/* bounce_log_close() closes an open bounce or defer logfile and
72
/* releases memory for the specified handle. The result is non-zero
73
/* in case of I/O errors.
77
/* The bounce or defer queue name.
79
/* The message queue id of bounce or defer logfile. This
80
/* file has the same name as the original message file.
82
/* File open flags, as with open(2).
84
/* File permissions, as with open(2).
88
/* The final recipient address in RFC 822 external form, or <>
89
/* in case of the null recipient address.
91
/* Null pointer or the original recipient address in RFC 822
94
/* Queue file offset of recipient record.
96
/* The text that explains why the recipient was undeliverable.
98
/* String with DSN compatible status code (digit.digit.digit).
100
/* "delivered", "failed", "delayed" and so on.
102
/* Other fields will be added as the code evolves.
106
/* The Secure Mailer license must be distributed with this software.
109
/* IBM T.J. Watson Research
111
/* Yorktown Heights, NY 10598, USA
114
/* System library. */
116
#include <sys_defs.h>
122
/* Utility library. */
125
#include <mymalloc.h>
128
#include <vstring_vstream.h>
129
#include <stringops.h>
131
/* Global library. */
133
#include <mail_params.h>
134
#include <mail_proto.h>
135
#include <mail_queue.h>
136
#include <bounce_log.h>
138
/* Application-specific. */
140
#define STR(x) vstring_str(x)
142
/* bounce_log_init - initialize structure */
144
static BOUNCE_LOG *bounce_log_init(VSTREAM *fp,
150
const char *compat_status,
152
const char *compat_action,
157
#define SET_BUFFER(bp, buf, str) { \
159
bp->str = (buf && STR(buf)[0] ? STR(buf) : 0); \
162
bp = (BOUNCE_LOG *) mymalloc(sizeof(*bp));
165
SET_BUFFER(bp, orcp_buf, orig_rcpt);
166
SET_BUFFER(bp, rcpt_buf, recipient);
167
bp->rcpt_offset = rcpt_offset;
168
SET_BUFFER(bp, status_buf, dsn_status);
169
bp->compat_status = compat_status;
170
SET_BUFFER(bp, action_buf, dsn_action);
171
bp->compat_action = compat_action;
172
SET_BUFFER(bp, text_buf, text);
176
/* bounce_log_open - open bounce read stream */
178
BOUNCE_LOG *bounce_log_open(const char *queue_name, const char *queue_id,
183
#define STREQ(x,y) (strcmp((x),(y)) == 0)
184
#define SAVE_TO_VSTRING(s) vstring_strcpy(vstring_alloc(10), (s))
187
* TODO: peek at the first byte to see if this is an old-style log
188
* (<recipient>: text) or a new-style extensible log with multiple
189
* attributes per recipient. That would not help during a transition from
190
* old to new style, where one can expect to find mixed format files.
192
* Kluge up default DSN status and action for old-style logfiles.
194
if ((fp = mail_queue_open(queue_name, queue_id, flags, mode)) == 0) {
197
return (bounce_log_init(fp, /* stream */
198
vstring_alloc(100), /* buffer */
199
vstring_alloc(10), /* orig_rcpt */
200
vstring_alloc(10), /* recipient */
201
(long) 0, /* offset */
202
vstring_alloc(10), /* dsn_status */
203
STREQ(queue_name, MAIL_QUEUE_DEFER) ?
204
"4.0.0" : "5.0.0", /* compatibility */
205
vstring_alloc(10), /* dsn_action */
206
STREQ(queue_name, MAIL_QUEUE_DEFER) ?
207
"delayed" : "failed", /* compatibility */
208
vstring_alloc(10))); /* text */
212
/* bounce_log_read - read one record from bounce log file */
214
BOUNCE_LOG *bounce_log_read(BOUNCE_LOG *bp)
222
* Our trivial logfile parser state machine.
224
#define START 0 /* still searching */
225
#define FOUND 1 /* in logfile entry */
231
bp->recipient = "(unavailable)";
234
bp->dsn_status = "(unavailable)";
235
bp->dsn_action = "(unavailable)";
236
bp->text = "(unavailable)";
239
* Support mixed logfile formats to make transitions easier. The same
240
* file can start with old-style records and end with new-style records.
241
* With backwards compatibility, we even have old format followed by new
242
* format within the same logfile entry!
244
while ((vstring_get_nonl(bp->buf, bp->fp) != VSTREAM_EOF)) {
247
* Logfile entries are separated by blank lines. Even the old ad-hoc
248
* logfile format has a blank line after the last record. This means
249
* we can safely use blank lines to detect the start and end of
252
if (STR(bp->buf)[0] == 0) {
260
* Sanitize. XXX This needs to be done more carefully with new-style
263
cp = printable(STR(bp->buf), '?');
269
* New style logfile entries are in "name = value" format.
277
* Split into name and value.
279
if ((err = split_nameval(cp, &name, &value)) != 0) {
280
msg_warn("%s: malformed record: %s", VSTREAM_PATH(bp->fp), err);
285
* Save attribute value.
287
if (STREQ(name, MAIL_ATTR_RECIP)) {
288
bp->recipient = STR(vstring_strcpy(bp->rcpt_buf, *value ?
289
value : "(MAILER-DAEMON)"));
290
} else if (STREQ(name, MAIL_ATTR_ORCPT)) {
291
bp->orig_rcpt = STR(vstring_strcpy(bp->orcp_buf, *value ?
292
value : "(MAILER-DAEMON)"));
293
} else if (STREQ(name, MAIL_ATTR_OFFSET)) {
294
bp->rcpt_offset = atol(value);
295
} else if (STREQ(name, MAIL_ATTR_STATUS)) {
296
bp->dsn_status = STR(vstring_strcpy(bp->status_buf, value));
297
} else if (STREQ(name, MAIL_ATTR_ACTION)) {
298
bp->dsn_action = STR(vstring_strcpy(bp->action_buf, value));
299
} else if (STREQ(name, MAIL_ATTR_WHY)) {
300
bp->text = STR(vstring_strcpy(bp->text_buf, value));
302
msg_warn("%s: unknown attribute name: %s, ignored",
303
VSTREAM_PATH(bp->fp), name);
309
* Old-style logfile record. Find the recipient address.
312
msg_warn("%s: malformed record: %.30s...",
313
VSTREAM_PATH(bp->fp), cp);
317
if ((cp = strstr(recipient, ">: ")) == 0) {
318
msg_warn("%s: malformed record: %.30s...",
319
VSTREAM_PATH(bp->fp), cp);
323
vstring_strcpy(bp->rcpt_buf, *recipient ?
324
recipient : "(MAILER-DAEMON)");
325
bp->recipient = STR(bp->rcpt_buf);
328
* Find the text that explains why mail was not deliverable.
331
while (*text && ISSPACE(*text))
333
vstring_strcpy(bp->text_buf, text);
334
bp->text = STR(bp->text_buf);
337
* Add compatibility status and action info, to make up for data that
338
* was not stored in old-style bounce logfiles.
340
bp->dsn_status = bp->compat_status;
341
bp->dsn_action = bp->compat_action;
346
/* bounce_log_forge - forge one recipient status record */
348
BOUNCE_LOG *bounce_log_forge(const char *orig_rcpt, const char *recipient,
349
long rcpt_offset, const char *dsn_status,
350
const char *dsn_action, const char *text)
352
return (bounce_log_init((VSTREAM *) 0,
354
SAVE_TO_VSTRING(orig_rcpt),
355
SAVE_TO_VSTRING(recipient),
357
SAVE_TO_VSTRING(dsn_status),
359
SAVE_TO_VSTRING(dsn_action),
361
SAVE_TO_VSTRING(text)));
364
/* bounce_log_close - close bounce reader stream */
366
int bounce_log_close(BOUNCE_LOG *bp)
371
ret = vstream_fclose(bp->fp);
375
vstring_free(bp->buf);
377
vstring_free(bp->rcpt_buf);
379
vstring_free(bp->orcp_buf);
381
vstring_free(bp->status_buf);
383
vstring_free(bp->action_buf);
385
vstring_free(bp->text_buf);