1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*************************************************
* Exim - an Internet mail transport agent *
*************************************************/
/* Copyright (c) University of Cambridge 1995 - 2009 */
/* See the file NOTICE for conditions of use and distribution. */
/* Code for the filter test function. */
#include "exim.h"
/*************************************************
* Read message and set body/size variables *
*************************************************/
/* We have to read the remainder of the message in order to find its size, so
we can set up the message_body variables at the same time (in normal use, the
message_body variables are not set up unless needed). The reading code is
written out here rather than having options in read_message_data, in order to
keep that function as efficient as possible. (Later: this function is now
global because it is also used by the -bem testing option.) Handling
message_body_end is somewhat more tedious. Pile it all into a circular buffer
and sort out at the end.
Arguments:
dot_ended TRUE if message already terminated by '.'
Returns: nothing
*/
void
read_message_body(BOOL dot_ended)
{
register int ch;
int body_len, body_end_len, header_size;
uschar *s;
message_body = store_malloc(message_body_visible + 1);
message_body_end = store_malloc(message_body_visible + 1);
s = message_body_end;
body_len = 0;
body_linecount = 0;
header_size = message_size;
if (!dot_ended && !feof(stdin))
{
if (!f.dot_ends)
{
while ((ch = getc(stdin)) != EOF)
{
if (ch == 0) body_zerocount++;
if (ch == '\n') body_linecount++;
if (body_len < message_body_visible) message_body[body_len++] = ch;
*s++ = ch;
if (s > message_body_end + message_body_visible) s = message_body_end;
message_size++;
}
}
else
{
int ch_state = 1;
while ((ch = getc(stdin)) != EOF)
{
if (ch == 0) body_zerocount++;
switch (ch_state)
{
case 0: /* Normal state */
if (ch == '\n') { body_linecount++; ch_state = 1; }
break;
case 1: /* After "\n" */
if (ch == '.')
{
ch_state = 2;
continue;
}
if (ch != '\n') ch_state = 0;
break;
case 2: /* After "\n." */
if (ch == '\n') goto READ_END;
if (body_len < message_body_visible) message_body[body_len++] = '.';
*s++ = '.';
if (s > message_body_end + message_body_visible)
s = message_body_end;
message_size++;
ch_state = 0;
break;
}
if (body_len < message_body_visible) message_body[body_len++] = ch;
*s++ = ch;
if (s > message_body_end + message_body_visible) s = message_body_end;
message_size++;
}
READ_END: ch = ch; /* Some compilers don't like null statements */
}
if (s == message_body_end || s[-1] != '\n') body_linecount++;
}
message_body[body_len] = 0;
message_body_size = message_size - header_size;
/* body_len stops at message_body_visible; it if got there, we may have
wrapped round in message_body_end. */
if (body_len >= message_body_visible)
{
int below = s - message_body_end;
int above = message_body_visible - below;
if (above > 0)
{
uschar *temp = store_get(below);
memcpy(temp, message_body_end, below);
memmove(message_body_end, s+1, above);
memcpy(message_body_end + above, temp, below);
s = message_body_end + message_body_visible;
}
}
*s = 0;
body_end_len = s - message_body_end;
/* Convert newlines and nulls in the body variables to spaces */
while (body_len > 0)
{
if (message_body[--body_len] == '\n' || message_body[body_len] == 0)
message_body[body_len] = ' ';
}
while (body_end_len > 0)
{
if (message_body_end[--body_end_len] == '\n' ||
message_body_end[body_end_len] == 0)
message_body_end[body_end_len] = ' ';
}
}
/*************************************************
* Test a mail filter *
*************************************************/
/* This is called when exim is run with the -bf option. At this point it is
running under an unprivileged uid/gid. A test message's headers have been read
into store, and the body of the message is still accessible on the standard
input if this is the first time this function has been called. It may be called
twice if both system and user filters are being tested.
Argument:
fd an fd containing the filter file
filename the name of the filter file
is_system TRUE if testing is to be as a system filter
dot_ended TRUE if message already terminated by '.'
Returns: TRUE if no errors
*/
BOOL
filter_runtest(int fd, uschar *filename, BOOL is_system, BOOL dot_ended)
{
int rc, filter_type;
BOOL yield;
struct stat statbuf;
address_item *generated = NULL;
uschar *error, *filebuf;
/* Read the filter file into store as will be done by the router in a real
case. */
if (fstat(fd, &statbuf) != 0)
{
printf("exim: failed to get size of %s: %s\n", filename, strerror(errno));
return FALSE;
}
filebuf = store_get(statbuf.st_size + 1);
rc = read(fd, filebuf, statbuf.st_size);
(void)close(fd);
if (rc != statbuf.st_size)
{
printf("exim: error while reading %s: %s\n", filename, strerror(errno));
return FALSE;
}
filebuf[statbuf.st_size] = 0;
/* Check the filter type. User filters start with "# Exim filter" or "# Sieve
filter". If the filter type is not recognized, the file is treated as an
ordinary .forward file. System filters do not need the "# Exim filter" in order
to be recognized as Exim filters. */
filter_type = rda_is_filter(filebuf);
if (is_system && filter_type == FILTER_FORWARD) filter_type = FILTER_EXIM;
printf("Testing %s file \"%s\"\n\n",
(filter_type == FILTER_EXIM)? "Exim filter" :
(filter_type == FILTER_SIEVE)? "Sieve filter" :
"forward file",
filename);
/* Handle a plain .forward file */
if (filter_type == FILTER_FORWARD)
{
yield = parse_forward_list(filebuf,
RDO_REWRITE,
&generated, /* for generated addresses */
&error, /* for errors */
deliver_domain, /* incoming domain for \name */
NULL, /* no check on includes */
NULL); /* fail on syntax errors */
switch(yield)
{
case FF_FAIL:
printf("exim: forward file contains \":fail:\"\n");
break;
case FF_BLACKHOLE:
printf("exim: forwardfile contains \":blackhole:\"\n");
break;
case FF_ERROR:
printf("exim: error in forward file: %s\n", error);
return FALSE;
}
if (generated == NULL)
printf("exim: no addresses generated from forward file\n");
else
{
printf("exim: forward file generated:\n");
while (generated != NULL)
{
printf(" %s\n", generated->address);
generated = generated->next;
}
}
return TRUE;
}
/* For a filter, set up the message_body variables and the message size if this
is the first time this function has been called. */
if (message_body == NULL) read_message_body(dot_ended);
/* Now pass the filter file to the function that interprets it. Because
filter_test is not FILTER_NONE, the interpreter will output comments about what
it is doing. No need to clean up store. Indeed, we must not, because we may be
testing a system filter that is going to be followed by a user filter test. */
if (is_system)
{
f.system_filtering = TRUE;
f.enable_dollar_recipients = TRUE; /* Permit $recipients in system filter */
yield = filter_interpret
(filebuf,
RDO_DEFER|RDO_FAIL|RDO_FILTER|RDO_FREEZE|RDO_REWRITE, &generated, &error);
f.enable_dollar_recipients = FALSE;
f.system_filtering = FALSE;
}
else
{
yield = (filter_type == FILTER_SIEVE)?
sieve_interpret(filebuf, RDO_REWRITE, NULL, NULL, NULL, NULL, &generated, &error)
:
filter_interpret(filebuf, RDO_REWRITE, &generated, &error);
}
return yield != FF_ERROR;
}
/* End of filtertest.c */
|