xref: /netbsd-src/external/ibm-public/postfix/dist/src/local/forward.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: forward.c,v 1.1.1.3 2013/09/25 19:06:31 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	forward 3
6 /* SUMMARY
7 /*	message forwarding
8 /* SYNOPSIS
9 /*	#include "local.h"
10 /*
11 /*	int	forward_init()
12 /*
13 /*	int	forward_append(attr)
14 /*	DELIVER_ATTR attr;
15 /*
16 /*	int	forward_finish(request, attr, cancel)
17 /*	DELIVER_REQUEST *request;
18 /*	DELIVER_ATTR attr;
19 /*	int	cancel;
20 /* DESCRIPTION
21 /*	This module implements the client interface for message
22 /*	forwarding.
23 /*
24 /*	forward_init() initializes internal data structures.
25 /*
26 /*	forward_append() appends a recipient to the list of recipients
27 /*	that will receive a message with the specified message sender
28 /*	and delivered-to addresses.
29 /*
30 /*	forward_finish() forwards the actual message contents and
31 /*	releases the memory allocated by forward_init() and by
32 /*	forward_append(). When the \fIcancel\fR argument is true, no
33 /*	messages will be forwarded. The \fIattr\fR argument specifies
34 /*	the original message delivery attributes as they were before
35 /*	alias or forward expansions.
36 /* DIAGNOSTICS
37 /*	A non-zero result means that the requested operation should
38 /*	be tried again.
39 /*	Warnings: problems connecting to the forwarding service,
40 /*	corrupt message file. A corrupt message is saved to the
41 /*	"corrupt" queue for further inspection.
42 /*	Fatal: out of memory.
43 /*	Panic: missing forward_init() or forward_finish() call.
44 /* LICENSE
45 /* .ad
46 /* .fi
47 /*	The Secure Mailer license must be distributed with this software.
48 /* AUTHOR(S)
49 /*	Wietse Venema
50 /*	IBM T.J. Watson Research
51 /*	P.O. Box 704
52 /*	Yorktown Heights, NY 10598, USA
53 /*--*/
54 
55 /* System library. */
56 
57 #include <sys_defs.h>
58 #include <sys/time.h>
59 #include <unistd.h>
60 
61 /* Utility library. */
62 
63 #include <msg.h>
64 #include <mymalloc.h>
65 #include <htable.h>
66 #include <argv.h>
67 #include <vstring.h>
68 #include <vstream.h>
69 #include <vstring_vstream.h>
70 #include <iostuff.h>
71 #include <stringops.h>
72 
73 /* Global library. */
74 
75 #include <mail_proto.h>
76 #include <cleanup_user.h>
77 #include <sent.h>
78 #include <record.h>
79 #include <rec_type.h>
80 #include <mark_corrupt.h>
81 #include <mail_date.h>
82 #include <mail_params.h>
83 #include <dsn_mask.h>
84 
85 /* Application-specific. */
86 
87 #include "local.h"
88 
89  /*
90   * Use one cleanup service connection for each (delivered to, sender) pair.
91   */
92 static HTABLE *forward_dt;
93 
94 typedef struct FORWARD_INFO {
95     VSTREAM *cleanup;			/* clean up service handle */
96     char   *queue_id;			/* forwarded message queue id */
97     struct timeval posting_time;	/* posting time */
98 } FORWARD_INFO;
99 
100 /* forward_init - prepare for forwarding */
101 
102 int     forward_init(void)
103 {
104 
105     /*
106      * Sanity checks.
107      */
108     if (forward_dt != 0)
109 	msg_panic("forward_init: missing forward_finish call");
110 
111     forward_dt = htable_create(0);
112     return (0);
113 }
114 
115 /* forward_open - open connection to cleanup service */
116 
117 static FORWARD_INFO *forward_open(DELIVER_REQUEST *request, const char *sender)
118 {
119     VSTRING *buffer = vstring_alloc(100);
120     FORWARD_INFO *info;
121     VSTREAM *cleanup;
122 
123 #define FORWARD_OPEN_RETURN(res) do { \
124 	vstring_free(buffer); \
125 	return (res); \
126     } while (0)
127 
128     /*
129      * Contact the cleanup service and save the new mail queue id. Request
130      * that the cleanup service bounces bad messages to the sender so that we
131      * can avoid the trouble of bounce management.
132      *
133      * In case you wonder what kind of bounces, examples are "too many hops",
134      * "message too large", perhaps some others. The reason not to bounce
135      * ourselves is that we don't really know who the recipients are.
136      */
137     cleanup = mail_connect(MAIL_CLASS_PUBLIC, var_cleanup_service, BLOCKING);
138     if (cleanup == 0)
139 	FORWARD_OPEN_RETURN(0);
140     close_on_exec(vstream_fileno(cleanup), CLOSE_ON_EXEC);
141     if (attr_scan(cleanup, ATTR_FLAG_STRICT,
142 		  ATTR_TYPE_STR, MAIL_ATTR_QUEUEID, buffer,
143 		  ATTR_TYPE_END) != 1) {
144 	vstream_fclose(cleanup);
145 	FORWARD_OPEN_RETURN(0);
146     }
147     info = (FORWARD_INFO *) mymalloc(sizeof(FORWARD_INFO));
148     info->cleanup = cleanup;
149     info->queue_id = mystrdup(STR(buffer));
150     GETTIMEOFDAY(&info->posting_time);
151 
152 #define FORWARD_CLEANUP_FLAGS (CLEANUP_FLAG_BOUNCE | CLEANUP_FLAG_MASK_INTERNAL)
153 
154     attr_print(cleanup, ATTR_FLAG_NONE,
155 	       ATTR_TYPE_INT, MAIL_ATTR_FLAGS, FORWARD_CLEANUP_FLAGS,
156 	       ATTR_TYPE_END);
157 
158     /*
159      * Send initial message envelope information. For bounces, set the
160      * designated sender: mailing list owner, posting user, whatever.
161      */
162     rec_fprintf(cleanup, REC_TYPE_TIME, REC_TYPE_TIME_FORMAT,
163 		REC_TYPE_TIME_ARG(info->posting_time));
164     rec_fputs(cleanup, REC_TYPE_FROM, sender);
165 
166     /*
167      * Don't send the original envelope ID or full/headers return mask if it
168      * was reset due to mailing list expansion.
169      */
170     if (request->dsn_ret)
171 	rec_fprintf(cleanup, REC_TYPE_ATTR, "%s=%d",
172 		    MAIL_ATTR_DSN_RET, request->dsn_ret);
173     if (request->dsn_envid && *(request->dsn_envid))
174 	rec_fprintf(cleanup, REC_TYPE_ATTR, "%s=%s",
175 		    MAIL_ATTR_DSN_ENVID, request->dsn_envid);
176 
177     /*
178      * Zero-length attribute values are place holders for unavailable
179      * attribute values. See qmgr_message.c. They are not meant to be
180      * propagated to queue files.
181      */
182 #define PASS_ATTR(fp, name, value) do { \
183     if ((value) && *(value)) \
184 	rec_fprintf((fp), REC_TYPE_ATTR, "%s=%s", (name), (value)); \
185     } while (0)
186 
187     /*
188      * XXX encapsulate these as one object.
189      */
190     PASS_ATTR(cleanup, MAIL_ATTR_LOG_CLIENT_NAME, request->client_name);
191     PASS_ATTR(cleanup, MAIL_ATTR_LOG_CLIENT_ADDR, request->client_addr);
192     PASS_ATTR(cleanup, MAIL_ATTR_LOG_PROTO_NAME, request->client_proto);
193     PASS_ATTR(cleanup, MAIL_ATTR_LOG_HELO_NAME, request->client_helo);
194     PASS_ATTR(cleanup, MAIL_ATTR_SASL_METHOD, request->sasl_method);
195     PASS_ATTR(cleanup, MAIL_ATTR_SASL_USERNAME, request->sasl_username);
196     PASS_ATTR(cleanup, MAIL_ATTR_SASL_SENDER, request->sasl_sender);
197     PASS_ATTR(cleanup, MAIL_ATTR_LOG_IDENT, request->log_ident);
198     PASS_ATTR(cleanup, MAIL_ATTR_RWR_CONTEXT, request->rewrite_context);
199 
200     FORWARD_OPEN_RETURN(info);
201 }
202 
203 /* forward_append - append recipient to message envelope */
204 
205 int     forward_append(DELIVER_ATTR attr)
206 {
207     FORWARD_INFO *info;
208     HTABLE *table_snd;
209 
210     /*
211      * Sanity checks.
212      */
213     if (msg_verbose)
214 	msg_info("forward delivered=%s sender=%s recip=%s",
215 		 attr.delivered, attr.sender, attr.rcpt.address);
216     if (forward_dt == 0)
217 	msg_panic("forward_append: missing forward_init call");
218 
219     /*
220      * In order to find the recipient list, first index a table by
221      * delivered-to header address, then by envelope sender address.
222      */
223     if ((table_snd = (HTABLE *) htable_find(forward_dt, attr.delivered)) == 0) {
224 	table_snd = htable_create(0);
225 	htable_enter(forward_dt, attr.delivered, (char *) table_snd);
226     }
227     if ((info = (FORWARD_INFO *) htable_find(table_snd, attr.sender)) == 0) {
228 	if ((info = forward_open(attr.request, attr.sender)) == 0)
229 	    return (-1);
230 	htable_enter(table_snd, attr.sender, (char *) info);
231     }
232 
233     /*
234      * Append the recipient to the message envelope. Don't send the original
235      * recipient or notification mask if it was reset due to mailing list
236      * expansion.
237      */
238     if (*attr.rcpt.dsn_orcpt)
239 	rec_fprintf(info->cleanup, REC_TYPE_ATTR, "%s=%s",
240 		    MAIL_ATTR_DSN_ORCPT, attr.rcpt.dsn_orcpt);
241     if (attr.rcpt.dsn_notify)
242 	rec_fprintf(info->cleanup, REC_TYPE_ATTR, "%s=%d",
243 		    MAIL_ATTR_DSN_NOTIFY, attr.rcpt.dsn_notify);
244     if (*attr.rcpt.orig_addr)
245 	rec_fputs(info->cleanup, REC_TYPE_ORCP, attr.rcpt.orig_addr);
246     rec_fputs(info->cleanup, REC_TYPE_RCPT, attr.rcpt.address);
247 
248     return (vstream_ferror(info->cleanup));
249 }
250 
251 /* forward_send - send forwarded message */
252 
253 static int forward_send(FORWARD_INFO *info, DELIVER_REQUEST *request,
254 			        DELIVER_ATTR attr, char *delivered)
255 {
256     const char *myname = "forward_send";
257     VSTRING *buffer = vstring_alloc(100);
258     int     status;
259     int     rec_type = 0;
260 
261     /*
262      * Start the message content segment. Prepend our Delivered-To: header to
263      * the message data. Stop at the first error. XXX Rely on the front-end
264      * services to enforce record size limits.
265      */
266     rec_fputs(info->cleanup, REC_TYPE_MESG, "");
267     vstring_strcpy(buffer, delivered);
268     rec_fprintf(info->cleanup, REC_TYPE_NORM, "Received: by %s (%s)",
269 		var_myhostname, var_mail_name);
270     rec_fprintf(info->cleanup, REC_TYPE_NORM, "\tid %s; %s",
271 		info->queue_id, mail_date(info->posting_time.tv_sec));
272     if (local_deliver_hdr_mask & DELIVER_HDR_FWD)
273 	rec_fprintf(info->cleanup, REC_TYPE_NORM, "Delivered-To: %s",
274 		    lowercase(STR(buffer)));
275     if ((status = vstream_ferror(info->cleanup)) == 0)
276 	if (vstream_fseek(attr.fp, attr.offset, SEEK_SET) < 0)
277 	    msg_fatal("%s: seek queue file %s: %m:",
278 		      myname, VSTREAM_PATH(attr.fp));
279     while (status == 0 && (rec_type = rec_get(attr.fp, buffer, 0)) > 0) {
280 	if (rec_type != REC_TYPE_CONT && rec_type != REC_TYPE_NORM)
281 	    break;
282 	status = (REC_PUT_BUF(info->cleanup, rec_type, buffer) != rec_type);
283     }
284     if (status == 0 && rec_type != REC_TYPE_XTRA) {
285 	msg_warn("%s: bad record type: %d in message content",
286 		 info->queue_id, rec_type);
287 	status |= mark_corrupt(attr.fp);
288     }
289 
290     /*
291      * Send the end-of-data marker only when there were no errors.
292      */
293     if (status == 0) {
294 	rec_fputs(info->cleanup, REC_TYPE_XTRA, "");
295 	rec_fputs(info->cleanup, REC_TYPE_END, "");
296     }
297 
298     /*
299      * Retrieve the cleanup service completion status only if there are no
300      * problems.
301      */
302     if (status == 0)
303 	if (vstream_fflush(info->cleanup)
304 	    || attr_scan(info->cleanup, ATTR_FLAG_MISSING,
305 			 ATTR_TYPE_INT, MAIL_ATTR_STATUS, &status,
306 			 ATTR_TYPE_END) != 1)
307 	    status = 1;
308 
309     /*
310      * Log successful forwarding.
311      *
312      * XXX DSN alias and .forward expansion already report SUCCESS, so don't do
313      * it again here.
314      */
315     if (status == 0) {
316 	attr.rcpt.dsn_notify =
317 	    (attr.rcpt.dsn_notify == DSN_NOTIFY_SUCCESS ?
318 	     DSN_NOTIFY_NEVER : attr.rcpt.dsn_notify & ~DSN_NOTIFY_SUCCESS);
319 	dsb_update(attr.why, "2.0.0", "relayed", DSB_SKIP_RMTA, DSB_SKIP_REPLY,
320 		   "forwarded as %s", info->queue_id);
321 	status = sent(BOUNCE_FLAGS(request), SENT_ATTR(attr));
322     }
323 
324     /*
325      * Cleanup.
326      */
327     vstring_free(buffer);
328     return (status);
329 }
330 
331 /* forward_finish - complete message forwarding requests and clean up */
332 
333 int     forward_finish(DELIVER_REQUEST *request, DELIVER_ATTR attr, int cancel)
334 {
335     HTABLE_INFO **dt_list;
336     HTABLE_INFO **dt;
337     HTABLE_INFO **sn_list;
338     HTABLE_INFO **sn;
339     HTABLE *table_snd;
340     char   *delivered;
341     char   *sender;
342     FORWARD_INFO *info;
343     int     status = cancel;
344 
345     /*
346      * Sanity checks.
347      */
348     if (forward_dt == 0)
349 	msg_panic("forward_finish: missing forward_init call");
350 
351     /*
352      * Walk over all delivered-to header addresses and over each envelope
353      * sender address.
354      */
355     for (dt = dt_list = htable_list(forward_dt); *dt; dt++) {
356 	delivered = dt[0]->key;
357 	table_snd = (HTABLE *) dt[0]->value;
358 	for (sn = sn_list = htable_list(table_snd); *sn; sn++) {
359 	    sender = sn[0]->key;
360 	    info = (FORWARD_INFO *) sn[0]->value;
361 	    if (status == 0)
362 		status |= forward_send(info, request, attr, delivered);
363 	    if (msg_verbose)
364 		msg_info("forward_finish: delivered %s sender %s status %d",
365 			 delivered, sender, status);
366 	    (void) vstream_fclose(info->cleanup);
367 	    myfree(info->queue_id);
368 	    myfree((char *) info);
369 	}
370 	myfree((char *) sn_list);
371 	htable_free(table_snd, (void (*) (char *)) 0);
372     }
373     myfree((char *) dt_list);
374     htable_free(forward_dt, (void (*) (char *)) 0);
375     forward_dt = 0;
376     return (status);
377 }
378