xref: /netbsd-src/external/ibm-public/postfix/dist/src/cleanup/cleanup_out.c (revision 67b9b338a7386232ac596b5fd0cd5a9cc8a03c71)
1 /*	$NetBSD: cleanup_out.c,v 1.3 2022/10/08 16:12:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	cleanup_out 3
6 /* SUMMARY
7 /*	record output support
8 /* SYNOPSIS
9 /*	#include "cleanup.h"
10 /*
11 /*	int	CLEANUP_OUT_OK(state)
12 /*	CLEANUP_STATE *state;
13 /*
14 /*	void	cleanup_out(state, type, data, len)
15 /*	CLEANUP_STATE *state;
16 /*	int	type;
17 /*	const char *data;
18 /*	ssize_t	len;
19 /*
20 /*	void	cleanup_out_string(state, type, str)
21 /*	CLEANUP_STATE *state;
22 /*	int	type;
23 /*	const char *str;
24 /*
25 /*	void	CLEANUP_OUT_BUF(state, type, buf)
26 /*	CLEANUP_STATE *state;
27 /*	int	type;
28 /*	VSTRING	*buf;
29 /*
30 /*	void	cleanup_out_format(state, type, format, ...)
31 /*	CLEANUP_STATE *state;
32 /*	int	type;
33 /*	const char *format;
34 /*
35 /*	void	cleanup_out_header(state, buf)
36 /*	CLEANUP_STATE *state;
37 /*	VSTRING	*buf;
38 /* DESCRIPTION
39 /*	This module writes records to the output stream.
40 /*
41 /*	CLEANUP_OUT_OK() is a macro that evaluates to non-zero
42 /*	as long as it makes sense to produce output. All output
43 /*	routines below check for this condition.
44 /*
45 /*	cleanup_out() is the main record output routine. It writes
46 /*	one record of the specified type, with the specified data
47 /*	and length to the output stream.
48 /*
49 /*	cleanup_out_string() outputs one string as a record.
50 /*
51 /*	CLEANUP_OUT_BUF() is an unsafe macro that outputs
52 /*	one string buffer as a record.
53 /*
54 /*	cleanup_out_format() formats its arguments and writes
55 /*	the result as a record.
56 /*
57 /*	cleanup_out_header() outputs a multi-line header as records
58 /*	of the specified type. The input is expected to be newline
59 /*	separated (not newline terminated), and is modified.
60 /* LICENSE
61 /* .ad
62 /* .fi
63 /*	The Secure Mailer license must be distributed with this software.
64 /* AUTHOR(S)
65 /*	Wietse Venema
66 /*	IBM T.J. Watson Research
67 /*	P.O. Box 704
68 /*	Yorktown Heights, NY 10598, USA
69 /*
70 /*	Wietse Venema
71 /*	Google, Inc.
72 /*	111 8th Avenue
73 /*	New York, NY 10011, USA
74 /*--*/
75 
76 /* System library. */
77 
78 #include <sys_defs.h>
79 #include <errno.h>
80 #include <stdlib.h>			/* 44BSD stdarg.h uses abort() */
81 #include <stdarg.h>
82 #include <string.h>
83 
84 /* Utility library. */
85 
86 #include <msg.h>
87 #include <vstring.h>
88 #include <vstream.h>
89 #include <split_at.h>
90 #include <stringops.h>
91 
92 /* Global library. */
93 
94 #include <record.h>
95 #include <rec_type.h>
96 #include <cleanup_user.h>
97 #include <mail_params.h>
98 #include <lex_822.h>
99 #include <smtputf8.h>
100 
101 /* Application-specific. */
102 
103 #include "cleanup.h"
104 
105 #define STR	vstring_str
106 
107 /* cleanup_out - output one single record */
108 
cleanup_out(CLEANUP_STATE * state,int type,const char * string,ssize_t len)109 void    cleanup_out(CLEANUP_STATE *state, int type, const char *string, ssize_t len)
110 {
111     int     err = 0;
112 
113     /*
114      * Long message header lines have to be read and written as multiple
115      * records. Other header/body content, and envelope data, is copied one
116      * record at a time. Be sure to not skip a zero-length request.
117      *
118      * XXX We don't know if we're writing a message header or not, but that is
119      * not a problem. A REC_TYPE_NORM or REC_TYPE_CONT record can always be
120      * chopped up into an equivalent set of REC_TYPE_CONT plus REC_TYPE_NORM
121      * records.
122      */
123     if (CLEANUP_OUT_OK(state) == 0)
124 	return;
125 
126 #define TEXT_RECORD(t)	((t) == REC_TYPE_NORM || (t) == REC_TYPE_CONT)
127 
128     if (msg_verbose && !TEXT_RECORD(type))
129 	msg_info("cleanup_out: %c %.*s", type, (int) len, string);
130 
131     if (var_line_limit <= 0)
132 	msg_panic("cleanup_out: bad line length limit: %d", var_line_limit);
133     do {
134 	if (len > var_line_limit && TEXT_RECORD(type)) {
135 	    err = rec_put(state->dst, REC_TYPE_CONT, string, var_line_limit);
136 	    string += var_line_limit;
137 	    len -= var_line_limit;
138 	} else {
139 	    err = rec_put(state->dst, type, string, len);
140 	    break;
141 	}
142     } while (len > 0 && err >= 0);
143 
144     if (err < 0) {
145 	if (errno == EFBIG) {
146 	    msg_warn("%s: queue file size limit exceeded",
147 		     state->queue_id);
148 	    state->errs |= CLEANUP_STAT_SIZE;
149 	} else {
150 	    msg_warn("%s: write queue file: %m", state->queue_id);
151 	    state->errs |= CLEANUP_STAT_WRITE;
152 	}
153     }
154 }
155 
156 /* cleanup_out_string - output string to one single record */
157 
cleanup_out_string(CLEANUP_STATE * state,int type,const char * string)158 void    cleanup_out_string(CLEANUP_STATE *state, int type, const char *string)
159 {
160     cleanup_out(state, type, string, strlen(string));
161 }
162 
163 /* cleanup_out_format - output one formatted record */
164 
cleanup_out_format(CLEANUP_STATE * state,int type,const char * fmt,...)165 void    cleanup_out_format(CLEANUP_STATE *state, int type, const char *fmt,...)
166 {
167     static VSTRING *vp;
168     va_list ap;
169 
170     if (vp == 0)
171 	vp = vstring_alloc(100);
172     va_start(ap, fmt);
173     vstring_vsprintf(vp, fmt, ap);
174     va_end(ap);
175     CLEANUP_OUT_BUF(state, type, vp);
176 }
177 
178 /* cleanup_out_header - output one multi-line header as a bunch of records */
179 
cleanup_out_header(CLEANUP_STATE * state,VSTRING * header_buf)180 void    cleanup_out_header(CLEANUP_STATE *state, VSTRING *header_buf)
181 {
182     char   *start = vstring_str(header_buf);
183     char   *line;
184     char   *next_line;
185     ssize_t line_len;
186 
187     /*
188      * Fix 20140711: Auto-detect the presence of a non-ASCII header.
189      */
190     if (var_smtputf8_enable && *STR(header_buf) && !allascii(STR(header_buf))) {
191 	state->smtputf8 |= SMTPUTF8_FLAG_HEADER;
192 	/* Fix 20140713: request SMTPUTF8 support selectively. */
193 	if (state->flags & CLEANUP_FLAG_AUTOUTF8)
194 	    state->smtputf8 |= SMTPUTF8_FLAG_REQUESTED;
195     }
196 
197     /*
198      * Prepend a tab to continued header lines that went through the address
199      * rewriting machinery. See cleanup_fold_header(state) below for the form
200      * of such header lines. NB: This code destroys the header. We could try
201      * to avoid clobbering it, but we're not going to use the data any
202      * further.
203      *
204      * XXX We prefer to truncate a header at the last line boundary before the
205      * header size limit. If this would undershoot the limit by more than
206      * 10%, we truncate between line boundaries to avoid losing too much
207      * text. This "unkind cut" may result in syntax errors and may trigger
208      * warnings from down-stream MTAs.
209      *
210      * If Milter is enabled, pad a short header record with a dummy record so
211      * that a header record can safely be overwritten by a pointer record.
212      * This simplifies header modification enormously.
213      */
214     for (line = start; line; line = next_line) {
215 	next_line = split_at(line, '\n');
216 	line_len = next_line ? next_line - 1 - line : strlen(line);
217 	if (line + line_len > start + var_header_limit) {
218 	    if (line - start > 0.9 * var_header_limit)	/* nice cut */
219 		break;
220 	    start[var_header_limit] = 0;	/* unkind cut */
221 	    next_line = 0;
222 	}
223 	if (line == start) {
224 	    cleanup_out_string(state, REC_TYPE_NORM, line);
225 	    if ((state->milters || cleanup_milters)
226 		&& line_len < REC_TYPE_PTR_PAYL_SIZE)
227 		rec_pad(state->dst, REC_TYPE_DTXT,
228 			REC_TYPE_PTR_PAYL_SIZE - line_len);
229 	} else if (IS_SPACE_TAB(*line)) {
230 	    cleanup_out_string(state, REC_TYPE_NORM, line);
231 	} else {
232 	    cleanup_out_format(state, REC_TYPE_NORM, "\t%s", line);
233 	}
234     }
235 }
236