~ubuntu-branches/ubuntu/quantal/dovecot/quantal

« back to all changes in this revision

Viewing changes to sieve/src/lib-sieve/plugins/variables/ext-variables-arguments.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-06-29 09:21:32 UTC
  • mfrom: (4.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20100629092132-q4pr5lfuvmjqou19
Tags: 1:1.2.12-1ubuntu1
* Merge from Debian Unstable, remaining changes:
  + Add mail-stack-delivery as per server-maverick-mail-integration spec:
   - Update debian/rules
   - Convert existing package to a dummy package and add new binary in debian/control
   - Update maintainer scripts.
   - Move previously installed backups and config files to new package name
     space in preinst
   - Add new debian/mail-stack-delivery.prerm to handle downgrades
   - Rename debian/dovecot-postfix.* to debian/mail-stack-delivery.*
  + Use Snakeoil SSL certificates by default.
    - debian/control: Depend on ssl-cert.
    - debian/patches/ssl-cert-snakeoil.dpatch: Change default SSL cert paths to snakeoil.
    - debian/dovecot-common.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - Created debian/dovecot-common.ufw.profile.
    - debian/rules: install profile.
    - debian/control: suggest ufw.
  + debian/{control,rules}: enable PIE hardening.
  + debian/control: Update Vcs-* headers.
  + Add SMTP-AUTH support for Outlook (login auth mechanism) 
  + debian/dovecot-common.dirs: Added usr/share/doc/dovecot-common
  + debian/patches/fix-dovecot-config-parser.patch: Fix ordering of external config 
    files. (LP: #597818)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "ext-variables-namespaces.h"
22
22
#include "ext-variables-arguments.h"
23
23
 
24
 
/* 
 
24
/*
25
25
 * Variable argument implementation
26
26
 */
27
27
 
28
28
static bool arg_variable_generate
29
 
        (const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg, 
 
29
        (const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
30
30
                struct sieve_command *context);
31
31
 
32
 
const struct sieve_argument_def variable_argument = { 
33
 
        "@variable", 
 
32
const struct sieve_argument_def variable_argument = {
 
33
        "@variable",
34
34
        NULL, NULL, NULL, NULL,
35
 
        arg_variable_generate 
 
35
        arg_variable_generate
36
36
};
37
37
 
38
38
static bool ext_variables_variable_argument_activate
39
 
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr, 
 
39
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
40
40
        struct sieve_ast_argument *arg, const char *variable)
41
41
{
42
42
        struct sieve_ast *ast = arg->ast;
43
43
        struct sieve_variable *var;
44
 
        
 
44
 
45
45
        var = ext_variables_validator_get_variable(this_ext, valdtr, variable, TRUE);
46
46
 
47
47
        if ( var == NULL ) {
48
 
                sieve_argument_validate_error(valdtr, arg, 
 
48
                sieve_argument_validate_error(valdtr, arg,
49
49
                        "(implicit) declaration of new variable '%s' exceeds the limit "
50
 
                        "(max variables: %u)", variable, 
 
50
                        "(max variables: %u)", variable,
51
51
                        EXT_VARIABLES_MAX_SCOPE_SIZE);
52
52
                return FALSE;
53
53
        }
54
 
        
 
54
 
55
55
        arg->argument = sieve_argument_create(ast, &variable_argument, this_ext, 0);
56
56
        arg->argument->data = (void *) var;
57
 
        
 
57
 
58
58
        return arg;
59
59
}
60
60
 
61
61
static struct sieve_ast_argument *ext_variables_variable_argument_create
62
 
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr, 
 
62
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
63
63
        struct sieve_ast_argument *parent_arg, const char *variable)
64
64
{
65
65
        struct sieve_ast *ast = parent_arg->ast;
66
66
        struct sieve_ast_argument *new_arg;
67
 
                
 
67
 
68
68
        new_arg = sieve_ast_argument_create(ast, sieve_ast_argument_line(parent_arg));
69
69
        new_arg->type = SAAT_STRING;
70
70
 
71
71
        if ( !ext_variables_variable_argument_activate
72
72
                (this_ext, valdtr, new_arg, variable) )
73
73
                return NULL;
74
 
        
 
74
 
75
75
        return new_arg;
76
76
}
77
77
 
78
78
static bool arg_variable_generate
79
 
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg, 
 
79
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
80
80
        struct sieve_command *context ATTR_UNUSED)
81
81
{
82
82
        struct sieve_argument *argument = arg->argument;
83
83
        struct sieve_variable *var = (struct sieve_variable *) argument->data;
84
 
        
 
84
 
85
85
        sieve_variables_opr_variable_emit(cgenv->sbin, argument->ext, var);
86
86
 
87
87
        return TRUE;
88
88
}
89
89
 
90
 
/* 
 
90
/*
91
91
 * Match value argument implementation
92
92
 */
93
93
 
94
94
static bool arg_match_value_generate
95
 
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg, 
 
95
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
96
96
        struct sieve_command *context ATTR_UNUSED);
97
97
 
98
 
const struct sieve_argument_def match_value_argument = { 
99
 
        "@match_value", 
 
98
const struct sieve_argument_def match_value_argument = {
 
99
        "@match_value",
100
100
        NULL, NULL, NULL, NULL,
101
 
        arg_match_value_generate 
 
101
        arg_match_value_generate
102
102
};
103
103
 
104
104
static bool ext_variables_match_value_argument_activate
105
 
(const struct sieve_extension *this_ext, 
106
 
        struct sieve_validator *valdtr, struct sieve_ast_argument *arg, 
 
105
(const struct sieve_extension *this_ext,
 
106
        struct sieve_validator *valdtr, struct sieve_ast_argument *arg,
107
107
        unsigned int index, bool assignment)
108
108
{
109
109
        struct sieve_ast *ast = arg->ast;
110
110
 
111
111
        if ( assignment ) {
112
 
                sieve_argument_validate_error(valdtr, arg, 
 
112
                sieve_argument_validate_error(valdtr, arg,
113
113
                        "cannot assign to match variable");
114
114
                return FALSE;
115
115
        }
116
116
 
117
117
        if ( index > EXT_VARIABLES_MAX_MATCH_INDEX ) {
118
 
                sieve_argument_validate_error(valdtr, arg, 
119
 
                        "match value index %u out of range (max: %u)", index, 
 
118
                sieve_argument_validate_error(valdtr, arg,
 
119
                        "match value index %u out of range (max: %u)", index,
120
120
                        EXT_VARIABLES_MAX_MATCH_INDEX);
121
121
                return FALSE;
122
 
        } 
 
122
        }
123
123
 
124
124
        arg->argument = sieve_argument_create
125
125
                (ast, &match_value_argument, this_ext, 0);
128
128
}
129
129
 
130
130
static struct sieve_ast_argument *ext_variables_match_value_argument_create
131
 
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr, 
 
131
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
132
132
        struct sieve_ast_argument *parent_arg, unsigned int index)
133
133
{
134
134
        struct sieve_ast *ast = parent_arg->ast;
135
135
        struct sieve_ast_argument *new_arg;
136
 
        
 
136
 
137
137
        new_arg = sieve_ast_argument_create(ast, sieve_ast_argument_line(parent_arg));
138
138
        new_arg->type = SAAT_STRING;
139
139
 
141
141
                (this_ext, valdtr, new_arg, index, FALSE) ) {
142
142
                return NULL;
143
143
  }
144
 
        
 
144
 
145
145
        return new_arg;
146
146
}
147
147
 
148
148
static bool arg_match_value_generate
149
 
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg, 
 
149
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
150
150
        struct sieve_command *context ATTR_UNUSED)
151
151
{
152
152
        struct sieve_argument *argument = arg->argument;
153
153
        unsigned int index = POINTER_CAST_TO(argument->data, unsigned int);
154
 
        
 
154
 
155
155
        sieve_variables_opr_match_value_emit(cgenv->sbin, argument->ext, index);
156
156
 
157
157
        return TRUE;
158
158
}
159
159
 
160
 
/* 
 
160
/*
161
161
 * Variable string argument implementation
162
162
 */
163
163
 
164
164
static bool arg_variable_string_validate
165
 
        (struct sieve_validator *valdtr, struct sieve_ast_argument **arg, 
 
165
        (struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
166
166
                struct sieve_command *cmd);
167
167
 
168
 
const struct sieve_argument_def variable_string_argument = { 
169
 
        "@variable-string", 
 
168
const struct sieve_argument_def variable_string_argument = {
 
169
        "@variable-string",
170
170
        NULL,
171
 
        arg_variable_string_validate, 
172
 
        NULL, NULL, 
 
171
        arg_variable_string_validate,
 
172
        NULL, NULL,
173
173
        sieve_arg_catenated_string_generate,
174
174
};
175
175
 
176
176
static bool arg_variable_string_validate
177
 
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg, 
 
177
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
178
178
        struct sieve_command *cmd)
179
179
{
180
180
        const struct sieve_extension *this_ext = (*arg)->argument->ext;
186
186
        const char *strval = (const char *) str_data(str);
187
187
        const char *strend = strval + str_len(str);
188
188
        bool result = TRUE;
189
 
        ARRAY_TYPE(sieve_variable_name) substitution;   
 
189
        ARRAY_TYPE(sieve_variable_name) substitution;
190
190
        int nelements = 0;
191
 
        
 
191
 
192
192
        T_BEGIN {
193
193
                /* Initialize substitution structure */
194
 
                t_array_init(&substitution, 2);         
195
 
        
 
194
                t_array_init(&substitution, 2);
 
195
 
196
196
                p = strval;
197
197
                strstart = p;
198
198
                while ( result && p < strend ) {
212
212
                                if ( *p == '{' ) {
213
213
                                        state = ST_VARIABLE;
214
214
                                        p++;
215
 
                                } else 
 
215
                                } else
216
216
                                        state = ST_NONE;
217
217
                                break;
218
218
 
219
 
                        /* Got '${' */ 
 
219
                        /* Got '${' */
220
220
                        case ST_VARIABLE:
221
221
                                nelements = ext_variable_name_parse(&substitution, &p, strend);
222
 
                        
 
222
 
223
223
                                if ( nelements < 0 )
224
224
                                        state = ST_NONE;
225
 
                                else 
 
225
                                else
226
226
                                        state = ST_CLOSE;
227
 
                        
 
227
 
228
228
                                break;
229
229
 
230
230
                        /* Finished parsing name, expecting '}' */
231
231
                        case ST_CLOSE:
232
 
                                if ( *p == '}' ) {                              
 
232
                                if ( *p == '}' ) {
233
233
                                        struct sieve_ast_argument *strarg;
234
 
                                
235
 
                                        /* We now know that the substitution is valid */        
236
 
                                        
 
234
 
 
235
                                        /* We now know that the substitution is valid */
 
236
 
237
237
                                        if ( catstr == NULL ) {
238
238
                                                catstr = sieve_arg_catenated_string_create(*arg);
239
239
                                        }
240
 
                                
241
 
                                        /* Add the substring that is before the substitution to the 
 
240
 
 
241
                                        /* Add the substring that is before the substitution to the
242
242
                                         * variable-string AST.
243
243
                                         *
244
 
                                         * FIXME: For efficiency, if the variable is not found we should 
 
244
                                         * FIXME: For efficiency, if the variable is not found we should
245
245
                                         * coalesce this substring with the one after the substitution.
246
246
                                         */
247
247
                                        if ( substart > strstart ) {
248
248
                                                string_t *newstr = str_new(pool, substart - strstart);
249
 
                                                str_append_n(newstr, strstart, substart - strstart); 
250
 
                                                
 
249
                                                str_append_n(newstr, strstart, substart - strstart);
 
250
 
251
251
                                                strarg = sieve_ast_argument_string_create_raw
252
252
                                                        ((*arg)->ast, newstr, (*arg)->source_line);
253
253
                                                sieve_arg_catenated_string_add_element(catstr, strarg);
254
 
                                        
 
254
 
255
255
                                                /* Give other substitution extensions a chance to do their work */
256
256
                                                if ( !sieve_validator_argument_activate_super
257
257
                                                        (valdtr, cmd, strarg, FALSE) ) {
259
259
                                                        break;
260
260
                                                }
261
261
                                        }
262
 
                                
 
262
 
263
263
                                        /* Find the variable */
264
264
                                        if ( nelements == 1 ) {
265
 
                                                const struct sieve_variable_name *cur_element = 
 
265
                                                const struct sieve_variable_name *cur_element =
266
266
                                                        array_idx(&substitution, 0);
267
 
                                                
 
267
 
268
268
                                                if ( cur_element->num_variable == -1 ) {
269
269
                                                        /* Add variable argument '${identifier}' */
270
270
 
273
273
 
274
274
                                                } else {
275
275
                                                        /* Add match value argument '${000}' */
276
 
                                
 
276
 
277
277
                                                        strarg = ext_variables_match_value_argument_create
278
278
                                                                (this_ext, valdtr, *arg, cur_element->num_variable);
279
279
                                                }
280
280
                                        } else {
281
281
                                                strarg = ext_variables_namespace_argument_create
282
 
                                                        (this_ext, valdtr, *arg, cmd, &substitution);                           
 
282
                                                        (this_ext, valdtr, *arg, cmd, &substitution);
283
283
                                        }
284
284
 
285
285
                                        if ( strarg != NULL ) {
288
288
                                                result = FALSE;
289
289
                                                break;
290
290
                                        }
291
 
                                
 
291
 
292
292
                                        strstart = p + 1;
293
293
                                        substart = strstart;
294
294
 
295
 
                                        p++;    
 
295
                                        p++;
296
296
                                }
297
 
                
298
 
                                /* Finished, reset for the next substitution */ 
 
297
 
 
298
                                /* Finished, reset for the next substitution */
299
299
                                state = ST_NONE;
300
300
                        }
301
301
                }
303
303
 
304
304
        /* Bail out early if substitution is invalid */
305
305
        if ( !result ) return FALSE;
306
 
        
 
306
 
307
307
        /* Check whether any substitutions were found */
308
308
        if ( catstr == NULL ) {
309
309
                /* No substitutions in this string, pass it on to any other substution
311
311
                 */
312
312
                return sieve_validator_argument_activate_super(valdtr, cmd, *arg, TRUE);
313
313
        }
314
 
        
315
 
        /* Add the final substring that comes after the last substitution to the 
 
314
 
 
315
        /* Add the final substring that comes after the last substitution to the
316
316
         * variable-string AST.
317
317
         */
318
318
        if ( strend > strstart ) {
319
319
                struct sieve_ast_argument *strarg;
320
320
                string_t *newstr = str_new(pool, strend - strstart);
321
 
                str_append_n(newstr, strstart, strend - strstart); 
 
321
                str_append_n(newstr, strstart, strend - strstart);
322
322
 
323
323
                strarg = sieve_ast_argument_string_create_raw
324
324
                        ((*arg)->ast, newstr, (*arg)->source_line);
325
325
                sieve_arg_catenated_string_add_element(catstr, strarg);
326
 
                        
327
 
                /* Give other substitution extensions a chance to do their work */      
 
326
 
 
327
                /* Give other substitution extensions a chance to do their work */
328
328
                if ( !sieve_validator_argument_activate_super
329
329
                        (valdtr, cmd, strarg, FALSE) )
330
330
                        return FALSE;
331
 
        }       
332
 
        
 
331
        }
 
332
 
333
333
        return TRUE;
334
334
}
335
335
 
338
338
 */
339
339
 
340
340
static bool _sieve_variable_argument_activate
341
 
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr, 
 
341
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
342
342
        struct sieve_command *cmd, struct sieve_ast_argument *arg, bool assignment)
343
343
{
344
344
        bool result = FALSE;
345
345
        string_t *variable;
346
346
        const char *varstr, *varend;
347
 
        ARRAY_TYPE(sieve_variable_name) vname;  
 
347
        ARRAY_TYPE(sieve_variable_name) vname;
348
348
        int nelements = 0;
349
349
 
350
350
        T_BEGIN {
351
 
                t_array_init(&vname, 2);                        
352
 
        
 
351
                t_array_init(&vname, 2);
 
352
 
353
353
                variable = sieve_ast_argument_str(arg);
354
354
                varstr = str_c(variable);
355
355
                varend = PTR_OFFSET(varstr, str_len(variable));
356
356
                nelements = ext_variable_name_parse(&vname, &varstr, varend);
357
357
 
358
 
                /* Check whether name parsing succeeded */      
 
358
                /* Check whether name parsing succeeded */
359
359
                if ( nelements < 0 || varstr != varend ) {
360
360
                        /* Parse failed */
361
 
                        sieve_argument_validate_error(valdtr, arg, 
 
361
                        sieve_argument_validate_error(valdtr, arg,
362
362
                                "invalid variable name '%s'", str_sanitize(str_c(variable),80));
363
363
                } else if ( nelements == 1 ) {
364
364
                        /* Normal (match) variable */
365
365
 
366
 
                        const struct sieve_variable_name *cur_element = 
 
366
                        const struct sieve_variable_name *cur_element =
367
367
                                array_idx(&vname, 0);
368
368
 
369
369
                        if ( cur_element->num_variable < 0 ) {
372
372
                                        (this_ext, valdtr, arg, str_c(cur_element->identifier));
373
373
 
374
374
                        } else {
375
 
                                /* Match value */       
 
375
                                /* Match value */
376
376
                                result = ext_variables_match_value_argument_activate
377
377
                                        (this_ext, valdtr, arg, cur_element->num_variable, assignment);
378
378
                        }
388
388
}
389
389
 
390
390
bool sieve_variable_argument_activate
391
 
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr, 
392
 
        struct sieve_command *cmd, struct sieve_ast_argument *arg, 
 
391
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
 
392
        struct sieve_command *cmd, struct sieve_ast_argument *arg,
393
393
        bool assignment)
394
394
{
395
395
        if ( sieve_ast_argument_type(arg) == SAAT_STRING ) {
396
396
                /* Single string */
397
397
                return _sieve_variable_argument_activate
398
398
                        (this_ext, valdtr, cmd, arg, assignment);
399
 
                
 
399
 
400
400
        } else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
401
401
                /* String list */
402
402
                struct sieve_ast_argument *stritem;
403
 
                
 
403
 
404
404
                i_assert ( !assignment );
405
 
                
 
405
 
406
406
                stritem = sieve_ast_strlist_first(arg);
407
407
                while ( stritem != NULL ) {
408
408
                        if ( !_sieve_variable_argument_activate
409
409
                                (this_ext, valdtr, cmd, stritem, assignment) )
410
410
                                return FALSE;
411
 
                        
 
411
 
412
412
                        stritem = sieve_ast_strlist_next(stritem);
413
413
                }
414
 
                
 
414
 
415
415
                arg->argument = sieve_argument_create
416
416
                        (arg->ast, &string_list_argument, NULL, 0);
417
 
                
 
417
 
418
418
                return TRUE;
419
 
        } 
420
 
        
 
419
        }
 
420
 
421
421
        return FALSE;
422
422
}
423
423