/src/pigeonhole/src/lib-sieve/plugins/spamvirustest/tst-spamvirustest.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | |
5 | | #include "sieve-common.h" |
6 | | #include "sieve-extensions.h" |
7 | | #include "sieve-commands.h" |
8 | | #include "sieve-stringlist.h" |
9 | | #include "sieve-code.h" |
10 | | #include "sieve-comparators.h" |
11 | | #include "sieve-match-types.h" |
12 | | #include "sieve-address-parts.h" |
13 | | #include "sieve-validator.h" |
14 | | #include "sieve-generator.h" |
15 | | #include "sieve-interpreter.h" |
16 | | #include "sieve-dump.h" |
17 | | #include "sieve-match.h" |
18 | | |
19 | | #include "ext-spamvirustest-common.h" |
20 | | |
21 | | /* |
22 | | * Tests |
23 | | */ |
24 | | |
25 | | static bool |
26 | | tst_spamvirustest_validate(struct sieve_validator *valdtr, |
27 | | struct sieve_command *tst); |
28 | | static bool |
29 | | tst_spamvirustest_generate(const struct sieve_codegen_env *cgenv, |
30 | | struct sieve_command *ctx); |
31 | | static bool |
32 | | tst_spamvirustest_registered(struct sieve_validator *valdtr, |
33 | | const struct sieve_extension *ext, |
34 | | struct sieve_command_registration *cmd_reg); |
35 | | |
36 | | /* Spamtest test |
37 | | * |
38 | | * Syntax: |
39 | | * spamtest [":percent"] [COMPARATOR] [MATCH-TYPE] <value: string> |
40 | | */ |
41 | | |
42 | | const struct sieve_command_def spamtest_test = { |
43 | | .identifier = "spamtest", |
44 | | .type = SCT_TEST, |
45 | | .positional_args = 1, |
46 | | .subtests = 0, |
47 | | .block_allowed = FALSE, |
48 | | .block_required = FALSE, |
49 | | .registered = tst_spamvirustest_registered, |
50 | | .validate = tst_spamvirustest_validate, |
51 | | .generate = tst_spamvirustest_generate, |
52 | | }; |
53 | | |
54 | | /* Virustest test |
55 | | * |
56 | | * Syntax: |
57 | | * virustest [COMPARATOR] [MATCH-TYPE] <value: string> |
58 | | */ |
59 | | |
60 | | const struct sieve_command_def virustest_test = { |
61 | | .identifier = "virustest", |
62 | | .type = SCT_TEST, |
63 | | .positional_args = 1, |
64 | | .subtests = 0, |
65 | | .block_allowed = FALSE, |
66 | | .block_required = FALSE, |
67 | | .registered = tst_spamvirustest_registered, |
68 | | .validate = tst_spamvirustest_validate, |
69 | | .generate = tst_spamvirustest_generate, |
70 | | }; |
71 | | |
72 | | /* |
73 | | * Tagged arguments |
74 | | */ |
75 | | |
76 | | static bool |
77 | | tst_spamtest_validate_percent_tag(struct sieve_validator *valdtr, |
78 | | struct sieve_ast_argument **arg, |
79 | | struct sieve_command *tst); |
80 | | |
81 | | static const struct sieve_argument_def spamtest_percent_tag = { |
82 | | .identifier = "percent", |
83 | | .validate = tst_spamtest_validate_percent_tag, |
84 | | }; |
85 | | |
86 | | /* |
87 | | * Spamtest and virustest operations |
88 | | */ |
89 | | |
90 | | static bool |
91 | | tst_spamvirustest_operation_dump(const struct sieve_dumptime_env *denv, |
92 | | sieve_size_t *address); |
93 | | static int |
94 | | tst_spamvirustest_operation_execute(const struct sieve_runtime_env *renv, |
95 | | sieve_size_t *address); |
96 | | |
97 | | const struct sieve_operation_def spamtest_operation = { |
98 | | .mnemonic = "SPAMTEST", |
99 | | .ext_def = &spamtest_extension, |
100 | | .dump = tst_spamvirustest_operation_dump, |
101 | | .execute = tst_spamvirustest_operation_execute, |
102 | | }; |
103 | | |
104 | | const struct sieve_operation_def virustest_operation = { |
105 | | .mnemonic = "VIRUSTEST", |
106 | | .ext_def = &virustest_extension, |
107 | | .dump = tst_spamvirustest_operation_dump, |
108 | | .execute = tst_spamvirustest_operation_execute, |
109 | | }; |
110 | | |
111 | | /* |
112 | | * Optional operands |
113 | | */ |
114 | | |
115 | | enum tst_spamvirustest_optional { |
116 | | OPT_SPAMTEST_PERCENT = SIEVE_MATCH_OPT_LAST, |
117 | | OPT_SPAMTEST_LAST, |
118 | | }; |
119 | | |
120 | | /* |
121 | | * Test registration |
122 | | */ |
123 | | |
124 | | static bool |
125 | | tst_spamvirustest_registered(struct sieve_validator *valdtr, |
126 | | const struct sieve_extension *ext, |
127 | | struct sieve_command_registration *cmd_reg) |
128 | 0 | { |
129 | 0 | sieve_comparators_link_tag(valdtr, cmd_reg, |
130 | 0 | SIEVE_MATCH_OPT_COMPARATOR); |
131 | 0 | sieve_match_types_link_tags(valdtr, cmd_reg, |
132 | 0 | SIEVE_MATCH_OPT_MATCH_TYPE); |
133 | |
|
134 | 0 | if (sieve_extension_is(ext, spamtestplus_extension) || |
135 | 0 | sieve_extension_is(ext, spamtest_extension)) { |
136 | 0 | sieve_validator_register_tag( |
137 | 0 | valdtr, cmd_reg, ext, &spamtest_percent_tag, |
138 | 0 | OPT_SPAMTEST_PERCENT); |
139 | 0 | } |
140 | 0 | return TRUE; |
141 | 0 | } |
142 | | |
143 | | /* |
144 | | * Validation |
145 | | */ |
146 | | |
147 | | static bool |
148 | | tst_spamtest_validate_percent_tag(struct sieve_validator *valdtr, |
149 | | struct sieve_ast_argument **arg, |
150 | | struct sieve_command *tst) |
151 | 0 | { |
152 | 0 | if (!sieve_extension_is(tst->ext, spamtestplus_extension)) { |
153 | 0 | sieve_argument_validate_error( |
154 | 0 | valdtr, *arg, |
155 | 0 | "the spamtest test only accepts the :percent argument when " |
156 | 0 | "the spamtestplus extension is active"); |
157 | 0 | return FALSE; |
158 | 0 | } |
159 | | |
160 | | /* Skip tag */ |
161 | 0 | *arg = sieve_ast_argument_next(*arg); |
162 | |
|
163 | 0 | return TRUE; |
164 | 0 | } |
165 | | |
166 | | static bool |
167 | | tst_spamvirustest_validate(struct sieve_validator *valdtr, |
168 | | struct sieve_command *tst) |
169 | 0 | { |
170 | 0 | struct sieve_ast_argument *arg = tst->first_positional; |
171 | 0 | const struct sieve_match_type mcht_default = |
172 | 0 | SIEVE_MATCH_TYPE_DEFAULT(is_match_type); |
173 | 0 | const struct sieve_comparator cmp_default = |
174 | 0 | SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator); |
175 | | |
176 | | /* Check value */ |
177 | |
|
178 | 0 | if (!sieve_validate_positional_argument(valdtr, tst, arg, "value", 1, |
179 | 0 | SAAT_STRING)) |
180 | 0 | return FALSE; |
181 | | |
182 | 0 | if (!sieve_validator_argument_activate(valdtr, tst, arg, FALSE)) |
183 | 0 | return FALSE; |
184 | | |
185 | | /* Validate the key argument to a specified match type */ |
186 | 0 | return sieve_match_type_validate(valdtr, tst, arg, |
187 | 0 | &mcht_default, &cmp_default); |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * Code generation |
192 | | */ |
193 | | |
194 | | static bool |
195 | | tst_spamvirustest_generate(const struct sieve_codegen_env *cgenv, |
196 | | struct sieve_command *tst) |
197 | 0 | { |
198 | 0 | if (sieve_command_is(tst, spamtest_test)) { |
199 | 0 | sieve_operation_emit(cgenv->sblock, tst->ext, |
200 | 0 | &spamtest_operation); |
201 | 0 | } else if (sieve_command_is(tst, virustest_test)) { |
202 | 0 | sieve_operation_emit(cgenv->sblock, tst->ext, |
203 | 0 | &virustest_operation); |
204 | 0 | } else { |
205 | 0 | i_unreached(); |
206 | 0 | } |
207 | | |
208 | | /* Generate arguments */ |
209 | 0 | return sieve_generate_arguments(cgenv, tst, NULL); |
210 | 0 | } |
211 | | |
212 | | /* |
213 | | * Code dump |
214 | | */ |
215 | | |
216 | | static bool |
217 | | tst_spamvirustest_operation_dump(const struct sieve_dumptime_env *denv, |
218 | | sieve_size_t *address) |
219 | 0 | { |
220 | 0 | int opt_code = 0; |
221 | 0 | const struct sieve_operation *op = denv->oprtn; |
222 | |
|
223 | 0 | sieve_code_dumpf(denv, "%s", sieve_operation_mnemonic(op)); |
224 | 0 | sieve_code_descend(denv); |
225 | | |
226 | | /* Optional operands */ |
227 | 0 | for (;;) { |
228 | 0 | int opt; |
229 | |
|
230 | 0 | opt = sieve_match_opr_optional_dump(denv, address, &opt_code); |
231 | 0 | if (opt < 0) |
232 | 0 | return FALSE; |
233 | 0 | if (opt == 0) |
234 | 0 | break; |
235 | | |
236 | 0 | switch (opt_code) { |
237 | 0 | case OPT_SPAMTEST_PERCENT: |
238 | 0 | sieve_code_dumpf(denv, "percent"); |
239 | 0 | break; |
240 | 0 | default: |
241 | 0 | return FALSE; |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | return sieve_opr_string_dump(denv, address, "value"); |
246 | 0 | } |
247 | | |
248 | | /* |
249 | | * Code execution |
250 | | */ |
251 | | |
252 | | static int |
253 | | tst_spamvirustest_operation_execute(const struct sieve_runtime_env *renv, |
254 | | sieve_size_t *address) |
255 | 0 | { |
256 | 0 | const struct sieve_operation *op = renv->oprtn; |
257 | 0 | const struct sieve_extension *this_ext = op->ext; |
258 | 0 | int opt_code = 0; |
259 | 0 | struct sieve_match_type mcht = |
260 | 0 | SIEVE_MATCH_TYPE_DEFAULT(is_match_type); |
261 | 0 | struct sieve_comparator cmp = |
262 | 0 | SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator); |
263 | 0 | bool percent = FALSE; |
264 | 0 | struct sieve_stringlist *value_list, *key_list; |
265 | 0 | const char *score_value; |
266 | 0 | int match, ret; |
267 | | |
268 | | /* Read optional operands */ |
269 | 0 | for (;;) { |
270 | 0 | int opt; |
271 | |
|
272 | 0 | opt = sieve_match_opr_optional_read(renv, address, &opt_code, |
273 | 0 | &ret, &cmp, &mcht); |
274 | 0 | if (opt < 0) |
275 | 0 | return ret; |
276 | 0 | if (opt == 0) |
277 | 0 | break; |
278 | | |
279 | 0 | switch (opt_code) { |
280 | 0 | case OPT_SPAMTEST_PERCENT: |
281 | 0 | percent = TRUE; |
282 | 0 | break; |
283 | 0 | default: |
284 | 0 | sieve_runtime_trace_error( |
285 | 0 | renv, "unknown optional operand"); |
286 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | | /* Read value part */ |
291 | 0 | ret = sieve_opr_stringlist_read(renv, address, "value", &key_list); |
292 | 0 | if (ret <= 0) |
293 | 0 | return ret; |
294 | | |
295 | | /* Perform test */ |
296 | | |
297 | 0 | if (sieve_operation_is(op, spamtest_operation)) { |
298 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, |
299 | 0 | "spamtest test [percent=%s]", |
300 | 0 | (percent ? "true" : "false")); |
301 | 0 | } else { |
302 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, "virustest test"); |
303 | 0 | } |
304 | | |
305 | | /* Get score value */ |
306 | 0 | sieve_runtime_trace_descend(renv); |
307 | 0 | ret = ext_spamvirustest_get_value(renv, this_ext, percent, |
308 | 0 | &score_value); |
309 | 0 | if (ret <= 0) |
310 | 0 | return ret; |
311 | 0 | sieve_runtime_trace_ascend(renv); |
312 | | |
313 | | /* Construct value list */ |
314 | 0 | value_list = sieve_single_stringlist_create_cstr( |
315 | 0 | renv, score_value, TRUE); |
316 | | |
317 | | /* Perform match */ |
318 | 0 | match = sieve_match(renv, &mcht, &cmp, value_list, key_list, &ret); |
319 | 0 | if (match < 0) |
320 | 0 | return ret; |
321 | | |
322 | | /* Set test result for subsequent conditional jump */ |
323 | 0 | sieve_interpreter_set_test_result(renv->interp, match > 0); |
324 | 0 | return SIEVE_EXEC_OK; |
325 | 0 | } |