File: cmd-pipe.c

package info (click to toggle)
dovecot 1%3A2.2.27-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 48,308 kB
  • sloc: ansic: 430,242; sh: 17,401; makefile: 6,581; cpp: 1,557; perl: 295; xml: 44; pascal: 27; python: 27
file content (386 lines) | stat: -rw-r--r-- 8,856 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
/* Copyright (c) 2002-2016 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "array.h"
#include "str.h"
#include "str-sanitize.h"

#include "sieve-common.h"
#include "sieve-stringlist.h"
#include "sieve-code.h"
#include "sieve-message.h"
#include "sieve-extensions.h"
#include "sieve-commands.h"
#include "sieve-actions.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "sieve-result.h"

#include "sieve-extprograms-common.h"

/* Pipe command
 *
 * Syntax:
 *   pipe [":copy"] [":try"] <program-name: string> [<arguments: string-list>]
 *
 */

static bool cmd_pipe_registered
	(struct sieve_validator *valdtr, const struct sieve_extension *ext,
		struct sieve_command_registration *cmd_reg);
static bool cmd_pipe_generate
	(const struct sieve_codegen_env *cgenv, 
		struct sieve_command *ctx);

const struct sieve_command_def cmd_pipe = {
	.identifier = "pipe",
	.type = SCT_COMMAND,
	.positional_args = -1, /* We check positional arguments ourselves */
	.subtests = 0,
	.block_allowed = FALSE,
	.block_required = FALSE,
	.registered = cmd_pipe_registered,
	.validate = sieve_extprogram_command_validate,
	.generate = cmd_pipe_generate
};

/*
 * Tagged arguments
 */

static const struct sieve_argument_def pipe_try_tag = { 
	.identifier = "try"
};

/* 
 * Pipe operation 
 */

static bool cmd_pipe_operation_dump
	(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int cmd_pipe_operation_execute
	(const struct sieve_runtime_env *renv, sieve_size_t *address);

const struct sieve_operation_def cmd_pipe_operation = { 
	.mnemonic = "PIPE",
	.ext_def = &vnd_pipe_extension,
	.dump = cmd_pipe_operation_dump, 
	.execute = cmd_pipe_operation_execute
};

/* Codes for optional operands */

enum cmd_pipe_optional {
  OPT_END,
  OPT_TRY
};

/* 
 * Pipe action 
 */

/* Forward declarations */

static int act_pipe_check_duplicate
	(const struct sieve_runtime_env *renv, 
		const struct sieve_action *act,
		const struct sieve_action *act_other);
static void act_pipe_print
	(const struct sieve_action *action,
		const struct sieve_result_print_env *rpenv, bool *keep);	
static int act_pipe_commit
	(const struct sieve_action *action,	
		const struct sieve_action_exec_env *aenv, void *tr_context, bool *keep);

/* Action object */

const struct sieve_action_def act_pipe = {
	.name = "pipe",
	.flags = SIEVE_ACTFLAG_TRIES_DELIVER,
	.check_duplicate = act_pipe_check_duplicate, 
	.print = act_pipe_print,
	.commit = act_pipe_commit
};

/* Action context information */
		
struct ext_pipe_action {
	const char *program_name;
	const char * const *args;
	bool try;
};

/*
 * Command registration
 */

static bool cmd_pipe_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
	struct sieve_command_registration *cmd_reg)
{
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &pipe_try_tag, OPT_TRY);

	return TRUE;
}

/*
 * Code generation
 */

static bool cmd_pipe_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
	sieve_operation_emit(cgenv->sblock, cmd->ext, &cmd_pipe_operation);

	/* Generate arguments */
	if ( !sieve_generate_arguments(cgenv, cmd, NULL) )
		return FALSE;

	/* Emit a placeholder when the <arguments> argument is missing */
	if ( sieve_ast_argument_next(cmd->first_positional) == NULL )
		sieve_opr_omitted_emit(cgenv->sblock);

	return TRUE;
}

/* 
 * Code dump
 */
 
static bool cmd_pipe_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{	
	int opt_code = 0;
	
	sieve_code_dumpf(denv, "PIPE");
	sieve_code_descend(denv);	

	/* Dump optional operands */
	for (;;) {
		int opt;

		if ( (opt=sieve_action_opr_optional_dump(denv, address, &opt_code)) < 0 )
			return FALSE;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_TRY:
			sieve_code_dumpf(denv, "try");	
			break;
		default:
			return FALSE;
		}
	}
	
	if ( !sieve_opr_string_dump(denv, address, "program-name") )
		return FALSE;

	return sieve_opr_stringlist_dump_ex(denv, address, "arguments", "");
}

/* 
 * Code execution
 */

static int cmd_pipe_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address)
{	
	const struct sieve_extension *this_ext = renv->oprtn->ext;
	struct sieve_side_effects_list *slist = NULL;
	struct ext_pipe_action *act;
	pool_t pool;
	int opt_code = 0;
	struct sieve_stringlist *args_list = NULL;
	string_t *pname = NULL;
	bool try = FALSE;
	int ret;

	/*
	 * Read operands
	 */

	/* Optional operands */	

	for (;;) {
		int opt;

		if ( (opt=sieve_action_opr_optional_read
			(renv, address, &opt_code, &ret, &slist)) < 0 )
			return ret;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_TRY:
			try = TRUE;
			break;
		default:
			sieve_runtime_trace_error(renv, "unknown optional operand");
			return SIEVE_EXEC_BIN_CORRUPT;
		}
	}

	/* Fixed operands */

	if ( (ret=sieve_extprogram_command_read_operands
		(renv, address, &pname, &args_list)) <= 0 )
		return ret;

	/*
	 * Perform operation
	 */

	/* Trace */

	sieve_runtime_trace(renv, SIEVE_TRLVL_ACTIONS, "pipe action");	

	/* Compose action */

	pool = sieve_result_pool(renv->result);
	act = p_new(pool, struct ext_pipe_action, 1);

	if ( args_list != NULL &&
		sieve_stringlist_read_all(args_list, pool, &act->args) < 0 ) {
		sieve_runtime_trace_error(renv, "failed to read args operand");
		return args_list->exec_status;
	}
	
	act->program_name = p_strdup(pool, str_c(pname));
	act->try = try;

	if ( sieve_result_add_action
		(renv, this_ext, &act_pipe, slist, (void *) act, 0, TRUE) < 0 ) {
		return SIEVE_EXEC_FAILURE;
	}

	return SIEVE_EXEC_OK;
}

/*
 * Action
 */

/* Runtime verification */

static int act_pipe_check_duplicate
(const struct sieve_runtime_env *renv ATTR_UNUSED, 
	const struct sieve_action *act,
	const struct sieve_action *act_other)
{
	struct ext_pipe_action *new_act, *old_act;
		
	if ( act->context == NULL || act_other->context == NULL )
		return 0;

	new_act = (struct ext_pipe_action *) act->context;
	old_act = (struct ext_pipe_action *) act_other->context;

	if ( strcmp(new_act->program_name, old_act->program_name) == 0 ) {
		sieve_runtime_error(renv, act->location,
			"duplicate pipe \"%s\" action not allowed "
			"(previously triggered one was here: %s)",
			new_act->program_name, act_other->location);
		return -1;
	}

	return 0;
}

/* Result printing */
 
static void act_pipe_print
(const struct sieve_action *action,	const struct sieve_result_print_env *rpenv, 
	bool *keep ATTR_UNUSED)	
{
	const struct ext_pipe_action *act = 
		(const struct ext_pipe_action *) action->context;

	sieve_result_action_printf
		( rpenv, "pipe message to external program '%s':", act->program_name);
	
	/* Print main method parameters */

	sieve_result_printf
		( rpenv, "    => try           : %s\n", (act->try ? "yes" : "no") );

	/* FIXME: print args */

	/* Finish output with an empty line */

	sieve_result_printf(rpenv, "\n");
}

/* Result execution */

static int act_pipe_commit
(const struct sieve_action *action,
	const struct sieve_action_exec_env *aenv, 
	void *tr_context ATTR_UNUSED, bool *keep)
{
	const struct ext_pipe_action *act = 
		(const struct ext_pipe_action *) action->context;
	enum sieve_error error = SIEVE_ERROR_NONE;
	struct mail *mail =	( action->mail != NULL ?
		action->mail : sieve_message_get_mail(aenv->msgctx) );
	struct sieve_extprogram *sprog;
	int ret;

	sprog = sieve_extprogram_create
		(action->ext, aenv->scriptenv, aenv->msgdata, "pipe",
			act->program_name, act->args, &error);
	if ( sprog != NULL ) {
		if ( sieve_extprogram_set_input_mail(sprog, mail) < 0 ) {
			sieve_extprogram_destroy(&sprog);
			return sieve_result_mail_error(aenv, mail,
				"pipe action: failed to read input message");
		}
		ret = sieve_extprogram_run(sprog);
	} else {
		ret = -1;
	}
	if ( sprog != NULL )
		sieve_extprogram_destroy(&sprog);

	if ( ret > 0 ) {
		sieve_result_global_log(aenv, "pipe action: "
			"piped message to program `%s'", str_sanitize(act->program_name, 128));

		/* Indicate that message was successfully 'forwarded' */
		aenv->exec_status->message_forwarded = TRUE;
	} else {
		if ( ret < 0 ) {
			if ( error == SIEVE_ERROR_NOT_FOUND ) {
				sieve_result_error(aenv, "pipe action: "
					"failed to pipe message to program: program `%s' not found",
					str_sanitize(act->program_name, 80));						
			} else {
				sieve_extprogram_exec_error(aenv->ehandler, NULL,
					"pipe action: failed to pipe message to program `%s'",
					str_sanitize(act->program_name, 80));
			}
		} else {
			sieve_extprogram_exec_error(aenv->ehandler, NULL,
				"pipe action: failed to execute to program `%s'",
				str_sanitize(act->program_name, 80));
		}

		if ( act->try ) return SIEVE_EXEC_OK;

		return SIEVE_EXEC_FAILURE;
	}

	*keep = FALSE;
	return SIEVE_EXEC_OK;
}