~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/global/defer.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-ooo6yfg6kmoteu04
Tags: upstream-2.1.3
ImportĀ upstreamĀ versionĀ 2.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, orig_rcpt, recipient, offset, relay,
 
10
/*                              entry, format, ...)
 
11
/*      int     flags;
 
12
/*      const char *id;
 
13
/*      const char *orig_rcpt;
 
14
/*      const char *recipient;
 
15
/*      long    offset;
 
16
/*      const char *relay;
 
17
/*      time_t  entry;
 
18
/*      const char *format;
 
19
/*
 
20
/*      int     vdefer_append(flags, id, orig_rcpt, recipient, offset, relay,
 
21
/*                              entry, format, ap)
 
22
/*      int     flags;
 
23
/*      const char *id;
 
24
/*      const char *orig_rcpt;
 
25
/*      const char *recipient;
 
26
/*      long    offset;
 
27
/*      const char *relay;
 
28
/*      time_t  entry;
 
29
/*      const char *format;
 
30
/*      va_list ap;
 
31
/*
 
32
/*      int     defer_flush(flags, queue, id, encoding, sender)
 
33
/*      int     flags;
 
34
/*      const char *queue;
 
35
/*      const char *id;
 
36
/*      const char *encoding;
 
37
/*      const char *sender;
 
38
/*
 
39
/*      int     defer_warn(flags, queue, id, sender)
 
40
/*      int     flags;
 
41
/*      const char *queue;
 
42
/*      const char *id;
 
43
/*      const char *sender;
 
44
/* DESCRIPTION
 
45
/*      This module implements a client interface to the defer service,
 
46
/*      which maintains a per-message logfile with status records for
 
47
/*      each recipient whose delivery is deferred, and the reason why.
 
48
/*
 
49
/*      defer_append() appends a record to the per-message defer log,
 
50
/*      with the reason for delayed delivery to the named recipient,
 
51
/*      updates the address verification service, or updates a message
 
52
/*      delivery record on request by the sender. The flags argument
 
53
/*      determines the action.
 
54
/*      The result is a convenient non-zero value.
 
55
/*      When the fast flush cache is enabled, the fast flush server is
 
56
/*      notified of deferred mail.
 
57
/*
 
58
/*      vdefer_append() implements an alternative client interface.
 
59
/*
 
60
/*      defer_flush() bounces the specified message to the specified
 
61
/*      sender, including the defer log that was built with defer_append().
 
62
/*      defer_flush() requests that the deferred recipients are deleted
 
63
/*      from the original queue file.
 
64
/*      The result is zero in case of success, non-zero otherwise.
 
65
/*
 
66
/*      defer_warn() sends a warning message that the mail in question has
 
67
/*      been deferred.  It does not flush the log.
 
68
/*
 
69
/*      Arguments:
 
70
/* .IP flags
 
71
/*      The bit-wise OR of zero or more of the following (specify
 
72
/*      BOUNCE_FLAG_NONE to explicitly request not special processing):
 
73
/* .RS
 
74
/* .IP BOUNCE_FLAG_CLEAN
 
75
/*      Delete the defer log in case of an error (as in: pretend
 
76
/*      that we never even tried to defer this message).
 
77
/* .IP DEL_REQ_FLAG_VERIFY
 
78
/*      The message is an address verification probe. Update the
 
79
/*      address verification database instead of deferring mail.
 
80
/* .IP DEL_REQ_FLAG_EXPAND
 
81
/*      The message is an address expansion probe. Update the
 
82
/*      the message delivery record instead of deferring mail.
 
83
/* .IP DEL_REQ_FLAG_RECORD
 
84
/*      This is a normal message with logged delivery. Update the
 
85
/*      message delivery record and defer mail delivery.
 
86
/* .RE
 
87
/* .IP queue
 
88
/*      The message queue name of the original message file.
 
89
/* .IP id
 
90
/*      The queue id of the original message file.
 
91
/* .IP orig_rcpt
 
92
/*      The original envelope recipient address. If unavailable,
 
93
/*      specify a null string or null pointer.
 
94
/* .IP recipient
 
95
/*      A recipient address that is being deferred. The domain part
 
96
/*      of the address is marked dead (for a limited amount of time).
 
97
/* .IP offset
 
98
/*      Queue file offset of recipient record.
 
99
/* .IP encoding
 
100
/*      The body content encoding: MAIL_ATTR_ENC_{7BIT,8BIT,NONE}.
 
101
/* .IP sender
 
102
/*      The sender envelope address.
 
103
/* .IP relay
 
104
/*      Host we could not talk to.
 
105
/* .IP entry
 
106
/*      Message arrival time.
 
107
/* .IP format
 
108
/*      The reason for non-delivery.
 
109
/* .IP ap
 
110
/*      Variable-length argument list.
 
111
/* .PP
 
112
/*      For convenience, these functions always return a non-zero result.
 
113
/* DIAGNOSTICS
 
114
/*      Warnings: problems connecting to the defer service.
 
115
/*      Fatal: out of memory.
 
116
/* BUGS
 
117
/*      Should be replaced by routines with an attribute-value based
 
118
/*      interface instead of an interface that uses a rigid argument list.
 
119
/* LICENSE
 
120
/* .ad
 
121
/* .fi
 
122
/*      The Secure Mailer license must be distributed with this software.
 
123
/* AUTHOR(S)
 
124
/*      Wietse Venema
 
125
/*      IBM T.J. Watson Research
 
126
/*      P.O. Box 704
 
127
/*      Yorktown Heights, NY 10598, USA
 
128
/*--*/
 
129
 
 
130
/* System library. */
 
131
 
 
132
#include <sys_defs.h>
 
133
#include <stdlib.h>                     /* 44BSD stdarg.h uses abort() */
 
134
#include <stdarg.h>
 
135
#include <string.h>
 
136
 
 
137
#ifdef STRCASECMP_IN_STRINGS_H
 
138
#include <strings.h>
 
139
#endif
 
140
 
 
141
/* Utility library. */
 
142
 
 
143
#include <msg.h>
 
144
#include <vstring.h>
 
145
 
 
146
/* Global library. */
 
147
 
 
148
#include <mail_params.h>
 
149
#include <mail_queue.h>
 
150
#include <mail_proto.h>
 
151
#include <flush_clnt.h>
 
152
#include <verify.h>
 
153
#include <log_adhoc.h>
 
154
#include <trace.h>
 
155
#include <bounce.h>
 
156
#include <defer.h>
 
157
 
 
158
#define STR(x)  vstring_str(x)
 
159
 
 
160
/* defer_append - defer message delivery */
 
161
 
 
162
int     defer_append(int flags, const char *id, const char *orig_rcpt,
 
163
                      const char *recipient, long offset, const char *relay,
 
164
                             time_t entry, const char *fmt,...)
 
165
{
 
166
    va_list ap;
 
167
    int     status;
 
168
 
 
169
    va_start(ap, fmt);
 
170
    status = vdefer_append(flags, id, orig_rcpt, recipient,
 
171
                           offset, relay, entry, fmt, ap);
 
172
    va_end(ap);
 
173
    return (status);
 
174
}
 
175
 
 
176
/* vdefer_append - defer delivery of queue file */
 
177
 
 
178
int     vdefer_append(int flags, const char *id, const char *orig_rcpt,
 
179
                      const char *recipient, long offset, const char *relay,
 
180
                              time_t entry, const char *fmt, va_list ap)
 
181
{
 
182
    const char *rcpt_domain;
 
183
    int     status;
 
184
 
 
185
    /*
 
186
     * MTA-requested address verification information is stored in the verify
 
187
     * service database.
 
188
     */
 
189
    if (flags & DEL_REQ_FLAG_VERIFY) {
 
190
        status = vverify_append(id, orig_rcpt, recipient, relay, entry,
 
191
                             "undeliverable", DEL_RCPT_STAT_DEFER, fmt, ap);
 
192
        return (status);
 
193
    }
 
194
 
 
195
    /*
 
196
     * User-requested address verification information is logged and mailed
 
197
     * to the requesting user.
 
198
     */
 
199
    if (flags & DEL_REQ_FLAG_EXPAND) {
 
200
        status = vtrace_append(flags, id, orig_rcpt, recipient, relay,
 
201
                               entry, "4.0.0", "undeliverable", fmt, ap);
 
202
        return (status);
 
203
    }
 
204
 
 
205
    /*
 
206
     * Normal mail delivery. May also send a delivery record to the user.
 
207
     */
 
208
    else {
 
209
        VSTRING *why = vstring_alloc(100);
 
210
 
 
211
        vstring_vsprintf(why, fmt, ap);
 
212
 
 
213
        if (orig_rcpt == 0)
 
214
            orig_rcpt = "";
 
215
        if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
 
216
                           ATTR_TYPE_NUM, MAIL_ATTR_NREQ, BOUNCE_CMD_APPEND,
 
217
                                ATTR_TYPE_NUM, MAIL_ATTR_FLAGS, flags,
 
218
                                ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
 
219
                                ATTR_TYPE_STR, MAIL_ATTR_ORCPT, orig_rcpt,
 
220
                                ATTR_TYPE_STR, MAIL_ATTR_RECIP, recipient,
 
221
                                ATTR_TYPE_LONG, MAIL_ATTR_OFFSET, offset,
 
222
                                ATTR_TYPE_STR, MAIL_ATTR_STATUS, "4.0.0",
 
223
                                ATTR_TYPE_STR, MAIL_ATTR_ACTION, "delayed",
 
224
                             ATTR_TYPE_STR, MAIL_ATTR_WHY, vstring_str(why),
 
225
                                ATTR_TYPE_END) != 0)
 
226
            msg_warn("%s: %s service failure", id, var_defer_service);
 
227
        log_adhoc(id, orig_rcpt, recipient, relay, entry, "deferred",
 
228
                  "%s", vstring_str(why));
 
229
 
 
230
        /*
 
231
         * Traced delivery.
 
232
         */
 
233
        if (flags & DEL_REQ_FLAG_RECORD)
 
234
            if (trace_append(flags, id, orig_rcpt, recipient, relay,
 
235
                             entry, "4.0.0", "deferred",
 
236
                             "%s", vstring_str(why)) != 0)
 
237
                msg_warn("%s: %s service failure", id, var_trace_service);
 
238
 
 
239
        /*
 
240
         * Notify the fast flush service. XXX Should not this belong in the
 
241
         * bounce/defer daemon? Well, doing it here is more robust.
 
242
         */
 
243
        if ((rcpt_domain = strrchr(recipient, '@')) != 0 && *++rcpt_domain != 0)
 
244
            switch (flush_add(rcpt_domain, id)) {
 
245
            case FLUSH_STAT_OK:
 
246
            case FLUSH_STAT_DENY:
 
247
                break;
 
248
            default:
 
249
                msg_warn("%s: %s service failure", id, var_flush_service);
 
250
                break;
 
251
            }
 
252
        vstring_free(why);
 
253
        return (-1);
 
254
    }
 
255
}
 
256
 
 
257
/* defer_flush - flush the defer log and deliver to the sender */
 
258
 
 
259
int     defer_flush(int flags, const char *queue, const char *id,
 
260
                            const char *encoding, const char *sender)
 
261
{
 
262
    flags |= BOUNCE_FLAG_DELRCPT;
 
263
 
 
264
    if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
 
265
                            ATTR_TYPE_NUM, MAIL_ATTR_NREQ, BOUNCE_CMD_FLUSH,
 
266
                            ATTR_TYPE_NUM, MAIL_ATTR_FLAGS, flags,
 
267
                            ATTR_TYPE_STR, MAIL_ATTR_QUEUE, queue,
 
268
                            ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
 
269
                            ATTR_TYPE_STR, MAIL_ATTR_ENCODING, encoding,
 
270
                            ATTR_TYPE_STR, MAIL_ATTR_SENDER, sender,
 
271
                            ATTR_TYPE_END) == 0) {
 
272
        return (0);
 
273
    } else {
 
274
        return (-1);
 
275
    }
 
276
}
 
277
 
 
278
/* defer_warn - send a copy of the defer log to the sender as a warning bounce
 
279
 * do not flush the log */
 
280
 
 
281
int     defer_warn(int flags, const char *queue, const char *id,
 
282
                           const char *sender)
 
283
{
 
284
    if (mail_command_client(MAIL_CLASS_PRIVATE, var_defer_service,
 
285
                            ATTR_TYPE_NUM, MAIL_ATTR_NREQ, BOUNCE_CMD_WARN,
 
286
                            ATTR_TYPE_NUM, MAIL_ATTR_FLAGS, flags,
 
287
                            ATTR_TYPE_STR, MAIL_ATTR_QUEUE, queue,
 
288
                            ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, id,
 
289
                            ATTR_TYPE_STR, MAIL_ATTR_SENDER, sender,
 
290
                            ATTR_TYPE_END) == 0) {
 
291
        return (0);
 
292
    } else {
 
293
        return (-1);
 
294
    }
 
295
}