52 |
static void rc_stmt_destroy(RC_STMT *); |
static void rc_stmt_destroy(RC_STMT *); |
53 |
static void rc_stmt_list_destroy(RC_STMT *); |
static void rc_stmt_list_destroy(RC_STMT *); |
54 |
static void rc_stmt_print(RC_STMT *, int); |
static void rc_stmt_print(RC_STMT *, int); |
55 |
static int reg_option_add(int *, char *); |
static int reg_modifier_add(int *, char *); |
56 |
|
|
57 |
static RC_SECTION *rc_section; |
static RC_SECTION *rc_section; |
58 |
static int debug_level; |
static int debug_level; |
59 |
|
static int error_count; |
60 |
|
static int def_regex_modifier = R_POSIX; |
61 |
%} |
%} |
62 |
|
|
63 |
%union { |
%union { |
72 |
int num; |
int num; |
73 |
struct { |
struct { |
74 |
int part; |
int part; |
75 |
char *key; |
RC_REGEX *key; |
76 |
|
char *string; |
77 |
} msgpart; |
} msgpart; |
78 |
LIST *list; |
LIST *list; |
79 |
}; |
}; |
80 |
|
|
81 |
%token EOL T_BEGIN T_END AND OR |
%token EOL T_BEGIN T_END AND OR |
82 |
%token T_HEADER T_COMMAND IF FI ELSE RULE DONE |
%token IF FI ELSE RULE DONE |
83 |
%token CALL STOP ADD REMOVE MODIFY |
%token CALL STOP ADD REMOVE MODIFY |
84 |
%token <string> IDENT STRING REGEX D_BEGIN |
%token <string> IDENT STRING REGEX D_BEGIN |
85 |
%token <num> T_MSGPART |
%token <num> T_MSGPART |
88 |
%left AND |
%left AND |
89 |
%left NOT |
%left NOT |
90 |
|
|
91 |
%type <string> begin keyword string option key opt_key arg |
%type <string> begin keyword string modifier arg string_key |
92 |
%type <section> section seclist |
%type <section> section seclist |
93 |
%type <stmtlist> stmtlist |
%type <stmtlist> stmtlist |
94 |
%type <stmt> stmt asgn_stmt cond_stmt rule_stmt inst_stmt |
%type <stmt> stmt asgn_stmt cond_stmt rule_stmt inst_stmt modf_stmt |
95 |
%type <num> optlist |
%type <num> modlist opt_modlist |
96 |
%type <node> rule_start cond expr |
%type <node> rule_start cond expr |
97 |
%type <msgpart> msgpart |
%type <msgpart> msgpart s_msgpart r_msgpart key opt_key |
98 |
%type <list> arglist |
%type <list> arglist |
99 |
|
%type <regex> regex |
100 |
|
|
101 |
%% |
%% |
102 |
|
|
171 |
| cond_stmt EOL |
| cond_stmt EOL |
172 |
| rule_stmt EOL |
| rule_stmt EOL |
173 |
| inst_stmt EOL |
| inst_stmt EOL |
174 |
|
| modf_stmt EOL |
175 |
; |
; |
176 |
|
|
177 |
asgn_stmt: keyword meq { verbatim(); } arglist |
asgn_stmt: keyword meq { verbatim(); } arglist |
248 |
| '=' |
| '=' |
249 |
; |
; |
250 |
|
|
251 |
key : '[' string ']' |
key : regex |
252 |
{ |
{ |
253 |
$$ = $2; |
$$.part = HEADER; |
254 |
|
$$.key = $1; |
255 |
|
$$.string = NULL; |
256 |
} |
} |
257 |
|
| '[' string ']' |
258 |
|
{ |
259 |
|
$$.part = HEADER; |
260 |
|
$$.key = NULL; |
261 |
|
$$.string = $2; |
262 |
|
} |
263 |
; |
; |
264 |
|
|
265 |
opt_key : /* empty */ |
opt_key : /* empty */ |
266 |
{ |
{ |
267 |
$$ = NULL; |
$$.string = $$.key = NULL; |
268 |
} |
} |
269 |
| key |
| key |
270 |
; |
; |
271 |
|
|
272 |
msgpart : T_MSGPART opt_key |
msgpart : T_MSGPART opt_key |
273 |
{ |
{ |
274 |
|
$$ = $2; |
275 |
$$.part = $1; |
$$.part = $1; |
|
$$.key = $2; |
|
276 |
} |
} |
277 |
| '[' string ']' |
| key |
278 |
|
; |
279 |
|
|
280 |
|
s_msgpart: msgpart |
281 |
{ |
{ |
282 |
$$.part = HEADER; |
$$ = $1; |
283 |
$$.key = $2; |
if ($$.key) |
284 |
|
yyerror("regexp is not allowed in this context"); |
285 |
|
} |
286 |
|
; |
287 |
|
|
288 |
|
r_msgpart: msgpart |
289 |
|
{ |
290 |
|
$$ = $1; |
291 |
|
if (!$$.key) { |
292 |
|
$$.key = anubis_regex_compile($$.string, 0); |
293 |
|
xfree($$.string); |
294 |
|
} |
295 |
} |
} |
296 |
; |
; |
297 |
|
|
298 |
expr : msgpart optlist meq optlist string |
regex : modlist '[' string ']' |
299 |
|
{ |
300 |
|
$$ = anubis_regex_compile($3, $1); |
301 |
|
free($3); |
302 |
|
} |
303 |
|
; |
304 |
|
|
305 |
|
string_key: /* empty */ |
306 |
|
{ |
307 |
|
$$ = NULL; |
308 |
|
} |
309 |
|
| '[' string ']' |
310 |
|
{ |
311 |
|
$$ = $2; |
312 |
|
} |
313 |
|
; |
314 |
|
|
315 |
|
expr : s_msgpart opt_modlist meq opt_modlist string |
316 |
{ |
{ |
317 |
$$ = rc_node_create(rc_node_expr); |
$$ = rc_node_create(rc_node_expr); |
318 |
$$->v.expr.part = $1.part; |
$$->v.expr.part = $1.part; |
319 |
$$->v.expr.key = $1.key; |
$$->v.expr.key = $1.string; |
320 |
$$->v.expr.re = anubis_regex_compile($5, $4|$2); |
$$->v.expr.re = anubis_regex_compile($5, $4|$2); |
321 |
|
free($5); |
322 |
} |
} |
323 |
; |
; |
324 |
|
|
325 |
optlist : /* empty */ |
modlist : modifier |
|
{ |
|
|
$$ = 0; |
|
|
} |
|
|
| option |
|
326 |
{ |
{ |
327 |
int rc; |
$$ = def_regex_modifier; |
328 |
$$ = 0; |
reg_modifier_add(&$$, $1); |
|
rc = reg_option_add(&$$, $1); |
|
329 |
xfree($1); |
xfree($1); |
|
if (rc) |
|
|
YYERROR; |
|
330 |
} |
} |
331 |
| optlist option |
| modlist modifier |
332 |
{ |
{ |
333 |
int rc = reg_option_add(&$1, $2); |
reg_modifier_add(&$1, $2); |
334 |
xfree($2); |
xfree($2); |
|
if (rc) |
|
|
YYERROR; |
|
335 |
$$ = $1; |
$$ = $1; |
336 |
} |
} |
337 |
; |
; |
338 |
|
|
339 |
option : ':' IDENT |
opt_modlist: /* empty */ |
340 |
|
{ |
341 |
|
$$ = def_regex_modifier; |
342 |
|
} |
343 |
|
| modlist |
344 |
|
; |
345 |
|
|
346 |
|
modifier : ':' IDENT |
347 |
{ |
{ |
348 |
$$ = $2; |
$$ = $2; |
349 |
} |
} |
366 |
} |
} |
367 |
; |
; |
368 |
|
|
369 |
rule_start: RULE optlist string |
rule_start: RULE opt_modlist string |
370 |
{ |
{ |
371 |
$$ = rc_node_create(rc_node_expr); |
$$ = rc_node_create(rc_node_expr); |
372 |
$$->v.expr.part = HEADER; |
$$->v.expr.part = HEADER; |
373 |
$$->v.expr.key = strdup(X_ANUBIS_RULE_HEADER); |
$$->v.expr.key = strdup(X_ANUBIS_RULE_HEADER); |
374 |
$$->v.expr.re = anubis_regex_compile($3, $2); |
$$->v.expr.re = anubis_regex_compile($3, $2); |
375 |
|
free($3); |
376 |
} |
} |
377 |
; |
; |
378 |
|
|
393 |
{ |
{ |
394 |
$$ = rc_stmt_create(rc_stmt_inst); |
$$ = rc_stmt_create(rc_stmt_inst); |
395 |
$$->v.inst.opcode = inst_call; |
$$->v.inst.opcode = inst_call; |
396 |
$$->v.inst.key = $2; |
$$->v.inst.key = NULL; |
397 |
$$->v.inst.part = NIL; |
$$->v.inst.part = NIL; |
398 |
$$->v.inst.key2 = NULL; |
$$->v.inst.key2 = NULL; |
399 |
$$->v.inst.arg = NULL; |
$$->v.inst.arg = $2; |
400 |
} |
} |
401 |
| ADD msgpart string |
| ADD s_msgpart string |
402 |
{ |
{ |
403 |
$$ = rc_stmt_create(rc_stmt_inst); |
$$ = rc_stmt_create(rc_stmt_inst); |
404 |
$$->v.inst.opcode = inst_add; |
$$->v.inst.opcode = inst_add; |
405 |
$$->v.inst.part = $2.part; |
$$->v.inst.part = $2.part; |
406 |
$$->v.inst.key = $2.key; |
$$->v.inst.key = NULL; |
407 |
$$->v.inst.key2 = NULL; |
$$->v.inst.key2 = $2.string; |
408 |
$$->v.inst.arg = $3; |
$$->v.inst.arg = $3; |
409 |
} |
} |
410 |
| REMOVE msgpart |
| REMOVE r_msgpart |
411 |
{ |
{ |
412 |
$$ = rc_stmt_create(rc_stmt_inst); |
$$ = rc_stmt_create(rc_stmt_inst); |
413 |
$$->v.inst.opcode = inst_remove; |
$$->v.inst.opcode = inst_remove; |
414 |
$$->v.inst.part = $2.part; |
$$->v.inst.part = $2.part; |
415 |
$$->v.inst.key = $2.key; |
$$->v.inst.key = $2.key; |
416 |
$$->v.inst.key2 = NULL; |
$$->v.inst.key2 = NULL; |
417 |
$$->v.inst.arg = NULL; |
$$->v.inst.arg = NULL; |
418 |
} |
} |
419 |
| MODIFY msgpart opt_key string |
| MODIFY r_msgpart string_key string |
420 |
{ |
{ |
421 |
$$ = rc_stmt_create(rc_stmt_inst); |
$$ = rc_stmt_create(rc_stmt_inst); |
422 |
$$->v.inst.opcode = inst_modify; |
$$->v.inst.opcode = inst_modify; |
425 |
$$->v.inst.key2 = $3; |
$$->v.inst.key2 = $3; |
426 |
$$->v.inst.arg = $4; |
$$->v.inst.arg = $4; |
427 |
} |
} |
428 |
| MODIFY msgpart key |
| MODIFY r_msgpart string_key |
429 |
{ |
{ |
430 |
$$ = rc_stmt_create(rc_stmt_inst); |
$$ = rc_stmt_create(rc_stmt_inst); |
431 |
$$->v.inst.opcode = inst_modify; |
$$->v.inst.opcode = inst_modify; |
432 |
$$->v.inst.part = $2.part; |
$$->v.inst.part = $2.part; |
433 |
$$->v.inst.key = $2.key; |
$$->v.inst.key = $2.key; |
434 |
|
if ($3 == NULL) { |
435 |
|
yyerror(_("missing replacement value")); |
436 |
|
} |
437 |
$$->v.inst.key2 = $3; |
$$->v.inst.key2 = $3; |
438 |
$$->v.inst.arg = NULL; |
$$->v.inst.arg = NULL; |
439 |
} |
} |
440 |
; |
; |
441 |
|
|
442 |
|
modf_stmt: REGEX modlist |
443 |
|
{ |
444 |
|
def_regex_modifier = $2; |
445 |
|
$$ = NULL; |
446 |
|
} |
447 |
|
; |
448 |
|
|
449 |
%% |
%% |
450 |
|
|
451 |
int |
int |
453 |
{ |
{ |
454 |
anubis_error(SOFT, "%s:%d: %s", |
anubis_error(SOFT, "%s:%d: %s", |
455 |
cfg_file, cfg_line_num, s); |
cfg_file, cfg_line_num, s); |
456 |
|
error_count++; |
457 |
return 0; |
return 0; |
458 |
} |
} |
459 |
|
|
464 |
if (rc_open(name)) |
if (rc_open(name)) |
465 |
return NULL; |
return NULL; |
466 |
rc_section = NULL; |
rc_section = NULL; |
467 |
|
error_count = 0; |
468 |
status = yyparse(); |
status = yyparse(); |
469 |
if (status) { |
if (status || error_count) { |
470 |
rc_section_list_destroy(rc_section); |
rc_section_list_destroy(rc_section); |
471 |
rc_section = NULL; |
rc_section = NULL; |
472 |
} |
} |
680 |
void |
void |
681 |
rc_inst_destroy(RC_INST *inst) |
rc_inst_destroy(RC_INST *inst) |
682 |
{ |
{ |
683 |
free(inst->key); |
anubis_regex_free(inst->key); |
684 |
free(inst->key2); |
free(inst->key2); |
685 |
free(inst->arg); |
free(inst->arg); |
686 |
} |
} |
712 |
break; |
break; |
713 |
|
|
714 |
case inst_call: |
case inst_call: |
715 |
printf(" %s", inst->key); |
printf(" %s", inst->arg); |
716 |
break; |
break; |
717 |
|
|
718 |
|
case inst_add: |
719 |
|
printf(" %s[%s]", part_string(inst->part), inst->key2); |
720 |
|
if (inst->arg) |
721 |
|
printf(" \"%s\"", inst->arg); |
722 |
|
break; |
723 |
|
|
724 |
default: |
default: |
725 |
printf(" %s[%s]", part_string(inst->part), |
printf(" %s[%s]", part_string(inst->part), |
726 |
inst->key ? inst->key : ""); |
inst->key ? anubis_regex_source(inst->key) : ""); |
727 |
if (inst->key2) |
if (inst->key2) |
728 |
printf(" [%s]", inst->key2); |
printf(" [%s]", inst->key2); |
729 |
if (inst->arg) |
if (inst->arg) |
835 |
} |
} |
836 |
|
|
837 |
int |
int |
838 |
reg_option_add(int *flag, char *opt) |
reg_modifier_add(int *flag, char *opt) |
839 |
{ |
{ |
840 |
if (strcasecmp(opt, "basic") == 0) |
/* Regex types */ |
841 |
*flag |= R_BASIC; |
if (strcasecmp(opt, "re") == 0 || strcasecmp(opt, "regex") == 0) |
842 |
else if (strcasecmp(opt, "scase") == 0) |
re_set_type(*flag, re_typeof(def_regex_modifier)); |
843 |
*flag |= R_SCASE; |
else if (strcasecmp(opt, "posix") == 0) |
844 |
|
re_set_type(*flag, R_POSIX); |
845 |
#ifdef HAVE_PCRE |
#ifdef HAVE_PCRE |
846 |
else if (strcasecmp(opt, "perlre") == 0) |
else if (strcasecmp(opt, "perlre") == 0 |
847 |
*flag |= R_PERLRE; |
|| strcasecmp(opt, "perl") == 0) |
848 |
|
re_set_type(*flag, R_PERLRE); |
849 |
#endif |
#endif |
850 |
|
else if (strcasecmp(opt, "ex") == 0 || strcasecmp(opt, "exact") == 0) |
851 |
|
re_set_type(*flag, R_EXACT); |
852 |
|
|
853 |
|
/* Modifiers: */ |
854 |
|
else if (strcasecmp(opt, "basic") == 0) { |
855 |
|
re_set_type(*flag, R_POSIX); |
856 |
|
re_set_flag(*flag, R_BASIC); |
857 |
|
} else if (strcasecmp(opt, "extended") == 0) { |
858 |
|
re_set_type(*flag, R_POSIX); |
859 |
|
re_clear_flag(*flag, R_BASIC); |
860 |
|
} else if (strcasecmp(opt, "scase") == 0) |
861 |
|
re_set_flag(*flag, R_SCASE); |
862 |
|
else if (strcasecmp(opt, "icase") == 0) |
863 |
|
re_clear_flag(*flag, R_SCASE); |
864 |
else { |
else { |
865 |
yyerror(_("Unknown regexp option")); |
yyerror(_("Unknown regexp modifier")); |
866 |
return 1; |
return 1; |
867 |
} |
} |
868 |
return 0; |
return 0; |
869 |
} |
} |
870 |
|
|
871 |
|
|
872 |
/* ******************************* Runtime ********************************* */ |
/* ******************************* Runtime ********************************* */ |
873 |
static struct rc_secdef_child * |
static struct rc_secdef_child * |
874 |
child_copy(struct rc_secdef_child *p) |
child_copy(struct rc_secdef_child *p) |
950 |
break; |
break; |
951 |
|
|
952 |
case inst_call: |
case inst_call: |
953 |
rcfile_call_section(env->method, inst->key, |
rcfile_call_section(env->method, inst->arg, |
954 |
env->data, env->msg); |
env->data, env->msg); |
955 |
break; |
break; |
956 |
|
|
957 |
case inst_add: |
case inst_add: |
958 |
if (inst->part == BODY) |
if (inst->part == BODY) |
959 |
message_add_body(env->msg, inst->key, arg); |
message_add_body(env->msg, inst->key2, arg); |
960 |
else |
else |
961 |
message_add_header(env->msg, inst->key, arg); |
message_add_header(env->msg, inst->key2, arg); |
962 |
break; |
break; |
963 |
|
|
964 |
case inst_modify: |
case inst_modify: |