File: ext-spamvirustest-common.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (649 lines) | stat: -rw-r--r-- 15,451 bytes parent folder | download | duplicates (3)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "strfuncs.h"

#include "sieve-common.h"
#include "sieve-settings.h"
#include "sieve-error.h"
#include "sieve-extensions.h"
#include "sieve-message.h"
#include "sieve-interpreter.h"
#include "sieve-runtime-trace.h"

#include "ext-spamvirustest-common.h"

#include <sys/types.h>
#include <regex.h>
#include <ctype.h>

/*
 * Extension data
 */

enum ext_spamvirustest_status_type {
	EXT_SPAMVIRUSTEST_STATUS_TYPE_SCORE,
	EXT_SPAMVIRUSTEST_STATUS_TYPE_STRLEN,
	EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT,
};

struct ext_spamvirustest_header_spec {
	const char *header_name;
	regex_t regexp;
	bool regexp_match;
};

struct ext_spamvirustest_data {
	pool_t pool;

	int reload;

	struct ext_spamvirustest_header_spec status_header;
	struct ext_spamvirustest_header_spec max_header;

	enum ext_spamvirustest_status_type status_type;

	float max_value;

	const char *text_values[11];
};

/*
 * Regexp utility
 */

static bool _regexp_compile
(regex_t *regexp, const char *data, const char **error_r)
{
	size_t errsize;
	int ret;

	*error_r = "";

	if ( (ret=regcomp(regexp, data, REG_EXTENDED)) == 0 ) {
		return TRUE;
	}

	errsize = regerror(ret, regexp, NULL, 0);

	if ( errsize > 0 ) {
		char *errbuf = t_malloc(errsize);

		(void)regerror(ret, regexp, errbuf, errsize);

		/* We don't want the error to start with a capital letter */
		errbuf[0] = i_tolower(errbuf[0]);

		*error_r = errbuf;
	}

	return FALSE;
}

static const char *_regexp_match_get_value
(const char *string, int index, regmatch_t pmatch[], int nmatch)
{
	if ( index > -1 && index < nmatch && pmatch[index].rm_so != -1 ) {
		return t_strndup(string + pmatch[index].rm_so,
						pmatch[index].rm_eo - pmatch[index].rm_so);
	}
	return NULL;
}

/*
 * Configuration parser
 */

static bool ext_spamvirustest_header_spec_parse
(struct ext_spamvirustest_header_spec *spec, pool_t pool, const char *data,
	const char **error_r)
{
	const char *p;
	const char *regexp_error;

	if ( *data == '\0' ) {
		*error_r = "empty header specification";
		return FALSE;
	}

	/* Parse header name */

	p = data;

	while ( *p == ' ' || *p == '\t' ) p++;
	while ( *p != ':' && *p != '\0' && *p != ' ' && *p != '\t' ) p++;

	if ( *p == '\0' ) {
		spec->header_name = p_strdup(pool, data);
		return TRUE;
	}

	spec->header_name = p_strdup_until(pool, data, p);
	while ( *p == ' ' || *p == '\t' ) p++;

	if ( *p == '\0' ) {
		spec->regexp_match = FALSE;
		return TRUE;
	}

	/* Parse and compile regular expression */

	if ( *p != ':' ) {
		*error_r = t_strdup_printf("expecting ':', but found '%c'", *p);
		return FALSE;
	}
	p++;
	while ( *p == ' ' || *p == '\t' ) p++;

	spec->regexp_match = TRUE;
	if ( !_regexp_compile(&spec->regexp, p, &regexp_error) ) {
		*error_r = t_strdup_printf("failed to compile regular expression '%s': "
			"%s", p, regexp_error);
		return FALSE;
	}

	return TRUE;
}

static void ext_spamvirustest_header_spec_free
(struct ext_spamvirustest_header_spec *spec)
{
	regfree(&spec->regexp);
}

static bool ext_spamvirustest_parse_strlen_value
(const char *str_value, float *value_r, const char **error_r)
{
	const char *p = str_value;
	char ch = *p;

	if ( *str_value == '\0' ) {
		*value_r = 0;
		return TRUE;
	}

	while ( *p == ch ) p++;

	if ( *p != '\0' ) {
		*error_r = t_strdup_printf(
			"different character '%c' encountered in strlen value",
			*p);
		return FALSE;
	}

	*value_r = ( p - str_value );

	return TRUE;
}

static bool ext_spamvirustest_parse_decimal_value
(const char *str_value, float *value_r, const char **error_r)
{
	const char *p = str_value;
	float value;
	float sign = 1;
	int digits;

	if ( *p == '\0' ) {
		*error_r = "empty value";
		return FALSE;
	}

	if ( *p == '+' || *p == '-' ) {
		if ( *p == '-' )
			sign = -1;

		p++;
	}

	value = 0;
	digits = 0;
	while ( i_isdigit(*p) ) {
		value = value*10 + (*p-'0');
		if ( digits++ > 4 ) {
			*error_r = t_strdup_printf
				("decimal value has too many digits before radix point: %s",
					str_value);
			return FALSE;
		}
		p++;
	}

	if ( *p == '.' || *p == ',' ) {
		float radix = .1;
		p++;

		digits = 0;
		while ( i_isdigit(*p) ) {
			value = value + (*p-'0')*radix;

			if ( digits++ > 4 ) {
				*error_r = t_strdup_printf
					("decimal value has too many digits after radix point: %s",
						str_value);
				return FALSE;
			}
			radix /= 10;
			p++;
		}
	}

	if ( *p != '\0' ) {
		*error_r = t_strdup_printf
			("invalid decimal point value: %s", str_value);
		return FALSE;
	}

	*value_r = value * sign;

	return TRUE;
}

/*
 * Extension initialization
 */

bool ext_spamvirustest_load
(const struct sieve_extension *ext, void **context)
{
	struct ext_spamvirustest_data *ext_data =
		(struct ext_spamvirustest_data *) *context;
	struct sieve_instance *svinst = ext->svinst;
	const char *ext_name, *status_header, *max_header, *status_type,
		*max_value;
	enum ext_spamvirustest_status_type type;
	const char *error;
	pool_t pool;
	bool result = TRUE;
	int reload = 0;

	if ( *context != NULL ) {
		reload = ext_data->reload + 1;
		ext_spamvirustest_unload(ext);
		*context = NULL;
	}

	/* FIXME:
	 *   Prevent loading of both spamtest and spamtestplus: let these share
	 *   contexts.
	 */

	if ( sieve_extension_is(ext, spamtest_extension) ||
		sieve_extension_is(ext, spamtestplus_extension) ) {
		ext_name = spamtest_extension.name;
	} else {
		ext_name = sieve_extension_name(ext);
	}

	/* Get settings */

	status_header = sieve_setting_get
		(svinst, t_strconcat("sieve_", ext_name, "_status_header", NULL));
	status_type = sieve_setting_get
		(svinst, t_strconcat("sieve_", ext_name, "_status_type", NULL));
	max_header = sieve_setting_get
		(svinst, t_strconcat("sieve_", ext_name, "_max_header", NULL));
	max_value = sieve_setting_get
		(svinst, t_strconcat("sieve_", ext_name, "_max_value", NULL));

	/* Base configuration */

	if ( status_header == NULL ) {
		return TRUE;
	}

	if ( status_type == NULL || strcmp(status_type, "score") == 0 ) {
		type = EXT_SPAMVIRUSTEST_STATUS_TYPE_SCORE;
	} else if ( strcmp(status_type, "strlen") == 0 ) {
		type = EXT_SPAMVIRUSTEST_STATUS_TYPE_STRLEN;
	} else if ( strcmp(status_type, "text") == 0 ) {
		type = EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT;
	} else {
		sieve_sys_error(svinst,
			"%s: invalid status type '%s'", ext_name, status_type);
		return FALSE;
	}

	/* Verify settings */

	if ( type != EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT ) {

		if ( max_header != NULL && max_value != NULL ) {
			sieve_sys_error(svinst,
				"%s: sieve_%s_max_header and sieve_%s_max_value "
				"cannot both be configured", ext_name, ext_name, ext_name);
			return TRUE;
		}

		if ( max_header == NULL && max_value == NULL ) {
			sieve_sys_error(svinst,
				"%s: none of sieve_%s_max_header or sieve_%s_max_value "
				"is configured", ext_name, ext_name, ext_name);
			return TRUE;
		}
	} else {
		if ( max_header != NULL ) {
			sieve_sys_warning(svinst,
				"%s: setting sieve_%s_max_header has no meaning "
				"for sieve_%s_status_type=text", ext_name, ext_name, ext_name);
		}

		if ( max_value != NULL ) {
			sieve_sys_warning(svinst,
				"%s: setting sieve_%s_max_value has no meaning "
				"for sieve_%s_status_type=text", ext_name, ext_name, ext_name);
		}
	}

	pool = pool_alloconly_create("spamvirustest_data", 512);
	ext_data = p_new(pool, struct ext_spamvirustest_data, 1);
	ext_data->pool = pool;
	ext_data->reload = reload;
	ext_data->status_type = type;

	if ( !ext_spamvirustest_header_spec_parse
		(&ext_data->status_header, ext_data->pool, status_header, &error) ) {
		sieve_sys_error(svinst,
			"%s: invalid status header specification "
			"'%s': %s", ext_name, status_header, error);
		result = FALSE;
	}

	if ( result ) {
		if ( type != EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT ) {
			/* Parse max header */

			if ( max_header != NULL && !ext_spamvirustest_header_spec_parse
				(&ext_data->max_header, ext_data->pool, max_header, &error) ) {
				sieve_sys_error(svinst,
					"%s: invalid max header specification "
					"'%s': %s", ext_name, max_header, error);
				result = FALSE;
			}

			/* Parse max value */

			if ( result && max_value != NULL ) {
				if ( !ext_spamvirustest_parse_decimal_value
					(max_value, &ext_data->max_value, &error) ) {
					sieve_sys_error(svinst,
						"%s: invalid max value specification "
						"'%s': %s", ext_name, max_value, error);
					result = FALSE;
				}
			}

		} else {
			unsigned int i, max_text;

			max_text = ( sieve_extension_is(ext, virustest_extension) ? 5 : 10 );

			/* Get text values */
			for ( i = 0; i <= max_text; i++ ) {
				const char *value = sieve_setting_get
					(svinst, t_strdup_printf("sieve_%s_text_value%d", ext_name, i));

				if ( value != NULL && *value != '\0' )
					ext_data->text_values[i] = p_strdup(ext_data->pool, value);
			}

			ext_data->max_value = 1;
		}
	}

	if ( result ) {
		*context = (void *) ext_data;
	} else {
		sieve_sys_warning(svinst,
			"%s: extension not configured, tests will always match against \"0\"",
			ext_name);
		ext_spamvirustest_unload(ext);
		*context = NULL;
	}

	return result;
}

void ext_spamvirustest_unload(const struct sieve_extension *ext)
{
	struct ext_spamvirustest_data *ext_data =
		(struct ext_spamvirustest_data *) ext->context;

	if ( ext_data != NULL ) {
		ext_spamvirustest_header_spec_free(&ext_data->status_header);
		ext_spamvirustest_header_spec_free(&ext_data->max_header);
		pool_unref(&ext_data->pool);
	}
}

/*
 * Runtime
 */

struct ext_spamvirustest_message_context {
	int reload;
	float score_ratio;
};

static const char *ext_spamvirustest_get_score
(const struct sieve_extension *ext, float score_ratio, bool percent)
{
	int score;

	if ( score_ratio < 0 )
		return "0";

	if ( score_ratio > 1 )
		score_ratio = 1;

	if ( percent )
		score = score_ratio * 100 + 0.001;
	else if ( sieve_extension_is(ext, virustest_extension) )
		score = score_ratio * 4 + 1.001;
	else
		score = score_ratio * 9 + 1.001;

	return t_strdup_printf("%d", score);
}

const char *ext_spamvirustest_get_value
(const struct sieve_runtime_env *renv, const struct sieve_extension *ext,
	 bool percent)
{
	struct ext_spamvirustest_data *ext_data =
		(struct ext_spamvirustest_data *) ext->context;
	struct ext_spamvirustest_header_spec *status_header, *max_header;
	const struct sieve_message_data *msgdata = renv->msgdata;
	struct sieve_message_context *msgctx = renv->msgctx;
	struct ext_spamvirustest_message_context *mctx;
	regmatch_t match_values[2];
	const char *header_value, *error;
	const char *status = NULL, *max = NULL;
	float status_value, max_value;
	unsigned int i, max_text;
	pool_t pool = sieve_interpreter_pool(renv->interp);

	/*
	 * Check whether extension is properly configured
	 */
	if ( ext_data == NULL ) {
		sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
			"error: extension not configured");
		return "0";
	}

	/*
	 * Check wether a cached result is available
	 */

	mctx = 	(struct ext_spamvirustest_message_context *)
		sieve_message_context_extension_get(msgctx, ext);

	if ( mctx == NULL ) {
		mctx = p_new(pool, struct ext_spamvirustest_message_context, 1);
		sieve_message_context_extension_set(msgctx, ext, (void *)mctx);
	} else if ( mctx->reload == ext_data->reload ) {
		return ext_spamvirustest_get_score(ext, mctx->score_ratio, percent);
	}

	mctx->reload = ext_data->reload;

	/*
	 * Get max status value
	 */

	status_header = &ext_data->status_header;
	max_header = &ext_data->max_header;

	if ( ext_data->status_type != EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT ) {
		if ( max_header->header_name != NULL ) {
			/* Get header from message */
			if ( mail_get_first_header_utf8
				(msgdata->mail, max_header->header_name, &header_value) < 0 ||
				header_value == NULL ) {
				sieve_runtime_trace(renv,  SIEVE_TRLVL_TESTS,
					"header '%s' not found in message",
					max_header->header_name);
				goto failed;
			}

			if ( max_header->regexp_match ) {
				/* Execute regex */
				if ( regexec(&max_header->regexp, header_value, 2, match_values, 0)
					!= 0 ) {
					sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
						"regexp for header '%s' did not match "
						"on value '%s'", max_header->header_name, header_value);
					goto failed;
				}

				max = _regexp_match_get_value(header_value, 1, match_values, 2);
				if ( max == NULL ) {
					sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
						"regexp did not return match value "
						"for string '%s'", header_value);
					goto failed;
				}
			} else {
				max = header_value;
			}

			if ( !ext_spamvirustest_parse_decimal_value(max, &max_value, &error) ) {
				sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
					"failed to parse maximum value: %s", error);
				goto failed;
			}
		} else {
			max_value = ext_data->max_value;
		}

		if ( max_value == 0 ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"error: max value is 0");
			goto failed;
		}
	} else {
		max_value = ( sieve_extension_is(ext, virustest_extension) ? 5 : 10 );
	}

	/*
	 * Get status value
	 */

	/* Get header from message */
	if ( mail_get_first_header_utf8
		(msgdata->mail, status_header->header_name, &header_value) < 0 ||
		header_value == NULL ) {
		sieve_runtime_trace(renv,  SIEVE_TRLVL_TESTS,
			"header '%s' not found in message",
			status_header->header_name);
		goto failed;
	}

	/* Execute regex */
	if ( status_header->regexp_match ) {
		if ( regexec(&status_header->regexp, header_value, 2, match_values, 0)
			!= 0 ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"regexp for header '%s' did not match on value '%s'",
				status_header->header_name, header_value);
			goto failed;
		}

		status = _regexp_match_get_value(header_value, 1, match_values, 2);
		if ( status == NULL ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"regexp did not return match value for string '%s'",
				header_value);
			goto failed;
		}
	} else {
		status = header_value;
	}

	switch ( ext_data->status_type ) {
	case EXT_SPAMVIRUSTEST_STATUS_TYPE_SCORE:
		if ( !ext_spamvirustest_parse_decimal_value
			(status, &status_value, &error) ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"failed to parse status value '%s': %s",
				status, error);
			goto failed;
		}
		break;
	case EXT_SPAMVIRUSTEST_STATUS_TYPE_STRLEN:
		if ( !ext_spamvirustest_parse_strlen_value
			(status, &status_value, &error) ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"failed to parse status value '%s': %s",
				status, error);
			goto failed;
		}
		break;
	case EXT_SPAMVIRUSTEST_STATUS_TYPE_TEXT:
		max_text = ( sieve_extension_is(ext, virustest_extension) ? 5 : 10 );
		status_value = 0;

		i = 0;
		while ( i <= max_text ) {
			if ( ext_data->text_values[i] != NULL &&
				strcmp(status, ext_data->text_values[i]) == 0 ) {
				status_value = (float) i;
				break;
			}
			i++;
		}

		if ( i > max_text ) {
			sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
				"failed to match textstatus value '%s'",
				status);
			goto failed;
		}
		break;
	default:
		i_unreached();
		break;
	}

	/* Calculate value */
	if ( status_value < 0 )
		mctx->score_ratio = 0;
	else if ( status_value > max_value )
		mctx->score_ratio = 1;
	else
		mctx->score_ratio = (status_value / max_value);

	sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
		"extracted score=%.3f, max=%.3f, ratio=%.0f %%",
		status_value, max_value, mctx->score_ratio * 100);

	return ext_spamvirustest_get_score(ext, mctx->score_ratio, percent);

failed:
	mctx->score_ratio = -1;
	return "0";
}