File: ext-include-binary.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 (489 lines) | stat: -rw-r--r-- 14,036 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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"

#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-script.h"
#include "sieve-binary.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"

#include "sieve-ext-variables.h"

#include "ext-include-common.h"
#include "ext-include-limits.h"
#include "ext-include-variables.h"
#include "ext-include-binary.h"

/*
 * Forward declarations
 */

static bool ext_include_binary_save
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context);
static bool ext_include_binary_open
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context);
static bool ext_include_binary_up_to_date
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context,
		enum sieve_compile_flags cpflags);
static void ext_include_binary_free
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context);

/*
 * Binary include extension
 */

const struct sieve_binary_extension include_binary_ext = {
	&include_extension,
	ext_include_binary_save,
	ext_include_binary_open,
	ext_include_binary_free,
	ext_include_binary_up_to_date
};

/*
 * Binary context management
 */

struct ext_include_binary_context {
	struct sieve_binary *binary;
	struct sieve_binary_block *dependency_block;

	HASH_TABLE(struct sieve_script *,
		   struct ext_include_script_info *) included_scripts;
	ARRAY(struct ext_include_script_info *) include_index;

	struct sieve_variable_scope_binary *global_vars;

	unsigned int outdated:1;
};

static struct ext_include_binary_context *ext_include_binary_create_context
(const struct sieve_extension *this_ext, struct sieve_binary *sbin)
{
	pool_t pool = sieve_binary_pool(sbin);

	struct ext_include_binary_context *ctx =
		p_new(pool, struct ext_include_binary_context, 1);

	ctx->binary = sbin;
	hash_table_create(&ctx->included_scripts, pool, 0,
		sieve_script_hash, sieve_script_cmp);
	p_array_init(&ctx->include_index, pool, 128);

	sieve_binary_extension_set(sbin, this_ext, &include_binary_ext, ctx);

	return ctx;
}

struct ext_include_binary_context *ext_include_binary_get_context
(const struct sieve_extension *this_ext, struct sieve_binary *sbin)
{
	struct ext_include_binary_context *ctx = (struct ext_include_binary_context *)
		sieve_binary_extension_get_context(sbin, this_ext);

	if ( ctx == NULL )
		ctx = ext_include_binary_create_context(this_ext, sbin);

	return ctx;
}

struct ext_include_binary_context *ext_include_binary_init
(const struct sieve_extension *this_ext, struct sieve_binary *sbin,
	struct sieve_ast *ast)
{
	struct ext_include_ast_context *ast_ctx =
		ext_include_get_ast_context(this_ext, ast);
	struct ext_include_binary_context *ctx;

	/* Get/create our context from the binary we are working on */
	ctx = ext_include_binary_get_context(this_ext, sbin);

	/* Create dependency block */
	if ( ctx->dependency_block == 0 )
		ctx->dependency_block =
			sieve_binary_extension_create_block(sbin, this_ext);

	if ( ctx->global_vars == NULL ) {
		ctx->global_vars =
			sieve_variable_scope_binary_create(ast_ctx->global_vars);
		sieve_variable_scope_binary_ref(ctx->global_vars);
	}

	return ctx;
}

/*
 * Script inclusion
 */

const struct ext_include_script_info *ext_include_binary_script_include
(struct ext_include_binary_context *binctx, 
	enum ext_include_script_location location, enum ext_include_flags flags,
	struct sieve_script *script,	struct sieve_binary_block *inc_block)
{
	pool_t pool = sieve_binary_pool(binctx->binary);
	struct ext_include_script_info *incscript;

	incscript = p_new(pool, struct ext_include_script_info, 1);
	incscript->id = array_count(&binctx->include_index)+1;
	incscript->location = location;
	incscript->flags = flags;
	incscript->script = script;
	incscript->block = inc_block;

	/* Unreferenced on binary_free */
	sieve_script_ref(script);

	hash_table_insert(binctx->included_scripts, script, incscript);
	array_append(&binctx->include_index, &incscript, 1);

	return incscript;
}

bool ext_include_binary_script_is_included
(struct ext_include_binary_context *binctx, struct sieve_script *script,
	const struct ext_include_script_info **script_info_r)
{
	struct ext_include_script_info *incscript =
		hash_table_lookup(binctx->included_scripts, script);

	if ( incscript == NULL )
		return FALSE;

	*script_info_r = incscript;
	return TRUE;
}

const struct ext_include_script_info *ext_include_binary_script_get_included
(struct ext_include_binary_context *binctx, unsigned int include_id)
{
	if ( include_id > 0 &&
		(include_id - 1) < array_count(&binctx->include_index) ) {
		struct ext_include_script_info *const *sinfo =
			array_idx(&binctx->include_index, include_id - 1);

		return *sinfo;
	}

	return NULL;
}

const struct ext_include_script_info *ext_include_binary_script_get
(struct ext_include_binary_context *binctx, struct sieve_script *script)
{
	return hash_table_lookup(binctx->included_scripts, script);
}

unsigned int ext_include_binary_script_get_count
(struct ext_include_binary_context *binctx)
{
	return array_count(&binctx->include_index);
}

/*
 * Variables
 */

struct sieve_variable_scope_binary *ext_include_binary_get_global_scope
(const struct sieve_extension *this_ext, struct sieve_binary *sbin)
{
	struct ext_include_binary_context *binctx =
		ext_include_binary_get_context(this_ext, sbin);

	return binctx->global_vars;
}

/*
 * Binary extension
 */

static bool ext_include_binary_save
(const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_binary *sbin ATTR_UNUSED, void *context)
{
	struct ext_include_binary_context *binctx =
		(struct ext_include_binary_context *) context;
	struct ext_include_script_info *const *scripts;
	struct sieve_binary_block *sblock = binctx->dependency_block;
	unsigned int script_count, i;
	bool result = TRUE;

	sieve_binary_block_clear(sblock);

	scripts = array_get(&binctx->include_index, &script_count);

	sieve_binary_emit_unsigned(sblock, script_count);

	for ( i = 0; i < script_count; i++ ) {
		struct ext_include_script_info *incscript = scripts[i];

		if ( incscript->block != NULL ) {
			sieve_binary_emit_unsigned
				(sblock, sieve_binary_block_get_id(incscript->block));
		} else {
			sieve_binary_emit_unsigned(sblock, 0);
		}
		sieve_binary_emit_byte(sblock, incscript->location);
		sieve_binary_emit_cstring(sblock, sieve_script_name(incscript->script));
		sieve_binary_emit_byte(sblock, incscript->flags);
		sieve_script_binary_write_metadata(incscript->script, sblock);
	}

	result = ext_include_variables_save(sblock, binctx->global_vars);

	return result;
}

static bool ext_include_binary_open
(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context)
{
	struct sieve_instance *svinst = ext->svinst;
	struct ext_include_context *ext_ctx =
		(struct ext_include_context *)ext->context;
	struct ext_include_binary_context *binctx =
		(struct ext_include_binary_context *) context;
	struct sieve_binary_block *sblock;
	unsigned int depcount, i, block_id;
	sieve_size_t offset;

	sblock = sieve_binary_extension_get_block(sbin, ext);
	block_id = sieve_binary_block_get_id(sblock);

	offset = 0;

	if ( !sieve_binary_read_unsigned(sblock, &offset, &depcount) ) {
		sieve_sys_error(svinst,
			"include: failed to read include count "
			"for dependency block %d of binary %s", block_id,
			sieve_binary_path(sbin));
		return FALSE;
	}

	/* Check include limit */
	if ( depcount > ext_ctx->max_includes ) {
		sieve_sys_error(svinst,
			"include: binary %s includes too many scripts (%u > %u)",
			sieve_binary_path(sbin), depcount, ext_ctx->max_includes);
		return FALSE;
	}

	/* Read dependencies */
	for ( i = 0; i < depcount; i++ ) {
		unsigned int inc_block_id;
		struct sieve_binary_block *inc_block = NULL;
		unsigned int location, flags;
		string_t *script_name;
		const char *script_location;
		struct sieve_script *script;
		enum sieve_error error;
		int ret;

		if (
			!sieve_binary_read_unsigned(sblock, &offset, &inc_block_id) ||
			!sieve_binary_read_byte(sblock, &offset, &location) ||
			!sieve_binary_read_string(sblock, &offset, &script_name) ||
			!sieve_binary_read_byte(sblock, &offset, &flags) ) {
			/* Binary is corrupt, recompile */
			sieve_sys_error(svinst,
				"include: failed to read included script "
				"from dependency block %d of binary %s", block_id,
				sieve_binary_path(sbin));
			return FALSE;
		}

		if ( inc_block_id != 0 &&
			(inc_block=sieve_binary_block_get(sbin, inc_block_id)) == NULL ) {
			sieve_sys_error(svinst,
				"include: failed to find block %d for included script "
				"from dependency block %d of binary %s", inc_block_id, block_id,
				sieve_binary_path(sbin));
			return FALSE;
		}

		if ( location >= EXT_INCLUDE_LOCATION_INVALID ) {
			/* Binary is corrupt, recompile */
			sieve_sys_error(svinst,
				"include: dependency block %d of binary %s "
				"uses invalid script location (id %d)",
				block_id, sieve_binary_path(sbin), location);
			return FALSE;
		}

		/* Can we find the script dependency ? */
		script_location = ext_include_get_script_location
			(ext, location, str_c(script_name));
		if ( script_location == NULL ) {
			/* No, recompile */
			return FALSE;
		}

		/* Can we open the script dependency ? */
		script = sieve_script_create
			(ext->svinst, script_location, str_c(script_name), NULL, &error);
		if ( script == NULL ) {
			/* No, recompile */
			return FALSE;
		}
		if ( sieve_script_open(script, &error) < 0 ) {			
			if ( error != SIEVE_ERROR_NOT_FOUND ) {
				/* No, recompile */
				sieve_script_unref(&script);
				return FALSE;
			}

			if ( (flags & EXT_INCLUDE_FLAG_OPTIONAL) == 0 ) {
				/* Not supposed to be missing, recompile */
				if ( svinst->debug ) {
					sieve_sys_debug(svinst,
						"include: script '%s' included in binary %s is missing, "
						"so recompile", str_c(script_name), sieve_binary_path(sbin));
				}
				sieve_script_unref(&script);
				return FALSE;
			}

		} else if (inc_block == NULL) {
			/* Script exists, but it is missing from the binary, recompile no matter
			 * what.
			 */
			if ( svinst->debug ) {
				sieve_sys_debug(svinst,
					"include: script '%s' is missing in binary %s, but is now available, "
					"so recompile", str_c(script_name), sieve_binary_path(sbin));
			}
			sieve_script_unref(&script);
			return FALSE;
		}

		/* Can we read script metadata ? */
		if ( (ret=sieve_script_binary_read_metadata
			(script, sblock, &offset))	< 0 ) {
			/* Binary is corrupt, recompile */
			sieve_sys_error(svinst,
				"include: dependency block %d of binary %s "
				"contains invalid script metadata for script %s",
				block_id, sieve_binary_path(sbin), sieve_script_location(script));
			sieve_script_unref(&script);
			return FALSE;
		}

		if ( ret == 0 )
			binctx->outdated = TRUE;

		(void)ext_include_binary_script_include
			(binctx, location, flags, script, inc_block);

		sieve_script_unref(&script);
	}

	if ( !ext_include_variables_load
		(ext, sblock, &offset, &binctx->global_vars) )
		return FALSE;

	return TRUE;
}

static bool ext_include_binary_up_to_date
(const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_binary *sbin ATTR_UNUSED, void *context,
	enum sieve_compile_flags cpflags ATTR_UNUSED)
{
	struct ext_include_binary_context *binctx =
		(struct ext_include_binary_context *) context;

	return !binctx->outdated;
}

static void ext_include_binary_free
(const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_binary *sbin ATTR_UNUSED, void *context)
{
	struct ext_include_binary_context *binctx =
		(struct ext_include_binary_context *) context;
	struct hash_iterate_context *hctx;
	struct sieve_script *script;
	struct ext_include_script_info *incscript;

	/* Release references to all included script objects */
	hctx = hash_table_iterate_init(binctx->included_scripts);
	while ( hash_table_iterate
		(hctx, binctx->included_scripts, &script, &incscript) )
		sieve_script_unref(&incscript->script);
	hash_table_iterate_deinit(&hctx);

	hash_table_destroy(&binctx->included_scripts);

	if ( binctx->global_vars != NULL )
		sieve_variable_scope_binary_unref(&binctx->global_vars);
}

/*
 * Dumping the binary
 */

bool ext_include_binary_dump
(const struct sieve_extension *ext, struct sieve_dumptime_env *denv)
{
	struct sieve_binary *sbin = denv->sbin;
	struct ext_include_binary_context *binctx =
		ext_include_binary_get_context(ext, sbin);
	struct hash_iterate_context *hctx;
	struct sieve_script *script;
	struct ext_include_script_info *incscript;

	if ( !ext_include_variables_dump(denv, binctx->global_vars) )
		return FALSE;

	hctx = hash_table_iterate_init(binctx->included_scripts);
	while ( hash_table_iterate
		(hctx, binctx->included_scripts, &script, &incscript) ) {

		if ( incscript->block == NULL ) {
			sieve_binary_dump_sectionf(denv, "Included %s script '%s' (MISSING)",
				ext_include_script_location_name(incscript->location),
				sieve_script_name(incscript->script));

		} else {
			unsigned int block_id = sieve_binary_block_get_id(incscript->block);

			sieve_binary_dump_sectionf(denv, "Included %s script '%s' (block: %d)",
				ext_include_script_location_name(incscript->location),
				sieve_script_name(incscript->script), block_id);

			denv->sblock = incscript->block;
			denv->cdumper = sieve_code_dumper_create(denv);

			if ( denv->cdumper == NULL )
				return FALSE;

			sieve_code_dumper_run(denv->cdumper);
			sieve_code_dumper_free(&(denv->cdumper));
		}
	}
	hash_table_iterate_deinit(&hctx);

	return TRUE;
}

bool ext_include_code_dump
(const struct sieve_extension *ext, const struct sieve_dumptime_env *denv,
	sieve_size_t *address ATTR_UNUSED)
{
	struct sieve_binary *sbin = denv->sbin;
	struct ext_include_binary_context *binctx =
		ext_include_binary_get_context(ext, sbin);
	struct ext_include_context *ectx = ext_include_get_context(ext);

	sieve_ext_variables_dump_set_scope
		(ectx->var_ext, denv, ext,
			sieve_variable_scope_binary_get(binctx->global_vars));

	return TRUE;
}