-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.c
2924 lines (2549 loc) · 83.1 KB
/
eval.c
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
/* $Id: eval.c,v 1.1.1.1 2007/01/11 15:49:52 dhartmei Exp $ */
/*
* Copyright (c) 2004-2006 Daniel Hartmeier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
static __attribute__((unused)) const char rcsid[] = "$Id: eval.c,v 1.1.1.1 2007/01/11 15:49:52 dhartmei Exp $";
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <ctype.h>
#include <syslog.h>
#include "milter-regex.h"
#ifdef GEOIP2
#include <stdio.h>
#endif
#ifndef REG_BASIC
#define REG_BASIC 0
#endif
extern int yyerror(const char *, ...);
static int check_cond(struct context *context, struct cond *, const char *, const char *);
static void push_expr_result(struct context *context, struct expr *, int);
static void push_cond_result(struct context *context, struct cond *, int);
static int build_regex(struct cond_arg *);
#ifdef GEOIP2
static int build_geoip2_path(struct cond_arg *);
#endif
static void free_expr_list(struct expr_list *, struct expr *);
#if 0
static pthread_mutex_t eval_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static struct action default_action;
int
eval_init(int type)
{
memset(&default_action, 0, sizeof(default_action));
default_action.type = type;
return 0;
}
#define eval_mutex_lock()
#if 0
static void
eval_mutex_lock(void)
{
int rv = pthread_mutex_lock(&eval_mutex);
if (rv)
die_with_errno(rv,"pthread_mutex_lock");
}
#endif
#define eval_mutex_unlock()
#if 0
static void
eval_mutex_unlock(void)
{
int rv = pthread_mutex_unlock(&eval_mutex);
if (rv)
die_with_errno(rv,"pthread_mutex_unlock");
}
#endif
static const char *lookup_res_name(int resval) {
switch(resval) {
case VAL_UNDEF: return "UNDEF";
case VAL_TRUE: return "TRUE";
case VAL_FALSE: return "FALSE";
case VAL_INPROGRESS: return "INPROGRESS";
default: return "BAD";
}
}
struct ruleset *
create_ruleset(void)
{
struct ruleset *rs;
rs = calloc(1, sizeof(struct ruleset));
if (rs == NULL) {
yyerror("create_ruleset: calloc: %s", strerror(errno));
return (NULL);
}
return (rs);
}
static void append_expr_to_list(struct expr_list *elc, struct expr_list **list) {
/* use tail insert for exprs too, to keep context.res in order of config file appearance (modulo duplicates). after
* unreverse_ruleset_cond_list(), evaluation will proceed in the
* order designated by the config file.
*
* actions have always been tail-inserted, and that is what drives the actual outcome of evaluation.
*/
elc->next = NULL;
if (*list) {
struct expr_list *elp = *list, *el = elp->next;
while (el) {
elp = el;
el = el->next;
}
elp->next = elc;
} else
*list = elc;
}
static cond_t phase_of_envelope_member(const char *name);
struct expr *
create_cond_4(struct ruleset *rs, cond_t type, const char *a, const char *b, const char *c, const char *d, int lineno, int colno)
{
struct cond *cond = NULL;
struct cond_list *cl = NULL;
struct expr *expr = NULL;
struct expr_list *elc = NULL;
eval_mutex_lock();
expr = calloc(1, sizeof(struct expr));
if (expr == NULL)
goto error;
expr->lineno = lineno;
expr->colno = colno;
elc = calloc(1, sizeof(struct expr_list));
if (elc == NULL)
goto error;
/* if this exact condition was used earlier in the config file, recycle it. */
for (cl = rs->cond[type]; cl != NULL; cl = cl->next) {
if ((cl->cond->args[0].src == NULL) != (a == NULL) ||
(cl->cond->args[1].src == NULL) != (b == NULL) ||
(cl->cond->args[2].src == NULL) != (c == NULL) ||
(cl->cond->args[3].src == NULL) != (d == NULL) ||
(a != NULL && strcmp(a, cl->cond->args[0].src)) ||
(b != NULL && strcmp(b, cl->cond->args[1].src)) ||
(c != NULL && strcmp(c, cl->cond->args[2].src)) ||
(d != NULL && strcmp(d, cl->cond->args[3].src)))
continue;
break;
}
if (cl != NULL)
cond = cl->cond;
else {
cl = calloc(1, sizeof(struct cond_list));
if (cl == NULL)
goto error;
cond = calloc(1, sizeof(struct cond));
if (cond == NULL)
goto error;
cond->type = type;
cond->lineno = lineno;
cond->colno = colno;
if (a != NULL) {
cond->args[0].src = strdup(a);
if (cond->args[0].src == NULL)
goto error;
if (type == COND_COMPARE_CAPTURES) {
/* nothing to prepare. */
} else
#ifdef GEOIP2
if (type == COND_CONNECTGEO) {
if (build_geoip2_path(&cond->args[0]))
goto error;
} else {
#endif
if (((type == COND_CAPTURE_ONCE_BODY) ||
(type == COND_CAPTURE_ALL_BODY)
#ifdef GEOIP2
|| (type == COND_CAPTURE_ONCE_BODY_GEO) ||
(type == COND_CAPTURE_ALL_BODY_GEO)
#endif
) && cond->args[0].not) {
yyerror("create_cond_4: 1st arg (capture arg) must not be negated");
goto error;
}
if (build_regex(&cond->args[0]))
goto error;
#ifdef GEOIP2
}
#endif
}
if (b != NULL) {
cond->args[1].src = strdup(b);
if (cond->args[1].src == NULL)
goto error;
if ((type == COND_CAPTURE_ONCE_BODY) ||
(type == COND_CAPTURE_ALL_BODY)) {
/* nothing to prepare */
}
#ifdef GEOIP2
else if ((type == COND_CAPTURE_ONCE_BODY_GEO) ||
(type == COND_CAPTURE_ALL_BODY_GEO)) {
if (build_geoip2_path(&cond->args[1]))
goto error;
}
#endif
else {
if (build_regex(&cond->args[1]))
goto error;
}
#ifdef GEOIP2
if ((type == COND_HEADERGEO) &&
(cond->args[1].not || cond->args[1].empty)) {
yyerror("create_cond_4: 2nd arg (capture arg) must not be empty or negated");
goto error;
}
#endif
if (((type == COND_CAPTURE_ONCE_HEADER) ||
(type == COND_CAPTURE_ALL_HEADER) ||
(type == COND_CAPTURE_MACRO) ||
(type == COND_COMPARE_HEADER) ||
(type == COND_COMPARE_CAPTURES)
#ifdef GEOIP2
|| (type == COND_CAPTURE_ONCE_HEADER_GEO) ||
(type == COND_CAPTURE_ALL_HEADER_GEO) ||
(type == COND_CAPTURE_MACRO_GEO)
#endif
) &&
cond->args[1].not) {
yyerror("create_cond_4: 2nd arg (capture arg) must not be negated");
goto error;
}
}
if (c != NULL) {
cond->args[2].src = strdup(c);
if (cond->args[2].src == NULL)
goto error;
if ((type == COND_CAPTURE_ONCE_HEADER) ||
(type == COND_CAPTURE_ALL_HEADER) ||
(type == COND_CAPTURE_MACRO)) {
if (phase_of_envelope_member(c) != COND_NONE) {
yyerror("create_cond_4: variable name \"%s\" is reserved", c);
goto error;
}
} else
if ((type == COND_COMPARE_HEADER) ||
(type == COND_COMPARE_CAPTURES)
#ifdef GEOIP2
|| (type == COND_CAPTURE_ONCE_BODY_GEO) ||
(type == COND_CAPTURE_ALL_BODY_GEO)
#endif
) {
/* nothing to prepare. */
} else
#ifdef GEOIP2
if ((type == COND_HEADERGEO) ||
(type == COND_CAPTURE_ONCE_HEADER_GEO) ||
(type == COND_CAPTURE_ALL_HEADER_GEO) ||
(type == COND_CAPTURE_MACRO_GEO)) {
if (build_geoip2_path(&cond->args[2]))
goto error;
} else {
#endif
if (build_regex(&cond->args[2]))
goto error;
#ifdef GEOIP2
}
#endif
}
if (d != NULL) {
cond->args[3].src = strdup(d);
if (cond->args[3].src == NULL)
goto error;
#ifdef GEOIP2
if ((type == COND_CAPTURE_ONCE_HEADER_GEO) ||
(type == COND_CAPTURE_ALL_HEADER_GEO) ||
(type == COND_CAPTURE_MACRO_GEO)) {
if (phase_of_envelope_member(d) != COND_NONE) {
yyerror("create_cond_4: variable name \"%s\" is reserved", d);
goto error;
}
} else
#endif
{
if (build_regex(&cond->args[3]))
goto error;
if (((type == COND_COMPARE_HEADER) ||
(type == COND_COMPARE_CAPTURES)) &&
cond->args[3].not) {
yyerror("create_cond_4: 4th arg (capture arg) must not be negated");
goto error;
}
}
}
cond->idx = rs->maxidx++;
cl->cond = cond;
cl->next = rs->cond[type];
rs->cond[type] = cl;
}
expr->type = EXPR_COND;
expr->cond = cond;
expr->idx = rs->maxidx++;
elc->expr = expr;
append_expr_to_list(elc, &cond->expr);
eval_mutex_unlock();
return (expr);
error:
if (elc != NULL)
free(elc);
if (expr != NULL)
free(expr);
if (cl != NULL)
free(cl);
if (cond != NULL) {
for (int i=0; i<4; ++i) {
if (!cond->args[i].empty)
regfree(&cond->args[i].re);
if (cond->args[i].src != NULL)
free(cond->args[i].src);
}
free(cond);
}
eval_mutex_unlock();
return (NULL);
}
struct expr *
create_cond(struct ruleset *rs, cond_t type, const char *a, const char *b, int lineno, int colno)
{
return create_cond_4(rs, type, a, b, NULL, NULL, lineno, colno);
}
struct expr *
create_capture(struct ruleset *rs, cond_t type, const char *a, const char *b, const char *c, const char *d, int lineno, int colno)
{
struct expr *cap_expr = create_cond_4(rs, type, a, b, c, d, lineno, colno);
if (cap_expr == NULL)
return NULL;
if (cap_expr->cond->expr && cap_expr->cond->expr->next) {
yyerror("yyparse: duplicate capture expression");
return NULL;
}
return cap_expr;
}
struct expr *
create_expr(struct ruleset *rs, int type, struct expr *a, struct expr *b, int lineno, int colno)
{
struct expr *e = NULL;
struct expr_list *ela = NULL, *elb = NULL;
eval_mutex_lock();
e = calloc(1, sizeof(struct expr));
if (e == NULL)
goto error;
e->lineno = lineno;
e->colno = colno;
if (a != NULL) {
ela = calloc(1, sizeof(struct expr_list));
if (ela == NULL)
goto error;
}
if (b != NULL) {
elb = calloc(1, sizeof(struct expr_list));
if (elb == NULL)
goto error;
}
e->type = type;
e->idx = rs->maxidx++;
if (a != NULL) {
e->args[0] = a;
ela->expr = e;
append_expr_to_list(ela, &a->expr);
}
if (b != NULL) {
e->args[1] = b;
elb->expr = e;
append_expr_to_list(elb, &b->expr);
}
eval_mutex_unlock();
return (e);
error:
yyerror("create_expr: calloc: %s", strerror(errno));
if (elb != NULL)
free(elb);
if (ela != NULL)
free(ela);
if (e != NULL)
free(e);
eval_mutex_unlock();
return (NULL);
}
struct action *
create_action(struct ruleset *rs, int type, const char *msgtxt, int lineno, int colno)
{
struct action *a = NULL;
struct action_list *al = NULL;
eval_mutex_lock();
a = calloc(1, sizeof(struct action));
if (a == NULL)
goto error;
al = calloc(1, sizeof(struct action_list));
if (al == NULL)
goto error;
a->type = type;
a->msg = msgtxt == NULL ? NULL : strdup(msgtxt);
a->idx = rs->maxidx++;
a->lineno = lineno;
a->colno = colno;
al->action = a;
/* tail insert, so actions have same order as file */
if (rs->action == NULL)
rs->action = al;
else {
struct action_list *t = rs->action;
while (t->next != NULL)
t = t->next;
t->next = al;
}
eval_mutex_unlock();
return (a);
error:
yyerror("create_action: calloc: %s", strerror(errno));
if (al != NULL)
free(al);
if (a != NULL)
free(a);
eval_mutex_unlock();
return (NULL);
}
static struct action *
eval_cond_1(struct context *context, cond_t type,
const char *a, const char *b)
{
struct ruleset *rs = context->rs;
int *res = context->res;
struct cond_list *cl;
int initial_captures_change_count = context->captures_change_count;
again:
for (cl = rs->cond[type]; cl != NULL; cl = cl->next) {
int r;
if ((res[cl->cond->idx] != VAL_UNDEF) &&
(res[cl->cond->idx] != VAL_INPROGRESS))
continue;
r = check_cond(context, cl->cond, a, b);
if (r < 0)
return (NULL);
else if (!r) {
switch(cl->cond->type) {
case COND_CAPTURE_ALL_HEADER:
case COND_CAPTURE_ALL_BODY:
#ifdef GEOIP2
case COND_CAPTURE_ALL_HEADER_GEO:
case COND_CAPTURE_ALL_BODY_GEO:
#endif
if (res[cl->cond->idx] != VAL_INPROGRESS)
push_cond_result(context, cl->cond, VAL_INPROGRESS);
break;
default:
push_cond_result(context, cl->cond, VAL_TRUE);
break;
}
}
}
if (context->current_winning_action)
return context->current_winning_action;
if ((type != COND_COMPARE_CAPTURES) &&
(context->captures_change_count != initial_captures_change_count)) {
type = COND_COMPARE_CAPTURES;
goto again;
}
return (NULL);
}
static long long int now_usecs(void) {
if (! debug)
return 0;
struct timeval now;
(void)gettimeofday(&now,0);
return ((long long int)now.tv_sec * 1000000LL) + (long long int)now.tv_usec;
}
struct action *
eval_cond(struct context *context, cond_t type,
const char *a, const char *b)
{
long long int start_at = now_usecs();
eval_mutex_lock();
struct action *ret = eval_cond_1(context,type,a,b);
eval_mutex_unlock();
if (debug)
context->eval_time_cum += now_usecs() - start_at;
return ret;
}
struct action *
eval_end(struct context *context, cond_t type)
{
long long int start_at = now_usecs();
struct ruleset *rs = context->rs;
int *res = context->res;
struct action *ret = &default_action;
struct cond_list *cl;
eval_mutex_lock();
context->last_phase_done = type;
for (cl = rs->cond[type]; cl != NULL; cl = cl->next) {
if (res[cl->cond->idx] == VAL_UNDEF) {
push_cond_result(context, cl->cond, VAL_FALSE);
} else if (res[cl->cond->idx] == VAL_INPROGRESS) {
/* this won't actually do anything, because captures
* aren't real conds, but it will force it to VAL_TRUE,
* which usefully records that the capture was closed
* out.
*/
push_cond_result(context, cl->cond, VAL_TRUE);
}
}
if (type < COND_COMPARE_CAPTURES) {
for (cl = rs->cond[COND_COMPARE_CAPTURES]; cl != NULL; cl = cl->next) {
if ((cl->cond->end_phase == COND_NONE) || (cl->cond->end_phase > type))
continue;
if (res[cl->cond->idx] == VAL_UNDEF) {
push_cond_result(context, cl->cond, VAL_FALSE);
} else if (res[cl->cond->idx] == VAL_INPROGRESS) {
/* same as above -- this won't actually do
* anything.
*/
push_cond_result(context, cl->cond, VAL_TRUE);
}
}
}
if (context->current_winning_action) {
eval_mutex_unlock();
if (debug)
context->eval_time_cum += now_usecs() - start_at;
return context->current_winning_action;
}
ret = eval_cond_1(context, COND_PHASEDONE, 0, 0);
eval_mutex_unlock();
if (debug)
context->eval_time_cum += now_usecs() - start_at;
return ret;
}
void
eval_clear(struct context *context, cond_t type)
{
long long int start_at = now_usecs();
struct ruleset *rs = context->rs;
struct cond_list *cl;
eval_mutex_lock();
#if 0
for (; type < COND_MAX; ++type) {
for (cl = rs->cond[type]; cl != NULL; cl = cl->next)
push_cond_result(context, cl->cond, VAL_UNDEF);
#endif
for (int type_i = COND_NONE + 1; type_i < COND_MAX; ++type_i) {
for (cl = rs->cond[type]; cl != NULL; cl = cl->next) {
if ((context->res_phase[cl->cond->idx] != COND_NONE) &&
(context->res_phase[cl->cond->idx] >= type))
push_cond_result(context, cl->cond, VAL_UNDEF);
}
}
context->current_winning_action = NULL;
for (struct action_list *al = rs->action; al != NULL; al = al->next) {
if (context->res[al->action->idx] != VAL_UNDEF) {
msg(LOG_DEBUG, context,
"cleared action %s @L%d@%d %s@%s -> %s@%s",
lookup_action_name(al->action->type),
al->action->lineno,
al->action->colno,
lookup_res_name(context->res[al->action->idx]),
lookup_cond_name(context->res_phase[al->action->idx]),
lookup_res_name(VAL_UNDEF),
lookup_cond_name(context->current_phase));
context->res[al->action->idx] = VAL_UNDEF;
}
}
free_kv_bindings(context, &context->captures, type);
eval_mutex_unlock();
if (debug)
context->eval_time_cum += now_usecs() - start_at;
}
int insert_kv_binding(struct context *context, const char *key, const char *val, size_t val_len, struct kv_binding **point) {
struct kv_binding *new_kv = malloc(sizeof *new_kv + val_len + 1);
struct kv_binding *local_point = 0;
if (! new_kv)
return -1;
if (! point)
point = &local_point;
new_kv->key = key;
new_kv->val_len = val_len;
new_kv->capture_phase = context->current_phase;
memcpy(new_kv->val, val, val_len);
new_kv->val[val_len] = 0;
if (! *point) {
for (struct kv_binding *i = context->captures; i; i = i->next) {
if (! strcmp(i->key,key))
*point = i;
else if (*point)
break;
}
}
if (*point) {
new_kv->prev = *point;
new_kv->next = (*point)->next;
(*point)->next = new_kv;
} else {
new_kv->prev = 0;
new_kv->next = context->captures;
context->captures = new_kv;
}
if (new_kv->next)
new_kv->next->prev = new_kv;
*point = new_kv;
msg(LOG_DEBUG, context, "inserted KV \"%s\" = \"%.*s\"", key, (int)val_len, val);
++context->captures_change_count;
return 0;
}
const char *get_kv_binding_next(const struct kv_binding **next) {
if (! *next)
return 0;
const char *ret = (*next)->val;
if ((*next)->next && (! strcmp((*next)->key, (*next)->next->key)))
*next = (*next)->next;
else
*next = 0;
return ret;
}
const char *get_kv_binding_first(struct context *context, const char *key, const struct kv_binding **next) {
for (struct kv_binding *i = context->captures; i; i = i->next) {
if (! strcmp(i->key,key)) {
*next = i;
return get_kv_binding_next(next);
}
}
return 0;
}
void free_kv_bindings(struct context *context, struct kv_binding **list_pp, cond_t keep_if_before) {
struct kv_binding *list = *list_pp;
while (list) {
if (list->capture_phase < keep_if_before)
list = list->next;
else {
struct kv_binding *prev = list->prev, *next = list->next;
msg(LOG_DEBUG, context, "deleting KV \"%s\" = \"%.*s\"", list->key, (int)list->val_len, list->val);
free(list);
if (prev)
prev->next = next;
else
*list_pp = next;
if (next)
next->prev = prev;
list = next;
}
}
}
static int compare_values(const char *first, size_t first_len, struct cond_arg *first_cond,
const char *second, size_t second_len, struct cond_arg *second_cond) {
int (*cmp_fn)(const char *s1, const char *s2, size_t n);
if (first_cond->compare_case_insensitively || second_cond->compare_case_insensitively)
cmp_fn = strncasecmp;
else
cmp_fn = (int (*)(const char *, const char *, size_t))memcmp;
if (first_len == second_len)
return cmp_fn(first, second, first_len);
else if (second_len > first_len) {
if (first_cond->compare_as_suffix) {
if (! cmp_fn(first, second + (second_len - first_len), first_len))
return 0;
}
if (first_cond->compare_as_dname_suffix) {
if ((*(second + (second_len - first_len) - 1) == '.') &&
(! cmp_fn(first, second + (second_len - first_len), first_len)))
return 0;
}
if (first_cond->compare_as_prefix) {
if (! cmp_fn(first, second, first_len))
return 0;
}
if (first_cond->compare_as_dname_prefix) {
if ((second[first_len] == '.') &&
(! cmp_fn(first, second, first_len)))
return 0;
}
} else {
if (second_cond->compare_as_suffix) {
if (! cmp_fn(second, first + (first_len - second_len), second_len))
return 0;
}
if (second_cond->compare_as_dname_suffix) {
if ((*(first + (first_len - second_len) - 1) == '.') &&
(! cmp_fn(second, first + (first_len - second_len), second_len)))
return 0;
}
if (second_cond->compare_as_prefix) {
if (! cmp_fn(second, first, second_len))
return 0;
}
if (second_cond->compare_as_dname_prefix) {
if ((first[second_len] == '.') &&
(! cmp_fn(second, first, second_len)))
return 0;
}
}
return 1;
}
static int get_envelope_member(struct context *context, const char *name, const char **value, size_t *value_len) {
if (! name)
return -EINVAL;
#define CHECK_ENVELOPE(checkname, membername) if (! strcmp(name,checkname)) { if (! *context->membername) { errno = EAGAIN; return -1; } *value = context->membername; *value_len = strnlen(context->membername, sizeof context->membername); return 0; }
switch(name[0]) {
case 'c':
CHECK_ENVELOPE("cc", hdr_cc);
CHECK_ENVELOPE("client_resolve", client_resolve);
CHECK_ENVELOPE("connect:hostname", host_name);
CHECK_ENVELOPE("connect:address", host_addr);
#ifdef GEOIP2
if (! strcmp(name,"connect:geoip")) {
if (! context->geoip2_result_summary) {
if (prime_geoip2(context) == 0)
(void)geoip2_refresh_summary(context);
}
if (context->geoip2_result_summary) {
*value = context->geoip2_result_summary;
*value_len = strlen(*value);
return 0;
} else
return -ENOENT;
}
#endif
return -ENOENT;
case 'e':
CHECK_ENVELOPE("envfrom", env_from);
CHECK_ENVELOPE("envrcpt", env_rcpt);
return -ENOENT;
case 'f':
CHECK_ENVELOPE("from", hdr_from);
return -ENOENT;
case 'h':
CHECK_ENVELOPE("helo", helo);
return -ENOENT;
case 'l':
if (! strncmp(name,"literal:",strlen("literal:"))) {
*value = name + strlen("literal:");
*value_len = strlen(*value);
return 0;
}
return -ENOENT;
case 'm':
CHECK_ENVELOPE("message_id", message_id);
CHECK_ENVELOPE("my_name", my_name);
return -ENOENT;
case 's':
CHECK_ENVELOPE("subject", hdr_subject);
return -ENOENT;
case 't':
CHECK_ENVELOPE("tls_status", tls_status);
CHECK_ENVELOPE("to", hdr_to);
return -ENOENT;
default:
return -ENOENT;
}
#undef CHECK_ENVELOPE
}
static cond_t phase_of_envelope_member(const char *name) {
if (! name)
return COND_NONE;
#define CHECK_ENVELOPE(checkname, phase) if (! strcmp(name,checkname)) return (phase)
CHECK_ENVELOPE("cc", COND_HEADER);
CHECK_ENVELOPE("client_resolve", COND_CONNECT);
CHECK_ENVELOPE("connect:hostname", COND_CONNECT);
CHECK_ENVELOPE("connect:address", COND_CONNECT);
#ifdef GEOIP2
if (! strcmp(name,"connect:geoip"))
return COND_CONNECTGEO;
#endif
CHECK_ENVELOPE("envfrom", COND_ENVFROM);
CHECK_ENVELOPE("envrcpt", COND_ENVRCPT);
CHECK_ENVELOPE("from", COND_HEADER);
CHECK_ENVELOPE("helo", COND_HELO);
if (! strncmp(name,"literal:",strlen("literal:")))
return COND_STATIC;
CHECK_ENVELOPE("message_id", COND_ENVFROM);
CHECK_ENVELOPE("my_name", COND_CONNECT);
CHECK_ENVELOPE("subject", COND_HEADER);
CHECK_ENVELOPE("tls_status", COND_HELO);
CHECK_ENVELOPE("to", COND_HEADER);
return COND_NONE;
#undef CHECK_ENVELOPE
}
#ifdef USE_PCRE2
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#ifndef COMPILE_ERROR_BASE
#define COMPILE_ERROR_BASE 100
#endif
/* cribbed from pcre2-10.42/src/pcre2posix.c.
*
* only changes are passing in a pre-initialized PCRE2 options register, adding
* support for compile_extra_options, and fixing md to be a threadsafe
* allocation in my_regexec().
*/
static int PCRE2_CALL_CONVENTION
my_regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags, int options)
{
int rc, so, eo;
pcre2_match_data *md;
if (string == NULL) return REG_INVARG;
if ((eflags & REG_NOTBOL) != 0) options |= PCRE2_NOTBOL;
if ((eflags & REG_NOTEOL) != 0) options |= PCRE2_NOTEOL;
if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE2_NOTEMPTY;
/* When REG_NOSUB was specified, or if no vector has been passed in which to
put captured strings, ensure that nmatch is zero. This will stop any attempt to
write to pmatch. */
if ((preg->re_cflags & REG_NOSUB) != 0 || pmatch == NULL) nmatch = 0;
/* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings.
The man page from OS X says "REG_STARTEND affects only the location of the
string, not how it is matched". That is why the "so" value is used to bump the
start location rather than being passed as a PCRE2 "starting offset". */
if ((eflags & REG_STARTEND) != 0)
{
if (pmatch == NULL) return REG_INVARG;
so = pmatch[0].rm_so;
eo = pmatch[0].rm_eo;
}
else
{
so = 0;
eo = (int)strlen(string);
}
md = pcre2_match_data_create((uint32_t)nmatch, NULL /* pcre2_general_context *gcontext */);
if (md == NULL) return REG_ESPACE;
rc = pcre2_match((const pcre2_code *)preg->re_pcre2_code,
(PCRE2_SPTR)string + so, (eo - so), 0, options, md, NULL);
/* Successful match */
if (rc >= 0)
{
size_t i;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md);
if ((size_t)rc > nmatch) rc = (int)nmatch;
for (i = 0; i < (size_t)rc; i++)
{
pmatch[i].rm_so = (ovector[i*2] == PCRE2_UNSET)? -1 :
(int)(ovector[i*2] + so);
pmatch[i].rm_eo = (ovector[i*2+1] == PCRE2_UNSET)? -1 :
(int)(ovector[i*2+1] + so);
}
pcre2_match_data_free(md);
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
return 0;
}
/* Unsuccessful match */
pcre2_match_data_free(md);
if (rc <= PCRE2_ERROR_UTF8_ERR1 && rc >= PCRE2_ERROR_UTF8_ERR21)
return REG_INVARG;
/* Most of these are events that won't occur during testing, so exclude them
from coverage. */
switch(rc)
{
case PCRE2_ERROR_HEAPLIMIT: return REG_ESPACE;
case PCRE2_ERROR_NOMATCH: return REG_NOMATCH;
/* LCOV_EXCL_START */
case PCRE2_ERROR_BADMODE: return REG_INVARG;
case PCRE2_ERROR_BADMAGIC: return REG_INVARG;
case PCRE2_ERROR_BADOPTION: return REG_INVARG;
case PCRE2_ERROR_BADUTFOFFSET: return REG_INVARG;
case PCRE2_ERROR_MATCHLIMIT: return REG_ESPACE;
case PCRE2_ERROR_NOMEMORY: return REG_ESPACE;
case PCRE2_ERROR_NULL: return REG_INVARG;
default: return REG_ASSERT;
/* LCOV_EXCL_STOP */
}
}
#define my_regexec(preg, string, nmatch, pmatch, eflags, pcre2_options) my_regexec(preg, string, nmatch, pmatch, eflags, pcre2_options)
#else /* !USE_PCRE2 */
#define my_regexec(preg, string, nmatch, pmatch, eflags, pcre2_options) regexec(preg, string, nmatch, pmatch, eflags)
#endif /* USE_PCRE2 */
static int
check_cond(struct context *context, struct cond *c, const char *a, const char *b)
{
++context->check_cond_count;
switch (c->type) {
case COND_PHASEDONE: {
if ((! c->args[0].src) || c->args[0].empty) /* nonsense existence check */
return c->args[0].not;
if (context->last_phase_done == COND_NONE)