7 /* record output support
9 /* #include "cleanup.h"
11 /* int CLEANUP_OUT_OK(state)
12 /* CLEANUP_STATE *state;
14 /* void cleanup_out(state, type, data, len)
15 /* CLEANUP_STATE *state;
20 /* void cleanup_out_string(state, type, str)
21 /* CLEANUP_STATE *state;
25 /* void CLEANUP_OUT_BUF(state, type, buf)
26 /* CLEANUP_STATE *state;
30 /* void cleanup_out_format(state, type, format, ...)
31 /* CLEANUP_STATE *state;
33 /* const char *format;
35 /* void cleanup_out_header(state, buf)
36 /* CLEANUP_STATE *state;
39 /* This module writes records to the output stream.
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.
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.
49 /* cleanup_out_string() outputs one string as a record.
51 /* CLEANUP_OUT_BUF() is an unsafe macro that outputs
52 /* one string buffer as a record.
54 /* cleanup_out_format() formats its arguments and writes
55 /* the result as a record.
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.
63 /* The Secure Mailer license must be distributed with this software.
66 /* IBM T.J. Watson Research
68 /* Yorktown Heights, NY 10598, USA
75 #include <stdlib.h> /* 44BSD stdarg.h uses abort() */
79 /* Utility library. */
90 #include <cleanup_user.h>
91 #include <mail_params.h>
94 /* Application-specific. */
98 /* cleanup_out - output one single record */
100 void cleanup_out(CLEANUP_STATE
*state
, int type
, const char *string
, ssize_t len
)
105 * Long message header lines have to be read and written as multiple
106 * records. Other header/body content, and envelope data, is copied one
107 * record at a time. Be sure to not skip a zero-length request.
109 * XXX We don't know if we're writing a message header or not, but that is
110 * not a problem. A REC_TYPE_NORM or REC_TYPE_CONT record can always be
111 * chopped up into an equivalent set of REC_TYPE_CONT plus REC_TYPE_NORM
114 if (CLEANUP_OUT_OK(state
) == 0)
117 #define TEXT_RECORD(t) ((t) == REC_TYPE_NORM || (t) == REC_TYPE_CONT)
119 if (var_line_limit
<= 0)
120 msg_panic("cleanup_out: bad line length limit: %d", var_line_limit
);
122 if (len
> var_line_limit
&& TEXT_RECORD(type
)) {
123 err
= rec_put(state
->dst
, REC_TYPE_CONT
, string
, var_line_limit
);
124 string
+= var_line_limit
;
125 len
-= var_line_limit
;
127 err
= rec_put(state
->dst
, type
, string
, len
);
130 } while (len
> 0 && err
>= 0);
133 if (errno
== EFBIG
) {
134 msg_warn("%s: queue file size limit exceeded",
136 state
->errs
|= CLEANUP_STAT_SIZE
;
138 msg_warn("%s: write queue file: %m", state
->queue_id
);
139 state
->errs
|= CLEANUP_STAT_WRITE
;
144 /* cleanup_out_string - output string to one single record */
146 void cleanup_out_string(CLEANUP_STATE
*state
, int type
, const char *string
)
148 cleanup_out(state
, type
, string
, strlen(string
));
151 /* cleanup_out_format - output one formatted record */
153 void cleanup_out_format(CLEANUP_STATE
*state
, int type
, const char *fmt
,...)
159 vp
= vstring_alloc(100);
161 vstring_vsprintf(vp
, fmt
, ap
);
163 CLEANUP_OUT_BUF(state
, type
, vp
);
166 /* cleanup_out_header - output one multi-line header as a bunch of records */
168 void cleanup_out_header(CLEANUP_STATE
*state
, VSTRING
*header_buf
)
170 char *start
= vstring_str(header_buf
);
176 * Prepend a tab to continued header lines that went through the address
177 * rewriting machinery. See cleanup_fold_header(state) below for the form
178 * of such header lines. NB: This code destroys the header. We could try
179 * to avoid clobbering it, but we're not going to use the data any
182 * XXX We prefer to truncate a header at the last line boundary before the
183 * header size limit. If this would undershoot the limit by more than
184 * 10%, we truncate between line boundaries to avoid losing too much
185 * text. This "unkind cut" may result in syntax errors and may trigger
186 * warnings from down-stream MTAs.
188 * If Milter is enabled, pad a short header record with a dummy record so
189 * that a header record can safely be overwritten by a pointer record.
190 * This simplifies header modification enormously.
192 for (line
= start
; line
; line
= next_line
) {
193 next_line
= split_at(line
, '\n');
194 line_len
= next_line
? next_line
- 1 - line
: strlen(line
);
195 if (line
+ line_len
> start
+ var_header_limit
) {
196 if (line
- start
> 0.9 * var_header_limit
) /* nice cut */
198 start
[var_header_limit
] = 0; /* unkind cut */
202 cleanup_out_string(state
, REC_TYPE_NORM
, line
);
203 if ((state
->milters
|| cleanup_milters
)
204 && line_len
< REC_TYPE_PTR_PAYL_SIZE
)
205 rec_pad(state
->dst
, REC_TYPE_DTXT
,
206 REC_TYPE_PTR_PAYL_SIZE
- line_len
);
207 } else if (IS_SPACE_TAB(*line
)) {
208 cleanup_out_string(state
, REC_TYPE_NORM
, line
);
210 cleanup_out_format(state
, REC_TYPE_NORM
, "\t%s", line
);