/
cond_tokenize.c
1657 lines (1416 loc) · 36.9 KB
/
cond_tokenize.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @file src/lib/server/cond_eval.c
* @brief Parse complex conditions
*
* @copyright 2013 Alan DeKok (aland@freeradius.org)
*/
RCSID("$Id$")
#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/cond_eval.h>
#include <freeradius-devel/server/cond.h>
#include <freeradius-devel/util/debug.h>
#include <ctype.h>
static fr_table_num_sorted_t const allowed_return_codes[] = {
{ "fail", 1 },
{ "handled", 1 },
{ "invalid", 1 },
{ "noop", 1 },
{ "notfound", 1 },
{ "ok", 1 },
{ "reject", 1 },
{ "updated", 1 },
{ "disallow", 1 }
};
static size_t allowed_return_codes_len = NUM_ELEMENTS(allowed_return_codes);
/*
* This file shouldn't use any functions from the server core.
*/
size_t cond_snprint(size_t *need, char *out, size_t outlen, fr_cond_t const *in)
{
size_t len;
char *p = out;
char *end = out + outlen - 1;
fr_cond_t const *c = in;
size_t our_need;
if (!need) need = &our_need;
RETURN_IF_NO_SPACE_INIT(need, 1, p, out, end);
next:
if (!c) {
p[0] = '\0';
return 0;
}
if (c->negate) {
*(p++) = '!'; /* FIXME: only allow for child? */
}
switch (c->type) {
case COND_TYPE_EXISTS:
fr_assert(c->data.vpt != NULL);
if (c->cast) {
len = snprintf(p, end - p, "<%s>", fr_table_str_by_value(fr_value_box_type_table,
c->cast->type, "??"));
RETURN_IF_TRUNCATED(need, len, p, out, end);
}
len = tmpl_snprint(need, p, end - p, c->data.vpt);
if (*need) return len;
p += len;
break;
case COND_TYPE_MAP:
fr_assert(c->data.map != NULL);
#if 0
*(p++) = '['; /* for extra-clear debugging */
#endif
if (c->cast) {
len = snprintf(p, end - p, "<%s>", fr_table_str_by_value(fr_value_box_type_table, c->cast->type, "??"));
RETURN_IF_TRUNCATED(need, len, p, out, end);
}
len = map_snprint(need, p, end - p, c->data.map);
if (*need) return len;
p += len;
#if 0
*(p++) = ']';
#endif
break;
case COND_TYPE_CHILD:
fr_assert(c->data.child != NULL);
*(p++) = '(';
len = cond_snprint(need, p, (end - p) - 1, c->data.child); /* -1 for proceeding ')' */
if (*need) return len;
if (len >= (outlen - 1)) return len;
p += len;
*(p++) = ')';
break;
case COND_TYPE_TRUE:
len = strlcpy(out, "true", outlen);
RETURN_IF_TRUNCATED(need, len, p, out, end);
return p - out;
case COND_TYPE_FALSE:
len = strlcpy(out, "false", outlen);
RETURN_IF_TRUNCATED(need, len, p, out, end);
return p - out;
default:
*out = '\0';
return 0;
}
if (c->next_op == COND_NONE) {
fr_assert(c->next == NULL);
*p = '\0';
return p - out;
}
if (c->next_op == COND_AND) {
len = strlcpy(p, " && ", end - p);
RETURN_IF_TRUNCATED(need, len, p, out, end);
} else if (c->next_op == COND_OR) {
len = strlcpy(p, " || ", end - p);
RETURN_IF_TRUNCATED(need, len, p, out, end);
} else {
fr_assert(0 == 1);
}
c = c->next;
goto next;
}
static bool cond_type_check(fr_cond_t *c, fr_type_t lhs_type)
{
/*
* SOME integer mismatch is OK. If the LHS has a large type,
* and the RHS has a small type, it's OK.
*
* If the LHS has a small type, and the RHS has a large type,
* then add a cast to the LHS.
*/
if (lhs_type == FR_TYPE_UINT64) {
if ((tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT32) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT16) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT8)) {
c->cast = NULL;
return true;
}
}
if (lhs_type == FR_TYPE_UINT32) {
if ((tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT16) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT8)) {
c->cast = NULL;
return true;
}
if (tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT64) {
c->cast = tmpl_da(c->data.map->rhs);
return true;
}
}
if (lhs_type == FR_TYPE_UINT16) {
if (tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT8) {
c->cast = NULL;
return true;
}
if ((tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT64) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT32)) {
c->cast = tmpl_da(c->data.map->rhs);
return true;
}
}
if (lhs_type == FR_TYPE_UINT8) {
if ((tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT64) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT32) ||
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_UINT16)) {
c->cast = tmpl_da(c->data.map->rhs);
return true;
}
}
if ((lhs_type == FR_TYPE_IPV4_PREFIX) &&
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_IPV4_ADDR)) {
return true;
}
if ((lhs_type == FR_TYPE_IPV6_PREFIX) &&
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_IPV6_ADDR)) {
return true;
}
/*
* Same checks as above, but with the types swapped, and
* with explicit cast for the interpretor.
*/
if ((lhs_type == FR_TYPE_IPV4_ADDR) &&
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_IPV4_PREFIX)) {
c->cast = tmpl_da(c->data.map->rhs);
return true;
}
if ((lhs_type == FR_TYPE_IPV6_ADDR) &&
(tmpl_da(c->data.map->rhs)->type == FR_TYPE_IPV6_PREFIX)) {
c->cast = tmpl_da(c->data.map->rhs);
return true;
}
return false;
}
/*
* Less code means less bugs
*/
#define return_P(_x) *error = _x;goto return_p
#define return_0(_x) *error = _x;goto return_0
#define return_lhs(_x) *error = _x;goto return_lhs
#define return_rhs(_x) *error = _x;goto return_rhs
#define return_SLEN goto return_slen
static ssize_t cond_check_cast(fr_cond_t *c, char const *start,
char const *lhs, char const *rhs, char const **error)
{
if (tmpl_is_attr(c->data.map->rhs) &&
(c->cast->type != tmpl_da(c->data.map->rhs)->type)) {
if (cond_type_check(c, c->cast->type)) {
return 1;
}
*error = "Attribute comparisons must be of the same data type";
return 0;
}
#ifdef HAVE_REGEX
if (tmpl_is_regex_unparsed(c->data.map->rhs)) {
*error = "Cannot use cast with regex comparison";
return -(rhs - start);
}
#endif
/*
* The LHS is a literal which has been cast to a data type.
* Cast it to the appropriate data type.
*/
if (tmpl_is_unparsed(c->data.map->lhs) &&
(tmpl_cast_in_place(c->data.map->lhs, c->cast->type, c->cast) < 0)) {
*error = "Failed to parse field";
return -(lhs - start);
}
/*
* The RHS is a literal, and the LHS has been cast to a data
* type.
*/
if ((tmpl_is_data(c->data.map->lhs)) &&
(tmpl_is_unparsed(c->data.map->rhs)) &&
(tmpl_cast_in_place(c->data.map->rhs, c->cast->type, c->cast) < 0)) {
*error = "Failed to parse field";
return -(rhs - start);
}
/*
* We may be casting incompatible
* types. We check this based on
* their size.
*/
if (tmpl_is_attr(c->data.map->lhs)) {
/*
* dst.min == src.min
* dst.max == src.max
*/
if ((dict_attr_sizes[c->cast->type][0] == dict_attr_sizes[tmpl_da(c->data.map->lhs)->type][0]) &&
(dict_attr_sizes[c->cast->type][1] == dict_attr_sizes[tmpl_da(c->data.map->lhs)->type][1])) {
goto cast_ok;
}
/*
* Run-time parsing of strings.
* Run-time copying of octets.
*/
if ((tmpl_da(c->data.map->lhs)->type == FR_TYPE_STRING) ||
(tmpl_da(c->data.map->lhs)->type == FR_TYPE_OCTETS)) {
goto cast_ok;
}
/*
* ifid to uint64 is OK
*/
if ((tmpl_da(c->data.map->lhs)->type == FR_TYPE_IFID) &&
(c->cast->type == FR_TYPE_UINT64)) {
goto cast_ok;
}
/*
* ipaddr to ipv4prefix is OK
*/
if ((tmpl_da(c->data.map->lhs)->type == FR_TYPE_IPV4_ADDR) &&
(c->cast->type == FR_TYPE_IPV4_PREFIX)) {
goto cast_ok;
}
/*
* ipv6addr to ipv6prefix is OK
*/
if ((tmpl_da(c->data.map->lhs)->type == FR_TYPE_IPV6_ADDR) &&
(c->cast->type == FR_TYPE_IPV6_PREFIX)) {
goto cast_ok;
}
/*
* uint64 to ethernet is OK.
*/
if ((tmpl_da(c->data.map->lhs)->type == FR_TYPE_UINT64) &&
(c->cast->type == FR_TYPE_ETHERNET)) {
goto cast_ok;
}
/*
* dst.max < src.min
* dst.min > src.max
*/
if ((dict_attr_sizes[c->cast->type][1] < dict_attr_sizes[tmpl_da(c->data.map->lhs)->type][0]) ||
(dict_attr_sizes[c->cast->type][0] > dict_attr_sizes[tmpl_da(c->data.map->lhs)->type][1])) {
*error = "Cannot cast to attribute of incompatible size";
return 0;
}
}
cast_ok:
/*
* Casting to a redundant type means we don't need the cast.
*
* Do this LAST, as the rest of the code above assumes c->cast
* is not NULL.
*/
if (tmpl_is_attr(c->data.map->lhs) &&
(c->cast->type == tmpl_da(c->data.map->lhs)->type)) {
c->cast = NULL;
}
return 1;
}
/*
* See if two attribute comparisons are OK.
*/
static ssize_t cond_check_attrs(fr_cond_t *c, char const *start,
char const *lhs, fr_token_t lhs_type,
char const *rhs, fr_token_t rhs_type,
char const **error)
{
vp_tmpl_t *vpt;
/*
* Two attributes? They must be of the same type
*/
if (tmpl_is_attr(c->data.map->rhs) &&
tmpl_is_attr(c->data.map->lhs) &&
(tmpl_da(c->data.map->lhs)->type != tmpl_da(c->data.map->rhs)->type)) {
if (cond_type_check(c, tmpl_da(c->data.map->lhs)->type)) {
return 1;
}
*error = "Attribute comparisons must be of the same data type";
return 0;
}
/*
* Invalid: User-Name == bob
* Valid: User-Name == "bob"
*
* There's no real reason for
* this, other than consistency.
*/
if (tmpl_is_attr(c->data.map->lhs) &&
!tmpl_is_attr(c->data.map->rhs) &&
(tmpl_da(c->data.map->lhs)->type == FR_TYPE_STRING) &&
(c->data.map->op != T_OP_CMP_TRUE) &&
(c->data.map->op != T_OP_CMP_FALSE) &&
(c->data.map->rhs->quote == T_BARE_WORD)) {
return_rhs("Comparison value must be a quoted string");
}
/*
* Quotes around non-string
* attributes mean that it's
* either xlat, or an exec.
*/
if (tmpl_is_attr(c->data.map->lhs) &&
!tmpl_is_attr(c->data.map->rhs) &&
(tmpl_da(c->data.map->lhs)->type != FR_TYPE_STRING) &&
(tmpl_da(c->data.map->lhs)->type != FR_TYPE_OCTETS) &&
(tmpl_da(c->data.map->lhs)->type != FR_TYPE_DATE) &&
(rhs_type == T_SINGLE_QUOTED_STRING)) {
*error = "Comparison value must be an unquoted string";
return_rhs:
return -(rhs - start);
}
/*
* The LHS has been cast to a data type, and the RHS is a
* literal. Cast the RHS to the type of the cast.
*/
if (c->cast && tmpl_is_unparsed(c->data.map->rhs) &&
(tmpl_cast_in_place(c->data.map->rhs, c->cast->type, c->cast) < 0)) {
return_rhs("Failed to parse field");
}
/*
* The LHS is an attribute, and the RHS is a literal. Cast the
* RHS to the data type of the LHS.
*
* Note: There's a hack in here to always parse RHS as the
* equivalent prefix type if the LHS is an IP address.
*
* This allows Framed-IP-Address < 192.168.0.0./24
*/
if (tmpl_is_attr(c->data.map->lhs) &&
(tmpl_is_unparsed(c->data.map->rhs) ||
tmpl_is_data(c->data.map->rhs))) {
fr_type_t type = tmpl_da(c->data.map->lhs)->type;
switch (tmpl_da(c->data.map->lhs)->type) {
case FR_TYPE_IPV4_ADDR:
if (strchr(c->data.map->rhs->name, '/') != NULL) {
type = FR_TYPE_IPV4_PREFIX;
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()), FR_CAST_BASE + type);
}
break;
case FR_TYPE_IPV6_ADDR:
if (strchr(c->data.map->rhs->name, '/') != NULL) {
type = FR_TYPE_IPV6_PREFIX;
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()), FR_CAST_BASE + type);
}
break;
default:
break;
}
/*
* Do not pass LHS as enumv if we're casting
* as that means there's now a type mismatch between
* lhs and rhs, which means the enumerations
* can never match.
*/
if (tmpl_cast_in_place(c->data.map->rhs, type,
c->cast ? NULL : tmpl_da(c->data.map->lhs)) < 0) {
fr_dict_attr_t const *da = tmpl_da(c->data.map->lhs);
if (!fr_dict_attr_is_top_level(da)) goto bad_type;
switch (da->attr) {
case FR_AUTH_TYPE:
/*
* The types for these attributes are dynamically allocated
* by module.c, so we can't enforce strictness here.
*/
c->pass2_fixup = PASS2_FIXUP_TYPE;
break;
default:
bad_type:
return_rhs("Failed to parse value for attribute");
}
}
/*
* Stupid WiMAX shit.
* Cast the LHS to the
* type of the RHS.
*/
if (tmpl_da(c->data.map->lhs)->type == FR_TYPE_COMBO_IP_ADDR) {
fr_dict_attr_t const *da;
da = fr_dict_attr_by_type(tmpl_da(c->data.map->lhs),
tmpl_value_type(c->data.map->rhs));
if (!da) {
return_rhs("Cannot find type for attribute");
}
tmpl_da(c->data.map->lhs) = da;
}
} /* attr to literal comparison */
/*
* The RHS will turn into... something. Allow for prefixes
* there, too.
*/
if (tmpl_is_attr(c->data.map->lhs) &&
(tmpl_is_xlat_unparsed(c->data.map->rhs) ||
tmpl_is_xlat(c->data.map->rhs) ||
tmpl_is_exec(c->data.map->rhs))) {
if (tmpl_da(c->data.map->lhs)->type == FR_TYPE_IPV4_ADDR) {
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()),
FR_CAST_BASE + FR_TYPE_IPV4_PREFIX);
}
if (tmpl_da(c->data.map->lhs)->type == FR_TYPE_IPV6_ADDR) {
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()),
FR_CAST_BASE + FR_TYPE_IPV6_PREFIX);
}
}
/*
* If the LHS is a bare word, AND it looks like
* an attribute, try to parse it as such.
*
* This allows LDAP-Group and SQL-Group to work.
*
* The real fix is to just read the config files,
* and do no parsing until after all of the modules
* are loaded. But that has issues, too.
*/
if (tmpl_is_unparsed(c->data.map->lhs) && (lhs_type == T_BARE_WORD)) {
int hyphens = 0;
bool may_be_attr = true;
size_t i;
ssize_t attr_slen;
/*
* Backwards compatibility: Allow Foo-Bar,
* e.g. LDAP-Group and SQL-Group.
*/
for (i = 0; i < c->data.map->lhs->len; i++) {
if (!fr_dict_attr_allowed_chars[(uint8_t) c->data.map->lhs->name[i]]) {
may_be_attr = false;
break;
}
if (c->data.map->lhs->name[i] == '-') {
hyphens++;
}
}
if (!hyphens || (hyphens > 3)) may_be_attr = false;
if (may_be_attr) {
attr_slen = tmpl_afrom_attr_str(c->data.map, NULL, &vpt, lhs,
&(vp_tmpl_rules_t){
.allow_unknown = true,
.allow_undefined = true
});
if ((attr_slen > 0) && (vpt->len == c->data.map->lhs->len)) {
talloc_free(c->data.map->lhs);
c->data.map->lhs = vpt;
c->pass2_fixup = PASS2_FIXUP_ATTR;
}
}
}
return 1;
}
/*
* Like tmpl_preparse(), but expands variables.
*/
static ssize_t cond_preparse(TALLOC_CTX *ctx, char const **out, size_t *outlen, char const *in, size_t inlen,
fr_token_t *type, char const **error,
fr_dict_attr_t const **castda, bool require_regex,
CONF_SECTION *cs)
{
ssize_t slen, my_slen;
char *p, *expanded;
char buffer[8192];
/*
* When 'request_regex == false', tmpl_preparse() treats
* '/' as a bare word. This is so that the configuration
* file parser can parse filenames, which may begin with
* '/'. We therefore check for leading '/' here, as
* conditions don't use filenames.
*/
if (!require_regex && (*in == '/')) {
*error = "Unexpected regular expression";
return 0;
}
/*
* Allow dynamic xlat expansion everywhere.
*/
slen = tmpl_preparse(out, outlen, in, inlen, type, error, castda, require_regex, true);
if (slen <= 0) return slen;
p = strchr(in, '$');
if (!p) return slen;
if (!((p[1] == '{') ||
((p[1] == 'E') && (p[2] == 'N') && (p[3] == 'V') &&
(p[4] == '{')))) {
return slen;
}
if (!cf_expand_variables(cf_filename(cs), cf_lineno(cs), cf_item_to_section(cf_parent(cs)),
buffer, sizeof(buffer), in, slen, NULL)) {
*error = "Failed expanding configuration variable";
return -1;
}
/*
* We need to tell the caller how many *input* bytes to
* skip. Which means that we need to keep treat this
* length as different.
*/
my_slen = tmpl_preparse(out, outlen, buffer, strlen(buffer), type, error, castda, require_regex, true);
if (my_slen <= 0) return my_slen;
if (!*out) return 0; /* for sanity checks, *outlen can be 0 for empty strings */
/*
* 'out' now points to 'buffer', which we don't want. So
* we need to return a string which the caller can keep track of.
*/
expanded = talloc_strndup(ctx, *out, *outlen);
if (!expanded) {
*error = "Failed allocating memory";
return -1;
}
*out = expanded;
return slen; /* NOT my_slen */
}
/** Tokenize a conditional check
*
* @param[in] ctx talloc ctx
* @param[in] cs our configuration section
* @param[out] pcond pointer to the returned condition structure
* @param[out] error the parse error (if any)
* @param[in] in the start of the string to process. Should be "(..."
* @param[in] inlen the length of the string to process
* @param[in] brace look for a closing brace (how many deep we are)
* @param[in] rules for attribute parsing
* @return
* - Length of the string skipped.
* - < 0 (the offset to the offending error) on error.
*/
static ssize_t cond_tokenize(TALLOC_CTX *ctx, CONF_SECTION *cs,
fr_cond_t **pcond, char const **error,
char const *in, size_t inlen, int brace,
vp_tmpl_rules_t const *rules)
{
ssize_t slen, tlen;
char const *p = in, *end = in + inlen;
char const *lhs, *rhs;
fr_cond_t *c;
size_t lhs_len, rhs_len;
fr_token_t op, lhs_type, rhs_type;
bool regex = false;
vp_tmpl_rules_t parse_rules;
/*
* We allow unknown and undefined attributes here
*/
parse_rules = *rules;
parse_rules.allow_unknown = true;
parse_rules.allow_undefined = true;
c = talloc_zero(ctx, fr_cond_t);
fr_assert(c != NULL);
lhs_type = rhs_type = T_INVALID;
fr_skip_whitespace(p);
if (!*p) {
return_P("Empty condition is invalid");
}
/*
* !COND
*/
if (*p == '!') {
p++;
c->negate = true;
fr_skip_whitespace(p);
/*
* Just for stupidity
*/
if (*p == '!') {
return_P("Double negation is invalid");
}
}
/*
* (COND)
*/
if (*p == '(') {
p++;
/*
* We've already eaten one layer of
* brackets. Go recurse to get more.
*/
c->type = COND_TYPE_CHILD;
c->ci = cf_section_to_item(cs);
slen = cond_tokenize(c, cs, &c->data.child, error, p, end - p, brace + 1, rules);
if (slen <= 0) return_SLEN;
if (!c->data.child) {
return_P("Empty condition is invalid");
}
p += slen;
fr_skip_whitespace(p);
goto closing_brace;
}
/*
* We didn't see anything special. The condition must be one of
*
* FOO
* FOO OP BAR
*/
/*
* Grab the LHS
*/
slen = cond_preparse(c, &lhs, &lhs_len, p, end - p, &lhs_type, error, &c->cast, false, cs);
if (slen <= 0) {
return_SLEN;
}
/*
* We may (or not) have an operator
*/
/*
* If the LHS is 0xabcdef, then automatically case it to octets.
*/
if (!c->cast && (lhs_type == T_BARE_WORD) &&
(lhs[0] == '0') && (lhs[1] == 'x') &&
((slen & 0x01) == 0)) {
if (slen == 2) {
return_P("Empty octet string is invalid");
}
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()),
FR_CAST_BASE + FR_TYPE_OCTETS);
}
p += slen;
fr_skip_whitespace(p);
/*
* (FOO)
*/
if (*p == ')') {
if (slen == 0) {
return_P("Empty string is invalid");
}
/*
* don't skip the brace. We'll look for it later.
*/
goto exists;
/*
* FOO
*/
} else if (!*p) {
if (brace) {
return_P("No closing brace at end of string");
}
goto exists;
/*
* FOO && ...
*/
} else if (((p[0] == '&') && (p[1] == '&')) ||
((p[0] == '|') && (p[1] == '|'))) {
exists:
if (c->cast) {
*error = "Cannot do cast for existence check";
return_0:
talloc_free(c);
return 0;
}
c->type = COND_TYPE_EXISTS;
c->ci = cf_section_to_item(cs);
tlen = tmpl_afrom_str(c, &c->data.vpt,
lhs, lhs_len, lhs_type, &parse_rules, true);
if (tlen < 0) {
p = lhs - tlen;
return_P(fr_strerror());
}
fr_assert(!tmpl_is_regex_unparsed(c->data.vpt));
if (tmpl_define_unknown_attr(c->data.vpt) < 0) {
p = lhs - tlen;
return_P("Failed defining attribute");
}
if (tmpl_is_attr_unparsed(c->data.vpt)) {
c->pass2_fixup = PASS2_FIXUP_ATTR;
}
} else { /* it's an operator */
vp_map_t *map;
/*
* The next thing should now be a comparison operator.
*/
c->type = COND_TYPE_MAP;
c->ci = cf_section_to_item(cs);
switch (*p) {
default:
return_P("Invalid text. Expected comparison operator");
case '!':
if (p[1] == '=') {
op = T_OP_NE;
p += 2;
#ifdef HAVE_REGEX
} else if (p[1] == '~') {
regex = true;
op = T_OP_REG_NE;
p += 2;
#endif
} else if (p[1] == '*') {
if (lhs_type != T_BARE_WORD) {
return_P("Cannot use !* on a string");
}
op = T_OP_CMP_FALSE;
p += 2;
} else {
goto invalid_operator;
}
break;
case '=':
if (p[1] == '=') {
op = T_OP_CMP_EQ;
p += 2;
#ifdef HAVE_REGEX
} else if (p[1] == '~') {
regex = true;
op = T_OP_REG_EQ;
p += 2;
#endif
} else if (p[1] == '*') {
if (lhs_type != T_BARE_WORD) {
return_P("Cannot use =* on a string");
}
op = T_OP_CMP_TRUE;
p += 2;
} else {
invalid_operator:
return_P("Invalid operator");
}
break;
case '<':
if (p[1] == '=') {
op = T_OP_LE;
p += 2;
} else {
op = T_OP_LT;
p++;
}
break;
case '>':
if (p[1] == '=') {
op = T_OP_GE;
p += 2;
} else {
op = T_OP_GT;
p++;
}
break;
}
fr_skip_whitespace(p);
if (!*p) {
return_P("Expected text after operator");
}
slen = cond_preparse(c, &rhs, &rhs_len, p, end - p, &rhs_type, error, NULL, regex, cs);
if (slen <= 0) {
return_SLEN;
}
/*
* Duplicate map_from_fields here, as we
* want to separate parse errors in the
* LHS from ones in the RHS.
*/
c->data.map = map = talloc_zero(c, vp_map_t);
tlen = tmpl_afrom_str(map, &map->lhs, lhs, lhs_len,
lhs_type, &parse_rules, true);
if (tlen < 0) {
p = lhs - tlen;
return_P(fr_strerror());
}
if (tmpl_define_unknown_attr(map->lhs) < 0) {
*error = "Failed defining attribute";
return_lhs:
talloc_free(c);
return -(lhs - in);
}
map->op = op;
/*
* If the RHS is 0xabcdef... automatically cast it to octets
* unless the LHS is an attribute of type octets, or an
* integer type.
*/
if (!c->cast && (rhs_type == T_BARE_WORD) &&
(rhs[0] == '0') && (rhs[1] == 'x') &&
((slen & 0x01) == 0)) {
if (slen == 2) {
return_P("Empty octet string is invalid");
}
if ((map->lhs->type != TMPL_TYPE_ATTR) ||
!((tmpl_da(map->lhs)->type == FR_TYPE_OCTETS) ||
(tmpl_da(map->lhs)->type == FR_TYPE_UINT8) ||
(tmpl_da(map->lhs)->type == FR_TYPE_UINT16) ||
(tmpl_da(map->lhs)->type == FR_TYPE_UINT32) ||
(tmpl_da(map->lhs)->type == FR_TYPE_UINT64))) {
c->cast = fr_dict_attr_child_by_num(fr_dict_root(fr_dict_internal()),
FR_CAST_BASE + FR_TYPE_OCTETS);
}
}
/*
* This code converts '&Attr-1.2.3.4 == 0xabcdef'
* into "&Known-Attribure == value"
*
* So that the debug output makes more sense, AND
* so that the comparisons are done to known attributes.
*/
if (tmpl_is_attr(map->lhs) &&
tmpl_da(map->lhs)->flags.is_raw &&
map_cast_from_hex(map, rhs_type, rhs)) {
/* do nothing */
} else {
tlen = tmpl_afrom_str(map, &map->rhs, rhs, rhs_len, rhs_type,
&parse_rules, true);
if (tlen < 0) {
p = rhs - tlen;
return_P(fr_strerror());
}
}
if (tmpl_define_unknown_attr(map->rhs) < 0) {
*error = "Failed defining attribute";
return_rhs:
talloc_free(c);